blob: e11aad78f0dd8281a2f687ce539659e7cce8d218 [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 +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
28class PathBench : public SkBenchmark {
29 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:
bsalomon@google.com1647a192012-04-11 15:34:46 +000045 virtual 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
commit-bot@chromium.org33614712013-12-03 18:17:16 +000053 virtual 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:
77 typedef SkBenchmark INHERITED;
78};
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
bsalomon@google.com1647a192012-04-11 15:34:46 +000084 virtual void appendName(SkString* name) SK_OVERRIDE {
reed@google.comd34658a2011-04-11 13:12:51 +000085 name->append("triangle");
86 }
bsalomon@google.com1647a192012-04-11 15:34:46 +000087 virtual 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
bsalomon@google.com1647a192012-04-11 15:34:46 +0000104 virtual void appendName(SkString* name) SK_OVERRIDE {
reed@google.comd34658a2011-04-11 13:12:51 +0000105 name->append("rect");
106 }
bsalomon@google.com1647a192012-04-11 15:34:46 +0000107 virtual 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
bsalomon@google.com1647a192012-04-11 15:34:46 +0000119 virtual void appendName(SkString* name) SK_OVERRIDE {
reed@google.comd34658a2011-04-11 13:12:51 +0000120 name->append("oval");
121 }
bsalomon@google.com1647a192012-04-11 15:34:46 +0000122 virtual 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
134 virtual void appendName(SkString* name) SK_OVERRIDE {
135 name->append("circle");
136 }
137 virtual void makePath(SkPath* path) SK_OVERRIDE {
138 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
bsalomon@google.com1647a192012-04-11 15:34:46 +0000149 virtual 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 }
bsalomon@google.com1647a192012-04-11 15:34:46 +0000170 virtual 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
bsalomon@google.com1647a192012-04-11 15:34:46 +0000179 virtual void appendName(SkString* name) SK_OVERRIDE {
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000180 name->append("long_curved");
181 }
bsalomon@google.com1647a192012-04-11 15:34:46 +0000182 virtual 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 }
bsalomon@google.com1647a192012-04-11 15:34:46 +0000193 virtual 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
bsalomon@google.com1647a192012-04-11 15:34:46 +0000202 virtual void appendName(SkString* name) SK_OVERRIDE {
senorblanco@chromium.orge50f7362012-01-12 19:10:35 +0000203 name->append("long_line");
204 }
bsalomon@google.com1647a192012-04-11 15:34:46 +0000205 virtual 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 }
bsalomon@google.com1647a192012-04-11 15:34:46 +0000212 virtual 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
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000217class RandomPathBench : public SkBenchmark {
218public:
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000219 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
220 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;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000315 typedef SkBenchmark INHERITED;
316};
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:
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000324 virtual const char* onGetName() SK_OVERRIDE {
325 return "path_create";
326 }
327
328 virtual 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
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000332 virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
333 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
342 virtual void onPostDraw() SK_OVERRIDE {
343 this->finishedMakingPaths();
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000344 }
345
346private:
commit-bot@chromium.org8f881172014-01-06 20:19:14 +0000347 SkPath fPath;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000348
349 typedef RandomPathBench INHERITED;
350};
351
352class PathCopyBench : public RandomPathBench {
353public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000354 PathCopyBench() {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000355 }
356
357protected:
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000358 virtual const char* onGetName() SK_OVERRIDE {
359 return "path_copy";
360 }
361 virtual void onPreDraw() SK_OVERRIDE {
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000362 this->createData(10, 100);
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000363 fPaths.reset(kPathCnt);
364 fCopies.reset(kPathCnt);
365 for (int i = 0; i < kPathCnt; ++i) {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000366 this->makePath(&fPaths[i]);
367 }
368 this->finishedMakingPaths();
369 }
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000370 virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
371 for (int i = 0; i < loops; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000372 int idx = i & (kPathCnt - 1);
373 fCopies[idx] = fPaths[idx];
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000374 }
375 }
376 virtual void onPostDraw() SK_OVERRIDE {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000377 fPaths.reset(0);
378 fCopies.reset(0);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000379 }
380
381private:
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000382 enum {
383 // must be a pow 2
384 kPathCnt = 1 << 5,
385 };
386 SkAutoTArray<SkPath> fPaths;
387 SkAutoTArray<SkPath> fCopies;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000388
389 typedef RandomPathBench INHERITED;
390};
391
392class PathTransformBench : public RandomPathBench {
393public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000394 PathTransformBench(bool inPlace) : fInPlace(inPlace) {}
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000395
396protected:
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000397 virtual const char* onGetName() SK_OVERRIDE {
398 return fInPlace ? "path_transform_in_place" : "path_transform_copy";
399 }
400
401 virtual void onPreDraw() SK_OVERRIDE {
402 fMatrix.setScale(5 * SK_Scalar1, 6 * SK_Scalar1);
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000403 this->createData(10, 100);
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000404 fPaths.reset(kPathCnt);
405 for (int i = 0; i < kPathCnt; ++i) {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000406 this->makePath(&fPaths[i]);
407 }
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000408 this->finishedMakingPaths();
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000409 if (!fInPlace) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000410 fTransformed.reset(kPathCnt);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000411 }
412 }
413
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000414 virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000415 if (fInPlace) {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000416 for (int i = 0; i < loops; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000417 fPaths[i & (kPathCnt - 1)].transform(fMatrix);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000418 }
419 } else {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000420 for (int i = 0; i < loops; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000421 int idx = i & (kPathCnt - 1);
422 fPaths[idx].transform(fMatrix, &fTransformed[idx]);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000423 }
424 }
425 }
426
427 virtual void onPostDraw() SK_OVERRIDE {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000428 fPaths.reset(0);
429 fTransformed.reset(0);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000430 }
431
432private:
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000433 enum {
434 // must be a pow 2
435 kPathCnt = 1 << 5,
436 };
437 SkAutoTArray<SkPath> fPaths;
438 SkAutoTArray<SkPath> fTransformed;
439
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000440 SkMatrix fMatrix;
441 bool fInPlace;
442 typedef RandomPathBench INHERITED;
443};
444
445class PathEqualityBench : public RandomPathBench {
446public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000447 PathEqualityBench() { }
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000448
449protected:
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000450 virtual const char* onGetName() SK_OVERRIDE {
451 return "path_equality_50%";
452 }
453
454 virtual void onPreDraw() SK_OVERRIDE {
455 fParity = 0;
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000456 this->createData(10, 100);
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000457 fPaths.reset(kPathCnt);
458 fCopies.reset(kPathCnt);
459 for (int i = 0; i < kPathCnt; ++i) {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000460 this->makePath(&fPaths[i]);
461 fCopies[i] = fPaths[i];
462 }
463 this->finishedMakingPaths();
464 }
465
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000466 virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
467 for (int i = 0; i < loops; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000468 int idx = i & (kPathCnt - 1);
469 fParity ^= (fPaths[idx] == fCopies[idx & ~0x1]);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000470 }
471 }
472
473 virtual void onPostDraw() SK_OVERRIDE {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000474 fPaths.reset(0);
475 fCopies.reset(0);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000476 }
477
478private:
479 bool fParity; // attempt to keep compiler from optimizing out the ==
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000480 enum {
481 // must be a pow 2
482 kPathCnt = 1 << 5,
483 };
484 SkAutoTArray<SkPath> fPaths;
485 SkAutoTArray<SkPath> fCopies;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000486 typedef RandomPathBench INHERITED;
487};
488
489class SkBench_AddPathTest : public RandomPathBench {
490public:
491 enum AddType {
492 kAdd_AddType,
493 kAddTrans_AddType,
494 kAddMatrix_AddType,
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000495 kReverseAdd_AddType,
496 kReversePathTo_AddType,
497 };
498
mtklein@google.com410e6e82013-09-13 19:52:27 +0000499 SkBench_AddPathTest(AddType type) : fType(type) {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000500 fMatrix.setRotate(60 * SK_Scalar1);
501 }
502
503protected:
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000504 virtual const char* onGetName() SK_OVERRIDE {
505 switch (fType) {
506 case kAdd_AddType:
507 return "path_add_path";
508 case kAddTrans_AddType:
509 return "path_add_path_trans";
510 case kAddMatrix_AddType:
511 return "path_add_path_matrix";
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000512 case kReverseAdd_AddType:
513 return "path_reverse_add_path";
514 case kReversePathTo_AddType:
515 return "path_reverse_path_to";
516 default:
517 SkDEBUGFAIL("Bad add type");
518 return "";
519 }
520 }
521
522 virtual void onPreDraw() SK_OVERRIDE {
commit-bot@chromium.orga1a097e2013-11-14 16:53:22 +0000523 // reversePathTo assumes a single contour path.
524 bool allowMoves = kReversePathTo_AddType != fType;
bsalomon@google.com6d552ee2012-08-14 15:10:09 +0000525 this->createData(10, 100, allowMoves);
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000526 fPaths0.reset(kPathCnt);
527 fPaths1.reset(kPathCnt);
528 for (int i = 0; i < kPathCnt; ++i) {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000529 this->makePath(&fPaths0[i]);
530 this->makePath(&fPaths1[i]);
531 }
532 this->finishedMakingPaths();
533 }
534
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000535 virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000536 switch (fType) {
537 case kAdd_AddType:
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000538 for (int i = 0; i < loops; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000539 int idx = i & (kPathCnt - 1);
540 SkPath result = fPaths0[idx];
541 result.addPath(fPaths1[idx]);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000542 }
543 break;
544 case kAddTrans_AddType:
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000545 for (int i = 0; i < loops; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000546 int idx = i & (kPathCnt - 1);
547 SkPath result = fPaths0[idx];
548 result.addPath(fPaths1[idx], 2 * SK_Scalar1, 5 * SK_Scalar1);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000549 }
550 break;
551 case kAddMatrix_AddType:
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000552 for (int i = 0; i < loops; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000553 int idx = i & (kPathCnt - 1);
554 SkPath result = fPaths0[idx];
555 result.addPath(fPaths1[idx], fMatrix);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000556 }
557 break;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000558 case kReverseAdd_AddType:
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000559 for (int i = 0; i < loops; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000560 int idx = i & (kPathCnt - 1);
561 SkPath result = fPaths0[idx];
562 result.reverseAddPath(fPaths1[idx]);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000563 }
564 break;
565 case kReversePathTo_AddType:
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000566 for (int i = 0; i < loops; ++i) {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000567 int idx = i & (kPathCnt - 1);
568 SkPath result = fPaths0[idx];
569 result.reversePathTo(fPaths1[idx]);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000570 }
571 break;
572 }
573 }
574
575 virtual void onPostDraw() SK_OVERRIDE {
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000576 fPaths0.reset(0);
577 fPaths1.reset(0);
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000578 }
579
580private:
581 AddType fType; // or reverseAddPath
bsalomon@google.comb5e47032012-08-14 17:49:46 +0000582 enum {
583 // must be a pow 2
584 kPathCnt = 1 << 5,
585 };
586 SkAutoTArray<SkPath> fPaths0;
587 SkAutoTArray<SkPath> fPaths1;
bsalomon@google.com30e6d2c2012-08-13 14:03:31 +0000588 SkMatrix fMatrix;
589 typedef RandomPathBench INHERITED;
590};
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000591
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000592
593class CirclesBench : public SkBenchmark {
594protected:
595 SkString fName;
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000596 Flags fFlags;
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000597
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000598public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000599 CirclesBench(Flags flags) : fFlags(flags) {
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000600 fName.printf("circles_%s", fFlags & kStroke_Flag ? "stroke" : "fill");
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000601 }
602
603protected:
604 virtual const char* onGetName() SK_OVERRIDE {
605 return fName.c_str();
606 }
607
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000608 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000609 SkPaint paint;
610
611 paint.setColor(SK_ColorBLACK);
612 paint.setAntiAlias(true);
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000613 if (fFlags & kStroke_Flag) {
614 paint.setStyle(SkPaint::kStroke_Style);
615 }
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000616
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000617 SkRandom rand;
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000618
619 SkRect r;
620
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000621 for (int i = 0; i < loops; ++i) {
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000622 SkScalar radius = rand.nextUScalar1() * 3;
623 r.fLeft = rand.nextUScalar1() * 300;
624 r.fTop = rand.nextUScalar1() * 300;
625 r.fRight = r.fLeft + 2 * radius;
626 r.fBottom = r.fTop + 2 * radius;
627
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000628 if (fFlags & kStroke_Flag) {
629 paint.setStrokeWidth(rand.nextUScalar1() * 5.0f);
630 }
631
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000632 SkPath temp;
633
634 // mimic how Chrome does circles
635 temp.arcTo(r, 0, 0, false);
636 temp.addOval(r, SkPath::kCCW_Direction);
637 temp.arcTo(r, 360, 0, true);
638 temp.close();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000639
robertphillips@google.com17bb4582012-08-20 17:24:16 +0000640 canvas->drawPath(temp, paint);
641 }
642 }
643
644private:
645 typedef SkBenchmark INHERITED;
646};
647
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000648
robertphillips@google.com158618e2012-10-23 16:56:56 +0000649// Chrome creates its own round rects with each corner possibly being different.
650// In its "zero radius" incarnation it creates degenerate round rects.
skia.committer@gmail.com1e34ff72012-10-24 02:01:24 +0000651// Note: PathTest::test_arb_round_rect_is_convex and
robertphillips@google.com158618e2012-10-23 16:56:56 +0000652// test_arb_zero_rad_round_rect_is_rect perform almost exactly
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000653// the same test (but with no drawing)
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000654class ArbRoundRectBench : public SkBenchmark {
655protected:
656 SkString fName;
657
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000658public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000659 ArbRoundRectBench(bool zeroRad) : fZeroRad(zeroRad) {
robertphillips@google.com158618e2012-10-23 16:56:56 +0000660 if (zeroRad) {
661 fName.printf("zeroradroundrect");
662 } else {
663 fName.printf("arbroundrect");
664 }
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000665 }
666
667protected:
668 virtual const char* onGetName() SK_OVERRIDE {
669 return fName.c_str();
670 }
671
skia.committer@gmail.com989a95e2012-10-18 02:01:23 +0000672 static void add_corner_arc(SkPath* path, const SkRect& rect,
673 SkScalar xIn, SkScalar yIn,
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000674 int startAngle)
675 {
676
677 SkScalar rx = SkMinScalar(rect.width(), xIn);
678 SkScalar ry = SkMinScalar(rect.height(), yIn);
679
680 SkRect arcRect;
681 arcRect.set(-rx, -ry, rx, ry);
682 switch (startAngle) {
683 case 0:
684 arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom);
685 break;
686 case 90:
687 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom);
688 break;
689 case 180:
690 arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop);
691 break;
692 case 270:
693 arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop);
694 break;
695 default:
696 break;
697 }
698
699 path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false);
700 }
701
skia.committer@gmail.com989a95e2012-10-18 02:01:23 +0000702 static void make_arb_round_rect(SkPath* path, const SkRect& r,
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000703 SkScalar xCorner, SkScalar yCorner) {
704 // we are lazy here and use the same x & y for each corner
705 add_corner_arc(path, r, xCorner, yCorner, 270);
706 add_corner_arc(path, r, xCorner, yCorner, 0);
707 add_corner_arc(path, r, xCorner, yCorner, 90);
708 add_corner_arc(path, r, xCorner, yCorner, 180);
robertphillips@google.com158618e2012-10-23 16:56:56 +0000709 path->close();
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000710
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000711 SkASSERT(path->isConvex());
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000712 }
713
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000714 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000715 SkRandom rand;
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000716 SkRect r;
717
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000718 for (int i = 0; i < loops; ++i) {
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000719 SkPaint paint;
720 paint.setColor(0xff000000 | rand.nextU());
721 paint.setAntiAlias(true);
722
robertphillips@google.com158618e2012-10-23 16:56:56 +0000723 SkScalar size = rand.nextUScalar1() * 30;
724 if (size < SK_Scalar1) {
robertphillips@google.comb95eaa82012-10-18 15:26:12 +0000725 continue;
726 }
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000727 r.fLeft = rand.nextUScalar1() * 300;
728 r.fTop = rand.nextUScalar1() * 300;
robertphillips@google.com158618e2012-10-23 16:56:56 +0000729 r.fRight = r.fLeft + 2 * size;
730 r.fBottom = r.fTop + 2 * size;
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000731
732 SkPath temp;
733
robertphillips@google.com158618e2012-10-23 16:56:56 +0000734 if (fZeroRad) {
735 make_arb_round_rect(&temp, r, 0, 0);
736
737 SkASSERT(temp.isRect(NULL));
738 } else {
739 make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15);
740 }
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000741
742 canvas->drawPath(temp, paint);
743 }
744 }
745
746private:
robertphillips@google.com158618e2012-10-23 16:56:56 +0000747 bool fZeroRad; // should 0 radius rounds rects be tested?
748
robertphillips@google.comf6fc3fc2012-10-17 15:23:21 +0000749 typedef SkBenchmark INHERITED;
750};
751
bsalomon@google.com9bee33a2012-11-13 21:51:38 +0000752class ConservativelyContainsBench : public SkBenchmark {
753public:
754 enum Type {
755 kRect_Type,
756 kRoundRect_Type,
757 kOval_Type,
758 };
759
mtklein@google.com410e6e82013-09-13 19:52:27 +0000760 ConservativelyContainsBench(Type type) {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +0000761 fParity = false;
762 fName = "conservatively_contains_";
763 switch (type) {
764 case kRect_Type:
765 fName.append("rect");
766 fPath.addRect(kBaseRect);
767 break;
768 case kRoundRect_Type:
769 fName.append("round_rect");
770 fPath.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1]);
771 break;
772 case kOval_Type:
773 fName.append("oval");
774 fPath.addOval(kBaseRect);
775 break;
776 }
777 }
778
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000779 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
780 return backend == kNonRendering_Backend;
781 }
782
bsalomon@google.com9bee33a2012-11-13 21:51:38 +0000783private:
784 virtual const char* onGetName() SK_OVERRIDE {
785 return fName.c_str();
786 }
787
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000788 virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
789 for (int i = 0; i < loops; ++i) {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +0000790 const SkRect& rect = fQueryRects[i % kQueryRectCnt];
791 fParity = fParity != fPath.conservativelyContainsRect(rect);
792 }
793 }
794
795 virtual void onPreDraw() SK_OVERRIDE {
796 fQueryRects.setCount(kQueryRectCnt);
797
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000798 SkRandom rand;
bsalomon@google.com9bee33a2012-11-13 21:51:38 +0000799 for (int i = 0; i < kQueryRectCnt; ++i) {
800 SkSize size;
801 SkPoint xy;
802 size.fWidth = rand.nextRangeScalar(kQueryMin.fWidth, kQueryMax.fWidth);
803 size.fHeight = rand.nextRangeScalar(kQueryMin.fHeight, kQueryMax.fHeight);
804 xy.fX = rand.nextRangeScalar(kBounds.fLeft, kBounds.fRight - size.fWidth);
805 xy.fY = rand.nextRangeScalar(kBounds.fTop, kBounds.fBottom - size.fHeight);
806
807 fQueryRects[i] = SkRect::MakeXYWH(xy.fX, xy.fY, size.fWidth, size.fHeight);
808 }
809 }
810
811 virtual void onPostDraw() SK_OVERRIDE {
812 fQueryRects.setCount(0);
813 }
814
815 enum {
bsalomon@google.com9bee33a2012-11-13 21:51:38 +0000816 kQueryRectCnt = 400,
817 };
818 static const SkRect kBounds; // bounds for all random query rects
819 static const SkSize kQueryMin; // minimum query rect size, should be <= kQueryMax
820 static const SkSize kQueryMax; // max query rect size, should < kBounds
821 static const SkRect kBaseRect; // rect that is used to construct the path
822 static const SkScalar kRRRadii[2]; // x and y radii for round rect
823
824 SkString fName;
825 SkPath fPath;
826 bool fParity;
827 SkTDArray<SkRect> fQueryRects;
828
829 typedef SkBenchmark INHERITED;
830};
831
mike@reedtribe.org8d551012013-04-14 02:40:50 +0000832///////////////////////////////////////////////////////////////////////////////
833
834#include "SkGeometry.h"
835
mike@reedtribe.org28552e12013-04-26 00:58:29 +0000836class ConicBench_Chop5 : public SkBenchmark {
mike@reedtribe.org28552e12013-04-26 00:58:29 +0000837 SkConic fRQ;
mike@reedtribe.org8d551012013-04-14 02:40:50 +0000838public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000839 ConicBench_Chop5() {
mike@reedtribe.org8d551012013-04-14 02:40:50 +0000840 fRQ.fPts[0].set(0, 0);
841 fRQ.fPts[1].set(100, 0);
842 fRQ.fPts[2].set(100, 100);
843 fRQ.fW = SkScalarCos(SK_ScalarPI/4);
844 }
skia.committer@gmail.comab38e562013-04-14 07:01:08 +0000845
mike@reedtribe.org8d551012013-04-14 02:40:50 +0000846private:
847 virtual const char* onGetName() SK_OVERRIDE {
848 return "ratquad-chop-0.5";
849 }
skia.committer@gmail.comab38e562013-04-14 07:01:08 +0000850
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000851 virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
mike@reedtribe.org28552e12013-04-26 00:58:29 +0000852 SkConic dst[2];
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000853 for (int i = 0; i < loops; ++i) {
mike@reedtribe.org8d551012013-04-14 02:40:50 +0000854 fRQ.chopAt(0.5f, dst);
855 }
856 }
skia.committer@gmail.comab38e562013-04-14 07:01:08 +0000857
mike@reedtribe.org8d551012013-04-14 02:40:50 +0000858 typedef SkBenchmark INHERITED;
859};
860
mike@reedtribe.org28552e12013-04-26 00:58:29 +0000861class ConicBench_ChopHalf : public SkBenchmark {
mike@reedtribe.org28552e12013-04-26 00:58:29 +0000862 SkConic fRQ;
mike@reedtribe.org8d551012013-04-14 02:40:50 +0000863public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000864 ConicBench_ChopHalf() {
mike@reedtribe.org8d551012013-04-14 02:40:50 +0000865 fRQ.fPts[0].set(0, 0);
866 fRQ.fPts[1].set(100, 0);
867 fRQ.fPts[2].set(100, 100);
868 fRQ.fW = SkScalarCos(SK_ScalarPI/4);
869 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000870
mike@reedtribe.org8d551012013-04-14 02:40:50 +0000871private:
872 virtual const char* onGetName() SK_OVERRIDE {
873 return "ratquad-chop-half";
874 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000875
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000876 virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
mike@reedtribe.org28552e12013-04-26 00:58:29 +0000877 SkConic dst[2];
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000878 for (int i = 0; i < loops; ++i) {
mike@reedtribe.org8d551012013-04-14 02:40:50 +0000879 fRQ.chop(dst);
880 }
881 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000882
mike@reedtribe.org8d551012013-04-14 02:40:50 +0000883 typedef SkBenchmark INHERITED;
884};
885
886///////////////////////////////////////////////////////////////////////////////
887
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000888static void rand_conic(SkConic* conic, SkRandom& rand) {
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000889 for (int i = 0; i < 3; ++i) {
890 conic->fPts[i].set(rand.nextUScalar1() * 100, rand.nextUScalar1() * 100);
891 }
892 if (rand.nextUScalar1() > 0.5f) {
893 conic->fW = rand.nextUScalar1();
894 } else {
895 conic->fW = 1 + rand.nextUScalar1() * 4;
896 }
897}
898
899class ConicBench : public SkBenchmark {
900public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000901 ConicBench() {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000902 SkRandom rand;
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000903 for (int i = 0; i < CONICS; ++i) {
904 rand_conic(&fConics[i], rand);
905 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000906 }
907
908 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
909 return backend == kNonRendering_Backend;
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000910 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000911
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000912protected:
913 enum {
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000914 CONICS = 100
915 };
916 SkConic fConics[CONICS];
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000917
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000918private:
919 typedef SkBenchmark INHERITED;
920};
921
922class ConicBench_ComputeError : public ConicBench {
923public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000924 ConicBench_ComputeError() {}
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000925
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000926protected:
927 virtual const char* onGetName() SK_OVERRIDE {
928 return "conic-compute-error";
929 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000930
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000931 virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000932 SkVector err;
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000933 for (int i = 0; i < loops; ++i) {
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000934 for (int j = 0; j < CONICS; ++j) {
935 fConics[j].computeAsQuadError(&err);
936 }
937 }
938 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000939
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000940private:
941 typedef ConicBench INHERITED;
942};
943
944class ConicBench_asQuadTol : public ConicBench {
945public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000946 ConicBench_asQuadTol() {}
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000947
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000948protected:
949 virtual const char* onGetName() SK_OVERRIDE {
950 return "conic-asQuadTol";
951 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000952
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000953 virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
954 for (int i = 0; i < loops; ++i) {
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000955 for (int j = 0; j < CONICS; ++j) {
956 fConics[j].asQuadTol(SK_ScalarHalf);
957 }
958 }
959 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000960
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000961private:
962 typedef ConicBench INHERITED;
963};
964
965class ConicBench_quadPow2 : public ConicBench {
966public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000967 ConicBench_quadPow2() {}
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000968
969protected:
970 virtual const char* onGetName() SK_OVERRIDE {
971 return "conic-quadPow2";
972 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000973
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000974 virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
975 for (int i = 0; i < loops; ++i) {
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000976 for (int j = 0; j < CONICS; ++j) {
977 fConics[j].computeQuadPOW2(SK_ScalarHalf);
978 }
979 }
980 }
skia.committer@gmail.com81521132013-04-30 07:01:03 +0000981
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +0000982private:
983 typedef ConicBench INHERITED;
984};
985
986///////////////////////////////////////////////////////////////////////////////
987
bsalomon@google.com9bee33a2012-11-13 21:51:38 +0000988const SkRect ConservativelyContainsBench::kBounds = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
989const SkSize ConservativelyContainsBench::kQueryMin = SkSize::Make(SkIntToScalar(1), SkIntToScalar(1));
990const SkSize ConservativelyContainsBench::kQueryMax = SkSize::Make(SkIntToScalar(40), SkIntToScalar(40));
991const SkRect ConservativelyContainsBench::kBaseRect = SkRect::MakeXYWH(SkIntToScalar(25), SkIntToScalar(25), SkIntToScalar(50), SkIntToScalar(50));
992const SkScalar ConservativelyContainsBench::kRRRadii[2] = {SkIntToScalar(5), SkIntToScalar(10)};
993
mtklein@google.com410e6e82013-09-13 19:52:27 +0000994DEF_BENCH( return new TrianglePathBench(FLAGS00); )
995DEF_BENCH( return new TrianglePathBench(FLAGS01); )
996DEF_BENCH( return new TrianglePathBench(FLAGS10); )
997DEF_BENCH( return new TrianglePathBench(FLAGS11); )
reed@google.comd34658a2011-04-11 13:12:51 +0000998
mtklein@google.com410e6e82013-09-13 19:52:27 +0000999DEF_BENCH( return new RectPathBench(FLAGS00); )
1000DEF_BENCH( return new RectPathBench(FLAGS01); )
1001DEF_BENCH( return new RectPathBench(FLAGS10); )
1002DEF_BENCH( return new RectPathBench(FLAGS11); )
reed@google.comd34658a2011-04-11 13:12:51 +00001003
mtklein@google.com410e6e82013-09-13 19:52:27 +00001004DEF_BENCH( return new OvalPathBench(FLAGS00); )
1005DEF_BENCH( return new OvalPathBench(FLAGS01); )
1006DEF_BENCH( return new OvalPathBench(FLAGS10); )
1007DEF_BENCH( return new OvalPathBench(FLAGS11); )
reed@google.comd34658a2011-04-11 13:12:51 +00001008
mtklein@google.com410e6e82013-09-13 19:52:27 +00001009DEF_BENCH( return new CirclePathBench(FLAGS00); )
1010DEF_BENCH( return new CirclePathBench(FLAGS01); )
1011DEF_BENCH( return new CirclePathBench(FLAGS10); )
1012DEF_BENCH( return new CirclePathBench(FLAGS11); )
bsalomon@google.com1647a192012-04-11 15:34:46 +00001013
mtklein@google.com410e6e82013-09-13 19:52:27 +00001014DEF_BENCH( return new SawToothPathBench(FLAGS00); )
1015DEF_BENCH( return new SawToothPathBench(FLAGS01); )
reed@google.comd34658a2011-04-11 13:12:51 +00001016
mtklein@google.com410e6e82013-09-13 19:52:27 +00001017DEF_BENCH( return new LongCurvedPathBench(FLAGS00); )
1018DEF_BENCH( return new LongCurvedPathBench(FLAGS01); )
1019DEF_BENCH( return new LongLinePathBench(FLAGS00); )
1020DEF_BENCH( return new LongLinePathBench(FLAGS01); )
tomhudson@google.com6e8d3352011-06-22 17:16:35 +00001021
mtklein@google.com410e6e82013-09-13 19:52:27 +00001022DEF_BENCH( return new PathCreateBench(); )
1023DEF_BENCH( return new PathCopyBench(); )
1024DEF_BENCH( return new PathTransformBench(true); )
1025DEF_BENCH( return new PathTransformBench(false); )
1026DEF_BENCH( return new PathEqualityBench(); )
senorblanco@chromium.orge50f7362012-01-12 19:10:35 +00001027
mtklein@google.com410e6e82013-09-13 19:52:27 +00001028DEF_BENCH( return new SkBench_AddPathTest(SkBench_AddPathTest::kAdd_AddType); )
1029DEF_BENCH( return new SkBench_AddPathTest(SkBench_AddPathTest::kAddTrans_AddType); )
1030DEF_BENCH( return new SkBench_AddPathTest(SkBench_AddPathTest::kAddMatrix_AddType); )
mtklein@google.com410e6e82013-09-13 19:52:27 +00001031DEF_BENCH( return new SkBench_AddPathTest(SkBench_AddPathTest::kReverseAdd_AddType); )
1032DEF_BENCH( return new SkBench_AddPathTest(SkBench_AddPathTest::kReversePathTo_AddType); )
senorblanco@chromium.orge50f7362012-01-12 19:10:35 +00001033
mtklein@google.com410e6e82013-09-13 19:52:27 +00001034DEF_BENCH( return new CirclesBench(FLAGS00); )
1035DEF_BENCH( return new CirclesBench(FLAGS01); )
1036DEF_BENCH( return new ArbRoundRectBench(false); )
1037DEF_BENCH( return new ArbRoundRectBench(true); )
1038DEF_BENCH( return new ConservativelyContainsBench(ConservativelyContainsBench::kRect_Type); )
1039DEF_BENCH( return new ConservativelyContainsBench(ConservativelyContainsBench::kRoundRect_Type); )
1040DEF_BENCH( return new ConservativelyContainsBench(ConservativelyContainsBench::kOval_Type); )
mike@reedtribe.org8d551012013-04-14 02:40:50 +00001041
mtklein@google.com410e6e82013-09-13 19:52:27 +00001042DEF_BENCH( return new ConicBench_Chop5() )
1043DEF_BENCH( return new ConicBench_ChopHalf() )
1044DEF_BENCH( return new ConicBench_ComputeError() )
1045DEF_BENCH( return new ConicBench_asQuadTol() )
1046DEF_BENCH( return new ConicBench_quadPow2() )