[gegl] buffer,opencl: split opencl code from GeglBuffer



commit c3c9b63f5fcca2b023d64177d959079215e889b8
Author: Øyvind Kolås <pippin gimp org>
Date:   Mon Sep 10 14:05:50 2018 +0200

    buffer,opencl: split opencl code from GeglBuffer
    
    The integration between the opencl buffer cache and GeglBuffer consists of
    two (three) function calls that are conditionally done for updates are
    correctly propagated between opencl / cpu.
    
    This commit in work towards making GeglBuffer possible to reuse as a separate
    libraryt without needing to link and load opencl replaces these conditionals
    with a check whether a function pointer is set, and if it is set it uses it.
    The opencl code sets/unsets the function pointers depending on whether it is
    active.

 gegl/buffer/Makefile.am                           |  2 --
 gegl/buffer/gegl-buffer-access.c                  | 16 +++++++++-------
 gegl/buffer/gegl-buffer-iterator.c                |  9 ++++-----
 gegl/buffer/gegl-buffer-private.h                 |  6 ++++++
 gegl/buffer/gegl-buffer.c                         | 17 +++++++++++++----
 gegl/buffer/gegl-sampler.c                        | 14 +++++++-------
 gegl/buffer/gegl-tile-handler-cache.c             | 15 +++++++--------
 gegl/opencl/Makefile.am                           |  6 +++++-
 gegl/{buffer => opencl}/gegl-buffer-cl-cache.c    |  0
 gegl/{buffer => opencl}/gegl-buffer-cl-cache.h    |  0
 gegl/{buffer => opencl}/gegl-buffer-cl-iterator.c |  0
 gegl/{buffer => opencl}/gegl-buffer-cl-iterator.h |  0
 gegl/opencl/gegl-cl-init.c                        | 17 +++++++++++++++++
 13 files changed, 68 insertions(+), 34 deletions(-)
---
diff --git a/gegl/buffer/Makefile.am b/gegl/buffer/Makefile.am
index 5c96011ea..e27620562 100644
--- a/gegl/buffer/Makefile.am
+++ b/gegl/buffer/Makefile.am
@@ -26,8 +26,6 @@ libbuffer_la_SOURCES = \
     gegl-buffer-access.c       \
     gegl-buffer-index.h                \
     gegl-buffer-iterator.c     \
-    gegl-buffer-cl-iterator.c  \
-    gegl-buffer-cl-cache.c     \
     gegl-buffer-linear.c       \
        gegl-buffer-load.c      \
     gegl-buffer-save.c         \
diff --git a/gegl/buffer/gegl-buffer-access.c b/gegl/buffer/gegl-buffer-access.c
index 43d6be692..76335ab7f 100644
--- a/gegl/buffer/gegl-buffer-access.c
+++ b/gegl/buffer/gegl-buffer-access.c
@@ -37,8 +37,8 @@
 #include "gegl-tile-backend.h"
 #include "gegl-buffer-iterator.h"
 #include "gegl-buffer-iterator-private.h"
-#include "gegl-buffer-cl-cache.h"
 #include "gegl-config.h"
