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