-
Notifications
You must be signed in to change notification settings - Fork 0
/
speedyimage.cpp
363 lines (308 loc) · 10.7 KB
/
speedyimage.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include "speedyimage_p.h"
#include "imageloader.h"
#include <QSGSimpleTextureNode>
#include <QQuickWindow>
Q_LOGGING_CATEGORY(lcItem, "speedyimage.item")
static ImageLoader *imgLoader;
//
// Return a rectangle fitting content within box while preserving
// aspect ratio. If either dimension of box is zero, scale based
// on the other dimension.
QRectF fitContentRect(const QSizeF &box, const QSizeF &content)
{
QRectF fit;
if (content.isEmpty() || (box.width() == 0 && box.height() == 0)) {
return fit;
}
if (!box.isEmpty()) {
double fContent = content.width() / content.height();
double fBox = box.width() / box.height();
if (fContent > fBox) {
fit = QRectF(0, 0, box.width(), content.height() * (box.width() / content.width()));
} else {
fit = QRectF(0, 0, content.width() * (box.height() / content.height()), box.height());
}
fit.translate((box.width() - fit.width()) / 2, (box.height() - fit.height()) / 2);
} else if (box.width() > 0) {
// Calculate height by width
double f = double(content.width()) / double(content.width());
fit = QRectF(0, 0, box.width(), content.height() * f);
fit.translate(0, (box.height() - fit.height()) / 2);
} else {
// Calculate width by height
double f = double(box.height()) / double(content.height());
fit = QRectF(0, 0, qRound(content.width() * f), box.height());
fit.translate((box.width() - fit.width()) / 2, 0);
}
return fit;
}
SpeedyImage::SpeedyImage(QQuickItem *parent)
: QQuickItem(parent)
, d(new SpeedyImagePrivate(this))
{
setFlag(ItemHasContents);
if (!imgLoader) {
imgLoader = new ImageLoader;
}
}
SpeedyImage::~SpeedyImage()
{
}
QString SpeedyImage::source() const
{
return d->source;
}
void SpeedyImage::setSource(const QString &source)
{
if (d->source == source)
return;
d->clearImage();
d->source = source;
if (!d->source.isEmpty()) {
// reloadImage will start loading the image (if possible) or immediately set it
// from the cache entry. If the image is set immediately, status and other signals
// will have been sent via cacheEntryChanged.
d->reloadImage();
// If status is still null, there was no instant cache entry
if (d->status == Null) {
d->status = Loading;
emit statusChanged();
}
// These signals are necessary if we're still loading (because of clearImage), or
// if the cacheEntry was loaded and the value still matches what clearImage set.
if (d->status == Loading || d->paintRect.isNull())
emit paintedSizeChanged();
if (d->status == Loading || !imageSize().isValid())
emit imageSizeChanged();
} else {
emit statusChanged();
emit paintedSizeChanged();
emit imageSizeChanged();
}
emit sourceChanged();
}
QSize SpeedyImage::loadingSize() const
{
return d->loadingSize;
}
void SpeedyImage::setLoadingSize(QSize size) {
d->explicitLoadingSize = size.isValid();
if (!size.isValid())
size = QSize(width(), height());
d->applyLoadingSize(size);
}
SpeedyImage::Status SpeedyImage::status() const
{
return d->status;
}
QSize SpeedyImage::imageSize() const
{
return d->cacheEntry.imageSize();
}
QSizeF SpeedyImage::paintedSize() const
{
return d->paintRect.size();
}
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
void SpeedyImage::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
{
QQuickItem::geometryChange(newGeometry, oldGeometry);
#else
void SpeedyImage::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
{
QQuickItem::geometryChanged(newGeometry, oldGeometry);
#endif
QSize size = newGeometry.size().toSize();
if (size == oldGeometry.size().toSize())
return;
if (d->calcPaintRect())
emit paintedSizeChanged();
if (!d->explicitLoadingSize)
d->applyLoadingSize(newGeometry.size().toSize());
}
QSGNode *SpeedyImage::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *)
{
if (!d->cacheEntry.texture()) {
delete oldNode;
return nullptr;
}
QSGSimpleTextureNode *node = static_cast<QSGSimpleTextureNode*>(oldNode);
if (!node) {
node = new QSGSimpleTextureNode;
node->setFiltering(QSGTexture::Linear);
}
node->setTexture(d->cacheEntry.texture());
node->setRect(d->paintRect);
return node;
}
void SpeedyImage::componentComplete()
{
QQuickItem::componentComplete();
d->componentComplete = true;
d->reloadImage();
}
SpeedyImagePrivate::SpeedyImagePrivate(SpeedyImage *q)
: q(q)
, status(SpeedyImage::Null)
, componentComplete(false)
, explicitLoadingSize(false)
{
connect(q, &QQuickItem::windowChanged, this, &SpeedyImagePrivate::setWindow);
}
void SpeedyImagePrivate::setWindow(QQuickWindow *window)
{
if (imageCache) {
disconnect(imageCache.get(), nullptr, this, nullptr);
imageCache.reset();
// Must be cleared because cacheEntry's texture is specific to a window.
// If possible, reloadImage below will fill it in from the new imageCache.
clearImage();
}
if (window) {
imageCache = ImageTextureCache::forWindow(window);
connect(imageCache.get(), &ImageTextureCache::changed, this, &SpeedyImagePrivate::cacheEntryChanged);
connect(window, &QQuickWindow::sceneGraphInitialized, this, &SpeedyImagePrivate::reloadImage);
// Trigger reload in case one was blocked by not having imageCache earlier. Has no effect
// if this is not necessary.
reloadImage();
}
}
void SpeedyImagePrivate::clearImage()
{
cacheEntry.reset();
loadJob.reset();
paintRect = QRectF();
status = SpeedyImage::Null;
q->update();
}
void SpeedyImagePrivate::applyLoadingSize(QSize size)
{
// If size has a zero dimension, set based on imageSize
QSize imageSize = q->imageSize();
if (size.isEmpty() && !imageSize.isEmpty()) {
if (size.width() == 0 && size.height() == 0)
size = imageSize;
else
size = fitContentRect(size, imageSize).size().toSize();
}
if (loadingSize == size)
return;
loadingSize = size;
emit q->loadingSizeChanged();
// Does nothing if a reload is not necessary
reloadImage();
}
// Returns true if the the image needs to be reloaded based on the current loadingSize.
bool SpeedyImagePrivate::needsReloadForDrawSize()
{
if (status == SpeedyImage::Error || status == SpeedyImage::Null) {
return false;
} else if (!loadingSize.isValid()) {
// If loadingSize is invalid, do nothing. Does not include empty loadingSize.
return false;
}
QSizeF loadedSize = cacheEntry.loadedSize();
QSizeF imageSize = cacheEntry.imageSize();
if (imageSize.isEmpty() || loadedSize.isEmpty()) {
// If nothing is loaded yet, always reload for draw size; see reloadImage.
return true;
}
if (loadingSize.width() == 0 && loadingSize.height() == 0) {
// If loadingSize is exactly zero, reload only if full imageSize isn't loaded yet
return imageSize != loadedSize;
}
// Scale imageSize within loadingSize and reload if either dimension exceeds loadedSize
QSizeF fit = fitContentRect(loadingSize, imageSize).size();
if ((fit.width() > loadedSize.width() && imageSize.width() > loadedSize.width()) ||
(fit.height() > loadedSize.height() && imageSize.height() > loadedSize.height()))
{
return true;
}
return false;
}
void SpeedyImagePrivate::reloadImage()
{
if (!componentComplete || !imageCache || !q->window() || !q->window()->isSceneGraphInitialized()
|| source.isEmpty() || !loadingSize.isValid())
{
return;
}
if (cacheEntry.isNull()) {
cacheEntry = imageCache->get(source);
if (!cacheEntry.isEmpty()) {
// Call cacheEntryChanged to handle everything
cacheEntryChanged(source);
}
}
if ((!cacheEntry.isEmpty() && !needsReloadForDrawSize()) || !cacheEntry.error().isEmpty()) {
// Use cache entry
return;
}
if (!loadJob.isNull()) {
// We can attempt to change the drawSize on an existing job, but there
// is no guarantee it will take effect. That case can be handled with a
// check in setImage that will fire off a new job at a larger drawSize
// if the result is insufficient, and we'll still have an upscale to display
// meanwhile.
if (loadingSize != loadJob.drawSize()) {
qCDebug(lcItem) << this << "updating load size on existing job to" << loadingSize;
loadJob.setDrawSize(loadingSize);
}
} else if (imageCache) {
// Copy for lambda
auto src = source;
std::shared_ptr<ImageTextureCache> cache = imageCache;
loadJob = imgLoader->enqueue(source, loadingSize, 0,
[src,cache](const ImageLoaderJob &job) {
// Cache will signal the update to the cache entry
if (!job.error().isEmpty())
cache->insert(src, job.error());
else
cache->insert(src, job.result(), job.imageSize());
});
}
}
void SpeedyImagePrivate::cacheEntryChanged(const QString &key)
{
if (key != source)
return;
qCDebug(lcItem) << this << "cache updated for" << key;
loadJob.reset();
q->update();
auto oldStatus = status;
if (!cacheEntry.error().isEmpty()) {
status = SpeedyImage::Error;
} else {
status = SpeedyImage::Ready;
Q_ASSERT(cacheEntry.texture());
}
if (calcPaintRect())
emit q->paintedSizeChanged();
if (status != oldStatus)
emit q->statusChanged();
// Can't really tell if image size changed, but assume it won't between reloads
if (oldStatus != SpeedyImage::Ready) {
emit q->imageSizeChanged();
// If loadingSize has a zero dimension, update it based on the imageSize
if (loadingSize.isEmpty())
applyLoadingSize(loadingSize);
}
// Reload the image again if drawSize has changed and needs a larger scale
if (needsReloadForDrawSize())
{
qCDebug(lcItem) << this << "draw size increased while loading, reloading at larger size";
reloadImage();
}
}
bool SpeedyImagePrivate::calcPaintRect()
{
QSizeF box(q->width(), q->height());
QSizeF img(cacheEntry.loadedSize());
QRectF paint = fitContentRect(box, img);
// XXX Should paint be rounded? Might lead to bad results if it's not...
if (paint == paintRect)
return false;
paintRect = paint;
q->update();
return true;
}