[gtk+/wip/otte/clipboard: 97/102] gdk: Add (de)serializers for GDK_TYPE_TEXTURE



commit 234cba66e2d4f661c21e1ea0b6b6eaa4c50d4303
Author: Benjamin Otte <otte redhat com>
Date:   Sat Dec 2 14:36:57 2017 +0100

    gdk: Add (de)serializers for GDK_TYPE_TEXTURE
    
    We want to use textures more, so we should be able to use them for
    copy/paste and dnd.

 gdk/gdkcontentdeserializer.c |   25 +++++++++++++++++++++++--
 gdk/gdkcontentserializer.c   |   35 +++++++++++++++++++++++++++++++++--
 2 files changed, 56 insertions(+), 4 deletions(-)
---
diff --git a/gdk/gdkcontentdeserializer.c b/gdk/gdkcontentdeserializer.c
index 22f911d..64390b9 100644
--- a/gdk/gdkcontentdeserializer.c
+++ b/gdk/gdkcontentdeserializer.c
@@ -22,6 +22,7 @@
 #include "gdkcontentdeserializer.h"
 
 #include "gdkcontentformats.h"
+#include "gdktexture.h"
 
 #include <gdk-pixbuf/gdk-pixbuf.h>
 
@@ -439,6 +440,7 @@ pixbuf_deserializer_finish (GObject      *source,
                             gpointer      deserializer)
 {
   GdkPixbuf *pixbuf;
+  GValue *value;
   GError *error = NULL;
 
   pixbuf = gdk_pixbuf_new_from_stream_finish (res, &error);
@@ -448,8 +450,22 @@ pixbuf_deserializer_finish (GObject      *source,
       return;
     }
 
-  g_value_take_object (gdk_content_deserializer_get_value (deserializer),
-                       pixbuf);
+  value = gdk_content_deserializer_get_value (deserializer);
+  if (G_VALUE_HOLDS (value, GDK_TYPE_PIXBUF))
+    {
+      g_value_take_object (value, pixbuf);
+    }
+  else if (G_VALUE_HOLDS (value, GDK_TYPE_TEXTURE))
+    {
+      GdkTexture *texture;
+      texture = gdk_texture_new_for_pixbuf (pixbuf);
+      g_object_unref (pixbuf);
+      g_value_take_object (value, texture);
+    }
+  else
+    {
+      g_assert_not_reached ();
+    }
   gdk_content_deserializer_return_success (deserializer);
 }
 
@@ -633,6 +649,11 @@ init (void)
       for (m = mimes; *m; m++)
        {
           gdk_content_register_deserializer (*m,
+                                             GDK_TYPE_TEXTURE,
+                                             pixbuf_deserializer,
+                                             NULL,
+                                             NULL);
+          gdk_content_register_deserializer (*m,
                                              GDK_TYPE_PIXBUF,
                                              pixbuf_deserializer,
                                              NULL,
diff --git a/gdk/gdkcontentserializer.c b/gdk/gdkcontentserializer.c
index 6153e82..d4421ce 100644
--- a/gdk/gdkcontentserializer.c
+++ b/gdk/gdkcontentserializer.c
@@ -22,6 +22,8 @@
 #include "gdkcontentserializer.h"
 
 #include "gdkcontentformats.h"
+#include "gdkpixbuf.h"
+#include "gdktextureprivate.h"
 
 #include <gdk-pixbuf/gdk-pixbuf.h>
 #include <string.h>
@@ -449,9 +451,32 @@ pixbuf_serializer_finish (GObject      *source,
 static void
 pixbuf_serializer (GdkContentSerializer *serializer)
 {
-  const char *name = gdk_content_serializer_get_user_data (serializer);
+  const GValue *value;
+  GdkPixbuf *pixbuf;
+  const char *name;
+  
+  name = gdk_content_serializer_get_user_data (serializer);
+  value = gdk_content_serializer_get_value (serializer);
 
-  gdk_pixbuf_save_to_stream_async (g_value_get_object (gdk_content_serializer_get_value (serializer)),
+  if (G_VALUE_HOLDS (value, GDK_TYPE_PIXBUF))
+    {
+      pixbuf = g_value_dup_object (value);
+    }
+  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);
+    }
+  else
+    {
+      g_assert_not_reached ();
+    }
+
+  gdk_pixbuf_save_to_stream_async (pixbuf,
                                    gdk_content_serializer_get_output_stream (serializer),
                                    name,
                                   gdk_content_serializer_get_cancellable (serializer),
@@ -459,6 +484,7 @@ pixbuf_serializer (GdkContentSerializer *serializer)
                                    serializer,
                                    g_str_equal (name, "png") ? "compression" : NULL, "2",
                                    NULL);
+  g_object_unref (pixbuf);
 }
 
 static void
@@ -669,6 +695,11 @@ init (void)
       mimes = gdk_pixbuf_format_get_mime_types (fmt);
       for (m = mimes; *m; m++)
        {
+          gdk_content_register_serializer (GDK_TYPE_TEXTURE,
+                                           *m,
+                                           pixbuf_serializer,
+                                           g_strdup (gdk_pixbuf_format_get_name (fmt)),
+                                           g_free);
           gdk_content_register_serializer (GDK_TYPE_PIXBUF,
                                            *m,
                                            pixbuf_serializer,


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