[COLR] Reduce malloc pressure

By caching paint-extents.
This commit is contained in:
Behdad Esfahbod committed 2025-03-03 02:10:04 -07:00
1 parent 3f89843df3
commit ce12c19548
3 files changed
+79 -14

No files matched your search

+68 -12
View File
@@ -47,6 +47,11 @@ namespace OT {
struct hb_paint_context_t;
}
struct hb_colr_scratch_t
{
hb_paint_extents_context_t paint_extents;
};
namespace OT {
struct COLR;
@@ -2063,6 +2068,8 @@ struct COLR
{
static constexpr hb_tag_t tableTag = HB_OT_TAG_COLR;
bool has_data () const { return has_v0_data () || version; }
bool has_v0_data () const { return numBaseGlyphs; }
bool has_v1_data () const
{
@@ -2099,13 +2106,21 @@ struct COLR
~accelerator_t () { this->colr.destroy (); }
bool has_data () const { return colr->has_data (); }
#ifndef HB_NO_PAINT
bool
get_extents (hb_font_t *font,
hb_codepoint_t glyph,
hb_glyph_extents_t *extents) const
{
return colr->get_extents (font, glyph, extents);
if (unlikely (!has_data ())) return false;
hb_colr_scratch_t *scratch = acquire_scratch ();
if (unlikely (!scratch)) return true;
bool ret = colr->get_extents (font, glyph, extents, *scratch);
release_scratch (scratch);
return ret;
}
bool paint_glyph (hb_font_t *font,
@@ -2115,7 +2130,13 @@ struct COLR
hb_color_t foreground,
bool clip = true) const
{
return colr->paint_glyph (font, glyph, funcs, data, palette_index, foreground, clip);
if (unlikely (!has_data ())) return false;
hb_colr_scratch_t *scratch = acquire_scratch ();
if (unlikely (!scratch)) return true;
bool ret = colr->paint_glyph (font, glyph, funcs, data, palette_index, foreground, clip, *scratch);
release_scratch (scratch);
return ret;
}
#endif
@@ -2152,8 +2173,34 @@ struct COLR
const DeltaSetIndexMap *get_delta_set_index_map_ptr () const
{ return colr->get_delta_set_index_map_ptr (); }
private:
hb_colr_scratch_t *acquire_scratch () const
{
hb_colr_scratch_t *scratch = cached_scratch.get_acquire ();
if (!scratch || unlikely (!cached_scratch.cmpexch (scratch, nullptr)))
{
scratch = (hb_colr_scratch_t *) hb_calloc (1, sizeof (hb_colr_scratch_t));
if (unlikely (!scratch))
return nullptr;
}
return scratch;
}
void release_scratch (hb_colr_scratch_t *scratch) const
{
if (!cached_scratch.cmpexch (nullptr, scratch))
{
scratch->~hb_colr_scratch_t ();
hb_free (scratch);
}
}
public:
hb_blob_ptr_t<COLR> colr;
private:
hb_atomic_ptr_t<hb_colr_scratch_t> cached_scratch;
};
void closure_glyphs (hb_codepoint_t glyph,
@@ -2525,7 +2572,10 @@ struct COLR
#ifndef HB_NO_PAINT
bool
get_extents (hb_font_t *font, hb_codepoint_t glyph, hb_glyph_extents_t *extents) const
get_extents (hb_font_t *font,
hb_codepoint_t glyph,
hb_glyph_extents_t *extents,
hb_colr_scratch_t &scratch) const
{
ItemVarStoreInstancer instancer (get_var_store_ptr (),
@@ -2539,10 +2589,10 @@ struct COLR
}
auto *extents_funcs = hb_paint_extents_get_funcs ();
hb_paint_extents_context_t extents_data;
bool ret = paint_glyph (font, glyph, extents_funcs, &extents_data, 0, HB_COLOR(0,0,0,0));
scratch.paint_extents.clear ();
bool ret = paint_glyph (font, glyph, extents_funcs, &scratch.paint_extents, 0, HB_COLOR(0,0,0,0), true, scratch);
hb_extents_t<> e = extents_data.get_extents ();
hb_extents_t<> e = scratch.paint_extents.get_extents ();
if (e.is_void ())
{
extents->x_bearing = 0;
@@ -2588,7 +2638,12 @@ struct COLR
#ifndef HB_NO_PAINT
bool
paint_glyph (hb_font_t *font, hb_codepoint_t glyph, hb_paint_funcs_t *funcs, void *data, unsigned int palette_index, hb_color_t foreground, bool clip = true) const
paint_glyph (hb_font_t *font,
hb_codepoint_t glyph,
hb_paint_funcs_t *funcs, void *data,
unsigned int palette_index, hb_color_t foreground,
bool clip,
hb_colr_scratch_t &scratch) const
{
ItemVarStoreInstancer instancer (get_var_store_ptr (),
get_delta_set_index_map_ptr (),
@@ -2624,15 +2679,16 @@ struct COLR
else
{
auto *extents_funcs = hb_paint_extents_get_funcs ();
hb_paint_extents_context_t extents_data;
scratch.paint_extents.clear ();
paint_glyph (font, glyph,
extents_funcs, &extents_data,
extents_funcs, &scratch.paint_extents,
palette_index, foreground,
false);
false,
scratch);
hb_extents_t<> extents = extents_data.get_extents ();
is_bounded = extents_data.is_bounded ();
hb_extents_t<> extents = scratch.paint_extents.get_extents ();
is_bounded = scratch.paint_extents.is_bounded ();
c.funcs->push_clip_rectangle (c.data,
extents.xmin,
+1 -1
View File
@@ -306,7 +306,7 @@ struct hb_bounds_t
EMPTY,
};
hb_bounds_t (status_t status) : status (status) {}
hb_bounds_t (status_t status = UNBOUNDED) : status (status) {}
hb_bounds_t (const hb_extents_t<Float> &extents) :
status (extents.is_empty () ? EMPTY : BOUNDED), extents (extents) {}
+10 -1
View File
@@ -35,13 +35,22 @@ typedef struct hb_paint_extents_context_t hb_paint_extents_context_t;
struct hb_paint_extents_context_t
{
hb_paint_extents_context_t ()
void clear ()
{
transforms.clear ();
clips.clear ();
groups.clear ();
transforms.push (hb_transform_t<>{});
clips.push (hb_bounds_t<>{hb_bounds_t<>::UNBOUNDED});
groups.push (hb_bounds_t<>{hb_bounds_t<>::EMPTY});
}
hb_paint_extents_context_t ()
{
clear ();
}
hb_extents_t<> get_extents ()
{
return groups.tail().extents;