Blame gegl/operation/gegl-operation-point-composer.c

Packit bc1512
/* This file is part of GEGL
Packit bc1512
 *
Packit bc1512
 * GEGL is free software; you can redistribute it and/or
Packit bc1512
 * modify it under the terms of the GNU Lesser General Public
Packit bc1512
 * License as published by the Free Software Foundation; either
Packit bc1512
 * version 3 of the License, or (at your option) any later version.
Packit bc1512
 *
Packit bc1512
 * GEGL is distributed in the hope that it will be useful,
Packit bc1512
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit bc1512
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit bc1512
 * Lesser General Public License for more details.
Packit bc1512
 *
Packit bc1512
 * You should have received a copy of the GNU Lesser General Public
Packit bc1512
 * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
Packit bc1512
 *
Packit bc1512
 * Copyright 2006 Øyvind Kolås
Packit bc1512
 */
Packit bc1512
Packit bc1512
Packit bc1512
#include "config.h"
Packit bc1512
Packit bc1512
#include <glib-object.h>
Packit bc1512
Packit bc1512
#include "gegl.h"
Packit bc1512
#include "gegl/gegl-debug.h"
Packit bc1512
#include "gegl-types-internal.h"
Packit bc1512
#include "gegl-operation-point-composer.h"
Packit bc1512
#include "gegl-utils.h"
Packit bc1512
#include "graph/gegl-node.h"
Packit bc1512
#include "graph/gegl-pad.h"
Packit bc1512
#include <string.h>
Packit bc1512
Packit bc1512
static gboolean gegl_operation_point_composer_process
Packit bc1512
                              (GeglOperation       *operation,
Packit bc1512
                               GeglBuffer          *input,
Packit bc1512
                               GeglBuffer          *aux,
Packit bc1512
                               GeglBuffer          *output,
Packit bc1512
                               const GeglRectangle *result,
Packit bc1512
                               gint                 level);
Packit bc1512
Packit bc1512
static gboolean
Packit bc1512
gegl_operation_composer_process2 (GeglOperation        *operation,
Packit bc1512
                                  GeglOperationContext *context,
Packit bc1512
                                  const gchar          *output_prop,
Packit bc1512
                                  const GeglRectangle  *result,
Packit bc1512
                                  gint                  level);
Packit bc1512
Packit bc1512
G_DEFINE_TYPE (GeglOperationPointComposer, gegl_operation_point_composer, GEGL_TYPE_OPERATION_COMPOSER)
Packit bc1512
Packit bc1512
static void prepare (GeglOperation *operation)
Packit bc1512
{
Packit bc1512
  const Babl *format = babl_format ("RGBA float");
Packit bc1512
  gegl_operation_set_format (operation, "input", format);
Packit bc1512
  gegl_operation_set_format (operation, "aux", format);
Packit bc1512
  gegl_operation_set_format (operation, "output", format);
Packit bc1512
}
Packit bc1512
Packit bc1512
static void
Packit bc1512
gegl_operation_point_composer_class_init (GeglOperationPointComposerClass *klass)
Packit bc1512
{
Packit bc1512
  /*GObjectClass       *object_class    = G_OBJECT_CLASS (klass);*/
Packit bc1512
  GeglOperationClass *operation_class = GEGL_OPERATION_CLASS (klass);
Packit bc1512
  GeglOperationComposerClass *composer_class = GEGL_OPERATION_COMPOSER_CLASS (klass);
Packit bc1512
Packit bc1512
  composer_class->process = gegl_operation_point_composer_process;
Packit bc1512
  operation_class->prepare = prepare;
Packit bc1512
  operation_class->no_cache = FALSE;
Packit bc1512
  operation_class->process = gegl_operation_composer_process2;
Packit bc1512
Packit bc1512
  klass->process = NULL;
Packit bc1512
  klass->cl_process = NULL;
Packit bc1512
}
Packit bc1512
Packit bc1512
static void
Packit bc1512
gegl_operation_point_composer_init (GeglOperationPointComposer *self)
Packit bc1512
{
Packit bc1512
Packit bc1512
}
Packit bc1512
Packit bc1512
gboolean gegl_can_do_inplace_processing (GeglOperation       *operation,
Packit bc1512
                                         GeglBuffer          *input,
Packit bc1512
                                         const GeglRectangle *result);
Packit bc1512
Packit bc1512
/* we replicate the process function from GeglOperationComposer to be
Packit bc1512
 * able to bail out earlier for some common processing time pitfalls
Packit bc1512
 */
Packit bc1512
static gboolean
Packit bc1512
gegl_operation_composer_process2 (GeglOperation        *operation,
Packit bc1512
                                  GeglOperationContext *context,
Packit bc1512
                                  const gchar          *output_prop,
Packit bc1512
                                  const GeglRectangle  *result,
Packit bc1512
                                  gint                  level)
