From ad03e42b2890fd430e99e543a40be42f09f47fba Mon Sep 17 00:00:00 2001 From: Mikhail Pozdnyakov Date: Wed, 28 Nov 2018 16:03:09 +0200 Subject: [core] LayerManager can disable annotations At the moment, the annotations implementation in the `mapbox-gl-native` core is creating concrete layer instances apart from `LayerManager/LayerFactory` code path. So, annotations must be disabled if the `LayerManager` implementation does not provide line, fill or symbol layers (those, used by the annotations). Note: in future, annotations implementation will be moved from the core to the platform SDK level(see https://github.com/mapbox/mapbox-plugins-android/tree/master/plugin-annotation) and `LayerManager` won't need to disable it. --- include/mbgl/style/layer.hpp | 22 +++++++++++ .../android/src/style/layers/layer_manager.cpp | 2 + platform/darwin/src/MGLStyleLayerManager.mm | 2 + platform/default/layer_manager.cpp | 2 + src/mbgl/annotation/annotation_manager.cpp | 23 +++++++++++- src/mbgl/annotation/annotation_manager.hpp | 2 - src/mbgl/map/map.cpp | 43 +++++++++++++++------- src/mbgl/renderer/renderer.cpp | 9 +++++ src/mbgl/renderer/renderer_impl.cpp | 6 ++- 9 files changed, 93 insertions(+), 18 deletions(-) diff --git a/include/mbgl/style/layer.hpp b/include/mbgl/style/layer.hpp index 9e73e994e6..d85f459815 100644 --- a/include/mbgl/style/layer.hpp +++ b/include/mbgl/style/layer.hpp @@ -152,6 +152,12 @@ protected: /** * @brief A singleton class responsible for creating layer instances. + * + * The LayerManager has implementation per platform. The LayerManager implementation + * defines what layer types are available and it can also disable annotations. + * + * Linker excludes the unreachable code for the disabled annotations and layers + * from the binaries, significantly reducing their size. */ class LayerManager { public: @@ -168,6 +174,22 @@ public: /// Returns a new RenderLayer instance on success call; returns `nulltptr` otherwise. std::unique_ptr createRenderLayer(Immutable) noexcept; + /** + * @brief a build-time flag to enable/disable annotations in mapbox-gl-native core. + * + * At the moment, the annotations implementation in core is creating concrete + * layer instances apart from LayerManager/LayerFactory code path. + * + * So, annotations must be disabled if the LayerManager implementation does + * not provide line, fill or symbol layers (those, used by the annotations + * implementation). + * + * Note: in future, annotations implemantation will be moved from the core to platform + * SDK (see https://github.com/mapbox/mapbox-plugins-android/tree/master/plugin-annotation) + * and this flag won't be needed any more. + */ + static const bool annotationsEnabled; + protected: virtual ~LayerManager() = default; virtual LayerFactory* getFactory(const std::string& type) noexcept = 0; diff --git a/platform/android/src/style/layers/layer_manager.cpp b/platform/android/src/style/layers/layer_manager.cpp index 9f13fa0640..706c5a76a1 100644 --- a/platform/android/src/style/layers/layer_manager.cpp +++ b/platform/android/src/style/layers/layer_manager.cpp @@ -100,4 +100,6 @@ LayerManager* LayerManager::get() noexcept { return android::LayerManagerAndroid::get(); } +const bool LayerManager::annotationsEnabled = true; + } // namespace mbgl diff --git a/platform/darwin/src/MGLStyleLayerManager.mm b/platform/darwin/src/MGLStyleLayerManager.mm index 90272851c3..bdcc303de5 100644 --- a/platform/darwin/src/MGLStyleLayerManager.mm +++ b/platform/darwin/src/MGLStyleLayerManager.mm @@ -78,4 +78,6 @@ LayerManager* LayerManager::get() noexcept { return LayerManagerDarwin::get(); } +const bool LayerManager::annotationsEnabled = true; + } // namespace mbgl diff --git a/platform/default/layer_manager.cpp b/platform/default/layer_manager.cpp index 60cd0855b8..32d5cbc0fc 100644 --- a/platform/default/layer_manager.cpp +++ b/platform/default/layer_manager.cpp @@ -73,4 +73,6 @@ LayerManager* LayerManager::get() noexcept { return &instance; } +const bool LayerManager::annotationsEnabled = true; + } // namespace mbgl diff --git a/src/mbgl/annotation/annotation_manager.cpp b/src/mbgl/annotation/annotation_manager.cpp index 1baf83179e..17cf77865c 100644 --- a/src/mbgl/annotation/annotation_manager.cpp +++ b/src/mbgl/annotation/annotation_manager.cpp @@ -13,6 +13,15 @@ #include +// Note: LayerManager::annotationsEnabled is defined +// at compile time, so that linker (with LTO on) is able +// to optimize out the unreachable code. +#define CHECK_ANNOTATIONS_ENABLED_AND_RETURN(result) \ +if (!LayerManager::annotationsEnabled) { \ + assert(false); \ + return result; \ +} + namespace mbgl { using namespace style; @@ -28,14 +37,17 @@ AnnotationManager::AnnotationManager(Style& style_) AnnotationManager::~AnnotationManager() = default; void AnnotationManager::setStyle(Style& style_) { + CHECK_ANNOTATIONS_ENABLED_AND_RETURN(); style = style_; } void AnnotationManager::onStyleLoaded() { + CHECK_ANNOTATIONS_ENABLED_AND_RETURN(); updateStyle(); } AnnotationID AnnotationManager::addAnnotation(const Annotation& annotation) { + CHECK_ANNOTATIONS_ENABLED_AND_RETURN(nextID++); std::lock_guard lock(mutex); AnnotationID id = nextID++; Annotation::visit(annotation, [&] (const auto& annotation_) { @@ -46,6 +58,7 @@ AnnotationID AnnotationManager::addAnnotation(const Annotation& annotation) { } bool AnnotationManager::updateAnnotation(const AnnotationID& id, const Annotation& annotation) { + CHECK_ANNOTATIONS_ENABLED_AND_RETURN(true); std::lock_guard lock(mutex); Annotation::visit(annotation, [&] (const auto& annotation_) { this->update(id, annotation_); @@ -54,6 +67,7 @@ bool AnnotationManager::updateAnnotation(const AnnotationID& id, const Annotatio } void AnnotationManager::removeAnnotation(const AnnotationID& id) { + CHECK_ANNOTATIONS_ENABLED_AND_RETURN(); std::lock_guard lock(mutex); remove(id); dirty = true; @@ -119,6 +133,7 @@ void AnnotationManager::update(const AnnotationID& id, const FillAnnotation& ann } void AnnotationManager::remove(const AnnotationID& id) { + CHECK_ANNOTATIONS_ENABLED_AND_RETURN(); if (symbolAnnotations.find(id) != symbolAnnotations.end()) { symbolTree.remove(symbolAnnotations.at(id)); symbolAnnotations.erase(id); @@ -194,6 +209,7 @@ void AnnotationManager::updateStyle() { } void AnnotationManager::updateData() { + CHECK_ANNOTATIONS_ENABLED_AND_RETURN(); std::lock_guard lock(mutex); if (dirty) { for (auto& tile : tiles) { @@ -204,12 +220,14 @@ void AnnotationManager::updateData() { } void AnnotationManager::addTile(AnnotationTile& tile) { + CHECK_ANNOTATIONS_ENABLED_AND_RETURN(); std::lock_guard lock(mutex); tiles.insert(&tile); tile.setData(getTileData(tile.id.canonical)); } void AnnotationManager::removeTile(AnnotationTile& tile) { + CHECK_ANNOTATIONS_ENABLED_AND_RETURN(); std::lock_guard lock(mutex); tiles.erase(&tile); } @@ -221,6 +239,7 @@ static std::string prefixedImageID(const std::string& id) { } void AnnotationManager::addImage(std::unique_ptr image) { + CHECK_ANNOTATIONS_ENABLED_AND_RETURN(); std::lock_guard lock(mutex); const std::string id = prefixedImageID(image->getID()); images.erase(id); @@ -230,6 +249,7 @@ void AnnotationManager::addImage(std::unique_ptr image) { } void AnnotationManager::removeImage(const std::string& id_) { + CHECK_ANNOTATIONS_ENABLED_AND_RETURN(); std::lock_guard lock(mutex); const std::string id = prefixedImageID(id_); images.erase(id); @@ -237,10 +257,11 @@ void AnnotationManager::removeImage(const std::string& id_) { } double AnnotationManager::getTopOffsetPixelsForImage(const std::string& id_) { + CHECK_ANNOTATIONS_ENABLED_AND_RETURN(0.0); std::lock_guard lock(mutex); const std::string id = prefixedImageID(id_); auto it = images.find(id); - return it != images.end() ? -(it->second.getImage().size.height / it->second.getPixelRatio()) / 2 : 0; + return it != images.end() ? -(it->second.getImage().size.height / it->second.getPixelRatio()) / 2 : 0.0; } } // namespace mbgl diff --git a/src/mbgl/annotation/annotation_manager.hpp b/src/mbgl/annotation/annotation_manager.hpp index 326565f8bc..6c794d7f84 100644 --- a/src/mbgl/annotation/annotation_manager.hpp +++ b/src/mbgl/annotation/annotation_manager.hpp @@ -84,8 +84,6 @@ private: ImageMap images; std::unordered_set tiles; - - friend class AnnotationTile; }; } // namespace mbgl diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index c963140652..b8d5186729 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -239,7 +239,9 @@ void Map::setStyle(std::unique_ptr