1/****************************************************************************
2**
3** Copyright (C) 2015 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 <Qt3DCore/private/qnode_p.h>
31#include <Qt3DCore/private/qscene_p.h>
32#include <Qt3DCore/private/qnodecreatedchangegenerator_p.h>
33
34#include <Qt3DRender/QBlitFramebuffer>
35#include <Qt3DRender/private/qblitframebuffer_p.h>
36
37#include "testpostmanarbiter.h"
38
39class tst_QBlitFrameBuffer: public QObject
40{
41 Q_OBJECT
42
43public:
44 tst_QBlitFrameBuffer() : QObject()
45 {
46 qRegisterMetaType<Qt3DRender::QRenderTarget*>();
47 }
48
49private Q_SLOTS:
50
51 void checkCreationData_data()
52 {
53 QTest::addColumn<Qt3DRender::QBlitFramebuffer *>(name: "blitFramebuffer");
54
55 Qt3DRender::QBlitFramebuffer *defaultConstructed = new Qt3DRender::QBlitFramebuffer();
56 Qt3DRender::QRenderTarget *sourceRenderTarget = new Qt3DRender::QRenderTarget();
57 Qt3DRender::QRenderTarget *destinationRenderTarget = new Qt3DRender::QRenderTarget();
58 defaultConstructed->setSource(sourceRenderTarget);
59 defaultConstructed->setDestination(destinationRenderTarget);
60 QTest::newRow(dataTag: "defaultConstructed") << defaultConstructed;
61 }
62
63 void checkInitialState()
64 {
65 // GIVEN
66 Qt3DRender::QBlitFramebuffer blitFramebuffer;
67
68 // THEN
69 QCOMPARE(blitFramebuffer.source(), nullptr);
70 QCOMPARE(blitFramebuffer.destination(), nullptr);
71 QCOMPARE(blitFramebuffer.sourceRect(), QRect());
72 QCOMPARE(blitFramebuffer.destinationRect(), QRect());
73 QCOMPARE(blitFramebuffer.sourceAttachmentPoint(), Qt3DRender::QRenderTargetOutput::AttachmentPoint::Color0);
74 QCOMPARE(blitFramebuffer.destinationAttachmentPoint(), Qt3DRender::QRenderTargetOutput::AttachmentPoint::Color0);
75 QCOMPARE(blitFramebuffer.interpolationMethod(), Qt3DRender::QBlitFramebuffer::Linear);
76 }
77
78 void checkCreationData()
79 {
80 // GIVEN
81 QFETCH(Qt3DRender::QBlitFramebuffer *, blitFramebuffer);
82
83 // WHEN
84 Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(blitFramebuffer);
85 QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges = creationChangeGenerator.creationChanges();
86
87 // THEN
88 QCOMPARE(creationChanges.size(), 3); // 3 due to automatic parenting
89
90 const Qt3DCore::QNodeCreatedChangePtr<Qt3DRender::QBlitFramebufferData> creationChangeData =
91 qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QBlitFramebufferData>>(src: creationChanges.first());
92 const Qt3DRender::QBlitFramebufferData &cloneData = creationChangeData->data;
93
94 // THEN
95 QCOMPARE(blitFramebuffer->id(), creationChangeData->subjectId());
96 QCOMPARE(blitFramebuffer->isEnabled(), creationChangeData->isNodeEnabled());
97 QCOMPARE(blitFramebuffer->metaObject(), creationChangeData->metaObject());
98 QCOMPARE(blitFramebuffer->source()->id(), cloneData.m_sourceRenderTargetId);
99 QCOMPARE(blitFramebuffer->destination()->id(), cloneData.m_destinationRenderTargetId);
100 QCOMPARE(blitFramebuffer->sourceRect(), cloneData.m_sourceRect);
101 QCOMPARE(blitFramebuffer->destinationRect(), cloneData.m_destinationRect);
102 QCOMPARE(blitFramebuffer->sourceAttachmentPoint(), cloneData.m_sourceAttachmentPoint);
103 QCOMPARE(blitFramebuffer->destinationAttachmentPoint(), cloneData.m_destinationAttachmentPoint);
104
105
106 delete blitFramebuffer;
107 }
108
109 void checkPropertyUpdate()
110 {
111 // GIVEN
112 TestArbiter arbiter;
113 QScopedPointer<Qt3DRender::QBlitFramebuffer> blitFramebuffer(new Qt3DRender::QBlitFramebuffer());
114 arbiter.setArbiterOnNode(blitFramebuffer.data());
115
116 Qt3DRender::QRenderTarget *sourceRenderTarget = new Qt3DRender::QRenderTarget;
117 Qt3DRender::QRenderTarget *destinationRenderTarget = new Qt3DRender::QRenderTarget;
118
119 // sourceRenderTarget
120 // WHEN
121 blitFramebuffer->setSource(sourceRenderTarget);
122
123 // THEN
124 QCOMPARE(arbiter.events.size(), 0);
125 QCOMPARE(arbiter.dirtyNodes.size(), 1);
126 QCOMPARE(arbiter.dirtyNodes.front(), blitFramebuffer.data());
127
128 arbiter.dirtyNodes.clear();
129
130 // WHEN
131 blitFramebuffer->setSource(sourceRenderTarget);
132
133 // THEN
134 QCOMPARE(arbiter.events.size(), 0);
135 QCOMPARE(arbiter.dirtyNodes.size(), 0);
136
137 // WHEN
138 blitFramebuffer->setSource(nullptr);
139
140 // THEN
141 QCOMPARE(arbiter.events.size(), 0);
142 QCOMPARE(arbiter.dirtyNodes.size(), 1);
143 QCOMPARE(arbiter.dirtyNodes.front(), blitFramebuffer.data());
144
145 arbiter.dirtyNodes.clear();
146
147 // destinationRenderTarget
148 // WHEN
149 blitFramebuffer->setDestination(destinationRenderTarget);
150
151 // THEN
152 QCOMPARE(arbiter.events.size(), 0);
153 QCOMPARE(arbiter.dirtyNodes.size(), 1);
154 QCOMPARE(arbiter.dirtyNodes.front(), blitFramebuffer.data());
155
156 arbiter.dirtyNodes.clear();
157
158 // WHEN
159 blitFramebuffer->setDestination(destinationRenderTarget);
160
161 // THEN
162 QCOMPARE(arbiter.events.size(), 0);
163 QCOMPARE(arbiter.dirtyNodes.size(), 0);
164
165 // WHEN
166 blitFramebuffer->setDestination(nullptr);
167
168 // THEN
169 QCOMPARE(arbiter.events.size(), 0);
170 QCOMPARE(arbiter.dirtyNodes.size(), 1);
171 QCOMPARE(arbiter.dirtyNodes.front(), blitFramebuffer.data());
172
173 arbiter.dirtyNodes.clear();
174
175 // sourceRect
176 // WHEN
177 blitFramebuffer->setSourceRect(QRect(0,0,1,1));
178
179 // THEN
180 QCOMPARE(arbiter.events.size(), 0);
181 QCOMPARE(arbiter.dirtyNodes.size(), 1);
182 QCOMPARE(arbiter.dirtyNodes.front(), blitFramebuffer.data());
183
184 arbiter.dirtyNodes.clear();
185
186 // WHEN
187 blitFramebuffer->setSourceRect(QRect(0,0,1,1));
188
189 // THEN
190 QCOMPARE(arbiter.events.size(), 0);
191 QCOMPARE(arbiter.dirtyNodes.size(), 0);
192
193 // WHEN
194 blitFramebuffer->setSourceRect(QRect());
195
196 // THEN
197 QCOMPARE(arbiter.events.size(), 0);
198 QCOMPARE(arbiter.dirtyNodes.size(), 1);
199 QCOMPARE(arbiter.dirtyNodes.front(), blitFramebuffer.data());
200
201 arbiter.dirtyNodes.clear();
202
203 // destinationRect
204 blitFramebuffer->setDestinationRect(QRect(0,0,1,1));
205
206 // THEN
207 QCOMPARE(arbiter.events.size(), 0);
208 QCOMPARE(arbiter.dirtyNodes.size(), 1);
209 QCOMPARE(arbiter.dirtyNodes.front(), blitFramebuffer.data());
210
211 arbiter.dirtyNodes.clear();
212
213 // WHEN
214 blitFramebuffer->setDestinationRect(QRect(0,0,1,1));
215
216 // THEN
217 QCOMPARE(arbiter.events.size(), 0);
218 QCOMPARE(arbiter.dirtyNodes.size(), 0);
219
220 // WHEN
221 blitFramebuffer->setDestinationRect(QRect());
222
223 // THEN
224 QCOMPARE(arbiter.events.size(), 0);
225 QCOMPARE(arbiter.dirtyNodes.size(), 1);
226 QCOMPARE(arbiter.dirtyNodes.front(), blitFramebuffer.data());
227
228 arbiter.dirtyNodes.clear();
229
230 // sourceAttachmentPoint
231 // WHEN
232 blitFramebuffer->setSourceAttachmentPoint(Qt3DRender::QRenderTargetOutput::Color1);
233
234 // THEN
235 QCOMPARE(arbiter.events.size(), 0);
236 QCOMPARE(arbiter.dirtyNodes.size(), 1);
237 QCOMPARE(arbiter.dirtyNodes.front(), blitFramebuffer.data());
238
239 arbiter.dirtyNodes.clear();
240
241 // WHEN
242 blitFramebuffer->setSourceAttachmentPoint(Qt3DRender::QRenderTargetOutput::Color1);
243
244 // THEN
245 QCOMPARE(arbiter.events.size(), 0);
246 QCOMPARE(arbiter.dirtyNodes.size(), 0);
247
248 // WHEN
249 blitFramebuffer->setSourceAttachmentPoint(Qt3DRender::QRenderTargetOutput::Color0);
250
251 // THEN
252 QCOMPARE(arbiter.events.size(), 0);
253 QCOMPARE(arbiter.dirtyNodes.size(), 1);
254 QCOMPARE(arbiter.dirtyNodes.front(), blitFramebuffer.data());
255
256 arbiter.dirtyNodes.clear();
257
258 // destinationAttachmentPoint
259 // WHEN
260 blitFramebuffer->setDestinationAttachmentPoint(Qt3DRender::QRenderTargetOutput::Color1);
261
262 // THEN
263 QCOMPARE(arbiter.events.size(), 0);
264 QCOMPARE(arbiter.dirtyNodes.size(), 1);
265 QCOMPARE(arbiter.dirtyNodes.front(), blitFramebuffer.data());
266
267 arbiter.dirtyNodes.clear();
268
269 // WHEN
270 blitFramebuffer->setDestinationAttachmentPoint(Qt3DRender::QRenderTargetOutput::Color1);
271
272 // THEN
273 QCOMPARE(arbiter.events.size(), 0);
274 QCOMPARE(arbiter.dirtyNodes.size(), 0);
275
276 // WHEN
277 blitFramebuffer->setDestinationAttachmentPoint(Qt3DRender::QRenderTargetOutput::Color0);
278
279 // THEN
280 QCOMPARE(arbiter.events.size(), 0);
281 QCOMPARE(arbiter.dirtyNodes.size(), 1);
282 QCOMPARE(arbiter.dirtyNodes.front(), blitFramebuffer.data());
283
284 arbiter.dirtyNodes.clear();
285 }
286
287 void checkSourceDestReset()
288 {
289 // GIVEN
290 TestArbiter arbiter;
291 QScopedPointer<Qt3DRender::QBlitFramebuffer> blitFramebuffer(new Qt3DRender::QBlitFramebuffer());
292 arbiter.setArbiterOnNode(blitFramebuffer.data());
293
294 Qt3DRender::QRenderTarget *sourceRenderTarget = new Qt3DRender::QRenderTarget; // no parent
295 Qt3DRender::QRenderTarget *destinationRenderTarget = new Qt3DRender::QRenderTarget(sourceRenderTarget); // have a parent
296
297 // WHEN
298 blitFramebuffer->setSource(sourceRenderTarget);
299 blitFramebuffer->setDestination(destinationRenderTarget);
300 QCoreApplication::processEvents();
301
302 // THEN
303 QCOMPARE(sourceRenderTarget->parent(), blitFramebuffer.data());
304 QCOMPARE(destinationRenderTarget->parent(), sourceRenderTarget); // already set parent must not change
305
306 // WHEN
307 delete sourceRenderTarget;
308
309 // THEN
310 QVERIFY(!blitFramebuffer->source());
311 QVERIFY(!blitFramebuffer->destination()); // gone too since destinationRenderTarget was parented to sourceRenderTarget
312 }
313};
314
315QTEST_MAIN(tst_QBlitFrameBuffer)
316
317#include "tst_qblitframebuffer.moc"
318

source code of qt3d/tests/auto/render/qblitframebuffer/tst_qblitframebuffer.cpp