[nautilus/wip/antoniof/hidpi-and-icons-cleanup: 3/11] file: Scale down thumbnails using GtkSnapshot




commit b007fa5a105914ee89b9adbeef41eb01da84123a
Author: António Fernandes <antoniof gnome org>
Date:   Mon Aug 8 00:19:26 2022 +0100

    file: Scale down thumbnails using GtkSnapshot
    
    We've also been scaling thumbnails down in advance using GdkPixbuf API,
    and keeping a cache of scaled thumbnails.
    
    But nowadays we can push that scaling job into the render nodes,
    taking advantage of GPU. This is also a lot simpler.

 src/nautilus-directory-async.c |  5 ---
 src/nautilus-file-private.h    |  3 --
 src/nautilus-file.c            | 69 +++++++++---------------------------------
 3 files changed, 14 insertions(+), 63 deletions(-)
---
diff --git a/src/nautilus-directory-async.c b/src/nautilus-directory-async.c
index 4899ea7b6..3e7f421e6 100644
--- a/src/nautilus-directory-async.c
+++ b/src/nautilus-directory-async.c
@@ -3608,11 +3608,6 @@ thumbnail_done (NautilusDirectory *directory,
         g_object_unref (file->details->thumbnail);
         file->details->thumbnail = NULL;
     }
-    if (file->details->scaled_thumbnail)
-    {
-        g_object_unref (file->details->scaled_thumbnail);
-        file->details->scaled_thumbnail = NULL;
-    }
 
     if (pixbuf)
     {
diff --git a/src/nautilus-file-private.h b/src/nautilus-file-private.h
index 831ed8d44..e575edb62 100644
--- a/src/nautilus-file-private.h
+++ b/src/nautilus-file-private.h
@@ -91,9 +91,6 @@ struct NautilusFileDetails
        GdkPixbuf *thumbnail;
        time_t thumbnail_mtime;
 
-       GdkPixbuf *scaled_thumbnail;
-       double thumbnail_scale;
-
        GList *mime_list; /* If this is a directory, the list of MIME types in it. */
 
        /* Info you might get from a link (.desktop, .directory or nautilus link) */
diff --git a/src/nautilus-file.c b/src/nautilus-file.c
index 375f148dc..451f759d2 100644
--- a/src/nautilus-file.c
+++ b/src/nautilus-file.c
@@ -912,10 +912,6 @@ finalize (GObject *object)
     {
         g_object_unref (file->details->thumbnail);
     }
-    if (file->details->scaled_thumbnail)
-    {
-        g_object_unref (file->details->scaled_thumbnail);
-    }
 
     if (file->details->mount)
     {
@@ -5147,76 +5143,39 @@ nautilus_file_get_thumbnail_icon (NautilusFile          *file,
                                   NautilusFileIconFlags  flags)
 {
     g_autoptr (GdkPaintable) paintable = NULL;
-    int modified_size;
-    GdkPixbuf *pixbuf;
-    int w, h, s;
-    double thumb_scale;
     NautilusIconInfo *icon;
 
     icon = NULL;
-    pixbuf = NULL;
-
-    modified_size = size * scale;
 
-    if (file->details->thumbnail)
+    if (file->details->thumbnail != NULL)
     {
-        gdouble texture_width;
-        gdouble texture_height;
-        g_autoptr (GdkTexture) texture = NULL;
+        GdkPixbuf *pixbuf = file->details->thumbnail;
+        double width = gdk_pixbuf_get_width (pixbuf) / scale;
+        double height = gdk_pixbuf_get_height (pixbuf) / scale;
+        g_autoptr (GdkTexture) texture = gdk_texture_new_for_pixbuf (pixbuf);
         g_autoptr (GtkSnapshot) snapshot = gtk_snapshot_new ();
 
-        w = gdk_pixbuf_get_width (file->details->thumbnail);
-        h = gdk_pixbuf_get_height (file->details->thumbnail);
-
-        s = MAX (w, h);
-        /* Don't scale up small thumbnails in the standard view */
-        if (s <= NAUTILUS_GRID_ICON_SIZE_MEDIUM)
-        {
-            thumb_scale = (double) size / NAUTILUS_GRID_ICON_SIZE_SMALL;
-        }
-        else
-        {
-            thumb_scale = (double) modified_size / s;
-        }
-
-        /* Make sure that icons don't get smaller than NAUTILUS_LIST_ICON_SIZE_SMALL */
-        if (s * thumb_scale <= NAUTILUS_LIST_ICON_SIZE_SMALL)
-        {
-            thumb_scale = (double) NAUTILUS_LIST_ICON_SIZE_SMALL / s;
-        }
-
-        if (file->details->thumbnail_scale == thumb_scale &&
-            file->details->scaled_thumbnail != NULL)
-        {
-            pixbuf = file->details->scaled_thumbnail;
-        }
-        else
+        if (MAX (width, height) > size)
         {
-            pixbuf = gdk_pixbuf_scale_simple (file->details->thumbnail,
-                                              MAX (w * thumb_scale, 1),
-                                              MAX (h * thumb_scale, 1),
-                                              GDK_INTERP_BILINEAR);
+            float scale_down_factor = MAX (width, height) / size;
 
-            g_clear_object (&file->details->scaled_thumbnail);
-            file->details->scaled_thumbnail = pixbuf;
-            file->details->thumbnail_scale = thumb_scale;
+            width = width / scale_down_factor;
+            height = height / scale_down_factor;
         }
 
-        texture = gdk_texture_new_for_pixbuf (pixbuf);
-        texture_width = gdk_texture_get_width (texture);
-        texture_height = gdk_texture_get_height (texture);
         gdk_paintable_snapshot (GDK_PAINTABLE (texture),
                                 GDK_SNAPSHOT (snapshot),
-                                texture_width, texture_height);
+                                width, height);
+
         if (size >= NAUTILUS_GRID_ICON_SIZE_SMALL &&
             nautilus_is_video_file (file))
         {
-            nautilus_ui_frame_video (snapshot, texture_width, texture_height);
+            nautilus_ui_frame_video (snapshot, width, height);
         }
-        paintable = gtk_snapshot_to_paintable (snapshot, NULL);
 
         DEBUG ("Returning thumbnailed image, at size %d %d",
-               (int) (w * thumb_scale), (int) (h * thumb_scale));
+               (int) (width), (int) (height));
+        paintable = gtk_snapshot_to_paintable (snapshot, NULL);
     }
     else if (file->details->thumbnail_path == NULL &&
              file->details->can_read &&


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