write-buffer.c 4.75 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* This file is an image processing operation for GEGL
 *
 * GEGL 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.
 *
 * GEGL 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 2006 Øyvind Kolås <pippin@gimp.org>
 */
18 19 20 21 22

#include "config.h"
#include <glib/gi18n-lib.h>


23
#ifdef GEGL_PROPERTIES
24

25 26
property_object (buffer, _("Buffer location"), GEGL_TYPE_BUFFER)
    description(_("A pre-existing GeglBuffer to write incoming buffer data to."))
27 28 29

#else

30 31
#define GEGL_OP_SINK
#define GEGL_OP_C_FILE "write-buffer.c"
32

33
#include "gegl-op.h"
34

35 36
#include "gegl/gegl-debug.h"
#include "opencl/gegl-cl.h"
37
#include "gegl-buffer-cl-iterator.h"
38

39 40 41
static gboolean
process (GeglOperation       *operation,
         GeglBuffer          *input,
42 43
         const GeglRectangle *result,
         gint                 level)
44
{
45
  GeglProperties *o = GEGL_PROPERTIES (operation);
46 47 48 49

  if (o->buffer)
    {
      GeglBuffer *output = GEGL_BUFFER (o->buffer);
50 51
      const Babl *in_format = gegl_buffer_get_format (input);
      const Babl *out_format = gegl_buffer_get_format (output);
52

53
      if (gegl_operation_use_opencl (operation)
54
          && gegl_cl_color_supported (in_format, out_format) == GEGL_CL_COLOR_CONVERT)
55 56 57
        {
          size_t size;
          gboolean err;
58
          cl_int cl_err = 0;
59

60 61
          GeglBufferClIterator *i = gegl_buffer_cl_iterator_new (output,
                                                                 result,
62
                                                                 out_format,
63 64 65 66 67
                                                                 GEGL_CL_BUFFER_WRITE);

          gint read = gegl_buffer_cl_iterator_add (i,
                                                   input,
                                                   result,
68
                                                   out_format,
69 70
                                                   GEGL_CL_BUFFER_READ,
                                                   GEGL_ABYSS_NONE);
71

72
          gegl_cl_color_babl (out_format, &size);
73

74 75 76 77 78
          GEGL_NOTE (GEGL_DEBUG_OPENCL,
                     "write-buffer: "
                     "%p %p %s %s {%d %d %d %d}",
                     input,
                     output,
79 80
                     babl_get_name (in_format),
                     babl_get_name (out_format),
81 82 83 84
                     result->x,
                     result->y,
                     result->width,
                     result->height);
85

86 87 88
          while (gegl_buffer_cl_iterator_next (i, &err))
            {
              if (err) break;
89 90 91 92 93 94 95 96 97 98 99 100

              cl_err = gegl_clEnqueueCopyBuffer (gegl_cl_get_command_queue (),
                                                 i->tex[read],
                                                 i->tex[0],
                                                 0,
                                                 0,
                                                 i->size[0] * size,
                                                 0,
                                                 NULL,
                                                 NULL);

              if (cl_err != CL_SUCCESS)
101
                {
102
                  GEGL_NOTE (GEGL_DEBUG_OPENCL, "Error: %s", gegl_cl_errstring (cl_err));
103
                  break;
104 105
                }
            }
Victor Oliveira's avatar
Victor Oliveira committed
106 107 108

          if (cl_err || err)
            gegl_buffer_copy (input, result, output, result);
109 110 111
        }
      else
        gegl_buffer_copy (input, result, output, result);
112 113 114 115 116
    }

  return TRUE;
}

117 118 119
static void
dispose (GObject *object)
{
120
  GeglProperties *o = GEGL_PROPERTIES (object);
121 122 123 124 125 126 127

  if (o->buffer)
    {
      g_object_unref (o->buffer);
      o->buffer = NULL;
    }

128
  G_OBJECT_CLASS (gegl_op_parent_class)->dispose (object);
129
}
130 131

static void
132
gegl_op_class_init (GeglOpClass *klass)
133 134 135 136 137 138 139 140 141 142
{
  GeglOperationClass     *operation_class;
  GeglOperationSinkClass *sink_class;

  operation_class = GEGL_OPERATION_CLASS (klass);
  sink_class      = GEGL_OPERATION_SINK_CLASS (klass);

  sink_class->process = process;
  sink_class->needs_full = FALSE;

143 144
  G_OBJECT_CLASS (klass)->dispose = dispose;

145
  gegl_operation_class_set_keys (operation_class,
146 147
    "name",        "gegl:write-buffer",
    "title",       _("Write Buffer"),
148 149 150
    "categories" , "programming:output",
    "description", _("A GEGL buffer destination surface."),
    NULL);
151 152 153
}

#endif