From ba63d06cfc09b016ce0ddfcaaa297bd259cadf09 Mon Sep 17 00:00:00 2001 From: Asheem Mamoowala Date: Tue, 31 Oct 2017 16:27:49 -0700 Subject: [core] Use Actors for CustomTileLoader invocation from bindings. --- include/mbgl/style/sources/custom_geometry_source.hpp | 6 +++--- src/mbgl/style/custom_tile_loader.cpp | 9 +-------- src/mbgl/style/custom_tile_loader.hpp | 2 -- src/mbgl/style/sources/custom_geometry_source.cpp | 14 +++++++------- test/style/source.test.cpp | 4 ++-- 5 files changed, 13 insertions(+), 22 deletions(-) diff --git a/include/mbgl/style/sources/custom_geometry_source.hpp b/include/mbgl/style/sources/custom_geometry_source.hpp index 853526ac2d..a0b990b44b 100644 --- a/include/mbgl/style/sources/custom_geometry_source.hpp +++ b/include/mbgl/style/sources/custom_geometry_source.hpp @@ -5,12 +5,13 @@ #include #include #include -#include namespace mbgl { class OverscaledTileID; class CanonicalTileID; +template +class Actor; namespace style { @@ -43,8 +44,7 @@ public: class Impl; const Impl& impl() const; private: - std::shared_ptr mailbox; - std::unique_ptr loader; + std::unique_ptr> loader; }; template <> diff --git a/src/mbgl/style/custom_tile_loader.cpp b/src/mbgl/style/custom_tile_loader.cpp index 2fc94c9d5a..d5bebf0086 100644 --- a/src/mbgl/style/custom_tile_loader.cpp +++ b/src/mbgl/style/custom_tile_loader.cpp @@ -9,7 +9,6 @@ CustomTileLoader::CustomTileLoader(const TileFunction& fetchTileFn, const TileFu } void CustomTileLoader::fetchTile(const OverscaledTileID& tileID, ActorRef callbackRef) { - std::lock_guard lock(dataCacheMutex); auto cachedTileData = dataCache.find(tileID.canonical); if (cachedTileData != dataCache.end()) { callbackRef.invoke(&SetTileDataFunction::operator(), *(cachedTileData->second)); @@ -49,14 +48,12 @@ void CustomTileLoader::removeTile(const OverscaledTileID& tileID) { } } if (tileCallbacks->second.size() == 0) { - std::lock_guard lock(dataCacheMutex); tileCallbackMap.erase(tileCallbacks); dataCache.erase(tileID.canonical); } } void CustomTileLoader::setTileData(const CanonicalTileID& tileID, const GeoJSON& data) { - std::lock_guard lock(dataCacheMutex); auto iter = tileCallbackMap.find(tileID); if (iter == tileCallbackMap.end()) return; @@ -76,10 +73,7 @@ void CustomTileLoader::invalidateTile(const CanonicalTileID& tileID) { invokeTileCancel(tileID); } tileCallbackMap.erase(tileCallbacks); - { - std::lock_guard lock(dataCacheMutex); - dataCache.erase(tileID); - } + dataCache.erase(tileID); } void CustomTileLoader::invalidateRegion(const LatLngBounds& bounds, Range ) { @@ -87,7 +81,6 @@ void CustomTileLoader::invalidateRegion(const LatLngBounds& bounds, Rangefirst); if (tileBounds.intersects(bounds) || bounds.contains(tileBounds) || tileBounds.contains(bounds)) { for (auto iter = idtuple->second.begin(); iter != idtuple->second.end(); iter++) { - std::lock_guard lock(dataCacheMutex); auto actor = std::get<2>(*iter); actor.invoke(&SetTileDataFunction::operator(), mapbox::geojson::feature_collection()); invokeTileCancel(idtuple->first); diff --git a/src/mbgl/style/custom_tile_loader.hpp b/src/mbgl/style/custom_tile_loader.hpp index fa3173ffc0..149da69cfa 100644 --- a/src/mbgl/style/custom_tile_loader.hpp +++ b/src/mbgl/style/custom_tile_loader.hpp @@ -7,7 +7,6 @@ #include #include -#include namespace mbgl { namespace style { @@ -39,7 +38,6 @@ private: std::unordered_map> tileCallbackMap; // Keep around a cache of tile data to serve back for wrapped and over-zooomed tiles std::map> dataCache; - std::mutex dataCacheMutex; }; diff --git a/src/mbgl/style/sources/custom_geometry_source.cpp b/src/mbgl/style/sources/custom_geometry_source.cpp index 657573b1f4..ab46843d38 100644 --- a/src/mbgl/style/sources/custom_geometry_source.cpp +++ b/src/mbgl/style/sources/custom_geometry_source.cpp @@ -1,9 +1,10 @@ #include #include #include +#include #include #include - +#include #include #include @@ -13,8 +14,7 @@ namespace style { CustomGeometrySource::CustomGeometrySource(std::string id, const CustomGeometrySource::Options options) : Source(makeMutable(std::move(id), options)), - mailbox(std::make_shared(*Scheduler::GetCurrent())), - loader(std::make_unique(options.fetchTileFunction, options.cancelTileFunction)) { + loader(std::make_unique>(*sharedThreadPool(), options.fetchTileFunction, options.cancelTileFunction)) { } CustomGeometrySource::~CustomGeometrySource() = default; @@ -24,21 +24,21 @@ const CustomGeometrySource::Impl& CustomGeometrySource::impl() const { } void CustomGeometrySource::loadDescription(FileSource&) { - baseImpl = makeMutable(impl(), ActorRef(*loader, mailbox)); + baseImpl = makeMutable(impl(), loader->self()); loaded = true; } void CustomGeometrySource::setTileData(const CanonicalTileID& tileID, const GeoJSON& data) { - loader->setTileData(tileID, data); + loader->invoke(&CustomTileLoader::setTileData, tileID, data); } void CustomGeometrySource::invalidateTile(const CanonicalTileID& tileID) { - loader->invalidateTile(tileID); + loader->invoke(&CustomTileLoader::invalidateTile, tileID); } void CustomGeometrySource::invalidateRegion(const LatLngBounds& bounds) { - loader->invalidateRegion(bounds, impl().getZoomRange()); + loader->invoke(&CustomTileLoader::invalidateRegion, bounds, impl().getZoomRange()); } } // namespace style diff --git a/test/style/source.test.cpp b/test/style/source.test.cpp index 6a2122161d..eb419e8080 100644 --- a/test/style/source.test.cpp +++ b/test/style/source.test.cpp @@ -25,7 +25,7 @@ #include #include -#include +#include #include #include #include @@ -552,7 +552,7 @@ TEST(Source, ImageSourceImageUpdate) { TEST(Source, CustomGeometrySourceSetTileData) { SourceTest test; - + std::shared_ptr threadPool = sharedThreadPool(); CustomGeometrySource source("source", CustomGeometrySource::Options()); source.loadDescription(test.fileSource); -- cgit v1.2.1