diff --git a/src/hb-gpu-cu2qu.hh b/src/hb-gpu-cu2qu.hh index c98b140a1..7635938fe 100644 --- a/src/hb-gpu-cu2qu.hh +++ b/src/hb-gpu-cu2qu.hh @@ -170,6 +170,13 @@ hb_gpu_cubic_to_quadratics (hb_gpu_draw_t *g, hb_gpu_cu2qu_point_t c3, double tolerance, unsigned depth) { + /* Charge one unit per node for its fit test and split, and stop once + * the accumulator has failed or the work budget is spent: whatever we + * would emit from here is dropped by acc_emit() anyway. */ + if (unlikely (!g->success || + !hb_budget_spend (*g->get_budget_remaining (), HB_BUDGET_1))) + return; + hb_gpu_cu2qu_point_t q1; if (hb_gpu_cubic_approx_quadratic (c0, c1, c2, c3, tolerance, &q1)) { diff --git a/src/hb-gpu-paint.cc b/src/hb-gpu-paint.cc index 0b9503a07..0775d0a67 100644 --- a/src/hb-gpu-paint.cc +++ b/src/hb-gpu-paint.cc @@ -104,9 +104,35 @@ hb_gpu_paint_pop_clip (hb_paint_funcs_t *funcs HB_UNUSED, void *user_data HB_UNUSED) { hb_gpu_paint_t *c = (hb_gpu_paint_t *) paint_data; + + /* A rectangle clip pushed at this depth is popped before the + * glyph/path clip below it; its pop leaves clip_depth alone. */ + unsigned &rects = c->rect_clips[hb_min (c->clip_depth, (unsigned) HB_GPU_PAINT_MAX_CLIP_DEPTH)]; + if (rects) + { + rects--; + return; + } + if (likely (c->clip_depth > 0)) c->clip_depth--; } +/* Clip rectangles are not encoded in v1 (see the design notes in + * hb-gpu-paint.hh). Only count the push so that the matching + * pop_clip does not pop the glyph/path clip below it. */ +static void +hb_gpu_paint_push_clip_rectangle (hb_paint_funcs_t *funcs HB_UNUSED, + void *paint_data, + float xmin HB_UNUSED, + float ymin HB_UNUSED, + float xmax HB_UNUSED, + float ymax HB_UNUSED, + void *user_data HB_UNUSED) +{ + hb_gpu_paint_t *c = (hb_gpu_paint_t *) paint_data; + c->rect_clips[hb_min (c->clip_depth, (unsigned) HB_GPU_PAINT_MAX_CLIP_DEPTH)]++; +} + /* Arbitrary-path clip: hand the caller the same hb_gpu_draw * funcs the encoder uses for glyph outlines, with scratch_draw * as the accumulator. push_clip_path_end encodes the captured @@ -585,7 +611,8 @@ emit_clip_sub_blob (hb_gpu_paint_t *c, /* Emit a sub-blob for every clip currently on the stack and return * their indices in @out (set to -1 for unused slots). Returns * false if any clip's outline is empty (caller should skip the - * layer entirely) or hb_gpu_draw_encode failed. */ + * layer entirely), hb_gpu_draw_encode failed, or the session + * budget is exhausted. */ static bool emit_all_clip_sub_blobs (hb_gpu_paint_t *c, int out[HB_GPU_PAINT_MAX_CLIP_DEPTH]) { @@ -596,6 +623,18 @@ emit_all_clip_sub_blobs (hb_gpu_paint_t *c, int out[HB_GPU_PAINT_MAX_CLIP_DEPTH] out[i] = emit_clip_sub_blob (c, c->clip_stack[i]); if (out[i] < 0) return false; + + /* The memoized clip outline is encoded once however many layers + * share it, but the fragment shader walks its band tables again + * for every layer painted under it. Charge each reference to the + * session budget, in RGBA16I texels, so the layer count cannot + * multiply with the clip size past it. */ + unsigned texels = hb_blob_get_length (c->sub_blobs.arrayZ[out[i]]) / 8; + if (unlikely (!hb_budget_spend (c->budget_remaining, HB_BUDGET_1, texels))) + { + c->unsupported = true; + return false; + } } return true; } @@ -1084,6 +1123,7 @@ static struct hb_gpu_paint_funcs_lazy_loader_t hb_paint_funcs_set_push_transform_func (funcs, hb_gpu_paint_push_transform, nullptr, nullptr); hb_paint_funcs_set_pop_transform_func (funcs, hb_gpu_paint_pop_transform, nullptr, nullptr); hb_paint_funcs_set_push_clip_glyph_func (funcs, hb_gpu_paint_push_clip_glyph, nullptr, nullptr); + hb_paint_funcs_set_push_clip_rectangle_func (funcs, hb_gpu_paint_push_clip_rectangle, nullptr, nullptr); hb_paint_funcs_set_push_clip_path_start_func (funcs, hb_gpu_paint_push_clip_path_start, nullptr, nullptr); hb_paint_funcs_set_push_clip_path_end_func (funcs, hb_gpu_paint_push_clip_path_end, nullptr, nullptr); hb_paint_funcs_set_pop_clip_func (funcs, hb_gpu_paint_pop_clip, nullptr, nullptr); @@ -1646,6 +1686,7 @@ hb_gpu_paint_clear (hb_gpu_paint_t *paint) paint->sub_blobs.reset (); paint->sub_bytes = 0; paint->clip_depth = 0; + hb_memset (paint->rect_clips, 0, sizeof (paint->rect_clips)); paint->pending_clip_path = false; paint->unsupported = false; paint->recharge_budget (); diff --git a/src/hb-gpu-paint.hh b/src/hb-gpu-paint.hh index 1a8803d25..f66df0d84 100644 --- a/src/hb-gpu-paint.hh +++ b/src/hb-gpu-paint.hh @@ -287,7 +287,8 @@ struct hb_gpu_paint_t /* Work budget for the current paint session; reset by * hb_gpu_paint_clear(). All the session's work is outline-derived - * (outline traversal and the curves generated from it -- the GPU + * (outline traversal, the curves generated from it, and the clip + * outline texels the fragment shader walks per layer -- the GPU * backend never rasterizes), so a single counter suffices. It is the * public draw/paint budget: seeded into the scratch encoder through the * public draw-budget API before each glyph and read back afterwards. */ @@ -325,6 +326,14 @@ struct hb_gpu_paint_t pending_clip_t clip_stack[3]; unsigned clip_depth = 0; + /* Rectangle clips are not encoded (see "Out of scope for v1"), + * but their pop_clip must not pop the glyph/path clip below them. + * rect_clips[d] counts the rectangle clips pushed while clip_depth + * was d; the paint walk pops them before anything at depth d, so + * a non-zero count at the current depth identifies a rectangle + * pop. The last slot collects everything pushed past the cap. */ + unsigned rect_clips[3 + 1] = {}; + /* Carries the cur_transform captured at push_clip_path_start * through to push_clip_path_end, where the encoded clip is * committed to clip_stack. */ diff --git a/test/api/test-gpu.cc b/test/api/test-gpu.cc index 37cd582ce..ff989eb87 100644 --- a/test/api/test-gpu.cc +++ b/test/api/test-gpu.cc @@ -250,6 +250,24 @@ test_budget (void) hb_face_destroy (face); } +static void +test_budget_cubic (void) +{ + hb_gpu_draw_t *draw = hb_gpu_draw_create_or_fail (); + hb_draw_funcs_t *draw_funcs = hb_gpu_draw_get_funcs (draw); + hb_draw_state_t draw_state = HB_DRAW_STATE_DEFAULT; + + /* A cubic that fits no quadratic at any depth must not be subdivided + * once the budget is spent; the subdivision itself is charged. */ + g_assert_true (hb_draw_set_budget (draw_funcs, draw, 0)); + hb_draw_move_to (draw_funcs, draw, &draw_state, 0.f, 0.f); + hb_draw_cubic_to (draw_funcs, draw, &draw_state, + 1e9f, 3e8f, 1e9f, -7e8f, 3.f, 3e9f); + g_assert_cmpint (hb_draw_get_budget_remaining (draw_funcs, draw), ==, -1); + + hb_gpu_draw_destroy (draw); +} + static void test_draw_funcs (void) { @@ -741,6 +759,96 @@ test_paint_clip_path_depth_overflow (void) hb_gpu_paint_destroy (p); } +static void +test_paint_clip_rectangle_pop_balance (void) +{ + /* Rectangle clips are not encoded, but their pop_clip must not + * pop the glyph clip below them. COLRv1 emits such a pair around + * every PaintColrGlyph that has a ClipBox; when that sits inside a + * PaintGlyph, an unbalanced pop un-clipped (or dropped) the sibling + * layers that followed. */ + hb_face_t *face = hb_test_open_font_file (FONT_FILE); + g_assert_nonnull (face); + hb_font_t *font = hb_font_create (face); + + hb_gpu_paint_t *p = hb_gpu_paint_create_or_fail (); + g_assert_nonnull (p); + hb_gpu_paint_set_scale (p, 1000, 1000); + + hb_paint_funcs_t *pf = hb_gpu_paint_get_funcs (p); + + hb_codepoint_t gid; + g_assert_true (hb_font_get_nominal_glyph (font, 'a', &gid)); + + hb_paint_push_clip_glyph (pf, p, gid, font); + hb_paint_push_clip_rectangle (pf, p, 0.f, 0.f, 100.f, 100.f); + hb_paint_pop_clip (pf, p); + + /* Still inside the glyph clip: this layer must be emitted. */ + hb_paint_color (pf, p, false, HB_COLOR (0, 0, 255, 255)); + + hb_paint_pop_clip (pf, p); + + /* Outside every clip: this layer must be dropped. */ + hb_paint_color (pf, p, false, HB_COLOR (255, 0, 0, 255)); + + hb_blob_t *blob = hb_gpu_paint_encode (p, nullptr); + g_assert_nonnull (blob); + unsigned texel_count; + const hb_gpu_test_texel_t *texels = blob_as_texels (blob, &texel_count); + g_assert_cmpuint (texel_count, >, 0); + /* Header texel 0 .r holds num_ops. */ + g_assert_cmpint (texels[0].r, ==, 1); + hb_blob_destroy (blob); + + hb_gpu_paint_destroy (p); + hb_font_destroy (font); + hb_face_destroy (face); +} + +static void +test_paint_layers_under_shared_clip (void) +{ + /* Layers painted under an already-encoded clip cost the encoder a + * cached index and 24 bytes of op stream each, but the fragment + * shader walks every clip outline again for every layer in every + * fragment. A COLRv1 font can paint tens of thousands of layers + * under a few nested PaintGlyph clips; the encoder must bound the + * product of layers and clip size, not just each factor. */ + hb_face_t *face = hb_test_open_font_file (FONT_FILE); + hb_font_t *font = hb_font_create (face); + hb_codepoint_t gid; + g_assert_true (hb_font_get_nominal_glyph (font, 'a', &gid)); + + hb_gpu_paint_t *paint = hb_gpu_paint_create_or_fail (); + hb_paint_funcs_t *paint_funcs = hb_gpu_paint_get_funcs (paint); + + /* A modest number of layers under three nested clips encodes. */ + for (unsigned i = 0; i < 3; i++) + hb_paint_push_clip_glyph (paint_funcs, paint, gid, font); + for (unsigned i = 0; i < 100; i++) + hb_paint_color (paint_funcs, paint, false, HB_COLOR (0, 0, 0, 255)); + for (unsigned i = 0; i < 3; i++) + hb_paint_pop_clip (paint_funcs, paint); + hb_blob_t *blob = hb_gpu_paint_encode (paint, nullptr); + g_assert_nonnull (blob); + g_assert_cmpuint (hb_blob_get_length (blob), >, 0); + hb_blob_destroy (blob); + + /* As many layers as the op count allows does not. */ + for (unsigned i = 0; i < 3; i++) + hb_paint_push_clip_glyph (paint_funcs, paint, gid, font); + for (unsigned i = 0; i < 0x7fff; i++) + hb_paint_color (paint_funcs, paint, false, HB_COLOR (0, 0, 0, 255)); + for (unsigned i = 0; i < 3; i++) + hb_paint_pop_clip (paint_funcs, paint); + g_assert_null (hb_gpu_paint_encode (paint, nullptr)); + + hb_gpu_paint_destroy (paint); + hb_font_destroy (font); + hb_face_destroy (face); +} + static unsigned repro_color_stops (hb_color_line_t *cl HB_UNUSED, void *cl_data HB_UNUSED, unsigned start, unsigned *count, @@ -1014,6 +1122,7 @@ main (int argc, char **argv) hb_test_add (test_encode_glyph); hb_test_add (test_reset); hb_test_add (test_budget); + hb_test_add (test_budget_cubic); hb_test_add (test_draw_funcs); hb_test_add (test_shader_sources); hb_test_add (test_encode_quantizes_extents_outward); @@ -1033,6 +1142,8 @@ main (int argc, char **argv) hb_test_add (test_paint_reset); hb_test_add (test_paint_recycle_blob); hb_test_add (test_paint_clip_path_depth_overflow); + hb_test_add (test_paint_clip_rectangle_pop_balance); + hb_test_add (test_paint_layers_under_shared_clip); hb_test_add (test_paint_gradient_overflow_transform); hb_test_add (test_paint_empty_clip);