2012-03-22 08:10:43 +08:00
|
|
|
/* 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
|
2018-07-12 05:27:07 +08:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2012-03-22 08:10:43 +08:00
|
|
|
*/
|
|
|
|
|
2012-04-03 02:44:24 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
2012-03-22 08:10:43 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-04-03 02:44:24 +08:00
|
|
|
|
2012-03-22 08:10:43 +08:00
|
|
|
#include <gegl.h>
|
2012-04-03 02:44:24 +08:00
|
|
|
|
2012-05-03 07:50:14 +08:00
|
|
|
#define GIMP_DISABLE_DEPRECATION_WARNINGS
|
|
|
|
|
2012-03-22 08:10:43 +08:00
|
|
|
#include "gimp.h"
|
|
|
|
#include "gimptilebackendplugin.h"
|
|
|
|
|
2012-04-03 02:44:24 +08:00
|
|
|
|
2012-03-22 08:10:43 +08:00
|
|
|
#define TILE_WIDTH gimp_tile_width()
|
2016-04-25 02:04:42 +08:00
|
|
|
#define TILE_HEIGHT gimp_tile_height()
|
2012-03-22 08:10:43 +08:00
|
|
|
|
2012-04-03 02:44:24 +08:00
|
|
|
|
2012-03-22 08:10:43 +08:00
|
|
|
struct _GimpTileBackendPluginPrivate
|
|
|
|
{
|
|
|
|
GimpDrawable *drawable;
|
|
|
|
gboolean shadow;
|
2012-04-03 02:44:24 +08:00
|
|
|
gint mul;
|
2012-03-22 08:10:43 +08:00
|
|
|
};
|
|
|
|
|
2012-04-03 02:44:24 +08:00
|
|
|
|
|
|
|
static gint
|
|
|
|
gimp_gegl_tile_mul (void)
|
2012-03-22 08:10:43 +08:00
|
|
|
{
|
2012-04-03 02:44:24 +08:00
|
|
|
static gint mul = 2;
|
|
|
|
static gboolean inited = FALSE;
|
|
|
|
|
2012-03-22 08:10:43 +08:00
|
|
|
if (G_LIKELY (inited))
|
|
|
|
return mul;
|
2012-04-03 02:44:24 +08:00
|
|
|
|
|
|
|
inited = TRUE;
|
|
|
|
|
2012-03-22 08:10:43 +08:00
|
|
|
if (g_getenv ("GIMP_GEGL_TILE_MUL"))
|
|
|
|
mul = atoi (g_getenv ("GIMP_GEGL_TILE_MUL"));
|
2012-04-03 02:44:24 +08:00
|
|
|
|
2012-03-22 08:10:43 +08:00
|
|
|
if (mul < 1)
|
|
|
|
mul = 1;
|
|
|
|
|
2012-04-03 02:44:24 +08:00
|
|
|
return mul;
|
2012-03-22 08:10:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2012-03-22 18:24:03 +08:00
|
|
|
gint x,
|
|
|
|
gint y,
|
|
|
|
guchar *source);
|
2012-03-22 08:10:43 +08:00
|
|
|
|
|
|
|
static GeglTile * gimp_tile_read_mul (GimpTileBackendPlugin *backend_plugin,
|
2012-03-22 18:24:03 +08:00
|
|
|
gint x,
|
|
|
|
gint y);
|
2012-03-22 08:10:43 +08:00
|
|
|
|
2012-03-22 18:24:03 +08:00
|
|
|
|
|
|
|
G_DEFINE_TYPE (GimpTileBackendPlugin, _gimp_tile_backend_plugin,
|
2012-03-22 08:10:43 +08:00
|
|
|
GEGL_TYPE_TILE_BACKEND)
|
|
|
|
|
2012-03-22 18:24:03 +08:00
|
|
|
#define parent_class _gimp_tile_backend_plugin_parent_class
|
2012-03-22 08:10:43 +08:00
|
|
|
|
|
|
|
|
2017-11-22 17:23:05 +08:00
|
|
|
static GMutex backend_plugin_mutex;
|
|
|
|
|
|
|
|
|
2012-03-22 08:10:43 +08:00
|
|
|
static void
|
2012-03-22 18:24:03 +08:00
|
|
|
_gimp_tile_backend_plugin_class_init (GimpTileBackendPluginClass *klass)
|
2012-03-22 08:10:43 +08:00
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
object_class->finalize = gimp_tile_backend_plugin_finalize;
|
|
|
|
|
|
|
|
g_type_class_add_private (klass, sizeof (GimpTileBackendPluginPrivate));
|
2012-04-03 02:44:24 +08:00
|
|
|
|
|
|
|
gimp_tile_cache_ntiles (64);
|
2012-03-22 08:10:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-03-22 18:24:03 +08:00
|
|
|
_gimp_tile_backend_plugin_init (GimpTileBackendPlugin *backend)
|
2012-03-22 08:10:43 +08:00
|
|
|
{
|
|
|
|
GeglTileSource *source = GEGL_TILE_SOURCE (backend);
|
|
|
|
|
|
|
|
backend->priv = G_TYPE_INSTANCE_GET_PRIVATE (backend,
|
|
|
|
GIMP_TYPE_TILE_BACKEND_PLUGIN,
|
|
|
|
GimpTileBackendPluginPrivate);
|
|
|
|
|
|
|
|
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 */
|
2012-03-22 18:24:03 +08:00
|
|
|
gimp_drawable_detach (backend->priv->drawable);
|
2012-03-22 08:10:43 +08:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
gimp_tile_backend_plugin_command (GeglTileSource *tile_store,
|
2012-03-22 18:24:03 +08:00
|
|
|
GeglTileCommand command,
|
|
|
|
gint x,
|
|
|
|
gint y,
|
|
|
|
gint z,
|
|
|
|
gpointer data)
|
2012-03-22 08:10:43 +08:00
|
|
|
{
|
2012-04-03 02:44:24 +08:00
|
|
|
GimpTileBackendPlugin *backend_plugin = GIMP_TILE_BACKEND_PLUGIN (tile_store);
|
2017-11-20 22:09:59 +08:00
|
|
|
gpointer result = NULL;
|
2012-03-22 08:10:43 +08:00
|
|
|
|
|
|
|
switch (command)
|
|
|
|
{
|
|
|
|
case GEGL_TILE_GET:
|
2017-11-22 17:23:05 +08:00
|
|
|
g_mutex_lock (&backend_plugin_mutex);
|
2017-11-20 22:09:59 +08:00
|
|
|
|
|
|
|
result = gimp_tile_read_mul (backend_plugin, x, y);
|
|
|
|
|
2017-11-22 17:23:05 +08:00
|
|
|
g_mutex_unlock (&backend_plugin_mutex);
|
2017-11-20 22:09:59 +08:00
|
|
|
break;
|
2012-04-03 02:44:24 +08:00
|
|
|
|
2012-03-22 08:10:43 +08:00
|
|
|
case GEGL_TILE_SET:
|
2017-11-22 17:23:05 +08:00
|
|
|
g_mutex_lock (&backend_plugin_mutex);
|
2017-11-20 22:09:59 +08:00
|
|
|
|
2012-03-22 08:10:43 +08:00
|
|
|
gimp_tile_write_mul (backend_plugin, x, y, gegl_tile_get_data (data));
|
|
|
|
gegl_tile_mark_as_stored (data);
|
2017-11-20 22:09:59 +08:00
|
|
|
|
2017-11-22 17:23:05 +08:00
|
|
|
g_mutex_unlock (&backend_plugin_mutex);
|
2012-03-22 20:26:53 +08:00
|
|
|
break;
|
2012-04-03 02:44:24 +08:00
|
|
|
|
2012-03-22 20:26:53 +08:00
|
|
|
case GEGL_TILE_FLUSH:
|
2017-11-22 17:23:05 +08:00
|
|
|
g_mutex_lock (&backend_plugin_mutex);
|
2017-11-20 22:09:59 +08:00
|
|
|
|
2012-03-22 20:26:53 +08:00
|
|
|
gimp_drawable_flush (backend_plugin->priv->drawable);
|
2017-11-20 22:09:59 +08:00
|
|
|
|
2017-11-22 17:23:05 +08:00
|
|
|
g_mutex_unlock (&backend_plugin_mutex);
|
2012-03-22 20:26:53 +08:00
|
|
|
break;
|
2012-04-03 02:44:24 +08:00
|
|
|
|
2012-03-22 08:10:43 +08:00
|
|
|
default:
|
2018-08-20 07:43:10 +08:00
|
|
|
result = gegl_tile_backend_command (GEGL_TILE_BACKEND (tile_store),
|
|
|
|
command, x, y, z, data);
|
2018-08-19 18:43:59 +08:00
|
|
|
break;
|
2012-03-22 08:10:43 +08:00
|
|
|
}
|
|
|
|
|
2017-11-20 22:09:59 +08:00
|
|
|
return result;
|
2012-03-22 08:10:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static GeglTile *
|
|
|
|
gimp_tile_read_mul (GimpTileBackendPlugin *backend_plugin,
|
2012-03-22 18:24:03 +08:00
|
|
|
gint x,
|
|
|
|
gint y)
|
2012-03-22 08:10:43 +08:00
|
|
|
{
|
2012-04-03 02:44:24 +08:00
|
|
|
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;
|
2012-03-22 08:10:43 +08:00
|
|
|
|
|
|
|
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++)
|
2012-04-03 02:44:24 +08:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2012-03-22 08:10:43 +08:00
|
|
|
{
|
|
|
|
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,
|
2012-04-03 02:44:24 +08:00
|
|
|
((gchar *) gimp_tile->data) + row * gimp_tile_stride,
|
2012-03-22 08:10:43 +08:00
|
|
|
gimp_tile_stride);
|
|
|
|
}
|
|
|
|
}
|
2012-04-03 02:44:24 +08:00
|
|
|
|
|
|
|
gimp_tile_unref (gimp_tile, FALSE);
|
|
|
|
}
|
|
|
|
}
|
2012-03-22 08:10:43 +08:00
|
|
|
|
|
|
|
return tile;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_tile_write_mul (GimpTileBackendPlugin *backend_plugin,
|
|
|
|
gint x,
|
|
|
|
gint y,
|
|
|
|
guchar *source)
|
|
|
|
{
|
|
|
|
GimpTileBackendPluginPrivate *priv = backend_plugin->priv;
|
2012-04-03 02:44:24 +08:00
|
|
|
gint u, v;
|
|
|
|
gint mul = priv->mul;
|
2012-03-22 08:10:43 +08:00
|
|
|
|
|
|
|
x *= mul;
|
|
|
|
y *= mul;
|
|
|
|
|
|
|
|
for (v = 0; v < mul; v++)
|
|
|
|
{
|
2012-04-03 02:44:24 +08:00
|
|
|
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);
|
|
|
|
}
|
2012-03-22 08:10:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GeglTileBackend *
|
2012-03-22 18:24:03 +08:00
|
|
|
_gimp_tile_backend_plugin_new (GimpDrawable *drawable,
|
|
|
|
gint shadow)
|
2012-03-22 08:10:43 +08:00
|
|
|
{
|
2012-04-03 02:44:24 +08:00
|
|
|
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 ();
|
2012-03-22 08:10:43 +08:00
|
|
|
|
2012-04-03 02:44:24 +08:00
|
|
|
format = gimp_drawable_get_format (drawable->drawable_id);
|
2012-03-22 08:10:43 +08:00
|
|
|
|
2012-04-03 02:44:24 +08:00
|
|
|
backend = g_object_new (GIMP_TYPE_TILE_BACKEND_PLUGIN,
|
|
|
|
"tile-width", TILE_WIDTH * mul,
|
|
|
|
"tile-height", TILE_HEIGHT * mul,
|
|
|
|
"format", format,
|
|
|
|
NULL);
|
2012-03-22 08:10:43 +08:00
|
|
|
|
2012-04-03 02:44:24 +08:00
|
|
|
backend_plugin = GIMP_TILE_BACKEND_PLUGIN (backend);
|
2012-03-22 08:10:43 +08:00
|
|
|
|
2012-04-03 02:44:24 +08:00
|
|
|
backend_plugin->priv->drawable = drawable;
|
|
|
|
backend_plugin->priv->mul = mul;
|
|
|
|
backend_plugin->priv->shadow = shadow;
|
2012-03-22 08:10:43 +08:00
|
|
|
|
2012-04-03 02:44:24 +08:00
|
|
|
gegl_tile_backend_set_extent (backend,
|
|
|
|
GEGL_RECTANGLE (0, 0, width, height));
|
2012-03-22 18:24:03 +08:00
|
|
|
|
2012-04-03 02:44:24 +08:00
|
|
|
return backend;
|
2012-03-22 08:10:43 +08:00
|
|
|
}
|