2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <glib/gi18n.h>
30 #include "preferences.h"
31 #include "vik_compat.h"
33 #define MC_KEY_SIZE 64
35 typedef struct _List {
40 /* a circular linked list, a pointer to the tail, and the tail points to the head */
41 /* this is so we can free the last */
42 static List *queue_tail = NULL;
43 static int queue_count = 0;
45 static guint32 cache_size = 0;
46 static guint32 max_cache_size = VIK_CONFIG_MAPCACHE_SIZE * 1024 * 1024;
48 static GHashTable *cache = NULL;
52 mapcache_extra_t extra;
55 static GMutex *mc_mutex = NULL;
57 #define HASHKEY_FORMAT_STRING "%d-%d-%d-%d-%d-%d-%d-%.3f-%.3f"
58 #define HASHKEY_FORMAT_STRING_NOSHRINK_NOR_ALPHA "%d-%d-%d-%d-%d-%d-"
59 #define HASHKEY_FORMAT_STRING_TYPE "%d-"
61 static VikLayerParamScale params_scales[] = {
62 /* min, max, step, digits (decimal places) */
66 static VikLayerParam prefs[] = {
67 { 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 },
70 static void cache_item_free (cache_item_t *ci)
72 g_object_unref ( ci->pixbuf );
76 void a_mapcache_init ()
78 VikLayerParamData tmp;
79 tmp.u = VIK_CONFIG_MAPCACHE_SIZE;
80 a_preferences_register(prefs, tmp, VIKING_PREFERENCES_GROUP_KEY);
82 mc_mutex = vik_mutex_new ();
83 cache = g_hash_table_new_full ( g_str_hash, g_str_equal, g_free, (GDestroyNotify) cache_item_free );
86 static void cache_add(gchar *key, GdkPixbuf *pixbuf, mapcache_extra_t extra)
88 cache_item_t *ci = g_malloc ( sizeof(cache_item_t) );
91 #if !GLIB_CHECK_VERSION(2,26,0)
92 // Only later versions of GLib actually return a value for this function
93 // Annoyingly the documentation doesn't say anything about this interface change :(
94 if ( g_hash_table_insert ( cache, key, ci ) )
96 g_hash_table_insert ( cache, key, ci );
99 cache_size += gdk_pixbuf_get_rowstride(pixbuf) * gdk_pixbuf_get_height(pixbuf);
100 // ATM size of 'extra' data hardly worth trying to count (compared to pixbuf sizes)
101 // Not sure what this 100 represents anyway - probably a guess at an average pixbuf metadata size
106 static void cache_remove(const gchar *key)
108 cache_item_t *ci = g_hash_table_lookup ( cache, key );
109 if (ci && ci->pixbuf) {
110 cache_size -= gdk_pixbuf_get_rowstride(ci->pixbuf) * gdk_pixbuf_get_height(ci->pixbuf);
112 g_hash_table_remove ( cache, key );
116 /* returns key from head, adds on newtailkey to tail. */
117 static gchar *list_shift_add_entry ( gchar *newtailkey )
119 gchar *oldheadkey = queue_tail->next->key;
120 queue_tail->next->key = newtailkey;
121 queue_tail = queue_tail->next;
125 static gchar *list_shift ()
127 gchar *oldheadkey = queue_tail->next->key;
128 List *oldhead = queue_tail->next;
129 queue_tail->next = queue_tail->next->next;
135 /* adds key to tail */
136 static void list_add_entry ( gchar *key )
138 List *newlist = g_malloc ( sizeof ( List ) );
141 newlist->next = queue_tail->next;
142 queue_tail->next = newlist;
143 queue_tail = newlist;
145 newlist->next = newlist;
146 queue_tail = newlist;
152 * Function increments reference counter of pixbuf.
153 * Caller may (and should) decrease it's reference.
155 void a_mapcache_add ( GdkPixbuf *pixbuf, mapcache_extra_t extra, gint x, gint y, gint z, guint16 type, gint zoom, guint8 alpha, gdouble xshrinkfactor, gdouble yshrinkfactor, const gchar* name )
157 if ( ! GDK_IS_PIXBUF(pixbuf) ) {
158 g_debug ( "Not caching corrupt pixbuf for maptype %d at %d %d %d %d", type, x, y, z, zoom );
162 guint nn = name ? g_str_hash ( name ) : 0;
163 gchar *key = g_strdup_printf ( HASHKEY_FORMAT_STRING, type, x, y, z, zoom, nn, alpha, xshrinkfactor, yshrinkfactor );
165 g_mutex_lock(mc_mutex);
166 g_object_ref(pixbuf);
167 cache_add(key, pixbuf, extra);
169 // TODO: that should be done on preference change only...
170 max_cache_size = a_preferences_get(VIKING_PREFERENCES_NAMESPACE "mapcache_size")->u * 1024 * 1024;
172 if ( cache_size > max_cache_size ) {
174 gchar *oldkey = list_shift_add_entry ( key );
175 cache_remove(oldkey);
177 while ( cache_size > max_cache_size &&
178 (queue_tail->next != queue_tail) ) { /* make sure there's more than one thing to delete */
179 oldkey = list_shift ();
180 cache_remove(oldkey);
183 /* chop off 'start' etc */
185 list_add_entry ( key );
186 /* business as usual */
188 g_mutex_unlock(mc_mutex);
191 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; }
195 * Function increases reference counter of pixels buffer in behalf of caller.
196 * Caller have to decrease references counter, when buffer is no longer needed.
198 GdkPixbuf *a_mapcache_get ( gint x, gint y, gint z, guint16 type, gint zoom, guint8 alpha, gdouble xshrinkfactor, gdouble yshrinkfactor, const gchar* name )
200 static char key[MC_KEY_SIZE];
201 guint nn = name ? g_str_hash ( name ) : 0;
202 g_snprintf ( key, sizeof(key), HASHKEY_FORMAT_STRING, type, x, y, z, zoom, nn, alpha, xshrinkfactor, yshrinkfactor );
203 g_mutex_lock(mc_mutex); /* prevent returning pixbuf when cache is being cleared */
204 cache_item_t *ci = g_hash_table_lookup ( cache, key );
206 g_object_ref(ci->pixbuf);
207 g_mutex_unlock(mc_mutex);
210 g_mutex_unlock(mc_mutex);
215 mapcache_extra_t a_mapcache_get_extra ( gint x, gint y, gint z, guint16 type, gint zoom, guint8 alpha, gdouble xshrinkfactor, gdouble yshrinkfactor, const gchar* name )
217 static char key[MC_KEY_SIZE];
218 guint nn = name ? g_str_hash ( name ) : 0;
219 g_snprintf ( key, sizeof(key), HASHKEY_FORMAT_STRING, type, x, y, z, zoom, nn, alpha, xshrinkfactor, yshrinkfactor );
220 cache_item_t *ci = g_hash_table_lookup ( cache, key );
224 return (mapcache_extra_t) { 0.0 };
228 * Common function to remove cache items for keys starting with the specified string
230 static void flush_matching ( gchar *str )
232 g_mutex_lock(mc_mutex);
234 if ( queue_tail == NULL ) {
235 g_mutex_unlock(mc_mutex);
239 // The 'loop' variable must be assigned within the mutex lock section,
240 // otherwise where it points to might not be valid anymore when the actual processing occurs
241 List *loop = queue_tail;
243 gint len = strlen(str);
248 if ( strncmp(tmp->key, str, len) == 0 )
250 cache_remove(tmp->key);
251 if ( tmp == loop ) /* we deleted the last thing in the queue! */
252 loop = queue_tail = NULL;
254 loop->next = tmp->next;
255 if ( tmp == queue_tail )
256 queue_tail = tmp->next;
266 } while ( loop && (loop != queue_tail || tmp == NULL) );
267 /* loop thru list, looking for the one, compare first whatever chars */
270 g_mutex_unlock(mc_mutex);
274 * Appears this is only used when redownloading tiles (i.e. to invalidate old images)
276 void a_mapcache_remove_all_shrinkfactors ( gint x, gint y, gint z, guint16 type, gint zoom, const gchar* name )
278 char key[MC_KEY_SIZE];
279 guint nn = name ? g_str_hash ( name ) : 0;
280 g_snprintf ( key, sizeof(key), HASHKEY_FORMAT_STRING_NOSHRINK_NOR_ALPHA, type, x, y, z, zoom, nn );
281 flush_matching ( key );
284 void a_mapcache_flush ()
286 // Everything happens within the mutex lock section
287 g_mutex_lock(mc_mutex);
289 List *loop = queue_tail;
294 cache_remove(tmp->key);
295 if ( tmp == queue_tail ) /* we deleted the last thing in the queue */
296 loop = queue_tail = NULL;
298 loop->next = tmp->next;
303 g_mutex_unlock(mc_mutex);
307 * a_mapcache_flush_type:
308 * @type: Specified map type
310 * Just remove cache items for the specified map type
311 * i.e. all related xyz+zoom+alpha+etc...
313 void a_mapcache_flush_type ( guint16 type )
315 char key[MC_KEY_SIZE];
316 g_snprintf ( key, sizeof(key), HASHKEY_FORMAT_STRING_TYPE, type );
317 flush_matching ( key );
320 void a_mapcache_uninit ()
322 g_hash_table_destroy ( cache );
325 vik_mutex_free (mc_mutex);
328 // Size of mapcache in memory
329 gint a_mapcache_get_size ()
334 // Count of items in the mapcache
335 gint a_mapcache_get_count ()
337 return g_hash_table_size ( cache );