-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTextureCache.h
More file actions
116 lines (92 loc) · 3 KB
/
TextureCache.h
File metadata and controls
116 lines (92 loc) · 3 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
#pragma once
#include <utility>
#include "BlockingQueue.h"
#include "DbAlbumCollection.h"
#include "Image.h"
#include "utils.h"
namespace bomi = boost::multi_index;
class ImgTexture;
class EngineThread;
struct TextureCacheMeta {
std::string groupString;
unsigned int collectionVersion{0};
// (generation, -distance to center)
std::pair<unsigned int, int> priority;
};
class TextureLoadingThreads {
public:
TextureLoadingThreads();
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::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;
class ScriptedCoverPositions& coverPos;
public:
TextureCache(EngineThread&, DbAlbumCollection&, ScriptedCoverPositions&);
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 class TextureLoadingThreads;
};