1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
|
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* gimptilebackendplugin.c
* Copyright (C) 2011-2019 Øyvind Kolås <pippin@gimp.org>
* Michael Natterer <mitch@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
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include "gimp.h"
#include "libgimpbase/gimpprotocol.h"
#include "libgimpbase/gimpwire.h"
#include "gimp-shm.h"
#include "gimpplugin-private.h"
#include "gimptilebackendplugin.h"
#define TILE_WIDTH gimp_tile_width()
#define TILE_HEIGHT gimp_tile_height()
typedef struct _GimpTile GimpTile;
struct _GimpTile
{
guint tile_num; /* the number of this tile within the drawable */
guint ewidth; /* the effective width of the tile */
guint eheight; /* the effective height of the tile */
guchar *data; /* the pixel data for the tile */
};
struct _GimpTileBackendPluginPrivate
{
gint32 drawable_id;
gboolean shadow;
gint width;
gint height;
gint bpp;
gint ntile_rows;
gint ntile_cols;
};
static gpointer gimp_tile_backend_plugin_command (GeglTileSource *tile_store,
GeglTileCommand command,
gint x,
gint y,
gint z,
gpointer data);
static gboolean gimp_tile_write (GimpTileBackendPlugin *backend_plugin,
gint x,
gint y,
GeglTile *tile);
static GeglTile * gimp_tile_read (GimpTileBackendPlugin *backend_plugin,
gint x,
gint y);
static gboolean gimp_tile_init (GimpTileBackendPlugin *backend_plugin,
GimpTile *tile,
gint row,
gint col);
static void gimp_tile_unset (GimpTileBackendPlugin *backend_plugin,
GimpTile *tile);
static void gimp_tile_get (GimpTileBackendPlugin *backend_plugin,
GimpTile *tile);
static void gimp_tile_put (GimpTileBackendPlugin *backend_plugin,
GimpTile *tile);
G_DEFINE_TYPE_WITH_PRIVATE (GimpTileBackendPlugin, _gimp_tile_backend_plugin,
GEGL_TYPE_TILE_BACKEND)
#define parent_class _gimp_tile_backend_plugin_parent_class
static GMutex backend_plugin_mutex;
static void
_gimp_tile_backend_plugin_class_init (GimpTileBackendPluginClass *klass)
{
}
static void
_gimp_tile_backend_plugin_init (GimpTileBackendPlugin *backend)
{
GeglTileSource *source = GEGL_TILE_SOURCE (backend);
backend->priv = _gimp_tile_backend_plugin_get_instance_private (backend);
source->command = gimp_tile_backend_plugin_command;
}
static gpointer
gimp_tile_backend_plugin_command (GeglTileSource *tile_store,
GeglTileCommand command,
gint x,
gint y,
gint z,
gpointer data)
{
GimpTileBackendPlugin *backend_plugin = GIMP_TILE_BACKEND_PLUGIN (tile_store);
gpointer result = NULL;
switch (command)
{
case GEGL_TILE_GET:
/* TODO: fetch mipmapped tiles directly from gimp, instead of returning
* NULL to render them locally
*/
if (z == 0)
{
g_mutex_lock (&backend_plugin_mutex);
result = gimp_tile_read (backend_plugin, x, y);
g_mutex_unlock (&backend_plugin_mutex);
}
break;
case GEGL_TILE_SET:
/* TODO: actually store mipmapped tiles */
if (z == 0)
{
g_mutex_lock (&backend_plugin_mutex);
gimp_tile_write (backend_plugin, x, y, data);
g_mutex_unlock (&backend_plugin_mutex);
}
gegl_tile_mark_as_stored (data);
break;
case GEGL_TILE_FLUSH:
break;
default:
result = gegl_tile_backend_command (GEGL_TILE_BACKEND (tile_store),
command, x, y, z, data);
break;
}
return result;
}
/* public functions */
GeglTileBackend *
_gimp_tile_backend_plugin_new (GimpDrawable *drawable,
gint shadow)
{
GeglTileBackend *backend;
GimpTileBackendPlugin *backend_plugin;
const Babl *format = gimp_drawable_get_format (drawable);
gint width = gimp_drawable_get_width (drawable);
gint height = gimp_drawable_get_height (drawable);
backend = g_object_new (GIMP_TYPE_TILE_BACKEND_PLUGIN,
"tile-width", TILE_WIDTH,
"tile-height", TILE_HEIGHT,
"format", format,
NULL);
backend_plugin = GIMP_TILE_BACKEND_PLUGIN (backend);
backend_plugin->priv->drawable_id = gimp_item_get_id (GIMP_ITEM (drawable));
backend_plugin->priv->shadow = shadow;
backend_plugin->priv->width = width;
backend_plugin->priv->height = height;
backend_plugin->priv->bpp = gimp_drawable_get_bpp (drawable);
backend_plugin->priv->ntile_rows = (height + TILE_HEIGHT - 1) / TILE_HEIGHT;
backend_plugin->priv->ntile_cols = (width + TILE_WIDTH - 1) / TILE_WIDTH;
gegl_tile_backend_set_extent (backend,
GEGL_RECTANGLE (0, 0, width, height));
return backend;
}
/* private functions */
static GeglTile *
gimp_tile_read (GimpTileBackendPlugin *backend_plugin,
gint x,
gint y)
{
GimpTileBackendPluginPrivate *priv = backend_plugin->priv;
GeglTileBackend *backend = GEGL_TILE_BACKEND (backend_plugin);
GeglTile *tile;
GimpTile gimp_tile = { 0, };
gint tile_size;
guchar *tile_data;
if (! gimp_tile_init (backend_plugin, &gimp_tile, y, x))
return NULL;
tile_size = gegl_tile_backend_get_tile_size (backend);
tile = gegl_tile_new (tile_size);
tile_data = gegl_tile_get_data (tile);
gimp_tile_get (backend_plugin, &gimp_tile);
if (gimp_tile.ewidth * gimp_tile.eheight * priv->bpp == tile_size)
{
memcpy (tile_data, gimp_tile.data, tile_size);
}
else
{
gint tile_stride = TILE_WIDTH * priv->bpp;
gint gimp_tile_stride = gimp_tile.ewidth * priv->bpp;
guint row;
for (row = 0; row < gimp_tile.eheight; row++)
{
memcpy (tile_data + row * tile_stride,
gimp_tile.data + row * gimp_tile_stride,
gimp_tile_stride);
}
}
gimp_tile_unset (backend_plugin, &gimp_tile);
return tile;
}
static gboolean
gimp_tile_write (GimpTileBackendPlugin *backend_plugin,
gint x,
gint y,
GeglTile *tile)
{
GimpTileBackendPluginPrivate *priv = backend_plugin->priv;
GeglTileBackend *backend = GEGL_TILE_BACKEND (backend_plugin);
GimpTile gimp_tile = { 0, };
gint tile_size;
guchar *tile_data;
if (! gimp_tile_init (backend_plugin, &gimp_tile, y, x))
return FALSE;
tile_size = gegl_tile_backend_get_tile_size (backend);
tile_data = gegl_tile_get_data (tile);
gimp_tile.data = g_new (guchar,
gimp_tile.ewidth * gimp_tile.eheight *
priv->bpp);
if (gimp_tile.ewidth * gimp_tile.eheight * priv->bpp == tile_size)
{
memcpy (gimp_tile.data, tile_data, tile_size);
}
else
{
gint tile_stride = TILE_WIDTH * priv->bpp;
gint gimp_tile_stride = gimp_tile.ewidth * priv->bpp;
guint row;
for (row = 0; row < gimp_tile.eheight; row++)
{
memcpy (gimp_tile.data + row * gimp_tile_stride,
tile_data + row * tile_stride,
gimp_tile_stride);
}
}
gimp_tile_put (backend_plugin, &gimp_tile);
gimp_tile_unset (backend_plugin, &gimp_tile);
return TRUE;
}
static gboolean
gimp_tile_init (GimpTileBackendPlugin *backend_plugin,
GimpTile *tile,
gint row,
gint col)
{
GimpTileBackendPluginPrivate *priv = backend_plugin->priv;
if (row > priv->ntile_rows - 1 ||
col > priv->ntile_cols - 1)
{
return FALSE;
}
tile->tile_num = row * priv->ntile_cols + col;
if (col == (priv->ntile_cols - 1))
tile->ewidth = priv->width - ((priv->ntile_cols - 1) * TILE_WIDTH);
else
tile->ewidth = TILE_WIDTH;
if (row == (priv->ntile_rows - 1))
tile->eheight = priv->height - ((priv->ntile_rows - 1) * TILE_HEIGHT);
else
tile->eheight = TILE_HEIGHT;
tile->data = NULL;
return TRUE;
}
static void
gimp_tile_unset (GimpTileBackendPlugin *backend_plugin,
GimpTile *tile)
{
g_clear_pointer (&tile->data, g_free);
}
static void
gimp_tile_get (GimpTileBackendPlugin *backend_plugin,
GimpTile *tile)
{
GimpTileBackendPluginPrivate *priv = backend_plugin->priv;
GimpPlugIn *plug_in = gimp_get_plug_in ();
GPTileReq tile_req;
GPTileData *tile_data;
GimpWireMessage msg;
tile_req.drawable_id = priv->drawable_id;
tile_req.tile_num = tile->tile_num;
tile_req.shadow = priv->shadow;
if (! gp_tile_req_write (_gimp_plug_in_get_write_channel (plug_in),
&tile_req, plug_in))
gimp_quit ();
_gimp_plug_in_read_expect_msg (plug_in, &msg, GP_TILE_DATA);
tile_data = msg.data;
if (tile_data->drawable_id != priv->drawable_id ||
tile_data->tile_num != tile->tile_num ||
tile_data->shadow != priv->shadow ||
tile_data->width != tile->ewidth ||
tile_data->height != tile->eheight ||
tile_data->bpp != priv->bpp)
{
#if 0
g_printerr ("tile_data: %d %d %d %d %d %d\n"
"tile: %d %d %d %d %d %d\n",
tile_data->drawable_id,
tile_data->tile_num,
tile_data->shadow,
tile_data->width,
tile_data->height,
tile_data->bpp,
priv->drawable_id,
tile->tile_num,
priv->shadow,
tile->ewidth,
tile->eheight,
priv->bpp);
#endif
g_printerr ("received tile info did not match computed tile info");
gimp_quit ();
}
if (tile_data->use_shm)
{
tile->data = g_memdup2 (_gimp_shm_addr (),
tile->ewidth * tile->eheight * priv->bpp);
}
else
{
tile->data = tile_data->data;
tile_data->data = NULL;
}
if (! gp_tile_ack_write (_gimp_plug_in_get_write_channel (plug_in),
plug_in))
gimp_quit ();
gimp_wire_destroy (&msg);
}
static void
gimp_tile_put (GimpTileBackendPlugin *backend_plugin,
GimpTile *tile)
{
GimpTileBackendPluginPrivate *priv = backend_plugin->priv;
GimpPlugIn *plug_in = gimp_get_plug_in ();
GPTileReq tile_req;
GPTileData tile_data;
GPTileData *tile_info;
GimpWireMessage msg;
tile_req.drawable_id = -1;
tile_req.tile_num = 0;
tile_req.shadow = 0;
if (! gp_tile_req_write (_gimp_plug_in_get_write_channel (plug_in),
&tile_req, plug_in))
gimp_quit ();
_gimp_plug_in_read_expect_msg (plug_in, &msg, GP_TILE_DATA);
tile_info = msg.data;
tile_data.drawable_id = priv->drawable_id;
tile_data.tile_num = tile->tile_num;
tile_data.shadow = priv->shadow;
tile_data.bpp = priv->bpp;
tile_data.width = tile->ewidth;
tile_data.height = tile->eheight;
tile_data.use_shm = tile_info->use_shm;
tile_data.data = NULL;
if (tile_info->use_shm)
{
memcpy (_gimp_shm_addr (),
tile->data,
tile->ewidth * tile->eheight * priv->bpp);
}
else
{
tile_data.data = tile->data;
}
if (! gp_tile_data_write (_gimp_plug_in_get_write_channel (plug_in),
&tile_data, plug_in))
gimp_quit ();
if (! tile_info->use_shm)
tile_data.data = NULL;
gimp_wire_destroy (&msg);
_gimp_plug_in_read_expect_msg (plug_in, &msg, GP_TILE_ACK);
gimp_wire_destroy (&msg);
}
|