aboutsummaryrefslogtreecommitdiff
path: root/tests/GrQuadListTest.cpp
blob: fd88dde2d70fab6c6ef3d5c60f3ddca360de49fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 * Copyright 2019 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "Test.h"

#include "GrQuad.h"

#define ASSERT(cond) REPORTER_ASSERT(r, cond)
#define ASSERTF(cond, ...) REPORTER_ASSERT(r, cond, __VA_ARGS__)
#define TEST(name) DEF_TEST(GrQuadList##name, r)

struct TestData {
    int fItem1;
    float fItem2;
};

// Simple factories to make placeholder quads used in the tests. The 2D quads
// will have the kRect quad type.
static GrQuad make_2d_quad() {
    return GrQuad(SkRect::MakeLTRB(1.f, 2.f, 3.f, 4.f));
}
static bool is_2d_quad(const GrPerspQuad& quad) {
    return quad.x(0) == 1.f && quad.x(1) == 1.f && quad.x(2) == 3.f && quad.x(3) == 3.f &&
           quad.y(0) == 2.f && quad.y(1) == 4.f && quad.y(2) == 2.f && quad.y(3) == 4.f &&
           quad.w(0) == 1.f && quad.w(1) == 1.f && quad.w(2) == 1.f && quad.w(3) == 1.f;
}

static GrPerspQuad make_2d_persp_quad() {
    return GrPerspQuad(SkRect::MakeLTRB(5.f, 6.f, 7.f, 8.f), SkMatrix::I());
}
static bool is_2d_persp_quad(const GrPerspQuad& quad) {
    return quad.x(0) == 5.f && quad.x(1) == 5.f && quad.x(2) == 7.f && quad.x(3) == 7.f &&
           quad.y(0) == 6.f && quad.y(1) == 8.f && quad.y(2) == 6.f && quad.y(3) == 8.f &&
           quad.w(0) == 1.f && quad.w(1) == 1.f && quad.w(2) == 1.f && quad.w(3) == 1.f;
}

static GrPerspQuad make_3d_persp_quad() {
    // This perspective matrix leaves x and y unmodified, and sets w to the persp2 value
    SkMatrix p = SkMatrix::I();
    p[SkMatrix::kMPersp2] = 13.f;
    SkASSERT(p.hasPerspective()); // Sanity check
    return GrPerspQuad(SkRect::MakeLTRB(9.f, 10.f, 11.f, 12.f), p);
}
static bool is_3d_persp_quad(const GrPerspQuad& quad) {
    return quad.x(0) == 9.f && quad.x(1) == 9.f && quad.x(2) == 11.f && quad.x(3) == 11.f &&
           quad.y(0) == 10.f && quad.y(1) == 12.f && quad.y(2) == 10.f && quad.y(3) == 12.f &&
           quad.w(0) == 13.f && quad.w(1) == 13.f && quad.w(2) == 13.f && quad.w(3) == 13.f;
}

TEST(Add2D) {
    GrQuadList list2D;
    // Add a plain quad, a 2D persp quad, and then a 3D persp quad, then read back and make sure
    // the coordinates make sense (including that the type was lifted to perspective).
    list2D.push_back(make_2d_quad(), GrQuadType::kRect);
    list2D.push_back(make_2d_persp_quad(), GrQuadType::kRect);

    // Check 2D state of the list
    ASSERTF(list2D.count() == 2, "Unexpected count: %d", list2D.count());
    ASSERTF(list2D.quadType() == GrQuadType::kRect, "Unexpected quad type: %d",
            (uint32_t) list2D.quadType());
    ASSERTF(is_2d_quad(list2D[0]), "Incorrect quad at i=0");
    ASSERTF(is_2d_persp_quad(list2D[1]), "Incorrect quad at i=1");

    // Force the 2D quads to be updated to store ws by adding a perspective quad
    list2D.push_back(make_3d_persp_quad(), GrQuadType::kPerspective);
    ASSERTF(list2D.quadType() == GrQuadType::kPerspective,
            "Expected 2D list to be upgraded to perspective");

    // Re-check full state of list after type upgrade
    ASSERTF(list2D.count() == 3, "Unexpected count: %d", list2D.count());
    ASSERTF(is_2d_quad(list2D[0]), "Incorrect quad at i=0 after upgrade");
    ASSERTF(is_2d_persp_quad(list2D[1]), "Incorrect quad at i=1 after upgrade");
    ASSERTF(is_3d_persp_quad(list2D[2]), "Incorrect quad at i=2");
}

