Merge pull request #3605 from harfbuzz/cache-varstore

Cache varstore
https://github.com/harfbuzz/harfbuzz/pull/3605
This commit is contained in:
Behdad Esfahbod 2022-05-23 12:24:38 -06:00 committed by GitHub
commit d473397831
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 139 additions and 36 deletions

View File

@ -110,12 +110,24 @@ hb_ot_get_glyph_h_advances (hb_font_t* font, void* font_data,
const hb_ot_face_t *ot_face = (const hb_ot_face_t *) font_data; const hb_ot_face_t *ot_face = (const hb_ot_face_t *) font_data;
const OT::hmtx_accelerator_t &hmtx = *ot_face->hmtx; const OT::hmtx_accelerator_t &hmtx = *ot_face->hmtx;
#ifndef HB_NO_VAR
const OT::HVARVVAR &HVAR = *hmtx.var_table;
const OT::VariationStore &varStore = &HVAR + HVAR.varStore;
OT::VariationStore::cache_t *cache = font->num_coords ? varStore.create_cache () : nullptr;
#else
OT::VariationStore::cache_t *cache = nullptr;
#endif
for (unsigned int i = 0; i < count; i++) for (unsigned int i = 0; i < count; i++)
{ {
*first_advance = font->em_scale_x (hmtx.get_advance (*first_glyph, font)); *first_advance = font->em_scale_x (hmtx.get_advance (*first_glyph, font, cache));
first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride); first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride);
first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride); first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
} }
#ifndef HB_NO_VAR
OT::VariationStore::destroy_cache (cache);
#endif
} }
#ifndef HB_NO_VERTICAL #ifndef HB_NO_VERTICAL
@ -132,12 +144,26 @@ hb_ot_get_glyph_v_advances (hb_font_t* font, void* font_data,
const OT::vmtx_accelerator_t &vmtx = *ot_face->vmtx; const OT::vmtx_accelerator_t &vmtx = *ot_face->vmtx;
if (vmtx.has_data ()) if (vmtx.has_data ())
{
#ifndef HB_NO_VAR
const OT::HVARVVAR &VVAR = *vmtx.var_table;
const OT::VariationStore &varStore = &VVAR + VVAR.varStore;
OT::VariationStore::cache_t *cache = font->num_coords ? varStore.create_cache () : nullptr;
#else
OT::VariationStore::cache_t *cache = nullptr;
#endif
for (unsigned int i = 0; i < count; i++) for (unsigned int i = 0; i < count; i++)
{ {
*first_advance = font->em_scale_y (-(int) vmtx.get_advance (*first_glyph, font)); *first_advance = font->em_scale_y (-(int) vmtx.get_advance (*first_glyph, font, cache));
first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride); first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride);
first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride); first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
} }
#ifndef HB_NO_VAR
OT::VariationStore::destroy_cache (cache);
#endif
}
else else
{ {
hb_font_extents_t font_extents; hb_font_extents_t font_extents;

View File

@ -242,7 +242,7 @@ struct hmtxvmtx
return side_bearing; return side_bearing;
if (var_table.get_length ()) if (var_table.get_length ())
return side_bearing + var_table->get_side_bearing_var (glyph, font->coords, font->num_coords); // TODO Optimize?! return side_bearing + var_table->get_side_bearing_var (glyph, font->coords, font->num_coords);
return _glyf_get_side_bearing_var (font, glyph, T::tableTag == HB_OT_TAG_vmtx); return _glyf_get_side_bearing_var (font, glyph, T::tableTag == HB_OT_TAG_vmtx);
#else #else
@ -284,7 +284,8 @@ struct hmtxvmtx
} }
unsigned int get_advance (hb_codepoint_t glyph, unsigned int get_advance (hb_codepoint_t glyph,
hb_font_t *font) const hb_font_t *font,
VariationStore::cache_t *store_cache = nullptr) const
{ {
unsigned int advance = get_advance (glyph); unsigned int advance = get_advance (glyph);
@ -293,7 +294,7 @@ struct hmtxvmtx
return advance; return advance;
if (var_table.get_length ()) if (var_table.get_length ())
return advance + roundf (var_table->get_advance_var (glyph, font)); // TODO Optimize?! return advance + roundf (var_table->get_advance_var (glyph, font, store_cache)); // TODO Optimize?!
return _glyf_get_advance_var (font, glyph, T::tableTag == HB_OT_TAG_vmtx); return _glyf_get_advance_var (font, glyph, T::tableTag == HB_OT_TAG_vmtx);
#else #else
@ -310,7 +311,7 @@ struct hmtxvmtx
unsigned int default_advance; unsigned int default_advance;
private: public:
hb_blob_ptr_t<hmtxvmtx> table; hb_blob_ptr_t<hmtxvmtx> table;
hb_blob_ptr_t<HVARVVAR> var_table; hb_blob_ptr_t<HVARVVAR> var_table;
}; };

