blob: 399aba2f2170d092f52cc90f5d1be0adba45923a [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
8#include "SkVertices.h"
9#include "Test.h"
10
11static bool equal(const SkVertices* v0, const SkVertices* v1) {
12 if (v0->mode() != v1->mode()) {
13 return false;
14 }
15 if (v0->vertexCount() != v1->vertexCount()) {
16 return false;
17 }
18 if (v0->indexCount() != v1->indexCount()) {
19 return false;
20 }
21
22 if (!!v0->texCoords() != !!v1->texCoords()) {
23 return false;
24 }
25 if (!!v0->colors() != !!v1->colors()) {
26 return false;
27 }
28
29 for (int i = 0; i < v0->vertexCount(); ++i) {
30 if (v0->positions()[i] != v1->positions()[i]) {
31 return false;
32 }
33 if (v0->texCoords()) {
34 if (v0->texCoords()[i] != v1->texCoords()[i]) {
35 return false;
36 }
37 }
38 if (v0->colors()) {
39 if (v0->colors()[i] != v1->colors()[i]) {
40 return false;
41 }
42 }
43 }
44 for (int i = 0; i < v0->indexCount(); ++i) {
45 if (v0->indices()[i] != v1->indices()[i]) {
46 return false;
47 }
48 }
49 return true;
50}
51
52DEF_TEST(Vertices, reporter) {
Mike Reed6ff6af92017-04-05 17:05:16 -040053 int vCount = 5;
54 int iCount = 9; // odd value exercises padding logic in encode()
Mike Reedbdce9c22017-03-14 14:10:54 -040055
Mike Reedaa9e3322017-03-16 14:38:48 -040056 const uint32_t texFlags[] = { 0, SkVertices::kHasTexCoords_BuilderFlag };
57 const uint32_t colFlags[] = { 0, SkVertices::kHasColors_BuilderFlag };
Mike Reedbdce9c22017-03-14 14:10:54 -040058 for (auto texF : texFlags) {
59 for (auto colF : colFlags) {
60 uint32_t flags = texF | colF;
61
Mike Reed887cdf12017-04-03 11:11:09 -040062 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, vCount, iCount, flags);
Mike Reedbdce9c22017-03-14 14:10:54 -040063
64 for (int i = 0; i < vCount; ++i) {
65 float x = (float)i;
66 builder.positions()[i].set(x, 1);
67 if (builder.texCoords()) {
68 builder.texCoords()[i].set(x, 2);
69 }
70 if (builder.colors()) {
71 builder.colors()[i] = SkColorSetARGB(0xFF, i, 0x80, 0);
72 }
73 }
74 for (int i = 0; i < builder.indexCount(); ++i) {
75 builder.indices()[i] = i % vCount;
76 }
77
78 sk_sp<SkVertices> v0 = builder.detach();
79 sk_sp<SkData> data = v0->encode();
80 sk_sp<SkVertices> v1 = SkVertices::Decode(data->data(), data->size());
81
Mike Reed9a8065d2017-03-14 21:05:17 -040082 REPORTER_ASSERT(reporter, v0->uniqueID() != 0);
83 REPORTER_ASSERT(reporter, v1->uniqueID() != 0);
84 REPORTER_ASSERT(reporter, v0->uniqueID() != v1->uniqueID());
Mike Reedbdce9c22017-03-14 14:10:54 -040085 REPORTER_ASSERT(reporter, equal(v0.get(), v1.get()));
86 }
87 }
88}