epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 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.com | d34658a | 2011-04-11 13:12:51 +0000 | [diff] [blame] | 8 | #include "SkBenchmark.h" |
| 9 | #include "SkBitmap.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkColorPriv.h" |
| 12 | #include "SkPaint.h" |
tomhudson@google.com | 6e8d335 | 2011-06-22 17:16:35 +0000 | [diff] [blame] | 13 | #include "SkRandom.h" |
reed@google.com | d34658a | 2011-04-11 13:12:51 +0000 | [diff] [blame] | 14 | #include "SkShader.h" |
| 15 | #include "SkString.h" |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 16 | #include "SkTArray.h" |
| 17 | |
reed@google.com | d34658a | 2011-04-11 13:12:51 +0000 | [diff] [blame] | 18 | |
| 19 | enum Flags { |
| 20 | kStroke_Flag = 1 << 0, |
| 21 | kBig_Flag = 1 << 1 |
| 22 | }; |
| 23 | |
| 24 | #define FLAGS00 Flags(0) |
| 25 | #define FLAGS01 Flags(kStroke_Flag) |
| 26 | #define FLAGS10 Flags(kBig_Flag) |
| 27 | #define FLAGS11 Flags(kStroke_Flag | kBig_Flag) |
| 28 | |
| 29 | class PathBench : public SkBenchmark { |
| 30 | SkPaint fPaint; |
| 31 | SkString fName; |
| 32 | Flags fFlags; |
tomhudson@google.com | ca529d3 | 2011-10-28 15:34:49 +0000 | [diff] [blame] | 33 | enum { N = SkBENCHLOOP(1000) }; |
reed@google.com | d34658a | 2011-04-11 13:12:51 +0000 | [diff] [blame] | 34 | public: |
| 35 | PathBench(void* param, Flags flags) : INHERITED(param), fFlags(flags) { |
| 36 | fPaint.setStyle(flags & kStroke_Flag ? SkPaint::kStroke_Style : |
| 37 | SkPaint::kFill_Style); |
| 38 | fPaint.setStrokeWidth(SkIntToScalar(5)); |
| 39 | fPaint.setStrokeJoin(SkPaint::kBevel_Join); |
| 40 | } |
| 41 | |
| 42 | virtual void appendName(SkString*) = 0; |
| 43 | virtual void makePath(SkPath*) = 0; |
tomhudson@google.com | 6e8d335 | 2011-06-22 17:16:35 +0000 | [diff] [blame] | 44 | virtual int complexity() { return 0; } |
reed@google.com | d34658a | 2011-04-11 13:12:51 +0000 | [diff] [blame] | 45 | |
| 46 | protected: |
bsalomon@google.com | 1647a19 | 2012-04-11 15:34:46 +0000 | [diff] [blame] | 47 | virtual const char* onGetName() SK_OVERRIDE { |
reed@google.com | d34658a | 2011-04-11 13:12:51 +0000 | [diff] [blame] | 48 | fName.printf("path_%s_%s_", |
| 49 | fFlags & kStroke_Flag ? "stroke" : "fill", |
| 50 | fFlags & kBig_Flag ? "big" : "small"); |
| 51 | this->appendName(&fName); |
| 52 | return fName.c_str(); |
| 53 | } |
| 54 | |
bsalomon@google.com | 1647a19 | 2012-04-11 15:34:46 +0000 | [diff] [blame] | 55 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
reed@google.com | d34658a | 2011-04-11 13:12:51 +0000 | [diff] [blame] | 56 | SkPaint paint(fPaint); |
| 57 | this->setupPaint(&paint); |
| 58 | |
| 59 | SkPath path; |
| 60 | this->makePath(&path); |
| 61 | if (fFlags & kBig_Flag) { |
| 62 | SkMatrix m; |
| 63 | m.setScale(SkIntToScalar(10), SkIntToScalar(10)); |
| 64 | path.transform(m); |
| 65 | } |
| 66 | |
| 67 | int count = N; |
| 68 | if (fFlags & kBig_Flag) { |
| 69 | count >>= 2; |
| 70 | } |
tomhudson@google.com | 6e8d335 | 2011-06-22 17:16:35 +0000 | [diff] [blame] | 71 | count >>= (3 * complexity()); |
reed@google.com | d34658a | 2011-04-11 13:12:51 +0000 | [diff] [blame] | 72 | |
| 73 | for (int i = 0; i < count; i++) { |
| 74 | canvas->drawPath(path, paint); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | private: |
| 79 | typedef SkBenchmark INHERITED; |
| 80 | }; |
| 81 | |
| 82 | class TrianglePathBench : public PathBench { |
| 83 | public: |
| 84 | TrianglePathBench(void* param, Flags flags) : INHERITED(param, flags) {} |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 85 | |
bsalomon@google.com | 1647a19 | 2012-04-11 15:34:46 +0000 | [diff] [blame] | 86 | virtual void appendName(SkString* name) SK_OVERRIDE { |
reed@google.com | d34658a | 2011-04-11 13:12:51 +0000 | [diff] [blame] | 87 | name->append("triangle"); |
| 88 | } |
bsalomon@google.com | 1647a19 | 2012-04-11 15:34:46 +0000 | [diff] [blame] | 89 | virtual void makePath(SkPath* path) SK_OVERRIDE { |
reed@google.com | d34658a | 2011-04-11 13:12:51 +0000 | [diff] [blame] | 90 | static const int gCoord[] = { |
| 91 | 10, 10, 15, 5, 20, 20 |
| 92 | }; |
| 93 | path->moveTo(SkIntToScalar(gCoord[0]), SkIntToScalar(gCoord[1])); |
| 94 | path->lineTo(SkIntToScalar(gCoord[2]), SkIntToScalar(gCoord[3])); |
| 95 | path->lineTo(SkIntToScalar(gCoord[4]), SkIntToScalar(gCoord[5])); |
| 96 | path->close(); |
| 97 | } |
| 98 | private: |
| 99 | typedef PathBench INHERITED; |
| 100 | }; |
| 101 | |
| 102 | class RectPathBench : public PathBench { |
| 103 | public: |
| 104 | RectPathBench(void* param, Flags flags) : INHERITED(param, flags) {} |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 105 | |
bsalomon@google.com | 1647a19 | 2012-04-11 15:34:46 +0000 | [diff] [blame] | 106 | virtual void appendName(SkString* name) SK_OVERRIDE { |
reed@google.com | d34658a | 2011-04-11 13:12:51 +0000 | [diff] [blame] | 107 | name->append("rect"); |
| 108 | } |
bsalomon@google.com | 1647a19 | 2012-04-11 15:34:46 +0000 | [diff] [blame] | 109 | virtual void makePath(SkPath* path) SK_OVERRIDE { |
reed@google.com | d34658a | 2011-04-11 13:12:51 +0000 | [diff] [blame] | 110 | SkRect r = { 10, 10, 20, 20 }; |
| 111 | path->addRect(r); |
| 112 | } |
| 113 | private: |
| 114 | typedef PathBench INHERITED; |
| 115 | }; |
| 116 | |
| 117 | class OvalPathBench : public PathBench { |
| 118 | public: |
| 119 | OvalPathBench(void* param, Flags flags) : INHERITED(param, flags) {} |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 120 | |
bsalomon@google.com | 1647a19 | 2012-04-11 15:34:46 +0000 | [diff] [blame] | 121 | virtual void appendName(SkString* name) SK_OVERRIDE { |
reed@google.com | d34658a | 2011-04-11 13:12:51 +0000 | [diff] [blame] | 122 | name->append("oval"); |
| 123 | } |
bsalomon@google.com | 1647a19 | 2012-04-11 15:34:46 +0000 | [diff] [blame] | 124 | virtual void makePath(SkPath* path) SK_OVERRIDE { |
jvanverth@google.com | e2bfd8b | 2013-01-24 15:45:35 +0000 | [diff] [blame] | 125 | SkRect r = { 10, 10, 23, 20 }; |
reed@google.com | d34658a | 2011-04-11 13:12:51 +0000 | [diff] [blame] | 126 | path->addOval(r); |
| 127 | } |
| 128 | private: |
| 129 | typedef PathBench INHERITED; |
| 130 | }; |
| 131 | |
bsalomon@google.com | 1647a19 | 2012-04-11 15:34:46 +0000 | [diff] [blame] | 132 | class CirclePathBench: public PathBench { |
| 133 | public: |
| 134 | CirclePathBench(void* param, Flags flags) : INHERITED(param, flags) {} |
| 135 | |
| 136 | virtual void appendName(SkString* name) SK_OVERRIDE { |
| 137 | name->append("circle"); |
| 138 | } |
| 139 | virtual void makePath(SkPath* path) SK_OVERRIDE { |
| 140 | path->addCircle(SkIntToScalar(20), SkIntToScalar(20), |
| 141 | SkIntToScalar(10)); |
| 142 | } |
| 143 | private: |
| 144 | typedef PathBench INHERITED; |
| 145 | }; |
| 146 | |
reed@google.com | d34658a | 2011-04-11 13:12:51 +0000 | [diff] [blame] | 147 | class SawToothPathBench : public PathBench { |
| 148 | public: |
| 149 | SawToothPathBench(void* param, Flags flags) : INHERITED(param, flags) {} |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 150 | |
bsalomon@google.com | 1647a19 | 2012-04-11 15:34:46 +0000 | [diff] [blame] | 151 | virtual void appendName(SkString* name) SK_OVERRIDE { |
reed@google.com | d34658a | 2011-04-11 13:12:51 +0000 | [diff] [blame] | 152 | name->append("sawtooth"); |
| 153 | } |
| 154 | virtual void makePath(SkPath* path) { |
| 155 | SkScalar x = SkIntToScalar(20); |
| 156 | SkScalar y = SkIntToScalar(20); |
| 157 | const SkScalar x0 = x; |
| 158 | const SkScalar dx = SK_Scalar1 * 5; |
| 159 | const SkScalar dy = SK_Scalar1 * 10; |
| 160 | |
| 161 | path->moveTo(x, y); |
| 162 | for (int i = 0; i < 32; i++) { |
| 163 | x += dx; |
| 164 | path->lineTo(x, y - dy); |
| 165 | x += dx; |
| 166 | path->lineTo(x, y + dy); |
| 167 | } |
| 168 | path->lineTo(x, y + 2 * dy); |
| 169 | path->lineTo(x0, y + 2 * dy); |
| 170 | path->close(); |
| 171 | } |
bsalomon@google.com | 1647a19 | 2012-04-11 15:34:46 +0000 | [diff] [blame] | 172 | virtual int complexity() SK_OVERRIDE { return 1; } |
reed@google.com | d34658a | 2011-04-11 13:12:51 +0000 | [diff] [blame] | 173 | private: |
| 174 | typedef PathBench INHERITED; |
| 175 | }; |
| 176 | |
tomhudson@google.com | 6e8d335 | 2011-06-22 17:16:35 +0000 | [diff] [blame] | 177 | class LongCurvedPathBench : public PathBench { |
| 178 | public: |
| 179 | LongCurvedPathBench(void * param, Flags flags) |
| 180 | : INHERITED(param, flags) { |
| 181 | } |
| 182 | |
bsalomon@google.com | 1647a19 | 2012-04-11 15:34:46 +0000 | [diff] [blame] | 183 | virtual void appendName(SkString* name) SK_OVERRIDE { |
tomhudson@google.com | 6e8d335 | 2011-06-22 17:16:35 +0000 | [diff] [blame] | 184 | name->append("long_curved"); |
| 185 | } |
bsalomon@google.com | 1647a19 | 2012-04-11 15:34:46 +0000 | [diff] [blame] | 186 | virtual void makePath(SkPath* path) SK_OVERRIDE { |
tomhudson@google.com | 6e8d335 | 2011-06-22 17:16:35 +0000 | [diff] [blame] | 187 | SkRandom rand (12); |
| 188 | int i; |
| 189 | for (i = 0; i < 100; i++) { |
| 190 | path->quadTo(SkScalarMul(rand.nextUScalar1(), SkIntToScalar(640)), |
| 191 | SkScalarMul(rand.nextUScalar1(), SkIntToScalar(480)), |
| 192 | SkScalarMul(rand.nextUScalar1(), SkIntToScalar(640)), |
| 193 | SkScalarMul(rand.nextUScalar1(), SkIntToScalar(480))); |
| 194 | } |
| 195 | path->close(); |
| 196 | } |
bsalomon@google.com | 1647a19 | 2012-04-11 15:34:46 +0000 | [diff] [blame] | 197 | virtual int complexity() SK_OVERRIDE { return 2; } |
tomhudson@google.com | 6e8d335 | 2011-06-22 17:16:35 +0000 | [diff] [blame] | 198 | private: |
| 199 | typedef PathBench INHERITED; |
| 200 | }; |
| 201 | |
senorblanco@chromium.org | e50f736 | 2012-01-12 19:10:35 +0000 | [diff] [blame] | 202 | class LongLinePathBench : public PathBench { |
| 203 | public: |
| 204 | LongLinePathBench(void * param, Flags flags) |
| 205 | : INHERITED(param, flags) { |
| 206 | } |
| 207 | |
bsalomon@google.com | 1647a19 | 2012-04-11 15:34:46 +0000 | [diff] [blame] | 208 | virtual void appendName(SkString* name) SK_OVERRIDE { |
senorblanco@chromium.org | e50f736 | 2012-01-12 19:10:35 +0000 | [diff] [blame] | 209 | name->append("long_line"); |
| 210 | } |
bsalomon@google.com | 1647a19 | 2012-04-11 15:34:46 +0000 | [diff] [blame] | 211 | virtual void makePath(SkPath* path) SK_OVERRIDE { |
senorblanco@chromium.org | e50f736 | 2012-01-12 19:10:35 +0000 | [diff] [blame] | 212 | SkRandom rand; |
| 213 | path->moveTo(rand.nextUScalar1() * 640, rand.nextUScalar1() * 480); |
| 214 | for (size_t i = 1; i < 100; i++) { |
| 215 | path->lineTo(rand.nextUScalar1() * 640, rand.nextUScalar1() * 480); |
| 216 | } |
| 217 | } |
bsalomon@google.com | 1647a19 | 2012-04-11 15:34:46 +0000 | [diff] [blame] | 218 | virtual int complexity() SK_OVERRIDE { return 2; } |
senorblanco@chromium.org | e50f736 | 2012-01-12 19:10:35 +0000 | [diff] [blame] | 219 | private: |
| 220 | typedef PathBench INHERITED; |
| 221 | }; |
tomhudson@google.com | 6e8d335 | 2011-06-22 17:16:35 +0000 | [diff] [blame] | 222 | |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 223 | class RandomPathBench : public SkBenchmark { |
| 224 | public: |
| 225 | RandomPathBench(void* param) : INHERITED(param) { |
robertphillips@google.com | 83187a2 | 2012-09-13 16:39:08 +0000 | [diff] [blame] | 226 | fIsRendering = false; |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | protected: |
| 230 | void createData(int minVerbs, |
| 231 | int maxVerbs, |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 232 | bool allowMoves = true, |
| 233 | SkRect* bounds = NULL) { |
| 234 | SkRect tempBounds; |
| 235 | if (NULL == bounds) { |
| 236 | tempBounds.setXYWH(0, 0, SK_Scalar1, SK_Scalar1); |
| 237 | bounds = &tempBounds; |
| 238 | } |
bsalomon@google.com | 6d552ee | 2012-08-14 15:10:09 +0000 | [diff] [blame] | 239 | fVerbCnts.reset(kNumVerbCnts); |
| 240 | for (int i = 0; i < kNumVerbCnts; ++i) { |
| 241 | fVerbCnts[i] = fRandom.nextRangeU(minVerbs, maxVerbs + 1); |
| 242 | } |
| 243 | fVerbs.reset(kNumVerbs); |
| 244 | for (int i = 0; i < kNumVerbs; ++i) { |
| 245 | do { |
| 246 | fVerbs[i] = static_cast<SkPath::Verb>(fRandom.nextULessThan(SkPath::kDone_Verb)); |
| 247 | } while (!allowMoves && SkPath::kMove_Verb == fVerbs[i]); |
| 248 | } |
| 249 | fPoints.reset(kNumPoints); |
| 250 | for (int i = 0; i < kNumPoints; ++i) { |
| 251 | fPoints[i].set(fRandom.nextRangeScalar(bounds->fLeft, bounds->fRight), |
| 252 | fRandom.nextRangeScalar(bounds->fTop, bounds->fBottom)); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 253 | } |
| 254 | this->restartMakingPaths(); |
| 255 | } |
| 256 | |
| 257 | void restartMakingPaths() { |
| 258 | fCurrPath = 0; |
| 259 | fCurrVerb = 0; |
| 260 | fCurrPoint = 0; |
| 261 | } |
| 262 | |
| 263 | void makePath(SkPath* path) { |
bsalomon@google.com | 6d552ee | 2012-08-14 15:10:09 +0000 | [diff] [blame] | 264 | int vCount = fVerbCnts[(fCurrPath++) & (kNumVerbCnts - 1)]; |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 265 | for (int v = 0; v < vCount; ++v) { |
bsalomon@google.com | 6d552ee | 2012-08-14 15:10:09 +0000 | [diff] [blame] | 266 | int verb = fVerbs[(fCurrVerb++) & (kNumVerbs - 1)]; |
| 267 | switch (verb) { |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 268 | case SkPath::kMove_Verb: |
bsalomon@google.com | 6d552ee | 2012-08-14 15:10:09 +0000 | [diff] [blame] | 269 | path->moveTo(fPoints[(fCurrPoint++) & (kNumPoints - 1)]); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 270 | break; |
| 271 | case SkPath::kLine_Verb: |
bsalomon@google.com | 6d552ee | 2012-08-14 15:10:09 +0000 | [diff] [blame] | 272 | path->lineTo(fPoints[(fCurrPoint++) & (kNumPoints - 1)]); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 273 | break; |
| 274 | case SkPath::kQuad_Verb: |
bsalomon@google.com | 373ebc6 | 2012-09-26 13:08:56 +0000 | [diff] [blame] | 275 | path->quadTo(fPoints[(fCurrPoint + 0) & (kNumPoints - 1)], |
| 276 | fPoints[(fCurrPoint + 1) & (kNumPoints - 1)]); |
| 277 | fCurrPoint += 2; |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 278 | break; |
| 279 | case SkPath::kCubic_Verb: |
bsalomon@google.com | 373ebc6 | 2012-09-26 13:08:56 +0000 | [diff] [blame] | 280 | path->cubicTo(fPoints[(fCurrPoint + 0) & (kNumPoints - 1)], |
| 281 | fPoints[(fCurrPoint + 1) & (kNumPoints - 1)], |
| 282 | fPoints[(fCurrPoint + 2) & (kNumPoints - 1)]); |
| 283 | fCurrPoint += 3; |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 284 | break; |
| 285 | case SkPath::kClose_Verb: |
| 286 | path->close(); |
| 287 | break; |
| 288 | default: |
| 289 | SkDEBUGFAIL("Unexpected path verb"); |
| 290 | break; |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | void finishedMakingPaths() { |
bsalomon@google.com | 6d552ee | 2012-08-14 15:10:09 +0000 | [diff] [blame] | 296 | fVerbCnts.reset(0); |
| 297 | fVerbs.reset(0); |
| 298 | fPoints.reset(0); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | private: |
bsalomon@google.com | 6d552ee | 2012-08-14 15:10:09 +0000 | [diff] [blame] | 302 | enum { |
| 303 | // these should all be pow 2 |
bsalomon@google.com | b5e4703 | 2012-08-14 17:49:46 +0000 | [diff] [blame] | 304 | kNumVerbCnts = 1 << 5, |
| 305 | kNumVerbs = 1 << 5, |
| 306 | kNumPoints = 1 << 5, |
bsalomon@google.com | 6d552ee | 2012-08-14 15:10:09 +0000 | [diff] [blame] | 307 | }; |
| 308 | SkAutoTArray<int> fVerbCnts; |
| 309 | SkAutoTArray<SkPath::Verb> fVerbs; |
| 310 | SkAutoTArray<SkPoint> fPoints; |
| 311 | int fCurrPath; |
| 312 | int fCurrVerb; |
| 313 | int fCurrPoint; |
| 314 | SkRandom fRandom; |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 315 | typedef SkBenchmark INHERITED; |
| 316 | }; |
| 317 | |
| 318 | class PathCreateBench : public RandomPathBench { |
| 319 | public: |
| 320 | PathCreateBench(void* param) : INHERITED(param) { |
| 321 | } |
| 322 | |
| 323 | protected: |
| 324 | enum { N = SkBENCHLOOP(5000) }; |
| 325 | |
| 326 | virtual const char* onGetName() SK_OVERRIDE { |
| 327 | return "path_create"; |
| 328 | } |
| 329 | |
| 330 | virtual void onPreDraw() SK_OVERRIDE { |
bsalomon@google.com | 6d552ee | 2012-08-14 15:10:09 +0000 | [diff] [blame] | 331 | this->createData(10, 100); |
bsalomon@google.com | b5e4703 | 2012-08-14 17:49:46 +0000 | [diff] [blame] | 332 | fPaths.reset(kPathCnt); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | virtual void onDraw(SkCanvas*) SK_OVERRIDE { |
| 336 | for (int i = 0; i < N; ++i) { |
bsalomon@google.com | b5e4703 | 2012-08-14 17:49:46 +0000 | [diff] [blame] | 337 | this->makePath(&fPaths[i & (kPathCnt - 1)]); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 338 | } |
| 339 | this->restartMakingPaths(); |
| 340 | } |
| 341 | |
| 342 | virtual void onPostDraw() SK_OVERRIDE { |
| 343 | this->finishedMakingPaths(); |
bsalomon@google.com | b5e4703 | 2012-08-14 17:49:46 +0000 | [diff] [blame] | 344 | fPaths.reset(0); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | private: |
bsalomon@google.com | b5e4703 | 2012-08-14 17:49:46 +0000 | [diff] [blame] | 348 | enum { |
| 349 | // must be a pow 2 |
| 350 | kPathCnt = 1 << 5, |
| 351 | }; |
| 352 | SkAutoTArray<SkPath> fPaths; |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 353 | |
| 354 | typedef RandomPathBench INHERITED; |
| 355 | }; |
| 356 | |
| 357 | class PathCopyBench : public RandomPathBench { |
| 358 | public: |
| 359 | PathCopyBench(void* param) : INHERITED(param) { |
| 360 | } |
| 361 | |
| 362 | protected: |
bsalomon@google.com | 62e4190 | 2012-08-13 16:59:21 +0000 | [diff] [blame] | 363 | enum { N = SkBENCHLOOP(30000) }; |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 364 | |
| 365 | virtual const char* onGetName() SK_OVERRIDE { |
| 366 | return "path_copy"; |
| 367 | } |
| 368 | virtual void onPreDraw() SK_OVERRIDE { |
bsalomon@google.com | 6d552ee | 2012-08-14 15:10:09 +0000 | [diff] [blame] | 369 | this->createData(10, 100); |
bsalomon@google.com | b5e4703 | 2012-08-14 17:49:46 +0000 | [diff] [blame] | 370 | fPaths.reset(kPathCnt); |
| 371 | fCopies.reset(kPathCnt); |
| 372 | for (int i = 0; i < kPathCnt; ++i) { |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 373 | this->makePath(&fPaths[i]); |
| 374 | } |
| 375 | this->finishedMakingPaths(); |
| 376 | } |
| 377 | virtual void onDraw(SkCanvas*) SK_OVERRIDE { |
| 378 | for (int i = 0; i < N; ++i) { |
bsalomon@google.com | b5e4703 | 2012-08-14 17:49:46 +0000 | [diff] [blame] | 379 | int idx = i & (kPathCnt - 1); |
| 380 | fCopies[idx] = fPaths[idx]; |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | virtual void onPostDraw() SK_OVERRIDE { |
bsalomon@google.com | b5e4703 | 2012-08-14 17:49:46 +0000 | [diff] [blame] | 384 | fPaths.reset(0); |
| 385 | fCopies.reset(0); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | private: |
bsalomon@google.com | b5e4703 | 2012-08-14 17:49:46 +0000 | [diff] [blame] | 389 | enum { |
| 390 | // must be a pow 2 |
| 391 | kPathCnt = 1 << 5, |
| 392 | }; |
| 393 | SkAutoTArray<SkPath> fPaths; |
| 394 | SkAutoTArray<SkPath> fCopies; |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 395 | |
| 396 | typedef RandomPathBench INHERITED; |
| 397 | }; |
| 398 | |
| 399 | class PathTransformBench : public RandomPathBench { |
| 400 | public: |
| 401 | PathTransformBench(bool inPlace, void* param) |
| 402 | : INHERITED(param) |
| 403 | , fInPlace(inPlace) { |
| 404 | } |
| 405 | |
| 406 | protected: |
| 407 | enum { N = SkBENCHLOOP(30000) }; |
| 408 | |
| 409 | virtual const char* onGetName() SK_OVERRIDE { |
| 410 | return fInPlace ? "path_transform_in_place" : "path_transform_copy"; |
| 411 | } |
| 412 | |
| 413 | virtual void onPreDraw() SK_OVERRIDE { |
| 414 | fMatrix.setScale(5 * SK_Scalar1, 6 * SK_Scalar1); |
bsalomon@google.com | 6d552ee | 2012-08-14 15:10:09 +0000 | [diff] [blame] | 415 | this->createData(10, 100); |
bsalomon@google.com | b5e4703 | 2012-08-14 17:49:46 +0000 | [diff] [blame] | 416 | fPaths.reset(kPathCnt); |
| 417 | for (int i = 0; i < kPathCnt; ++i) { |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 418 | this->makePath(&fPaths[i]); |
| 419 | } |
bsalomon@google.com | b5e4703 | 2012-08-14 17:49:46 +0000 | [diff] [blame] | 420 | this->finishedMakingPaths(); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 421 | if (!fInPlace) { |
bsalomon@google.com | b5e4703 | 2012-08-14 17:49:46 +0000 | [diff] [blame] | 422 | fTransformed.reset(kPathCnt); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
| 426 | virtual void onDraw(SkCanvas*) SK_OVERRIDE { |
| 427 | if (fInPlace) { |
| 428 | for (int i = 0; i < N; ++i) { |
bsalomon@google.com | b5e4703 | 2012-08-14 17:49:46 +0000 | [diff] [blame] | 429 | fPaths[i & (kPathCnt - 1)].transform(fMatrix); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 430 | } |
| 431 | } else { |
| 432 | for (int i = 0; i < N; ++i) { |
bsalomon@google.com | b5e4703 | 2012-08-14 17:49:46 +0000 | [diff] [blame] | 433 | int idx = i & (kPathCnt - 1); |
| 434 | fPaths[idx].transform(fMatrix, &fTransformed[idx]); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | virtual void onPostDraw() SK_OVERRIDE { |
bsalomon@google.com | b5e4703 | 2012-08-14 17:49:46 +0000 | [diff] [blame] | 440 | fPaths.reset(0); |
| 441 | fTransformed.reset(0); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | private: |
bsalomon@google.com | b5e4703 | 2012-08-14 17:49:46 +0000 | [diff] [blame] | 445 | enum { |
| 446 | // must be a pow 2 |
| 447 | kPathCnt = 1 << 5, |
| 448 | }; |
| 449 | SkAutoTArray<SkPath> fPaths; |
| 450 | SkAutoTArray<SkPath> fTransformed; |
| 451 | |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 452 | SkMatrix fMatrix; |
| 453 | bool fInPlace; |
| 454 | typedef RandomPathBench INHERITED; |
| 455 | }; |
| 456 | |
| 457 | class PathEqualityBench : public RandomPathBench { |
| 458 | public: |
| 459 | PathEqualityBench(void* param) |
| 460 | : INHERITED(param) { |
| 461 | } |
| 462 | |
| 463 | protected: |
| 464 | enum { N = SkBENCHLOOP(40000) }; |
| 465 | |
| 466 | virtual const char* onGetName() SK_OVERRIDE { |
| 467 | return "path_equality_50%"; |
| 468 | } |
| 469 | |
| 470 | virtual void onPreDraw() SK_OVERRIDE { |
| 471 | fParity = 0; |
bsalomon@google.com | 6d552ee | 2012-08-14 15:10:09 +0000 | [diff] [blame] | 472 | this->createData(10, 100); |
bsalomon@google.com | b5e4703 | 2012-08-14 17:49:46 +0000 | [diff] [blame] | 473 | fPaths.reset(kPathCnt); |
| 474 | fCopies.reset(kPathCnt); |
| 475 | for (int i = 0; i < kPathCnt; ++i) { |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 476 | this->makePath(&fPaths[i]); |
| 477 | fCopies[i] = fPaths[i]; |
| 478 | } |
| 479 | this->finishedMakingPaths(); |
| 480 | } |
| 481 | |
| 482 | virtual void onDraw(SkCanvas*) SK_OVERRIDE { |
| 483 | for (int i = 0; i < N; ++i) { |
bsalomon@google.com | b5e4703 | 2012-08-14 17:49:46 +0000 | [diff] [blame] | 484 | int idx = i & (kPathCnt - 1); |
| 485 | fParity ^= (fPaths[idx] == fCopies[idx & ~0x1]); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | |
| 489 | virtual void onPostDraw() SK_OVERRIDE { |
bsalomon@google.com | b5e4703 | 2012-08-14 17:49:46 +0000 | [diff] [blame] | 490 | fPaths.reset(0); |
| 491 | fCopies.reset(0); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | private: |
| 495 | bool fParity; // attempt to keep compiler from optimizing out the == |
bsalomon@google.com | b5e4703 | 2012-08-14 17:49:46 +0000 | [diff] [blame] | 496 | enum { |
| 497 | // must be a pow 2 |
| 498 | kPathCnt = 1 << 5, |
| 499 | }; |
| 500 | SkAutoTArray<SkPath> fPaths; |
| 501 | SkAutoTArray<SkPath> fCopies; |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 502 | typedef RandomPathBench INHERITED; |
| 503 | }; |
| 504 | |
| 505 | class SkBench_AddPathTest : public RandomPathBench { |
| 506 | public: |
| 507 | enum AddType { |
| 508 | kAdd_AddType, |
| 509 | kAddTrans_AddType, |
| 510 | kAddMatrix_AddType, |
| 511 | kPathTo_AddType, |
| 512 | kReverseAdd_AddType, |
| 513 | kReversePathTo_AddType, |
| 514 | }; |
| 515 | |
| 516 | SkBench_AddPathTest(AddType type, void* param) |
| 517 | : INHERITED(param) |
| 518 | , fType(type) { |
| 519 | fMatrix.setRotate(60 * SK_Scalar1); |
| 520 | } |
| 521 | |
| 522 | protected: |
| 523 | enum { N = SkBENCHLOOP(15000) }; |
| 524 | |
| 525 | virtual const char* onGetName() SK_OVERRIDE { |
| 526 | switch (fType) { |
| 527 | case kAdd_AddType: |
| 528 | return "path_add_path"; |
| 529 | case kAddTrans_AddType: |
| 530 | return "path_add_path_trans"; |
| 531 | case kAddMatrix_AddType: |
| 532 | return "path_add_path_matrix"; |
| 533 | case kPathTo_AddType: |
| 534 | return "path_path_to"; |
| 535 | case kReverseAdd_AddType: |
| 536 | return "path_reverse_add_path"; |
| 537 | case kReversePathTo_AddType: |
| 538 | return "path_reverse_path_to"; |
| 539 | default: |
| 540 | SkDEBUGFAIL("Bad add type"); |
| 541 | return ""; |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | virtual void onPreDraw() SK_OVERRIDE { |
| 546 | // pathTo and reversePathTo assume a single contour path. |
| 547 | bool allowMoves = kPathTo_AddType != fType && |
| 548 | kReversePathTo_AddType != fType; |
bsalomon@google.com | 6d552ee | 2012-08-14 15:10:09 +0000 | [diff] [blame] | 549 | this->createData(10, 100, allowMoves); |
bsalomon@google.com | b5e4703 | 2012-08-14 17:49:46 +0000 | [diff] [blame] | 550 | fPaths0.reset(kPathCnt); |
| 551 | fPaths1.reset(kPathCnt); |
| 552 | for (int i = 0; i < kPathCnt; ++i) { |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 553 | this->makePath(&fPaths0[i]); |
| 554 | this->makePath(&fPaths1[i]); |
| 555 | } |
| 556 | this->finishedMakingPaths(); |
| 557 | } |
| 558 | |
| 559 | virtual void onDraw(SkCanvas*) SK_OVERRIDE { |
| 560 | switch (fType) { |
| 561 | case kAdd_AddType: |
| 562 | for (int i = 0; i < N; ++i) { |
bsalomon@google.com | b5e4703 | 2012-08-14 17:49:46 +0000 | [diff] [blame] | 563 | int idx = i & (kPathCnt - 1); |
| 564 | SkPath result = fPaths0[idx]; |
| 565 | result.addPath(fPaths1[idx]); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 566 | } |
| 567 | break; |
| 568 | case kAddTrans_AddType: |
| 569 | for (int i = 0; i < N; ++i) { |
bsalomon@google.com | b5e4703 | 2012-08-14 17:49:46 +0000 | [diff] [blame] | 570 | int idx = i & (kPathCnt - 1); |
| 571 | SkPath result = fPaths0[idx]; |
| 572 | result.addPath(fPaths1[idx], 2 * SK_Scalar1, 5 * SK_Scalar1); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 573 | } |
| 574 | break; |
| 575 | case kAddMatrix_AddType: |
| 576 | for (int i = 0; i < N; ++i) { |
bsalomon@google.com | b5e4703 | 2012-08-14 17:49:46 +0000 | [diff] [blame] | 577 | int idx = i & (kPathCnt - 1); |
| 578 | SkPath result = fPaths0[idx]; |
| 579 | result.addPath(fPaths1[idx], fMatrix); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 580 | } |
| 581 | break; |
| 582 | case kPathTo_AddType: |
| 583 | for (int i = 0; i < N; ++i) { |
bsalomon@google.com | b5e4703 | 2012-08-14 17:49:46 +0000 | [diff] [blame] | 584 | int idx = i & (kPathCnt - 1); |
| 585 | SkPath result = fPaths0[idx]; |
| 586 | result.pathTo(fPaths1[idx]); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 587 | } |
| 588 | break; |
| 589 | case kReverseAdd_AddType: |
| 590 | for (int i = 0; i < N; ++i) { |
bsalomon@google.com | b5e4703 | 2012-08-14 17:49:46 +0000 | [diff] [blame] | 591 | int idx = i & (kPathCnt - 1); |
| 592 | SkPath result = fPaths0[idx]; |
| 593 | result.reverseAddPath(fPaths1[idx]); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 594 | } |
| 595 | break; |
| 596 | case kReversePathTo_AddType: |
| 597 | for (int i = 0; i < N; ++i) { |
bsalomon@google.com | b5e4703 | 2012-08-14 17:49:46 +0000 | [diff] [blame] | 598 | int idx = i & (kPathCnt - 1); |
| 599 | SkPath result = fPaths0[idx]; |
| 600 | result.reversePathTo(fPaths1[idx]); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 601 | } |
| 602 | break; |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | virtual void onPostDraw() SK_OVERRIDE { |
bsalomon@google.com | b5e4703 | 2012-08-14 17:49:46 +0000 | [diff] [blame] | 607 | fPaths0.reset(0); |
| 608 | fPaths1.reset(0); |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | private: |
| 612 | AddType fType; // or reverseAddPath |
bsalomon@google.com | b5e4703 | 2012-08-14 17:49:46 +0000 | [diff] [blame] | 613 | enum { |
| 614 | // must be a pow 2 |
| 615 | kPathCnt = 1 << 5, |
| 616 | }; |
| 617 | SkAutoTArray<SkPath> fPaths0; |
| 618 | SkAutoTArray<SkPath> fPaths1; |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 619 | SkMatrix fMatrix; |
| 620 | typedef RandomPathBench INHERITED; |
| 621 | }; |
tomhudson@google.com | 6e8d335 | 2011-06-22 17:16:35 +0000 | [diff] [blame] | 622 | |
robertphillips@google.com | 17bb458 | 2012-08-20 17:24:16 +0000 | [diff] [blame] | 623 | |
| 624 | class CirclesBench : public SkBenchmark { |
| 625 | protected: |
| 626 | SkString fName; |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 627 | Flags fFlags; |
robertphillips@google.com | 17bb458 | 2012-08-20 17:24:16 +0000 | [diff] [blame] | 628 | |
| 629 | enum { |
| 630 | N = SkBENCHLOOP(100) |
| 631 | }; |
| 632 | public: |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 633 | CirclesBench(void* param, Flags flags) : INHERITED(param), fFlags(flags) { |
| 634 | fName.printf("circles_%s", fFlags & kStroke_Flag ? "stroke" : "fill"); |
robertphillips@google.com | 17bb458 | 2012-08-20 17:24:16 +0000 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | protected: |
| 638 | virtual const char* onGetName() SK_OVERRIDE { |
| 639 | return fName.c_str(); |
| 640 | } |
| 641 | |
| 642 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 643 | SkPaint paint; |
| 644 | |
| 645 | paint.setColor(SK_ColorBLACK); |
| 646 | paint.setAntiAlias(true); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 647 | if (fFlags & kStroke_Flag) { |
| 648 | paint.setStyle(SkPaint::kStroke_Style); |
| 649 | } |
robertphillips@google.com | 17bb458 | 2012-08-20 17:24:16 +0000 | [diff] [blame] | 650 | |
| 651 | SkRandom rand; |
| 652 | |
| 653 | SkRect r; |
| 654 | |
| 655 | for (int i = 0; i < 5000; ++i) { |
| 656 | SkScalar radius = rand.nextUScalar1() * 3; |
| 657 | r.fLeft = rand.nextUScalar1() * 300; |
| 658 | r.fTop = rand.nextUScalar1() * 300; |
| 659 | r.fRight = r.fLeft + 2 * radius; |
| 660 | r.fBottom = r.fTop + 2 * radius; |
| 661 | |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 662 | if (fFlags & kStroke_Flag) { |
| 663 | paint.setStrokeWidth(rand.nextUScalar1() * 5.0f); |
| 664 | } |
| 665 | |
robertphillips@google.com | 17bb458 | 2012-08-20 17:24:16 +0000 | [diff] [blame] | 666 | SkPath temp; |
| 667 | |
| 668 | // mimic how Chrome does circles |
| 669 | temp.arcTo(r, 0, 0, false); |
| 670 | temp.addOval(r, SkPath::kCCW_Direction); |
| 671 | temp.arcTo(r, 360, 0, true); |
| 672 | temp.close(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 673 | |
robertphillips@google.com | 17bb458 | 2012-08-20 17:24:16 +0000 | [diff] [blame] | 674 | canvas->drawPath(temp, paint); |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | private: |
| 679 | typedef SkBenchmark INHERITED; |
| 680 | }; |
| 681 | |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 682 | |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 683 | // Chrome creates its own round rects with each corner possibly being different. |
| 684 | // In its "zero radius" incarnation it creates degenerate round rects. |
skia.committer@gmail.com | 1e34ff7 | 2012-10-24 02:01:24 +0000 | [diff] [blame] | 685 | // Note: PathTest::test_arb_round_rect_is_convex and |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 686 | // test_arb_zero_rad_round_rect_is_rect perform almost exactly |
robertphillips@google.com | b95eaa8 | 2012-10-18 15:26:12 +0000 | [diff] [blame] | 687 | // the same test (but with no drawing) |
robertphillips@google.com | f6fc3fc | 2012-10-17 15:23:21 +0000 | [diff] [blame] | 688 | class ArbRoundRectBench : public SkBenchmark { |
| 689 | protected: |
| 690 | SkString fName; |
| 691 | |
| 692 | enum { |
| 693 | N = SkBENCHLOOP(100) |
| 694 | }; |
| 695 | public: |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 696 | ArbRoundRectBench(void* param, bool zeroRad) : INHERITED(param), fZeroRad(zeroRad) { |
| 697 | if (zeroRad) { |
| 698 | fName.printf("zeroradroundrect"); |
| 699 | } else { |
| 700 | fName.printf("arbroundrect"); |
| 701 | } |
robertphillips@google.com | f6fc3fc | 2012-10-17 15:23:21 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | protected: |
| 705 | virtual const char* onGetName() SK_OVERRIDE { |
| 706 | return fName.c_str(); |
| 707 | } |
| 708 | |
skia.committer@gmail.com | 989a95e | 2012-10-18 02:01:23 +0000 | [diff] [blame] | 709 | static void add_corner_arc(SkPath* path, const SkRect& rect, |
| 710 | SkScalar xIn, SkScalar yIn, |
robertphillips@google.com | f6fc3fc | 2012-10-17 15:23:21 +0000 | [diff] [blame] | 711 | int startAngle) |
| 712 | { |
| 713 | |
| 714 | SkScalar rx = SkMinScalar(rect.width(), xIn); |
| 715 | SkScalar ry = SkMinScalar(rect.height(), yIn); |
| 716 | |
| 717 | SkRect arcRect; |
| 718 | arcRect.set(-rx, -ry, rx, ry); |
| 719 | switch (startAngle) { |
| 720 | case 0: |
| 721 | arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom); |
| 722 | break; |
| 723 | case 90: |
| 724 | arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom); |
| 725 | break; |
| 726 | case 180: |
| 727 | arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop); |
| 728 | break; |
| 729 | case 270: |
| 730 | arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop); |
| 731 | break; |
| 732 | default: |
| 733 | break; |
| 734 | } |
| 735 | |
| 736 | path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false); |
| 737 | } |
| 738 | |
skia.committer@gmail.com | 989a95e | 2012-10-18 02:01:23 +0000 | [diff] [blame] | 739 | static void make_arb_round_rect(SkPath* path, const SkRect& r, |
robertphillips@google.com | f6fc3fc | 2012-10-17 15:23:21 +0000 | [diff] [blame] | 740 | SkScalar xCorner, SkScalar yCorner) { |
| 741 | // we are lazy here and use the same x & y for each corner |
| 742 | add_corner_arc(path, r, xCorner, yCorner, 270); |
| 743 | add_corner_arc(path, r, xCorner, yCorner, 0); |
| 744 | add_corner_arc(path, r, xCorner, yCorner, 90); |
| 745 | add_corner_arc(path, r, xCorner, yCorner, 180); |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 746 | path->close(); |
robertphillips@google.com | f6fc3fc | 2012-10-17 15:23:21 +0000 | [diff] [blame] | 747 | |
robertphillips@google.com | b95eaa8 | 2012-10-18 15:26:12 +0000 | [diff] [blame] | 748 | SkASSERT(path->isConvex()); |
robertphillips@google.com | f6fc3fc | 2012-10-17 15:23:21 +0000 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 752 | SkRandom rand; |
| 753 | SkRect r; |
| 754 | |
| 755 | for (int i = 0; i < 5000; ++i) { |
| 756 | SkPaint paint; |
| 757 | paint.setColor(0xff000000 | rand.nextU()); |
| 758 | paint.setAntiAlias(true); |
| 759 | |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 760 | SkScalar size = rand.nextUScalar1() * 30; |
| 761 | if (size < SK_Scalar1) { |
robertphillips@google.com | b95eaa8 | 2012-10-18 15:26:12 +0000 | [diff] [blame] | 762 | continue; |
| 763 | } |
robertphillips@google.com | f6fc3fc | 2012-10-17 15:23:21 +0000 | [diff] [blame] | 764 | r.fLeft = rand.nextUScalar1() * 300; |
| 765 | r.fTop = rand.nextUScalar1() * 300; |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 766 | r.fRight = r.fLeft + 2 * size; |
| 767 | r.fBottom = r.fTop + 2 * size; |
robertphillips@google.com | f6fc3fc | 2012-10-17 15:23:21 +0000 | [diff] [blame] | 768 | |
| 769 | SkPath temp; |
| 770 | |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 771 | if (fZeroRad) { |
| 772 | make_arb_round_rect(&temp, r, 0, 0); |
| 773 | |
| 774 | SkASSERT(temp.isRect(NULL)); |
| 775 | } else { |
| 776 | make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15); |
| 777 | } |
robertphillips@google.com | f6fc3fc | 2012-10-17 15:23:21 +0000 | [diff] [blame] | 778 | |
| 779 | canvas->drawPath(temp, paint); |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | private: |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 784 | bool fZeroRad; // should 0 radius rounds rects be tested? |
| 785 | |
robertphillips@google.com | f6fc3fc | 2012-10-17 15:23:21 +0000 | [diff] [blame] | 786 | typedef SkBenchmark INHERITED; |
| 787 | }; |
| 788 | |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 789 | class ConservativelyContainsBench : public SkBenchmark { |
| 790 | public: |
| 791 | enum Type { |
| 792 | kRect_Type, |
| 793 | kRoundRect_Type, |
| 794 | kOval_Type, |
| 795 | }; |
| 796 | |
| 797 | ConservativelyContainsBench(void* param, Type type) : INHERITED(param) { |
| 798 | fIsRendering = false; |
| 799 | fParity = false; |
| 800 | fName = "conservatively_contains_"; |
| 801 | switch (type) { |
| 802 | case kRect_Type: |
| 803 | fName.append("rect"); |
| 804 | fPath.addRect(kBaseRect); |
| 805 | break; |
| 806 | case kRoundRect_Type: |
| 807 | fName.append("round_rect"); |
| 808 | fPath.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1]); |
| 809 | break; |
| 810 | case kOval_Type: |
| 811 | fName.append("oval"); |
| 812 | fPath.addOval(kBaseRect); |
| 813 | break; |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | private: |
| 818 | virtual const char* onGetName() SK_OVERRIDE { |
| 819 | return fName.c_str(); |
| 820 | } |
| 821 | |
sugoi@google.com | 77472f0 | 2013-03-05 18:50:01 +0000 | [diff] [blame] | 822 | virtual void onDraw(SkCanvas*) SK_OVERRIDE { |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 823 | for (int i = 0; i < N; ++i) { |
| 824 | const SkRect& rect = fQueryRects[i % kQueryRectCnt]; |
| 825 | fParity = fParity != fPath.conservativelyContainsRect(rect); |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | virtual void onPreDraw() SK_OVERRIDE { |
| 830 | fQueryRects.setCount(kQueryRectCnt); |
| 831 | |
| 832 | SkRandom rand; |
| 833 | for (int i = 0; i < kQueryRectCnt; ++i) { |
| 834 | SkSize size; |
| 835 | SkPoint xy; |
| 836 | size.fWidth = rand.nextRangeScalar(kQueryMin.fWidth, kQueryMax.fWidth); |
| 837 | size.fHeight = rand.nextRangeScalar(kQueryMin.fHeight, kQueryMax.fHeight); |
| 838 | xy.fX = rand.nextRangeScalar(kBounds.fLeft, kBounds.fRight - size.fWidth); |
| 839 | xy.fY = rand.nextRangeScalar(kBounds.fTop, kBounds.fBottom - size.fHeight); |
| 840 | |
| 841 | fQueryRects[i] = SkRect::MakeXYWH(xy.fX, xy.fY, size.fWidth, size.fHeight); |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | virtual void onPostDraw() SK_OVERRIDE { |
| 846 | fQueryRects.setCount(0); |
| 847 | } |
| 848 | |
| 849 | enum { |
| 850 | N = SkBENCHLOOP(100000), |
| 851 | kQueryRectCnt = 400, |
| 852 | }; |
| 853 | static const SkRect kBounds; // bounds for all random query rects |
| 854 | static const SkSize kQueryMin; // minimum query rect size, should be <= kQueryMax |
| 855 | static const SkSize kQueryMax; // max query rect size, should < kBounds |
| 856 | static const SkRect kBaseRect; // rect that is used to construct the path |
| 857 | static const SkScalar kRRRadii[2]; // x and y radii for round rect |
| 858 | |
| 859 | SkString fName; |
| 860 | SkPath fPath; |
| 861 | bool fParity; |
| 862 | SkTDArray<SkRect> fQueryRects; |
| 863 | |
| 864 | typedef SkBenchmark INHERITED; |
| 865 | }; |
| 866 | |
mike@reedtribe.org | 8d55101 | 2013-04-14 02:40:50 +0000 | [diff] [blame] | 867 | /////////////////////////////////////////////////////////////////////////////// |
| 868 | |
| 869 | #include "SkGeometry.h" |
| 870 | |
mike@reedtribe.org | 28552e1 | 2013-04-26 00:58:29 +0000 | [diff] [blame^] | 871 | class ConicBench_Chop5 : public SkBenchmark { |
mike@reedtribe.org | 8d55101 | 2013-04-14 02:40:50 +0000 | [diff] [blame] | 872 | enum { |
| 873 | N = 100000 |
| 874 | }; |
mike@reedtribe.org | 28552e1 | 2013-04-26 00:58:29 +0000 | [diff] [blame^] | 875 | SkConic fRQ; |
mike@reedtribe.org | 8d55101 | 2013-04-14 02:40:50 +0000 | [diff] [blame] | 876 | public: |
mike@reedtribe.org | 28552e1 | 2013-04-26 00:58:29 +0000 | [diff] [blame^] | 877 | ConicBench_Chop5(void* param) : INHERITED(param) { |
mike@reedtribe.org | 8d55101 | 2013-04-14 02:40:50 +0000 | [diff] [blame] | 878 | fRQ.fPts[0].set(0, 0); |
| 879 | fRQ.fPts[1].set(100, 0); |
| 880 | fRQ.fPts[2].set(100, 100); |
| 881 | fRQ.fW = SkScalarCos(SK_ScalarPI/4); |
| 882 | } |
skia.committer@gmail.com | ab38e56 | 2013-04-14 07:01:08 +0000 | [diff] [blame] | 883 | |
mike@reedtribe.org | 8d55101 | 2013-04-14 02:40:50 +0000 | [diff] [blame] | 884 | private: |
| 885 | virtual const char* onGetName() SK_OVERRIDE { |
| 886 | return "ratquad-chop-0.5"; |
| 887 | } |
skia.committer@gmail.com | ab38e56 | 2013-04-14 07:01:08 +0000 | [diff] [blame] | 888 | |
mike@reedtribe.org | 8d55101 | 2013-04-14 02:40:50 +0000 | [diff] [blame] | 889 | virtual void onDraw(SkCanvas*) SK_OVERRIDE { |
mike@reedtribe.org | 28552e1 | 2013-04-26 00:58:29 +0000 | [diff] [blame^] | 890 | SkConic dst[2]; |
mike@reedtribe.org | 8d55101 | 2013-04-14 02:40:50 +0000 | [diff] [blame] | 891 | for (int i = 0; i < N; ++i) { |
| 892 | fRQ.chopAt(0.5f, dst); |
| 893 | } |
| 894 | } |
skia.committer@gmail.com | ab38e56 | 2013-04-14 07:01:08 +0000 | [diff] [blame] | 895 | |
mike@reedtribe.org | 8d55101 | 2013-04-14 02:40:50 +0000 | [diff] [blame] | 896 | typedef SkBenchmark INHERITED; |
| 897 | }; |
| 898 | |
mike@reedtribe.org | 28552e1 | 2013-04-26 00:58:29 +0000 | [diff] [blame^] | 899 | class ConicBench_ChopHalf : public SkBenchmark { |
mike@reedtribe.org | 8d55101 | 2013-04-14 02:40:50 +0000 | [diff] [blame] | 900 | enum { |
| 901 | N = 100000 |
| 902 | }; |
mike@reedtribe.org | 28552e1 | 2013-04-26 00:58:29 +0000 | [diff] [blame^] | 903 | SkConic fRQ; |
mike@reedtribe.org | 8d55101 | 2013-04-14 02:40:50 +0000 | [diff] [blame] | 904 | public: |
mike@reedtribe.org | 28552e1 | 2013-04-26 00:58:29 +0000 | [diff] [blame^] | 905 | ConicBench_ChopHalf(void* param) : INHERITED(param) { |
mike@reedtribe.org | 8d55101 | 2013-04-14 02:40:50 +0000 | [diff] [blame] | 906 | fRQ.fPts[0].set(0, 0); |
| 907 | fRQ.fPts[1].set(100, 0); |
| 908 | fRQ.fPts[2].set(100, 100); |
| 909 | fRQ.fW = SkScalarCos(SK_ScalarPI/4); |
| 910 | } |
skia.committer@gmail.com | ab38e56 | 2013-04-14 07:01:08 +0000 | [diff] [blame] | 911 | |
mike@reedtribe.org | 8d55101 | 2013-04-14 02:40:50 +0000 | [diff] [blame] | 912 | private: |
| 913 | virtual const char* onGetName() SK_OVERRIDE { |
| 914 | return "ratquad-chop-half"; |
| 915 | } |
skia.committer@gmail.com | ab38e56 | 2013-04-14 07:01:08 +0000 | [diff] [blame] | 916 | |
mike@reedtribe.org | 8d55101 | 2013-04-14 02:40:50 +0000 | [diff] [blame] | 917 | virtual void onDraw(SkCanvas*) SK_OVERRIDE { |
mike@reedtribe.org | 28552e1 | 2013-04-26 00:58:29 +0000 | [diff] [blame^] | 918 | SkConic dst[2]; |
mike@reedtribe.org | 8d55101 | 2013-04-14 02:40:50 +0000 | [diff] [blame] | 919 | for (int i = 0; i < N; ++i) { |
| 920 | fRQ.chop(dst); |
| 921 | } |
| 922 | } |
skia.committer@gmail.com | ab38e56 | 2013-04-14 07:01:08 +0000 | [diff] [blame] | 923 | |
mike@reedtribe.org | 8d55101 | 2013-04-14 02:40:50 +0000 | [diff] [blame] | 924 | typedef SkBenchmark INHERITED; |
| 925 | }; |
| 926 | |
| 927 | /////////////////////////////////////////////////////////////////////////////// |
| 928 | |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 929 | const SkRect ConservativelyContainsBench::kBounds = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100)); |
| 930 | const SkSize ConservativelyContainsBench::kQueryMin = SkSize::Make(SkIntToScalar(1), SkIntToScalar(1)); |
| 931 | const SkSize ConservativelyContainsBench::kQueryMax = SkSize::Make(SkIntToScalar(40), SkIntToScalar(40)); |
| 932 | const SkRect ConservativelyContainsBench::kBaseRect = SkRect::MakeXYWH(SkIntToScalar(25), SkIntToScalar(25), SkIntToScalar(50), SkIntToScalar(50)); |
| 933 | const SkScalar ConservativelyContainsBench::kRRRadii[2] = {SkIntToScalar(5), SkIntToScalar(10)}; |
| 934 | |
mike@reedtribe.org | 0cf7b82 | 2013-02-04 05:05:36 +0000 | [diff] [blame] | 935 | DEF_BENCH( return new TrianglePathBench(p, FLAGS00); ) |
| 936 | DEF_BENCH( return new TrianglePathBench(p, FLAGS01); ) |
| 937 | DEF_BENCH( return new TrianglePathBench(p, FLAGS10); ) |
| 938 | DEF_BENCH( return new TrianglePathBench(p, FLAGS11); ) |
reed@google.com | d34658a | 2011-04-11 13:12:51 +0000 | [diff] [blame] | 939 | |
mike@reedtribe.org | 0cf7b82 | 2013-02-04 05:05:36 +0000 | [diff] [blame] | 940 | DEF_BENCH( return new RectPathBench(p, FLAGS00); ) |
| 941 | DEF_BENCH( return new RectPathBench(p, FLAGS01); ) |
| 942 | DEF_BENCH( return new RectPathBench(p, FLAGS10); ) |
| 943 | DEF_BENCH( return new RectPathBench(p, FLAGS11); ) |
reed@google.com | d34658a | 2011-04-11 13:12:51 +0000 | [diff] [blame] | 944 | |
mike@reedtribe.org | 0cf7b82 | 2013-02-04 05:05:36 +0000 | [diff] [blame] | 945 | DEF_BENCH( return new OvalPathBench(p, FLAGS00); ) |
| 946 | DEF_BENCH( return new OvalPathBench(p, FLAGS01); ) |
| 947 | DEF_BENCH( return new OvalPathBench(p, FLAGS10); ) |
| 948 | DEF_BENCH( return new OvalPathBench(p, FLAGS11); ) |
reed@google.com | d34658a | 2011-04-11 13:12:51 +0000 | [diff] [blame] | 949 | |
mike@reedtribe.org | 0cf7b82 | 2013-02-04 05:05:36 +0000 | [diff] [blame] | 950 | DEF_BENCH( return new CirclePathBench(p, FLAGS00); ) |
| 951 | DEF_BENCH( return new CirclePathBench(p, FLAGS01); ) |
| 952 | DEF_BENCH( return new CirclePathBench(p, FLAGS10); ) |
| 953 | DEF_BENCH( return new CirclePathBench(p, FLAGS11); ) |
bsalomon@google.com | 1647a19 | 2012-04-11 15:34:46 +0000 | [diff] [blame] | 954 | |
mike@reedtribe.org | 0cf7b82 | 2013-02-04 05:05:36 +0000 | [diff] [blame] | 955 | DEF_BENCH( return new SawToothPathBench(p, FLAGS00); ) |
| 956 | DEF_BENCH( return new SawToothPathBench(p, FLAGS01); ) |
reed@google.com | d34658a | 2011-04-11 13:12:51 +0000 | [diff] [blame] | 957 | |
mike@reedtribe.org | 0cf7b82 | 2013-02-04 05:05:36 +0000 | [diff] [blame] | 958 | DEF_BENCH( return new LongCurvedPathBench(p, FLAGS00); ) |
| 959 | DEF_BENCH( return new LongCurvedPathBench(p, FLAGS01); ) |
| 960 | DEF_BENCH( return new LongLinePathBench(p, FLAGS00); ) |
| 961 | DEF_BENCH( return new LongLinePathBench(p, FLAGS01); ) |
tomhudson@google.com | 6e8d335 | 2011-06-22 17:16:35 +0000 | [diff] [blame] | 962 | |
mike@reedtribe.org | 0cf7b82 | 2013-02-04 05:05:36 +0000 | [diff] [blame] | 963 | DEF_BENCH( return new PathCreateBench(p); ) |
| 964 | DEF_BENCH( return new PathCopyBench(p); ) |
| 965 | DEF_BENCH( return new PathTransformBench(true, p); ) |
| 966 | DEF_BENCH( return new PathTransformBench(false, p); ) |
| 967 | DEF_BENCH( return new PathEqualityBench(p); ) |
senorblanco@chromium.org | e50f736 | 2012-01-12 19:10:35 +0000 | [diff] [blame] | 968 | |
mike@reedtribe.org | 0cf7b82 | 2013-02-04 05:05:36 +0000 | [diff] [blame] | 969 | DEF_BENCH( return new SkBench_AddPathTest(SkBench_AddPathTest::kAdd_AddType, p); ) |
| 970 | DEF_BENCH( return new SkBench_AddPathTest(SkBench_AddPathTest::kAddTrans_AddType, p); ) |
| 971 | DEF_BENCH( return new SkBench_AddPathTest(SkBench_AddPathTest::kAddMatrix_AddType, p); ) |
| 972 | DEF_BENCH( return new SkBench_AddPathTest(SkBench_AddPathTest::kPathTo_AddType, p); ) |
| 973 | DEF_BENCH( return new SkBench_AddPathTest(SkBench_AddPathTest::kReverseAdd_AddType, p); ) |
| 974 | DEF_BENCH( return new SkBench_AddPathTest(SkBench_AddPathTest::kReversePathTo_AddType, p); ) |
senorblanco@chromium.org | e50f736 | 2012-01-12 19:10:35 +0000 | [diff] [blame] | 975 | |
mike@reedtribe.org | 0cf7b82 | 2013-02-04 05:05:36 +0000 | [diff] [blame] | 976 | DEF_BENCH( return new CirclesBench(p, FLAGS00); ) |
| 977 | DEF_BENCH( return new CirclesBench(p, FLAGS01); ) |
| 978 | DEF_BENCH( return new ArbRoundRectBench(p, false); ) |
| 979 | DEF_BENCH( return new ArbRoundRectBench(p, true); ) |
| 980 | DEF_BENCH( return new ConservativelyContainsBench(p, ConservativelyContainsBench::kRect_Type); ) |
| 981 | DEF_BENCH( return new ConservativelyContainsBench(p, ConservativelyContainsBench::kRoundRect_Type); ) |
| 982 | DEF_BENCH( return new ConservativelyContainsBench(p, ConservativelyContainsBench::kOval_Type); ) |
mike@reedtribe.org | 8d55101 | 2013-04-14 02:40:50 +0000 | [diff] [blame] | 983 | |
mike@reedtribe.org | 28552e1 | 2013-04-26 00:58:29 +0000 | [diff] [blame^] | 984 | DEF_BENCH( return new ConicBench_Chop5(p) ) |
| 985 | DEF_BENCH( return new ConicBench_ChopHalf(p) ) |