2019-05-22 07:33:25 +00:00
|
|
|
#include "gskgliconcacheprivate.h"
|
|
|
|
#include "gskgltextureatlasprivate.h"
|
|
|
|
#include "gdk/gdktextureprivate.h"
|
2019-06-04 21:27:09 +00:00
|
|
|
#include "gdk/gdkglcontextprivate.h"
|
2019-05-22 07:33:25 +00:00
|
|
|
|
2019-06-02 16:47:57 +00:00
|
|
|
#include <epoxy/gl.h>
|
|
|
|
|
2019-10-12 16:34:56 +00:00
|
|
|
#define MAX_FRAME_AGE 60
|
2019-05-22 07:33:25 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
icon_data_free (gpointer p)
|
|
|
|
{
|
2019-10-22 06:39:56 +00:00
|
|
|
g_object_unref (((IconData *)p)->source_texture);
|
2019-06-02 15:10:40 +00:00
|
|
|
g_free (p);
|
2019-05-22 07:33:25 +00:00
|
|
|
}
|
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
GskGLIconCache *
|
|
|
|
gsk_gl_icon_cache_new (GdkDisplay *display,
|
|
|
|
GskGLTextureAtlases *atlases)
|
2019-06-02 15:11:11 +00:00
|
|
|
{
|
2019-06-04 21:27:09 +00:00
|
|
|
GskGLIconCache *self;
|
|
|
|
|
|
|
|
self = g_new0 (GskGLIconCache, 1);
|
2019-06-02 15:11:11 +00:00
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
self->display = display;
|
|
|
|
self->icons = g_hash_table_new_full (NULL, NULL, NULL, icon_data_free);
|
|
|
|
self->atlases = gsk_gl_texture_atlases_ref (atlases);
|
|
|
|
self->ref_count = 1;
|
2019-06-02 15:11:11 +00:00
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
return self;
|
2019-06-02 15:11:11 +00:00
|
|
|
}
|
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
GskGLIconCache *
|
|
|
|
gsk_gl_icon_cache_ref (GskGLIconCache *self)
|
2019-05-22 07:33:25 +00:00
|
|
|
{
|
2019-06-04 21:27:09 +00:00
|
|
|
self->ref_count++;
|
2019-05-22 07:33:25 +00:00
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
return self;
|
2019-05-22 07:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-06-04 21:27:09 +00:00
|
|
|
gsk_gl_icon_cache_unref (GskGLIconCache *self)
|
2019-05-22 07:33:25 +00:00
|
|
|
{
|
2019-06-04 21:27:09 +00:00
|
|
|
g_assert (self->ref_count > 0);
|
2019-05-22 07:33:25 +00:00
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
if (self->ref_count == 1)
|
2019-05-22 07:33:25 +00:00
|
|
|
{
|
2019-06-04 21:27:09 +00:00
|
|
|
gsk_gl_texture_atlases_unref (self->atlases);
|
|
|
|
g_hash_table_unref (self->icons);
|
|
|
|
g_free (self);
|
|
|
|
return;
|
2019-05-22 07:33:25 +00:00
|
|
|
}
|
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
self->ref_count--;
|
2019-05-22 07:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-10-12 00:42:24 +00:00
|
|
|
gsk_gl_icon_cache_begin_frame (GskGLIconCache *self,
|
|
|
|
GPtrArray *removed_atlases)
|
2019-05-22 07:33:25 +00:00
|
|
|
{
|
|
|
|
GHashTableIter iter;
|
|
|
|
GdkTexture *texture;
|
|
|
|
IconData *icon_data;
|
|
|
|
|
2019-10-12 15:46:10 +00:00
|
|
|
self->timestamp++;
|
|
|
|
|
2019-10-12 00:42:24 +00:00
|
|
|
/* Drop icons on removed atlases */
|
|
|
|
if (removed_atlases->len > 0)
|
|
|
|
{
|
2019-10-12 16:32:49 +00:00
|
|
|
guint dropped = 0;
|
|
|
|
|
2019-10-12 00:42:24 +00:00
|
|
|
g_hash_table_iter_init (&iter, self->icons);
|
|
|
|
while (g_hash_table_iter_next (&iter, (gpointer *)&texture, (gpointer *)&icon_data))
|
|
|
|
{
|
|
|
|
if (g_ptr_array_find (removed_atlases, icon_data->atlas, NULL))
|
2019-10-12 16:32:49 +00:00
|
|
|
{
|
|
|
|
g_hash_table_iter_remove (&iter);
|
|
|
|
dropped++;
|
|
|
|
}
|
2019-10-12 00:42:24 +00:00
|
|
|
}
|
2019-10-12 16:32:49 +00:00
|
|
|
|
|
|
|
GSK_NOTE(GLYPH_CACHE, if (dropped > 0) g_message ("Dropped %d icons", dropped));
|
2019-10-12 00:42:24 +00:00
|
|
|
}
|
2019-05-22 07:33:25 +00:00
|
|
|
|
2019-10-12 15:46:10 +00:00
|
|
|
if (self->timestamp % MAX_FRAME_AGE == 0)
|
2019-12-17 08:14:54 +00:00
|
|
|
{
|
2019-10-12 15:46:10 +00:00
|
|
|
g_hash_table_iter_init (&iter, self->icons);
|
|
|
|
while (g_hash_table_iter_next (&iter, (gpointer *)&texture, (gpointer *)&icon_data))
|
2019-05-22 07:33:25 +00:00
|
|
|
{
|
2019-10-12 15:46:10 +00:00
|
|
|
if (!icon_data->accessed)
|
2019-06-02 15:11:11 +00:00
|
|
|
{
|
2019-10-12 15:46:10 +00:00
|
|
|
if (icon_data->used)
|
|
|
|
{
|
2019-10-15 12:27:16 +00:00
|
|
|
const int width = icon_data->source_texture->width;
|
|
|
|
const int height = icon_data->source_texture->height;
|
2019-10-15 11:51:05 +00:00
|
|
|
gsk_gl_texture_atlas_mark_unused (icon_data->atlas, width + 2, height + 2);
|
2019-10-12 15:46:10 +00:00
|
|
|
icon_data->used = FALSE;
|
|
|
|
}
|
2019-10-22 06:39:56 +00:00
|
|
|
}
|
2019-10-12 00:42:24 +00:00
|
|
|
|
2019-10-12 15:46:10 +00:00
|
|
|
icon_data->accessed = FALSE;
|
2019-05-22 07:33:25 +00:00
|
|
|
}
|
2019-10-12 16:32:49 +00:00
|
|
|
|
|
|
|
GSK_NOTE(GLYPH_CACHE, g_message ("%d icons cached", g_hash_table_size (self->icons)));
|
2019-05-22 07:33:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gsk_gl_icon_cache_lookup_or_add (GskGLIconCache *self,
|
|
|
|
GdkTexture *texture,
|
2019-10-15 11:51:05 +00:00
|
|
|
const IconData **out_icon_data)
|
2019-05-22 07:33:25 +00:00
|
|
|
{
|
2019-10-22 06:39:56 +00:00
|
|
|
IconData *icon_data = g_hash_table_lookup (self->icons, texture);
|
2019-05-22 07:33:25 +00:00
|
|
|
|
|
|
|
if (icon_data)
|
|
|
|
{
|
|
|
|
if (!icon_data->used)
|
|
|
|
{
|
2019-10-15 12:27:16 +00:00
|
|
|
gsk_gl_texture_atlas_mark_used (icon_data->atlas, texture->width + 2, texture->height + 2);
|
2019-05-22 07:33:25 +00:00
|
|
|
icon_data->used = TRUE;
|
|
|
|
}
|
2019-10-12 15:46:10 +00:00
|
|
|
icon_data->accessed = TRUE;
|
2019-05-22 07:33:25 +00:00
|
|
|
|
2019-10-15 11:51:05 +00:00
|
|
|
*out_icon_data = icon_data;
|
2019-05-22 07:33:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* texture not on any atlas yet. Find a suitable one. */
|
|
|
|
{
|
2019-10-15 12:27:16 +00:00
|
|
|
const int width = texture->width;
|
|
|
|
const int height = texture->height;
|
2019-05-22 07:33:25 +00:00
|
|
|
GskGLTextureAtlas *atlas = NULL;
|
2019-06-04 21:27:09 +00:00
|
|
|
int packed_x = 0;
|
|
|
|
int packed_y = 0;
|
2019-05-22 07:33:25 +00:00
|
|
|
cairo_surface_t *surface;
|
2019-10-12 07:17:24 +00:00
|
|
|
unsigned char *surface_data;
|
2019-12-17 08:14:54 +00:00
|
|
|
guint gl_format;
|
2019-05-22 07:33:25 +00:00
|
|
|
|
2019-06-04 21:27:09 +00:00
|
|
|
gsk_gl_texture_atlases_pack (self->atlases, width + 2, height + 2, &atlas, &packed_x, &packed_y);
|
2019-05-22 07:33:25 +00:00
|
|
|
|
|
|
|
icon_data = g_new0 (IconData, 1);
|
|
|
|
icon_data->atlas = atlas;
|
2019-10-12 15:46:10 +00:00
|
|
|
icon_data->accessed = TRUE;
|
2019-05-22 07:33:25 +00:00
|
|
|
icon_data->used = TRUE;
|
2019-10-15 11:51:05 +00:00
|
|
|
icon_data->texture_id = atlas->texture_id;
|
2019-08-10 17:36:56 +00:00
|
|
|
icon_data->source_texture = g_object_ref (texture);
|
2019-10-15 11:51:05 +00:00
|
|
|
icon_data->x = (float)(packed_x + 1) / atlas->width;
|
|
|
|
icon_data->y = (float)(packed_y + 1) / atlas->width;
|
|
|
|
icon_data->x2 = icon_data->x + (float)width / atlas->width;
|
|
|
|
icon_data->y2 = icon_data->y + (float)height / atlas->height;
|
2019-05-22 07:33:25 +00:00
|
|
|
|
|
|
|
g_hash_table_insert (self->icons, texture, icon_data);
|
|
|
|
|
|
|
|
/* actually upload the texture */
|
|
|
|
surface = gdk_texture_download_surface (texture);
|
2019-10-12 07:17:24 +00:00
|
|
|
surface_data = cairo_image_surface_get_data (surface);
|
2019-06-04 21:27:09 +00:00
|
|
|
gdk_gl_context_push_debug_group_printf (gdk_gl_context_get_current (),
|
|
|
|
"Uploading texture");
|
2019-12-17 08:14:54 +00:00
|
|
|
|
|
|
|
if (gdk_gl_context_get_use_es (gdk_gl_context_get_current ()))
|
|
|
|
gl_format = GL_RGBA;
|
|
|
|
else
|
|
|
|
gl_format = GL_BGRA;
|
|
|
|
|
2019-10-12 07:17:24 +00:00
|
|
|
glBindTexture (GL_TEXTURE_2D, atlas->texture_id);
|
|
|
|
|
|
|
|
glTexSubImage2D (GL_TEXTURE_2D, 0,
|
|
|
|
packed_x + 1, packed_y + 1,
|
|
|
|
width, height,
|
2019-12-17 08:14:54 +00:00
|
|
|
gl_format,
|
|
|
|
GL_UNSIGNED_BYTE,
|
2019-10-12 07:17:24 +00:00
|
|
|
surface_data);
|
|
|
|
/* Padding top */
|
|
|
|
glTexSubImage2D (GL_TEXTURE_2D, 0,
|
|
|
|
packed_x + 1, packed_y,
|
|
|
|
width, 1,
|
2019-12-17 08:14:54 +00:00
|
|
|
gl_format, GL_UNSIGNED_BYTE,
|
2019-10-12 07:17:24 +00:00
|
|
|
surface_data);
|
|
|
|
/* Padding left */
|
|
|
|
glTexSubImage2D (GL_TEXTURE_2D, 0,
|
|
|
|
packed_x, packed_y + 1,
|
|
|
|
1, height,
|
2019-12-17 08:14:54 +00:00
|
|
|
gl_format, GL_UNSIGNED_BYTE,
|
2019-10-12 07:17:24 +00:00
|
|
|
surface_data);
|
|
|
|
/* Padding top left */
|
|
|
|
glTexSubImage2D (GL_TEXTURE_2D, 0,
|
|
|
|
packed_x, packed_y,
|
|
|
|
1, 1,
|
2019-12-17 08:14:54 +00:00
|
|
|
gl_format, GL_UNSIGNED_BYTE,
|
2019-10-12 07:17:24 +00:00
|
|
|
surface_data);
|
|
|
|
|
|
|
|
/* Padding right */
|
|
|
|
glPixelStorei (GL_UNPACK_ROW_LENGTH, width);
|
|
|
|
glPixelStorei (GL_UNPACK_SKIP_PIXELS, width - 1);
|
|
|
|
glTexSubImage2D (GL_TEXTURE_2D, 0,
|
|
|
|
packed_x + width + 1, packed_y + 1,
|
|
|
|
1, height,
|
2019-12-17 08:14:54 +00:00
|
|
|
gl_format, GL_UNSIGNED_BYTE,
|
2019-10-12 07:17:24 +00:00
|
|
|
surface_data);
|
|
|
|
/* Padding top right */
|
|
|
|
glTexSubImage2D (GL_TEXTURE_2D, 0,
|
|
|
|
packed_x + width + 1, packed_y,
|
|
|
|
1, 1,
|
2019-12-17 08:14:54 +00:00
|
|
|
gl_format, GL_UNSIGNED_BYTE,
|
2019-10-12 07:17:24 +00:00
|
|
|
surface_data);
|
|
|
|
/* Padding bottom */
|
|
|
|
glPixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
|
|
|
|
glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
|
|
|
|
glPixelStorei (GL_UNPACK_SKIP_ROWS, height - 1);
|
|
|
|
glTexSubImage2D (GL_TEXTURE_2D, 0,
|
|
|
|
packed_x + 1, packed_y + 1 + height,
|
|
|
|
width, 1,
|
2019-12-17 08:14:54 +00:00
|
|
|
gl_format, GL_UNSIGNED_BYTE,
|
2019-10-12 07:17:24 +00:00
|
|
|
surface_data);
|
|
|
|
/* Padding bottom left */
|
|
|
|
glTexSubImage2D (GL_TEXTURE_2D, 0,
|
|
|
|
packed_x, packed_y + 1 + height,
|
|
|
|
1, 1,
|
2019-12-17 08:14:54 +00:00
|
|
|
gl_format, GL_UNSIGNED_BYTE,
|
2019-10-12 07:17:24 +00:00
|
|
|
surface_data);
|
|
|
|
/* Padding bottom right */
|
|
|
|
glPixelStorei (GL_UNPACK_ROW_LENGTH, width);
|
|
|
|
glPixelStorei (GL_UNPACK_SKIP_PIXELS, width - 1);
|
|
|
|
glTexSubImage2D (GL_TEXTURE_2D, 0,
|
|
|
|
packed_x + 1 + width, packed_y + 1 + height,
|
|
|
|
1, 1,
|
2019-12-17 08:14:54 +00:00
|
|
|
gl_format, GL_UNSIGNED_BYTE,
|
2019-10-12 07:17:24 +00:00
|
|
|
surface_data);
|
|
|
|
/* Reset this */
|
|
|
|
glPixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
|
|
|
|
glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
|
|
|
|
glPixelStorei (GL_UNPACK_SKIP_ROWS, 0);
|
2019-06-04 21:27:09 +00:00
|
|
|
|
|
|
|
gdk_gl_context_pop_debug_group (gdk_gl_context_get_current ());
|
2019-05-22 07:33:25 +00:00
|
|
|
|
2019-10-15 11:51:05 +00:00
|
|
|
*out_icon_data = icon_data;
|
2019-05-22 07:33:25 +00:00
|
|
|
|
|
|
|
cairo_surface_destroy (surface);
|
|
|
|
|
|
|
|
#if 0
|
2019-08-10 13:29:22 +00:00
|
|
|
{
|
|
|
|
static int k;
|
|
|
|
const int stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, atlas->width);
|
|
|
|
guchar *data = g_malloc (atlas->height * stride);
|
|
|
|
cairo_surface_t *s;
|
|
|
|
char *filename = g_strdup_printf ("atlas_%u_%d.png", atlas->texture_id, k++);
|
|
|
|
|
|
|
|
glBindTexture (GL_TEXTURE_2D, atlas->texture_id);
|
2019-12-17 08:14:54 +00:00
|
|
|
glGetTexImage (GL_TEXTURE_2D, 0, GL_BGRA, GL_UNSIGNED_BYTE, data);
|
2019-08-10 13:29:22 +00:00
|
|
|
s = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_ARGB32, atlas->width, atlas->height, stride);
|
|
|
|
cairo_surface_write_to_png (s, filename);
|
|
|
|
|
|
|
|
cairo_surface_destroy (s);
|
|
|
|
g_free (data);
|
|
|
|
g_free (filename);
|
|
|
|
}
|
2019-05-22 07:33:25 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|