blob: d3437c41f9544e5b8393319ba59b07302ba2b03b [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"
16
17enum Flags {
18 kStroke_Flag = 1 << 0,
19 kBig_Flag = 1 << 1
20};
21
22#define FLAGS00 Flags(0)
23#define FLAGS01 Flags(kStroke_Flag)
24#define FLAGS10 Flags(kBig_Flag)
25#define FLAGS11 Flags(kStroke_Flag | kBig_Flag)
26
27class PathBench : public SkBenchmark {
28 SkPaint fPaint;
29 SkString fName;
30 Flags fFlags;
tomhudson@google.comca529d32011-10-28 15:34:49 +000031 enum { N = SkBENCHLOOP(1000) };
reed@google.comd34658a2011-04-11 13:12:51 +000032public:
33 PathBench(void* param, Flags flags) : INHERITED(param), fFlags(flags) {
34 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
bsalomon@google.com1647a192012-04-11 15:34:46 +000053 virtual void onDraw(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
65 int count = N;
66 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:
82 TrianglePathBench(void* param, Flags flags) : INHERITED(param, flags) {}
83
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:
102 RectPathBench(void* param, Flags flags) : INHERITED(param, flags) {}
103
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:
117 OvalPathBench(void* param, Flags flags) : INHERITED(param, flags) {}
118
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 {
reed@google.comd34658a2011-04-11 13:12:51 +0000123 SkRect r = { 10, 10, 20, 20 };
124 path->addOval(r);
125 }
126private:
127 typedef PathBench INHERITED;
128};
129
bsalomon@google.com1647a192012-04-11 15:34:46 +0000130class CirclePathBench: public PathBench {
131public:
132 CirclePathBench(void* param, Flags flags) : INHERITED(param, flags) {}
133
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:
147 SawToothPathBench(void* param, Flags flags) : INHERITED(param, flags) {}
148
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:
177 LongCurvedPathBench(void * param, Flags flags)
178 : INHERITED(param, flags) {
179 }
180
bsalomon@google.com1647a192012-04-11 15:34:46 +0000181 virtual void appendName(SkString* name) SK_OVERRIDE {
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000182 name->append("long_curved");
183 }
bsalomon@google.com1647a192012-04-11 15:34:46 +0000184 virtual void makePath(SkPath* path) SK_OVERRIDE {
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000185 SkRandom rand (12);
186 int i;
187 for (i = 0; i < 100; i++) {
188 path->quadTo(SkScalarMul(rand.nextUScalar1(), SkIntToScalar(640)),
189 SkScalarMul(rand.nextUScalar1(), SkIntToScalar(480)),
190 SkScalarMul(rand.nextUScalar1(), SkIntToScalar(640)),
191 SkScalarMul(rand.nextUScalar1(), SkIntToScalar(480)));
192 }
193 path->close();
194 }
bsalomon@google.com1647a192012-04-11 15:34:46 +0000195 virtual int complexity() SK_OVERRIDE { return 2; }
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000196private:
197 typedef PathBench INHERITED;
198};
199
senorblanco@chromium.orge50f7362012-01-12 19:10:35 +0000200class LongLinePathBench : public PathBench {
201public:
202 LongLinePathBench(void * param, Flags flags)
203 : INHERITED(param, flags) {
204 }
205
bsalomon@google.com1647a192012-04-11 15:34:46 +0000206 virtual void appendName(SkString* name) SK_OVERRIDE {
senorblanco@chromium.orge50f7362012-01-12 19:10:35 +0000207 name->append("long_line");
208 }
bsalomon@google.com1647a192012-04-11 15:34:46 +0000209 virtual void makePath(SkPath* path) SK_OVERRIDE {
senorblanco@chromium.orge50f7362012-01-12 19:10:35 +0000210 SkRandom rand;
211 path->moveTo(rand.nextUScalar1() * 640, rand.nextUScalar1() * 480);
212 for (size_t i = 1; i < 100; i++) {
213 path->lineTo(rand.nextUScalar1() * 640, rand.nextUScalar1() * 480);
214 }
215 }
bsalomon@google.com1647a192012-04-11 15:34:46 +0000216 virtual int complexity() SK_OVERRIDE { return 2; }
senorblanco@chromium.orge50f7362012-01-12 19:10:35 +0000217private:
218 typedef PathBench INHERITED;
219};
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000220
221
reed@google.comd34658a2011-04-11 13:12:51 +0000222static SkBenchmark* FactT00(void* p) { return new TrianglePathBench(p, FLAGS00); }
223static SkBenchmark* FactT01(void* p) { return new TrianglePathBench(p, FLAGS01); }
224static SkBenchmark* FactT10(void* p) { return new TrianglePathBench(p, FLAGS10); }
225static SkBenchmark* FactT11(void* p) { return new TrianglePathBench(p, FLAGS11); }
226
227static SkBenchmark* FactR00(void* p) { return new RectPathBench(p, FLAGS00); }
228static SkBenchmark* FactR01(void* p) { return new RectPathBench(p, FLAGS01); }
229static SkBenchmark* FactR10(void* p) { return new RectPathBench(p, FLAGS10); }
230static SkBenchmark* FactR11(void* p) { return new RectPathBench(p, FLAGS11); }
231
232static SkBenchmark* FactO00(void* p) { return new OvalPathBench(p, FLAGS00); }
233static SkBenchmark* FactO01(void* p) { return new OvalPathBench(p, FLAGS01); }
234static SkBenchmark* FactO10(void* p) { return new OvalPathBench(p, FLAGS10); }
235static SkBenchmark* FactO11(void* p) { return new OvalPathBench(p, FLAGS11); }
236
bsalomon@google.com1647a192012-04-11 15:34:46 +0000237static SkBenchmark* FactC00(void* p) { return new CirclePathBench(p, FLAGS00); }
238static SkBenchmark* FactC01(void* p) { return new CirclePathBench(p, FLAGS01); }
239static SkBenchmark* FactC10(void* p) { return new CirclePathBench(p, FLAGS10); }
240static SkBenchmark* FactC11(void* p) { return new CirclePathBench(p, FLAGS11); }
241
reed@google.comd34658a2011-04-11 13:12:51 +0000242static SkBenchmark* FactS00(void* p) { return new SawToothPathBench(p, FLAGS00); }
243static SkBenchmark* FactS01(void* p) { return new SawToothPathBench(p, FLAGS01); }
244
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000245static SkBenchmark* FactLC00(void* p) {
246 return new LongCurvedPathBench(p, FLAGS00);
247}
248static SkBenchmark* FactLC01(void* p) {
249 return new LongCurvedPathBench(p, FLAGS01);
250}
251
senorblanco@chromium.orge50f7362012-01-12 19:10:35 +0000252static SkBenchmark* FactLL00(void* p) {
253 return new LongLinePathBench(p, FLAGS00);
254}
255
256static SkBenchmark* FactLL01(void* p) {
257 return new LongLinePathBench(p, FLAGS01);
258}
259
reed@google.comd34658a2011-04-11 13:12:51 +0000260static BenchRegistry gRegT00(FactT00);
261static BenchRegistry gRegT01(FactT01);
262static BenchRegistry gRegT10(FactT10);
263static BenchRegistry gRegT11(FactT11);
264
265static BenchRegistry gRegR00(FactR00);
266static BenchRegistry gRegR01(FactR01);
267static BenchRegistry gRegR10(FactR10);
268static BenchRegistry gRegR11(FactR11);
269
270static BenchRegistry gRegO00(FactO00);
271static BenchRegistry gRegO01(FactO01);
272static BenchRegistry gRegO10(FactO10);
273static BenchRegistry gRegO11(FactO11);
274
bsalomon@google.com1647a192012-04-11 15:34:46 +0000275static BenchRegistry gRegC00(FactC00);
276static BenchRegistry gRegC01(FactC01);
277static BenchRegistry gRegC10(FactC10);
278static BenchRegistry gRegC11(FactC11);
279
reed@google.comd34658a2011-04-11 13:12:51 +0000280static BenchRegistry gRegS00(FactS00);
281static BenchRegistry gRegS01(FactS01);
282
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000283static BenchRegistry gRegLC00(FactLC00);
284static BenchRegistry gRegLC01(FactLC01);
285
senorblanco@chromium.orge50f7362012-01-12 19:10:35 +0000286static BenchRegistry gRegLL00(FactLL00);
287static BenchRegistry gRegLL01(FactLL01);
288