TEST(Add3D) {
    // Now make a list that starts with a 3D persp quad, then has conventional quads added to it
    // and make sure its state is correct
    GrQuadList list3D;
    list3D.push_back(make_3d_persp_quad(), GrQuadType::kPerspective);
    list3D.push_back(make_2d_persp_quad(), GrQuadType::kRect);
    list3D.push_back(make_2d_quad(), GrQuadType::kRect);

    ASSERTF(list3D.count() == 3, "Unexpected count: %d", list3D.count());
    ASSERTF(is_3d_persp_quad(list3D[0]), "Incorrect quad at i=0");
    ASSERTF(is_2d_persp_quad(list3D[1]), "Incorrect quad at i=1");
    ASSERTF(is_2d_quad(list3D[2]), "Incorrect quad at i=2");
}

TEST(AddWithMetadata2D) {
    // As above, but also make sure that the metadata is saved and read properly
    GrTQuadList<TestData> list2D;
    // Add a plain quad, a 2D persp quad, and then a 3D persp quad, then read back and make sure
    // the coordinates make sense (including that the type was lifted to perspective).
    list2D.push_back(make_2d_quad(), GrQuadType::kRect, {1, 1.f});
    list2D.push_back(make_2d_persp_quad(), GrQuadType::kRect, {2, 2.f});

    // Check 2D state of the list
    ASSERTF(list2D.count() == 2, "Unexpected count: %d", list2D.count());
    ASSERTF(list2D.quadType() == GrQuadType::kRect, "Unexpected quad type: %d",
            (uint32_t) list2D.quadType());
    ASSERTF(is_2d_quad(list2D[0]), "Incorrect quad at i=0");
    ASSERTF(list2D.metadata(0).fItem1 == 1 && list2D.metadata(0).fItem2 == 1.f,
            "Incorrect metadata at i=0");
    ASSERTF(is_2d_persp_quad(list2D[1]), "Incorrect quad at i=1");
    ASSERTF(list2D.metadata(1).fItem1 == 2 && list2D.metadata(1).fItem2 == 2.f,
            "Incorrect metadata at i=1");

    // Force the 2D quads to be updated to store ws by adding a perspective quad
    list2D.push_back(make_3d_persp_quad(), GrQuadType::kPerspective, {3, 3.f});
    ASSERTF(list2D.quadType() == GrQuadType::kPerspective,
            "Expected 2D list to be upgraded to perspective");

    // Re-check full state of list after type upgrade
    ASSERTF(list2D.count() == 3, "Unexpected count: %d", list2D.count());
    ASSERTF(is_2d_quad(list2D[0]), "Incorrect quad at i=0 after upgrade");
    ASSERTF(list2D.metadata(0).fItem1 == 1 && list2D.metadata(0).fItem2 == 1.f,
            "Incorrect metadata at i=0");
    ASSERTF(is_2d_persp_quad(list2D[1]), "Incorrect quad at i=1 after upgrade");
    ASSERTF(list2D.metadata(1).fItem1 == 2 && list2D.metadata(1).fItem2 == 2.f,
            "Incorrect metadata at i=1");
    ASSERTF(is_3d_persp_quad(list2D[2]), "Incorrect quad at i=2");
    ASSERTF(list2D.metadata(2).fItem1 == 3 && list2D.metadata(2).fItem2 == 3.f,
            "Incorrect metadata at i=2");
}

