Loading...
Searching...
No Matches
AssetTable.hpp
1#pragma once
2
3#include <score_plugin_gfx_export.h>
4
5#include <ossia/detail/hash_map.hpp>
6
7#include <QImage>
8
9#include <cstddef>
10#include <cstdint>
11#include <list>
12#include <memory>
13#include <mutex>
14#include <string>
15#include <vector>
16
17namespace Gfx
18{
19
70class SCORE_PLUGIN_GFX_EXPORT AssetTable
71{
72public:
77 {
78 QImage image;
79 std::shared_ptr<const std::vector<uint8_t>> bytes;
80 std::string mime_type;
81 int64_t refcount{0};
82 // Approximate storage cost. Computed at stage() time; the
83 // allocator may report a different value but this is the number
84 // the LRU trim budgets against.
85 std::size_t byte_size{0};
86 };
87
88 // For byte-range hashing use `ossia::hash_bytes` from
89 // `ossia/detail/hash.hpp` — it's the canonical rapidhash-tiered
90 // dispatcher that produces stable `content_hash` values across
91 // the codebase. Parsers call it directly when stamping
92 // `texture_source::content_hash` / `buffer_resource::content_hash`.
93
94 AssetTable() = default;
95 AssetTable(const AssetTable&) = delete;
96 AssetTable& operator=(const AssetTable&) = delete;
97 ~AssetTable() = default;
98
102 void stage(uint64_t content_hash, QImage image);
103 void stage(
104 uint64_t content_hash, std::shared_ptr<const std::vector<uint8_t>> bytes,
105 std::string mime_type = {});
106
109 std::shared_ptr<const DecodedAsset> acquire(uint64_t content_hash);
110
116 std::shared_ptr<const DecodedAsset> peek(uint64_t content_hash) const;
117
120 void release(uint64_t content_hash);
121
126 std::size_t trim(std::size_t max_bytes_budget);
127
137 void maybeAutoTrim(
138 float utilization, float high_watermark = 0.80f,
139 float target = 0.60f);
140
142 std::size_t size() const noexcept;
144 std::size_t totalBytes() const noexcept;
146 std::size_t coldCount() const noexcept;
147
148private:
149 struct Slot; // forward
150
151 // Linked list of cold entries, newest at head. std::list for
152 // stable iterators under concurrent erase.
153 using LruList = std::list<uint64_t>;
154
155 struct Slot
156 {
157 std::shared_ptr<DecodedAsset> asset;
158 LruList::iterator lru_it; // valid only when refcount == 0
159 bool in_lru{false};
160 };
161
162 void evictOne() noexcept; // Pops the LRU tail. Caller holds m_mutex.
163
164 mutable std::mutex m_mutex;
165 ossia::hash_map<uint64_t, Slot> m_entries;
166 LruList m_lru; // cold entries, newest at front
167 std::size_t m_total_bytes{0};
168 std::size_t m_cold_bytes{0};
169};
170
171} // namespace Gfx
Cross-RenderList content-hash dedup for decoded asset bytes.
Definition AssetTable.hpp:71
Binds the rendering pipeline to ossia processes.
Definition AssetTable.cpp:7
STL namespace.
Definition AssetTable.hpp:77