blob: 6e522ec67cfe9a6c13166a59080cf5cd8ff2a3ba [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 */
reed@google.comd34658a2011-04-11 13:12:51 +00008#include "SkBenchmark.h"
9#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 +000018
19enum 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
29class PathBench : public SkBenchmark {
30 SkPaint fPaint;
31 SkString fName;
32 Flags fFlags;
tomhudson@google.comca529d32011-10-28 15:34:49 +000033 enum { N = SkBENCHLOOP(1000) };
reed@google.comd34658a2011-04-11 13:12:51 +000034public:
35 PathBench(void* param, Flags flags) : INHERITED(param), fFlags(flags) {
36 fPaint.setStyle(flags & kStroke_Flag ? SkPaint::kStroke_Style :
37 SkPaint::kFill_Style);
38 fPaint.setStrokeWidth(SkIntToScalar(5));
39 fPaint.setStrokeJoin(SkPaint::kBevel_Join);
40 }
41
42 virtual void appendName(SkString*) = 0;
43 virtual void makePath(SkPath*) = 0;
tomhudson@google.com6e8d3352011-06-22 17:16:35 +000044 virtual int complexity() { return 0; }
reed@google.comd34658a2011-04-11 13:12:51 +000045
46protected:
bsalomon@google.com1647a192012-04-11 15:34:46 +000047 virtual const char* onGetName() SK_OVERRIDE {
reed@google.comd34658a2011-04-11 13:12:51 +000048 fName.printf("path_%s_%s_",
49 fFlags & kStroke_Flag ? "stroke" : "fill",
50 fFlags & kBig_Flag ? "big" : "small");
51 this->appendName(&fName);
52 return fName.c_str();
53 }
54
bsalomon@google.com1647a192012-04-11 15:34:46 +000055 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
reed@google.comd34658a2011-04-11 13:12:51 +000056 SkPaint paint(fPaint);
57 this->setupPaint(&paint);
58
59 SkPath path;
60 this->makePath(&path);
61 if (fFlags & kBig_Flag) {
62 SkMatrix m;
63 m.setScale(SkIntToScalar(10), SkIntToScalar(10));
64 path.transform(m);
65 }
66
67 int count = N;
68 if (fFlags & kBig_Flag) {
69 count >>= 2;
70 }
tomhudson@google.com6e8d3352011-06-22 17:16:35 +000071 count >>= (3 * complexity());
reed@google.comd34658a2011-04-11 13:12:51 +000072
73 for (int i = 0; i < count; i++) {
74 canvas->drawPath(path, paint);
75 }
76 }
77
78private:
79 typedef SkBenchmark INHERITED;
80};
81
82class TrianglePathBench : public PathBench {
83public:
84 TrianglePathBench(void* param, Flags flags) : INHERITED(param, flags) {}
rmistry@google.comfbfcd562012-08-23 18:09:54 +000085
bsalomon@google.com1647a192012-04-11 15:34:46 +000086 virtual void appendName(SkString* name) SK_OVERRIDE {
reed@google.comd34658a2011-04-11 13:12:51 +000087 name->append("triangle");
88 }
bsalomon@google.com1647a192012-04-11 15:34:46 +000089 virtual void makePath(SkPath* path) SK_OVERRIDE {
reed@google.comd34658a2011-04-11 13:12:51 +000090 static const int gCoord[] = {
91 10, 10, 15, 5, 20, 20
92 };
93 path->moveTo(SkIntToScalar(gCoord[0]), SkIntToScalar(gCoord[1]));
94 path->lineTo(SkIntToScalar(gCoord[2]), SkIntToScalar(gCoord[3]));
95 path->lineTo(SkIntToScalar(gCoord[4]), SkIntToScalar(gCoord[5]));
96 path->close();
97 }
98private:
99 typedef PathBench INHERITED;
100};
101
102class RectPathBench : public PathBench {
103public:
104 RectPathBench(void* param, Flags flags) : INHERITED(param, flags) {}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000105
bsalomon@google.com1647a192012-04-11 15:34:46 +0000106 virtual void appendName(SkString* name) SK_OVERRIDE {
reed@google.comd34658a2011-04-11 13:12:51 +0000107 name->append("rect");
108 }
bsalomon@google.com1647a192012-04-11 15:34:46 +0000109 virtual void makePath(SkPath* path) SK_OVERRIDE {
reed@google.comd34658a2011-04-11 13:12:51 +0000110 SkRect r = { 10, 10, 20, 20 };
111 path->addRect(r);
112 }
113private:
114 typedef PathBench INHERITED;
115};
116
117class OvalPathBench : public PathBench {
118public:
119 OvalPathBench(void* param, Flags flags) : INHERITED(param, flags) {}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000120
bsalomon@google.com1647a192012-04-11 15:34:46 +0000121 virtual void appendName(SkString* name) SK_OVERRIDE {
reed@google.comd34658a2011-04-11 13:12:51 +0000122 name->append("oval");
123 }
bsalomon@google.com1647a192012-04-11 15:34:46 +0000124 virtual void makePath(SkPath* path) SK_OVERRIDE {
reed@google.comd34658a2011-04-11 13:12:51 +0000125 SkRect r = { 10, 10, 20, 20 };
126 path->addOval(r);
127 }
128private:
129 typedef PathBench INHERITED;
130};
131
bsalomon@google.com1647a192012-04-11 15:34:46 +0000132class CirclePathBench: public PathBench {
133public:
134 CirclePathBench(void* param, Flags flags) : INHERITED(param, flags) {}
135
136 virtual void appendName(SkString* name) SK_OVERRIDE {
137 name->append("circle");
138 }
139 virtual void makePath(SkPath* path) SK_OVERRIDE {
140 path->addCircle(SkIntToScalar(20), SkIntToScalar(20),
141 SkIntToScalar(10));
142 }
143private:
144 typedef PathBench INHERITED;
145};
146
reed@google.comd34658a2011-04-11 13:12:51 +0000147class SawToothPathBench : public PathBench {
148public:
149 SawToothPathBench(void* param, Flags flags) : INHERITED(param, flags) {}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000150
bsalomon@google.com1647a192012-04-11 15:34:46 +0000151 virtual void appendName(SkString* name) SK_OVERRIDE {
reed@google.comd34658a2011-04-11 13:12:51 +0000152 name->append("sawtooth");
153 }
154 virtual void makePath(SkPath* path) {
155 SkScalar x = SkIntToScalar(20);
156 SkScalar y = SkIntToScalar(20);
157 const SkScalar x0 = x;
158 const SkScalar dx = SK_Scalar1 * 5;
159 const SkScalar dy = SK_Scalar1 * 10;
160
161 path->moveTo(x, y);
162 for (int i = 0; i < 32; i++) {
163 x += dx;
164 path->lineTo(x, y - dy);
165 x += dx;
166 path->lineTo(x, y + dy);
167 }
168 path->lineTo(x, y + 2 * dy);
169 path->lineTo(x0, y + 2 * dy);
170 path->close();
171 }
bsalomon@google.com1647a192012-04-11 15:34:46 +0000172 virtual int complexity() SK_OVERRIDE { return 1; }
reed@google.comd34658a2011-04-11 13:12:51 +0000173private:
174 typedef PathBench INHERITED;
175};
176
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000177class LongCurvedPathBench : public PathBench {
178public:
179 LongCurvedPathBench(void * param, Flags flags)
180 : INHERITED(param, flags) {
181 }
182
bsalomon@google.com1647a192012-04-11 15:34:46 +0000183 virtual void appendName(SkString* name) SK_OVERRIDE {
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000184 name->append("long_curved");
185 }
bsalomon@google.com1647a192012-04-11 15:34:46 +0000186 virtual void makePath(SkPath* path) SK_OVERRIDE {
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000187 SkRandom rand (12);
188 int i;
189 for (i = 0; i < 100; i++) {
190 path->quadTo(SkScalarMul(rand.nextUScalar1(), SkIntToScalar(640)),
191 SkScalarMul(rand.nextUScalar1(), SkIntToScalar(480)),
192 SkScalarMul(rand.nextUScalar1(), SkIntToScalar(640)),
193 SkScalarMul(rand.nextUScalar1(), SkIntToScalar(480)));
194 }
195 path->close();
196 }
bsalomon@google.com1647a192012-04-11 15:34:46 +0000197 virtual int complexity() SK_OVERRIDE { return 2; }
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000198private:
199 typedef PathBench INHERITED;
200};
201
senorblanco@chromium.orge50f7362012-01-12 19:10:35 +0000202class LongLinePathBench : public PathBench {
203public:
204 LongLinePathBench(void * param, Flags flags)
205 : INHERITED(param, flags) {
206 }
207
bsalomon@google.com1647a192012-04-11 15:34:46 +0000208 virtual void appendName(SkString* name) SK_OVERRIDE {
senorblanco@chromium.orge50f7362012-01-12 19:10:35 +0000209 name->append("long_line");
210 }
bsalomon@google.com1647a192012-04-11 15:34:46 +0000211 virtual void makePath(SkPath* path) SK_OVERRIDE {
senorblanco@chromium.orge50f7362012-01-12 19:10:35 +0000212 SkRandom rand;
213 path->moveTo(rand.nextUScalar1() * 640, rand.nextUScalar1() * 480);
214 for (size_t i = 1; i < 100; i++) {
215 path->lineTo(rand.nextUScalar1() * 640, rand.nextUScalar1() * 480);
216 }
217 }
bsalomon@google.com1647a192012-04-11 15:34:46 +0000218 virtual int complexity() SK_OVERRIDE { return 2; }
senorblanco@chromium.orge50f7362012-01-12 19:10:35 +0000219private:
220 typedef PathBench INHERITED;
221};
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000222
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000223class RandomPathBench : public SkBenchmark {
224public:
225 RandomPathBench(void* param) : INHERITED(param) {
robertphillips@google.com83187a22012-09-13 16:39:08 +0000226 fIsRendering = false;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000227 }
228
229protected:
230 void createData(int minVerbs,
231 int maxVerbs,
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000232 bool allowMoves = true,
233 SkRect* bounds = NULL) {
234 SkRect tempBounds;
235 if (NULL == bounds) {
236 tempBounds.setXYWH(0, 0, SK_Scalar1, SK_Scalar1);
237 bounds = &tempBounds;
238 }
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000239 fVerbCnts.reset(kNumVerbCnts);
240 for (int i = 0; i < kNumVerbCnts; ++i) {
241 fVerbCnts[i] = fRandom.nextRangeU(minVerbs, maxVerbs + 1);
242 }
243 fVerbs.reset(kNumVerbs);
244 for (int i = 0; i < kNumVerbs; ++i) {
245 do {
246 fVerbs[i] = static_cast<SkPath::Verb>(fRandom.nextULessThan(SkPath::kDone_Verb));
247 } while (!allowMoves && SkPath::kMove_Verb == fVerbs[i]);
248 }
249 fPoints.reset(kNumPoints);
250 for (int i = 0; i < kNumPoints; ++i) {
251 fPoints[i].set(fRandom.nextRangeScalar(bounds->fLeft, bounds->fRight),
252 fRandom.nextRangeScalar(bounds->fTop, bounds->fBottom));
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000253 }
254 this->restartMakingPaths();
255 }
256
257 void restartMakingPaths() {
258 fCurrPath = 0;
259 fCurrVerb = 0;
260 fCurrPoint = 0;
261 }
262
263 void makePath(SkPath* path) {
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000264 int vCount = fVerbCnts[(fCurrPath++) & (kNumVerbCnts - 1)];
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000265 for (int v = 0; v < vCount; ++v) {
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000266 int verb = fVerbs[(fCurrVerb++) & (kNumVerbs - 1)];
267 switch (verb) {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000268 case SkPath::kMove_Verb:
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000269 path->moveTo(fPoints[(fCurrPoint++) & (kNumPoints - 1)]);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000270 break;
271 case SkPath::kLine_Verb:
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000272 path->lineTo(fPoints[(fCurrPoint++) & (kNumPoints - 1)]);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000273 break;
274 case SkPath::kQuad_Verb:
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000275 path->quadTo(fPoints[(fCurrPoint + 0) & (kNumPoints - 1)],
276 fPoints[(fCurrPoint + 1) & (kNumPoints - 1)]);
277 fCurrPoint += 2;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000278 break;
279 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;
314 SkRandom fRandom;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000315 typedef SkBenchmark INHERITED;
316};
317
318class PathCreateBench : public RandomPathBench {
319public:
320 PathCreateBench(void* param) : INHERITED(param) {
321 }
322
323protected:
324 enum { N = SkBENCHLOOP(5000) };
325
326 virtual const char* onGetName() SK_OVERRIDE {
327 return "path_create";
328 }
329
330 virtual void onPreDraw() SK_OVERRIDE {
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000331 this->createData(10, 100);
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000332 fPaths.reset(kPathCnt);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000333 }
334
335 virtual void onDraw(SkCanvas*) SK_OVERRIDE {
336 for (int i = 0; i < N; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000337 this->makePath(&fPaths[i & (kPathCnt - 1)]);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000338 }
339 this->restartMakingPaths();
340 }
341
342 virtual void onPostDraw() SK_OVERRIDE {
343 this->finishedMakingPaths();
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000344 fPaths.reset(0);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000345 }
346
347private:
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000348 enum {
349 // must be a pow 2
350 kPathCnt = 1 << 5,
351 };
352 SkAutoTArray<SkPath> fPaths;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000353
354 typedef RandomPathBench INHERITED;
355};
356
357class PathCopyBench : public RandomPathBench {
358public:
359 PathCopyBench(void* param) : INHERITED(param) {
360 }
361
362protected:
bsalomon@google.com62e41902012-08-13 16:59:21 +0000363 enum { N = SkBENCHLOOP(30000) };
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000364
365 virtual const char* onGetName() SK_OVERRIDE {
366 return "path_copy";
367 }
368 virtual void onPreDraw() SK_OVERRIDE {
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000369 this->createData(10, 100);
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000370 fPaths.reset(kPathCnt);
371 fCopies.reset(kPathCnt);
372 for (int i = 0; i < kPathCnt; ++i) {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000373 this->makePath(&fPaths[i]);
374 }
375 this->finishedMakingPaths();
376 }
377 virtual void onDraw(SkCanvas*) SK_OVERRIDE {
378 for (int i = 0; i < N; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000379 int idx = i & (kPathCnt - 1);
380 fCopies[idx] = fPaths[idx];
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000381 }
382 }
383 virtual void onPostDraw() SK_OVERRIDE {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000384 fPaths.reset(0);
385 fCopies.reset(0);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000386 }
387
388private:
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000389 enum {
390 // must be a pow 2
391 kPathCnt = 1 << 5,
392 };
393 SkAutoTArray<SkPath> fPaths;
394 SkAutoTArray<SkPath> fCopies;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000395
396 typedef RandomPathBench INHERITED;
397};
398
399class PathTransformBench : public RandomPathBench {
400public:
401 PathTransformBench(bool inPlace, void* param)
402 : INHERITED(param)
403 , fInPlace(inPlace) {
404 }
405
406protected:
407 enum { N = SkBENCHLOOP(30000) };
408
409 virtual const char* onGetName() SK_OVERRIDE {
410 return fInPlace ? "path_transform_in_place" : "path_transform_copy";
411 }
412
413 virtual void onPreDraw() SK_OVERRIDE {
414 fMatrix.setScale(5 * SK_Scalar1, 6 * SK_Scalar1);
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000415 this->createData(10, 100);
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000416 fPaths.reset(kPathCnt);
417 for (int i = 0; i < kPathCnt; ++i) {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000418 this->makePath(&fPaths[i]);
419 }
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000420 this->finishedMakingPaths();
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000421 if (!fInPlace) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000422 fTransformed.reset(kPathCnt);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000423 }
424 }
425
426 virtual void onDraw(SkCanvas*) SK_OVERRIDE {
427 if (fInPlace) {
428 for (int i = 0; i < N; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000429 fPaths[i & (kPathCnt - 1)].transform(fMatrix);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000430 }
431 } else {
432 for (int i = 0; i < N; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000433 int idx = i & (kPathCnt - 1);
434 fPaths[idx].transform(fMatrix, &fTransformed[idx]);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000435 }
436 }
437 }
438
439 virtual void onPostDraw() SK_OVERRIDE {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000440 fPaths.reset(0);
441 fTransformed.reset(0);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000442 }
443
444private:
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000445 enum {
446 // must be a pow 2
447 kPathCnt = 1 << 5,
448 };
449 SkAutoTArray<SkPath> fPaths;
450 SkAutoTArray<SkPath> fTransformed;
451
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000452 SkMatrix fMatrix;
453 bool fInPlace;
454 typedef RandomPathBench INHERITED;
455};
456
457class PathEqualityBench : public RandomPathBench {
458public:
459 PathEqualityBench(void* param)
460 : INHERITED(param) {
461 }
462
463protected:
464 enum { N = SkBENCHLOOP(40000) };
465
466 virtual const char* onGetName() SK_OVERRIDE {
467 return "path_equality_50%";
468 }
469
470 virtual void onPreDraw() SK_OVERRIDE {
471 fParity = 0;
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000472 this->createData(10, 100);
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000473 fPaths.reset(kPathCnt);
474 fCopies.reset(kPathCnt);
475 for (int i = 0; i < kPathCnt; ++i) {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000476 this->makePath(&fPaths[i]);
477 fCopies[i] = fPaths[i];
478 }
479 this->finishedMakingPaths();
480 }
481
482 virtual void onDraw(SkCanvas*) SK_OVERRIDE {
483 for (int i = 0; i < N; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000484 int idx = i & (kPathCnt - 1);
485 fParity ^= (fPaths[idx] == fCopies[idx & ~0x1]);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000486 }
487 }
488
489 virtual void onPostDraw() SK_OVERRIDE {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000490 fPaths.reset(0);
491 fCopies.reset(0);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000492 }
493
494private:
495 bool fParity; // attempt to keep compiler from optimizing out the ==
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000496 enum {
497 // must be a pow 2
498 kPathCnt = 1 << 5,
499 };
500 SkAutoTArray<SkPath> fPaths;
501 SkAutoTArray<SkPath> fCopies;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000502 typedef RandomPathBench INHERITED;
503};
504
505class SkBench_AddPathTest : public RandomPathBench {
506public:
507 enum AddType {
508 kAdd_AddType,
509 kAddTrans_AddType,
510 kAddMatrix_AddType,
511 kPathTo_AddType,
512 kReverseAdd_AddType,
513 kReversePathTo_AddType,
514 };
515
516 SkBench_AddPathTest(AddType type, void* param)
517 : INHERITED(param)
518 , fType(type) {
519 fMatrix.setRotate(60 * SK_Scalar1);
520 }
521
522protected:
523 enum { N = SkBENCHLOOP(15000) };
524
525 virtual const char* onGetName() SK_OVERRIDE {
526 switch (fType) {
527 case kAdd_AddType:
528 return "path_add_path";
529 case kAddTrans_AddType:
530 return "path_add_path_trans";
531 case kAddMatrix_AddType:
532 return "path_add_path_matrix";
533 case kPathTo_AddType:
534 return "path_path_to";
535 case kReverseAdd_AddType:
536 return "path_reverse_add_path";
537 case kReversePathTo_AddType:
538 return "path_reverse_path_to";
539 default:
540 SkDEBUGFAIL("Bad add type");
541 return "";
542 }
543 }
544
545 virtual void onPreDraw() SK_OVERRIDE {
546 // pathTo and reversePathTo assume a single contour path.
547 bool allowMoves = kPathTo_AddType != fType &&
548 kReversePathTo_AddType != fType;
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000549 this->createData(10, 100, allowMoves);
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000550 fPaths0.reset(kPathCnt);
551 fPaths1.reset(kPathCnt);
552 for (int i = 0; i < kPathCnt; ++i) {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000553 this->makePath(&fPaths0[i]);
554 this->makePath(&fPaths1[i]);
555 }
556 this->finishedMakingPaths();
557 }
558
559 virtual void onDraw(SkCanvas*) SK_OVERRIDE {
560 switch (fType) {
561 case kAdd_AddType:
562 for (int i = 0; i < N; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000563 int idx = i & (kPathCnt - 1);
564 SkPath result = fPaths0[idx];
565 result.addPath(fPaths1[idx]);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000566 }
567 break;
568 case kAddTrans_AddType:
569 for (int i = 0; i < N; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000570 int idx = i & (kPathCnt - 1);
571 SkPath result = fPaths0[idx];
572 result.addPath(fPaths1[idx], 2 * SK_Scalar1, 5 * SK_Scalar1);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000573 }
574 break;
575 case kAddMatrix_AddType:
576 for (int i = 0; i < N; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000577 int idx = i & (kPathCnt - 1);
578 SkPath result = fPaths0[idx];
579 result.addPath(fPaths1[idx], fMatrix);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000580 }
581 break;
582 case kPathTo_AddType:
583 for (int i = 0; i < N; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000584 int idx = i & (kPathCnt - 1);
585 SkPath result = fPaths0[idx];
586 result.pathTo(fPaths1[idx]);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000587 }
588 break;
589 case kReverseAdd_AddType:
590 for (int i = 0; i < N; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000591 int idx = i & (kPathCnt - 1);
592 SkPath result = fPaths0[idx];
593 result.reverseAddPath(fPaths1[idx]);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000594 }
595 break;
596 case kReversePathTo_AddType:
597 for (int i = 0; i < N; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000598 int idx = i & (kPathCnt - 1);
599 SkPath result = fPaths0[idx];
600 result.reversePathTo(fPaths1[idx]);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000601 }
602 break;
603 }
604 }
605
606 virtual void onPostDraw() SK_OVERRIDE {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000607 fPaths0.reset(0);
608 fPaths1.reset(0);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000609 }
610
611private:
612 AddType fType; // or reverseAddPath
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000613 enum {
614 // must be a pow 2
615 kPathCnt = 1 << 5,
616 };
617 SkAutoTArray<SkPath> fPaths0;
618 SkAutoTArray<SkPath> fPaths1;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000619 SkMatrix fMatrix;
620 typedef RandomPathBench INHERITED;
621};
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000622
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000623
624class CirclesBench : public SkBenchmark {
625protected:
626 SkString fName;
627
628 enum {
629 N = SkBENCHLOOP(100)
630 };
631public:
632 CirclesBench(void* param) : INHERITED(param) {
633 fName.printf("circles");
634 }
635
636protected:
637 virtual const char* onGetName() SK_OVERRIDE {
638 return fName.c_str();
639 }
640
641 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
642 SkPaint paint;
643
644 paint.setColor(SK_ColorBLACK);
645 paint.setAntiAlias(true);
646
647 SkRandom rand;
648
649 SkRect r;
650
651 for (int i = 0; i < 5000; ++i) {
652 SkScalar radius = rand.nextUScalar1() * 3;
653 r.fLeft = rand.nextUScalar1() * 300;
654 r.fTop = rand.nextUScalar1() * 300;
655 r.fRight = r.fLeft + 2 * radius;
656 r.fBottom = r.fTop + 2 * radius;
657
658 SkPath temp;
659
660 // mimic how Chrome does circles
661 temp.arcTo(r, 0, 0, false);
662 temp.addOval(r, SkPath::kCCW_Direction);
663 temp.arcTo(r, 360, 0, true);
664 temp.close();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000665
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000666 canvas->drawPath(temp, paint);
667 }
668 }
669
670private:
671 typedef SkBenchmark INHERITED;
672};
673
robertphillips@google.com158618e2012-10-23 16:56:56 +0000674// Chrome creates its own round rects with each corner possibly being different.
675// In its "zero radius" incarnation it creates degenerate round rects.
skia.committer@gmail.com1e34ff72012-10-24 02:01:24 +0000676// Note: PathTest::test_arb_round_rect_is_convex and
robertphillips@google.com158618e2012-10-23 16:56:56 +0000677// test_arb_zero_rad_round_rect_is_rect perform almost exactly
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000678// the same test (but with no drawing)
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000679class ArbRoundRectBench : public SkBenchmark {
680protected:
681 SkString fName;
682
683 enum {
684 N = SkBENCHLOOP(100)
685 };
686public:
robertphillips@google.com158618e2012-10-23 16:56:56 +0000687 ArbRoundRectBench(void* param, bool zeroRad) : INHERITED(param), fZeroRad(zeroRad) {
688 if (zeroRad) {
689 fName.printf("zeroradroundrect");
690 } else {
691 fName.printf("arbroundrect");
692 }
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000693 }
694
695protected:
696 virtual const char* onGetName() SK_OVERRIDE {
697 return fName.c_str();
698 }
699
skia.committer@gmail.com989a95e2012-10-18 02:01:23 +0000700 static void add_corner_arc(SkPath* path, const SkRect& rect,
701 SkScalar xIn, SkScalar yIn,
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000702 int startAngle)
703 {
704
705 SkScalar rx = SkMinScalar(rect.width(), xIn);
706 SkScalar ry = SkMinScalar(rect.height(), yIn);
707
708 SkRect arcRect;
709 arcRect.set(-rx, -ry, rx, ry);
710 switch (startAngle) {
711 case 0:
712 arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom);
713 break;
714 case 90:
715 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom);
716 break;
717 case 180:
718 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop);
719 break;
720 case 270:
721 arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop);
722 break;
723 default:
724 break;
725 }
726
727 path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false);
728 }
729
skia.committer@gmail.com989a95e2012-10-18 02:01:23 +0000730 static void make_arb_round_rect(SkPath* path, const SkRect& r,
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000731 SkScalar xCorner, SkScalar yCorner) {
732 // we are lazy here and use the same x & y for each corner
733 add_corner_arc(path, r, xCorner, yCorner, 270);
734 add_corner_arc(path, r, xCorner, yCorner, 0);
735 add_corner_arc(path, r, xCorner, yCorner, 90);
736 add_corner_arc(path, r, xCorner, yCorner, 180);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000737 path->close();
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000738
robertphillips@google.comc7a37c72012-10-19 01:26:18 +0000739#ifdef SK_REDEFINE_ROOT2OVER2_TO_MAKE_ARCTOS_CONVEX
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000740 SkASSERT(path->isConvex());
robertphillips@google.comc7a37c72012-10-19 01:26:18 +0000741#endif
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000742 }
743
744 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
745 SkRandom rand;
746 SkRect r;
747
748 for (int i = 0; i < 5000; ++i) {
749 SkPaint paint;
750 paint.setColor(0xff000000 | rand.nextU());
751 paint.setAntiAlias(true);
752
robertphillips@google.com158618e2012-10-23 16:56:56 +0000753 SkScalar size = rand.nextUScalar1() * 30;
754 if (size < SK_Scalar1) {
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000755 continue;
756 }
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000757 r.fLeft = rand.nextUScalar1() * 300;
758 r.fTop = rand.nextUScalar1() * 300;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000759 r.fRight = r.fLeft + 2 * size;
760 r.fBottom = r.fTop + 2 * size;
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000761
762 SkPath temp;
763
robertphillips@google.com158618e2012-10-23 16:56:56 +0000764 if (fZeroRad) {
765 make_arb_round_rect(&temp, r, 0, 0);
766
767 SkASSERT(temp.isRect(NULL));
768 } else {
769 make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15);
770 }
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000771
772 canvas->drawPath(temp, paint);
773 }
774 }
775
776private:
robertphillips@google.com158618e2012-10-23 16:56:56 +0000777 bool fZeroRad; // should 0 radius rounds rects be tested?
778
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000779 typedef SkBenchmark INHERITED;
780};
781
reed@google.comd34658a2011-04-11 13:12:51 +0000782static SkBenchmark* FactT00(void* p) { return new TrianglePathBench(p, FLAGS00); }
783static SkBenchmark* FactT01(void* p) { return new TrianglePathBench(p, FLAGS01); }
784static SkBenchmark* FactT10(void* p) { return new TrianglePathBench(p, FLAGS10); }
785static SkBenchmark* FactT11(void* p) { return new TrianglePathBench(p, FLAGS11); }
786
787static SkBenchmark* FactR00(void* p) { return new RectPathBench(p, FLAGS00); }
788static SkBenchmark* FactR01(void* p) { return new RectPathBench(p, FLAGS01); }
789static SkBenchmark* FactR10(void* p) { return new RectPathBench(p, FLAGS10); }
790static SkBenchmark* FactR11(void* p) { return new RectPathBench(p, FLAGS11); }
791
792static SkBenchmark* FactO00(void* p) { return new OvalPathBench(p, FLAGS00); }
793static SkBenchmark* FactO01(void* p) { return new OvalPathBench(p, FLAGS01); }
794static SkBenchmark* FactO10(void* p) { return new OvalPathBench(p, FLAGS10); }
795static SkBenchmark* FactO11(void* p) { return new OvalPathBench(p, FLAGS11); }
796
bsalomon@google.com1647a192012-04-11 15:34:46 +0000797static SkBenchmark* FactC00(void* p) { return new CirclePathBench(p, FLAGS00); }
798static SkBenchmark* FactC01(void* p) { return new CirclePathBench(p, FLAGS01); }
799static SkBenchmark* FactC10(void* p) { return new CirclePathBench(p, FLAGS10); }
800static SkBenchmark* FactC11(void* p) { return new CirclePathBench(p, FLAGS11); }
801
reed@google.comd34658a2011-04-11 13:12:51 +0000802static SkBenchmark* FactS00(void* p) { return new SawToothPathBench(p, FLAGS00); }
803static SkBenchmark* FactS01(void* p) { return new SawToothPathBench(p, FLAGS01); }
804
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000805static SkBenchmark* FactLC00(void* p) {
806 return new LongCurvedPathBench(p, FLAGS00);
807}
808static SkBenchmark* FactLC01(void* p) {
809 return new LongCurvedPathBench(p, FLAGS01);
810}
811
senorblanco@chromium.orge50f7362012-01-12 19:10:35 +0000812static SkBenchmark* FactLL00(void* p) {
813 return new LongLinePathBench(p, FLAGS00);
814}
815
816static SkBenchmark* FactLL01(void* p) {
817 return new LongLinePathBench(p, FLAGS01);
818}
819
reed@google.comd34658a2011-04-11 13:12:51 +0000820static BenchRegistry gRegT00(FactT00);
821static BenchRegistry gRegT01(FactT01);
822static BenchRegistry gRegT10(FactT10);
823static BenchRegistry gRegT11(FactT11);
824
825static BenchRegistry gRegR00(FactR00);
826static BenchRegistry gRegR01(FactR01);
827static BenchRegistry gRegR10(FactR10);
828static BenchRegistry gRegR11(FactR11);
829
830static BenchRegistry gRegO00(FactO00);
831static BenchRegistry gRegO01(FactO01);
832static BenchRegistry gRegO10(FactO10);
833static BenchRegistry gRegO11(FactO11);
834
bsalomon@google.com1647a192012-04-11 15:34:46 +0000835static BenchRegistry gRegC00(FactC00);
836static BenchRegistry gRegC01(FactC01);
837static BenchRegistry gRegC10(FactC10);
838static BenchRegistry gRegC11(FactC11);
839
reed@google.comd34658a2011-04-11 13:12:51 +0000840static BenchRegistry gRegS00(FactS00);
841static BenchRegistry gRegS01(FactS01);
842
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000843static BenchRegistry gRegLC00(FactLC00);
844static BenchRegistry gRegLC01(FactLC01);
845
senorblanco@chromium.orge50f7362012-01-12 19:10:35 +0000846static BenchRegistry gRegLL00(FactLL00);
847static BenchRegistry gRegLL01(FactLL01);
848
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000849static SkBenchmark* FactCreate(void* p) { return new PathCreateBench(p); }
850static BenchRegistry gRegCreate(FactCreate);
851
852static SkBenchmark* FactCopy(void* p) { return new PathCopyBench(p); }
853static BenchRegistry gRegCopy(FactCopy);
854
855static SkBenchmark* FactPathTransformInPlace(void* p) { return new PathTransformBench(true, p); }
856static BenchRegistry gRegPathTransformInPlace(FactPathTransformInPlace);
857
858static SkBenchmark* FactPathTransformCopy(void* p) { return new PathTransformBench(false, p); }
859static BenchRegistry gRegPathTransformCopy(FactPathTransformCopy);
860
861static SkBenchmark* FactEquality(void* p) { return new PathEqualityBench(p); }
862static BenchRegistry gRegEquality(FactEquality);
863
864static SkBenchmark* FactAdd(void* p) { return new SkBench_AddPathTest(SkBench_AddPathTest::kAdd_AddType, p); }
865static SkBenchmark* FactAddTrans(void* p) { return new SkBench_AddPathTest(SkBench_AddPathTest::kAddTrans_AddType, p); }
866static SkBenchmark* FactAddMatrix(void* p) { return new SkBench_AddPathTest(SkBench_AddPathTest::kAddMatrix_AddType, p); }
867static SkBenchmark* FactPathTo(void* p) { return new SkBench_AddPathTest(SkBench_AddPathTest::kPathTo_AddType, p); }
868static SkBenchmark* FactReverseAdd(void* p) { return new SkBench_AddPathTest(SkBench_AddPathTest::kReverseAdd_AddType, p); }
869static SkBenchmark* FactReverseTo(void* p) { return new SkBench_AddPathTest(SkBench_AddPathTest::kReversePathTo_AddType, p); }
870
871static BenchRegistry gRegAdd(FactAdd);
872static BenchRegistry gRegAddTrans(FactAddTrans);
873static BenchRegistry gRegAddMatrix(FactAddMatrix);
874static BenchRegistry gRegPathTo(FactPathTo);
875static BenchRegistry gRegReverseAdd(FactReverseAdd);
876static BenchRegistry gRegReverseTo(FactReverseTo);
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000877
878static SkBenchmark* CirclesTest(void* p) { return new CirclesBench(p); }
879static BenchRegistry gRegCirclesTest(CirclesTest);
880
robertphillips@google.com158618e2012-10-23 16:56:56 +0000881static SkBenchmark* ArbRoundRectTest(void* p) { return new ArbRoundRectBench(p, false); }
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000882static BenchRegistry gRegArbRoundRectTest(ArbRoundRectTest);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000883
884static SkBenchmark* ZeroRadRoundRectTest(void* p) { return new ArbRoundRectBench(p, true); }
885static BenchRegistry gRegZeroRadRoundRectTest(ZeroRadRoundRectTest);