gimptilebackendplugin.c 9.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* gimptilebackendtilemanager.c
 * Copyright (C) 2012 Øyvind Kolås <pippin@gimp.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
15
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16 17
 */

18 19
#include "config.h"

20 21
#include <stdlib.h>
#include <string.h>
22

23
#include <gegl.h>
24

25 26
#define GIMP_DISABLE_DEPRECATION_WARNINGS

27 28 29
#include "gimp.h"
#include "gimptilebackendplugin.h"

30

31
#define TILE_WIDTH  gimp_tile_width()
32
#define TILE_HEIGHT gimp_tile_height()
33

34

35 36 37 38
struct _GimpTileBackendPluginPrivate
{
  GimpDrawable *drawable;
  gboolean      shadow;
39
  gint          mul;
40 41
};

42 43 44

static gint
gimp_gegl_tile_mul (void)
45
{
46 47 48
  static gint     mul    = 2;
  static gboolean inited = FALSE;

49 50
  if (G_LIKELY (inited))
    return mul;
51 52 53

  inited = TRUE;

54 55
  if (g_getenv ("GIMP_GEGL_TILE_MUL"))
    mul = atoi (g_getenv ("GIMP_GEGL_TILE_MUL"));
56

57 58 59
  if (mul < 1)
    mul = 1;

60
  return mul;
61 62 63 64 65 66 67 68 69 70 71
}

static void     gimp_tile_backend_plugin_finalize (GObject         *object);
static gpointer gimp_tile_backend_plugin_command  (GeglTileSource  *tile_store,
                                                   GeglTileCommand  command,
                                                   gint             x,
                                                   gint             y,
                                                   gint             z,
                                                   gpointer         data);

static void       gimp_tile_write_mul (GimpTileBackendPlugin *backend_plugin,
72 73 74
                                       gint                   x,
                                       gint                   y,
                                       guchar                *source);
75 76

static GeglTile * gimp_tile_read_mul (GimpTileBackendPlugin *backend_plugin,
77 78
                                      gint                   x,
                                      gint                   y);
79

80

81 82
G_DEFINE_TYPE_WITH_PRIVATE (GimpTileBackendPlugin, _gimp_tile_backend_plugin,
                            GEGL_TYPE_TILE_BACKEND)
83

84
#define parent_class _gimp_tile_backend_plugin_parent_class
85 86


87 88 89
static GMutex backend_plugin_mutex;


90
static void
91
_gimp_tile_backend_plugin_class_init (GimpTileBackendPluginClass *klass)
92 93 94 95 96
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->finalize = gimp_tile_backend_plugin_finalize;

97
  gimp_tile_cache_ntiles (64);
98 99 100
}

static void
101
_gimp_tile_backend_plugin_init (GimpTileBackendPlugin *backend)
102 103 104
{
  GeglTileSource *source = GEGL_TILE_SOURCE (backend);

105
  backend->priv = _gimp_tile_backend_plugin_get_instance_private (backend);
106 107 108 109 110 111 112 113 114 115

  source->command = gimp_tile_backend_plugin_command;
}

