Debian Codegraph @

Source/Core/VideoCommon/Assets/CustomAsset.h

raw symbols SCIP
1 // Copyright 2023 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include "Common/CommonTypes.h"
7 #include "VideoCommon/Assets/CustomAssetLibrary.h"
8
9 #include <atomic>
10 #include <memory>
11 #include <mutex>
12
13 namespace VideoCommon
14 {
15 // An abstract class that provides operations for loading
16 // data from a 'CustomAssetLibrary'
17 class CustomAsset
18 {
19 public:
20 using ClockType = std::chrono::steady_clock;
21 using TimeType = ClockType::time_point;
22
23 CustomAsset(std::shared_ptr<CustomAssetLibrary> library, CustomAssetLibrary::AssetID asset_id,
24 u64 session_id);
25 virtual ~CustomAsset() = default;
26 CustomAsset(const CustomAsset&) = delete;
27 CustomAsset(CustomAsset&&) = delete;
28 CustomAsset& operator=(const CustomAsset&) = delete;
29 CustomAsset& operator=(CustomAsset&&) = delete;
30
31 // Loads the asset from the library returning the number of bytes loaded
32 std::size_t Load();
33
34 // Unloads the asset data, resets the bytes loaded and
35 // returns the number of bytes unloaded
36 std::size_t Unload();
37
38 // Returns the time that the data was last loaded
39 TimeType GetLastLoadedTime() const;
40
41 // Returns an id that uniquely identifies this asset
42 const CustomAssetLibrary::AssetID& GetAssetId() const;
43
44 // Returns an id that is unique to this game session
45 // This is a faster form to hash and can be used
46 // as an index
47 std::size_t GetHandle() const;
48
49 protected:
50 const std::shared_ptr<CustomAssetLibrary> m_owning_library;
51
52 private:
53 virtual CustomAssetLibrary::LoadInfo LoadImpl(const CustomAssetLibrary::AssetID& asset_id) = 0;
54 virtual void UnloadImpl() = 0;
55 CustomAssetLibrary::AssetID m_asset_id;
56 std::size_t m_handle;
57
58 mutable std::mutex m_info_lock;
59 std::size_t m_bytes_loaded = 0;
60 std::atomic<TimeType> m_last_loaded_time = {};
61 };
62
63 // An abstract class that is expected to
64 // be the base class for all assets
65 // It provides a simple interface for
66 // verifying that an asset data of type
67 // 'UnderlyingType' is loaded by
68 // checking against 'GetData()'
69 template <typename UnderlyingType>
70 class CustomLoadableAsset : public CustomAsset
71 {
72 public:
73 using CustomAsset::CustomAsset;
74
75 // Callees should understand that the type returned is
76 // a local copy and 'GetData()' needs to be called
77 // to ensure the latest copy is available if
78 // they want to handle reloads
79 [[nodiscard]] std::shared_ptr<UnderlyingType> GetData() const
80 {
81 std::lock_guard lk(m_data_lock);
82 if (m_loaded)
83 return m_data;
84 return nullptr;
85 }
86
87 protected:
88 bool m_loaded = false;
89 mutable std::mutex m_data_lock;
90 std::shared_ptr<UnderlyingType> m_data;
91
92 private:
93 void UnloadImpl() override
94 {
95 std::lock_guard lk(m_data_lock);
96 m_loaded = false;
97 m_data.reset();
98 }
99 };
100
101 // A helper struct that contains
102 // an asset with a last cached write time
103 // This can be used to determine if the asset
104 // has diverged from the last known state
105 // To know if it is time to update other dependencies
106 // based on the asset data
107 // TODO C++20: use a 'derived_from' concept against 'CustomAsset' when available
108 template <typename AssetType>
109 struct CachedAsset
110 {
111 std::shared_ptr<AssetType> m_asset;
112 CustomAsset::TimeType m_cached_write_time;
113 };
114
115 } // namespace VideoCommon