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