| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <list> |
| 7 |
#include <map> |
| 8 |
#include <memory> |
| 9 |
#include <set> |
| 10 |
#include <vector> |
| 11 |
|
| 12 |
#include "Common/CommonTypes.h" |
| 13 |
|
| 14 |
#include "VideoCommon/Assets/AssetListener.h" |
| 15 |
#include "VideoCommon/Assets/CustomAsset.h" |
| 16 |
#include "VideoCommon/Assets/CustomAssetLibrary.h" |
| 17 |
#include "VideoCommon/Assets/CustomAssetLoader.h" |
| 18 |
|
| 19 |
namespace VideoCommon |
| 20 |
{ |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
class CustomAssetCache |
| 27 |
{ |
| 28 |
public: |
| 29 |
|
| 30 |
|
| 31 |
struct AssetData |
| 32 |
{ |
| 33 |
std::unique_ptr<CustomAsset> asset; |
| 34 |
|
| 35 |
std::vector<AssetListener*> listeners; |
| 36 |
|
| 37 |
CustomAsset::TimeType load_request_time = {}; |
| 38 |
bool has_load_error = false; |
| 39 |
|
| 40 |
enum class LoadStatus |
| 41 |
{ |
| 42 |
PendingReload, |
| 43 |
LoadFinished, |
| 44 |
Unloaded, |
| 45 |
}; |
| 46 |
LoadStatus load_status = LoadStatus::PendingReload; |
| 47 |
}; |
| 48 |
|
| 49 |
template <typename T> |
| 50 |
T* CreateAsset(const CustomAssetLibrary::AssetID& asset_id, |
| 51 |
std::shared_ptr<VideoCommon::CustomAssetLibrary> library, AssetListener* listener) |
| 52 |
{ |
| 53 |
const auto [it, added] = |
| 54 |
m_asset_id_to_handle.try_emplace(asset_id, m_asset_handle_to_data.size()); |
| 55 |
if (added) |
| 56 |
{ |
| 57 |
AssetData asset_data; |
| 58 |
asset_data.asset = std::make_unique<T>(std::move(library), asset_id, it->second); |
| 59 |
asset_data.load_request_time = {}; |
| 60 |
asset_data.has_load_error = false; |
| 61 |
|
| 62 |
m_asset_handle_to_data.insert_or_assign(it->second, std::move(asset_data)); |
| 63 |
} |
| 64 |
|
| 65 |
auto& asset_data_from_handle = m_asset_handle_to_data[it->second]; |
| 66 |
asset_data_from_handle.listeners.push_back(listener); |
| 67 |
asset_data_from_handle.load_status = AssetData::LoadStatus::PendingReload; |
| 68 |
|
| 69 |
return static_cast<T*>(asset_data_from_handle.asset.get()); |
| 70 |
} |
| 71 |
|
| 72 |
AssetData* GetAssetData(const CustomAssetLibrary::AssetID& asset_id) |
| 73 |
{ |
| 74 |
const auto it_handle = m_asset_id_to_handle.find(asset_id); |
| 75 |
if (it_handle == m_asset_id_to_handle.end()) |
| 76 |
return nullptr; |
| 77 |
return &m_asset_handle_to_data[it_handle->second]; |
| 78 |
} |
| 79 |
|
| 80 |
void Initialize(); |
| 81 |
void Shutdown(); |
| 82 |
|
| 83 |
void Reset(); |
| 84 |
|
| 85 |
|
| 86 |
void MarkAssetDirty(const CustomAssetLibrary::AssetID& asset_id); |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
void MarkAssetPending(CustomAsset* asset); |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
void MarkAssetActive(CustomAsset* asset); |
| 95 |
|
| 96 |
void Update(); |
| 97 |
|
| 98 |
private: |
| 99 |
void ProcessDirtyAssets(); |
| 100 |
void ProcessLoadedAssets(); |
| 101 |
void RemoveAssetsUntilBelowMemoryLimit(); |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
class AssetPriorityQueue |
| 107 |
{ |
| 108 |
public: |
| 109 |
const auto& Elements() const { return m_assets; } |
| 110 |
|
| 111 |
|
| 112 |
void MakeAssetHighestPriority(u64 asset_handle, CustomAsset* asset) |
| 113 |
{ |
| 114 |
RemoveAsset(asset_handle); |
| 115 |
m_assets.push_front(asset); |
| 116 |
|
| 117 |
|
| 118 |
if (asset_handle >= m_iterator_lookup.size()) |
| 119 |
m_iterator_lookup.resize(asset_handle + 1, m_assets.end()); |
| 120 |
|
| 121 |
m_iterator_lookup[asset_handle] = m_assets.begin(); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
void InsertAsset(u64 asset_handle, CustomAsset* asset) |
| 127 |
{ |
| 128 |
if (asset_handle >= m_iterator_lookup.size()) |
| 129 |
m_iterator_lookup.resize(asset_handle + 1, m_assets.end()); |
| 130 |
|
| 131 |
if (m_iterator_lookup[asset_handle] == m_assets.end()) |
| 132 |
{ |
| 133 |
m_assets.push_back(asset); |
| 134 |
m_iterator_lookup[asset_handle] = std::prev(m_assets.end()); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
CustomAsset* RemoveLowestPriorityAsset() |
| 139 |
{ |
| 140 |
if (m_assets.empty()) [[unlikely]] |
| 141 |
return nullptr; |
| 142 |
auto* const ret = m_assets.back(); |
| 143 |
if (ret != nullptr) |
| 144 |
{ |
| 145 |
m_iterator_lookup[ret->GetHandle()] = m_assets.end(); |
| 146 |
} |
| 147 |
m_assets.pop_back(); |
| 148 |
return ret; |
| 149 |
} |
| 150 |
|
| 151 |
void RemoveAsset(u64 asset_handle) |
| 152 |
{ |
| 153 |
if (asset_handle >= m_iterator_lookup.size()) |
| 154 |
return; |
| 155 |
|
| 156 |
const auto iter = m_iterator_lookup[asset_handle]; |
| 157 |
if (iter != m_assets.end()) |
| 158 |
{ |
| 159 |
m_assets.erase(iter); |
| 160 |
m_iterator_lookup[asset_handle] = m_assets.end(); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
bool IsEmpty() const { return m_assets.empty(); } |
| 165 |
|
| 166 |
std::size_t Size() const { return m_assets.size(); } |
| 167 |
|
| 168 |
private: |
| 169 |
std::list<CustomAsset*> m_assets; |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
std::vector<decltype(m_assets)::iterator> m_iterator_lookup; |
| 174 |
}; |
| 175 |
|
| 176 |
|
| 177 |
AssetPriorityQueue m_active_assets; |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
AssetPriorityQueue m_pending_assets; |
| 183 |
|
| 184 |
std::map<std::size_t, AssetData> m_asset_handle_to_data; |
| 185 |
std::map<CustomAssetLibrary::AssetID, std::size_t> m_asset_id_to_handle; |
| 186 |
|
| 187 |
|
| 188 |
u64 m_ram_used = 0; |
| 189 |
|
| 190 |
|
| 191 |
u64 m_max_ram_available = 0; |
| 192 |
|
| 193 |
std::mutex m_dirty_mutex; |
| 194 |
std::set<CustomAssetLibrary::AssetID> m_dirty_assets; |
| 195 |
|
| 196 |
CustomAssetLoader m_asset_loader; |
| 197 |
}; |
| 198 |
|
| 199 |
} |