From 9cb2aa36f411c5907a767a6f7585e0cbe99c6081 Mon Sep 17 00:00:00 2001 From: Jan Holthuis Date: Sat, 12 Feb 2022 18:00:49 +0100 Subject: [PATCH] AsyncImageProvider: Demonstrate issue when retrieving tracks in other thread --- src/qml/asyncimageprovider.cpp | 21 ++++++++++++++++++--- src/qml/asyncimageprovider.h | 10 +++++++++- src/qml/qmlapplication.cpp | 3 ++- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/qml/asyncimageprovider.cpp b/src/qml/asyncimageprovider.cpp index f0cec416124..13c5d7d8733 100644 --- a/src/qml/asyncimageprovider.cpp +++ b/src/qml/asyncimageprovider.cpp @@ -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 pTrackCollectionManager) + : m_id(id), + m_requestedSize(requestedSize), + m_pTrackCollectionManager(pTrackCollectionManager) { setAutoDelete(false); } @@ -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 @@ -49,9 +57,16 @@ void AsyncImageResponse::run() { emit finished(); } +AsyncImageProvider::AsyncImageProvider( + std::shared_ptr 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; } diff --git a/src/qml/asyncimageprovider.h b/src/qml/asyncimageprovider.h index ed2ed83d9dd..905a41d88bf 100644 --- a/src/qml/asyncimageprovider.h +++ b/src/qml/asyncimageprovider.h @@ -4,8 +4,10 @@ #include #include #include +#include #include "library/coverart.h" +#include "library/trackcollectionmanager.h" namespace mixxx { namespace qml { @@ -13,17 +15,22 @@ 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 pTrackCollectionManager); QQuickTextureFactory* textureFactory() const override; void run() override; QString m_id; QSize m_requestedSize; QImage m_image; + std::shared_ptr m_pTrackCollectionManager; }; class AsyncImageProvider : public QQuickAsyncImageProvider { public: + AsyncImageProvider(std::shared_ptr pTrackCollectionManager); + QQuickImageResponse* requestImageResponse( const QString& id, const QSize& requestedSize) override; @@ -33,6 +40,7 @@ class AsyncImageProvider : public QQuickAsyncImageProvider { private: QThreadPool pool; + std::shared_ptr m_pTrackCollectionManager; }; } // namespace qml diff --git a/src/qml/qmlapplication.cpp b/src/qml/qmlapplication.cpp index 1c3384b95dd..d48ea0a4b03 100644 --- a/src/qml/qmlapplication.cpp +++ b/src/qml/qmlapplication.cpp @@ -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);