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" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 11 | #include "SkPath.h" |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 12 | #include "SkRandom.h" |
| 13 | #include "SkShader.h" |
| 14 | #include "SkString.h" |
| 15 | |
| 16 | enum Flags { |
| 17 | kBig_Flag = 1 << 0, |
| 18 | kAA_Flag = 1 << 1 |
| 19 | }; |
| 20 | |
| 21 | #define FLAGS00 Flags(0) |
| 22 | #define FLAGS01 Flags(kBig_Flag) |
| 23 | #define FLAGS10 Flags(kAA_Flag) |
| 24 | #define FLAGS11 Flags(kBig_Flag | kAA_Flag) |
| 25 | |
| 26 | static const int points[] = { |
| 27 | 10, 10, 15, 5, 20, 20, |
| 28 | 30, 5, 25, 20, 15, 12, |
| 29 | 21, 21, 30, 30, 12, 4, |
| 30 | 32, 28, 20, 18, 12, 10 |
| 31 | }; |
| 32 | |
| 33 | static const int kMaxPathSize = 10; |
| 34 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 35 | class HairlinePathBench : public Benchmark { |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 36 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 37 | HairlinePathBench(Flags flags) : fFlags(flags) { |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 38 | fPaint.setStyle(SkPaint::kStroke_Style); |
| 39 | fPaint.setStrokeWidth(SkIntToScalar(0)); |
| 40 | } |
| 41 | |
| 42 | virtual void appendName(SkString*) = 0; |
| 43 | virtual void makePath(SkPath*) = 0; |
| 44 | |
| 45 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 46 | const char* onGetName() override { |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 47 | fName.printf("path_hairline_%s_%s_", |
| 48 | fFlags & kBig_Flag ? "big" : "small", |
| 49 | fFlags & kAA_Flag ? "AA" : "noAA"); |
| 50 | this->appendName(&fName); |
| 51 | return fName.c_str(); |
| 52 | } |
| 53 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 54 | void onDraw(int loops, SkCanvas* canvas) override { |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 55 | SkPaint paint(fPaint); |
| 56 | this->setupPaint(&paint); |
| 57 | |
| 58 | paint.setAntiAlias(fFlags & kAA_Flag ? true : false); |
skia.committer@gmail.com | f7d01ce | 2013-07-18 07:00:56 +0000 | [diff] [blame] | 59 | |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 60 | SkPath path; |
| 61 | this->makePath(&path); |
| 62 | if (fFlags & kBig_Flag) { |
robertphillips | 1d24b8d | 2015-03-26 19:57:08 -0700 | [diff] [blame] | 63 | const SkMatrix m = SkMatrix::MakeScale(SkIntToScalar(3), SkIntToScalar(3)); |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 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++) { |
reed | 9645ce1 | 2015-04-13 11:12:06 -0700 | [diff] [blame] | 68 | for (int j = 0; j < 100; ++j) { |
| 69 | canvas->drawPath(path, paint); |
| 70 | } |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
| 74 | private: |
| 75 | SkPaint fPaint; |
| 76 | SkString fName; |
| 77 | Flags fFlags; |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 78 | typedef Benchmark INHERITED; |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | class LinePathBench : public HairlinePathBench { |
| 82 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 83 | LinePathBench(Flags flags) : INHERITED(flags) {} |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 84 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 85 | void appendName(SkString* name) override { |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 86 | name->append("line"); |
| 87 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 88 | void makePath(SkPath* path) override { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 89 | SkRandom rand; |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 90 | int size = SK_ARRAY_COUNT(points); |
| 91 | int hSize = size / 2; |
| 92 | for (int i = 0; i < kMaxPathSize; ++i) { |
| 93 | int xTrans = 10 + 40 * (i%(kMaxPathSize/2)); |
| 94 | int yTrans = 0; |
| 95 | if (i > kMaxPathSize/2 - 1) { |
| 96 | yTrans = 40; |
| 97 | } |
| 98 | int base1 = 2 * rand.nextULessThan(hSize); |
| 99 | int base2 = 2 * rand.nextULessThan(hSize); |
| 100 | int base3 = 2 * rand.nextULessThan(hSize); |
| 101 | path->moveTo(SkIntToScalar(points[base1] + xTrans), |
| 102 | SkIntToScalar(points[base1+1] + yTrans)); |
| 103 | path->lineTo(SkIntToScalar(points[base2] + xTrans), |
| 104 | SkIntToScalar(points[base2+1] + yTrans)); |
| 105 | path->lineTo(SkIntToScalar(points[base3] + xTrans), |
| 106 | SkIntToScalar(points[base3+1] + yTrans)); |
| 107 | } |
| 108 | } |
| 109 | private: |
| 110 | typedef HairlinePathBench INHERITED; |
| 111 | }; |
| 112 | |
| 113 | class QuadPathBench : public HairlinePathBench { |
| 114 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 115 | QuadPathBench(Flags flags) : INHERITED(flags) {} |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 116 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 117 | void appendName(SkString* name) override { |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 118 | name->append("quad"); |
| 119 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 120 | void makePath(SkPath* path) override { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 121 | SkRandom rand; |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 122 | int size = SK_ARRAY_COUNT(points); |
| 123 | int hSize = size / 2; |
| 124 | for (int i = 0; i < kMaxPathSize; ++i) { |
| 125 | int xTrans = 10 + 40 * (i%(kMaxPathSize/2)); |
| 126 | int yTrans = 0; |
| 127 | if (i > kMaxPathSize/2 - 1) { |
| 128 | yTrans = 40; |
| 129 | } |
| 130 | int base1 = 2 * rand.nextULessThan(hSize); |
| 131 | int base2 = 2 * rand.nextULessThan(hSize); |
| 132 | int base3 = 2 * rand.nextULessThan(hSize); |
| 133 | path->moveTo(SkIntToScalar(points[base1] + xTrans), |
| 134 | SkIntToScalar(points[base1+1] + yTrans)); |
| 135 | path->quadTo(SkIntToScalar(points[base2] + xTrans), |
| 136 | SkIntToScalar(points[base2+1] + yTrans), |
| 137 | SkIntToScalar(points[base3] + xTrans), |
| 138 | SkIntToScalar(points[base3+1] + yTrans)); |
| 139 | } |
| 140 | } |
| 141 | private: |
| 142 | typedef HairlinePathBench INHERITED; |
| 143 | }; |
| 144 | |
| 145 | class ConicPathBench : public HairlinePathBench { |
| 146 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 147 | ConicPathBench(Flags flags) : INHERITED(flags) {} |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 148 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 149 | void appendName(SkString* name) override { |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 150 | name->append("conic"); |
| 151 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 152 | void makePath(SkPath* path) override { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 153 | SkRandom rand; |
| 154 | SkRandom randWeight; |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 155 | int size = SK_ARRAY_COUNT(points); |
| 156 | int hSize = size / 2; |
| 157 | for (int i = 0; i < kMaxPathSize; ++i) { |
| 158 | int xTrans = 10 + 40 * (i%(kMaxPathSize/2)); |
| 159 | int yTrans = 0; |
| 160 | if (i > kMaxPathSize/2 - 1) { |
| 161 | yTrans = 40; |
| 162 | } |
| 163 | int base1 = 2 * rand.nextULessThan(hSize); |
| 164 | int base2 = 2 * rand.nextULessThan(hSize); |
| 165 | int base3 = 2 * rand.nextULessThan(hSize); |
| 166 | float weight = randWeight.nextRangeF(0.0f, 2.0f); |
| 167 | path->moveTo(SkIntToScalar(points[base1] + xTrans), |
| 168 | SkIntToScalar(points[base1+1] + yTrans)); |
| 169 | path->conicTo(SkIntToScalar(points[base2] + xTrans), |
| 170 | SkIntToScalar(points[base2+1] + yTrans), |
| 171 | SkIntToScalar(points[base3] + xTrans), |
| 172 | SkIntToScalar(points[base3+1] + yTrans), |
| 173 | weight); |
| 174 | } |
| 175 | } |
commit-bot@chromium.org | 78dd91d | 2014-01-27 19:52:51 +0000 | [diff] [blame] | 176 | |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 177 | private: |
| 178 | typedef HairlinePathBench INHERITED; |
| 179 | }; |
| 180 | |
| 181 | class CubicPathBench : public HairlinePathBench { |
| 182 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 183 | CubicPathBench(Flags flags) : INHERITED(flags) {} |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 184 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 185 | void appendName(SkString* name) override { |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 186 | name->append("cubic"); |
| 187 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 188 | void makePath(SkPath* path) override { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 189 | SkRandom rand; |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 190 | int size = SK_ARRAY_COUNT(points); |
| 191 | int hSize = size / 2; |
| 192 | for (int i = 0; i < kMaxPathSize; ++i) { |
| 193 | int xTrans = 10 + 40 * (i%(kMaxPathSize/2)); |
| 194 | int yTrans = 0; |
| 195 | if (i > kMaxPathSize/2 - 1) { |
| 196 | yTrans = 40; |
| 197 | } |
| 198 | int base1 = 2 * rand.nextULessThan(hSize); |
| 199 | int base2 = 2 * rand.nextULessThan(hSize); |
| 200 | int base3 = 2 * rand.nextULessThan(hSize); |
| 201 | int base4 = 2 * rand.nextULessThan(hSize); |
| 202 | path->moveTo(SkIntToScalar(points[base1] + xTrans), |
| 203 | SkIntToScalar(points[base1+1] + yTrans)); |
| 204 | path->cubicTo(SkIntToScalar(points[base2] + xTrans), |
| 205 | SkIntToScalar(points[base2+1] + yTrans), |
| 206 | SkIntToScalar(points[base3] + xTrans), |
| 207 | SkIntToScalar(points[base3+1] + yTrans), |
| 208 | SkIntToScalar(points[base4] + xTrans), |
| 209 | SkIntToScalar(points[base4+1] + yTrans)); |
| 210 | } |
| 211 | } |
| 212 | private: |
| 213 | typedef HairlinePathBench INHERITED; |
| 214 | }; |
| 215 | |
| 216 | // FLAG00 - no AA, small |
| 217 | // FLAG01 - no AA, small |
| 218 | // FLAG10 - AA, big |
| 219 | // FLAG11 - AA, big |
| 220 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 221 | DEF_BENCH( return new LinePathBench(FLAGS00); ) |
| 222 | DEF_BENCH( return new LinePathBench(FLAGS01); ) |
| 223 | DEF_BENCH( return new LinePathBench(FLAGS10); ) |
| 224 | DEF_BENCH( return new LinePathBench(FLAGS11); ) |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 225 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 226 | DEF_BENCH( return new QuadPathBench(FLAGS00); ) |
| 227 | DEF_BENCH( return new QuadPathBench(FLAGS01); ) |
| 228 | DEF_BENCH( return new QuadPathBench(FLAGS10); ) |
| 229 | DEF_BENCH( return new QuadPathBench(FLAGS11); ) |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 230 | |
| 231 | // 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] | 232 | // DEF_BENCH( return new ConicPathBench(FLAGS00); ) |
| 233 | // DEF_BENCH( return new ConicPathBench(FLAGS01); ) |
| 234 | DEF_BENCH( return new ConicPathBench(FLAGS10); ) |
| 235 | DEF_BENCH( return new ConicPathBench(FLAGS11); ) |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 236 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 237 | DEF_BENCH( return new CubicPathBench(FLAGS00); ) |
| 238 | DEF_BENCH( return new CubicPathBench(FLAGS01); ) |
| 239 | DEF_BENCH( return new CubicPathBench(FLAGS10); ) |
| 240 | DEF_BENCH( return new CubicPathBench(FLAGS11); ) |