summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com>2018-11-28 16:03:09 +0200
committerMikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com>2018-11-28 17:18:38 +0200
commitad03e42b2890fd430e99e543a40be42f09f47fba (patch)
tree0644ddaab4f30ac1dc2c59507125cad832b366b2
parenta0b8f01c1baf640f34c0f46ba22efd399b7a0012 (diff)
downloadqtlocation-mapboxgl-ad03e42b2890fd430e99e543a40be42f09f47fba.tar.gz
[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.
-rw-r--r--include/mbgl/style/layer.hpp22
-rw-r--r--platform/android/src/style/layers/layer_manager.cpp2
-rw-r--r--platform/darwin/src/MGLStyleLayerManager.mm2
-rw-r--r--platform/default/layer_manager.cpp2
-rw-r--r--src/mbgl/annotation/annotation_manager.cpp23
-rw-r--r--src/mbgl/annotation/annotation_manager.hpp2
-rw-r--r--src/mbgl/map/map.cpp43
-rw-r--r--src/mbgl/renderer/renderer.cpp9
-rw-r--r--src/mbgl/renderer/renderer_impl.cpp6
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<RenderLayer> createRenderLayer(Immutable<style::Layer::Impl>) 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 <boost/function_output_iterator.hpp>
+// 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> lock(mutex);
tiles.insert(&tile);
tile.setData(getTileData(tile.id.canonical));
}
void AnnotationManager::removeTile(AnnotationTile& tile) {
+ CHECK_ANNOTATIONS_ENABLED_AND_RETURN();
std::lock_guard<std::mutex> lock(mutex);
tiles.erase(&tile);
}
@@ -221,6 +239,7 @@ static std::string prefixedImageID(const std::string& id) {
}
void AnnotationManager::addImage(std::unique_ptr<style::Image> image) {
+ CHECK_ANNOTATIONS_ENABLED_AND_RETURN();
std::lock_guard<std::mutex> lock(mutex);
const std::string id = prefixedImageID(image->getID());
images.erase(id);
@@ -230,6 +249,7 @@ void AnnotationManager::addImage(std::unique_ptr<style::Image> image) {
}
void AnnotationManager::removeImage(const std::string& id_) {
+ CHECK_ANNOTATIONS_ENABLED_AND_RETURN();
std::lock_guard<std::mutex> 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<std::mutex> 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<AnnotationTile*> 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<Style> style) {
assert(style);
impl->onStyleLoading();
impl->style = std::move(style);
- impl->annotationManager.setStyle(*impl->style);
+ if (LayerManager::annotationsEnabled) {
+ impl->annotationManager.setStyle(*impl->style);
+ }
}
#pragma mark - Transitions
@@ -636,32 +638,46 @@ LatLng Map::latLngForPixel(const ScreenCoordinate& pixel) const {
#pragma mark - Annotations
void Map::addAnnotationImage(std::unique_ptr<style::Image> image) {
- impl->annotationManager.addImage(std::move(image));
+ if (LayerManager::annotationsEnabled) {
+ impl->annotationManager.addImage(std::move(image));
+ }
}
void Map::removeAnnotationImage(const std::string& id) {
- impl->annotationManager.removeImage(id);
+ if (LayerManager::annotationsEnabled) {
+ impl->annotationManager.removeImage(id);
+ }
}
double Map::getTopOffsetPixelsForAnnotationImage(const std::string& id) {
- return impl->annotationManager.getTopOffsetPixelsForImage(id);
+ if (LayerManager::annotationsEnabled) {
+ return impl->annotationManager.getTopOffsetPixelsForImage(id);
+ }
+ return 0.0;
}
AnnotationID Map::addAnnotation(const Annotation& annotation) {
- auto result = impl->annotationManager.addAnnotation(annotation);
- impl->onUpdate();
- return result;
+ if (LayerManager::annotationsEnabled) {
+ auto result = impl->annotationManager.addAnnotation(annotation);
+ impl->onUpdate();
+ return result;
+ }
+ return 0;
}
void Map::updateAnnotation(AnnotationID id, const Annotation& annotation) {
- if (impl->annotationManager.updateAnnotation(id, annotation)) {
- impl->onUpdate();
+ if (LayerManager::annotationsEnabled) {
+ if (impl->annotationManager.updateAnnotation(id, annotation)) {
+ impl->onUpdate();
+ }
}
}
void Map::removeAnnotation(AnnotationID annotation) {
- impl->annotationManager.removeAnnotation(annotation);
- impl->onUpdate();
+ if (LayerManager::annotationsEnabled) {
+ impl->annotationManager.removeAnnotation(annotation);
+ impl->onUpdate();
+ }
}
#pragma mark - Toggles
@@ -762,8 +778,9 @@ void Map::Impl::onStyleLoaded() {
if (!cameraMutated) {
map.jumpTo(style->getDefaultCamera());
}
-
- annotationManager.onStyleLoaded();
+ if (LayerManager::annotationsEnabled) {
+ annotationManager.onStyleLoaded();
+ }
observer.onDidFinishLoadingStyle();
}
diff --git a/src/mbgl/renderer/renderer.cpp b/src/mbgl/renderer/renderer.cpp
index c7bd46106d..9976c1b6dc 100644
--- a/src/mbgl/renderer/renderer.cpp
+++ b/src/mbgl/renderer/renderer.cpp
@@ -55,6 +55,9 @@ std::vector<Feature> Renderer::queryRenderedFeatures(const ScreenBox& box, const
}
AnnotationIDs Renderer::queryPointAnnotations(const ScreenBox& box) const {
+ if (!LayerManager::annotationsEnabled) {
+ return {};
+ }
RenderedQueryOptions options;
options.layerIDs = {{ AnnotationManager::PointLayerID }};
auto features = queryRenderedFeatures(box, options);
@@ -62,6 +65,9 @@ AnnotationIDs Renderer::queryPointAnnotations(const ScreenBox& box) const {
}
AnnotationIDs Renderer::queryShapeAnnotations(const ScreenBox& box) const {
+ if (!LayerManager::annotationsEnabled) {
+ return {};
+ }
auto features = impl->queryShapeAnnotations({
box.min,
{box.max.x, box.min.y},
@@ -73,6 +79,9 @@ AnnotationIDs Renderer::queryShapeAnnotations(const ScreenBox& box) const {
}
AnnotationIDs Renderer::getAnnotationIDs(const std::vector<Feature>& features) const {
+ if (!LayerManager::annotationsEnabled) {
+ return {};
+ }
std::set<AnnotationID> set;
for (auto &feature : features) {
assert(feature.id.is<uint64_t>());
diff --git a/src/mbgl/renderer/renderer_impl.cpp b/src/mbgl/renderer/renderer_impl.cpp
index c4c4851345..7d1653cdbd 100644
--- a/src/mbgl/renderer/renderer_impl.cpp
+++ b/src/mbgl/renderer/renderer_impl.cpp
@@ -85,8 +85,9 @@ void Renderer::Impl::render(const UpdateParameters& updateParameters) {
}
assert(BackendScope::exists());
-
- updateParameters.annotationManager.updateData();
+ if (LayerManager::annotationsEnabled) {
+ updateParameters.annotationManager.updateData();
+ }
const bool zoomChanged = zoomHistory.update(updateParameters.transformState.getZoom(), updateParameters.timePoint);
@@ -698,6 +699,7 @@ std::vector<Feature> Renderer::Impl::queryRenderedFeatures(const ScreenLineStrin
}
std::vector<Feature> Renderer::Impl::queryShapeAnnotations(const ScreenLineString& geometry) const {
+ assert(LayerManager::annotationsEnabled);
std::vector<const RenderLayer*> shapeAnnotationLayers;
RenderedQueryOptions options;
for (const auto& layerImpl : *layerImpls) {