2000-12-29 23:22:01 +08:00
|
|
|
/* The GIMP -- an image manipulation program
|
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
|
|
|
* 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 2 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, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2001-12-08 00:10:53 +08:00
|
|
|
#include <glib-object.h>
|
1998-07-03 07:29:44 +08:00
|
|
|
|
2001-05-15 19:25:25 +08:00
|
|
|
#include "base-types.h"
|
2000-12-29 23:22:01 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
#include "tile.h"
|
2001-05-15 19:25:25 +08:00
|
|
|
#include "tile-private.h"
|
|
|
|
#include "tile-cache.h"
|
|
|
|
#include "tile-manager.h"
|
|
|
|
#include "tile-swap.h"
|
1997-11-25 06:05:25 +08:00
|
|
|
|
1998-07-03 07:29:44 +08:00
|
|
|
|
2004-02-18 21:53:19 +08:00
|
|
|
/* Uncomment for verbose debugging on copy-on-write logic */
|
|
|
|
/* #define TILE_DEBUG */
|
|
|
|
|
|
|
|
/* Sanity checks on tile hinting code */
|
|
|
|
/* #define HINTS_SANITY */
|
|
|
|
|
|
|
|
|
1998-07-10 10:43:12 +08:00
|
|
|
static void tile_destroy (Tile *tile);
|
1998-07-03 07:29:44 +08:00
|
|
|
|
2001-01-22 11:30:42 +08:00
|
|
|
|
|
|
|
gint tile_count = 0;
|
|
|
|
|
1999-12-14 03:48:24 +08:00
|
|
|
void
|
|
|
|
tile_sanitize_rowhints (Tile *tile)
|
|
|
|
{
|
2004-02-18 21:53:19 +08:00
|
|
|
if (! tile->rowhint)
|
|
|
|
tile->rowhint = g_new0 (TileRowHint, tile->eheight);
|
1999-12-14 03:48:24 +08:00
|
|
|
}
|
|
|
|
|
1999-05-09 23:45:37 +08:00
|
|
|
TileRowHint
|
2000-12-29 23:22:01 +08:00
|
|
|
tile_get_rowhint (Tile *tile,
|
|
|
|
gint yoff)
|
1999-05-09 23:45:37 +08:00
|
|
|
{
|
|
|
|
#ifdef HINTS_SANITY
|
|
|
|
if (yoff < tile_eheight(tile) && yoff>=0)
|
|
|
|
{
|
|
|
|
return tile->rowhint[yoff];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
g_error("GET_ROWHINT OUT OF RANGE");
|
1999-12-14 03:48:24 +08:00
|
|
|
return TILEROWHINT_OUTOFRANGE;
|
1999-05-09 23:45:37 +08:00
|
|
|
#else
|
2004-02-18 21:53:19 +08:00
|
|
|
return tile->rowhint[yoff];
|
1999-05-09 23:45:37 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-12-29 23:22:01 +08:00
|
|
|
tile_set_rowhint (Tile *tile,
|
|
|
|
gint yoff,
|
|
|
|
TileRowHint rowhint)
|
1999-05-09 23:45:37 +08:00
|
|
|
{
|
|
|
|
#ifdef HINTS_SANITY
|
|
|
|
if (yoff < tile_eheight(tile) && yoff>=0)
|
|
|
|
{
|
|
|
|
tile->rowhint[yoff] = rowhint;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
g_error("SET_ROWHINT OUT OF RANGE");
|
|
|
|
#else
|
|
|
|
tile->rowhint[yoff] = rowhint;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
void
|
|
|
|
tile_init (Tile *tile,
|
2000-12-29 23:22:01 +08:00
|
|
|
gint bpp)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2000-12-29 23:22:01 +08:00
|
|
|
tile->ref_count = 0;
|
1998-07-10 10:43:12 +08:00
|
|
|
tile->write_count = 0;
|
|
|
|
tile->share_count = 0;
|
2000-12-29 23:22:01 +08:00
|
|
|
tile->dirty = FALSE;
|
|
|
|
tile->valid = FALSE;
|
|
|
|
tile->data = NULL;
|
|
|
|
tile->ewidth = TILE_WIDTH;
|
|
|
|
tile->eheight = TILE_HEIGHT;
|
|
|
|
tile->bpp = bpp;
|
|
|
|
tile->swap_num = 1;
|
1997-11-25 06:05:25 +08:00
|
|
|
tile->swap_offset = -1;
|
2000-12-29 23:22:01 +08:00
|
|
|
tile->tlink = NULL;
|
|
|
|
tile->next = NULL;
|
|
|
|
tile->prev = NULL;
|
|
|
|
tile->listhead = NULL;
|
|
|
|
tile->rowhint = NULL;
|
1999-05-09 23:45:37 +08:00
|
|
|
|
1998-06-21 01:09:32 +08:00
|
|
|
#ifdef USE_PTHREADS
|
|
|
|
{
|
2001-01-22 11:30:42 +08:00
|
|
|
pthread_mutex_init (&tile->mutex, NULL);
|
1998-06-21 01:09:32 +08:00
|
|
|
}
|
|
|
|
#endif
|
2001-01-22 11:30:42 +08:00
|
|
|
|
|
|
|
tile_count++;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
2001-01-22 11:30:42 +08:00
|
|
|
gint tile_ref_count = 0;
|
|
|
|
gint tile_share_count = 0;
|
|
|
|
gint tile_active_count = 0;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
1999-12-14 03:48:24 +08:00
|
|
|
#ifdef HINTS_SANITY
|
2001-01-22 11:30:42 +08:00
|
|
|
gint tile_exist_peak = 0;
|
|
|
|
gint tile_exist_count = 0;
|
1999-12-14 03:48:24 +08:00
|
|
|
#endif
|
|
|
|
|
1998-07-03 07:29:44 +08:00
|
|
|
void
|
1998-07-10 10:43:12 +08:00
|
|
|
tile_lock (Tile *tile)
|
1998-07-03 07:29:44 +08:00
|
|
|
{
|
|
|
|
/* Increment the global reference count.
|
|
|
|
*/
|
2001-01-22 11:30:42 +08:00
|
|
|
tile_ref_count++;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
1998-07-03 07:29:44 +08:00
|
|
|
/* Increment this tile's reference count.
|
1997-11-25 06:05:25 +08:00
|
|
|
*/
|
|
|
|
|
1998-07-10 10:43:12 +08:00
|
|
|
TILE_MUTEX_LOCK (tile);
|
2001-01-22 11:30:42 +08:00
|
|
|
tile->ref_count++;
|
1998-07-03 07:29:44 +08:00
|
|
|
|
2004-02-18 21:53:19 +08:00
|
|
|
if (tile->ref_count == 1)
|
1998-07-03 07:29:44 +08:00
|
|
|
{
|
2004-02-18 21:53:19 +08:00
|
|
|
if (tile->listhead)
|
1998-07-03 07:29:44 +08:00
|
|
|
{
|
1998-07-10 10:43:12 +08:00
|
|
|
/* remove from cache, move to main store */
|
|
|
|
tile_cache_flush (tile);
|
|
|
|
}
|
2001-01-22 11:30:42 +08:00
|
|
|
tile_active_count++;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
1998-07-22 07:34:28 +08:00
|
|
|
if (tile->data == NULL)
|
|
|
|
{
|
|
|
|
/* There is no data, so the tile must be swapped out */
|
|
|
|
tile_swap_in (tile);
|
|
|
|
}
|
|
|
|
|
1998-07-10 10:43:12 +08:00
|
|
|
TILE_MUTEX_UNLOCK (tile);
|
1998-06-28 13:34:46 +08:00
|
|
|
|
1998-07-03 07:29:44 +08:00
|
|
|
/* Call 'tile_manager_validate' if the tile was invalid.
|
|
|
|
*/
|
1998-06-28 13:34:46 +08:00
|
|
|
if (!tile->valid)
|
1998-07-10 10:43:12 +08:00
|
|
|
{
|
|
|
|
/* an invalid tile should never be shared, so this should work */
|
|
|
|
tile_manager_validate ((TileManager*) tile->tlink->tm, tile);
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-02-18 21:53:19 +08:00
|
|
|
tile_release (Tile *tile,
|
2001-01-24 23:36:55 +08:00
|
|
|
gboolean dirty)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
1998-07-03 07:29:44 +08:00
|
|
|
/* Decrement the global reference count.
|
|
|
|
*/
|
2001-01-22 11:30:42 +08:00
|
|
|
tile_ref_count--;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-01-24 23:36:55 +08:00
|
|
|
TILE_MUTEX_LOCK (tile);
|
1998-07-10 10:43:12 +08:00
|
|
|
|
1998-07-03 07:29:44 +08:00
|
|
|
/* Decrement this tile's reference count.
|
1997-11-25 06:05:25 +08:00
|
|
|
*/
|
2001-01-22 11:30:42 +08:00
|
|
|
tile->ref_count--;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
1998-07-10 10:43:12 +08:00
|
|
|
/* Decrement write ref count if dirtying
|
1997-11-25 06:05:25 +08:00
|
|
|
*/
|
1998-07-10 10:43:12 +08:00
|
|
|
if (dirty)
|
1999-05-09 23:45:37 +08:00
|
|
|
{
|
2001-01-22 11:30:42 +08:00
|
|
|
gint y;
|
1999-05-09 23:45:37 +08:00
|
|
|
|
2001-01-22 11:30:42 +08:00
|
|
|
tile->write_count--;
|
1999-12-14 03:48:24 +08:00
|
|
|
|
|
|
|
if (tile->rowhint)
|
1999-05-09 23:45:37 +08:00
|
|
|
{
|
1999-12-14 03:48:24 +08:00
|
|
|
for (y = 0; y < tile->eheight; y++)
|
|
|
|
{
|
|
|
|
tile->rowhint[y] = TILEROWHINT_UNKNOWN;
|
|
|
|
}
|
1999-05-09 23:45:37 +08:00
|
|
|
}
|
|
|
|
}
|
1998-07-03 07:29:44 +08:00
|
|
|
|
1998-07-10 10:43:12 +08:00
|
|
|
if (tile->ref_count == 0)
|
1998-07-03 07:29:44 +08:00
|
|
|
{
|
1999-12-06 04:31:50 +08:00
|
|
|
tile_active_count--;
|
2001-01-22 11:30:42 +08:00
|
|
|
|
1998-07-10 10:43:12 +08:00
|
|
|
if (tile->share_count == 0)
|
1998-07-03 07:29:44 +08:00
|
|
|
{
|
1999-12-14 03:48:24 +08:00
|
|
|
/* tile is truly dead */
|
1998-07-10 10:43:12 +08:00
|
|
|
tile_destroy (tile);
|
|
|
|
return; /* skip terminal unlock */
|
1998-07-03 07:29:44 +08:00
|
|
|
}
|
2004-02-18 21:53:19 +08:00
|
|
|
else
|
1998-07-03 07:29:44 +08:00
|
|
|
{
|
1998-07-10 10:43:12 +08:00
|
|
|
/* last reference was just released, so move the tile to the
|
|
|
|
tile cache */
|
|
|
|
tile_cache_insert (tile);
|
1998-07-03 07:29:44 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
1998-07-10 10:43:12 +08:00
|
|
|
|
|
|
|
TILE_MUTEX_UNLOCK (tile);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tile_alloc (Tile *tile)
|
|
|
|
{
|
|
|
|
if (tile->data)
|
1998-07-12 01:23:03 +08:00
|
|
|
return;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/* Allocate the data for the tile.
|
|
|
|
*/
|
|
|
|
tile->data = g_new (guchar, tile_size (tile));
|
1999-12-14 03:48:24 +08:00
|
|
|
|
|
|
|
#ifdef HINTS_SANITY
|
|
|
|
tile_exist_count++;
|
|
|
|
if (tile_exist_count > tile_exist_peak)
|
|
|
|
tile_exist_peak = tile_exist_count;
|
|
|
|
#endif
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
1998-07-10 10:43:12 +08:00
|
|
|
static void
|
|
|
|
tile_destroy (Tile *tile)
|
|
|
|
{
|
2004-02-18 21:53:19 +08:00
|
|
|
if (tile->ref_count)
|
1998-07-12 01:23:03 +08:00
|
|
|
{
|
1999-06-06 07:41:45 +08:00
|
|
|
g_warning ("tried to destroy a ref'd tile");
|
1998-07-12 01:23:03 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (tile->share_count)
|
|
|
|
{
|
1999-06-06 07:41:45 +08:00
|
|
|
g_warning ("tried to destroy an attached tile");
|
1998-07-12 01:23:03 +08:00
|
|
|
return;
|
|
|
|
}
|
2004-02-18 21:53:19 +08:00
|
|
|
if (tile->data)
|
1998-07-10 10:43:12 +08:00
|
|
|
{
|
|
|
|
g_free (tile->data);
|
|
|
|
tile->data = NULL;
|
|
|
|
}
|
2004-02-18 21:53:19 +08:00
|
|
|
if (tile->rowhint)
|
1999-12-14 03:48:24 +08:00
|
|
|
{
|
|
|
|
g_free (tile->rowhint);
|
|
|
|
tile->rowhint = NULL;
|
|
|
|
}
|
1998-07-10 10:43:12 +08:00
|
|
|
if (tile->swap_offset != -1)
|
|
|
|
{
|
|
|
|
/* If the tile is on disk, then delete its
|
|
|
|
* presence there.
|
|
|
|
*/
|
|
|
|
tile_swap_delete (tile);
|
|
|
|
}
|
|
|
|
if (tile->listhead)
|
|
|
|
tile_cache_flush (tile);
|
2004-02-18 21:53:19 +08:00
|
|
|
|
|
|
|
TILE_MUTEX_UNLOCK (tile);
|
1998-07-10 10:43:12 +08:00
|
|
|
g_free (tile);
|
2001-01-22 11:30:42 +08:00
|
|
|
|
|
|
|
tile_count--;
|
1999-12-14 03:48:24 +08:00
|
|
|
|
|
|
|
#ifdef HINTS_SANITY
|
|
|
|
tile_exist_count--;
|
|
|
|
#endif
|
1998-07-10 10:43:12 +08:00
|
|
|
}
|
|
|
|
|
2001-01-22 11:30:42 +08:00
|
|
|
gint
|
1997-11-25 06:05:25 +08:00
|
|
|
tile_size (Tile *tile)
|
|
|
|
{
|
|
|
|
/* Return the actual size of the tile data.
|
|
|
|
* (Based on its effective width and height).
|
|
|
|
*/
|
2001-01-22 11:30:42 +08:00
|
|
|
return tile->ewidth * tile->eheight * tile->bpp;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
2001-01-22 11:30:42 +08:00
|
|
|
gint
|
1998-08-12 01:35:34 +08:00
|
|
|
tile_ewidth (Tile *tile)
|
|
|
|
{
|
|
|
|
return tile->ewidth;
|
|
|
|
}
|
|
|
|
|
2001-01-22 11:30:42 +08:00
|
|
|
gint
|
1998-08-12 01:35:34 +08:00
|
|
|
tile_eheight (Tile *tile)
|
|
|
|
{
|
|
|
|
return tile->eheight;
|
|
|
|
}
|
|
|
|
|
2001-01-22 11:30:42 +08:00
|
|
|
gint
|
1998-08-12 01:35:34 +08:00
|
|
|
tile_bpp (Tile *tile)
|
|
|
|
{
|
|
|
|
return tile->bpp;
|
|
|
|
}
|
|
|
|
|
2004-02-18 21:53:19 +08:00
|
|
|
gint
|
1998-08-12 01:35:34 +08:00
|
|
|
tile_is_valid (Tile *tile)
|
|
|
|
{
|
|
|
|
return tile->valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tile_mark_valid (Tile *tile)
|
|
|
|
{
|
|
|
|
TILE_MUTEX_LOCK (tile);
|
|
|
|
tile->valid = TRUE;
|
|
|
|
TILE_MUTEX_UNLOCK (tile);
|
|
|
|
}
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
void
|
2001-01-22 11:30:42 +08:00
|
|
|
tile_attach (Tile *tile,
|
|
|
|
void *tm,
|
2001-12-03 21:44:59 +08:00
|
|
|
gint tile_num)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
1998-07-10 10:43:12 +08:00
|
|
|
TileLink *tmp;
|
|
|
|
|
2004-02-18 21:53:19 +08:00
|
|
|
if ((tile->share_count > 0) && (!tile->valid))
|
1998-07-03 07:29:44 +08:00
|
|
|
{
|
1998-07-10 10:43:12 +08:00
|
|
|
/* trying to share invalid tiles is problematic, not to mention silly */
|
|
|
|
tile_manager_validate ((TileManager*) tile->tlink->tm, tile);
|
1998-07-03 07:29:44 +08:00
|
|
|
}
|
2001-01-22 11:30:42 +08:00
|
|
|
|
1998-07-10 10:43:12 +08:00
|
|
|
tile->share_count++;
|
1998-07-12 01:23:03 +08:00
|
|
|
tile_share_count++;
|
2001-01-22 11:30:42 +08:00
|
|
|
|
1998-07-10 10:43:12 +08:00
|
|
|
#ifdef TILE_DEBUG
|
|
|
|
g_print("tile_attach: %p -> (%p,%d) *%d\n", tile, tm, tile_num, tile->share_count);
|
1998-07-03 07:29:44 +08:00
|
|
|
#endif
|
|
|
|
|
1998-07-10 10:43:12 +08:00
|
|
|
/* link this tile into the tile's tilelink chain */
|
|
|
|
tmp = g_new (TileLink, 1);
|
2001-01-22 11:30:42 +08:00
|
|
|
tmp->tm = tm;
|
1998-07-10 10:43:12 +08:00
|
|
|
tmp->tile_num = tile_num;
|
2001-01-22 11:30:42 +08:00
|
|
|
tmp->next = tile->tlink;
|
|
|
|
|
1998-07-10 10:43:12 +08:00
|
|
|
tile->tlink = tmp;
|
|
|
|
}
|
1998-07-03 07:29:44 +08:00
|
|
|
|
1998-07-10 10:43:12 +08:00
|
|
|
void
|
2001-01-22 11:30:42 +08:00
|
|
|
tile_detach (Tile *tile,
|
|
|
|
void *tm,
|
2001-12-03 21:44:59 +08:00
|
|
|
gint tile_num)
|
1998-07-10 10:43:12 +08:00
|
|
|
{
|
|
|
|
TileLink **link;
|
2001-01-22 11:30:42 +08:00
|
|
|
TileLink *tmp;
|
1998-07-10 10:43:12 +08:00
|
|
|
|
|
|
|
#ifdef TILE_DEBUG
|
1999-02-01 04:52:15 +08:00
|
|
|
g_print("tile_detach: %p ~> (%p,%d) r%d *%d\n", tile, tm, tile_num,
|
|
|
|
tile->ref_count, tile->share_count);
|
1998-07-03 07:29:44 +08:00
|
|
|
#endif
|
|
|
|
|
1999-12-14 03:48:24 +08:00
|
|
|
for (link = &tile->tlink;
|
|
|
|
*link != NULL;
|
|
|
|
link = &(*link)->next)
|
1999-02-01 04:52:15 +08:00
|
|
|
{
|
1999-12-14 03:48:24 +08:00
|
|
|
if (((*link)->tm == tm) && ((*link)->tile_num == tile_num))
|
1999-02-01 04:52:15 +08:00
|
|
|
break;
|
|
|
|
}
|
1998-07-10 10:43:12 +08:00
|
|
|
|
2004-02-18 21:53:19 +08:00
|
|
|
if (*link == NULL)
|
1998-07-03 07:29:44 +08:00
|
|
|
{
|
1999-12-14 03:48:24 +08:00
|
|
|
g_warning ("Tried to detach a nonattached tile -- TILE BUG!");
|
1998-07-03 07:29:44 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1998-07-10 10:43:12 +08:00
|
|
|
tmp = *link;
|
|
|
|
*link = tmp->next;
|
|
|
|
g_free (tmp);
|
1998-07-03 07:29:44 +08:00
|
|
|
|
1998-07-12 01:23:03 +08:00
|
|
|
tile_share_count--;
|
1998-07-10 10:43:12 +08:00
|
|
|
tile->share_count--;
|
2004-02-18 21:53:19 +08:00
|
|
|
|
1998-07-10 10:43:12 +08:00
|
|
|
if (tile->share_count == 0 && tile->ref_count == 0)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
1998-07-10 10:43:12 +08:00
|
|
|
tile_destroy (tile);
|
|
|
|
return;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
1998-07-10 10:43:12 +08:00
|
|
|
TILE_MUTEX_UNLOCK (tile);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
1998-07-03 07:29:44 +08:00
|
|
|
|
2001-01-22 11:30:42 +08:00
|
|
|
gpointer
|
|
|
|
tile_data_pointer (Tile *tile,
|
|
|
|
gint xoff,
|
|
|
|
gint yoff)
|
1998-08-12 01:35:34 +08:00
|
|
|
{
|
2001-01-22 11:30:42 +08:00
|
|
|
gint offset;
|
|
|
|
|
|
|
|
offset = yoff * tile->ewidth + xoff;
|
|
|
|
|
|
|
|
return (gpointer) (tile->data + offset * tile->bpp);
|
1998-08-12 01:35:34 +08:00
|
|
|
}
|