[libshumate] map-layer: Use an internal memory cache



commit 8038fd871e49c34cae962c120799a1d837d23eb6
Author: James Westman <james jwestman net>
Date:   Tue Mar 23 22:58:44 2021 -0500

    map-layer: Use an internal memory cache
    
    Memory caching is now done by the ShumateMapLayer rather than as part
    of the map source chain. This is part of an ongoing simplification and
    eventual removal of map source chains.

 docs/reference/libshumate-sections.txt |  1 -
 shumate/shumate-map-layer.c            | 59 +++++++++++++++++++++++++++++++---
 shumate/shumate-map-source-factory.c   | 41 +----------------------
 shumate/shumate-map-source-factory.h   |  2 --
 4 files changed, 56 insertions(+), 47 deletions(-)
---
diff --git a/docs/reference/libshumate-sections.txt b/docs/reference/libshumate-sections.txt
index a548abd..39b5805 100644
--- a/docs/reference/libshumate-sections.txt
+++ b/docs/reference/libshumate-sections.txt
@@ -387,7 +387,6 @@ ShumateMapSourceFactory
 shumate_map_source_factory_dup_default
 shumate_map_source_factory_create
 shumate_map_source_factory_create_cached_source
-shumate_map_source_factory_create_memcached_source
 shumate_map_source_factory_create_error_source
 shumate_map_source_factory_register
 shumate_map_source_factory_get_registered
diff --git a/shumate/shumate-map-layer.c b/shumate/shumate-map-layer.c
index ee53807..13fb7bf 100644
--- a/shumate/shumate-map-layer.c
+++ b/shumate/shumate-map-layer.c
@@ -19,6 +19,7 @@
 
 #include "shumate-map-layer.h"
 #include "shumate-view.h"
+#include "shumate-memory-cache.h"
 
 struct _ShumateMapLayer
 {
@@ -32,6 +33,8 @@ struct _ShumateMapLayer
   GHashTable *tile_fill;
 
   guint recompute_grid_idle_id;
+
+  ShumateMemoryCache *memcache;
 };
 
 G_DEFINE_TYPE (ShumateMapLayer, shumate_map_layer, SHUMATE_TYPE_LAYER)
@@ -422,6 +425,7 @@ shumate_map_layer_dispose (GObject *object)
   g_clear_pointer (&self->tile_fill, g_hash_table_unref);
   g_clear_pointer (&self->tiles_positions, g_ptr_array_unref);
   g_clear_object (&self->map_source);
+  g_clear_object (&self->memcache);
 
   G_OBJECT_CLASS (shumate_map_layer_parent_class)->dispose (object);
 }
@@ -441,6 +445,42 @@ shumate_map_layer_constructed (GObject *object)
 
 }
 
