blob: 5f2cbbc3b535d7e52d4ea5d88a2748c10f5698c4 [file] [log] [blame]
Mike Reedbdce9c22017-03-14 14:10:54 -04001/*
2 * Copyright 2017 Google Inc.
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkSurface.h"
10#include "include/core/SkVertices.h"
Brian Osman6b3d6e92020-06-01 16:33:28 -040011#include "src/core/SkAutoMalloc.h"
12#include "src/core/SkReadBuffer.h"
Mike Reedba962562020-03-12 20:33:21 -040013#include "src/core/SkVerticesPriv.h"
Brian Osman6b3d6e92020-06-01 16:33:28 -040014#include "src/core/SkWriteBuffer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "tests/Test.h"
16#include "tools/ToolUtils.h"
Mike Reedbdce9c22017-03-14 14:10:54 -040017
Mike Reedba962562020-03-12 20:33:21 -040018static bool equal(const SkVertices* vert0, const SkVertices* vert1) {
Brian Osman8cbedf92020-03-31 10:38:31 -040019 SkVerticesPriv v0(vert0->priv()), v1(vert1->priv());
Mike Reedba962562020-03-12 20:33:21 -040020
Brian Osman8cbedf92020-03-31 10:38:31 -040021 if (v0.mode() != v1.mode()) {
Mike Reedbdce9c22017-03-14 14:10:54 -040022 return false;
23 }
Brian Osman8cbedf92020-03-31 10:38:31 -040024 if (v0.vertexCount() != v1.vertexCount()) {
Mike Reedbdce9c22017-03-14 14:10:54 -040025 return false;
26 }
Brian Osman8cbedf92020-03-31 10:38:31 -040027 if (v0.indexCount() != v1.indexCount()) {
Mike Reedbdce9c22017-03-14 14:10:54 -040028 return false;
29 }
Brian Osmanffd11f4a2020-03-30 09:57:53 -040030 if (v0.attributeCount() != v1.attributeCount()) {
Mike Reed50c64ab2020-03-05 13:01:00 -050031 return false;
32 }
Brian Osmanffd11f4a2020-03-30 09:57:53 -040033 for (int i = 0; i < v0.attributeCount(); ++i) {
34 if (v0.attributes()[i] != v1.attributes()[i]) {
35 return false;
36 }
37 }
Mike Reedbdce9c22017-03-14 14:10:54 -040038
Brian Osmanffd11f4a2020-03-30 09:57:53 -040039 if (!!v0.customData() != !!v1.customData()) {
Mike Reed50c64ab2020-03-05 13:01:00 -050040 return false;
41 }
Brian Osman8cbedf92020-03-31 10:38:31 -040042 if (!!v0.texCoords() != !!v1.texCoords()) {
Mike Reedbdce9c22017-03-14 14:10:54 -040043 return false;
44 }
Brian Osman8cbedf92020-03-31 10:38:31 -040045 if (!!v0.colors() != !!v1.colors()) {
Mike Reedbdce9c22017-03-14 14:10:54 -040046 return false;
47 }
48
Brian Osman8cbedf92020-03-31 10:38:31 -040049 for (int i = 0; i < v0.vertexCount(); ++i) {
50 if (v0.positions()[i] != v1.positions()[i]) {
Mike Reedbdce9c22017-03-14 14:10:54 -040051 return false;
52 }
Brian Osman8cbedf92020-03-31 10:38:31 -040053 if (v0.texCoords()) {
54 if (v0.texCoords()[i] != v1.texCoords()[i]) {
Mike Reedbdce9c22017-03-14 14:10:54 -040055 return false;
56 }
57 }
Brian Osman8cbedf92020-03-31 10:38:31 -040058 if (v0.colors()) {
59 if (v0.colors()[i] != v1.colors()[i]) {
Mike Reedbdce9c22017-03-14 14:10:54 -040060 return false;
61 }
62 }
63 }
Brian Osmanffd11f4a2020-03-30 09:57:53 -040064 size_t totalCustomDataSize = v0.vertexCount() * v0.customDataSize();
65 if (totalCustomDataSize) {
66 if (memcmp(v0.customData(), v1.customData(), totalCustomDataSize) != 0) {
Mike Reed50c64ab2020-03-05 13:01:00 -050067 return false;
68 }
69 }
Brian Osman8cbedf92020-03-31 10:38:31 -040070 for (int i = 0; i < v0.indexCount(); ++i) {
71 if (v0.indices()[i] != v1.indices()[i]) {
Mike Reedbdce9c22017-03-14 14:10:54 -040072 return false;
73 }
74 }
75 return true;
76}
77
Mike Reed50c64ab2020-03-05 13:01:00 -050078static void self_test(sk_sp<SkVertices> v0, skiatest::Reporter* reporter) {
Brian Osman6b3d6e92020-06-01 16:33:28 -040079 SkBinaryWriteBuffer writer;
80 v0->priv().encode(writer);
Mike Reed50c64ab2020-03-05 13:01:00 -050081
Brian Osman6b3d6e92020-06-01 16:33:28 -040082 SkAutoMalloc buf(writer.bytesWritten());
83 writer.writeToMemory(buf.get());
84 SkReadBuffer reader(buf.get(), writer.bytesWritten());
85
86 sk_sp<SkVertices> v1 = SkVerticesPriv::Decode(reader);
87
88 REPORTER_ASSERT(reporter, v1 != nullptr);
Mike Reed50c64ab2020-03-05 13:01:00 -050089 REPORTER_ASSERT(reporter, v0->uniqueID() != 0);
90 REPORTER_ASSERT(reporter, v1->uniqueID() != 0);
91 REPORTER_ASSERT(reporter, v0->uniqueID() != v1->uniqueID());
92 REPORTER_ASSERT(reporter, equal(v0.get(), v1.get()));
93}
94
Mike Reedbdce9c22017-03-14 14:10:54 -040095DEF_TEST(Vertices, reporter) {
Mike Reed6ff6af92017-04-05 17:05:16 -040096 int vCount = 5;
97 int iCount = 9; // odd value exercises padding logic in encode()
Mike Reedbdce9c22017-03-14 14:10:54 -040098
Mike Reed50c64ab2020-03-05 13:01:00 -050099 // color-tex tests
Mike Reedaa9e3322017-03-16 14:38:48 -0400100 const uint32_t texFlags[] = { 0, SkVertices::kHasTexCoords_BuilderFlag };
101 const uint32_t colFlags[] = { 0, SkVertices::kHasColors_BuilderFlag };
Mike Reedbdce9c22017-03-14 14:10:54 -0400102 for (auto texF : texFlags) {
103 for (auto colF : colFlags) {
104 uint32_t flags = texF | colF;
105
Mike Reed887cdf12017-04-03 11:11:09 -0400106 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, vCount, iCount, flags);
Mike Reedbdce9c22017-03-14 14:10:54 -0400107
108 for (int i = 0; i < vCount; ++i) {
109 float x = (float)i;
110 builder.positions()[i].set(x, 1);
111 if (builder.texCoords()) {
112 builder.texCoords()[i].set(x, 2);
113 }
114 if (builder.colors()) {
115 builder.colors()[i] = SkColorSetARGB(0xFF, i, 0x80, 0);
116 }
117 }
Brian Osman46aacc72020-04-01 15:48:53 -0400118 for (int i = 0; i < iCount; ++i) {
Mike Reedbdce9c22017-03-14 14:10:54 -0400119 builder.indices()[i] = i % vCount;
120 }
Mike Reed50c64ab2020-03-05 13:01:00 -0500121 self_test(builder.detach(), reporter);
Mike Reedbdce9c22017-03-14 14:10:54 -0400122 }
123 }
Brian Osmanffd11f4a2020-03-30 09:57:53 -0400124
125 // custom data tests
126 using AttrType = SkVertices::Attribute::Type;
127 struct {
128 int count;
129 size_t expected_size;
130 SkVertices::Attribute attrs[4];
131 } attrTests[] = {
132 { 1, 4, { AttrType::kFloat } },
133 { 1, 8, { AttrType::kFloat2 } },
134 { 1, 12, { AttrType::kFloat3 } },
135 { 1, 16, { AttrType::kFloat4 } },
136 { 1, 4, { AttrType::kByte4_unorm } },
137 { 4, 16, { AttrType::kFloat, AttrType::kFloat, AttrType::kFloat, AttrType::kFloat } },
138 { 2, 12, { AttrType::kFloat2, AttrType::kByte4_unorm } },
139 { 2, 12, { AttrType::kByte4_unorm, AttrType::kFloat2 } },
140 };
141
142 for (const auto& test : attrTests) {
Mike Reed50c64ab2020-03-05 13:01:00 -0500143 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, vCount, iCount,
Brian Osmanffd11f4a2020-03-30 09:57:53 -0400144 test.attrs, test.count);
Mike Reed50c64ab2020-03-05 13:01:00 -0500145
Brian Osmanffd11f4a2020-03-30 09:57:53 -0400146 float* customData = (float*)builder.customData();
147 int customDataCount = test.expected_size / sizeof(float);
Brian Osman46aacc72020-04-01 15:48:53 -0400148 for (int i = 0; i < vCount; ++i) {
Mike Reed50c64ab2020-03-05 13:01:00 -0500149 builder.positions()[i].set((float)i, 1);
Brian Osmanffd11f4a2020-03-30 09:57:53 -0400150 for (int j = 0; j < customDataCount; ++j) {
151 customData[i * customDataCount + j] = (float)j;
Mike Reed50c64ab2020-03-05 13:01:00 -0500152 }
153 }
Brian Osman46aacc72020-04-01 15:48:53 -0400154 for (int i = 0; i < iCount; ++i) {
Mike Reed50c64ab2020-03-05 13:01:00 -0500155 builder.indices()[i] = i % vCount;
156 }
157 self_test(builder.detach(), reporter);
158 }
Brian Osmanffd11f4a2020-03-30 09:57:53 -0400159
Brian Salomoncccafe82018-04-28 16:13:08 -0400160 {
161 // This has the maximum number of vertices to be rewritten as indexed triangles without
162 // overflowing a 16bit index.
Ben Wagnerb0897652018-06-15 15:37:57 +0000163 SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, UINT16_MAX + 1, 0,
Brian Salomoncccafe82018-04-28 16:13:08 -0400164 SkVertices::kHasColors_BuilderFlag);
165 REPORTER_ASSERT(reporter, builder.isValid());
166 }
167 {
168 // This has too many to be rewritten.
Ben Wagnerb0897652018-06-15 15:37:57 +0000169 SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, UINT16_MAX + 2, 0,
Brian Salomoncccafe82018-04-28 16:13:08 -0400170 SkVertices::kHasColors_BuilderFlag);
171 REPORTER_ASSERT(reporter, !builder.isValid());
172 }
173 {
174 // Only two vertices - can't be rewritten.
175 SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, 2, 0,
176 SkVertices::kHasColors_BuilderFlag);
177 REPORTER_ASSERT(reporter, !builder.isValid());
178 }
179 {
180 // Minimum number of indices to be rewritten.
181 SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, 10, 3,
182 SkVertices::kHasColors_BuilderFlag);
183 REPORTER_ASSERT(reporter, builder.isValid());
184 }
185 {
186 // Too few indices to be rewritten.
187 SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, 10, 2,
188 SkVertices::kHasColors_BuilderFlag);
189 REPORTER_ASSERT(reporter, !builder.isValid());
190 }
Mike Reed50c64ab2020-03-05 13:01:00 -0500191
192 // validity tests for per-vertex-data
193
Brian Osmanffd11f4a2020-03-30 09:57:53 -0400194 // Check that invalid counts fail to initialize the builder
195 for (int attrCount : {-1, 0, SkVertices::kMaxCustomAttributes + 1}) {
196 SkVertices::Attribute attrs[] = { AttrType::kFloat };
197 SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, 10, 0, attrs, attrCount);
Mike Reed50c64ab2020-03-05 13:01:00 -0500198 REPORTER_ASSERT(reporter, !builder.isValid());
199 }
Brian Osmanffd11f4a2020-03-30 09:57:53 -0400200 { // nullptr is definitely bad
201 SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, 10, 0, nullptr, 4);
202 REPORTER_ASSERT(reporter, !builder.isValid());
Mike Reed50c64ab2020-03-05 13:01:00 -0500203 }
Brian Osmanffd11f4a2020-03-30 09:57:53 -0400204 { // "normal" number of per-vertex-data (all floats)
205 SkVertices::Attribute attrs[] = {AttrType::kFloat2, AttrType::kFloat2};
206 SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, 10, 0, attrs, 2);
Mike Reed50c64ab2020-03-05 13:01:00 -0500207 REPORTER_ASSERT(reporter, builder.isValid());
Brian Osmanffd11f4a2020-03-30 09:57:53 -0400208 REPORTER_ASSERT(reporter, builder.customData() != nullptr);
209 }
210 { // "normal" number of per-vertex-data (with packed bytes)
211 SkVertices::Attribute attrs[] = {AttrType::kFloat2, AttrType::kByte4_unorm};
212 SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, 10, 0, attrs, 2);
213 REPORTER_ASSERT(reporter, builder.isValid());
214 REPORTER_ASSERT(reporter, builder.customData() != nullptr);
Mike Reed50c64ab2020-03-05 13:01:00 -0500215 }
Mike Reedbdce9c22017-03-14 14:10:54 -0400216}
Mike Reed63227ca2018-01-30 10:12:22 -0500217
218static void fill_triangle(SkCanvas* canvas, const SkPoint pts[], SkColor c) {
219 SkColor colors[] = { c, c, c };
220 auto verts = SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode, 3, pts, nullptr, colors);
221 canvas->drawVertices(verts, SkBlendMode::kSrc, SkPaint());
222}
223
224DEF_TEST(Vertices_clipping, reporter) {
225 // A very large triangle has to be geometrically clipped (since its "fast" clipping is
226 // normally done in after building SkFixed coordinates). Check that we handle this.
227 // (and don't assert).
228 auto surf = SkSurface::MakeRasterN32Premul(3, 3);
229
230 SkPoint pts[] = { { -10, 1 }, { -10, 2 }, { 1e9f, 1.5f } };
231 fill_triangle(surf->getCanvas(), pts, SK_ColorBLACK);
232
Mike Kleinea3f0142019-03-20 11:12:10 -0500233 ToolUtils::PixelIter iter(surf.get());
Mike Reed63227ca2018-01-30 10:12:22 -0500234 SkIPoint loc;
235 while (void* addr = iter.next(&loc)) {
236 SkPMColor c = *(SkPMColor*)addr;
237 if (loc.fY == 1) {
238 REPORTER_ASSERT(reporter, c == 0xFF000000);
239 } else {
240 REPORTER_ASSERT(reporter, c == 0);
241 }
242 }
243}