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