[gtk/gamma-shenanigans] Add async png load/save code



commit 0ab4f7629efda4b3481d98c91b295fc003c6949f
Author: Matthias Clasen <mclasen redhat com>
Date:   Thu Sep 9 22:22:22 2021 -0400

    Add async png load/save code
    
    This will be used in content (de)serialization.

 gdk/gdkpng.c | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 gdk/gdkpng.h |  38 ++++++++++++++++-----
 2 files changed, 136 insertions(+), 9 deletions(-)
---
diff --git a/gdk/gdkpng.c b/gdk/gdkpng.c
index ca091acfa8..967c03a6a2 100644
--- a/gdk/gdkpng.c
+++ b/gdk/gdkpng.c
@@ -90,6 +90,7 @@ static cookie_io_functions_t cookie_funcs = {
 };
 
 /* }}} */
+/* {{{ Public API */
 
 GdkTexture *
 gdk_load_png (GInputStream  *stream,
@@ -174,4 +175,110 @@ gdk_save_png (GOutputStream    *stream,
   return result;
 }
 
+/* }}} */
+/* {{{ Async code */
+
+static void
+load_png_in_thread (GTask        *task,
+                    gpointer      source_object,
+                    gpointer      task_data,
+                    GCancellable *cancellable)
+{
+  GInputStream *stream = source_object;
+  GdkTexture *texture;
+  GError *error = NULL;
+
+  texture = gdk_load_png (stream, &error);
+
+  if (texture)
+    g_task_return_pointer (task, texture, g_object_unref);
+  else
+    g_task_return_error (task, error);
+}
+
+void
+gdk_load_png_async (GInputStream         *stream,
+                    GCancellable         *cancellable,
+                    GAsyncReadyCallback   callback,
+                    gpointer              user_data)
+{
+  GTask *task;
+
+  task = g_task_new (stream, cancellable, callback, user_data);
+  g_task_run_in_thread (task, load_png_in_thread);
+  g_object_unref (task);
+}
+
+GdkTexture *
+gdk_load_png_finish (GAsyncResult  *result,
+                     GError       **error)
+{
+  return g_task_propagate_pointer (G_TASK (result), error);
+}
+
+typedef struct {
+  const guchar *data;
+  int width;
+  int height;
+  GdkMemoryFormat format;
+} SavePngData;
+
+static void
+save_png_in_thread (GTask        *task,
+                    gpointer      source_object,
+                    gpointer      task_data,
+                    GCancellable *cancellable)
+{
+  GOutputStream *stream = source_object;
+  SavePngData *data = task_data;
+  GError *error = NULL;
+  gboolean result;
+
+  result = gdk_save_png (stream,
+                         data->data,
+                         data->width,
+                         data->height,
+                         data->format,
+                         &error);
+
+  if (result)
+    g_task_return_boolean (task, result);
+  else
+    g_task_return_error (task, error);
+}
+
+void
+gdk_save_png_async (GOutputStream          *stream,
+                    const guchar           *data,
+                    int                     width,
+                    int                     height,
+                    GdkMemoryFormat         format,
+                    GCancellable           *cancellable,
+                    GAsyncReadyCallback     callback,
+                    gpointer                user_data)
+{
+  GTask *task;
+  SavePngData *save_data;
+
+  save_data = g_new0 (SavePngData, 1);
+  save_data->data = data;
+  save_data->width = width;
+  save_data->height = height;
+  save_data->format = format;
+
+  task = g_task_new (stream, cancellable, callback, user_data);
+  g_task_set_task_data (task, save_data, g_free);
+  g_task_run_in_thread (task, save_png_in_thread);
+  g_object_unref (task);
+}
+
+gboolean
+gdk_save_png_finish (GAsyncResult  *result,
+                     GError       **error)
+{
+  return g_task_propagate_boolean (G_TASK (result), error);
+}
+
+/* }}} */
+
 /* vim:set foldmethod=marker expandtab: */
diff --git a/gdk/gdkpng.h b/gdk/gdkpng.h
index 36413aec72..59b4fdf7cc 100644
--- a/gdk/gdkpng.h
+++ b/gdk/gdkpng.h
@@ -21,14 +21,34 @@
 #include "gdkmemorytexture.h"
 #include <gio/gio.h>
 
-GdkTexture *gdk_load_png (GInputStream     *stream,
-                          GError          **error);
-
-gboolean    gdk_save_png (GOutputStream    *stream,
-                          const guchar     *data,
-                          int               width,
-                          int               height,
-                          GdkMemoryFormat   format,
-                          GError          **error);
+GdkTexture *gdk_load_png        (GInputStream           *stream,
+                                 GError                **error);
+
+void        gdk_load_png_async  (GInputStream           *stream,
+                                 GCancellable           *cancellable,
+                                 GAsyncReadyCallback     callback,
+                                 gpointer                user_data);
+
+GdkTexture *gdk_load_png_finish (GAsyncResult           *result,
+                                 GError                **error);
+
+gboolean    gdk_save_png        (GOutputStream          *stream,
+                                 const guchar           *data,
+                                 int                     width,
+                                 int                     height,
+                                 GdkMemoryFormat         format,
+                                 GError                **error);
+
+void        gdk_save_png_async  (GOutputStream          *stream,
+                                 const guchar           *data,
+                                 int                     width,
+                                 int                     height,
+                                 GdkMemoryFormat         format,
+                                 GCancellable           *cancellable,
+                                 GAsyncReadyCallback     callback,
+                                 gpointer                user_data);
+
+gboolean    gdk_save_png_finish (GAsyncResult           *result,
+                                 GError                **error);
 
 #endif


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