2023-08-12 20:10:16 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gskgpudeviceprivate.h"
|
|
|
|
|
2023-09-06 00:11:39 +00:00
|
|
|
#include "gskgpuframeprivate.h"
|
|
|
|
#include "gskgpuuploadopprivate.h"
|
|
|
|
|
2023-08-12 20:10:16 +00:00
|
|
|
#include "gdk/gdkdisplayprivate.h"
|
2023-09-01 10:11:06 +00:00
|
|
|
#include "gdk/gdktextureprivate.h"
|
|
|
|
|
2023-09-25 19:12:20 +00:00
|
|
|
#define MAX_SLICES_PER_ATLAS 64
|
|
|
|
|
|
|
|
#define ATLAS_SIZE 1024
|
|
|
|
|
|
|
|
#define MAX_ATLAS_ITEM_SIZE 256
|
|
|
|
|
2023-11-14 10:54:17 +00:00
|
|
|
typedef struct _GskGpuCached GskGpuCached;
|
|
|
|
typedef struct _GskGpuCachedClass GskGpuCachedClass;
|
|
|
|
typedef struct _GskGpuCachedAtlas GskGpuCachedAtlas;
|
|
|
|
typedef struct _GskGpuCachedGlyph GskGpuCachedGlyph;
|
|
|
|
typedef struct _GskGpuCachedTexture GskGpuCachedTexture;
|
|
|
|
typedef struct _GskGpuDevicePrivate GskGpuDevicePrivate;
|
|
|
|
|
|
|
|
struct _GskGpuDevicePrivate
|
2023-09-01 10:11:06 +00:00
|
|
|
{
|
2023-11-14 10:54:17 +00:00
|
|
|
GdkDisplay *display;
|
|
|
|
gsize max_image_size;
|
2023-09-01 10:11:06 +00:00
|
|
|
|
2023-11-14 10:54:17 +00:00
|
|
|
GskGpuCached *first_cached;
|
|
|
|
GskGpuCached *last_cached;
|
|
|
|
guint cache_gc_source;
|
|
|
|
|
|
|
|
GHashTable *texture_cache;
|
|
|
|
GHashTable *glyph_cache;
|
|
|
|
|
|
|
|
GskGpuCachedAtlas *current_atlas;
|
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_PRIVATE (GskGpuDevice, gsk_gpu_device, G_TYPE_OBJECT)
|
|
|
|
|
|
|
|
/* {{{ Cached base class */
|
|
|
|
|
|
|
|
struct _GskGpuCachedClass
|
|
|
|
{
|
|
|
|
gsize size;
|
2023-09-01 10:11:06 +00:00
|
|
|
|
2023-11-14 10:54:17 +00:00
|
|
|
void (* free) (GskGpuDevice *device,
|
|
|
|
GskGpuCached *cached);
|
|
|
|
gboolean (* should_collect) (GskGpuDevice *device,
|
|
|
|
GskGpuCached *cached,
|
|
|
|
gint64 timestsamp);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _GskGpuCached
|
2023-09-01 10:11:06 +00:00
|
|
|
{
|
2023-11-14 10:54:17 +00:00
|
|
|
const GskGpuCachedClass *class;
|
|
|
|
|
|
|
|
GskGpuCachedAtlas *atlas;
|
|
|
|
GskGpuCached *next;
|
|
|
|
GskGpuCached *prev;
|
2023-09-01 10:11:06 +00:00
|
|
|
};
|
|
|
|
|
2023-11-14 10:54:17 +00:00
|
|
|
static void
|
|
|
|
gsk_gpu_cached_free (GskGpuDevice *device,
|
|
|
|
GskGpuCached *cached)
|
|
|
|
{
|
|
|
|
GskGpuDevicePrivate *priv = gsk_gpu_device_get_instance_private (device);
|
|
|
|
|
|
|
|
if (cached->next)
|
|
|
|
cached->next->prev = cached->prev;
|
|
|
|
else
|
|
|
|
priv->last_cached = cached->prev;
|
|
|
|
if (cached->prev)
|
|
|
|
cached->prev->next = cached->next;
|
|
|
|
else
|
|
|
|
priv->first_cached = cached->next;
|
|
|
|
|
|
|
|
cached->class->free (device, cached);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gsk_gpu_cached_should_collect (GskGpuDevice *device,
|
|
|
|
GskGpuCached *cached,
|
|
|
|
gint64 timestamp)
|
|
|
|
{
|
|
|
|
return cached->class->should_collect (device, cached, timestamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
gsk_gpu_cached_new (GskGpuDevice *device,
|
|
|
|
const GskGpuCachedClass *class,
|
|
|
|
GskGpuCachedAtlas *atlas)
|
|
|
|
{
|
|
|
|
GskGpuDevicePrivate *priv = gsk_gpu_device_get_instance_private (device);
|
|
|
|
GskGpuCached *cached;
|
|
|
|
|
|
|
|
cached = g_malloc0 (class->size);
|
|
|
|
|
|
|
|
cached->class = class;
|
|
|
|
cached->atlas = atlas;
|
|
|
|
|
|
|
|
cached->prev = priv->last_cached;
|
|
|
|
priv->last_cached = cached;
|
|
|
|
if (cached->prev)
|
|
|
|
cached->prev->next = cached;
|
|
|
|
else
|
|
|
|
priv->first_cached = cached;
|
|
|
|
|
|
|
|
return cached;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_gpu_cached_use (GskGpuDevice *device,
|
|
|
|
GskGpuCached *cached,
|
|
|
|
gint64 timestamp)
|
|
|
|
{
|
|
|
|
/* FIXME */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
/* {{{ CachedAtlas */
|
|
|
|
|
2023-09-25 19:12:20 +00:00
|
|
|
struct _GskGpuCachedAtlas
|
|
|
|
{
|
2023-11-14 10:54:17 +00:00
|
|
|
GskGpuCached parent;
|
|
|
|
|
2023-09-25 19:12:20 +00:00
|
|
|
GskGpuImage *image;
|
|
|
|
|
|
|
|
gsize n_slices;
|
|
|
|
struct {
|
|
|
|
gsize width;
|
|
|
|
gsize height;
|
|
|
|
} slices[MAX_SLICES_PER_ATLAS];
|
|
|
|
};
|
|
|
|
|
2023-11-14 10:54:17 +00:00
|
|
|
static void
|
|
|
|
gsk_gpu_cached_atlas_free (GskGpuDevice *device,
|
|
|
|
GskGpuCached *cached)
|
|
|
|
{
|
|
|
|
GskGpuCachedAtlas *self = (GskGpuCachedAtlas *) cached;
|
|
|
|
|
|
|
|
g_object_unref (self->image);
|
|
|
|
|
|
|
|
g_free (self);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gsk_gpu_cached_atlas_should_collect (GskGpuDevice *device,
|
|
|
|
GskGpuCached *cached,
|
|
|
|
gint64 timestamp)
|
|
|
|
{
|
|
|
|
/* FIXME */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const GskGpuCachedClass GSK_GPU_CACHED_ATLAS_CLASS =
|
|
|
|
{
|
|
|
|
sizeof (GskGpuCachedAtlas),
|
|
|
|
gsk_gpu_cached_atlas_free,
|
|
|
|
gsk_gpu_cached_atlas_should_collect
|
|
|
|
};
|
|
|
|
|
|
|
|
static GskGpuCachedAtlas *
|
|
|
|
gsk_gpu_cached_atlas_new (GskGpuDevice *device)
|
|
|
|
{
|
|
|
|
GskGpuCachedAtlas *self;
|
|
|
|
|
|
|
|
self = gsk_gpu_cached_new (device, &GSK_GPU_CACHED_ATLAS_CLASS, NULL);
|
|
|
|
self->image = GSK_GPU_DEVICE_GET_CLASS (device)->create_atlas_image (device, ATLAS_SIZE, ATLAS_SIZE);
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
/* {{{ CachedTexture */
|
|
|
|
|
2023-09-01 10:11:06 +00:00
|
|
|
struct _GskGpuCachedTexture
|
|
|
|
{
|
2023-11-14 10:54:17 +00:00
|
|
|
GskGpuCached parent;
|
|
|
|
|
2023-09-01 10:11:06 +00:00
|
|
|
/* atomic */ GdkTexture *texture;
|
|
|
|
GskGpuImage *image;
|
|
|
|
};
|
|
|
|
|
2023-11-14 10:54:17 +00:00
|
|
|
static void
|
|
|
|
gsk_gpu_cached_texture_free (GskGpuDevice *device,
|
|
|
|
GskGpuCached *cached)
|
|
|
|
{
|
|
|
|
GskGpuCachedTexture *self = (GskGpuCachedTexture *) cached;
|
|
|
|
gboolean texture_still_alive;
|
|
|
|
|
|
|
|
texture_still_alive = g_atomic_pointer_exchange (&self->texture, NULL) != NULL;
|
|
|
|
g_object_unref (self->image);
|
|
|
|
|
|
|
|
if (!texture_still_alive)
|
|
|
|
g_free (self);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gsk_gpu_cached_texture_should_collect (GskGpuDevice *device,
|
|
|
|
GskGpuCached *cached,
|
|
|
|
gint64 timestamp)
|
|
|
|
{
|
|
|
|
/* FIXME */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const GskGpuCachedClass GSK_GPU_CACHED_TEXTURE_CLASS =
|
|
|
|
{
|
|
|
|
sizeof (GskGpuCachedTexture),
|
|
|
|
gsk_gpu_cached_texture_free,
|
|
|
|
gsk_gpu_cached_texture_should_collect
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_gpu_cached_texture_destroy_cb (gpointer data)
|
|
|
|
{
|
|
|
|
GskGpuCachedTexture *cache = data;
|
|
|
|
gboolean cache_still_alive;
|
|
|
|
|
|
|
|
cache_still_alive = g_atomic_pointer_exchange (&cache->texture, NULL) != NULL;
|
|
|
|
|
|
|
|
if (!cache_still_alive)
|
|
|
|
g_free (cache);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GskGpuCachedTexture *
|
|
|
|
gsk_gpu_cached_texture_new (GskGpuDevice *device,
|
|
|
|
GdkTexture *texture,
|
|
|
|
GskGpuImage *image)
|
|
|
|
{
|
|
|
|
GskGpuDevicePrivate *priv = gsk_gpu_device_get_instance_private (device);
|
|
|
|
GskGpuCachedTexture *self;
|
|
|
|
|
|
|
|
if (gdk_texture_get_render_data (texture, device))
|
|
|
|
gdk_texture_clear_render_data (texture);
|
|
|
|
else if ((self = g_hash_table_lookup (priv->texture_cache, texture)))
|
|
|
|
{
|
|
|
|
g_hash_table_remove (priv->texture_cache, texture);
|
|
|
|
g_object_weak_unref (G_OBJECT (texture), (GWeakNotify) gsk_gpu_cached_texture_destroy_cb, self);
|
|
|
|
}
|
|
|
|
|
|
|
|
self = gsk_gpu_cached_new (device, &GSK_GPU_CACHED_TEXTURE_CLASS, NULL);
|
|
|
|
self->texture = texture;
|
|
|
|
self->image = g_object_ref (image);
|
|
|
|
|
|
|
|
if (!gdk_texture_set_render_data (texture, device, self, gsk_gpu_cached_texture_destroy_cb))
|
|
|
|
{
|
|
|
|
g_object_weak_ref (G_OBJECT (texture), (GWeakNotify) gsk_gpu_cached_texture_destroy_cb, self);
|
|
|
|
g_hash_table_insert (priv->texture_cache, texture, self);
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
/* {{{ CachedGlyph */
|
|
|
|
|
2023-09-06 00:11:39 +00:00
|
|
|
struct _GskGpuCachedGlyph
|
|
|
|
{
|
2023-11-14 10:54:17 +00:00
|
|
|
GskGpuCached parent;
|
|
|
|
|
2023-09-06 00:11:39 +00:00
|
|
|
PangoFont *font;
|
|
|
|
PangoGlyph glyph;
|
|
|
|
GskGpuGlyphLookupFlags flags;
|
|
|
|
float scale;
|
|
|
|
|
|
|
|
GskGpuImage *image;
|
|
|
|
graphene_rect_t bounds;
|
|
|
|
graphene_point_t origin;
|
|
|
|
};
|
|
|
|
|
2023-11-14 10:54:17 +00:00
|
|
|
static void
|
|
|
|
gsk_gpu_cached_glyph_free (GskGpuDevice *device,
|
|
|
|
GskGpuCached *cached)
|
2023-08-12 20:10:16 +00:00
|
|
|
{
|
2023-11-14 10:54:17 +00:00
|
|
|
GskGpuCachedGlyph *self = (GskGpuCachedGlyph *) cached;
|
2023-09-01 10:11:06 +00:00
|
|
|
|
2023-11-14 10:54:17 +00:00
|
|
|
g_object_unref (self->font);
|
|
|
|
g_object_unref (self->image);
|
2023-09-25 19:12:20 +00:00
|
|
|
|
2023-11-14 10:54:17 +00:00
|
|
|
g_free (self);
|
|
|
|
}
|
2023-08-12 20:10:16 +00:00
|
|
|
|
2023-11-14 10:54:17 +00:00
|
|
|
static gboolean
|
|
|
|
gsk_gpu_cached_glyph_should_collect (GskGpuDevice *device,
|
|
|
|
GskGpuCached *cached,
|
|
|
|
gint64 timestsamp)
|
|
|
|
{
|
|
|
|
/* FIXME */
|
|
|
|
return FALSE;
|
|
|
|
}
|
2023-08-12 20:10:16 +00:00
|
|
|
|
2023-09-06 00:11:39 +00:00
|
|
|
static guint
|
|
|
|
gsk_gpu_cached_glyph_hash (gconstpointer data)
|
|
|
|
{
|
|
|
|
const GskGpuCachedGlyph *glyph = data;
|
|
|
|
|
|
|
|
return GPOINTER_TO_UINT (glyph->font) ^
|
|
|
|
glyph->glyph ^
|
|
|
|
(glyph->flags << 24) ^
|
|
|
|
((guint) glyph->scale * PANGO_SCALE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gsk_gpu_cached_glyph_equal (gconstpointer v1,
|
|
|
|
gconstpointer v2)
|
|
|
|
{
|
|
|
|
const GskGpuCachedGlyph *glyph1 = v1;
|
|
|
|
const GskGpuCachedGlyph *glyph2 = v2;
|
|
|
|
|
|
|
|
return glyph1->font == glyph2->font
|
|
|
|
&& glyph1->glyph == glyph2->glyph
|
|
|
|
&& glyph1->flags == glyph2->flags
|
|
|
|
&& glyph1->scale == glyph2->scale;
|
|
|
|
}
|
|
|
|
|
2023-11-14 10:54:17 +00:00
|
|
|
static const GskGpuCachedClass GSK_GPU_CACHED_GLYPH_CLASS =
|
|
|
|
{
|
|
|
|
sizeof (GskGpuCachedGlyph),
|
|
|
|
gsk_gpu_cached_glyph_free,
|
|
|
|
gsk_gpu_cached_glyph_should_collect
|
|
|
|
};
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
/* {{{ GskGpuDevice */
|
|
|
|
|
2023-09-01 10:11:06 +00:00
|
|
|
void
|
|
|
|
gsk_gpu_device_gc (GskGpuDevice *self,
|
|
|
|
gint64 timestamp)
|
|
|
|
{
|
|
|
|
GskGpuDevicePrivate *priv = gsk_gpu_device_get_instance_private (self);
|
2023-11-14 10:54:17 +00:00
|
|
|
GskGpuCached *cached, *next;
|
2023-09-01 10:11:06 +00:00
|
|
|
|
2023-11-14 10:54:17 +00:00
|
|
|
for (cached = priv->first_cached; cached != NULL; cached = next)
|
2023-09-01 10:11:06 +00:00
|
|
|
{
|
2023-11-14 10:54:17 +00:00
|
|
|
next = cached->next;
|
|
|
|
if (gsk_gpu_cached_should_collect (self, cached, timestamp))
|
|
|
|
gsk_gpu_cached_free (self, priv->first_cached);
|
2023-09-01 10:11:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_gpu_device_clear_cache (GskGpuDevice *self)
|
|
|
|
{
|
|
|
|
GskGpuDevicePrivate *priv = gsk_gpu_device_get_instance_private (self);
|
|
|
|
|
2023-11-14 10:54:17 +00:00
|
|
|
for (GskGpuCached *cached = priv->first_cached; cached; cached = cached->next)
|
2023-09-01 10:11:06 +00:00
|
|
|
{
|
2023-11-14 10:54:17 +00:00
|
|
|
if (cached->prev == NULL)
|
|
|
|
g_assert (priv->first_cached == cached);
|
|
|
|
else
|
|
|
|
g_assert (cached->prev->next == cached);
|
|
|
|
if (cached->next == NULL)
|
|
|
|
g_assert (priv->last_cached == cached);
|
|
|
|
else
|
|
|
|
g_assert (cached->next->prev == cached);
|
2023-09-01 10:11:06 +00:00
|
|
|
}
|
|
|
|
|
2023-11-14 10:54:17 +00:00
|
|
|
while (priv->first_cached)
|
|
|
|
gsk_gpu_cached_free (self, priv->first_cached);
|
|
|
|
|
|
|
|
g_assert (priv->last_cached == NULL);
|
2023-09-01 10:11:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_gpu_device_dispose (GObject *object)
|
|
|
|
{
|
|
|
|
GskGpuDevice *self = GSK_GPU_DEVICE (object);
|
|
|
|
GskGpuDevicePrivate *priv = gsk_gpu_device_get_instance_private (self);
|
|
|
|
|
|
|
|
gsk_gpu_device_clear_cache (self);
|
2023-11-14 10:54:17 +00:00
|
|
|
g_hash_table_unref (priv->glyph_cache);
|
|
|
|
g_hash_table_unref (priv->texture_cache);
|
2023-09-01 10:11:06 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (gsk_gpu_device_parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
2023-08-12 20:10:16 +00:00
|
|
|
static void
|
|
|
|
gsk_gpu_device_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
GskGpuDevice *self = GSK_GPU_DEVICE (object);
|
|
|
|
GskGpuDevicePrivate *priv = gsk_gpu_device_get_instance_private (self);
|
|
|
|
|
|
|
|
g_object_unref (priv->display);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (gsk_gpu_device_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_gpu_device_class_init (GskGpuDeviceClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
2023-09-01 10:11:06 +00:00
|
|
|
object_class->dispose = gsk_gpu_device_dispose;
|
2023-08-12 20:10:16 +00:00
|
|
|
object_class->finalize = gsk_gpu_device_finalize;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_gpu_device_init (GskGpuDevice *self)
|
|
|
|
{
|
2023-09-01 10:11:06 +00:00
|
|
|
GskGpuDevicePrivate *priv = gsk_gpu_device_get_instance_private (self);
|
|
|
|
|
2023-09-06 00:11:39 +00:00
|
|
|
priv->glyph_cache = g_hash_table_new (gsk_gpu_cached_glyph_hash,
|
|
|
|
gsk_gpu_cached_glyph_equal);
|
2023-11-14 10:54:17 +00:00
|
|
|
priv->texture_cache = g_hash_table_new (g_direct_hash,
|
|
|
|
g_direct_equal);
|
2023-08-12 20:10:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gsk_gpu_device_setup (GskGpuDevice *self,
|
2023-10-12 21:53:12 +00:00
|
|
|
GdkDisplay *display,
|
|
|
|
gsize max_image_size)
|
2023-08-12 20:10:16 +00:00
|
|
|
{
|
|
|
|
GskGpuDevicePrivate *priv = gsk_gpu_device_get_instance_private (self);
|
|
|
|
|
|
|
|
priv->display = g_object_ref (display);
|
2023-10-12 21:53:12 +00:00
|
|
|
priv->max_image_size = max_image_size;
|
2023-08-12 20:10:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GdkDisplay *
|
|
|
|
gsk_gpu_device_get_display (GskGpuDevice *self)
|
|
|
|
{
|
|
|
|
GskGpuDevicePrivate *priv = gsk_gpu_device_get_instance_private (self);
|
|
|
|
|
|
|
|
return priv->display;
|
|
|
|
}
|
|
|
|
|
2023-10-12 21:53:12 +00:00
|
|
|
gsize
|
|
|
|
gsk_gpu_device_get_max_image_size (GskGpuDevice *self)
|
|
|
|
{
|
|
|
|
GskGpuDevicePrivate *priv = gsk_gpu_device_get_instance_private (self);
|
|
|
|
|
|
|
|
return priv->max_image_size;
|
|
|
|
}
|
|
|
|
|
2023-08-12 20:10:16 +00:00
|
|
|
GskGpuImage *
|
|
|
|
gsk_gpu_device_create_offscreen_image (GskGpuDevice *self,
|
2023-11-01 05:43:15 +00:00
|
|
|
gboolean with_mipmap,
|
2023-08-12 20:10:16 +00:00
|
|
|
GdkMemoryDepth depth,
|
|
|
|
gsize width,
|
|
|
|
gsize height)
|
|
|
|
{
|
2023-11-01 05:43:15 +00:00
|
|
|
return GSK_GPU_DEVICE_GET_CLASS (self)->create_offscreen_image (self, with_mipmap, depth, width, height);
|
2023-08-12 20:10:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GskGpuImage *
|
|
|
|
gsk_gpu_device_create_upload_image (GskGpuDevice *self,
|
2023-11-01 05:43:15 +00:00
|
|
|
gboolean with_mipmap,
|
2023-08-12 20:10:16 +00:00
|
|
|
GdkMemoryFormat format,
|
|
|
|
gsize width,
|
|
|
|
gsize height)
|
|
|
|
{
|
2023-11-01 05:43:15 +00:00
|
|
|
return GSK_GPU_DEVICE_GET_CLASS (self)->create_upload_image (self, with_mipmap, format, width, height);
|
2023-08-12 20:10:16 +00:00
|
|
|
}
|
2023-09-01 10:11:06 +00:00
|
|
|
|
2023-11-24 00:27:44 +00:00
|
|
|
GskGpuImage *
|
|
|
|
gsk_gpu_device_create_download_image (GskGpuDevice *self,
|
|
|
|
GdkMemoryDepth depth,
|
|
|
|
gsize width,
|
|
|
|
gsize height)
|
|
|
|
{
|
|
|
|
return GSK_GPU_DEVICE_GET_CLASS (self)->create_download_image (self, depth, width, height);
|
|
|
|
}
|
|
|
|
|
2023-09-25 19:12:20 +00:00
|
|
|
/* This rounds up to the next number that has <= 2 bits set:
|
|
|
|
* 1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, ...
|
|
|
|
* That is roughly sqrt(2), so it should limit waste
|
|
|
|
*/
|
|
|
|
static gsize
|
|
|
|
round_up_atlas_size (gsize num)
|
|
|
|
{
|
|
|
|
gsize storage = g_bit_storage (num);
|
|
|
|
|
|
|
|
num = num + (((1 << storage) - 1) >> 2);
|
|
|
|
num &= (((gsize) 7) << storage) >> 2;
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gsk_gpu_cached_atlas_allocate (GskGpuCachedAtlas *atlas,
|
|
|
|
gsize width,
|
|
|
|
gsize height,
|
|
|
|
gsize *out_x,
|
|
|
|
gsize *out_y)
|
|
|
|
{
|
|
|
|
gsize i;
|
|
|
|
gsize waste, slice_waste;
|
|
|
|
gsize best_slice;
|
|
|
|
gsize y, best_y;
|
|
|
|
gboolean can_add_slice;
|
|
|
|
|
|
|
|
best_y = 0;
|
|
|
|
best_slice = G_MAXSIZE;
|
|
|
|
can_add_slice = atlas->n_slices < MAX_SLICES_PER_ATLAS;
|
|
|
|
if (can_add_slice)
|
|
|
|
waste = height; /* Require less than 100% waste */
|
|
|
|
else
|
|
|
|
waste = G_MAXSIZE; /* Accept any slice, we can't make better ones */
|
|
|
|
|
|
|
|
for (i = 0, y = 0; i < atlas->n_slices; y += atlas->slices[i].height, i++)
|
|
|
|
{
|
|
|
|
if (atlas->slices[i].height < height || ATLAS_SIZE - atlas->slices[i].width < width)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
slice_waste = atlas->slices[i].height - height;
|
|
|
|
if (slice_waste < waste)
|
|
|
|
{
|
|
|
|
waste = slice_waste;
|
|
|
|
best_slice = i;
|
|
|
|
best_y = y;
|
|
|
|
if (waste == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (best_slice >= i && i == atlas->n_slices)
|
|
|
|
{
|
|
|
|
if (!can_add_slice)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
atlas->n_slices++;
|
|
|
|
if (atlas->n_slices == MAX_SLICES_PER_ATLAS)
|
|
|
|
atlas->slices[i].height = ATLAS_SIZE - y;
|
|
|
|
else
|
|
|
|
atlas->slices[i].height = round_up_atlas_size (MAX (height, 4));
|
|
|
|
atlas->slices[i].width = 0;
|
|
|
|
best_y = y;
|
|
|
|
best_slice = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out_x = atlas->slices[best_slice].width;
|
|
|
|
*out_y = best_y;
|
|
|
|
|
|
|
|
atlas->slices[best_slice].width += width;
|
|
|
|
g_assert (atlas->slices[best_slice].width <= ATLAS_SIZE);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2023-11-04 18:38:21 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_gpu_device_ensure_atlas (GskGpuDevice *self,
|
|
|
|
gboolean recreate,
|
|
|
|
gint64 timestamp)
|
|
|
|
{
|
|
|
|
GskGpuDevicePrivate *priv = gsk_gpu_device_get_instance_private (self);
|
|
|
|
|
|
|
|
if (priv->current_atlas && !recreate)
|
|
|
|
return;
|
|
|
|
|
2023-11-14 10:54:17 +00:00
|
|
|
priv->current_atlas = gsk_gpu_cached_atlas_new (self);
|
2023-11-04 18:38:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GskGpuImage *
|
|
|
|
gsk_gpu_device_get_atlas_image (GskGpuDevice *self)
|
|
|
|
{
|
|
|
|
GskGpuDevicePrivate *priv = gsk_gpu_device_get_instance_private (self);
|
|
|
|
|
|
|
|
gsk_gpu_device_ensure_atlas (self, FALSE, g_get_monotonic_time ());
|
|
|
|
|
|
|
|
return priv->current_atlas->image;
|
|
|
|
}
|
|
|
|
|
2023-09-25 19:12:20 +00:00
|
|
|
static GskGpuImage *
|
|
|
|
gsk_gpu_device_add_atlas_image (GskGpuDevice *self,
|
|
|
|
gint64 timestamp,
|
|
|
|
gsize width,
|
|
|
|
gsize height,
|
|
|
|
gsize *out_x,
|
|
|
|
gsize *out_y)
|
|
|
|
{
|
|
|
|
GskGpuDevicePrivate *priv = gsk_gpu_device_get_instance_private (self);
|
|
|
|
|
|
|
|
if (width > MAX_ATLAS_ITEM_SIZE || height > MAX_ATLAS_ITEM_SIZE)
|
|
|
|
return NULL;
|
|
|
|
|
2023-11-04 18:38:21 +00:00
|
|
|
gsk_gpu_device_ensure_atlas (self, FALSE, timestamp);
|
|
|
|
|
|
|
|
if (gsk_gpu_cached_atlas_allocate (priv->current_atlas, width, height, out_x, out_y))
|
2023-09-25 19:12:20 +00:00
|
|
|
{
|
2023-11-14 10:54:17 +00:00
|
|
|
gsk_gpu_cached_use (self, (GskGpuCached *) priv->current_atlas, timestamp);
|
2023-09-25 19:12:20 +00:00
|
|
|
return priv->current_atlas->image;
|
|
|
|
}
|
|
|
|
|
2023-11-04 18:38:21 +00:00
|
|
|
gsk_gpu_device_ensure_atlas (self, TRUE, timestamp);
|
2023-09-25 19:12:20 +00:00
|
|
|
|
|
|
|
if (gsk_gpu_cached_atlas_allocate (priv->current_atlas, width, height, out_x, out_y))
|
|
|
|
{
|
2023-11-14 10:54:17 +00:00
|
|
|
gsk_gpu_cached_use (self, (GskGpuCached *) priv->current_atlas, timestamp);
|
2023-09-25 19:12:20 +00:00
|
|
|
return priv->current_atlas->image;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-09-01 10:11:06 +00:00
|
|
|
GskGpuImage *
|
|
|
|
gsk_gpu_device_lookup_texture_image (GskGpuDevice *self,
|
|
|
|
GdkTexture *texture,
|
|
|
|
gint64 timestamp)
|
|
|
|
{
|
2023-11-14 10:54:17 +00:00
|
|
|
GskGpuDevicePrivate *priv = gsk_gpu_device_get_instance_private (self);
|
2023-09-01 10:11:06 +00:00
|
|
|
GskGpuCachedTexture *cache;
|
|
|
|
|
|
|
|
cache = gdk_texture_get_render_data (texture, self);
|
2023-11-14 10:54:17 +00:00
|
|
|
if (cache == NULL)
|
|
|
|
cache = g_hash_table_lookup (priv->texture_cache, texture);
|
|
|
|
|
2023-09-01 10:11:06 +00:00
|
|
|
if (cache)
|
|
|
|
{
|
|
|
|
return g_object_ref (cache->image);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gsk_gpu_device_cache_texture_image (GskGpuDevice *self,
|
|
|
|
GdkTexture *texture,
|
|
|
|
gint64 timestamp,
|
|
|
|
GskGpuImage *image)
|
|
|
|
{
|
|
|
|
GskGpuCachedTexture *cache;
|
|
|
|
|
2023-11-14 10:54:17 +00:00
|
|
|
cache = gsk_gpu_cached_texture_new (self, texture, image);
|
2023-09-01 10:11:06 +00:00
|
|
|
|
2023-11-14 10:54:17 +00:00
|
|
|
gsk_gpu_cached_use (self, (GskGpuCached *) cache, timestamp);
|
2023-09-01 10:11:06 +00:00
|
|
|
}
|
2023-09-06 00:11:39 +00:00
|
|
|
|
|
|
|
GskGpuImage *
|
|
|
|
gsk_gpu_device_lookup_glyph_image (GskGpuDevice *self,
|
|
|
|
GskGpuFrame *frame,
|
|
|
|
PangoFont *font,
|
|
|
|
PangoGlyph glyph,
|
|
|
|
GskGpuGlyphLookupFlags flags,
|
|
|
|
float scale,
|
|
|
|
graphene_rect_t *out_bounds,
|
|
|
|
graphene_point_t *out_origin)
|
|
|
|
{
|
|
|
|
GskGpuDevicePrivate *priv = gsk_gpu_device_get_instance_private (self);
|
|
|
|
GskGpuCachedGlyph lookup = {
|
|
|
|
.font = font,
|
|
|
|
.glyph = glyph,
|
|
|
|
.flags = flags,
|
|
|
|
.scale = scale
|
|
|
|
};
|
|
|
|
GskGpuCachedGlyph *cache;
|
|
|
|
PangoRectangle ink_rect;
|
|
|
|
graphene_rect_t rect;
|
2023-09-25 19:12:20 +00:00
|
|
|
graphene_point_t origin;
|
|
|
|
GskGpuImage *image;
|
|
|
|
gsize atlas_x, atlas_y, padding;
|
2023-09-06 00:11:39 +00:00
|
|
|
|
|
|
|
cache = g_hash_table_lookup (priv->glyph_cache, &lookup);
|
|
|
|
if (cache)
|
|
|
|
{
|
2023-11-14 10:54:17 +00:00
|
|
|
gsk_gpu_cached_use (self, (GskGpuCached *) cache, gsk_gpu_frame_get_timestamp (frame));
|
|
|
|
|
2023-09-06 00:11:39 +00:00
|
|
|
*out_bounds = cache->bounds;
|
|
|
|
*out_origin = cache->origin;
|
|
|
|
return cache->image;
|
|
|
|
}
|
|
|
|
|
|
|
|
cache = g_new (GskGpuCachedGlyph, 1);
|
|
|
|
pango_font_get_glyph_extents (font, glyph, &ink_rect, NULL);
|
2023-09-25 19:12:20 +00:00
|
|
|
origin.x = floor (ink_rect.x * scale / PANGO_SCALE);
|
|
|
|
origin.y = floor (ink_rect.y * scale / PANGO_SCALE);
|
|
|
|
rect.size.width = ceil ((ink_rect.x + ink_rect.width) * scale / PANGO_SCALE) - origin.x;
|
|
|
|
rect.size.height = ceil ((ink_rect.y + ink_rect.height) * scale / PANGO_SCALE) - origin.y;
|
|
|
|
padding = 1;
|
|
|
|
|
|
|
|
image = gsk_gpu_device_add_atlas_image (self,
|
|
|
|
gsk_gpu_frame_get_timestamp (frame),
|
|
|
|
rect.size.width + 2 * padding, rect.size.height + 2 * padding,
|
|
|
|
&atlas_x, &atlas_y);
|
|
|
|
if (image)
|
|
|
|
{
|
|
|
|
g_object_ref (image);
|
|
|
|
rect.origin.x = atlas_x + padding;
|
|
|
|
rect.origin.y = atlas_y + padding;
|
2023-11-14 10:54:17 +00:00
|
|
|
cache = gsk_gpu_cached_new (self, &GSK_GPU_CACHED_GLYPH_CLASS, priv->current_atlas);
|
2023-09-25 19:12:20 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-11-01 05:43:15 +00:00
|
|
|
image = gsk_gpu_device_create_upload_image (self, FALSE, GDK_MEMORY_DEFAULT, rect.size.width, rect.size.height),
|
2023-09-25 19:12:20 +00:00
|
|
|
rect.origin.x = 0;
|
|
|
|
rect.origin.y = 0;
|
|
|
|
padding = 0;
|
2023-11-14 10:54:17 +00:00
|
|
|
cache = gsk_gpu_cached_new (self, &GSK_GPU_CACHED_GLYPH_CLASS, NULL);
|
2023-09-25 19:12:20 +00:00
|
|
|
}
|
|
|
|
|
2023-11-14 10:54:17 +00:00
|
|
|
cache->font = g_object_ref (font),
|
|
|
|
cache->glyph = glyph,
|
|
|
|
cache->flags = flags,
|
|
|
|
cache->scale = scale,
|
|
|
|
cache->bounds = rect,
|
|
|
|
cache->image = image,
|
|
|
|
cache->origin = GRAPHENE_POINT_INIT (- origin.x + (flags & 3) / 4.f,
|
|
|
|
- origin.y + ((flags >> 2) & 3) / 4.f);
|
2023-09-06 00:11:39 +00:00
|
|
|
|
|
|
|
gsk_gpu_upload_glyph_op (frame,
|
|
|
|
cache->image,
|
|
|
|
font,
|
|
|
|
glyph,
|
|
|
|
&(cairo_rectangle_int_t) {
|
2023-09-25 19:12:20 +00:00
|
|
|
.x = rect.origin.x - padding,
|
|
|
|
.y = rect.origin.y - padding,
|
|
|
|
.width = rect.size.width + 2 * padding,
|
|
|
|
.height = rect.size.height + 2 * padding,
|
2023-09-06 00:11:39 +00:00
|
|
|
},
|
|
|
|
scale,
|
2023-09-25 19:12:20 +00:00
|
|
|
&GRAPHENE_POINT_INIT (cache->origin.x + 1,
|
|
|
|
cache->origin.y + 1));
|
2023-09-06 00:11:39 +00:00
|
|
|
|
|
|
|
g_hash_table_insert (priv->glyph_cache, cache, cache);
|
2023-11-14 10:54:17 +00:00
|
|
|
gsk_gpu_cached_use (self, (GskGpuCached *) cache, gsk_gpu_frame_get_timestamp (frame));
|
2023-09-06 00:11:39 +00:00
|
|
|
|
|
|
|
*out_bounds = cache->bounds;
|
|
|
|
*out_origin = cache->origin;
|
|
|
|
return cache->image;
|
|
|
|
}
|
|
|
|
|
2023-11-14 10:54:17 +00:00
|
|
|
/* }}} */
|