From 19c3c24c666a117f840902c8c3604335f02c82ac Mon Sep 17 00:00:00 2001 From: Rob Norris Date: Fri, 9 Jan 2015 22:55:54 +0000 Subject: [PATCH] Extend map cache data to hold extra data associated with the pixbuf. ATM just a value for Mapnik rendering time, but should be easily extendable. --- src/mapcache.c | 52 +++++++++++++++++++++++++++++++++++--------- src/mapcache.h | 7 +++++- src/vikmapniklayer.c | 7 +++--- src/vikmapslayer.c | 2 +- 4 files changed, 53 insertions(+), 15 deletions(-) diff --git a/src/mapcache.c b/src/mapcache.c index 3e5b077c..3fe7a06e 100644 --- a/src/mapcache.c +++ b/src/mapcache.c @@ -47,6 +47,11 @@ static guint32 max_cache_size = VIK_CONFIG_MAPCACHE_SIZE * 1024 * 1024; static GHashTable *cache = NULL; +typedef struct { + GdkPixbuf *pixbuf; + mapcache_extra_t extra; +} cache_item_t; + static GMutex *mc_mutex = NULL; #define HASHKEY_FORMAT_STRING "%d-%d-%d-%d-%d-%d-%d-%.3f-%.3f" @@ -62,6 +67,12 @@ static VikLayerParam prefs[] = { { 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 }, }; +static void cache_item_free (cache_item_t *ci) +{ + g_object_unref ( ci->pixbuf ); + g_free ( ci ); +} + void a_mapcache_init () { VikLayerParamData tmp; @@ -69,29 +80,34 @@ void a_mapcache_init () a_preferences_register(prefs, tmp, VIKING_PREFERENCES_GROUP_KEY); mc_mutex = vik_mutex_new (); - cache = g_hash_table_new_full ( g_str_hash, g_str_equal, g_free, g_object_unref ); + cache = g_hash_table_new_full ( g_str_hash, g_str_equal, g_free, (GDestroyNotify) cache_item_free ); } -static void cache_add(gchar *key, GdkPixbuf *pixbuf) +static void cache_add(gchar *key, GdkPixbuf *pixbuf, mapcache_extra_t extra) { + cache_item_t *ci = g_malloc ( sizeof(cache_item_t) ); + ci->pixbuf = pixbuf; + ci->extra = extra; #if !GLIB_CHECK_VERSION(2,26,0) // Only later versions of GLib actually return a value for this function // Annoyingly the documentation doesn't say anything about this interface change :( - if ( g_hash_table_insert ( cache, key, pixbuf ) ) + if ( g_hash_table_insert ( cache, key, ci ) ) #else - g_hash_table_insert ( cache, key, pixbuf ); + g_hash_table_insert ( cache, key, ci ); #endif { cache_size += gdk_pixbuf_get_rowstride(pixbuf) * gdk_pixbuf_get_height(pixbuf); + // ATM size of 'extra' data hardly worth trying to count (compared to pixbuf sizes) + // Not sure what this 100 represents anyway - probably a guess at an average pixbuf metadata size cache_size += 100; } } static void cache_remove(const gchar *key) { - GdkPixbuf *buf = g_hash_table_lookup ( cache, key ); - if (buf) { - cache_size -= gdk_pixbuf_get_rowstride(buf) * gdk_pixbuf_get_height(buf); + cache_item_t *ci = g_hash_table_lookup ( cache, key ); + if (ci && ci->pixbuf) { + cache_size -= gdk_pixbuf_get_rowstride(ci->pixbuf) * gdk_pixbuf_get_height(ci->pixbuf); cache_size -= 100; g_hash_table_remove ( cache, key ); } @@ -132,13 +148,13 @@ static void list_add_entry ( gchar *key ) queue_count++; } -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 ) +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 ) { guint nn = name ? g_str_hash ( name ) : 0; gchar *key = g_strdup_printf ( HASHKEY_FORMAT_STRING, type, x, y, z, zoom, nn, alpha, xshrinkfactor, yshrinkfactor ); g_mutex_lock(mc_mutex); - cache_add(key, pixbuf); + cache_add(key, pixbuf, extra); // TODO: that should be done on preference change only... max_cache_size = a_preferences_get(VIKING_PREFERENCES_NAMESPACE "mapcache_size")->u * 1024 * 1024; @@ -170,7 +186,23 @@ GdkPixbuf *a_mapcache_get ( gint x, gint y, gint z, guint16 type, gint zoom, gui static char key[MC_KEY_SIZE]; guint nn = name ? g_str_hash ( name ) : 0; g_snprintf ( key, sizeof(key), HASHKEY_FORMAT_STRING, type, x, y, z, zoom, nn, alpha, xshrinkfactor, yshrinkfactor ); - return g_hash_table_lookup ( cache, key ); + cache_item_t *ci = g_hash_table_lookup ( cache, key ); + if ( ci ) + return ci->pixbuf; + else + return NULL; +} + +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 ) +{ + static char key[MC_KEY_SIZE]; + guint nn = name ? g_str_hash ( name ) : 0; + g_snprintf ( key, sizeof(key), HASHKEY_FORMAT_STRING, type, x, y, z, zoom, nn, alpha, xshrinkfactor, yshrinkfactor ); + cache_item_t *ci = g_hash_table_lookup ( cache, key ); + if ( ci ) + return ci->extra; + else + return (mapcache_extra_t) { 0.0 }; } /** diff --git a/src/mapcache.h b/src/mapcache.h index 25b46f78..d481cdd7 100644 --- a/src/mapcache.h +++ b/src/mapcache.h @@ -27,9 +27,14 @@ G_BEGIN_DECLS +typedef struct { + gdouble duration; // Mostly for Mapnik Rendering duration - negative values indicate not rendered (i.e. read from disk) +} mapcache_extra_t; + void a_mapcache_init (); -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 ); +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 ); GdkPixbuf *a_mapcache_get ( gint x, gint y, gint z, guint16 type, gint zoom, guint8 alpha, gdouble xshrinkfactor, gdouble yshrinkfactor, const gchar *name ); +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 ); void a_mapcache_remove_all_shrinkfactors ( gint x, gint y, gint z, guint16 type, gint zoom ); void a_mapcache_flush (); void a_mapcache_flush_type ( guint16 type ); diff --git a/src/vikmapniklayer.c b/src/vikmapniklayer.c index 06f2b70b..652466bd 100644 --- a/src/vikmapniklayer.c +++ b/src/vikmapniklayer.c @@ -647,7 +647,8 @@ static void render ( VikMapnikLayer *vml, VikCoord *ul, VikCoord *br, MapCoord * gint64 tt1 = g_get_real_time (); GdkPixbuf *pixbuf = mapnik_interface_render ( vml->mi, ul->north_south, ul->east_west, br->north_south, br->east_west ); gint64 tt2 = g_get_real_time (); - g_debug ( "Mapnik rendering completed in %.3f seconds", (gdouble)(tt2-tt1)/1000000 ); + gdouble tt = (gdouble)(tt2-tt1)/1000000; + g_debug ( "Mapnik rendering completed in %.3f seconds", tt ); if ( !pixbuf ) { // A pixbuf to stick into cache incase of an unrenderable area - otherwise will get continually re-requested pixbuf = gdk_pixbuf_scale_simple ( gdk_pixbuf_from_pixdata(&vikmapniklayer_pixbuf, FALSE, NULL), vml->tile_size_x, vml->tile_size_x, GDK_INTERP_BILINEAR ); @@ -657,7 +658,7 @@ static void render ( VikMapnikLayer *vml, VikCoord *ul, VikCoord *br, MapCoord * // NB Mapnik can apply alpha, but use our own function for now if ( vml->alpha < 255 ) pixbuf = ui_pixbuf_set_alpha ( pixbuf, vml->alpha ); - a_mapcache_add ( pixbuf, ulm->x, ulm->y, ulm->z, MAP_ID_MAPNIK_RENDER, ulm->scale, vml->alpha, 0.0, 0.0, vml->filename_xml ); + a_mapcache_add ( pixbuf, (mapcache_extra_t){ tt }, ulm->x, ulm->y, ulm->z, MAP_ID_MAPNIK_RENDER, ulm->scale, vml->alpha, 0.0, 0.0, vml->filename_xml ); } static void render_info_free ( RenderInfo *data ) @@ -757,7 +758,7 @@ static GdkPixbuf *load_pixbuf ( VikMapnikLayer *vml, MapCoord *ulm, MapCoord *br else { if ( vml->alpha < 255 ) pixbuf = ui_pixbuf_set_alpha ( pixbuf, vml->alpha ); - a_mapcache_add ( pixbuf, ulm->x, ulm->y, ulm->z, MAP_ID_MAPNIK_RENDER, ulm->scale, vml->alpha, 0.0, 0.0, vml->filename_xml ); + a_mapcache_add ( pixbuf, (mapcache_extra_t) { -42.0 }, ulm->x, ulm->y, ulm->z, MAP_ID_MAPNIK_RENDER, ulm->scale, vml->alpha, 0.0, 0.0, vml->filename_xml ); } // If file is too old mark for rerendering if ( planet_import_time < gsb.st_mtime ) { diff --git a/src/vikmapslayer.c b/src/vikmapslayer.c index 2e79bb43..8568f2ee 100644 --- a/src/vikmapslayer.c +++ b/src/vikmapslayer.c @@ -1046,7 +1046,7 @@ static GdkPixbuf *pixbuf_apply_settings ( GdkPixbuf *pixbuf, VikMapsLayer *vml, pixbuf = pixbuf_shrink ( pixbuf, xshrinkfactor, yshrinkfactor ); if ( pixbuf ) - a_mapcache_add ( pixbuf, mapcoord->x, mapcoord->y, + a_mapcache_add ( pixbuf, (mapcache_extra_t) {0.0}, mapcoord->x, mapcoord->y, mapcoord->z, vik_map_source_get_uniq_id(MAPS_LAYER_NTH_TYPE(vml->maptype)), mapcoord->scale, vml->alpha, xshrinkfactor, yshrinkfactor, vml->filename ); -- 2.39.5