[gegl] buffer: add GeglTileBackendBuffer



commit d67dc06639df55eb2f8cd9bb64e20608b5421d27
Author: Ell <ell_se yahoo com>
Date:   Thu May 3 16:06:26 2018 -0400

    buffer: add GeglTileBackendBuffer
    
    GeglTileBackendBuffer is a tile backend that loads/stores its tiles
    from/to an underlying GeglBuffer.

 gegl/buffer/Makefile.am                |    2 +
 gegl/buffer/gegl-tile-backend-buffer.c |  299 ++++++++++++++++++++++++++++++++
 gegl/buffer/gegl-tile-backend-buffer.h |   56 ++++++
 3 files changed, 357 insertions(+), 0 deletions(-)
---
diff --git a/gegl/buffer/Makefile.am b/gegl/buffer/Makefile.am
index 80ac65e..5c96011 100644
--- a/gegl/buffer/Makefile.am
+++ b/gegl/buffer/Makefile.am
@@ -43,6 +43,7 @@ libbuffer_la_SOURCES = \
     gegl-tile-source.c         \
     gegl-tile-storage.c                \
     gegl-tile-backend.c                \
+    gegl-tile-backend-buffer.c \
        gegl-tile-backend-file-async.c  \
     gegl-tile-backend-ram.c    \
        gegl-tile-backend-swap.c \
@@ -74,6 +75,7 @@ libbuffer_la_SOURCES = \
     gegl-tile-source.h         \
     gegl-tile-storage.h                \
     gegl-tile-backend.h                \
+    gegl-tile-backend-buffer.h \
     gegl-tile-backend-file.h   \
        gegl-tile-backend-swap.h \
     gegl-tile-backend-ram.h    \
