gimp/app/tile_cache.c

405 lines
8.8 KiB
C
Raw Normal View History

app/Makefile.am app/channel_pvt.h app/drawable_pvt.h app/gdisplayF.h 2000-12-29 Michael Natterer <mitch@gimp.org> * app/Makefile.am * app/channel_pvt.h * app/drawable_pvt.h * app/gdisplayF.h * app/gimpdrawableP.h * app/gimpimageP.h * app/layer_pvt.h * app/toolsF.h: removed these files. * app/apptypes.h * tools/pdbgen/enums.pl: added tons of opaque typedefs and enums. * tools/pdbgen/pdb/brush_select.pdb * tools/pdbgen/pdb/brushes.pdb * tools/pdbgen/pdb/channel.pdb * tools/pdbgen/pdb/color.pdb * tools/pdbgen/pdb/convert.pdb * tools/pdbgen/pdb/display.pdb * tools/pdbgen/pdb/drawable.pdb * tools/pdbgen/pdb/fileops.pdb * tools/pdbgen/pdb/gradient_select.pdb * tools/pdbgen/pdb/gradients.pdb * tools/pdbgen/pdb/help.pdb * tools/pdbgen/pdb/image.pdb * tools/pdbgen/pdb/layer.pdb * tools/pdbgen/pdb/pattern_select.pdb * tools/pdbgen/pdb/patterns.pdb * tools/pdbgen/pdb/selection.pdb * tools/pdbgen/pdb/tools.pdb * app/*: chainsaw #include cleanup: - Never (never!!) include stuff in header files except where we need access to structures' contents (like derived objects). - Added prototypes and proper formating in many files. - The #include order in *all* *.c files is as follows: #include "config.h" #include <system stuff> #include <gtk/gtk.h> #include "apptypes.h" #include "gimp stuff" #include "libgimp stuff" #include "libgimp/gimpintl.h" By following this scheme we can easily see a file's dependencies from it's #include's and can grep for the inclusion to find out where a file is used. * tools/pdbgen/app.pl: changed to follow the include scheme above. * libgimp/Makefile.am * libgimp/gimpuitypes.h: new file, included from libgimp/gimpui.h and from app/apptypes.h. * libgimp/gimpcolorbutton.[ch] * libgimp/gimpdialog.[ch] * libgimp/gimphelpui.[ch] * libgimp/gimpparasite.[ch] * libgimp/gimppatheditor.[ch] * libgimp/gimpprotocol.c * libgimp/gimpquerybox.[ch] * libgimp/gimpsizeentry.[ch] * libgimp/gimptypes.h * libgimp/gimpui.h * libgimp/gimpunit.h * libgimp/gimpunitmenu.[ch] * libgimp/gimpwidgets.[ch]: changed accordingly. * plug-ins/FractalExplorer/Dialogs.c * plug-ins/gdyntext/message_window.c * plug-ins/imagemap/imap_default_dialog.c * plug-ins/imagemap/imap_file.c: these files used to include "libgimp/gimpui.h" without including "libgimp/gimp.h". This is no longer possible because the libgimpui headers don't inlcude "libgimp/gimpunit.h" any more.
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"
#ifdef USE_PTHREADS
#include <pthread.h>
#endif
app/Makefile.am app/channel_pvt.h app/drawable_pvt.h app/gdisplayF.h 2000-12-29 Michael Natterer <mitch@gimp.org> * app/Makefile.am * app/channel_pvt.h * app/drawable_pvt.h * app/gdisplayF.h * app/gimpdrawableP.h * app/gimpimageP.h * app/layer_pvt.h * app/toolsF.h: removed these files. * app/apptypes.h * tools/pdbgen/enums.pl: added tons of opaque typedefs and enums. * tools/pdbgen/pdb/brush_select.pdb * tools/pdbgen/pdb/brushes.pdb * tools/pdbgen/pdb/channel.pdb * tools/pdbgen/pdb/color.pdb * tools/pdbgen/pdb/convert.pdb * tools/pdbgen/pdb/display.pdb * tools/pdbgen/pdb/drawable.pdb * tools/pdbgen/pdb/fileops.pdb * tools/pdbgen/pdb/gradient_select.pdb * tools/pdbgen/pdb/gradients.pdb * tools/pdbgen/pdb/help.pdb * tools/pdbgen/pdb/image.pdb * tools/pdbgen/pdb/layer.pdb * tools/pdbgen/pdb/pattern_select.pdb * tools/pdbgen/pdb/patterns.pdb * tools/pdbgen/pdb/selection.pdb * tools/pdbgen/pdb/tools.pdb * app/*: chainsaw #include cleanup: - Never (never!!) include stuff in header files except where we need access to structures' contents (like derived objects). - Added prototypes and proper formating in many files. - The #include order in *all* *.c files is as follows: #include "config.h" #include <system stuff> #include <gtk/gtk.h> #include "apptypes.h" #include "gimp stuff" #include "libgimp stuff" #include "libgimp/gimpintl.h" By following this scheme we can easily see a file's dependencies from it's #include's and can grep for the inclusion to find out where a file is used. * tools/pdbgen/app.pl: changed to follow the include scheme above. * libgimp/Makefile.am * libgimp/gimpuitypes.h: new file, included from libgimp/gimpui.h and from app/apptypes.h. * libgimp/gimpcolorbutton.[ch] * libgimp/gimpdialog.[ch] * libgimp/gimphelpui.[ch] * libgimp/gimpparasite.[ch] * libgimp/gimppatheditor.[ch] * libgimp/gimpprotocol.c * libgimp/gimpquerybox.[ch] * libgimp/gimpsizeentry.[ch] * libgimp/gimptypes.h * libgimp/gimpui.h * libgimp/gimpunit.h * libgimp/gimpunitmenu.[ch] * libgimp/gimpwidgets.[ch]: changed accordingly. * plug-ins/FractalExplorer/Dialogs.c * plug-ins/gdyntext/message_window.c * plug-ins/imagemap/imap_default_dialog.c * plug-ins/imagemap/imap_file.c: these files used to include "libgimp/gimpui.h" without including "libgimp/gimp.h". This is no longer possible because the libgimpui headers don't inlcude "libgimp/gimpunit.h" any more.
2000-12-29 23:22:01 +08:00
#include <gtk/gtk.h>
#include "apptypes.h"
#include "gimprc.h"
#include "tile.h"
#include "tile_cache.h"
#include "tile_swap.h"
#include "tile_pvt.h"
1997-11-25 06:05:25 +08:00
/* This is the percentage of the maximum cache size that should be cleared
* from the cache when an eviction is necessary
*/
#define FREE_QUANTUM 0.1
1997-11-25 06:05:25 +08:00
#define IDLE_SWAPPER_TIMEOUT 250
static void tile_cache_init (void);
static gint tile_cache_zorch_next (void);
static void tile_cache_flush_internal (Tile *tile);
1997-11-25 06:05:25 +08:00
#ifdef USE_PTHREADS
static gpointer tile_idle_thread (gpointer data);
#else
static gint tile_idle_preswap (gpointer data);
#endif
1997-11-25 06:05:25 +08:00
1997-11-25 06:05:25 +08:00
static int initialize = TRUE;
typedef struct _TileList
{
Tile *first;
Tile *last;
} TileList;
1997-11-25 06:05:25 +08:00
static unsigned long max_tile_size = TILE_WIDTH * TILE_HEIGHT * 4;
static unsigned long cur_cache_size = 0;
static unsigned long max_cache_size = 0;
static unsigned long cur_cache_dirty = 0;
static TileList clean_list = { NULL, NULL };
static TileList dirty_list = { NULL, NULL };
#ifdef USE_PTHREADS
static pthread_t preswap_thread;
static pthread_mutex_t dirty_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t dirty_signal = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t tile_mutex = PTHREAD_MUTEX_INITIALIZER;
#define CACHE_LOCK pthread_mutex_lock(&tile_mutex)
#define CACHE_UNLOCK pthread_mutex_unlock(&tile_mutex)
#else
static gint idle_swapper = 0;
#define CACHE_LOCK /*nothing*/
#define CACHE_UNLOCK /*nothing*/
#endif
1997-11-25 06:05:25 +08:00
void
tile_cache_insert (Tile *tile)
{
TileList *list;
TileList *newlist;
1997-11-25 06:05:25 +08:00
if (initialize)
tile_cache_init ();
CACHE_LOCK;
if (tile->data == NULL) goto out;
1997-11-25 06:05:25 +08:00
/* First check and see if the tile is already
* in the cache. In that case we will simply place
* it at the end of the tile list to indicate that
* it was the most recently accessed tile.
*/
list = (TileList*)(tile->listhead);
newlist = (tile->dirty || tile->swap_offset == -1) ? &dirty_list : &clean_list;
1997-11-25 06:05:25 +08:00
/* if list is NULL, the tile is not in the cache */
1997-11-25 06:05:25 +08:00
if (list)
{
/* Tile is in the cache. Remove it from its current list and
put it at the tail of the proper list (clean or dirty) */
if (tile->next)
tile->next->prev = tile->prev;
else
list->last = tile->prev;
if (tile->prev)
tile->prev->next = tile->next;
else
list->first = tile->next;
tile->listhead = NULL;
if (list == &dirty_list) cur_cache_dirty -= tile_size (tile);
1997-11-25 06:05:25 +08:00
}
else
{
/* The tile was not in the cache. First check and see
* if there is room in the cache. If not then we'll have
* to make room first. Note: it might be the case that the
* cache is smaller than the size of a tile in which case
* it won't be possible to put it in the cache.
*/
while ((cur_cache_size + max_tile_size) > max_cache_size)
1997-11-25 06:05:25 +08:00
{
1998-07-12 01:23:03 +08:00
if (!tile_cache_zorch_next())
{
g_warning ("cache: unable to find room for a tile");
1998-07-12 01:23:03 +08:00
goto out;
/* goto grump;*/
1998-07-12 01:23:03 +08:00
}
1997-11-25 06:05:25 +08:00
}
/*grump:*/
1997-11-25 06:05:25 +08:00
/* Note the increase in the number of bytes the cache
* is referencing.
*/
cur_cache_size += tile_size (tile);
}
/* Put the tile at the end of the proper list */
tile->next = NULL;
tile->prev = newlist->last;
tile->listhead = newlist;
if (newlist->last) newlist->last->next = tile;
else newlist->first = tile;
newlist->last = tile;
/* gosgood@idt.net 1999-12-04 */
/* bytes on cur_cache_dirty miscounted in CVS 1.12: */
/* Invariant: test for selecting dirty list should be the same */
/* as counting files dirty. */
if ((tile->dirty) || ( tile->swap_offset == -1))
{
cur_cache_dirty += tile_size (tile);
1998-07-13 06:35:01 +08:00
if (1)
{
#ifdef USE_PTHREADS
1998-07-13 06:35:01 +08:00
pthread_mutex_lock(&dirty_mutex);
pthread_cond_signal(&dirty_signal);
pthread_mutex_unlock(&dirty_mutex);
#endif
1998-07-13 06:35:01 +08:00
}
}
out:
CACHE_UNLOCK;
1997-11-25 06:05:25 +08:00
}
void
tile_cache_flush (Tile *tile)
{
if (initialize)
tile_cache_init ();
CACHE_LOCK;
tile_cache_flush_internal (tile);
CACHE_UNLOCK;
}
static void
tile_cache_flush_internal (Tile *tile)
{
TileList *list;
1997-11-25 06:05:25 +08:00
/* Find where the tile is in the cache.
*/
list = (TileList*)(tile->listhead);
1997-11-25 06:05:25 +08:00
if (list)
1997-11-25 06:05:25 +08:00
{
/* Note the decrease in the number of bytes the cache
* is referencing.
*/
cur_cache_size -= tile_size (tile);
if (list == &dirty_list) cur_cache_dirty -= tile_size (tile);
if (tile->next)
tile->next->prev = tile->prev;
else
list->last = tile->prev;
if (tile->prev)
tile->prev->next = tile->next;
else
list->first = tile->next;
tile->listhead = NULL;
1997-11-25 06:05:25 +08:00
}
}
/* untested -- ADM */
1997-11-25 06:05:25 +08:00
void
tile_cache_set_size (gulong cache_size)
1997-11-25 06:05:25 +08:00
{
if (initialize)
tile_cache_init ();
max_cache_size = cache_size;
CACHE_LOCK;
while (cur_cache_size > max_cache_size)
1997-11-25 06:05:25 +08:00
{
if (!tile_cache_zorch_next ())
break;
1997-11-25 06:05:25 +08:00
}
CACHE_UNLOCK;
1997-11-25 06:05:25 +08:00
}
static void
tile_cache_init (void)
1997-11-25 06:05:25 +08:00
{
if (initialize)
{
initialize = FALSE;
clean_list.first = clean_list.last = NULL;
dirty_list.first = dirty_list.last = NULL;
1997-11-25 06:05:25 +08:00
max_cache_size = tile_cache_size;
#ifdef USE_PTHREADS
pthread_create (&preswap_thread, NULL, &tile_idle_thread, NULL);
#else
idle_swapper = gtk_timeout_add (IDLE_SWAPPER_TIMEOUT,
(GtkFunction) tile_idle_preswap,
(gpointer) 0);
#endif
1997-11-25 06:05:25 +08:00
}
}
static gint
tile_cache_zorch_next (void)
1997-11-25 06:05:25 +08:00
{
Tile *tile;
if (clean_list.first)
tile = clean_list.first;
else if (dirty_list.first)
tile = dirty_list.first;
else
return FALSE;
CACHE_UNLOCK;
TILE_MUTEX_LOCK (tile);
CACHE_LOCK;
tile_cache_flush_internal (tile);
if (tile->dirty || tile->swap_offset == -1)
1998-07-13 06:35:01 +08:00
{
tile_swap_out (tile);
}
if (! tile->dirty)
{
g_free (tile->data);
tile->data = NULL;
TILE_MUTEX_UNLOCK (tile);
return TRUE;
}
/* unable to swap out tile for some reason */
TILE_MUTEX_UNLOCK (tile);
return FALSE;
}
#if USE_PTHREADS
static gpointer
tile_idle_thread (gpointer data)
{
Tile *tile;
TileList *list;
gint count;
g_printerr ("starting tile preswapper\n");
1998-07-13 06:35:01 +08:00
count = 0;
while (TRUE)
{
CACHE_LOCK;
if (count > 5 || dirty_list.first == NULL)
{
CACHE_UNLOCK;
count = 0;
pthread_mutex_lock (&dirty_mutex);
pthread_cond_wait (&dirty_signal, &dirty_mutex);
pthread_mutex_unlock (&dirty_mutex);
CACHE_LOCK;
}
if ((tile = dirty_list.first))
{
CACHE_UNLOCK;
TILE_MUTEX_LOCK (tile);
CACHE_LOCK;
if (tile->dirty || tile->swap_offset == -1)
{
list = tile->listhead;
if (list == &dirty_list) cur_cache_dirty -= tile_size (tile);
if (tile->next)
tile->next->prev = tile->prev;
else
list->last = tile->prev;
if (tile->prev)
tile->prev->next = tile->next;
else
list->first = tile->next;
tile->next = NULL;
tile->prev = clean_list.last;
tile->listhead = &clean_list;
if (clean_list.last) clean_list.last->next = tile;
else clean_list.first = tile;
clean_list.last = tile;
CACHE_UNLOCK;
tile_swap_out (tile);
}
else
{
CACHE_UNLOCK;
}
TILE_MUTEX_UNLOCK (tile);
}
else
{
CACHE_UNLOCK;
}
1998-07-13 06:35:01 +08:00
count++;
}
1997-11-25 06:05:25 +08:00
}
#else
static gint
tile_idle_preswap (gpointer data)
1997-11-25 06:05:25 +08:00
{
Tile *tile;
if (cur_cache_dirty*2 < max_cache_size) return TRUE;
if ((tile = dirty_list.first))
{
tile_swap_out(tile);
dirty_list.first = tile->next;
if (tile->next)
tile->next->prev = NULL;
else
dirty_list.last = NULL;
tile->next = NULL;
tile->prev = clean_list.last;
tile->listhead = &clean_list;
if (clean_list.last) clean_list.last->next = tile;
else clean_list.first = tile;
clean_list.last = tile;
cur_cache_dirty -= tile_size (tile);
}
return TRUE;
1997-11-25 06:05:25 +08:00
}
#endif