Merge pull request #6515 from harfbuzz/scanner/gpu

[gpu] Fix clip-pop imbalance and bound cu2qu / per-layer shader work
This commit is contained in:
Behdad Esfahbod authored and GitHub committed 2026-10-30 23:10:13 -06:00
commit 07bc8effbb
4 files changed
+170 -2

No files matched your search

+111
View File
@@ -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);