[gtk/image-loading: 13/18] Use our own loaders for content (de)serialization
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk/image-loading: 13/18] Use our own loaders for content (de)serialization
- Date: Tue, 14 Sep 2021 11:57:19 +0000 (UTC)
commit 56e9014f2e2ac6f53abb1aba27978cf563dd70e7
Author: Matthias Clasen <mclasen redhat com>
Date: Thu Sep 9 22:23:37 2021 -0400
Use our own loaders for content (de)serialization
Use our own loader to (de)serialiaze textures
to and from png and tiff.
We still fall back to gdk-pixbuf for handling all
the other image formats, and for pixbufs.
gdk/gdkcontentdeserializer.c | 109 +++++++++++++++++++++++++++++++------
gdk/gdkcontentserializer.c | 124 ++++++++++++++++++++++++++++++++++++-------
2 files changed, 198 insertions(+), 35 deletions(-)
---
diff --git a/gdk/gdkcontentdeserializer.c b/gdk/gdkcontentdeserializer.c
index 4959b5af4d..56a62f7ae3 100644
--- a/gdk/gdkcontentdeserializer.c
+++ b/gdk/gdkcontentdeserializer.c
@@ -25,6 +25,8 @@
#include "filetransferportalprivate.h"
#include "gdktexture.h"
#include "gdkrgbaprivate.h"
+#include "loaders/gdkpngprivate.h"
+#include "loaders/gdktiffprivate.h"
#include <gdk-pixbuf/gdk-pixbuf.h>
@@ -655,6 +657,56 @@ pixbuf_deserializer (GdkContentDeserializer *deserializer)
deserializer);
}
+static void
+texture_deserializer_finish (GObject *source,
+ GAsyncResult *result,
+ gpointer deserializer)
+{
+ GOutputStream *stream = G_OUTPUT_STREAM (source);
+ GBytes *bytes;
+ GError *error = NULL;
+ GdkTexture *texture = NULL;
+ gssize written;
+
+ written = g_output_stream_splice_finish (stream, result, &error);
+ if (written < 0)
+ {
+ gdk_content_deserializer_return_error (deserializer, error);
+ return;
+ }
+
+ bytes = g_memory_output_stream_steal_as_bytes (G_MEMORY_OUTPUT_STREAM (stream));
+
+ texture = gdk_texture_new_from_bytes (bytes, &error);
+ g_bytes_unref (bytes);
+ if (texture == NULL)
+ {
+ gdk_content_deserializer_return_error (deserializer, error);
+ return;
+ }
+
+ g_value_take_object (gdk_content_deserializer_get_value (deserializer), texture);
+ gdk_content_deserializer_return_success (deserializer);
+}
+
+static void
+texture_deserializer (GdkContentDeserializer *deserializer)
+{
+ GOutputStream *output;
+
+ output = g_memory_output_stream_new_resizable ();
+
+ g_output_stream_splice_async (output,
+ gdk_content_deserializer_get_input_stream (deserializer),
+ G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE
+ | G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
+ gdk_content_deserializer_get_priority (deserializer),
+ gdk_content_deserializer_get_cancellable (deserializer),
+ texture_deserializer_finish,
+ deserializer);
+ g_object_unref (output);
+}
+
static void
string_deserializer_finish (GObject *source,
GAsyncResult *result,
@@ -863,48 +915,71 @@ init (void)
initialized = TRUE;
+ gdk_content_register_deserializer ("image/png",
+ GDK_TYPE_TEXTURE,
+ texture_deserializer,
+ NULL,
+ NULL);
+ gdk_content_register_deserializer ("image/tiff",
+ GDK_TYPE_TEXTURE,
+ texture_deserializer,
+ NULL,
+ NULL);
+ gdk_content_register_deserializer ("image/jpeg",
+ 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)
{
GdkPixbufFormat *fmt = f->data;
char **mimes, **m;
+ char *name;
+ name = gdk_pixbuf_format_get_name (fmt);
mimes = gdk_pixbuf_format_get_mime_types (fmt);
for (m = mimes; *m; m++)
- {
- gdk_content_register_deserializer (*m,
- GDK_TYPE_TEXTURE,
- pixbuf_deserializer,
- NULL,
- NULL);
+ {
+ /* Turning pngs, jpegs and tiffs into textures is handled above */
+ if (!g_str_equal (name, "png") &&
+ !g_str_equal (name, "jpeg") &&
+ !g_str_equal (name, "tiff"))
+ gdk_content_register_deserializer (*m,
+ GDK_TYPE_TEXTURE,
+ pixbuf_deserializer,
+ NULL,
+ NULL);
gdk_content_register_deserializer (*m,
GDK_TYPE_PIXBUF,
pixbuf_deserializer,
NULL,
NULL);
- }
+ }
g_strfreev (mimes);
+ g_free (name);
}
g_slist_free (formats);
diff --git a/gdk/gdkcontentserializer.c b/gdk/gdkcontentserializer.c
index bc9deeb8d5..e0bf34bd53 100644
--- a/gdk/gdkcontentserializer.c
+++ b/gdk/gdkcontentserializer.c
@@ -26,6 +26,9 @@
#include "filetransferportalprivate.h"
#include "gdktextureprivate.h"
#include "gdkrgba.h"
+#include "loaders/gdkpngprivate.h"
+#include "loaders/gdktiffprivate.h"
+#include "gdkmemorytextureprivate.h"
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <string.h>
@@ -606,6 +609,7 @@ gdk_content_serialize_finish (GAsyncResult *result,
/*** SERIALIZERS ***/
+
static void
pixbuf_serializer_finish (GObject *source,
GAsyncResult *res,
@@ -658,6 +662,75 @@ pixbuf_serializer (GdkContentSerializer *serializer)
g_object_unref (pixbuf);
}
+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;
+ const GValue *value;
+ GdkTexture *texture;
+ GBytes *bytes = NULL;
+ GError *error = NULL;
+ gboolean result = FALSE;
+ GInputStream *input;
+ gssize spliced;
+
+ value = gdk_content_serializer_get_value (serializer);
+ texture = g_value_get_object (value);
+
+ if (strcmp (gdk_content_serializer_get_mime_type (serializer), "image/png") == 0)
+ bytes = gdk_save_png (texture);
+ else if (strcmp (gdk_content_serializer_get_mime_type (serializer), "image/tiff") == 0)
+ bytes = gdk_save_tiff (texture);
+ else
+ g_assert_not_reached ();
+
+ input = g_memory_input_stream_new_from_bytes (bytes);
+ spliced = g_output_stream_splice (gdk_content_serializer_get_output_stream (serializer),
+ input,
+ G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE,
+ gdk_content_serializer_get_cancellable (serializer),
+ &error);
+ g_object_unref (input);
+ g_bytes_unref (bytes);
+
+ result = spliced != -1;
+
+ if (result)
+ g_task_return_boolean (task, result);
+ else
+ g_task_return_error (task, error);
+}
+
+static void
+texture_serializer (GdkContentSerializer *serializer)
+{
+ GTask *task;
+
+ task = g_task_new (serializer,
+ gdk_content_serializer_get_cancellable (serializer),
+ texture_serializer_finish,
+ NULL);
+ g_task_run_in_thread (task, serialize_texture_in_thread);
+ g_object_unref (task);
+}
+
static void
string_serializer_finish (GObject *source,
GAsyncResult *result,
@@ -877,51 +950,66 @@ init (void)
initialized = TRUE;
+ gdk_content_register_serializer (GDK_TYPE_TEXTURE,
+ "image/png",
+ texture_serializer,
+ NULL, NULL);
+
+ gdk_content_register_serializer (GDK_TYPE_TEXTURE,
+ "image/tiff",
+ 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)
{
GdkPixbufFormat *fmt = f->data;
char **mimes, **m;
+ char *name;
if (!gdk_pixbuf_format_is_writable (fmt))
- continue;
+ continue;
+ name = gdk_pixbuf_format_get_name (fmt);
mimes = gdk_pixbuf_format_get_mime_types (fmt);
for (m = mimes; *m; m++)
- {
- gdk_content_register_serializer (GDK_TYPE_TEXTURE,
- *m,
- pixbuf_serializer,
- gdk_pixbuf_format_get_name (fmt),
- g_free);
+ {
+ /* Turning textures into pngs or tiffs is handled above */
+ if (!g_str_equal (name, "png") &&
+ !g_str_equal (name, "tiff"))
+ gdk_content_register_serializer (GDK_TYPE_TEXTURE,
+ *m,
+ pixbuf_serializer,
+ gdk_pixbuf_format_get_name (fmt),
+ g_free);
gdk_content_register_serializer (GDK_TYPE_PIXBUF,
*m,
pixbuf_serializer,
gdk_pixbuf_format_get_name (fmt),
g_free);
- }
+ }
g_strfreev (mimes);
+ g_free (name);
}
g_slist_free (formats);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]