[raster] Charge Bézier flattening to a work budget as it happens

The paint-session budget charged outlines only after a whole glyph
had been emitted, by counting accumulated edges.  That accounting
misses two things: flattening work that emits no edges at all
(degenerate curves whose subdivided segments collapse or clamp away),
and the single-glyph overshoot before the post-hoc charge lands.

Charge one budget unit per Bézier subdivision inside the flatteners
instead, degrading to the chord once the budget is spent.  When
driven by raster-paint, the charge goes to the paint session budget
(threaded in via hb_raster_draw_set_external_work), so per-glyph
work cannot multiply with the paint traversal.  Standalone
hb-raster-draw sessions get their own HB_RASTER_MAX_DRAW_WORK
budget, re-armed by hb_raster_draw_clear().

This is belt-and-suspenders on top of the previous commit: the
visibility clip already collapses the degenerate out-of-box curves,
and the budget bounds whatever in-box work remains.

Full test suite passes; rendering all glyphs of Bungee, Bitcount
(COLR), Amiri, and AktivGrotesk VF at size 256 stays byte-identical.

https://oss-fuzz.com/testcase-detail/4912367159541760

Assisted-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Behdad Esfahbod committed 2026-08-17 14:37:46 -06:00
1 parent a948dfd56a
commit 45aeb16b95
4 files changed
+59 -3

No files matched your search

+8
View File
@@ -196,6 +196,14 @@
#define HB_RASTER_MAX_PAINT_WORK_PASSES 4
#endif
/* One raster draw session (everything drawn between two render/clear
* calls) through the standalone hb-raster-draw API, in Bézier
* subdivision steps. When driven by raster-paint, the paint session
* budget above is charged instead. */
#ifndef HB_RASTER_MAX_DRAW_WORK
#define HB_RASTER_MAX_DRAW_WORK ((int64_t) 1 << 24)
#endif
#ifndef HB_REPACKER_MAX_ITERATIONS
#define HB_REPACKER_MAX_ITERATIONS 500
+37
View File
@@ -77,6 +77,12 @@ struct hb_raster_draw_t
bool has_clip_box = false;
float clip_x0 = 0.f, clip_y0 = 0.f, clip_x1 = 0.f, clip_y1 = 0.f;
/* Curve-flattening work, charged one unit per Bézier subdivision.
When external_work is set (by raster-paint), that session budget
is charged instead of the standalone per-session one. */
int64_t flatten_work_left = HB_RASTER_MAX_DRAW_WORK;
int64_t *external_work = nullptr;
/* Accumulated geometry */
hb_vector_t<hb_raster_edge_t> edges;
@@ -429,6 +435,8 @@ hb_raster_draw_clear (hb_raster_draw_t *draw)
draw->fixed_extents = {};
draw->has_extents = false;
draw->has_clip_box = false;
draw->flatten_work_left = HB_RASTER_MAX_DRAW_WORK;
draw->external_work = nullptr;
draw->edges.clear ();
draw->active_edges.clear ();
}
@@ -464,6 +472,13 @@ hb_raster_draw_set_clip_box (hb_raster_draw_t *draw,
draw->clip_y1 = y1;
}
void
hb_raster_draw_set_external_work (hb_raster_draw_t *draw,
int64_t *work_left)
{
draw->external_work = work_left;
}
int64_t
hb_raster_draw_get_edge_work (hb_raster_draw_t *draw, unsigned max_rows)
{
@@ -588,6 +603,7 @@ flatten_quadratic_recursive (hb_raster_draw_t *draw,
unsigned top = 0;
hb_raster_flatten_clip_t clip = resolve_flatten_clip (draw);
int64_t *work = draw->external_work ? draw->external_work : &draw->flatten_work_left;
while (true)
{
@@ -632,6 +648,16 @@ flatten_quadratic_recursive (hb_raster_draw_t *draw,
emit_chord = is_flat;
}
/* Charge one unit per subdivision; degrade to the chord when the
session work budget is spent. */
if (!emit_chord)
{
if (unlikely (*work <= 0))
emit_chord = true;
else
(*work)--;
}
if (emit_chord)
{
emit_segment (draw, x0, y0, x2, y2);
@@ -773,6 +799,7 @@ flatten_cubic_recursive (hb_raster_draw_t *draw,
unsigned top = 0;
hb_raster_flatten_clip_t clip = resolve_flatten_clip (draw);
int64_t *work = draw->external_work ? draw->external_work : &draw->flatten_work_left;
while (true)
{
@@ -822,6 +849,16 @@ flatten_cubic_recursive (hb_raster_draw_t *draw,
emit_chord = is_flat;
}
/* Charge one unit per subdivision; degrade to the chord when the
session work budget is spent. */
if (!emit_chord)
{
if (unlikely (*work <= 0))
emit_chord = true;
else
(*work)--;
}
if (emit_chord)
{
emit_segment (draw, x0, y0, x3, y3);
+6 -3
View File
@@ -204,10 +204,12 @@ hb_raster_paint_color_glyph (hb_paint_funcs_t *pfuncs HB_UNUSED,
typedef void (*hb_raster_paint_clip_mask_emit_t) (hb_raster_draw_t *rdr, void *user_data);
/* Attach the surface box to @rdr before outlines are drawn into it,
* so curves outside the surface collapse to their chord. */
/* Attach the surface box and the session work budget to @rdr before
* outlines are drawn into it: curves outside the surface collapse to
* their chord, and flattening work is charged as it happens instead
* of only after the whole outline has been emitted. */
static void
hb_raster_paint_attach_clip_rdr (hb_raster_paint_t *c HB_UNUSED,
hb_raster_paint_attach_clip_rdr (hb_raster_paint_t *c,
hb_raster_draw_t *rdr,
const hb_raster_image_t *surf)
{
@@ -216,6 +218,7 @@ hb_raster_paint_attach_clip_rdr (hb_raster_paint_t *c HB_UNUSED,
(float) surf->extents.y_origin,
(float) surf->extents.x_origin + (float) surf->extents.width,
(float) surf->extents.y_origin + (float) surf->extents.height);
hb_raster_draw_set_external_work (rdr, &c->work_left);
}
static void
+8
View File
@@ -49,6 +49,14 @@ hb_raster_draw_set_clip_box (hb_raster_draw_t *draw,
float x0, float y0,
float x1, float y1);
/* Points curve-flattening work charges at the caller's session budget
* (one unit per Bézier subdivision) instead of the standalone
* per-session HB_RASTER_MAX_DRAW_WORK budget. Cleared by
* hb_raster_draw_clear() (and hence after every render). */
HB_INTERNAL void
hb_raster_draw_set_external_work (hb_raster_draw_t *draw,
int64_t *work_left);
/* Shared pixel helpers (used by paint and image compositing). */
static HB_ALWAYS_INLINE uint8_t