Skip to content

Commit

Permalink
make CoverArtCache independent of devicePixelRatio
Browse files Browse the repository at this point in the history
  • Loading branch information
Be-ing committed Aug 27, 2019
1 parent 543b392 commit 2998cae
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 40 deletions.
6 changes: 2 additions & 4 deletions src/library/coverart.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,10 @@ class CoverArt : public CoverInfo {
: resizedToWidth(0) {
}

CoverArt(const CoverInfo& base, const QImage& img, int rtw, double devicePixelRatio)
CoverArt(const CoverInfo& base, const QImage& img, int rtw)
: CoverInfo(base),
image(img),
resizedToWidth(rtw),
devicePixelRatio(devicePixelRatio) {
resizedToWidth(rtw) {
}

// all-default memory management
Expand All @@ -103,7 +102,6 @@ class CoverArt : public CoverInfo {
// outside the GUI thread
QImage image;
int resizedToWidth;
double devicePixelRatio;
};

QDebug operator<<(QDebug dbg, const CoverArt& art);
Expand Down
32 changes: 14 additions & 18 deletions src/library/coverartcache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ namespace {

mixxx::Logger kLogger("CoverArtCache");

QString pixmapCacheKey(quint16 hash, int actualWidth) {
QString pixmapCacheKey(quint16 hash, int width) {
return QString("CoverArtCache_%1_%2")
.arg(QString::number(hash)).arg(actualWidth);
.arg(QString::number(hash)).arg(width);
}

// The transformation mode when scaling images
Expand Down Expand Up @@ -47,14 +47,13 @@ CoverArtCache::~CoverArtCache() {

QPixmap CoverArtCache::requestCover(const CoverInfo& requestInfo,
const QObject* pRequestor,
const int deviceIndependentWidth,
const double devicePixelRatio,
const int desiredWidth,
const bool onlyCached,
const bool signalWhenDone) {
if (sDebug) {
kLogger.debug() << "requestCover"
<< requestInfo << pRequestor <<
deviceIndependentWidth << onlyCached << signalWhenDone;
desiredWidth << onlyCached << signalWhenDone;
}

if (requestInfo.type == CoverInfo::NONE) {
Expand All @@ -77,11 +76,10 @@ QPixmap CoverArtCache::requestCover(const CoverInfo& requestInfo,
// 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(requestInfo.hash, deviceIndependentWidth * devicePixelRatio);
QString cacheKey = pixmapCacheKey(requestInfo.hash, desiredWidth);

QPixmap pixmap;
if (QPixmapCache::find(cacheKey, &pixmap)) {
pixmap.setDevicePixelRatio(devicePixelRatio);
if (sDebug) {
kLogger.debug() << "CoverArtCache::requestCover cover found in cache" << requestInfo << signalWhenDone;
}
Expand All @@ -106,31 +104,30 @@ QPixmap CoverArtCache::requestCover(const CoverInfo& requestInfo,
QFutureWatcher<FutureResult>* watcher = new QFutureWatcher<FutureResult>(this);
QFuture<FutureResult> future = QtConcurrent::run(
this, &CoverArtCache::loadCover, requestInfo, pRequestor,
deviceIndependentWidth, devicePixelRatio, signalWhenDone);
desiredWidth, signalWhenDone);
connect(watcher, SIGNAL(finished()), this, SLOT(coverLoaded()));
watcher->setFuture(future);
return QPixmap();
}

//static
void CoverArtCache::requestCover(const Track& track,
const QObject* pRequestor, double devicePixelRatio) {
const QObject* pRequestor) {
CoverArtCache* pCache = CoverArtCache::instance();
if (pCache == nullptr) return;

CoverInfo info = track.getCoverInfoWithLocation();
pCache->requestCover(info, pRequestor, 0, devicePixelRatio, false, true);
pCache->requestCover(info, pRequestor, 0, false, true);
}

CoverArtCache::FutureResult CoverArtCache::loadCover(
const CoverInfo& info,
const QObject* pRequestor,
const int deviceIndependentWidth,
const double devicePixelRatio,
const int desiredWidth,
const bool signalWhenDone) {
if (sDebug) {
kLogger.debug() << "loadCover"
<< info << deviceIndependentWidth << signalWhenDone;
<< info << desiredWidth << signalWhenDone;
}

QImage image = CoverArtUtils::loadCover(info);
Expand All @@ -142,13 +139,13 @@ CoverArtCache::FutureResult CoverArtCache::loadCover(

// Adjust the cover size according to the request or downsize the image for
// efficiency.
if (!image.isNull() && deviceIndependentWidth > 0) {
image = resizeImageWidth(image, deviceIndependentWidth * devicePixelRatio);
if (!image.isNull() && desiredWidth > 0) {
image = resizeImageWidth(image, desiredWidth);
}

FutureResult res;
res.pRequestor = pRequestor;
res.cover = CoverArt(info, image, deviceIndependentWidth, devicePixelRatio);
res.cover = CoverArt(info, image, desiredWidth);
res.signalWhenDone = signalWhenDone;

return res;
Expand Down Expand Up @@ -182,10 +179,9 @@ void CoverArtCache::coverLoaded() {
// we have to be sure that res.cover.hash is unique
// because insert replaces the images with the same key
QString cacheKey = pixmapCacheKey(
res.cover.hash, res.cover.resizedToWidth * res.cover.devicePixelRatio);
res.cover.hash, res.cover.resizedToWidth);
QPixmapCache::insert(cacheKey, pixmap);
}
pixmap.setDevicePixelRatio(res.cover.devicePixelRatio);

m_runningRequests.remove(qMakePair(res.pRequestor, res.cover.hash));

Expand Down
5 changes: 1 addition & 4 deletions src/library/coverartcache.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ class CoverArtCache : public QObject, public Singleton<CoverArtCache> {
QPixmap requestCover(const CoverInfo& info,
const QObject* pRequestor,
const int desiredWidth,
const double devicePixelRatio,
const bool onlyCached,
const bool signalWhenDone);

static void requestCover(const Track& track,
const QObject* pRequestor,
const double devicePixelRatio);
const QObject* pRequestor);

// Guesses the cover art for the provided tracks by searching the tracks'
// metadata and folders for image files. All I/O is done in a separate
Expand Down Expand Up @@ -69,7 +67,6 @@ class CoverArtCache : public QObject, public Singleton<CoverArtCache> {
FutureResult loadCover(const CoverInfo& coverInfo,
const QObject* pRequestor,
const int desiredWidth,
const double devicePixelRatio,
const bool emitSignals);

// Guesses the cover art for each track.
Expand Down
5 changes: 3 additions & 2 deletions src/library/coverartdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ void CoverArtDelegate::paintItem(QPainter *painter,
double scaleFactor = getDevicePixelRatioF(dynamic_cast<QWidget*>(parent()));
// We listen for updates via slotCoverFound above and signal to
// BaseSqlTableModel when a row's cover is ready.
QPixmap pixmap = pCache->requestCover(info, this, option.rect.width(),
scaleFactor, m_bOnlyCachedCover, true);
QPixmap pixmap = pCache->requestCover(info, this, option.rect.width() * scaleFactor,
m_bOnlyCachedCover, true);
pixmap.setDevicePixelRatio(scaleFactor);
if (!pixmap.isNull()) {
int width = math_min(pixmap.width(), option.rect.width()) * scaleFactor;
int height = math_min(pixmap.height(), option.rect.height()) * scaleFactor;
Expand Down
4 changes: 3 additions & 1 deletion src/library/dlgcoverartfullsize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void DlgCoverArtFullSize::slotLoadTrack(TrackPointer pTrack) {

void DlgCoverArtFullSize::slotTrackCoverArtUpdated() {
if (m_pLoadedTrack != nullptr) {
CoverArtCache::requestCover(*m_pLoadedTrack, this, getDevicePixelRatioF(this));
CoverArtCache::requestCover(*m_pLoadedTrack, this);
}
}

Expand Down Expand Up @@ -134,6 +134,7 @@ void DlgCoverArtFullSize::slotCoverFound(const QObject* pRequestor,
}
QPixmap resizedPixmap = m_pixmap.scaled(size() * getDevicePixelRatioF(this),
Qt::KeepAspectRatio, Qt::SmoothTransformation);
resizedPixmap.setDevicePixelRatio(getDevicePixelRatioF(this));
coverArt->setPixmap(resizedPixmap);
// center the window
setGeometry(QStyle::alignedRect(
Expand Down Expand Up @@ -184,6 +185,7 @@ void DlgCoverArtFullSize::resizeEvent(QResizeEvent* event) {
// qDebug() << "DlgCoverArtFullSize::resizeEvent" << size();
QPixmap resizedPixmap = m_pixmap.scaled(size() * getDevicePixelRatioF(this),
Qt::KeepAspectRatio, Qt::SmoothTransformation);
resizedPixmap.setDevicePixelRatio(getDevicePixelRatioF(this));
coverArt->setPixmap(resizedPixmap);
}

Expand Down
4 changes: 2 additions & 2 deletions src/library/dlgtrackinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ void DlgTrackInfo::populateFields(const Track& track) {
m_pWCoverArtLabel->setCoverArt(m_loadedCoverInfo, QPixmap());
CoverArtCache* pCache = CoverArtCache::instance();
if (pCache != NULL) {
pCache->requestCover(m_loadedCoverInfo, this, 0, getDevicePixelRatioF(this), false, true);
pCache->requestCover(m_loadedCoverInfo, this, 0, false, true);
}
}

Expand Down Expand Up @@ -254,7 +254,7 @@ void DlgTrackInfo::slotCoverInfoSelected(const CoverInfoRelative& coverInfo) {
m_loadedCoverInfo = CoverInfo(coverInfo, m_pLoadedTrack->getLocation());
CoverArtCache* pCache = CoverArtCache::instance();
if (pCache) {
pCache->requestCover(m_loadedCoverInfo, this, 0, getDevicePixelRatioF(this), false, true);
pCache->requestCover(m_loadedCoverInfo, this, 0, false, true);
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/widget/wcoverart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ void WCoverArt::slotReset() {

void WCoverArt::slotTrackCoverArtUpdated() {
if (m_loadedTrack) {
CoverArtCache::requestCover(*m_loadedTrack, this, getDevicePixelRatioF(this));
CoverArtCache::requestCover(*m_loadedTrack, this);
}
}

Expand Down Expand Up @@ -191,7 +191,11 @@ QPixmap WCoverArt::scaledCoverArt(const QPixmap& normal) {
if (normal.isNull()) {
return QPixmap();
}
return normal.scaled(size() * getDevicePixelRatioF(this), Qt::KeepAspectRatio, Qt::SmoothTransformation);
QPixmap scaled;
scaled = normal.scaled(size() * getDevicePixelRatioF(this),
Qt::KeepAspectRatio, Qt::SmoothTransformation);
scaled.setDevicePixelRatio(getDevicePixelRatioF(this));
return scaled;
}

void WCoverArt::paintEvent(QPaintEvent* /*unused*/) {
Expand Down
10 changes: 5 additions & 5 deletions src/widget/wcoverartlabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ void WCoverArtLabel::setCoverArt(const CoverInfo& coverInfo,
QPixmap px) {
qDebug() << "WCoverArtLabel::setCoverArt" << coverInfo << px.size();

m_loadedCover = px;
m_loadedCover = px.scaled(s_labelDisplaySize * getDevicePixelRatioF(this),
Qt::KeepAspectRatio, Qt::SmoothTransformation);
m_loadedCover.setDevicePixelRatio(getDevicePixelRatioF(this));
m_pCoverMenu->setCoverArt(coverInfo);


if (px.isNull()) {
if (m_loadedCover.isNull()) {
setPixmap(m_defaultCover);
} else {
setPixmap(px.scaled(s_labelDisplaySize * getDevicePixelRatioF(this), Qt::KeepAspectRatio,
Qt::SmoothTransformation));
setPixmap(m_loadedCover);
}

QSize frameSize = pixmap()->size() / getDevicePixelRatioF(this);
Expand Down
7 changes: 5 additions & 2 deletions src/widget/wspinny.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ void WSpinny::slotLoadingTrack(TrackPointer pNewTrack, TrackPointer pOldTrack) {

void WSpinny::slotTrackCoverArtUpdated() {
if (m_loadedTrack) {
CoverArtCache::requestCover(*m_loadedTrack, this, getDevicePixelRatioF(this));
CoverArtCache::requestCover(*m_loadedTrack, this);
}
}

Expand Down Expand Up @@ -406,7 +406,10 @@ QPixmap WSpinny::scaledCoverArt(const QPixmap& normal) {
if (normal.isNull()) {
return QPixmap();
}
return normal.scaled(size() * getDevicePixelRatioF(this), Qt::KeepAspectRatio, Qt::SmoothTransformation);
QPixmap scaled = normal.scaled(size() * getDevicePixelRatioF(this),
Qt::KeepAspectRatio, Qt::SmoothTransformation);
scaled.setDevicePixelRatio(getDevicePixelRatioF(this));
return scaled;
}

void WSpinny::resizeEvent(QResizeEvent* /*unused*/) {
Expand Down

0 comments on commit 2998cae

Please sign in to comment.