2017-09-19 23:32:04 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gskvulkanglyphcacheprivate.h"
|
|
|
|
|
|
|
|
#include "gskvulkanimageprivate.h"
|
|
|
|
#include "gskdebugprivate.h"
|
|
|
|
#include "gskprivate.h"
|
2018-01-14 14:52:52 +00:00
|
|
|
#include "gskrendererprivate.h"
|
2017-09-19 23:32:04 +00:00
|
|
|
|
|
|
|
#include <graphene.h>
|
|
|
|
|
2017-09-20 21:52:40 +00:00
|
|
|
/* Parameters for our cache eviction strategy.
|
|
|
|
*
|
|
|
|
* 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_AGE frames are considered old. We keep
|
|
|
|
* count of the pixels of each atlas that are taken up by old glyphs. We check the
|
|
|
|
* fraction of old pixels every CHECK_INTERVAL frames, and if it is above MAX_OLD, then
|
|
|
|
* we drop the atlas an all the glyphs contained in it from the cache.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MAX_AGE 60
|
|
|
|
#define CHECK_INTERVAL 10
|
|
|
|
#define MAX_OLD 0.333
|
|
|
|
|
|
|
|
|
2017-09-20 03:32:07 +00:00
|
|
|
typedef struct {
|
|
|
|
GskVulkanImage *image;
|
2017-09-19 23:32:04 +00:00
|
|
|
int width, height;
|
|
|
|
int x, y, y0;
|
2017-09-20 03:59:44 +00:00
|
|
|
int num_glyphs;
|
2017-09-20 14:02:29 +00:00
|
|
|
GList *dirty_glyphs;
|
2017-09-20 21:52:40 +00:00
|
|
|
guint old_pixels;
|
2017-09-20 03:32:07 +00:00
|
|
|
} Atlas;
|
2017-09-19 23:32:04 +00:00
|
|
|
|
2017-09-20 03:32:07 +00:00
|
|
|
struct _GskVulkanGlyphCache {
|
|
|
|
GObject parent_instance;
|
|
|
|
|
2017-09-20 14:01:58 +00:00
|
|
|
GdkVulkanContext *vulkan;
|
2018-01-14 14:52:52 +00:00
|
|
|
GskRenderer *renderer;
|
2017-09-20 14:01:58 +00:00
|
|
|
|
2017-09-20 03:32:07 +00:00
|
|
|
GHashTable *hash_table;
|
|
|
|
GPtrArray *atlases;
|
2017-09-20 21:52:40 +00:00
|
|
|
|
|
|
|
guint64 timestamp;
|
2017-09-19 23:32:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _GskVulkanGlyphCacheClass {
|
|
|
|
GObjectClass parent_class;
|
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (GskVulkanGlyphCache, gsk_vulkan_glyph_cache, G_TYPE_OBJECT)
|
|
|
|
|
|
|
|
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);
|
2017-09-21 17:45:01 +00:00
|
|
|
static void dirty_glyph_free (gpointer v);
|
2017-09-19 23:32:04 +00:00
|
|
|
|
2017-09-20 03:32:07 +00:00
|
|
|
static Atlas *
|
2017-09-20 14:02:29 +00:00
|
|
|
create_atlas (GskVulkanGlyphCache *cache)
|
2017-09-20 03:32:07 +00:00
|
|
|
{
|
|
|
|
Atlas *atlas;
|
|
|
|
|
2017-09-20 21:52:40 +00:00
|
|
|
atlas = g_new0 (Atlas, 1);
|
2017-09-20 03:32:07 +00:00
|
|
|
atlas->width = 512;
|
|
|
|
atlas->height = 512;
|
|
|
|
atlas->y0 = 1;
|
|
|
|
atlas->y = 1;
|
|
|
|
atlas->x = 1;
|
|
|
|
atlas->image = NULL;
|
2017-09-20 03:59:44 +00:00
|
|
|
atlas->num_glyphs = 0;
|
2017-09-20 14:02:29 +00:00
|
|
|
atlas->dirty_glyphs = NULL;
|
2017-09-20 03:32:07 +00:00
|
|
|
|
|
|
|
return atlas;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_atlas (gpointer v)
|
|
|
|
{
|
|
|
|
Atlas *atlas = v;
|
|
|
|
|
|
|
|
g_clear_object (&atlas->image);
|
2017-09-21 17:45:01 +00:00
|
|
|
g_list_free_full (atlas->dirty_glyphs, dirty_glyph_free);
|
2017-09-20 03:32:07 +00:00
|
|
|
g_free (atlas);
|
|
|
|
}
|
|
|
|
|
2017-09-19 23:32:04 +00:00
|
|
|
static void
|
|
|
|
gsk_vulkan_glyph_cache_init (GskVulkanGlyphCache *cache)
|
|
|
|
{
|
|
|
|
cache->hash_table = g_hash_table_new_full (glyph_cache_hash, glyph_cache_equal,
|
|
|
|
glyph_cache_key_free, glyph_cache_value_free);
|
2017-09-20 03:32:07 +00:00
|
|
|
cache->atlases = g_ptr_array_new_with_free_func (free_atlas);
|
2017-09-19 23:32:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_vulkan_glyph_cache_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
GskVulkanGlyphCache *cache = GSK_VULKAN_GLYPH_CACHE (object);
|
|
|
|
|
2017-09-20 03:32:07 +00:00
|
|
|
g_ptr_array_unref (cache->atlases);
|
2017-09-20 14:02:29 +00:00
|
|
|
g_hash_table_unref (cache->hash_table);
|
2017-09-19 23:32:04 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (gsk_vulkan_glyph_cache_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_vulkan_glyph_cache_class_init (GskVulkanGlyphCacheClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
object_class->finalize = gsk_vulkan_glyph_cache_finalize;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PangoFont *font;
|
|
|
|
PangoGlyph glyph;
|
2019-07-25 21:50:31 +00:00
|
|
|
guint xshift;
|
|
|
|
guint yshift;
|
2017-10-28 17:13:31 +00:00
|
|
|
guint scale; /* times 1024 */
|
2017-09-19 23:32:04 +00:00
|
|
|
} GlyphCacheKey;
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
glyph_cache_equal (gconstpointer v1, gconstpointer v2)
|
|
|
|
{
|
|
|
|
const GlyphCacheKey *key1 = v1;
|
|
|
|
const GlyphCacheKey *key2 = v2;
|
|
|
|
|
|
|
|
return key1->font == key2->font &&
|
2017-10-28 17:13:31 +00:00
|
|
|
key1->glyph == key2->glyph &&
|
2019-07-25 21:50:31 +00:00
|
|
|
key1->xshift == key2->xshift &&
|
|
|
|
key1->yshift == key2->yshift &&
|
2017-10-28 17:13:31 +00:00
|
|
|
key1->scale == key2->scale;
|
2017-09-19 23:32:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static guint
|
|
|
|
glyph_cache_hash (gconstpointer v)
|
|
|
|
{
|
|
|
|
const GlyphCacheKey *key = v;
|
|
|
|
|
2019-07-25 21:50:31 +00:00
|
|
|
return GPOINTER_TO_UINT (key->font) ^ key->glyph ^ (key->xshift << 24) ^ (key->yshift << 26) ^ key->scale;
|
2017-09-19 23:32:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-09-20 14:02:29 +00:00
|
|
|
typedef struct {
|
|
|
|
GlyphCacheKey *key;
|
|
|
|
GskVulkanCachedGlyph *value;
|
2017-09-21 17:45:01 +00:00
|
|
|
cairo_surface_t *surface;
|
2017-09-20 14:02:29 +00:00
|
|
|
} DirtyGlyph;
|
|
|
|
|
2017-09-21 17:45:01 +00:00
|
|
|
static void
|
|
|
|
dirty_glyph_free (gpointer v)
|
|
|
|
{
|
|
|
|
DirtyGlyph *glyph = v;
|
|
|
|
|
|
|
|
if (glyph->surface)
|
|
|
|
cairo_surface_destroy (glyph->surface);
|
|
|
|
g_free (glyph);
|
|
|
|
}
|
|
|
|
|
2017-09-19 23:32:04 +00:00
|
|
|
static void
|
|
|
|
add_to_cache (GskVulkanGlyphCache *cache,
|
2017-09-20 14:02:29 +00:00
|
|
|
GlyphCacheKey *key,
|
2017-09-19 23:32:04 +00:00
|
|
|
GskVulkanCachedGlyph *value)
|
|
|
|
{
|
2017-09-20 03:32:07 +00:00
|
|
|
Atlas *atlas;
|
|
|
|
int i;
|
2017-09-20 14:02:29 +00:00
|
|
|
DirtyGlyph *dirty;
|
2017-10-28 17:13:31 +00:00
|
|
|
int width = value->draw_width * key->scale / 1024;
|
|
|
|
int height = value->draw_height * key->scale / 1024;
|
2017-09-19 23:32:04 +00:00
|
|
|
|
2017-09-20 03:32:07 +00:00
|
|
|
for (i = 0; i < cache->atlases->len; i++)
|
2017-09-19 23:32:04 +00:00
|
|
|
{
|
2017-09-20 03:32:07 +00:00
|
|
|
int x, y, y0;
|
|
|
|
|
|
|
|
atlas = g_ptr_array_index (cache->atlases, i);
|
|
|
|
x = atlas->x;
|
|
|
|
y = atlas->y;
|
|
|
|
y0 = atlas->y0;
|
|
|
|
|
2017-10-28 17:13:31 +00:00
|
|
|
if (atlas->x + width + 1 >= atlas->width)
|
2017-09-20 03:32:07 +00:00
|
|
|
{
|
|
|
|
/* start a new row */
|
|
|
|
y0 = y + 1;
|
|
|
|
x = 1;
|
|
|
|
}
|
|
|
|
|
2017-10-28 17:13:31 +00:00
|
|
|
if (y0 + height + 1 >= atlas->height)
|
2017-09-20 03:32:07 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
atlas->y0 = y0;
|
|
|
|
atlas->x = x;
|
|
|
|
atlas->y = y;
|
|
|
|
break;
|
2017-09-19 23:32:04 +00:00
|
|
|
}
|
|
|
|
|
2017-09-20 03:32:07 +00:00
|
|
|
if (i == cache->atlases->len)
|
2017-09-19 23:32:04 +00:00
|
|
|
{
|
2017-09-20 14:02:29 +00:00
|
|
|
atlas = create_atlas (cache);
|
2017-09-20 03:32:07 +00:00
|
|
|
g_ptr_array_add (cache->atlases, atlas);
|
2017-09-19 23:32:04 +00:00
|
|
|
}
|
|
|
|
|
2017-09-20 14:02:29 +00:00
|
|
|
value->tx = (float)atlas->x / atlas->width;
|
|
|
|
value->ty = (float)atlas->y0 / atlas->height;
|
2017-10-28 17:13:31 +00:00
|
|
|
value->tw = (float)width / atlas->width;
|
|
|
|
value->th = (float)height / atlas->height;
|
2017-09-19 23:32:04 +00:00
|
|
|
|
2017-09-20 14:02:29 +00:00
|
|
|
value->texture_index = i;
|
2017-09-19 23:32:04 +00:00
|
|
|
|
2017-09-20 14:02:29 +00:00
|
|
|
dirty = g_new (DirtyGlyph, 1);
|
|
|
|
dirty->key = key;
|
|
|
|
dirty->value = value;
|
|
|
|
atlas->dirty_glyphs = g_list_prepend (atlas->dirty_glyphs, dirty);
|
2017-09-19 23:32:04 +00:00
|
|
|
|
2017-10-28 17:13:31 +00:00
|
|
|
atlas->x = atlas->x + width + 1;
|
|
|
|
atlas->y = MAX (atlas->y, atlas->y0 + height + 1);
|
2017-09-19 23:32:04 +00:00
|
|
|
|
2017-09-20 03:59:44 +00:00
|
|
|
atlas->num_glyphs++;
|
|
|
|
|
|
|
|
#ifdef G_ENABLE_DEBUG
|
2018-01-14 14:52:52 +00:00
|
|
|
if (GSK_RENDERER_DEBUG_CHECK (cache->renderer, GLYPH_CACHE))
|
2017-09-20 03:59:44 +00:00
|
|
|
{
|
|
|
|
g_print ("Glyph cache:\n");
|
|
|
|
for (i = 0; i < cache->atlases->len; i++)
|
|
|
|
{
|
|
|
|
atlas = g_ptr_array_index (cache->atlases, i);
|
2017-09-20 21:52:40 +00:00
|
|
|
g_print ("\tAtlas %d (%dx%d): %d glyphs (%d dirty), %.2g%% old pixels, filled to %d, %d / %d\n",
|
2017-09-20 14:02:29 +00:00
|
|
|
i, atlas->width, atlas->height,
|
|
|
|
atlas->num_glyphs, g_list_length (atlas->dirty_glyphs),
|
2017-09-20 21:52:40 +00:00
|
|
|
100.0 * (double)atlas->old_pixels / (double)(atlas->width * atlas->height),
|
2017-09-20 03:59:44 +00:00
|
|
|
atlas->x, atlas->y0, atlas->y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2017-09-19 23:32:04 +00:00
|
|
|
}
|
|
|
|
|
2017-09-20 14:02:29 +00:00
|
|
|
static void
|
2017-09-21 17:45:01 +00:00
|
|
|
render_glyph (Atlas *atlas,
|
|
|
|
DirtyGlyph *glyph,
|
|
|
|
GskImageRegion *region)
|
2017-09-20 14:02:29 +00:00
|
|
|
{
|
2017-09-21 17:45:01 +00:00
|
|
|
GlyphCacheKey *key = glyph->key;
|
|
|
|
GskVulkanCachedGlyph *value = glyph->value;
|
2017-09-20 14:02:29 +00:00
|
|
|
cairo_surface_t *surface;
|
|
|
|
cairo_t *cr;
|
2018-01-06 01:30:14 +00:00
|
|
|
PangoGlyphString glyphs;
|
|
|
|
PangoGlyphInfo gi;
|
2017-09-20 14:02:29 +00:00
|
|
|
|
|
|
|
surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
|
2017-10-28 17:13:31 +00:00
|
|
|
value->draw_width * key->scale / 1024,
|
|
|
|
value->draw_height * key->scale / 1024);
|
|
|
|
cairo_surface_set_device_scale (surface, key->scale / 1024.0, key->scale / 1024.0);
|
2017-09-20 14:02:29 +00:00
|
|
|
|
|
|
|
cr = cairo_create (surface);
|
|
|
|
cairo_set_source_rgba (cr, 1, 1, 1, 1);
|
|
|
|
|
2018-01-06 01:30:14 +00:00
|
|
|
gi.glyph = key->glyph;
|
|
|
|
gi.geometry.width = value->draw_width * 1024;
|
|
|
|
if (key->glyph & PANGO_GLYPH_UNKNOWN_FLAG)
|
2019-07-25 21:50:31 +00:00
|
|
|
gi.geometry.x_offset = key->xshift * 256;
|
2018-01-06 01:30:14 +00:00
|
|
|
else
|
2019-07-25 21:50:31 +00:00
|
|
|
gi.geometry.x_offset = key->xshift * 256 - value->draw_x * 1024;
|
|
|
|
gi.geometry.y_offset = key->yshift * 256 - value->draw_y * 1024;
|
2018-01-06 01:30:14 +00:00
|
|
|
|
|
|
|
glyphs.num_glyphs = 1;
|
|
|
|
glyphs.glyphs = &gi;
|
2017-09-20 14:02:29 +00:00
|
|
|
|
2018-01-06 01:30:14 +00:00
|
|
|
pango_cairo_show_glyph_string (cr, key->font, &glyphs);
|
2017-09-20 14:02:29 +00:00
|
|
|
|
|
|
|
cairo_destroy (cr);
|
|
|
|
|
2017-09-21 17:45:01 +00:00
|
|
|
glyph->surface = surface;
|
|
|
|
|
|
|
|
region->data = cairo_image_surface_get_data (surface);
|
|
|
|
region->width = cairo_image_surface_get_width (surface);
|
|
|
|
region->height = cairo_image_surface_get_height (surface);
|
|
|
|
region->stride = cairo_image_surface_get_stride (surface);
|
|
|
|
region->x = (gsize)(value->tx * atlas->width);
|
|
|
|
region->y = (gsize)(value->ty * atlas->height);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-01-14 14:52:52 +00:00
|
|
|
upload_dirty_glyphs (GskVulkanGlyphCache *cache,
|
|
|
|
Atlas *atlas,
|
|
|
|
GskVulkanUploader *uploader)
|
2017-09-21 17:45:01 +00:00
|
|
|
{
|
|
|
|
GList *l;
|
|
|
|
guint num_regions;
|
|
|
|
GskImageRegion *regions;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
num_regions = g_list_length (atlas->dirty_glyphs);
|
|
|
|
regions = alloca (sizeof (GskImageRegion) * num_regions);
|
|
|
|
|
|
|
|
for (l = atlas->dirty_glyphs, i = 0; l; l = l->next, i++)
|
|
|
|
render_glyph (atlas, (DirtyGlyph *)l->data, ®ions[i]);
|
|
|
|
|
2018-01-14 14:52:52 +00:00
|
|
|
GSK_RENDERER_NOTE (cache->renderer, GLYPH_CACHE,
|
|
|
|
g_message ("uploading %d glyphs to cache", num_regions));
|
2017-09-20 14:02:29 +00:00
|
|
|
|
2017-09-21 17:45:01 +00:00
|
|
|
gsk_vulkan_image_upload_regions (atlas->image, uploader, num_regions, regions);
|
|
|
|
|
|
|
|
g_list_free_full (atlas->dirty_glyphs, dirty_glyph_free);
|
|
|
|
atlas->dirty_glyphs = NULL;
|
2017-09-20 14:02:29 +00:00
|
|
|
}
|
|
|
|
|
2017-09-19 23:32:04 +00:00
|
|
|
GskVulkanGlyphCache *
|
2018-01-14 14:52:52 +00:00
|
|
|
gsk_vulkan_glyph_cache_new (GskRenderer *renderer,
|
|
|
|
GdkVulkanContext *vulkan)
|
2017-09-19 23:32:04 +00:00
|
|
|
{
|
2017-09-20 14:01:58 +00:00
|
|
|
GskVulkanGlyphCache *cache;
|
|
|
|
|
|
|
|
cache = GSK_VULKAN_GLYPH_CACHE (g_object_new (GSK_TYPE_VULKAN_GLYPH_CACHE, NULL));
|
2018-01-14 14:52:52 +00:00
|
|
|
cache->renderer = renderer;
|
2017-09-20 14:01:58 +00:00
|
|
|
cache->vulkan = vulkan;
|
2017-09-20 14:02:29 +00:00
|
|
|
g_ptr_array_add (cache->atlases, create_atlas (cache));
|
2017-09-20 14:01:58 +00:00
|
|
|
|
|
|
|
return cache;
|
2017-09-19 23:32:04 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 21:50:31 +00:00
|
|
|
#define PHASE(x) ((x % PANGO_SCALE) * 4 / PANGO_SCALE)
|
|
|
|
|
2017-09-19 23:32:04 +00:00
|
|
|
GskVulkanCachedGlyph *
|
|
|
|
gsk_vulkan_glyph_cache_lookup (GskVulkanGlyphCache *cache,
|
|
|
|
gboolean create,
|
|
|
|
PangoFont *font,
|
2017-10-28 17:13:31 +00:00
|
|
|
PangoGlyph glyph,
|
2019-07-25 21:50:31 +00:00
|
|
|
int x,
|
|
|
|
int y,
|
2017-10-28 17:13:31 +00:00
|
|
|
float scale)
|
2017-09-19 23:32:04 +00:00
|
|
|
{
|
|
|
|
GlyphCacheKey lookup_key;
|
|
|
|
GskVulkanCachedGlyph *value;
|
2019-07-25 21:50:31 +00:00
|
|
|
guint xshift;
|
|
|
|
guint yshift;
|
|
|
|
|
|
|
|
xshift = PHASE (x);
|
|
|
|
yshift = PHASE (y);
|
2017-09-19 23:32:04 +00:00
|
|
|
|
|
|
|
lookup_key.font = font;
|
|
|
|
lookup_key.glyph = glyph;
|
2019-07-25 21:50:31 +00:00
|
|
|
lookup_key.xshift = xshift;
|
|
|
|
lookup_key.yshift = yshift;
|
2017-10-28 17:13:31 +00:00
|
|
|
lookup_key.scale = (guint)(scale * 1024);
|
2017-09-19 23:32:04 +00:00
|
|
|
|
|
|
|
value = g_hash_table_lookup (cache->hash_table, &lookup_key);
|
|
|
|
|
2017-09-20 21:52:40 +00:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
if (cache->timestamp - value->timestamp >= MAX_AGE)
|
|
|
|
{
|
|
|
|
Atlas *atlas = g_ptr_array_index (cache->atlases, value->texture_index);
|
|
|
|
|
|
|
|
atlas->old_pixels -= value->draw_width * value->draw_height;
|
|
|
|
value->timestamp = cache->timestamp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-19 23:32:04 +00:00
|
|
|
if (create && value == NULL)
|
|
|
|
{
|
|
|
|
GlyphCacheKey *key;
|
|
|
|
PangoRectangle ink_rect;
|
|
|
|
|
2017-09-20 14:02:29 +00:00
|
|
|
key = g_new (GlyphCacheKey, 1);
|
2017-09-20 03:32:07 +00:00
|
|
|
value = g_new0 (GskVulkanCachedGlyph, 1);
|
2017-09-19 23:32:04 +00:00
|
|
|
|
|
|
|
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;
|
2017-09-20 21:52:40 +00:00
|
|
|
value->timestamp = cache->timestamp;
|
2017-09-19 23:32:04 +00:00
|
|
|
|
|
|
|
key->font = g_object_ref (font);
|
|
|
|
key->glyph = glyph;
|
2019-07-25 21:50:31 +00:00
|
|
|
key->xshift = xshift;
|
|
|
|
key->yshift = yshift;
|
2017-10-28 17:13:31 +00:00
|
|
|
key->scale = (guint)(scale * 1024);
|
2017-09-19 23:32:04 +00:00
|
|
|
|
2017-09-20 14:02:29 +00:00
|
|
|
if (ink_rect.width > 0 && ink_rect.height > 0)
|
|
|
|
add_to_cache (cache, key, value);
|
|
|
|
|
2017-09-19 23:32:04 +00:00
|
|
|
g_hash_table_insert (cache->hash_table, key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
GskVulkanImage *
|
|
|
|
gsk_vulkan_glyph_cache_get_glyph_image (GskVulkanGlyphCache *cache,
|
|
|
|
GskVulkanUploader *uploader,
|
|
|
|
guint index)
|
|
|
|
{
|
2017-09-20 03:32:07 +00:00
|
|
|
Atlas *atlas;
|
|
|
|
|
|
|
|
g_return_val_if_fail (index < cache->atlases->len, NULL);
|
|
|
|
|
|
|
|
atlas = g_ptr_array_index (cache->atlases, index);
|
|
|
|
|
|
|
|
if (atlas->image == NULL)
|
2017-09-20 23:12:16 +00:00
|
|
|
atlas->image = gsk_vulkan_image_new_for_atlas (cache->vulkan, atlas->width, atlas->height);
|
2017-09-20 14:02:29 +00:00
|
|
|
|
2017-09-21 17:45:01 +00:00
|
|
|
if (atlas->dirty_glyphs)
|
2018-01-14 14:52:52 +00:00
|
|
|
upload_dirty_glyphs (cache, atlas, uploader);
|
2017-09-20 03:32:07 +00:00
|
|
|
|
|
|
|
return atlas->image;
|
2017-09-19 23:32:04 +00:00
|
|
|
}
|
2017-09-20 21:52:40 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
gsk_vulkan_glyph_cache_begin_frame (GskVulkanGlyphCache *cache)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
guint *drops;
|
|
|
|
guint *shifts;
|
|
|
|
guint len;
|
|
|
|
GHashTableIter iter;
|
|
|
|
GlyphCacheKey *key;
|
|
|
|
GskVulkanCachedGlyph *value;
|
|
|
|
guint dropped = 0;
|
|
|
|
|
|
|
|
cache->timestamp++;
|
|
|
|
|
|
|
|
if (cache->timestamp % CHECK_INTERVAL != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
len = cache->atlases->len;
|
|
|
|
|
|
|
|
/* look for glyphs that have grown old since last time */
|
|
|
|
g_hash_table_iter_init (&iter, cache->hash_table);
|
|
|
|
while (g_hash_table_iter_next (&iter, (gpointer *)&key, (gpointer *)&value))
|
|
|
|
{
|
|
|
|
guint age;
|
|
|
|
|
|
|
|
age = cache->timestamp - value->timestamp;
|
|
|
|
if (MAX_AGE <= age && age < MAX_AGE + CHECK_INTERVAL)
|
|
|
|
{
|
|
|
|
Atlas *atlas = g_ptr_array_index (cache->atlases, value->texture_index);
|
|
|
|
atlas->old_pixels += value->draw_width * value->draw_height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
drops = g_alloca (sizeof (guint) * len);
|
|
|
|
shifts = g_alloca (sizeof (guint) * len);
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
drops[i] = 0;
|
|
|
|
shifts[i] = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* look for atlases to drop, and create a mapping of updated texture indices */
|
|
|
|
for (i = cache->atlases->len - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
Atlas *atlas = g_ptr_array_index (cache->atlases, i);
|
|
|
|
|
|
|
|
if (atlas->old_pixels > MAX_OLD * atlas->width * atlas->height)
|
|
|
|
{
|
2018-01-14 14:52:52 +00:00
|
|
|
GSK_RENDERER_NOTE(cache->renderer, GLYPH_CACHE,
|
|
|
|
g_message ("Dropping atlas %d (%g.2%% old)", i, 100.0 * (double)atlas->old_pixels / (double)(atlas->width * atlas->height)));
|
2017-09-20 21:52:40 +00:00
|
|
|
g_ptr_array_remove_index (cache->atlases, i);
|
|
|
|
|
|
|
|
drops[i] = 1;
|
|
|
|
for (j = i; j + 1 < len; j++)
|
|
|
|
shifts[j + 1] = shifts[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* no atlas dropped, we're done */
|
|
|
|
if (len == cache->atlases->len)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* purge glyphs and update texture indices */
|
|
|
|
g_hash_table_iter_init (&iter, cache->hash_table);
|
|
|
|
|
|
|
|
while (g_hash_table_iter_next (&iter, (gpointer *)&key, (gpointer *)&value))
|
|
|
|
{
|
|
|
|
if (drops[value->texture_index])
|
|
|
|
{
|
|
|
|
dropped++;
|
|
|
|
g_hash_table_iter_remove (&iter);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
value->texture_index = shifts[value->texture_index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-14 14:52:52 +00:00
|
|
|
GSK_RENDERER_NOTE(cache->renderer, GLYPH_CACHE, g_message ("Dropped %d glyphs", dropped));
|
2017-09-20 21:52:40 +00:00
|
|
|
}
|