TEST(AddWithMetadata3D) {
    // Now make a list that starts with a 3D persp quad, then has conventional quads added to it
    // and make sure its state is correct
    GrTQuadList<TestData> list3D;
    list3D.push_back(make_3d_persp_quad(), GrQuadType::kPerspective, {3, 3.f});
    list3D.push_back(make_2d_persp_quad(), GrQuadType::kRect, {2, 2.f});
    list3D.push_back(make_2d_quad(), GrQuadType::kRect, {1, 1.f});

    ASSERTF(list3D.count() == 3, "Unexpected count: %d", list3D.count());
    ASSERTF(is_3d_persp_quad(list3D[0]), "Incorrect quad at i=0");
    ASSERTF(list3D.metadata(0).fItem1 == 3 && list3D.metadata(0).fItem2 == 3.f,
            "Incorrect metadata at i=0");
    ASSERTF(is_2d_persp_quad(list3D[1]), "Incorrect quad at i=1");
    ASSERTF(list3D.metadata(1).fItem1 == 2 && list3D.metadata(1).fItem2 == 2.f,
            "Incorrect metadata at i=1");
    ASSERTF(is_2d_quad(list3D[2]), "Incorrect quad at i=2");
    ASSERTF(list3D.metadata(2).fItem1 == 1 && list3D.metadata(2).fItem2 == 1.f,
            "Incorrect metadata at i=2");
}

TEST(Concat2DWith2D) {
    GrQuadList a2D;
    a2D.push_back(make_2d_quad(), GrQuadType::kRect);
    GrQuadList b2D;
    b2D.push_back(make_2d_persp_quad(), GrQuadType::kRect);

    a2D.concat(b2D);

    ASSERTF(a2D.count() == 2, "Unexpected count: %d", a2D.count());
    ASSERTF(is_2d_quad(a2D[0]), "Incorrect quad at i=0");
    ASSERTF(is_2d_persp_quad(a2D[1]), "Incorrect quad at i=1");
}

TEST(Concat2DWith3D) {
    GrQuadList a2D;
    a2D.push_back(make_2d_quad(), GrQuadType::kRect);
    GrQuadList b3D;
    b3D.push_back(make_3d_persp_quad(), GrQuadType::kPerspective);

    a2D.concat(b3D);

    ASSERTF(a2D.count() == 2, "Unexpected count: %d", a2D.count());
    ASSERTF(is_2d_quad(a2D[0]), "Incorrect quad at i=0");
    ASSERTF(is_3d_persp_quad(a2D[1]), "Incorrect quad at i=1");
}

TEST(Concat3DWith2D) {
    GrQuadList a3D;
    a3D.push_back(make_3d_persp_quad(), GrQuadType::kPerspective);
    GrQuadList b2D;
    b2D.push_back(make_2d_quad(), GrQuadType::kRect);

    a3D.concat(b2D);

    ASSERTF(a3D.count() == 2, "Unexpected count: %d", a3D.count());
    ASSERTF(is_3d_persp_quad(a3D[0]), "Incorrect quad at i=0");
    ASSERTF(is_2d_quad(a3D[1]), "Incorrect quad at i=1");
}

TEST(Concat3DWith3D) {
    GrQuadList a3D;
    a3D.push_back(make_3d_persp_quad(), GrQuadType::kPerspective);
    GrQuadList b3D;
    b3D.push_back(make_3d_persp_quad(), GrQuadType::kPerspective);

    a3D.concat(b3D);

    ASSERTF(a3D.count() == 2, "Unexpected count: %d", a3D.count());
    ASSERTF(is_3d_persp_quad(a3D[0]), "Incorrect quad at i=0");
    ASSERTF(is_3d_persp_quad(a3D[1]), "Incorrect quad at i=1");
}

TEST(Concat2DWith2DMetadata) {
    GrTQuadList<TestData> a2D;
    a2D.push_back(make_2d_quad(), GrQuadType::kRect, {1, 1.f});
    GrTQuadList<TestData> b2D;
    b2D.push_back(make_2d_persp_quad(), GrQuadType::kRect, {2, 2.f});

    a2D.concat(b2D);

    ASSERTF(a2D.count() == 2, "Unexpected count: %d", a2D.count());
    ASSERTF(is_2d_quad(a2D[0]), "Incorrect quad at i=0");
    ASSERTF(a2D.metadata(0).fItem1 == 1 && a2D.metadata(0).fItem2 == 1.f,
            "Incorrect metadata at i=0");
    ASSERTF(is_2d_persp_quad(a2D[1]), "Incorrect quad at i=1");
    ASSERTF(a2D.metadata(1).fItem1 == 2 && a2D.metadata(1).fItem2 == 2.f,
            "Incorrect metadata at i=1");
}

