[nautilus/wip/antoniof/gtk4-restore-clipboard: 5/6] list-model: Use GdkTexture for icon




commit df02d2bb1b99b5e1ac6a615f0e521e06b9f78ae4
Author: Ernestas Kulik <ernestask gnome org>
Date:   Mon Jul 23 21:02:40 2018 +0300

    list-model: Use GdkTexture for icon
    
    GtkCellRendererPixbuf no longer allows using Cairo surfaces.
    
    (Cherry-picked and rebased by António Fernandes <antoniof gnome org>.)

 eel/eel-graphic-effects.c | 114 ++++++++++++++++------------------------------
 eel/eel-graphic-effects.h |   5 +-
 src/nautilus-list-model.c |  12 ++---
 3 files changed, 46 insertions(+), 85 deletions(-)
---
diff --git a/eel/eel-graphic-effects.c b/eel/eel-graphic-effects.c
index 06af09178..d3189947a 100644
--- a/eel/eel-graphic-effects.c
+++ b/eel/eel-graphic-effects.c
@@ -18,93 +18,59 @@
  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  */
 
-/* This file contains pixbuf manipulation routines used for graphical effects like pre-lighting
- *  and selection hilighting */
+#include "eel-graphic-effects.h"
 
-#include <config.h>
+GdkTexture *
+eel_create_spotlight_texture (GdkTexture *texture)
+{
+    int width;
+    int height;
+    cairo_surface_t *surface;
+    cairo_t *cr;
+    unsigned char *data;
+    int stride;
+    g_autoptr (GBytes) bytes = NULL;
+    GdkTexture *prelit_texture;
 
-#include "eel-graphic-effects.h"
-#include "eel-glib-extensions.h"
+    g_return_val_if_fail (GDK_IS_TEXTURE (texture), NULL);
 
-#include <math.h>
-#include <string.h>
+    width = gdk_texture_get_width (texture);
+    height = gdk_texture_get_height (texture);
+    surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
+    cr = cairo_create (surface);
+    data = cairo_image_surface_get_data (surface);
+    stride = cairo_image_surface_get_stride (surface);
 
-/* shared utility to create a new pixbuf from the passed-in one */
+    gdk_texture_download (texture, data, stride);
 
-static GdkPixbuf *
-create_new_pixbuf (GdkPixbuf *src)
-{
-    g_assert (gdk_pixbuf_get_colorspace (src) == GDK_COLORSPACE_RGB);
-    g_assert ((!gdk_pixbuf_get_has_alpha (src)
-               && gdk_pixbuf_get_n_channels (src) == 3)
-              || (gdk_pixbuf_get_has_alpha (src)
-                  && gdk_pixbuf_get_n_channels (src) == 4));
-
-    return gdk_pixbuf_new (gdk_pixbuf_get_colorspace (src),
-                           gdk_pixbuf_get_has_alpha (src),
-                           gdk_pixbuf_get_bits_per_sample (src),
-                           gdk_pixbuf_get_width (src),
-                           gdk_pixbuf_get_height (src));
-}
+    cairo_surface_mark_dirty (surface);
 
-/* utility routine to bump the level of a color component with pinning */
+    cairo_set_source_surface (cr, surface, 0, 0);
+    cairo_paint (cr);
 
-const int HOVER_COMPONENT_ADDITION = 15;
+    cairo_set_operator (cr, CAIRO_OPERATOR_ADD);
 
-static guchar
-lighten_component (guchar cur_value)
-{
-    int new_value = cur_value;
-    new_value = cur_value + HOVER_COMPONENT_ADDITION;
-    if (new_value > 255)
-    {
-        new_value = 255;
-    }
-    return (guchar) new_value;
-}
+    cairo_push_group (cr);
 
-GdkPixbuf *
-eel_create_spotlight_pixbuf (GdkPixbuf *src)
-{
-    GdkPixbuf *dest;
-    int i, j;
-    int width, height, has_alpha, src_row_stride, dst_row_stride;
-    guchar *target_pixels, *original_pixels;
-    guchar *pixsrc, *pixdest;
+    /* This is *close enough* to the original look.
+     * The magic alpha value was selected after visual comparison.
+     */
+    cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.18);
+    cairo_paint (cr);
 
-    g_return_val_if_fail (gdk_pixbuf_get_colorspace (src) == GDK_COLORSPACE_RGB, NULL);
-    g_return_val_if_fail ((!gdk_pixbuf_get_has_alpha (src)
-                           && gdk_pixbuf_get_n_channels (src) == 3)
-                          || (gdk_pixbuf_get_has_alpha (src)
-                              && gdk_pixbuf_get_n_channels (src) == 4), NULL);
-    g_return_val_if_fail (gdk_pixbuf_get_bits_per_sample (src) == 8, NULL);
+    cairo_pop_group_to_source (cr);
 
-    dest = create_new_pixbuf (src);
+    cairo_mask_surface (cr, surface, 0.0, 0.0);
 
-    has_alpha = gdk_pixbuf_get_has_alpha (src);
-    width = gdk_pixbuf_get_width (src);
-    height = gdk_pixbuf_get_height (src);
-    dst_row_stride = gdk_pixbuf_get_rowstride (dest);
-    src_row_stride = gdk_pixbuf_get_rowstride (src);
-    target_pixels = gdk_pixbuf_get_pixels (dest);
-    original_pixels = gdk_pixbuf_get_pixels (src);
+    cairo_surface_flush (surface);
 
-    for (i = 0; i < height; i++)
-    {
-        pixdest = target_pixels + i * dst_row_stride;
-        pixsrc = original_pixels + i * src_row_stride;
-        for (j = 0; j < width; j++)
-        {
-            *pixdest++ = lighten_component (*pixsrc++);
-            *pixdest++ = lighten_component (*pixsrc++);
-            *pixdest++ = lighten_component (*pixsrc++);
-            if (has_alpha)
-            {
-                *pixdest++ = *pixsrc++;
-            }
-        }
-    }
-    return dest;
+    bytes = g_bytes_new (data, height * stride);
+    prelit_texture = gdk_memory_texture_new (width, height, GDK_MEMORY_B8G8R8A8_PREMULTIPLIED, bytes, 
stride);
+
+    cairo_destroy (cr);
+    cairo_surface_destroy (surface);
+
+    return prelit_texture;
 }
 
 /* This routine colorizes %src by multiplying each pixel with colors in %dest. */