diff --git a/gegl/buffer/gegl-tile-backend-buffer.c b/gegl/buffer/gegl-tile-backend-buffer.c
new file mode 100644
index 0000000..b060ab2
--- /dev/null
+++ b/gegl/buffer/gegl-tile-backend-buffer.c
@@ -0,0 +1,299 @@
+/* This file is part of GEGL.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright 2018 Ell
+ */
+
+#include "config.h"
+
+#include <glib-object.h>
+
+#include "gegl-buffer-types.h"
+
+#include "gegl-config.h"
+
+#include "gegl-buffer-backend.h"
+#include "gegl-buffer-private.h"
+#include "gegl-tile-backend.h"
+#include "gegl-tile-backend-buffer.h"
+#include "gegl-tile-handler-cache.h"
+#include "gegl-tile-storage.h"
+
+
+enum
+{
+  PROP_0,
+  PROP_BUFFER
+};
+
+
+/*  local function prototypes  */
+
+static void       gegl_tile_backend_buffer_dispose         (GObject               *object);
+static void       gegl_tile_backend_buffer_set_property    (GObject               *object,
+                                                            guint                  property_id,
+                                                            const GValue          *value,
+                                                            GParamSpec            *pspec);
+static void       gegl_tile_backend_buffer_get_property    (GObject               *object,
+                                                            guint                  property_id,
+                                                            GValue                *value,
+                                                            GParamSpec            *pspec);
+
+static gpointer   gegl_tile_backend_buffer_command         (GeglTileSource        *tile_source,
+                                                            GeglTileCommand        command,
+                                                            gint                   x,
+                                                            gint                   y,
+                                                            gint                   z,
+                                                            gpointer               data);
+
+static GeglTile * gegl_tile_backend_buffer_get_tile        (GeglTileBackendBuffer *tile_backend_buffer,
+                                                            gint                   x,
+                                                            gint                   y,
+                                                            gint                   z);
+static void       gegl_tile_backend_buffer_set_tile        (GeglTileBackendBuffer *tile_backend_buffer,
+                                                            GeglTile              *tile,
+                                                            gint                   x,
+                                                            gint                   y,
+                                                            gint                   z);
+static gpointer   gegl_tile_backend_buffer_forward_command (GeglTileBackendBuffer *tile_backend_buffer,
+                                                            GeglTileCommand        command,
+                                                            gint                   x,
+                                                            gint                   y,
+                                                            gint                   z,
+                                                            gpointer               data);
+
+
+G_DEFINE_TYPE (GeglTileBackendBuffer, gegl_tile_backend_buffer, GEGL_TYPE_TILE_BACKEND)
+
+#define parent_class gegl_tile_backend_buffer_parent_class
+
+
+/*  private functions  */
+
+
+static void
+gegl_tile_backend_buffer_class_init (GeglTileBackendBufferClass *klass)
+{
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+
+  gobject_class->dispose      = gegl_tile_backend_buffer_dispose;
+  gobject_class->set_property = gegl_tile_backend_buffer_set_property;
+  gobject_class->get_property = gegl_tile_backend_buffer_get_property;
+
+  g_object_class_install_property (gobject_class, PROP_BUFFER,
+                                   g_param_spec_object ("buffer", NULL, NULL,
+                                                        GEGL_TYPE_BUFFER,
+                                                        G_PARAM_READWRITE |
+                                                        G_PARAM_CONSTRUCT_ONLY));
+}
+
+static void
+gegl_tile_backend_buffer_init (GeglTileBackendBuffer *tile_backend_buffer)
+{
+  GeglTileSource *tile_source = GEGL_TILE_SOURCE (tile_backend_buffer);
+
+  tile_source->command = gegl_tile_backend_buffer_command;
+}
+
+static void
+gegl_tile_backend_buffer_dispose (GObject *object)
+{
+  GeglTileBackendBuffer *tile_backend_buffer = GEGL_TILE_BACKEND_BUFFER (object);
+
+  g_clear_object (&tile_backend_buffer->buffer);
+
+  G_OBJECT_CLASS (parent_class)->dispose (object);
+}
+
+static void
+gegl_tile_backend_buffer_set_property (GObject      *object,
+                                       guint         property_id,
+                                       const GValue *value,
+                                       GParamSpec   *pspec)
+{
+  GeglTileBackendBuffer *tile_backend_buffer = GEGL_TILE_BACKEND_BUFFER (object);
+
+  switch (property_id)
+    {
+    case PROP_BUFFER:
+      if (tile_backend_buffer->buffer)
+        g_object_unref (tile_backend_buffer->buffer);
+
+      tile_backend_buffer->buffer = g_value_dup_object (value);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+      break;
+    }
+}
+
+static void
+gegl_tile_backend_buffer_get_property (GObject    *object,
+                                       guint       property_id,
+                                       GValue     *value,
+                                       GParamSpec *pspec)
+{
+  GeglTileBackendBuffer *tile_backend_buffer = GEGL_TILE_BACKEND_BUFFER (object);
+
+  switch (property_id)
+    {
+    case PROP_BUFFER:
+      g_value_set_object (value, tile_backend_buffer->buffer);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+      break;
+    }
+}
+
+static gpointer
+gegl_tile_backend_buffer_command (GeglTileSource  *tile_source,
+                                  GeglTileCommand  command,
+                                  gint             x,
+                                  gint             y,
+                                  gint             z,
+                                  gpointer         data)
+{
+  GeglTileBackendBuffer *tile_backend_buffer = GEGL_TILE_BACKEND_BUFFER (tile_source);
+
+  if (! tile_backend_buffer->buffer)
+    return NULL;
+
+  switch (command)
+    {
+    case GEGL_TILE_GET:
+      return gegl_tile_backend_buffer_get_tile (tile_backend_buffer,
+                                                x, y, z);
+
+    case GEGL_TILE_SET:
+      gegl_tile_backend_buffer_set_tile (tile_backend_buffer,
+                                         data, x, y, z);
+
+      return NULL;
+
+    case GEGL_TILE_VOID:
+    case GEGL_TILE_EXIST:
+      return gegl_tile_backend_buffer_forward_command (tile_backend_buffer,
+                                                       command, x, y, z, data);
+
+    default:
+      g_return_val_if_fail (command >= 0 && command < GEGL_TILE_LAST_COMMAND,
+                            NULL);
+
+      return NULL;
+    }
+}
+
+static GeglTile *
+gegl_tile_backend_buffer_get_tile (GeglTileBackendBuffer *tile_backend_buffer,
+                                   gint                   x,
+                                   gint                   y,
+                                   gint                   z)
+{
+  GeglBuffer *buffer = tile_backend_buffer->buffer;
+  GeglTile   *src_tile;
+  GeglTile   *tile   = NULL;
+
+  src_tile = gegl_buffer_get_tile (buffer, x, y, z);
+
+  if (src_tile)
+    {
+      tile = gegl_tile_dup (src_tile);
+
+      gegl_tile_unref (src_tile);
+
+      gegl_tile_mark_as_stored (tile);
+    }
+
+  return tile;
+}
+
+static void
+gegl_tile_backend_buffer_set_tile (GeglTileBackendBuffer *tile_backend_buffer,
+                                   GeglTile              *tile,
+                                   gint                   x,
+                                   gint                   y,
+                                   gint                   z)
+{
+  GeglBuffer           *buffer = tile_backend_buffer->buffer;
+  GeglTileHandlerCache *cache  = buffer->tile_storage->cache;
+  GeglTile             *dest_tile;
+
+  dest_tile = gegl_tile_dup (tile);
+
+  if (gegl_config_threads()>1)
+    g_rec_mutex_lock (&buffer->tile_storage->mutex);
+
+  gegl_tile_handler_cache_insert (cache, dest_tile, x, y, z);
+
+  if (gegl_config_threads()>1)
+    g_rec_mutex_unlock (&buffer->tile_storage->mutex);
+
+  gegl_tile_unref (dest_tile);
+
+  if (buffer->changed_signal_connections)
+    {
+      GeglRectangle rect;
+
+      rect.width  = buffer->tile_width  >> z;
+      rect.height = buffer->tile_height >> z;
+      rect.x      = x * rect.width  - buffer->shift_x;
+      rect.y      = y * rect.height - buffer->shift_y;
+
+      gegl_buffer_emit_changed_signal (buffer, &rect);
+    }
+}
+
+static gpointer
+gegl_tile_backend_buffer_forward_command (GeglTileBackendBuffer *tile_backend_buffer,
+                                          GeglTileCommand        command,
+                                          gint                   x,
+                                          gint                   y,
+                                          gint                   z,
+                                          gpointer               data)
+{
+  GeglBuffer     *buffer = tile_backend_buffer->buffer;
+  GeglTileSource *source = GEGL_TILE_SOURCE (buffer);
+  gpointer        result;
+
+  if (gegl_config_threads()>1)
+    g_rec_mutex_lock (&buffer->tile_storage->mutex);
+
+  result = gegl_tile_source_command (source, command, x, y, z, data);
+
+  if (gegl_config_threads()>1)
+    g_rec_mutex_unlock (&buffer->tile_storage->mutex);
+
+  return result;
+}
+
+
+/*  public functions  */
+
+
+GeglTileBackend *
+gegl_tile_backend_buffer_new (GeglBuffer *buffer)
+{
+  g_return_val_if_fail (GEGL_IS_BUFFER (buffer), NULL);
+
+  return g_object_new (GEGL_TYPE_TILE_BACKEND_BUFFER,
+                       "tile-width",  buffer->tile_width,
+                       "tile-height", buffer->tile_height,
+                       "format",      buffer->soft_format,
+                       "buffer",      buffer,
+                       NULL);
+}
diff --git a/gegl/buffer/gegl-tile-backend-buffer.h b/gegl/buffer/gegl-tile-backend-buffer.h
new file mode 100644
index 0000000..c95ecd6
--- /dev/null
+++ b/gegl/buffer/gegl-tile-backend-buffer.h
@@ -0,0 +1,56 @@
+/* This file is part of GEGL.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright 2018 Ell
+ */
+
+#ifndef __GEGL_TILE_BACKEND_BUFFER_H__
+#define __GEGL_TILE_BACKEND_BUFFER_H__
+
+#include <glib.h>
+#include "gegl-tile-backend.h"
+
+G_BEGIN_DECLS
+
+#define GEGL_TYPE_TILE_BACKEND_BUFFER            (gegl_tile_backend_buffer_get_type ())
+#define GEGL_TILE_BACKEND_BUFFER(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), 
GEGL_TYPE_TILE_BACKEND_BUFFER, GeglTileBackendBuffer))
+#define GEGL_TILE_BACKEND_BUFFER_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass),  
GEGL_TYPE_TILE_BACKEND_BUFFER, GeglTileBackendBufferClass))
+#define GEGL_IS_TILE_BACKEND_BUFFER(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), 
GEGL_TYPE_TILE_BACKEND_BUFFER))
+#define GEGL_IS_TILE_BACKEND_BUFFER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass),  
GEGL_TYPE_TILE_BACKEND_BUFFER))
+#define GEGL_TILE_BACKEND_BUFFER_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj),  
GEGL_TYPE_TILE_BACKEND_BUFFER, GeglTileBackendBufferClass))
+
+
+typedef struct _GeglTileBackendBuffer      GeglTileBackendBuffer;
+typedef struct _GeglTileBackendBufferClass GeglTileBackendBufferClass;
+
+struct _GeglTileBackendBuffer
+{
+  GeglTileBackend  parent_instance;
+
+  GeglBuffer      *buffer;
+};
+
+struct _GeglTileBackendBufferClass
+{
+  GeglTileBackendClass parent_class;
+};
+
+GType             gegl_tile_backend_buffer_get_type (void) G_GNUC_CONST;
+
+GeglTileBackend * gegl_tile_backend_buffer_new      (GeglBuffer *buffer);
+
+G_END_DECLS
+
+#endif


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