[gtk/wip/otte/for-master: 3/4] gdk: Rework gdk_pixbuf_get_from_texture()




commit dcba78338949d2e1edaf30cd580ddf348d39f181
Author: Benjamin Otte <otte redhat com>
Date:   Sun Sep 26 00:22:20 2021 +0200

    gdk: Rework gdk_pixbuf_get_from_texture()
    
    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   |  6 +-----
 gdk/gdkmemoryformat.c        | 12 ++++++------
 gdk/gdkmemoryformatprivate.h |  7 +++++++
 gdk/gdkpixbuf-drawable.c     | 34 ++++++++++++++++++++++++----------
 4 files changed, 38 insertions(+), 21 deletions(-)
---
diff --git a/gdk/gdkcontentserializer.c b/gdk/gdkcontentserializer.c
index 6faaa0121a..d359c558f8 100644
--- a/gdk/gdkcontentserializer.c
+++ b/gdk/gdkcontentserializer.c
@@ -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
     {
diff --git a/gdk/gdkmemoryformat.c b/gdk/gdkmemoryformat.c
index 940e9cea17..60c3317bc8 100644
--- a/gdk/gdkmemoryformat.c
+++ b/gdk/gdkmemoryformat.c
@@ -27,12 +27,6 @@
 
 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)
 {
diff --git a/gdk/gdkmemoryformatprivate.h b/gdk/gdkmemoryformatprivate.h
index bdcb2265e6..bd75192dfe 100644
--- a/gdk/gdkmemoryformatprivate.h
+++ b/gdk/gdkmemoryformatprivate.h
@@ -24,7 +24,14 @@
 
 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,
diff --git a/gdk/gdkpixbuf-drawable.c b/gdk/gdkpixbuf-drawable.c
index 8cf98feeaf..15b886357e 100644
--- a/gdk/gdkpixbuf-drawable.c
+++ b/gdk/gdkpixbuf-drawable.c
@@ -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);
 }


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