From 9c9a37c642f045995b653b05e7915fae9e6a2434 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20M=C3=A4rki?= Date: Sat, 13 Jan 2024 14:01:35 +0100 Subject: [PATCH] ignore animations that are only cached symbols --- shared/public/Tiled2dMapSource.h | 2 +- shared/public/Tiled2dMapSourceImpl.h | 5 ++++- .../tiled/vector/Tiled2dMapVectorLayer.cpp | 6 ++++-- .../symbol/SymbolAnimationCoordinator.h | 21 ++++++++++++++++++- ...iled2dMapVectorSourceSymbolDataManager.cpp | 12 +++++++++++ .../symbol/Tiled2dMapVectorSymbolGroup.cpp | 6 ++++++ .../symbol/Tiled2dMapVectorSymbolGroup.h | 2 ++ .../symbol/Tiled2dMapVectorSymbolObject.cpp | 7 +++++++ .../symbol/Tiled2dMapVectorSymbolObject.h | 2 ++ 9 files changed, 58 insertions(+), 5 deletions(-) diff --git a/shared/public/Tiled2dMapSource.h b/shared/public/Tiled2dMapSource.h index 783609a5d..b5ef4f085 100644 --- a/shared/public/Tiled2dMapSource.h +++ b/shared/public/Tiled2dMapSource.h @@ -159,7 +159,7 @@ class Tiled2dMapSource : float screenDensityPpi; std::set readyTiles; - size_t lastVisibleTilesHash; + size_t lastVisibleTilesHash = -1; void onVisibleTilesChanged(const std::vector &pyramid, int keepZoomLevelOffset = 0); diff --git a/shared/public/Tiled2dMapSourceImpl.h b/shared/public/Tiled2dMapSourceImpl.h index 1a497829b..83ff8fdcc 100644 --- a/shared/public/Tiled2dMapSourceImpl.h +++ b/shared/public/Tiled2dMapSourceImpl.h @@ -78,7 +78,10 @@ void Tiled2dMapSource::onVisibleBoundsChanged(const ::RectCoord &visibl if (!zoomInfo.underzoom && (zoomLevelInfos.empty() || zoomLevelInfos[0].zoom * zoomInfo.zoomLevelScaleFactor * screenScaleFactor < zoom) && (zoomLevelInfos.empty() || zoomLevelInfos[0].zoomLevelIdentifier != 0)) { // enable underzoom if the first zoomLevel is zoomLevelIdentifier == 0 - onVisibleTilesChanged({}); + if (lastVisibleTilesHash != 0) { + lastVisibleTilesHash = 0; + onVisibleTilesChanged({}); + } return; } diff --git a/shared/src/map/layers/tiled/vector/Tiled2dMapVectorLayer.cpp b/shared/src/map/layers/tiled/vector/Tiled2dMapVectorLayer.cpp index 09dbe7115..8e5dbb3f6 100644 --- a/shared/src/map/layers/tiled/vector/Tiled2dMapVectorLayer.cpp +++ b/shared/src/map/layers/tiled/vector/Tiled2dMapVectorLayer.cpp @@ -541,7 +541,9 @@ void Tiled2dMapVectorLayer::update() { auto now = DateHelper::currentTimeMillis(); bool newIsAnimating = false; bool tilesChanged = !tilesStillValid.test_and_set(); - if (abs(newZoom-lastDataManagerZoom) / std::max(newZoom, 1.0) > 0.001 || now - lastDataManagerUpdate > 1000 || isAnimating || tilesChanged) { + double zoomChange = abs(newZoom-lastDataManagerZoom) / std::max(newZoom, 1.0); + double timeDiff = now - lastDataManagerUpdate; + if (zoomChange > 0.001 || timeDiff > 2000 || isAnimating || tilesChanged) { lastDataManagerUpdate = now; lastDataManagerZoom = newZoom; @@ -556,7 +558,7 @@ void Tiled2dMapVectorLayer::update() { newIsAnimating |= a; } isAnimating = newIsAnimating; - if (now - lastCollitionCheck > 2000 || tilesChanged) { + if (now - lastCollitionCheck > 3000 || tilesChanged) { lastCollitionCheck = now; bool enforceUpdate = !prevCollisionStillValid.test_and_set(); collisionManager.syncAccess([&vpMatrix, &viewportSize, viewportRotation, enforceUpdate](const auto &manager) { diff --git a/shared/src/map/layers/tiled/vector/symbol/SymbolAnimationCoordinator.h b/shared/src/map/layers/tiled/vector/symbol/SymbolAnimationCoordinator.h index 79455e9a6..e1358dc56 100644 --- a/shared/src/map/layers/tiled/vector/symbol/SymbolAnimationCoordinator.h +++ b/shared/src/map/layers/tiled/vector/symbol/SymbolAnimationCoordinator.h @@ -52,7 +52,17 @@ class SymbolAnimationCoordinator { } bool isAnimating() { - return isIconAnimating() || isStretchIconAnimating() || isTextAnimating(); + if (cacheCount >= usageCount) { + return false; + + } + if (isIconAnimating() || isStretchIconAnimating() || isTextAnimating()) { + // printf("Animate %p: %d, %d, %d, %d: %f|%f\n", static_cast(this), isIconAnimating(), isStretchIconAnimating(), isTextAnimating(), isUsed(), coordinate.x, coordinate.y); + return true; + } + else { + return false; + } } bool isIconAnimating() { @@ -83,6 +93,14 @@ class SymbolAnimationCoordinator { return --usageCount; } + int increaseCache() {; + return ++cacheCount; + } + + int decreaseCache() { + return --cacheCount; + } + std::atomic_flag isOwned = ATOMIC_FLAG_INIT; // returns true if the value was changed @@ -112,6 +130,7 @@ class SymbolAnimationCoordinator { float lastTextAlpha = 0; std::atomic_int usageCount = 0; + std::atomic_int cacheCount = 0; bool collides = true; diff --git a/shared/src/map/layers/tiled/vector/symbol/Tiled2dMapVectorSourceSymbolDataManager.cpp b/shared/src/map/layers/tiled/vector/symbol/Tiled2dMapVectorSourceSymbolDataManager.cpp index 3b293c5d6..b3ab657a6 100644 --- a/shared/src/map/layers/tiled/vector/symbol/Tiled2dMapVectorSourceSymbolDataManager.cpp +++ b/shared/src/map/layers/tiled/vector/symbol/Tiled2dMapVectorSourceSymbolDataManager.cpp @@ -530,6 +530,18 @@ void Tiled2dMapVectorSourceSymbolDataManager::updateSymbolGroups() { if (tileStateIt == tileStateMap.end()) { tileStateMap[tile] = state; } else { + if (tileStateIt->second == TileState::CACHED && state != TileState::CACHED) { + auto tileSymbolGroupMapIt = tileSymbolGroupMap.find(tile); + if (tileSymbolGroupMapIt != tileSymbolGroupMap.end()) { + for (const auto &[layerIdentifier, symbolGroups]: tileSymbolGroupMapIt->second) { + for (auto &symbolGroup: std::get<1>(symbolGroups)) { + symbolGroup.syncAccess([&](auto group) { + group->removeFromCache(); + }); + } + } + } + } tileStateIt->second = state; } if (state == TileState::CACHED) { diff --git a/shared/src/map/layers/tiled/vector/symbol/Tiled2dMapVectorSymbolGroup.cpp b/shared/src/map/layers/tiled/vector/symbol/Tiled2dMapVectorSymbolGroup.cpp index 8717b546b..bf6633e20 100644 --- a/shared/src/map/layers/tiled/vector/symbol/Tiled2dMapVectorSymbolGroup.cpp +++ b/shared/src/map/layers/tiled/vector/symbol/Tiled2dMapVectorSymbolGroup.cpp @@ -841,6 +841,12 @@ void Tiled2dMapVectorSymbolGroup::placedInCache() { } } +void Tiled2dMapVectorSymbolGroup::removeFromCache() { + for (auto const object: symbolObjects) { + object->removeFromCache(); + } +} + std::vector> Tiled2dMapVectorSymbolGroup::getRenderObjects() { std::vector> renderObjects; diff --git a/shared/src/map/layers/tiled/vector/symbol/Tiled2dMapVectorSymbolGroup.h b/shared/src/map/layers/tiled/vector/symbol/Tiled2dMapVectorSymbolGroup.h index 34cbd6019..b37bf52b3 100644 --- a/shared/src/map/layers/tiled/vector/symbol/Tiled2dMapVectorSymbolGroup.h +++ b/shared/src/map/layers/tiled/vector/symbol/Tiled2dMapVectorSymbolGroup.h @@ -65,6 +65,8 @@ class Tiled2dMapVectorSymbolGroup : public ActorObject, public std::enable_share void placedInCache(); + void removeFromCache(); + void clear(); void updateLayerDescription(const std::shared_ptr layerDescription); diff --git a/shared/src/map/layers/tiled/vector/symbol/Tiled2dMapVectorSymbolObject.cpp b/shared/src/map/layers/tiled/vector/symbol/Tiled2dMapVectorSymbolObject.cpp index c1084d85e..98e7e3300 100644 --- a/shared/src/map/layers/tiled/vector/symbol/Tiled2dMapVectorSymbolObject.cpp +++ b/shared/src/map/layers/tiled/vector/symbol/Tiled2dMapVectorSymbolObject.cpp @@ -177,6 +177,13 @@ void Tiled2dMapVectorSymbolObject::placedInCache() { animationCoordinator->isOwned.clear(); isCoordinateOwner = false; } + animationCoordinator->increaseCache(); + } +} + +void Tiled2dMapVectorSymbolObject::removeFromCache() { + if (animationCoordinator) { + animationCoordinator->decreaseCache(); } } diff --git a/shared/src/map/layers/tiled/vector/symbol/Tiled2dMapVectorSymbolObject.h b/shared/src/map/layers/tiled/vector/symbol/Tiled2dMapVectorSymbolObject.h index 3eaf269bc..4c25851f7 100644 --- a/shared/src/map/layers/tiled/vector/symbol/Tiled2dMapVectorSymbolObject.h +++ b/shared/src/map/layers/tiled/vector/symbol/Tiled2dMapVectorSymbolObject.h @@ -56,6 +56,8 @@ class Tiled2dMapVectorSymbolObject { void placedInCache(); + void removeFromCache(); + struct SymbolObjectInstanceCounts { int icons, textCharacters, stretchedIcons; }; const SymbolObjectInstanceCounts getInstanceCounts() const;