diff --git a/src/hb-limits.hh b/src/hb-limits.hh index 3ff0f5ac3..d28a67746 100644 --- a/src/hb-limits.hh +++ b/src/hb-limits.hh @@ -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 diff --git a/src/hb-raster-draw.cc b/src/hb-raster-draw.cc index 637a7bcfb..9e885d1d8 100644 --- a/src/hb-raster-draw.cc +++ b/src/hb-raster-draw.cc @@ -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 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); diff --git a/src/hb-raster-paint.cc b/src/hb-raster-paint.cc index ded1bed0d..1cb710d46 100644 --- a/src/hb-raster-paint.cc +++ b/src/hb-raster-paint.cc @@ -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 diff --git a/src/hb-raster.hh b/src/hb-raster.hh index 0ac31e708..68caf3890 100644 --- a/src/hb-raster.hh +++ b/src/hb-raster.hh @@ -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