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