diff options
author | Gunnar Sletta <gunnar.sletta@nokia.com> | 2011-12-02 07:59:44 +0100 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2011-12-02 09:05:05 +0100 |
commit | e32eb8ee5d413ab6a908c2e3274645a489cb9df2 (patch) | |
tree | a6e0491905ad6af212f40e41ee2e2ed5bb0b90ef /src | |
parent | 310618d504aed92ac532c69471ef196829b3f07c (diff) |
Remove QSGContext and QSGTexture from QDeclarativePixmapData
In order to cleanly support multiple windows we need to not
have the QSGContext reference in QDeclarativeEngine and we
need to be able to have one copy of the texture for each
QSGContext that is not sharing when we have multiple windows.
This also makes it easier to the release graphical resources from
a running application, which is another feature that we want
to eventually implement.
This patch does remove an adaptation feature which is the
decodeToTexture, which seemed like a good idea at the time
but in hindsight has not been used for anything.
Edit: resolved conflict in qquickimage.cpp
Edit: updated QDeclarativePixmap testcase
Edit: updated QQuickImage testcase
Edit: Rebased on top of QDeclarativePixmapCacheChanges
Change-Id: Ifc61dd8158d3f841437d029b6031a91196145517
Reviewed-by: Martin Jones <martin.jones@nokia.com>
Diffstat (limited to 'src')
22 files changed, 240 insertions, 269 deletions
diff --git a/src/declarative/items/context2d/qquickcanvasitem.cpp b/src/declarative/items/context2d/qquickcanvasitem.cpp index 8a312c68e6..81b7a49722 100644 --- a/src/declarative/items/context2d/qquickcanvasitem.cpp +++ b/src/declarative/items/context2d/qquickcanvasitem.cpp @@ -552,7 +552,7 @@ QImage QQuickCanvasItem::loadedImage(const QUrl& url) if (pix->isLoading() || pix->isError()) { return QImage(); } - return pix->pixmap().toImage(); + return pix->image(); } /*! diff --git a/src/declarative/items/context2d/qquickcanvasitem_p.h b/src/declarative/items/context2d/qquickcanvasitem_p.h index e0261192ed..293434209a 100644 --- a/src/declarative/items/context2d/qquickcanvasitem_p.h +++ b/src/declarative/items/context2d/qquickcanvasitem_p.h @@ -102,6 +102,7 @@ public: QImage toImage(const QRectF& region = QRectF()) const; QImage loadedImage(const QUrl& url); + Q_SIGNALS: void paint(QDeclarativeV8Handle context, const QRect ®ion); void painted(); diff --git a/src/declarative/items/context2d/qquickcontext2d.cpp b/src/declarative/items/context2d/qquickcontext2d.cpp index ef8631f098..1f7a84baa6 100644 --- a/src/declarative/items/context2d/qquickcontext2d.cpp +++ b/src/declarative/items/context2d/qquickcontext2d.cpp @@ -2455,7 +2455,7 @@ static v8::Handle<v8::Value> ctx2d_drawImage(const v8::Arguments &args) if (pix) { image = pix->image; } else if (imageItem) { - image = imageItem->pixmap().toImage(); + image = imageItem->image(); } else if (canvas) { image = canvas->toImage(); } else { diff --git a/src/declarative/items/qquickanimatedimage.cpp b/src/declarative/items/qquickanimatedimage.cpp index 3a11bb4f64..61447c87e2 100644 --- a/src/declarative/items/qquickanimatedimage.cpp +++ b/src/declarative/items/qquickanimatedimage.cpp @@ -256,7 +256,7 @@ void QQuickAnimatedImage::load() if (d->url.isEmpty()) { delete d->_movie; - d->setPixmap(QPixmap()); + d->setImage(QImage()); d->progress = 0; d->status = Null; if (d->status != oldStatus) @@ -288,7 +288,7 @@ void QQuickAnimatedImage::load() d->_movie->jumpToFrame(0); if (d->paused) d->_movie->setPaused(true); - d->setPixmap(d->_movie->currentPixmap()); + d->setImage(d->_movie->currentPixmap().toImage()); d->status = Ready; d->progress = 1.0; if (d->status != oldStatus) @@ -355,7 +355,7 @@ void QQuickAnimatedImage::movieRequestFinished() } if (d->paused) d->_movie->setPaused(true); - d->setPixmap(d->_movie->currentPixmap()); + d->setImage(d->_movie->currentPixmap().toImage()); d->status = Ready; emit statusChanged(d->status); } @@ -363,7 +363,7 @@ void QQuickAnimatedImage::movieRequestFinished() void QQuickAnimatedImage::movieUpdate() { Q_D(QQuickAnimatedImage); - d->setPixmap(d->_movie->currentPixmap()); + d->setImage(d->_movie->currentPixmap().toImage()); emit frameChanged(); } diff --git a/src/declarative/items/qquickborderimage.cpp b/src/declarative/items/qquickborderimage.cpp index 5f30c40626..e0ffd716aa 100644 --- a/src/declarative/items/qquickborderimage.cpp +++ b/src/declarative/items/qquickborderimage.cpp @@ -552,7 +552,7 @@ QSGNode *QQuickBorderImage::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeDat { Q_D(QQuickBorderImage); - QSGTexture *texture = d->pix.texture(d->sceneGraphContext()); + QSGTexture *texture = d->sceneGraphContext()->textureForFactory(d->pix.textureFactory()); if (!texture || width() <= 0 || height() <= 0) { delete oldNode; diff --git a/src/declarative/items/qquickcanvas.h b/src/declarative/items/qquickcanvas.h index 6f9d40da48..c27776f234 100644 --- a/src/declarative/items/qquickcanvas.h +++ b/src/declarative/items/qquickcanvas.h @@ -45,6 +45,7 @@ #include <QtCore/qmetatype.h> #include <QtGui/qopengl.h> #include <QtGui/qwindow.h> +#include <QtGui/qevent.h> QT_BEGIN_HEADER diff --git a/src/declarative/items/qquickimage.cpp b/src/declarative/items/qquickimage.cpp index 2f924fa313..46b8ea0915 100644 --- a/src/declarative/items/qquickimage.cpp +++ b/src/declarative/items/qquickimage.cpp @@ -167,10 +167,10 @@ QQuickImage::~QQuickImage() d->provider->deleteLater(); } -void QQuickImagePrivate::setPixmap(const QPixmap &pixmap) +void QQuickImagePrivate::setImage(const QImage &image) { Q_Q(QQuickImage); - pix.setPixmap(pixmap); + pix.setImage(image); q->pixmapChange(); status = pix.isNull() ? QQuickImageBase::Null : QQuickImageBase::Ready; @@ -544,7 +544,7 @@ QSGTextureProvider *QQuickImage::textureProvider() const QQuickImagePrivate *dd = const_cast<QQuickImagePrivate *>(d); dd->provider = new QQuickImageTextureProvider; dd->provider->m_smooth = d->smooth; - dd->provider->m_texture = d->pix.texture(d->sceneGraphContext()); + dd->provider->m_texture = d->sceneGraphContext()->textureForFactory(d->pix.textureFactory()); } return d->provider; @@ -554,7 +554,7 @@ QSGNode *QQuickImage::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *) { Q_D(QQuickImage); - QSGTexture *texture = d->pix.texture(d->sceneGraphContext()); + QSGTexture *texture = d->sceneGraphContext()->textureForFactory(d->pix.textureFactory()); // Copy over the current texture state into the texture provider... if (d->provider) { diff --git a/src/declarative/items/qquickimage_p_p.h b/src/declarative/items/qquickimage_p_p.h index b343821d2f..2077dfb0b3 100644 --- a/src/declarative/items/qquickimage_p_p.h +++ b/src/declarative/items/qquickimage_p_p.h @@ -71,7 +71,7 @@ public: QQuickImage::FillMode fillMode; qreal paintedWidth; qreal paintedHeight; - void setPixmap(const QPixmap &pix); + void setImage(const QImage &img); bool pixmapChanged : 1; QQuickImage::HAlignment hAlign; diff --git a/src/declarative/items/qquickimagebase.cpp b/src/declarative/items/qquickimagebase.cpp index e5030c5e09..d0e495eff5 100644 --- a/src/declarative/items/qquickimagebase.cpp +++ b/src/declarative/items/qquickimagebase.cpp @@ -163,10 +163,10 @@ void QQuickImageBase::setCache(bool cache) load(); } -QPixmap QQuickImageBase::pixmap() const +QImage QQuickImageBase::image() const { Q_D(const QQuickImageBase); - return d->pix.pixmap(); + return d->pix.image(); } void QQuickImageBase::setMirror(bool mirror) diff --git a/src/declarative/items/qquickimagebase_p.h b/src/declarative/items/qquickimagebase_p.h index f15ab6e5b1..51b5f088d4 100644 --- a/src/declarative/items/qquickimagebase_p.h +++ b/src/declarative/items/qquickimagebase_p.h @@ -79,7 +79,7 @@ public: bool cache() const; void setCache(bool); - QPixmap pixmap() const; + QImage image() const; virtual void setSourceSize(const QSize&); QSize sourceSize() const; diff --git a/src/declarative/items/qquicktext.cpp b/src/declarative/items/qquicktext.cpp index c35c61d9b8..630b466d80 100644 --- a/src/declarative/items/qquicktext.cpp +++ b/src/declarative/items/qquicktext.cpp @@ -155,7 +155,7 @@ QVariant QQuickTextDocumentWithImageResources::loadResource(int type, const QUrl QDeclarativePixmap *p = *iter; if (p->isReady()) { - return p->pixmap(); + return p->image(); } else if (p->isError()) { if (!errors.contains(url)) { errors.insert(url); diff --git a/src/declarative/items/qquickview.cpp b/src/declarative/items/qquickview.cpp index bf01a895a6..9d431c865a 100644 --- a/src/declarative/items/qquickview.cpp +++ b/src/declarative/items/qquickview.cpp @@ -60,8 +60,6 @@ void QQuickViewPrivate::init() { Q_Q(QQuickView); - QDeclarativeEnginePrivate::get(&engine)->sgContext = QQuickCanvasPrivate::context; - engine.setIncubationController(q->incubationController()); if (QDeclarativeDebugService::isDebuggingEnabled()) diff --git a/src/declarative/qml/qdeclarativeengine.cpp b/src/declarative/qml/qdeclarativeengine.cpp index 5de1e573e6..d0d095567e 100644 --- a/src/declarative/qml/qdeclarativeengine.cpp +++ b/src/declarative/qml/qdeclarativeengine.cpp @@ -341,7 +341,7 @@ QDeclarativeEnginePrivate::QDeclarativeEnginePrivate(QDeclarativeEngine *e) workerScriptEngine(0), activeVME(0), networkAccessManager(0), networkAccessManagerFactory(0), scarceResourcesRefCount(0), typeLoader(e), importDatabase(e), uniqueId(1), - incubatorCount(0), incubationController(0), sgContext(0), mutex(QMutex::Recursive) + incubatorCount(0), incubationController(0), mutex(QMutex::Recursive) { if (!qt_QmlQtModule_registered) { qt_QmlQtModule_registered = true; @@ -714,7 +714,7 @@ QDeclarativeImageProvider::ImageType QDeclarativeEnginePrivate::getImageProvider return QDeclarativeImageProvider::Invalid; } -QSGTexture *QDeclarativeEnginePrivate::getTextureFromProvider(const QUrl &url, QSize *size, const QSize& req_size) +QDeclarativeTextureFactory *QDeclarativeEnginePrivate::getTextureFromProvider(const QUrl &url, QSize *size, const QSize& req_size) { QMutexLocker locker(&mutex); QSharedPointer<QDeclarativeImageProvider> provider = imageProviders.value(url.host()); diff --git a/src/declarative/qml/qdeclarativeengine_p.h b/src/declarative/qml/qdeclarativeengine_p.h index 14ee62872d..36d349aa26 100644 --- a/src/declarative/qml/qdeclarativeengine_p.h +++ b/src/declarative/qml/qdeclarativeengine_p.h @@ -101,9 +101,7 @@ class QDeclarativeDelayedError; class QDeclarativeWorkerScriptEngine; class QDeclarativeVME; class QDir; -class QSGTexture; class QDeclarativeIncubator; -class QSGContext; // This needs to be declared here so that the pool for it can live in QDeclarativeEnginePrivate. // The inline method definitions are in qdeclarativeexpression_p.h @@ -176,7 +174,7 @@ public: QHash<QString,QSharedPointer<QDeclarativeImageProvider> > imageProviders; QDeclarativeImageProvider::ImageType getImageProviderType(const QUrl &url); - QSGTexture *getTextureFromProvider(const QUrl &url, QSize *size, const QSize& req_size); + QDeclarativeTextureFactory *getTextureFromProvider(const QUrl &url, QSize *size, const QSize& req_size); QImage getImageFromProvider(const QUrl &url, QSize *size, const QSize& req_size); QPixmap getPixmapFromProvider(const QUrl &url, QSize *size, const QSize& req_size); @@ -270,8 +268,6 @@ public: static bool qml_debugging_enabled; - QSGContext *sgContext; - mutable QMutex mutex; private: diff --git a/src/declarative/qml/qdeclarativeimageprovider.cpp b/src/declarative/qml/qdeclarativeimageprovider.cpp index 56e04c963f..33d240c66b 100644 --- a/src/declarative/qml/qdeclarativeimageprovider.cpp +++ b/src/declarative/qml/qdeclarativeimageprovider.cpp @@ -50,6 +50,48 @@ public: }; /*! + \class QDeclarativeTextureFactory + \since 5.0 + \brief The QDeclarativeTextureFactory class provides an interface for loading custom textures from QML. + + The purpose of the texture factory is to provide a placeholder for a image + data that can be converted into an OpenGL texture. + + Creating a texture directly is not possible as there is rarely an OpenGL context + available in the thread that is responsible for loading the image data. + */ + +QDeclarativeTextureFactory::QDeclarativeTextureFactory() +{ +} + +QDeclarativeTextureFactory::~QDeclarativeTextureFactory() +{ +} + + + +/*! + \fn QSGTexture *QDeclarativeTextureFactory::createTexture() const + + This function is called on the scene graph rendering thread to create a QSGTexture + instance from the factory. + + QML will internally cache the returned texture as needed. Each call to this + function should return a unique instance. + + The OpenGL context used for rendering is bound when this function is called. + */ + +/*! + \fn QSize QDeclarativeTextureFactory::textureSize() const + + Returns the size of the texture. This function will be called from arbitrary threads + and should not rely on an OpenGL context bound. + */ + + +/*! \class QDeclarativeImageProvider \since 4.7 \brief The QDeclarativeImageProvider class provides an interface for supporting pixmaps and threaded image requests in QML. @@ -278,7 +320,7 @@ QPixmap QDeclarativeImageProvider::requestPixmap(const QString &id, QSize *size, implementation of this method is reentrant. */ -QSGTexture *QDeclarativeImageProvider::requestTexture(const QString &id, QSize *size, const QSize &requestedSize) +QDeclarativeTextureFactory *QDeclarativeImageProvider::requestTexture(const QString &id, QSize *size, const QSize &requestedSize) { Q_UNUSED(id); Q_UNUSED(size); diff --git a/src/declarative/qml/qdeclarativeimageprovider.h b/src/declarative/qml/qdeclarativeimageprovider.h index 8d35d14ccf..e85a83d48e 100644 --- a/src/declarative/qml/qdeclarativeimageprovider.h +++ b/src/declarative/qml/qdeclarativeimageprovider.h @@ -54,6 +54,17 @@ QT_MODULE(Declarative) class QDeclarativeImageProviderPrivate; class QSGTexture; +class Q_DECLARATIVE_EXPORT QDeclarativeTextureFactory : public QObject +{ +public: + QDeclarativeTextureFactory(); + ~QDeclarativeTextureFactory(); + + virtual QSGTexture *createTexture() const = 0; + virtual QSize textureSize() const = 0; + virtual int textureByteCount() const = 0; +}; + class Q_DECLARATIVE_EXPORT QDeclarativeImageProvider { public: @@ -71,7 +82,7 @@ public: virtual QImage requestImage(const QString &id, QSize *size, const QSize& requestedSize); virtual QPixmap requestPixmap(const QString &id, QSize *size, const QSize& requestedSize); - virtual QSGTexture *requestTexture(const QString &id, QSize *size, const QSize &requestedSize); + virtual QDeclarativeTextureFactory *requestTexture(const QString &id, QSize *size, const QSize &requestedSize); private: QDeclarativeImageProviderPrivate *d; diff --git a/src/declarative/scenegraph/qsgcontext.cpp b/src/declarative/scenegraph/qsgcontext.cpp index fa07f91392..1545303d4a 100644 --- a/src/declarative/scenegraph/qsgcontext.cpp +++ b/src/declarative/scenegraph/qsgcontext.cpp @@ -43,6 +43,8 @@ #include <private/qsgrenderer_p.h> #include "qsgnode.h" +#include <private/qdeclarativepixmapcache_p.h> + #include <private/qsgdefaultrenderer_p.h> #include <private/qsgdistancefieldutil_p.h> @@ -56,6 +58,8 @@ #include <QGuiApplication> #include <QOpenGLContext> +#include <QDeclarativeImageProvider> + #include <private/qobject_p.h> #include <qmutex.h> @@ -107,12 +111,10 @@ public: QOpenGLContext *gl; QHash<QSGMaterialType *, QSGMaterialShader *> materials; + QHash<QDeclarativeTextureFactory *, QSGTexture *> textures; QSGDistanceFieldGlyphCacheManager *distanceFieldCacheManager; - QMutex textureMutex; - QList<QSGTexture *> texturesToClean; - bool flashMode; float renderAlpha; bool distanceFieldDisabled; @@ -139,43 +141,46 @@ QSGContext::QSGContext(QObject *parent) : QSGContext::~QSGContext() { Q_D(QSGContext); + qDeleteAll(d->textures.values()); + d->textures.clear(); delete d->renderer; delete d->rootNode; - cleanupTextures(); qDeleteAll(d->materials.values()); delete d->distanceFieldCacheManager; } -/*! - Schedules the texture to be cleaned up on the rendering thread - at a later time. - The texture can be considered as deleted after this function has - been called. - */ -void QSGContext::scheduleTextureForCleanup(QSGTexture *texture) +QSGTexture *QSGContext::textureForFactory(QDeclarativeTextureFactory *factory) { Q_D(QSGContext); - d->textureMutex.lock(); - Q_ASSERT(!d->texturesToClean.contains(texture)); - d->texturesToClean << texture; - d->textureMutex.unlock(); + if (!factory) + return 0; + + QSGTexture *texture = d->textures.value(factory); + if (!texture) { + if (QDeclarativeDefaultTextureFactory *dtf = qobject_cast<QDeclarativeDefaultTextureFactory *>(factory)) + texture = createTexture(dtf->image()); + else + texture = factory->createTexture(); + d->textures.insert(factory, texture); + connect(factory, SIGNAL(destroyed(QObject *)), this, SLOT(textureFactoryDestroyed(QObject *))); + } + return texture; } - -/*! - Deletes all textures that have been scheduled for cleanup - */ -void QSGContext::cleanupTextures() +void QSGContext::textureFactoryDestroyed(QObject *o) { Q_D(QSGContext); - d->textureMutex.lock(); - qDeleteAll(d->texturesToClean); - d->texturesToClean.clear(); - d->textureMutex.unlock(); + QDeclarativeTextureFactory *f = static_cast<QDeclarativeTextureFactory *>(o); + + // This function will only be called on the scene graph thread, so it is + // safe to directly delete the texture here. + delete d->textures.take(f); } + + /*! Returns the renderer. The renderer instance is created through the adaptation layer. */ @@ -240,8 +245,6 @@ void QSGContext::renderNextFrame(QOpenGLFramebufferObject *fbo) { Q_D(QSGContext); - cleanupTextures(); - if (fbo) { QSGBindableFbo bindable(fbo); d->renderer->renderScene(bindable); diff --git a/src/declarative/scenegraph/qsgcontext_p.h b/src/declarative/scenegraph/qsgcontext_p.h index 41a9a4a89b..ed1654b8fc 100644 --- a/src/declarative/scenegraph/qsgcontext_p.h +++ b/src/declarative/scenegraph/qsgcontext_p.h @@ -73,6 +73,8 @@ class QSGEngine; class QOpenGLContext; class QOpenGLFramebufferObject; +class QDeclarativeTextureFactory; + class Q_DECLARATIVE_EXPORT QSGContext : public QObject { Q_OBJECT @@ -113,10 +115,9 @@ public: virtual QSurfaceFormat defaultSurfaceFormat() const; - static QSGContext *createDefaultContext(); + QSGTexture *textureForFactory(QDeclarativeTextureFactory *factory); - void scheduleTextureForCleanup(QSGTexture *texture); - void cleanupTextures(); + static QSGContext *createDefaultContext(); void setFlashModeEnabled(bool enabled); bool isFlashModeEnabled() const; @@ -131,6 +132,9 @@ public: signals: void ready(); + +public slots: + void textureFactoryDestroyed(QObject *o); }; QT_END_NAMESPACE diff --git a/src/declarative/util/qdeclarativepixmapcache.cpp b/src/declarative/util/qdeclarativepixmapcache.cpp index fab7aebc24..b57bfac585 100644 --- a/src/declarative/util/qdeclarativepixmapcache.cpp +++ b/src/declarative/util/qdeclarativepixmapcache.cpp @@ -47,9 +47,7 @@ #include <private/qdeclarativeglobal_p.h> #include <private/qdeclarativeengine_p.h> -#include <qsgtexture.h> #include <private/qsgtexture_p.h> -#include <private/qsgcontext_p.h> #include <QCoreApplication> #include <QImageReader> @@ -78,6 +76,13 @@ QT_BEGIN_NAMESPACE // The cache limit describes the maximum "junk" in the cache. static int cache_limit = 2048 * 1024; // 2048 KB cache limit for embedded in qpixmapcache.cpp +QSGTexture *QDeclarativeDefaultTextureFactory::createTexture() const +{ + QSGPlainTexture *t = new QSGPlainTexture(); + t->setImage(im); + return t; +} + class QDeclarativePixmapReader; class QDeclarativePixmapData; class QDeclarativePixmapReply : public QObject @@ -99,18 +104,16 @@ public: class Event : public QEvent { public: - Event(ReadError, const QString &, const QSize &, const QImage &image); - Event(ReadError, const QString &, const QSize &, QSGTexture *t, QSGContext *context, const QImage &image); + Event(ReadError, const QString &, const QSize &, QDeclarativeTextureFactory *factory, const QImage &image); ReadError error; QString errorString; QSize implicitSize; QImage image; - QSGTexture *texture; - QSGContext *context; + QDeclarativeTextureFactory *textureFactory; }; - void postReply(ReadError, const QString &, const QSize &, const QImage &); - void postReply(ReadError, const QString &, const QSize &, QSGTexture *t, QSGContext *context, const QImage &image); + void postReply(ReadError, const QString &, const QSize &, const QImage &image); + void postReply(ReadError, const QString &, const QSize &, QDeclarativeTextureFactory *factory, const QImage &image); Q_SIGNALS: @@ -191,40 +194,40 @@ class QDeclarativePixmapData public: QDeclarativePixmapData(QDeclarativePixmap *pixmap, const QUrl &u, const QSize &s, const QString &e) : refCount(1), inCache(false), pixmapStatus(QDeclarativePixmap::Error), - url(u), errorString(e), requestSize(s), texture(0), context(0), - reply(0), prevUnreferenced(0), prevUnreferencedPtr(0), nextUnreferenced(0) + url(u), errorString(e), requestSize(s), textureFactory(0), reply(0), prevUnreferenced(0), + prevUnreferencedPtr(0), nextUnreferenced(0) { declarativePixmaps.insert(pixmap); } QDeclarativePixmapData(QDeclarativePixmap *pixmap, const QUrl &u, const QSize &r) : refCount(1), inCache(false), pixmapStatus(QDeclarativePixmap::Loading), - url(u), requestSize(r), texture(0), context(0), - reply(0), prevUnreferenced(0), prevUnreferencedPtr(0), nextUnreferenced(0) + url(u), requestSize(r), textureFactory(0), reply(0), prevUnreferenced(0), prevUnreferencedPtr(0), + nextUnreferenced(0) { declarativePixmaps.insert(pixmap); } - QDeclarativePixmapData(QDeclarativePixmap *pixmap, const QUrl &u, const QPixmap &p, const QSize &s, const QSize &r) + QDeclarativePixmapData(QDeclarativePixmap *pixmap, const QUrl &u, const QImage &p, const QSize &s, const QSize &r) : refCount(1), inCache(false), privatePixmap(false), pixmapStatus(QDeclarativePixmap::Ready), - url(u), pixmap(p), implicitSize(s), requestSize(r), texture(0), context(0), - reply(0), prevUnreferenced(0), prevUnreferencedPtr(0), nextUnreferenced(0) + url(u), image(p), implicitSize(s), requestSize(r), textureFactory(new QDeclarativeDefaultTextureFactory(p)), reply(0), prevUnreferenced(0), + prevUnreferencedPtr(0), nextUnreferenced(0) { declarativePixmaps.insert(pixmap); } - QDeclarativePixmapData(QDeclarativePixmap *pixmap, const QUrl &u, QSGTexture *t, QSGContext *c, const QPixmap &p, const QSize &s, const QSize &r) + QDeclarativePixmapData(QDeclarativePixmap *pixmap, const QUrl &u, QDeclarativeTextureFactory *factory, const QImage &p, const QSize &s, const QSize &r) : refCount(1), inCache(false), privatePixmap(false), pixmapStatus(QDeclarativePixmap::Ready), - url(u), pixmap(p), implicitSize(s), requestSize(r), texture(t), context(c), - reply(0), prevUnreferenced(0), prevUnreferencedPtr(0), nextUnreferenced(0) + url(u), image(p), implicitSize(s), requestSize(r), textureFactory(factory), reply(0), prevUnreferenced(0), + prevUnreferencedPtr(0), nextUnreferenced(0) { declarativePixmaps.insert(pixmap); } - QDeclarativePixmapData(QDeclarativePixmap *pixmap, const QPixmap &p) + QDeclarativePixmapData(QDeclarativePixmap *pixmap, const QImage &p) : refCount(1), inCache(false), privatePixmap(true), pixmapStatus(QDeclarativePixmap::Ready), - pixmap(p), implicitSize(p.size()), requestSize(p.size()), texture(0), context(0), - reply(0), prevUnreferenced(0), prevUnreferencedPtr(0), nextUnreferenced(0) + image(p), implicitSize(p.size()), requestSize(p.size()), textureFactory(new QDeclarativeDefaultTextureFactory(p)), reply(0), prevUnreferenced(0), + prevUnreferencedPtr(0), nextUnreferenced(0) { declarativePixmaps.insert(pixmap); } @@ -236,10 +239,7 @@ public: declarativePixmaps.remove(referencer); referencer->d = 0; } - - if (texture && context) { - context->scheduleTextureForCleanup(texture); - } + delete textureFactory; } int cost() const; @@ -256,12 +256,11 @@ public: QDeclarativePixmap::Status pixmapStatus; QUrl url; QString errorString; - QPixmap pixmap; + QImage image; QSize implicitSize; QSize requestSize; - QSGTexture *texture; - QSGContext *context; + QDeclarativeTextureFactory *textureFactory; QIntrusiveList<QDeclarativePixmap, &QDeclarativePixmap::dataListNode> declarativePixmaps; QDeclarativePixmapReply *reply; @@ -288,24 +287,19 @@ void QDeclarativePixmapReply::postReply(ReadError error, const QString &errorStr const QSize &implicitSize, const QImage &image) { loading = false; - QCoreApplication::postEvent(this, new Event(error, errorString, implicitSize, image)); + QCoreApplication::postEvent(this, new Event(error, errorString, implicitSize, new QDeclarativeDefaultTextureFactory(image), image)); } void QDeclarativePixmapReply::postReply(ReadError error, const QString &errorString, - const QSize &implicitSize, QSGTexture *texture, - QSGContext *context, const QImage &image) + const QSize &implicitSize, QDeclarativeTextureFactory *factory, + const QImage &image) { loading = false; - QCoreApplication::postEvent(this, new Event(error, errorString, implicitSize, texture, context, image)); + QCoreApplication::postEvent(this, new Event(error, errorString, implicitSize, factory, image)); } -QDeclarativePixmapReply::Event::Event(ReadError e, const QString &s, const QSize &iSize, const QImage &i) - : QEvent(QEvent::User), error(e), errorString(s), implicitSize(iSize), image(i), texture(0), context(0) -{ -} - -QDeclarativePixmapReply::Event::Event(ReadError e, const QString &s, const QSize &iSize, QSGTexture *t, QSGContext *c, const QImage &i) - : QEvent(QEvent::User), error(e), errorString(s), implicitSize(iSize), image(i), texture(t), context(c) +QDeclarativePixmapReply::Event::Event(ReadError e, const QString &s, const QSize &iSize, QDeclarativeTextureFactory *factory, const QImage &i) + : QEvent(QEvent::User), error(e), errorString(s), implicitSize(iSize), image(i), textureFactory(factory) { } @@ -420,8 +414,6 @@ void QDeclarativePixmapReader::networkRequestDone(QNetworkReply *reply) } QImage image; - QSGTexture *texture = 0; - QSGContext *ctx = QDeclarativeEnginePrivate::get(engine)->sgContext; QDeclarativePixmapReply::ReadError error = QDeclarativePixmapReply::NoError; QString errorString; QSize readSize; @@ -432,25 +424,13 @@ void QDeclarativePixmapReader::networkRequestDone(QNetworkReply *reply) QByteArray all = reply->readAll(); QBuffer buff(&all); buff.open(QIODevice::ReadOnly); - if (ctx && ctx->canDecodeImageToTexture()) - texture = ctx->decodeImageToTexture(&buff, &readSize, job->requestSize); - if (!texture) { - if (!readImage(reply->url(), &buff, &image, &errorString, &readSize, job->requestSize)) { - error = QDeclarativePixmapReply::Decoding; - } else if (ctx) { - texture = ctx->createTexture(image); - } - } - } + if (!readImage(reply->url(), &buff, &image, &errorString, &readSize, job->requestSize)) + error = QDeclarativePixmapReply::Decoding; + } // send completion event to the QDeclarativePixmapReply mutex.lock(); if (!cancelled.contains(job)) { - if (texture) - job->postReply(error, errorString, readSize, texture, ctx, image); - else - job->postReply(error, errorString, readSize, image); - } else { - delete texture; + job->postReply(error, errorString, readSize, image); } mutex.unlock(); } @@ -526,8 +506,6 @@ void QDeclarativePixmapReader::processJobs() void QDeclarativePixmapReader::processJob(QDeclarativePixmapReply *runningJob, const QUrl &url, const QSize &requestSize) { - QSGContext *sgContext = QDeclarativeEnginePrivate::get(engine)->sgContext; - // fetch if (url.scheme() == QLatin1String("image")) { // Use QmlImageProvider @@ -539,12 +517,8 @@ void QDeclarativePixmapReader::processJob(QDeclarativePixmapReply *runningJob, c QString errorStr = QDeclarativePixmap::tr("Invalid image provider: %1").arg(url.toString()); QImage image; mutex.lock(); - if (!cancelled.contains(runningJob)) { - if (sgContext) - runningJob->postReply(errorCode, errorStr, readSize, sgContext->createTexture(image), sgContext, image); - else - runningJob->postReply(errorCode, errorStr, readSize, image); - } + if (!cancelled.contains(runningJob)) + runningJob->postReply(errorCode, errorStr, readSize, image); mutex.unlock(); } else if (imageType == QDeclarativeImageProvider::Image) { QImage image = ep->getImageFromProvider(url, &readSize, requestSize); @@ -555,15 +529,11 @@ void QDeclarativePixmapReader::processJob(QDeclarativePixmapReply *runningJob, c errorStr = QDeclarativePixmap::tr("Failed to get image from provider: %1").arg(url.toString()); } mutex.lock(); - if (!cancelled.contains(runningJob)) { - if (sgContext) - runningJob->postReply(errorCode, errorStr, readSize, sgContext->createTexture(image), sgContext, image); - else - runningJob->postReply(errorCode, errorStr, readSize, image); - } + if (!cancelled.contains(runningJob)) + runningJob->postReply(errorCode, errorStr, readSize, image); mutex.unlock(); } else { - QSGTexture *t = ep->getTextureFromProvider(url, &readSize, requestSize); + QDeclarativeTextureFactory *t = ep->getTextureFromProvider(url, &readSize, requestSize); QDeclarativePixmapReply::ReadError errorCode = QDeclarativePixmapReply::NoError; QString errorStr; if (!t) { @@ -572,7 +542,7 @@ void QDeclarativePixmapReader::processJob(QDeclarativePixmapReply *runningJob, c } mutex.lock(); if (!cancelled.contains(runningJob)) - runningJob->postReply(errorCode, errorStr, readSize, t, sgContext, QImage()); + runningJob->postReply(errorCode, errorStr, readSize, t, QImage()); mutex.unlock(); } @@ -586,30 +556,16 @@ void QDeclarativePixmapReader::processJob(QDeclarativePixmapReply *runningJob, c QString errorStr; QFile f(lf); QSize readSize; - QSGTexture *texture = 0; if (f.open(QIODevice::ReadOnly)) { - if (sgContext && sgContext ->canDecodeImageToTexture()) - texture = sgContext->decodeImageToTexture(&f, &readSize, requestSize); - if (!texture) { - if (!readImage(url, &f, &image, &errorStr, &readSize, requestSize)) { - errorCode = QDeclarativePixmapReply::Loading; - } else if (sgContext) { - texture = sgContext->createTexture(image); - } - } + if (!readImage(url, &f, &image, &errorStr, &readSize, requestSize)) + errorCode = QDeclarativePixmapReply::Loading; } else { errorStr = QDeclarativePixmap::tr("Cannot open: %1").arg(url.toString()); errorCode = QDeclarativePixmapReply::Loading; } mutex.lock(); - if (!cancelled.contains(runningJob)) { - if (texture) - runningJob->postReply(errorCode, errorStr, readSize, texture, sgContext, image); - else - runningJob->postReply(errorCode, errorStr, readSize, image); - } else { - delete texture; - } + if (!cancelled.contains(runningJob)) + runningJob->postReply(errorCode, errorStr, readSize, image); mutex.unlock(); } else { // Network resource @@ -728,9 +684,6 @@ protected: public: QHash<QDeclarativePixmapKey, QDeclarativePixmapData *> m_cache; - void cleanTexturesForContext(QSGContext *context); - void cleanTextureForContext(QDeclarativePixmapData *data); - private: void shrinkCache(int remove); @@ -743,12 +696,6 @@ private: Q_GLOBAL_STATIC(QDeclarativePixmapStore, pixmapStore); -void qt_declarative_pixmapstore_clean(QSGContext *context) -{ - pixmapStore()->cleanTexturesForContext(context); -} - - QDeclarativePixmapStore::QDeclarativePixmapStore() : m_unreferencedPixmaps(0), m_lastUnreferencedPixmap(0), m_unreferencedCost(0), m_timerId(-1) { @@ -757,7 +704,6 @@ QDeclarativePixmapStore::QDeclarativePixmapStore() QDeclarativePixmapStore::~QDeclarativePixmapStore() { int leakedPixmaps = 0; - int leakedTextures = 0; QList<QDeclarativePixmapData*> cachedData = m_cache.values(); // unreference all (leaked) pixmaps @@ -765,8 +711,6 @@ QDeclarativePixmapStore::~QDeclarativePixmapStore() int currRefCount = pixmap->refCount; if (currRefCount) { leakedPixmaps++; - if (pixmap->texture) - leakedTextures++; while (currRefCount > 0) { pixmap->release(); currRefCount--; @@ -780,43 +724,9 @@ QDeclarativePixmapStore::~QDeclarativePixmapStore() } if (leakedPixmaps) - qDebug("Number of leaked pixmaps: %i (of which %i are leaked textures)", leakedPixmaps, leakedTextures); + qDebug("Number of leaked pixmaps: %i", leakedPixmaps); } -void QDeclarativePixmapStore::cleanTextureForContext(QDeclarativePixmapData *data) -{ - if (data->context) { - Q_ASSERT(QOpenGLContext::currentContext()); - delete data->texture; - data->context = 0; - data->texture = 0; - } -} - -void QDeclarativePixmapStore::cleanTexturesForContext(QSGContext *context) -{ - QDeclarativePixmapData *data = m_unreferencedPixmaps; - while (data) { - if (data->context == context) - cleanTextureForContext(data); - if (data == m_lastUnreferencedPixmap) - break; - data = data->nextUnreferenced; - } - - QHash<QDeclarativePixmapKey, QDeclarativePixmapData *>::iterator it = m_cache.begin(); - while (it != m_cache.end()) { - data = *it; - if (data->context == context) { - cleanTextureForContext(data); - } - ++it; - } - -} - - - void QDeclarativePixmapStore::unreferencePixmap(QDeclarativePixmapData *data) { Q_ASSERT(data->prevUnreferenced == 0); @@ -914,11 +824,10 @@ bool QDeclarativePixmapReply::event(QEvent *event) data->pixmapStatus = (de->error == NoError) ? QDeclarativePixmap::Ready : QDeclarativePixmap::Error; if (data->pixmapStatus == QDeclarativePixmap::Ready) { - if (de->texture) { - data->texture = de->texture; - data->context = de->context; + if (de->textureFactory) { + data->textureFactory = de->textureFactory; } - data->pixmap = QPixmap::fromImage(de->image); + data->image = de->image; data->implicitSize = de->implicitSize; } else { data->errorString = de->errorString; @@ -938,11 +847,9 @@ bool QDeclarativePixmapReply::event(QEvent *event) int QDeclarativePixmapData::cost() const { - if (texture) { - const QSize textureSize = texture->textureSize(); - return textureSize.width() * textureSize.height(); - } - return (pixmap.width() * pixmap.height() * pixmap.depth()) / 8; + if (textureFactory) + return textureFactory->textureByteCount(); + return image.byteCount(); } void QDeclarativePixmapData::addref() @@ -997,8 +904,6 @@ void QDeclarativePixmapData::removeFromCache() static QDeclarativePixmapData* createPixmapDataSync(QDeclarativePixmap *declarativePixmap, QDeclarativeEngine *engine, const QUrl &url, const QSize &requestSize, bool *ok) { - QSGContext *sgContext = QDeclarativeEnginePrivate::get(engine)->sgContext; - if (url.scheme() == QLatin1String("image")) { QSize readSize; QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(engine); @@ -1010,10 +915,10 @@ static QDeclarativePixmapData* createPixmapDataSync(QDeclarativePixmap *declarat QDeclarativePixmap::tr("Invalid image provider: %1").arg(url.toString())); case QDeclarativeImageProvider::Texture: { - QSGTexture *texture = ep->getTextureFromProvider(url, &readSize, requestSize); + QDeclarativeTextureFactory *texture = ep->getTextureFromProvider(url, &readSize, requestSize); if (texture) { *ok = true; - return new QDeclarativePixmapData(declarativePixmap, url, texture, sgContext, QPixmap(), readSize, requestSize); + return new QDeclarativePixmapData(declarativePixmap, url, texture, QImage(), readSize, requestSize); } } @@ -1022,11 +927,7 @@ static QDeclarativePixmapData* createPixmapDataSync(QDeclarativePixmap *declarat QImage image = ep->getImageFromProvider(url, &readSize, requestSize); if (!image.isNull()) { *ok = true; - if (sgContext) { - QSGTexture *t = sgContext->createTexture(image); - return new QDeclarativePixmapData(declarativePixmap, url, t, sgContext, QPixmap::fromImage(image), readSize, requestSize); - } - return new QDeclarativePixmapData(declarativePixmap, url, QPixmap::fromImage(image), readSize, requestSize); + return new QDeclarativePixmapData(declarativePixmap, url, image, readSize, requestSize); } } case QDeclarativeImageProvider::Pixmap: @@ -1034,11 +935,7 @@ static QDeclarativePixmapData* createPixmapDataSync(QDeclarativePixmap *declarat QPixmap pixmap = ep->getPixmapFromProvider(url, &readSize, requestSize); if (!pixmap.isNull()) { *ok = true; - if (sgContext) { - QSGTexture *t = sgContext->createTexture(pixmap.toImage()); - return new QDeclarativePixmapData(declarativePixmap, url, t, sgContext, pixmap, readSize, requestSize); - } - return new QDeclarativePixmapData(declarativePixmap, url, pixmap, readSize, requestSize); + return new QDeclarativePixmapData(declarativePixmap, url, pixmap.toImage(), readSize, requestSize); } } } @@ -1057,28 +954,13 @@ static QDeclarativePixmapData* createPixmapDataSync(QDeclarativePixmap *declarat QString errorString; if (f.open(QIODevice::ReadOnly)) { - QSGContext *ctx = QDeclarativeEnginePrivate::get(engine)->sgContext; - QSGTexture *texture = 0; QImage image; - if (ctx && ctx->canDecodeImageToTexture()) { - texture = ctx->decodeImageToTexture(&f, &readSize, requestSize); + if (readImage(url, &f, &image, &errorString, &readSize, requestSize)) { *ok = true; + return new QDeclarativePixmapData(declarativePixmap, url, image, readSize, requestSize); } - - if (!texture) { - if (readImage(url, &f, &image, &errorString, &readSize, requestSize)) - *ok = true; - - if (ok && ctx) { - texture = ctx->createTexture(image); - } - } - - if (texture) - return new QDeclarativePixmapData(declarativePixmap, url, texture, ctx, QPixmap::fromImage(image), readSize, requestSize); - else - return new QDeclarativePixmapData(declarativePixmap, url, QPixmap::fromImage(image), readSize, requestSize); + errorString = QDeclarativePixmap::tr("Invalid image data: %1").arg(url.toString()); } else { errorString = QDeclarativePixmap::tr("Cannot open: %1").arg(url.toString()); @@ -1089,7 +971,7 @@ static QDeclarativePixmapData* createPixmapDataSync(QDeclarativePixmap *declarat struct QDeclarativePixmapNull { QUrl url; - QPixmap pixmap; + QImage image; QSize size; }; Q_GLOBAL_STATIC(QDeclarativePixmapNull, nullPixmap); @@ -1180,29 +1062,23 @@ const QSize &QDeclarativePixmap::requestSize() const return nullPixmap()->size; } -QSGTexture *QDeclarativePixmap::texture(QSGContext *context) const +QDeclarativeTextureFactory *QDeclarativePixmap::textureFactory() const { - if (d) { - if (d->texture) - return d->texture; - else if (d->pixmapStatus == Ready) { - d->texture = context->createTexture(d->pixmap.toImage()); - d->context = context; - return d->texture; - } - } + if (d) + return d->textureFactory; + return 0; } -const QPixmap &QDeclarativePixmap::pixmap() const +const QImage &QDeclarativePixmap::image() const { if (d) - return d->pixmap; + return d->image; else - return nullPixmap()->pixmap; + return nullPixmap()->image; } -void QDeclarativePixmap::setPixmap(const QPixmap &p) +void QDeclarativePixmap::setImage(const QImage &p) { clear(); @@ -1213,7 +1089,7 @@ void QDeclarativePixmap::setPixmap(const QPixmap &p) int QDeclarativePixmap::width() const { if (d) - return d->texture ? d->texture->textureSize().width() : d->pixmap.width(); + return d->textureFactory ? d->textureFactory->textureSize().width() : d->image.width(); else return 0; } @@ -1221,7 +1097,7 @@ int QDeclarativePixmap::width() const int QDeclarativePixmap::height() const { if (d) - return d->texture? d->texture->textureSize().height() : d->pixmap.height(); + return d->textureFactory ? d->textureFactory->textureSize().height() : d->image.height(); else return 0; } @@ -1229,7 +1105,7 @@ int QDeclarativePixmap::height() const QRect QDeclarativePixmap::rect() const { if (d) - return d->texture ? QRect(QPoint(), d->texture->textureSize()) : d->pixmap.rect(); + return d->textureFactory ? QRect(QPoint(), d->textureFactory->textureSize()) : d->image.rect(); else return QRect(); } diff --git a/src/declarative/util/qdeclarativepixmapcache_p.h b/src/declarative/util/qdeclarativepixmapcache_p.h index 542ac3e3a4..a0e35aba5d 100644 --- a/src/declarative/util/qdeclarativepixmapcache_p.h +++ b/src/declarative/util/qdeclarativepixmapcache_p.h @@ -48,6 +48,7 @@ #include <QtCore/qurl.h> #include <private/qintrusivelist_p.h> +#include <qdeclarativeimageprovider.h> QT_BEGIN_HEADER @@ -57,8 +58,25 @@ QT_MODULE(Declarative) class QDeclarativeEngine; class QDeclarativePixmapData; -class QSGTexture; -class QSGContext; +class QDeclarativeTextureFactory; + +class QDeclarativeDefaultTextureFactory : public QDeclarativeTextureFactory +{ + Q_OBJECT +public: + QDeclarativeDefaultTextureFactory(const QImage &i) + : im(i) + { + } + + QSGTexture *createTexture() const; + QSize textureSize() const { return im.size(); } + int textureByteCount() const { return im.byteCount(); } + QImage image() const { return im; } + +private: + QImage im; +}; class Q_DECLARATIVE_EXPORT QDeclarativePixmap { @@ -87,15 +105,14 @@ public: const QUrl &url() const; const QSize &implicitSize() const; const QSize &requestSize() const; - const QPixmap &pixmap() const; - void setPixmap(const QPixmap &); + const QImage &image() const; + void setImage(const QImage &); - QSGTexture *texture(QSGContext *context) const; + QDeclarativeTextureFactory *textureFactory() const; QRect rect() const; int width() const; int height() const; - inline operator const QPixmap &() const; void load(QDeclarativeEngine *, const QUrl &); void load(QDeclarativeEngine *, const QUrl &, QDeclarativePixmap::Options options); @@ -117,11 +134,6 @@ private: friend class QDeclarativePixmapData; }; -inline QDeclarativePixmap::operator const QPixmap &() const -{ - return pixmap(); -} - Q_DECLARE_OPERATORS_FOR_FLAGS(QDeclarativePixmap::Options) QT_END_NAMESPACE diff --git a/src/imports/etcprovider/qetcprovider.cpp b/src/imports/etcprovider/qetcprovider.cpp index 053258a794..5ea7ea9b6b 100644 --- a/src/imports/etcprovider/qetcprovider.cpp +++ b/src/imports/etcprovider/qetcprovider.cpp @@ -143,10 +143,37 @@ QSize EtcTexture::textureSize() const return m_size; } -QSGTexture *QEtcProvider::requestTexture(const QString &id, QSize *size, const QSize &requestedSize) + +class QEtcTextureFactory : public QDeclarativeTextureFactory +{ +public: + QByteArray m_data; + QSize m_size; + QSize m_paddedSize; + + QSize textureSize() const { + return m_size; + } + + int textureByteCount() const { + return m_data.size(); + } + + QSGTexture *createTexture() const { + EtcTexture *texture = new EtcTexture; + texture->m_data = m_data; + texture->m_size = m_size; + texture->m_paddedSize = m_paddedSize; + return texture; + } + +}; + + +QDeclarativeTextureFactory *QEtcProvider::requestTexture(const QString &id, QSize *size, const QSize &requestedSize) { Q_UNUSED(requestedSize); - EtcTexture *ret = 0; + QEtcTextureFactory *ret = 0; size->setHeight(0); size->setWidth(0); @@ -156,7 +183,7 @@ QSGTexture *QEtcProvider::requestTexture(const QString &id, QSize *size, const Q qDebug() << "requestTexture opening file: " << id; #endif if (file.open(QIODevice::ReadOnly)) { - ret = new EtcTexture(); + ret = new QEtcTextureFactory; ret->m_data = file.readAll(); if (!ret->m_data.isEmpty()) { ETCHeader *pETCHeader = NULL; @@ -168,7 +195,7 @@ QSGTexture *QEtcProvider::requestTexture(const QString &id, QSize *size, const Q ret->m_paddedSize.setWidth(getPaddedWidth(pETCHeader)); } else { - free (ret); + delete ret; ret = 0; } } diff --git a/src/imports/etcprovider/qetcprovider.h b/src/imports/etcprovider/qetcprovider.h index 46ec421162..cac37584d4 100644 --- a/src/imports/etcprovider/qetcprovider.h +++ b/src/imports/etcprovider/qetcprovider.h @@ -90,7 +90,7 @@ public: qDebug () << "Creating QEtcProvider."; #endif } - QSGTexture *requestTexture(const QString &id, QSize *size, const QSize &requestedSize); + QDeclarativeTextureFactory *requestTexture(const QString &id, QSize *size, const QSize &requestedSize); }; QT_END_NAMESPACE |