Packit bc1512
{
Packit bc1512
  GeglOperationComposerClass *klass   = GEGL_OPERATION_COMPOSER_GET_CLASS (operation);
Packit bc1512
  GeglBuffer                 *input;
Packit bc1512
  GeglBuffer                 *aux;
Packit bc1512
  GeglBuffer                 *output;
Packit bc1512
  gboolean                    success = FALSE;
Packit bc1512
Packit bc1512
  if (strcmp (output_prop, "output"))
Packit bc1512
    {
Packit bc1512
      g_warning ("requested processing of %s pad on a composer", output_prop);
Packit bc1512
      return FALSE;
Packit bc1512
    }
Packit bc1512
Packit bc1512
  input = gegl_operation_context_get_source (context, "input");
Packit bc1512
  aux   = gegl_operation_context_get_source (context, "aux");
Packit bc1512
Packit bc1512
  if (gegl_can_do_inplace_processing (operation, input, result))
Packit bc1512
    {
Packit bc1512
      output = g_object_ref (input);
Packit bc1512
      gegl_operation_context_take_object (context, "output", G_OBJECT (output));
Packit bc1512
    }
Packit bc1512
  else
Packit bc1512
    output = gegl_operation_context_get_target (context, "output");
Packit bc1512
Packit bc1512
    {
Packit bc1512
      gboolean done = FALSE;
Packit bc1512
Packit bc1512
      if (result->width == 0 ||
Packit bc1512
          result->height == 0)
Packit bc1512
        done = TRUE;
Packit bc1512
Packit bc1512
      success = done;
Packit bc1512
      if (!done)
Packit bc1512
        {
Packit bc1512
          success = klass->process (operation, input, aux, output, result, level);
Packit bc1512
Packit bc1512
          if (output == GEGL_BUFFER (operation->node->cache))
Packit bc1512
            gegl_cache_computed (operation->node->cache, result);
Packit bc1512
        }
Packit bc1512
      if (input)
Packit bc1512
         g_object_unref (input);
Packit bc1512
      if (aux)
Packit bc1512
         g_object_unref (aux);
Packit bc1512
    }
Packit bc1512
Packit bc1512
  return success;
Packit bc1512
}
Packit bc1512
Packit bc1512
static gboolean
Packit bc1512
gegl_operation_point_composer_cl_process (GeglOperation       *operation,
Packit bc1512
                                          GeglBuffer          *input,
Packit bc1512
                                          GeglBuffer          *aux,
Packit bc1512
                                          GeglBuffer          *output,
Packit bc1512
                                          const GeglRectangle *result,
Packit bc1512
                                          gint                 level)
