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 "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) {
53    int vCount = 5;
54    int iCount = 9; // odd value exercises padding logic in encode()
55
56    const uint32_t texFlags[] = { 0, SkVertices::kHasTexCoords_BuilderFlag };
57    const uint32_t colFlags[] = { 0, SkVertices::kHasColors_BuilderFlag };
58    for (auto texF : texFlags) {
59        for (auto colF : colFlags) {
60            uint32_t flags = texF | colF;
61
62            SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, vCount, iCount, flags);
63
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
82            REPORTER_ASSERT(reporter, v0->uniqueID() != 0);
83            REPORTER_ASSERT(reporter, v1->uniqueID() != 0);
84            REPORTER_ASSERT(reporter, v0->uniqueID() != v1->uniqueID());
85            REPORTER_ASSERT(reporter, equal(v0.get(), v1.get()));
86        }
87    }
88}
89