2006-12-10 05:33:38 +08:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
2000-12-29 23:22:01 +08:00
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
2000-12-29 23:22:01 +08:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2009-01-18 06:28:01 +08:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2000-12-29 23:22:01 +08:00
|
|
|
* (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
|
2009-01-18 06:28:01 +08:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2000-12-29 23:22:01 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#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-cache.h"
|
|
|
|
#include "tile-manager.h"
|
2007-10-10 00:14:10 +08:00
|
|
|
#include "tile-rowhints.h"
|
2001-05-15 19:25:25 +08:00
|
|
|
#include "tile-swap.h"
|
2007-10-10 00:14:10 +08:00
|
|
|
#include "tile-private.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 */
|
|
|
|
|
2007-02-23 06:28:24 +08:00
|
|
|
/* This is being used from tile-swap, but just for debugging purposes. */
|
|
|
|
static gint tile_ref_count = 0;
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef TILE_PROFILING
|
|
|
|
|
|
|
|
static gint tile_count = 0;
|
|
|
|
static gint tile_share_count = 0;
|
|
|
|
static gint tile_active_count = 0;
|
|
|
|
|
2009-05-26 16:39:10 +08:00
|
|
|
gint tile_exist_peak = 0;
|
|
|
|
gint tile_exist_count = 0;
|
2007-02-23 06:28:24 +08:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
1998-07-03 07:29:44 +08:00
|
|
|
|
2008-08-25 21:00:28 +08:00
|
|
|
static void tile_destroy (Tile *tile);
|
2001-01-22 11:30:42 +08:00
|
|
|
|
2007-05-22 23:12:30 +08:00
|
|
|
Tile *
|
|
|
|
tile_new (gint bpp)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2007-06-20 19:59:50 +08:00
|
|
|
Tile *tile = g_slice_new0 (Tile);
|
|
|
|
|
2000-12-29 23:22:01 +08:00
|
|
|
tile->ewidth = TILE_WIDTH;
|
|
|
|
tile->eheight = TILE_HEIGHT;
|
|
|
|
tile->bpp = bpp;
|
1997-11-25 06:05:25 +08:00
|
|
|
tile->swap_offset = -1;
|
1999-05-09 23:45:37 +08:00
|
|
|
|
2007-02-23 06:28:24 +08:00
|
|
|
#ifdef TILE_PROFILING
|
2001-01-22 11:30:42 +08:00
|
|
|
tile_count++;
|
1999-12-14 03:48:24 +08:00
|
|
|
#endif
|
2007-05-22 23:12:30 +08:00
|
|
|
|
|
|
|
return tile;
|
2007-02-23 06:28:24 +08:00
|
|
|
}
|
1999-12-14 03:48:24 +08:00
|
|
|
|
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
|
|
|
*/
|
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
|
|
|
{
|
2009-05-28 05:56:22 +08:00
|
|
|
/* remove from cache, move to main store */
|
|
|
|
tile_cache_flush (tile);
|
2007-02-23 06:28:24 +08:00
|
|
|
|
|
|
|
#ifdef TILE_PROFILING
|
2001-01-22 11:30:42 +08:00
|
|
|
tile_active_count++;
|
2007-02-23 06:28:24 +08:00
|
|
|
#endif
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
2007-02-23 06:28:24 +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-03 07:29:44 +08:00
|
|
|
/* Call 'tile_manager_validate' if the tile was invalid.
|
|
|
|
*/
|
2004-02-19 02:57:43 +08:00
|
|
|
if (! tile->valid)
|
1998-07-10 10:43:12 +08:00
|
|
|
{
|
|
|
|
/* an invalid tile should never be shared, so this should work */
|
2007-12-18 21:07:01 +08:00
|
|
|
tile_manager_validate_tile (tile->tlink->tm, tile);
|
1998-07-10 10:43:12 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-02-18 21:53:19 +08:00
|
|
|
tile_release (Tile *tile,
|
2006-04-12 20:49:29 +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
|
|
|
|
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)
|
2006-04-12 20:49:29 +08:00
|
|
|
{
|
|
|
|
for (y = 0; y < tile->eheight; y++)
|
2007-02-23 06:28:24 +08:00
|
|
|
tile->rowhint[y] = TILEROWHINT_UNKNOWN;
|
2006-04-12 20:49:29 +08:00
|
|
|
}
|
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
|
|
|
{
|
2007-02-23 06:28:24 +08:00
|
|
|
#ifdef TILE_PROFILING
|
1999-12-06 04:31:50 +08:00
|
|
|
tile_active_count--;
|
2007-02-23 06:28:24 +08:00
|
|
|
#endif
|
2001-01-22 11:30:42 +08:00
|
|
|
|
1998-07-10 10:43:12 +08:00
|
|
|
if (tile->share_count == 0)
|
2006-04-12 20:49:29 +08:00
|
|
|
{
|
|
|
|
/* tile is truly dead */
|
|
|
|
tile_destroy (tile);
|
|
|
|
return; /* skip terminal unlock */
|
|
|
|
}
|
2004-02-18 21:53:19 +08:00
|
|
|
else
|
2006-04-12 20:49:29 +08:00
|
|
|
{
|
|
|
|
/* last reference was just released, so move the tile to the
|
|
|
|
tile cache */
|
|
|
|
tile_cache_insert (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.
|
|
|
|
*/
|
2005-09-30 06:24:57 +08:00
|
|
|
tile->data = g_new (guchar, tile->size);
|
1999-12-14 03:48:24 +08:00
|
|
|
|
2007-02-23 06:28:24 +08:00
|
|
|
#ifdef TILE_PROFILING
|
1999-12-14 03:48:24 +08:00
|
|
|
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)
|
|
|
|
{
|
2006-07-06 20:27:32 +08:00
|
|
|
if (G_UNLIKELY (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;
|
|
|
|
}
|
2006-07-06 20:27:32 +08:00
|
|
|
if (G_UNLIKELY (tile->share_count))
|
1998-07-12 01:23:03 +08:00
|
|
|
{
|
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;
|
2009-05-26 16:39:10 +08:00
|
|
|
#ifdef TILE_PROFILING
|
|
|
|
tile_exist_count--;
|
|
|
|
#endif
|
1998-07-10 10:43:12 +08:00
|
|
|
}
|
2004-02-18 21:53:19 +08:00
|
|
|
if (tile->rowhint)
|
1999-12-14 03:48:24 +08:00
|
|
|
{
|
2007-05-23 02:29:33 +08:00
|
|
|
g_slice_free1 (sizeof (TileRowHint) * TILE_HEIGHT, tile->rowhint);
|
1999-12-14 03:48:24 +08:00
|
|
|
tile->rowhint = NULL;
|
|
|
|
}
|
2009-05-28 05:56:22 +08:00
|
|
|
|
|
|
|
/* must flush before deleting swap */
|
|
|
|
tile_cache_flush (tile);
|
|
|
|
|
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);
|
|
|
|
}
|
2004-02-18 21:53:19 +08:00
|
|
|
|
2007-05-22 23:12:30 +08:00
|
|
|
g_slice_free (Tile, tile);
|
2001-01-22 11:30:42 +08:00
|
|
|
|
2007-02-23 06:28:24 +08:00
|
|
|
#ifdef TILE_PROFILING
|
2001-01-22 11:30:42 +08:00
|
|
|
tile_count--;
|
1999-12-14 03:48:24 +08:00
|
|
|
#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).
|
|
|
|
*/
|
2005-09-30 06:24:57 +08:00
|
|
|
return tile->size;
|
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-19 02:57:43 +08:00
|
|
|
gboolean
|
1998-08-12 01:35:34 +08:00
|
|
|
tile_is_valid (Tile *tile)
|
|
|
|
{
|
|
|
|
return tile->valid;
|
|
|
|
}
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
void
|
2001-01-22 11:30:42 +08:00
|
|
|
tile_attach (Tile *tile,
|
2006-04-12 20:49:29 +08:00
|
|
|
void *tm,
|
|
|
|
gint tile_num)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2008-08-08 04:08:21 +08:00
|
|
|
TileLink *new;
|
1998-07-10 10:43:12 +08:00
|
|
|
|
2004-02-19 02:57:43 +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 */
|
2007-12-18 21:07:01 +08:00
|
|
|
tile_manager_validate_tile (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++;
|
2007-02-23 06:28:24 +08:00
|
|
|
|
|
|
|
#ifdef TILE_PROFILING
|
1998-07-12 01:23:03 +08:00
|
|
|
tile_share_count++;
|
2007-02-23 06:28:24 +08:00
|
|
|
#endif
|
2001-01-22 11:30:42 +08:00
|
|
|
|
1998-07-10 10:43:12 +08:00
|
|
|
#ifdef TILE_DEBUG
|
2004-02-19 02:57:43 +08:00
|
|
|
g_printerr ("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 */
|
2008-08-08 04:08:21 +08:00
|
|
|
new = g_slice_new (TileLink);
|
2007-05-23 02:29:33 +08:00
|
|
|
|
2008-08-08 04:08:21 +08:00
|
|
|
new->tm = tm;
|
|
|
|
new->tile_num = tile_num;
|
|
|
|
new->next = tile->tlink;
|
2001-01-22 11:30:42 +08:00
|
|
|
|
2008-08-08 04:08:21 +08:00
|
|
|
tile->tlink = new;
|
1998-07-10 10:43:12 +08:00
|
|
|
}
|
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,
|
2006-04-12 20:49:29 +08:00
|
|
|
void *tm,
|
|
|
|
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
|
2004-02-19 02:57:43 +08:00
|
|
|
g_printerr ("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))
|
2006-04-12 20:49:29 +08:00
|
|
|
break;
|
1999-02-01 04:52:15 +08:00
|
|
|
}
|
1998-07-10 10:43:12 +08:00
|
|
|
|
2006-07-06 20:27:32 +08:00
|
|
|
if (G_UNLIKELY (*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;
|
2007-05-23 02:29:33 +08:00
|
|
|
|
|
|
|
g_slice_free (TileLink, tmp);
|
1998-07-03 07:29:44 +08:00
|
|
|
|
2007-02-23 06:28:24 +08:00
|
|
|
#ifdef TILE_PROFILING
|
1998-07-12 01:23:03 +08:00
|
|
|
tile_share_count--;
|
2007-02-23 06:28:24 +08:00
|
|
|
#endif
|
|
|
|
|
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)
|
2006-09-07 17:41:23 +08:00
|
|
|
tile_destroy (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,
|
2006-04-12 20:49:29 +08:00
|
|
|
gint xoff,
|
|
|
|
gint yoff)
|
1998-08-12 01:35:34 +08:00
|
|
|
{
|
2008-08-25 21:00:28 +08:00
|
|
|
return TILE_DATA_POINTER (tile, xoff, yoff);
|
1998-08-12 01:35:34 +08:00
|
|
|
}
|
2007-02-23 06:28:24 +08:00
|
|
|
|
|
|
|
gint
|
|
|
|
tile_global_refcount (void)
|
|
|
|
{
|
|
|
|
return tile_ref_count;
|
|
|
|
}
|