| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the Qt scene graph research project. | 
| 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 <QtCore/QString> | 
| 30 | #include <QtTest/QtTest> | 
| 31 |  | 
| 32 | #include <QtQuick/qsggeometry.h> | 
| 33 |  | 
| 34 | class GeometryTest : public QObject | 
| 35 | { | 
| 36 |     Q_OBJECT | 
| 37 |  | 
| 38 | public: | 
| 39 |  | 
| 40 | private Q_SLOTS: | 
| 41 |     void testPoint2D(); | 
| 42 |     void testTexturedPoint2D(); | 
| 43 |     void testCustomGeometry(); | 
| 44 |  | 
| 45 | private: | 
| 46 | }; | 
| 47 |  | 
| 48 | void GeometryTest::testPoint2D() | 
| 49 | { | 
| 50 |     QSGGeometry geometry(QSGGeometry::defaultAttributes_Point2D(), 4, 0); | 
| 51 |  | 
| 52 |     QCOMPARE(geometry.attributeCount(), 1); | 
| 53 |     QCOMPARE(geometry.sizeOfVertex(), (int) sizeof(float) * 2); | 
| 54 |     QCOMPARE(geometry.vertexCount(), 4); | 
| 55 |     QCOMPARE(geometry.indexCount(), 0); | 
| 56 |     QVERIFY(!geometry.indexData()); | 
| 57 |  | 
| 58 |     QSGGeometry::updateRectGeometry(g: &geometry, rect: QRectF(1, 2, 3, 4)); | 
| 59 |  | 
| 60 |     QSGGeometry::Point2D *pts = geometry.vertexDataAsPoint2D(); | 
| 61 |     QVERIFY(pts != nullptr); | 
| 62 |  | 
| 63 |     QCOMPARE(pts[0].x, (float) 1); | 
| 64 |     QCOMPARE(pts[0].y, (float) 2); | 
| 65 |     QCOMPARE(pts[3].x, (float) 4); | 
| 66 |     QCOMPARE(pts[3].y, (float) 6); | 
| 67 |  | 
| 68 |     // Verify that resize gives me enough allocated data without crashing... | 
| 69 |     geometry.allocate(vertexCount: 100, indexCount: 100); | 
| 70 |     pts = geometry.vertexDataAsPoint2D(); | 
| 71 |     quint16 *is = geometry.indexDataAsUShort(); | 
| 72 |     for (int i=0; i<100; ++i) { | 
| 73 |         pts[i].x = i; | 
| 74 |         pts[i].y = i + 100; | 
| 75 |         is[i] = i; | 
| 76 |     } | 
| 77 |     QVERIFY(true); | 
| 78 | } | 
| 79 |  | 
| 80 |  | 
| 81 | void GeometryTest::testTexturedPoint2D() | 
| 82 | { | 
| 83 |     QSGGeometry geometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4, 0); | 
| 84 |  | 
| 85 |     QCOMPARE(geometry.attributeCount(), 2); | 
| 86 |     QCOMPARE(geometry.sizeOfVertex(), (int) sizeof(float) * 4); | 
| 87 |     QCOMPARE(geometry.vertexCount(), 4); | 
| 88 |     QCOMPARE(geometry.indexCount(), 0); | 
| 89 |     QVERIFY(!geometry.indexData()); | 
| 90 |  | 
| 91 |     QSGGeometry::updateTexturedRectGeometry(g: &geometry, rect: QRectF(1, 2, 3, 4), sourceRect: QRectF(5, 6, 7, 8)); | 
| 92 |  | 
| 93 |     QSGGeometry::TexturedPoint2D *pts = geometry.vertexDataAsTexturedPoint2D(); | 
| 94 |     QVERIFY(pts != nullptr); | 
| 95 |  | 
| 96 |     QCOMPARE(pts[0].x, (float) 1); | 
| 97 |     QCOMPARE(pts[0].y, (float) 2); | 
| 98 |     QCOMPARE(pts[0].tx, (float) 5); | 
| 99 |     QCOMPARE(pts[0].ty, (float) 6); | 
| 100 |  | 
| 101 |     QCOMPARE(pts[3].x, (float) 4); | 
| 102 |     QCOMPARE(pts[3].y, (float) 6); | 
| 103 |     QCOMPARE(pts[3].tx, (float) 12); | 
| 104 |     QCOMPARE(pts[3].ty, (float) 14); | 
| 105 |  | 
| 106 |     // Verify that resize gives me enough allocated data without crashing... | 
| 107 |     geometry.allocate(vertexCount: 100, indexCount: 100); | 
| 108 |     pts = geometry.vertexDataAsTexturedPoint2D(); | 
| 109 |     quint16 *is = geometry.indexDataAsUShort(); | 
| 110 |     for (int i=0; i<100; ++i) { | 
| 111 |         pts[i].x = i; | 
| 112 |         pts[i].y = i + 100; | 
| 113 |         pts[i].tx = i + 200; | 
| 114 |         pts[i].ty = i + 300; | 
| 115 |         is[i] = i; | 
| 116 |     } | 
| 117 |     QVERIFY(true); | 
| 118 | } | 
| 119 |  | 
| 120 | void GeometryTest::testCustomGeometry() | 
| 121 | { | 
| 122 |     struct V { | 
| 123 |         float x, y; | 
| 124 |         unsigned char r, g, b, a; | 
| 125 |         float v1, v2, v3, v4; | 
| 126 |     }; | 
| 127 |  | 
| 128 |     static QSGGeometry::Attribute attributes[] = { | 
| 129 |         QSGGeometry::Attribute::create(pos: 0, tupleSize: 2, primitiveType: QSGGeometry::FloatType, isPosition: false), | 
| 130 |         QSGGeometry::Attribute::create(pos: 1, tupleSize: 4, primitiveType: QSGGeometry::UnsignedByteType, isPosition: false), | 
| 131 |         QSGGeometry::Attribute::create(pos: 2, tupleSize: 4, primitiveType: QSGGeometry::FloatType, isPosition: false) | 
| 132 |     }; | 
| 133 |     static QSGGeometry::AttributeSet set = { .count: 4, .stride: 6 * sizeof(float) + 4 * sizeof(unsigned char), .attributes: attributes }; | 
| 134 |  | 
| 135 |     QSGGeometry geometry(set, 1000, 4000); | 
| 136 |  | 
| 137 |     // Verify that space has been allocated. | 
| 138 |     quint16 *ii = geometry.indexDataAsUShort(); | 
| 139 |     for (int i=0; i<geometry.indexCount(); ++i) { | 
| 140 |         ii[i] = i; | 
| 141 |     } | 
| 142 |  | 
| 143 |     V *v = (V *) geometry.vertexData(); | 
| 144 |     for (int i=0; i<geometry.vertexCount(); ++i) { | 
| 145 |         v[i].x = 0; | 
| 146 |         v[i].y = 1; | 
| 147 |         v[i].r = 2; | 
| 148 |         v[i].g = 3; | 
| 149 |         v[i].b = 4; | 
| 150 |         v[i].a = 5; | 
| 151 |         v[i].v1 = 6; | 
| 152 |         v[i].v2 = 7; | 
| 153 |         v[i].v3 = 8; | 
| 154 |         v[i].v4 = 9; | 
| 155 |     } | 
| 156 |  | 
| 157 |     // Verify the data's integrity | 
| 158 |     for (int i=0; i<4000; ++i) | 
| 159 |         QCOMPARE(ii[i], (quint16) i); | 
| 160 |     for (int i=0; i<1000; ++i) | 
| 161 |         QCOMPARE(v[i].v1, float(6)); | 
| 162 |  | 
| 163 | } | 
| 164 |  | 
| 165 |  | 
| 166 | QTEST_MAIN(GeometryTest); | 
| 167 |  | 
| 168 | #include "tst_geometry.moc" | 
| 169 |  |