blob: 67e002ba240fe62c380978b59b63e21140c6ec72 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 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 */
bungemand3ebb482015-08-05 13:57:49 -07007
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
reed@google.comd34658a2011-04-11 13:12:51 +00009#include "SkBitmap.h"
10#include "SkCanvas.h"
11#include "SkColorPriv.h"
12#include "SkPaint.h"
bungemand3ebb482015-08-05 13:57:49 -070013#include "SkPath.h"
tomhudson@google.com6e8d3352011-06-22 17:16:35 +000014#include "SkRandom.h"
reed@google.comd34658a2011-04-11 13:12:51 +000015#include "SkShader.h"
16#include "SkString.h"
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +000017#include "SkTArray.h"
18
reed@google.comd34658a2011-04-11 13:12:51 +000019enum Flags {
20 kStroke_Flag = 1 << 0,
21 kBig_Flag = 1 << 1
22};
23
24#define FLAGS00 Flags(0)
25#define FLAGS01 Flags(kStroke_Flag)
26#define FLAGS10 Flags(kBig_Flag)
27#define FLAGS11 Flags(kStroke_Flag | kBig_Flag)
28
tfarinaf168b862014-06-19 12:32:29 -070029class PathBench : public Benchmark {
reed@google.comd34658a2011-04-11 13:12:51 +000030 SkPaint fPaint;
31 SkString fName;
32 Flags fFlags;
reed@google.comd34658a2011-04-11 13:12:51 +000033public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000034 PathBench(Flags flags) : fFlags(flags) {
reed@google.comd34658a2011-04-11 13:12:51 +000035 fPaint.setStyle(flags & kStroke_Flag ? SkPaint::kStroke_Style :
36 SkPaint::kFill_Style);
37 fPaint.setStrokeWidth(SkIntToScalar(5));
38 fPaint.setStrokeJoin(SkPaint::kBevel_Join);
39 }
40
41 virtual void appendName(SkString*) = 0;
42 virtual void makePath(SkPath*) = 0;
tomhudson@google.com6e8d3352011-06-22 17:16:35 +000043 virtual int complexity() { return 0; }
reed@google.comd34658a2011-04-11 13:12:51 +000044
45protected:
mtklein36352bf2015-03-25 18:17:31 -070046 const char* onGetName() override {
reed@google.comd34658a2011-04-11 13:12:51 +000047 fName.printf("path_%s_%s_",
48 fFlags & kStroke_Flag ? "stroke" : "fill",
49 fFlags & kBig_Flag ? "big" : "small");
50 this->appendName(&fName);
51 return fName.c_str();
52 }
53
mtkleina1ebeb22015-10-01 09:43:39 -070054 void onDraw(int loops, SkCanvas* canvas) override {
reed@google.comd34658a2011-04-11 13:12:51 +000055 SkPaint paint(fPaint);
56 this->setupPaint(&paint);
57
58 SkPath path;
59 this->makePath(&path);
60 if (fFlags & kBig_Flag) {
robertphillips1d24b8d2015-03-26 19:57:08 -070061 const SkMatrix m = SkMatrix::MakeScale(SkIntToScalar(10), SkIntToScalar(10));
reed@google.comd34658a2011-04-11 13:12:51 +000062 path.transform(m);
63 }
64
commit-bot@chromium.org33614712013-12-03 18:17:16 +000065 int count = loops;
reed@google.comd34658a2011-04-11 13:12:51 +000066 if (fFlags & kBig_Flag) {
67 count >>= 2;
68 }
tomhudson@google.com6e8d3352011-06-22 17:16:35 +000069 count >>= (3 * complexity());
reed@google.comd34658a2011-04-11 13:12:51 +000070
71 for (int i = 0; i < count; i++) {
72 canvas->drawPath(path, paint);
73 }
74 }
75
76private:
tfarinaf168b862014-06-19 12:32:29 -070077 typedef Benchmark INHERITED;
reed@google.comd34658a2011-04-11 13:12:51 +000078};
79
80class TrianglePathBench : public PathBench {
81public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000082 TrianglePathBench(Flags flags) : INHERITED(flags) {}
rmistry@google.comfbfcd562012-08-23 18:09:54 +000083
mtklein36352bf2015-03-25 18:17:31 -070084 void appendName(SkString* name) override {
reed@google.comd34658a2011-04-11 13:12:51 +000085 name->append("triangle");
86 }
mtklein36352bf2015-03-25 18:17:31 -070087 void makePath(SkPath* path) override {
reed@google.comd34658a2011-04-11 13:12:51 +000088 static const int gCoord[] = {
89 10, 10, 15, 5, 20, 20
90 };
91 path->moveTo(SkIntToScalar(gCoord[0]), SkIntToScalar(gCoord[1]));
92 path->lineTo(SkIntToScalar(gCoord[2]), SkIntToScalar(gCoord[3]));
93 path->lineTo(SkIntToScalar(gCoord[4]), SkIntToScalar(gCoord[5]));
94 path->close();
95 }
96private:
97 typedef PathBench INHERITED;
98};
99
100class RectPathBench : public PathBench {
101public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000102 RectPathBench(Flags flags) : INHERITED(flags) {}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000103
mtklein36352bf2015-03-25 18:17:31 -0700104 void appendName(SkString* name) override {
reed@google.comd34658a2011-04-11 13:12:51 +0000105 name->append("rect");
106 }
mtklein36352bf2015-03-25 18:17:31 -0700107 void makePath(SkPath* path) override {
reed@google.comd34658a2011-04-11 13:12:51 +0000108 SkRect r = { 10, 10, 20, 20 };
109 path->addRect(r);
110 }
111private:
112 typedef PathBench INHERITED;
113};
114
liyuqiana27f6692016-06-20 14:05:27 -0700115class RotatedRectBench : public PathBench {
116public:
117 RotatedRectBench(Flags flags, bool aa, int degrees) : INHERITED(flags) {
118 fAA = aa;
119 fDegrees = degrees;
120 }
121
122 void appendName(SkString* name) override {
123 SkString suffix;
124 suffix.printf("rotated_rect_%s_%d", fAA ? "aa" : "noaa", fDegrees);
125 name->append(suffix);
126 }
127
128 void makePath(SkPath* path) override {
129 SkRect r = { 10, 10, 20, 20 };
130 path->addRect(r);
131 SkMatrix rotateMatrix;
132 rotateMatrix.setRotate((SkScalar)fDegrees);
133 path->transform(rotateMatrix);
134 }
135
136 virtual void setupPaint(SkPaint* paint) override {
137 PathBench::setupPaint(paint);
138 paint->setAntiAlias(fAA);
139 }
140private:
141 typedef PathBench INHERITED;
142 int fDegrees;
143 bool fAA;
144};
145
reed@google.comd34658a2011-04-11 13:12:51 +0000146class OvalPathBench : public PathBench {
147public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000148 OvalPathBench(Flags flags) : INHERITED(flags) {}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000149
mtklein36352bf2015-03-25 18:17:31 -0700150 void appendName(SkString* name) override {
reed@google.comd34658a2011-04-11 13:12:51 +0000151 name->append("oval");
152 }
mtklein36352bf2015-03-25 18:17:31 -0700153 void makePath(SkPath* path) override {
jvanverth@google.come2bfd8b2013-01-24 15:45:35 +0000154 SkRect r = { 10, 10, 23, 20 };
reed@google.comd34658a2011-04-11 13:12:51 +0000155 path->addOval(r);
156 }
157private:
158 typedef PathBench INHERITED;
159};
160
bsalomon@google.com1647a192012-04-11 15:34:46 +0000161class CirclePathBench: public PathBench {
162public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000163 CirclePathBench(Flags flags) : INHERITED(flags) {}
bsalomon@google.com1647a192012-04-11 15:34:46 +0000164
mtklein36352bf2015-03-25 18:17:31 -0700165 void appendName(SkString* name) override {
bsalomon@google.com1647a192012-04-11 15:34:46 +0000166 name->append("circle");
167 }
mtklein36352bf2015-03-25 18:17:31 -0700168 void makePath(SkPath* path) override {
bsalomon@google.com1647a192012-04-11 15:34:46 +0000169 path->addCircle(SkIntToScalar(20), SkIntToScalar(20),
170 SkIntToScalar(10));
171 }
172private:
173 typedef PathBench INHERITED;
174};
175
reed@google.comd34658a2011-04-11 13:12:51 +0000176class SawToothPathBench : public PathBench {
177public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000178 SawToothPathBench(Flags flags) : INHERITED(flags) {}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000179
mtklein36352bf2015-03-25 18:17:31 -0700180 void appendName(SkString* name) override {
reed@google.comd34658a2011-04-11 13:12:51 +0000181 name->append("sawtooth");
182 }
mtkleinf0599002015-07-13 06:18:39 -0700183 void makePath(SkPath* path) override {
reed@google.comd34658a2011-04-11 13:12:51 +0000184 SkScalar x = SkIntToScalar(20);
185 SkScalar y = SkIntToScalar(20);
186 const SkScalar x0 = x;
187 const SkScalar dx = SK_Scalar1 * 5;
188 const SkScalar dy = SK_Scalar1 * 10;
189
190 path->moveTo(x, y);
191 for (int i = 0; i < 32; i++) {
192 x += dx;
193 path->lineTo(x, y - dy);
194 x += dx;
195 path->lineTo(x, y + dy);
196 }
197 path->lineTo(x, y + 2 * dy);
198 path->lineTo(x0, y + 2 * dy);
199 path->close();
200 }
mtklein36352bf2015-03-25 18:17:31 -0700201 int complexity() override { return 1; }
reed@google.comd34658a2011-04-11 13:12:51 +0000202private:
203 typedef PathBench INHERITED;
204};
205
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000206class LongCurvedPathBench : public PathBench {
207public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000208 LongCurvedPathBench(Flags flags) : INHERITED(flags) {}
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000209
mtklein36352bf2015-03-25 18:17:31 -0700210 void appendName(SkString* name) override {
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000211 name->append("long_curved");
212 }
mtklein36352bf2015-03-25 18:17:31 -0700213 void makePath(SkPath* path) override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000214 SkRandom rand (12);
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000215 int i;
216 for (i = 0; i < 100; i++) {
217 path->quadTo(SkScalarMul(rand.nextUScalar1(), SkIntToScalar(640)),
218 SkScalarMul(rand.nextUScalar1(), SkIntToScalar(480)),
219 SkScalarMul(rand.nextUScalar1(), SkIntToScalar(640)),
220 SkScalarMul(rand.nextUScalar1(), SkIntToScalar(480)));
221 }
222 path->close();
223 }
mtklein36352bf2015-03-25 18:17:31 -0700224 int complexity() override { return 2; }
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000225private:
226 typedef PathBench INHERITED;
227};
228
senorblanco@chromium.orge50f7362012-01-12 19:10:35 +0000229class LongLinePathBench : public PathBench {
230public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000231 LongLinePathBench(Flags flags) : INHERITED(flags) {}
senorblanco@chromium.orge50f7362012-01-12 19:10:35 +0000232
mtklein36352bf2015-03-25 18:17:31 -0700233 void appendName(SkString* name) override {
senorblanco@chromium.orge50f7362012-01-12 19:10:35 +0000234 name->append("long_line");
235 }
mtklein36352bf2015-03-25 18:17:31 -0700236 void makePath(SkPath* path) override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000237 SkRandom rand;
senorblanco@chromium.orge50f7362012-01-12 19:10:35 +0000238 path->moveTo(rand.nextUScalar1() * 640, rand.nextUScalar1() * 480);
239 for (size_t i = 1; i < 100; i++) {
240 path->lineTo(rand.nextUScalar1() * 640, rand.nextUScalar1() * 480);
241 }
242 }
mtklein36352bf2015-03-25 18:17:31 -0700243 int complexity() override { return 2; }
senorblanco@chromium.orge50f7362012-01-12 19:10:35 +0000244private:
245 typedef PathBench INHERITED;
246};
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000247
tfarinaf168b862014-06-19 12:32:29 -0700248class RandomPathBench : public Benchmark {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000249public:
mtklein36352bf2015-03-25 18:17:31 -0700250 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000251 return backend == kNonRendering_Backend;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000252 }
253
254protected:
255 void createData(int minVerbs,
256 int maxVerbs,
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000257 bool allowMoves = true,
halcanary96fcdcc2015-08-27 07:41:13 -0700258 SkRect* bounds = nullptr) {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000259 SkRect tempBounds;
halcanary96fcdcc2015-08-27 07:41:13 -0700260 if (nullptr == bounds) {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000261 tempBounds.setXYWH(0, 0, SK_Scalar1, SK_Scalar1);
262 bounds = &tempBounds;
263 }
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000264 fVerbCnts.reset(kNumVerbCnts);
265 for (int i = 0; i < kNumVerbCnts; ++i) {
266 fVerbCnts[i] = fRandom.nextRangeU(minVerbs, maxVerbs + 1);
267 }
268 fVerbs.reset(kNumVerbs);
269 for (int i = 0; i < kNumVerbs; ++i) {
270 do {
271 fVerbs[i] = static_cast<SkPath::Verb>(fRandom.nextULessThan(SkPath::kDone_Verb));
272 } while (!allowMoves && SkPath::kMove_Verb == fVerbs[i]);
273 }
274 fPoints.reset(kNumPoints);
275 for (int i = 0; i < kNumPoints; ++i) {
276 fPoints[i].set(fRandom.nextRangeScalar(bounds->fLeft, bounds->fRight),
277 fRandom.nextRangeScalar(bounds->fTop, bounds->fBottom));
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000278 }
279 this->restartMakingPaths();
280 }
281
282 void restartMakingPaths() {
283 fCurrPath = 0;
284 fCurrVerb = 0;
285 fCurrPoint = 0;
286 }
287
288 void makePath(SkPath* path) {
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000289 int vCount = fVerbCnts[(fCurrPath++) & (kNumVerbCnts - 1)];
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000290 for (int v = 0; v < vCount; ++v) {
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000291 int verb = fVerbs[(fCurrVerb++) & (kNumVerbs - 1)];
292 switch (verb) {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000293 case SkPath::kMove_Verb:
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000294 path->moveTo(fPoints[(fCurrPoint++) & (kNumPoints - 1)]);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000295 break;
296 case SkPath::kLine_Verb:
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000297 path->lineTo(fPoints[(fCurrPoint++) & (kNumPoints - 1)]);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000298 break;
299 case SkPath::kQuad_Verb:
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000300 path->quadTo(fPoints[(fCurrPoint + 0) & (kNumPoints - 1)],
301 fPoints[(fCurrPoint + 1) & (kNumPoints - 1)]);
302 fCurrPoint += 2;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000303 break;
reed@google.com277c3f82013-05-31 15:17:50 +0000304 case SkPath::kConic_Verb:
305 path->conicTo(fPoints[(fCurrPoint + 0) & (kNumPoints - 1)],
306 fPoints[(fCurrPoint + 1) & (kNumPoints - 1)],
307 SK_ScalarHalf);
308 fCurrPoint += 2;
309 break;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000310 case SkPath::kCubic_Verb:
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000311 path->cubicTo(fPoints[(fCurrPoint + 0) & (kNumPoints - 1)],
312 fPoints[(fCurrPoint + 1) & (kNumPoints - 1)],
313 fPoints[(fCurrPoint + 2) & (kNumPoints - 1)]);
314 fCurrPoint += 3;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000315 break;
316 case SkPath::kClose_Verb:
317 path->close();
318 break;
319 default:
320 SkDEBUGFAIL("Unexpected path verb");
321 break;
322 }
323 }
324 }
325
326 void finishedMakingPaths() {
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000327 fVerbCnts.reset(0);
328 fVerbs.reset(0);
329 fPoints.reset(0);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000330 }
331
332private:
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000333 enum {
334 // these should all be pow 2
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000335 kNumVerbCnts = 1 << 5,
336 kNumVerbs = 1 << 5,
337 kNumPoints = 1 << 5,
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000338 };
339 SkAutoTArray<int> fVerbCnts;
340 SkAutoTArray<SkPath::Verb> fVerbs;
341 SkAutoTArray<SkPoint> fPoints;
342 int fCurrPath;
343 int fCurrVerb;
344 int fCurrPoint;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000345 SkRandom fRandom;
tfarinaf168b862014-06-19 12:32:29 -0700346 typedef Benchmark INHERITED;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000347};
348
349class PathCreateBench : public RandomPathBench {
350public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000351 PathCreateBench() {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000352 }
353
354protected:
mtklein36352bf2015-03-25 18:17:31 -0700355 const char* onGetName() override {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000356 return "path_create";
357 }
358
joshualitt8a6697a2015-09-30 12:11:07 -0700359 void onDelayedSetup() override {
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000360 this->createData(10, 100);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000361 }
362
mtkleina1ebeb22015-10-01 09:43:39 -0700363 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000364 for (int i = 0; i < loops; ++i) {
commit-bot@chromium.org8f881172014-01-06 20:19:14 +0000365 if (i % 1000 == 0) {
366 fPath.reset(); // PathRef memory can grow without bound otherwise.
367 }
368 this->makePath(&fPath);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000369 }
370 this->restartMakingPaths();
371 }
372
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000373private:
commit-bot@chromium.org8f881172014-01-06 20:19:14 +0000374 SkPath fPath;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000375
376 typedef RandomPathBench INHERITED;
377};
378
379class PathCopyBench : public RandomPathBench {
380public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000381 PathCopyBench() {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000382 }
383
384protected:
mtklein36352bf2015-03-25 18:17:31 -0700385 const char* onGetName() override {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000386 return "path_copy";
387 }
joshualitt8a6697a2015-09-30 12:11:07 -0700388 void onDelayedSetup() override {
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000389 this->createData(10, 100);
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000390 fPaths.reset(kPathCnt);
391 fCopies.reset(kPathCnt);
392 for (int i = 0; i < kPathCnt; ++i) {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000393 this->makePath(&fPaths[i]);
394 }
395 this->finishedMakingPaths();
396 }
mtkleina1ebeb22015-10-01 09:43:39 -0700397 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000398 for (int i = 0; i < loops; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000399 int idx = i & (kPathCnt - 1);
400 fCopies[idx] = fPaths[idx];
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000401 }
402 }
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000403
404private:
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000405 enum {
406 // must be a pow 2
407 kPathCnt = 1 << 5,
408 };
409 SkAutoTArray<SkPath> fPaths;
410 SkAutoTArray<SkPath> fCopies;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000411
412 typedef RandomPathBench INHERITED;
413};
414
415class PathTransformBench : public RandomPathBench {
416public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000417 PathTransformBench(bool inPlace) : fInPlace(inPlace) {}
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000418
419protected:
mtklein36352bf2015-03-25 18:17:31 -0700420 const char* onGetName() override {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000421 return fInPlace ? "path_transform_in_place" : "path_transform_copy";
422 }
423
joshualitt8a6697a2015-09-30 12:11:07 -0700424 void onDelayedSetup() override {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000425 fMatrix.setScale(5 * SK_Scalar1, 6 * SK_Scalar1);
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000426 this->createData(10, 100);
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000427 fPaths.reset(kPathCnt);
428 for (int i = 0; i < kPathCnt; ++i) {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000429 this->makePath(&fPaths[i]);
430 }
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000431 this->finishedMakingPaths();
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000432 if (!fInPlace) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000433 fTransformed.reset(kPathCnt);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000434 }
435 }
436
mtkleina1ebeb22015-10-01 09:43:39 -0700437 void onDraw(int loops, SkCanvas*) override {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000438 if (fInPlace) {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000439 for (int i = 0; i < loops; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000440 fPaths[i & (kPathCnt - 1)].transform(fMatrix);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000441 }
442 } else {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000443 for (int i = 0; i < loops; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000444 int idx = i & (kPathCnt - 1);
445 fPaths[idx].transform(fMatrix, &fTransformed[idx]);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000446 }
447 }
448 }
449
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000450private:
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000451 enum {
452 // must be a pow 2
453 kPathCnt = 1 << 5,
454 };
455 SkAutoTArray<SkPath> fPaths;
456 SkAutoTArray<SkPath> fTransformed;
457
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000458 SkMatrix fMatrix;
459 bool fInPlace;
460 typedef RandomPathBench INHERITED;
461};
462
463class PathEqualityBench : public RandomPathBench {
464public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000465 PathEqualityBench() { }
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000466
467protected:
mtklein36352bf2015-03-25 18:17:31 -0700468 const char* onGetName() override {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000469 return "path_equality_50%";
470 }
471
joshualitt8a6697a2015-09-30 12:11:07 -0700472 void onDelayedSetup() override {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000473 fParity = 0;
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000474 this->createData(10, 100);
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000475 fPaths.reset(kPathCnt);
476 fCopies.reset(kPathCnt);
477 for (int i = 0; i < kPathCnt; ++i) {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000478 this->makePath(&fPaths[i]);
479 fCopies[i] = fPaths[i];
480 }
481 this->finishedMakingPaths();
482 }
483
mtkleina1ebeb22015-10-01 09:43:39 -0700484 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000485 for (int i = 0; i < loops; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000486 int idx = i & (kPathCnt - 1);
487 fParity ^= (fPaths[idx] == fCopies[idx & ~0x1]);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000488 }
489 }
490
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000491private:
492 bool fParity; // attempt to keep compiler from optimizing out the ==
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000493 enum {
494 // must be a pow 2
495 kPathCnt = 1 << 5,
496 };
497 SkAutoTArray<SkPath> fPaths;
498 SkAutoTArray<SkPath> fCopies;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000499 typedef RandomPathBench INHERITED;
500};
501
502class SkBench_AddPathTest : public RandomPathBench {
503public:
504 enum AddType {
505 kAdd_AddType,
506 kAddTrans_AddType,
507 kAddMatrix_AddType,
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000508 kReverseAdd_AddType,
509 kReversePathTo_AddType,
510 };
511
mtklein@google.com410e6e82013-09-13 19:52:27 +0000512 SkBench_AddPathTest(AddType type) : fType(type) {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000513 fMatrix.setRotate(60 * SK_Scalar1);
514 }
515
516protected:
mtklein36352bf2015-03-25 18:17:31 -0700517 const char* onGetName() override {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000518 switch (fType) {
519 case kAdd_AddType:
520 return "path_add_path";
521 case kAddTrans_AddType:
522 return "path_add_path_trans";
523 case kAddMatrix_AddType:
524 return "path_add_path_matrix";
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000525 case kReverseAdd_AddType:
526 return "path_reverse_add_path";
527 case kReversePathTo_AddType:
528 return "path_reverse_path_to";
529 default:
530 SkDEBUGFAIL("Bad add type");
531 return "";
532 }
533 }
534
joshualitt8a6697a2015-09-30 12:11:07 -0700535 void onDelayedSetup() override {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000536 // reversePathTo assumes a single contour path.
537 bool allowMoves = kReversePathTo_AddType != fType;
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000538 this->createData(10, 100, allowMoves);
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000539 fPaths0.reset(kPathCnt);
540 fPaths1.reset(kPathCnt);
541 for (int i = 0; i < kPathCnt; ++i) {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000542 this->makePath(&fPaths0[i]);
543 this->makePath(&fPaths1[i]);
544 }
545 this->finishedMakingPaths();
546 }
547
mtkleina1ebeb22015-10-01 09:43:39 -0700548 void onDraw(int loops, SkCanvas*) override {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000549 switch (fType) {
550 case kAdd_AddType:
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000551 for (int i = 0; i < loops; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000552 int idx = i & (kPathCnt - 1);
553 SkPath result = fPaths0[idx];
554 result.addPath(fPaths1[idx]);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000555 }
556 break;
557 case kAddTrans_AddType:
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000558 for (int i = 0; i < loops; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000559 int idx = i & (kPathCnt - 1);
560 SkPath result = fPaths0[idx];
561 result.addPath(fPaths1[idx], 2 * SK_Scalar1, 5 * SK_Scalar1);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000562 }
563 break;
564 case kAddMatrix_AddType:
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000565 for (int i = 0; i < loops; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000566 int idx = i & (kPathCnt - 1);
567 SkPath result = fPaths0[idx];
568 result.addPath(fPaths1[idx], fMatrix);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000569 }
570 break;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000571 case kReverseAdd_AddType:
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000572 for (int i = 0; i < loops; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000573 int idx = i & (kPathCnt - 1);
574 SkPath result = fPaths0[idx];
575 result.reverseAddPath(fPaths1[idx]);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000576 }
577 break;
578 case kReversePathTo_AddType:
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000579 for (int i = 0; i < loops; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000580 int idx = i & (kPathCnt - 1);
581 SkPath result = fPaths0[idx];
582 result.reversePathTo(fPaths1[idx]);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000583 }
584 break;
585 }
586 }
587
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000588private:
589 AddType fType; // or reverseAddPath
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000590 enum {
591 // must be a pow 2
592 kPathCnt = 1 << 5,
593 };
594 SkAutoTArray<SkPath> fPaths0;
595 SkAutoTArray<SkPath> fPaths1;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000596 SkMatrix fMatrix;
597 typedef RandomPathBench INHERITED;
598};
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000599
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000600
tfarinaf168b862014-06-19 12:32:29 -0700601class CirclesBench : public Benchmark {
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000602protected:
603 SkString fName;
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000604 Flags fFlags;
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000605
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000606public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000607 CirclesBench(Flags flags) : fFlags(flags) {
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000608 fName.printf("circles_%s", fFlags & kStroke_Flag ? "stroke" : "fill");
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000609 }
610
611protected:
mtklein36352bf2015-03-25 18:17:31 -0700612 const char* onGetName() override {
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000613 return fName.c_str();
614 }
615
mtkleina1ebeb22015-10-01 09:43:39 -0700616 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000617 SkPaint paint;
618
619 paint.setColor(SK_ColorBLACK);
620 paint.setAntiAlias(true);
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000621 if (fFlags & kStroke_Flag) {
622 paint.setStyle(SkPaint::kStroke_Style);
623 }
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000624
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000625 SkRandom rand;
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000626
627 SkRect r;
628
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000629 for (int i = 0; i < loops; ++i) {
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000630 SkScalar radius = rand.nextUScalar1() * 3;
631 r.fLeft = rand.nextUScalar1() * 300;
632 r.fTop = rand.nextUScalar1() * 300;
633 r.fRight = r.fLeft + 2 * radius;
634 r.fBottom = r.fTop + 2 * radius;
635
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000636 if (fFlags & kStroke_Flag) {
637 paint.setStrokeWidth(rand.nextUScalar1() * 5.0f);
638 }
639
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000640 SkPath temp;
641
642 // mimic how Chrome does circles
643 temp.arcTo(r, 0, 0, false);
644 temp.addOval(r, SkPath::kCCW_Direction);
645 temp.arcTo(r, 360, 0, true);
646 temp.close();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000647
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000648 canvas->drawPath(temp, paint);
649 }
650 }
651
652private:
tfarinaf168b862014-06-19 12:32:29 -0700653 typedef Benchmark INHERITED;
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000654};
655
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000656
robertphillips@google.com158618e2012-10-23 16:56:56 +0000657// Chrome creates its own round rects with each corner possibly being different.
658// In its "zero radius" incarnation it creates degenerate round rects.
skia.committer@gmail.com1e34ff72012-10-24 02:01:24 +0000659// Note: PathTest::test_arb_round_rect_is_convex and
robertphillips@google.com158618e2012-10-23 16:56:56 +0000660// test_arb_zero_rad_round_rect_is_rect perform almost exactly
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000661// the same test (but with no drawing)
tfarinaf168b862014-06-19 12:32:29 -0700662class ArbRoundRectBench : public Benchmark {
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000663protected:
664 SkString fName;
665
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000666public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000667 ArbRoundRectBench(bool zeroRad) : fZeroRad(zeroRad) {
robertphillips@google.com158618e2012-10-23 16:56:56 +0000668 if (zeroRad) {
669 fName.printf("zeroradroundrect");
670 } else {
671 fName.printf("arbroundrect");
672 }
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000673 }
674
675protected:
mtklein36352bf2015-03-25 18:17:31 -0700676 const char* onGetName() override {
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000677 return fName.c_str();
678 }
679
skia.committer@gmail.com989a95e2012-10-18 02:01:23 +0000680 static void add_corner_arc(SkPath* path, const SkRect& rect,
681 SkScalar xIn, SkScalar yIn,
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000682 int startAngle)
683 {
684
685 SkScalar rx = SkMinScalar(rect.width(), xIn);
686 SkScalar ry = SkMinScalar(rect.height(), yIn);
687
688 SkRect arcRect;
689 arcRect.set(-rx, -ry, rx, ry);
690 switch (startAngle) {
691 case 0:
692 arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom);
693 break;
694 case 90:
695 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom);
696 break;
697 case 180:
698 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop);
699 break;
700 case 270:
701 arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop);
702 break;
703 default:
704 break;
705 }
706
707 path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false);
708 }
709
skia.committer@gmail.com989a95e2012-10-18 02:01:23 +0000710 static void make_arb_round_rect(SkPath* path, const SkRect& r,
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000711 SkScalar xCorner, SkScalar yCorner) {
712 // we are lazy here and use the same x & y for each corner
713 add_corner_arc(path, r, xCorner, yCorner, 270);
714 add_corner_arc(path, r, xCorner, yCorner, 0);
715 add_corner_arc(path, r, xCorner, yCorner, 90);
716 add_corner_arc(path, r, xCorner, yCorner, 180);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000717 path->close();
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000718
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000719 SkASSERT(path->isConvex());
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000720 }
721
mtkleina1ebeb22015-10-01 09:43:39 -0700722 void onDraw(int loops, SkCanvas* canvas) override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000723 SkRandom rand;
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000724 SkRect r;
725
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000726 for (int i = 0; i < loops; ++i) {
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000727 SkPaint paint;
728 paint.setColor(0xff000000 | rand.nextU());
729 paint.setAntiAlias(true);
730
robertphillips@google.com158618e2012-10-23 16:56:56 +0000731 SkScalar size = rand.nextUScalar1() * 30;
732 if (size < SK_Scalar1) {
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000733 continue;
734 }
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000735 r.fLeft = rand.nextUScalar1() * 300;
736 r.fTop = rand.nextUScalar1() * 300;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000737 r.fRight = r.fLeft + 2 * size;
738 r.fBottom = r.fTop + 2 * size;
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000739
740 SkPath temp;
741
robertphillips@google.com158618e2012-10-23 16:56:56 +0000742 if (fZeroRad) {
743 make_arb_round_rect(&temp, r, 0, 0);
744
halcanary96fcdcc2015-08-27 07:41:13 -0700745 SkASSERT(temp.isRect(nullptr));
robertphillips@google.com158618e2012-10-23 16:56:56 +0000746 } else {
747 make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15);
748 }
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000749
750 canvas->drawPath(temp, paint);
751 }
752 }
753
754private:
robertphillips@google.com158618e2012-10-23 16:56:56 +0000755 bool fZeroRad; // should 0 radius rounds rects be tested?
756
tfarinaf168b862014-06-19 12:32:29 -0700757 typedef Benchmark INHERITED;
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000758};
759
tfarinaf168b862014-06-19 12:32:29 -0700760class ConservativelyContainsBench : public Benchmark {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +0000761public:
762 enum Type {
763 kRect_Type,
764 kRoundRect_Type,
765 kOval_Type,
766 };
767
mtklein@google.com410e6e82013-09-13 19:52:27 +0000768 ConservativelyContainsBench(Type type) {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +0000769 fParity = false;
770 fName = "conservatively_contains_";
771 switch (type) {
772 case kRect_Type:
773 fName.append("rect");
774 fPath.addRect(kBaseRect);
775 break;
776 case kRoundRect_Type:
777 fName.append("round_rect");
778 fPath.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1]);
779 break;
780 case kOval_Type:
781 fName.append("oval");
782 fPath.addOval(kBaseRect);
783 break;
784 }
785 }
786
mtklein36352bf2015-03-25 18:17:31 -0700787 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000788 return backend == kNonRendering_Backend;
789 }
790
bsalomon@google.com9bee33a2012-11-13 21:51:38 +0000791private:
mtklein36352bf2015-03-25 18:17:31 -0700792 const char* onGetName() override {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +0000793 return fName.c_str();
794 }
795
mtkleina1ebeb22015-10-01 09:43:39 -0700796 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000797 for (int i = 0; i < loops; ++i) {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +0000798 const SkRect& rect = fQueryRects[i % kQueryRectCnt];
799 fParity = fParity != fPath.conservativelyContainsRect(rect);
800 }
801 }
802
joshualitt8a6697a2015-09-30 12:11:07 -0700803 void onDelayedSetup() override {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +0000804 fQueryRects.setCount(kQueryRectCnt);
805
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000806 SkRandom rand;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +0000807 for (int i = 0; i < kQueryRectCnt; ++i) {
808 SkSize size;
809 SkPoint xy;
810 size.fWidth = rand.nextRangeScalar(kQueryMin.fWidth, kQueryMax.fWidth);
811 size.fHeight = rand.nextRangeScalar(kQueryMin.fHeight, kQueryMax.fHeight);
812 xy.fX = rand.nextRangeScalar(kBounds.fLeft, kBounds.fRight - size.fWidth);
813 xy.fY = rand.nextRangeScalar(kBounds.fTop, kBounds.fBottom - size.fHeight);
814
815 fQueryRects[i] = SkRect::MakeXYWH(xy.fX, xy.fY, size.fWidth, size.fHeight);
816 }
817 }
818
bsalomon@google.com9bee33a2012-11-13 21:51:38 +0000819 enum {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +0000820 kQueryRectCnt = 400,
821 };
822 static const SkRect kBounds; // bounds for all random query rects
823 static const SkSize kQueryMin; // minimum query rect size, should be <= kQueryMax
824 static const SkSize kQueryMax; // max query rect size, should < kBounds
825 static const SkRect kBaseRect; // rect that is used to construct the path
826 static const SkScalar kRRRadii[2]; // x and y radii for round rect
827
828 SkString fName;
829 SkPath fPath;
830 bool fParity;
831 SkTDArray<SkRect> fQueryRects;
832
tfarinaf168b862014-06-19 12:32:29 -0700833 typedef Benchmark INHERITED;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +0000834};
835
mike@reedtribe.org8d551012013-04-14 02:40:50 +0000836///////////////////////////////////////////////////////////////////////////////
837
838#include "SkGeometry.h"
839
reed55011032015-03-26 09:10:22 -0700840class ConicBench_Chop : public Benchmark {
reedb6402032015-03-20 13:23:43 -0700841protected:
842 SkConic fRQ, fDst[2];
843 SkString fName;
mike@reedtribe.org8d551012013-04-14 02:40:50 +0000844public:
reed55011032015-03-26 09:10:22 -0700845 ConicBench_Chop() : fName("conic-chop") {
mike@reedtribe.org8d551012013-04-14 02:40:50 +0000846 fRQ.fPts[0].set(0, 0);
847 fRQ.fPts[1].set(100, 0);
848 fRQ.fPts[2].set(100, 100);
849 fRQ.fW = SkScalarCos(SK_ScalarPI/4);
reedb6402032015-03-20 13:23:43 -0700850 }
851
mtklein36352bf2015-03-25 18:17:31 -0700852 bool isSuitableFor(Backend backend) override {
reedb6402032015-03-20 13:23:43 -0700853 return backend == kNonRendering_Backend;
mike@reedtribe.org8d551012013-04-14 02:40:50 +0000854 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000855
mike@reedtribe.org8d551012013-04-14 02:40:50 +0000856private:
mtklein36352bf2015-03-25 18:17:31 -0700857 const char* onGetName() override { return fName.c_str(); }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000858
mtkleina1ebeb22015-10-01 09:43:39 -0700859 void onDraw(int loops, SkCanvas*) override {
reed55011032015-03-26 09:10:22 -0700860 for (int i = 0; i < loops; ++i) {
861 fRQ.chop(fDst);
mike@reedtribe.org8d551012013-04-14 02:40:50 +0000862 }
863 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000864
tfarinaf168b862014-06-19 12:32:29 -0700865 typedef Benchmark INHERITED;
mike@reedtribe.org8d551012013-04-14 02:40:50 +0000866};
reed55011032015-03-26 09:10:22 -0700867DEF_BENCH( return new ConicBench_Chop; )
reedb6402032015-03-20 13:23:43 -0700868
reed55011032015-03-26 09:10:22 -0700869class ConicBench_EvalPos : public ConicBench_Chop {
870 const bool fUseV2;
reedb6402032015-03-20 13:23:43 -0700871public:
reed55011032015-03-26 09:10:22 -0700872 ConicBench_EvalPos(bool useV2) : fUseV2(useV2) {
reedb6402032015-03-20 13:23:43 -0700873 fName.printf("conic-eval-pos%d", useV2);
874 }
mtkleina1ebeb22015-10-01 09:43:39 -0700875 void onDraw(int loops, SkCanvas*) override {
reedb6402032015-03-20 13:23:43 -0700876 if (fUseV2) {
877 for (int i = 0; i < loops; ++i) {
878 for (int j = 0; j < 1000; ++j) {
879 fDst[0].fPts[0] = fRQ.evalAt(0.4f);
880 }
881 }
882 } else {
883 for (int i = 0; i < loops; ++i) {
884 for (int j = 0; j < 1000; ++j) {
halcanary96fcdcc2015-08-27 07:41:13 -0700885 fRQ.evalAt(0.4f, &fDst[0].fPts[0], nullptr);
reedb6402032015-03-20 13:23:43 -0700886 }
887 }
888 }
889 }
890};
891DEF_BENCH( return new ConicBench_EvalPos(false); )
892DEF_BENCH( return new ConicBench_EvalPos(true); )
893
reed55011032015-03-26 09:10:22 -0700894class ConicBench_EvalTan : public ConicBench_Chop {
895 const bool fUseV2;
reedb6402032015-03-20 13:23:43 -0700896public:
reed55011032015-03-26 09:10:22 -0700897 ConicBench_EvalTan(bool useV2) : fUseV2(useV2) {
reedb6402032015-03-20 13:23:43 -0700898 fName.printf("conic-eval-tan%d", useV2);
899 }
mtkleina1ebeb22015-10-01 09:43:39 -0700900 void onDraw(int loops, SkCanvas*) override {
reedb6402032015-03-20 13:23:43 -0700901 if (fUseV2) {
902 for (int i = 0; i < loops; ++i) {
903 for (int j = 0; j < 1000; ++j) {
904 fDst[0].fPts[0] = fRQ.evalTangentAt(0.4f);
905 }
906 }
907 } else {
908 for (int i = 0; i < loops; ++i) {
909 for (int j = 0; j < 1000; ++j) {
halcanary96fcdcc2015-08-27 07:41:13 -0700910 fRQ.evalAt(0.4f, nullptr, &fDst[0].fPts[0]);
reedb6402032015-03-20 13:23:43 -0700911 }
912 }
913 }
914 }
915};
916DEF_BENCH( return new ConicBench_EvalTan(false); )
917DEF_BENCH( return new ConicBench_EvalTan(true); )
mike@reedtribe.org8d551012013-04-14 02:40:50 +0000918
919///////////////////////////////////////////////////////////////////////////////
920
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000921static void rand_conic(SkConic* conic, SkRandom& rand) {
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000922 for (int i = 0; i < 3; ++i) {
923 conic->fPts[i].set(rand.nextUScalar1() * 100, rand.nextUScalar1() * 100);
924 }
925 if (rand.nextUScalar1() > 0.5f) {
926 conic->fW = rand.nextUScalar1();
927 } else {
928 conic->fW = 1 + rand.nextUScalar1() * 4;
929 }
930}
931
tfarinaf168b862014-06-19 12:32:29 -0700932class ConicBench : public Benchmark {
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000933public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000934 ConicBench() {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000935 SkRandom rand;
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000936 for (int i = 0; i < CONICS; ++i) {
937 rand_conic(&fConics[i], rand);
938 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000939 }
940
mtklein36352bf2015-03-25 18:17:31 -0700941 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000942 return backend == kNonRendering_Backend;
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000943 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000944
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000945protected:
946 enum {
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000947 CONICS = 100
948 };
949 SkConic fConics[CONICS];
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000950
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000951private:
tfarinaf168b862014-06-19 12:32:29 -0700952 typedef Benchmark INHERITED;
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000953};
954
955class ConicBench_ComputeError : public ConicBench {
956public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000957 ConicBench_ComputeError() {}
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000958
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000959protected:
mtklein36352bf2015-03-25 18:17:31 -0700960 const char* onGetName() override {
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000961 return "conic-compute-error";
962 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000963
mtkleina1ebeb22015-10-01 09:43:39 -0700964 void onDraw(int loops, SkCanvas*) override {
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000965 SkVector err;
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000966 for (int i = 0; i < loops; ++i) {
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000967 for (int j = 0; j < CONICS; ++j) {
968 fConics[j].computeAsQuadError(&err);
969 }
970 }
971 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000972
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000973private:
974 typedef ConicBench INHERITED;
975};
976
977class ConicBench_asQuadTol : public ConicBench {
978public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000979 ConicBench_asQuadTol() {}
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000980
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000981protected:
mtklein36352bf2015-03-25 18:17:31 -0700982 const char* onGetName() override {
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000983 return "conic-asQuadTol";
984 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000985
mtkleina1ebeb22015-10-01 09:43:39 -0700986 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000987 for (int i = 0; i < loops; ++i) {
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000988 for (int j = 0; j < CONICS; ++j) {
989 fConics[j].asQuadTol(SK_ScalarHalf);
990 }
991 }
992 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000993
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000994private:
995 typedef ConicBench INHERITED;
996};
997
998class ConicBench_quadPow2 : public ConicBench {
999public:
mtklein@google.com410e6e82013-09-13 19:52:27 +00001000 ConicBench_quadPow2() {}
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +00001001
1002protected:
mtklein36352bf2015-03-25 18:17:31 -07001003 const char* onGetName() override {
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +00001004 return "conic-quadPow2";
1005 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +00001006
mtkleina1ebeb22015-10-01 09:43:39 -07001007 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +00001008 for (int i = 0; i < loops; ++i) {
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +00001009 for (int j = 0; j < CONICS; ++j) {
1010 fConics[j].computeQuadPOW2(SK_ScalarHalf);
1011 }
1012 }
1013 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +00001014
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +00001015private:
1016 typedef ConicBench INHERITED;
1017};
1018
1019///////////////////////////////////////////////////////////////////////////////
1020
bsalomon@google.com9bee33a2012-11-13 21:51:38 +00001021const SkRect ConservativelyContainsBench::kBounds = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
1022const SkSize ConservativelyContainsBench::kQueryMin = SkSize::Make(SkIntToScalar(1), SkIntToScalar(1));
1023const SkSize ConservativelyContainsBench::kQueryMax = SkSize::Make(SkIntToScalar(40), SkIntToScalar(40));
1024const SkRect ConservativelyContainsBench::kBaseRect = SkRect::MakeXYWH(SkIntToScalar(25), SkIntToScalar(25), SkIntToScalar(50), SkIntToScalar(50));
1025const SkScalar ConservativelyContainsBench::kRRRadii[2] = {SkIntToScalar(5), SkIntToScalar(10)};
1026
mtklein@google.com410e6e82013-09-13 19:52:27 +00001027DEF_BENCH( return new TrianglePathBench(FLAGS00); )
1028DEF_BENCH( return new TrianglePathBench(FLAGS01); )
1029DEF_BENCH( return new TrianglePathBench(FLAGS10); )
1030DEF_BENCH( return new TrianglePathBench(FLAGS11); )
reed@google.comd34658a2011-04-11 13:12:51 +00001031
mtklein@google.com410e6e82013-09-13 19:52:27 +00001032DEF_BENCH( return new RectPathBench(FLAGS00); )
1033DEF_BENCH( return new RectPathBench(FLAGS01); )
1034DEF_BENCH( return new RectPathBench(FLAGS10); )
1035DEF_BENCH( return new RectPathBench(FLAGS11); )
reed@google.comd34658a2011-04-11 13:12:51 +00001036
liyuqiana27f6692016-06-20 14:05:27 -07001037DEF_BENCH( return new RotatedRectBench(FLAGS00, false, 45));
1038DEF_BENCH( return new RotatedRectBench(FLAGS10, false, 45));
1039DEF_BENCH( return new RotatedRectBench(FLAGS00, true, 45));
1040DEF_BENCH( return new RotatedRectBench(FLAGS10, true, 45));
1041
mtklein@google.com410e6e82013-09-13 19:52:27 +00001042DEF_BENCH( return new OvalPathBench(FLAGS00); )
1043DEF_BENCH( return new OvalPathBench(FLAGS01); )
1044DEF_BENCH( return new OvalPathBench(FLAGS10); )
1045DEF_BENCH( return new OvalPathBench(FLAGS11); )
reed@google.comd34658a2011-04-11 13:12:51 +00001046
mtklein@google.com410e6e82013-09-13 19:52:27 +00001047DEF_BENCH( return new CirclePathBench(FLAGS00); )
1048DEF_BENCH( return new CirclePathBench(FLAGS01); )
1049DEF_BENCH( return new CirclePathBench(FLAGS10); )
1050DEF_BENCH( return new CirclePathBench(FLAGS11); )
bsalomon@google.com1647a192012-04-11 15:34:46 +00001051
mtklein@google.com410e6e82013-09-13 19:52:27 +00001052DEF_BENCH( return new SawToothPathBench(FLAGS00); )
1053DEF_BENCH( return new SawToothPathBench(FLAGS01); )
reed@google.comd34658a2011-04-11 13:12:51 +00001054
mtklein@google.com410e6e82013-09-13 19:52:27 +00001055DEF_BENCH( return new LongCurvedPathBench(FLAGS00); )
1056DEF_BENCH( return new LongCurvedPathBench(FLAGS01); )
1057DEF_BENCH( return new LongLinePathBench(FLAGS00); )
1058DEF_BENCH( return new LongLinePathBench(FLAGS01); )
tomhudson@google.com6e8d3352011-06-22 17:16:35 +00001059
mtklein@google.com410e6e82013-09-13 19:52:27 +00001060DEF_BENCH( return new PathCreateBench(); )
1061DEF_BENCH( return new PathCopyBench(); )
1062DEF_BENCH( return new PathTransformBench(true); )
1063DEF_BENCH( return new PathTransformBench(false); )
1064DEF_BENCH( return new PathEqualityBench(); )
senorblanco@chromium.orge50f7362012-01-12 19:10:35 +00001065
mtklein@google.com410e6e82013-09-13 19:52:27 +00001066DEF_BENCH( return new SkBench_AddPathTest(SkBench_AddPathTest::kAdd_AddType); )
1067DEF_BENCH( return new SkBench_AddPathTest(SkBench_AddPathTest::kAddTrans_AddType); )
1068DEF_BENCH( return new SkBench_AddPathTest(SkBench_AddPathTest::kAddMatrix_AddType); )
mtklein@google.com410e6e82013-09-13 19:52:27 +00001069DEF_BENCH( return new SkBench_AddPathTest(SkBench_AddPathTest::kReverseAdd_AddType); )
1070DEF_BENCH( return new SkBench_AddPathTest(SkBench_AddPathTest::kReversePathTo_AddType); )
senorblanco@chromium.orge50f7362012-01-12 19:10:35 +00001071
mtklein@google.com410e6e82013-09-13 19:52:27 +00001072DEF_BENCH( return new CirclesBench(FLAGS00); )
1073DEF_BENCH( return new CirclesBench(FLAGS01); )
1074DEF_BENCH( return new ArbRoundRectBench(false); )
1075DEF_BENCH( return new ArbRoundRectBench(true); )
1076DEF_BENCH( return new ConservativelyContainsBench(ConservativelyContainsBench::kRect_Type); )
1077DEF_BENCH( return new ConservativelyContainsBench(ConservativelyContainsBench::kRoundRect_Type); )
1078DEF_BENCH( return new ConservativelyContainsBench(ConservativelyContainsBench::kOval_Type); )
mike@reedtribe.org8d551012013-04-14 02:40:50 +00001079
reedb6402032015-03-20 13:23:43 -07001080
Mike Klein3f376a52014-07-16 20:23:43 -04001081// These seem to be optimized away, which is troublesome for timing.
1082/*
mtklein@google.com410e6e82013-09-13 19:52:27 +00001083DEF_BENCH( return new ConicBench_Chop5() )
mtklein@google.com410e6e82013-09-13 19:52:27 +00001084DEF_BENCH( return new ConicBench_ComputeError() )
1085DEF_BENCH( return new ConicBench_asQuadTol() )
1086DEF_BENCH( return new ConicBench_quadPow2() )
Mike Klein3f376a52014-07-16 20:23:43 -04001087*/