From c52ce231563227efe64cf2373eec16525e91e81b Mon Sep 17 00:00:00 2001 From: Emil Axelsson Date: Wed, 15 Nov 2017 22:53:04 +0100 Subject: [PATCH] Asset management --- include/openspace/scene/assetmanager.h | 6 +- .../openspace/util/resourcesynchronization.h | 2 + src/scene/assetmanager.cpp | 93 ++++++++++++------- src/scene/assetsynchronizer.cpp | 23 ++++- src/util/resourcesynchronization.cpp | 11 ++- 5 files changed, 95 insertions(+), 40 deletions(-) diff --git a/include/openspace/scene/assetmanager.h b/include/openspace/scene/assetmanager.h index 4c820a012d..ab4c46a782 100644 --- a/include/openspace/scene/assetmanager.h +++ b/include/openspace/scene/assetmanager.h @@ -89,12 +89,16 @@ private: std::shared_ptr tryLoadAsset(const std::string& path); void unloadAsset(Asset* asset); bool tryInitializeAsset(Asset& asset); + bool tryDeinitializeAsset(Asset& asset); + void startSynchronization(Asset& asset); + void cancelSynchronization(Asset& asset); std::vector _managedAssets; std::unordered_map _pendingStateChangeCommands; - std::unordered_map _stateChangesInProgress; + //std::unordered_map _stateChangesInProgress; + std::unordered_set _pendingInitializations; std::unordered_map> _syncAncestors; std::unordered_map> _syncDependencies; diff --git a/include/openspace/util/resourcesynchronization.h b/include/openspace/util/resourcesynchronization.h index 5e3500c9d1..abaad59476 100644 --- a/include/openspace/util/resourcesynchronization.h +++ b/include/openspace/util/resourcesynchronization.h @@ -32,6 +32,8 @@ #include +#include + namespace openspace { class ResourceSynchronization diff --git a/src/scene/assetmanager.cpp b/src/scene/assetmanager.cpp index 43c54114ee..ebc56e0a8a 100644 --- a/src/scene/assetmanager.cpp +++ b/src/scene/assetmanager.cpp @@ -57,7 +57,7 @@ bool AssetManager::update() { } } - // Start/cancel synchronizations + // Start/cancel synchronizations and/or deinitialize for (const auto& c : _pendingStateChangeCommands) { const std::string& path = c.first; const AssetState targetState = c.second; @@ -67,48 +67,64 @@ bool AssetManager::update() { continue; } - AssetSynchronizer::SynchronizationState state = + AssetSynchronizer::SynchronizationState currentSyncState = _assetSynchronizer->assetState(asset.get()); - const bool alreadySyncedOrSyncing = - (state == AssetSynchronizer::SynchronizationState::Resolved || - state == AssetSynchronizer::SynchronizationState::Synchronizing); + const bool syncedOrSyncing = + (currentSyncState == AssetSynchronizer::SynchronizationState::Resolved || + currentSyncState == AssetSynchronizer::SynchronizationState::Synchronizing); - const bool shouldSync = + const bool shouldBeSynced = (targetState == AssetState::Synchronized || targetState == AssetState::Initialized); - if (shouldSync && !alreadySyncedOrSyncing) { - startSynchronization(asset); + + if (shouldBeSynced && !syncedOrSyncing) { + startSynchronization(*asset); + } else if (!shouldBeSynced && syncedOrSyncing) { + cancelSynchronization(*asset); + } + + const bool shouldBeInitialized = targetState == AssetState::Initialized; + const bool isInitialized = asset->readyState() == Asset::ReadyState::Initialized; + + if (shouldBeInitialized && !isInitialized) { + _pendingInitializations.insert(asset.get()); + } else { + _pendingInitializations.erase(asset.get()); + tryDeinitializeAsset(*asset); } } + std::vector stateChanges = + _assetSynchronizer->getStateChanges(); - - - - // Start synchronizations - /*for (const auto& loadedAsset : loadedAssets) { - const std::string& path = loadedAsset.first; - Asset* asset = loadedAsset.second.get(); - const AssetState targetState = _pendingStateChangeCommands[path]; - updateSyncState(asset, targetState); - }*/ - - // Unload assets - - - // Collect assets that were resolved and rejected. - // Initialize if requested. - // Update current state accordingly. - /*for (const auto& stateChange : _assetSynchronizer->getStateChanges()) { - handleSyncStateChange(stateChange); - }*/ + for (AssetSynchronizer::StateChange& stateChange : stateChanges) { + Asset* a = stateChange.asset.get(); + auto it = _pendingInitializations.find(a); + if (it == _pendingInitializations.end()) { + continue; + } + Asset::ReadyState currentState = a->readyState(); + if (currentState != Asset::ReadyState::Initialized) + { + _pendingInitializations.erase(it); + tryInitializeAsset(*a); + } + } _pendingStateChangeCommands.clear(); return false; } +void AssetManager::startSynchronization(Asset&) { + // todo: implement +} + +void AssetManager::cancelSynchronization(Asset&) { + // todo: implement this +} + /** * Load or unload asset depening on target state * Return shared pointer to asset if this loads the asset @@ -243,8 +259,8 @@ AssetManager::AssetState AssetManager::currentAssetState(const std::string& asse void AssetManager::clearAllTargetAssets() { _pendingStateChangeCommands.clear(); - for (const auto& i : _assetLoader->loadedAssets()) { - _pendingStateChangeCommands[i->id()] = AssetState::Unloaded; + for (const auto& ma : _managedAssets) { + _pendingStateChangeCommands[ma.path] = AssetState::Unloaded; } } @@ -296,7 +312,7 @@ scripting::LuaLibrary AssetManager::luaLibrary() { } bool AssetManager::isDone() { - return _stateChangesInProgress.size() == 0; + return _pendingStateChangeCommands.size() == 0 && _pendingInitializations.size() == 0; } void AssetManager::unloadAsset(Asset* asset) { @@ -340,12 +356,21 @@ std::shared_ptr AssetManager::tryLoadAsset(const std::string& path) { bool AssetManager::tryInitializeAsset(Asset& asset) { try { asset.initialize(); - return true; } catch (const ghoul::RuntimeError& e) { - LERROR(e.component << ": " << e.message); + LERROR("Error when initializing asset. " << e.component << ": " << e.message); return false; } + return true; +} + +bool AssetManager::tryDeinitializeAsset(Asset& asset) { + try { + asset.deinitialize(); + } catch (const ghoul::RuntimeError& e) { + LERROR("Error when deinitializing asset. " << e.component << ": " << e.message); + return false; + } + return true; } - } diff --git a/src/scene/assetsynchronizer.cpp b/src/scene/assetsynchronizer.cpp index a9efe1eed1..191c10e1ae 100644 --- a/src/scene/assetsynchronizer.cpp +++ b/src/scene/assetsynchronizer.cpp @@ -160,9 +160,30 @@ std::vector AssetSynchronizer::getStateChanges() } AssetSynchronizer::SynchronizationState AssetSynchronizer::assetState(Asset*) { - return SynchronizationState::Unknown; + return SynchronizationState::Unsynced; } +void AssetSynchronizer::startAssetResourceSync( + std::shared_ptr a, + std::shared_ptr rs) +{ + // Todo: implement this +} + +void AssetSynchronizer::cancelAssetResourceSync( + std::shared_ptr a, + std::shared_ptr rs) +{ + // Todo: implement this +} + + +void AssetSynchronizer::setState( + std::shared_ptr a, + AssetSynchronizer::SynchronizationState state) +{ + // Todo: implement this +} /* bool AssetSynchronizer::assetIsSynchronized(Asset * asset) { diff --git a/src/util/resourcesynchronization.cpp b/src/util/resourcesynchronization.cpp index a95729ef58..a32a3a8fc7 100644 --- a/src/util/resourcesynchronization.cpp +++ b/src/util/resourcesynchronization.cpp @@ -139,10 +139,13 @@ void ResourceSynchronization::begin() { void ResourceSynchronization::setState(State state) { _state = state; _callbackMutex.lock(); - std::vector callbacks( - _stateChangeCallbacks.begin(), - _stateChangeCallbacks.end() - ); + + + std::vector callbacks; + callbacks.reserve(_stateChangeCallbacks.size()); + for (const auto& it : _stateChangeCallbacks) { + callbacks.push_back(it.second); + } _callbackMutex.unlock(); for (auto& cb : callbacks) { cb(state);