From 40c961bdf2523ebdcb6717f2537f97156fd19ab4 Mon Sep 17 00:00:00 2001 From: Marijn van Vliet Date: Sun, 7 Feb 2010 13:59:18 +0000 Subject: [PATCH] Graphics caching not operational. git-svn-id: http://lassie.dyndns-server.com/svn/gargoyle-gtk@224 ddfedd41-794f-dd11-ae45-00112f111e67 --- libchimara/chimara-glk-private.h | 2 ++ libchimara/chimara-glk.c | 5 ++++- libchimara/graphics.c | 31 +++++++++++++++++-------------- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/libchimara/chimara-glk-private.h b/libchimara/chimara-glk-private.h index 99e8589..85e3af2 100644 --- a/libchimara/chimara-glk-private.h +++ b/libchimara/chimara-glk-private.h @@ -69,6 +69,8 @@ struct _ChimaraGlkPrivate { GCond *resource_loaded; GCond *resource_info_available; guint32 resource_available; + /* Image cache */ + GSList *image_cache; /* *** Glk library data *** */ /* User-defined interrupt handler */ diff --git a/libchimara/chimara-glk.c b/libchimara/chimara-glk.c index 4f1d391..f4cd156 100644 --- a/libchimara/chimara-glk.c +++ b/libchimara/chimara-glk.c @@ -107,10 +107,11 @@ chimara_glk_init(ChimaraGlk *self) priv->ignore_next_arrange_event = FALSE; priv->char_input_queue = g_async_queue_new(); priv->line_input_queue = g_async_queue_new(); + /* Should be g_async_queue_new_full(g_free); but only in GTK >= 2.16 */ priv->resource_lock = g_mutex_new(); priv->resource_loaded = g_cond_new(); priv->resource_info_available = g_cond_new(); - /* Should be g_async_queue_new_full(g_free); but only in GTK >= 2.16 */ + priv->image_cache = NULL; priv->interrupt_handler = NULL; priv->root_window = NULL; priv->fileref_list = NULL; @@ -223,6 +224,8 @@ chimara_glk_finalize(GObject *object) g_cond_free(priv->resource_info_available); g_mutex_unlock(priv->resource_lock); g_mutex_free(priv->resource_lock); + g_slist_foreach(priv->image_cache, (GFunc)clear_image_cache, NULL); + g_slist_free(priv->image_cache); /* Unref input queues (this should destroy them since any Glk thread has stopped by now */ g_async_queue_unref(priv->char_input_queue); g_async_queue_unref(priv->line_input_queue); diff --git a/libchimara/graphics.c b/libchimara/graphics.c index 13048f9..2b2258d 100644 --- a/libchimara/graphics.c +++ b/libchimara/graphics.c @@ -71,20 +71,22 @@ load_image_in_cache(glui32 image, gint width, gint height) g_mutex_unlock(glk_data->resource_lock); /* Store the image in the cache */ + gdk_threads_enter(); + if( g_slist_length(glk_data->image_cache) >= IMAGE_CACHE_MAX_NUM ) { - printf("Cache size exceeded\n"); struct image_info *head = (struct image_info*) glk_data->image_cache->data; gdk_pixbuf_unref(head->pixbuf); g_free(head); glk_data->image_cache = g_slist_remove_link(glk_data->image_cache, glk_data->image_cache); } - printf("Loading pixbuf\n"); info->pixbuf = gdk_pixbuf_loader_get_pixbuf(loader); + gdk_pixbuf_ref(info->pixbuf); info->width = gdk_pixbuf_get_width(info->pixbuf); info->height = gdk_pixbuf_get_height(info->pixbuf); - printf("Caching pixbuf\n"); glk_data->image_cache = g_slist_prepend(glk_data->image_cache, info); + gdk_threads_leave(); + g_object_unref(loader); return info; } @@ -128,54 +130,57 @@ clear_image_cache(struct image_info *data, gpointer user_data) static struct image_info* image_cache_find(struct image_info* to_find) { - printf("Finding image %d\n", to_find->resource_number); ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key); GSList *link = glk_data->image_cache; + gdk_threads_enter(); + /* Empty cache */ if(link == NULL) { - printf("Cache is empty\n"); + gdk_threads_leave(); + printf("Cache miss for image %d\n", to_find->resource_number); return NULL; } /* Iterate over the cache to find the correct image and size */ do { struct image_info *info = (struct image_info*) link->data; - printf("Examining cache entry %d\n", info->resource_number); if(info->resource_number == to_find->resource_number) { /* Check size: are we looking for a scaled version or the original one? */ if(to_find->scaled) { if(info->width >= to_find->width && info->height >= to_find->height) { + gdk_threads_leave(); + printf("Cache hit for image %d\n", to_find->resource_number); return info; /* Found a good enough match */ } } else { if(!info->scaled) { - printf("Cache hit\n"); + gdk_threads_leave(); + printf("Cache hit for image %d\n", to_find->resource_number); return info; /* Found a match */ } } } } while( (link = g_slist_next(link)) ); + gdk_threads_leave(); + + printf("Cache miss for image %d\n", to_find->resource_number); return NULL; /* No match found */ } glui32 glk_image_get_info(glui32 image, glui32 *width, glui32 *height) { - printf("get_info(%d)\n", image); struct image_info *to_find = g_new0(struct image_info, 1); struct image_info *found; to_find->resource_number = image; to_find->scaled = FALSE; /* we want the original image size */ if( !(found = image_cache_find(to_find)) ) { - printf("Cache miss for %d\n", image); found = load_image_in_cache(image, 0, 0); if(found == NULL) return FALSE; - } else { - printf("Cache hit for %d\n", image); } if(width != NULL) @@ -188,7 +193,6 @@ glk_image_get_info(glui32 image, glui32 *width, glui32 *height) glui32 glk_image_draw(winid_t win, glui32 image, glsi32 val1, glsi32 val2) { - printf("image_draw(%d)\n", image); VALID_WINDOW(win, return FALSE); g_return_val_if_fail(win->type == wintype_Graphics, FALSE); @@ -214,8 +218,7 @@ glk_image_draw(winid_t win, glui32 image, glsi32 val1, glsi32 val2) return FALSE; } - printf("image info: %d x %d, scaled=%d, pixbufaddr=%d\n", info->width, info->height, (int)info->scaled, (int)info->pixbuf); - gdk_draw_pixbuf( GDK_DRAWABLE(canvas), NULL, info->pixbuf, 0, 0, val1, val2, -1, -1, GDK_RGB_DITHER_NONE, 0, 0 ); + gdk_draw_pixbuf( GDK_DRAWABLE(canvas), NULL, GDK_PIXBUF((GdkPixbuf*)info->pixbuf), 0, 0, val1, val2, -1, -1, GDK_RGB_DITHER_NONE, 0, 0 ); /* Update the screen */ gtk_widget_queue_draw(win->widget); -- 2.30.2