[gnome-control-center/gbsneto/background: 12/20] background: Cache thumbnails in CcBackgroundItem



commit 5e4495b907033194eda5f43c3abca2a646c2290e
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Wed May 22 13:10:39 2019 -0300

    background: Cache thumbnails in CcBackgroundItem
    
    We don't actually need to use custom qdata in GObjects to do
    that; instead, cache the thumbnail directly in the item.

 panels/background/cc-background-item.c  | 24 ++++++++++++++++++++++++
 panels/background/cc-background-panel.c | 21 +++++++--------------
 2 files changed, 31 insertions(+), 14 deletions(-)
---
diff --git a/panels/background/cc-background-item.c b/panels/background/cc-background-item.c
index c547cb150..aa960ef5b 100644
--- a/panels/background/cc-background-item.c
+++ b/panels/background/cc-background-item.c
@@ -57,6 +57,14 @@ struct _CcBackgroundItem
         char            *mime_type;
         int              width;
         int              height;
+
+        struct {
+                int        width;
+                int        height;
+                int        frame;
+                int        scale_factor;
+                GdkPixbuf *thumbnail;
+        } cached_thumbnail;
 };
 
 enum {
@@ -227,6 +235,14 @@ cc_background_item_get_frame_thumbnail (CcBackgroundItem             *item,
        g_return_val_if_fail (CC_IS_BACKGROUND_ITEM (item), NULL);
        g_return_val_if_fail (width > 0 && height > 0, NULL);
 
+        /* Use the cached thumbnail if the sizes match */
+        if (item->cached_thumbnail.thumbnail &&
+            item->cached_thumbnail.width == width &&
+            item->cached_thumbnail.height == height &&
+            item->cached_thumbnail.scale_factor == scale_factor &&
+            item->cached_thumbnail.frame == frame)
+                    return g_object_ref (item->cached_thumbnail.thumbnail);
+
         set_bg_properties (item);
 
         if (force_size) {
@@ -272,6 +288,13 @@ cc_background_item_get_frame_thumbnail (CcBackgroundItem             *item,
 
         update_size (item);
 
+        /* Cache the new thumbnail */
+        g_set_object (&item->cached_thumbnail.thumbnail, retval);
+        item->cached_thumbnail.width = width;
+        item->cached_thumbnail.height = height;
+        item->cached_thumbnail.scale_factor = scale_factor;
+        item->cached_thumbnail.frame = frame;
+
         return g_steal_pointer (&retval);
 }
 
@@ -820,6 +843,7 @@ cc_background_item_finalize (GObject *object)
 
         g_return_if_fail (item != NULL);
 
+        g_clear_object (&item->cached_thumbnail.thumbnail);
         g_free (item->name);
         g_free (item->uri);
         g_free (item->primary_color);
diff --git a/panels/background/cc-background-panel.c b/panels/background/cc-background-panel.c
index 098a86ae7..47f632247 100644
--- a/panels/background/cc-background-panel.c
+++ b/panels/background/cc-background-panel.c
@@ -78,25 +78,18 @@ get_or_create_cached_pixbuf (CcBackgroundPanel *panel,
                              GtkWidget         *widget,
                              CcBackgroundItem  *background)
 {
-  GtkAllocation allocation;
   const gint preview_width = 309;
   const gint preview_height = 168;
   gint scale_factor;
   GdkPixbuf *pixbuf;
 
-  pixbuf = g_object_get_data (G_OBJECT (background), "pixbuf");
-  if (pixbuf == NULL)
-    {
-      gtk_widget_get_allocation (widget, &allocation);
-      scale_factor = gtk_widget_get_scale_factor (widget);
-      pixbuf = cc_background_item_get_frame_thumbnail (background,
-                                                       panel->thumb_factory,
-                                                       preview_width,
-                                                       preview_height,
-                                                       scale_factor,
-                                                       -2, TRUE);
-      g_object_set_data_full (G_OBJECT (background), "pixbuf", pixbuf, g_object_unref);
-    }
+  scale_factor = gtk_widget_get_scale_factor (widget);
+  pixbuf = cc_background_item_get_frame_thumbnail (background,
+                                                   panel->thumb_factory,
+                                                   preview_width,
+                                                   preview_height,
+                                                   scale_factor,
+                                                   -2, TRUE);
 
   return pixbuf;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]