+
+typedef struct {
+  ShumateMapLayer *self;
+  ShumateTile *tile;
+  char *source_id;
+} TileFilledData;
+
+static void
+tile_filled_data_free (TileFilledData *data)
+{
+  g_clear_object (&data->self);
+  g_clear_object (&data->tile);
+  g_clear_pointer (&data->source_id, g_free);
+  g_free (data);
+}
+G_DEFINE_AUTOPTR_CLEANUP_FUNC (TileFilledData, tile_filled_data_free);
+
+static void
+on_tile_filled (GObject *source_object, GAsyncResult *res, gpointer user_data)
+{
+  g_autoptr(TileFilledData) data = user_data;
+  g_autoptr(GError) error = NULL;
+  gboolean success;
+
+  success = shumate_map_source_fill_tile_finish (SHUMATE_MAP_SOURCE (source_object), res, &error);
+
+  // TODO: Report the error
+  if (!success)
+    return;
+
+  shumate_memory_cache_store_texture (data->self->memcache,
+                                      data->tile,
+                                      shumate_tile_get_texture (data->tile),
+                                      data->source_id);
+}
+
 static void
 shumate_map_layer_size_allocate (GtkWidget *widget,
                                  int        width,
@@ -511,6 +551,8 @@ shumate_map_layer_size_allocate (GtkWidget *widget,
               shumate_tile_get_state (child) == SHUMATE_STATE_NONE)
             {
               GCancellable *cancellable = g_hash_table_lookup (self->tile_fill, child);
+              const char *source_id = shumate_map_source_get_id (self->map_source);
+
               if (cancellable)
                 g_cancellable_cancel (cancellable);
 
@@ -518,10 +560,18 @@ shumate_map_layer_size_allocate (GtkWidget *widget,
               shumate_tile_set_x (child, tile_column % source_columns);
               shumate_tile_set_y (child, tile_row % source_rows);
 
-              cancellable = g_cancellable_new ();
-              shumate_tile_set_texture (child, NULL);
-              shumate_map_source_fill_tile (self->map_source, child, cancellable);
-              g_hash_table_insert (self->tile_fill, g_object_ref (child), cancellable);
+              if (!shumate_memory_cache_try_fill_tile (self->memcache, tile_child->tile, source_id))
+                {
+                  TileFilledData *data = g_new0 (TileFilledData, 1);
+                  data->self = g_object_ref (self);
+                  data->tile = g_object_ref (tile_child->tile);
+                  data->source_id = g_strdup (source_id);
+
+                  cancellable = g_cancellable_new ();
+                  shumate_tile_set_texture (child, NULL);
+                  shumate_map_source_fill_tile_async (self->map_source, child, cancellable, on_tile_filled, 
data);
+                  g_hash_table_insert (self->tile_fill, g_object_ref (child), cancellable);
+                }
             }
 
           child_allocation.x += tile_size;
@@ -603,6 +653,7 @@ shumate_map_layer_init (ShumateMapLayer *self)
 {
   self->tiles_positions = g_ptr_array_new_with_free_func ((GDestroyNotify) tile_grid_position_free);
   self->tile_fill = g_hash_table_new_full (g_direct_hash, g_direct_equal, g_object_unref, g_object_unref);
+  self->memcache = shumate_memory_cache_new_full (100);
 }
 
 ShumateMapLayer *
diff --git a/shumate/shumate-map-source-factory.c b/shumate/shumate-map-source-factory.c
index 41084eb..86f68c8 100644
--- a/shumate/shumate-map-source-factory.c
+++ b/shumate/shumate-map-source-factory.c
@@ -298,7 +298,7 @@ shumate_map_source_factory_create (ShumateMapSourceFactory *factory,
  * Creates a cached map source.
  *
  * Returns: (transfer none): a ready to use #ShumateMapSourceChain consisting of
- * #ShumateMemoryCache, #ShumateMapSource matching the given name, and
+ * #ShumateMapSource matching the given name and
  * an error tile source created with shumate_map_source_factory_create_error_source ().
  * Returns NULL if the source with the given name doesn't exist.
  */
@@ -309,7 +309,6 @@ shumate_map_source_factory_create_cached_source (ShumateMapSourceFactory *factor
   ShumateMapSourceChain *source_chain;
   ShumateMapSource *tile_source;
   ShumateMapSource *error_source;
-  ShumateMapSource *memory_cache;
   guint tile_size;
 
   g_return_val_if_fail (SHUMATE_IS_MAP_SOURCE_FACTORY (factory), NULL);
@@ -321,47 +320,9 @@ shumate_map_source_factory_create_cached_source (ShumateMapSourceFactory *factor
   tile_size = shumate_map_source_get_tile_size (tile_source);
   error_source = shumate_map_source_factory_create_error_source (factory, tile_size);
 
-  memory_cache = SHUMATE_MAP_SOURCE (shumate_memory_cache_new_full (100));
-
   source_chain = shumate_map_source_chain_new ();
   shumate_map_source_chain_push (source_chain, error_source);
   shumate_map_source_chain_push (source_chain, tile_source);
-  shumate_map_source_chain_push (source_chain, memory_cache);
-
-  return SHUMATE_MAP_SOURCE (source_chain);
-}
-
-
-/**
- * shumate_map_source_factory_create_memcached_source:
- * @factory: the Factory
- * @id: the wanted map source id
- *
- * Creates a memory cached map source.
- *
- * Returns: (transfer none): a ready to use #ShumateMapSourceChain consisting of
- * #ShumateMemoryCache and #ShumateMapSource matching the given name.
- * Returns NULL if the source with the given name doesn't exist.
- */
-ShumateMapSource *
-shumate_map_source_factory_create_memcached_source (ShumateMapSourceFactory *factory,
-    const char *id)
-{
-  ShumateMapSourceChain *source_chain;
-  ShumateMapSource *tile_source;
-  ShumateMapSource *memory_cache;
-
-  g_return_val_if_fail (SHUMATE_IS_MAP_SOURCE_FACTORY (factory), NULL);
-
-  tile_source = shumate_map_source_factory_create (factory, id);
-  if (!tile_source)
-    return NULL;
-
-  memory_cache = SHUMATE_MAP_SOURCE (shumate_memory_cache_new_full (100));
-
-  source_chain = shumate_map_source_chain_new ();
-  shumate_map_source_chain_push (source_chain, tile_source);
-  shumate_map_source_chain_push (source_chain, memory_cache);
 
   return SHUMATE_MAP_SOURCE (source_chain);
 }
diff --git a/shumate/shumate-map-source-factory.h b/shumate/shumate-map-source-factory.h
index 0cc6938..eb4ca15 100644
--- a/shumate/shumate-map-source-factory.h
+++ b/shumate/shumate-map-source-factory.h
@@ -48,8 +48,6 @@ ShumateMapSource *shumate_map_source_factory_create (ShumateMapSourceFactory *fa
     const char *id);
 ShumateMapSource *shumate_map_source_factory_create_cached_source (ShumateMapSourceFactory *factory,
     const char *id);
-ShumateMapSource *shumate_map_source_factory_create_memcached_source (ShumateMapSourceFactory *factory,
-    const char *id);
 ShumateMapSource *shumate_map_source_factory_create_error_source (ShumateMapSourceFactory *factory,
     guint tile_size);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]