Skip to content

Commit

Permalink
Debounce new tile loading, use actor mutex for thread-safety
Browse files Browse the repository at this point in the history
  • Loading branch information
maurhofer-ubique authored and maerki committed Jan 24, 2024
1 parent 2074653 commit 50da5a7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
6 changes: 5 additions & 1 deletion shared/public/Tiled2dMapSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,9 @@ class Tiled2dMapSource :
void onVisibleTilesChanged(const std::vector<VisibleTilesLayer> &pyramid, int keepZoomLevelOffset = 0);

private:
void performLoadingTask(Tiled2dMapTileInfo tile, size_t loaderIndex);
void onStableTriggerNewTileLoading(uint64_t updateId);

void performLoadingTask(Tiled2dMapTileInfo tile, size_t loaderIndex);

void updateTileMasks();

Expand All @@ -181,11 +182,14 @@ class Tiled2dMapSource :

std::unordered_map<size_t, std::map<Tiled2dMapTileInfo, ErrorInfo>> errorTiles;
std::optional<long long> nextDelayTaskExecution;
std::vector<PrioritizedTiled2dMapTileInfo> pendingTilesToLoad;

std::unordered_set<Tiled2dMapTileInfo> notFoundTiles;

std::string layerName;

uint64_t currentNewTilesUpdateId = 0;
const static int64_t PENDING_NEW_TILE_DEBOUNCE_MS = 200;
};

#include "Tiled2dMapSourceImpl.h"
33 changes: 29 additions & 4 deletions shared/public/Tiled2dMapSourceImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -361,17 +361,42 @@ void Tiled2dMapSource<T, L, R>::onVisibleTilesChanged(const std::vector<VisibleT
}
}

std::sort(toAdd.begin(), toAdd.end());

for (const auto &addedTile : toAdd) {
performLoadingTask(addedTile.tileInfo, 0);
if (auto strongScheduler = scheduler.lock()) {
std::lock_guard<std::recursive_mutex> lock(mailbox->receivingMutex);
std::weak_ptr<Tiled2dMapSource> weakSelfPtr = std::dynamic_pointer_cast<Tiled2dMapSource>(
shared_from_this());
std::sort(toAdd.begin(), toAdd.end());
if (toAdd != pendingTilesToLoad) {
long newId = ++currentNewTilesUpdateId;
pendingTilesToLoad = toAdd;
strongScheduler->addTask(std::make_shared<LambdaTask>(
TaskConfig("DebounceNewTilesLoad_" + std::to_string(newId), PENDING_NEW_TILE_DEBOUNCE_MS, TaskPriority::NORMAL,
ExecutionEnvironment::COMPUTATION),
[weakSelfPtr, newId] {
if (auto strongSelf = weakSelfPtr.lock()) {
strongSelf->onStableTriggerNewTileLoading(newId);
}
}));
}
}

//if we removed tiles, we potentially need to update the tilemasks - also if no new tile is loaded
updateTileMasks();

notifyTilesUpdates();
}

template<class T, class L, class R>
void Tiled2dMapSource<T, L, R>::onStableTriggerNewTileLoading(uint64_t updateId) {
for (const auto &tileToLoad : pendingTilesToLoad) {
std::lock_guard<std::recursive_mutex> lock(mailbox->receivingMutex);
if (updateId != currentNewTilesUpdateId) {
break;
}
performLoadingTask(tileToLoad.tileInfo, 0);
}
}

template<class T, class L, class R>
void Tiled2dMapSource<T, L, R>::performLoadingTask(Tiled2dMapTileInfo tile, size_t loaderIndex) {
if (currentlyLoading.count(tile) != 0) return;
Expand Down

0 comments on commit 50da5a7

Please sign in to comment.