blob: b597f919f8f9ebbb9831f7f627999f3f9dd929f7 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
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"
tomhudson@google.com6e8d3352011-06-22 17:16:35 +000013#include "SkRandom.h"
reed@google.comd34658a2011-04-11 13:12:51 +000014#include "SkShader.h"
15#include "SkString.h"
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +000016#include "SkTArray.h"
17
reed@google.comd34658a2011-04-11 13:12:51 +000018enum Flags {
19 kStroke_Flag = 1 << 0,
20 kBig_Flag = 1 << 1
21};
22
23#define FLAGS00 Flags(0)
24#define FLAGS01 Flags(kStroke_Flag)
25#define FLAGS10 Flags(kBig_Flag)
26#define FLAGS11 Flags(kStroke_Flag | kBig_Flag)
27
tfarinaf168b862014-06-19 12:32:29 -070028class PathBench : public Benchmark {
reed@google.comd34658a2011-04-11 13:12:51 +000029 SkPaint fPaint;
30 SkString fName;
31 Flags fFlags;
reed@google.comd34658a2011-04-11 13:12:51 +000032public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000033 PathBench(Flags flags) : fFlags(flags) {
reed@google.comd34658a2011-04-11 13:12:51 +000034 fPaint.setStyle(flags & kStroke_Flag ? SkPaint::kStroke_Style :
35 SkPaint::kFill_Style);
36 fPaint.setStrokeWidth(SkIntToScalar(5));
37 fPaint.setStrokeJoin(SkPaint::kBevel_Join);
38 }
39
40 virtual void appendName(SkString*) = 0;
41 virtual void makePath(SkPath*) = 0;
tomhudson@google.com6e8d3352011-06-22 17:16:35 +000042 virtual int complexity() { return 0; }
reed@google.comd34658a2011-04-11 13:12:51 +000043
44protected:
mtklein72c9faa2015-01-09 10:06:39 -080045 const char* onGetName() SK_OVERRIDE {
reed@google.comd34658a2011-04-11 13:12:51 +000046 fName.printf("path_%s_%s_",
47 fFlags & kStroke_Flag ? "stroke" : "fill",
48 fFlags & kBig_Flag ? "big" : "small");
49 this->appendName(&fName);
50 return fName.c_str();
51 }
52
mtklein72c9faa2015-01-09 10:06:39 -080053 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
reed@google.comd34658a2011-04-11 13:12:51 +000054 SkPaint paint(fPaint);
55 this->setupPaint(&paint);
56
57 SkPath path;
58 this->makePath(&path);
59 if (fFlags & kBig_Flag) {
60 SkMatrix m;
61 m.setScale(SkIntToScalar(10), SkIntToScalar(10));
62 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
mtklein72c9faa2015-01-09 10:06:39 -080084 void appendName(SkString* name) SK_OVERRIDE {
reed@google.comd34658a2011-04-11 13:12:51 +000085 name->append("triangle");
86 }
mtklein72c9faa2015-01-09 10:06:39 -080087 void makePath(SkPath* path) SK_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
mtklein72c9faa2015-01-09 10:06:39 -0800104 void appendName(SkString* name) SK_OVERRIDE {
reed@google.comd34658a2011-04-11 13:12:51 +0000105 name->append("rect");
106 }
mtklein72c9faa2015-01-09 10:06:39 -0800107 void makePath(SkPath* path) SK_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
115class OvalPathBench : public PathBench {
116public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000117 OvalPathBench(Flags flags) : INHERITED(flags) {}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000118
mtklein72c9faa2015-01-09 10:06:39 -0800119 void appendName(SkString* name) SK_OVERRIDE {
reed@google.comd34658a2011-04-11 13:12:51 +0000120 name->append("oval");
121 }
mtklein72c9faa2015-01-09 10:06:39 -0800122 void makePath(SkPath* path) SK_OVERRIDE {
jvanverth@google.come2bfd8b2013-01-24 15:45:35 +0000123 SkRect r = { 10, 10, 23, 20 };
reed@google.comd34658a2011-04-11 13:12:51 +0000124 path->addOval(r);
125 }
126private:
127 typedef PathBench INHERITED;
128};
129
bsalomon@google.com1647a192012-04-11 15:34:46 +0000130class CirclePathBench: public PathBench {
131public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000132 CirclePathBench(Flags flags) : INHERITED(flags) {}
bsalomon@google.com1647a192012-04-11 15:34:46 +0000133
mtklein72c9faa2015-01-09 10:06:39 -0800134 void appendName(SkString* name) SK_OVERRIDE {
bsalomon@google.com1647a192012-04-11 15:34:46 +0000135 name->append("circle");
136 }
mtklein72c9faa2015-01-09 10:06:39 -0800137 void makePath(SkPath* path) SK_OVERRIDE {
bsalomon@google.com1647a192012-04-11 15:34:46 +0000138 path->addCircle(SkIntToScalar(20), SkIntToScalar(20),
139 SkIntToScalar(10));
140 }
141private:
142 typedef PathBench INHERITED;
143};
144
reed@google.comd34658a2011-04-11 13:12:51 +0000145class SawToothPathBench : public PathBench {
146public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000147 SawToothPathBench(Flags flags) : INHERITED(flags) {}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000148
mtklein72c9faa2015-01-09 10:06:39 -0800149 void appendName(SkString* name) SK_OVERRIDE {
reed@google.comd34658a2011-04-11 13:12:51 +0000150 name->append("sawtooth");
151 }
152 virtual void makePath(SkPath* path) {
153 SkScalar x = SkIntToScalar(20);
154 SkScalar y = SkIntToScalar(20);
155 const SkScalar x0 = x;
156 const SkScalar dx = SK_Scalar1 * 5;
157 const SkScalar dy = SK_Scalar1 * 10;
158
159 path->moveTo(x, y);
160 for (int i = 0; i < 32; i++) {
161 x += dx;
162 path->lineTo(x, y - dy);
163 x += dx;
164 path->lineTo(x, y + dy);
165 }
166 path->lineTo(x, y + 2 * dy);
167 path->lineTo(x0, y + 2 * dy);
168 path->close();
169 }
mtklein72c9faa2015-01-09 10:06:39 -0800170 int complexity() SK_OVERRIDE { return 1; }
reed@google.comd34658a2011-04-11 13:12:51 +0000171private:
172 typedef PathBench INHERITED;
173};
174
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000175class LongCurvedPathBench : public PathBench {
176public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000177 LongCurvedPathBench(Flags flags) : INHERITED(flags) {}
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000178
mtklein72c9faa2015-01-09 10:06:39 -0800179 void appendName(SkString* name) SK_OVERRIDE {
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000180 name->append("long_curved");
181 }
mtklein72c9faa2015-01-09 10:06:39 -0800182 void makePath(SkPath* path) SK_OVERRIDE {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000183 SkRandom rand (12);
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000184 int i;
185 for (i = 0; i < 100; i++) {
186 path->quadTo(SkScalarMul(rand.nextUScalar1(), SkIntToScalar(640)),
187 SkScalarMul(rand.nextUScalar1(), SkIntToScalar(480)),
188 SkScalarMul(rand.nextUScalar1(), SkIntToScalar(640)),
189 SkScalarMul(rand.nextUScalar1(), SkIntToScalar(480)));
190 }
191 path->close();
192 }
mtklein72c9faa2015-01-09 10:06:39 -0800193 int complexity() SK_OVERRIDE { return 2; }
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000194private:
195 typedef PathBench INHERITED;
196};
197
senorblanco@chromium.orge50f7362012-01-12 19:10:35 +0000198class LongLinePathBench : public PathBench {
199public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000200 LongLinePathBench(Flags flags) : INHERITED(flags) {}
senorblanco@chromium.orge50f7362012-01-12 19:10:35 +0000201
mtklein72c9faa2015-01-09 10:06:39 -0800202 void appendName(SkString* name) SK_OVERRIDE {
senorblanco@chromium.orge50f7362012-01-12 19:10:35 +0000203 name->append("long_line");
204 }
mtklein72c9faa2015-01-09 10:06:39 -0800205 void makePath(SkPath* path) SK_OVERRIDE {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000206 SkRandom rand;
senorblanco@chromium.orge50f7362012-01-12 19:10:35 +0000207 path->moveTo(rand.nextUScalar1() * 640, rand.nextUScalar1() * 480);
208 for (size_t i = 1; i < 100; i++) {
209 path->lineTo(rand.nextUScalar1() * 640, rand.nextUScalar1() * 480);
210 }
211 }
mtklein72c9faa2015-01-09 10:06:39 -0800212 int complexity() SK_OVERRIDE { return 2; }
senorblanco@chromium.orge50f7362012-01-12 19:10:35 +0000213private:
214 typedef PathBench INHERITED;
215};
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000216
tfarinaf168b862014-06-19 12:32:29 -0700217class RandomPathBench : public Benchmark {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000218public:
mtklein72c9faa2015-01-09 10:06:39 -0800219 bool isSuitableFor(Backend backend) SK_OVERRIDE {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000220 return backend == kNonRendering_Backend;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000221 }
222
223protected:
224 void createData(int minVerbs,
225 int maxVerbs,
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000226 bool allowMoves = true,
227 SkRect* bounds = NULL) {
228 SkRect tempBounds;
229 if (NULL == bounds) {
230 tempBounds.setXYWH(0, 0, SK_Scalar1, SK_Scalar1);
231 bounds = &tempBounds;
232 }
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000233 fVerbCnts.reset(kNumVerbCnts);
234 for (int i = 0; i < kNumVerbCnts; ++i) {
235 fVerbCnts[i] = fRandom.nextRangeU(minVerbs, maxVerbs + 1);
236 }
237 fVerbs.reset(kNumVerbs);
238 for (int i = 0; i < kNumVerbs; ++i) {
239 do {
240 fVerbs[i] = static_cast<SkPath::Verb>(fRandom.nextULessThan(SkPath::kDone_Verb));
241 } while (!allowMoves && SkPath::kMove_Verb == fVerbs[i]);
242 }
243 fPoints.reset(kNumPoints);
244 for (int i = 0; i < kNumPoints; ++i) {
245 fPoints[i].set(fRandom.nextRangeScalar(bounds->fLeft, bounds->fRight),
246 fRandom.nextRangeScalar(bounds->fTop, bounds->fBottom));
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000247 }
248 this->restartMakingPaths();
249 }
250
251 void restartMakingPaths() {
252 fCurrPath = 0;
253 fCurrVerb = 0;
254 fCurrPoint = 0;
255 }
256
257 void makePath(SkPath* path) {
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000258 int vCount = fVerbCnts[(fCurrPath++) & (kNumVerbCnts - 1)];
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000259 for (int v = 0; v < vCount; ++v) {
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000260 int verb = fVerbs[(fCurrVerb++) & (kNumVerbs - 1)];
261 switch (verb) {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000262 case SkPath::kMove_Verb:
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000263 path->moveTo(fPoints[(fCurrPoint++) & (kNumPoints - 1)]);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000264 break;
265 case SkPath::kLine_Verb:
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000266 path->lineTo(fPoints[(fCurrPoint++) & (kNumPoints - 1)]);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000267 break;
268 case SkPath::kQuad_Verb:
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000269 path->quadTo(fPoints[(fCurrPoint + 0) & (kNumPoints - 1)],
270 fPoints[(fCurrPoint + 1) & (kNumPoints - 1)]);
271 fCurrPoint += 2;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000272 break;
reed@google.com277c3f82013-05-31 15:17:50 +0000273 case SkPath::kConic_Verb:
274 path->conicTo(fPoints[(fCurrPoint + 0) & (kNumPoints - 1)],
275 fPoints[(fCurrPoint + 1) & (kNumPoints - 1)],
276 SK_ScalarHalf);
277 fCurrPoint += 2;
278 break;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000279 case SkPath::kCubic_Verb:
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000280 path->cubicTo(fPoints[(fCurrPoint + 0) & (kNumPoints - 1)],
281 fPoints[(fCurrPoint + 1) & (kNumPoints - 1)],
282 fPoints[(fCurrPoint + 2) & (kNumPoints - 1)]);
283 fCurrPoint += 3;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000284 break;
285 case SkPath::kClose_Verb:
286 path->close();
287 break;
288 default:
289 SkDEBUGFAIL("Unexpected path verb");
290 break;
291 }
292 }
293 }
294
295 void finishedMakingPaths() {
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000296 fVerbCnts.reset(0);
297 fVerbs.reset(0);
298 fPoints.reset(0);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000299 }
300
301private:
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000302 enum {
303 // these should all be pow 2
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000304 kNumVerbCnts = 1 << 5,
305 kNumVerbs = 1 << 5,
306 kNumPoints = 1 << 5,
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000307 };
308 SkAutoTArray<int> fVerbCnts;
309 SkAutoTArray<SkPath::Verb> fVerbs;
310 SkAutoTArray<SkPoint> fPoints;
311 int fCurrPath;
312 int fCurrVerb;
313 int fCurrPoint;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000314 SkRandom fRandom;
tfarinaf168b862014-06-19 12:32:29 -0700315 typedef Benchmark INHERITED;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000316};
317
318class PathCreateBench : public RandomPathBench {
319public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000320 PathCreateBench() {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000321 }
322
323protected:
mtklein72c9faa2015-01-09 10:06:39 -0800324 const char* onGetName() SK_OVERRIDE {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000325 return "path_create";
326 }
327
mtklein72c9faa2015-01-09 10:06:39 -0800328 void onPreDraw() SK_OVERRIDE {
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000329 this->createData(10, 100);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000330 }
331
mtklein72c9faa2015-01-09 10:06:39 -0800332 void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000333 for (int i = 0; i < loops; ++i) {
commit-bot@chromium.org8f881172014-01-06 20:19:14 +0000334 if (i % 1000 == 0) {
335 fPath.reset(); // PathRef memory can grow without bound otherwise.
336 }
337 this->makePath(&fPath);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000338 }
339 this->restartMakingPaths();
340 }
341
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000342private:
commit-bot@chromium.org8f881172014-01-06 20:19:14 +0000343 SkPath fPath;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000344
345 typedef RandomPathBench INHERITED;
346};
347
348class PathCopyBench : public RandomPathBench {
349public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000350 PathCopyBench() {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000351 }
352
353protected:
mtklein72c9faa2015-01-09 10:06:39 -0800354 const char* onGetName() SK_OVERRIDE {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000355 return "path_copy";
356 }
mtklein72c9faa2015-01-09 10:06:39 -0800357 void onPreDraw() SK_OVERRIDE {
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000358 this->createData(10, 100);
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000359 fPaths.reset(kPathCnt);
360 fCopies.reset(kPathCnt);
361 for (int i = 0; i < kPathCnt; ++i) {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000362 this->makePath(&fPaths[i]);
363 }
364 this->finishedMakingPaths();
365 }
mtklein72c9faa2015-01-09 10:06:39 -0800366 void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000367 for (int i = 0; i < loops; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000368 int idx = i & (kPathCnt - 1);
369 fCopies[idx] = fPaths[idx];
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000370 }
371 }
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000372
373private:
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000374 enum {
375 // must be a pow 2
376 kPathCnt = 1 << 5,
377 };
378 SkAutoTArray<SkPath> fPaths;
379 SkAutoTArray<SkPath> fCopies;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000380
381 typedef RandomPathBench INHERITED;
382};
383
384class PathTransformBench : public RandomPathBench {
385public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000386 PathTransformBench(bool inPlace) : fInPlace(inPlace) {}
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000387
388protected:
mtklein72c9faa2015-01-09 10:06:39 -0800389 const char* onGetName() SK_OVERRIDE {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000390 return fInPlace ? "path_transform_in_place" : "path_transform_copy";
391 }
392
mtklein72c9faa2015-01-09 10:06:39 -0800393 void onPreDraw() SK_OVERRIDE {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000394 fMatrix.setScale(5 * SK_Scalar1, 6 * SK_Scalar1);
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000395 this->createData(10, 100);
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000396 fPaths.reset(kPathCnt);
397 for (int i = 0; i < kPathCnt; ++i) {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000398 this->makePath(&fPaths[i]);
399 }
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000400 this->finishedMakingPaths();
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000401 if (!fInPlace) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000402 fTransformed.reset(kPathCnt);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000403 }
404 }
405
mtklein72c9faa2015-01-09 10:06:39 -0800406 void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000407 if (fInPlace) {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000408 for (int i = 0; i < loops; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000409 fPaths[i & (kPathCnt - 1)].transform(fMatrix);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000410 }
411 } else {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000412 for (int i = 0; i < loops; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000413 int idx = i & (kPathCnt - 1);
414 fPaths[idx].transform(fMatrix, &fTransformed[idx]);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000415 }
416 }
417 }
418
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000419private:
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000420 enum {
421 // must be a pow 2
422 kPathCnt = 1 << 5,
423 };
424 SkAutoTArray<SkPath> fPaths;
425 SkAutoTArray<SkPath> fTransformed;
426
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000427 SkMatrix fMatrix;
428 bool fInPlace;
429 typedef RandomPathBench INHERITED;
430};
431
432class PathEqualityBench : public RandomPathBench {
433public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000434 PathEqualityBench() { }
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000435
436protected:
mtklein72c9faa2015-01-09 10:06:39 -0800437 const char* onGetName() SK_OVERRIDE {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000438 return "path_equality_50%";
439 }
440
mtklein72c9faa2015-01-09 10:06:39 -0800441 void onPreDraw() SK_OVERRIDE {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000442 fParity = 0;
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000443 this->createData(10, 100);
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000444 fPaths.reset(kPathCnt);
445 fCopies.reset(kPathCnt);
446 for (int i = 0; i < kPathCnt; ++i) {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000447 this->makePath(&fPaths[i]);
448 fCopies[i] = fPaths[i];
449 }
450 this->finishedMakingPaths();
451 }
452
mtklein72c9faa2015-01-09 10:06:39 -0800453 void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000454 for (int i = 0; i < loops; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000455 int idx = i & (kPathCnt - 1);
456 fParity ^= (fPaths[idx] == fCopies[idx & ~0x1]);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000457 }
458 }
459
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000460private:
461 bool fParity; // attempt to keep compiler from optimizing out the ==
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000462 enum {
463 // must be a pow 2
464 kPathCnt = 1 << 5,
465 };
466 SkAutoTArray<SkPath> fPaths;
467 SkAutoTArray<SkPath> fCopies;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000468 typedef RandomPathBench INHERITED;
469};
470
471class SkBench_AddPathTest : public RandomPathBench {
472public:
473 enum AddType {
474 kAdd_AddType,
475 kAddTrans_AddType,
476 kAddMatrix_AddType,
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000477 kReverseAdd_AddType,
478 kReversePathTo_AddType,
479 };
480
mtklein@google.com410e6e82013-09-13 19:52:27 +0000481 SkBench_AddPathTest(AddType type) : fType(type) {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000482 fMatrix.setRotate(60 * SK_Scalar1);
483 }
484
485protected:
mtklein72c9faa2015-01-09 10:06:39 -0800486 const char* onGetName() SK_OVERRIDE {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000487 switch (fType) {
488 case kAdd_AddType:
489 return "path_add_path";
490 case kAddTrans_AddType:
491 return "path_add_path_trans";
492 case kAddMatrix_AddType:
493 return "path_add_path_matrix";
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000494 case kReverseAdd_AddType:
495 return "path_reverse_add_path";
496 case kReversePathTo_AddType:
497 return "path_reverse_path_to";
498 default:
499 SkDEBUGFAIL("Bad add type");
500 return "";
501 }
502 }
503
mtklein72c9faa2015-01-09 10:06:39 -0800504 void onPreDraw() SK_OVERRIDE {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000505 // reversePathTo assumes a single contour path.
506 bool allowMoves = kReversePathTo_AddType != fType;
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000507 this->createData(10, 100, allowMoves);
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000508 fPaths0.reset(kPathCnt);
509 fPaths1.reset(kPathCnt);
510 for (int i = 0; i < kPathCnt; ++i) {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000511 this->makePath(&fPaths0[i]);
512 this->makePath(&fPaths1[i]);
513 }
514 this->finishedMakingPaths();
515 }
516
mtklein72c9faa2015-01-09 10:06:39 -0800517 void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000518 switch (fType) {
519 case kAdd_AddType:
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000520 for (int i = 0; i < loops; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000521 int idx = i & (kPathCnt - 1);
522 SkPath result = fPaths0[idx];
523 result.addPath(fPaths1[idx]);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000524 }
525 break;
526 case kAddTrans_AddType:
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000527 for (int i = 0; i < loops; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000528 int idx = i & (kPathCnt - 1);
529 SkPath result = fPaths0[idx];
530 result.addPath(fPaths1[idx], 2 * SK_Scalar1, 5 * SK_Scalar1);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000531 }
532 break;
533 case kAddMatrix_AddType:
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000534 for (int i = 0; i < loops; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000535 int idx = i & (kPathCnt - 1);
536 SkPath result = fPaths0[idx];
537 result.addPath(fPaths1[idx], fMatrix);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000538 }
539 break;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000540 case kReverseAdd_AddType:
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000541 for (int i = 0; i < loops; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000542 int idx = i & (kPathCnt - 1);
543 SkPath result = fPaths0[idx];
544 result.reverseAddPath(fPaths1[idx]);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000545 }
546 break;
547 case kReversePathTo_AddType:
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000548 for (int i = 0; i < loops; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000549 int idx = i & (kPathCnt - 1);
550 SkPath result = fPaths0[idx];
551 result.reversePathTo(fPaths1[idx]);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000552 }
553 break;
554 }
555 }
556
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000557private:
558 AddType fType; // or reverseAddPath
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000559 enum {
560 // must be a pow 2
561 kPathCnt = 1 << 5,
562 };
563 SkAutoTArray<SkPath> fPaths0;
564 SkAutoTArray<SkPath> fPaths1;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000565 SkMatrix fMatrix;
566 typedef RandomPathBench INHERITED;
567};
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000568
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000569
tfarinaf168b862014-06-19 12:32:29 -0700570class CirclesBench : public Benchmark {
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000571protected:
572 SkString fName;
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000573 Flags fFlags;
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000574
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000575public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000576 CirclesBench(Flags flags) : fFlags(flags) {
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000577 fName.printf("circles_%s", fFlags & kStroke_Flag ? "stroke" : "fill");
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000578 }
579
580protected:
mtklein72c9faa2015-01-09 10:06:39 -0800581 const char* onGetName() SK_OVERRIDE {
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000582 return fName.c_str();
583 }
584
mtklein72c9faa2015-01-09 10:06:39 -0800585 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000586 SkPaint paint;
587
588 paint.setColor(SK_ColorBLACK);
589 paint.setAntiAlias(true);
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000590 if (fFlags & kStroke_Flag) {
591 paint.setStyle(SkPaint::kStroke_Style);
592 }
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000593
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000594 SkRandom rand;
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000595
596 SkRect r;
597
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000598 for (int i = 0; i < loops; ++i) {
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000599 SkScalar radius = rand.nextUScalar1() * 3;
600 r.fLeft = rand.nextUScalar1() * 300;
601 r.fTop = rand.nextUScalar1() * 300;
602 r.fRight = r.fLeft + 2 * radius;
603 r.fBottom = r.fTop + 2 * radius;
604
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000605 if (fFlags & kStroke_Flag) {
606 paint.setStrokeWidth(rand.nextUScalar1() * 5.0f);
607 }
608
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000609 SkPath temp;
610
611 // mimic how Chrome does circles
612 temp.arcTo(r, 0, 0, false);
613 temp.addOval(r, SkPath::kCCW_Direction);
614 temp.arcTo(r, 360, 0, true);
615 temp.close();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000616
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000617 canvas->drawPath(temp, paint);
618 }
619 }
620
621private:
tfarinaf168b862014-06-19 12:32:29 -0700622 typedef Benchmark INHERITED;
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000623};
624
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000625
robertphillips@google.com158618e2012-10-23 16:56:56 +0000626// Chrome creates its own round rects with each corner possibly being different.
627// In its "zero radius" incarnation it creates degenerate round rects.
skia.committer@gmail.com1e34ff72012-10-24 02:01:24 +0000628// Note: PathTest::test_arb_round_rect_is_convex and
robertphillips@google.com158618e2012-10-23 16:56:56 +0000629// test_arb_zero_rad_round_rect_is_rect perform almost exactly
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000630// the same test (but with no drawing)
tfarinaf168b862014-06-19 12:32:29 -0700631class ArbRoundRectBench : public Benchmark {
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000632protected:
633 SkString fName;
634
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000635public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000636 ArbRoundRectBench(bool zeroRad) : fZeroRad(zeroRad) {
robertphillips@google.com158618e2012-10-23 16:56:56 +0000637 if (zeroRad) {
638 fName.printf("zeroradroundrect");
639 } else {
640 fName.printf("arbroundrect");
641 }
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000642 }
643
644protected:
mtklein72c9faa2015-01-09 10:06:39 -0800645 const char* onGetName() SK_OVERRIDE {
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000646 return fName.c_str();
647 }
648
skia.committer@gmail.com989a95e2012-10-18 02:01:23 +0000649 static void add_corner_arc(SkPath* path, const SkRect& rect,
650 SkScalar xIn, SkScalar yIn,
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000651 int startAngle)
652 {
653
654 SkScalar rx = SkMinScalar(rect.width(), xIn);
655 SkScalar ry = SkMinScalar(rect.height(), yIn);
656
657 SkRect arcRect;
658 arcRect.set(-rx, -ry, rx, ry);
659 switch (startAngle) {
660 case 0:
661 arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom);
662 break;
663 case 90:
664 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom);
665 break;
666 case 180:
667 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop);
668 break;
669 case 270:
670 arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop);
671 break;
672 default:
673 break;
674 }
675
676 path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false);
677 }
678
skia.committer@gmail.com989a95e2012-10-18 02:01:23 +0000679 static void make_arb_round_rect(SkPath* path, const SkRect& r,
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000680 SkScalar xCorner, SkScalar yCorner) {
681 // we are lazy here and use the same x & y for each corner
682 add_corner_arc(path, r, xCorner, yCorner, 270);
683 add_corner_arc(path, r, xCorner, yCorner, 0);
684 add_corner_arc(path, r, xCorner, yCorner, 90);
685 add_corner_arc(path, r, xCorner, yCorner, 180);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000686 path->close();
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000687
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000688 SkASSERT(path->isConvex());
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000689 }
690
mtklein72c9faa2015-01-09 10:06:39 -0800691 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000692 SkRandom rand;
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000693 SkRect r;
694
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000695 for (int i = 0; i < loops; ++i) {
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000696 SkPaint paint;
697 paint.setColor(0xff000000 | rand.nextU());
698 paint.setAntiAlias(true);
699
robertphillips@google.com158618e2012-10-23 16:56:56 +0000700 SkScalar size = rand.nextUScalar1() * 30;
701 if (size < SK_Scalar1) {
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000702 continue;
703 }
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000704 r.fLeft = rand.nextUScalar1() * 300;
705 r.fTop = rand.nextUScalar1() * 300;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000706 r.fRight = r.fLeft + 2 * size;
707 r.fBottom = r.fTop + 2 * size;
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000708
709 SkPath temp;
710
robertphillips@google.com158618e2012-10-23 16:56:56 +0000711 if (fZeroRad) {
712 make_arb_round_rect(&temp, r, 0, 0);
713
714 SkASSERT(temp.isRect(NULL));
715 } else {
716 make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15);
717 }
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000718
719 canvas->drawPath(temp, paint);
720 }
721 }
722
723private:
robertphillips@google.com158618e2012-10-23 16:56:56 +0000724 bool fZeroRad; // should 0 radius rounds rects be tested?
725
tfarinaf168b862014-06-19 12:32:29 -0700726 typedef Benchmark INHERITED;
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000727};
728
tfarinaf168b862014-06-19 12:32:29 -0700729class ConservativelyContainsBench : public Benchmark {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +0000730public:
731 enum Type {
732 kRect_Type,
733 kRoundRect_Type,
734 kOval_Type,
735 };
736
mtklein@google.com410e6e82013-09-13 19:52:27 +0000737 ConservativelyContainsBench(Type type) {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +0000738 fParity = false;
739 fName = "conservatively_contains_";
740 switch (type) {
741 case kRect_Type:
742 fName.append("rect");
743 fPath.addRect(kBaseRect);
744 break;
745 case kRoundRect_Type:
746 fName.append("round_rect");
747 fPath.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1]);
748 break;
749 case kOval_Type:
750 fName.append("oval");
751 fPath.addOval(kBaseRect);
752 break;
753 }
754 }
755
mtklein72c9faa2015-01-09 10:06:39 -0800756 bool isSuitableFor(Backend backend) SK_OVERRIDE {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000757 return backend == kNonRendering_Backend;
758 }
759
bsalomon@google.com9bee33a2012-11-13 21:51:38 +0000760private:
mtklein72c9faa2015-01-09 10:06:39 -0800761 const char* onGetName() SK_OVERRIDE {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +0000762 return fName.c_str();
763 }
764
mtklein72c9faa2015-01-09 10:06:39 -0800765 void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000766 for (int i = 0; i < loops; ++i) {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +0000767 const SkRect& rect = fQueryRects[i % kQueryRectCnt];
768 fParity = fParity != fPath.conservativelyContainsRect(rect);
769 }
770 }
771
mtklein72c9faa2015-01-09 10:06:39 -0800772 void onPreDraw() SK_OVERRIDE {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +0000773 fQueryRects.setCount(kQueryRectCnt);
774
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000775 SkRandom rand;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +0000776 for (int i = 0; i < kQueryRectCnt; ++i) {
777 SkSize size;
778 SkPoint xy;
779 size.fWidth = rand.nextRangeScalar(kQueryMin.fWidth, kQueryMax.fWidth);
780 size.fHeight = rand.nextRangeScalar(kQueryMin.fHeight, kQueryMax.fHeight);
781 xy.fX = rand.nextRangeScalar(kBounds.fLeft, kBounds.fRight - size.fWidth);
782 xy.fY = rand.nextRangeScalar(kBounds.fTop, kBounds.fBottom - size.fHeight);
783
784 fQueryRects[i] = SkRect::MakeXYWH(xy.fX, xy.fY, size.fWidth, size.fHeight);
785 }
786 }
787
bsalomon@google.com9bee33a2012-11-13 21:51:38 +0000788 enum {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +0000789 kQueryRectCnt = 400,
790 };
791 static const SkRect kBounds; // bounds for all random query rects
792 static const SkSize kQueryMin; // minimum query rect size, should be <= kQueryMax
793 static const SkSize kQueryMax; // max query rect size, should < kBounds
794 static const SkRect kBaseRect; // rect that is used to construct the path
795 static const SkScalar kRRRadii[2]; // x and y radii for round rect
796
797 SkString fName;
798 SkPath fPath;
799 bool fParity;
800 SkTDArray<SkRect> fQueryRects;
801
tfarinaf168b862014-06-19 12:32:29 -0700802 typedef Benchmark INHERITED;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +0000803};
804
mike@reedtribe.org8d551012013-04-14 02:40:50 +0000805///////////////////////////////////////////////////////////////////////////////
806
807#include "SkGeometry.h"
808
tfarinaf168b862014-06-19 12:32:29 -0700809class ConicBench_Chop5 : public Benchmark {
mike@reedtribe.org28552e12013-04-26 00:58:29 +0000810 SkConic fRQ;
mike@reedtribe.org8d551012013-04-14 02:40:50 +0000811public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000812 ConicBench_Chop5() {
mike@reedtribe.org8d551012013-04-14 02:40:50 +0000813 fRQ.fPts[0].set(0, 0);
814 fRQ.fPts[1].set(100, 0);
815 fRQ.fPts[2].set(100, 100);
816 fRQ.fW = SkScalarCos(SK_ScalarPI/4);
817 }
skia.committer@gmail.comab38e562013-04-14 07:01:08 +0000818
mike@reedtribe.org8d551012013-04-14 02:40:50 +0000819private:
mtklein72c9faa2015-01-09 10:06:39 -0800820 const char* onGetName() SK_OVERRIDE {
mike@reedtribe.org8d551012013-04-14 02:40:50 +0000821 return "ratquad-chop-0.5";
822 }
skia.committer@gmail.comab38e562013-04-14 07:01:08 +0000823
mtklein72c9faa2015-01-09 10:06:39 -0800824 void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
mike@reedtribe.org28552e12013-04-26 00:58:29 +0000825 SkConic dst[2];
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000826 for (int i = 0; i < loops; ++i) {
mike@reedtribe.org8d551012013-04-14 02:40:50 +0000827 fRQ.chopAt(0.5f, dst);
828 }
829 }
skia.committer@gmail.comab38e562013-04-14 07:01:08 +0000830
tfarinaf168b862014-06-19 12:32:29 -0700831 typedef Benchmark INHERITED;
mike@reedtribe.org8d551012013-04-14 02:40:50 +0000832};
833
tfarinaf168b862014-06-19 12:32:29 -0700834class ConicBench_ChopHalf : public Benchmark {
mike@reedtribe.org28552e12013-04-26 00:58:29 +0000835 SkConic fRQ;
mike@reedtribe.org8d551012013-04-14 02:40:50 +0000836public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000837 ConicBench_ChopHalf() {
mike@reedtribe.org8d551012013-04-14 02:40:50 +0000838 fRQ.fPts[0].set(0, 0);
839 fRQ.fPts[1].set(100, 0);
840 fRQ.fPts[2].set(100, 100);
841 fRQ.fW = SkScalarCos(SK_ScalarPI/4);
842 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000843
mike@reedtribe.org8d551012013-04-14 02:40:50 +0000844private:
mtklein72c9faa2015-01-09 10:06:39 -0800845 const char* onGetName() SK_OVERRIDE {
mike@reedtribe.org8d551012013-04-14 02:40:50 +0000846 return "ratquad-chop-half";
847 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000848
mtklein72c9faa2015-01-09 10:06:39 -0800849 void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
mike@reedtribe.org28552e12013-04-26 00:58:29 +0000850 SkConic dst[2];
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000851 for (int i = 0; i < loops; ++i) {
mike@reedtribe.org8d551012013-04-14 02:40:50 +0000852 fRQ.chop(dst);
853 }
854 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000855
tfarinaf168b862014-06-19 12:32:29 -0700856 typedef Benchmark INHERITED;
mike@reedtribe.org8d551012013-04-14 02:40:50 +0000857};
858
859///////////////////////////////////////////////////////////////////////////////
860
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000861static void rand_conic(SkConic* conic, SkRandom& rand) {
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000862 for (int i = 0; i < 3; ++i) {
863 conic->fPts[i].set(rand.nextUScalar1() * 100, rand.nextUScalar1() * 100);
864 }
865 if (rand.nextUScalar1() > 0.5f) {
866 conic->fW = rand.nextUScalar1();
867 } else {
868 conic->fW = 1 + rand.nextUScalar1() * 4;
869 }
870}
871
tfarinaf168b862014-06-19 12:32:29 -0700872class ConicBench : public Benchmark {
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000873public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000874 ConicBench() {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000875 SkRandom rand;
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000876 for (int i = 0; i < CONICS; ++i) {
877 rand_conic(&fConics[i], rand);
878 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000879 }
880
mtklein72c9faa2015-01-09 10:06:39 -0800881 bool isSuitableFor(Backend backend) SK_OVERRIDE {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000882 return backend == kNonRendering_Backend;
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000883 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000884
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000885protected:
886 enum {
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000887 CONICS = 100
888 };
889 SkConic fConics[CONICS];
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000890
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000891private:
tfarinaf168b862014-06-19 12:32:29 -0700892 typedef Benchmark INHERITED;
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000893};
894
895class ConicBench_ComputeError : public ConicBench {
896public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000897 ConicBench_ComputeError() {}
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000898
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000899protected:
mtklein72c9faa2015-01-09 10:06:39 -0800900 const char* onGetName() SK_OVERRIDE {
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000901 return "conic-compute-error";
902 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000903
mtklein72c9faa2015-01-09 10:06:39 -0800904 void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000905 SkVector err;
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000906 for (int i = 0; i < loops; ++i) {
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000907 for (int j = 0; j < CONICS; ++j) {
908 fConics[j].computeAsQuadError(&err);
909 }
910 }
911 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000912
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000913private:
914 typedef ConicBench INHERITED;
915};
916
917class ConicBench_asQuadTol : public ConicBench {
918public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000919 ConicBench_asQuadTol() {}
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000920
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000921protected:
mtklein72c9faa2015-01-09 10:06:39 -0800922 const char* onGetName() SK_OVERRIDE {
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000923 return "conic-asQuadTol";
924 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000925
mtklein72c9faa2015-01-09 10:06:39 -0800926 void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000927 for (int i = 0; i < loops; ++i) {
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000928 for (int j = 0; j < CONICS; ++j) {
929 fConics[j].asQuadTol(SK_ScalarHalf);
930 }
931 }
932 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000933
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000934private:
935 typedef ConicBench INHERITED;
936};
937
938class ConicBench_quadPow2 : public ConicBench {
939public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000940 ConicBench_quadPow2() {}
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000941
942protected:
mtklein72c9faa2015-01-09 10:06:39 -0800943 const char* onGetName() SK_OVERRIDE {
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000944 return "conic-quadPow2";
945 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000946
mtklein72c9faa2015-01-09 10:06:39 -0800947 void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000948 for (int i = 0; i < loops; ++i) {
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000949 for (int j = 0; j < CONICS; ++j) {
950 fConics[j].computeQuadPOW2(SK_ScalarHalf);
951 }
952 }
953 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000954
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000955private:
956 typedef ConicBench INHERITED;
957};
958
959///////////////////////////////////////////////////////////////////////////////
960
bsalomon@google.com9bee33a2012-11-13 21:51:38 +0000961const SkRect ConservativelyContainsBench::kBounds = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
962const SkSize ConservativelyContainsBench::kQueryMin = SkSize::Make(SkIntToScalar(1), SkIntToScalar(1));
963const SkSize ConservativelyContainsBench::kQueryMax = SkSize::Make(SkIntToScalar(40), SkIntToScalar(40));
964const SkRect ConservativelyContainsBench::kBaseRect = SkRect::MakeXYWH(SkIntToScalar(25), SkIntToScalar(25), SkIntToScalar(50), SkIntToScalar(50));
965const SkScalar ConservativelyContainsBench::kRRRadii[2] = {SkIntToScalar(5), SkIntToScalar(10)};
966
mtklein@google.com410e6e82013-09-13 19:52:27 +0000967DEF_BENCH( return new TrianglePathBench(FLAGS00); )
968DEF_BENCH( return new TrianglePathBench(FLAGS01); )
969DEF_BENCH( return new TrianglePathBench(FLAGS10); )
970DEF_BENCH( return new TrianglePathBench(FLAGS11); )
reed@google.comd34658a2011-04-11 13:12:51 +0000971
mtklein@google.com410e6e82013-09-13 19:52:27 +0000972DEF_BENCH( return new RectPathBench(FLAGS00); )
973DEF_BENCH( return new RectPathBench(FLAGS01); )
974DEF_BENCH( return new RectPathBench(FLAGS10); )
975DEF_BENCH( return new RectPathBench(FLAGS11); )
reed@google.comd34658a2011-04-11 13:12:51 +0000976
mtklein@google.com410e6e82013-09-13 19:52:27 +0000977DEF_BENCH( return new OvalPathBench(FLAGS00); )
978DEF_BENCH( return new OvalPathBench(FLAGS01); )
979DEF_BENCH( return new OvalPathBench(FLAGS10); )
980DEF_BENCH( return new OvalPathBench(FLAGS11); )
reed@google.comd34658a2011-04-11 13:12:51 +0000981
mtklein@google.com410e6e82013-09-13 19:52:27 +0000982DEF_BENCH( return new CirclePathBench(FLAGS00); )
983DEF_BENCH( return new CirclePathBench(FLAGS01); )
984DEF_BENCH( return new CirclePathBench(FLAGS10); )
985DEF_BENCH( return new CirclePathBench(FLAGS11); )
bsalomon@google.com1647a192012-04-11 15:34:46 +0000986
mtklein@google.com410e6e82013-09-13 19:52:27 +0000987DEF_BENCH( return new SawToothPathBench(FLAGS00); )
988DEF_BENCH( return new SawToothPathBench(FLAGS01); )
reed@google.comd34658a2011-04-11 13:12:51 +0000989
mtklein@google.com410e6e82013-09-13 19:52:27 +0000990DEF_BENCH( return new LongCurvedPathBench(FLAGS00); )
991DEF_BENCH( return new LongCurvedPathBench(FLAGS01); )
992DEF_BENCH( return new LongLinePathBench(FLAGS00); )
993DEF_BENCH( return new LongLinePathBench(FLAGS01); )
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000994
mtklein@google.com410e6e82013-09-13 19:52:27 +0000995DEF_BENCH( return new PathCreateBench(); )
996DEF_BENCH( return new PathCopyBench(); )
997DEF_BENCH( return new PathTransformBench(true); )
998DEF_BENCH( return new PathTransformBench(false); )
999DEF_BENCH( return new PathEqualityBench(); )
senorblanco@chromium.orge50f7362012-01-12 19:10:35 +00001000
mtklein@google.com410e6e82013-09-13 19:52:27 +00001001DEF_BENCH( return new SkBench_AddPathTest(SkBench_AddPathTest::kAdd_AddType); )
1002DEF_BENCH( return new SkBench_AddPathTest(SkBench_AddPathTest::kAddTrans_AddType); )
1003DEF_BENCH( return new SkBench_AddPathTest(SkBench_AddPathTest::kAddMatrix_AddType); )
mtklein@google.com410e6e82013-09-13 19:52:27 +00001004DEF_BENCH( return new SkBench_AddPathTest(SkBench_AddPathTest::kReverseAdd_AddType); )
1005DEF_BENCH( return new SkBench_AddPathTest(SkBench_AddPathTest::kReversePathTo_AddType); )
senorblanco@chromium.orge50f7362012-01-12 19:10:35 +00001006
mtklein@google.com410e6e82013-09-13 19:52:27 +00001007DEF_BENCH( return new CirclesBench(FLAGS00); )
1008DEF_BENCH( return new CirclesBench(FLAGS01); )
1009DEF_BENCH( return new ArbRoundRectBench(false); )
1010DEF_BENCH( return new ArbRoundRectBench(true); )
1011DEF_BENCH( return new ConservativelyContainsBench(ConservativelyContainsBench::kRect_Type); )
1012DEF_BENCH( return new ConservativelyContainsBench(ConservativelyContainsBench::kRoundRect_Type); )
1013DEF_BENCH( return new ConservativelyContainsBench(ConservativelyContainsBench::kOval_Type); )
mike@reedtribe.org8d551012013-04-14 02:40:50 +00001014
Mike Klein3f376a52014-07-16 20:23:43 -04001015// These seem to be optimized away, which is troublesome for timing.
1016/*
mtklein@google.com410e6e82013-09-13 19:52:27 +00001017DEF_BENCH( return new ConicBench_Chop5() )
1018DEF_BENCH( return new ConicBench_ChopHalf() )
1019DEF_BENCH( return new ConicBench_ComputeError() )
1020DEF_BENCH( return new ConicBench_asQuadTol() )
1021DEF_BENCH( return new ConicBench_quadPow2() )
Mike Klein3f376a52014-07-16 20:23:43 -04001022*/