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 | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 125 | SkRect r = { 10, 10, 30, 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 | c7a37c7 | 2012-10-19 01:26:18 +0000 | [diff] [blame] | 748 | #ifdef SK_REDEFINE_ROOT2OVER2_TO_MAKE_ARCTOS_CONVEX |
robertphillips@google.com | b95eaa8 | 2012-10-18 15:26:12 +0000 | [diff] [blame] | 749 | SkASSERT(path->isConvex()); |
robertphillips@google.com | c7a37c7 | 2012-10-19 01:26:18 +0000 | [diff] [blame] | 750 | #endif |
robertphillips@google.com | f6fc3fc | 2012-10-17 15:23:21 +0000 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 754 | SkRandom rand; |
| 755 | SkRect r; |
| 756 | |
| 757 | for (int i = 0; i < 5000; ++i) { |
| 758 | SkPaint paint; |
| 759 | paint.setColor(0xff000000 | rand.nextU()); |
| 760 | paint.setAntiAlias(true); |
| 761 | |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 762 | SkScalar size = rand.nextUScalar1() * 30; |
| 763 | if (size < SK_Scalar1) { |
robertphillips@google.com | b95eaa8 | 2012-10-18 15:26:12 +0000 | [diff] [blame] | 764 | continue; |
| 765 | } |
robertphillips@google.com | f6fc3fc | 2012-10-17 15:23:21 +0000 | [diff] [blame] | 766 | r.fLeft = rand.nextUScalar1() * 300; |
| 767 | r.fTop = rand.nextUScalar1() * 300; |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 768 | r.fRight = r.fLeft + 2 * size; |
| 769 | r.fBottom = r.fTop + 2 * size; |
robertphillips@google.com | f6fc3fc | 2012-10-17 15:23:21 +0000 | [diff] [blame] | 770 | |
| 771 | SkPath temp; |
| 772 | |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 773 | if (fZeroRad) { |
| 774 | make_arb_round_rect(&temp, r, 0, 0); |
| 775 | |
| 776 | SkASSERT(temp.isRect(NULL)); |
| 777 | } else { |
| 778 | make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15); |
| 779 | } |
robertphillips@google.com | f6fc3fc | 2012-10-17 15:23:21 +0000 | [diff] [blame] | 780 | |
| 781 | canvas->drawPath(temp, paint); |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | private: |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 786 | bool fZeroRad; // should 0 radius rounds rects be tested? |
| 787 | |
robertphillips@google.com | f6fc3fc | 2012-10-17 15:23:21 +0000 | [diff] [blame] | 788 | typedef SkBenchmark INHERITED; |
| 789 | }; |
| 790 | |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 791 | class ConservativelyContainsBench : public SkBenchmark { |
| 792 | public: |
| 793 | enum Type { |
| 794 | kRect_Type, |
| 795 | kRoundRect_Type, |
| 796 | kOval_Type, |
| 797 | }; |
| 798 | |
| 799 | ConservativelyContainsBench(void* param, Type type) : INHERITED(param) { |
| 800 | fIsRendering = false; |
| 801 | fParity = false; |
| 802 | fName = "conservatively_contains_"; |
| 803 | switch (type) { |
| 804 | case kRect_Type: |
| 805 | fName.append("rect"); |
| 806 | fPath.addRect(kBaseRect); |
| 807 | break; |
| 808 | case kRoundRect_Type: |
| 809 | fName.append("round_rect"); |
| 810 | fPath.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1]); |
| 811 | break; |
| 812 | case kOval_Type: |
| 813 | fName.append("oval"); |
| 814 | fPath.addOval(kBaseRect); |
| 815 | break; |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | private: |
| 820 | virtual const char* onGetName() SK_OVERRIDE { |
| 821 | return fName.c_str(); |
| 822 | } |
| 823 | |
| 824 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 825 | for (int i = 0; i < N; ++i) { |
| 826 | const SkRect& rect = fQueryRects[i % kQueryRectCnt]; |
| 827 | fParity = fParity != fPath.conservativelyContainsRect(rect); |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | virtual void onPreDraw() SK_OVERRIDE { |
| 832 | fQueryRects.setCount(kQueryRectCnt); |
| 833 | |
| 834 | SkRandom rand; |
| 835 | for (int i = 0; i < kQueryRectCnt; ++i) { |
| 836 | SkSize size; |
| 837 | SkPoint xy; |
| 838 | size.fWidth = rand.nextRangeScalar(kQueryMin.fWidth, kQueryMax.fWidth); |
| 839 | size.fHeight = rand.nextRangeScalar(kQueryMin.fHeight, kQueryMax.fHeight); |
| 840 | xy.fX = rand.nextRangeScalar(kBounds.fLeft, kBounds.fRight - size.fWidth); |
| 841 | xy.fY = rand.nextRangeScalar(kBounds.fTop, kBounds.fBottom - size.fHeight); |
| 842 | |
| 843 | fQueryRects[i] = SkRect::MakeXYWH(xy.fX, xy.fY, size.fWidth, size.fHeight); |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | virtual void onPostDraw() SK_OVERRIDE { |
| 848 | fQueryRects.setCount(0); |
| 849 | } |
| 850 | |
| 851 | enum { |
| 852 | N = SkBENCHLOOP(100000), |
| 853 | kQueryRectCnt = 400, |
| 854 | }; |
| 855 | static const SkRect kBounds; // bounds for all random query rects |
| 856 | static const SkSize kQueryMin; // minimum query rect size, should be <= kQueryMax |
| 857 | static const SkSize kQueryMax; // max query rect size, should < kBounds |
| 858 | static const SkRect kBaseRect; // rect that is used to construct the path |
| 859 | static const SkScalar kRRRadii[2]; // x and y radii for round rect |
| 860 | |
| 861 | SkString fName; |
| 862 | SkPath fPath; |
| 863 | bool fParity; |
| 864 | SkTDArray<SkRect> fQueryRects; |
| 865 | |
| 866 | typedef SkBenchmark INHERITED; |
| 867 | }; |
| 868 | |
| 869 | const SkRect ConservativelyContainsBench::kBounds = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100)); |
| 870 | const SkSize ConservativelyContainsBench::kQueryMin = SkSize::Make(SkIntToScalar(1), SkIntToScalar(1)); |
| 871 | const SkSize ConservativelyContainsBench::kQueryMax = SkSize::Make(SkIntToScalar(40), SkIntToScalar(40)); |
| 872 | const SkRect ConservativelyContainsBench::kBaseRect = SkRect::MakeXYWH(SkIntToScalar(25), SkIntToScalar(25), SkIntToScalar(50), SkIntToScalar(50)); |
| 873 | const SkScalar ConservativelyContainsBench::kRRRadii[2] = {SkIntToScalar(5), SkIntToScalar(10)}; |
| 874 | |
reed@google.com | d34658a | 2011-04-11 13:12:51 +0000 | [diff] [blame] | 875 | static SkBenchmark* FactT00(void* p) { return new TrianglePathBench(p, FLAGS00); } |
| 876 | static SkBenchmark* FactT01(void* p) { return new TrianglePathBench(p, FLAGS01); } |
| 877 | static SkBenchmark* FactT10(void* p) { return new TrianglePathBench(p, FLAGS10); } |
| 878 | static SkBenchmark* FactT11(void* p) { return new TrianglePathBench(p, FLAGS11); } |
| 879 | |
| 880 | static SkBenchmark* FactR00(void* p) { return new RectPathBench(p, FLAGS00); } |
| 881 | static SkBenchmark* FactR01(void* p) { return new RectPathBench(p, FLAGS01); } |
| 882 | static SkBenchmark* FactR10(void* p) { return new RectPathBench(p, FLAGS10); } |
| 883 | static SkBenchmark* FactR11(void* p) { return new RectPathBench(p, FLAGS11); } |
| 884 | |
| 885 | static SkBenchmark* FactO00(void* p) { return new OvalPathBench(p, FLAGS00); } |
| 886 | static SkBenchmark* FactO01(void* p) { return new OvalPathBench(p, FLAGS01); } |
| 887 | static SkBenchmark* FactO10(void* p) { return new OvalPathBench(p, FLAGS10); } |
| 888 | static SkBenchmark* FactO11(void* p) { return new OvalPathBench(p, FLAGS11); } |
| 889 | |
bsalomon@google.com | 1647a19 | 2012-04-11 15:34:46 +0000 | [diff] [blame] | 890 | static SkBenchmark* FactC00(void* p) { return new CirclePathBench(p, FLAGS00); } |
| 891 | static SkBenchmark* FactC01(void* p) { return new CirclePathBench(p, FLAGS01); } |
| 892 | static SkBenchmark* FactC10(void* p) { return new CirclePathBench(p, FLAGS10); } |
| 893 | static SkBenchmark* FactC11(void* p) { return new CirclePathBench(p, FLAGS11); } |
| 894 | |
reed@google.com | d34658a | 2011-04-11 13:12:51 +0000 | [diff] [blame] | 895 | static SkBenchmark* FactS00(void* p) { return new SawToothPathBench(p, FLAGS00); } |
| 896 | static SkBenchmark* FactS01(void* p) { return new SawToothPathBench(p, FLAGS01); } |
| 897 | |
tomhudson@google.com | 6e8d335 | 2011-06-22 17:16:35 +0000 | [diff] [blame] | 898 | static SkBenchmark* FactLC00(void* p) { |
| 899 | return new LongCurvedPathBench(p, FLAGS00); |
| 900 | } |
| 901 | static SkBenchmark* FactLC01(void* p) { |
| 902 | return new LongCurvedPathBench(p, FLAGS01); |
| 903 | } |
| 904 | |
senorblanco@chromium.org | e50f736 | 2012-01-12 19:10:35 +0000 | [diff] [blame] | 905 | static SkBenchmark* FactLL00(void* p) { |
| 906 | return new LongLinePathBench(p, FLAGS00); |
| 907 | } |
| 908 | |
| 909 | static SkBenchmark* FactLL01(void* p) { |
| 910 | return new LongLinePathBench(p, FLAGS01); |
| 911 | } |
| 912 | |
reed@google.com | d34658a | 2011-04-11 13:12:51 +0000 | [diff] [blame] | 913 | static BenchRegistry gRegT00(FactT00); |
| 914 | static BenchRegistry gRegT01(FactT01); |
| 915 | static BenchRegistry gRegT10(FactT10); |
| 916 | static BenchRegistry gRegT11(FactT11); |
| 917 | |
| 918 | static BenchRegistry gRegR00(FactR00); |
| 919 | static BenchRegistry gRegR01(FactR01); |
| 920 | static BenchRegistry gRegR10(FactR10); |
| 921 | static BenchRegistry gRegR11(FactR11); |
| 922 | |
| 923 | static BenchRegistry gRegO00(FactO00); |
| 924 | static BenchRegistry gRegO01(FactO01); |
| 925 | static BenchRegistry gRegO10(FactO10); |
| 926 | static BenchRegistry gRegO11(FactO11); |
| 927 | |
bsalomon@google.com | 1647a19 | 2012-04-11 15:34:46 +0000 | [diff] [blame] | 928 | static BenchRegistry gRegC00(FactC00); |
| 929 | static BenchRegistry gRegC01(FactC01); |
| 930 | static BenchRegistry gRegC10(FactC10); |
| 931 | static BenchRegistry gRegC11(FactC11); |
| 932 | |
reed@google.com | d34658a | 2011-04-11 13:12:51 +0000 | [diff] [blame] | 933 | static BenchRegistry gRegS00(FactS00); |
| 934 | static BenchRegistry gRegS01(FactS01); |
| 935 | |
tomhudson@google.com | 6e8d335 | 2011-06-22 17:16:35 +0000 | [diff] [blame] | 936 | static BenchRegistry gRegLC00(FactLC00); |
| 937 | static BenchRegistry gRegLC01(FactLC01); |
| 938 | |
senorblanco@chromium.org | e50f736 | 2012-01-12 19:10:35 +0000 | [diff] [blame] | 939 | static BenchRegistry gRegLL00(FactLL00); |
| 940 | static BenchRegistry gRegLL01(FactLL01); |
| 941 | |
bsalomon@google.com | 30e6d2c | 2012-08-13 14:03:31 +0000 | [diff] [blame] | 942 | static SkBenchmark* FactCreate(void* p) { return new PathCreateBench(p); } |
| 943 | static BenchRegistry gRegCreate(FactCreate); |
| 944 | |
| 945 | static SkBenchmark* FactCopy(void* p) { return new PathCopyBench(p); } |
| 946 | static BenchRegistry gRegCopy(FactCopy); |
| 947 | |
| 948 | static SkBenchmark* FactPathTransformInPlace(void* p) { return new PathTransformBench(true, p); } |
| 949 | static BenchRegistry gRegPathTransformInPlace(FactPathTransformInPlace); |
| 950 | |
| 951 | static SkBenchmark* FactPathTransformCopy(void* p) { return new PathTransformBench(false, p); } |
| 952 | static BenchRegistry gRegPathTransformCopy(FactPathTransformCopy); |
| 953 | |
| 954 | static SkBenchmark* FactEquality(void* p) { return new PathEqualityBench(p); } |
| 955 | static BenchRegistry gRegEquality(FactEquality); |
| 956 | |
| 957 | static SkBenchmark* FactAdd(void* p) { return new SkBench_AddPathTest(SkBench_AddPathTest::kAdd_AddType, p); } |
| 958 | static SkBenchmark* FactAddTrans(void* p) { return new SkBench_AddPathTest(SkBench_AddPathTest::kAddTrans_AddType, p); } |
| 959 | static SkBenchmark* FactAddMatrix(void* p) { return new SkBench_AddPathTest(SkBench_AddPathTest::kAddMatrix_AddType, p); } |
| 960 | static SkBenchmark* FactPathTo(void* p) { return new SkBench_AddPathTest(SkBench_AddPathTest::kPathTo_AddType, p); } |
| 961 | static SkBenchmark* FactReverseAdd(void* p) { return new SkBench_AddPathTest(SkBench_AddPathTest::kReverseAdd_AddType, p); } |
| 962 | static SkBenchmark* FactReverseTo(void* p) { return new SkBench_AddPathTest(SkBench_AddPathTest::kReversePathTo_AddType, p); } |
| 963 | |
| 964 | static BenchRegistry gRegAdd(FactAdd); |
| 965 | static BenchRegistry gRegAddTrans(FactAddTrans); |
| 966 | static BenchRegistry gRegAddMatrix(FactAddMatrix); |
| 967 | static BenchRegistry gRegPathTo(FactPathTo); |
| 968 | static BenchRegistry gRegReverseAdd(FactReverseAdd); |
| 969 | static BenchRegistry gRegReverseTo(FactReverseTo); |
robertphillips@google.com | 17bb458 | 2012-08-20 17:24:16 +0000 | [diff] [blame] | 970 | |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 971 | static SkBenchmark* CirclesTest(void* p) { return new CirclesBench(p, FLAGS00); } |
robertphillips@google.com | 17bb458 | 2012-08-20 17:24:16 +0000 | [diff] [blame] | 972 | static BenchRegistry gRegCirclesTest(CirclesTest); |
| 973 | |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 974 | static SkBenchmark* CirclesStrokeTest(void* p) { return new CirclesBench(p, FLAGS01); } |
| 975 | static BenchRegistry gRegCirclesStrokeTest(CirclesStrokeTest); |
| 976 | |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 977 | static SkBenchmark* ArbRoundRectTest(void* p) { return new ArbRoundRectBench(p, false); } |
robertphillips@google.com | f6fc3fc | 2012-10-17 15:23:21 +0000 | [diff] [blame] | 978 | static BenchRegistry gRegArbRoundRectTest(ArbRoundRectTest); |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 979 | |
| 980 | static SkBenchmark* ZeroRadRoundRectTest(void* p) { return new ArbRoundRectBench(p, true); } |
| 981 | static BenchRegistry gRegZeroRadRoundRectTest(ZeroRadRoundRectTest); |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 982 | |
| 983 | static SkBenchmark* RectConservativelyContainsTest(void* p) { return new ConservativelyContainsBench(p, ConservativelyContainsBench::kRect_Type); } |
| 984 | static BenchRegistry gRegRectConservativelyContainsTest(RectConservativelyContainsTest); |
| 985 | |
| 986 | static SkBenchmark* RoundRectConservativelyContainsTest(void* p) { return new ConservativelyContainsBench(p, ConservativelyContainsBench::kRoundRect_Type); } |
| 987 | static BenchRegistry gRegRoundRectConservativelyContainsTest(RoundRectConservativelyContainsTest); |
| 988 | |
| 989 | static SkBenchmark* OvalConservativelyContainsTest(void* p) { return new ConservativelyContainsBench(p, ConservativelyContainsBench::kOval_Type); } |
| 990 | static BenchRegistry gRegOvalConservativelyContainsTest(OvalConservativelyContainsTest); |