| 1 | // Copyright 2025 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/Assets/CustomAssetLoader.h" |
| 5 | |
| 6 | #include <fmt/format.h> |
| 7 | |
| 8 | #include "Common/Logging/Log.h" |
| 9 | #include "Common/Thread.h" |
| 10 | |
| 11 | #include "UICommon/UICommon.h" |
| 12 | |
| 13 | namespace VideoCommon |
| 14 | { |
| 15 | void CustomAssetLoader::Initialize() |
| 16 | { |
| 17 | ResizeWorkerThreads(2); |
| 18 | } |
| 19 | |
| 20 | void CustomAssetLoader::Shutdown() |
| 21 | { |
| 22 | Reset(false); |
| 23 | } |
| 24 | |
| 25 | bool CustomAssetLoader::StartWorkerThreads(u32 num_worker_threads) |
| 26 | { |
| 27 | for (u32 i = 0; i < num_worker_threads; i++) |
| 28 | { |
| 29 | m_worker_threads.emplace_back(&CustomAssetLoader::WorkerThreadRun, this, i); |
| 30 | } |
| 31 | |
| 32 | return HasWorkerThreads(); |
| 33 | } |
| 34 | |
| 35 | bool CustomAssetLoader::ResizeWorkerThreads(u32 num_worker_threads) |
| 36 | { |
| 37 | if (m_worker_threads.size() == num_worker_threads) |
| 38 | return true; |
| 39 | |
| 40 | StopWorkerThreads(); |
| 41 | return StartWorkerThreads(num_worker_threads); |
| 42 | } |
| 43 | |
| 44 | bool CustomAssetLoader::HasWorkerThreads() const |
| 45 | { |
| 46 | return !m_worker_threads.empty(); |
| 47 | } |
| 48 | |
| 49 | void CustomAssetLoader::StopWorkerThreads() |
| 50 | { |
| 51 | if (!HasWorkerThreads()) |
| 52 | return; |
| 53 | |
| 54 | // Signal worker threads to stop, and wake all of them. |
| 55 | { |
| 56 | std::lock_guard guard(m_assets_to_load_lock); |
| 57 | m_exit_flag.Set(); |
| 58 | m_worker_thread_wake.notify_all(); |
| 59 | } |
| 60 | |
| 61 | // Wait for worker threads to exit. |
| 62 | for (std::thread& thr : m_worker_threads) |
| 63 | thr.join(); |
| 64 | m_worker_threads.clear(); |
| 65 | m_exit_flag.Clear(); |
| 66 | } |
| 67 | |
| 68 | void CustomAssetLoader::WorkerThreadRun(u32 thread_index) |
| 69 | { |
| 70 | Common::SetCurrentThreadName(fmt::format("Asset Loader {}", thread_index).c_str()); |
| 71 | |
| 72 | std::unique_lock load_lock(m_assets_to_load_lock); |
| 73 | while (true) |
| 74 | { |
| 75 | m_worker_thread_wake.wait(load_lock, |
| 76 | [&] { return !m_assets_to_load.empty() || m_exit_flag.IsSet(); }); |
| 77 | |
| 78 | if (m_exit_flag.IsSet()) |
| 79 | return; |
| 80 | |
| 81 | // If more memory than allowed has already been loaded, we will load nothing more |
| 82 | // until the next ScheduleAssetsToLoad from Manager. |
| 83 | if (m_change_in_memory > m_allowed_memory) |
| 84 | { |
| 85 | m_assets_to_load.clear(); |
| 86 | continue; |
| 87 | } |
| 88 | |
| 89 | auto* const item = m_assets_to_load.front(); |
| 90 | m_assets_to_load.pop_front(); |
| 91 | |
| 92 | // Make sure another thread isn't loading this handle. |
| 93 | if (!m_handles_in_progress.insert(item->GetHandle()).second) |
| 94 | continue; |
| 95 | |
| 96 | load_lock.unlock(); |
| 97 | |
| 98 | // Unload previously loaded asset. |
| 99 | m_change_in_memory -= item->Unload(); |
| 100 | |
| 101 | const std::size_t bytes_loaded = item->Load(); |
| 102 | m_change_in_memory += s64(bytes_loaded); |
| 103 | |
| 104 | load_lock.lock(); |
| 105 | |
| 106 | { |
| 107 | INFO_LOG_FMT(VIDEO, "CustomAssetLoader thread {} loaded: {} ({})", thread_index, |
| 108 | item->GetAssetId(), UICommon::FormatSize(bytes_loaded)); |
| 109 | |
| 110 | std::lock_guard lk{m_assets_loaded_lock}; |
| 111 | m_asset_handles_loaded.emplace_back(item->GetHandle(), bytes_loaded > 0); |
| 112 | |
| 113 | // Make sure no other threads try to re-process this item. |
| 114 | // Manager will take the handles and re-ScheduleAssetsToLoad based on timestamps if needed. |
| 115 | std::erase(m_assets_to_load, item); |
| 116 | } |
| 117 | |
| 118 | m_handles_in_progress.erase(item->GetHandle()); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | auto CustomAssetLoader::TakeLoadResults() -> LoadResults |
| 123 | { |
| 124 | std::lock_guard guard(m_assets_loaded_lock); |
| 125 | return {std::move(m_asset_handles_loaded), m_change_in_memory.exchange(0)}; |
| 126 | } |
| 127 | |
| 128 | void CustomAssetLoader::ScheduleAssetsToLoad(std::list<CustomAsset*> assets_to_load, |
| 129 | u64 allowed_memory) |
| 130 | { |
| 131 | if (assets_to_load.empty()) [[unlikely]] |
| 132 | return; |
| 133 | |
| 134 | // There's new assets to process, notify worker threads |
| 135 | std::lock_guard guard(m_assets_to_load_lock); |
| 136 | m_allowed_memory = allowed_memory; |
| 137 | m_assets_to_load = std::move(assets_to_load); |
| 138 | m_worker_thread_wake.notify_all(); |
| 139 | } |
| 140 | |
| 141 | void CustomAssetLoader::Reset(bool restart_worker_threads) |
| 142 | { |
| 143 | const std::size_t worker_thread_count = m_worker_threads.size(); |
| 144 | StopWorkerThreads(); |
| 145 | |
| 146 | m_assets_to_load.clear(); |
| 147 | m_asset_handles_loaded.clear(); |
| 148 | m_allowed_memory = 0; |
| 149 | m_change_in_memory = 0; |
| 150 | |
| 151 | if (restart_worker_threads) |
| 152 | { |
| 153 | StartWorkerThreads(static_cast<u32>(worker_thread_count)); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | } // namespace VideoCommon |