Graphics caching not operational.
authorrodin <rodin@ddfedd41-794f-dd11-ae45-00112f111e67>
Sun, 7 Feb 2010 13:59:18 +0000 (13:59 +0000)
committerrodin <rodin@ddfedd41-794f-dd11-ae45-00112f111e67>
Sun, 7 Feb 2010 13:59:18 +0000 (13:59 +0000)
libchimara/chimara-glk-private.h
libchimara/chimara-glk.c
libchimara/graphics.c

index 99e858992b42f1012e6e9e032cd04f8f7a0a9815..85e3af28dbd4d0c238a05a71584cfe6550d9424c 100644 (file)
@@ -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 */
index 4f1d39102dd9db62754eb4ecc30a6bfe5a8f3b6c..f4cd156fe3da9ae3a1d06127dde408aed12efd5d 100644 (file)
@@ -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);
index 13048f9aa41587f81af0520c6307bcf8c0787e43..2b2258d934e959c8e7c101c612d38faa8ea76ac8 100644 (file)
@@ -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);