+#include "gegl-types-internal.h"
 
 static void gegl_buffer_iterate_read_fringed (GeglBuffer          *buffer,
                                               const GeglRectangle *roi,
@@ -862,9 +862,9 @@ gegl_buffer_set_internal (GeglBuffer          *buffer,
                           const void          *src,
                           gint                 rowstride)
 {
-  if (gegl_cl_is_accelerated ())
+  if (gegl_buffer_ext_flush)
     {
-      gegl_buffer_cl_cache_flush (buffer, rect);
+      gegl_buffer_ext_flush (buffer, rect);
     }
 
   gegl_buffer_iterate_write (buffer, rect, (void *) src, rowstride, format, level);
@@ -2023,9 +2023,9 @@ _gegl_buffer_get_unlocked (GeglBuffer          *buffer,
   if (format == NULL)
     format = buffer->soft_format;
 
-  if (gegl_cl_is_accelerated ())
+  if (gegl_buffer_ext_flush)
     {
-      gegl_buffer_cl_cache_flush (buffer, rect);
+      gegl_buffer_ext_flush (buffer, rect);
     }
 
   if (scale == 1.0 &&
@@ -2551,8 +2551,10 @@ gegl_buffer_clear2 (GeglBuffer          *dst,
 
   pxsize = babl_format_get_bytes_per_pixel (dst->soft_format);
 
-  if (gegl_cl_is_accelerated ())
-    gegl_buffer_cl_cache_invalidate (dst, dst_rect);
+  if (gegl_buffer_ext_invalidate)
+    {
+      gegl_buffer_ext_invalidate (dst, dst_rect);
+    }
 
   i = gegl_buffer_iterator_new (dst, dst_rect, 0, dst->soft_format,
                                 GEGL_ACCESS_WRITE | GEGL_ITERATOR_NO_NOTIFY,
diff --git a/gegl/buffer/gegl-buffer-iterator.c b/gegl/buffer/gegl-buffer-iterator.c
index 0adfb6610..ceb60daab 100644
--- a/gegl/buffer/gegl-buffer-iterator.c
+++ b/gegl/buffer/gegl-buffer-iterator.c
@@ -30,7 +30,6 @@
 #include "gegl-buffer-iterator.h"
 #include "gegl-buffer-iterator-private.h"
 #include "gegl-buffer-private.h"
-#include "gegl-buffer-cl-cache.h"
 #include "gegl-config.h"
 
 typedef enum {
@@ -666,11 +665,11 @@ gegl_buffer_iterator_next (GeglBufferIterator *iter)
           && FALSE) /* XXX: conditions are not strict enough, GIMPs TIFF
                        plug-in fails; but GEGLs buffer test suite passes */
       {
-        if (gegl_cl_is_accelerated ())
+        if (gegl_buffer_ext_flush)
           for (index = 0; index < priv->num_buffers; index++)
             {
               SubIterState *sub = &priv->sub_iter[index];
-              gegl_buffer_cl_cache_flush (sub->buffer, &sub->full_rect);
+              gegl_buffer_ext_flush (sub->buffer, &sub->full_rect);
             }
         linear_shortcut (iter);
         return TRUE;
@@ -678,11 +677,11 @@ gegl_buffer_iterator_next (GeglBufferIterator *iter)
 
       prepare_iteration (iter);
 
-      if (gegl_cl_is_accelerated ())
+      if (gegl_buffer_ext_flush)
         for (index = 0; index < priv->num_buffers; index++)
           {
             SubIterState *sub = &priv->sub_iter[index];
-            gegl_buffer_cl_cache_flush (sub->buffer, &sub->full_rect);
+            gegl_buffer_ext_flush (sub->buffer, &sub->full_rect);
           }
 
       initialize_rects (iter);
diff --git a/gegl/buffer/gegl-buffer-private.h b/gegl/buffer/gegl-buffer-private.h
index e858d89ac..0c507ce22 100644
--- a/gegl/buffer/gegl-buffer-private.h
+++ b/gegl/buffer/gegl-buffer-private.h
@@ -206,6 +206,12 @@ gboolean gegl_buffer_scan_compatible (GeglBuffer *bufferA,
                                       gint        xB,
                                       gint        yB);
 
+
+extern void (*gegl_tile_handler_cache_ext_flush) (void *tile_handler_cache, const GeglRectangle *rect);
+extern void (*gegl_buffer_ext_flush) (GeglBuffer *buffer, const GeglRectangle *rect);
+extern void (*gegl_buffer_ext_invalidate) (GeglBuffer *buffer, const GeglRectangle *rect);
+
+
 #ifndef __GEGL_TILE_H__
 #define gegl_tile_get_data(tile)  ((tile)->data)
 #endif
diff --git a/gegl/buffer/gegl-buffer.c b/gegl/buffer/gegl-buffer.c
index e07019e1e..2c73023d3 100644
--- a/gegl/buffer/gegl-buffer.c
+++ b/gegl/buffer/gegl-buffer.c
@@ -44,9 +44,12 @@
 #include "gegl-tile-backend-file.h"
 #include "gegl-tile-backend-swap.h"
 #include "gegl-tile-backend-ram.h"
-#include "gegl-buffer-cl-cache.h"
+
+//#include "opencl/gegl-cl.h"
 #include "gegl-config.h" /* XXX: include of file outside buffer dir */
 
+#include "gegl-types-internal.h"
+
 #ifdef GEGL_ENABLE_DEBUG
 #define DEBUG_ALLOCATIONS (gegl_debug_flags & GEGL_DEBUG_BUFFER_ALLOC)
 #endif
@@ -279,6 +282,8 @@ static GList         *allocated_buffers_list = NULL;
 static volatile gint  allocated_buffers      = 0;
 static volatile gint  de_allocated_buffers   = 0;
 
+
+
 /* this should only be possible if this buffer matches all the buffers down to
  * storage, all of those parent buffers would change size as well, no tiles
  * would be voided as a result of changing the extent.
@@ -372,8 +377,8 @@ gegl_buffer_dispose (GObject *object)
   GeglBuffer  *buffer  = GEGL_BUFFER (object);
   GeglTileHandler *handler = GEGL_TILE_HANDLER (object);
 
-  if (gegl_cl_is_accelerated ())
-    gegl_buffer_cl_cache_invalidate (GEGL_BUFFER (object), NULL);
+  if (gegl_buffer_ext_flush)
+    gegl_buffer_ext_flush (buffer, NULL);
 
   if (GEGL_IS_TILE_STORAGE (handler->source))
     {
@@ -525,7 +530,7 @@ gegl_buffer_constructor (GType                  type,
           if (!buffer->format)
             {
               g_warning ("Buffer constructed without format, assuming RGBA float");
-              buffer->format = gegl_babl_rgba_linear_float ();
+              buffer->format = babl_format ("RGBA float");
             }
 
           /* make a new backend & storage */
@@ -1235,3 +1240,7 @@ gegl_buffer_get_tile (GeglBuffer *buffer,
 
   return tile;
 }
+
+void (*gegl_tile_handler_cache_ext_flush) (void *cache, const GeglRectangle *rect)=NULL;
+void (*gegl_buffer_ext_flush) (GeglBuffer *buffer, const GeglRectangle *rect)=NULL;
+void (*gegl_buffer_ext_invalidate) (GeglBuffer *buffer, const GeglRectangle *rect)=NULL;
diff --git a/gegl/buffer/gegl-sampler.c b/gegl/buffer/gegl-sampler.c
index f099fad5d..2064b46e6 100644
--- a/gegl/buffer/gegl-sampler.c
+++ b/gegl/buffer/gegl-sampler.c
@@ -28,13 +28,13 @@
 #include "gegl-buffer-types.h"
 #include "gegl-buffer.h"
 #include "gegl-buffer-private.h"
-#include "gegl-buffer-cl-cache.h"
 
 #include "gegl-sampler-nearest.h"
 #include "gegl-sampler-linear.h"
 #include "gegl-sampler-cubic.h"
 #include "gegl-sampler-nohalo.h"
 #include "gegl-sampler-lohalo.h"
+#include "gegl-types-internal.h"
 
 #include "gegl-config.h"
 
@@ -169,10 +169,10 @@ gegl_sampler_get (GeglSampler     *self,
     return;
   }
 
-  if (gegl_cl_is_accelerated ())
+  if (gegl_buffer_ext_flush)
     {
       GeglRectangle rect={x,y,1,1};
-      gegl_buffer_cl_cache_flush (self->buffer, &rect);
+      gegl_buffer_ext_flush (self->buffer, &rect);
     }
   self->get (self, x, y, scale, output, repeat_mode);
 }
@@ -268,10 +268,10 @@ gegl_sampler_get_from_mipmap (GeglSampler    *sampler,
   const gint maximum_width  = GEGL_SAMPLER_MAXIMUM_WIDTH;
   const gint maximum_height = GEGL_SAMPLER_MAXIMUM_HEIGHT;
 
-  if (gegl_cl_is_accelerated ())
+  if (gegl_buffer_ext_flush)
     {
       GeglRectangle rect = {x, y, 1, 1};
-      gegl_buffer_cl_cache_flush (sampler->buffer, &rect);
+      gegl_buffer_ext_flush (sampler->buffer, &rect);
     }
 
   g_assert (level_no >= 0 && level_no < GEGL_SAMPLER_MIPMAP_LEVELS);
@@ -554,8 +554,8 @@ GeglSamplerGetFun gegl_sampler_get_fun (GeglSampler *sampler)
   /* this flushes the buffer in preparation for the use of the sampler,
    * thus one can consider the handed out sampler function only temporarily
    * available*/
-  if (gegl_cl_is_accelerated ())
-    gegl_buffer_cl_cache_flush (sampler->buffer, NULL);
+  if (gegl_buffer_ext_flush)
+    gegl_buffer_ext_flush (sampler->buffer, NULL);
   return sampler->get;
 }
 
diff --git a/gegl/buffer/gegl-tile-handler-cache.c b/gegl/buffer/gegl-tile-handler-cache.c
index 93c51e8ee..4b5df4aba 100644
--- a/gegl/buffer/gegl-tile-handler-cache.c
+++ b/gegl/buffer/gegl-tile-handler-cache.c
@@ -26,10 +26,9 @@
 #include "gegl-buffer-private.h"
 #include "gegl-tile.h"
 #include "gegl-tile-handler-cache.h"
+#include "gegl-tile-storage.h"
 #include "gegl-debug.h"
 
-#include "gegl-buffer-cl-cache.h"
-
 /*
 #define GEGL_DEBUG_CACHE_HITS
 */
@@ -193,8 +192,8 @@ gegl_tile_handler_cache_get_tile_command (GeglTileSource *tile_store,
   GeglTileSource       *source   = ((GeglTileHandler*) (tile_store))->source;
   GeglTile             *tile     = NULL;
 
-  if (G_UNLIKELY (gegl_cl_is_accelerated ()))
-    gegl_buffer_cl_cache_flush2 (cache, NULL);
+  if (gegl_tile_handler_cache_ext_flush)
+    gegl_tile_handler_cache_ext_flush (cache, NULL);
 
   tile = gegl_tile_handler_cache_get_tile (cache, x, y, z);
   if (tile)
@@ -233,8 +232,8 @@ gegl_tile_handler_cache_command (GeglTileSource  *tile_store,
         {
           GList *link;
 
-          if (gegl_cl_is_accelerated ())
-            gegl_buffer_cl_cache_flush2 (cache, NULL);
+          if (gegl_tile_handler_cache_ext_flush)
+            gegl_tile_handler_cache_ext_flush (cache, NULL);
 
           for (link = g_queue_peek_head_link (&cache->queue);
                link;
@@ -655,8 +654,8 @@ gegl_tile_handler_cache_copy (GeglTileHandlerCache     *cache,
   GeglTile *dst_tile = NULL;
   gboolean  success  = FALSE;
 
-  if (G_UNLIKELY (gegl_cl_is_accelerated ()))
-    gegl_buffer_cl_cache_flush2 (cache, NULL);
+  if (gegl_tile_handler_cache_ext_flush)
+    gegl_tile_handler_cache_ext_flush (cache, NULL);
 
   tile = gegl_tile_handler_cache_get_tile (cache, x, y, z);
 
diff --git a/gegl/opencl/Makefile.am b/gegl/opencl/Makefile.am
index 1447c0b3c..3f0214728 100644
--- a/gegl/opencl/Makefile.am
+++ b/gegl/opencl/Makefile.am
@@ -43,7 +43,11 @@ libcl_sources = \
        gegl-cl-color.h \
        gegl-cl-introspection-support.h \
        gegl-cl-random.c \
-       gegl-cl-random.h
+       gegl-cl-random.h \
+       gegl-buffer-cl-iterator.c \
+       gegl-buffer-cl-cache.c \
+       gegl-buffer-cl-iterator.h \
+       gegl-buffer-cl-cache.h
 
 noinst_LTLIBRARIES = libcl.la
 
diff --git a/gegl/buffer/gegl-buffer-cl-cache.c b/gegl/opencl/gegl-buffer-cl-cache.c
similarity index 100%
rename from gegl/buffer/gegl-buffer-cl-cache.c
rename to gegl/opencl/gegl-buffer-cl-cache.c
diff --git a/gegl/buffer/gegl-buffer-cl-cache.h b/gegl/opencl/gegl-buffer-cl-cache.h
similarity index 100%
rename from gegl/buffer/gegl-buffer-cl-cache.h
rename to gegl/opencl/gegl-buffer-cl-cache.h
diff --git a/gegl/buffer/gegl-buffer-cl-iterator.c b/gegl/opencl/gegl-buffer-cl-iterator.c
similarity index 100%
rename from gegl/buffer/gegl-buffer-cl-iterator.c
rename to gegl/opencl/gegl-buffer-cl-iterator.c
diff --git a/gegl/buffer/gegl-buffer-cl-iterator.h b/gegl/opencl/gegl-buffer-cl-iterator.h
similarity index 100%
rename from gegl/buffer/gegl-buffer-cl-iterator.h
rename to gegl/opencl/gegl-buffer-cl-iterator.h
diff --git a/gegl/opencl/gegl-cl-init.c b/gegl/opencl/gegl-cl-init.c
index f02093ad8..322cab0ba 100644
--- a/gegl/opencl/gegl-cl-init.c
+++ b/gegl/opencl/gegl-cl-init.c
@@ -39,6 +39,9 @@
 
 #include "gegl/gegl-debug.h"
 
+#include "gegl/buffer/gegl-buffer-private.h"
+#include "gegl-buffer-cl-cache.h"
+
 GQuark gegl_opencl_error_quark (void);
 
 GQuark
@@ -170,6 +173,10 @@ void
 gegl_cl_disable (void)
 {
   _gegl_cl_is_accelerated = FALSE;
+
+  gegl_buffer_ext_flush = NULL;
+  gegl_buffer_ext_invalidate = NULL;
+  gegl_tile_handler_cache_ext_flush = NULL;
 }
 
 void
@@ -177,6 +184,10 @@ gegl_cl_hard_disable (void)
 {
   cl_state.hard_disable = TRUE;
   _gegl_cl_is_accelerated = FALSE;
+
+  gegl_buffer_ext_flush = NULL;
+  gegl_buffer_ext_invalidate = NULL;
+  gegl_tile_handler_cache_ext_flush = NULL;
 }
 
 cl_platform_id
@@ -779,6 +790,12 @@ gegl_cl_init_common (cl_device_type          requested_device_type,
   if (cl_state.is_loaded)
     _gegl_cl_is_accelerated = TRUE;
 
+  {
+    gegl_buffer_ext_flush = (void*)gegl_buffer_cl_cache_flush;
+    gegl_buffer_ext_invalidate = (void*)gegl_buffer_cl_cache_invalidate;
+    gegl_tile_handler_cache_ext_flush = (void*)gegl_buffer_cl_cache_flush2;
+  }
+
   return TRUE;
 }
 


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