TEST(Concat2DWith3DMetadata) {
    GrTQuadList<TestData> a2D;
    a2D.push_back(make_2d_quad(), GrQuadType::kRect, {1, 1.f});
    GrTQuadList<TestData> b3D;
    b3D.push_back(make_3d_persp_quad(), GrQuadType::kPerspective, {2, 2.f});

    a2D.concat(b3D);

    ASSERTF(a2D.count() == 2, "Unexpected count: %d", a2D.count());
    ASSERTF(is_2d_quad(a2D[0]), "Incorrect quad at i=0");
    ASSERTF(a2D.metadata(0).fItem1 == 1 && a2D.metadata(0).fItem2 == 1.f,
            "Incorrect metadata at i=0");
    ASSERTF(is_3d_persp_quad(a2D[1]), "Incorrect quad at i=1");
    ASSERTF(a2D.metadata(1).fItem1 == 2 && a2D.metadata(1).fItem2 == 2.f,
            "Incorrect metadata at i=1");
}

TEST(Concat3DWith2DMetadata) {
    GrTQuadList<TestData> a3D;
    a3D.push_back(make_3d_persp_quad(), GrQuadType::kPerspective, {1, 1.f});
    GrTQuadList<TestData> b2D;
    b2D.push_back(make_2d_quad(), GrQuadType::kRect, {2, 2.f});

    a3D.concat(b2D);

    ASSERTF(a3D.count() == 2, "Unexpected count: %d", a3D.count());
    ASSERTF(is_3d_persp_quad(a3D[0]), "Incorrect quad at i=0");
    ASSERTF(a3D.metadata(0).fItem1 == 1 && a3D.metadata(0).fItem2 == 1.f,
            "Incorrect metadata at i=0");
    ASSERTF(is_2d_quad(a3D[1]), "Incorrect quad at i=1");
    ASSERTF(a3D.metadata(1).fItem1 == 2 && a3D.metadata(1).fItem2 == 2.f,
            "Incorrect metadata at i=1");
}

TEST(Concat3DWith3DMetadata) {
    GrTQuadList<TestData> a3D;
    a3D.push_back(make_3d_persp_quad(), GrQuadType::kPerspective, {1, 1.f});
    GrTQuadList<TestData> b3D;
    b3D.push_back(make_3d_persp_quad(), GrQuadType::kPerspective, {2, 2.f});

    a3D.concat(b3D);

    ASSERTF(a3D.count() == 2, "Unexpected count: %d", a3D.count());
    ASSERTF(is_3d_persp_quad(a3D[0]), "Incorrect quad at i=0");
    ASSERTF(a3D.metadata(0).fItem1 == 1 && a3D.metadata(0).fItem2 == 1.f,
            "Incorrect metadata at i=0");
    ASSERTF(is_3d_persp_quad(a3D[1]), "Incorrect quad at i=1");
    ASSERTF(a3D.metadata(1).fItem1 == 2 && a3D.metadata(1).fItem2 == 2.f,
            "Incorrect metadata at i=1");
}

TEST(WriteMetadata) {
    GrTQuadList<TestData> list;
    list.push_back(make_2d_quad(), GrQuadType::kRect, {1, 1.f});
    ASSERTF(list.metadata(0).fItem1 == 1 && list.metadata(0).fItem2 == 1.f,
            "Incorrect metadata at i=0"); // Sanity check

    // Rewrite metadata within the list and read back
    list.metadata(0).fItem1 = 2;
    list.metadata(0).fItem2 = 2.f;
    ASSERTF(list.metadata(0).fItem1 == 2 && list.metadata(0).fItem2 == 2.f,
            "Incorrect metadata at i=0 after edit");
}