2017-11-18 13:30:57 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gskglglyphcacheprivate.h"
|
|
|
|
#include "gskgldriverprivate.h"
|
|
|
|
#include "gskdebugprivate.h"
|
|
|
|
#include "gskprivate.h"
|
2019-05-22 05:33:45 +00:00
|
|
|
#include "gskgltextureatlasprivate.h"
|
2017-11-18 13:30:57 +00:00
|
|
|
|
2019-04-24 11:27:43 +00:00
|
|
|
#include "gdk/gdkglcontextprivate.h"
|
|
|
|
|
2017-11-18 13:30:57 +00:00
|
|
|
#include <graphene.h>
|
2018-03-28 03:23:43 +00:00
|
|
|
#include <cairo.h>
|
2017-11-18 13:30:57 +00:00
|
|
|
#include <epoxy/gl.h>
|
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
/* Cache eviction strategy
|
2017-11-18 13:30:57 +00:00
|
|
|
*
|
2019-06-04 21:27:09 +00:00
|
|
|
* Each cached glyph has an age that gets reset every time a cached
|
|
|
|
* glyph gets used. Glyphs that have not been used for the
|
|
|
|
* MAX_FRAME_AGE frames are considered old.
|
|
|
|
*
|
|
|
|
* We keep count of the pixels of each atlas that are taken up by old
|
|
|
|
* data. When the fraction of old pixels gets too high, we drop the
|
|
|
|
* atlas and all the items it contained.
|
2017-11-18 13:30:57 +00:00
|
|
|
*/
|
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
#define MAX_FRAME_AGE (5 * 60)
|
2017-11-18 13:30:57 +00:00
|
|
|
|
|
|
|
static guint glyph_cache_hash (gconstpointer v);
|
|
|
|
static gboolean glyph_cache_equal (gconstpointer v1,
|
|
|
|
gconstpointer v2);
|
|
|
|
static void glyph_cache_key_free (gpointer v);
|
|
|
|
static void glyph_cache_value_free (gpointer v);
|
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
GskGLGlyphCache *
|
|
|
|
gsk_gl_glyph_cache_new (GdkDisplay *display,
|
|
|
|
GskGLTextureAtlases *atlases)
|
2017-11-18 13:30:57 +00:00
|
|
|
{
|
2019-06-04 21:27:09 +00:00
|
|
|
GskGLGlyphCache *glyph_cache;
|
2017-11-18 13:30:57 +00:00
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
glyph_cache = g_new0 (GskGLGlyphCache, 1);
|
2019-02-10 09:12:47 +00:00
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
glyph_cache->display = display;
|
|
|
|
glyph_cache->hash_table = g_hash_table_new_full (glyph_cache_hash, glyph_cache_equal,
|
|
|
|
glyph_cache_key_free, glyph_cache_value_free);
|
2017-11-18 13:30:57 +00:00
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
glyph_cache->atlases = gsk_gl_texture_atlases_ref (atlases);
|
2017-11-18 13:30:57 +00:00
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
glyph_cache->ref_count = 1;
|
2019-01-06 14:35:54 +00:00
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
return glyph_cache;
|
2017-11-18 13:30:57 +00:00
|
|
|
}
|
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
GskGLGlyphCache *
|
|
|
|
gsk_gl_glyph_cache_ref (GskGLGlyphCache *self)
|
2017-11-18 13:30:57 +00:00
|
|
|
{
|
2019-06-04 21:27:09 +00:00
|
|
|
self->ref_count++;
|
|
|
|
|
|
|
|
return self;
|
2017-11-18 13:30:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-06-04 21:27:09 +00:00
|
|
|
gsk_gl_glyph_cache_unref (GskGLGlyphCache *self)
|
2017-11-18 13:30:57 +00:00
|
|
|
{
|
2019-06-04 21:27:09 +00:00
|
|
|
g_assert (self->ref_count > 0);
|
2017-11-18 13:30:57 +00:00
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
if (self->ref_count == 1)
|
2017-11-18 13:30:57 +00:00
|
|
|
{
|
2019-06-04 21:27:09 +00:00
|
|
|
gsk_gl_texture_atlases_unref (self->atlases);
|
|
|
|
g_hash_table_unref (self->hash_table);
|
|
|
|
g_free (self);
|
|
|
|
return;
|
2017-11-18 13:30:57 +00:00
|
|
|
}
|
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
self->ref_count--;
|
2017-11-18 13:30:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
glyph_cache_equal (gconstpointer v1, gconstpointer v2)
|
|
|
|
{
|
|
|
|
const GlyphCacheKey *key1 = v1;
|
|
|
|
const GlyphCacheKey *key2 = v2;
|
|
|
|
|
|
|
|
return key1->font == key2->font &&
|
|
|
|
key1->glyph == key2->glyph &&
|
|
|
|
key1->scale == key2->scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
static guint
|
|
|
|
glyph_cache_hash (gconstpointer v)
|
|
|
|
{
|
|
|
|
const GlyphCacheKey *key = v;
|
|
|
|
|
|
|
|
return GPOINTER_TO_UINT (key->font) ^ key->glyph ^ key->scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
glyph_cache_key_free (gpointer v)
|
|
|
|
{
|
|
|
|
GlyphCacheKey *f = v;
|
|
|
|
|
|
|
|
g_object_unref (f->font);
|
|
|
|
g_free (f);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
glyph_cache_value_free (gpointer v)
|
|
|
|
{
|
|
|
|
g_free (v);
|
|
|
|
}
|
|
|
|
|
2019-05-20 13:12:15 +00:00
|
|
|
static gboolean
|
2019-06-04 21:27:09 +00:00
|
|
|
render_glyph (GlyphCacheKey *key,
|
|
|
|
GskGLCachedGlyph *value,
|
|
|
|
GskImageRegion *region)
|
2017-11-18 13:30:57 +00:00
|
|
|
{
|
|
|
|
cairo_surface_t *surface;
|
|
|
|
cairo_t *cr;
|
|
|
|
cairo_scaled_font_t *scaled_font;
|
2018-01-06 12:02:31 +00:00
|
|
|
PangoGlyphString glyph_string;
|
|
|
|
PangoGlyphInfo glyph_info;
|
2019-05-20 13:12:15 +00:00
|
|
|
int surface_width, surface_height;
|
2019-06-03 02:46:12 +00:00
|
|
|
int stride;
|
|
|
|
unsigned char *data;
|
2017-11-18 13:30:57 +00:00
|
|
|
|
|
|
|
scaled_font = pango_cairo_font_get_scaled_font ((PangoCairoFont *)key->font);
|
|
|
|
if (G_UNLIKELY (!scaled_font || cairo_scaled_font_status (scaled_font) != CAIRO_STATUS_SUCCESS))
|
2019-06-04 21:27:09 +00:00
|
|
|
{
|
|
|
|
g_warning ("Failed to get a font");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2019-05-20 13:12:15 +00:00
|
|
|
|
|
|
|
surface_width = value->draw_width * key->scale / 1024;
|
|
|
|
surface_height = value->draw_height * key->scale / 1024;
|
2017-11-18 13:30:57 +00:00
|
|
|
|
2019-06-03 02:46:12 +00:00
|
|
|
stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, surface_width);
|
|
|
|
data = g_malloc0 (stride * surface_height);
|
|
|
|
surface = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_ARGB32,
|
|
|
|
surface_width, surface_height,
|
|
|
|
stride);
|
2017-11-18 13:30:57 +00:00
|
|
|
cairo_surface_set_device_scale (surface, key->scale / 1024.0, key->scale / 1024.0);
|
|
|
|
|
|
|
|
cr = cairo_create (surface);
|
|
|
|
|
|
|
|
cairo_set_scaled_font (cr, scaled_font);
|
|
|
|
cairo_set_source_rgba (cr, 1, 1, 1, 1);
|
|
|
|
|
2018-01-06 12:02:31 +00:00
|
|
|
glyph_info.glyph = key->glyph;
|
|
|
|
glyph_info.geometry.width = value->draw_width * 1024;
|
|
|
|
if (key->glyph & PANGO_GLYPH_UNKNOWN_FLAG)
|
|
|
|
glyph_info.geometry.x_offset = 0;
|
|
|
|
else
|
|
|
|
glyph_info.geometry.x_offset = - value->draw_x * 1024;
|
|
|
|
glyph_info.geometry.y_offset = - value->draw_y * 1024;
|
2017-11-18 13:30:57 +00:00
|
|
|
|
2018-01-06 12:02:31 +00:00
|
|
|
glyph_string.num_glyphs = 1;
|
|
|
|
glyph_string.glyphs = &glyph_info;
|
2017-11-18 13:30:57 +00:00
|
|
|
|
2018-01-06 12:02:31 +00:00
|
|
|
pango_cairo_show_glyph_string (cr, key->font, &glyph_string);
|
2017-11-18 13:30:57 +00:00
|
|
|
cairo_destroy (cr);
|
|
|
|
|
2019-02-08 10:01:39 +00:00
|
|
|
cairo_surface_flush (surface);
|
2017-11-18 13:30:57 +00:00
|
|
|
|
|
|
|
region->width = cairo_image_surface_get_width (surface);
|
|
|
|
region->height = cairo_image_surface_get_height (surface);
|
|
|
|
region->stride = cairo_image_surface_get_stride (surface);
|
2019-06-03 02:46:12 +00:00
|
|
|
region->data = data;
|
2019-06-04 21:27:09 +00:00
|
|
|
if (value->atlas)
|
|
|
|
{
|
|
|
|
region->x = (gsize)(value->tx * value->atlas->width);
|
|
|
|
region->y = (gsize)(value->ty * value->atlas->height);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
region->x = 0;
|
|
|
|
region->y = 0;
|
|
|
|
}
|
2019-02-08 10:01:39 +00:00
|
|
|
|
|
|
|
cairo_surface_destroy (surface);
|
2019-06-04 21:27:09 +00:00
|
|
|
|
2019-05-20 13:12:15 +00:00
|
|
|
return TRUE;
|
2017-11-18 13:30:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-06-04 21:27:09 +00:00
|
|
|
upload_glyph (GlyphCacheKey *key,
|
|
|
|
GskGLCachedGlyph *value)
|
2017-11-18 13:30:57 +00:00
|
|
|
{
|
2019-06-04 21:27:09 +00:00
|
|
|
GskImageRegion r;
|
2017-11-18 13:30:57 +00:00
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
gdk_gl_context_push_debug_group_printf (gdk_gl_context_get_current (),
|
|
|
|
"Uploading glyph %d",
|
|
|
|
key->glyph);
|
2019-04-24 11:28:11 +00:00
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
if (render_glyph (key, value, &r))
|
2019-05-20 13:12:15 +00:00
|
|
|
{
|
2019-06-04 21:27:09 +00:00
|
|
|
glBindTexture (GL_TEXTURE_2D, value->texture_id);
|
|
|
|
glTextureSubImage2D (value->texture_id, 0,
|
|
|
|
r.x, r.y, r.width, r.height,
|
|
|
|
GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV,
|
|
|
|
r.data);
|
|
|
|
g_free (r.data);
|
|
|
|
}
|
2017-11-18 13:30:57 +00:00
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
gdk_gl_context_pop_debug_group (gdk_gl_context_get_current ());
|
|
|
|
}
|
2019-04-24 11:28:11 +00:00
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
static void
|
|
|
|
add_to_cache (GskGLGlyphCache *self,
|
|
|
|
GlyphCacheKey *key,
|
|
|
|
GskGLCachedGlyph *value)
|
|
|
|
{
|
|
|
|
const int width = value->draw_width * key->scale / 1024;
|
|
|
|
const int height = value->draw_height * key->scale / 1024;
|
|
|
|
GskGLTextureAtlas *atlas = NULL;
|
|
|
|
int packed_x = 0;
|
|
|
|
int packed_y = 0;
|
|
|
|
|
|
|
|
gsk_gl_texture_atlases_pack (self->atlases, width + 2, height + 2, &atlas, &packed_x, &packed_y);
|
|
|
|
|
|
|
|
value->tx = (float)(packed_x + 1) / atlas->width;
|
|
|
|
value->ty = (float)(packed_y + 1) / atlas->height;
|
|
|
|
value->tw = (float)width / atlas->width;
|
|
|
|
value->th = (float)height / atlas->height;
|
|
|
|
value->used = TRUE;
|
|
|
|
|
|
|
|
value->atlas = atlas;
|
|
|
|
value->texture_id = atlas->texture_id;
|
|
|
|
|
|
|
|
upload_glyph (key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gsk_gl_glyph_cache_get_texture (GskGLDriver *driver,
|
|
|
|
PangoFont *font,
|
|
|
|
PangoGlyph glyph,
|
|
|
|
float scale,
|
|
|
|
GskGLCachedGlyph *value)
|
|
|
|
{
|
|
|
|
PangoRectangle ink_rect;
|
|
|
|
GlyphCacheKey key;
|
|
|
|
int width, height;
|
|
|
|
guint texture_id;
|
|
|
|
|
|
|
|
pango_font_get_glyph_extents (font, glyph, &ink_rect, NULL);
|
|
|
|
pango_extents_to_pixels (&ink_rect, NULL);
|
|
|
|
|
|
|
|
key.font = font;
|
|
|
|
key.glyph = glyph;
|
|
|
|
key.scale = (guint)(scale * 1024);
|
|
|
|
|
|
|
|
value->atlas = NULL;
|
|
|
|
value->timestamp = 0;
|
|
|
|
|
|
|
|
value->draw_x = ink_rect.x;
|
|
|
|
value->draw_y = ink_rect.y;
|
|
|
|
value->draw_width = ink_rect.width;
|
|
|
|
value->draw_height = ink_rect.height;
|
|
|
|
|
|
|
|
value->tx = 0.0f;
|
|
|
|
value->ty = 0.0f;
|
|
|
|
value->tw = 1.0f;
|
|
|
|
value->th = 1.0f;
|
|
|
|
|
|
|
|
width = value->draw_width * key.scale / 1024;
|
|
|
|
height = value->draw_height * key.scale / 1024;
|
|
|
|
|
|
|
|
texture_id = gsk_gl_driver_create_texture (driver, width, height);
|
|
|
|
gsk_gl_driver_bind_source_texture (driver, texture_id);
|
|
|
|
gsk_gl_driver_init_texture_empty (driver, texture_id, GL_NEAREST, GL_NEAREST);
|
2019-02-08 10:01:39 +00:00
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
value->texture_id = texture_id;
|
|
|
|
|
|
|
|
upload_glyph (&key, value);
|
2017-11-18 13:30:57 +00:00
|
|
|
}
|
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
gboolean
|
|
|
|
gsk_gl_glyph_cache_lookup (GskGLGlyphCache *cache,
|
|
|
|
PangoFont *font,
|
|
|
|
PangoGlyph glyph,
|
|
|
|
float scale,
|
|
|
|
GskGLCachedGlyph *cached_glyph_out)
|
2017-11-18 13:30:57 +00:00
|
|
|
{
|
|
|
|
GskGLCachedGlyph *value;
|
|
|
|
|
2018-03-02 16:22:46 +00:00
|
|
|
value = g_hash_table_lookup (cache->hash_table,
|
|
|
|
&(GlyphCacheKey) {
|
|
|
|
.font = font,
|
|
|
|
.glyph = glyph,
|
|
|
|
.scale = (guint)(scale * 1024)
|
|
|
|
});
|
2017-11-18 13:30:57 +00:00
|
|
|
|
|
|
|
if (value)
|
|
|
|
{
|
2019-02-10 09:07:24 +00:00
|
|
|
const guint age = cache->timestamp - value->timestamp;
|
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
if (age > MAX_FRAME_AGE)
|
2017-11-18 13:30:57 +00:00
|
|
|
{
|
2019-05-22 05:33:45 +00:00
|
|
|
GskGLTextureAtlas *atlas = value->atlas;
|
2017-11-18 13:30:57 +00:00
|
|
|
|
2019-05-22 05:33:45 +00:00
|
|
|
if (atlas && !value->used)
|
|
|
|
{
|
|
|
|
gsk_gl_texture_atlas_mark_used (atlas, value->draw_width, value->draw_height);
|
|
|
|
value->used = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
value->timestamp = cache->timestamp;
|
2017-11-18 13:30:57 +00:00
|
|
|
}
|
2019-06-02 20:55:34 +00:00
|
|
|
|
|
|
|
value->timestamp = cache->timestamp;
|
2017-11-18 13:30:57 +00:00
|
|
|
}
|
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
if (value == NULL)
|
2017-11-18 13:30:57 +00:00
|
|
|
{
|
|
|
|
PangoRectangle ink_rect;
|
|
|
|
|
|
|
|
pango_font_get_glyph_extents (font, glyph, &ink_rect, NULL);
|
|
|
|
pango_extents_to_pixels (&ink_rect, NULL);
|
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
value = g_new0 (GskGLCachedGlyph, 1);
|
|
|
|
|
2017-11-18 13:30:57 +00:00
|
|
|
value->draw_x = ink_rect.x;
|
|
|
|
value->draw_y = ink_rect.y;
|
|
|
|
value->draw_width = ink_rect.width;
|
|
|
|
value->draw_height = ink_rect.height;
|
|
|
|
value->timestamp = cache->timestamp;
|
|
|
|
value->atlas = NULL; /* For now */
|
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
if (ink_rect.width < 128 && ink_rect.height < 128)
|
|
|
|
{
|
|
|
|
GlyphCacheKey *key;
|
2017-11-18 13:30:57 +00:00
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
key = g_new0 (GlyphCacheKey, 1);
|
2017-11-18 13:30:57 +00:00
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
key->font = g_object_ref (font);
|
|
|
|
key->glyph = glyph;
|
|
|
|
key->scale = (guint)(scale * 1024);
|
2017-11-18 13:30:57 +00:00
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
if (ink_rect.width > 0 && ink_rect.height > 0 && key->scale > 0)
|
|
|
|
add_to_cache (cache, key, value);
|
2017-11-18 13:30:57 +00:00
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
*cached_glyph_out = *value;
|
|
|
|
g_hash_table_insert (cache->hash_table, key, value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*cached_glyph_out = *value;
|
|
|
|
glyph_cache_value_free (value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2017-11-18 13:30:57 +00:00
|
|
|
{
|
2019-06-04 21:27:09 +00:00
|
|
|
*cached_glyph_out = *value;
|
2017-11-18 13:30:57 +00:00
|
|
|
}
|
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
return cached_glyph_out->atlas != NULL;
|
2017-11-18 13:30:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-06-04 21:27:09 +00:00
|
|
|
gsk_gl_glyph_cache_begin_frame (GskGLGlyphCache *self)
|
2017-11-18 13:30:57 +00:00
|
|
|
{
|
|
|
|
GHashTableIter iter;
|
|
|
|
GlyphCacheKey *key;
|
|
|
|
GskGLCachedGlyph *value;
|
2019-05-22 05:33:45 +00:00
|
|
|
guint dropped = 0;
|
2017-11-18 13:30:57 +00:00
|
|
|
|
|
|
|
self->timestamp++;
|
|
|
|
|
2019-05-22 05:33:45 +00:00
|
|
|
g_hash_table_iter_init (&iter, self->hash_table);
|
|
|
|
while (g_hash_table_iter_next (&iter, (gpointer *)&key, (gpointer *)&value))
|
2019-06-02 22:21:05 +00:00
|
|
|
{
|
2019-06-04 21:27:09 +00:00
|
|
|
guint pos;
|
|
|
|
|
|
|
|
if (!g_ptr_array_find (self->atlases->atlases, value->atlas, &pos))
|
2019-06-02 22:21:05 +00:00
|
|
|
{
|
2019-05-22 05:33:45 +00:00
|
|
|
g_hash_table_iter_remove (&iter);
|
|
|
|
dropped++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const guint age = self->timestamp - value->timestamp;
|
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
if (age > MAX_FRAME_AGE)
|
2019-06-02 22:21:05 +00:00
|
|
|
{
|
2019-05-22 05:33:45 +00:00
|
|
|
GskGLTextureAtlas *atlas = value->atlas;
|
|
|
|
|
|
|
|
if (atlas && value->used)
|
|
|
|
{
|
|
|
|
gsk_gl_texture_atlas_mark_unused (atlas, value->draw_width, value->draw_height);
|
|
|
|
value->used = FALSE;
|
|
|
|
}
|
2019-06-02 22:21:05 +00:00
|
|
|
}
|
2017-11-18 13:30:57 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-02 22:22:17 +00:00
|
|
|
|
2019-06-03 19:00:17 +00:00
|
|
|
GSK_NOTE(GLYPH_CACHE, if (dropped > 0) g_message ("Dropped %d glyphs", dropped));
|
2017-11-18 13:30:57 +00:00
|
|
|
}
|