[gtk/image-loading: 9/14] Use our png loader for content (de)serialization
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk/image-loading: 9/14] Use our png loader for content (de)serialization
- Date: Sun, 12 Sep 2021 23:36:42 +0000 (UTC)
commit 1cbd0d420de4d17e4323baa3db220c6d290d26a7
Author: Matthias Clasen <mclasen redhat com>
Date: Thu Sep 9 22:23:37 2021 -0400
Use our png loader for content (de)serialization
We still fall back to gdk-pixbuf for handling all
the other image formats. But with this in place,
we no longer need to tweak the pixbuf formats to
ensure that png comes first.
gdk/gdkcontentdeserializer.c | 115 +++++++++++++++++++++++++++++++++++----
gdk/gdkcontentserializer.c | 124 +++++++++++++++++++++++++++++++++++++++----
2 files changed, 219 insertions(+), 20 deletions(-)
---
diff --git a/gdk/gdkcontentdeserializer.c b/gdk/gdkcontentdeserializer.c
index 4959b5af4d..64a0f3b1db 100644
--- a/gdk/gdkcontentdeserializer.c
+++ b/gdk/gdkcontentdeserializer.c
@@ -25,6 +25,7 @@
#include "filetransferportalprivate.h"
#include "gdktexture.h"
#include "gdkrgbaprivate.h"
+#include "loaders/gdkpngprivate.h"
#include <gdk-pixbuf/gdk-pixbuf.h>
@@ -655,6 +656,95 @@ pixbuf_deserializer (GdkContentDeserializer *deserializer)
deserializer);
}
+static void
+texture_deserializer_finish (GObject *source,
+ GAsyncResult *res,
+ gpointer data)
+{
+ GdkContentDeserializer *deserializer = GDK_CONTENT_DESERIALIZER (source);
+ GdkTexture *texture;
+ GValue *value;
+ GError *error = NULL;
+
+ texture = g_task_propagate_pointer (G_TASK (res), &error);
+ if (texture == NULL)
+ {
+ gdk_content_deserializer_return_error (deserializer, error);
+ return;
+ }
+
+ value = gdk_content_deserializer_get_value (deserializer);
+ g_value_take_object (value, texture);
+ gdk_content_deserializer_return_success (deserializer);
+}
+
+static GBytes *
+read_all_data (GInputStream *source,
+ GError **error)
+{
+ GOutputStream *output;
+ gssize size;
+ GBytes *bytes;
+
+ output = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
+ size = g_output_stream_splice (output, source, 0, NULL, error);
+ if (size == -1)
+ {
+ g_object_unref (output);
+ return NULL;
+ }
+
+ g_output_stream_close (output, NULL, NULL);
+ bytes = g_memory_output_stream_steal_as_bytes (G_MEMORY_OUTPUT_STREAM (output));
+ g_object_unref (output);
+
+ return bytes;
+}
+
+static void
+deserialize_texture_in_thread (GTask *task,
+ gpointer source_object,
+ gpointer task_data,
+ GCancellable *cancellable)
+{
+ GdkContentDeserializer *deserializer = source_object;
+ GError *error = NULL;
+ GdkTexture *texture = NULL;
+ GBytes *bytes;
+
+ bytes = read_all_data (gdk_content_deserializer_get_input_stream (deserializer), &error);
+ if (!bytes)
+ {
+ g_task_return_error (task, error);
+ return;
+ }
+
+ if (strcmp (gdk_content_deserializer_get_mime_type (deserializer), "image/png") == 0)
+ texture = gdk_load_png (bytes, &error);
+ else
+ g_assert_not_reached ();
+
+ g_bytes_unref (bytes);
+
+ if (texture)
+ g_task_return_pointer (task, texture, g_object_unref);
+ else
+ g_task_return_error (task, error);
+}
+
+static void
+texture_deserializer (GdkContentDeserializer *deserializer)
+{
+ GTask *task;
+
+ task = g_task_new (deserializer,
+ gdk_content_deserializer_get_cancellable (deserializer),
+ texture_deserializer_finish,
+ NULL);
+ g_task_run_in_thread (task, deserialize_texture_in_thread);
+ g_object_unref (task);
+}
+
static void
string_deserializer_finish (GObject *source,
GAsyncResult *result,
@@ -863,27 +953,32 @@ init (void)
initialized = TRUE;
+ gdk_content_register_deserializer ("image/png",
+ GDK_TYPE_TEXTURE,
+ texture_deserializer,
+ NULL,
+ NULL);
+
formats = gdk_pixbuf_get_formats ();
/* Make sure png comes first */
for (f = formats; f; f = f->next)
{
GdkPixbufFormat *fmt = f->data;
- char *name;
-
+ char *name;
+
name = gdk_pixbuf_format_get_name (fmt);
if (g_str_equal (name, "png"))
- {
- formats = g_slist_delete_link (formats, f);
- formats = g_slist_prepend (formats, fmt);
-
- g_free (name);
+ {
+ formats = g_slist_delete_link (formats, f);
+ formats = g_slist_prepend (formats, fmt);
- break;
- }
+ g_free (name);
+ break;
+ }
g_free (name);
- }
+ }
for (f = formats; f; f = f->next)
{
diff --git a/gdk/gdkcontentserializer.c b/gdk/gdkcontentserializer.c
index bc9deeb8d5..f884595630 100644
--- a/gdk/gdkcontentserializer.c
+++ b/gdk/gdkcontentserializer.c
@@ -26,6 +26,8 @@
#include "filetransferportalprivate.h"
#include "gdktextureprivate.h"
#include "gdkrgba.h"
+#include "loaders/gdkpngprivate.h"
+#include "gdkmemorytextureprivate.h"
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <string.h>
@@ -606,6 +608,7 @@ gdk_content_serialize_finish (GAsyncResult *result,
/*** SERIALIZERS ***/
+
static void
pixbuf_serializer_finish (GObject *source,
GAsyncResult *res,
@@ -658,6 +661,103 @@ pixbuf_serializer (GdkContentSerializer *serializer)
g_object_unref (pixbuf);
}
+typedef struct {
+ GBytes *bytes;
+ int width;
+ int height;
+ int stride;
+ GdkMemoryFormat format;
+} TextureSerializerData;
+
+static void
+texture_serializer_data_free (gpointer data)
+{
+ TextureSerializerData *sdata = data;
+
+ g_bytes_unref (sdata->bytes);
+ g_free (data);
+}
+
+static void
+texture_serializer_finish (GObject *source,
+ GAsyncResult *res,
+ gpointer user_data)
+{
+ GdkContentSerializer *serializer = GDK_CONTENT_SERIALIZER (source);
+ GError *error = NULL;
+
+ if (!g_task_propagate_boolean (G_TASK (res), &error))
+ gdk_content_serializer_return_error (serializer, error);
+ else
+ gdk_content_serializer_return_success (serializer);
+}
+
+static void
+serialize_texture_in_thread (GTask *task,
+ gpointer source_object,
+ gpointer task_data,
+ GCancellable *cancellable)
+{
+ GdkContentSerializer *serializer = source_object;
+ TextureSerializerData *data = task_data;
+ GError *error = NULL;
+ gboolean result;
+
+ if (strcmp (gdk_content_serializer_get_mime_type (serializer), "image/png") == 0)
+ result = gdk_save_png (gdk_content_serializer_get_output_stream (serializer),
+ g_bytes_get_data (data->bytes, NULL),
+ data->width,
+ data->height,
+ data->stride,
+ data->format,
+ &error);
+ else
+ g_assert_not_reached ();
+
+ if (result)
+ g_task_return_boolean (task, result);
+ else
+ g_task_return_error (task, error);
+}
+
+static void
+texture_serializer (GdkContentSerializer *serializer)
+{
+ const GValue *value;
+ GdkTexture *texture;
+ int width, height;
+ guchar *buffer;
+ GBytes *bytes = NULL;
+ TextureSerializerData *data;
+ GTask *task;
+
+ value = gdk_content_serializer_get_value (serializer);
+
+ texture = g_value_get_object (value);
+
+ width = gdk_texture_get_width (texture);
+ height = gdk_texture_get_height (texture);
+ buffer = g_malloc (width * height * 4);
+
+ gdk_texture_download (texture, buffer, width * 4);
+ bytes = g_bytes_new_take (buffer, width * height * 4);
+
+ data = g_new0 (TextureSerializerData, 1);
+ data->bytes = bytes;
+ data->width = width;
+ data->height = height;
+ data->stride = width * 4;
+ data->format = GDK_MEMORY_DEFAULT;
+
+ task = g_task_new (serializer,
+ gdk_content_serializer_get_cancellable (serializer),
+ texture_serializer_finish,
+ NULL);
+ g_task_set_task_data (task, data, texture_serializer_data_free);
+ g_task_run_in_thread (task, serialize_texture_in_thread);
+ g_object_unref (task);
+}
+
static void
string_serializer_finish (GObject *source,
GAsyncResult *result,
@@ -877,27 +977,31 @@ init (void)
initialized = TRUE;
+ gdk_content_register_serializer (GDK_TYPE_TEXTURE,
+ "image/png",
+ texture_serializer,
+ NULL, NULL);
+
formats = gdk_pixbuf_get_formats ();
/* Make sure png comes first */
for (f = formats; f; f = f->next)
{
GdkPixbufFormat *fmt = f->data;
- char *name;
-
+ char *name;
+
name = gdk_pixbuf_format_get_name (fmt);
if (g_str_equal (name, "png"))
- {
- formats = g_slist_delete_link (formats, f);
- formats = g_slist_prepend (formats, fmt);
-
- g_free (name);
+ {
+ formats = g_slist_delete_link (formats, f);
+ formats = g_slist_prepend (formats, fmt);
- break;
- }
+ g_free (name);
+ break;
+ }
g_free (name);
- }
+ }
for (f = formats; f; f = f->next)
{
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]