From 3baafbcb3db1c2d7d407f8ef3a84ce7d6a68ae35 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 10 Sep 2026 14:30:09 -0600 Subject: [PATCH] [raster] Clip the edge sweep's column walk to the surface (#6251) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [raster] Clip the edge sweep's column walk to the surface edge_sweep_row() walked every fixed-point pixel column an edge spans, even though columns right of the surface contribute nothing and columns left of it only contribute a cover delta that telescopes into column 0. A near-horizontal edge with extreme coordinates could span ~2^24 columns per row, making sweep cost proportional to coordinate range instead of surface width: the ClusterFuzz testcase took 114s in a plain release build. Fold the left-of-surface portion of the walk into a single column-0 cover update, skip the right-of-surface portion, and enter/exit the per-column loop at the visible window. Jumping the incremental fy accumulator by k*delta_fy is exact, since the increment is constant and the running sums are bounded, so output is bit-identical; verified by comparing 178 hb-raster renders (draw and paint paths, several fonts, sizes, and negative-margin crops that clip edges off-surface) byte-for-byte against the previous code. The testcase now runs in 0.17s. Also add it as a fuzz seed. Fixes https://oss-fuzz.com/testcase-detail/5267980968329216 Assisted-by: Claude Fable 5 * [raster] Keep an unclipped fast path in the edge sweep The window clipping added in the previous commit made every multi-column edge-row pay for the clip setup, measurable as ~1% more instructions rasterizing all glyphs of NotoSansCJKsc/SourceHanSans-VF at 256px with hb-raster-all. Guard it with a two-compare fully-inside-the-surface check and walk such edges with the original unclipped code, which is also the everyday case: auto-sized surfaces always contain their edges. Interleaved perf-stat A/B (5 rounds, all glyphs at 256px): instruction overhead drops to +0.24% (CJK) / +0.9% (AwamiNastaliq, tiny workload); cycles flat to -1.3%, within noise. Output remains bit-identical on the 178-render comparison matrix, and the oss-fuzz 5267980968329216 testcase stays at 0.19s. Assisted-by: Claude Fable 5 * [raster] Charge column spans in the pixel-work estimate hb_raster_draw_get_pixel_work() charged each edge only its scanline span, so a nearly-horizontal edge crossing one pixel row but sweeping the full surface width cost up to `width` cells while being charged 2 units — the paint path's pixel-budget precharge could undercharge the sweep by a factor of the surface width. This is the same blind spot that let the previous commits' column-walk timeout through the paint path's precharge. Charge each edge its column span too, clipped to the surface width the same way the row span is clipped to its height. Since an edge is a straight segment, row span plus clipped column span is a true upper bound on the post-clipping sweep work. Normal outlines are barely affected: flattening yields short segments whose column spans are commensurate with their row spans, far below the 2^26 budget. No rendering change: the 178-render comparison matrix remains bit-identical and the full test suite passes. Assisted-by: Claude Fable 5 --- src/hb-raster-draw.cc | 120 +++++++++++++++++- src/hb-raster-paint.cc | 5 +- src/hb-raster.hh | 9 +- ...inimized-hb-raster-fuzzer-5267980968329216 | Bin 0 -> 1369 bytes 4 files changed, 122 insertions(+), 12 deletions(-) create mode 100644 test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-raster-fuzzer-5267980968329216 diff --git a/src/hb-raster-draw.cc b/src/hb-raster-draw.cc index 760bfb42e..c39583daf 100644 --- a/src/hb-raster-draw.cc +++ b/src/hb-raster-draw.cc @@ -529,12 +529,19 @@ hb_raster_draw_set_clip_box (hb_raster_draw_t *draw, int64_t hb_raster_draw_get_pixel_work (const hb_raster_draw_t *draw, - unsigned int max_rows) + unsigned int max_rows, + unsigned int max_cols) { int64_t work = 0; for (const auto &edge : draw->edges) + { + int64_t dx = (int64_t) edge.xH - edge.xL; + if (dx < 0) dx = -dx; work += 1 + hb_min (((int64_t) edge.yH - edge.yL) >> HB_RASTER_PIXEL_BITS, - (int64_t) max_rows); + (int64_t) max_rows) + + hb_min (dx >> HB_RASTER_PIXEL_BITS, + (int64_t) max_cols); + } return work; } @@ -1319,38 +1326,137 @@ edge_sweep_row (int32_t *area, if (total_dx > 0) { /* Left-to-right edge. */ + if (likely ((unsigned) (cx0 - x_org) < width && (unsigned) (cx1 - x_org) < width)) + { + /* Entirely inside the surface: unclipped walk. */ + int32_t x_b = (int32_t) hb_clamp (((int64_t) cx0 + 1) * HB_RASTER_ONE_PIXEL, + (int64_t) INT32_MIN, (int64_t) INT32_MAX); + int32_t fy_b = fy0 + (int32_t) ((((int64_t) x_b - (int64_t) x0) * total_dy) / total_dx); + cell_add (area, cover, width, cx0 - x_org, fx0, fy0, HB_RASTER_ONE_PIXEL, fy_b, wind, x_min, x_max); + + int32_t fy_prev = fy_b; + for (int32_t cx = cx0 + 1; cx < cx1; cx++) + { + fy_b = fy_prev + delta_fy; + cell_add (area, cover, width, cx - x_org, 0, fy_prev, HB_RASTER_ONE_PIXEL, fy_b, wind, x_min, x_max); + fy_prev = fy_b; + } + + cell_add (area, cover, width, cx1 - x_org, 0, fy_prev, fx1, fy1, wind, x_min, x_max); + return; + } + + /* Visible column window. Columns left of it only contribute their + * total cover to column 0 (see cell_add) and that total telescopes, + * while columns right of it contribute nothing; so neither side is + * walked cell by cell. Skipping ahead in the fy accumulation is + * exact: the increment is constant and the running sums are bounded, + * so k steps equal one k·delta_fy jump. */ + int64_t col_min = (int64_t) x_org; + int64_t col_max = (int64_t) x_org + (int64_t) width - 1; + + if (unlikely (cx1 < col_min)) + { + /* Entirely left of the surface. */ + cell_add (area, cover, width, -1, 0, fy0, 0, fy1, wind, x_min, x_max); + return; + } + if (unlikely (cx0 > col_max)) + return; /* Entirely right of the surface. */ + int32_t x_b = (int32_t) hb_clamp (((int64_t) cx0 + 1) * HB_RASTER_ONE_PIXEL, (int64_t) INT32_MIN, (int64_t) INT32_MAX); int32_t fy_b = fy0 + (int32_t) ((((int64_t) x_b - (int64_t) x0) * total_dy) / total_dx); - cell_add (area, cover, width, cx0 - x_org, fx0, fy0, HB_RASTER_ONE_PIXEL, fy_b, wind, x_min, x_max); + int32_t cx = cx0 + 1; int32_t fy_prev = fy_b; - for (int32_t cx = cx0 + 1; cx < cx1; cx++) + if (likely (cx0 >= col_min)) + cell_add (area, cover, width, cx0 - x_org, fx0, fy0, HB_RASTER_ONE_PIXEL, fy_b, wind, x_min, x_max); + else + { + /* Fold the first cell and the mid columns left of the surface + * into one column-0 cover update. */ + int32_t cx_skip = (int32_t) col_min; /* ≤ cx1, so it fits */ + fy_prev = fy_b + (int32_t) ((int64_t) (cx_skip - cx) * delta_fy); + cell_add (area, cover, width, -1, 0, fy0, 0, fy_prev, wind, x_min, x_max); + cx = cx_skip; + } + + int32_t cx_end = (int32_t) hb_min ((int64_t) cx1, col_max + 1); + for (; cx < cx_end; cx++) { fy_b = fy_prev + delta_fy; cell_add (area, cover, width, cx - x_org, 0, fy_prev, HB_RASTER_ONE_PIXEL, fy_b, wind, x_min, x_max); fy_prev = fy_b; } - cell_add (area, cover, width, cx1 - x_org, 0, fy_prev, fx1, fy1, wind, x_min, x_max); + if (likely (cx1 <= col_max)) + cell_add (area, cover, width, cx1 - x_org, 0, fy_prev, fx1, fy1, wind, x_min, x_max); } else { /* Right-to-left edge. */ + if (likely ((unsigned) (cx0 - x_org) < width && (unsigned) (cx1 - x_org) < width)) + { + /* Entirely inside the surface: unclipped walk. */ + int32_t x_b = (int32_t) hb_clamp ((int64_t) cx0 * HB_RASTER_ONE_PIXEL, + (int64_t) INT32_MIN, (int64_t) INT32_MAX); + int32_t fy_b = fy0 + (int32_t) ((((int64_t) x_b - (int64_t) x0) * total_dy) / total_dx); + cell_add (area, cover, width, cx0 - x_org, fx0, fy0, 0, fy_b, wind, x_min, x_max); + + int32_t fy_prev = fy_b; + for (int32_t cx = cx0 - 1; cx > cx1; cx--) + { + fy_b = fy_prev - delta_fy; + cell_add (area, cover, width, cx - x_org, HB_RASTER_ONE_PIXEL, fy_prev, 0, fy_b, wind, x_min, x_max); + fy_prev = fy_b; + } + + cell_add (area, cover, width, cx1 - x_org, HB_RASTER_ONE_PIXEL, fy_prev, fx1, fy1, wind, x_min, x_max); + return; + } + + int64_t col_min = (int64_t) x_org; + int64_t col_max = (int64_t) x_org + (int64_t) width - 1; + + if (unlikely (cx0 < col_min)) + { + /* Entirely left of the surface. */ + cell_add (area, cover, width, -1, 0, fy0, 0, fy1, wind, x_min, x_max); + return; + } + if (unlikely (cx1 > col_max)) + return; /* Entirely right of the surface. */ + int32_t x_b = (int32_t) hb_clamp ((int64_t) cx0 * HB_RASTER_ONE_PIXEL, (int64_t) INT32_MIN, (int64_t) INT32_MAX); int32_t fy_b = fy0 + (int32_t) ((((int64_t) x_b - (int64_t) x0) * total_dy) / total_dx); cell_add (area, cover, width, cx0 - x_org, fx0, fy0, 0, fy_b, wind, x_min, x_max); + int32_t cx = cx0 - 1; int32_t fy_prev = fy_b; - for (int32_t cx = cx0 - 1; cx > cx1; cx--) + if (unlikely (cx > col_max)) + { + /* Mid columns right of the surface contribute nothing. */ + int32_t cx_skip = (int32_t) col_max; /* ≥ cx1, so it fits */ + fy_prev = fy_b - (int32_t) ((int64_t) (cx - cx_skip) * delta_fy); + cx = cx_skip; + } + + int32_t cx_stop = (int32_t) hb_max ((int64_t) cx1, col_min - 1); + for (; cx > cx_stop; cx--) { fy_b = fy_prev - delta_fy; cell_add (area, cover, width, cx - x_org, HB_RASTER_ONE_PIXEL, fy_prev, 0, fy_b, wind, x_min, x_max); fy_prev = fy_b; } - cell_add (area, cover, width, cx1 - x_org, HB_RASTER_ONE_PIXEL, fy_prev, fx1, fy1, wind, x_min, x_max); + if (likely (cx1 >= col_min)) + cell_add (area, cover, width, cx1 - x_org, HB_RASTER_ONE_PIXEL, fy_prev, fx1, fy1, wind, x_min, x_max); + else + /* Fold the mid columns left of the surface and the last cell + * into one column-0 cover update. */ + cell_add (area, cover, width, -1, 0, fy_prev, 0, fy1, wind, x_min, x_max); } } diff --git a/src/hb-raster-paint.cc b/src/hb-raster-paint.cc index 2bb40067e..4091d2ec2 100644 --- a/src/hb-raster-paint.cc +++ b/src/hb-raster-paint.cc @@ -284,7 +284,7 @@ hb_raster_paint_finalize_path_clip (hb_raster_paint_t *c, hb_raster_image_t *surf, unsigned w, unsigned h) { - if (unlikely (!c->precharge_work (hb_raster_draw_get_pixel_work (rdr, h)))) + if (unlikely (!c->precharge_work (hb_raster_draw_get_pixel_work (rdr, h, w)))) { hb_raster_draw_clear (rdr); hb_raster_paint_push_empty_clip (c, w, h); @@ -955,7 +955,8 @@ hb_raster_paint_fill_glyph (hb_paint_funcs_t *pfuncs HB_UNUSED, hb_raster_paint_readback_budget (c, rdr); if (unlikely (!c->precharge_work (hb_raster_draw_get_pixel_work (rdr, - surf->extents.height)))) + surf->extents.height, + surf->extents.width)))) { hb_raster_draw_clear (rdr); return; diff --git a/src/hb-raster.hh b/src/hb-raster.hh index d648610de..06952cf9a 100644 --- a/src/hb-raster.hh +++ b/src/hb-raster.hh @@ -40,11 +40,14 @@ hb_raster_draw_set_clip_box (hb_raster_draw_t *draw, float x0, float y0, float x1, float y1); -/* Estimated scanline work for the accumulated edges. Raster paint uses - * this to precharge its separate pixel budget before rendering a mask. */ +/* Estimated sweep work for the accumulated edges: each edge is charged + * its scanline span plus its column span, both clipped to the surface. + * Raster paint uses this to precharge its separate pixel budget before + * rendering a mask. */ HB_INTERNAL int64_t hb_raster_draw_get_pixel_work (const hb_raster_draw_t *draw, - unsigned int max_rows); + unsigned int max_rows, + unsigned int max_cols); /* Shared pixel helpers (used by paint and image compositing). */ diff --git a/test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-raster-fuzzer-5267980968329216 b/test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-raster-fuzzer-5267980968329216 new file mode 100644 index 0000000000000000000000000000000000000000..4b27eff34bb80f3d40f03b141a9d0eac1629d096 GIT binary patch literal 1369 zcmd5*T}YEr7=F(=`*FI~l&`c{GTSJTptZS~7sclcu@C~0Afcb-Z{SkY!h&dXyU2^W zDheusE;6uE?^2|TGzc>xkXT`M6Xb>2ouKgT>p9;RSZLk$9?m)M&-4DA_x(O%L}aHJ z2~tl~)mJ-9Zw83OS4OO&DH`b_`)mUdYX+*k^-`qU;jLGR%r3|yZ5@|eZ;gH*f_((? z^)}e!UhgT`XNXM8?Jbe!bGNHUVS`Meb|@?lMFsFC$lms7Z(r^eaKvhH?Cj`lilCAy z$oC-^MI(J(FNZd@Z;LnfJzNWm#{Dr6!zZW9*dr%cyzlCC$P@z8@(@vxRBnPRL1*>!}N zzN&u~ReYSb5*3ELp~>m#g>5B!g~ejBnkQNCW4BhoU+q5qGW})BQ`kNH?S{X`-t+oi zhl@BL)Sru_@SqASo*f8!L?GY8-;RxW>*`o#O?tn$i2RMnP|L?1z?k!cCFfh3BO#x! zfWO(};Y0o!r*=}-rO-;YR%XmIQ#b$hCZnwu%E@B#JJfEm?t)2q(Y5G693~$BQD{CDMHJ<&~hvr zw(*fs&VgZ(3d^xsQD0_YnmoL)bnamCFk& zC?Wsw&rYBS$TDpC?@nO9haPPux+c&_m+(eA6%=9C-KzFTMcv#?P`A@!(b>lSH4QBci`fEI2 literal 0 HcmV?d00001