From 8e2170c8855456258de8ffd49d22a621b95e9fb2 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Tue, 30 Aug 2016 12:43:46 -0700 Subject: [core] Batch source updates --- include/mbgl/map/update.hpp | 5 ++--- src/mbgl/map/map.cpp | 4 ++++ src/mbgl/style/style.cpp | 35 ++++++++++++++++++++++------------- src/mbgl/style/style.hpp | 3 +++ src/mbgl/style/update_batch.hpp | 15 +++++++++++++++ 5 files changed, 46 insertions(+), 16 deletions(-) create mode 100644 src/mbgl/style/update_batch.hpp diff --git a/include/mbgl/map/update.hpp b/include/mbgl/map/update.hpp index 36ce59c01d..1da7e3ac92 100644 --- a/include/mbgl/map/update.hpp +++ b/include/mbgl/map/update.hpp @@ -2,11 +2,9 @@ #include -#include - namespace mbgl { -enum class Update : uint8_t { +enum class Update { Nothing = 0, Dimensions = 1 << 1, Classes = 1 << 2, @@ -15,6 +13,7 @@ enum class Update : uint8_t { Repaint = 1 << 5, AnnotationStyle = 1 << 6, AnnotationData = 1 << 7, + Layout = 1 << 8 }; constexpr Update operator|(Update lhs, Update rhs) { diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index fe0be15b87..9e18e67f3a 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -212,6 +212,10 @@ void Map::Impl::update() { annotationManager->updateData(); } + if (updateFlags & Update::Layout) { + style->relayout(); + } + if (updateFlags & Update::Classes) { style->cascade(timePoint, mode); } diff --git a/src/mbgl/style/style.cpp b/src/mbgl/style/style.cpp index e797ec7a5b..1009654fc4 100644 --- a/src/mbgl/style/style.cpp +++ b/src/mbgl/style/style.cpp @@ -92,6 +92,7 @@ void Style::setJSON(const std::string& json) { sources.clear(); layers.clear(); classes.clear(); + updateBatch = {}; Parser parser; auto error = parser.parse(json); @@ -133,9 +134,13 @@ void Style::removeSource(const std::string& id) { auto it = std::find_if(sources.begin(), sources.end(), [&](const auto& source) { return source->getID() == id; }); - if (it == sources.end()) + + if (it == sources.end()) { throw std::runtime_error("no such source"); + } + sources.erase(it); + updateBatch.sourceIDs.erase(id); } std::vector Style::getLayers() const { @@ -220,6 +225,15 @@ void Style::updateTiles(const UpdateParameters& parameters) { } } +void Style::relayout() { + for (const auto& sourceID : updateBatch.sourceIDs) { + Source* source = getSource(sourceID); + if (!source) continue; + source->baseImpl->reload(); + } + updateBatch.sourceIDs.clear(); +} + void Style::cascade(const TimePoint& timePoint, MapMode mode) { // When in continuous mode, we can either have user- or style-defined // transitions. Still mode is always immediate. @@ -487,8 +501,8 @@ void Style::onSpriteError(std::exception_ptr error) { observer->onResourceError(error); } -struct LayerSourceReloadVisitor { - Style& style; +struct QueueSourceReloadVisitor { + UpdateBatch& updateBatch; void operator()(CustomLayer&) { assert(false); } void operator()(RasterLayer&) { assert(false); } @@ -496,18 +510,13 @@ struct LayerSourceReloadVisitor { template void operator()(VectorLayer& layer) { - Source* source = style.getSource(layer.getSourceID()); - if (!source) return; - source->baseImpl->reload(); + updateBatch.sourceIDs.insert(layer.getSourceID()); } }; -void Style::reloadLayerSource(Layer& layer) { - layer.accept(LayerSourceReloadVisitor { *this }); -} - void Style::onLayerFilterChanged(Layer& layer) { - reloadLayerSource(layer); + layer.accept(QueueSourceReloadVisitor { updateBatch }); + observer->onUpdate(Update::Layout); } void Style::onLayerPaintPropertyChanged(Layer&) { @@ -515,8 +524,8 @@ void Style::onLayerPaintPropertyChanged(Layer&) { } void Style::onLayerLayoutPropertyChanged(Layer& layer) { - observer->onUpdate(Update::RecalculateStyle); - reloadLayerSource(layer); + layer.accept(QueueSourceReloadVisitor { updateBatch }); + observer->onUpdate(Update::Layout); } void Style::dumpDebugLogs() const { diff --git a/src/mbgl/style/style.hpp b/src/mbgl/style/style.hpp index 26b96fba22..b3f70e1eb2 100644 --- a/src/mbgl/style/style.hpp +++ b/src/mbgl/style/style.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -52,6 +53,7 @@ public: // a tile is ready so observers can render the tile. void updateTiles(const UpdateParameters&); + void relayout(); void cascade(const TimePoint&, MapMode); void recalculate(float z, const TimePoint&, MapMode); @@ -142,6 +144,7 @@ private: std::exception_ptr lastError; + UpdateBatch updateBatch; ZoomHistory zoomHistory; bool hasPendingTransitions = false; diff --git a/src/mbgl/style/update_batch.hpp b/src/mbgl/style/update_batch.hpp new file mode 100644 index 0000000000..ee82c98fda --- /dev/null +++ b/src/mbgl/style/update_batch.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include +#include + +namespace mbgl { +namespace style { + +class UpdateBatch { +public: + std::set sourceIDs; +}; + +} // namespace style +} // namespace mbgl -- cgit v1.2.1