code cleanup. Removed the PreviewCache struct that used to wrap TempBuf

2003-09-05  Sven Neumann  <sven@gimp.org>

	* app/core/gimppreviewcache.[ch]: code cleanup. Removed the
	PreviewCache struct that used to wrap TempBuf but didn't add any
	additional information. Actually free the cached temp_buf when
	removing an entry from cache. Free the GSList when invalidating
	the preview cache.
This commit is contained in:
Sven Neumann
2003-09-05 16:51:51 +00:00
committed by Sven Neumann
parent 86133ac68a
commit 3a3a57014b
3 changed files with 112 additions and 157 deletions

View File

@ -1,3 +1,11 @@
2003-09-05 Sven Neumann <sven@gimp.org>
* app/core/gimppreviewcache.[ch]: code cleanup. Removed the
PreviewCache struct that used to wrap TempBuf but didn't add any
additional information. Actually free the cached temp_buf when
removing an entry from cache. Free the GSList when invalidating
the preview cache.
2003-09-05 Helvetix Victorinox <helvetix@gimp.org> 2003-09-05 Helvetix Victorinox <helvetix@gimp.org>
* app/paint-funcs/paint-funcs.c (initial_sub_region): Fixed * app/paint-funcs/paint-funcs.c (initial_sub_region): Fixed

View File

