[raster] Clip the edge sweep's column walk to the surface (#6251)

* [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 <noreply@anthropic.com>

* [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 <noreply@anthropic.com>

* [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 <noreply@anthropic.com>
This commit is contained in:
Behdad Esfahbod authored and GitHub committed 2026-09-10 14:30:09 -06:00
1 parent 00f966c719
commit 3baafbcb3d
4 files changed
+122 -12

No files matched your search

+113 -7
View File
@@ -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);
}
}
+3 -2
View File
@@ -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;
+6 -3
View File
@@ -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). */