View File

@ -2601,14 +2601,27 @@ struct VarRegionAxis
DEFINE_SIZE_STATIC (6); DEFINE_SIZE_STATIC (6);
}; };
#define REGION_CACHE_ITEM_CACHE_INVALID 2.f
struct VarRegionList struct VarRegionList
{ {
using cache_t = float;
float evaluate (unsigned int region_index, float evaluate (unsigned int region_index,
const int *coords, unsigned int coord_len) const const int *coords, unsigned int coord_len,
cache_t *cache = nullptr) const
{ {
if (unlikely (region_index >= regionCount)) if (unlikely (region_index >= regionCount))
return 0.; return 0.;
float *cached = nullptr;
if (cache)
{
cached = &(cache[region_index]);
if (likely (*cached != REGION_CACHE_ITEM_CACHE_INVALID))
return *cached;
}
const VarRegionAxis *axes = axesZ.arrayZ + (region_index * axisCount); const VarRegionAxis *axes = axesZ.arrayZ + (region_index * axisCount);
float v = 1.; float v = 1.;
@ -2618,9 +2631,16 @@ struct VarRegionList
int coord = i < coord_len ? coords[i] : 0; int coord = i < coord_len ? coords[i] : 0;
float factor = axes[i].evaluate (coord); float factor = axes[i].evaluate (coord);
if (factor == 0.f) if (factor == 0.f)
{
if (cache)
*cached = 0.;
return 0.; return 0.;
}
v *= factor; v *= factor;
} }
if (cache)
*cached = v;
return v; return v;
} }
@ -2678,7 +2698,8 @@ struct VarData
float get_delta (unsigned int inner, float get_delta (unsigned int inner,
const int *coords, unsigned int coord_count, const int *coords, unsigned int coord_count,
const VarRegionList &regions) const const VarRegionList &regions,
VarRegionList::cache_t *cache = nullptr) const
{ {
if (unlikely (inner >= itemCount)) if (unlikely (inner >= itemCount))
return 0.; return 0.;
@ -2695,13 +2716,13 @@ struct VarData
const HBINT16 *scursor = reinterpret_cast<const HBINT16 *> (row); const HBINT16 *scursor = reinterpret_cast<const HBINT16 *> (row);
for (; i < scount; i++) for (; i < scount; i++)
{ {
float scalar = regions.evaluate (regionIndices.arrayZ[i], coords, coord_count); float scalar = regions.evaluate (regionIndices.arrayZ[i], coords, coord_count, cache);
delta += scalar * *scursor++; delta += scalar * *scursor++;
} }
const HBINT8 *bcursor = reinterpret_cast<const HBINT8 *> (scursor); const HBINT8 *bcursor = reinterpret_cast<const HBINT8 *> (scursor);
for (; i < count; i++) for (; i < count; i++)
{ {
float scalar = regions.evaluate (regionIndices.arrayZ[i], coords, coord_count); float scalar = regions.evaluate (regionIndices.arrayZ[i], coords, coord_count, cache);
delta += scalar * *bcursor++; delta += scalar * *bcursor++;
} }
@ -2846,9 +2867,28 @@ struct VarData
struct VariationStore struct VariationStore
{ {
using cache_t = VarRegionList::cache_t;
cache_t *create_cache () const
{
auto &r = this+regions;
unsigned count = r.regionCount;
float *cache = (float *) hb_malloc (sizeof (float) * count);
if (unlikely (!cache)) return nullptr;
for (unsigned i = 0; i < count; i++)
cache[i] = REGION_CACHE_ITEM_CACHE_INVALID;
return cache;
}
static void destroy_cache (cache_t *cache) { hb_free (cache); }
private: private:
float get_delta (unsigned int outer, unsigned int inner, float get_delta (unsigned int outer, unsigned int inner,
const int *coords, unsigned int coord_count) const const int *coords, unsigned int coord_count,
VarRegionList::cache_t *cache = nullptr) const
{ {
#ifdef HB_NO_VAR #ifdef HB_NO_VAR
return 0.f; return 0.f;
@ -2859,16 +2899,18 @@ struct VariationStore
return (this+dataSets[outer]).get_delta (inner, return (this+dataSets[outer]).get_delta (inner,
coords, coord_count, coords, coord_count,
this+regions); this+regions,
cache);
} }
public: public:
float get_delta (unsigned int index, float get_delta (unsigned int index,
const int *coords, unsigned int coord_count) const const int *coords, unsigned int coord_count,
VarRegionList::cache_t *cache = nullptr) const
{ {
unsigned int outer = index >> 16; unsigned int outer = index >> 16;
unsigned int inner = index & 0xFFFF; unsigned int inner = index & 0xFFFF;
return get_delta (outer, inner, coords, coord_count); return get_delta (outer, inner, coords, coord_count, cache);
} }
bool sanitize (hb_sanitize_context_t *c) const bool sanitize (hb_sanitize_context_t *c) const
@ -2995,6 +3037,8 @@ struct VariationStore
DEFINE_SIZE_ARRAY_SIZED (8, dataSets); DEFINE_SIZE_ARRAY_SIZED (8, dataSets);
}; };
#undef REGION_CACHE_ITEM_CACHE_INVALID
/* /*
* Feature Variations * Feature Variations
*/ */
@ -3462,11 +3506,15 @@ struct VariationDevice
private: private:
hb_position_t get_x_delta (hb_font_t *font, const VariationStore &store) const hb_position_t get_x_delta (hb_font_t *font,
{ return font->em_scalef_x (get_delta (font, store)); } const VariationStore &store,
VariationStore::cache_t *store_cache = nullptr) const
{ return font->em_scalef_x (get_delta (font, store, store_cache)); }
hb_position_t get_y_delta (hb_font_t *font, const VariationStore &store) const hb_position_t get_y_delta (hb_font_t *font,
{ return font->em_scalef_y (get_delta (font, store)); } const VariationStore &store,
VariationStore::cache_t *store_cache = nullptr) const
{ return font->em_scalef_y (get_delta (font, store, store_cache)); }
VariationDevice* copy (hb_serialize_context_t *c, const hb_map_t *layout_variation_idx_map) const VariationDevice* copy (hb_serialize_context_t *c, const hb_map_t *layout_variation_idx_map) const
{ {
@ -3500,9 +3548,11 @@ struct VariationDevice
private: private:
float get_delta (hb_font_t *font, const VariationStore &store) const float get_delta (hb_font_t *font,
const VariationStore &store,
VariationStore::cache_t *store_cache = nullptr) const
{ {
return store.get_delta (varIdx, font->coords, font->num_coords); return store.get_delta (varIdx, font->coords, font->num_coords, (VariationStore::cache_t *) store_cache);
} }
protected: protected:
@ -3525,7 +3575,9 @@ struct DeviceHeader
struct Device struct Device
{ {
hb_position_t get_x_delta (hb_font_t *font, const VariationStore &store=Null (VariationStore)) const hb_position_t get_x_delta (hb_font_t *font,
const VariationStore &store=Null (VariationStore),
VariationStore::cache_t *store_cache = nullptr) const
{ {
switch (u.b.format) switch (u.b.format)
{ {
@ -3535,13 +3587,15 @@ struct Device
#endif #endif
#ifndef HB_NO_VAR #ifndef HB_NO_VAR
case 0x8000: case 0x8000:
return u.variation.get_x_delta (font, store); return u.variation.get_x_delta (font, store, store_cache);
#endif #endif
default: default:
return 0; return 0;
} }
} }
hb_position_t get_y_delta (hb_font_t *font, const VariationStore &store=Null (VariationStore)) const hb_position_t get_y_delta (hb_font_t *font,
const VariationStore &store=Null (VariationStore),
VariationStore::cache_t *store_cache = nullptr) const
{ {
switch (u.b.format) switch (u.b.format)
{ {
@ -3551,7 +3605,7 @@ struct Device
#endif #endif
#ifndef HB_NO_VAR #ifndef HB_NO_VAR
case 0x8000: case 0x8000:
return u.variation.get_y_delta (font, store); return u.variation.get_y_delta (font, store, store_cache);
#endif #endif
default: default:
return 0; return 0;

View File

@ -146,23 +146,24 @@ struct ValueFormat : HBUINT16
if (!use_x_device && !use_y_device) return ret; if (!use_x_device && !use_y_device) return ret;
const VariationStore &store = c->var_store; const VariationStore &store = c->var_store;
auto *cache = c->var_store_cache;
/* pixel -> fractional pixel */ /* pixel -> fractional pixel */
if (format & xPlaDevice) { if (format & xPlaDevice) {
if (use_x_device) glyph_pos.x_offset += (base + get_device (values, &ret)).get_x_delta (font, store); if (use_x_device) glyph_pos.x_offset += (base + get_device (values, &ret)).get_x_delta (font, store, cache);
values++; values++;
} }
if (format & yPlaDevice) { if (format & yPlaDevice) {
if (use_y_device) glyph_pos.y_offset += (base + get_device (values, &ret)).get_y_delta (font, store); if (use_y_device) glyph_pos.y_offset += (base + get_device (values, &ret)).get_y_delta (font, store, cache);
values++; values++;
} }
if (format & xAdvDevice) { if (format & xAdvDevice) {
if (horizontal && use_x_device) glyph_pos.x_advance += (base + get_device (values, &ret)).get_x_delta (font, store); if (horizontal && use_x_device) glyph_pos.x_advance += (base + get_device (values, &ret)).get_x_delta (font, store, cache);
values++; values++;
} }
if (format & yAdvDevice) { if (format & yAdvDevice) {
/* y_advance values grow downward but font-space grows upward, hence negation */ /* y_advance values grow downward but font-space grows upward, hence negation */
if (!horizontal && use_y_device) glyph_pos.y_advance -= (base + get_device (values, &ret)).get_y_delta (font, store); if (!horizontal && use_y_device) glyph_pos.y_advance -= (base + get_device (values, &ret)).get_y_delta (font, store, cache);
values++; values++;
} }
return ret; return ret;
@ -465,9 +466,9 @@ struct AnchorFormat3
*y = font->em_fscale_y (yCoordinate); *y = font->em_fscale_y (yCoordinate);
if (font->x_ppem || font->num_coords) if (font->x_ppem || font->num_coords)
*x += (this+xDeviceTable).get_x_delta (font, c->var_store); *x += (this+xDeviceTable).get_x_delta (font, c->var_store, c->var_store_cache);
if (font->y_ppem || font->num_coords) if (font->y_ppem || font->num_coords)
*y += (this+yDeviceTable).get_y_delta (font, c->var_store); *y += (this+yDeviceTable).get_y_delta (font, c->var_store, c->var_store_cache);
} }
bool sanitize (hb_sanitize_context_t *c) const bool sanitize (hb_sanitize_context_t *c) const

View File

@ -621,16 +621,17 @@ struct hb_ot_apply_context_t :
skipping_iterator_t iter_input, iter_context; skipping_iterator_t iter_input, iter_context;
unsigned int table_index; /* GSUB/GPOS */
hb_font_t *font; hb_font_t *font;
hb_face_t *face; hb_face_t *face;
hb_buffer_t *buffer; hb_buffer_t *buffer;
recurse_func_t recurse_func; recurse_func_t recurse_func;
const GDEF &gdef; const GDEF &gdef;
const VariationStore &var_store; const VariationStore &var_store;
VariationStore::cache_t *var_store_cache;
hb_direction_t direction; hb_direction_t direction;
hb_mask_t lookup_mask; hb_mask_t lookup_mask;
unsigned int table_index; /* GSUB/GPOS */
unsigned int lookup_index; unsigned int lookup_index;
unsigned int lookup_props; unsigned int lookup_props;
unsigned int nesting_level_left; unsigned int nesting_level_left;
@ -648,6 +649,7 @@ struct hb_ot_apply_context_t :
hb_font_t *font_, hb_font_t *font_,
hb_buffer_t *buffer_) : hb_buffer_t *buffer_) :
iter_input (), iter_context (), iter_input (), iter_context (),
table_index (table_index_),
font (font_), face (font->face), buffer (buffer_), font (font_), face (font->face), buffer (buffer_),
recurse_func (nullptr), recurse_func (nullptr),
gdef ( gdef (
@ -658,9 +660,15 @@ struct hb_ot_apply_context_t :
#endif #endif
), ),
var_store (gdef.get_var_store ()), var_store (gdef.get_var_store ()),
var_store_cache (
#ifndef HB_NO_VAR
table_index == 1 && font->num_coords ? var_store.create_cache () : nullptr
#else
nullptr
#endif
),
direction (buffer_->props.direction), direction (buffer_->props.direction),
lookup_mask (1), lookup_mask (1),
table_index (table_index_),
lookup_index ((unsigned int) -1), lookup_index ((unsigned int) -1),
lookup_props (0), lookup_props (0),
nesting_level_left (HB_MAX_NESTING_LEVEL), nesting_level_left (HB_MAX_NESTING_LEVEL),
@ -669,7 +677,15 @@ struct hb_ot_apply_context_t :
auto_zwj (true), auto_zwj (true),
per_syllable (false), per_syllable (false),
random (false), random (false),
random_state (1) { init_iters (); } random_state (1)
{ init_iters (); }
~hb_ot_apply_context_t ()
{
#ifndef HB_NO_VAR
VariationStore::destroy_cache (var_store_cache);
#endif
}
void init_iters () void init_iters ()
{ {

View File

@ -319,10 +319,15 @@ struct HVARVVAR
hvar_plan.index_map_plans.as_array ())); hvar_plan.index_map_plans.as_array ()));
} }
float get_advance_var (hb_codepoint_t glyph, hb_font_t *font) const float get_advance_var (hb_codepoint_t glyph,
hb_font_t *font,
VariationStore::cache_t *store_cache = nullptr) const
{ {
uint32_t varidx = (this+advMap).map (glyph); uint32_t varidx = (this+advMap).map (glyph);
return (this+varStore).get_delta (varidx, font->coords, font->num_coords); return (this+varStore).get_delta (varidx,
font->coords,
font->num_coords,
store_cache);
} }
float get_side_bearing_var (hb_codepoint_t glyph, float get_side_bearing_var (hb_codepoint_t glyph,
@ -335,7 +340,7 @@ struct HVARVVAR
bool has_side_bearing_deltas () const { return lsbMap && rsbMap; } bool has_side_bearing_deltas () const { return lsbMap && rsbMap; }
protected: public:
FixedVersion<>version; /* Version of the metrics variation table FixedVersion<>version; /* Version of the metrics variation table
* initially set to 0x00010000u */ * initially set to 0x00010000u */
Offset32To<VariationStore> Offset32To<VariationStore>