[raster] Crop paint masks to their visible destination

A paint mask can extend far beyond its destination. Auto-sizing the
mask before imposing the raster dimension cap wastes pixel work and
can discard the visible part of an oversized outline altogether.

Intersect auto-sized masks with the destination clip box before the
dimension cap, and use those clipped dimensions for work accounting.
Widen the extent subtraction to cover disjoint bounds safely.

Add a regression for an oversized outline enclosing a small surface,
and update the sweep test to expect successful one-pixel clipping.
The standalone tall-edge sweep still verifies budget exhaustion.

Tested with the full configured Meson suite and the Bitcount Ink audit.
The worst measured 256 px pixel work falls from 6.84B to 152M units.

Assisted-by: Codex (OpenAI)
This commit is contained in:
Behdad Esfahbod committed 2026-11-12 18:11:03 -06:00
1 parent f6b7a4cb72
commit 3bb72a7b4c
3 files changed
+49 -3

No files matched your search

+18 -2
View File
@@ -540,6 +540,11 @@ hb_raster_draw_get_pixel_work (const hb_raster_draw_t *draw)
max_rows = draw->fixed_extents.height;
max_cols = draw->fixed_extents.width;
}
else if (draw->has_clip_box)
{
max_rows = hb_max ((int64_t) 0, hb_clamp_to<int64_t> (ceil ((double) draw->clip_y1) - floor ((double) draw->clip_y0)));
max_cols = hb_max ((int64_t) 0, hb_clamp_to<int64_t> (ceil ((double) draw->clip_x1) - floor ((double) draw->clip_x0)));
}
int64_t work = 0;
for (const auto &edge : draw->edges)
@@ -1666,10 +1671,21 @@ hb_raster_draw_render (hb_raster_draw_t *draw)
int x1 = (int) (((int64_t) xmax + HB_RASTER_PIXEL_MASK) >> HB_RASTER_PIXEL_BITS);
int y1 = (int) (((int64_t) ymax + HB_RASTER_PIXEL_MASK) >> HB_RASTER_PIXEL_BITS);
/* Paint masks only need the part visible on their destination.
* Clip before the dimension cap, so a large off-surface outline
* neither allocates a huge mask nor loses its visible portion. */
if (draw->has_clip_box)
{
x0 = hb_max (x0, hb_clamp_to<int> (floorf (draw->clip_x0)));
y0 = hb_max (y0, hb_clamp_to<int> (floorf (draw->clip_y0)));
x1 = hb_min (x1, hb_clamp_to<int> (ceilf (draw->clip_x1)));
y1 = hb_min (y1, hb_clamp_to<int> (ceilf (draw->clip_y1)));
}
ext.x_origin = x0;
ext.y_origin = y0;
ext.width = (unsigned) hb_min (hb_max (0, x1 - x0), HB_RASTER_MAX_AUTO_DIMENSION);
ext.height = (unsigned) hb_min (hb_max (0, y1 - y0), HB_RASTER_MAX_AUTO_DIMENSION);
ext.width = (unsigned) hb_min (hb_max ((int64_t) 0, (int64_t) x1 - x0), (int64_t) HB_RASTER_MAX_AUTO_DIMENSION);
ext.height = (unsigned) hb_min (hb_max ((int64_t) 0, (int64_t) y1 - y0), (int64_t) HB_RASTER_MAX_AUTO_DIMENSION);
ext.stride = 0; /* filled below */
}
}
+29
View File
@@ -56,6 +56,35 @@ main ()
hb_always_assert (hb_paint_set_budget (funcs, paint, HB_BUDGET_DEFAULT));
hb_always_assert (paint->pixel_remaining == initial_budget);
/* An outline can surround the small visible surface while extending far
* past the automatic dimension cap. Rasterize its visible part: a mask
* starting at -10000 and truncated to 4096 pixels misses the surface. */
hb_raster_paint_clear (paint);
hb_raster_paint_set_extents (paint, &root);
void *draw_data = nullptr;
hb_draw_funcs_t *draw_funcs = hb_paint_push_clip_path_start (funcs, paint, &draw_data);
hb_always_assert (draw_funcs);
hb_draw_state_t state = HB_DRAW_STATE_DEFAULT;
hb_draw_move_to (draw_funcs, draw_data, &state, -10000.f, -10000.f);
hb_draw_line_to (draw_funcs, draw_data, &state, 10000.f, -10000.f);
hb_draw_line_to (draw_funcs, draw_data, &state, 10000.f, 10000.f);
hb_draw_line_to (draw_funcs, draw_data, &state, -10000.f, 10000.f);
hb_draw_close_path (draw_funcs, draw_data, &state);
hb_paint_push_clip_path_end (funcs, paint);
hb_paint_color (funcs, paint, false, HB_COLOR (255, 255, 255, 255));
hb_paint_pop_clip (funcs, paint);
hb_raster_image_t *image = hb_raster_paint_render (paint);
hb_always_assert (image);
hb_raster_extents_t extents;
hb_raster_image_get_extents (image, &extents);
hb_always_assert (extents.width == root.width && extents.height == root.height);
const uint8_t *pixels = hb_raster_image_get_buffer (image);
for (unsigned y = 0; y < root.height; y++)
for (unsigned x = 0; x < root.width * 4; x++)
hb_always_assert (pixels[y * extents.stride + x] == 255);
hb_always_assert (paint->pixel_remaining >= 0);
hb_raster_image_destroy (image);
hb_raster_paint_destroy (paint);
return 0;
}
+2 -1
View File
@@ -804,7 +804,8 @@ test_budget_sweep (void)
hb_paint_pop_clip (funcs, paint);
img = hb_raster_paint_render (paint);
g_assert_nonnull (img);
g_assert_cmpuint (hb_raster_image_get_buffer (img)[3], ==, 0);
/* Paint masks crop the same edges to the visible one-pixel surface. */
g_assert_cmpuint (hb_raster_image_get_buffer (img)[3], ==, 255);
hb_raster_image_destroy (img);
hb_raster_paint_destroy (paint);
}