summaryrefslogtreecommitdiff
path: root/src/mbgl/renderer
diff options
context:
space:
mode:
authorJuha Alanen <juha.alanen@mapbox.com>2019-08-21 14:35:50 +0300
committerJuha Alanen <19551460+jmalanen@users.noreply.github.com>2019-09-18 14:29:15 +0300
commit46eab5f1488570262b218dfaec99c476829ab21f (patch)
tree82e82f846d14966f67755342ac2a9514ee0400c3 /src/mbgl/renderer
parent1ac8e3c16145b4ec4ee341ad8d407f389575e52c (diff)
downloadqtlocation-mapboxgl-46eab5f1488570262b218dfaec99c476829ab21f.tar.gz
[core] Add SourceFeatureState class to handle feature states
Diffstat (limited to 'src/mbgl/renderer')
-rw-r--r--src/mbgl/renderer/source_state.cpp69
-rw-r--r--src/mbgl/renderer/source_state.hpp26
2 files changed, 95 insertions, 0 deletions
diff --git a/src/mbgl/renderer/source_state.cpp b/src/mbgl/renderer/source_state.cpp
new file mode 100644
index 0000000000..087e28b2bb
--- /dev/null
+++ b/src/mbgl/renderer/source_state.cpp
@@ -0,0 +1,69 @@
+#include <mbgl/renderer/source_state.hpp>
+#include <mbgl/style/conversion_impl.hpp>
+#include <mbgl/renderer/render_tile.hpp>
+#include <mbgl/util/logging.hpp>
+
+namespace mbgl {
+
+void SourceFeatureState::updateState(const optional<std::string>& sourceLayerID, const std::string& featureID, const FeatureState& newState) {
+ std::string sourceLayer = sourceLayerID.value_or(std::string());
+ for (const auto& state : newState) {
+ auto& layerStates = stateChanges[sourceLayer];
+ auto& featureStates = layerStates[featureID];
+ featureStates[state.first] = state.second;
+ }
+}
+
+void SourceFeatureState::getState(FeatureState& result, const optional<std::string>& sourceLayerID, const std::string& featureID) const {
+ std::string sourceLayer = sourceLayerID.value_or(std::string());
+ FeatureState current, changes;
+ auto layerStates = currentStates.find(sourceLayer);
+ if (layerStates != currentStates.end()) {
+ const auto currentStateEntry = layerStates->second.find(featureID);
+ if (currentStateEntry != layerStates->second.end()) {
+ current = currentStateEntry->second;
+ }
+ }
+
+ layerStates = stateChanges.find(sourceLayer);
+ if (layerStates != stateChanges.end()) {
+ const auto stateChangesEntry = layerStates->second.find(featureID);
+ if (stateChangesEntry != layerStates->second.end()) {
+ changes = stateChangesEntry->second;
+ }
+ }
+ result = std::move(changes);
+ result.insert(current.begin(), current.end());
+}
+
+void SourceFeatureState::coalesceChanges(std::vector<RenderTile>& tiles) {
+ LayerFeatureStates changes;
+ for (const auto& layerStatesEntry : stateChanges) {
+ const auto& sourceLayer = layerStatesEntry.first;
+ FeatureStates layerStates;
+ for (const auto& featureStatesEntry : stateChanges[sourceLayer]) {
+ const auto& featureID = featureStatesEntry.first;
+ for (const auto& stateEntry : stateChanges[sourceLayer][featureID]) {
+ const auto& stateKey = stateEntry.first;
+ const auto& stateVal = stateEntry.second;
+
+ auto currentState = currentStates[sourceLayer][featureID].find(stateKey);
+ if (currentState != currentStates[sourceLayer][featureID].end()) {
+ currentState->second = stateVal;
+ } else {
+ currentStates[sourceLayer][featureID].insert(std::make_pair(stateKey, stateVal));
+ }
+ }
+ layerStates[featureID] = currentStates[sourceLayer][featureID];
+ }
+ changes[sourceLayer] = std::move(layerStates);
+ }
+ stateChanges.clear();
+ if (changes.empty()) return;
+
+ for (auto& tile : tiles) {
+ tile.setFeatureState(changes);
+ }
+}
+
+} // namespace mbgl
diff --git a/src/mbgl/renderer/source_state.hpp b/src/mbgl/renderer/source_state.hpp
new file mode 100644
index 0000000000..9944709b0d
--- /dev/null
+++ b/src/mbgl/renderer/source_state.hpp
@@ -0,0 +1,26 @@
+#pragma once
+
+#include <mbgl/style/conversion.hpp>
+#include <mbgl/util/feature.hpp>
+
+namespace mbgl {
+
+class RenderTile;
+using namespace style::conversion;
+
+class SourceFeatureState {
+public:
+ SourceFeatureState() = default;
+ ~SourceFeatureState() = default;
+
+ void updateState(const optional<std::string>& sourceLayerID, const std::string& featureID, const FeatureState& newState);
+ void getState(FeatureState& result, const optional<std::string>& sourceLayerID, const std::string& featureID) const;
+
+ void coalesceChanges(std::vector<RenderTile>& tiles);
+
+private:
+ LayerFeatureStates currentStates;
+ LayerFeatureStates stateChanges;
+};
+
+} // namespace mbgl