gdk: Rework gdk_pixbuf_get_from_texture()
authorBenjamin Otte <otte@redhat.com>
Sat, 25 Sep 2021 22:22:20 +0000 (00:22 +0200)
committerBenjamin Otte <otte@redhat.com>
Mon, 18 Oct 2021 10:08:57 +0000 (12:08 +0200)
Make it use gdk_memory_texture_from_texture().

Also make gdk_memory_format_alpha() privately available so that we can
detect if an image contains an alpha channel.

gdk/gdkcontentserializer.c
gdk/gdkmemoryformat.c
gdk/gdkmemoryformatprivate.h
gdk/gdkpixbuf-drawable.c

index 6faaa0121af03bd599c0f1769ce89f9c1d3b79cc..d359c558f84dc937db0e6ab520bea85b092f8fd4 100644 (file)
@@ -641,11 +641,7 @@ pixbuf_serializer (GdkContentSerializer *serializer)
   else if (G_VALUE_HOLDS (value, GDK_TYPE_TEXTURE))
     {
       GdkTexture *texture = g_value_get_object (value);
-      cairo_surface_t *surface = gdk_texture_download_surface (texture);
-      pixbuf = gdk_pixbuf_get_from_surface (surface,
-                                            0, 0,
-                                            gdk_texture_get_width (texture), gdk_texture_get_height (texture));
-      cairo_surface_destroy (surface);
+      pixbuf = gdk_pixbuf_get_from_texture (texture);
     }
   else
     {
index 940e9cea1701e0e261ddb090eafaf87dc899a005..60c3317bc82fe402d6ff72f72759a1b6510463b0 100644 (file)
 
 typedef struct _GdkMemoryFormatDescription GdkMemoryFormatDescription;
 
-typedef enum {
-  GDK_MEMORY_ALPHA_PREMULTIPLIED,
-  GDK_MEMORY_ALPHA_STRAIGHT,
-  GDK_MEMORY_ALPHA_OPAQUE
-} GdkMemoryAlpha;
-
 #define TYPED_FUNCS(name, T, R, G, B, A, bpp, scale) \
 static void \
 name ## _to_float (float        *dest, \
@@ -386,6 +380,12 @@ gdk_memory_format_bytes_per_pixel (GdkMemoryFormat format)
   return memory_formats[format].bytes_per_pixel;
 }
 
+GdkMemoryAlpha
+gdk_memory_format_alpha (GdkMemoryFormat format)
+{
+  return memory_formats[format].alpha;
+}
+
 gsize
 gdk_memory_format_alignment (GdkMemoryFormat format)
 {
index bdcb2265e60f4fd7dd1b243b7573be02de7189bb..bd75192dfe8f3feb4d57adf1c197b710188d9323 100644 (file)
 
 G_BEGIN_DECLS
 
+typedef enum {
+  GDK_MEMORY_ALPHA_PREMULTIPLIED,
+  GDK_MEMORY_ALPHA_STRAIGHT,
+  GDK_MEMORY_ALPHA_OPAQUE
+} GdkMemoryAlpha;
+
 gsize                   gdk_memory_format_alignment         (GdkMemoryFormat             format) G_GNUC_CONST;
+GdkMemoryAlpha          gdk_memory_format_alpha             (GdkMemoryFormat             format) G_GNUC_CONST;
 gsize                   gdk_memory_format_bytes_per_pixel   (GdkMemoryFormat             format) G_GNUC_CONST;
 gboolean                gdk_memory_format_prefers_high_depth(GdkMemoryFormat             format) G_GNUC_CONST;
 gboolean                gdk_memory_format_gl_format         (GdkMemoryFormat             format,
index 8cf98feeafadf958b447fd90613ac9cbcc418587..15b886357e052f03f7dcba8b7486ed2fe0fff637 100644 (file)
@@ -24,6 +24,8 @@
 
 #include "gdkpixbuf.h"
 
+#include "gdkmemoryformatprivate.h"
+#include "gdkmemorytextureprivate.h"
 #include "gdksurface.h"
 #include "gdktextureprivate.h"
 
@@ -214,6 +216,13 @@ gdk_pixbuf_get_from_surface (cairo_surface_t *surface,
   return dest;
 }
 
+static void
+pixbuf_texture_unref_cb (guchar   *pixels,
+                         gpointer  texture)
+{
+  g_object_unref (texture);
+}
+
 /**
  * gdk_pixbuf_get_from_texture:
  * @texture: a `GdkTexture`
@@ -229,17 +238,22 @@ gdk_pixbuf_get_from_surface (cairo_surface_t *surface,
 GdkPixbuf *
 gdk_pixbuf_get_from_texture (GdkTexture *texture)
 {
-  GdkPixbuf *pixbuf;
-  cairo_surface_t *surface;
-  int width, height;
+  GdkMemoryTexture *memtex;
+  gboolean alpha;
 
-  g_return_val_if_fail (GDK_IS_TEXTURE (texture), NULL);
+  alpha = gdk_memory_format_alpha (gdk_texture_get_format (texture)) != GDK_MEMORY_ALPHA_OPAQUE;
 
-  width = gdk_texture_get_width (texture);
-  height = gdk_texture_get_height (texture);
-  surface = gdk_texture_download_surface (texture);
-  pixbuf = gdk_pixbuf_get_from_surface (surface, 0, 0, width, height);
-  cairo_surface_destroy (surface);
+  memtex = gdk_memory_texture_from_texture (texture,
+                                            alpha ? GDK_MEMORY_GDK_PIXBUF_ALPHA
+                                                  : GDK_MEMORY_GDK_PIXBUF_OPAQUE);
 
-  return pixbuf;
+  return gdk_pixbuf_new_from_data (gdk_memory_texture_get_data (memtex),
+                                   GDK_COLORSPACE_RGB,
+                                   alpha,
+                                   8,
+                                   gdk_texture_get_width (GDK_TEXTURE (memtex)),
+                                   gdk_texture_get_height (GDK_TEXTURE (memtex)),
+                                   gdk_memory_texture_get_stride (memtex),
+                                   pixbuf_texture_unref_cb,
+                                   memtex);
 }