static void
gimp_tile_backend_plugin_finalize (GObject *object)
{
  GimpTileBackendPlugin *backend = GIMP_TILE_BACKEND_PLUGIN (object);

  if (backend->priv->drawable) /* This also causes a flush */
116
    gimp_drawable_detach (backend->priv->drawable);
117 118 119 120 121 122

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static gpointer
gimp_tile_backend_plugin_command (GeglTileSource  *tile_store,
123 124 125 126 127
                                  GeglTileCommand  command,
                                  gint             x,
                                  gint             y,
                                  gint             z,
                                  gpointer         data)
128
{
129
  GimpTileBackendPlugin *backend_plugin = GIMP_TILE_BACKEND_PLUGIN (tile_store);
130
  gpointer               result         = NULL;
131 132 133 134

  switch (command)
    {
    case GEGL_TILE_GET:
135
      g_mutex_lock (&backend_plugin_mutex);
136 137 138

      result = gimp_tile_read_mul (backend_plugin, x, y);

139
      g_mutex_unlock (&backend_plugin_mutex);
140
      break;
141

142
    case GEGL_TILE_SET:
143
      g_mutex_lock (&backend_plugin_mutex);
144

145 146
      gimp_tile_write_mul (backend_plugin, x, y, gegl_tile_get_data (data));
      gegl_tile_mark_as_stored (data);
147

148
      g_mutex_unlock (&backend_plugin_mutex);
149
      break;
150

151
    case GEGL_TILE_FLUSH:
152
      g_mutex_lock (&backend_plugin_mutex);
153

154
      gimp_drawable_flush (backend_plugin->priv->drawable);
155

156
      g_mutex_unlock (&backend_plugin_mutex);
157
      break;
158

159
    default:
160 161
      result = gegl_tile_backend_command (GEGL_TILE_BACKEND (tile_store),
                                          command, x, y, z, data);
162
      break;
163 164
    }

165
  return result;
166 167 168 169
}

static GeglTile *
gimp_tile_read_mul (GimpTileBackendPlugin *backend_plugin,
170 171
                    gint                   x,
                    gint                   y)
172
{
173 174 175 176 177 178 179
  GimpTileBackendPluginPrivate *priv    = backend_plugin->priv;
  GeglTileBackend              *backend = GEGL_TILE_BACKEND (backend_plugin);
  GeglTile                     *tile;
  gint                          tile_size;
  gint                          u, v;
  gint                          mul = priv->mul;
  guchar                       *tile_data;
180 181 182 183 184 185 186 187 188

  x *= mul;
  y *= mul;

  tile_size  = gegl_tile_backend_get_tile_size (backend);
  tile       = gegl_tile_new (tile_size);
  tile_data  = gegl_tile_get_data (tile);

  for (u = 0; u < mul; u++)
189 190 191 192 193 194 195 196 197 198 199 200 201 202
    {
      for (v = 0; v < mul; v++)
        {
          GimpTile *gimp_tile;

          if (x + u >= priv->drawable->ntile_cols ||
              y + v >= priv->drawable->ntile_rows)
            continue;

          gimp_tile = gimp_drawable_get_tile (priv->drawable,
                                              priv->shadow,
                                              y + v, x + u);
          gimp_tile_ref (gimp_tile);

203 204 205 206 207 208 209 210 211 212 213 214
          {
            gint ewidth           = gimp_tile->ewidth;
            gint eheight          = gimp_tile->eheight;
            gint bpp              = gimp_tile->bpp;
            gint tile_stride      = mul * TILE_WIDTH * bpp;
            gint gimp_tile_stride = ewidth * bpp;
            gint row;

            for (row = 0; row < eheight; row++)
              {
                memcpy (tile_data + (row + TILE_HEIGHT * v) *
                        tile_stride + u * TILE_WIDTH * bpp,
215
                        ((gchar *) gimp_tile->data) + row * gimp_tile_stride,
216 217 218
                        gimp_tile_stride);
              }
          }
219 220 221 222

          gimp_tile_unref (gimp_tile, FALSE);
        }
    }
223 224 225 226 227 228 229 230 231 232 233

  return tile;
}

static void
gimp_tile_write_mul (GimpTileBackendPlugin *backend_plugin,
                     gint                   x,
                     gint                   y,
                     guchar                *source)
{
  GimpTileBackendPluginPrivate *priv = backend_plugin->priv;
234 235
  gint                          u, v;
  gint                          mul = priv->mul;
236 237 238 239 240 241

  x *= mul;
  y *= mul;

  for (v = 0; v < mul; v++)
    {
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
      for (u = 0; u < mul; u++)
        {
          GimpTile *gimp_tile;

          if (x + u >= priv->drawable->ntile_cols ||
              y + v >= priv->drawable->ntile_rows)
            continue;

          gimp_tile = gimp_drawable_get_tile (priv->drawable,
                                              priv->shadow,
                                              y+v, x+u);
          gimp_tile_ref (gimp_tile);

          {
            gint ewidth           = gimp_tile->ewidth;
            gint eheight          = gimp_tile->eheight;
            gint bpp              = gimp_tile->bpp;
            gint tile_stride      = mul * TILE_WIDTH * bpp;
            gint gimp_tile_stride = ewidth * bpp;
            gint row;

            for (row = 0; row < eheight; row++)
              memcpy (((gchar *)gimp_tile->data) + row * gimp_tile_stride,
                      source + (row + v * TILE_HEIGHT) *
                      tile_stride + u * TILE_WIDTH * bpp,
                      gimp_tile_stride);
          }

          gimp_tile_unref (gimp_tile, TRUE);
        }
272 273 274 275
    }
}

GeglTileBackend *
276 277
_gimp_tile_backend_plugin_new (GimpDrawable *drawable,
                               gint          shadow)
278
{
279 280 281 282 283 284
  GeglTileBackend       *backend;
  GimpTileBackendPlugin *backend_plugin;
  const Babl            *format;
  gint                   width  = gimp_drawable_width (drawable->drawable_id);
  gint                   height = gimp_drawable_height (drawable->drawable_id);
  gint                   mul    = gimp_gegl_tile_mul ();
285

286
  format = gimp_drawable_get_format (drawable->drawable_id);
287

288 289 290 291 292
  backend = g_object_new (GIMP_TYPE_TILE_BACKEND_PLUGIN,
                          "tile-width",  TILE_WIDTH  * mul,
                          "tile-height", TILE_HEIGHT * mul,
                          "format",      format,
                          NULL);
293

294
  backend_plugin = GIMP_TILE_BACKEND_PLUGIN (backend);
295

296 297 298
  backend_plugin->priv->drawable = drawable;
  backend_plugin->priv->mul      = mul;
  backend_plugin->priv->shadow   = shadow;
299

300 301
  gegl_tile_backend_set_extent (backend,
                                GEGL_RECTANGLE (0, 0, width, height));
302

303
  return backend;
304
}