1 /*
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
8 #include "include/core/SkBlendMode.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkPoint.h"
13 #include "include/core/SkRefCnt.h"
14 #include "include/core/SkSurface.h"
15 #include "include/core/SkVertices.h"
16 #include "src/base/SkAutoMalloc.h"
17 #include "src/core/SkReadBuffer.h"
18 #include "src/core/SkVerticesPriv.h"
19 #include "src/core/SkWriteBuffer.h"
20 #include "tests/Test.h"
21 #include "tools/ToolUtils.h"
22
23 #include <cstdint>
24
equal(const SkVertices * vert0,const SkVertices * vert1)25 static bool equal(const SkVertices* vert0, const SkVertices* vert1) {
26 SkVerticesPriv v0(vert0->priv()), v1(vert1->priv());
27
28 if (v0.mode() != v1.mode()) {
29 return false;
30 }
31 if (v0.vertexCount() != v1.vertexCount()) {
32 return false;
33 }
34 if (v0.indexCount() != v1.indexCount()) {
35 return false;
36 }
37
38 if (!!v0.texCoords() != !!v1.texCoords()) {
39 return false;
40 }
41 if (!!v0.colors() != !!v1.colors()) {
42 return false;
43 }
44
45 for (int i = 0; i < v0.vertexCount(); ++i) {
46 if (v0.positions()[i] != v1.positions()[i]) {
47 return false;
48 }
49 if (v0.texCoords()) {
50 if (v0.texCoords()[i] != v1.texCoords()[i]) {
51 return false;
52 }
53 }
54 if (v0.colors()) {
55 if (v0.colors()[i] != v1.colors()[i]) {
56 return false;
57 }
58 }
59 }
60 for (int i = 0; i < v0.indexCount(); ++i) {
61 if (v0.indices()[i] != v1.indices()[i]) {
62 return false;
63 }
64 }
65 return true;
66 }
67
self_test(sk_sp<SkVertices> v0,skiatest::Reporter * reporter)68 static void self_test(sk_sp<SkVertices> v0, skiatest::Reporter* reporter) {
69 SkBinaryWriteBuffer writer;
70 v0->priv().encode(writer);
71
72 SkAutoMalloc buf(writer.bytesWritten());
73 writer.writeToMemory(buf.get());
74 SkReadBuffer reader(buf.get(), writer.bytesWritten());
75
76 sk_sp<SkVertices> v1 = SkVerticesPriv::Decode(reader);
77
78 REPORTER_ASSERT(reporter, v1 != nullptr);
79 REPORTER_ASSERT(reporter, v0->uniqueID() != 0);
80 REPORTER_ASSERT(reporter, v1->uniqueID() != 0);
81 REPORTER_ASSERT(reporter, v0->uniqueID() != v1->uniqueID());
82 REPORTER_ASSERT(reporter, equal(v0.get(), v1.get()));
83 }
84
DEF_TEST(Vertices,reporter)85 DEF_TEST(Vertices, reporter) {
86 int vCount = 5;
87 int iCount = 9; // odd value exercises padding logic in encode()
88
89 // color-tex tests
90 const uint32_t texFlags[] = { 0, SkVertices::kHasTexCoords_BuilderFlag };
91 const uint32_t colFlags[] = { 0, SkVertices::kHasColors_BuilderFlag };
92 for (auto texF : texFlags) {
93 for (auto colF : colFlags) {
94 uint32_t flags = texF | colF;
95
96 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, vCount, iCount, flags);
97
98 for (int i = 0; i < vCount; ++i) {
99 float x = (float)i;
100 builder.positions()[i].set(x, 1);
101 if (builder.texCoords()) {
102 builder.texCoords()[i].set(x, 2);
103 }
104 if (builder.colors()) {
105 builder.colors()[i] = SkColorSetARGB(0xFF, i, 0x80, 0);
106 }
107 }
108 for (int i = 0; i < iCount; ++i) {
109 builder.indices()[i] = i % vCount;
110 }
111 self_test(builder.detach(), reporter);
112 }
113 }
114
115 {
116 // This has the maximum number of vertices to be rewritten as indexed triangles without
117 // overflowing a 16bit index.
118 SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, UINT16_MAX + 1, 0,
119 SkVertices::kHasColors_BuilderFlag);
120 REPORTER_ASSERT(reporter, builder.isValid());
121 }
122 {
123 // This has too many to be rewritten.
124 SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, UINT16_MAX + 2, 0,
125 SkVertices::kHasColors_BuilderFlag);
126 REPORTER_ASSERT(reporter, !builder.isValid());
127 }
128 {
129 // Only two vertices - can't be rewritten.
130 SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, 2, 0,
131 SkVertices::kHasColors_BuilderFlag);
132 REPORTER_ASSERT(reporter, !builder.isValid());
133 }
134 {
135 // Minimum number of indices to be rewritten.
136 SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, 10, 3,
137 SkVertices::kHasColors_BuilderFlag);
138 REPORTER_ASSERT(reporter, builder.isValid());
139 }
140 {
141 // Too few indices to be rewritten.
142 SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, 10, 2,
143 SkVertices::kHasColors_BuilderFlag);
144 REPORTER_ASSERT(reporter, !builder.isValid());
145 }
146 }
147
fill_triangle(SkCanvas * canvas,const SkPoint pts[],SkColor c)148 static void fill_triangle(SkCanvas* canvas, const SkPoint pts[], SkColor c) {
149 SkColor colors[] = { c, c, c };
150 auto verts = SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode, 3, pts, nullptr, colors);
151 canvas->drawVertices(verts, SkBlendMode::kSrc, SkPaint());
152 }
153
DEF_TEST(Vertices_clipping,reporter)154 DEF_TEST(Vertices_clipping, reporter) {
155 // A very large triangle has to be geometrically clipped (since its "fast" clipping is
156 // normally done in after building SkFixed coordinates). Check that we handle this.
157 // (and don't assert).
158 auto surf = SkSurface::MakeRasterN32Premul(3, 3);
159
160 SkPoint pts[] = { { -10, 1 }, { -10, 2 }, { 1e9f, 1.5f } };
161 fill_triangle(surf->getCanvas(), pts, SK_ColorBLACK);
162
163 ToolUtils::PixelIter iter(surf.get());
164 SkIPoint loc;
165 while (void* addr = iter.next(&loc)) {
166 SkPMColor c = *(SkPMColor*)addr;
167 if (loc.fY == 1) {
168 REPORTER_ASSERT(reporter, c == 0xFF000000);
169 } else {
170 REPORTER_ASSERT(reporter, c == 0);
171 }
172 }
173 }
174