[raster/vector/gpu] Streamline color-stop fetching

This commit is contained in:
Behdad Esfahbod authored and Khaled Hosny committed 2026-04-20 01:29:49 +02:00
1 parent 74514fc7d6
commit 0b718a9f09
7 files changed
+71 -114

No files matched your search

+12 -34
View File
@@ -622,26 +622,16 @@ hb_gpu_paint_emit_linear (hb_gpu_paint_t *c,
}
/* Resolve stops (triggers custom_palette_color, etc). */
unsigned count = hb_color_line_get_color_stops (color_line, 0, nullptr, nullptr);
if (unlikely (!count))
if (unlikely (!c->fetch_color_stops (color_line)))
return;
hb_color_stop_t stack_stops[16];
hb_color_stop_t *stops = stack_stops;
hb_color_stop_t *heap_stops = nullptr;
if (count > 16)
{
heap_stops = (hb_color_stop_t *) hb_malloc (count * sizeof (hb_color_stop_t));
if (unlikely (!heap_stops)) { c->unsupported = true; return; }
stops = heap_stops;
}
unsigned got = count;
hb_color_line_get_color_stops (color_line, 0, &got, stops);
hb_color_stop_t *stops = c->color_stops_scratch.arrayZ;
unsigned got = c->color_stops_scratch.length;
hb_paint_extend_t extend = hb_color_line_get_extend (color_line);
/* Emit clip sub-blob(s) first. */
int clip_idx[HB_GPU_PAINT_MAX_CLIP_DEPTH];
if (!emit_all_clip_sub_blobs (c, clip_idx)) { hb_free (heap_stops); return; }
if (!emit_all_clip_sub_blobs (c, clip_idx)) return;
/* Build gradient params sub-blob.
* texel 0: (p0_rendered_x, p0_rendered_y, d_canonical_x, d_canonical_y)
@@ -654,7 +644,7 @@ hb_gpu_paint_emit_linear (hb_gpu_paint_t *c,
* handled exactly rather than approximated. */
hb_vector_t<int16_t> grad_data;
if (unlikely (!grad_data.resize (8)))
{ c->unsupported = true; hb_free (heap_stops); return; }
{ c->unsupported = true; return; }
float lx0, ly0, lx1, ly1;
hb_paint_reduce_linear_anchors (x0, y0, x1, y1, x2, y2, &lx0, &ly0, &lx1, &ly1);
@@ -679,8 +669,7 @@ hb_gpu_paint_emit_linear (hb_gpu_paint_t *c,
grad_data[7] = Linv[3];
if (unlikely (!append_color_stops (grad_data, stops, got)))
{ c->unsupported = true; hb_free (heap_stops); return; }
hb_free (heap_stops);
{ c->unsupported = true; return; }
unsigned grad_bytes = grad_data.length * sizeof (int16_t);
hb_blob_t *grad_blob = hb_blob_create ((const char *) grad_data.arrayZ,
@@ -717,25 +706,15 @@ hb_gpu_paint_emit_radial (hb_gpu_paint_t *c,
return;
}
unsigned count = hb_color_line_get_color_stops (color_line, 0, nullptr, nullptr);
if (unlikely (!count))
if (unlikely (!c->fetch_color_stops (color_line)))
return;
hb_color_stop_t stack_stops[16];
hb_color_stop_t *stops = stack_stops;
hb_color_stop_t *heap_stops = nullptr;
if (count > 16)
{
heap_stops = (hb_color_stop_t *) hb_malloc (count * sizeof (hb_color_stop_t));
if (unlikely (!heap_stops)) { c->unsupported = true; return; }
stops = heap_stops;
}
unsigned got = count;
hb_color_line_get_color_stops (color_line, 0, &got, stops);
hb_color_stop_t *stops = c->color_stops_scratch.arrayZ;
unsigned got = c->color_stops_scratch.length;
hb_paint_extend_t extend = hb_color_line_get_extend (color_line);
int clip_idx[HB_GPU_PAINT_MAX_CLIP_DEPTH];
if (!emit_all_clip_sub_blobs (c, clip_idx)) { hb_free (heap_stops); return; }
if (!emit_all_clip_sub_blobs (c, clip_idx)) return;
/* Build gradient params sub-blob.
* texel 0: (c0_rendered_x, c0_rendered_y, d_canonical_x, d_canonical_y)
@@ -751,7 +730,7 @@ hb_gpu_paint_emit_radial (hb_gpu_paint_t *c,
*/
hb_vector_t<int16_t> grad_data;
if (unlikely (!grad_data.resize (12)))
{ c->unsupported = true; hb_free (heap_stops); return; }
{ c->unsupported = true; return; }
float mn, mx;
hb_paint_normalize_color_line (stops, got, &mn, &mx);
float dx = x1 - x0, dy = y1 - y0, dr = r1 - r0;
@@ -776,8 +755,7 @@ hb_gpu_paint_emit_radial (hb_gpu_paint_t *c,
grad_data[11] = Linv[3];
if (unlikely (!append_color_stops (grad_data, stops, got)))
{ c->unsupported = true; hb_free (heap_stops); return; }
hb_free (heap_stops);
{ c->unsupported = true; return; }
unsigned grad_bytes = grad_data.length * sizeof (int16_t);
hb_blob_t *grad_blob = hb_blob_create ((const char *) grad_data.arrayZ,
+14
View File
@@ -316,9 +316,23 @@ struct hb_gpu_paint_t
/* Scratch: used to rasterize each clip-glyph outline. */
hb_gpu_draw_t *scratch_draw = nullptr;
/* Scratch: color stops fetched per gradient callback. */
hb_vector_t<hb_color_stop_t> color_stops_scratch;
/* Recycled output blob. */
hb_blob_t *recycled_blob = nullptr;
bool fetch_color_stops (hb_color_line_t *color_line)
{
unsigned count = hb_color_line_get_color_stops (color_line, 0, nullptr, nullptr);
if (unlikely (!count || !color_stops_scratch.resize (count)))
{
color_stops_scratch.resize (0);
return false;
}
hb_color_line_get_color_stops (color_line, 0, &count, color_stops_scratch.arrayZ);
return true;
}
};
+9 -38
View File
@@ -916,32 +916,9 @@ hb_raster_paint_image (hb_paint_funcs_t *pfuncs HB_UNUSED,
* Gradient helpers
*/
#define PREALLOCATED_COLOR_STOPS 16
#define GRADIENT_LUT_SIZE 256
#define GRADIENT_LUT_MIN_PIXELS (64u * 64u)
static bool
get_color_stops (hb_raster_paint_t *c,
hb_color_line_t *color_line,
unsigned *count,
hb_color_stop_t **stops)
{
unsigned len = hb_color_line_get_color_stops (color_line, 0, nullptr, nullptr);
if (unlikely (!len))
return false;
if (len > *count)
{
if (unlikely (!c->scratch_color_stops.resize (len)))
return false;
*stops = c->scratch_color_stops.arrayZ;
}
hb_color_line_get_color_stops (color_line, 0, &len, *stops);
if (unlikely (!len))
return false;
*count = len;
return true;
}
static HB_ALWAYS_INLINE float
reflect_gradient_t (float t)
{
@@ -1080,12 +1057,10 @@ hb_raster_paint_linear_gradient (hb_paint_funcs_t *pfuncs HB_UNUSED,
hb_raster_image_t *surf = c->current_surface ();
if (unlikely (!surf)) return;
unsigned len = PREALLOCATED_COLOR_STOPS;
hb_color_stop_t stops_[PREALLOCATED_COLOR_STOPS];
hb_color_stop_t *stops = stops_;
if (unlikely (!get_color_stops (c, color_line, &len, &stops)))
if (unlikely (!c->fetch_color_stops (color_line)))
return;
hb_color_stop_t *stops = c->scratch_color_stops.arrayZ;
unsigned len = c->scratch_color_stops.length;
float mn, mx;
hb_paint_normalize_color_line (stops, len, &mn, &mx);
@@ -1231,12 +1206,10 @@ hb_raster_paint_radial_gradient (hb_paint_funcs_t *pfuncs HB_UNUSED,
hb_raster_image_t *surf = c->current_surface ();
if (unlikely (!surf)) return;
unsigned len = PREALLOCATED_COLOR_STOPS;
hb_color_stop_t stops_[PREALLOCATED_COLOR_STOPS];
hb_color_stop_t *stops = stops_;
if (unlikely (!get_color_stops (c, color_line, &len, &stops)))
if (unlikely (!c->fetch_color_stops (color_line)))
return;
hb_color_stop_t *stops = c->scratch_color_stops.arrayZ;
unsigned len = c->scratch_color_stops.length;
float mn, mx;
hb_paint_normalize_color_line (stops, len, &mn, &mx);
@@ -1509,12 +1482,10 @@ hb_raster_paint_sweep_gradient (hb_paint_funcs_t *pfuncs HB_UNUSED,
hb_raster_image_t *surf = c->current_surface ();
if (unlikely (!surf)) return;
unsigned len = PREALLOCATED_COLOR_STOPS;
hb_color_stop_t stops_[PREALLOCATED_COLOR_STOPS];
hb_color_stop_t *stops = stops_;
if (unlikely (!get_color_stops (c, color_line, &len, &stops)))
if (unlikely (!c->fetch_color_stops (color_line)))
return;
hb_color_stop_t *stops = c->scratch_color_stops.arrayZ;
unsigned len = c->scratch_color_stops.length;
float mn, mx;
hb_paint_normalize_color_line (stops, len, &mn, &mx);
+12
View File
@@ -216,6 +216,18 @@ struct hb_raster_paint_t
apply_scale_factor (t);
return t;
}
bool fetch_color_stops (hb_color_line_t *color_line)
{
unsigned count = hb_color_line_get_color_stops (color_line, 0, nullptr, nullptr);
if (unlikely (!count || !scratch_color_stops.resize (count)))
{
scratch_color_stops.resize (0);
return false;
}
hb_color_line_get_color_stops (color_line, 0, &count, scratch_color_stops.arrayZ);
return true;
}
};
+6 -16
View File
@@ -967,12 +967,9 @@ hb_pdf_paint_linear_gradient (hb_paint_funcs_t *,
return;
/* Fetch, normalize stops to [0,1], and adjust coordinates. */
hb_vector_t<hb_color_stop_t> &stops = paint->color_stops_scratch;
unsigned count = hb_color_line_get_color_stops (color_line, 0, nullptr, nullptr);
if (!count || unlikely (!stops.resize (count)))
if (!paint->fetch_color_stops (color_line))
return;
hb_color_line_get_color_stops (color_line, 0, &count, stops.arrayZ);
stops.length = count;
hb_vector_t<hb_color_stop_t> &stops = paint->color_stops_scratch;
float mn, mx;
hb_paint_normalize_color_line (stops.arrayZ, stops.length, &mn, &mx);
@@ -1060,12 +1057,9 @@ hb_pdf_paint_radial_gradient (hb_paint_funcs_t *,
return;
/* Fetch, normalize stops to [0,1], and adjust coordinates. */
hb_vector_t<hb_color_stop_t> &stops = paint->color_stops_scratch;
unsigned count = hb_color_line_get_color_stops (color_line, 0, nullptr, nullptr);
if (!count || unlikely (!stops.resize (count)))
if (!paint->fetch_color_stops (color_line))
return;
hb_color_line_get_color_stops (color_line, 0, &count, stops.arrayZ);
stops.length = count;
hb_vector_t<hb_color_stop_t> &stops = paint->color_stops_scratch;
float mn, mx;
hb_paint_normalize_color_line (stops.arrayZ, stops.length, &mn, &mx);
@@ -1343,17 +1337,13 @@ hb_pdf_paint_sweep_gradient (hb_paint_funcs_t *,
return;
/* Get and sort color stops. */
unsigned n_stops = hb_color_line_get_color_stops (color_line, 0, nullptr, nullptr);
if (unlikely (!paint->color_stops_scratch.resize (n_stops)))
if (!paint->fetch_color_stops (color_line))
return;
hb_color_line_get_color_stops (color_line, 0, &n_stops, paint->color_stops_scratch.arrayZ);
paint->color_stops_scratch.as_array ().qsort (
[] (const hb_color_stop_t &a, const hb_color_stop_t &b)
{ return a.offset < b.offset; });
hb_color_stop_t *stops = paint->color_stops_scratch.arrayZ;
if (!n_stops)
return;
unsigned n_stops = paint->color_stops_scratch.length;
hb_paint_extend_t extend = hb_color_line_get_extend (color_line);
+6 -26
View File
@@ -32,26 +32,6 @@
#include <math.h>
static hb_bool_t
hb_vector_get_color_stops (hb_color_line_t *color_line,
hb_vector_t<hb_color_stop_t> *stops)
{
unsigned len = hb_color_line_get_color_stops (color_line, 0, nullptr, nullptr);
if (unlikely (!len))
{
stops->length = 0;
return false;
}
if (unlikely (!stops->resize (len)))
return false;
hb_color_line_get_color_stops (color_line, 0, &len, stops->arrayZ);
stops->length = len;
if (unlikely (!len))
return false;
return true;
}
static const char *
hb_vector_svg_extend_mode_str (hb_paint_extend_t ext)
{
@@ -656,9 +636,9 @@ hb_vector_paint_linear_gradient (hb_paint_funcs_t *,
if (unlikely (!paint->ensure_initialized ()))
return;
hb_vector_t<hb_color_stop_t> &stops = paint->color_stops_scratch;
if (!hb_vector_get_color_stops (color_line, &stops) || !stops.length)
if (!paint->fetch_color_stops (color_line))
return;
hb_vector_t<hb_color_stop_t> &stops = paint->color_stops_scratch;
/* Sort + rescale stops to [0, 1]; shift the gradient axis
* by the original (mn, mx) so the visible gradient stays
@@ -719,9 +699,9 @@ hb_vector_paint_radial_gradient (hb_paint_funcs_t *,
if (unlikely (!paint->ensure_initialized ()))
return;
hb_vector_t<hb_color_stop_t> &stops = paint->color_stops_scratch;
if (!hb_vector_get_color_stops (color_line, &stops) || !stops.length)
if (!paint->fetch_color_stops (color_line))
return;
hb_vector_t<hb_color_stop_t> &stops = paint->color_stops_scratch;
float mn, mx;
hb_paint_normalize_color_line (stops.arrayZ, stops.length, &mn, &mx);
@@ -784,9 +764,9 @@ hb_vector_paint_sweep_gradient (hb_paint_funcs_t *,
if (unlikely (!paint->ensure_initialized ()))
return;
hb_vector_t<hb_color_stop_t> &stops = paint->color_stops_scratch;
if (!hb_vector_get_color_stops (color_line, &stops) || !stops.length)
if (!paint->fetch_color_stops (color_line))
return;
hb_vector_t<hb_color_stop_t> &stops = paint->color_stops_scratch;
float mn, mx;
hb_paint_normalize_color_line (stops.arrayZ, stops.length, &mn, &mx);
+12
View File
@@ -79,6 +79,18 @@ struct hb_vector_paint_t
float sx (float v) const { return v / x_scale_factor; }
float sy (float v) const { return v / y_scale_factor; }
bool fetch_color_stops (hb_color_line_t *color_line)
{
unsigned count = hb_color_line_get_color_stops (color_line, 0, nullptr, nullptr);
if (unlikely (!count || !color_stops_scratch.resize (count)))
{
color_stops_scratch.resize (0);
return false;
}
hb_color_line_get_color_stops (color_line, 0, &count, color_stops_scratch.arrayZ);
return true;
}
void set_precision (unsigned p)
{
p = hb_min (p, 12u);