]>
Commit | Line | Data |
---|---|---|
50a14534 EB |
1 | /* |
2 | * viking -- GPS Data and Topo Analyzer, Explorer, and Manager | |
3 | * | |
4 | * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net> | |
5 | * | |
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. | |
10 | * | |
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. | |
15 | * | |
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 | |
19 | * | |
20 | */ | |
065a9ace GB |
21 | #ifdef HAVE_CONFIG_H |
22 | #include "config.h" | |
23 | #endif | |
50a14534 | 24 | |
e8518759 | 25 | #include <glib.h> |
9de4d421 | 26 | #include <glib/gi18n.h> |
50a14534 | 27 | #include <string.h> |
44b37676 | 28 | #include "globals.h" |
50a14534 | 29 | #include "mapcache.h" |
9de4d421 | 30 | #include "preferences.h" |
fc6640a9 | 31 | #include "vik_compat.h" |
50a14534 | 32 | |
9095ea4c RN |
33 | #define MC_KEY_SIZE 64 |
34 | ||
50a14534 EB |
35 | typedef struct _List { |
36 | struct _List *next; | |
37 | gchar *key; | |
38 | } List; | |
39 | ||
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; | |
44 | ||
e8518759 RN |
45 | static guint32 cache_size = 0; |
46 | static guint32 max_cache_size = VIK_CONFIG_MAPCACHE_SIZE * 1024 * 1024; | |
50a14534 EB |
47 | |
48 | static GHashTable *cache = NULL; | |
49 | ||
19c3c24c RN |
50 | typedef struct { |
51 | GdkPixbuf *pixbuf; | |
52 | mapcache_extra_t extra; | |
53 | } cache_item_t; | |
54 | ||
9b79169d QT |
55 | static GMutex *mc_mutex = NULL; |
56 | ||
df5b2993 | 57 | #define HASHKEY_FORMAT_STRING "%d-%d-%d-%d-%d-%d-%d-%.3f-%.3f" |
57c1c96a | 58 | #define HASHKEY_FORMAT_STRING_NOSHRINK_NOR_ALPHA "%d-%d-%d-%d-%d-%d-" |
9095ea4c | 59 | #define HASHKEY_FORMAT_STRING_TYPE "%d-" |
50a14534 | 60 | |
9de4d421 JJ |
61 | static VikLayerParamScale params_scales[] = { |
62 | /* min, max, step, digits (decimal places) */ | |
e6e277a8 | 63 | { 1, 1024, 1, 0 }, |
9de4d421 JJ |
64 | }; |
65 | ||
66 | static VikLayerParam prefs[] = { | |
63959706 | 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 }, |
9de4d421 JJ |
68 | }; |
69 | ||
19c3c24c RN |
70 | static void cache_item_free (cache_item_t *ci) |
71 | { | |
72 | g_object_unref ( ci->pixbuf ); | |
73 | g_free ( ci ); | |
74 | } | |
75 | ||
50a14534 EB |
76 | void a_mapcache_init () |
77 | { | |
9de4d421 | 78 | VikLayerParamData tmp; |
e6e277a8 | 79 | tmp.u = VIK_CONFIG_MAPCACHE_SIZE; |
9de4d421 JJ |
80 | a_preferences_register(prefs, tmp, VIKING_PREFERENCES_GROUP_KEY); |
81 | ||
fc6640a9 | 82 | mc_mutex = vik_mutex_new (); |
19c3c24c | 83 | cache = g_hash_table_new_full ( g_str_hash, g_str_equal, g_free, (GDestroyNotify) cache_item_free ); |
50a14534 EB |
84 | } |
85 | ||
19c3c24c | 86 | static void cache_add(gchar *key, GdkPixbuf *pixbuf, mapcache_extra_t extra) |
fd4536cf | 87 | { |
19c3c24c RN |
88 | cache_item_t *ci = g_malloc ( sizeof(cache_item_t) ); |
89 | ci->pixbuf = pixbuf; | |
90 | ci->extra = extra; | |
02a5b4d7 RN |
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 :( | |
19c3c24c | 94 | if ( g_hash_table_insert ( cache, key, ci ) ) |
02a5b4d7 | 95 | #else |
19c3c24c | 96 | g_hash_table_insert ( cache, key, ci ); |
02a5b4d7 RN |
97 | #endif |
98 | { | |
e8518759 | 99 | cache_size += gdk_pixbuf_get_rowstride(pixbuf) * gdk_pixbuf_get_height(pixbuf); |
19c3c24c RN |
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 | |
e8518759 RN |
102 | cache_size += 100; |
103 | } | |
fd4536cf QT |
104 | } |
105 | ||
106 | static void cache_remove(const gchar *key) | |
107 | { | |
19c3c24c RN |
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); | |
e8518759 RN |
111 | cache_size -= 100; |
112 | g_hash_table_remove ( cache, key ); | |
113 | } | |
fd4536cf QT |
114 | } |
115 | ||
50a14534 EB |
116 | /* returns key from head, adds on newtailkey to tail. */ |
117 | static gchar *list_shift_add_entry ( gchar *newtailkey ) | |
118 | { | |
119 | gchar *oldheadkey = queue_tail->next->key; | |
120 | queue_tail->next->key = newtailkey; | |
121 | queue_tail = queue_tail->next; | |
122 | return oldheadkey; | |
123 | } | |
124 | ||
125 | static gchar *list_shift () | |
126 | { | |
127 | gchar *oldheadkey = queue_tail->next->key; | |
128 | List *oldhead = queue_tail->next; | |
129 | queue_tail->next = queue_tail->next->next; | |
130 | g_free ( oldhead ); | |
e8518759 | 131 | queue_count--; |
50a14534 EB |
132 | return oldheadkey; |
133 | } | |
134 | ||
135 | /* adds key to tail */ | |
136 | static void list_add_entry ( gchar *key ) | |
137 | { | |
138 | List *newlist = g_malloc ( sizeof ( List ) ); | |
139 | newlist->key = key; | |
140 | if ( queue_tail ) { | |
141 | newlist->next = queue_tail->next; | |
142 | queue_tail->next = newlist; | |
143 | queue_tail = newlist; | |
144 | } else { | |
145 | newlist->next = newlist; | |
146 | queue_tail = newlist; | |
147 | } | |
e8518759 | 148 | queue_count++; |
50a14534 EB |
149 | } |
150 | ||
19c3c24c | 151 | 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 ) |
50a14534 | 152 | { |
4cf4c9ea RN |
153 | if ( ! GDK_IS_PIXBUF(pixbuf) ) { |
154 | g_debug ( "Not caching corrupt pixbuf for maptype %d at %d %d %d %d", type, x, y, z, zoom ); | |
155 | return; | |
156 | } | |
157 | ||
df5b2993 | 158 | guint nn = name ? g_str_hash ( name ) : 0; |
9095ea4c | 159 | gchar *key = g_strdup_printf ( HASHKEY_FORMAT_STRING, type, x, y, z, zoom, nn, alpha, xshrinkfactor, yshrinkfactor ); |
50a14534 | 160 | |
9b79169d | 161 | g_mutex_lock(mc_mutex); |
19c3c24c | 162 | cache_add(key, pixbuf, extra); |
50a14534 | 163 | |
9de4d421 | 164 | // TODO: that should be done on preference change only... |
e8518759 | 165 | max_cache_size = a_preferences_get(VIKING_PREFERENCES_NAMESPACE "mapcache_size")->u * 1024 * 1024; |
9de4d421 | 166 | |
e8518759 | 167 | if ( cache_size > max_cache_size ) { |
df05b6e9 RN |
168 | if ( queue_tail ) { |
169 | gchar *oldkey = list_shift_add_entry ( key ); | |
fd4536cf | 170 | cache_remove(oldkey); |
50a14534 | 171 | |
e8518759 | 172 | while ( cache_size > max_cache_size && |
df05b6e9 RN |
173 | (queue_tail->next != queue_tail) ) { /* make sure there's more than one thing to delete */ |
174 | oldkey = list_shift (); | |
175 | cache_remove(oldkey); | |
176 | } | |
177 | } | |
50a14534 EB |
178 | /* chop off 'start' etc */ |
179 | } else { | |
180 | list_add_entry ( key ); | |
181 | /* business as usual */ | |
182 | } | |
9b79169d | 183 | g_mutex_unlock(mc_mutex); |
50a14534 | 184 | |
a3040a49 RN |
185 | static int tmp = 0; |
186 | 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; } | |
50a14534 EB |
187 | } |
188 | ||
b7464e99 | 189 | GdkPixbuf *a_mapcache_get ( gint x, gint y, gint z, guint16 type, gint zoom, guint8 alpha, gdouble xshrinkfactor, gdouble yshrinkfactor, const gchar* name ) |
50a14534 | 190 | { |
9095ea4c | 191 | static char key[MC_KEY_SIZE]; |
df5b2993 | 192 | guint nn = name ? g_str_hash ( name ) : 0; |
9095ea4c | 193 | g_snprintf ( key, sizeof(key), HASHKEY_FORMAT_STRING, type, x, y, z, zoom, nn, alpha, xshrinkfactor, yshrinkfactor ); |
19c3c24c RN |
194 | cache_item_t *ci = g_hash_table_lookup ( cache, key ); |
195 | if ( ci ) | |
196 | return ci->pixbuf; | |
197 | else | |
198 | return NULL; | |
199 | } | |
200 | ||
201 | 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 ) | |
202 | { | |
203 | static char key[MC_KEY_SIZE]; | |
204 | guint nn = name ? g_str_hash ( name ) : 0; | |
205 | g_snprintf ( key, sizeof(key), HASHKEY_FORMAT_STRING, type, x, y, z, zoom, nn, alpha, xshrinkfactor, yshrinkfactor ); | |
206 | cache_item_t *ci = g_hash_table_lookup ( cache, key ); | |
207 | if ( ci ) | |
208 | return ci->extra; | |
209 | else | |
210 | return (mapcache_extra_t) { 0.0 }; | |
50a14534 EB |
211 | } |
212 | ||
df5b2993 | 213 | /** |
9095ea4c | 214 | * Common function to remove cache items for keys starting with the specified string |
df5b2993 | 215 | */ |
9095ea4c | 216 | static void flush_matching ( gchar *str ) |
50a14534 | 217 | { |
50a14534 EB |
218 | if ( queue_tail == NULL ) |
219 | return; | |
220 | ||
9095ea4c RN |
221 | List *loop = queue_tail; |
222 | List *tmp; | |
223 | gint len = strlen(str); | |
50a14534 | 224 | |
9b79169d | 225 | g_mutex_lock(mc_mutex); |
50a14534 EB |
226 | do { |
227 | tmp = loop->next; | |
2ae6f542 | 228 | if ( tmp ) { |
9095ea4c | 229 | if ( strncmp(tmp->key, str, len) == 0 ) |
50a14534 | 230 | { |
fd4536cf | 231 | cache_remove(tmp->key); |
093c5c71 | 232 | if ( tmp == loop ) /* we deleted the last thing in the queue! */ |
50a14534 | 233 | loop = queue_tail = NULL; |
093c5c71 | 234 | else { |
50a14534 | 235 | loop->next = tmp->next; |
093c5c71 EB |
236 | if ( tmp == queue_tail ) |
237 | queue_tail = tmp->next; | |
238 | } | |
50a14534 EB |
239 | g_free ( tmp ); |
240 | tmp = NULL; | |
e8518759 | 241 | queue_count--; |
50a14534 EB |
242 | } |
243 | else | |
244 | loop = tmp; | |
2ae6f542 RN |
245 | } else |
246 | loop = NULL; | |
50a14534 | 247 | } while ( loop && (loop != queue_tail || tmp == NULL) ); |
50a14534 | 248 | /* loop thru list, looking for the one, compare first whatever chars */ |
9095ea4c RN |
249 | |
250 | cache_remove(str); | |
9b79169d | 251 | g_mutex_unlock(mc_mutex); |
50a14534 EB |
252 | } |
253 | ||
9095ea4c RN |
254 | /** |
255 | * Appears this is only used when redownloading tiles (i.e. to invalidate old images) | |
256 | */ | |
353996de | 257 | void a_mapcache_remove_all_shrinkfactors ( gint x, gint y, gint z, guint16 type, gint zoom, const gchar* name ) |
9095ea4c RN |
258 | { |
259 | char key[MC_KEY_SIZE]; | |
353996de RN |
260 | guint nn = name ? g_str_hash ( name ) : 0; |
261 | g_snprintf ( key, sizeof(key), HASHKEY_FORMAT_STRING_NOSHRINK_NOR_ALPHA, type, x, y, z, zoom, nn ); | |
9095ea4c RN |
262 | flush_matching ( key ); |
263 | } | |
264 | ||
c57a0943 JJ |
265 | void a_mapcache_flush () |
266 | { | |
267 | List *loop = queue_tail; | |
268 | List *tmp; | |
269 | ||
270 | if ( queue_tail == NULL ) | |
271 | return; | |
272 | ||
273 | g_mutex_lock(mc_mutex); | |
274 | do { | |
275 | tmp = loop->next; | |
276 | cache_remove(tmp->key); | |
277 | if ( tmp == queue_tail ) /* we deleted the last thing in the queue */ | |
278 | loop = queue_tail = NULL; | |
279 | else | |
280 | loop->next = tmp->next; | |
281 | g_free ( tmp ); | |
282 | tmp = NULL; | |
283 | } while ( loop ); | |
284 | ||
285 | g_mutex_unlock(mc_mutex); | |
286 | } | |
287 | ||
9095ea4c RN |
288 | /** |
289 | * a_mapcache_flush_type: | |
290 | * @type: Specified map type | |
291 | * | |
292 | * Just remove cache items for the specified map type | |
293 | * i.e. all related xyz+zoom+alpha+etc... | |
294 | */ | |
295 | void a_mapcache_flush_type ( guint16 type ) | |
296 | { | |
297 | char key[MC_KEY_SIZE]; | |
298 | g_snprintf ( key, sizeof(key), HASHKEY_FORMAT_STRING_TYPE, type ); | |
299 | flush_matching ( key ); | |
300 | } | |
301 | ||
50a14534 EB |
302 | void a_mapcache_uninit () |
303 | { | |
304 | g_hash_table_destroy ( cache ); | |
305 | /* free list */ | |
306 | cache = NULL; | |
fc6640a9 | 307 | vik_mutex_free (mc_mutex); |
50a14534 | 308 | } |
a3040a49 RN |
309 | |
310 | // Size of mapcache in memory | |
311 | gint a_mapcache_get_size () | |
312 | { | |
313 | return cache_size; | |
314 | } | |
315 | ||
316 | // Count of items in the mapcache | |
317 | gint a_mapcache_get_count () | |
318 | { | |
319 | return g_hash_table_size ( cache ); | |
320 | } |