From e35cbbae55ab01f33690b1bb2e918c5f8393b854 Mon Sep 17 00:00:00 2001 From: Ivo van Dongen Date: Thu, 22 Jun 2017 14:33:21 -0700 Subject: [core] mutate style on annotation mutations immediately --- src/mbgl/annotation/annotation_manager.cpp | 87 +++++++++++++----------------- src/mbgl/annotation/annotation_manager.hpp | 20 ++++--- src/mbgl/map/map.cpp | 20 ++++--- src/mbgl/map/update.hpp | 1 - 4 files changed, 60 insertions(+), 68 deletions(-) (limited to 'src') diff --git a/src/mbgl/annotation/annotation_manager.cpp b/src/mbgl/annotation/annotation_manager.cpp index a69dba1bf2..ec5a360a90 100644 --- a/src/mbgl/annotation/annotation_manager.cpp +++ b/src/mbgl/annotation/annotation_manager.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -18,9 +19,20 @@ using namespace style; const std::string AnnotationManager::SourceID = "com.mapbox.annotations"; const std::string AnnotationManager::PointLayerID = "com.mapbox.annotations.points"; -AnnotationManager::AnnotationManager() = default; +AnnotationManager::AnnotationManager(Style& style_) + : style(style_) { +}; + AnnotationManager::~AnnotationManager() = default; +void AnnotationManager::setStyle(Style& style_) { + style = style_; +} + +void AnnotationManager::onStyleLoaded() { + updateStyle(); +} + AnnotationID AnnotationManager::addAnnotation(const Annotation& annotation, const uint8_t maxZoom) { std::lock_guard lock(mutex); AnnotationID id = nextID++; @@ -51,13 +63,13 @@ void AnnotationManager::add(const AnnotationID& id, const SymbolAnnotation& anno void AnnotationManager::add(const AnnotationID& id, const LineAnnotation& annotation, const uint8_t maxZoom) { ShapeAnnotationImpl& impl = *shapeAnnotations.emplace(id, std::make_unique(id, annotation, maxZoom)).first->second; - obsoleteShapeAnnotationLayers.erase(impl.layerID); + impl.updateStyle(*style.get().impl); } void AnnotationManager::add(const AnnotationID& id, const FillAnnotation& annotation, const uint8_t maxZoom) { ShapeAnnotationImpl& impl = *shapeAnnotations.emplace(id, std::make_unique(id, annotation, maxZoom)).first->second; - obsoleteShapeAnnotationLayers.erase(impl.layerID); + impl.updateStyle(*style.get().impl); } Update AnnotationManager::update(const AnnotationID& id, const SymbolAnnotation& annotation, const uint8_t maxZoom) { @@ -71,16 +83,11 @@ Update AnnotationManager::update(const AnnotationID& id, const SymbolAnnotation& const SymbolAnnotation& existing = it->second->annotation; - if (existing.geometry != annotation.geometry) { + if (existing.geometry != annotation.geometry || existing.icon != annotation.icon) { result |= Update::AnnotationData; - } - - if (existing.icon != annotation.icon) { - result |= Update::AnnotationData | Update::AnnotationStyle; - } - if (result != Update::Nothing) { - removeAndAdd(id, annotation, maxZoom); + remove(id); + add(id, annotation, maxZoom); } return result; @@ -93,8 +100,9 @@ Update AnnotationManager::update(const AnnotationID& id, const LineAnnotation& a return Update::Nothing; } - removeAndAdd(id, annotation, maxZoom); - return Update::AnnotationData | Update::AnnotationStyle; + shapeAnnotations.erase(it); + add(id, annotation, maxZoom); + return Update::AnnotationData; } Update AnnotationManager::update(const AnnotationID& id, const FillAnnotation& annotation, const uint8_t maxZoom) { @@ -104,15 +112,9 @@ Update AnnotationManager::update(const AnnotationID& id, const FillAnnotation& a return Update::Nothing; } - removeAndAdd(id, annotation, maxZoom); - return Update::AnnotationData | Update::AnnotationStyle; -} - -void AnnotationManager::removeAndAdd(const AnnotationID& id, const Annotation& annotation, const uint8_t maxZoom) { - remove(id); - Annotation::visit(annotation, [&] (const auto& annotation_) { - this->add(id, annotation_, maxZoom); - }); + shapeAnnotations.erase(it); + add(id, annotation, maxZoom); + return Update::AnnotationData; } void AnnotationManager::remove(const AnnotationID& id) { @@ -120,8 +122,9 @@ void AnnotationManager::remove(const AnnotationID& id) { symbolTree.remove(symbolAnnotations.at(id)); symbolAnnotations.erase(id); } else if (shapeAnnotations.find(id) != shapeAnnotations.end()) { - obsoleteShapeAnnotationLayers.insert(shapeAnnotations.at(id)->layerID); - shapeAnnotations.erase(id); + auto it = shapeAnnotations.find(id); + *style.get().impl->removeLayer(it->second->layerID); + shapeAnnotations.erase(it); } else { assert(false); // Should never happen } @@ -149,11 +152,11 @@ std::unique_ptr AnnotationManager::getTileData(const Canonic return tileData; } -void AnnotationManager::updateStyle(Style::Impl& style) { +void AnnotationManager::updateStyle() { // Create annotation source, point layer, and point bucket. We do everything via Style::Impl // because we don't want annotation mutations to trigger Style::Impl::styleMutated to be set. - if (!style.getSource(SourceID)) { - style.addSource(std::make_unique()); + if (!style.get().impl->getSource(SourceID)) { + style.get().impl->addSource(std::make_unique()); std::unique_ptr layer = std::make_unique(PointLayerID, SourceID); @@ -162,13 +165,13 @@ void AnnotationManager::updateStyle(Style::Impl& style) { layer->setIconAllowOverlap(true); layer->setIconIgnorePlacement(true); - style.addLayer(std::move(layer)); + style.get().impl->addLayer(std::move(layer)); } std::lock_guard lock(mutex); for (const auto& shape : shapeAnnotations) { - shape.second->updateStyle(style); + shape.second->updateStyle(*style.get().impl); } for (const auto& image : images) { @@ -178,23 +181,8 @@ void AnnotationManager::updateStyle(Style::Impl& style) { // of which images need to be added because we don't know if the style is the same // instance as in the last updateStyle call. If it's a new style, we need to add all // images.) - style.addImage(std::make_unique(image.second)); - } - - for (const auto& layer : obsoleteShapeAnnotationLayers) { - if (style.getLayer(layer)) { - style.removeLayer(layer); - } - } - - for (const auto& image : obsoleteImages) { - if (style.getImage(image)) { - style.removeImage(image); - } + style.get().impl->addImage(std::make_unique(image.second)); } - - obsoleteShapeAnnotationLayers.clear(); - obsoleteImages.clear(); } void AnnotationManager::updateData() { @@ -225,16 +213,17 @@ void AnnotationManager::addImage(std::unique_ptr image) { std::lock_guard lock(mutex); const std::string id = prefixedImageID(image->getID()); images.erase(id); - images.emplace(id, - style::Image(id, image->getImage().clone(), image->getPixelRatio(), image->isSdf())); - obsoleteImages.erase(id); + auto inserted = images.emplace(id, style::Image(id, image->getImage().clone(), + image->getPixelRatio(), image->isSdf())); + + style.get().impl->addImage(std::make_unique(inserted.first->second)); } void AnnotationManager::removeImage(const std::string& id_) { std::lock_guard lock(mutex); const std::string id = prefixedImageID(id_); images.erase(id); - obsoleteImages.insert(id); + style.get().impl->removeImage(id); } double AnnotationManager::getTopOffsetPixelsForImage(const std::string& id_) { diff --git a/src/mbgl/annotation/annotation_manager.hpp b/src/mbgl/annotation/annotation_manager.hpp index 6906791db7..dee823bc0f 100644 --- a/src/mbgl/annotation/annotation_manager.hpp +++ b/src/mbgl/annotation/annotation_manager.hpp @@ -4,7 +4,6 @@ #include #include #include -#include #include #include @@ -21,9 +20,13 @@ class AnnotationTileData; class SymbolAnnotationImpl; class ShapeAnnotationImpl; +namespace style { +class Style; +} // namespace style + class AnnotationManager : private util::noncopyable { public: - AnnotationManager(); + AnnotationManager(style::Style&); ~AnnotationManager(); AnnotationID addAnnotation(const Annotation&, const uint8_t maxZoom); @@ -34,7 +37,9 @@ public: void removeImage(const std::string&); double getTopOffsetPixelsForImage(const std::string&); - void updateStyle(style::Style::Impl&); + void setStyle(style::Style&); + void onStyleLoaded(); + void updateData(); void addTile(AnnotationTile&); @@ -52,12 +57,14 @@ private: Update update(const AnnotationID&, const LineAnnotation&, const uint8_t); Update update(const AnnotationID&, const FillAnnotation&, const uint8_t); - void removeAndAdd(const AnnotationID&, const Annotation&, const uint8_t); - void remove(const AnnotationID&); + void updateStyle(); + std::unique_ptr getTileData(const CanonicalTileID&); + std::reference_wrapper style; + std::mutex mutex; AnnotationID nextID = 0; @@ -73,8 +80,7 @@ private: SymbolAnnotationMap symbolAnnotations; ShapeAnnotationMap shapeAnnotations; ImageMap images; - std::unordered_set obsoleteShapeAnnotationLayers; - std::unordered_set obsoleteImages; + std::unordered_set tiles; friend class AnnotationTile; diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index 26795f7814..e644f91c4f 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -87,11 +87,12 @@ public: Update updateFlags = Update::Nothing; - AnnotationManager annotationManager; std::unique_ptr painter; std::unique_ptr