broadway: Add a texture cache

This ensures we never upload a texture we already have locally available.
This commit is contained in:
Alexander Larsson 2017-11-29 18:16:24 +01:00
parent d284c3fa85
commit 907e071c90
2 changed files with 138 additions and 12 deletions

View File

@ -43,6 +43,92 @@
#endif #endif
#include <sys/types.h> #include <sys/types.h>
static gboolean
compare_surface (cairo_surface_t *a,
cairo_surface_t *b,
int width,
int height)
{
unsigned char *data_a, *data_b;
int stride_a, stride_b;
int y;
data_a = cairo_image_surface_get_data (a);
stride_a = cairo_image_surface_get_stride (a);
data_b = cairo_image_surface_get_data (b);
stride_b = cairo_image_surface_get_stride (b);
for (y = 0; y < height; y++)
{
if (memcmp (data_a, data_b, 4 * width) != 0)
return FALSE;
data_a += stride_a;
data_b += stride_b;
}
return TRUE;
}
static gboolean
gdk_texture_equal (GdkTexture *a,
GdkTexture *b)
{
cairo_surface_t *surface_a;
cairo_surface_t *surface_b;
gboolean res;
if (a == b)
return TRUE;
if (a->width != b->width ||
a->height != b->height)
return FALSE;
surface_a = gdk_texture_download_surface (a);
surface_b = gdk_texture_download_surface (b);
res = compare_surface (surface_a, surface_b, a->width, a->height);
cairo_surface_destroy (surface_a);
cairo_surface_destroy (surface_b);
return res;
}
static guint
gdk_texture_hash (GdkTexture *self)
{
cairo_surface_t *surface;
unsigned char *data;
int stride;
guint32 *row;
guint64 sum;
int x, y, width, height;
guint h;
surface = gdk_texture_download_surface (self);
data = cairo_image_surface_get_data (surface);
stride = cairo_image_surface_get_stride (surface);
width = MAX (self->width, 4);
height = MAX (self->height, 4);
sum = 0;
for (y = 0; y < height; y++, data += stride)
{
row = (guint32 *)data;
for (x = 0; x < width; x++)
sum += row[x];
}
cairo_surface_destroy (surface);
h = sum / (width * height);
return h ^ self->width ^ (self->height << 16);
}
static void gdk_broadway_display_dispose (GObject *object); static void gdk_broadway_display_dispose (GObject *object);
static void gdk_broadway_display_finalize (GObject *object); static void gdk_broadway_display_finalize (GObject *object);
@ -62,6 +148,7 @@ gdk_broadway_display_init (GdkBroadwayDisplay *display)
NULL); NULL);
gdk_monitor_set_manufacturer (display->monitor, "browser"); gdk_monitor_set_manufacturer (display->monitor, "browser");
gdk_monitor_set_model (display->monitor, "0"); gdk_monitor_set_model (display->monitor, "0");
display->texture_cache = g_hash_table_new ((GHashFunc)gdk_texture_hash, (GEqualFunc)gdk_texture_equal);
} }
static void static void
@ -380,17 +467,40 @@ gdk_broadway_display_get_last_seen_time (GdkDisplay *display)
typedef struct { typedef struct {
int id; int id;
GdkDisplay *display; GdkDisplay *display;
GdkTexture *in_cache;
GList *textures;
} BroadwayTextureData; } BroadwayTextureData;
static void static void
broadway_texture_data_free (BroadwayTextureData *data) broadway_texture_data_notify (BroadwayTextureData *data,
GdkTexture *disposed_texture)
{ {
GdkBroadwayDisplay *broadway_display = GDK_BROADWAY_DISPLAY (data->display); GdkBroadwayDisplay *broadway_display = GDK_BROADWAY_DISPLAY (data->display);
if (data->in_cache == disposed_texture)
{
g_hash_table_remove (broadway_display->texture_cache, disposed_texture);
data->in_cache = NULL;
}
g_object_set_data (G_OBJECT (disposed_texture), "broadway-data", NULL);
data->textures = g_list_remove (data->textures, disposed_texture);
if (data->textures == NULL)
{
gdk_broadway_server_release_texture (broadway_display->server, data->id); gdk_broadway_server_release_texture (broadway_display->server, data->id);
g_object_unref (data->display); g_object_unref (data->display);
g_free (data); g_free (data);
} }
else if (data->in_cache == NULL)
{
GdkTexture *first = data->textures->data;
data->in_cache = first;
g_hash_table_replace (broadway_display->texture_cache,
data->in_cache, data->in_cache);
}
}
guint32 guint32
gdk_broadway_display_ensure_texture (GdkDisplay *display, gdk_broadway_display_ensure_texture (GdkDisplay *display,
@ -399,21 +509,35 @@ gdk_broadway_display_ensure_texture (GdkDisplay *display,
GdkBroadwayDisplay *broadway_display = GDK_BROADWAY_DISPLAY (display); GdkBroadwayDisplay *broadway_display = GDK_BROADWAY_DISPLAY (display);
BroadwayTextureData *data; BroadwayTextureData *data;
guint32 id; guint32 id;
GdkTexture *cached;
data = gdk_texture_get_render_data (texture, display); data = g_object_get_data (G_OBJECT (texture), "broadway-data");
if (data != NULL) if (data != NULL)
return data->id; return data->id;
cached = g_hash_table_lookup (broadway_display->texture_cache, texture);
if (cached)
data = g_object_get_data (G_OBJECT (cached), "broadway-data");
if (data == NULL)
{
id = gdk_broadway_server_upload_texture (broadway_display->server, texture); id = gdk_broadway_server_upload_texture (broadway_display->server, texture);
data = g_new0 (BroadwayTextureData, 1); data = g_new0 (BroadwayTextureData, 1);
data->id = id; data->id = id;
data->display = g_object_ref (display); data->display = g_object_ref (display);
if (!gdk_texture_set_render_data (texture, display, data, (GDestroyNotify)broadway_texture_data_free)) data->in_cache = texture;
g_warning ("Failed to set render data, will leak texture"); g_hash_table_replace (broadway_display->texture_cache,
data->in_cache, data->in_cache);
}
return id; data->textures = g_list_prepend (data->textures, texture);
g_object_weak_ref (G_OBJECT (texture), (GWeakNotify)broadway_texture_data_notify, data);
g_object_set_data (G_OBJECT (texture), "broadway-data", data);
return data->id;
} }
static void static void

View File

@ -57,6 +57,8 @@ struct _GdkBroadwayDisplay
gpointer move_resize_data; gpointer move_resize_data;
GdkMonitor *monitor; GdkMonitor *monitor;
GHashTable *texture_cache;
}; };
struct _GdkBroadwayDisplayClass struct _GdkBroadwayDisplayClass