| 1 |
|
| 2 |
|
| 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 |
|
| 20 |
|
| 21 |
|
| 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 |
|
| 44 |
|
| 45 |
LoadResults TakeLoadResults(); |
| 46 |
|
| 47 |
|
| 48 |
|
| 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 |
|
| 73 |
s64 m_allowed_memory = 0; |
| 74 |
|
| 75 |
|
| 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 |
} |