1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "Benchmark.h"
#include "SkCanvas.h"
#include "SkCommandLineFlags.h"
#include "SkPaint.h"
#include "SkRandom.h"
#include "SkRRect.h"
#include "SkString.h"
#include <stdio.h>
#include <stdlib.h>
#include <functional>
#define ENABLE_COMMAND_LINE_SHAPES_BENCH 0
#if ENABLE_COMMAND_LINE_SHAPES_BENCH
DEFINE_string(shapesType, "mixed", "Type of shape to use in ShapesBench. Must be one of: "
"rect, oval, rrect, mixed.");
DEFINE_string(innerShapesType, "none", "Type of inner shape to use in ShapesBench. Must be one of: "
"none, rect, oval, rrect, mixed.");
DEFINE_int32(numShapes, 10000, "Number of shapes to draw in ShapesBench.");
DEFINE_string(shapesSize, "32x32", "Size of shapes to draw in ShapesBench.");
DEFINE_bool(shapesPersp, false, "Use slight perspective tilt in ShapesBench?");
#endif
/*
* This class is used for several benchmarks that draw different primitive Skia shapes at various
* sizes. It is used to test both CPU-bound and GPU-bound rendering situations. It draws large
* amounts of shapes internally (rather than relying on nanobench selecting lots of loops) in order
* to take advantage of instanced rendering approaches.
*/
class ShapesBench : public Benchmark {
public:
enum ShapesType {
kNone_ShapesType,
kRect_ShapesType,
kOval_ShapesType,
kRRect_ShapesType,
kMixed_ShapesType
};
ShapesBench(ShapesType shapesType, ShapesType innerShapesType,
int numShapes, const SkISize& shapesSize, bool perspective)
: fShapesType(shapesType)
, fInnerShapesType(innerShapesType)
, fNumShapes(numShapes)
, fShapesSize(shapesSize)
, fPerspective(perspective) {
clampShapeSize();
}
#if ENABLE_COMMAND_LINE_SHAPES_BENCH
ShapesBench() {
if (!strcmp(FLAGS_shapesType[0], "rect")) {
fShapesType = kRect_ShapesType;
} else if (!strcmp(FLAGS_shapesType[0], "oval")) {
fShapesType = kOval_ShapesType;
} else if (!strcmp(FLAGS_shapesType[0], "rrect")) {
fShapesType = kRRect_ShapesType;
} else if (!strcmp(FLAGS_shapesType[0], "mixed")) {
fShapesType = kMixed_ShapesType;
} else {
SkDebugf("Invalid shapesType \"%s\". Must be one of: rect, oval, rrect, mixed.",
FLAGS_shapesType[0]);
exit(-1);
}
if (!strcmp(FLAGS_innerShapesType[0], "none")) {
fInnerShapesType = kNone_ShapesType;
} else if (!strcmp(FLAGS_innerShapesType[0], "rect")) {
fInnerShapesType = kRect_ShapesType;
} else if (!strcmp(FLAGS_innerShapesType[0], "oval")) {
fInnerShapesType = kOval_ShapesType;
} else if (!strcmp(FLAGS_innerShapesType[0], "rrect")) {
fInnerShapesType = kRRect_ShapesType;
} else if (!strcmp(FLAGS_innerShapesType[0], "mixed")) {
fInnerShapesType = kMixed_ShapesType;
} else {
SkDebugf("Invalid innerShapesType \"%s\". Must be one of: "
"none, rect, oval, rrect, mixed.", FLAGS_innerShapesType[0]);
exit(-1);
}
if (2 != sscanf(FLAGS_shapesSize[0], "%ix%i", &fShapesSize.fWidth, &fShapesSize.fHeight)) {
SkDebugf("Could not parse shapesSize from \"%s\". Expected \"%%ix%%i\"\n",
FLAGS_shapesSize[0]);
exit(-1);
}
fNumShapes = FLAGS_numShapes;
fPerspective = FLAGS_shapesPersp;
clampShapeSize();
}
#endif
bool isVisual() override { return true; }
private:
void clampShapeSize() {
float maxDiagonal = static_cast<float>(SkTMin(kBenchWidth, kBenchHeight));
float diagonal = sqrtf(static_cast<float>(fShapesSize.width() * fShapesSize.width()) +
static_cast<float>(fShapesSize.height() * fShapesSize.height()));
if (diagonal > maxDiagonal) {
fShapesSize.fWidth = static_cast<int>(fShapesSize.width() * maxDiagonal / diagonal);
fShapesSize.fHeight = static_cast<int>(fShapesSize.height() * maxDiagonal / diagonal);
}
}
const char* onGetName() override {
const char* shapeTypeNames[] = {
"none", "rect", "oval", "rrect", "mixed"
};
fName.printf("shapes_%s", shapeTypeNames[fShapesType]);
if (kNone_ShapesType != fInnerShapesType) {
fName.appendf("_inner_%s", shapeTypeNames[fInnerShapesType]);
}
fName.appendf("_%i_%ix%i", fNumShapes, fShapesSize.width(), fShapesSize.height());
if (fPerspective) {
fName.append("_persp");
}
return fName.c_str();
}
SkIPoint onGetSize() override { return SkIPoint::Make(kBenchWidth, kBenchHeight); }
void onDelayedSetup() override {
SkScalar w = SkIntToScalar(fShapesSize.width());
SkScalar h = SkIntToScalar(fShapesSize.height());
fRect.setRect(SkRect::MakeXYWH(-w / 2, -h / 2, w, h));
fOval.setOval(fRect.rect());
fRRect.setNinePatch(fRect.rect(), w / 8, h / 13, w / 11, h / 7);
if (kNone_ShapesType != fInnerShapesType) {
fRect.inset(w / 7, h / 11, &fInnerRect);
fInnerRect.offset(w / 28, h / 44);
fInnerOval.setOval(fInnerRect.rect());
fInnerRRect.setRectXY(fInnerRect.rect(), w / 13, w / 7);
}
SkRandom rand;
fShapes.push_back_n(fNumShapes);
for (int i = 0; i < fNumShapes; i++) {
float pad = sqrtf(static_cast<float>(fShapesSize.width() * fShapesSize.width()) +
static_cast<float>(fShapesSize.height() * fShapesSize.height()));
fShapes[i].fMatrix.setTranslate(0.5f * pad + rand.nextF() * (kBenchWidth - pad),
0.5f * pad + rand.nextF() * (kBenchHeight - pad));
fShapes[i].fMatrix.preRotate(rand.nextF() * 360.0f);
if (fPerspective) {
fShapes[i].fMatrix.setPerspX(0.00015f);
fShapes[i].fMatrix.setPerspY(-0.00015f);
}
fShapes[i].fColor = rand.nextU() | 0xff808080;
}
for (int i = 0; i < fNumShapes; i++) {
// Do this in a separate loop so mixed shapes get the same random numbers during
// placement as non-mixed do.
int shapeType = fShapesType;
if (kMixed_ShapesType == shapeType) {
shapeType = rand.nextRangeU(kRect_ShapesType, kRRect_ShapesType);
}
int innerShapeType = fInnerShapesType;
if (kMixed_ShapesType == innerShapeType) {
innerShapeType = rand.nextRangeU(kRect_ShapesType, kRRect_ShapesType);
}
if (kNone_ShapesType == innerShapeType) {
switch (shapeType) {
using namespace std;
using namespace std::placeholders;
case kRect_ShapesType:
fShapes[i].fDraw = bind(&SkCanvas::drawRect, _1, cref(fRect.rect()), _2);
break;
case kOval_ShapesType:
fShapes[i].fDraw = bind(&SkCanvas::drawOval, _1, cref(fOval.rect()), _2);
break;
case kRRect_ShapesType:
fShapes[i].fDraw = bind(&SkCanvas::drawRRect, _1, cref(fRRect), _2);
break;
}
} else {
const SkRRect* outer;
switch (shapeType) {
case kRect_ShapesType: outer = &fRect; break;
case kOval_ShapesType: outer = &fOval; break;
case kRRect_ShapesType: outer = &fRRect; break;
}
const SkRRect* inner;
switch (innerShapeType) {
case kRect_ShapesType: inner = &fInnerRect; break;
case kOval_ShapesType: inner = &fInnerOval; break;
case kRRect_ShapesType: inner = &fInnerRRect; break;
}
fShapes[i].fDraw = std::bind(&SkCanvas::drawDRRect, std::placeholders::_1,
std::cref(*outer), std::cref(*inner),
std::placeholders::_2);
}
}
}
void onDraw(int loops, SkCanvas* canvas) override {
SkPaint paint;
this->setupPaint(&paint);
for (int j = 0; j < loops; j++) {
for (int i = 0; i < fNumShapes; i++) {
canvas->save();
canvas->setMatrix(fShapes[i].fMatrix);
paint.setColor(fShapes[i].fColor);
fShapes[i].fDraw(canvas, paint);
canvas->restore();
}
}
}
enum {
kBenchWidth = 1000,
kBenchHeight = 1000
};
struct ShapeInfo {
SkMatrix fMatrix;
SkColor fColor;
std::function<void(SkCanvas*, const SkPaint&)> fDraw;
};
ShapesType fShapesType;
ShapesType fInnerShapesType;
int fNumShapes;
SkISize fShapesSize;
bool fPerspective;
SkString fName;
SkRRect fRect;
SkRRect fOval;
SkRRect fRRect;
SkRRect fInnerRect;
SkRRect fInnerOval;
SkRRect fInnerRRect;
SkTArray<ShapeInfo> fShapes;
typedef Benchmark INHERITED;
};
// Small primitives (CPU bound, in theory):
DEF_BENCH(return new ShapesBench(ShapesBench::kRect_ShapesType, ShapesBench::kNone_ShapesType,
10000, SkISize::Make(32, 32), false);)
DEF_BENCH(return new ShapesBench(ShapesBench::kOval_ShapesType, ShapesBench::kNone_ShapesType,
10000, SkISize::Make(32, 32), false);)
DEF_BENCH(return new ShapesBench(ShapesBench::kOval_ShapesType, ShapesBench::kNone_ShapesType,
10000, SkISize::Make(32, 33), false);)
DEF_BENCH(return new ShapesBench(ShapesBench::kRRect_ShapesType, ShapesBench::kNone_ShapesType,
10000, SkISize::Make(32, 32), false);)
DEF_BENCH(return new ShapesBench(ShapesBench::kMixed_ShapesType, ShapesBench::kNone_ShapesType,
10000, SkISize::Make(32, 33), false);)
// Large primitives (GPU bound, in theory):
DEF_BENCH(return new ShapesBench(ShapesBench::kRect_ShapesType, ShapesBench::kNone_ShapesType,
100, SkISize::Make(500, 500), false);)
DEF_BENCH(return new ShapesBench(ShapesBench::kOval_ShapesType, ShapesBench::kNone_ShapesType,
100, SkISize::Make(500, 500), false);)
DEF_BENCH(return new ShapesBench(ShapesBench::kOval_ShapesType, ShapesBench::kNone_ShapesType,
100, SkISize::Make(500, 501), false);)
DEF_BENCH(return new ShapesBench(ShapesBench::kRRect_ShapesType, ShapesBench::kNone_ShapesType,
100, SkISize::Make(500, 500), false);)
DEF_BENCH(return new ShapesBench(ShapesBench::kMixed_ShapesType, ShapesBench::kNone_ShapesType,
100, SkISize::Make(500, 501), false);)
// Donuts (small and large). These fall-back to path rendering due to non-orthogonal rotation
// making them quite slow. Thus, reduce the counts substantially:
DEF_BENCH(return new ShapesBench(ShapesBench::kRect_ShapesType, ShapesBench::kRect_ShapesType,
500, SkISize::Make(32, 32), false);)
DEF_BENCH(return new ShapesBench(ShapesBench::kRRect_ShapesType, ShapesBench::kRRect_ShapesType,
500, SkISize::Make(32, 32), false);)
DEF_BENCH(return new ShapesBench(ShapesBench::kRect_ShapesType, ShapesBench::kRect_ShapesType,
50, SkISize::Make(500, 500), false);)
DEF_BENCH(return new ShapesBench(ShapesBench::kRRect_ShapesType, ShapesBench::kRRect_ShapesType,
50, SkISize::Make(500, 500), false);)
#if ENABLE_COMMAND_LINE_SHAPES_BENCH
DEF_BENCH(return new ShapesBench;)
#endif
|