| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include <QtQuick/private/qquickpixmapcache_p.h> |
| 5 | #include <QtQuick/private/qquickimageprovider_p.h> |
| 6 | #include <QtQuick/private/qquickprofiler_p.h> |
| 7 | #include <QtQuick/private/qsgcontext_p.h> |
| 8 | #include <QtQuick/private/qsgrenderer_p.h> |
| 9 | #include <QtQuick/private/qsgtexturereader_p.h> |
| 10 | #include <QtQuick/qquickwindow.h> |
| 11 | |
| 12 | #include <QtGui/private/qguiapplication_p.h> |
| 13 | #include <QtGui/private/qimage_p.h> |
| 14 | #include <QtGui/qpa/qplatformintegration.h> |
| 15 | #include <QtGui/qimagereader.h> |
| 16 | #include <QtGui/qpixmapcache.h> |
| 17 | |
| 18 | #include <QtQml/private/qqmlglobal_p.h> |
| 19 | #include <QtQml/private/qqmlengine_p.h> |
| 20 | #include <QtQml/qqmlfile.h> |
| 21 | |
| 22 | #include <QtCore/private/qobject_p.h> |
| 23 | #include <QtCore/qcoreapplication.h> |
| 24 | #include <QtCore/qhash.h> |
| 25 | #include <QtCore/qfile.h> |
| 26 | #include <QtCore/qthread.h> |
| 27 | #include <QtCore/qmutex.h> |
| 28 | #include <QtCore/qbuffer.h> |
| 29 | #include <QtCore/qdebug.h> |
| 30 | #include <QtCore/qmetaobject.h> |
| 31 | #include <QtCore/qscopeguard.h> |
| 32 | |
| 33 | #if QT_CONFIG(qml_network) |
| 34 | #include <QtQml/qqmlnetworkaccessmanagerfactory.h> |
| 35 | #include <QtNetwork/qnetworkreply.h> |
| 36 | #include <QtNetwork/qsslerror.h> |
| 37 | #endif |
| 38 | |
| 39 | #include <private/qdebug_p.h> |
| 40 | |
| 41 | #define IMAGEREQUEST_MAX_NETWORK_REQUEST_COUNT 8 |
| 42 | |
| 43 | // After QQuickPixmapCache::unreferencePixmap() it may get deleted via a timer in 30 seconds |
| 44 | #define CACHE_EXPIRE_TIME 30 |
| 45 | |
| 46 | // How many (1/4) of the unreferenced pixmaps to delete in QQuickPixmapCache::timerEvent() |
| 47 | #define CACHE_REMOVAL_FRACTION 4 |
| 48 | |
| 49 | #define PIXMAP_PROFILE(Code) Q_QUICK_PROFILE(QQuickProfiler::ProfilePixmapCache, Code) |
| 50 | |
| 51 | #if QT_CONFIG(thread) && !defined(Q_OS_WASM) |
| 52 | # define USE_THREADED_DOWNLOAD 1 |
| 53 | #else |
| 54 | # define USE_THREADED_DOWNLOAD 0 |
| 55 | #endif |
| 56 | |
| 57 | QT_BEGIN_NAMESPACE |
| 58 | |
| 59 | using namespace Qt::Literals::StringLiterals; |
| 60 | |
| 61 | Q_DECLARE_LOGGING_CATEGORY(lcQsgLeak) |
| 62 | |
| 63 | #if defined(QT_DEBUG) && QT_CONFIG(thread) |
| 64 | class ThreadAffinityMarker |
| 65 | { |
| 66 | public: |
| 67 | ThreadAffinityMarker() { attachToCurrentThread(); } |
| 68 | |
| 69 | void assertOnAssignedThread() |
| 70 | { |
| 71 | QMutexLocker locker(&m_mutex); |
| 72 | if (!m_assignedThread) |
| 73 | attachToCurrentThread(); |
| 74 | Q_ASSERT_X(m_assignedThread == QThread::currentThreadId(), Q_FUNC_INFO, |
| 75 | "Running on a wrong thread!" ); |
| 76 | } |
| 77 | |
| 78 | void detachFromCurrentThread() |
| 79 | { |
| 80 | QMutexLocker locker(&m_mutex); |
| 81 | m_assignedThread = nullptr; |
| 82 | } |
| 83 | |
| 84 | void attachToCurrentThread() { m_assignedThread = QThread::currentThreadId(); } |
| 85 | |
| 86 | private: |
| 87 | Qt::HANDLE m_assignedThread; |
| 88 | QMutex m_mutex; |
| 89 | }; |
| 90 | # define Q_THREAD_AFFINITY_MARKER(x) ThreadAffinityMarker x |
| 91 | # define Q_ASSERT_CALLED_ON_VALID_THREAD(x) x.assertOnAssignedThread() |
| 92 | # define Q_DETACH_THREAD_AFFINITY_MARKER(x) x.detachFromCurrentThread() |
| 93 | #else |
| 94 | # define Q_THREAD_AFFINITY_MARKER(x) |
| 95 | # define Q_ASSERT_CALLED_ON_VALID_THREAD(x) |
| 96 | # define Q_DETACH_THREAD_AFFINITY_MARKER(x) |
| 97 | #endif |
| 98 | |
| 99 | const QLatin1String QQuickPixmap::itemGrabberScheme = QLatin1String("itemgrabber" ); |
| 100 | |
| 101 | Q_LOGGING_CATEGORY(lcImg, "qt.quick.image" ) |
| 102 | |
| 103 | /*! \internal |
| 104 | The maximum currently-unused image data that can be stored for potential |
| 105 | later reuse, in bytes. See QQuickPixmapCache::shrinkCache() |
| 106 | */ |
| 107 | static int cache_limit = 2048 * 1024; |
| 108 | |
| 109 | static inline QString imageProviderId(const QUrl &url) |
| 110 | { |
| 111 | return url.host(); |
| 112 | } |
| 113 | |
| 114 | static inline QString imageId(const QUrl &url) |
| 115 | { |
| 116 | return url.toString(options: QUrl::RemoveScheme | QUrl::RemoveAuthority).mid(position: 1); |
| 117 | } |
| 118 | |
| 119 | QQuickDefaultTextureFactory::QQuickDefaultTextureFactory(const QImage &image) |
| 120 | { |
| 121 | if (image.format() == QImage::Format_ARGB32_Premultiplied |
| 122 | || image.format() == QImage::Format_RGB32) { |
| 123 | im = image; |
| 124 | } else { |
| 125 | im = image.convertToFormat(f: QImage::Format_ARGB32_Premultiplied); |
| 126 | } |
| 127 | size = im.size(); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | QSGTexture *QQuickDefaultTextureFactory::createTexture(QQuickWindow *window) const |
| 132 | { |
| 133 | QSGTexture *t = window->createTextureFromImage(image: im, options: QQuickWindow::TextureCanUseAtlas); |
| 134 | static bool transient = qEnvironmentVariableIsSet(varName: "QSG_TRANSIENT_IMAGES" ); |
| 135 | if (transient) |
| 136 | const_cast<QQuickDefaultTextureFactory *>(this)->im = QImage(); |
| 137 | return t; |
| 138 | } |
| 139 | |
| 140 | class QQuickPixmapReader; |
| 141 | class QQuickPixmapData; |
| 142 | class QQuickPixmapReply : public QObject |
| 143 | { |
| 144 | Q_OBJECT |
| 145 | public: |
| 146 | enum ReadError { NoError, Loading, Decoding }; |
| 147 | |
| 148 | QQuickPixmapReply(QQuickPixmapData *); |
| 149 | ~QQuickPixmapReply(); |
| 150 | |
| 151 | QQuickPixmapData *data; |
| 152 | QQmlEngine *engineForReader; // always access reader inside readerMutex |
| 153 | QRect requestRegion; |
| 154 | QSize requestSize; |
| 155 | QUrl url; |
| 156 | |
| 157 | bool loading; |
| 158 | QQuickImageProviderOptions providerOptions; |
| 159 | |
| 160 | class Event : public QEvent { |
| 161 | Q_EVENT_DISABLE_COPY(Event); |
| 162 | public: |
| 163 | Event(ReadError, const QString &, const QSize &, QQuickTextureFactory *factory); |
| 164 | ~Event(); |
| 165 | |
| 166 | ReadError error; |
| 167 | QString errorString; |
| 168 | QSize implicitSize; |
| 169 | QQuickTextureFactory *textureFactory; |
| 170 | }; |
| 171 | void postReply(ReadError, const QString &, const QSize &, QQuickTextureFactory *factory); |
| 172 | |
| 173 | |
| 174 | Q_SIGNALS: |
| 175 | void finished(); |
| 176 | void downloadProgress(qint64, qint64); |
| 177 | |
| 178 | protected: |
| 179 | bool event(QEvent *event) override; |
| 180 | |
| 181 | private: |
| 182 | Q_DISABLE_COPY(QQuickPixmapReply) |
| 183 | |
| 184 | public: |
| 185 | static int finishedMethodIndex; |
| 186 | static int downloadProgressMethodIndex; |
| 187 | }; |
| 188 | |
| 189 | /*! \internal |
| 190 | Serves as an endpoint for notifications on the connected reader's thread, thus enforcing |
| 191 | execution of their continuation on the thread. */ |
| 192 | class ReaderThreadExecutionEnforcer : public QObject |
| 193 | { |
| 194 | Q_OBJECT |
| 195 | public: |
| 196 | enum Event { |
| 197 | ProcessJobs = QEvent::User, |
| 198 | }; |
| 199 | |
| 200 | ReaderThreadExecutionEnforcer(QQuickPixmapReader *reader); |
| 201 | |
| 202 | /*! \internal |
| 203 | Forces the execution of processJobs() on the original reader on the thread it's running on. |
| 204 | */ |
| 205 | void processJobsOnReaderThreadLater(); |
| 206 | |
| 207 | public slots: |
| 208 | void asyncResponseFinished(QQuickImageResponse *response); |
| 209 | void asyncResponseFinished(); |
| 210 | private slots: |
| 211 | void networkRequestDone(); |
| 212 | private: |
| 213 | Q_DISABLE_COPY(ReaderThreadExecutionEnforcer) |
| 214 | bool event(QEvent *e) override; |
| 215 | |
| 216 | QQuickPixmapReader *reader; |
| 217 | }; |
| 218 | |
| 219 | class QQuickPixmapData; |
| 220 | class QQuickPixmapReader : public QThread |
| 221 | { |
| 222 | Q_OBJECT |
| 223 | public: |
| 224 | QQuickPixmapReader(QQmlEngine *eng); |
| 225 | ~QQuickPixmapReader(); |
| 226 | |
| 227 | QQuickPixmapReply *getImage(QQuickPixmapData *); |
| 228 | void cancel(QQuickPixmapReply *rep); |
| 229 | |
| 230 | static QQuickPixmapReader *instance(QQmlEngine *engine); |
| 231 | static QQuickPixmapReader *existingInstance(QQmlEngine *engine); |
| 232 | void startJob(QQuickPixmapReply *job); |
| 233 | |
| 234 | protected: |
| 235 | void run() override; |
| 236 | |
| 237 | private: |
| 238 | Q_DISABLE_COPY(QQuickPixmapReader) |
| 239 | friend class ReaderThreadExecutionEnforcer; |
| 240 | void processJobs(); |
| 241 | void processJob(QQuickPixmapReply *, const QUrl &, const QString &, QQuickImageProvider::ImageType, const QSharedPointer<QQuickImageProvider> &); |
| 242 | #if QT_CONFIG(qml_network) |
| 243 | void networkRequestDone(QNetworkReply *); |
| 244 | #endif |
| 245 | void asyncResponseFinished(QQuickImageResponse *); |
| 246 | |
| 247 | QList<QQuickPixmapReply*> jobs; |
| 248 | QList<QQuickPixmapReply *> cancelledJobs; |
| 249 | QQmlEngine *engine; |
| 250 | |
| 251 | #if QT_CONFIG(quick_pixmap_cache_threaded_download) |
| 252 | /*! \internal |
| 253 | Returns a pointer to the thread object owned by the run loop in QQuickPixmapReader::run. |
| 254 | */ |
| 255 | ReaderThreadExecutionEnforcer *readerThreadExecutionEnforcer() |
| 256 | { |
| 257 | return runLoopReaderThreadExecutionEnforcer; |
| 258 | } |
| 259 | QObject *eventLoopQuitHack; |
| 260 | QMutex mutex; |
| 261 | ReaderThreadExecutionEnforcer *runLoopReaderThreadExecutionEnforcer = nullptr; |
| 262 | #else |
| 263 | /*! \internal |
| 264 | Returns a pointer to the thread object owned by this instance. |
| 265 | */ |
| 266 | ReaderThreadExecutionEnforcer *readerThreadExecutionEnforcer() |
| 267 | { |
| 268 | return ownedReaderThreadExecutionEnforcer.get(); |
| 269 | } |
| 270 | std::unique_ptr<ReaderThreadExecutionEnforcer> ownedReaderThreadExecutionEnforcer; |
| 271 | #endif |
| 272 | |
| 273 | #if QT_CONFIG(qml_network) |
| 274 | QNetworkAccessManager *networkAccessManager(); |
| 275 | QNetworkAccessManager *accessManager; |
| 276 | QHash<QNetworkReply*,QQuickPixmapReply*> networkJobs; |
| 277 | #endif |
| 278 | QHash<QQuickImageResponse*,QQuickPixmapReply*> asyncResponses; |
| 279 | |
| 280 | Q_THREAD_AFFINITY_MARKER(m_creatorThreadAffinityMarker); |
| 281 | Q_THREAD_AFFINITY_MARKER(m_readerThreadAffinityMarker); |
| 282 | |
| 283 | static int replyDownloadProgressMethodIndex; |
| 284 | static int replyFinishedMethodIndex; |
| 285 | static int downloadProgressMethodIndex; |
| 286 | static int threadNetworkRequestDoneMethodIndex; |
| 287 | static QHash<QQmlEngine *,QQuickPixmapReader*> readers; |
| 288 | public: |
| 289 | static QMutex readerMutex; |
| 290 | }; |
| 291 | |
| 292 | #if QT_CONFIG(quick_pixmap_cache_threaded_download) |
| 293 | # define PIXMAP_READER_LOCK() QMutexLocker locker(&mutex) |
| 294 | #else |
| 295 | # define PIXMAP_READER_LOCK() |
| 296 | #endif |
| 297 | |
| 298 | class QQuickPixmapCache; |
| 299 | |
| 300 | /*! \internal |
| 301 | The private storage for QQuickPixmap. |
| 302 | */ |
| 303 | class QQuickPixmapData |
| 304 | { |
| 305 | public: |
| 306 | QQuickPixmapData(const QUrl &u, const QRect &r, const QSize &rs, |
| 307 | const QQuickImageProviderOptions &po, const QString &e) |
| 308 | : refCount(1), frameCount(1), frame(0), inCache(false), fromSpecialDevice(false), pixmapStatus(QQuickPixmap::Error), |
| 309 | url(u), errorString(e), requestRegion(r), requestSize(rs), |
| 310 | providerOptions(po), appliedTransform(QQuickImageProviderOptions::UsePluginDefaultTransform), |
| 311 | textureFactory(nullptr), reply(nullptr), prevUnreferenced(nullptr), |
| 312 | prevUnreferencedPtr(nullptr), nextUnreferenced(nullptr) |
| 313 | #ifdef Q_OS_WEBOS |
| 314 | , storeToCache(true) |
| 315 | #endif |
| 316 | { |
| 317 | } |
| 318 | |
| 319 | QQuickPixmapData(const QUrl &u, const QRect &r, const QSize &s, const QQuickImageProviderOptions &po, |
| 320 | QQuickImageProviderOptions::AutoTransform aTransform, int frame=0, int frameCount=1) |
| 321 | : refCount(1), frameCount(frameCount), frame(frame), inCache(false), fromSpecialDevice(false), pixmapStatus(QQuickPixmap::Loading), |
| 322 | url(u), requestRegion(r), requestSize(s), |
| 323 | providerOptions(po), appliedTransform(aTransform), |
| 324 | textureFactory(nullptr), reply(nullptr), prevUnreferenced(nullptr), prevUnreferencedPtr(nullptr), |
| 325 | nextUnreferenced(nullptr) |
| 326 | #ifdef Q_OS_WEBOS |
| 327 | , storeToCache(true) |
| 328 | #endif |
| 329 | { |
| 330 | } |
| 331 | |
| 332 | QQuickPixmapData(const QUrl &u, QQuickTextureFactory *texture, |
| 333 | const QSize &s, const QRect &r, const QSize &rs, const QQuickImageProviderOptions &po, |
| 334 | QQuickImageProviderOptions::AutoTransform aTransform, int frame=0, int frameCount=1) |
| 335 | : refCount(1), frameCount(frameCount), frame(frame), inCache(false), fromSpecialDevice(false), pixmapStatus(QQuickPixmap::Ready), |
| 336 | url(u), implicitSize(s), requestRegion(r), requestSize(rs), |
| 337 | providerOptions(po), appliedTransform(aTransform), |
| 338 | textureFactory(texture), reply(nullptr), prevUnreferenced(nullptr), |
| 339 | prevUnreferencedPtr(nullptr), nextUnreferenced(nullptr) |
| 340 | #ifdef Q_OS_WEBOS |
| 341 | , storeToCache(true) |
| 342 | #endif |
| 343 | { |
| 344 | } |
| 345 | |
| 346 | QQuickPixmapData(QQuickTextureFactory *texture) |
| 347 | : refCount(1), frameCount(1), frame(0), inCache(false), fromSpecialDevice(false), pixmapStatus(QQuickPixmap::Ready), |
| 348 | appliedTransform(QQuickImageProviderOptions::UsePluginDefaultTransform), |
| 349 | textureFactory(texture), reply(nullptr), prevUnreferenced(nullptr), |
| 350 | prevUnreferencedPtr(nullptr), nextUnreferenced(nullptr) |
| 351 | #ifdef Q_OS_WEBOS |
| 352 | , storeToCache(true) |
| 353 | #endif |
| 354 | { |
| 355 | if (texture) |
| 356 | requestSize = implicitSize = texture->textureSize(); |
| 357 | } |
| 358 | |
| 359 | ~QQuickPixmapData() |
| 360 | { |
| 361 | delete textureFactory; |
| 362 | } |
| 363 | |
| 364 | int cost() const; |
| 365 | void addref(); |
| 366 | void release(QQuickPixmapCache *store = nullptr); |
| 367 | void addToCache(); |
| 368 | void removeFromCache(QQuickPixmapCache *store = nullptr); |
| 369 | |
| 370 | uint refCount; |
| 371 | int frameCount; |
| 372 | int frame; |
| 373 | |
| 374 | bool inCache:1; |
| 375 | bool fromSpecialDevice:1; |
| 376 | |
| 377 | QQuickPixmap::Status pixmapStatus; |
| 378 | QUrl url; |
| 379 | QString errorString; |
| 380 | QSize implicitSize; |
| 381 | QRect requestRegion; |
| 382 | QSize requestSize; |
| 383 | QQuickImageProviderOptions providerOptions; |
| 384 | QQuickImageProviderOptions::AutoTransform appliedTransform; |
| 385 | QColorSpace targetColorSpace; |
| 386 | |
| 387 | QPointer<QIODevice> specialDevice; |
| 388 | |
| 389 | // actual image data, after loading |
| 390 | QQuickTextureFactory *textureFactory; |
| 391 | |
| 392 | QQuickPixmapReply *reply; |
| 393 | |
| 394 | // prev/next pointers to form a linked list for dereferencing pixmaps that are currently unused |
| 395 | // (those get lazily deleted in QQuickPixmapCache::shrinkCache()) |
| 396 | QQuickPixmapData *prevUnreferenced; |
| 397 | QQuickPixmapData**prevUnreferencedPtr; |
| 398 | QQuickPixmapData *nextUnreferenced; |
| 399 | |
| 400 | #ifdef Q_OS_WEBOS |
| 401 | bool storeToCache; |
| 402 | #endif |
| 403 | |
| 404 | private: |
| 405 | Q_DISABLE_COPY(QQuickPixmapData) |
| 406 | }; |
| 407 | |
| 408 | int QQuickPixmapReply::finishedMethodIndex = -1; |
| 409 | int QQuickPixmapReply::downloadProgressMethodIndex = -1; |
| 410 | |
| 411 | // XXX |
| 412 | QHash<QQmlEngine *,QQuickPixmapReader*> QQuickPixmapReader::readers; |
| 413 | QMutex QQuickPixmapReader::readerMutex; |
| 414 | |
| 415 | int QQuickPixmapReader::replyDownloadProgressMethodIndex = -1; |
| 416 | int QQuickPixmapReader::replyFinishedMethodIndex = -1; |
| 417 | int QQuickPixmapReader::downloadProgressMethodIndex = -1; |
| 418 | int QQuickPixmapReader::threadNetworkRequestDoneMethodIndex = -1; |
| 419 | |
| 420 | void QQuickPixmapReply::postReply(ReadError error, const QString &errorString, |
| 421 | const QSize &implicitSize, QQuickTextureFactory *factory) |
| 422 | { |
| 423 | loading = false; |
| 424 | QCoreApplication::postEvent(receiver: this, event: new Event(error, errorString, implicitSize, factory)); |
| 425 | } |
| 426 | |
| 427 | QQuickPixmapReply::Event::Event(ReadError e, const QString &s, const QSize &iSize, QQuickTextureFactory *factory) |
| 428 | : QEvent(QEvent::User), error(e), errorString(s), implicitSize(iSize), textureFactory(factory) |
| 429 | { |
| 430 | } |
| 431 | |
| 432 | QQuickPixmapReply::Event::~Event() |
| 433 | { |
| 434 | delete textureFactory; |
| 435 | } |
| 436 | |
| 437 | #if QT_CONFIG(qml_network) |
| 438 | QNetworkAccessManager *QQuickPixmapReader::networkAccessManager() |
| 439 | { |
| 440 | if (!accessManager) { |
| 441 | Q_ASSERT(readerThreadExecutionEnforcer()); |
| 442 | accessManager = QQmlEnginePrivate::get(e: engine)->createNetworkAccessManager( |
| 443 | parent: readerThreadExecutionEnforcer()); |
| 444 | } |
| 445 | return accessManager; |
| 446 | } |
| 447 | #endif |
| 448 | |
| 449 | static void maybeRemoveAlpha(QImage *image) |
| 450 | { |
| 451 | // If the image |
| 452 | if (image->hasAlphaChannel() && image->data_ptr() |
| 453 | && !image->data_ptr()->checkForAlphaPixels()) { |
| 454 | switch (image->format()) { |
| 455 | case QImage::Format_RGBA8888: |
| 456 | case QImage::Format_RGBA8888_Premultiplied: |
| 457 | if (image->data_ptr()->convertInPlace(newFormat: QImage::Format_RGBX8888, Qt::AutoColor)) |
| 458 | break; |
| 459 | |
| 460 | *image = image->convertToFormat(f: QImage::Format_RGBX8888); |
| 461 | break; |
| 462 | case QImage::Format_A2BGR30_Premultiplied: |
| 463 | if (image->data_ptr()->convertInPlace(newFormat: QImage::Format_BGR30, Qt::AutoColor)) |
| 464 | break; |
| 465 | |
| 466 | *image = image->convertToFormat(f: QImage::Format_BGR30); |
| 467 | break; |
| 468 | case QImage::Format_A2RGB30_Premultiplied: |
| 469 | if (image->data_ptr()->convertInPlace(newFormat: QImage::Format_RGB30, Qt::AutoColor)) |
| 470 | break; |
| 471 | |
| 472 | *image = image->convertToFormat(f: QImage::Format_RGB30); |
| 473 | break; |
| 474 | default: |
| 475 | if (image->data_ptr()->convertInPlace(newFormat: QImage::Format_RGB32, Qt::AutoColor)) |
| 476 | break; |
| 477 | |
| 478 | *image = image->convertToFormat(f: QImage::Format_RGB32); |
| 479 | break; |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | static bool readImage(const QUrl& url, QIODevice *dev, QImage *image, QString *errorString, QSize *impsize, int *frameCount, |
| 485 | const QRect &requestRegion, const QSize &requestSize, const QQuickImageProviderOptions &providerOptions, |
| 486 | QQuickImageProviderOptions::AutoTransform *appliedTransform = nullptr, int frame = 0, |
| 487 | qreal devicePixelRatio = 1.0) |
| 488 | { |
| 489 | QImageReader imgio(dev); |
| 490 | if (providerOptions.autoTransform() != QQuickImageProviderOptions::UsePluginDefaultTransform) |
| 491 | imgio.setAutoTransform(providerOptions.autoTransform() == QQuickImageProviderOptions::ApplyTransform); |
| 492 | else if (appliedTransform) |
| 493 | *appliedTransform = imgio.autoTransform() ? QQuickImageProviderOptions::ApplyTransform : QQuickImageProviderOptions::DoNotApplyTransform; |
| 494 | |
| 495 | if (frame < imgio.imageCount()) |
| 496 | imgio.jumpToImage(imageNumber: frame); |
| 497 | |
| 498 | if (frameCount) |
| 499 | *frameCount = imgio.imageCount(); |
| 500 | |
| 501 | QSize scSize = QQuickImageProviderWithOptions::loadSize(originalSize: imgio.size(), requestedSize: requestSize, format: imgio.format(), options: providerOptions, devicePixelRatio); |
| 502 | if (scSize.isValid()) |
| 503 | imgio.setScaledSize(scSize); |
| 504 | if (!requestRegion.isNull()) |
| 505 | imgio.setScaledClipRect(requestRegion); |
| 506 | const QSize originalSize = imgio.size(); |
| 507 | qCDebug(lcImg) << url << "frame" << frame << "of" << imgio.imageCount() |
| 508 | << "requestRegion" << requestRegion << "QImageReader size" << originalSize << "-> scSize" << scSize; |
| 509 | |
| 510 | if (impsize) |
| 511 | *impsize = originalSize; |
| 512 | |
| 513 | if (imgio.read(image)) { |
| 514 | maybeRemoveAlpha(image); |
| 515 | if (impsize && impsize->width() < 0) |
| 516 | *impsize = image->size(); |
| 517 | if (providerOptions.targetColorSpace().isValid()) { |
| 518 | if (image->colorSpace().isValid()) |
| 519 | image->convertToColorSpace(colorSpace: providerOptions.targetColorSpace()); |
| 520 | else |
| 521 | image->setColorSpace(providerOptions.targetColorSpace()); |
| 522 | } |
| 523 | return true; |
| 524 | } else { |
| 525 | if (errorString) |
| 526 | *errorString = QQuickPixmap::tr(sourceText: "Error decoding: %1: %2" ).arg(a: url.toString()) |
| 527 | .arg(a: imgio.errorString()); |
| 528 | return false; |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | static QStringList fromLatin1List(const QList<QByteArray> &list) |
| 533 | { |
| 534 | QStringList res; |
| 535 | res.reserve(asize: list.size()); |
| 536 | for (const QByteArray &item : list) |
| 537 | res.append(t: QString::fromLatin1(ba: item)); |
| 538 | return res; |
| 539 | } |
| 540 | |
| 541 | class BackendSupport |
| 542 | { |
| 543 | public: |
| 544 | BackendSupport() |
| 545 | { |
| 546 | delete QSGContext::createTextureFactoryFromImage(image: QImage()); // Force init of backend data |
| 547 | hasOpenGL = QQuickWindow::sceneGraphBackend().isEmpty(); // i.e. default |
| 548 | QList<QByteArray> list; |
| 549 | if (hasOpenGL) |
| 550 | list.append(other: QSGTextureReader::supportedFileFormats()); |
| 551 | list.append(other: QImageReader::supportedImageFormats()); |
| 552 | fileSuffixes = fromLatin1List(list); |
| 553 | } |
| 554 | bool hasOpenGL; |
| 555 | QStringList fileSuffixes; |
| 556 | private: |
| 557 | Q_DISABLE_COPY(BackendSupport) |
| 558 | }; |
| 559 | Q_GLOBAL_STATIC(BackendSupport, backendSupport); |
| 560 | |
| 561 | static QString existingImageFileForPath(const QString &localFile) |
| 562 | { |
| 563 | // Do nothing if given filepath exists or already has a suffix |
| 564 | QFileInfo fi(localFile); |
| 565 | if (!fi.suffix().isEmpty() || fi.exists()) |
| 566 | return localFile; |
| 567 | |
| 568 | QString tryFile = localFile + QStringLiteral(".xxxx" ); |
| 569 | const int suffixIdx = localFile.size() + 1; |
| 570 | for (const QString &suffix : backendSupport()->fileSuffixes) { |
| 571 | tryFile.replace(i: suffixIdx, len: 10, after: suffix); |
| 572 | if (QFileInfo::exists(file: tryFile)) |
| 573 | return tryFile; |
| 574 | } |
| 575 | return localFile; |
| 576 | } |
| 577 | |
| 578 | QQuickPixmapReader::QQuickPixmapReader(QQmlEngine *eng) |
| 579 | : QThread(eng), engine(eng) |
| 580 | #if QT_CONFIG(qml_network) |
| 581 | , accessManager(nullptr) |
| 582 | #endif |
| 583 | { |
| 584 | Q_DETACH_THREAD_AFFINITY_MARKER(m_readerThreadAffinityMarker); |
| 585 | #if QT_CONFIG(quick_pixmap_cache_threaded_download) |
| 586 | eventLoopQuitHack = new QObject; |
| 587 | eventLoopQuitHack->moveToThread(thread: this); |
| 588 | QObject::connect(sender: eventLoopQuitHack, signal: &QObject::destroyed, context: this, slot: &QThread::quit, type: Qt::DirectConnection); |
| 589 | start(QThread::LowestPriority); |
| 590 | #else |
| 591 | run(); // Call nonblocking run for ourselves. |
| 592 | #endif |
| 593 | } |
| 594 | |
| 595 | QQuickPixmapReader::~QQuickPixmapReader() |
| 596 | { |
| 597 | Q_ASSERT_CALLED_ON_VALID_THREAD(m_creatorThreadAffinityMarker); |
| 598 | |
| 599 | readerMutex.lock(); |
| 600 | readers.remove(key: engine); |
| 601 | readerMutex.unlock(); |
| 602 | |
| 603 | { |
| 604 | PIXMAP_READER_LOCK(); |
| 605 | // manually cancel all outstanding jobs. |
| 606 | for (QQuickPixmapReply *reply : std::as_const(t&: jobs)) { |
| 607 | if (reply->data && reply->data->reply == reply) |
| 608 | reply->data->reply = nullptr; |
| 609 | delete reply; |
| 610 | } |
| 611 | jobs.clear(); |
| 612 | const auto cancelJob = [this](QQuickPixmapReply *reply) { |
| 613 | if (reply->loading) { |
| 614 | cancelledJobs.append(t: reply); |
| 615 | reply->data = nullptr; |
| 616 | } |
| 617 | }; |
| 618 | #if QT_CONFIG(qml_network) |
| 619 | for (auto *reply : std::as_const(t&: networkJobs)) |
| 620 | cancelJob(reply); |
| 621 | #endif |
| 622 | for (auto *reply : std::as_const(t&: asyncResponses)) |
| 623 | cancelJob(reply); |
| 624 | #if !QT_CONFIG(quick_pixmap_cache_threaded_download) |
| 625 | // In this case we won't be waiting, but we are on the correct thread already, so we can |
| 626 | // perform housekeeping synchronously now. |
| 627 | processJobs(); |
| 628 | #else // QT_CONFIG(quick_pixmap_cache_threaded_download) is true |
| 629 | // Perform housekeeping on all the jobs cancelled above soon... |
| 630 | if (readerThreadExecutionEnforcer()) |
| 631 | readerThreadExecutionEnforcer()->processJobsOnReaderThreadLater(); |
| 632 | #endif |
| 633 | } |
| 634 | |
| 635 | #if QT_CONFIG(quick_pixmap_cache_threaded_download) |
| 636 | // ... schedule stopping of this thread via the eventLoopQuitHack (processJobs scheduled above |
| 637 | // will run first) ... |
| 638 | eventLoopQuitHack->deleteLater(); |
| 639 | // ... and wait() for it all to finish, as the thread will only quit after eventLoopQuitHack |
| 640 | // has been deleted. |
| 641 | wait(); |
| 642 | #endif |
| 643 | |
| 644 | // While we've been waiting, the other thread may have added |
| 645 | // more replies. No one will care about them anymore. |
| 646 | |
| 647 | auto deleteReply = [](QQuickPixmapReply *reply) { |
| 648 | if (reply->data && reply->data->reply == reply) |
| 649 | reply->data->reply = nullptr; |
| 650 | delete reply; |
| 651 | }; |
| 652 | #if QT_CONFIG(qml_network) |
| 653 | for (QQuickPixmapReply *reply : std::as_const(t&: networkJobs)) |
| 654 | deleteReply(reply); |
| 655 | #endif |
| 656 | for (QQuickPixmapReply *reply : std::as_const(t&: asyncResponses)) |
| 657 | deleteReply(reply); |
| 658 | |
| 659 | #if QT_CONFIG(qml_network) |
| 660 | networkJobs.clear(); |
| 661 | #endif |
| 662 | asyncResponses.clear(); |
| 663 | } |
| 664 | |
| 665 | #if QT_CONFIG(qml_network) |
| 666 | void QQuickPixmapReader::networkRequestDone(QNetworkReply *reply) |
| 667 | { |
| 668 | Q_ASSERT_CALLED_ON_VALID_THREAD(m_readerThreadAffinityMarker); |
| 669 | |
| 670 | QQuickPixmapReply *job = networkJobs.take(key: reply); |
| 671 | |
| 672 | if (job) { |
| 673 | QImage image; |
| 674 | QQuickPixmapReply::ReadError error = QQuickPixmapReply::NoError; |
| 675 | QString errorString; |
| 676 | QSize readSize; |
| 677 | QQuickTextureFactory *factory = nullptr; |
| 678 | if (reply->error()) { |
| 679 | error = QQuickPixmapReply::Loading; |
| 680 | errorString = reply->errorString(); |
| 681 | } else { |
| 682 | QByteArray all = reply->readAll(); |
| 683 | QBuffer buff(&all); |
| 684 | buff.open(openMode: QIODevice::ReadOnly); |
| 685 | QSGTextureReader texReader(&buff, reply->url().fileName()); |
| 686 | if (backendSupport()->hasOpenGL && texReader.isTexture()) { |
| 687 | factory = texReader.read(); |
| 688 | if (factory) { |
| 689 | readSize = factory->textureSize(); |
| 690 | } else { |
| 691 | error = QQuickPixmapReply::Decoding; |
| 692 | errorString = QQuickPixmap::tr(sourceText: "Error decoding: %1" ).arg(a: reply->url().toString()); |
| 693 | } |
| 694 | } else { |
| 695 | int frameCount; |
| 696 | int const frame = job->data ? job->data->frame : 0; |
| 697 | if (!readImage(url: reply->url(), dev: &buff, image: &image, errorString: &errorString, impsize: &readSize, frameCount: &frameCount, |
| 698 | requestRegion: job->requestRegion, requestSize: job->requestSize, providerOptions: job->providerOptions, appliedTransform: nullptr, frame)) |
| 699 | error = QQuickPixmapReply::Decoding; |
| 700 | else if (job->data) |
| 701 | job->data->frameCount = frameCount; |
| 702 | } |
| 703 | } |
| 704 | // send completion event to the QQuickPixmapReply |
| 705 | if (!factory) |
| 706 | factory = QQuickTextureFactory::textureFactoryForImage(image); |
| 707 | |
| 708 | PIXMAP_READER_LOCK(); |
| 709 | if (!cancelledJobs.contains(t: job)) |
| 710 | job->postReply(error, errorString, implicitSize: readSize, factory); |
| 711 | } |
| 712 | reply->deleteLater(); |
| 713 | |
| 714 | // kick off event loop again in case we have dropped below max request count |
| 715 | readerThreadExecutionEnforcer()->processJobsOnReaderThreadLater(); |
| 716 | } |
| 717 | #endif // qml_network |
| 718 | |
| 719 | void QQuickPixmapReader::asyncResponseFinished(QQuickImageResponse *response) |
| 720 | { |
| 721 | Q_ASSERT_CALLED_ON_VALID_THREAD(m_readerThreadAffinityMarker); |
| 722 | |
| 723 | QQuickPixmapReply *job = asyncResponses.take(key: response); |
| 724 | |
| 725 | if (job) { |
| 726 | QQuickTextureFactory *t = nullptr; |
| 727 | QQuickPixmapReply::ReadError error = QQuickPixmapReply::NoError; |
| 728 | QString errorString; |
| 729 | if (!response->errorString().isEmpty()) { |
| 730 | error = QQuickPixmapReply::Loading; |
| 731 | errorString = response->errorString(); |
| 732 | } else { |
| 733 | t = response->textureFactory(); |
| 734 | } |
| 735 | |
| 736 | PIXMAP_READER_LOCK(); |
| 737 | if (!cancelledJobs.contains(t: job)) |
| 738 | job->postReply(error, errorString, implicitSize: t ? t->textureSize() : QSize(), factory: t); |
| 739 | else |
| 740 | delete t; |
| 741 | } |
| 742 | response->deleteLater(); |
| 743 | |
| 744 | // kick off event loop again in case we have dropped below max request count |
| 745 | readerThreadExecutionEnforcer()->processJobsOnReaderThreadLater(); |
| 746 | } |
| 747 | |
| 748 | ReaderThreadExecutionEnforcer::ReaderThreadExecutionEnforcer(QQuickPixmapReader *i) : reader(i) { } |
| 749 | |
| 750 | void ReaderThreadExecutionEnforcer::processJobsOnReaderThreadLater() |
| 751 | { |
| 752 | QCoreApplication::postEvent( |
| 753 | receiver: this, event: new QEvent(QEvent::Type(ReaderThreadExecutionEnforcer::ProcessJobs))); |
| 754 | } |
| 755 | |
| 756 | bool ReaderThreadExecutionEnforcer::event(QEvent *e) |
| 757 | { |
| 758 | switch (e->type()) { |
| 759 | case QEvent::Type(ReaderThreadExecutionEnforcer::ProcessJobs): |
| 760 | reader->processJobs(); |
| 761 | return true; |
| 762 | default: |
| 763 | return QObject::event(event: e); |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | void ReaderThreadExecutionEnforcer::networkRequestDone() |
| 768 | { |
| 769 | #if QT_CONFIG(qml_network) |
| 770 | QNetworkReply *reply = static_cast<QNetworkReply *>(sender()); |
| 771 | reader->networkRequestDone(reply); |
| 772 | #endif |
| 773 | } |
| 774 | |
| 775 | void ReaderThreadExecutionEnforcer::asyncResponseFinished(QQuickImageResponse *response) |
| 776 | { |
| 777 | reader->asyncResponseFinished(response); |
| 778 | } |
| 779 | |
| 780 | void ReaderThreadExecutionEnforcer::asyncResponseFinished() |
| 781 | { |
| 782 | QQuickImageResponse *response = static_cast<QQuickImageResponse *>(sender()); |
| 783 | asyncResponseFinished(response); |
| 784 | } |
| 785 | |
| 786 | void QQuickPixmapReader::processJobs() |
| 787 | { |
| 788 | Q_ASSERT_CALLED_ON_VALID_THREAD(m_readerThreadAffinityMarker); |
| 789 | |
| 790 | PIXMAP_READER_LOCK(); |
| 791 | while (true) { |
| 792 | if (cancelledJobs.isEmpty() && jobs.isEmpty()) |
| 793 | return; // Nothing else to do |
| 794 | |
| 795 | // Clean cancelled jobs |
| 796 | if (!cancelledJobs.isEmpty()) { |
| 797 | for (int i = 0; i < cancelledJobs.size(); ++i) { |
| 798 | QQuickPixmapReply *job = cancelledJobs.at(i); |
| 799 | #if QT_CONFIG(qml_network) |
| 800 | QNetworkReply *reply = networkJobs.key(value: job, defaultKey: 0); |
| 801 | if (reply) { |
| 802 | networkJobs.remove(key: reply); |
| 803 | if (reply->isRunning()) { |
| 804 | // cancel any jobs already started |
| 805 | reply->close(); |
| 806 | } |
| 807 | } else |
| 808 | #endif |
| 809 | { |
| 810 | QQuickImageResponse *asyncResponse = asyncResponses.key(value: job); |
| 811 | if (asyncResponse) { |
| 812 | asyncResponses.remove(key: asyncResponse); |
| 813 | asyncResponse->cancel(); |
| 814 | } |
| 815 | } |
| 816 | PIXMAP_PROFILE(pixmapStateChanged<QQuickProfiler::PixmapLoadingError>(job->url)); |
| 817 | // deleteLater, since not owned by this thread |
| 818 | job->deleteLater(); |
| 819 | } |
| 820 | cancelledJobs.clear(); |
| 821 | } |
| 822 | |
| 823 | if (!jobs.isEmpty()) { |
| 824 | // Find a job we can use |
| 825 | bool usableJob = false; |
| 826 | for (int i = jobs.size() - 1; !usableJob && i >= 0; i--) { |
| 827 | QQuickPixmapReply *job = jobs.at(i); |
| 828 | const QUrl url = job->url; |
| 829 | QString localFile; |
| 830 | QQuickImageProvider::ImageType imageType = QQuickImageProvider::Invalid; |
| 831 | QSharedPointer<QQuickImageProvider> provider; |
| 832 | |
| 833 | if (url.scheme() == QLatin1String("image" )) { |
| 834 | QQmlEnginePrivate *enginePrivate = QQmlEnginePrivate::get(e: engine); |
| 835 | provider = enginePrivate->imageProvider(providerId: imageProviderId(url)).staticCast<QQuickImageProvider>(); |
| 836 | if (provider) |
| 837 | imageType = provider->imageType(); |
| 838 | |
| 839 | usableJob = true; |
| 840 | } else { |
| 841 | localFile = QQmlFile::urlToLocalFileOrQrc(url); |
| 842 | usableJob = !localFile.isEmpty() |
| 843 | #if QT_CONFIG(qml_network) |
| 844 | || networkJobs.size() < IMAGEREQUEST_MAX_NETWORK_REQUEST_COUNT |
| 845 | #endif |
| 846 | ; |
| 847 | } |
| 848 | |
| 849 | if (usableJob) { |
| 850 | jobs.removeAt(i); |
| 851 | |
| 852 | job->loading = true; |
| 853 | |
| 854 | PIXMAP_PROFILE(pixmapStateChanged<QQuickProfiler::PixmapLoadingStarted>(url)); |
| 855 | |
| 856 | #if QT_CONFIG(quick_pixmap_cache_threaded_download) |
| 857 | locker.unlock(); |
| 858 | auto relockMutexGuard = qScopeGuard(f: ([&locker]() { |
| 859 | locker.relock(); |
| 860 | })); |
| 861 | #endif |
| 862 | processJob(job, url, localFile, imageType, provider); |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | if (!usableJob) |
| 867 | return; |
| 868 | } |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | void QQuickPixmapReader::processJob(QQuickPixmapReply *runningJob, const QUrl &url, const QString &localFile, |
| 873 | QQuickImageProvider::ImageType imageType, const QSharedPointer<QQuickImageProvider> &provider) |
| 874 | { |
| 875 | Q_ASSERT_CALLED_ON_VALID_THREAD(m_readerThreadAffinityMarker); |
| 876 | |
| 877 | // fetch |
| 878 | if (url.scheme() == QLatin1String("image" )) { |
| 879 | // Use QQuickImageProvider |
| 880 | QSize readSize; |
| 881 | |
| 882 | if (imageType == QQuickImageProvider::Invalid) { |
| 883 | QString errorStr = QQuickPixmap::tr(sourceText: "Invalid image provider: %1" ).arg(a: url.toString()); |
| 884 | PIXMAP_READER_LOCK(); |
| 885 | if (!cancelledJobs.contains(t: runningJob)) |
| 886 | runningJob->postReply(error: QQuickPixmapReply::Loading, errorString: errorStr, implicitSize: readSize, factory: nullptr); |
| 887 | return; |
| 888 | } |
| 889 | |
| 890 | // This is safe because we ensure that provider does outlive providerV2 and it does not escape the function |
| 891 | QQuickImageProviderWithOptions *providerV2 = QQuickImageProviderWithOptions::checkedCast(provider: provider.get()); |
| 892 | |
| 893 | switch (imageType) { |
| 894 | case QQuickImageProvider::Invalid: |
| 895 | { |
| 896 | // Already handled |
| 897 | break; |
| 898 | } |
| 899 | |
| 900 | case QQuickImageProvider::Image: |
| 901 | { |
| 902 | QImage image; |
| 903 | if (providerV2) { |
| 904 | image = providerV2->requestImage(id: imageId(url), size: &readSize, requestedSize: runningJob->requestSize, options: runningJob->providerOptions); |
| 905 | } else { |
| 906 | image = provider->requestImage(id: imageId(url), size: &readSize, requestedSize: runningJob->requestSize); |
| 907 | } |
| 908 | QQuickPixmapReply::ReadError errorCode = QQuickPixmapReply::NoError; |
| 909 | QString errorStr; |
| 910 | if (image.isNull()) { |
| 911 | errorCode = QQuickPixmapReply::Loading; |
| 912 | errorStr = QQuickPixmap::tr(sourceText: "Failed to get image from provider: %1" ).arg(a: url.toString()); |
| 913 | } |
| 914 | PIXMAP_READER_LOCK(); |
| 915 | if (!cancelledJobs.contains(t: runningJob)) { |
| 916 | runningJob->postReply(error: errorCode, errorString: errorStr, implicitSize: readSize, |
| 917 | factory: QQuickTextureFactory::textureFactoryForImage(image)); |
| 918 | } |
| 919 | break; |
| 920 | } |
| 921 | |
| 922 | case QQuickImageProvider::Pixmap: |
| 923 | { |
| 924 | QPixmap pixmap; |
| 925 | if (providerV2) { |
| 926 | pixmap = providerV2->requestPixmap(id: imageId(url), size: &readSize, requestedSize: runningJob->requestSize, options: runningJob->providerOptions); |
| 927 | } else { |
| 928 | pixmap = provider->requestPixmap(id: imageId(url), size: &readSize, requestedSize: runningJob->requestSize); |
| 929 | } |
| 930 | QQuickPixmapReply::ReadError errorCode = QQuickPixmapReply::NoError; |
| 931 | QString errorStr; |
| 932 | if (pixmap.isNull()) { |
| 933 | errorCode = QQuickPixmapReply::Loading; |
| 934 | errorStr = QQuickPixmap::tr(sourceText: "Failed to get image from provider: %1" ).arg(a: url.toString()); |
| 935 | } |
| 936 | |
| 937 | PIXMAP_READER_LOCK(); |
| 938 | if (!cancelledJobs.contains(t: runningJob)) { |
| 939 | runningJob->postReply( |
| 940 | error: errorCode, errorString: errorStr, implicitSize: readSize, |
| 941 | factory: QQuickTextureFactory::textureFactoryForImage(image: pixmap.toImage())); |
| 942 | } |
| 943 | break; |
| 944 | } |
| 945 | |
| 946 | case QQuickImageProvider::Texture: |
| 947 | { |
| 948 | QQuickTextureFactory *t; |
| 949 | if (providerV2) { |
| 950 | t = providerV2->requestTexture(id: imageId(url), size: &readSize, requestedSize: runningJob->requestSize, options: runningJob->providerOptions); |
| 951 | } else { |
| 952 | t = provider->requestTexture(id: imageId(url), size: &readSize, requestedSize: runningJob->requestSize); |
| 953 | } |
| 954 | QQuickPixmapReply::ReadError errorCode = QQuickPixmapReply::NoError; |
| 955 | QString errorStr; |
| 956 | if (!t) { |
| 957 | errorCode = QQuickPixmapReply::Loading; |
| 958 | errorStr = QQuickPixmap::tr(sourceText: "Failed to get texture from provider: %1" ).arg(a: url.toString()); |
| 959 | } |
| 960 | PIXMAP_READER_LOCK(); |
| 961 | if (!cancelledJobs.contains(t: runningJob)) |
| 962 | runningJob->postReply(error: errorCode, errorString: errorStr, implicitSize: readSize, factory: t); |
| 963 | else |
| 964 | delete t; |
| 965 | break; |
| 966 | } |
| 967 | |
| 968 | case QQuickImageProvider::ImageResponse: |
| 969 | { |
| 970 | QQuickImageResponse *response; |
| 971 | if (providerV2) { |
| 972 | response = providerV2->requestImageResponse(id: imageId(url), requestedSize: runningJob->requestSize, options: runningJob->providerOptions); |
| 973 | } else { |
| 974 | QQuickAsyncImageProvider *asyncProvider = static_cast<QQuickAsyncImageProvider*>(provider.get()); |
| 975 | response = asyncProvider->requestImageResponse(id: imageId(url), requestedSize: runningJob->requestSize); |
| 976 | } |
| 977 | |
| 978 | { |
| 979 | QObject::connect(sender: response, signal: &QQuickImageResponse::finished, context: readerThreadExecutionEnforcer(), |
| 980 | slot: qOverload<>(&ReaderThreadExecutionEnforcer::asyncResponseFinished)); |
| 981 | // as the response object can outlive the provider QSharedPointer, we have to extend the pointee's lifetime by that of the response |
| 982 | // we do this by capturing a copy of the QSharedPointer in a lambda, and dropping it once the lambda has been called |
| 983 | auto provider_copy = provider; // capturing provider would capture it as a const reference, and copy capture with initializer is only available in C++14 |
| 984 | QObject::connect(sender: response, signal: &QQuickImageResponse::destroyed, context: response, slot: [provider_copy]() { |
| 985 | // provider_copy will be deleted when the connection gets deleted |
| 986 | }); |
| 987 | } |
| 988 | // Might be that the async provider was so quick it emitted the signal before we |
| 989 | // could connect to it. |
| 990 | // |
| 991 | // loadAcquire() synchronizes-with storeRelease() in QQuickImageResponsePrivate::_q_finished(): |
| 992 | if (static_cast<QQuickImageResponsePrivate*>(QObjectPrivate::get(o: response))->finished.loadAcquire()) { |
| 993 | QMetaObject::invokeMethod(obj: readerThreadExecutionEnforcer(), member: "asyncResponseFinished" , |
| 994 | c: Qt::QueuedConnection, |
| 995 | Q_ARG(QQuickImageResponse *, response)); |
| 996 | } |
| 997 | |
| 998 | asyncResponses.insert(key: response, value: runningJob); |
| 999 | break; |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | } else { |
| 1004 | if (!localFile.isEmpty()) { |
| 1005 | // Image is local - load/decode immediately |
| 1006 | QImage image; |
| 1007 | QQuickPixmapReply::ReadError errorCode = QQuickPixmapReply::NoError; |
| 1008 | QString errorStr; |
| 1009 | QSize readSize; |
| 1010 | |
| 1011 | if (runningJob->data && runningJob->data->fromSpecialDevice) { |
| 1012 | auto specialDevice = runningJob->data->specialDevice; |
| 1013 | if (specialDevice.isNull() || QObjectPrivate::get(o: specialDevice.data())->deleteLaterCalled) { |
| 1014 | qCDebug(lcImg) << "readImage job aborted" << url; |
| 1015 | return; |
| 1016 | } |
| 1017 | int frameCount; |
| 1018 | // Ensure that specialDevice's thread affinity is _this_ thread, to avoid deleteLater() |
| 1019 | // deleting prematurely, before readImage() is done. But this is only possible if it has already |
| 1020 | // relinquished its initial thread affinity. |
| 1021 | if (!specialDevice->thread()) { |
| 1022 | qCDebug(lcQsgLeak) << specialDevice.data() << ": changing thread affinity so that" |
| 1023 | << QThread::currentThread() << "will handle any deleteLater() calls" ; |
| 1024 | specialDevice->moveToThread(thread: QThread::currentThread()); |
| 1025 | } |
| 1026 | if (!readImage(url, dev: specialDevice.data(), image: &image, errorString: &errorStr, impsize: &readSize, frameCount: &frameCount, |
| 1027 | requestRegion: runningJob->requestRegion, requestSize: runningJob->requestSize, |
| 1028 | providerOptions: runningJob->providerOptions, appliedTransform: nullptr, frame: runningJob->data->frame)) { |
| 1029 | errorCode = QQuickPixmapReply::Loading; |
| 1030 | } else if (runningJob->data) { |
| 1031 | runningJob->data->frameCount = frameCount; |
| 1032 | } |
| 1033 | } else { |
| 1034 | QFile f(existingImageFileForPath(localFile)); |
| 1035 | if (f.open(flags: QIODevice::ReadOnly)) { |
| 1036 | QSGTextureReader texReader(&f, localFile); |
| 1037 | if (backendSupport()->hasOpenGL && texReader.isTexture()) { |
| 1038 | QQuickTextureFactory *factory = texReader.read(); |
| 1039 | if (factory) { |
| 1040 | readSize = factory->textureSize(); |
| 1041 | } else { |
| 1042 | errorStr = QQuickPixmap::tr(sourceText: "Error decoding: %1" ).arg(a: url.toString()); |
| 1043 | if (f.fileName() != localFile) |
| 1044 | errorStr += QString::fromLatin1(ba: " (%1)" ).arg(a: f.fileName()); |
| 1045 | errorCode = QQuickPixmapReply::Decoding; |
| 1046 | } |
| 1047 | PIXMAP_READER_LOCK(); |
| 1048 | if (!cancelledJobs.contains(t: runningJob)) |
| 1049 | runningJob->postReply(error: errorCode, errorString: errorStr, implicitSize: readSize, factory); |
| 1050 | return; |
| 1051 | } else { |
| 1052 | int frameCount; |
| 1053 | int const frame = runningJob->data ? runningJob->data->frame : 0; |
| 1054 | if (!readImage(url, dev: &f, image: &image, errorString: &errorStr, impsize: &readSize, frameCount: &frameCount, |
| 1055 | requestRegion: runningJob->requestRegion, requestSize: runningJob->requestSize, |
| 1056 | providerOptions: runningJob->providerOptions, appliedTransform: nullptr, frame)) { |
| 1057 | errorCode = QQuickPixmapReply::Loading; |
| 1058 | if (f.fileName() != localFile) |
| 1059 | errorStr += QString::fromLatin1(ba: " (%1)" ).arg(a: f.fileName()); |
| 1060 | } else if (runningJob->data) { |
| 1061 | runningJob->data->frameCount = frameCount; |
| 1062 | } |
| 1063 | } |
| 1064 | } else { |
| 1065 | errorStr = QQuickPixmap::tr(sourceText: "Cannot open: %1" ).arg(a: url.toString()); |
| 1066 | errorCode = QQuickPixmapReply::Loading; |
| 1067 | } |
| 1068 | } |
| 1069 | PIXMAP_READER_LOCK(); |
| 1070 | if (!cancelledJobs.contains(t: runningJob)) { |
| 1071 | runningJob->postReply(error: errorCode, errorString: errorStr, implicitSize: readSize, |
| 1072 | factory: QQuickTextureFactory::textureFactoryForImage(image)); |
| 1073 | } |
| 1074 | } else { |
| 1075 | #if QT_CONFIG(qml_network) |
| 1076 | // Network resource |
| 1077 | QNetworkRequest req(url); |
| 1078 | req.setAttribute(code: QNetworkRequest::HttpPipeliningAllowedAttribute, value: true); |
| 1079 | QNetworkReply *reply = networkAccessManager()->get(request: req); |
| 1080 | |
| 1081 | QMetaObject::connect(sender: reply, signal_index: replyDownloadProgressMethodIndex, receiver: runningJob, |
| 1082 | method_index: downloadProgressMethodIndex); |
| 1083 | QMetaObject::connect(sender: reply, signal_index: replyFinishedMethodIndex, receiver: readerThreadExecutionEnforcer(), |
| 1084 | method_index: threadNetworkRequestDoneMethodIndex); |
| 1085 | |
| 1086 | networkJobs.insert(key: reply, value: runningJob); |
| 1087 | #else |
| 1088 | // Silently fail if compiled with no_network |
| 1089 | #endif |
| 1090 | } |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | QQuickPixmapReader *QQuickPixmapReader::instance(QQmlEngine *engine) |
| 1095 | { |
| 1096 | // XXX NOTE: must be called within readerMutex locking. |
| 1097 | QQuickPixmapReader *reader = readers.value(key: engine); |
| 1098 | if (!reader) { |
| 1099 | reader = new QQuickPixmapReader(engine); |
| 1100 | readers.insert(key: engine, value: reader); |
| 1101 | } |
| 1102 | |
| 1103 | return reader; |
| 1104 | } |
| 1105 | |
| 1106 | QQuickPixmapReader *QQuickPixmapReader::existingInstance(QQmlEngine *engine) |
| 1107 | { |
| 1108 | // XXX NOTE: must be called within readerMutex locking. |
| 1109 | return readers.value(key: engine, defaultValue: 0); |
| 1110 | } |
| 1111 | |
| 1112 | QQuickPixmapReply *QQuickPixmapReader::getImage(QQuickPixmapData *data) |
| 1113 | { |
| 1114 | QQuickPixmapReply *reply = new QQuickPixmapReply(data); |
| 1115 | reply->engineForReader = engine; |
| 1116 | return reply; |
| 1117 | } |
| 1118 | |
| 1119 | void QQuickPixmapReader::startJob(QQuickPixmapReply *job) |
| 1120 | { |
| 1121 | PIXMAP_READER_LOCK(); |
| 1122 | jobs.append(t: job); |
| 1123 | if (readerThreadExecutionEnforcer()) |
| 1124 | readerThreadExecutionEnforcer()->processJobsOnReaderThreadLater(); |
| 1125 | } |
| 1126 | |
| 1127 | void QQuickPixmapReader::cancel(QQuickPixmapReply *reply) |
| 1128 | { |
| 1129 | PIXMAP_READER_LOCK(); |
| 1130 | if (reply->loading) { |
| 1131 | cancelledJobs.append(t: reply); |
| 1132 | reply->data = nullptr; |
| 1133 | // XXX |
| 1134 | if (readerThreadExecutionEnforcer()) |
| 1135 | readerThreadExecutionEnforcer()->processJobsOnReaderThreadLater(); |
| 1136 | } else { |
| 1137 | // If loading was started (reply removed from jobs) but the reply was never processed |
| 1138 | // (otherwise it would have deleted itself) we need to profile an error. |
| 1139 | if (jobs.removeAll(t: reply) == 0) { |
| 1140 | PIXMAP_PROFILE(pixmapStateChanged<QQuickProfiler::PixmapLoadingError>(reply->url)); |
| 1141 | } |
| 1142 | delete reply; |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | void QQuickPixmapReader::run() |
| 1147 | { |
| 1148 | Q_ASSERT_CALLED_ON_VALID_THREAD(m_readerThreadAffinityMarker); |
| 1149 | |
| 1150 | if (replyDownloadProgressMethodIndex == -1) { |
| 1151 | #if QT_CONFIG(qml_network) |
| 1152 | replyDownloadProgressMethodIndex = |
| 1153 | QMetaMethod::fromSignal(signal: &QNetworkReply::downloadProgress).methodIndex(); |
| 1154 | replyFinishedMethodIndex = QMetaMethod::fromSignal(signal: &QNetworkReply::finished).methodIndex(); |
| 1155 | const QMetaObject *ir = &ReaderThreadExecutionEnforcer::staticMetaObject; |
| 1156 | threadNetworkRequestDoneMethodIndex = ir->indexOfSlot(slot: "networkRequestDone()" ); |
| 1157 | #endif |
| 1158 | downloadProgressMethodIndex = |
| 1159 | QMetaMethod::fromSignal(signal: &QQuickPixmapReply::downloadProgress).methodIndex(); |
| 1160 | } |
| 1161 | |
| 1162 | #if QT_CONFIG(quick_pixmap_cache_threaded_download) |
| 1163 | const auto guard = qScopeGuard(f: [this]() { |
| 1164 | // We need to delete the runLoopReaderThreadExecutionEnforcer from the same thread. |
| 1165 | PIXMAP_READER_LOCK(); |
| 1166 | delete runLoopReaderThreadExecutionEnforcer; |
| 1167 | runLoopReaderThreadExecutionEnforcer = nullptr; |
| 1168 | }); |
| 1169 | |
| 1170 | { |
| 1171 | PIXMAP_READER_LOCK(); |
| 1172 | Q_ASSERT(!runLoopReaderThreadExecutionEnforcer); |
| 1173 | runLoopReaderThreadExecutionEnforcer = new ReaderThreadExecutionEnforcer(this); |
| 1174 | } |
| 1175 | |
| 1176 | processJobs(); |
| 1177 | exec(); |
| 1178 | #else |
| 1179 | ownedReaderThreadExecutionEnforcer = std::make_unique<ReaderThreadExecutionEnforcer>(this); |
| 1180 | processJobs(); |
| 1181 | #endif |
| 1182 | } |
| 1183 | |
| 1184 | inline bool operator==(const QQuickPixmapKey &lhs, const QQuickPixmapKey &rhs) |
| 1185 | { |
| 1186 | return *lhs.url == *rhs.url && |
| 1187 | *lhs.region == *rhs.region && |
| 1188 | *lhs.size == *rhs.size && |
| 1189 | lhs.frame == rhs.frame && |
| 1190 | lhs.options == rhs.options; |
| 1191 | } |
| 1192 | |
| 1193 | inline size_t qHash(const QQuickPixmapKey &key, size_t seed) noexcept |
| 1194 | { |
| 1195 | return qHashMulti(seed, args: *key.url, args: *key.region, args: *key.size, args: key.frame, args: key.options.autoTransform()); |
| 1196 | } |
| 1197 | |
| 1198 | #ifndef QT_NO_DEBUG_STREAM |
| 1199 | inline QDebug operator<<(QDebug debug, const QQuickPixmapKey &key) |
| 1200 | { |
| 1201 | QDebugStateSaver saver(debug); |
| 1202 | debug.nospace(); |
| 1203 | if (!key.url) { |
| 1204 | debug << "QQuickPixmapKey(0)" ; |
| 1205 | return debug; |
| 1206 | } |
| 1207 | |
| 1208 | debug << "QQuickPixmapKey(" << key.url->toString() << " frame=" << key.frame; |
| 1209 | if (!key.region->isEmpty()) { |
| 1210 | debug << " region=" ; |
| 1211 | QtDebugUtils::formatQRect(debug, rect: *key.region); |
| 1212 | } |
| 1213 | if (!key.size->isEmpty()) { |
| 1214 | debug << " size=" ; |
| 1215 | QtDebugUtils::formatQSize(debug, size: *key.size); |
| 1216 | } |
| 1217 | debug << ')'; |
| 1218 | return debug; |
| 1219 | } |
| 1220 | #endif |
| 1221 | |
| 1222 | QQuickPixmapCache *QQuickPixmapCache::instance() |
| 1223 | { |
| 1224 | static QQuickPixmapCache self; |
| 1225 | return &self; |
| 1226 | } |
| 1227 | |
| 1228 | QQuickPixmapCache::~QQuickPixmapCache() |
| 1229 | { |
| 1230 | destroyCache(); |
| 1231 | } |
| 1232 | |
| 1233 | /*! \internal |
| 1234 | Empty the cache completely, to prevent leaks. Returns the number of |
| 1235 | leaked pixmaps (should always be \c 0). |
| 1236 | |
| 1237 | This is work the destructor needs to do, but we put it into a function |
| 1238 | only to make it testable in autotests, because the static instance() |
| 1239 | cannot be destroyed before shutdown. |
| 1240 | */ |
| 1241 | int QQuickPixmapCache::destroyCache() |
| 1242 | { |
| 1243 | if (m_destroying) |
| 1244 | return -1; |
| 1245 | |
| 1246 | m_destroying = true; |
| 1247 | |
| 1248 | // Prevent unreferencePixmap() from assuming it needs to kick |
| 1249 | // off the cache expiry timer, as we're shrinking the cache |
| 1250 | // manually below after releasing all the pixmaps. |
| 1251 | m_timerId = -2; |
| 1252 | |
| 1253 | // unreference all (leaked) pixmaps |
| 1254 | int leakedPixmaps = 0; |
| 1255 | const auto cache = m_cache; // NOTE: intentional copy (QTBUG-65077); releasing items from the cache modifies m_cache. |
| 1256 | for (auto *pixmap : cache) { |
| 1257 | auto currRefCount = pixmap->refCount; |
| 1258 | if (currRefCount) { |
| 1259 | leakedPixmaps++; |
| 1260 | qCDebug(lcQsgLeak) << "leaked pixmap: refCount" << pixmap->refCount << pixmap->url << "frame" << pixmap->frame |
| 1261 | << "size" << pixmap->requestSize << "region" << pixmap->requestRegion; |
| 1262 | while (currRefCount > 0) { |
| 1263 | pixmap->release(store: this); |
| 1264 | currRefCount--; |
| 1265 | } |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | // free all unreferenced pixmaps |
| 1270 | while (m_lastUnreferencedPixmap) |
| 1271 | shrinkCache(remove: 20); |
| 1272 | |
| 1273 | qCDebug(lcQsgLeak, "Number of leaked pixmaps: %i" , leakedPixmaps); |
| 1274 | return leakedPixmaps; |
| 1275 | } |
| 1276 | |
| 1277 | qsizetype QQuickPixmapCache::referencedCost() const |
| 1278 | { |
| 1279 | qsizetype ret = 0; |
| 1280 | QMutexLocker locker(&m_cacheMutex); |
| 1281 | for (const auto *pixmap : std::as_const(t: m_cache)) { |
| 1282 | if (pixmap->refCount) |
| 1283 | ret += pixmap->cost(); |
| 1284 | } |
| 1285 | return ret; |
| 1286 | } |
| 1287 | |
| 1288 | /*! \internal |
| 1289 | Declare that \a data is currently unused so that shrinkCache() can lazily |
| 1290 | delete it later. |
| 1291 | */ |
| 1292 | void QQuickPixmapCache::unreferencePixmap(QQuickPixmapData *data) |
| 1293 | { |
| 1294 | Q_ASSERT(data->prevUnreferenced == nullptr); |
| 1295 | Q_ASSERT(data->prevUnreferencedPtr == nullptr); |
| 1296 | Q_ASSERT(data->nextUnreferenced == nullptr); |
| 1297 | |
| 1298 | data->nextUnreferenced = m_unreferencedPixmaps; |
| 1299 | data->prevUnreferencedPtr = &m_unreferencedPixmaps; |
| 1300 | if (!m_destroying) { // the texture factories may have been cleaned up already. |
| 1301 | m_unreferencedCost += data->cost(); |
| 1302 | qCDebug(lcImg) << data->url << "had cost" << data->cost() << "of total unreferenced" << m_unreferencedCost; |
| 1303 | } |
| 1304 | |
| 1305 | m_unreferencedPixmaps = data; |
| 1306 | if (m_unreferencedPixmaps->nextUnreferenced) { |
| 1307 | m_unreferencedPixmaps->nextUnreferenced->prevUnreferenced = m_unreferencedPixmaps; |
| 1308 | m_unreferencedPixmaps->nextUnreferenced->prevUnreferencedPtr = &m_unreferencedPixmaps->nextUnreferenced; |
| 1309 | } |
| 1310 | |
| 1311 | if (!m_lastUnreferencedPixmap) |
| 1312 | m_lastUnreferencedPixmap = data; |
| 1313 | |
| 1314 | shrinkCache(remove: -1); // Shrink the cache in case it has become larger than cache_limit |
| 1315 | |
| 1316 | if (m_timerId == -1 && m_unreferencedPixmaps |
| 1317 | && !m_destroying && !QCoreApplication::closingDown()) { |
| 1318 | m_timerId = startTimer(CACHE_EXPIRE_TIME * 1000); |
| 1319 | } |
| 1320 | } |
| 1321 | |
| 1322 | /*! \internal |
| 1323 | Declare that \a data is being used (by a QQuickPixmap) so that |
| 1324 | shrinkCache() won't delete it. (This is not reference counting though.) |
| 1325 | */ |
| 1326 | void QQuickPixmapCache::referencePixmap(QQuickPixmapData *data) |
| 1327 | { |
| 1328 | Q_ASSERT(data->prevUnreferencedPtr); |
| 1329 | |
| 1330 | *data->prevUnreferencedPtr = data->nextUnreferenced; |
| 1331 | if (data->nextUnreferenced) { |
| 1332 | data->nextUnreferenced->prevUnreferencedPtr = data->prevUnreferencedPtr; |
| 1333 | data->nextUnreferenced->prevUnreferenced = data->prevUnreferenced; |
| 1334 | } |
| 1335 | if (m_lastUnreferencedPixmap == data) |
| 1336 | m_lastUnreferencedPixmap = data->prevUnreferenced; |
| 1337 | |
| 1338 | data->nextUnreferenced = nullptr; |
| 1339 | data->prevUnreferencedPtr = nullptr; |
| 1340 | data->prevUnreferenced = nullptr; |
| 1341 | |
| 1342 | m_unreferencedCost -= data->cost(); |
| 1343 | qCDebug(lcImg) << data->url << "subtracts cost" << data->cost() << "of total" << m_unreferencedCost; |
| 1344 | } |
| 1345 | |
| 1346 | /*! \internal |
| 1347 | Delete the least-recently-released QQuickPixmapData instances |
| 1348 | until the remaining bytes are less than cache_limit. |
| 1349 | */ |
| 1350 | void QQuickPixmapCache::shrinkCache(int remove) |
| 1351 | { |
| 1352 | qCDebug(lcImg) << "reduce unreferenced cost" << m_unreferencedCost << "to less than limit" << cache_limit; |
| 1353 | while ((remove > 0 || m_unreferencedCost > cache_limit) && m_lastUnreferencedPixmap) { |
| 1354 | QQuickPixmapData *data = m_lastUnreferencedPixmap; |
| 1355 | Q_ASSERT(data->nextUnreferenced == nullptr); |
| 1356 | |
| 1357 | *data->prevUnreferencedPtr = nullptr; |
| 1358 | m_lastUnreferencedPixmap = data->prevUnreferenced; |
| 1359 | data->prevUnreferencedPtr = nullptr; |
| 1360 | data->prevUnreferenced = nullptr; |
| 1361 | |
| 1362 | if (!m_destroying) { |
| 1363 | remove -= data->cost(); |
| 1364 | m_unreferencedCost -= data->cost(); |
| 1365 | } |
| 1366 | data->removeFromCache(store: this); |
| 1367 | delete data; |
| 1368 | } |
| 1369 | } |
| 1370 | |
| 1371 | void QQuickPixmapCache::timerEvent(QTimerEvent *) |
| 1372 | { |
| 1373 | int removalCost = m_unreferencedCost / CACHE_REMOVAL_FRACTION; |
| 1374 | |
| 1375 | shrinkCache(remove: removalCost); |
| 1376 | |
| 1377 | if (m_unreferencedPixmaps == nullptr) { |
| 1378 | killTimer(id: m_timerId); |
| 1379 | m_timerId = -1; |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | void QQuickPixmapCache::purgeCache() |
| 1384 | { |
| 1385 | shrinkCache(remove: m_unreferencedCost); |
| 1386 | } |
| 1387 | |
| 1388 | void QQuickPixmap::purgeCache() |
| 1389 | { |
| 1390 | QQuickPixmapCache::instance()->purgeCache(); |
| 1391 | } |
| 1392 | |
| 1393 | QQuickPixmapReply::QQuickPixmapReply(QQuickPixmapData *d) |
| 1394 | : data(d), engineForReader(nullptr), requestRegion(d->requestRegion), requestSize(d->requestSize), |
| 1395 | url(d->url), loading(false), providerOptions(d->providerOptions) |
| 1396 | { |
| 1397 | if (finishedMethodIndex == -1) { |
| 1398 | finishedMethodIndex = QMetaMethod::fromSignal(signal: &QQuickPixmapReply::finished).methodIndex(); |
| 1399 | downloadProgressMethodIndex = |
| 1400 | QMetaMethod::fromSignal(signal: &QQuickPixmapReply::downloadProgress).methodIndex(); |
| 1401 | } |
| 1402 | } |
| 1403 | |
| 1404 | QQuickPixmapReply::~QQuickPixmapReply() |
| 1405 | { |
| 1406 | // note: this->data->reply must be set to zero if this->data->reply == this |
| 1407 | // but it must be done within mutex locking, to be guaranteed to be safe. |
| 1408 | } |
| 1409 | |
| 1410 | bool QQuickPixmapReply::event(QEvent *event) |
| 1411 | { |
| 1412 | if (event->type() == QEvent::User) { |
| 1413 | |
| 1414 | if (data) { |
| 1415 | Event *de = static_cast<Event *>(event); |
| 1416 | data->pixmapStatus = (de->error == NoError) ? QQuickPixmap::Ready : QQuickPixmap::Error; |
| 1417 | if (data->pixmapStatus == QQuickPixmap::Ready) { |
| 1418 | data->textureFactory = de->textureFactory; |
| 1419 | de->textureFactory = nullptr; |
| 1420 | data->implicitSize = de->implicitSize; |
| 1421 | PIXMAP_PROFILE(pixmapLoadingFinished(data->url, |
| 1422 | data->textureFactory != nullptr && data->textureFactory->textureSize().isValid() ? |
| 1423 | data->textureFactory->textureSize() : |
| 1424 | (data->requestSize.isValid() ? data->requestSize : data->implicitSize))); |
| 1425 | } else { |
| 1426 | PIXMAP_PROFILE(pixmapStateChanged<QQuickProfiler::PixmapLoadingError>(data->url)); |
| 1427 | data->errorString = de->errorString; |
| 1428 | data->removeFromCache(); // We don't continue to cache error'd pixmaps |
| 1429 | } |
| 1430 | |
| 1431 | data->reply = nullptr; |
| 1432 | emit finished(); |
| 1433 | } else { |
| 1434 | PIXMAP_PROFILE(pixmapStateChanged<QQuickProfiler::PixmapLoadingError>(url)); |
| 1435 | } |
| 1436 | |
| 1437 | delete this; |
| 1438 | return true; |
| 1439 | } else { |
| 1440 | return QObject::event(event); |
| 1441 | } |
| 1442 | } |
| 1443 | |
| 1444 | int QQuickPixmapData::cost() const |
| 1445 | { |
| 1446 | if (textureFactory) |
| 1447 | return textureFactory->textureByteCount(); |
| 1448 | return 0; |
| 1449 | } |
| 1450 | |
| 1451 | void QQuickPixmapData::addref() |
| 1452 | { |
| 1453 | ++refCount; |
| 1454 | PIXMAP_PROFILE(pixmapCountChanged<QQuickProfiler::PixmapReferenceCountChanged>(url, refCount)); |
| 1455 | if (prevUnreferencedPtr) |
| 1456 | QQuickPixmapCache::instance()->referencePixmap(data: this); |
| 1457 | } |
| 1458 | |
| 1459 | void QQuickPixmapData::release(QQuickPixmapCache *store) |
| 1460 | { |
| 1461 | Q_ASSERT(refCount > 0); |
| 1462 | --refCount; |
| 1463 | PIXMAP_PROFILE(pixmapCountChanged<QQuickProfiler::PixmapReferenceCountChanged>(url, refCount)); |
| 1464 | if (refCount == 0) { |
| 1465 | if (reply) { |
| 1466 | QQuickPixmapReply *cancelReply = reply; |
| 1467 | reply->data = nullptr; |
| 1468 | reply = nullptr; |
| 1469 | QQuickPixmapReader::readerMutex.lock(); |
| 1470 | QQuickPixmapReader *reader = QQuickPixmapReader::existingInstance(engine: cancelReply->engineForReader); |
| 1471 | if (reader) |
| 1472 | reader->cancel(reply: cancelReply); |
| 1473 | QQuickPixmapReader::readerMutex.unlock(); |
| 1474 | } |
| 1475 | |
| 1476 | store = store ? store : QQuickPixmapCache::instance(); |
| 1477 | if (pixmapStatus == QQuickPixmap::Ready |
| 1478 | #ifdef Q_OS_WEBOS |
| 1479 | && storeToCache |
| 1480 | #endif |
| 1481 | ) { |
| 1482 | if (inCache) |
| 1483 | store->unreferencePixmap(data: this); |
| 1484 | else |
| 1485 | delete this; |
| 1486 | } else { |
| 1487 | removeFromCache(store); |
| 1488 | delete this; |
| 1489 | } |
| 1490 | } |
| 1491 | } |
| 1492 | |
| 1493 | /*! \internal |
| 1494 | Add this to the QQuickPixmapCache singleton. |
| 1495 | |
| 1496 | \note The actual image will end up in QQuickPixmapData::textureFactory. |
| 1497 | At the time addToCache() is called, it's generally not yet loaded; so the |
| 1498 | qCDebug() below cannot say how much data we're committing to storing. |
| 1499 | (On the other hand, removeFromCache() can tell.) QQuickTextureFactory is an |
| 1500 | abstraction for image data. See QQuickDefaultTextureFactory for example: |
| 1501 | it stores a QImage directly. Other QQuickTextureFactory subclasses store data |
| 1502 | in other ways. |
| 1503 | */ |
| 1504 | void QQuickPixmapData::addToCache() |
| 1505 | { |
| 1506 | if (!inCache) { |
| 1507 | QQuickPixmapKey key = { .url: &url, .region: &requestRegion, .size: &requestSize, .frame: frame, .options: providerOptions }; |
| 1508 | QMutexLocker locker(&QQuickPixmapCache::instance()->m_cacheMutex); |
| 1509 | if (lcImg().isDebugEnabled()) { |
| 1510 | qCDebug(lcImg) << "adding" << key << "to total" << QQuickPixmapCache::instance()->m_cache.size(); |
| 1511 | for (auto it = QQuickPixmapCache::instance()->m_cache.keyBegin(); it != QQuickPixmapCache::instance()->m_cache.keyEnd(); ++it) { |
| 1512 | if (*(it->url) == url && it->frame == frame) |
| 1513 | qCDebug(lcImg) << " similar pre-existing:" << *it; |
| 1514 | } |
| 1515 | } |
| 1516 | QQuickPixmapCache::instance()->m_cache.insert(key, value: this); |
| 1517 | inCache = true; |
| 1518 | PIXMAP_PROFILE(pixmapCountChanged<QQuickProfiler::PixmapCacheCountChanged>( |
| 1519 | url, QQuickPixmapCache::instance()->m_cache.size())); |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | void QQuickPixmapData::removeFromCache(QQuickPixmapCache *store) |
| 1524 | { |
| 1525 | if (inCache) { |
| 1526 | if (!store) |
| 1527 | store = QQuickPixmapCache::instance(); |
| 1528 | QQuickPixmapKey key = { .url: &url, .region: &requestRegion, .size: &requestSize, .frame: frame, .options: providerOptions }; |
| 1529 | QMutexLocker locker(&QQuickPixmapCache::instance()->m_cacheMutex); |
| 1530 | store->m_cache.remove(key); |
| 1531 | qCDebug(lcImg) << "removed" << key << implicitSize << "; total remaining" << QQuickPixmapCache::instance()->m_cache.size(); |
| 1532 | inCache = false; |
| 1533 | PIXMAP_PROFILE(pixmapCountChanged<QQuickProfiler::PixmapCacheCountChanged>( |
| 1534 | url, store->m_cache.size())); |
| 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | static QQuickPixmapData* createPixmapDataSync(QQmlEngine *engine, const QUrl &url, |
| 1539 | const QRect &requestRegion, const QSize &requestSize, |
| 1540 | const QQuickImageProviderOptions &providerOptions, int frame, bool *ok, |
| 1541 | qreal devicePixelRatio) |
| 1542 | { |
| 1543 | if (url.scheme() == QLatin1String("image" )) { |
| 1544 | QSize readSize; |
| 1545 | |
| 1546 | QQuickImageProvider::ImageType imageType = QQuickImageProvider::Invalid; |
| 1547 | QQmlEnginePrivate *enginePrivate = QQmlEnginePrivate::get(e: engine); |
| 1548 | QSharedPointer<QQuickImageProvider> provider = enginePrivate->imageProvider(providerId: imageProviderId(url)).objectCast<QQuickImageProvider>(); |
| 1549 | // it is safe to use get() as providerV2 does not escape and is outlived by provider |
| 1550 | QQuickImageProviderWithOptions *providerV2 = QQuickImageProviderWithOptions::checkedCast(provider: provider.get()); |
| 1551 | if (provider) |
| 1552 | imageType = provider->imageType(); |
| 1553 | |
| 1554 | switch (imageType) { |
| 1555 | case QQuickImageProvider::Invalid: |
| 1556 | return new QQuickPixmapData(url, requestRegion, requestSize, providerOptions, |
| 1557 | QQuickPixmap::tr(sourceText: "Invalid image provider: %1" ).arg(a: url.toString())); |
| 1558 | case QQuickImageProvider::Texture: |
| 1559 | { |
| 1560 | QQuickTextureFactory *texture = providerV2 ? providerV2->requestTexture(id: imageId(url), size: &readSize, requestedSize: requestSize, options: providerOptions) |
| 1561 | : provider->requestTexture(id: imageId(url), size: &readSize, requestedSize: requestSize); |
| 1562 | if (texture) { |
| 1563 | *ok = true; |
| 1564 | return new QQuickPixmapData(url, texture, readSize, requestRegion, requestSize, |
| 1565 | providerOptions, QQuickImageProviderOptions::UsePluginDefaultTransform, frame); |
| 1566 | } |
| 1567 | break; |
| 1568 | } |
| 1569 | |
| 1570 | case QQuickImageProvider::Image: |
| 1571 | { |
| 1572 | QImage image = providerV2 ? providerV2->requestImage(id: imageId(url), size: &readSize, requestedSize: requestSize, options: providerOptions) |
| 1573 | : provider->requestImage(id: imageId(url), size: &readSize, requestedSize: requestSize); |
| 1574 | if (!image.isNull()) { |
| 1575 | *ok = true; |
| 1576 | return new QQuickPixmapData(url, QQuickTextureFactory::textureFactoryForImage(image), |
| 1577 | readSize, requestRegion, requestSize, providerOptions, |
| 1578 | QQuickImageProviderOptions::UsePluginDefaultTransform, frame); |
| 1579 | } |
| 1580 | break; |
| 1581 | } |
| 1582 | case QQuickImageProvider::Pixmap: |
| 1583 | { |
| 1584 | QPixmap pixmap = providerV2 ? providerV2->requestPixmap(id: imageId(url), size: &readSize, requestedSize: requestSize, options: providerOptions) |
| 1585 | : provider->requestPixmap(id: imageId(url), size: &readSize, requestedSize: requestSize); |
| 1586 | if (!pixmap.isNull()) { |
| 1587 | *ok = true; |
| 1588 | return new QQuickPixmapData(url, QQuickTextureFactory::textureFactoryForImage(image: pixmap.toImage()), |
| 1589 | readSize, requestRegion, requestSize, providerOptions, |
| 1590 | QQuickImageProviderOptions::UsePluginDefaultTransform, frame); |
| 1591 | } |
| 1592 | break; |
| 1593 | } |
| 1594 | case QQuickImageProvider::ImageResponse: |
| 1595 | { |
| 1596 | // Fall through, ImageResponse providers never get here |
| 1597 | Q_ASSERT(imageType != QQuickImageProvider::ImageResponse && "Sync call to ImageResponse provider" ); |
| 1598 | } |
| 1599 | } |
| 1600 | |
| 1601 | // provider has bad image type, or provider returned null image |
| 1602 | return new QQuickPixmapData(url, requestRegion, requestSize, providerOptions, |
| 1603 | QQuickPixmap::tr(sourceText: "Failed to get image from provider: %1" ).arg(a: url.toString())); |
| 1604 | } |
| 1605 | |
| 1606 | QString localFile = QQmlFile::urlToLocalFileOrQrc(url); |
| 1607 | if (localFile.isEmpty()) |
| 1608 | return nullptr; |
| 1609 | |
| 1610 | QFile f(existingImageFileForPath(localFile)); |
| 1611 | QSize readSize; |
| 1612 | QString errorString; |
| 1613 | |
| 1614 | if (f.open(flags: QIODevice::ReadOnly)) { |
| 1615 | QSGTextureReader texReader(&f, localFile); |
| 1616 | if (backendSupport()->hasOpenGL && texReader.isTexture()) { |
| 1617 | QQuickTextureFactory *factory = texReader.read(); |
| 1618 | if (factory) { |
| 1619 | *ok = true; |
| 1620 | return new QQuickPixmapData(url, factory, factory->textureSize(), requestRegion, requestSize, |
| 1621 | providerOptions, QQuickImageProviderOptions::UsePluginDefaultTransform, frame); |
| 1622 | } else { |
| 1623 | errorString = QQuickPixmap::tr(sourceText: "Error decoding: %1" ).arg(a: url.toString()); |
| 1624 | if (f.fileName() != localFile) |
| 1625 | errorString += QString::fromLatin1(ba: " (%1)" ).arg(a: f.fileName()); |
| 1626 | } |
| 1627 | } else { |
| 1628 | QImage image; |
| 1629 | QQuickImageProviderOptions::AutoTransform appliedTransform = providerOptions.autoTransform(); |
| 1630 | int frameCount; |
| 1631 | if (readImage(url, dev: &f, image: &image, errorString: &errorString, impsize: &readSize, frameCount: &frameCount, requestRegion, requestSize, |
| 1632 | providerOptions, appliedTransform: &appliedTransform, frame, devicePixelRatio)) { |
| 1633 | *ok = true; |
| 1634 | return new QQuickPixmapData(url, QQuickTextureFactory::textureFactoryForImage(image), readSize, requestRegion, requestSize, |
| 1635 | providerOptions, appliedTransform, frame, frameCount); |
| 1636 | } else if (f.fileName() != localFile) { |
| 1637 | errorString += QString::fromLatin1(ba: " (%1)" ).arg(a: f.fileName()); |
| 1638 | } |
| 1639 | } |
| 1640 | } else { |
| 1641 | errorString = QQuickPixmap::tr(sourceText: "Cannot open: %1" ).arg(a: url.toString()); |
| 1642 | } |
| 1643 | return new QQuickPixmapData(url, requestRegion, requestSize, providerOptions, errorString); |
| 1644 | } |
| 1645 | |
| 1646 | |
| 1647 | struct QQuickPixmapNull { |
| 1648 | QUrl url; |
| 1649 | QRect region; |
| 1650 | QSize size; |
| 1651 | }; |
| 1652 | Q_GLOBAL_STATIC(QQuickPixmapNull, nullPixmap); |
| 1653 | |
| 1654 | QQuickPixmap::QQuickPixmap() |
| 1655 | : d(nullptr) |
| 1656 | { |
| 1657 | } |
| 1658 | |
| 1659 | QQuickPixmap::QQuickPixmap(QQmlEngine *engine, const QUrl &url) |
| 1660 | : d(nullptr) |
| 1661 | { |
| 1662 | load(engine, url); |
| 1663 | } |
| 1664 | |
| 1665 | QQuickPixmap::QQuickPixmap(QQmlEngine *engine, const QUrl &url, Options options) |
| 1666 | : d(nullptr) |
| 1667 | { |
| 1668 | load(engine, url, options); |
| 1669 | } |
| 1670 | |
| 1671 | QQuickPixmap::QQuickPixmap(QQmlEngine *engine, const QUrl &url, const QRect ®ion, const QSize &size) |
| 1672 | : d(nullptr) |
| 1673 | { |
| 1674 | load(engine, url, requestRegion: region, requestSize: size); |
| 1675 | } |
| 1676 | |
| 1677 | QQuickPixmap::QQuickPixmap(const QUrl &url, const QImage &image) |
| 1678 | { |
| 1679 | d = new QQuickPixmapData(url, new QQuickDefaultTextureFactory(image), image.size(), QRect(), QSize(), |
| 1680 | QQuickImageProviderOptions(), QQuickImageProviderOptions::UsePluginDefaultTransform); |
| 1681 | d->addToCache(); |
| 1682 | } |
| 1683 | |
| 1684 | QQuickPixmap::~QQuickPixmap() |
| 1685 | { |
| 1686 | if (d) { |
| 1687 | d->release(); |
| 1688 | d = nullptr; |
| 1689 | } |
| 1690 | } |
| 1691 | |
| 1692 | bool QQuickPixmap::isNull() const |
| 1693 | { |
| 1694 | return d == nullptr; |
| 1695 | } |
| 1696 | |
| 1697 | bool QQuickPixmap::isReady() const |
| 1698 | { |
| 1699 | return status() == Ready; |
| 1700 | } |
| 1701 | |
| 1702 | bool QQuickPixmap::isError() const |
| 1703 | { |
| 1704 | return status() == Error; |
| 1705 | } |
| 1706 | |
| 1707 | bool QQuickPixmap::isLoading() const |
| 1708 | { |
| 1709 | return status() == Loading; |
| 1710 | } |
| 1711 | |
| 1712 | QString QQuickPixmap::error() const |
| 1713 | { |
| 1714 | if (d) |
| 1715 | return d->errorString; |
| 1716 | else |
| 1717 | return QString(); |
| 1718 | } |
| 1719 | |
| 1720 | QQuickPixmap::Status QQuickPixmap::status() const |
| 1721 | { |
| 1722 | if (d) |
| 1723 | return d->pixmapStatus; |
| 1724 | else |
| 1725 | return Null; |
| 1726 | } |
| 1727 | |
| 1728 | const QUrl &QQuickPixmap::url() const |
| 1729 | { |
| 1730 | if (d) |
| 1731 | return d->url; |
| 1732 | else |
| 1733 | return nullPixmap()->url; |
| 1734 | } |
| 1735 | |
| 1736 | const QSize &QQuickPixmap::implicitSize() const |
| 1737 | { |
| 1738 | if (d) |
| 1739 | return d->implicitSize; |
| 1740 | else |
| 1741 | return nullPixmap()->size; |
| 1742 | } |
| 1743 | |
| 1744 | const QSize &QQuickPixmap::requestSize() const |
| 1745 | { |
| 1746 | if (d) |
| 1747 | return d->requestSize; |
| 1748 | else |
| 1749 | return nullPixmap()->size; |
| 1750 | } |
| 1751 | |
| 1752 | const QRect &QQuickPixmap::requestRegion() const |
| 1753 | { |
| 1754 | if (d) |
| 1755 | return d->requestRegion; |
| 1756 | else |
| 1757 | return nullPixmap()->region; |
| 1758 | } |
| 1759 | |
| 1760 | QQuickImageProviderOptions::AutoTransform QQuickPixmap::autoTransform() const |
| 1761 | { |
| 1762 | if (d) |
| 1763 | return d->appliedTransform; |
| 1764 | else |
| 1765 | return QQuickImageProviderOptions::UsePluginDefaultTransform; |
| 1766 | } |
| 1767 | |
| 1768 | int QQuickPixmap::frameCount() const |
| 1769 | { |
| 1770 | if (d) |
| 1771 | return d->frameCount; |
| 1772 | return 0; |
| 1773 | } |
| 1774 | |
| 1775 | QQuickTextureFactory *QQuickPixmap::textureFactory() const |
| 1776 | { |
| 1777 | if (d) |
| 1778 | return d->textureFactory; |
| 1779 | |
| 1780 | return nullptr; |
| 1781 | } |
| 1782 | |
| 1783 | QImage QQuickPixmap::image() const |
| 1784 | { |
| 1785 | if (d && d->textureFactory) |
| 1786 | return d->textureFactory->image(); |
| 1787 | return QImage(); |
| 1788 | } |
| 1789 | |
| 1790 | void QQuickPixmap::setImage(const QImage &p) |
| 1791 | { |
| 1792 | clear(); |
| 1793 | |
| 1794 | if (!p.isNull()) { |
| 1795 | if (d) |
| 1796 | d->release(); |
| 1797 | d = new QQuickPixmapData(QQuickTextureFactory::textureFactoryForImage(image: p)); |
| 1798 | } |
| 1799 | } |
| 1800 | |
| 1801 | void QQuickPixmap::setPixmap(const QQuickPixmap &other) |
| 1802 | { |
| 1803 | if (d == other.d) |
| 1804 | return; |
| 1805 | clear(); |
| 1806 | |
| 1807 | if (other.d) { |
| 1808 | if (d) |
| 1809 | d->release(); |
| 1810 | d = other.d; |
| 1811 | d->addref(); |
| 1812 | } |
| 1813 | } |
| 1814 | |
| 1815 | int QQuickPixmap::width() const |
| 1816 | { |
| 1817 | if (d && d->textureFactory) |
| 1818 | return d->textureFactory->textureSize().width(); |
| 1819 | else |
| 1820 | return 0; |
| 1821 | } |
| 1822 | |
| 1823 | int QQuickPixmap::height() const |
| 1824 | { |
| 1825 | if (d && d->textureFactory) |
| 1826 | return d->textureFactory->textureSize().height(); |
| 1827 | else |
| 1828 | return 0; |
| 1829 | } |
| 1830 | |
| 1831 | QRect QQuickPixmap::rect() const |
| 1832 | { |
| 1833 | if (d && d->textureFactory) |
| 1834 | return QRect(QPoint(), d->textureFactory->textureSize()); |
| 1835 | else |
| 1836 | return QRect(); |
| 1837 | } |
| 1838 | |
| 1839 | void QQuickPixmap::load(QQmlEngine *engine, const QUrl &url) |
| 1840 | { |
| 1841 | load(engine, url, requestRegion: QRect(), requestSize: QSize(), options: QQuickPixmap::Cache); |
| 1842 | } |
| 1843 | |
| 1844 | void QQuickPixmap::load(QQmlEngine *engine, const QUrl &url, QQuickPixmap::Options options) |
| 1845 | { |
| 1846 | load(engine, url, requestRegion: QRect(), requestSize: QSize(), options); |
| 1847 | } |
| 1848 | |
| 1849 | void QQuickPixmap::load(QQmlEngine *engine, const QUrl &url, const QRect &requestRegion, const QSize &requestSize) |
| 1850 | { |
| 1851 | load(engine, url, requestRegion, requestSize, options: QQuickPixmap::Cache); |
| 1852 | } |
| 1853 | |
| 1854 | void QQuickPixmap::load(QQmlEngine *engine, const QUrl &url, const QRect &requestRegion, const QSize &requestSize, QQuickPixmap::Options options) |
| 1855 | { |
| 1856 | load(engine, url, requestRegion, requestSize, options, providerOptions: QQuickImageProviderOptions()); |
| 1857 | } |
| 1858 | |
| 1859 | void QQuickPixmap::load(QQmlEngine *engine, const QUrl &url, const QRect &requestRegion, const QSize &requestSize, |
| 1860 | QQuickPixmap::Options options, const QQuickImageProviderOptions &providerOptions, int frame, int frameCount, |
| 1861 | qreal devicePixelRatio) |
| 1862 | { |
| 1863 | if (d) { |
| 1864 | d->release(); |
| 1865 | d = nullptr; |
| 1866 | } |
| 1867 | |
| 1868 | QQuickPixmapKey key = { .url: &url, .region: &requestRegion, .size: &requestSize, .frame: frame, .options: providerOptions }; |
| 1869 | QQuickPixmapCache *store = QQuickPixmapCache::instance(); |
| 1870 | |
| 1871 | QMutexLocker locker(&QQuickPixmapCache::instance()->m_cacheMutex); |
| 1872 | QHash<QQuickPixmapKey, QQuickPixmapData *>::Iterator iter = store->m_cache.end(); |
| 1873 | |
| 1874 | #ifdef Q_OS_WEBOS |
| 1875 | QQuickPixmap::Options orgOptions = options; |
| 1876 | // In webOS, we suppose that cache is always enabled to share image instances along its source. |
| 1877 | // So, original option(orgOptions) for cache only decides whether to store the instances when it's unreferenced. |
| 1878 | options |= QQuickPixmap::Cache; |
| 1879 | #endif |
| 1880 | |
| 1881 | // If Cache is disabled, the pixmap will always be loaded, even if there is an existing |
| 1882 | // cached version. Unless it's an itemgrabber url, since the cache is used to pass |
| 1883 | // the result between QQuickItemGrabResult and QQuickImage. |
| 1884 | if (url.scheme() == itemGrabberScheme) { |
| 1885 | QRect dummyRegion; |
| 1886 | QSize dummySize; |
| 1887 | if (requestSize != dummySize) |
| 1888 | qWarning() << "Ignoring sourceSize request for image url that came from grabToImage. Use the targetSize parameter of the grabToImage() function instead." ; |
| 1889 | const QQuickPixmapKey grabberKey = { .url: &url, .region: &dummyRegion, .size: &dummySize, .frame: 0, .options: QQuickImageProviderOptions() }; |
| 1890 | iter = store->m_cache.find(key: grabberKey); |
| 1891 | } else if (options & QQuickPixmap::Cache) |
| 1892 | iter = store->m_cache.find(key); |
| 1893 | |
| 1894 | if (iter == store->m_cache.end()) { |
| 1895 | locker.unlock(); |
| 1896 | if (url.scheme() == QLatin1String("image" )) { |
| 1897 | QQmlEnginePrivate *enginePrivate = QQmlEnginePrivate::get(e: engine); |
| 1898 | if (auto provider = enginePrivate->imageProvider(providerId: imageProviderId(url)).staticCast<QQuickImageProvider>()) { |
| 1899 | const bool threadedPixmaps = QGuiApplicationPrivate::platformIntegration()->hasCapability(cap: QPlatformIntegration::ThreadedPixmaps); |
| 1900 | if (!threadedPixmaps && provider->imageType() == QQuickImageProvider::Pixmap) { |
| 1901 | // pixmaps can only be loaded synchronously |
| 1902 | options &= ~QQuickPixmap::Asynchronous; |
| 1903 | } else if (provider->flags() & QQuickImageProvider::ForceAsynchronousImageLoading) { |
| 1904 | options |= QQuickPixmap::Asynchronous; |
| 1905 | } |
| 1906 | } |
| 1907 | } |
| 1908 | |
| 1909 | if (!(options & QQuickPixmap::Asynchronous)) { |
| 1910 | bool ok = false; |
| 1911 | PIXMAP_PROFILE(pixmapStateChanged<QQuickProfiler::PixmapLoadingStarted>(url)); |
| 1912 | d = createPixmapDataSync(engine, url, requestRegion, requestSize, providerOptions, frame, ok: &ok, devicePixelRatio); |
| 1913 | if (ok) { |
| 1914 | PIXMAP_PROFILE(pixmapLoadingFinished(url, QSize(width(), height()))); |
| 1915 | if (options & QQuickPixmap::Cache) |
| 1916 | d->addToCache(); |
| 1917 | #ifdef Q_OS_WEBOS |
| 1918 | d->storeToCache = orgOptions & QQuickPixmap::Cache; |
| 1919 | #endif |
| 1920 | return; |
| 1921 | } |
| 1922 | if (d) { // loadable, but encountered error while loading |
| 1923 | PIXMAP_PROFILE(pixmapStateChanged<QQuickProfiler::PixmapLoadingError>(url)); |
| 1924 | return; |
| 1925 | } |
| 1926 | } |
| 1927 | |
| 1928 | if (!engine) |
| 1929 | return; |
| 1930 | |
| 1931 | |
| 1932 | d = new QQuickPixmapData(url, requestRegion, requestSize, providerOptions, |
| 1933 | QQuickImageProviderOptions::UsePluginDefaultTransform, frame, frameCount); |
| 1934 | if (options & QQuickPixmap::Cache) |
| 1935 | d->addToCache(); |
| 1936 | #ifdef Q_OS_WEBOS |
| 1937 | d->storeToCache = orgOptions & QQuickPixmap::Cache; |
| 1938 | #endif |
| 1939 | |
| 1940 | QQuickPixmapReader::readerMutex.lock(); |
| 1941 | QQuickPixmapReader *reader = QQuickPixmapReader::instance(engine); |
| 1942 | d->reply = reader->getImage(data: d); |
| 1943 | reader->startJob(job: d->reply); |
| 1944 | QQuickPixmapReader::readerMutex.unlock(); |
| 1945 | } else { |
| 1946 | d = *iter; |
| 1947 | d->addref(); |
| 1948 | qCDebug(lcImg) << "loaded from cache" << url << "frame" << frame; |
| 1949 | } |
| 1950 | } |
| 1951 | |
| 1952 | /*! \internal |
| 1953 | Attempts to load an image from the given \a url via the given \a device. |
| 1954 | This is for special cases when the QImageIOHandler can benefit from reusing |
| 1955 | the I/O device, or from something extra that a subclass of QIODevice |
| 1956 | carries with it. So far, this code doesn't support loading anything other |
| 1957 | than a QImage, for example compressed textures. It can be added if needed. |
| 1958 | */ |
| 1959 | void QQuickPixmap::loadImageFromDevice(QQmlEngine *engine, QIODevice *device, const QUrl &url, |
| 1960 | const QRect &requestRegion, const QSize &requestSize, |
| 1961 | const QQuickImageProviderOptions &providerOptions, int frame, int frameCount) |
| 1962 | { |
| 1963 | auto oldD = d; |
| 1964 | QQuickPixmapKey key = { .url: &url, .region: &requestRegion, .size: &requestSize, .frame: frame, .options: providerOptions }; |
| 1965 | QQuickPixmapCache *store = QQuickPixmapCache::instance(); |
| 1966 | QHash<QQuickPixmapKey, QQuickPixmapData *>::Iterator iter = store->m_cache.end(); |
| 1967 | QMutexLocker locker(&store->m_cacheMutex); |
| 1968 | iter = store->m_cache.find(key); |
| 1969 | if (iter == store->m_cache.end()) { |
| 1970 | if (!engine) |
| 1971 | return; |
| 1972 | |
| 1973 | locker.unlock(); |
| 1974 | d = new QQuickPixmapData(url, requestRegion, requestSize, providerOptions, |
| 1975 | QQuickImageProviderOptions::UsePluginDefaultTransform, frame, frameCount); |
| 1976 | d->specialDevice = device; |
| 1977 | d->fromSpecialDevice = true; |
| 1978 | d->addToCache(); |
| 1979 | |
| 1980 | QQuickPixmapReader::readerMutex.lock(); |
| 1981 | QQuickPixmapReader *reader = QQuickPixmapReader::instance(engine); |
| 1982 | d->reply = reader->getImage(data: d); |
| 1983 | if (oldD) { |
| 1984 | QObject::connect(sender: d->reply, signal: &QQuickPixmapReply::destroyed, context: store, slot: [oldD]() { |
| 1985 | oldD->release(); |
| 1986 | }, type: Qt::QueuedConnection); |
| 1987 | } |
| 1988 | reader->startJob(job: d->reply); |
| 1989 | QQuickPixmapReader::readerMutex.unlock(); |
| 1990 | } else { |
| 1991 | d = *iter; |
| 1992 | d->addref(); |
| 1993 | qCDebug(lcImg) << "loaded from cache" << url << "frame" << frame << "refCount" << d->refCount; |
| 1994 | locker.unlock(); |
| 1995 | if (oldD) |
| 1996 | oldD->release(); |
| 1997 | } |
| 1998 | } |
| 1999 | |
| 2000 | void QQuickPixmap::clear() |
| 2001 | { |
| 2002 | if (d) { |
| 2003 | d->release(); |
| 2004 | d = nullptr; |
| 2005 | } |
| 2006 | } |
| 2007 | |
| 2008 | void QQuickPixmap::clear(QObject *obj) |
| 2009 | { |
| 2010 | if (d) { |
| 2011 | if (d->reply) |
| 2012 | QObject::disconnect(sender: d->reply, signal: nullptr, receiver: obj, member: nullptr); |
| 2013 | d->release(); |
| 2014 | d = nullptr; |
| 2015 | } |
| 2016 | } |
| 2017 | |
| 2018 | bool QQuickPixmap::isCached(const QUrl &url, const QRect &requestRegion, const QSize &requestSize, |
| 2019 | const int frame, const QQuickImageProviderOptions &options) |
| 2020 | { |
| 2021 | QQuickPixmapKey key = { .url: &url, .region: &requestRegion, .size: &requestSize, .frame: frame, .options: options }; |
| 2022 | QQuickPixmapCache *store = QQuickPixmapCache::instance(); |
| 2023 | |
| 2024 | return store->m_cache.contains(key); |
| 2025 | } |
| 2026 | |
| 2027 | bool QQuickPixmap::isScalableImageFormat(const QUrl &url) |
| 2028 | { |
| 2029 | if (url.scheme() == "image"_L1 ) |
| 2030 | return true; |
| 2031 | |
| 2032 | const QString stringUrl = url.path(options: QUrl::PrettyDecoded); |
| 2033 | return stringUrl.endsWith(s: "svg"_L1 ) |
| 2034 | || stringUrl.endsWith(s: "svgz"_L1 ) |
| 2035 | || stringUrl.endsWith(s: "pdf"_L1 ); |
| 2036 | } |
| 2037 | |
| 2038 | bool QQuickPixmap::connectFinished(QObject *object, const char *method) |
| 2039 | { |
| 2040 | if (!d || !d->reply) { |
| 2041 | qWarning(msg: "QQuickPixmap: connectFinished() called when not loading." ); |
| 2042 | return false; |
| 2043 | } |
| 2044 | |
| 2045 | return QObject::connect(sender: d->reply, SIGNAL(finished()), receiver: object, member: method); |
| 2046 | } |
| 2047 | |
| 2048 | bool QQuickPixmap::connectFinished(QObject *object, int method) |
| 2049 | { |
| 2050 | if (!d || !d->reply) { |
| 2051 | qWarning(msg: "QQuickPixmap: connectFinished() called when not loading." ); |
| 2052 | return false; |
| 2053 | } |
| 2054 | |
| 2055 | return QMetaObject::connect(sender: d->reply, signal_index: QQuickPixmapReply::finishedMethodIndex, receiver: object, method_index: method); |
| 2056 | } |
| 2057 | |
| 2058 | bool QQuickPixmap::connectDownloadProgress(QObject *object, const char *method) |
| 2059 | { |
| 2060 | if (!d || !d->reply) { |
| 2061 | qWarning(msg: "QQuickPixmap: connectDownloadProgress() called when not loading." ); |
| 2062 | return false; |
| 2063 | } |
| 2064 | |
| 2065 | return QObject::connect(sender: d->reply, SIGNAL(downloadProgress(qint64,qint64)), receiver: object, |
| 2066 | member: method); |
| 2067 | } |
| 2068 | |
| 2069 | bool QQuickPixmap::connectDownloadProgress(QObject *object, int method) |
| 2070 | { |
| 2071 | if (!d || !d->reply) { |
| 2072 | qWarning(msg: "QQuickPixmap: connectDownloadProgress() called when not loading." ); |
| 2073 | return false; |
| 2074 | } |
| 2075 | |
| 2076 | return QMetaObject::connect(sender: d->reply, signal_index: QQuickPixmapReply::downloadProgressMethodIndex, receiver: object, |
| 2077 | method_index: method); |
| 2078 | } |
| 2079 | |
| 2080 | QColorSpace QQuickPixmap::colorSpace() const |
| 2081 | { |
| 2082 | if (!d || !d->textureFactory) |
| 2083 | return QColorSpace(); |
| 2084 | return d->textureFactory->image().colorSpace(); |
| 2085 | } |
| 2086 | |
| 2087 | QT_END_NAMESPACE |
| 2088 | |
| 2089 | #include <qquickpixmapcache.moc> |
| 2090 | |
| 2091 | #include "moc_qquickpixmapcache_p.cpp" |
| 2092 | |