Some fixes for the glyph cache

Copy the way cogl does its glyph caching some more.
At the minimum, this fixes problems where we were getting
wrong-sized Emoji inserted.
This commit is contained in:
Matthias Clasen 2017-09-11 14:02:44 -04:00
parent 40031930b2
commit b0d108291b

View File

@ -32,7 +32,7 @@ typedef struct {
typedef struct _GlyphCache GlyphCache; typedef struct _GlyphCache GlyphCache;
struct _GlyphCache { struct _GlyphCache {
GHashTable *fonts; GHashTable *hash_table;
cairo_surface_t *surface; cairo_surface_t *surface;
int width, height; int width, height;
@ -386,28 +386,30 @@ gsk_vulkan_renderer_ref_glyph_image (GskVulkanRenderer *self,
return g_object_ref (self->glyph_cache->image); return g_object_ref (self->glyph_cache->image);
} }
typedef struct _FontEntry FontEntry; typedef struct _GlyphCacheKey GlyphCacheKey;
typedef struct _GlyphEntry GlyphEntry; typedef struct _GlyphCacheValue GlyphCacheValue;
struct _FontEntry { struct _GlyphCacheKey {
PangoFont *font; PangoFont *font;
PangoFontDescription *desc;
guint hash;
GArray *glyphs;
};
struct _GlyphEntry {
PangoGlyph glyph; PangoGlyph glyph;
float tx, ty, tw, th;
float ascent, height;
}; };
static FontEntry *get_font_entry (GlyphCache *cache, struct _GlyphCacheValue {
PangoFont *font); float tx;
static GlyphEntry *get_glyph_entry (GlyphCache *cache, float ty;
FontEntry *fe, float tw;
PangoGlyph glyph, float th;
gboolean may_add);
float draw_x;
float draw_y;
float draw_width;
float draw_height;
};
static GlyphCacheValue *glyph_cache_lookup (GlyphCache *cache,
gboolean create,
PangoFont *font,
PangoGlyph glyph);
void void
gsk_vulkan_renderer_get_glyph_coords (GskVulkanRenderer *self, gsk_vulkan_renderer_get_glyph_coords (GskVulkanRenderer *self,
@ -420,84 +422,74 @@ gsk_vulkan_renderer_get_glyph_coords (GskVulkanRenderer *self,
float *ascent, float *ascent,
float *height) float *height)
{ {
FontEntry *fe; GlyphCacheValue *gv;
GlyphEntry *ge;
fe = get_font_entry (self->glyph_cache, font); gv = glyph_cache_lookup (self->glyph_cache, FALSE, font, glyph);
ge = get_glyph_entry (self->glyph_cache, fe, glyph, FALSE);
if (ge) if (gv)
{ {
*tx = ge->tx; *tx = gv->tx;
*ty = ge->ty; *ty = gv->ty;
*tw = ge->tw; *tw = gv->tw;
*th = ge->th; *th = gv->th;
*ascent = ge->ascent; *ascent = - gv->draw_y;
*height = ge->height; *height = gv->draw_height;
} }
} }
/*** Glyph cache ***/ /*** Glyph cache ***/
static gboolean static gboolean
font_equal (gconstpointer v1, gconstpointer v2) glyph_cache_equal (gconstpointer v1, gconstpointer v2)
{ {
const FontEntry *f1 = v1; const GlyphCacheKey *key1 = v1;
const FontEntry *f2 = v2; const GlyphCacheKey *key2 = v2;
return pango_font_description_equal (f1->desc, f2->desc); return key1->font == key2->font &&
key1->glyph == key2->glyph;
} }
static guint static guint
font_hash (gconstpointer v) glyph_cache_hash (gconstpointer v)
{ {
FontEntry *f = (FontEntry *)v; const GlyphCacheKey *key = v;
if (f->hash != 0) return GPOINTER_TO_UINT (key->font) ^ key->glyph;
return f->hash;
f->hash = pango_font_description_hash (f->desc);
if (f->hash == 0)
f->hash++;
return f->hash;
} }
static void static void
font_entry_free (gpointer v) glyph_cache_key_free (gpointer v)
{ {
FontEntry *f = v; GlyphCacheKey *f = v;
pango_font_description_free (f->desc);
g_object_unref (f->font); g_object_unref (f->font);
g_array_unref (f->glyphs);
g_free (f); g_free (f);
} }
static void static void
add_to_cache (GlyphCache *cache, glyph_cache_value_free (gpointer v)
PangoFont *font, {
PangoGlyph glyph, g_free (v);
GlyphEntry *ge) }
static void
add_to_cache (GlyphCache *cache,
PangoFont *font,
PangoGlyph glyph,
GlyphCacheValue *value)
{ {
PangoRectangle ink_rect;
cairo_t *cr; cairo_t *cr;
cairo_scaled_font_t *scaled_font; cairo_scaled_font_t *scaled_font;
cairo_glyph_t cg; cairo_glyph_t cg;
pango_font_get_glyph_extents (font, glyph, &ink_rect, NULL); if (cache->x + value->draw_width + 1 >= cache->width)
pango_extents_to_pixels (&ink_rect, NULL);
if (cache->x + ink_rect.width + 1 >= cache->width)
{ {
/* start a new row */ /* start a new row */
cache->y0 = cache->y + 1; cache->y0 = cache->y + 1;
cache->x = 1; cache->x = 1;
} }
if (cache->y0 + ink_rect.height + 1 >= cache->height) if (cache->y0 + value->draw_height + 1 >= cache->height)
{ {
g_critical ("Drats! Out of cache space. We should really handle this"); g_critical ("Drats! Out of cache space. We should really handle this");
return; return;
@ -514,80 +506,65 @@ add_to_cache (GlyphCache *cache,
cg.index = glyph; cg.index = glyph;
cg.x = cache->x; cg.x = cache->x;
cg.y = cache->y0 - ink_rect.y; cg.y = cache->y0 - value->draw_y;
cairo_show_glyphs (cr, &cg, 1); cairo_show_glyphs (cr, &cg, 1);
cairo_destroy (cr); cairo_destroy (cr);
cache->x = cache->x + ink_rect.width + 1; cache->x = cache->x + value->draw_width + 1;
cache->y = MAX (cache->y, cache->y0 + ink_rect.height + 1); cache->y = MAX (cache->y, cache->y0 + value->draw_height + 1);
ge->glyph = glyph; value->tx = cg.x / cache->width;
ge->tx = cg.x / cache->width; value->ty = (cg.y + value->draw_y) / cache->height;
ge->ty = (cg.y + ink_rect.y) / cache->height; value->tw = (float)value->draw_width / cache->width;
ge->tw = (float)ink_rect.width / cache->width; value->th = (float)value->draw_height / cache->height;
ge->th = (float)ink_rect.height / cache->height;
ge->ascent = (float) - ink_rect.y;
ge->height = (float) ink_rect.height;
} }
static GlyphEntry * static GlyphCacheValue *
get_glyph_entry (GlyphCache *cache, glyph_cache_lookup (GlyphCache *cache,
FontEntry *fe, gboolean create,
PangoGlyph glyph, PangoFont *font,
gboolean may_add) PangoGlyph glyph)
{ {
int i; GlyphCacheKey lookup_key;
GlyphEntry g; GlyphCacheValue *value;
GlyphEntry *ge;
for (i = 0; i < fe->glyphs->len; i++) lookup_key.font = font;
lookup_key.glyph = glyph;
value = g_hash_table_lookup (cache->hash_table, &lookup_key);
if (create && value == NULL)
{ {
ge = &g_array_index (fe->glyphs, GlyphEntry, i); GlyphCacheKey *key;
PangoRectangle ink_rect;
if (ge->glyph == glyph) value = g_new (GlyphCacheValue, 1);
return ge;
pango_font_get_glyph_extents (font, glyph, &ink_rect, NULL);
pango_extents_to_pixels (&ink_rect, NULL);
value->draw_x = ink_rect.x;
value->draw_y = ink_rect.y;
value->draw_width = ink_rect.width;
value->draw_height = ink_rect.height;
if (ink_rect.width > 0 && ink_rect.height > 0)
{
add_to_cache (cache, font, glyph, value);
g_clear_object (&cache->image);
}
key = g_new (GlyphCacheKey, 1);
key->font = g_object_ref (font);
key->glyph = glyph;
g_hash_table_insert (cache->hash_table, key, value);
} }
if (!may_add) return value;
return NULL;
g_clear_object (&cache->image);
add_to_cache (cache, fe->font, glyph, &g);
g_array_append_val (fe->glyphs, g);
ge = &g_array_index (fe->glyphs, GlyphEntry, fe->glyphs->len - 1);
return ge;
}
static FontEntry *
get_font_entry (GlyphCache *cache,
PangoFont *font)
{
FontEntry key;
FontEntry *fe;
key.font = font;
key.desc = pango_font_describe (font);
key.hash = 0;
fe = g_hash_table_lookup (cache->fonts, &key);
if (fe == NULL)
{
fe = g_new0 (FontEntry, 1);
fe->font = g_object_ref (font);
fe->desc = key.desc;
fe->hash = 0;
fe->glyphs = g_array_sized_new (FALSE, FALSE, sizeof (GlyphEntry), 256);
g_hash_table_add (cache->fonts, fe);
}
else
pango_font_description_free (key.desc);
return fe;
} }
void void
@ -595,11 +572,8 @@ gsk_vulkan_renderer_cache_glyphs (GskVulkanRenderer *self,
PangoFont *font, PangoFont *font,
PangoGlyphString *glyphs) PangoGlyphString *glyphs)
{ {
FontEntry *fe;
int i; int i;
fe = get_font_entry (self->glyph_cache, font);
for (i = 0; i < glyphs->num_glyphs; i++) for (i = 0; i < glyphs->num_glyphs; i++)
{ {
PangoGlyphInfo *gi = &glyphs->glyphs[i]; PangoGlyphInfo *gi = &glyphs->glyphs[i];
@ -607,7 +581,7 @@ gsk_vulkan_renderer_cache_glyphs (GskVulkanRenderer *self,
if (gi->glyph != PANGO_GLYPH_EMPTY) if (gi->glyph != PANGO_GLYPH_EMPTY)
{ {
if (!(gi->glyph & PANGO_GLYPH_UNKNOWN_FLAG)) if (!(gi->glyph & PANGO_GLYPH_UNKNOWN_FLAG))
get_glyph_entry (self->glyph_cache, fe, gi->glyph, TRUE); (void) glyph_cache_lookup (self->glyph_cache, TRUE, font, gi->glyph);
} }
} }
} }
@ -618,7 +592,8 @@ create_glyph_cache (void)
GlyphCache *cache; GlyphCache *cache;
cache = g_new0 (GlyphCache, 1); cache = g_new0 (GlyphCache, 1);
cache->fonts = g_hash_table_new_full (font_hash, font_equal, NULL, font_entry_free); cache->hash_table = g_hash_table_new_full (glyph_cache_hash, glyph_cache_equal,
glyph_cache_key_free, glyph_cache_value_free);
cache->width = 1024; cache->width = 1024;
cache->height = 1024; cache->height = 1024;
cache->surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, cache->width, cache->height); cache->surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, cache->width, cache->height);
@ -632,7 +607,7 @@ create_glyph_cache (void)
static void static void
free_glyph_cache (GlyphCache *cache) free_glyph_cache (GlyphCache *cache)
{ {
g_hash_table_unref (cache->fonts); g_hash_table_unref (cache->hash_table);
cairo_surface_destroy (cache->surface); cairo_surface_destroy (cache->surface);
g_free (cache); g_free (cache);
} }
@ -640,12 +615,10 @@ free_glyph_cache (GlyphCache *cache)
static void static void
dump_glyph_cache_stats (GlyphCache *cache) dump_glyph_cache_stats (GlyphCache *cache)
{ {
GHashTableIter iter;
FontEntry *fe;
static gint64 time; static gint64 time;
gint64 now; gint64 now;
if (!cache->fonts) if (!cache->hash_table)
return; return;
now = g_get_monotonic_time (); now = g_get_monotonic_time ();
@ -654,16 +627,5 @@ dump_glyph_cache_stats (GlyphCache *cache)
time = now; time = now;
g_print ("Glyph cache:\n"); cairo_surface_write_to_png (cache->surface, "gsk-glyph-cache.png");
g_hash_table_iter_init (&iter, cache->fonts);
while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&fe))
{
char *s;
s = pango_font_description_to_string (fe->desc);
g_print ("%s: %d glyphs cached\n", s, fe->glyphs->len);
g_free (s);
}
g_print ("---\n");
cairo_surface_write_to_png (cache->surface, "glyph-cache.png");
} }