This repository was archived by the owner on Jan 23, 2026. It is now read-only.
forked from Chronial/foo_chronflow
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTextureCache.h
More file actions
136 lines (112 loc) · 3.83 KB
/
Copy pathTextureCache.h
File metadata and controls
136 lines (112 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#pragma once
// clang-format off
#include <utility>
#include "cover_positions.h"
#include "DbAlbumCollection.h"
#include "EngineThread.fwd.h" //optional fwd
//#include "EngineThread.h"
#include "Image.h"
#include "utils.h"
// clang-format on
namespace render {
using db::DbAlbumCollection;
using db::DBIter;
using db::DBPos;
using engine::EngineThread;
namespace bomi = boost::multi_index;
struct TextureCacheMeta {
std::string groupString;
unsigned int collectionVersion{0};
// (generation, -distance to center)
std::pair<unsigned int, int> priority;
};
class TextureLoadingThreads {
public:
TextureLoadingThreads() {
unsigned int threadCount = std::thread::hardware_concurrency();
for (unsigned int i = 0; i < threadCount; i++) {
threads.emplace_back(catchThreadExceptions("TextureLoader", [&] { this->run(); }));
check(SetThreadPriority(threads.back().native_handle(),
THREAD_PRIORITY_BELOW_NORMAL));
// Disable dynamic priority boost. We don't want the texture loaders to ever have
// higher priority than the engine thread.
check(SetThreadPriorityBoost(threads.back().native_handle(), TRUE));
}
setPriority(true);
}
NO_MOVE_NO_COPY(TextureLoadingThreads);
~TextureLoadingThreads();
struct LoadRequest {
TextureCacheMeta meta;
metadb_handle_ptr track;
};
struct LoadResponse {
TextureCacheMeta meta;
std::optional<UploadReadyImage> image;
};
void flushQueue();
void setQueue(std::vector<LoadRequest>&& data);
std::optional<LoadResponse> getLoaded();
void pause();
void resume();
void setPriority(bool highPriority);
private:
std::pair<std::string, metadb_handle_ptr> takeJob();
void finishJob(const std::string&, std::optional<UploadReadyImage>);
std::pair<std::string, metadb_handle_ptr> getTarget();
std::vector<std::thread> threads;
abort_callback_impl abort;
std::atomic<bool> highPriority = false;
std::shared_mutex pauseMutex;
std::unique_lock<std::shared_mutex> pauseLock{pauseMutex, std::defer_lock};
std::mutex mutex;
std::condition_variable inCondition;
std::deque<LoadRequest> inQueue;
std::unordered_map<std::string, TextureCacheMeta> inProgress;
std::deque<LoadResponse> outQueue;
void run();
};
class TextureCache {
DbAlbumCollection& db;
EngineThread& thread;
ScriptedCoverPositions& coverPos;
public:
TextureCache(EngineThread& thread, DbAlbumCollection& db, ScriptedCoverPositions& coverPos);
const GLImage* getAlbumTexture(const std::string& albumName);
GLImage& getLoadingTexture();
void trimCache();
void clearCache();
void startLoading(const DBPos& target);
void onCollectionReload();
void updateLoadingQueue(const DBIter& queueCenter);
void uploadTextures();
void pauseLoading();
void resumeLoading();
void setPriority(bool highPriority);
private:
int maxCacheSize();
unsigned int collectionVersion = 0;
void reloadSpecialTextures();
GLImage noCoverTexture;
GLImage loadingTexture;
unsigned int cacheGeneration = 0;
struct CacheItem : TextureCacheMeta {
CacheItem(const TextureCacheMeta& meta, std::optional<GLImage>&& texture)
: TextureCacheMeta(meta), texture(std::move(texture)){};
std::optional<GLImage> texture;
};
using t_textureCache = bomi::multi_index_container<
CacheItem,
bomi::indexed_by<
bomi::hashed_unique<
bomi::member<TextureCacheMeta, std::string, &CacheItem::groupString>>,
bomi::ordered_non_unique<bomi::composite_key<
CacheItem,
bomi::member<TextureCacheMeta, unsigned int, &CacheItem::collectionVersion>,
bomi::member<TextureCacheMeta, std::pair<unsigned int, int>,
&CacheItem::priority>>>>>;
t_textureCache textureCache;
TextureLoadingThreads bgLoader;
friend TextureLoadingThreads;
};
} // namespace render