egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 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 | */ |
commit-bot@chromium.org | 78dd91d | 2014-01-27 19:52:51 +0000 | [diff] [blame] | 7 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 8 | #include "Benchmark.h" |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 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 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 34 | class HairlinePathBench : public Benchmark { |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 35 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 36 | HairlinePathBench(Flags flags) : fFlags(flags) { |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 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: |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 45 | const char* onGetName() SK_OVERRIDE { |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 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 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 53 | void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 54 | SkPaint paint(fPaint); |
| 55 | this->setupPaint(&paint); |
| 56 | |
| 57 | paint.setAntiAlias(fFlags & kAA_Flag ? true : false); |
skia.committer@gmail.com | f7d01ce | 2013-07-18 07:00:56 +0000 | [diff] [blame] | 58 | |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 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 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 67 | for (int i = 0; i < loops; i++) { |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 68 | canvas->drawPath(path, paint); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | private: |
| 73 | SkPaint fPaint; |
| 74 | SkString fName; |
| 75 | Flags fFlags; |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 76 | typedef Benchmark INHERITED; |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | class LinePathBench : public HairlinePathBench { |
| 80 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 81 | LinePathBench(Flags flags) : INHERITED(flags) {} |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 82 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 83 | void appendName(SkString* name) SK_OVERRIDE { |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 84 | name->append("line"); |
| 85 | } |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 86 | void makePath(SkPath* path) SK_OVERRIDE { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 87 | SkRandom rand; |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 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: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 113 | QuadPathBench(Flags flags) : INHERITED(flags) {} |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 114 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 115 | void appendName(SkString* name) SK_OVERRIDE { |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 116 | name->append("quad"); |
| 117 | } |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 118 | void makePath(SkPath* path) SK_OVERRIDE { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 119 | SkRandom rand; |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 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: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 145 | ConicPathBench(Flags flags) : INHERITED(flags) {} |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 146 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 147 | void appendName(SkString* name) SK_OVERRIDE { |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 148 | name->append("conic"); |
| 149 | } |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 150 | void makePath(SkPath* path) SK_OVERRIDE { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 151 | SkRandom rand; |
| 152 | SkRandom randWeight; |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 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 | } |
commit-bot@chromium.org | 78dd91d | 2014-01-27 19:52:51 +0000 | [diff] [blame] | 174 | |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 175 | private: |
| 176 | typedef HairlinePathBench INHERITED; |
| 177 | }; |
| 178 | |
| 179 | class CubicPathBench : public HairlinePathBench { |
| 180 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 181 | CubicPathBench(Flags flags) : INHERITED(flags) {} |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 182 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 183 | void appendName(SkString* name) SK_OVERRIDE { |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 184 | name->append("cubic"); |
| 185 | } |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 186 | void makePath(SkPath* path) SK_OVERRIDE { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 187 | SkRandom rand; |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 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 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 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); ) |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 223 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 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); ) |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 228 | |
| 229 | // Don't have default path renderer for conics yet on GPU, so must use AA |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 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); ) |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 234 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 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); ) |