| 1 | // Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <QtTest/QTest> |
| 5 | #include <Qt3DCore/private/qnode_p.h> |
| 6 | #include <Qt3DCore/private/qscene_p.h> |
| 7 | |
| 8 | #include <Qt3DRender/QBlitFramebuffer> |
| 9 | #include <Qt3DRender/private/qblitframebuffer_p.h> |
| 10 | |
| 11 | #include "testarbiter.h" |
| 12 | |
| 13 | class tst_QBlitFrameBuffer: public QObject |
| 14 | { |
| 15 | Q_OBJECT |
| 16 | |
| 17 | public: |
| 18 | tst_QBlitFrameBuffer() : QObject() |
| 19 | { |
| 20 | qRegisterMetaType<Qt3DRender::QRenderTarget*>(); |
| 21 | } |
| 22 | |
| 23 | private Q_SLOTS: |
| 24 | |
| 25 | void checkCreationData_data() |
| 26 | { |
| 27 | QTest::addColumn<Qt3DRender::QBlitFramebuffer *>("blitFramebuffer"); |
| 28 | |
| 29 | Qt3DRender::QBlitFramebuffer *defaultConstructed = new Qt3DRender::QBlitFramebuffer(); |
| 30 | Qt3DRender::QRenderTarget *sourceRenderTarget = new Qt3DRender::QRenderTarget(); |
| 31 | Qt3DRender::QRenderTarget *destinationRenderTarget = new Qt3DRender::QRenderTarget(); |
| 32 | defaultConstructed->setSource(sourceRenderTarget); |
| 33 | defaultConstructed->setDestination(destinationRenderTarget); |
| 34 | QTest::newRow("defaultConstructed") << defaultConstructed; |
| 35 | } |
| 36 | |
| 37 | void checkInitialState() |
| 38 | { |
| 39 | // GIVEN |
| 40 | Qt3DRender::QBlitFramebuffer blitFramebuffer; |
| 41 | |
| 42 | // THEN |
| 43 | QCOMPARE(blitFramebuffer.source(), nullptr); |
| 44 | QCOMPARE(blitFramebuffer.destination(), nullptr); |
| 45 | QCOMPARE(blitFramebuffer.sourceRect(), QRect()); |
| 46 | QCOMPARE(blitFramebuffer.destinationRect(), QRect()); |
| 47 | QCOMPARE(blitFramebuffer.sourceAttachmentPoint(), Qt3DRender::QRenderTargetOutput::AttachmentPoint::Color0); |
| 48 | QCOMPARE(blitFramebuffer.destinationAttachmentPoint(), Qt3DRender::QRenderTargetOutput::AttachmentPoint::Color0); |
| 49 | QCOMPARE(blitFramebuffer.interpolationMethod(), Qt3DRender::QBlitFramebuffer::Linear); |
| 50 | } |
| 51 | |
| 52 | void checkPropertyUpdate() |
| 53 | { |
| 54 | // GIVEN |
| 55 | TestArbiter arbiter; |
| 56 | QScopedPointer<Qt3DRender::QBlitFramebuffer> blitFramebuffer(new Qt3DRender::QBlitFramebuffer()); |
| 57 | arbiter.setArbiterOnNode(blitFramebuffer.data()); |
| 58 | |
| 59 | Qt3DRender::QRenderTarget *sourceRenderTarget = new Qt3DRender::QRenderTarget; |
| 60 | Qt3DRender::QRenderTarget *destinationRenderTarget = new Qt3DRender::QRenderTarget; |
| 61 | |
| 62 | // sourceRenderTarget |
| 63 | // WHEN |
| 64 | blitFramebuffer->setSource(sourceRenderTarget); |
| 65 | |
| 66 | // THEN |
| 67 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 68 | QCOMPARE(arbiter.dirtyNodes().front(), blitFramebuffer.data()); |
| 69 | |
| 70 | arbiter.clear(); |
| 71 | |
| 72 | // WHEN |
| 73 | blitFramebuffer->setSource(sourceRenderTarget); |
| 74 | |
| 75 | // THEN |
| 76 | QCOMPARE(arbiter.dirtyNodes().size(), 0); |
| 77 | |
| 78 | // WHEN |
| 79 | blitFramebuffer->setSource(nullptr); |
| 80 | |
| 81 | // THEN |
| 82 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 83 | QCOMPARE(arbiter.dirtyNodes().front(), blitFramebuffer.data()); |
| 84 | |
| 85 | arbiter.clear(); |
| 86 | |
| 87 | // destinationRenderTarget |
| 88 | // WHEN |
| 89 | blitFramebuffer->setDestination(destinationRenderTarget); |
| 90 | |
| 91 | // THEN |
| 92 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 93 | QCOMPARE(arbiter.dirtyNodes().front(), blitFramebuffer.data()); |
| 94 | |
| 95 | arbiter.clear(); |
| 96 | |
| 97 | // WHEN |
| 98 | blitFramebuffer->setDestination(destinationRenderTarget); |
| 99 | |
| 100 | // THEN |
| 101 | QCOMPARE(arbiter.dirtyNodes().size(), 0); |
| 102 | |
| 103 | // WHEN |
| 104 | blitFramebuffer->setDestination(nullptr); |
| 105 | |
| 106 | // THEN |
| 107 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 108 | QCOMPARE(arbiter.dirtyNodes().front(), blitFramebuffer.data()); |
| 109 | |
| 110 | arbiter.clear(); |
| 111 | |
| 112 | // sourceRect |
| 113 | // WHEN |
| 114 | blitFramebuffer->setSourceRect(QRect(0,0,1,1)); |
| 115 | |
| 116 | // THEN |
| 117 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 118 | QCOMPARE(arbiter.dirtyNodes().front(), blitFramebuffer.data()); |
| 119 | |
| 120 | arbiter.clear(); |
| 121 | |
| 122 | // WHEN |
| 123 | blitFramebuffer->setSourceRect(QRect(0,0,1,1)); |
| 124 | |
| 125 | // THEN |
| 126 | QCOMPARE(arbiter.dirtyNodes().size(), 0); |
| 127 | |
| 128 | // WHEN |
| 129 | blitFramebuffer->setSourceRect(QRect()); |
| 130 | |
| 131 | // THEN |
| 132 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 133 | QCOMPARE(arbiter.dirtyNodes().front(), blitFramebuffer.data()); |
| 134 | |
| 135 | arbiter.clear(); |
| 136 | |
| 137 | // destinationRect |
| 138 | blitFramebuffer->setDestinationRect(QRect(0,0,1,1)); |
| 139 | |
| 140 | // THEN |
| 141 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 142 | QCOMPARE(arbiter.dirtyNodes().front(), blitFramebuffer.data()); |
| 143 | |
| 144 | arbiter.clear(); |
| 145 | |
| 146 | // WHEN |
| 147 | blitFramebuffer->setDestinationRect(QRect(0,0,1,1)); |
| 148 | |
| 149 | // THEN |
| 150 | QCOMPARE(arbiter.dirtyNodes().size(), 0); |
| 151 | |
| 152 | // WHEN |
| 153 | blitFramebuffer->setDestinationRect(QRect()); |
| 154 | |
| 155 | // THEN |
| 156 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 157 | QCOMPARE(arbiter.dirtyNodes().front(), blitFramebuffer.data()); |
| 158 | |
| 159 | arbiter.clear(); |
| 160 | |
| 161 | // sourceAttachmentPoint |
| 162 | // WHEN |
| 163 | blitFramebuffer->setSourceAttachmentPoint(Qt3DRender::QRenderTargetOutput::Color1); |
| 164 | |
| 165 | // THEN |
| 166 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 167 | QCOMPARE(arbiter.dirtyNodes().front(), blitFramebuffer.data()); |
| 168 | |
| 169 | arbiter.clear(); |
| 170 | |
| 171 | // WHEN |
| 172 | blitFramebuffer->setSourceAttachmentPoint(Qt3DRender::QRenderTargetOutput::Color1); |
| 173 | |
| 174 | // THEN |
| 175 | QCOMPARE(arbiter.dirtyNodes().size(), 0); |
| 176 | |
| 177 | // WHEN |
| 178 | blitFramebuffer->setSourceAttachmentPoint(Qt3DRender::QRenderTargetOutput::Color0); |
| 179 | |
| 180 | // THEN |
| 181 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 182 | QCOMPARE(arbiter.dirtyNodes().front(), blitFramebuffer.data()); |
| 183 | |
| 184 | arbiter.clear(); |
| 185 | |
| 186 | // destinationAttachmentPoint |
| 187 | // WHEN |
| 188 | blitFramebuffer->setDestinationAttachmentPoint(Qt3DRender::QRenderTargetOutput::Color1); |
| 189 | |
| 190 | // THEN |
| 191 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 192 | QCOMPARE(arbiter.dirtyNodes().front(), blitFramebuffer.data()); |
| 193 | |
| 194 | arbiter.clear(); |
| 195 | |
| 196 | // WHEN |
| 197 | blitFramebuffer->setDestinationAttachmentPoint(Qt3DRender::QRenderTargetOutput::Color1); |
| 198 | |
| 199 | // THEN |
| 200 | QCOMPARE(arbiter.dirtyNodes().size(), 0); |
| 201 | |
| 202 | // WHEN |
| 203 | blitFramebuffer->setDestinationAttachmentPoint(Qt3DRender::QRenderTargetOutput::Color0); |
| 204 | |
| 205 | // THEN |
| 206 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 207 | QCOMPARE(arbiter.dirtyNodes().front(), blitFramebuffer.data()); |
| 208 | |
| 209 | arbiter.clear(); |
| 210 | } |
| 211 | |
| 212 | void checkSourceDestReset() |
| 213 | { |
| 214 | // GIVEN |
| 215 | TestArbiter arbiter; |
| 216 | QScopedPointer<Qt3DRender::QBlitFramebuffer> blitFramebuffer(new Qt3DRender::QBlitFramebuffer()); |
| 217 | arbiter.setArbiterOnNode(blitFramebuffer.data()); |
| 218 | |
| 219 | Qt3DRender::QRenderTarget *sourceRenderTarget = new Qt3DRender::QRenderTarget; // no parent |
| 220 | Qt3DRender::QRenderTarget *destinationRenderTarget = new Qt3DRender::QRenderTarget(sourceRenderTarget); // have a parent |
| 221 | |
| 222 | // WHEN |
| 223 | blitFramebuffer->setSource(sourceRenderTarget); |
| 224 | blitFramebuffer->setDestination(destinationRenderTarget); |
| 225 | QCoreApplication::processEvents(); |
| 226 | |
| 227 | // THEN |
| 228 | QCOMPARE(sourceRenderTarget->parent(), blitFramebuffer.data()); |
| 229 | QCOMPARE(destinationRenderTarget->parent(), sourceRenderTarget); // already set parent must not change |
| 230 | |
| 231 | // WHEN |
| 232 | delete sourceRenderTarget; |
| 233 | |
| 234 | // THEN |
| 235 | QVERIFY(!blitFramebuffer->source()); |
| 236 | QVERIFY(!blitFramebuffer->destination()); // gone too since destinationRenderTarget was parented to sourceRenderTarget |
| 237 | } |
| 238 | }; |
| 239 | |
| 240 | QTEST_MAIN(tst_QBlitFrameBuffer) |
| 241 | |
| 242 | #include "tst_qblitframebuffer.moc" |