Flutter Engine
The Flutter Engine
PolyUtilsBench.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2018 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 "bench/Benchmark.h"
10#include "include/core/SkRect.h"
14
15using namespace skia_private;
16
17#if !defined(SK_ENABLE_OPTIMIZE_SIZE)
18
19class PolyUtilsBench : public Benchmark {
20public:
21 // Evaluate SkTriangulateSimplePolygon's performance (via derived classes) on:
22 // a non-self-intersecting star, a circle of tiny line segments and a self-intersecting star
23 enum class Type { kConvexCheck, kSimpleCheck, kInsetConvex, kOffsetSimple, kTessellateSimple };
24
26
27 virtual void appendName(SkString*) = 0;
28 virtual void makePoly(SkTDArray<SkPoint>* poly) = 0;
29 virtual int complexity() { return 0; }
30
31protected:
32 const char* onGetName() override {
33 fName = "poly_utils_";
34 this->appendName(&fName);
35 switch (fType) {
37 fName.append("_c");
38 break;
40 fName.append("_s");
41 break;
43 fName.append("_i");
44 break;
46 fName.append("_o");
47 break;
49 fName.append("_t");
50 break;
51 }
52 return fName.c_str();
53 }
54
55 void onDraw(int loops, SkCanvas* canvas) override {
57 this->makePoly(&poly);
58 switch (fType) {
60 for (int i = 0; i < loops; i++) {
61 (void)SkIsConvexPolygon(poly.begin(), poly.size());
62 }
63 break;
65 for (int i = 0; i < loops; i++) {
66 (void)SkIsSimplePolygon(poly.begin(), poly.size());
67 }
68 break;
70 if (SkIsConvexPolygon(poly.begin(), poly.size())) {
72 for (int i = 0; i < loops; i++) {
73 (void)SkInsetConvexPolygon(poly.begin(), poly.size(), 10, &result);
74 (void)SkInsetConvexPolygon(poly.begin(), poly.size(), 40, &result);
75 }
76 }
77 break;
79 if (SkIsSimplePolygon(poly.begin(), poly.size())) {
82 bounds.setBounds(poly.begin(), poly.size());
83 for (int i = 0; i < loops; i++) {
84 (void)SkOffsetSimplePolygon(poly.begin(), poly.size(), bounds, 10,
85 &result);
86 (void)SkOffsetSimplePolygon(poly.begin(), poly.size(), bounds, -10,
87 &result);
88 }
89 }
90 break;
92 if (SkIsSimplePolygon(poly.begin(), poly.size())) {
93 AutoSTMalloc<64, uint16_t> indexMap(poly.size());
94 for (int i = 0; i < poly.size(); ++i) {
95 indexMap[i] = i;
96 }
97 SkTDArray<uint16_t> triangleIndices;
98 for (int i = 0; i < loops; i++) {
99 SkTriangulateSimplePolygon(poly.begin(), indexMap, poly.size(),
100 &triangleIndices);
101 }
102 }
103 break;
104 }
105 }
106
107private:
108 SkString fName;
109 Type fType;
110
111 using INHERITED = Benchmark;
112};
113
115public:
117
118 void appendName(SkString* name) override {
119 name->append("star");
120 }
121 void makePoly(SkTDArray<SkPoint>* poly) override {
122 // create non-intersecting star
123 const SkScalar c = SkIntToScalar(45);
124 const SkScalar r1 = SkIntToScalar(20);
125 const SkScalar r2 = SkIntToScalar(3);
126 const int n = 500;
127 SkScalar rad = 0;
128 const SkScalar drad = SK_ScalarPI / n;
129 for (int i = 0; i < n; i++) {
130 *poly->append() = SkPoint::Make(c + SkScalarCos(rad) * r1, c + SkScalarSin(rad) * r1);
131 rad += drad;
132 *poly->append() = SkPoint::Make(c + SkScalarCos(rad) * r2, c + SkScalarSin(rad) * r2);
133 rad += drad;
134 }
135 }
136private:
137 using INHERITED = PolyUtilsBench;
138};
139
141public:
143
144 void appendName(SkString* name) override {
145 name->append("circle");
146 }
147 void makePoly(SkTDArray<SkPoint>* poly) override {
148 // create circle with many vertices
149 const SkScalar c = SkIntToScalar(45);
150 const SkScalar r = SkIntToScalar(20);
151 const int n = 1000;
152 SkScalar rad = 0;
153 const SkScalar drad = 2 * SK_ScalarPI / n;
154 for (int i = 0; i < n; i++) {
155 *poly->append() = SkPoint::Make(c + SkScalarCos(rad) * r, c + SkScalarSin(rad) * r);
156 rad += drad;
157 }
158 }
159private:
160 using INHERITED = PolyUtilsBench;
161};
162
164public:
166
167 void appendName(SkString* name) override {
168 name->append("intersecting");
169 }
170 void makePoly(SkTDArray<SkPoint>* poly) override {
171 // create self-intersecting star
172 const SkScalar c = SkIntToScalar(45);
173 const SkScalar r = SkIntToScalar(20);
174 const int n = 1000;
175
176 SkScalar rad = -SK_ScalarPI / 2;
177 const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n;
178 *poly->append() = SkPoint::Make(c, c - r);
179 for (int i = 1; i < n; i++) {
180 rad += drad;
181 *poly->append() = SkPoint::Make(c + SkScalarCos(rad) * r, c + SkScalarSin(rad) * r);
182 }
183 }
184private:
185 using INHERITED = PolyUtilsBench;
186};
187
188// familiar videogame character
190public:
192
193 void appendName(SkString* name) override {
194 name->append("notch");
195 }
196 void makePoly(SkTDArray<SkPoint>* poly) override {
197 // create 3/4 circle with many vertices
198 const SkScalar c = SkIntToScalar(45);
199 const SkScalar r = SkIntToScalar(20);
200 const int n = 1000;
201 SkScalar rad = 0;
202 const SkScalar drad = 3 * SK_ScalarPI / (2*n);
203 for (int i = 0; i < n; i++) {
204 *poly->append() = SkPoint::Make(c + SkScalarCos(rad) * r, c + SkScalarSin(rad) * r);
205 rad += drad;
206 }
207 // and the mouth
208 *poly->append() = SkPoint::Make(45, 45);
209 }
210private:
211 using INHERITED = PolyUtilsBench;
212};
213
215public:
217
218 void appendName(SkString* name) override {
219 name->append("icecream");
220 }
221 void makePoly(SkTDArray<SkPoint>* poly) override {
222 // create 3/4 circle with many vertices
223 const SkScalar c = SkIntToScalar(45);
224 const SkScalar r = SkIntToScalar(20);
225 const int n = 1000;
226 SkScalar rad = 0;
227 const SkScalar drad = 3 * SK_ScalarPI / (2*n);
228 for (int i = 0; i < n; i++) {
229 *poly->append() = SkPoint::Make(c + SkScalarCos(rad) * r, c + SkScalarSin(rad) * r);
230 rad += drad;
231 }
232 // and the tip of the cone
233 *poly->append() = SkPoint::Make(90, 0);
234 }
235private:
236 using INHERITED = PolyUtilsBench;
237};
238
264
265#endif // !defined(SK_ENABLE_OPTIMIZE_SIZE)
#define DEF_BENCH(code)
Definition: Benchmark.h:20
bool SkIsConvexPolygon(const SkPoint *polygonVerts, int polygonSize)
bool SkTriangulateSimplePolygon(const SkPoint *polygonVerts, uint16_t *indexMap, int polygonSize, SkTDArray< uint16_t > *triangleIndices)
bool SkOffsetSimplePolygon(const SkPoint *inputPolygonVerts, int inputPolygonSize, const SkRect &bounds, SkScalar offset, SkTDArray< SkPoint > *offsetPolygon, SkTDArray< int > *polygonIndices)
bool SkIsSimplePolygon(const SkPoint *polygon, int polygonSize)
bool SkInsetConvexPolygon(const SkPoint *inputPolygonVerts, int inputPolygonSize, SkScalar inset, SkTDArray< SkPoint > *insetPolygon)
#define SkScalarSin(radians)
Definition: SkScalar.h:45
#define SkIntToScalar(x)
Definition: SkScalar.h:57
#define SkScalarCos(radians)
Definition: SkScalar.h:46
#define SK_ScalarPI
Definition: SkScalar.h:21
GLenum type
CirclePolyUtilsBench(PolyUtilsBench::Type type)
void appendName(SkString *name) override
void makePoly(SkTDArray< SkPoint > *poly) override
IceCreamPolyUtilsBench(PolyUtilsBench::Type type)
void makePoly(SkTDArray< SkPoint > *poly) override
void appendName(SkString *name) override
IntersectingPolyUtilsBench(PolyUtilsBench::Type type)
void makePoly(SkTDArray< SkPoint > *poly) override
void appendName(SkString *name) override
void makePoly(SkTDArray< SkPoint > *poly) override
NotchPolyUtilsBench(PolyUtilsBench::Type type)
void appendName(SkString *name) override
void onDraw(int loops, SkCanvas *canvas) override
PolyUtilsBench(Type type)
virtual void makePoly(SkTDArray< SkPoint > *poly)=0
virtual int complexity()
const char * onGetName() override
virtual void appendName(SkString *)=0
void append(const char text[])
Definition: SkString.h:203
const char * c_str() const
Definition: SkString.h:133
int size() const
Definition: SkTDArray.h:138
T * begin()
Definition: SkTDArray.h:150
T * append()
Definition: SkTDArray.h:191
void makePoly(SkTDArray< SkPoint > *poly) override
void appendName(SkString *name) override
StarPolyUtilsBench(PolyUtilsBench::Type type)
float SkScalar
Definition: extension.cpp:12
GAsyncResult * result
Optional< SkRect > bounds
Definition: SkRecords.h:189
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
static constexpr SkPoint Make(float x, float y)
Definition: SkPoint_impl.h:173