Mike Reed | bdce9c2 | 2017-03-14 14:10:54 -0400 | [diff] [blame] | 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 | |
| 11 | static 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 | |
| 52 | DEF_TEST(Vertices, reporter) { |
Mike Reed | 6ff6af9 | 2017-04-05 17:05:16 -0400 | [diff] [blame] | 53 | int vCount = 5; |
| 54 | int iCount = 9; // odd value exercises padding logic in encode() |
Mike Reed | bdce9c2 | 2017-03-14 14:10:54 -0400 | [diff] [blame] | 55 | |
Mike Reed | aa9e332 | 2017-03-16 14:38:48 -0400 | [diff] [blame] | 56 | const uint32_t texFlags[] = { 0, SkVertices::kHasTexCoords_BuilderFlag }; |
| 57 | const uint32_t colFlags[] = { 0, SkVertices::kHasColors_BuilderFlag }; |
Mike Reed | bdce9c2 | 2017-03-14 14:10:54 -0400 | [diff] [blame] | 58 | for (auto texF : texFlags) { |
| 59 | for (auto colF : colFlags) { |
| 60 | uint32_t flags = texF | colF; |
| 61 | |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 62 | SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, vCount, iCount, flags); |
Mike Reed | bdce9c2 | 2017-03-14 14:10:54 -0400 | [diff] [blame] | 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 | |
Mike Reed | 9a8065d | 2017-03-14 21:05:17 -0400 | [diff] [blame] | 82 | REPORTER_ASSERT(reporter, v0->uniqueID() != 0); |
| 83 | REPORTER_ASSERT(reporter, v1->uniqueID() != 0); |
| 84 | REPORTER_ASSERT(reporter, v0->uniqueID() != v1->uniqueID()); |
Mike Reed | bdce9c2 | 2017-03-14 14:10:54 -0400 | [diff] [blame] | 85 | REPORTER_ASSERT(reporter, equal(v0.get(), v1.get())); |
| 86 | } |
| 87 | } |
| 88 | } |