X-Git-Url: https://git.street.me.uk/andy/viking.git/blobdiff_plain/1fe49af0ed8ab6c226fd2b3486851a395ee657ab..6493fea21873e09118a6a151ea9f2f48b42cd22f:/src/mapcache.c?ds=inline diff --git a/src/mapcache.c b/src/mapcache.c index 6df3d8c1..7b731bc3 100644 --- a/src/mapcache.c +++ b/src/mapcache.c @@ -18,10 +18,16 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif #include +#include #include +#include "globals.h" #include "mapcache.h" +#include "preferences.h" #include "config.h" @@ -35,20 +41,56 @@ typedef struct _List { static List *queue_tail = NULL; static int queue_count = 0; -#define VIK_CONFIG_MAPCACHE_MAX_SIZE 50331648 /* 48 meggerbytes */ static guint32 queue_size = 0; +static guint32 max_queue_size = VIK_CONFIG_MAPCACHE_SIZE * 1024 * 1024; static GHashTable *cache = NULL; +static GMutex *mc_mutex = NULL; + #define HASHKEY_FORMAT_STRING "%d-%d-%d-%d-%d-%d-%.3f-%.3f" #define HASHKEY_FORMAT_STRING_NOSHRINK_NOR_ALPHA "%d-%d-%d-%d-%d-" +static VikLayerParamScale params_scales[] = { + /* min, max, step, digits (decimal places) */ + { 1, 1024, 1, 0 }, +}; + +static VikLayerParam prefs[] = { + { VIK_LAYER_NUM_TYPES, VIKING_PREFERENCES_NAMESPACE "mapcache_size", VIK_LAYER_PARAM_UINT, VIK_LAYER_GROUP_NONE, N_("Map cache memory size (MB):"), VIK_LAYER_WIDGET_HSCALE, params_scales, NULL, NULL }, +}; + void a_mapcache_init () { + VikLayerParamData tmp; + tmp.u = VIK_CONFIG_MAPCACHE_SIZE; + a_preferences_register(prefs, tmp, VIKING_PREFERENCES_GROUP_KEY); + + mc_mutex = g_mutex_new(); cache = g_hash_table_new_full ( g_str_hash, g_str_equal, g_free, g_object_unref ); } +static void cache_add(gchar *key, GdkPixbuf *pixbuf) +{ + /* TODO: Check if already exists */ + g_hash_table_insert ( cache, key, pixbuf ); + queue_size += gdk_pixbuf_get_rowstride(pixbuf) * gdk_pixbuf_get_height(pixbuf); + queue_size += 100; + queue_count++; +} + +static void cache_remove(const gchar *key) +{ + GdkPixbuf *buf = g_hash_table_lookup ( cache, key ); + if (buf) { + queue_size -= gdk_pixbuf_get_rowstride(buf) * gdk_pixbuf_get_height(buf); + queue_size -= 100; + queue_count --; + g_hash_table_remove ( cache, key ); + } +} + /* returns key from head, adds on newtailkey to tail. */ static gchar *list_shift_add_entry ( gchar *newtailkey ) { @@ -82,38 +124,25 @@ static void list_add_entry ( gchar *key ) } } -void a_mapcache_add ( GdkPixbuf *pixbuf, gint x, gint y, gint z, guint8 type, guint zoom, guint8 alpha, gdouble xshrinkfactor, gdouble yshrinkfactor ) +void a_mapcache_add ( GdkPixbuf *pixbuf, gint x, gint y, gint z, guint16 type, guint zoom, guint8 alpha, gdouble xshrinkfactor, gdouble yshrinkfactor ) { gchar *key = g_strdup_printf ( HASHKEY_FORMAT_STRING, x, y, z, type, zoom, alpha, xshrinkfactor, yshrinkfactor ); static int tmp = 0; - /* FIXME: what if already exists? problem! */ - g_hash_table_insert ( cache, key, pixbuf ); + g_mutex_lock(mc_mutex); + cache_add(key, pixbuf); - queue_size += gdk_pixbuf_get_rowstride(pixbuf) * gdk_pixbuf_get_height(pixbuf); - queue_size += 100; - queue_count++; + // TODO: that should be done on preference change only... + max_queue_size = a_preferences_get(VIKING_PREFERENCES_NAMESPACE "mapcache_size")->u * 1024 * 1024; - if ( queue_size > VIK_CONFIG_MAPCACHE_SIZE ) { + if ( queue_size > max_queue_size ) { gchar *oldkey = list_shift_add_entry ( key ); - GdkPixbuf *oldbuf = g_hash_table_lookup ( cache, oldkey ); - queue_size -= gdk_pixbuf_get_rowstride(oldbuf) * gdk_pixbuf_get_height(oldbuf); - queue_size -= 100; - queue_count --; - g_hash_table_remove ( cache, oldkey ); - - /* should delete BEFORE adding -- not really possible because don't know size, - * but you could guess size (only applic if add big file to full cache) */ - + cache_remove(oldkey); - while ( queue_size > VIK_CONFIG_MAPCACHE_SIZE && + while ( queue_size > max_queue_size && (queue_tail->next != queue_tail) ) { /* make sure there's more than one thing to delete */ oldkey = list_shift (); - oldbuf = g_hash_table_lookup ( cache, oldkey ); - queue_size -= gdk_pixbuf_get_rowstride(oldbuf) * gdk_pixbuf_get_height(oldbuf); - queue_size -= 100; - queue_count --; - g_hash_table_remove ( cache, oldkey ); + cache_remove(oldkey); } /* chop off 'start' etc */ @@ -121,18 +150,19 @@ void a_mapcache_add ( GdkPixbuf *pixbuf, gint x, gint y, gint z, guint8 type, gu list_add_entry ( key ); /* business as usual */ } + g_mutex_unlock(mc_mutex); - if ( (++tmp == 100 )) { g_print("DEBUG: queue count=%d %ld\n", queue_count, queue_size ); tmp=0; } + if ( (++tmp == 100 )) { g_print("DEBUG: queue count=%d size=%u\n", queue_count, queue_size ); tmp=0; } } -GdkPixbuf *a_mapcache_get ( gint x, gint y, gint z, guint8 type, guint zoom, guint8 alpha, gdouble xshrinkfactor, gdouble yshrinkfactor ) +GdkPixbuf *a_mapcache_get ( gint x, gint y, gint z, guint16 type, guint zoom, guint8 alpha, gdouble xshrinkfactor, gdouble yshrinkfactor ) { static char key[48]; g_snprintf ( key, sizeof(key), HASHKEY_FORMAT_STRING, x, y, z, type, zoom, alpha, xshrinkfactor, yshrinkfactor ); return g_hash_table_lookup ( cache, key ); } -void a_mapcache_remove_all_shrinkfactors ( guint16 x, guint16 y, guint16 z, guint8 type, guint zoom ) +void a_mapcache_remove_all_shrinkfactors ( gint x, gint y, gint z, guint16 type, guint zoom ) { char key[40]; List *loop = queue_tail; @@ -145,16 +175,20 @@ void a_mapcache_remove_all_shrinkfactors ( guint16 x, guint16 y, guint16 z, guin g_snprintf ( key, sizeof(key), HASHKEY_FORMAT_STRING_NOSHRINK_NOR_ALPHA, x, y, z, type, zoom ); len = strlen(key); + g_mutex_lock(mc_mutex); /* TODO: check logic here */ do { tmp = loop->next; if ( strncmp(tmp->key, key, len) == 0 ) { - g_hash_table_remove ( cache, tmp->key ); - if ( tmp == loop ) + cache_remove(tmp->key); + if ( tmp == loop ) /* we deleted the last thing in the queue! */ loop = queue_tail = NULL; - else + else { loop->next = tmp->next; + if ( tmp == queue_tail ) + queue_tail = tmp->next; + } g_free ( tmp ); tmp = NULL; } @@ -164,8 +198,31 @@ void a_mapcache_remove_all_shrinkfactors ( guint16 x, guint16 y, guint16 z, guin } while ( loop && (loop != queue_tail || tmp == NULL) ); /* loop thru list, looking for the one, compare first whatever chars */ + cache_remove(key); + g_mutex_unlock(mc_mutex); +} + +void a_mapcache_flush () +{ + List *loop = queue_tail; + List *tmp; + + if ( queue_tail == NULL ) + return; + + g_mutex_lock(mc_mutex); + do { + tmp = loop->next; + cache_remove(tmp->key); + if ( tmp == queue_tail ) /* we deleted the last thing in the queue */ + loop = queue_tail = NULL; + else + loop->next = tmp->next; + g_free ( tmp ); + tmp = NULL; + } while ( loop ); - g_hash_table_remove ( cache, key ); + g_mutex_unlock(mc_mutex); } void a_mapcache_uninit ()