1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2020 Klaralvdalens Datakonsult AB (KDAB). |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt3D module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | #include <QtTest/QTest> |
30 | #include <Qt3DRender/QTextureImageDataGenerator> |
31 | #include <glresourcemanagers_p.h> |
32 | #include <glresourcemanagers_p.h> |
33 | #include <gltexture_p.h> |
34 | #include <Qt3DCore/qnodeid.h> |
35 | #include "qbackendnodetester.h" |
36 | #include "testrenderer.h" |
37 | |
38 | class TestImageDataGenerator : public Qt3DRender::QTextureImageDataGenerator |
39 | { |
40 | int m_id; |
41 | public: |
42 | TestImageDataGenerator(int id) : m_id(id) {} |
43 | |
44 | Qt3DRender::QTextureImageDataPtr operator ()() override { |
45 | return Qt3DRender::QTextureImageDataPtr::create(); |
46 | } |
47 | |
48 | bool operator ==(const Qt3DRender::QTextureImageDataGenerator &other) const override { |
49 | const TestImageDataGenerator *otherFunctor = Qt3DRender::functor_cast<TestImageDataGenerator>(other: &other); |
50 | return (otherFunctor != nullptr && otherFunctor->m_id == m_id); |
51 | } |
52 | |
53 | QT3D_FUNCTOR(TestImageDataGenerator) |
54 | }; |
55 | |
56 | using ImageDataGeneratorPtr = QSharedPointer<TestImageDataGenerator>; |
57 | |
58 | class tst_GLTextureManager : public Qt3DCore::QBackendNodeTester |
59 | { |
60 | Q_OBJECT |
61 | |
62 | private Q_SLOTS: |
63 | void checkInitialState() |
64 | { |
65 | // GIVEN |
66 | Qt3DRender::Render::OpenGL::GLTextureManager manager; |
67 | const Qt3DCore::QNodeId id = Qt3DCore::QNodeId::createId(); |
68 | Qt3DRender::Render::OpenGL::GLTexture *t = manager.getOrCreateResource(id); |
69 | |
70 | // THEN |
71 | QVERIFY(t != nullptr); |
72 | QVERIFY(!t->isDirty()); |
73 | QVERIFY(!t->hasTextureData()); |
74 | QVERIFY(!t->hasImagesData()); |
75 | QVERIFY(t->dataGenerator().isNull()); |
76 | QVERIFY(t->textureDataUpdates().empty()); |
77 | QCOMPARE(t->dirtyFlags(), Qt3DRender::Render::OpenGL::GLTexture::None); |
78 | QVERIFY(t->images().empty()); |
79 | |
80 | QCOMPARE(t, manager.getOrCreateResource(id)); |
81 | } |
82 | |
83 | void checkCleanup() |
84 | { |
85 | // GIVEN |
86 | Qt3DRender::Render::OpenGL::GLTextureManager manager; |
87 | const Qt3DCore::QNodeId id = Qt3DCore::QNodeId::createId(); |
88 | Qt3DRender::Render::OpenGL::GLTexture *t = manager.getOrCreateResource(id); |
89 | |
90 | // WHEN |
91 | Qt3DRender::Render::OpenGL::GLTexture::Image img = { |
92 | .generator: ImageDataGeneratorPtr::create(arguments: 0), |
93 | .layer: 0, .mipLevel: 0, |
94 | .face: Qt3DRender::QAbstractTexture::AllFaces |
95 | }; |
96 | t->setImages({img}); |
97 | |
98 | // THEN |
99 | QCOMPARE(t->images().size(), 1); |
100 | QCOMPARE(t->dirtyFlags(), Qt3DRender::Render::OpenGL::GLTexture::TextureImageData); |
101 | |
102 | // WHEN |
103 | manager.releaseResource(id); |
104 | |
105 | // THEN -> Cleanup should have been called |
106 | Qt3DRender::Render::OpenGL::GLTexture *t2 = manager.getOrCreateResource(id); |
107 | QCOMPARE(t, t2); |
108 | QVERIFY(t->images().empty()); |
109 | QCOMPARE(t->dirtyFlags(), Qt3DRender::Render::OpenGL::GLTexture::None); |
110 | } |
111 | }; |
112 | |
113 | QTEST_APPLESS_MAIN(tst_GLTextureManager) |
114 | |
115 | #include "tst_gltexturemanager.moc" |
116 | |