@ -29,191 +29,155 @@
#define MAX_CACHE_PREVIEWS 5 #define MAX_CACHE_PREVIEWS 5
#undef PREVIEW_CACHE_DEBUG
typedef struct
typedef struct _PreviewCache
{ {
TempBuf *preview; TempBuf *buf;
gint width; gint width;
gint height; gint height;
} PreviewCache;
typedef struct _PreviewNearest
{
PreviewCache *pc;
gint width;
gint height;
} PreviewNearest; } PreviewNearest;
static gint static gint
preview_cache_compare (gconstpointer a, preview_cache_compare (gconstpointer a,
gconstpointer b) gconstpointer b)
{ {
PreviewCache *pc1 = (PreviewCache *) a; TempBuf *buf1 = (TempBuf *) a;
PreviewCache *pc2 = (PreviewCache *) b; TempBuf *buf2 = (TempBuf *) b;
if (pc1->width > pc2->width && pc1->height > pc2->height) if (buf1->width > buf2->width && buf1->height > buf2->height)
return -1; return -1;
return 1; return 1;
} }
static void static void
preview_cache_find_exact (gpointer data, preview_cache_find_exact (gpointer data,
gpointer udata) gpointer udata)
{ {
PreviewCache *pc = (PreviewCache *) data; TempBuf *buf = (TempBuf *) data;
PreviewNearest *pNearest = (PreviewNearest *) udata; PreviewNearest *nearest = (PreviewNearest *) udata;
/* g_print ("this value w,h [%d,%d]\n",pc->width,pc->height); */ if (nearest->buf)
/* if (pNearest->pc) */
/* g_print ("current nearest value w,h [%d,%d]\n",
pNearest->pc->width,pNearest->pc->height); */
if (pNearest->pc)
return; return;
if(pc->width == pNearest->width && if (buf->width == nearest->width && buf->height == nearest->height)
pc->height == pNearest->height)
{ {
/* Ok we could make the preview out of this one... nearest->buf = buf;
* If we already have it are these bigger dimensions?
*/
pNearest->pc = pc;
return; return;
} }
} }
static void static void
preview_cache_find_biggest (gpointer data, preview_cache_find_biggest (gpointer data,
gpointer udata) gpointer udata)
{ {
PreviewCache *pc = (PreviewCache *) data; TempBuf *buf = (TempBuf *) data;
PreviewNearest *pNearest = (PreviewNearest *) udata; PreviewNearest *nearest = (PreviewNearest *) udata;
/* g_print ("this value w,h [%d,%d]\n",pc->width,pc->height); */ if (buf->width >= nearest->width && buf->height >= nearest->height)
/* if (pNearest->pc) */
/* g_print ("current nearest value w,h [%d,%d]\n",
pNearest->pc->width,pNearest->pc->height); */
if (pc->width >= pNearest->width &&
pc->height >= pNearest->height)
{ {
/* Ok we could make the preview out of this one... /* Ok we could make the preview out of this one...
* If we already have it are these bigger dimensions? * If we already have it are these bigger dimensions?
*/ */
if (pNearest->pc) if (nearest->buf)
{ {
if (pNearest->pc->width > pc->width && if (nearest->buf->width > buf->width &&
pNearest->pc->height > pc->height) nearest->buf->height > buf->height)
return; return;
} }
pNearest->pc = pc;
nearest->buf = buf;
} }
} }
static void static void
preview_cache_remove_smallest (GSList **plist) preview_cache_remove_smallest (GSList **plist)
{ {
GSList *list; GSList *list;
PreviewCache *smallest = NULL; TempBuf *smallest = NULL;
/* g_print ("Removing smallest\n"); */
for (list = *plist; list; list = g_slist_next (list)) #ifdef PREVIEW_CACHE_DEBUG
g_print ("preview_cache_remove_smallest\n");
#endif
for (list = *plist; list; list = list->next)
{ {
if (!smallest) if (!smallest)
{ {
smallest = list->data; smallest = list->data;
/* g_print ("init smallest %d,%d\n",
smallest->width,smallest->height); */
} }
else else
{ {
PreviewCache *pcthis = list->data; TempBuf *this = list->data;
/* g_print ("Checking %d,%d\n",pcthis->width,pcthis->height); */ if ((smallest->height * smallest->width) >
if ((smallest->height * smallest->width) >= (this->height * this->width))
(pcthis->height * pcthis->width))
{ {
smallest = pcthis; smallest = this;
/* g_print ("smallest now %d,%d\n",
smallest->width,smallest->height); */
} }
} }
} }
if (*plist && smallest) if (smallest)
*plist = g_slist_remove (*plist, smallest); {
/* g_print ("removed %d,%d\n",smallest->width,smallest->height); */ *plist = g_slist_remove (*plist, smallest);
/* g_print ("removed smallest\n"); */
} #ifdef PREVIEW_CACHE_DEBUG
g_print ("preview_cache_remove_smallest: removed %d x %d\n",
static void smallest->width, smallest->height);
preview_cache_invalidate (gpointer data, #endif
gpointer udata)
{ temp_buf_free (smallest);
PreviewCache *pc = (PreviewCache *) data; }
temp_buf_free (pc->preview);
g_free(pc);
} }
#ifdef PREVIEW_CACHE_DEBUG
static void static void
preview_cache_print (GSList *plist) preview_cache_print (GSList *plist)
{ {
#ifdef PREVIEW_CACHE_DEBUG GSList *list;
GSList *list;
PreviewCache *pc;
g_print ("preview cache dump:\n"); g_print ("preview cache dump:\n");
for (list = plist; list; list = g_slist_next (list)) for (list = plist; list; list = list->next)
{ {
pc = (PreviewCache *) list->data; TempBuf *buf = (TempBuf *) list->data;
g_print ("\tvalue w,h [%d,%d] => %p\n", g_print ("\tvalue w,h [%d,%d]\n", buf->width, buf->height);
pc->width, pc->height, pc->preview);
} }
#endif /* PREVIEW_CACHE_DEBUG */
} }
#endif /* PREVIEW_CACHE_DEBUG */
void void
gimp_preview_cache_invalidate (GSList **plist) gimp_preview_cache_invalidate (GSList **plist)
{ {
/* g_print ("gimp_preview_cache_invalidate\n"); */ #ifdef PREVIEW_CACHE_DEBUG
g_print ("gimp_preview_cache_invalidate\n");
preview_cache_print (*plist); preview_cache_print (*plist);
#endif
g_slist_foreach (*plist, preview_cache_invalidate, NULL); g_slist_foreach (*plist, (GFunc) temp_buf_free, NULL);
g_slist_free (*plist);
*plist = NULL; *plist = NULL;
} }
void void
gimp_preview_cache_add (GSList **plist, gimp_preview_cache_add (GSList **plist,
TempBuf *buf) TempBuf *buf)
{ {
PreviewCache *pc; #ifdef PREVIEW_CACHE_DEBUG
g_print ("gimp_preview_cache_add: %d x %d\n", buf->width, buf->height);
/* g_print ("gimp_preview_cache_add %d %d\n",buf->width,buf->height); */
preview_cache_print (*plist); preview_cache_print (*plist);
#endif
if (g_slist_length (*plist) > MAX_CACHE_PREVIEWS) if (g_slist_length (*plist) >= MAX_CACHE_PREVIEWS)
{ {
/* Remove the smallest */ preview_cache_remove_smallest (plist);
preview_cache_remove_smallest(plist);
} }
pc = g_new0(PreviewCache,1); *plist = g_slist_insert_sorted (*plist, buf, preview_cache_compare);
pc->preview = buf;
pc->width = buf->width;
pc->height = buf->height;
*plist = g_slist_insert_sorted (*plist, pc, preview_cache_compare);
} }
TempBuf * TempBuf *
@ -221,29 +185,34 @@ gimp_preview_cache_get (GSList **plist,
gint width, gint width,
gint height) gint height)
{ {
PreviewNearest pn; PreviewNearest pn;
PreviewCache *pc;
/* g_print ("gimp_preview_cache_get %d %d\n",width,height); */ #ifdef PREVIEW_CACHE_DEBUG
g_print ("gimp_preview_cache_get: %d x %d\n", width, height);
preview_cache_print (*plist); preview_cache_print (*plist);
#endif
pn.pc = NULL; pn.buf = NULL;
pn.width = width; pn.width = width;
pn.height = height; pn.height = height;
g_slist_foreach (*plist, preview_cache_find_exact, &pn); g_slist_foreach (*plist, preview_cache_find_exact, &pn);
if (pn.pc && pn.pc->preview) if (pn.buf)
{ {
/* g_print ("extact value w,h [%d,%d] => %p\n", #ifdef PREVIEW_CACHE_DEBUG
pn.pc->width,pn.pc->height,pn.pc->preview); */ g_print ("gimp_preview_cache_get: found exact match %d x %d\n",
return pn.pc->preview; pn.buf->width, pn.buf->height);
#endif
return pn.buf;
} }
g_slist_foreach (*plist, preview_cache_find_biggest, &pn); g_slist_foreach (*plist, preview_cache_find_biggest, &pn);
if (pn.pc) if (pn.buf)
{ {
TempBuf *preview;
gint pwidth; gint pwidth;
gint pheight; gint pheight;
gdouble x_ratio; gdouble x_ratio;
@ -253,31 +222,19 @@ gimp_preview_cache_get (GSList **plist,
gint loop1; gint loop1;
gint loop2; gint loop2;
/* g_print ("nearest value w,h [%d,%d] => %p\n", #ifdef PREVIEW_CACHE_DEBUG
pn.pc->width,pn.pc->height,pn.pc->preview); */ g_print ("gimp_preview_cache_get: nearest value: %d x %d\n",
pn.buf->width, pn.buf->height);
/* if(pn.pc->width == width && */ #endif
/* pn.pc->height == height) */
/* return pn.pc->preview; */
if (!pn.pc->preview)
{
g_error ("gimp_preview_cache_get:: Invalid cache item");
return NULL;
}
/* Make up new preview from the large one... */ /* Make up new preview from the large one... */
pwidth = pn.buf->width;
pwidth = pn.pc->preview->width; pheight = pn.buf->height;
pheight = pn.pc->preview->height;
/* Now get the real one and add to cache */ /* Now get the real one and add to cache */
/* g_print ("Must create from large preview\n"); */ preview = temp_buf_new (width, height, pn.buf->bytes, 0, 0, NULL);
pc = g_new0 (PreviewCache, 1);
pc->preview = temp_buf_new (width, height, pn.pc->preview->bytes,
0, 0, NULL);
/* preview from nearest bigger one */
/* preview from nearest bigger one */
if (width) if (width)
x_ratio = (gdouble) pwidth / (gdouble) width; x_ratio = (gdouble) pwidth / (gdouble) width;
else else
@ -288,10 +245,8 @@ gimp_preview_cache_get (GSList **plist,
else else
y_ratio = 0.0; y_ratio = 0.0;
src_data = temp_buf_data(pn.pc->preview); src_data = temp_buf_data (pn.buf);
dest_data = temp_buf_data(pc->preview); dest_data = temp_buf_data (preview);
/* g_print ("x_ratio , y_ratio [%f,%f]\n",x_ratio,y_ratio); */
for (loop1 = 0 ; loop1 < height ; loop1++) for (loop1 = 0 ; loop1 < height ; loop1++)
for (loop2 = 0 ; loop2 < width ; loop2++) for (loop2 = 0 ; loop2 < width ; loop2++)
@ -301,26 +256,25 @@ gimp_preview_cache_get (GSList **plist,
guchar *dest_pixel; guchar *dest_pixel;
src_pixel = src_data + src_pixel = src_data +
((gint) (loop2 * x_ratio)) * pn.pc->preview->bytes + ((gint) (loop2 * x_ratio)) * preview->bytes +
((gint) (loop1 * y_ratio)) * pwidth * pn.pc->preview->bytes; ((gint) (loop1 * y_ratio)) * pwidth * preview->bytes;
dest_pixel = dest_data + dest_pixel = dest_data +
(loop2 + loop1 * width) * pn.pc->preview->bytes; (loop2 + loop1 * width) * preview->bytes;
for (i = 0; i < pn.pc->preview->bytes; i++) for (i = 0; i < preview->bytes; i++)
*dest_pixel++ = *src_pixel++; *dest_pixel++ = *src_pixel++;
} }
pc->width = width; gimp_preview_cache_add (plist, preview);
pc->height = height;
*plist = g_slist_insert_sorted (*plist, pc, preview_cache_compare);
/* g_print ("New preview created [%d,%d] => %p\n",
pc->width,pc->height,pc->preview); */
return pc->preview; return preview;
} }
/* g_print ("gimp_preview_cache_get returning NULL\n"); */ #ifdef PREVIEW_CACHE_DEBUG
g_print ("gimp_preview_cache_get returning NULL\n");
#endif
return NULL; return NULL;
} }
@ -332,15 +286,8 @@ gimp_preview_cache_get_memsize (GSList *cache)
g_return_val_if_fail (cache != NULL, 0); g_return_val_if_fail (cache != NULL, 0);
for (list = cache; list; list = g_slist_next (list)) for (list = cache; list; list = list->next)
{ memsize += sizeof (GSList) + temp_buf_get_memsize ((TempBuf *) list->data);
PreviewCache *pc;
pc = (PreviewCache *) list->data;
memsize += (sizeof (PreviewCache) +
temp_buf_get_memsize (pc->preview));
}
return memsize; return memsize;
} }

View File

@ -24,10 +24,10 @@
#define PREVIEW_CACHE_PRIME_HEIGHT 112 #define PREVIEW_CACHE_PRIME_HEIGHT 112
TempBuf * gimp_preview_cache_get (GSList **plist, TempBuf * gimp_preview_cache_get (GSList **plist,
gint width, gint width,
gint height); gint height);
void gimp_preview_cache_add (GSList **plist, void gimp_preview_cache_add (GSList **plist,
TempBuf *buf); TempBuf *buf);
void gimp_preview_cache_invalidate (GSList **plist); void gimp_preview_cache_invalidate (GSList **plist);