[core] Batch source updates

This commit is contained in:
John Firebaugh
2016-08-30 12:43:46 -07:00
parent 6c23a6a095
commit 8e2170c885
5 changed files with 46 additions and 16 deletions

View File

@@ -2,11 +2,9 @@
#include <mbgl/util/traits.hpp>
#include <cstdint>
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) {

View File

@@ -212,6 +212,10 @@ void Map::Impl::update() {
annotationManager->updateData();
}
if (updateFlags & Update::Layout) {
style->relayout();
}
if (updateFlags & Update::Classes) {
style->cascade(timePoint, mode);
}

View File

@@ -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<const Layer*> 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 <class VectorLayer>
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 {

View File

@@ -4,6 +4,7 @@
#include <mbgl/style/observer.hpp>
#include <mbgl/style/source_observer.hpp>
#include <mbgl/style/layer_observer.hpp>
#include <mbgl/style/update_batch.hpp>
#include <mbgl/text/glyph_store_observer.hpp>
#include <mbgl/sprite/sprite_store_observer.hpp>
#include <mbgl/map/mode.hpp>
@@ -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;

View File

@@ -0,0 +1,15 @@
#pragma once
#include <set>
#include <string>
namespace mbgl {
namespace style {
class UpdateBatch {
public:
std::set<std::string> sourceIDs;
};
} // namespace style
} // namespace mbgl