diff options
author | Lars Knoll <lars.knoll@nokia.com> | 2012-03-07 22:00:57 +0100 |
---|---|---|
committer | Lars Knoll <lars.knoll@nokia.com> | 2012-03-07 22:01:11 +0100 |
commit | 616bbd1988f3b92f7d980b6c9a1278f11b712573 (patch) | |
tree | c6f9489bc1b53649130be21de858870f574db906 /src/quick/util | |
parent | 3bc907d155034fe64efc8cb6056b48f0c6401bfb (diff) | |
parent | 70966df1be02dd94ecf9a122ff9e4976245aeb92 (diff) |
Merge remote-tracking branch 'origin/master' into api_changes
Change-Id: I121cb9c9316b5e47476dcb982bc28b6f3f358f78
Diffstat (limited to 'src/quick/util')
-rw-r--r-- | src/quick/util/qquickimageprovider.cpp | 17 | ||||
-rw-r--r-- | src/quick/util/qquickimageprovider.h | 1 | ||||
-rw-r--r-- | src/quick/util/qquickpixmapcache.cpp | 118 | ||||
-rw-r--r-- | src/quick/util/qquickpixmapcache_p.h | 2 |
4 files changed, 65 insertions, 73 deletions
diff --git a/src/quick/util/qquickimageprovider.cpp b/src/quick/util/qquickimageprovider.cpp index 4a1db050d6..dc85230943 100644 --- a/src/quick/util/qquickimageprovider.cpp +++ b/src/quick/util/qquickimageprovider.cpp @@ -70,6 +70,23 @@ QQuickTextureFactory::~QQuickTextureFactory() } +/*! + \fn QImage QQuickTextureFactory::image() const + + Returns an image version of this texture. + + The lifespan of the returned image is unknown, so the implementation should + return a self contained QImage, not make use of the QImage(uchar *, ...) + constructor. + + This function is not commonly used and is expected to be slow. + */ + +QImage QQuickTextureFactory::image() const +{ + return QImage(); +} + /*! \fn QSGTexture *QQuickTextureFactory::createTexture() const diff --git a/src/quick/util/qquickimageprovider.h b/src/quick/util/qquickimageprovider.h index 153a4bab8e..2a5d146124 100644 --- a/src/quick/util/qquickimageprovider.h +++ b/src/quick/util/qquickimageprovider.h @@ -65,6 +65,7 @@ public: virtual QSGTexture *createTexture(QQuickCanvas *canvas) const = 0; virtual QSize textureSize() const = 0; virtual int textureByteCount() const = 0; + virtual QImage image() const; }; class Q_QUICK_EXPORT QQuickImageProvider : public QQmlImageProviderBase diff --git a/src/quick/util/qquickpixmapcache.cpp b/src/quick/util/qquickpixmapcache.cpp index 350940c93c..5c73e519e1 100644 --- a/src/quick/util/qquickpixmapcache.cpp +++ b/src/quick/util/qquickpixmapcache.cpp @@ -93,6 +93,16 @@ QSGTexture *QQuickDefaultTextureFactory::createTexture(QQuickCanvas *) const return t; } +static QQuickTextureFactory *textureFactoryForImage(const QImage &image) +{ + if (image.isNull()) + return 0; + QQuickTextureFactory *texture = QSGContext::createTextureFactoryFromImage(image); + if (texture) + return texture; + return new QQuickDefaultTextureFactory(image); +} + class QQuickPixmapReader; class QQuickPixmapData; class QQuickPixmapReply : public QObject @@ -114,16 +124,14 @@ public: class Event : public QEvent { public: - Event(ReadError, const QString &, const QSize &, QQuickTextureFactory *factory, const QImage &image); + Event(ReadError, const QString &, const QSize &, QQuickTextureFactory *factory); ReadError error; QString errorString; QSize implicitSize; - QImage image; QQuickTextureFactory *textureFactory; }; - void postReply(ReadError, const QString &, const QSize &, const QImage &image); - void postReply(ReadError, const QString &, const QSize &, QQuickTextureFactory *factory, const QImage &image); + void postReply(ReadError, const QString &, const QSize &, QQuickTextureFactory *factory); Q_SIGNALS: @@ -218,27 +226,21 @@ public: declarativePixmaps.insert(pixmap); } - QQuickPixmapData(QQuickPixmap *pixmap, const QUrl &u, const QImage &p, const QSize &s, const QSize &r) + QQuickPixmapData(QQuickPixmap *pixmap, const QUrl &u, QQuickTextureFactory *texture, const QSize &s, const QSize &r) : refCount(1), inCache(false), privatePixmap(false), pixmapStatus(QQuickPixmap::Ready), - url(u), image(p), implicitSize(s), requestSize(r), textureFactory(new QQuickDefaultTextureFactory(p)), reply(0), prevUnreferenced(0), + url(u), implicitSize(s), requestSize(r), textureFactory(texture), reply(0), prevUnreferenced(0), prevUnreferencedPtr(0), nextUnreferenced(0) { declarativePixmaps.insert(pixmap); } - QQuickPixmapData(QQuickPixmap *pixmap, const QUrl &u, QQuickTextureFactory *factory, const QImage &p, const QSize &s, const QSize &r) - : refCount(1), inCache(false), privatePixmap(false), pixmapStatus(QQuickPixmap::Ready), - url(u), image(p), implicitSize(s), requestSize(r), textureFactory(factory), reply(0), prevUnreferenced(0), - prevUnreferencedPtr(0), nextUnreferenced(0) - { - declarativePixmaps.insert(pixmap); - } - - QQuickPixmapData(QQuickPixmap *pixmap, const QImage &p) + QQuickPixmapData(QQuickPixmap *pixmap, QQuickTextureFactory *texture) : refCount(1), inCache(false), privatePixmap(true), pixmapStatus(QQuickPixmap::Ready), - image(p), implicitSize(p.size()), requestSize(p.size()), textureFactory(new QQuickDefaultTextureFactory(p)), reply(0), prevUnreferenced(0), + textureFactory(texture), reply(0), prevUnreferenced(0), prevUnreferencedPtr(0), nextUnreferenced(0) { + if (texture) + requestSize = implicitSize = texture->textureSize(); declarativePixmaps.insert(pixmap); } @@ -266,7 +268,6 @@ public: QQuickPixmap::Status pixmapStatus; QUrl url; QString errorString; - QImage image; QSize implicitSize; QSize requestSize; @@ -294,22 +295,14 @@ int QQuickPixmapReader::threadNetworkRequestDone = -1; void QQuickPixmapReply::postReply(ReadError error, const QString &errorString, - const QSize &implicitSize, const QImage &image) + const QSize &implicitSize, QQuickTextureFactory *factory) { loading = false; - QCoreApplication::postEvent(this, new Event(error, errorString, implicitSize, new QQuickDefaultTextureFactory(image), image)); + QCoreApplication::postEvent(this, new Event(error, errorString, implicitSize, factory)); } -void QQuickPixmapReply::postReply(ReadError error, const QString &errorString, - const QSize &implicitSize, QQuickTextureFactory *factory, - const QImage &image) -{ - loading = false; - QCoreApplication::postEvent(this, new Event(error, errorString, implicitSize, factory, image)); -} - -QQuickPixmapReply::Event::Event(ReadError e, const QString &s, const QSize &iSize, QQuickTextureFactory *factory, const QImage &i) - : QEvent(QEvent::User), error(e), errorString(s), implicitSize(iSize), image(i), textureFactory(factory) +QQuickPixmapReply::Event::Event(ReadError e, const QString &s, const QSize &iSize, QQuickTextureFactory *factory) + : QEvent(QEvent::User), error(e), errorString(s), implicitSize(iSize), textureFactory(factory) { } @@ -441,13 +434,8 @@ void QQuickPixmapReader::networkRequestDone(QNetworkReply *reply) } // send completion event to the QQuickPixmapReply mutex.lock(); - if (!cancelled.contains(job)) { - QQuickTextureFactory *factory = QSGContext::createTextureFactoryFromImage(image); - if (factory) - job->postReply(error, errorString, readSize, factory, image); - else - job->postReply(error, errorString, readSize, image); - } + if (!cancelled.contains(job)) + job->postReply(error, errorString, readSize, textureFactoryForImage(image)); mutex.unlock(); } reply->deleteLater(); @@ -538,7 +526,7 @@ void QQuickPixmapReader::processJob(QQuickPixmapReply *runningJob, const QUrl &u QImage image; mutex.lock(); if (!cancelled.contains(runningJob)) - runningJob->postReply(errorCode, errorStr, readSize, image); + runningJob->postReply(errorCode, errorStr, readSize, textureFactoryForImage(image)); mutex.unlock(); } else if (imageType == QQuickImageProvider::Image) { QImage image = provider->requestImage(imageId(url), &readSize, requestSize); @@ -549,14 +537,8 @@ void QQuickPixmapReader::processJob(QQuickPixmapReply *runningJob, const QUrl &u errorStr = QQuickPixmap::tr("Failed to get image from provider: %1").arg(url.toString()); } mutex.lock(); - if (!cancelled.contains(runningJob)) { - QQuickTextureFactory *factory = QSGContext::createTextureFactoryFromImage(image); - if (factory) - runningJob->postReply(errorCode, errorStr, readSize, factory, image); - else - runningJob->postReply(errorCode, errorStr, readSize, image); - } - + if (!cancelled.contains(runningJob)) + runningJob->postReply(errorCode, errorStr, readSize, textureFactoryForImage(image)); mutex.unlock(); } else { QQuickTextureFactory *t = provider->requestTexture(imageId(url), &readSize, requestSize); @@ -568,7 +550,7 @@ void QQuickPixmapReader::processJob(QQuickPixmapReply *runningJob, const QUrl &u } mutex.lock(); if (!cancelled.contains(runningJob)) - runningJob->postReply(errorCode, errorStr, readSize, t, QImage()); + runningJob->postReply(errorCode, errorStr, readSize, t); mutex.unlock(); } @@ -590,13 +572,8 @@ void QQuickPixmapReader::processJob(QQuickPixmapReply *runningJob, const QUrl &u errorCode = QQuickPixmapReply::Loading; } mutex.lock(); - if (!cancelled.contains(runningJob)) { - QQuickTextureFactory *factory = QSGContext::createTextureFactoryFromImage(image); - if (factory) - runningJob->postReply(errorCode, errorStr, readSize, factory, image); - else - runningJob->postReply(errorCode, errorStr, readSize, image); - } + if (!cancelled.contains(runningJob)) + runningJob->postReply(errorCode, errorStr, readSize, textureFactoryForImage(image)); mutex.unlock(); } else { // Network resource @@ -877,10 +854,7 @@ bool QQuickPixmapReply::event(QEvent *event) data->pixmapStatus = (de->error == NoError) ? QQuickPixmap::Ready : QQuickPixmap::Error; if (data->pixmapStatus == QQuickPixmap::Ready) { - if (de->textureFactory) { - data->textureFactory = de->textureFactory; - } - data->image = de->image; + data->textureFactory = de->textureFactory; data->implicitSize = de->implicitSize; } else { data->errorString = de->errorString; @@ -902,7 +876,7 @@ int QQuickPixmapData::cost() const { if (textureFactory) return textureFactory->textureByteCount(); - return image.byteCount(); + return 0; } void QQuickPixmapData::addref() @@ -974,7 +948,7 @@ static QQuickPixmapData* createPixmapDataSync(QQuickPixmap *declarativePixmap, Q QQuickTextureFactory *texture = provider->requestTexture(imageId(url), &readSize, requestSize); if (texture) { *ok = true; - return new QQuickPixmapData(declarativePixmap, url, texture, QImage(), readSize, requestSize); + return new QQuickPixmapData(declarativePixmap, url, texture, readSize, requestSize); } } @@ -983,7 +957,7 @@ static QQuickPixmapData* createPixmapDataSync(QQuickPixmap *declarativePixmap, Q QImage image = provider->requestImage(imageId(url), &readSize, requestSize); if (!image.isNull()) { *ok = true; - return new QQuickPixmapData(declarativePixmap, url, image, readSize, requestSize); + return new QQuickPixmapData(declarativePixmap, url, textureFactoryForImage(image), readSize, requestSize); } } case QQuickImageProvider::Pixmap: @@ -991,7 +965,7 @@ static QQuickPixmapData* createPixmapDataSync(QQuickPixmap *declarativePixmap, Q QPixmap pixmap = provider->requestPixmap(imageId(url), &readSize, requestSize); if (!pixmap.isNull()) { *ok = true; - return new QQuickPixmapData(declarativePixmap, url, pixmap.toImage(), readSize, requestSize); + return new QQuickPixmapData(declarativePixmap, url, textureFactoryForImage(pixmap.toImage()), readSize, requestSize); } } } @@ -1014,7 +988,7 @@ static QQuickPixmapData* createPixmapDataSync(QQuickPixmap *declarativePixmap, Q if (readImage(url, &f, &image, &errorString, &readSize, requestSize)) { *ok = true; - return new QQuickPixmapData(declarativePixmap, url, image, readSize, requestSize); + return new QQuickPixmapData(declarativePixmap, url, textureFactoryForImage(image), readSize, requestSize); } errorString = QQuickPixmap::tr("Invalid image data: %1").arg(url.toString()); @@ -1126,10 +1100,10 @@ QQuickTextureFactory *QQuickPixmap::textureFactory() const return 0; } -const QImage &QQuickPixmap::image() const +QImage QQuickPixmap::image() const { - if (d) - return d->image; + if (d && d->textureFactory) + return d->textureFactory->image(); else return nullPixmap()->image; } @@ -1139,29 +1113,29 @@ void QQuickPixmap::setImage(const QImage &p) clear(); if (!p.isNull()) - d = new QQuickPixmapData(this, p); + d = new QQuickPixmapData(this, textureFactoryForImage(p)); } int QQuickPixmap::width() const { - if (d) - return d->textureFactory ? d->textureFactory->textureSize().width() : d->image.width(); + if (d && d->textureFactory) + return d->textureFactory->textureSize().width(); else return 0; } int QQuickPixmap::height() const { - if (d) - return d->textureFactory ? d->textureFactory->textureSize().height() : d->image.height(); + if (d && d->textureFactory) + return d->textureFactory->textureSize().height(); else return 0; } QRect QQuickPixmap::rect() const { - if (d) - return d->textureFactory ? QRect(QPoint(), d->textureFactory->textureSize()) : d->image.rect(); + if (d && d->textureFactory) + return QRect(QPoint(), d->textureFactory->textureSize()); else return QRect(); } diff --git a/src/quick/util/qquickpixmapcache_p.h b/src/quick/util/qquickpixmapcache_p.h index ef17a12709..03f2e65cc2 100644 --- a/src/quick/util/qquickpixmapcache_p.h +++ b/src/quick/util/qquickpixmapcache_p.h @@ -104,7 +104,7 @@ public: const QUrl &url() const; const QSize &implicitSize() const; const QSize &requestSize() const; - const QImage &image() const; + QImage image() const; void setImage(const QImage &); QQuickTextureFactory *textureFactory() const; |