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