summaryrefslogtreecommitdiff
path: root/src/mbgl/annotation
diff options
context:
space:
mode:
Diffstat (limited to 'src/mbgl/annotation')
-rw-r--r--src/mbgl/annotation/annotation_manager.cpp87
-rw-r--r--src/mbgl/annotation/annotation_manager.hpp20
2 files changed, 51 insertions, 56 deletions
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 <mbgl/annotation/symbol_annotation_impl.hpp>
#include <mbgl/annotation/line_annotation_impl.hpp>
#include <mbgl/annotation/fill_annotation_impl.hpp>
+#include <mbgl/style/style.hpp>
#include <mbgl/style/style_impl.hpp>
#include <mbgl/style/layers/symbol_layer.hpp>
#include <mbgl/style/layers/symbol_layer_impl.hpp>
@@ -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<std::mutex> 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<LineAnnotationImpl>(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<FillAnnotationImpl>(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<AnnotationTileData> 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<AnnotationSource>());
+ if (!style.get().impl->getSource(SourceID)) {
+ style.get().impl->addSource(std::make_unique<AnnotationSource>());
std::unique_ptr<SymbolLayer> layer = std::make_unique<SymbolLayer>(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<std::mutex> 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<style::Image>(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<style::Image>(image.second));
}
-
- obsoleteShapeAnnotationLayers.clear();
- obsoleteImages.clear();
}
void AnnotationManager::updateData() {
@@ -225,16 +213,17 @@ void AnnotationManager::addImage(std::unique_ptr<style::Image> image) {
std::lock_guard<std::mutex> 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<style::Image>(inserted.first->second));
}
void AnnotationManager::removeImage(const std::string& id_) {
std::lock_guard<std::mutex> 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 <mbgl/annotation/symbol_annotation_impl.hpp>
#include <mbgl/style/image.hpp>
#include <mbgl/map/update.hpp>
-#include <mbgl/style/style.hpp>
#include <mbgl/util/noncopyable.hpp>
#include <mutex>
@@ -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<AnnotationTileData> getTileData(const CanonicalTileID&);
+ std::reference_wrapper<style::Style> style;
+
std::mutex mutex;
AnnotationID nextID = 0;
@@ -73,8 +80,7 @@ private:
SymbolAnnotationMap symbolAnnotations;
ShapeAnnotationMap shapeAnnotations;
ImageMap images;
- std::unordered_set<std::string> obsoleteShapeAnnotationLayers;
- std::unordered_set<std::string> obsoleteImages;
+
std::unordered_set<AnnotationTile*> tiles;
friend class AnnotationTile;