Flutter Engine
The Flutter Engine
VerticesTest.cpp
Go to the documentation of this file.
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
21#include "tests/Test.h"
22#include "tools/ToolUtils.h"
23
24#include <cstdint>
25
26static bool equal(const SkVertices* vert0, const SkVertices* vert1) {
27 SkVerticesPriv v0(vert0->priv()), v1(vert1->priv());
28
29 if (v0.mode() != v1.mode()) {
30 return false;
31 }
32 if (v0.vertexCount() != v1.vertexCount()) {
33 return false;
34 }
35 if (v0.indexCount() != v1.indexCount()) {
36 return false;
37 }
38
39 if (!!v0.texCoords() != !!v1.texCoords()) {
40 return false;
41 }
42 if (!!v0.colors() != !!v1.colors()) {
43 return false;
44 }
45
46 for (int i = 0; i < v0.vertexCount(); ++i) {
47 if (v0.positions()[i] != v1.positions()[i]) {
48 return false;
49 }
50 if (v0.texCoords()) {
51 if (v0.texCoords()[i] != v1.texCoords()[i]) {
52 return false;
53 }
54 }
55 if (v0.colors()) {
56 if (v0.colors()[i] != v1.colors()[i]) {
57 return false;
58 }
59 }
60 }
61 for (int i = 0; i < v0.indexCount(); ++i) {
62 if (v0.indices()[i] != v1.indices()[i]) {
63 return false;
64 }
65 }
66 return true;
67}
68
70 SkBinaryWriteBuffer writer({});
71 v0->priv().encode(writer);
72
73 SkAutoMalloc buf(writer.bytesWritten());
74 writer.writeToMemory(buf.get());
75 SkReadBuffer reader(buf.get(), writer.bytesWritten());
76
78
79 REPORTER_ASSERT(reporter, v1 != nullptr);
83 REPORTER_ASSERT(reporter, equal(v0.get(), v1.get()));
84}
85
86DEF_TEST(Vertices, reporter) {
87 int vCount = 5;
88 int iCount = 9; // odd value exercises padding logic in encode()
89
90 // color-tex tests
91 const uint32_t texFlags[] = { 0, SkVertices::kHasTexCoords_BuilderFlag };
92 const uint32_t colFlags[] = { 0, SkVertices::kHasColors_BuilderFlag };
93 for (auto texF : texFlags) {
94 for (auto colF : colFlags) {
95 uint32_t flags = texF | colF;
96
98
99 for (int i = 0; i < vCount; ++i) {
100 float x = (float)i;
101 builder.positions()[i].set(x, 1);
102 if (builder.texCoords()) {
103 builder.texCoords()[i].set(x, 2);
104 }
105 if (builder.colors()) {
106 builder.colors()[i] = SkColorSetARGB(0xFF, i, 0x80, 0);
107 }
108 }
109 for (int i = 0; i < iCount; ++i) {
110 builder.indices()[i] = i % vCount;
111 }
112 self_test(builder.detach(), reporter);
113 }
114 }
115
116 {
117 // This has the maximum number of vertices to be rewritten as indexed triangles without
118 // overflowing a 16bit index.
121 REPORTER_ASSERT(reporter, builder.isValid());
122 }
123 {
124 // This has too many to be rewritten.
127 REPORTER_ASSERT(reporter, !builder.isValid());
128 }
129 {
130 // Only two vertices - can't be rewritten.
133 REPORTER_ASSERT(reporter, !builder.isValid());
134 }
135 {
136 // Minimum number of indices to be rewritten.
139 REPORTER_ASSERT(reporter, builder.isValid());
140 }
141 {
142 // Too few indices to be rewritten.
145 REPORTER_ASSERT(reporter, !builder.isValid());
146 }
147}
148
149static void fill_triangle(SkCanvas* canvas, const SkPoint pts[], SkColor c) {
150 SkColor colors[] = { c, c, c };
151 auto verts = SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode, 3, pts, nullptr, colors);
152 canvas->drawVertices(verts, SkBlendMode::kSrc, SkPaint());
153}
154
155DEF_TEST(Vertices_clipping, reporter) {
156 // A very large triangle has to be geometrically clipped (since its "fast" clipping is
157 // normally done in after building SkFixed coordinates). Check that we handle this.
158 // (and don't assert).
160
161 SkPoint pts[] = { { -10, 1 }, { -10, 2 }, { 1e9f, 1.5f } };
162 fill_triangle(surf->getCanvas(), pts, SK_ColorBLACK);
163
164 ToolUtils::PixelIter iter(surf.get());
165 SkIPoint loc;
166 while (void* addr = iter.next(&loc)) {
167 SkPMColor c = *(SkPMColor*)addr;
168 if (loc.fY == 1) {
169 REPORTER_ASSERT(reporter, c == 0xFF000000);
170 } else {
171 REPORTER_ASSERT(reporter, c == 0);
172 }
173 }
174}
reporter
Definition: FontMgrTest.cpp:39
uint32_t SkColor
Definition: SkColor.h:37
uint32_t SkPMColor
Definition: SkColor.h:205
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition: SkColor.h:49
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
static void fill_triangle(SkCanvas *canvas, const SkPoint pts[], SkColor c)
static bool equal(const SkVertices *vert0, const SkVertices *vert1)
DEF_TEST(Vertices, reporter)
static void self_test(const sk_sp< SkVertices > &v0, skiatest::Reporter *reporter)
void * get()
Definition: SkAutoMalloc.h:64
void drawVertices(const SkVertices *vertices, SkBlendMode mode, const SkPaint &paint)
Definition: SkCanvas.cpp:1720
SkVertices::VertexMode mode() const
void encode(SkWriteBuffer &) const
Definition: SkVertices.cpp:240
const SkColor * colors() const
const uint16_t * indices() const
const SkPoint * texCoords() const
static sk_sp< SkVertices > Decode(SkReadBuffer &)
Definition: SkVertices.cpp:267
int indexCount() const
int vertexCount() const
const SkPoint * positions() const
@ kHasTexCoords_BuilderFlag
Definition: SkVertices.h:63
@ kHasColors_BuilderFlag
Definition: SkVertices.h:64
static sk_sp< SkVertices > MakeCopy(VertexMode mode, int vertexCount, const SkPoint positions[], const SkPoint texs[], const SkColor colors[], int indexCount, const uint16_t indices[])
Definition: SkVertices.cpp:200
uint32_t uniqueID() const
Definition: SkVertices.h:97
@ kTriangleFan_VertexMode
Definition: SkVertices.h:33
@ kTriangles_VertexMode
Definition: SkVertices.h:31
SkVerticesPriv priv()
void * next(SkIPoint *loc=nullptr)
Definition: ToolUtils.h:286
T * get() const
Definition: SkRefCnt.h:303
FlutterSemanticsFlag flags
double x
PODArray< SkColor > colors
Definition: SkRecords.h:276
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
int32_t fY
y-axis value
Definition: SkPoint_impl.h:30
static SkImageInfo MakeN32Premul(int width, int height)