diff --git a/eel/eel-graphic-effects.h b/eel/eel-graphic-effects.h
index 67de5f7cc..37ff698c0 100644
--- a/eel/eel-graphic-effects.h
+++ b/eel/eel-graphic-effects.h
@@ -21,11 +21,10 @@
 
 #pragma once
 
-#include <gdk-pixbuf/gdk-pixbuf.h>
 #include <gdk/gdk.h>
 
-/* return a lightened pixbuf for pre-lighting */
-GdkPixbuf *eel_create_spotlight_pixbuf (GdkPixbuf *source_pixbuf);
+/* return a lightened texture for pre-lighting */
+GdkTexture *eel_create_spotlight_texture (GdkTexture *texture);
 
 /* return a pixbuf colorized with the color specified by the parameters */
 GdkPixbuf* eel_create_colorized_pixbuf (GdkPixbuf *source_pixbuf,
diff --git a/src/nautilus-list-model.c b/src/nautilus-list-model.c
index dba6b99e4..7610c8769 100644
--- a/src/nautilus-list-model.c
+++ b/src/nautilus-list-model.c
@@ -423,20 +423,16 @@ nautilus_list_model_get_value (GtkTreeModel *tree_model,
 
                 icon = nautilus_file_get_icon_texture (file, icon_size, icon_scale, flags);
 
-#if 0 && NAUTILUS_CLIPBOARD_NEEDS_GTK4_REIMPLEMENTATION
                 if (priv->highlight_files != NULL &&
                     g_list_find_custom (priv->highlight_files,
                                         file, (GCompareFunc) nautilus_file_compare_location))
                 {
-                    rendered_icon = eel_create_spotlight_pixbuf (icon);
+                    g_autoptr (GdkTexture) prelit_texture = NULL;
 
-                    if (rendered_icon != NULL)
-                    {
-                        g_object_unref (icon);
-                        icon = rendered_icon;
-                    }
+                    prelit_texture = eel_create_spotlight_texture (icon);
+
+                    g_set_object (&icon, prelit_texture);
                 }
-#endif
 
                 g_value_set_object (value, icon);
                 g_object_unref (icon);


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