Debian Codegraph @

Source/Core/VideoCommon/Assets/CustomAssetLoader.h

raw symbols SCIP
1 // Copyright 2025 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <atomic>
7 #include <condition_variable>
8 #include <list>
9 #include <mutex>
10 #include <set>
11 #include <thread>
12 #include <vector>
13
14 #include "Common/Flag.h"
15 #include "VideoCommon/Assets/CustomAsset.h"
16
17 namespace VideoCommon
18 {
19 // This class takes any number of assets
20 // and loads them across a configurable
21 // thread pool
22 class CustomAssetLoader
23 {
24 public:
25 CustomAssetLoader() = default;
26 ~CustomAssetLoader() = default;
27 CustomAssetLoader(const CustomAssetLoader&) = delete;
28 CustomAssetLoader(CustomAssetLoader&&) = delete;
29 CustomAssetLoader& operator=(const CustomAssetLoader&) = delete;
30 CustomAssetLoader& operator=(CustomAssetLoader&&) = delete;
31
32 void Initialize();
33 void Shutdown();
34
35 using AssetHandle = std::pair<std::size_t, bool>;
36 struct LoadResults
37
38 {
39 std::vector<AssetHandle> asset_handles;
40 s64 change_in_memory;
41 };
42
43 // Returns a vector of loaded asset handle / loaded result pairs
44 // and the change in memory.
45 LoadResults TakeLoadResults();
46
47 // Schedule assets to load on the worker threads
48 // and set how much memory is available for loading these additional assets.
49 void ScheduleAssetsToLoad(std::list<CustomAsset*> assets_to_load, u64 allowed_memory);
50
51 void Reset(bool restart_worker_threads = true);
52
53 private:
54 bool StartWorkerThreads(u32 num_worker_threads);
55 bool ResizeWorkerThreads(u32 num_worker_threads);
56 bool HasWorkerThreads() const;
57 void StopWorkerThreads();
58
59 void WorkerThreadRun(u32 thread_index);
60
61 Common::Flag m_exit_flag;
62
63 std::vector<std::thread> m_worker_threads;
64
65 std::mutex m_assets_to_load_lock;
66 std::list<CustomAsset*> m_assets_to_load;
67
68 std::condition_variable m_worker_thread_wake;
69
70 std::vector<AssetHandle> m_asset_handles_loaded;
71
72 // Memory available to load new assets.
73 s64 m_allowed_memory = 0;
74
75 // Change in memory from just-loaded/unloaded asset results yet to be taken by the Manager.
76 std::atomic<s64> m_change_in_memory = 0;
77
78 std::mutex m_assets_loaded_lock;
79
80 std::set<std::size_t> m_handles_in_progress;
81 };
82 } // namespace VideoCommon