]>
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 | |
50a14534 EB |
33 | typedef struct _List { |
34 | struct _List *next; | |
35 | gchar *key; | |
36 | } List; | |
37 | ||
38 | /* a circular linked list, a pointer to the tail, and the tail points to the head */ | |
39 | /* this is so we can free the last */ | |
40 | static List *queue_tail = NULL; | |
41 | static int queue_count = 0; | |
42 | ||
e8518759 RN |
43 | static guint32 cache_size = 0; |
44 | static guint32 max_cache_size = VIK_CONFIG_MAPCACHE_SIZE * 1024 * 1024; | |
50a14534 EB |
45 | |
46 | static GHashTable *cache = NULL; | |
47 | ||
9b79169d QT |
48 | static GMutex *mc_mutex = NULL; |
49 | ||
df5b2993 | 50 | #define HASHKEY_FORMAT_STRING "%d-%d-%d-%d-%d-%d-%d-%.3f-%.3f" |
57c1c96a | 51 | #define HASHKEY_FORMAT_STRING_NOSHRINK_NOR_ALPHA "%d-%d-%d-%d-%d-%d-" |
50a14534 | 52 | |
9de4d421 JJ |
53 | static VikLayerParamScale params_scales[] = { |
54 | /* min, max, step, digits (decimal places) */ | |
e6e277a8 | 55 | { 1, 1024, 1, 0 }, |
9de4d421 JJ |
56 | }; |
57 | ||
58 | static VikLayerParam prefs[] = { | |
63959706 | 59 | { 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 |
60 | }; |
61 | ||
50a14534 EB |
62 | void a_mapcache_init () |
63 | { | |
9de4d421 | 64 | VikLayerParamData tmp; |
e6e277a8 | 65 | tmp.u = VIK_CONFIG_MAPCACHE_SIZE; |
9de4d421 JJ |
66 | a_preferences_register(prefs, tmp, VIKING_PREFERENCES_GROUP_KEY); |
67 | ||
fc6640a9 | 68 | mc_mutex = vik_mutex_new (); |
50a14534 EB |
69 | cache = g_hash_table_new_full ( g_str_hash, g_str_equal, g_free, g_object_unref ); |
70 | } | |
71 | ||
fd4536cf QT |
72 | static void cache_add(gchar *key, GdkPixbuf *pixbuf) |
73 | { | |
02a5b4d7 RN |
74 | #if !GLIB_CHECK_VERSION(2,26,0) |
75 | // Only later versions of GLib actually return a value for this function | |
76 | // Annoyingly the documentation doesn't say anything about this interface change :( | |
77 | if ( g_hash_table_insert ( cache, key, pixbuf ) ) | |
78 | #else | |
79 | g_hash_table_insert ( cache, key, pixbuf ); | |
80 | #endif | |
81 | { | |
e8518759 RN |
82 | cache_size += gdk_pixbuf_get_rowstride(pixbuf) * gdk_pixbuf_get_height(pixbuf); |
83 | cache_size += 100; | |
84 | } | |
fd4536cf QT |
85 | } |
86 | ||
87 | static void cache_remove(const gchar *key) | |
88 | { | |
e8518759 RN |
89 | GdkPixbuf *buf = g_hash_table_lookup ( cache, key ); |
90 | if (buf) { | |
91 | cache_size -= gdk_pixbuf_get_rowstride(buf) * gdk_pixbuf_get_height(buf); | |
92 | cache_size -= 100; | |
93 | g_hash_table_remove ( cache, key ); | |
94 | } | |
fd4536cf QT |
95 | } |
96 | ||
50a14534 EB |
97 | /* returns key from head, adds on newtailkey to tail. */ |
98 | static gchar *list_shift_add_entry ( gchar *newtailkey ) | |
99 | { | |
100 | gchar *oldheadkey = queue_tail->next->key; | |
101 | queue_tail->next->key = newtailkey; | |
102 | queue_tail = queue_tail->next; | |
103 | return oldheadkey; | |
104 | } | |
105 | ||
106 | static gchar *list_shift () | |
107 | { | |
108 | gchar *oldheadkey = queue_tail->next->key; | |
109 | List *oldhead = queue_tail->next; | |
110 | queue_tail->next = queue_tail->next->next; | |
111 | g_free ( oldhead ); | |
e8518759 | 112 | queue_count--; |
50a14534 EB |
113 | return oldheadkey; |
114 | } | |
115 | ||
116 | /* adds key to tail */ | |
117 | static void list_add_entry ( gchar *key ) | |
118 | { | |
119 | List *newlist = g_malloc ( sizeof ( List ) ); | |
120 | newlist->key = key; | |
121 | if ( queue_tail ) { | |
122 | newlist->next = queue_tail->next; | |
123 | queue_tail->next = newlist; | |
124 | queue_tail = newlist; | |
125 | } else { | |
126 | newlist->next = newlist; | |
127 | queue_tail = newlist; | |
128 | } | |
e8518759 | 129 | queue_count++; |
50a14534 EB |
130 | } |
131 | ||
b7464e99 | 132 | 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 ) |
50a14534 | 133 | { |
df5b2993 RN |
134 | guint nn = name ? g_str_hash ( name ) : 0; |
135 | gchar *key = g_strdup_printf ( HASHKEY_FORMAT_STRING, x, y, z, type, zoom, nn, alpha, xshrinkfactor, yshrinkfactor ); | |
50a14534 | 136 | |
9b79169d | 137 | g_mutex_lock(mc_mutex); |
fd4536cf | 138 | cache_add(key, pixbuf); |
50a14534 | 139 | |
9de4d421 | 140 | // TODO: that should be done on preference change only... |
e8518759 | 141 | max_cache_size = a_preferences_get(VIKING_PREFERENCES_NAMESPACE "mapcache_size")->u * 1024 * 1024; |
9de4d421 | 142 | |
e8518759 | 143 | if ( cache_size > max_cache_size ) { |
df05b6e9 RN |
144 | if ( queue_tail ) { |
145 | gchar *oldkey = list_shift_add_entry ( key ); | |
fd4536cf | 146 | cache_remove(oldkey); |
50a14534 | 147 | |
e8518759 | 148 | while ( cache_size > max_cache_size && |
df05b6e9 RN |
149 | (queue_tail->next != queue_tail) ) { /* make sure there's more than one thing to delete */ |
150 | oldkey = list_shift (); | |
151 | cache_remove(oldkey); | |
152 | } | |
153 | } | |
50a14534 EB |
154 | /* chop off 'start' etc */ |
155 | } else { | |
156 | list_add_entry ( key ); | |
157 | /* business as usual */ | |
158 | } | |
9b79169d | 159 | g_mutex_unlock(mc_mutex); |
50a14534 | 160 | |
a3040a49 RN |
161 | static int tmp = 0; |
162 | 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 |
163 | } |
164 | ||
b7464e99 | 165 | GdkPixbuf *a_mapcache_get ( gint x, gint y, gint z, guint16 type, gint zoom, guint8 alpha, gdouble xshrinkfactor, gdouble yshrinkfactor, const gchar* name ) |
50a14534 | 166 | { |
df5b2993 RN |
167 | static char key[64]; |
168 | guint nn = name ? g_str_hash ( name ) : 0; | |
169 | g_snprintf ( key, sizeof(key), HASHKEY_FORMAT_STRING, x, y, z, type, zoom, nn, alpha, xshrinkfactor, yshrinkfactor ); | |
50a14534 EB |
170 | return g_hash_table_lookup ( cache, key ); |
171 | } | |
172 | ||
df5b2993 RN |
173 | /** |
174 | * Appears this is only used when redownloading tiles (i.e. to invalidate old images) | |
175 | */ | |
b7464e99 | 176 | void a_mapcache_remove_all_shrinkfactors ( gint x, gint y, gint z, guint16 type, gint zoom ) |
50a14534 | 177 | { |
df5b2993 | 178 | char key[64]; |
50a14534 EB |
179 | List *loop = queue_tail; |
180 | List *tmp; | |
181 | gint len; | |
182 | ||
183 | if ( queue_tail == NULL ) | |
184 | return; | |
185 | ||
57c1c96a | 186 | g_snprintf ( key, sizeof(key), HASHKEY_FORMAT_STRING_NOSHRINK_NOR_ALPHA, x, y, z, type, zoom, 0 ); |
50a14534 EB |
187 | len = strlen(key); |
188 | ||
9b79169d | 189 | g_mutex_lock(mc_mutex); |
50a14534 EB |
190 | /* TODO: check logic here */ |
191 | do { | |
192 | tmp = loop->next; | |
2ae6f542 | 193 | if ( tmp ) { |
50a14534 EB |
194 | if ( strncmp(tmp->key, key, len) == 0 ) |
195 | { | |
fd4536cf | 196 | cache_remove(tmp->key); |
093c5c71 | 197 | if ( tmp == loop ) /* we deleted the last thing in the queue! */ |
50a14534 | 198 | loop = queue_tail = NULL; |
093c5c71 | 199 | else { |
50a14534 | 200 | loop->next = tmp->next; |
093c5c71 EB |
201 | if ( tmp == queue_tail ) |
202 | queue_tail = tmp->next; | |
203 | } | |
50a14534 EB |
204 | g_free ( tmp ); |
205 | tmp = NULL; | |
e8518759 | 206 | queue_count--; |
50a14534 EB |
207 | } |
208 | else | |
209 | loop = tmp; | |
2ae6f542 RN |
210 | } else |
211 | loop = NULL; | |
50a14534 EB |
212 | } while ( loop && (loop != queue_tail || tmp == NULL) ); |
213 | ||
214 | /* loop thru list, looking for the one, compare first whatever chars */ | |
fd4536cf | 215 | cache_remove(key); |
9b79169d | 216 | g_mutex_unlock(mc_mutex); |
50a14534 EB |
217 | } |
218 | ||
c57a0943 JJ |
219 | void a_mapcache_flush () |
220 | { | |
221 | List *loop = queue_tail; | |
222 | List *tmp; | |
223 | ||
224 | if ( queue_tail == NULL ) | |
225 | return; | |
226 | ||
227 | g_mutex_lock(mc_mutex); | |
228 | do { | |
229 | tmp = loop->next; | |
230 | cache_remove(tmp->key); | |
231 | if ( tmp == queue_tail ) /* we deleted the last thing in the queue */ | |
232 | loop = queue_tail = NULL; | |
233 | else | |
234 | loop->next = tmp->next; | |
235 | g_free ( tmp ); | |
236 | tmp = NULL; | |
237 | } while ( loop ); | |
238 | ||
239 | g_mutex_unlock(mc_mutex); | |
240 | } | |
241 | ||
50a14534 EB |
242 | void a_mapcache_uninit () |
243 | { | |
244 | g_hash_table_destroy ( cache ); | |
245 | /* free list */ | |
246 | cache = NULL; | |
fc6640a9 | 247 | vik_mutex_free (mc_mutex); |
50a14534 | 248 | } |
a3040a49 RN |
249 | |
250 | // Size of mapcache in memory | |
251 | gint a_mapcache_get_size () | |
252 | { | |
253 | return cache_size; | |
254 | } | |
255 | ||
256 | // Count of items in the mapcache | |
257 | gint a_mapcache_get_count () | |
258 | { | |
259 | return g_hash_table_size ( cache ); | |
260 | } |