xref: /aosp_15_r20/external/skia/tests/GrQuadBufferTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2019 Google LLC
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "include/core/SkMatrix.h"
9 #include "include/core/SkRect.h"
10 #include "include/core/SkTypes.h"
11 #include "src/gpu/ganesh/geometry/GrQuad.h"
12 #include "src/gpu/ganesh/geometry/GrQuadBuffer.h"
13 #include "tests/Test.h"
14 
15 #include <utility>
16 #include <vector>
17 
18 #define ASSERT(cond) REPORTER_ASSERT(r, cond)
19 #define ASSERTF(cond, ...) REPORTER_ASSERT(r, cond, __VA_ARGS__)
20 #define TEST(name) DEF_TEST(GrQuadBuffer##name, r)
21 
22 struct TestData {
23     int fItem1;
24     float fItem2;
25 };
26 
assert_quad_eq(skiatest::Reporter * r,const GrQuad & expected,const GrQuad & actual)27 static void assert_quad_eq(skiatest::Reporter* r, const GrQuad& expected, const GrQuad& actual) {
28     ASSERTF(expected.quadType() == actual.quadType(), "Expected type %d, got %d",
29             (int) expected.quadType(), (int) actual.quadType());
30     for (int i = 0; i < 4; ++i) {
31         ASSERTF(expected.x(i) == actual.x(i), "Expected x(%d) = %f, got %f",
32                 i, expected.x(i), actual.x(i));
33         ASSERTF(expected.y(i) == actual.y(i), "Expected y(%d) = %f, got %f",
34                 i, expected.y(i), actual.y(i));
35         ASSERTF(expected.w(i) == actual.w(i), "Expected w(%d) = %f, got %f",
36                 i, expected.w(i), actual.w(i));
37     }
38 }
39 
assert_metadata_eq(skiatest::Reporter * r,const TestData & expected,const TestData & actual)40 static void assert_metadata_eq(skiatest::Reporter* r, const TestData& expected,
41                                const TestData& actual) {
42     ASSERTF(expected.fItem1 == actual.fItem1 && expected.fItem2 == actual.fItem2,
43             "Expected { %d, %f } for metadata, got: { %d %f }",
44             expected.fItem1, expected.fItem2, actual.fItem1, actual.fItem2);
45 }
46 
generate_quads(float seed,int cnt,const GrQuad::Type types[])47 static std::vector<GrQuad> generate_quads(float seed, int cnt, const GrQuad::Type types[]) {
48     // For convenience use matrix to derive each quad type, rely on different seed values to
49     // differentiate between quads of the same type
50     SkMatrix rotate;
51     rotate.setRotate(45.f);
52     SkMatrix skew;
53     skew.setSkew(0.5f, 0.5f);
54     SkMatrix perspective;
55     perspective.setPerspX(0.01f);
56     perspective.setPerspY(0.001f);
57 
58     std::vector<GrQuad> quads;
59     SkRect rect = SkRect::MakeXYWH(seed, 2.f * seed, 2.f * seed, seed);
60     for (int i = 0; i < cnt; ++i) {
61         GrQuad quad;
62         switch(types[i]) {
63             case GrQuad::Type::kAxisAligned:
64                 quad = GrQuad(rect);
65                 break;
66             case GrQuad::Type::kRectilinear:
67                 quad = GrQuad::MakeFromRect(rect, rotate);
68                 break;
69             case GrQuad::Type::kGeneral:
70                 quad = GrQuad::MakeFromRect(rect, skew);
71                 break;
72             default:
73                 SkASSERT(types[i] == GrQuad::Type::kPerspective);
74                 quad = GrQuad::MakeFromRect(rect, perspective);
75                 break;
76         }
77 
78         SkASSERT(quad.quadType() == types[i]);
79         quads.push_back(quad);
80     }
81     return quads;
82 }
83 
TEST(Append)84 TEST(Append) {
85     // Generate test data, which includes all quad types out of enum-order and duplicates
86     static const int kQuadCount = 6;
87     static const GrQuad::Type kDeviceTypes[] = {
88         GrQuad::Type::kAxisAligned, GrQuad::Type::kRectilinear, GrQuad::Type::kGeneral,
89         GrQuad::Type::kPerspective, GrQuad::Type::kRectilinear, GrQuad::Type::kAxisAligned
90     };
91     // Odd indexed quads will be ignored and not stored in the buffer
92     static const GrQuad::Type kLocalTypes[] = {
93         GrQuad::Type::kGeneral, GrQuad::Type::kGeneral, GrQuad::Type::kRectilinear,
94         GrQuad::Type::kRectilinear, GrQuad::Type::kAxisAligned, GrQuad::Type::kAxisAligned
95     };
96     static_assert(std::size(kDeviceTypes) == kQuadCount, "device quad count");
97     static_assert(std::size(kLocalTypes) == kQuadCount, "local quad count");
98 
99     std::vector<GrQuad> expectedDeviceQuads = generate_quads(1.f, kQuadCount, kDeviceTypes);
100     std::vector<GrQuad> expectedLocalQuads = generate_quads(2.f, kQuadCount, kLocalTypes);
101 
102     // Fill in the buffer with the device quads, and a local quad if the index is even
103     GrQuadBuffer<TestData> buffer;
104     for (int i = 0; i < kQuadCount; ++i) {
105         buffer.append(expectedDeviceQuads[i],                          // device quad
106                       { 2 * i, 3.f * i },                              // metadata
107                       i % 2 == 0 ? &expectedLocalQuads[i] : nullptr);  // optional local quad
108     }
109 
110     // Confirm the state of the buffer
111     ASSERT(kQuadCount == buffer.count());
112     ASSERT(GrQuad::Type::kPerspective == buffer.deviceQuadType());
113     ASSERT(GrQuad::Type::kGeneral == buffer.localQuadType());
114 
115     int i = 0;
116     auto iter = buffer.iterator();
117     while(iter.next()) {
118         // Each entry always has the device quad
119         assert_quad_eq(r, expectedDeviceQuads[i], *iter.deviceQuad());
120         assert_metadata_eq(r, {2 * i, 3.f * i}, iter.metadata());
121 
122         if (i % 2 == 0) {
123             // Confirm local quads included on even entries
124             ASSERT(iter.isLocalValid());
125             assert_quad_eq(r, expectedLocalQuads[i], *iter.localQuad());
126         } else {
127             // Should not have locals
128             ASSERT(!iter.isLocalValid());
129             ASSERT(!iter.localQuad());
130         }
131 
132         i++;
133     }
134     ASSERTF(i == kQuadCount, "Expected %d iterations, got: %d", kQuadCount, i);
135 }
136 
TEST(Concat)137 TEST(Concat) {
138     static const int kQuadCount = 2;
139     static const GrQuad::Type kTypesA[] = { GrQuad::Type::kAxisAligned, GrQuad::Type::kRectilinear };
140     static const GrQuad::Type kTypesB[] = { GrQuad::Type::kGeneral, GrQuad::Type::kPerspective };
141     static_assert(std::size(kTypesA) == kQuadCount, "quadsA count");
142     static_assert(std::size(kTypesB) == kQuadCount, "quadsB count");
143 
144     std::vector<GrQuad> quadsA = generate_quads(1.f, kQuadCount, kTypesA);
145     std::vector<GrQuad> quadsB = generate_quads(2.f, kQuadCount, kTypesB);
146     // Make two buffers, the first uses 'quadsA' for device quads and 'quadsB' for local quads
147     // on even indices. The second uses 'quadsB' for device quads and 'quadsA' for local quads
148     // on odd indices.
149     GrQuadBuffer<TestData> buffer1;
150     GrQuadBuffer<TestData> buffer2;
151     for (int i = 0; i < kQuadCount; ++i) {
152         buffer1.append(quadsA[i], {i, 2.f * i}, i % 2 == 0 ? &quadsB[i] : nullptr);
153         buffer2.append(quadsB[i], {2 * i, 0.5f * i}, i % 2 == 0 ? nullptr : &quadsA[i]);
154     }
155 
156     ASSERT(kQuadCount == buffer1.count());
157     ASSERT(kQuadCount == buffer2.count());
158 
159     // Perform the concatenation and then confirm the new state of buffer1
160     buffer1.concat(buffer2);
161 
162     ASSERT(2 * kQuadCount == buffer1.count());
163     int i = 0;
164     auto iter = buffer1.iterator();
165     while(iter.next()) {
166         if (i < kQuadCount) {
167             // First half should match original buffer1
168             assert_quad_eq(r, quadsA[i], *iter.deviceQuad());
169             assert_metadata_eq(r, {i, 2.f * i}, iter.metadata());
170             if (i % 2 == 0) {
171                 ASSERT(iter.isLocalValid());
172                 assert_quad_eq(r, quadsB[i], *iter.localQuad());
173             } else {
174                 ASSERT(!iter.isLocalValid());
175                 ASSERT(!iter.localQuad());
176             }
177 
178         } else {
179             // Second half should match buffer2
180             int j = i - kQuadCount;
181             assert_quad_eq(r, quadsB[j], *iter.deviceQuad());
182             assert_metadata_eq(r, {2 * j, 0.5f * j}, iter.metadata());
183             if (j % 2 == 0) {
184                 ASSERT(!iter.isLocalValid());
185                 ASSERT(!iter.localQuad());
186             } else {
187                 ASSERT(iter.isLocalValid());
188                 assert_quad_eq(r, quadsA[j], *iter.localQuad());
189             }
190         }
191 
192         i++;
193     }
194     ASSERTF(i == 2 * kQuadCount, "Expected %d iterations, got: %d",2 * kQuadCount, i);
195 }
196 
TEST(Metadata)197 TEST(Metadata) {
198     static const int kQuadCount = 3;
199 
200     // This test doesn't really care about the quad coordinates (except that they aren't modified
201     // when mutating the metadata)
202     GrQuad quad(SkRect::MakeLTRB(1.f, 2.f, 3.f, 4.f));
203 
204     GrQuadBuffer<TestData> buffer;
205     for (int i = 0; i < kQuadCount; ++i) {
206         buffer.append(quad, {i, 2.f * i}, i % 2 == 0 ? &quad : nullptr);
207     }
208 
209     // Iterate once using the metadata iterator, confirm the test data and rewrite
210     int i = 0;
211     auto meta = buffer.metadata();
212     while(meta.next()) {
213         // Confirm initial state
214         assert_metadata_eq(r, {i, 2.f * i}, *meta);
215         // Rewrite
216         *meta = {2 * i, 0.5f * i};
217         i++;
218     }
219     ASSERTF(i == kQuadCount, "Expected %d iterations, got: %d", kQuadCount, i);
220 
221     // Now that all metadata has been touched, read with regular iterator and confirm updated state
222     // and that no quad coordinates have been changed.
223     i = 0;
224     auto iter = buffer.iterator();
225     while(iter.next()) {
226         // New metadata
227         assert_metadata_eq(r, {2 * i, 0.5f * i}, iter.metadata());
228 
229         // Quad coordinates are unchanged
230         assert_quad_eq(r, quad, *iter.deviceQuad());
231         if (i % 2 == 0) {
232             ASSERT(iter.isLocalValid());
233             assert_quad_eq(r, quad, *iter.localQuad());
234         } else {
235             ASSERT(!iter.isLocalValid());
236             ASSERT(!iter.localQuad());
237         }
238         i++;
239     }
240     ASSERTF(i == kQuadCount, "Expected %d iterations, got: %d", kQuadCount, i);
241 }
242