Skip to content

Commit

Permalink
Merge pull request #581 from openmobilemaps/bugfix/reload-stability
Browse files Browse the repository at this point in the history
Bugfix/reload stability
  • Loading branch information
maurhofer-ubique authored Jan 25, 2024
2 parents 88d6458 + 3601619 commit 06e4ed2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
4 changes: 4 additions & 0 deletions shared/public/Tiled2dMapVectorLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ class Tiled2dMapVectorLayer
void updateReadyStateListenerIfNeeded();
std::optional<LayerReadyState> lastReadyState;
std::shared_ptr<::Tiled2dMapReadyStateListener> readyStateListener;

std::mutex setupMutex;
std::condition_variable setupCV;
bool setupReady = false;
};


28 changes: 26 additions & 2 deletions shared/src/map/layers/tiled/vector/Tiled2dMapVectorLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,12 @@ void Tiled2dMapVectorLayer::initializeVectorLayer() {
rasterSource.message(&Tiled2dMapRasterSource::resume);
}
}

{
std::unique_lock<std::mutex> lock(setupMutex);
setupReady = true;
}
setupCV.notify_all();
}

void Tiled2dMapVectorLayer::reloadDataSource(const std::string &sourceName) {
Expand All @@ -492,6 +498,11 @@ void Tiled2dMapVectorLayer::reloadDataSource(const std::string &sourceName) {
}

void Tiled2dMapVectorLayer::reloadLocalDataSource(const std::string &sourceName, const std::string &geoJson) {

if (!mapInterface) {
return;
}

if (const auto &geoSource = mapDescription->geoJsonSources[sourceName]) {

nlohmann::json json;
Expand All @@ -511,6 +522,7 @@ void Tiled2dMapVectorLayer::reloadLocalDataSource(const std::string &sourceName,
});
}

prevCollisionStillValid.clear();
tilesStillValid.clear();
mapInterface->invalidate();
}
Expand Down Expand Up @@ -543,7 +555,7 @@ void Tiled2dMapVectorLayer::update() {
bool tilesChanged = !tilesStillValid.test_and_set();
double zoomChange = abs(newZoom-lastDataManagerZoom) / std::max(newZoom, 1.0);
double timeDiff = now - lastDataManagerUpdate;
if (zoomChange > 0.001 || timeDiff > 2000 || isAnimating || tilesChanged) {
if (zoomChange > 0.001 || timeDiff > 1000 || isAnimating || tilesChanged) {
lastDataManagerUpdate = now;
lastDataManagerZoom = newZoom;

Expand All @@ -558,7 +570,7 @@ void Tiled2dMapVectorLayer::update() {
newIsAnimating |= a;
}
isAnimating = newIsAnimating;
if (now - lastCollitionCheck > 3000 || tilesChanged) {
if (now - lastCollitionCheck > 1000 || tilesChanged) {
lastCollitionCheck = now;
bool enforceUpdate = !prevCollisionStillValid.test_and_set();
collisionManager.syncAccess(
Expand Down Expand Up @@ -754,6 +766,10 @@ void Tiled2dMapVectorLayer::forceReload() {
}

void Tiled2dMapVectorLayer::onTilesUpdated(const std::string &layerName, std::unordered_set<Tiled2dMapRasterTileInfo> currentTileInfos) {

std::unique_lock<std::mutex> lock(setupMutex);
setupCV.wait(lock, [this]{ return setupReady; });

auto sourceManager = sourceDataManagers.find(layerName);
if (sourceManager != sourceDataManagers.end()) {
sourceManager->second.message(MailboxDuplicationStrategy::replaceNewest, &Tiled2dMapVectorSourceTileDataManager::onRasterTilesUpdated, layerName, currentTileInfos);
Expand All @@ -762,6 +778,10 @@ void Tiled2dMapVectorLayer::onTilesUpdated(const std::string &layerName, std::un
}

void Tiled2dMapVectorLayer::onTilesUpdated(const std::string &sourceName, std::unordered_set<Tiled2dMapVectorTileInfo> currentTileInfos) {

std::unique_lock<std::mutex> lock(setupMutex);
setupCV.wait(lock, [this]{ return setupReady; });

auto sourceManager = sourceDataManagers.find(sourceName);
if (sourceManager != sourceDataManagers.end()) {
sourceManager->second.message(MailboxDuplicationStrategy::replaceNewest, &Tiled2dMapVectorSourceTileDataManager::onVectorTilesUpdated, sourceName, currentTileInfos);
Expand Down Expand Up @@ -1235,6 +1255,10 @@ std::optional<int32_t> Tiled2dMapVectorLayer::getMaxZoomLevelIdentifier() {

void Tiled2dMapVectorLayer::invalidateCollisionState() {
prevCollisionStillValid.clear();
tilesStillValid.clear();
if (mapInterface) {
mapInterface->invalidate();
}
}

void Tiled2dMapVectorLayer::setReadyStateListener(const /*not-null*/ std::shared_ptr<::Tiled2dMapReadyStateListener> & listener) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ void Tiled2dMapVectorSourceSymbolDataManager::onVectorTilesUpdated(const std::st
auto selfActor = WeakActor(mailbox, weak_from_this());
selfActor.messagePrecisely(MailboxDuplicationStrategy::replaceNewest, MailboxExecutionEnvironment::graphics,
&Tiled2dMapVectorSourceSymbolDataManager::updateSymbolGroups);
vectorLayer.message(&Tiled2dMapVectorLayer::invalidateCollisionState);
}

std::vector<Actor<Tiled2dMapVectorSymbolGroup>> Tiled2dMapVectorSourceSymbolDataManager::createSymbolGroups(const Tiled2dMapVersionedTileInfo &tileInfo,
Expand Down Expand Up @@ -584,6 +585,7 @@ void Tiled2dMapVectorSourceSymbolDataManager::updateSymbolGroups() {
}

pregenerateRenderPasses();
vectorLayer.message(&Tiled2dMapVectorLayer::invalidateCollisionState);
}

void Tiled2dMapVectorSourceSymbolDataManager::setSprites(std::shared_ptr<SpriteData> spriteData, std::shared_ptr<TextureHolderInterface> spriteTexture) {
Expand Down

0 comments on commit 06e4ed2

Please sign in to comment.