]> git.street.me.uk Git - andy/viking.git/blobdiff - src/mapcache.c
Merge branch 'GeoRefImprove'
[andy/viking.git] / src / mapcache.c
index b6a2c2b3ff025ceddb4723a37621066a2bd7c56a..449caf8f9566c2e8409d64003d2af3731188a3eb 100644 (file)
 #include "config.h"
 #endif
 
-#include <gtk/gtk.h>
+#include <glib.h>
 #include <glib/gi18n.h>
 #include <string.h>
 #include "globals.h"
 #include "mapcache.h"
 #include "preferences.h"
 
-#include "config.h"
-
 typedef struct _List {
   struct _List *next;
   gchar *key;
@@ -41,16 +39,15 @@ typedef struct _List {
 static List *queue_tail = NULL;
 static int queue_count = 0;
 
-static guint32 queue_size = 0;
-static guint32 max_queue_size = VIK_CONFIG_MAPCACHE_SIZE * 1024 * 1024;
-
+static guint32 cache_size = 0;
+static guint32 max_cache_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-"
+#define HASHKEY_FORMAT_STRING "%d-%d-%d-%d-%d-%d-%d-%.3f-%.3f"
+#define HASHKEY_FORMAT_STRING_NOSHRINK_NOR_ALPHA "%d-%d-%d-%d-%d-%d-"
 
 static VikLayerParamScale params_scales[] = {
   /* min, max, step, digits (decimal places) */
@@ -58,7 +55,7 @@ static VikLayerParamScale params_scales[] = {
 };
 
 static VikLayerParam prefs[] = {
-  { 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 },
+  { 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, NULL, NULL, NULL },
 };
 
 void a_mapcache_init ()
@@ -73,22 +70,27 @@ void a_mapcache_init ()
 
 static void cache_add(gchar *key, GdkPixbuf *pixbuf)
 {
-  /* TODO: Check if already exists */
+#if !GLIB_CHECK_VERSION(2,26,0)
+  // Only later versions of GLib actually return a value for this function
+  // Annoyingly the documentation doesn't say anything about this interface change :(
+  if ( g_hash_table_insert ( cache, key, pixbuf ) )
+#else
   g_hash_table_insert ( cache, key, pixbuf );
-  queue_size += gdk_pixbuf_get_rowstride(pixbuf) * gdk_pixbuf_get_height(pixbuf);
-  queue_size += 100;
-  queue_count++;
+#endif
+  {
+    cache_size += gdk_pixbuf_get_rowstride(pixbuf) * gdk_pixbuf_get_height(pixbuf);
+    cache_size += 100;
+  }
 }
 
 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 );
-    }
+  GdkPixbuf *buf = g_hash_table_lookup ( cache, key );
+  if (buf) {
+    cache_size -= gdk_pixbuf_get_rowstride(buf) * gdk_pixbuf_get_height(buf);
+    cache_size -= 100;
+    g_hash_table_remove ( cache, key );
+  }
 }
 
 /* returns key from head, adds on newtailkey to tail. */
@@ -106,6 +108,7 @@ static gchar *list_shift ()
   List *oldhead = queue_tail->next;
   queue_tail->next = queue_tail->next->next;
   g_free ( oldhead );
+  queue_count--;
   return oldheadkey;
 }
 
@@ -122,29 +125,31 @@ static void list_add_entry ( gchar *key )
     newlist->next = newlist;
     queue_tail = newlist;
   }
+  queue_count++;
 }
 
