blob: f52fc3b1ff42389f1c846c50b6e3a1c8e062b661 [file] [log] [blame]
egdaniel@google.coma3088832013-07-17 19:30:41 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
commit-bot@chromium.org78dd91d2014-01-27 19:52:51 +00007
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
egdaniel@google.coma3088832013-07-17 19:30:41 +00009#include "SkCanvas.h"
10#include "SkPaint.h"
11#include "SkRandom.h"
12#include "SkShader.h"
13#include "SkString.h"
14
15enum Flags {
16 kBig_Flag = 1 << 0,
17 kAA_Flag = 1 << 1
18};
19
20#define FLAGS00 Flags(0)
21#define FLAGS01 Flags(kBig_Flag)
22#define FLAGS10 Flags(kAA_Flag)
23#define FLAGS11 Flags(kBig_Flag | kAA_Flag)
24
25static const int points[] = {
26 10, 10, 15, 5, 20, 20,
27 30, 5, 25, 20, 15, 12,
28 21, 21, 30, 30, 12, 4,
29 32, 28, 20, 18, 12, 10
30};
31
32static const int kMaxPathSize = 10;
33
tfarinaf168b862014-06-19 12:32:29 -070034class HairlinePathBench : public Benchmark {
egdaniel@google.coma3088832013-07-17 19:30:41 +000035public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000036 HairlinePathBench(Flags flags) : fFlags(flags) {
egdaniel@google.coma3088832013-07-17 19:30:41 +000037 fPaint.setStyle(SkPaint::kStroke_Style);
38 fPaint.setStrokeWidth(SkIntToScalar(0));
39 }
40
41 virtual void appendName(SkString*) = 0;
42 virtual void makePath(SkPath*) = 0;
43
44protected:
mtklein72c9faa2015-01-09 10:06:39 -080045 const char* onGetName() SK_OVERRIDE {
egdaniel@google.coma3088832013-07-17 19:30:41 +000046 fName.printf("path_hairline_%s_%s_",
47 fFlags & kBig_Flag ? "big" : "small",
48 fFlags & kAA_Flag ? "AA" : "noAA");
49 this->appendName(&fName);
50 return fName.c_str();
51 }
52
mtklein72c9faa2015-01-09 10:06:39 -080053 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
egdaniel@google.coma3088832013-07-17 19:30:41 +000054 SkPaint paint(fPaint);
55 this->setupPaint(&paint);
56
57 paint.setAntiAlias(fFlags & kAA_Flag ? true : false);
skia.committer@gmail.comf7d01ce2013-07-18 07:00:56 +000058
egdaniel@google.coma3088832013-07-17 19:30:41 +000059 SkPath path;
60 this->makePath(&path);
61 if (fFlags & kBig_Flag) {
62 SkMatrix m;
63 m.setScale(SkIntToScalar(3), SkIntToScalar(3));
64 path.transform(m);
65 }
66
commit-bot@chromium.org33614712013-12-03 18:17:16 +000067 for (int i = 0; i < loops; i++) {
egdaniel@google.coma3088832013-07-17 19:30:41 +000068 canvas->drawPath(path, paint);
69 }
70 }
71
72private:
73 SkPaint fPaint;
74 SkString fName;
75 Flags fFlags;
tfarinaf168b862014-06-19 12:32:29 -070076 typedef Benchmark INHERITED;
egdaniel@google.coma3088832013-07-17 19:30:41 +000077};
78
79class LinePathBench : public HairlinePathBench {
80public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000081 LinePathBench(Flags flags) : INHERITED(flags) {}
egdaniel@google.coma3088832013-07-17 19:30:41 +000082
mtklein72c9faa2015-01-09 10:06:39 -080083 void appendName(SkString* name) SK_OVERRIDE {
egdaniel@google.coma3088832013-07-17 19:30:41 +000084 name->append("line");
85 }
mtklein72c9faa2015-01-09 10:06:39 -080086 void makePath(SkPath* path) SK_OVERRIDE {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000087 SkRandom rand;
egdaniel@google.coma3088832013-07-17 19:30:41 +000088 int size = SK_ARRAY_COUNT(points);
89 int hSize = size / 2;
90 for (int i = 0; i < kMaxPathSize; ++i) {
91 int xTrans = 10 + 40 * (i%(kMaxPathSize/2));
92 int yTrans = 0;
93 if (i > kMaxPathSize/2 - 1) {
94 yTrans = 40;
95 }
96 int base1 = 2 * rand.nextULessThan(hSize);
97 int base2 = 2 * rand.nextULessThan(hSize);
98 int base3 = 2 * rand.nextULessThan(hSize);
99 path->moveTo(SkIntToScalar(points[base1] + xTrans),
100 SkIntToScalar(points[base1+1] + yTrans));
101 path->lineTo(SkIntToScalar(points[base2] + xTrans),
102 SkIntToScalar(points[base2+1] + yTrans));
103 path->lineTo(SkIntToScalar(points[base3] + xTrans),
104 SkIntToScalar(points[base3+1] + yTrans));
105 }
106 }
107private:
108 typedef HairlinePathBench INHERITED;
109};
110
111class QuadPathBench : public HairlinePathBench {
112public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000113 QuadPathBench(Flags flags) : INHERITED(flags) {}
egdaniel@google.coma3088832013-07-17 19:30:41 +0000114
mtklein72c9faa2015-01-09 10:06:39 -0800115 void appendName(SkString* name) SK_OVERRIDE {
egdaniel@google.coma3088832013-07-17 19:30:41 +0000116 name->append("quad");
117 }
mtklein72c9faa2015-01-09 10:06:39 -0800118 void makePath(SkPath* path) SK_OVERRIDE {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000119 SkRandom rand;
egdaniel@google.coma3088832013-07-17 19:30:41 +0000120 int size = SK_ARRAY_COUNT(points);
121 int hSize = size / 2;
122 for (int i = 0; i < kMaxPathSize; ++i) {
123 int xTrans = 10 + 40 * (i%(kMaxPathSize/2));
124 int yTrans = 0;
125 if (i > kMaxPathSize/2 - 1) {
126 yTrans = 40;
127 }
128 int base1 = 2 * rand.nextULessThan(hSize);
129 int base2 = 2 * rand.nextULessThan(hSize);
130 int base3 = 2 * rand.nextULessThan(hSize);
131 path->moveTo(SkIntToScalar(points[base1] + xTrans),
132 SkIntToScalar(points[base1+1] + yTrans));
133 path->quadTo(SkIntToScalar(points[base2] + xTrans),
134 SkIntToScalar(points[base2+1] + yTrans),
135 SkIntToScalar(points[base3] + xTrans),
136 SkIntToScalar(points[base3+1] + yTrans));
137 }
138 }
139private:
140 typedef HairlinePathBench INHERITED;
141};
142
143class ConicPathBench : public HairlinePathBench {
144public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000145 ConicPathBench(Flags flags) : INHERITED(flags) {}
egdaniel@google.coma3088832013-07-17 19:30:41 +0000146
mtklein72c9faa2015-01-09 10:06:39 -0800147 void appendName(SkString* name) SK_OVERRIDE {
egdaniel@google.coma3088832013-07-17 19:30:41 +0000148 name->append("conic");
149 }
mtklein72c9faa2015-01-09 10:06:39 -0800150 void makePath(SkPath* path) SK_OVERRIDE {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000151 SkRandom rand;
152 SkRandom randWeight;
egdaniel@google.coma3088832013-07-17 19:30:41 +0000153 int size = SK_ARRAY_COUNT(points);
154 int hSize = size / 2;
155 for (int i = 0; i < kMaxPathSize; ++i) {
156 int xTrans = 10 + 40 * (i%(kMaxPathSize/2));
157 int yTrans = 0;
158 if (i > kMaxPathSize/2 - 1) {
159 yTrans = 40;
160 }
161 int base1 = 2 * rand.nextULessThan(hSize);
162 int base2 = 2 * rand.nextULessThan(hSize);
163 int base3 = 2 * rand.nextULessThan(hSize);
164 float weight = randWeight.nextRangeF(0.0f, 2.0f);
165 path->moveTo(SkIntToScalar(points[base1] + xTrans),
166 SkIntToScalar(points[base1+1] + yTrans));
167 path->conicTo(SkIntToScalar(points[base2] + xTrans),
168 SkIntToScalar(points[base2+1] + yTrans),
169 SkIntToScalar(points[base3] + xTrans),
170 SkIntToScalar(points[base3+1] + yTrans),
171 weight);
172 }
173 }
commit-bot@chromium.org78dd91d2014-01-27 19:52:51 +0000174
egdaniel@google.coma3088832013-07-17 19:30:41 +0000175private:
176 typedef HairlinePathBench INHERITED;
177};
178
179class CubicPathBench : public HairlinePathBench {
180public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000181 CubicPathBench(Flags flags) : INHERITED(flags) {}
egdaniel@google.coma3088832013-07-17 19:30:41 +0000182
mtklein72c9faa2015-01-09 10:06:39 -0800183 void appendName(SkString* name) SK_OVERRIDE {
egdaniel@google.coma3088832013-07-17 19:30:41 +0000184 name->append("cubic");
185 }
mtklein72c9faa2015-01-09 10:06:39 -0800186 void makePath(SkPath* path) SK_OVERRIDE {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000187 SkRandom rand;
egdaniel@google.coma3088832013-07-17 19:30:41 +0000188 int size = SK_ARRAY_COUNT(points);
189 int hSize = size / 2;
190 for (int i = 0; i < kMaxPathSize; ++i) {
191 int xTrans = 10 + 40 * (i%(kMaxPathSize/2));
192 int yTrans = 0;
193 if (i > kMaxPathSize/2 - 1) {
194 yTrans = 40;
195 }
196 int base1 = 2 * rand.nextULessThan(hSize);
197 int base2 = 2 * rand.nextULessThan(hSize);
198 int base3 = 2 * rand.nextULessThan(hSize);
199 int base4 = 2 * rand.nextULessThan(hSize);
200 path->moveTo(SkIntToScalar(points[base1] + xTrans),
201 SkIntToScalar(points[base1+1] + yTrans));
202 path->cubicTo(SkIntToScalar(points[base2] + xTrans),
203 SkIntToScalar(points[base2+1] + yTrans),
204 SkIntToScalar(points[base3] + xTrans),
205 SkIntToScalar(points[base3+1] + yTrans),
206 SkIntToScalar(points[base4] + xTrans),
207 SkIntToScalar(points[base4+1] + yTrans));
208 }
209 }
210private:
211 typedef HairlinePathBench INHERITED;
212};
213
214// FLAG00 - no AA, small
215// FLAG01 - no AA, small
216// FLAG10 - AA, big
217// FLAG11 - AA, big
218
mtklein@google.com410e6e82013-09-13 19:52:27 +0000219DEF_BENCH( return new LinePathBench(FLAGS00); )
220DEF_BENCH( return new LinePathBench(FLAGS01); )
221DEF_BENCH( return new LinePathBench(FLAGS10); )
222DEF_BENCH( return new LinePathBench(FLAGS11); )
egdaniel@google.coma3088832013-07-17 19:30:41 +0000223
mtklein@google.com410e6e82013-09-13 19:52:27 +0000224DEF_BENCH( return new QuadPathBench(FLAGS00); )
225DEF_BENCH( return new QuadPathBench(FLAGS01); )
226DEF_BENCH( return new QuadPathBench(FLAGS10); )
227DEF_BENCH( return new QuadPathBench(FLAGS11); )
egdaniel@google.coma3088832013-07-17 19:30:41 +0000228
229// Don't have default path renderer for conics yet on GPU, so must use AA
mtklein@google.com410e6e82013-09-13 19:52:27 +0000230// DEF_BENCH( return new ConicPathBench(FLAGS00); )
231// DEF_BENCH( return new ConicPathBench(FLAGS01); )
232DEF_BENCH( return new ConicPathBench(FLAGS10); )
233DEF_BENCH( return new ConicPathBench(FLAGS11); )
egdaniel@google.coma3088832013-07-17 19:30:41 +0000234
mtklein@google.com410e6e82013-09-13 19:52:27 +0000235DEF_BENCH( return new CubicPathBench(FLAGS00); )
236DEF_BENCH( return new CubicPathBench(FLAGS01); )
237DEF_BENCH( return new CubicPathBench(FLAGS10); )
238DEF_BENCH( return new CubicPathBench(FLAGS11); )