Skip to content

Commit

Permalink
AsyncImageProvider: Demonstrate issue when retrieving tracks in other…
Browse files Browse the repository at this point in the history
… thread
  • Loading branch information
Holzhaus committed Feb 12, 2022
1 parent b519aba commit 9cb2aa3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
21 changes: 18 additions & 3 deletions src/qml/asyncimageprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ const QString kCoverArtPrefix = QStringLiteral("coverart/");
namespace mixxx {
namespace qml {

AsyncImageResponse::AsyncImageResponse(const QString& id, const QSize& requestedSize)
: m_id(id), m_requestedSize(requestedSize) {
AsyncImageResponse::AsyncImageResponse(const QString& id,
const QSize& requestedSize,
std::shared_ptr<TrackCollectionManager> pTrackCollectionManager)
: m_id(id),
m_requestedSize(requestedSize),
m_pTrackCollectionManager(pTrackCollectionManager) {
setAutoDelete(false);
}

Expand All @@ -22,6 +26,10 @@ void AsyncImageResponse::run() {
if (m_id.startsWith(kCoverArtPrefix)) {
QString trackLocation = AsyncImageProvider::coverArtUrlIdToTrackLocation(m_id);

const auto trackRef = TrackRef::fromFilePath(trackLocation);
const auto pTrack = m_pTrackCollectionManager->getTrackByRef(trackRef);
Q_UNUSED(pTrack);

// TODO: This code does not allow to override embedded cover art with
// a custom image, which is possible in Mixxx. We need to access the
// actual CoverInfo of the track instead of constructing a default
Expand Down Expand Up @@ -49,9 +57,16 @@ void AsyncImageResponse::run() {
emit finished();
}

AsyncImageProvider::AsyncImageProvider(
std::shared_ptr<TrackCollectionManager> pTrackCollectionManager)
: QQuickAsyncImageProvider(),
m_pTrackCollectionManager(pTrackCollectionManager) {
}

QQuickImageResponse* AsyncImageProvider::requestImageResponse(
const QString& id, const QSize& requestedSize) {
AsyncImageResponse* response = new AsyncImageResponse(id, requestedSize);
AsyncImageResponse* response = new AsyncImageResponse(
id, requestedSize, m_pTrackCollectionManager);
pool.start(response);
return response;
}
Expand Down
10 changes: 9 additions & 1 deletion src/qml/asyncimageprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,33 @@
#include <QSize>
#include <QString>
#include <QThreadPool>
#include <memory>

#include "library/coverart.h"
#include "library/trackcollectionmanager.h"

namespace mixxx {
namespace qml {

class AsyncImageResponse : public QQuickImageResponse, public QRunnable {
Q_OBJECT
public:
AsyncImageResponse(const QString& id, const QSize& requestedSize);
AsyncImageResponse(const QString& id,
const QSize& requestedSize,
std::shared_ptr<TrackCollectionManager> pTrackCollectionManager);
QQuickTextureFactory* textureFactory() const override;
void run() override;

QString m_id;
QSize m_requestedSize;
QImage m_image;
std::shared_ptr<TrackCollectionManager> m_pTrackCollectionManager;
};

class AsyncImageProvider : public QQuickAsyncImageProvider {
public:
AsyncImageProvider(std::shared_ptr<TrackCollectionManager> pTrackCollectionManager);

QQuickImageResponse* requestImageResponse(
const QString& id, const QSize& requestedSize) override;

Expand All @@ -33,6 +40,7 @@ class AsyncImageProvider : public QQuickAsyncImageProvider {

private:
QThreadPool pool;
std::shared_ptr<TrackCollectionManager> m_pTrackCollectionManager;
};

} // namespace qml
Expand Down
3 changes: 2 additions & 1 deletion src/qml/qmlapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ void QmlApplication::loadQml(const QString& path) {
m_pAppEngine->addImportPath(QStringLiteral(":/mixxx.org/imports"));

// No memory leak here, the QQmlEngine takes ownership of the provider
QQuickAsyncImageProvider* pImageProvider = new AsyncImageProvider();
QQuickAsyncImageProvider* pImageProvider = new AsyncImageProvider(
m_pCoreServices->getTrackCollectionManager());
m_pAppEngine->addImageProvider(AsyncImageProvider::kProviderName, pImageProvider);

m_pAppEngine->load(path);
Expand Down

0 comments on commit 9cb2aa3

Please sign in to comment.