-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, gint zoom, guint8 alpha, gdouble xshrinkfactor, gdouble yshrinkfactor, const gchar* name )
 {
-  gchar *key = g_strdup_printf ( HASHKEY_FORMAT_STRING, x, y, z, type, zoom, alpha, xshrinkfactor, yshrinkfactor );
-  static int tmp = 0;
+  guint nn = name ? g_str_hash ( name ) : 0;
+  gchar *key = g_strdup_printf ( HASHKEY_FORMAT_STRING, x, y, z, type, zoom, nn, alpha, xshrinkfactor, yshrinkfactor );
 
   g_mutex_lock(mc_mutex);
   cache_add(key, pixbuf);
 
   // 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 > max_queue_size ) {
-    gchar *oldkey = list_shift_add_entry ( key );
-    cache_remove(oldkey);
+  max_cache_size = a_preferences_get(VIKING_PREFERENCES_NAMESPACE "mapcache_size")->u * 1024 * 1024;
 
-    while ( queue_size > max_queue_size &&
-        (queue_tail->next != queue_tail) ) { /* make sure there's more than one thing to delete */
-      oldkey = list_shift ();
+  if ( cache_size > max_cache_size ) {
+    if ( queue_tail ) {
+      gchar *oldkey = list_shift_add_entry ( key );
       cache_remove(oldkey);
-    }
 
+      while ( cache_size > max_cache_size &&
+             (queue_tail->next != queue_tail) ) { /* make sure there's more than one thing to delete */
+        oldkey = list_shift ();
+        cache_remove(oldkey);
+      }
+    }
     /* chop off 'start' etc */
   } else {
     list_add_entry ( key );
@@ -152,19 +157,24 @@ void a_mapcache_add ( GdkPixbuf *pixbuf, gint x, gint y, gint z, guint8 type, gu
   }
   g_mutex_unlock(mc_mutex);
 
-  if ( (++tmp == 100 ))  { g_print("DEBUG: queue count=%d size=%u\n", queue_count, queue_size ); tmp=0; }
+  static int tmp = 0;
+  if ( (++tmp == 100 )) { g_debug("DEBUG: cache count=%d size=%u list count=%d\n", g_hash_table_size(cache), cache_size, queue_count ); 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, gint zoom, guint8 alpha, gdouble xshrinkfactor, gdouble yshrinkfactor, const gchar* name )
 {
-  static char key[48];
-  g_snprintf ( key, sizeof(key), HASHKEY_FORMAT_STRING, x, y, z, type, zoom, alpha, xshrinkfactor, yshrinkfactor );
+  static char key[64];
+  guint nn = name ? g_str_hash ( name ) : 0;
+  g_snprintf ( key, sizeof(key), HASHKEY_FORMAT_STRING, x, y, z, type, zoom, nn, alpha, xshrinkfactor, yshrinkfactor );
   return g_hash_table_lookup ( cache, key );
 }
 
-void a_mapcache_remove_all_shrinkfactors ( gint x, gint y, gint z, guint8 type, guint zoom )
+/**
+ * Appears this is only used when redownloading tiles (i.e. to invalidate old images)
+ */
+void a_mapcache_remove_all_shrinkfactors ( gint x, gint y, gint z, guint16 type, gint zoom )
 {
-  char key[40];
+  char key[64];
   List *loop = queue_tail;
   List *tmp;
   gint len;
@@ -172,13 +182,14 @@ void a_mapcache_remove_all_shrinkfactors ( gint x, gint y, gint z, guint8 type,
   if ( queue_tail == NULL )
     return;
 
-  g_snprintf ( key, sizeof(key), HASHKEY_FORMAT_STRING_NOSHRINK_NOR_ALPHA, x, y, z, type, zoom );
+  g_snprintf ( key, sizeof(key), HASHKEY_FORMAT_STRING_NOSHRINK_NOR_ALPHA, x, y, z, type, zoom, 0 );
   len = strlen(key);
 
   g_mutex_lock(mc_mutex);
   /* TODO: check logic here */
   do {
     tmp = loop->next;
+    if ( tmp ) {
     if ( strncmp(tmp->key, key, len) == 0 )
     {
       cache_remove(tmp->key);
@@ -191,10 +202,12 @@ void a_mapcache_remove_all_shrinkfactors ( gint x, gint y, gint z, guint8 type,
       }
       g_free ( tmp );
       tmp = NULL;
+      queue_count--;
     }
     else
       loop = tmp;
-
+    } else
+      loop = NULL;
   } while ( loop && (loop != queue_tail || tmp == NULL) );
 
   /* loop thru list, looking for the one, compare first whatever chars */
@@ -232,4 +245,14 @@ void a_mapcache_uninit ()
   cache = NULL;
 }
 
+// Size of mapcache in memory
+gint a_mapcache_get_size ()
+{
+  return cache_size;
+}
 
+// Count of items in the mapcache
+gint a_mapcache_get_count ()
+{
+  return g_hash_table_size ( cache );
+}