-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagetexturecache.cpp
232 lines (195 loc) · 5.82 KB
/
imagetexturecache.cpp
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include "imagetexturecache_p.h"
#include <QLoggingCategory>
#include <QSGTexture>
Q_LOGGING_CATEGORY(lcCache, "speedyimage.cache")
QHash<QQuickWindow*,std::weak_ptr<ImageTextureCache>> ImageTextureCachePrivate::instances;
// Only valid on GUI thread
std::shared_ptr<ImageTextureCache> ImageTextureCache::forWindow(QQuickWindow *window)
{
auto p = ImageTextureCachePrivate::instances.value(window).lock();
if (!p) {
p = std::shared_ptr<ImageTextureCache>(new ImageTextureCache(window));
ImageTextureCachePrivate::instances.insert(window, p);
}
return p;
}
ImageTextureCache::ImageTextureCache(QQuickWindow *window)
: QObject(window)
, d(std::make_shared<ImageTextureCachePrivate>(window))
{
}
ImageTextureCachePrivate::ImageTextureCachePrivate(QQuickWindow *window)
: window(window)
, freeThrottle(0)
, softLimit(qgetenv("SPEEDYIMAGE_CACHE_SIZE").toInt())
{
if (softLimit < 1) {
softLimit = 128 * 1048576;
}
connect(window, &QQuickWindow::beforeSynchronizing, this, &ImageTextureCachePrivate::renderThreadFree, Qt::DirectConnection);
}
ImageTextureCache::~ImageTextureCache()
{
ImageTextureCachePrivate::instances.remove(d->window);
}
ImageTextureCachePrivate::~ImageTextureCachePrivate()
{
}
ImageTextureCacheEntry ImageTextureCache::get(const QString &key)
{
QMutexLocker l(&d->mutex);
auto data = d->cache.value(key);
if (!data) {
data = std::make_shared<ImageTextureCacheData>(d.get(), key);
d->cache.insert(key, data);
data->updateCost();
}
return ImageTextureCacheEntry(data);
}
void ImageTextureCache::insert(const QString &key, const QImage &image, const QSize &imageSize)
{
auto entry = get(key);
entry.d->image = image;
entry.d->imageSize = imageSize;
entry.d->error = QString();
// XXX smarter texture management
// XXX Atlas won't be used because this isn't done from render thread
// XXX overwrite of texture will leak
entry.d->texture = d->window->createTextureFromImage(image, {QQuickWindow::TextureCanUseAtlas, QQuickWindow::TextureIsOpaque});
Q_ASSERT(entry.d->texture);
entry.d->updateCost();
emit changed(key);
}
void ImageTextureCache::insert(const QString &key, const QString &error)
{
auto entry = get(key);
entry.d->image = QImage();
entry.d->imageSize = QSize();
entry.d->error = error;
// XXX leak if texture exists
entry.d->texture = nullptr;
entry.d->updateCost();
emit changed(key);
}
void ImageTextureCachePrivate::setFreeable(const std::shared_ptr<ImageTextureCacheData> &data, bool set)
{
QMutexLocker l(&freeMutex);
if (set) {
freeable.append(data);
} else {
freeable.removeOne(data);
}
}
void ImageTextureCachePrivate::renderThreadFree()
{
// Only check cache every 100 frames
// XXX Would a timer with affinity to the render thread do this without being as reliant on render timing?
if (++freeThrottle < 100)
return;
freeThrottle = 0;
qCDebug(lcCache) << "cache using" << cacheCost << "of" << softLimit;
if (cacheCost <= softLimit)
return;
// Copy freeable and clear so we can release the mutex while working on cache, to avoid deadlocks
QMutexLocker freeLock(&freeMutex);
if (freeable.isEmpty())
return;
auto freeList = freeable;
freeable.clear();
freeLock.unlock();
// There is no path for a data to go from 0 to 1 ref without holding the cache mutex,
// so holding it guarantees that data with 0 ref can be freed safely.
QMutexLocker cacheLock(&mutex);
while (!freeList.isEmpty()) {
auto data = freeList.takeFirst();
if (data->getRefCount() > 0)
continue;
qCDebug(lcCache) << "cache freeing" << data->cost << "from" << data->key;
delete data->texture;
data->texture = nullptr;
Q_ASSERT(cache.value(data->key) == data);
cache.remove(data->key);
cacheCost -= data->cost;
if (cacheCost <= softLimit)
break;
}
qCDebug(lcCache) << "cache using" << cacheCost << "of" << softLimit << "after free;" << freeList.size() << "items still freeable";
cacheLock.unlock();
// Move anything we didn't free back into freeable
if (!freeList.isEmpty()) {
freeLock.relock();
freeList += freeable;
freeable = freeList;
freeLock.unlock();
}
}
ImageTextureCacheEntry::ImageTextureCacheEntry()
{
}
ImageTextureCacheEntry::ImageTextureCacheEntry(const std::shared_ptr<ImageTextureCacheData> &dp)
: d(dp)
{
if (d)
d->ref();
}
ImageTextureCacheEntry::ImageTextureCacheEntry(const ImageTextureCacheEntry &o)
: d(o.d)
{
if (d)
d->ref();
}
ImageTextureCacheEntry::~ImageTextureCacheEntry()
{
if (d)
d->deref();
}
ImageTextureCacheEntry &ImageTextureCacheEntry::operator=(const ImageTextureCacheEntry &o)
{
if (d != o.d) {
if (d)
d->deref();
d = o.d;
if (d)
d->ref();
}
return *this;
}
void ImageTextureCacheEntry::reset()
{
if (d)
d->deref();
d.reset();
}
QImage ImageTextureCacheEntry::image() const
{
return d ? d->image : QImage();
}
QString ImageTextureCacheEntry::error() const
{
return d ? d->error : QString();
}
QSize ImageTextureCacheEntry::loadedSize() const
{
return d ? d->image.size() : QSize();
}
QSize ImageTextureCacheEntry::imageSize() const
{
return d ? d->imageSize : QSize();
}
QSGTexture *ImageTextureCacheEntry::texture() const
{
return d ? d->texture : nullptr;
}
void ImageTextureCacheData::updateCost()
{
int newCost = 1;
if (texture) {
QSize sz = texture->textureSize();
newCost = qMax(sz.width() * sz.height() * 3, 1);
}
if (cost != newCost) {
int delta = newCost - cost;
cost = newCost;
cache->cacheCost += delta;
}
}