]> err.no Git - mapper/commitdiff
Use generic image cache system for map tiles.
authorKaj-Michael Lang <milang@tal.org>
Fri, 11 Apr 2008 10:24:10 +0000 (13:24 +0300)
committerKaj-Michael Lang <milang@tal.org>
Fri, 11 Apr 2008 10:24:10 +0000 (13:24 +0300)
src/map.c

index 106c6a58a0e3f7e18f0770298fbec8d1f6a6b45c..c1f4a65e896cd457c85334b22ef660b48535a5cb 100644 (file)
--- a/src/map.c
+++ b/src/map.c
@@ -61,6 +61,7 @@
 #include "announcements.h"
 #include "gtkcompass.h"
 #include "dialogs.h"
+#include "image-cache.h"
 
 #define DEBUG_MAP_TIME 1
 
@@ -91,6 +92,10 @@ cairo_t *ct_pixmap;
 /** The backing pixmap of map_widget. */
 static GdkPixmap *map_pixmap;
 
+/* Tile cache, this might need some adjustment */
+#define MAP_CACHE_MAX (64)
+static ImageCache *map_ic;
+
 /** The "base tile" is the upper-left tile in the pixmap. */
 guint _base_tilex = -5;
 guint _base_tiley = -5;
@@ -166,6 +171,7 @@ GtkWidget *map_widget;
 map_widget=gtk_drawing_area_new();
 map_timer=g_timer_new();
 
+map_ic=image_cache_new(MAP_CACHE_MAX);
 g_debug("World size: %u", WORLD_SIZE_UNITS);
 
 #ifdef WITH_GL
@@ -319,7 +325,7 @@ mark_height = abs(mark_y1 - mark_y2) + (4 * _draw_width);
  * this method, but I guess it's not general-purpose enough.
  */
 static void
-map_pixbuf_scale_inplace(GdkPixbuf * pixbuf, guint ratio_p2, guint src_x, guint src_y)
+map_pixbuf_scale_inplace(GdkPixbuf *pixbuf, guint ratio_p2, guint src_x, guint src_y)
 {
 guint dest_x = 0, dest_y = 0, dest_dim = TILE_SIZE_PIXELS;
 guint rowstride = gdk_pixbuf_get_rowstride(pixbuf);
@@ -379,6 +385,7 @@ pixbuf_trim(GdkPixbuf * pixbuf)
 GdkPixbuf *mpixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, gdk_pixbuf_get_has_alpha(pixbuf),
                   8, TILE_SIZE_PIXELS, TILE_SIZE_PIXELS);
 
+g_debug("TRIM!!");
 gdk_pixbuf_copy_area(pixbuf,
                     (gdk_pixbuf_get_width(pixbuf) - TILE_SIZE_PIXELS) / 2,
                     (gdk_pixbuf_get_height(pixbuf) - TILE_SIZE_PIXELS) / 2, 
@@ -395,30 +402,38 @@ map_pixmap_get(void)
 return map_pixmap;
 }
 
-
 static GdkPixbuf *
 map_tile_load(guint tilex, guint tiley, gint zoff, gboolean download)
 {
 GdkPixbuf *pixbuf;
 GError *error = NULL;
 gchar buffer[BUFFER_SIZE];
+gchar key[BUFFER_SIZE];
 struct stat tstat;
 gint se;
 
 g_snprintf(buffer, sizeof(buffer), "%s/%u/%u/%u.jpg", _curr_repo->cache_dir, _zoom + zoff, (tilex >> zoff), (tiley >> zoff));
+g_snprintf(key, sizeof(key), "%s/%u/%u/%u", _curr_repo->cache_dir, _zoom + zoff, (tilex >> zoff), (tiley >> zoff));
 
-pixbuf=gdk_pixbuf_new_from_file(buffer, &error);
-if (error || !pixbuf) {
+pixbuf=image_cache_get(map_ic, key, buffer);
+if (!pixbuf) {
        g_unlink(buffer);
 
        if (_auto_download && _curr_repo->type != REPOTYPE_NONE && !((_zoom + zoff - (_curr_repo->double_size ? 1 : 0)) % _curr_repo->dl_zoom_steps)) {
                if (download)
                        map_initiate_download(tilex >> zoff, tiley >> zoff, _zoom + zoff, -INITIAL_DOWNLOAD_RETRIES);
        }
-
        return NULL;
 }
 
+g_object_ref(pixbuf);
+
+#if 0
+/* Check if we need to trim. */
+if (gdk_pixbuf_get_width(pixbuf) != TILE_SIZE_PIXELS || gdk_pixbuf_get_height(pixbuf) != TILE_SIZE_PIXELS)
+       pixbuf=pixbuf_trim(pixbuf);
+#endif
+
 /* Check tile age, if file date is ower a week old, redownload if autodownload enabled */
 se=stat(buffer, &tstat);
 if (se==0) {
@@ -429,6 +444,7 @@ if (se==0) {
                        if (download) {
                                g_debug("Tile: %s is old, re-downloading\n", buffer);
                                map_initiate_download(tilex >> zoff, tiley >> zoff, _zoom + zoff, -INITIAL_DOWNLOAD_RETRIES);
+                               image_cache_invalidate(map_ic, key);
                        }
                }
        }
@@ -458,15 +474,12 @@ for (zoff = (_curr_repo->double_size ? 1 : 0); !pixbuf && (_zoom + zoff) <= map_
                if (!fast_fail)
                        fast_fail=TRUE;
        } else {
-               /* Check if we need to trim. */
-               if (gdk_pixbuf_get_width(pixbuf) != TILE_SIZE_PIXELS || gdk_pixbuf_get_height(pixbuf) != TILE_SIZE_PIXELS)
-                       pixbuf = pixbuf_trim(pixbuf);
-
                /* Check if we need to blit. */
                if (zoff) {
                        map_pixbuf_scale_inplace(pixbuf, zoff,
-                                        (tilex - ((tilex >> zoff) << zoff)) << (TILE_SIZE_P2 - zoff),
-                                        (tiley - ((tiley >> zoff) << zoff)) << (TILE_SIZE_P2 - zoff));
+                                       (tilex - ((tilex >> zoff) << zoff)) << (TILE_SIZE_P2 - zoff),
+                                       (tiley - ((tiley >> zoff) << zoff)) << (TILE_SIZE_P2 - zoff));
+                       image_cache_invalidate_by_image(map_ic, pixbuf);
                }
        }
 }
@@ -1144,6 +1157,7 @@ gint nzoom;
 
 nzoom=_zoom+zdir;
 if ((nzoom >= 0) && (nzoom < map_max_zoom - 1)) {
+       image_cache_clear(map_ic);
        map_set_zoom(nzoom);
 }
 return nzoom;