[gtk/wip/otte/color-profiles: 14/18] gdk: Rework gdk_pixbuf_get_from_texture()




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

    gdk: Rework gdk_pixbuf_get_from_texture()

 gdk/gdkcontentserializer.c   |  6 +-----
 gdk/gdkmemoryformat.c        | 12 ++++++------
 gdk/gdkmemoryformatprivate.h |  7 +++++++
 gdk/gdkpixbuf-drawable.c     | 39 ++++++++++++++++++++++++++++-----------
 4 files changed, 42 insertions(+), 22 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 7ba631c203..65d945b0fd 100644
--- a/gdk/gdkmemoryformat.c
+++ b/gdk/gdkmemoryformat.c
@@ -28,12 +28,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, \
@@ -333,6 +327,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 26e84e737b..348319c09b 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_gl_format         (GdkMemoryFormat             format,
                                                              gboolean                    gles,
diff --git a/gdk/gdkpixbuf-drawable.c b/gdk/gdkpixbuf-drawable.c
index 8cf98feeaf..a4b82b7759 100644
--- a/gdk/gdkpixbuf-drawable.c
+++ b/gdk/gdkpixbuf-drawable.c
@@ -24,6 +24,9 @@
 
 #include "gdkpixbuf.h"
 
+#include "gdkcolorprofile.h"
+#include "gdkmemoryformatprivate.h"
+#include "gdkmemorytextureprivate.h"
 #include "gdksurface.h"
 #include "gdktextureprivate.h"
 
@@ -214,6 +217,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 +239,24 @@ 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;
-
-  g_return_val_if_fail (GDK_IS_TEXTURE (texture), NULL);
+  GdkMemoryTexture *memtex;
+  gboolean alpha;
 
-  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 (gdk_texture_download_texture (texture));
+  alpha = gdk_memory_format_alpha (gdk_memory_texture_get_format (memtex)) != GDK_MEMORY_ALPHA_OPAQUE;
 
-  return pixbuf;
+  memtex = gdk_memory_texture_convert (memtex,
+                                       alpha ? GDK_MEMORY_GDK_PIXBUF_ALPHA
+                                             : GDK_MEMORY_GDK_PIXBUF_OPAQUE,
+                                       gdk_color_profile_get_srgb (),
+                                       NULL);
+  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]