blob: 59d06472ac9133538f4c0df0283ac38501b4c4a5 [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 Reed63227ca2018-01-30 10:12:22 -05008#include "SkCanvas.h"
9#include "SkSurface.h"
Mike Reedbdce9c22017-03-14 14:10:54 -040010#include "SkVertices.h"
Mike Reed63227ca2018-01-30 10:12:22 -050011#include "sk_pixel_iter.h"
Mike Reedbdce9c22017-03-14 14:10:54 -040012#include "Test.h"
13
14static 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
55DEF_TEST(Vertices, reporter) {
Mike Reed6ff6af92017-04-05 17:05:16 -040056 int vCount = 5;
57 int iCount = 9; // odd value exercises padding logic in encode()
Mike Reedbdce9c22017-03-14 14:10:54 -040058
Mike Reedaa9e3322017-03-16 14:38:48 -040059 const uint32_t texFlags[] = { 0, SkVertices::kHasTexCoords_BuilderFlag };
60 const uint32_t colFlags[] = { 0, SkVertices::kHasColors_BuilderFlag };
Mike Reedbdce9c22017-03-14 14:10:54 -040061 for (auto texF : texFlags) {
62 for (auto colF : colFlags) {
63 uint32_t flags = texF | colF;
64
Mike Reed887cdf12017-04-03 11:11:09 -040065 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, vCount, iCount, flags);
Mike Reedbdce9c22017-03-14 14:10:54 -040066
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
Mike Reed9a8065d2017-03-14 21:05:17 -040085 REPORTER_ASSERT(reporter, v0->uniqueID() != 0);
86 REPORTER_ASSERT(reporter, v1->uniqueID() != 0);
87 REPORTER_ASSERT(reporter, v0->uniqueID() != v1->uniqueID());
Mike Reedbdce9c22017-03-14 14:10:54 -040088 REPORTER_ASSERT(reporter, equal(v0.get(), v1.get()));
89 }
90 }
Brian Salomoncccafe82018-04-28 16:13:08 -040091 {
92 // This has the maximum number of vertices to be rewritten as indexed triangles without
93 // overflowing a 16bit index.
Brian Salomon3fc6a182018-05-01 09:13:16 -040094 SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, SK_MaxU16 + 1, 0,
Brian Salomoncccafe82018-04-28 16:13:08 -040095 SkVertices::kHasColors_BuilderFlag);
96 REPORTER_ASSERT(reporter, builder.isValid());
97 }
98 {
99 // This has too many to be rewritten.
Brian Salomon3fc6a182018-05-01 09:13:16 -0400100 SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, SK_MaxU16 + 2, 0,
Brian Salomoncccafe82018-04-28 16:13:08 -0400101 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 }
Mike Reedbdce9c22017-03-14 14:10:54 -0400122}
Mike Reed63227ca2018-01-30 10:12:22 -0500123
124static 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
130DEF_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}