blob: f9de53c9f063af64c4a75993c262ff20af7f9d9c [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:
45 virtual const char* onGetName() {
46 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
53 virtual void onDraw(SkCanvas* canvas) {
54 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
84 virtual void appendName(SkString* name) {
85 name->append("triangle");
86 }
87 virtual void makePath(SkPath* path) {
88 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
104 virtual void appendName(SkString* name) {
105 name->append("rect");
106 }
107 virtual void makePath(SkPath* path) {
108 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
119 virtual void appendName(SkString* name) {
120 name->append("oval");
121 }
122 virtual void makePath(SkPath* path) {
123 SkRect r = { 10, 10, 20, 20 };
124 path->addOval(r);
125 }
126private:
127 typedef PathBench INHERITED;
128};
129
130class SawToothPathBench : public PathBench {
131public:
132 SawToothPathBench(void* param, Flags flags) : INHERITED(param, flags) {}
133
134 virtual void appendName(SkString* name) {
135 name->append("sawtooth");
136 }
137 virtual void makePath(SkPath* path) {
138 SkScalar x = SkIntToScalar(20);
139 SkScalar y = SkIntToScalar(20);
140 const SkScalar x0 = x;
141 const SkScalar dx = SK_Scalar1 * 5;
142 const SkScalar dy = SK_Scalar1 * 10;
143
144 path->moveTo(x, y);
145 for (int i = 0; i < 32; i++) {
146 x += dx;
147 path->lineTo(x, y - dy);
148 x += dx;
149 path->lineTo(x, y + dy);
150 }
151 path->lineTo(x, y + 2 * dy);
152 path->lineTo(x0, y + 2 * dy);
153 path->close();
154 }
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000155 virtual int complexity() { return 1; }
reed@google.comd34658a2011-04-11 13:12:51 +0000156private:
157 typedef PathBench INHERITED;
158};
159
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000160class LongCurvedPathBench : public PathBench {
161public:
162 LongCurvedPathBench(void * param, Flags flags)
163 : INHERITED(param, flags) {
164 }
165
166 virtual void appendName(SkString* name) {
167 name->append("long_curved");
168 }
169 virtual void makePath(SkPath* path) {
170 SkRandom rand (12);
171 int i;
172 for (i = 0; i < 100; i++) {
173 path->quadTo(SkScalarMul(rand.nextUScalar1(), SkIntToScalar(640)),
174 SkScalarMul(rand.nextUScalar1(), SkIntToScalar(480)),
175 SkScalarMul(rand.nextUScalar1(), SkIntToScalar(640)),
176 SkScalarMul(rand.nextUScalar1(), SkIntToScalar(480)));
177 }
178 path->close();
179 }
180 virtual int complexity() { return 2; }
181private:
182 typedef PathBench INHERITED;
183};
184
senorblanco@chromium.orge50f7362012-01-12 19:10:35 +0000185class LongLinePathBench : public PathBench {
186public:
187 LongLinePathBench(void * param, Flags flags)
188 : INHERITED(param, flags) {
189 }
190
191 virtual void appendName(SkString* name) {
192 name->append("long_line");
193 }
194 virtual void makePath(SkPath* path) {
195 SkRandom rand;
196 path->moveTo(rand.nextUScalar1() * 640, rand.nextUScalar1() * 480);
197 for (size_t i = 1; i < 100; i++) {
198 path->lineTo(rand.nextUScalar1() * 640, rand.nextUScalar1() * 480);
199 }
200 }
201 virtual int complexity() { return 2; }
202private:
203 typedef PathBench INHERITED;
204};
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000205
206
reed@google.comd34658a2011-04-11 13:12:51 +0000207static SkBenchmark* FactT00(void* p) { return new TrianglePathBench(p, FLAGS00); }
208static SkBenchmark* FactT01(void* p) { return new TrianglePathBench(p, FLAGS01); }
209static SkBenchmark* FactT10(void* p) { return new TrianglePathBench(p, FLAGS10); }
210static SkBenchmark* FactT11(void* p) { return new TrianglePathBench(p, FLAGS11); }
211
212static SkBenchmark* FactR00(void* p) { return new RectPathBench(p, FLAGS00); }
213static SkBenchmark* FactR01(void* p) { return new RectPathBench(p, FLAGS01); }
214static SkBenchmark* FactR10(void* p) { return new RectPathBench(p, FLAGS10); }
215static SkBenchmark* FactR11(void* p) { return new RectPathBench(p, FLAGS11); }
216
217static SkBenchmark* FactO00(void* p) { return new OvalPathBench(p, FLAGS00); }
218static SkBenchmark* FactO01(void* p) { return new OvalPathBench(p, FLAGS01); }
219static SkBenchmark* FactO10(void* p) { return new OvalPathBench(p, FLAGS10); }
220static SkBenchmark* FactO11(void* p) { return new OvalPathBench(p, FLAGS11); }
221
222static SkBenchmark* FactS00(void* p) { return new SawToothPathBench(p, FLAGS00); }
223static SkBenchmark* FactS01(void* p) { return new SawToothPathBench(p, FLAGS01); }
224
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000225static SkBenchmark* FactLC00(void* p) {
226 return new LongCurvedPathBench(p, FLAGS00);
227}
228static SkBenchmark* FactLC01(void* p) {
229 return new LongCurvedPathBench(p, FLAGS01);
230}
231
senorblanco@chromium.orge50f7362012-01-12 19:10:35 +0000232static SkBenchmark* FactLL00(void* p) {
233 return new LongLinePathBench(p, FLAGS00);
234}
235
236static SkBenchmark* FactLL01(void* p) {
237 return new LongLinePathBench(p, FLAGS01);
238}
239
reed@google.comd34658a2011-04-11 13:12:51 +0000240static BenchRegistry gRegT00(FactT00);
241static BenchRegistry gRegT01(FactT01);
242static BenchRegistry gRegT10(FactT10);
243static BenchRegistry gRegT11(FactT11);
244
245static BenchRegistry gRegR00(FactR00);
246static BenchRegistry gRegR01(FactR01);
247static BenchRegistry gRegR10(FactR10);
248static BenchRegistry gRegR11(FactR11);
249
250static BenchRegistry gRegO00(FactO00);
251static BenchRegistry gRegO01(FactO01);
252static BenchRegistry gRegO10(FactO10);
253static BenchRegistry gRegO11(FactO11);
254
255static BenchRegistry gRegS00(FactS00);
256static BenchRegistry gRegS01(FactS01);
257
tomhudson@google.com6e8d3352011-06-22 17:16:35 +0000258static BenchRegistry gRegLC00(FactLC00);
259static BenchRegistry gRegLC01(FactLC01);
260
senorblanco@chromium.orge50f7362012-01-12 19:10:35 +0000261static BenchRegistry gRegLL00(FactLL00);
262static BenchRegistry gRegLL01(FactLL01);
263