QSGSimpleTextureNode: Fix ownership of QSGTexture
authorTobias Koenig <tobias.koenig@kdab.com>
Fri, 3 Jul 2015 08:55:35 +0000 (10:55 +0200)
committerTobias Koenig <tobias.koenig@kdab.com>
Fri, 3 Jul 2015 17:10:15 +0000 (17:10 +0000)
Make sure the QSGSimpleTextureNode deletes the old QSGTexture
if a new one is set via setTexture() and the ownsTexture flag
is true.

[ChangeLog][QtQuick][SceneGraph] QSGSimpleTextureNode will now delete
the currently set QSGTexture object, if a new QSGTexture is set and
the ownsTexture flag is on.

Change-Id: Iabfbccd390e16948d4575baf29e6c8b4a184a404
Reviewed-by: Gunnar Sletta <gunnar@sletta.org>
src/quick/scenegraph/util/qsgsimpletexturenode.cpp
tests/auto/quick/nodes/tst_nodestest.cpp

index 8d38e830293f9878470d05b7f4b6eb23eab0d108..d9f3c443740d737005bf9eeb9afd2abc87e3018c 100644 (file)
@@ -223,9 +223,11 @@ QRectF QSGSimpleTextureNode::sourceRect() const
 void QSGSimpleTextureNode::setTexture(QSGTexture *texture)
 {
     Q_ASSERT(texture);
+    Q_D(QSGSimpleTextureNode);
+    if (d->ownsTexture)
+        delete m_material.texture();
     m_material.setTexture(texture);
     m_opaque_material.setTexture(texture);
-    Q_D(QSGSimpleTextureNode);
     qsgsimpletexturenode_update(&m_geometry, texture, m_rect, d->sourceRect, d->texCoordMode);
 
     DirtyState dirty = DirtyMaterial;
index b49ce349515a1e612474e19ade2a45d210a66c6f..c49f01d5a77e75a800606bffd1d681bd34da5798 100644 (file)
@@ -283,7 +283,7 @@ void NodesTest::textureNodeTextureOwnership()
         QVERIFY(!texture.isNull());
     }
 
-    { // Check that it is deleted when we so desire
+    { // Check that it is deleted on destruction when we so desire
         QPointer<QSGTexture> texture(new QSGPlainTexture());
 
         QSGSimpleTextureNode *tn = new QSGSimpleTextureNode();
@@ -294,6 +294,25 @@ void NodesTest::textureNodeTextureOwnership()
         delete tn;
         QVERIFY(texture.isNull());
     }
+
+    { // Check that it is deleted on update when we so desire
+        QPointer<QSGTexture> oldTexture(new QSGPlainTexture());
+        QPointer<QSGTexture> newTexture(new QSGPlainTexture());
+
+        QSGSimpleTextureNode *tn = new QSGSimpleTextureNode();
+        tn->setOwnsTexture(true);
+        QVERIFY(tn->ownsTexture());
+
+        tn->setTexture(oldTexture);
+        QVERIFY(!oldTexture.isNull());
+        QVERIFY(!newTexture.isNull());
+
+        tn->setTexture(newTexture);
+        QVERIFY(oldTexture.isNull());
+        QVERIFY(!newTexture.isNull());
+
+        delete tn;
+    }
 }
 
 void NodesTest::textureNodeRect()