Packit bc1512
{
Packit bc1512
  const Babl *in_format  = gegl_operation_get_format (operation, "input");
Packit bc1512
  const Babl *aux_format = gegl_operation_get_format (operation, "aux");
Packit bc1512
  const Babl *out_format = gegl_operation_get_format (operation, "output");
Packit bc1512
Packit bc1512
  GeglOperationPointComposerClass *point_composer_class = GEGL_OPERATION_POINT_COMPOSER_GET_CLASS (operation);
Packit bc1512
Packit bc1512
  gint j;
Packit bc1512
  cl_int cl_err = 0;
Packit bc1512
  gboolean err;
Packit bc1512
Packit bc1512
  /* non-texturizable format! */
Packit bc1512
  if (!gegl_cl_color_babl (in_format,  NULL) ||
Packit bc1512
      !gegl_cl_color_babl (aux_format, NULL) ||
Packit bc1512
      !gegl_cl_color_babl (out_format, NULL))
Packit bc1512
    {
Packit bc1512
      GEGL_NOTE (GEGL_DEBUG_OPENCL, "Non-texturizable format!");
Packit bc1512
      return FALSE;
Packit bc1512
    }
Packit bc1512
Packit bc1512
  /* Process */
Packit bc1512
  {
Packit bc1512
    GeglBufferClIterator *i = gegl_buffer_cl_iterator_new (output,   result, out_format, GEGL_CL_BUFFER_WRITE, GEGL_ABYSS_NONE);
Packit bc1512
                  gint read = gegl_buffer_cl_iterator_add (i, input, result, in_format,  GEGL_CL_BUFFER_READ, GEGL_ABYSS_NONE);
Packit bc1512
    if (aux)
Packit bc1512
      {
Packit bc1512
        gint foo = gegl_buffer_cl_iterator_add (i, aux, result, aux_format,  GEGL_CL_BUFFER_READ, GEGL_ABYSS_NONE);
Packit bc1512
Packit bc1512
        while (gegl_buffer_cl_iterator_next (i, &err))
Packit bc1512
          {
Packit bc1512
            if (err) return FALSE;
Packit bc1512
            for (j=0; j < i->n; j++)
Packit bc1512
              {
Packit bc1512
                cl_err = point_composer_class->cl_process(operation, i->tex[read][j], i->tex[foo][j], i->tex[0][j],
Packit bc1512
                                                          i->size[0][j], &i->roi[0][j], level);
Packit bc1512
                if (cl_err != CL_SUCCESS)
Packit bc1512
                  {
Packit bc1512
                    GEGL_NOTE (GEGL_DEBUG_OPENCL, "Error in %s [GeglOperationPointComposer] Kernel",
Packit bc1512
                               GEGL_OPERATION_CLASS (operation)->name);
Packit bc1512
                    return FALSE;
Packit bc1512
                  }
Packit bc1512
              }
Packit bc1512
          }
Packit bc1512
      }
Packit bc1512
    else
Packit bc1512
      {
Packit bc1512
        while (gegl_buffer_cl_iterator_next (i, &err))
Packit bc1512
          {
Packit bc1512
            if (err) return FALSE;
Packit bc1512
            for (j=0; j < i->n; j++)
Packit bc1512
              {
Packit bc1512
                cl_err = point_composer_class->cl_process(operation, i->tex[read][j], NULL, i->tex[0][j],
Packit bc1512
                                                          i->size[0][j], &i->roi[0][j], level);
Packit bc1512
                if (cl_err != CL_SUCCESS)
Packit bc1512
                  {
Packit bc1512
                    GEGL_NOTE (GEGL_DEBUG_OPENCL, "Error in %s [GeglOperationPointComposer] Kernel",
Packit bc1512
                               GEGL_OPERATION_CLASS (operation)->name);
Packit bc1512
                    return FALSE;
Packit bc1512
                  }
Packit bc1512
              }
Packit bc1512
          }
Packit bc1512
      }
Packit bc1512
  }
Packit bc1512
  return TRUE;
Packit bc1512
}
Packit bc1512
Packit bc1512
static gboolean
Packit bc1512
gegl_operation_point_composer_process (GeglOperation       *operation,
Packit bc1512
                                       GeglBuffer          *input,
Packit bc1512
                                       GeglBuffer          *aux,
Packit bc1512
                                       GeglBuffer          *output,
Packit bc1512
                                       const GeglRectangle *result,
Packit bc1512
                                       gint                 level)
Packit bc1512
{
Packit bc1512
  GeglOperationPointComposerClass *point_composer_class = GEGL_OPERATION_POINT_COMPOSER_GET_CLASS (operation);
Packit bc1512
  const Babl *in_format  = gegl_operation_get_format (operation, "input");
Packit bc1512
  const Babl *aux_format = gegl_operation_get_format (operation, "aux");
Packit bc1512
  const Babl *out_format = gegl_operation_get_format (operation, "output");
Packit bc1512
Packit bc1512
  if ((result->width > 0) && (result->height > 0))
Packit bc1512
    {
Packit bc1512
      if (gegl_cl_is_accelerated () && point_composer_class->cl_process)
Packit bc1512
        {
Packit bc1512
          if (gegl_operation_point_composer_cl_process (operation, input, aux, output, result, level))
Packit bc1512
            return TRUE;
Packit bc1512
        }
Packit bc1512
Packit bc1512
      {
Packit bc1512
        GeglBufferIterator *i = gegl_buffer_iterator_new (output, result, level, out_format, GEGL_BUFFER_WRITE, GEGL_ABYSS_NONE);
Packit bc1512
        gint read = /*output == input ? 0 :*/ gegl_buffer_iterator_add (i, input,  result, level, in_format, GEGL_BUFFER_READ, GEGL_ABYSS_NONE);
Packit bc1512
        /* using separate read and write iterators for in-place ideally a single
Packit bc1512
         * readwrite indice would be sufficient
Packit bc1512
         */
Packit bc1512
Packit bc1512
        if (aux)
Packit bc1512
          {
Packit bc1512
            gint foo = gegl_buffer_iterator_add (i, aux, result, level, aux_format, GEGL_BUFFER_READ, GEGL_ABYSS_NONE);
Packit bc1512
Packit bc1512
            while (gegl_buffer_iterator_next (i))
Packit bc1512
              {
Packit bc1512
                 point_composer_class->process (operation, i->data[read], i->data[foo], i->data[0], i->length, &(i->roi[0]), level);
Packit bc1512
              }
Packit bc1512
          }
Packit bc1512
        else
Packit bc1512
          {
Packit bc1512
            while (gegl_buffer_iterator_next (i))
Packit bc1512
              {
Packit bc1512
                 point_composer_class->process (operation, i->data[read], NULL, i->data[0], i->length, &(i->roi[0]), level);
Packit bc1512
              }
Packit bc1512
          }
Packit bc1512
      }
Packit bc1512
      return TRUE;
Packit bc1512
    }
Packit bc1512
  return TRUE;
Packit bc1512
}