-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
coverartcache.cpp
278 lines (248 loc) · 9.32 KB
/
coverartcache.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
#include "library/coverartcache.h"
#include <QFutureWatcher>
#include <QPixmapCache>
#include <QtConcurrentRun>
#include <QtDebug>
#include "library/coverartutils.h"
#include "moc_coverartcache.cpp"
#include "track/track.h"
#include "util/logger.h"
#include "util/thread_affinity.h"
namespace {
mixxx::Logger kLogger("CoverArtCache");
// The initial QPixmapCache limit is 10MB.
// But it is not used just by the coverArt stuff,
// it is also used by Qt to handle other things behind the scenes.
// Consequently coverArt cache will always have less than those
// 10MB available to store the pixmaps.
// So, we must increase this size a bit more,
// in order to allow CoverCache handle more covers (performance gain).
constexpr int kPixmapCacheLimit = 20480;
QString pixmapCacheKey(mixxx::cache_key_t hash, int width) {
return QString("CoverArtCache_%1_%2")
.arg(QString::number(hash), QString::number(width));
}
// The transformation mode when scaling images
const Qt::TransformationMode kTransformationMode = Qt::SmoothTransformation;
// Resizes the image (preserving aspect ratio) to width.
inline QImage resizeImageWidth(const QImage& image, int width) {
return image.scaledToWidth(width, kTransformationMode);
}
} // anonymous namespace
CoverArtCache::CoverArtCache() {
QPixmapCache::setCacheLimit(kPixmapCacheLimit);
}
//static
void CoverArtCache::requestCover(
const QObject* pRequestor,
const CoverInfo& coverInfo,
const TrackPointer& pTrack) {
CoverArtCache* pCache = CoverArtCache::instance();
VERIFY_OR_DEBUG_ASSERT(pCache) {
return;
}
pCache->tryLoadCover(
pRequestor,
pTrack,
coverInfo,
0, // original size
Loading::Default);
}
//static
void CoverArtCache::requestTrackCover(
const QObject* pRequestor,
const TrackPointer& pTrack) {
VERIFY_OR_DEBUG_ASSERT(pTrack) {
return;
}
requestCover(
pRequestor,
pTrack->getCoverInfoWithLocation(),
pTrack);
}
QPixmap CoverArtCache::tryLoadCover(
const QObject* pRequestor,
const TrackPointer& pTrack,
const CoverInfo& coverInfo,
int desiredWidth,
Loading loading) {
if (kLogger.traceEnabled()) {
kLogger.trace()
<< "requestCover"
<< pRequestor
<< coverInfo
<< desiredWidth
<< loading;
}
DEBUG_ASSERT(!pTrack ||
pTrack->getLocation() == coverInfo.trackLocation);
const auto requestedCacheKey = coverInfo.cacheKey();
if (coverInfo.type == CoverInfo::NONE) {
if (loading == Loading::Default) {
emit coverFound(pRequestor, coverInfo, QPixmap(), requestedCacheKey, false);
}
return QPixmap();
}
// keep a list of trackIds for which a future is currently running
// to avoid loading the same picture again while we are loading it
QPair<const QObject*, mixxx::cache_key_t> requestId = qMakePair(pRequestor, requestedCacheKey);
if (m_runningRequests.contains(requestId)) {
return QPixmap();
}
// If this request comes from CoverDelegate (table view), it'll want to get
// a cropped cover which is ready to be drawn in the table view (cover art
// column). It's very important to keep the cropped covers in cache because
// it avoids having to rescale+crop it ALWAYS (which brings a lot of
// performance issues).
QString cacheKey = pixmapCacheKey(requestedCacheKey, desiredWidth);
QPixmap pixmap;
if (QPixmapCache::find(cacheKey, &pixmap)) {
if (kLogger.traceEnabled()) {
kLogger.trace()
<< "requestCover cache hit"
<< coverInfo
<< loading;
}
if (loading == Loading::Default) {
emit coverFound(pRequestor, coverInfo, pixmap, requestedCacheKey, false);
}
return pixmap;
}
if (loading == Loading::CachedOnly) {
if (kLogger.traceEnabled()) {
kLogger.trace() << "requestCover cache miss";
}
return QPixmap();
}
if (kLogger.traceEnabled()) {
kLogger.trace()
<< "requestCover starting future for"
<< coverInfo;
}
m_runningRequests.insert(requestId);
// The watcher will be deleted in coverLoaded()
QFutureWatcher<FutureResult>* watcher = new QFutureWatcher<FutureResult>(this);
QFuture<FutureResult> future = QtConcurrent::run(
&CoverArtCache::loadCover,
pRequestor,
pTrack,
coverInfo,
desiredWidth,
loading == Loading::Default);
connect(watcher,
&QFutureWatcher<FutureResult>::finished,
this,
&CoverArtCache::coverLoaded);
watcher->setFuture(future);
return QPixmap();
}
//static
CoverArtCache::FutureResult CoverArtCache::loadCover(
const QObject* pRequestor,
TrackPointer pTrack,
CoverInfo coverInfo,
int desiredWidth,
bool signalWhenDone) {
if (kLogger.traceEnabled()) {
kLogger.trace()
<< "loadCover"
<< coverInfo
<< desiredWidth
<< signalWhenDone;
}
DEBUG_ASSERT(!pTrack ||
pTrack->getLocation() == coverInfo.trackLocation);
auto res = FutureResult(
pRequestor,
coverInfo.cacheKey(),
signalWhenDone);
DEBUG_ASSERT(!res.coverInfoUpdated);
auto loadedImage = coverInfo.loadImage(
pTrack ? pTrack->getFileAccess().token() : SecurityTokenPointer());
if (!loadedImage.image.isNull()) {
// Refresh hash before resizing the original image!
res.coverInfoUpdated = coverInfo.refreshImageDigest(loadedImage.image);
if (pTrack && res.coverInfoUpdated) {
kLogger.info()
<< "Updating cover info of track"
<< coverInfo.trackLocation;
pTrack->setCoverInfo(coverInfo);
}
// Resize image to requested size
if (desiredWidth > 0) {
// Adjust the cover size according to the request
// or downsize the image for efficiency.
loadedImage.image = resizeImageWidth(loadedImage.image, desiredWidth);
}
}
res.coverArt = CoverArt(
std::move(coverInfo),
std::move(loadedImage),
desiredWidth);
return res;
}
// watcher
void CoverArtCache::coverLoaded() {
FutureResult res;
{
QFutureWatcher<FutureResult>* pFutureWatcher =
static_cast<QFutureWatcher<FutureResult>*>(sender());
VERIFY_OR_DEBUG_ASSERT(pFutureWatcher) {
return;
}
res = pFutureWatcher->result();
pFutureWatcher->deleteLater();
}
if (kLogger.traceEnabled()) {
kLogger.trace() << "coverLoaded" << res.coverArt;
}
QPixmap pixmap;
if (res.coverArt.loadedImage.result != CoverInfo::LoadedImage::Result::NoImage) {
if (res.coverArt.loadedImage.result == CoverInfo::LoadedImage::Result::Ok) {
DEBUG_ASSERT(!res.coverArt.loadedImage.location.isEmpty());
} else {
DEBUG_ASSERT(res.coverArt.loadedImage.image.isNull());
kLogger.warning()
<< "Failed to load cover art image"
<< res.coverArt.loadedImage
<< "for track"
<< res.coverArt.trackLocation;
// Substitute missing cover art with a placeholder image to avoid high CPU load
// See also: https://github.com/mixxxdj/mixxx/issues/9974
const int imageSize = math_max(1, res.coverArt.resizedToWidth);
QImage placeholderImage(imageSize, imageSize, QImage::Format_RGB32);
placeholderImage.fill(
mixxx::RgbColor::toQColor(res.coverArt.color, Qt::darkGray));
res.coverArt.loadedImage.image = placeholderImage;
}
// Create pixmap, GUI thread only!
DEBUG_ASSERT_MAIN_THREAD_AFFINITY();
DEBUG_ASSERT(!res.coverArt.loadedImage.image.isNull());
pixmap = QPixmap::fromImage(res.coverArt.loadedImage.image);
// Don't cache full size covers (resizedToWidth = 0)
// Large cover art wastes space in our cache and will likely
// uncache a lot of the small covers we need in the library
// table.
// Full size covers are used in the Skin Widgets, which are
// loaded with an artificial delay anyway and an additional
// re-load delay can be accepted.
if (res.coverArt.resizedToWidth > 0) {
DEBUG_ASSERT(!pixmap.isNull());
// It is very unlikely that res.coverArt.hash generates the
// same hash for different images. Otherwise the wrong image would
// be displayed when loaded from the cache.
QString cacheKey = pixmapCacheKey(
res.coverArt.cacheKey(), res.coverArt.resizedToWidth);
QPixmapCache::insert(cacheKey, pixmap);
}
}
m_runningRequests.remove(qMakePair(res.pRequestor, res.requestedCacheKey));
if (res.signalWhenDone) {
emit coverFound(
res.pRequestor,
std::move(res.coverArt),
pixmap,
res.requestedCacheKey,
res.coverInfoUpdated);
}
}