C++11 override should now be supported by all of {bots,Chrome,Android,Mozilla}
[platform/upstream/libSkiaSharp.git] / bench / HairlinePathBench.cpp
1 /*
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  */
7
8 #include "Benchmark.h"
9 #include "SkCanvas.h"
10 #include "SkPaint.h"
11 #include "SkRandom.h"
12 #include "SkShader.h"
13 #include "SkString.h"
14
15 enum 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
25 static 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
32 static const int kMaxPathSize = 10;
33
34 class HairlinePathBench : public Benchmark {
35 public:
36     HairlinePathBench(Flags flags) : fFlags(flags) {
37         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
44 protected:
45     const char* onGetName() override {
46         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
53     void onDraw(const int loops, SkCanvas* canvas) override {
54         SkPaint paint(fPaint);
55         this->setupPaint(&paint);
56
57         paint.setAntiAlias(fFlags & kAA_Flag ? true : false);
58
59         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
67         for (int i = 0; i < loops; i++) {
68             canvas->drawPath(path, paint);
69         }
70     }
71
72 private:
73     SkPaint     fPaint;
74     SkString    fName;
75     Flags       fFlags;
76     typedef Benchmark INHERITED;
77 };
78
79 class LinePathBench : public HairlinePathBench {
80 public:
81     LinePathBench(Flags flags) : INHERITED(flags) {}
82
83     void appendName(SkString* name) override {
84         name->append("line");
85     }
86     void makePath(SkPath* path) override {
87         SkRandom rand;
88         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     }
107 private:
108     typedef HairlinePathBench INHERITED;
109 };
110
111 class QuadPathBench : public HairlinePathBench {
112 public:
113     QuadPathBench(Flags flags) : INHERITED(flags) {}
114
115     void appendName(SkString* name) override {
116         name->append("quad");
117     }
118     void makePath(SkPath* path) override {
119         SkRandom rand;
120         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     }
139 private:
140     typedef HairlinePathBench INHERITED;
141 };
142
143 class ConicPathBench : public HairlinePathBench {
144 public:
145     ConicPathBench(Flags flags) : INHERITED(flags) {}
146
147     void appendName(SkString* name) override {
148         name->append("conic");
149     }
150     void makePath(SkPath* path) override {
151         SkRandom rand;
152         SkRandom randWeight;
153         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     }
174
175 private:
176     typedef HairlinePathBench INHERITED;
177 };
178
179 class CubicPathBench : public HairlinePathBench {
180 public:
181     CubicPathBench(Flags flags) : INHERITED(flags) {}
182
183     void appendName(SkString* name) override {
184         name->append("cubic");
185     }
186     void makePath(SkPath* path) override {
187         SkRandom rand;
188         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     }
210 private:
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
219 DEF_BENCH( return new LinePathBench(FLAGS00); )
220 DEF_BENCH( return new LinePathBench(FLAGS01); )
221 DEF_BENCH( return new LinePathBench(FLAGS10); )
222 DEF_BENCH( return new LinePathBench(FLAGS11); )
223
224 DEF_BENCH( return new QuadPathBench(FLAGS00); )
225 DEF_BENCH( return new QuadPathBench(FLAGS01); )
226 DEF_BENCH( return new QuadPathBench(FLAGS10); )
227 DEF_BENCH( return new QuadPathBench(FLAGS11); )
228
229 // Don't have default path renderer for conics yet on GPU, so must use AA
230 // DEF_BENCH( return new ConicPathBench(FLAGS00); )
231 // DEF_BENCH( return new ConicPathBench(FLAGS01); )
232 DEF_BENCH( return new ConicPathBench(FLAGS10); )
233 DEF_BENCH( return new ConicPathBench(FLAGS11); )
234
235 DEF_BENCH( return new CubicPathBench(FLAGS00); )
236 DEF_BENCH( return new CubicPathBench(FLAGS01); )
237 DEF_BENCH( return new CubicPathBench(FLAGS10); )
238 DEF_BENCH( return new CubicPathBench(FLAGS11); )