From 24a58344284ca8c882bf2ab83b9129ca8ce9d4b3 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Tue, 22 Sep 2015 18:09:40 -0700 Subject: Move updateAnnotationTiles[IfNeeded] to AnnotationManager --- src/mbgl/annotation/annotation_manager.cpp | 103 ++++++++++++++++++++++---- src/mbgl/annotation/annotation_manager.hpp | 12 ++-- src/mbgl/map/map_context.cpp | 111 ++--------------------------- src/mbgl/map/map_context.hpp | 1 - 4 files changed, 97 insertions(+), 130 deletions(-) (limited to 'src') diff --git a/src/mbgl/annotation/annotation_manager.cpp b/src/mbgl/annotation/annotation_manager.cpp index 427401eea4..31b50b1c51 100644 --- a/src/mbgl/annotation/annotation_manager.cpp +++ b/src/mbgl/annotation/annotation_manager.cpp @@ -7,6 +7,9 @@ #include #include #include +#include +#include +#include #include @@ -63,14 +66,6 @@ AnnotationManager::~AnnotationManager() { // Annotation so we can't destruct the object with just the header file. } -void AnnotationManager::markStaleTiles(std::unordered_set ids) { - std::copy(ids.begin(), ids.end(), std::inserter(staleTiles, staleTiles.begin())); -} - -std::unordered_set AnnotationManager::resetStaleTiles() { - return std::move(staleTiles); -} - void AnnotationManager::setDefaultPointAnnotationSymbol(const std::string& symbol) { defaultPointAnnotationSymbol = symbol; } @@ -322,13 +317,6 @@ std::unordered_set AnnotationManager::removeAnnotations(co return affectedTiles; } -const StyleProperties AnnotationManager::getAnnotationStyleProperties(uint32_t annotationID) const { - auto anno_it = annotations.find(annotationID); - assert(anno_it != annotations.end()); - - return anno_it->second->styleProperties; -} - AnnotationIDs AnnotationManager::getAnnotationsInBounds(const LatLngBounds& queryBounds, const uint8_t maxZoom, const AnnotationType& type) const { @@ -490,6 +478,91 @@ const LiveTile* AnnotationManager::getTile(const TileID& id) { return renderTile; } +void AnnotationManager::updateTilesIfNeeded(Style* style) { + if (!staleTiles.empty()) { + updateTiles(staleTiles, style); + } +} + +void AnnotationManager::updateTiles(const AffectedTiles& ids, Style* style) { + std::copy(ids.begin(), ids.end(), std::inserter(staleTiles, staleTiles.begin())); + + if (!style) { + return; + } + + // grab existing, single shape annotations source + const auto& shapeID = AnnotationManager::ShapeLayerID; + Source* shapeAnnotationSource = style->getSource(shapeID); + + // Style not parsed yet + if (!shapeAnnotationSource) { + return; + } + + shapeAnnotationSource->enabled = true; + + const auto& layers = style->layers; + + // create (if necessary) layers and buckets for each shape + for (const auto& shapeAnnotationID : orderedShapeAnnotations) { + const std::string shapeLayerID = shapeID + "." + util::toString(shapeAnnotationID); + + if (std::find_if(layers.begin(), layers.end(), [&](auto l) { return l->id == shapeLayerID; }) != layers.end()) { + continue; + } + + // apply shape paint properties + const StyleProperties& shapeStyle = annotations.at(shapeAnnotationID)->styleProperties; + ClassProperties paintProperties; + + if (shapeStyle.is()) { + const LineProperties& lineProperties = shapeStyle.get(); + paintProperties.set(PropertyKey::LineOpacity, ConstantFunction(lineProperties.opacity)); + paintProperties.set(PropertyKey::LineWidth, ConstantFunction(lineProperties.width)); + paintProperties.set(PropertyKey::LineColor, ConstantFunction(lineProperties.color)); + } else if (shapeStyle.is()) { + const FillProperties& fillProperties = shapeStyle.get(); + paintProperties.set(PropertyKey::FillOpacity, ConstantFunction(fillProperties.opacity)); + paintProperties.set(PropertyKey::FillColor, ConstantFunction(fillProperties.fill_color)); + paintProperties.set(PropertyKey::FillOutlineColor, ConstantFunction(fillProperties.stroke_color)); + } + + std::map shapePaints; + shapePaints.emplace(ClassID::Default, std::move(paintProperties)); + + // create shape layer + util::ptr shapeLayer = std::make_shared(shapeLayerID, std::move(shapePaints)); + shapeLayer->type = (shapeStyle.is() ? StyleLayerType::Line : StyleLayerType::Fill); + + // add to end of other shape layers just before (last) point layer + style->layers.emplace((style->layers.end() - 1), shapeLayer); + + // create shape bucket & connect to source + util::ptr shapeBucket = std::make_shared(shapeLayer->type); + shapeBucket->name = shapeLayer->id; + shapeBucket->source = shapeID; + shapeBucket->source_layer = shapeLayer->id; + + // apply line layout properties to bucket + if (shapeStyle.is()) { + shapeBucket->layout.set(PropertyKey::LineJoin, ConstantFunction(JoinType::Round)); + } + + // connect layer to bucket + shapeLayer->bucket = shapeBucket; + } + + // invalidate annotations layer tiles + for (const auto &source : style->sources) { + if (source->info.type == SourceType::Annotations) { + source->invalidateTiles(ids); + } + } + + staleTiles.clear(); +} + const std::string AnnotationManager::PointLayerID = "com.mapbox.annotations.points"; const std::string AnnotationManager::ShapeLayerID = "com.mapbox.annotations.shape"; diff --git a/src/mbgl/annotation/annotation_manager.hpp b/src/mbgl/annotation/annotation_manager.hpp index 60cafdf342..cf8d8e3e77 100644 --- a/src/mbgl/annotation/annotation_manager.hpp +++ b/src/mbgl/annotation/annotation_manager.hpp @@ -23,6 +23,7 @@ class Annotation; class PointAnnotation; class ShapeAnnotation; class LiveTile; +class Style; using GeoJSONVT = mapbox::util::geojsonvt::GeoJSONVT; @@ -33,10 +34,6 @@ public: AnnotationManager(); ~AnnotationManager(); - void markStaleTiles(std::unordered_set); - size_t getStaleTileCount() const { return staleTiles.size(); } - std::unordered_set resetStaleTiles(); - void setDefaultPointAnnotationSymbol(const std::string& symbol); std::pair @@ -48,8 +45,8 @@ public: AffectedTiles removeAnnotations(const AnnotationIDs&, const uint8_t maxZoom); - AnnotationIDs getOrderedShapeAnnotations() const { return orderedShapeAnnotations; } - const StyleProperties getAnnotationStyleProperties(uint32_t) const; + void updateTilesIfNeeded(Style*); + void updateTiles(const AffectedTiles&, Style*); AnnotationIDs getAnnotationsInBounds(const LatLngBounds&, const uint8_t maxZoom, const AnnotationType& = AnnotationType::Any) const; LatLngBounds getBoundsForAnnotations(const AnnotationIDs&) const; @@ -66,7 +63,8 @@ private: uint32_t addShapeAnnotation(const ShapeAnnotation&, const uint8_t maxZoom); uint32_t addPointAnnotation(const PointAnnotation&, const uint8_t maxZoom, AffectedTiles&); -private: + const StyleProperties getAnnotationStyleProperties(uint32_t) const; + std::string defaultPointAnnotationSymbol; std::unordered_map> annotations; std::vector orderedShapeAnnotations; diff --git a/src/mbgl/map/map_context.cpp b/src/mbgl/map/map_context.cpp index 5858918ae9..1c0c98b599 100644 --- a/src/mbgl/map/map_context.cpp +++ b/src/mbgl/map/map_context.cpp @@ -15,8 +15,6 @@ #include #include -#include -#include #include #include @@ -143,113 +141,10 @@ void MapContext::loadStyleJSON(const std::string& json, const std::string& base) asyncUpdate->send(); } -void MapContext::updateAnnotationTilesIfNeeded() { - if (data.getAnnotationManager()->getStaleTileCount()) { - auto staleTiles = data.getAnnotationManager()->resetStaleTiles(); - updateAnnotationTiles(staleTiles); - } -} - void MapContext::updateAnnotationTiles(const std::unordered_set& ids) { - assert(util::ThreadContext::currentlyOn(util::ThreadType::Map)); - - util::exclusive annotationManager = data.getAnnotationManager(); - annotationManager->markStaleTiles(ids); - - if (!style) { - return; - } - - // grab existing, single shape annotations source - const auto& shapeID = AnnotationManager::ShapeLayerID; - Source* shapeAnnotationSource = style->getSource(shapeID); - - // Style not parsed yet - if (!shapeAnnotationSource) { - return; - } - - shapeAnnotationSource->enabled = true; - - // create (if necessary) layers and buckets for each shape - for (const auto &shapeAnnotationID : annotationManager->getOrderedShapeAnnotations()) { - const std::string shapeLayerID = shapeID + "." + util::toString(shapeAnnotationID); - - const auto layer_it = std::find_if(style->layers.begin(), style->layers.end(), - [&shapeLayerID](util::ptr layer) { - return (layer->id == shapeLayerID); - }); - - if (layer_it == style->layers.end()) { - // query shape styling - auto& shapeStyle = annotationManager->getAnnotationStyleProperties(shapeAnnotationID); - - // apply shape paint properties - ClassProperties paintProperties; - - if (shapeStyle.is()) { - // opacity - PropertyValue lineOpacity = ConstantFunction(shapeStyle.get().opacity); - paintProperties.set(PropertyKey::LineOpacity, lineOpacity); - - // line width - PropertyValue lineWidth = ConstantFunction(shapeStyle.get().width); - paintProperties.set(PropertyKey::LineWidth, lineWidth); - - // stroke color - PropertyValue strokeColor = ConstantFunction(shapeStyle.get().color); - paintProperties.set(PropertyKey::LineColor, strokeColor); - } else if (shapeStyle.is()) { - // opacity - PropertyValue fillOpacity = ConstantFunction(shapeStyle.get().opacity); - paintProperties.set(PropertyKey::FillOpacity, fillOpacity); - - // fill color - PropertyValue fillColor = ConstantFunction(shapeStyle.get().fill_color); - paintProperties.set(PropertyKey::FillColor, fillColor); - - // stroke color - PropertyValue strokeColor = ConstantFunction(shapeStyle.get().stroke_color); - paintProperties.set(PropertyKey::FillOutlineColor, strokeColor); - } - - std::map shapePaints; - shapePaints.emplace(ClassID::Default, std::move(paintProperties)); - - // create shape layer - util::ptr shapeLayer = std::make_shared(shapeLayerID, std::move(shapePaints)); - shapeLayer->type = (shapeStyle.is() ? StyleLayerType::Line : StyleLayerType::Fill); - - // add to end of other shape layers just before (last) point layer - style->layers.emplace((style->layers.end() - 1), shapeLayer); - - // create shape bucket & connect to source - util::ptr shapeBucket = std::make_shared(shapeLayer->type); - shapeBucket->name = shapeLayer->id; - shapeBucket->source = shapeID; - shapeBucket->source_layer = shapeLayer->id; - - // apply line layout properties to bucket - if (shapeStyle.is()) { - shapeBucket->layout.set(PropertyKey::LineJoin, ConstantFunction(JoinType::Round)); - } - - // connect layer to bucket - shapeLayer->bucket = shapeBucket; - } - } - - // invalidate annotations layer tiles - for (const auto &source : style->sources) { - if (source->info.type == SourceType::Annotations) { - source->invalidateTiles(ids); - } - } - + data.getAnnotationManager()->updateTiles(ids, style.get()); updateFlags |= Update::Classes; asyncUpdate->send(); - - annotationManager->resetStaleTiles(); } void MapContext::update() { @@ -419,7 +314,9 @@ void MapContext::onResourceLoadingFailed(std::exception_ptr error) { } void MapContext::onSpriteStoreLoaded() { - updateAnnotationTilesIfNeeded(); + data.getAnnotationManager()->updateTilesIfNeeded(style.get()); + updateFlags |= Update::Classes; + asyncUpdate->send(); } } diff --git a/src/mbgl/map/map_context.hpp b/src/mbgl/map/map_context.hpp index 449a0f1d1c..f3ac636a40 100644 --- a/src/mbgl/map/map_context.hpp +++ b/src/mbgl/map/map_context.hpp @@ -54,7 +54,6 @@ public: bool isLoaded() const; double getTopOffsetPixelsForAnnotationSymbol(const std::string& symbol); - void updateAnnotationTilesIfNeeded(); void updateAnnotationTiles(const std::unordered_set&); void setSourceTileCacheSize(size_t size); -- cgit v1.2.1