From 907e071c9003977d44a9bcb56a0df1f6dd7ce7cb Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Wed, 29 Nov 2017 18:16:24 +0100 Subject: [PATCH] broadway: Add a texture cache This ensures we never upload a texture we already have locally available. --- gdk/broadway/gdkdisplay-broadway.c | 148 ++++++++++++++++++++++++++--- gdk/broadway/gdkdisplay-broadway.h | 2 + 2 files changed, 138 insertions(+), 12 deletions(-) diff --git a/gdk/broadway/gdkdisplay-broadway.c b/gdk/broadway/gdkdisplay-broadway.c index 59ad53317c..4d1b0a4262 100644 --- a/gdk/broadway/gdkdisplay-broadway.c +++ b/gdk/broadway/gdkdisplay-broadway.c @@ -43,6 +43,92 @@ #endif #include +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_finalize (GObject *object); @@ -62,6 +148,7 @@ gdk_broadway_display_init (GdkBroadwayDisplay *display) NULL); gdk_monitor_set_manufacturer (display->monitor, "browser"); gdk_monitor_set_model (display->monitor, "0"); + display->texture_cache = g_hash_table_new ((GHashFunc)gdk_texture_hash, (GEqualFunc)gdk_texture_equal); } static void @@ -380,16 +467,39 @@ gdk_broadway_display_get_last_seen_time (GdkDisplay *display) typedef struct { int id; GdkDisplay *display; + GdkTexture *in_cache; + GList *textures; } BroadwayTextureData; 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); - gdk_broadway_server_release_texture (broadway_display->server, data->id); - g_object_unref (data->display); - g_free (data); + 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); + g_object_unref (data->display); + 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 @@ -399,21 +509,35 @@ gdk_broadway_display_ensure_texture (GdkDisplay *display, GdkBroadwayDisplay *broadway_display = GDK_BROADWAY_DISPLAY (display); BroadwayTextureData *data; 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) return data->id; - id = gdk_broadway_server_upload_texture (broadway_display->server, texture); + cached = g_hash_table_lookup (broadway_display->texture_cache, texture); + if (cached) + data = g_object_get_data (G_OBJECT (cached), "broadway-data"); - data = g_new0 (BroadwayTextureData, 1); - data->id = id; - data->display = g_object_ref (display); + if (data == NULL) + { + id = gdk_broadway_server_upload_texture (broadway_display->server, texture); - if (!gdk_texture_set_render_data (texture, display, data, (GDestroyNotify)broadway_texture_data_free)) - g_warning ("Failed to set render data, will leak texture"); + data = g_new0 (BroadwayTextureData, 1); + data->id = id; + data->display = g_object_ref (display); - return id; + data->in_cache = texture; + g_hash_table_replace (broadway_display->texture_cache, + data->in_cache, data->in_cache); + } + + 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 diff --git a/gdk/broadway/gdkdisplay-broadway.h b/gdk/broadway/gdkdisplay-broadway.h index 69bd34af55..51d59ae077 100644 --- a/gdk/broadway/gdkdisplay-broadway.h +++ b/gdk/broadway/gdkdisplay-broadway.h @@ -57,6 +57,8 @@ struct _GdkBroadwayDisplay gpointer move_resize_data; GdkMonitor *monitor; + + GHashTable *texture_cache; }; struct _GdkBroadwayDisplayClass