From 46eab5f1488570262b218dfaec99c476829ab21f Mon Sep 17 00:00:00 2001 From: Juha Alanen Date: Wed, 21 Aug 2019 14:35:50 +0300 Subject: [core] Add SourceFeatureState class to handle feature states --- src/mbgl/renderer/source_state.cpp | 69 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/mbgl/renderer/source_state.cpp (limited to 'src/mbgl/renderer/source_state.cpp') 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 +#include +#include +#include + +namespace mbgl { + +void SourceFeatureState::updateState(const optional& 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& 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& 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 -- cgit v1.2.1 From 48f462bc7b09285c09c2a6f3cd07b555fcd21f43 Mon Sep 17 00:00:00 2001 From: Juha Alanen Date: Fri, 13 Sep 2019 11:15:21 +0300 Subject: [core] Add removeFeatureState API --- src/mbgl/renderer/source_state.cpp | 54 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'src/mbgl/renderer/source_state.cpp') diff --git a/src/mbgl/renderer/source_state.cpp b/src/mbgl/renderer/source_state.cpp index 087e28b2bb..c1b95aca68 100644 --- a/src/mbgl/renderer/source_state.cpp +++ b/src/mbgl/renderer/source_state.cpp @@ -58,7 +58,37 @@ void SourceFeatureState::coalesceChanges(std::vector& tiles) { } changes[sourceLayer] = std::move(layerStates); } + + for (const auto& layerStatesEntry : deletedStates) { + const auto& sourceLayer = layerStatesEntry.first; + FeatureStates layerStates = { {}, {} }; + + if (deletedStates[sourceLayer].empty()) { + for (const auto& featureStatesEntry : currentStates[sourceLayer]) { + const auto& featureID = featureStatesEntry.first; + layerStates[featureID] = {}; + currentStates[sourceLayer][featureID] = {}; + } + } else { + for (const auto& feature : deletedStates[sourceLayer]) { + const auto& featureID = feature.first; + bool deleteWholeFeatureState = deletedStates[sourceLayer][featureID].empty(); + if (deleteWholeFeatureState) { + currentStates[sourceLayer][featureID] = {}; + } else { + for (const auto& stateEntry : deletedStates[sourceLayer][featureID]) { + currentStates[sourceLayer][featureID].erase(stateEntry.first); + } + } + layerStates[featureID] = currentStates[sourceLayer][featureID]; + } + } + changes[sourceLayer] = std::move(layerStates); + } + stateChanges.clear(); + deletedStates.clear(); + if (changes.empty()) return; for (auto& tile : tiles) { @@ -66,4 +96,28 @@ void SourceFeatureState::coalesceChanges(std::vector& tiles) { } } +void SourceFeatureState::removeState(const optional& sourceLayerID, const optional& featureID, const optional& stateKey) { + std::string sourceLayer = sourceLayerID.value_or(std::string()); + + bool sourceLayerDeleted = (deletedStates.count(sourceLayer) > 0) && deletedStates[sourceLayer].empty(); + if (sourceLayerDeleted) return; + + if (stateKey && featureID) { + if ((deletedStates.count(sourceLayer) == 0) && (deletedStates[sourceLayer].count(*featureID)) == 0) { + deletedStates[sourceLayer][*featureID][*stateKey] = {}; + } + } else if (featureID) { + bool updateInQueue = stateChanges.count(sourceLayer) && stateChanges[sourceLayer].count(*featureID); + if (updateInQueue) { + for (const auto& changeEntry : stateChanges[sourceLayer][*featureID]) { + deletedStates[sourceLayer][*featureID][changeEntry.first] = {}; + } + } else { + deletedStates[sourceLayer][*featureID] = {}; + } + } else { + deletedStates[sourceLayer] = {}; + } +} + } // namespace mbgl -- cgit v1.2.1 From 6b13c42314f28e3b27111367c4f3296c3c3c1248 Mon Sep 17 00:00:00 2001 From: Juha Alanen Date: Tue, 17 Sep 2019 15:40:01 +0300 Subject: [build] Fix clang format and tidy checks --- src/mbgl/renderer/source_state.cpp | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to 'src/mbgl/renderer/source_state.cpp') diff --git a/src/mbgl/renderer/source_state.cpp b/src/mbgl/renderer/source_state.cpp index c1b95aca68..d057c211b5 100644 --- a/src/mbgl/renderer/source_state.cpp +++ b/src/mbgl/renderer/source_state.cpp @@ -1,11 +1,12 @@ +#include #include #include -#include #include namespace mbgl { -void SourceFeatureState::updateState(const optional& sourceLayerID, const std::string& featureID, const FeatureState& newState) { +void SourceFeatureState::updateState(const optional& 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]; @@ -14,9 +15,11 @@ void SourceFeatureState::updateState(const optional& sourceLayerID, } } -void SourceFeatureState::getState(FeatureState& result, const optional& sourceLayerID, const std::string& featureID) const { +void SourceFeatureState::getState(FeatureState& result, const optional& sourceLayerID, + const std::string& featureID) const { std::string sourceLayer = sourceLayerID.value_or(std::string()); - FeatureState current, changes; + FeatureState current; + FeatureState changes; auto layerStates = currentStates.find(sourceLayer); if (layerStates != currentStates.end()) { const auto currentStateEntry = layerStates->second.find(featureID); @@ -61,7 +64,7 @@ void SourceFeatureState::coalesceChanges(std::vector& tiles) { for (const auto& layerStatesEntry : deletedStates) { const auto& sourceLayer = layerStatesEntry.first; - FeatureStates layerStates = { {}, {} }; + FeatureStates layerStates = {{}, {}}; if (deletedStates[sourceLayer].empty()) { for (const auto& featureStatesEntry : currentStates[sourceLayer]) { @@ -89,25 +92,31 @@ void SourceFeatureState::coalesceChanges(std::vector& tiles) { stateChanges.clear(); deletedStates.clear(); - if (changes.empty()) return; + if (changes.empty()) { + return; + } for (auto& tile : tiles) { tile.setFeatureState(changes); } } -void SourceFeatureState::removeState(const optional& sourceLayerID, const optional& featureID, const optional& stateKey) { +void SourceFeatureState::removeState(const optional& sourceLayerID, const optional& featureID, + const optional& stateKey) { std::string sourceLayer = sourceLayerID.value_or(std::string()); bool sourceLayerDeleted = (deletedStates.count(sourceLayer) > 0) && deletedStates[sourceLayer].empty(); - if (sourceLayerDeleted) return; + if (sourceLayerDeleted) { + return; + } if (stateKey && featureID) { if ((deletedStates.count(sourceLayer) == 0) && (deletedStates[sourceLayer].count(*featureID)) == 0) { deletedStates[sourceLayer][*featureID][*stateKey] = {}; } } else if (featureID) { - bool updateInQueue = stateChanges.count(sourceLayer) && stateChanges[sourceLayer].count(*featureID); + bool updateInQueue = + (stateChanges.count(sourceLayer) != 0U) && (stateChanges[sourceLayer].count(*featureID) != 0U); if (updateInQueue) { for (const auto& changeEntry : stateChanges[sourceLayer][*featureID]) { deletedStates[sourceLayer][*featureID][changeEntry.first] = {}; -- cgit v1.2.1