blob: cabee01b4b38b7fbfa69d17af6454e487cef6d3b [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 }
30
Brian Osman8cbedf92020-03-31 10:38:31 -040031 if (!!v0.texCoords() != !!v1.texCoords()) {
Mike Reedbdce9c22017-03-14 14:10:54 -040032 return false;
33 }
Brian Osman8cbedf92020-03-31 10:38:31 -040034 if (!!v0.colors() != !!v1.colors()) {
Mike Reedbdce9c22017-03-14 14:10:54 -040035 return false;
36 }
37
Brian Osman8cbedf92020-03-31 10:38:31 -040038 for (int i = 0; i < v0.vertexCount(); ++i) {
39 if (v0.positions()[i] != v1.positions()[i]) {
Mike Reedbdce9c22017-03-14 14:10:54 -040040 return false;
41 }
Brian Osman8cbedf92020-03-31 10:38:31 -040042 if (v0.texCoords()) {
43 if (v0.texCoords()[i] != v1.texCoords()[i]) {
Mike Reedbdce9c22017-03-14 14:10:54 -040044 return false;
45 }
46 }
Brian Osman8cbedf92020-03-31 10:38:31 -040047 if (v0.colors()) {
48 if (v0.colors()[i] != v1.colors()[i]) {
Mike Reedbdce9c22017-03-14 14:10:54 -040049 return false;
50 }
51 }
52 }
Brian Osman8cbedf92020-03-31 10:38:31 -040053 for (int i = 0; i < v0.indexCount(); ++i) {
54 if (v0.indices()[i] != v1.indices()[i]) {
Mike Reedbdce9c22017-03-14 14:10:54 -040055 return false;
56 }
57 }
58 return true;
59}
60
Mike Reed50c64ab2020-03-05 13:01:00 -050061static void self_test(sk_sp<SkVertices> v0, skiatest::Reporter* reporter) {
Brian Osman6b3d6e92020-06-01 16:33:28 -040062 SkBinaryWriteBuffer writer;
63 v0->priv().encode(writer);
Mike Reed50c64ab2020-03-05 13:01:00 -050064
Brian Osman6b3d6e92020-06-01 16:33:28 -040065 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);
Mike Reed50c64ab2020-03-05 13:01:00 -050072 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
Mike Reedbdce9c22017-03-14 14:10:54 -040078DEF_TEST(Vertices, reporter) {
Mike Reed6ff6af92017-04-05 17:05:16 -040079 int vCount = 5;
80 int iCount = 9; // odd value exercises padding logic in encode()
Mike Reedbdce9c22017-03-14 14:10:54 -040081
Mike Reed50c64ab2020-03-05 13:01:00 -050082 // color-tex tests
Mike Reedaa9e3322017-03-16 14:38:48 -040083 const uint32_t texFlags[] = { 0, SkVertices::kHasTexCoords_BuilderFlag };
84 const uint32_t colFlags[] = { 0, SkVertices::kHasColors_BuilderFlag };
Mike Reedbdce9c22017-03-14 14:10:54 -040085 for (auto texF : texFlags) {
86 for (auto colF : colFlags) {
87 uint32_t flags = texF | colF;
88
Mike Reed887cdf12017-04-03 11:11:09 -040089 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, vCount, iCount, flags);
Mike Reedbdce9c22017-03-14 14:10:54 -040090
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 }
Brian Osman46aacc72020-04-01 15:48:53 -0400101 for (int i = 0; i < iCount; ++i) {
Mike Reedbdce9c22017-03-14 14:10:54 -0400102 builder.indices()[i] = i % vCount;
103 }
Mike Reed50c64ab2020-03-05 13:01:00 -0500104 self_test(builder.detach(), reporter);
Mike Reedbdce9c22017-03-14 14:10:54 -0400105 }
106 }
Brian Osmanffd11f4a2020-03-30 09:57:53 -0400107
Brian Salomoncccafe82018-04-28 16:13:08 -0400108 {
109 // This has the maximum number of vertices to be rewritten as indexed triangles without
110 // overflowing a 16bit index.
Ben Wagnerb0897652018-06-15 15:37:57 +0000111 SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, UINT16_MAX + 1, 0,
Brian Salomoncccafe82018-04-28 16:13:08 -0400112 SkVertices::kHasColors_BuilderFlag);
113 REPORTER_ASSERT(reporter, builder.isValid());
114 }
115 {
116 // This has too many to be rewritten.
Ben Wagnerb0897652018-06-15 15:37:57 +0000117 SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, UINT16_MAX + 2, 0,
Brian Salomoncccafe82018-04-28 16:13:08 -0400118 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 }
Mike Reedbdce9c22017-03-14 14:10:54 -0400139}
Mike Reed63227ca2018-01-30 10:12:22 -0500140
141static 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
147DEF_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
Mike Kleinea3f0142019-03-20 11:12:10 -0500156 ToolUtils::PixelIter iter(surf.get());
Mike Reed63227ca2018-01-30 10:12:22 -0500157 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}