reed@google.com | 57c4957 | 2011-10-31 14:33:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkBenchmark.h" |
| 9 | #include "SkAAClip.h" |
| 10 | #include "SkPath.h" |
| 11 | #include "SkRegion.h" |
| 12 | #include "SkString.h" |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 13 | #include "SkCanvas.h" |
robertphillips@google.com | 6bb99e3 | 2012-05-09 15:48:31 +0000 | [diff] [blame] | 14 | #include "SkRandom.h" |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 15 | |
| 16 | //////////////////////////////////////////////////////////////////////////////// |
| 17 | // This bench tests out AA/BW clipping via canvas' clipPath and clipRect calls |
| 18 | class AAClipBench : public SkBenchmark { |
| 19 | SkString fName; |
| 20 | SkPath fClipPath; |
| 21 | SkRect fClipRect; |
| 22 | SkRect fDrawRect; |
| 23 | bool fDoPath; |
| 24 | bool fDoAA; |
| 25 | |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 26 | public: |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 27 | AAClipBench(void* param, bool doPath, bool doAA) |
robertphillips@google.com | 6bb99e3 | 2012-05-09 15:48:31 +0000 | [diff] [blame] | 28 | : INHERITED(param) |
| 29 | , fDoPath(doPath) |
| 30 | , fDoAA(doAA) { |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 31 | |
| 32 | fName.printf("aaclip_%s_%s", |
| 33 | doPath ? "path" : "rect", |
| 34 | doAA ? "AA" : "BW"); |
| 35 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 36 | fClipRect.set(SkFloatToScalar(10.5f), SkFloatToScalar(10.5f), |
caryclark@google.com | 19069a2 | 2012-06-06 12:11:45 +0000 | [diff] [blame] | 37 | SkFloatToScalar(50.5f), SkFloatToScalar(50.5f)); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 38 | fClipPath.addRoundRect(fClipRect, SkIntToScalar(10), SkIntToScalar(10)); |
| 39 | fDrawRect.set(SkIntToScalar(0), SkIntToScalar(0), |
| 40 | SkIntToScalar(100), SkIntToScalar(100)); |
| 41 | |
| 42 | SkASSERT(fClipPath.isConvex()); |
| 43 | } |
| 44 | |
| 45 | protected: |
| 46 | virtual const char* onGetName() { return fName.c_str(); } |
| 47 | virtual void onDraw(SkCanvas* canvas) { |
| 48 | |
| 49 | SkPaint paint; |
| 50 | this->setupPaint(&paint); |
| 51 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame^] | 52 | for (int i = 0; i < this->getLoops(); ++i) { |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 53 | // jostle the clip regions each time to prevent caching |
| 54 | fClipRect.offset((i % 2) == 0 ? SkIntToScalar(10) : SkIntToScalar(-10), 0); |
| 55 | fClipPath.reset(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 56 | fClipPath.addRoundRect(fClipRect, |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 57 | SkIntToScalar(5), SkIntToScalar(5)); |
| 58 | SkASSERT(fClipPath.isConvex()); |
| 59 | |
| 60 | canvas->save(); |
| 61 | #if 1 |
| 62 | if (fDoPath) { |
| 63 | canvas->clipPath(fClipPath, SkRegion::kReplace_Op, fDoAA); |
| 64 | } else { |
| 65 | canvas->clipRect(fClipRect, SkRegion::kReplace_Op, fDoAA); |
| 66 | } |
| 67 | |
| 68 | canvas->drawRect(fDrawRect, paint); |
| 69 | #else |
| 70 | // this path tests out directly draw the clip primitive |
| 71 | // use it to comparing just drawing the clip vs. drawing using |
| 72 | // the clip |
| 73 | if (fDoPath) { |
| 74 | canvas->drawPath(fClipPath, paint); |
| 75 | } else { |
| 76 | canvas->drawRect(fClipRect, paint); |
| 77 | } |
| 78 | #endif |
| 79 | canvas->restore(); |
| 80 | } |
| 81 | } |
| 82 | private: |
| 83 | typedef SkBenchmark INHERITED; |
| 84 | }; |
| 85 | |
| 86 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | 6bb99e3 | 2012-05-09 15:48:31 +0000 | [diff] [blame] | 87 | // This bench tests out nested clip stacks. It is intended to simulate |
| 88 | // how WebKit nests clips. |
| 89 | class NestedAAClipBench : public SkBenchmark { |
| 90 | SkString fName; |
| 91 | bool fDoAA; |
| 92 | SkRect fDrawRect; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 93 | SkRandom fRandom; |
robertphillips@google.com | 6bb99e3 | 2012-05-09 15:48:31 +0000 | [diff] [blame] | 94 | |
robertphillips@google.com | b1af07a | 2012-05-09 16:27:10 +0000 | [diff] [blame] | 95 | static const int kNestingDepth = 3; |
robertphillips@google.com | 6bb99e3 | 2012-05-09 15:48:31 +0000 | [diff] [blame] | 96 | static const int kImageSize = 400; |
| 97 | |
| 98 | SkPoint fSizes[kNestingDepth+1]; |
| 99 | |
| 100 | public: |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 101 | NestedAAClipBench(void* param, bool doAA) |
robertphillips@google.com | 6bb99e3 | 2012-05-09 15:48:31 +0000 | [diff] [blame] | 102 | : INHERITED(param) |
| 103 | , fDoAA(doAA) { |
| 104 | |
| 105 | fName.printf("nested_aaclip_%s", doAA ? "AA" : "BW"); |
| 106 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 107 | fDrawRect = SkRect::MakeLTRB(0, 0, |
| 108 | SkIntToScalar(kImageSize), |
robertphillips@google.com | 6bb99e3 | 2012-05-09 15:48:31 +0000 | [diff] [blame] | 109 | SkIntToScalar(kImageSize)); |
| 110 | |
| 111 | fSizes[0].set(SkIntToScalar(kImageSize), SkIntToScalar(kImageSize)); |
| 112 | |
| 113 | for (int i = 1; i < kNestingDepth+1; ++i) { |
| 114 | fSizes[i].set(fSizes[i-1].fX/2, fSizes[i-1].fY/2); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | protected: |
| 119 | virtual const char* onGetName() { return fName.c_str(); } |
| 120 | |
| 121 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 122 | void recurse(SkCanvas* canvas, |
robertphillips@google.com | 6bb99e3 | 2012-05-09 15:48:31 +0000 | [diff] [blame] | 123 | int depth, |
| 124 | const SkPoint& offset) { |
| 125 | |
| 126 | canvas->save(); |
| 127 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 128 | SkRect temp = SkRect::MakeLTRB(0, 0, |
robertphillips@google.com | 6bb99e3 | 2012-05-09 15:48:31 +0000 | [diff] [blame] | 129 | fSizes[depth].fX, fSizes[depth].fY); |
| 130 | temp.offset(offset); |
| 131 | |
| 132 | SkPath path; |
| 133 | path.addRoundRect(temp, SkIntToScalar(3), SkIntToScalar(3)); |
| 134 | SkASSERT(path.isConvex()); |
| 135 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 136 | canvas->clipPath(path, |
| 137 | 0 == depth ? SkRegion::kReplace_Op : |
robertphillips@google.com | 6bb99e3 | 2012-05-09 15:48:31 +0000 | [diff] [blame] | 138 | SkRegion::kIntersect_Op, |
| 139 | fDoAA); |
| 140 | |
| 141 | if (kNestingDepth == depth) { |
| 142 | // we only draw the draw rect at the lowest nesting level |
| 143 | SkPaint paint; |
| 144 | paint.setColor(0xff000000 | fRandom.nextU()); |
| 145 | canvas->drawRect(fDrawRect, paint); |
| 146 | } else { |
| 147 | SkPoint childOffset = offset; |
| 148 | this->recurse(canvas, depth+1, childOffset); |
| 149 | |
| 150 | childOffset += fSizes[depth+1]; |
| 151 | this->recurse(canvas, depth+1, childOffset); |
| 152 | |
| 153 | childOffset.fX = offset.fX + fSizes[depth+1].fX; |
| 154 | childOffset.fY = offset.fY; |
| 155 | this->recurse(canvas, depth+1, childOffset); |
| 156 | |
| 157 | childOffset.fX = offset.fX; |
| 158 | childOffset.fY = offset.fY + fSizes[depth+1].fY; |
| 159 | this->recurse(canvas, depth+1, childOffset); |
| 160 | } |
| 161 | |
| 162 | canvas->restore(); |
| 163 | } |
| 164 | |
| 165 | virtual void onDraw(SkCanvas* canvas) { |
| 166 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame^] | 167 | for (int i = 0; i < this->getLoops(); ++i) { |
robertphillips@google.com | 6bb99e3 | 2012-05-09 15:48:31 +0000 | [diff] [blame] | 168 | SkPoint offset = SkPoint::Make(0, 0); |
| 169 | this->recurse(canvas, 0, offset); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | private: |
| 174 | typedef SkBenchmark INHERITED; |
| 175 | }; |
| 176 | |
| 177 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | 57c4957 | 2011-10-31 14:33:35 +0000 | [diff] [blame] | 178 | class AAClipBuilderBench : public SkBenchmark { |
| 179 | SkString fName; |
| 180 | SkPath fPath; |
| 181 | SkRect fRect; |
| 182 | SkRegion fRegion; |
| 183 | bool fDoPath; |
| 184 | bool fDoAA; |
| 185 | |
reed@google.com | 57c4957 | 2011-10-31 14:33:35 +0000 | [diff] [blame] | 186 | public: |
| 187 | AAClipBuilderBench(void* param, bool doPath, bool doAA) : INHERITED(param) { |
| 188 | fDoPath = doPath; |
| 189 | fDoAA = doAA; |
| 190 | |
| 191 | fName.printf("aaclip_build_%s_%s", doPath ? "path" : "rect", |
| 192 | doAA ? "AA" : "BW"); |
| 193 | |
| 194 | fRegion.setRect(0, 0, 640, 480); |
| 195 | fRect.set(fRegion.getBounds()); |
| 196 | fRect.inset(SK_Scalar1/4, SK_Scalar1/4); |
| 197 | fPath.addRoundRect(fRect, SkIntToScalar(20), SkIntToScalar(20)); |
| 198 | } |
| 199 | |
| 200 | protected: |
| 201 | virtual const char* onGetName() { return fName.c_str(); } |
sugoi@google.com | 77472f0 | 2013-03-05 18:50:01 +0000 | [diff] [blame] | 202 | virtual void onDraw(SkCanvas*) { |
reed@google.com | 57c4957 | 2011-10-31 14:33:35 +0000 | [diff] [blame] | 203 | SkPaint paint; |
| 204 | this->setupPaint(&paint); |
| 205 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame^] | 206 | for (int i = 0; i < this->getLoops(); ++i) { |
reed@google.com | 57c4957 | 2011-10-31 14:33:35 +0000 | [diff] [blame] | 207 | SkAAClip clip; |
| 208 | if (fDoPath) { |
| 209 | clip.setPath(fPath, &fRegion, fDoAA); |
| 210 | } else { |
| 211 | clip.setRect(fRect, fDoAA); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | private: |
| 216 | typedef SkBenchmark INHERITED; |
| 217 | }; |
| 218 | |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 219 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | a069c8f | 2011-11-28 19:54:56 +0000 | [diff] [blame] | 220 | class AAClipRegionBench : public SkBenchmark { |
| 221 | public: |
| 222 | AAClipRegionBench(void* param) : INHERITED(param) { |
| 223 | SkPath path; |
| 224 | // test conversion of a complex clip to a aaclip |
| 225 | path.addCircle(0, 0, SkIntToScalar(200)); |
| 226 | path.addCircle(0, 0, SkIntToScalar(180)); |
| 227 | // evenodd means we've constructed basically a stroked circle |
| 228 | path.setFillType(SkPath::kEvenOdd_FillType); |
| 229 | |
| 230 | SkIRect bounds; |
| 231 | path.getBounds().roundOut(&bounds); |
| 232 | fRegion.setPath(path, SkRegion(bounds)); |
| 233 | } |
| 234 | |
| 235 | protected: |
| 236 | virtual const char* onGetName() { return "aaclip_setregion"; } |
sugoi@google.com | 77472f0 | 2013-03-05 18:50:01 +0000 | [diff] [blame] | 237 | virtual void onDraw(SkCanvas*) { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame^] | 238 | for (int i = 0; i < this->getLoops(); ++i) { |
reed@google.com | a069c8f | 2011-11-28 19:54:56 +0000 | [diff] [blame] | 239 | SkAAClip clip; |
| 240 | clip.setRegion(fRegion); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | private: |
reed@google.com | a069c8f | 2011-11-28 19:54:56 +0000 | [diff] [blame] | 245 | SkRegion fRegion; |
| 246 | typedef SkBenchmark INHERITED; |
| 247 | }; |
| 248 | |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 249 | //////////////////////////////////////////////////////////////////////////////// |
reed@google.com | 57c4957 | 2011-10-31 14:33:35 +0000 | [diff] [blame] | 250 | |
| 251 | static SkBenchmark* Fact0(void* p) { return SkNEW_ARGS(AAClipBuilderBench, (p, false, false)); } |
| 252 | static SkBenchmark* Fact1(void* p) { return SkNEW_ARGS(AAClipBuilderBench, (p, false, true)); } |
| 253 | static SkBenchmark* Fact2(void* p) { return SkNEW_ARGS(AAClipBuilderBench, (p, true, false)); } |
| 254 | static SkBenchmark* Fact3(void* p) { return SkNEW_ARGS(AAClipBuilderBench, (p, true, true)); } |
| 255 | |
| 256 | static BenchRegistry gReg0(Fact0); |
| 257 | static BenchRegistry gReg1(Fact1); |
| 258 | static BenchRegistry gReg2(Fact2); |
| 259 | static BenchRegistry gReg3(Fact3); |
reed@google.com | a069c8f | 2011-11-28 19:54:56 +0000 | [diff] [blame] | 260 | |
| 261 | static SkBenchmark* Fact01(void* p) { return SkNEW_ARGS(AAClipRegionBench, (p)); } |
| 262 | static BenchRegistry gReg01(Fact01); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 263 | |
| 264 | static SkBenchmark* Fact000(void* p) { return SkNEW_ARGS(AAClipBench, (p, false, false)); } |
| 265 | static SkBenchmark* Fact001(void* p) { return SkNEW_ARGS(AAClipBench, (p, false, true)); } |
| 266 | static SkBenchmark* Fact002(void* p) { return SkNEW_ARGS(AAClipBench, (p, true, false)); } |
| 267 | static SkBenchmark* Fact003(void* p) { return SkNEW_ARGS(AAClipBench, (p, true, true)); } |
| 268 | |
| 269 | static BenchRegistry gReg000(Fact000); |
| 270 | static BenchRegistry gReg001(Fact001); |
| 271 | static BenchRegistry gReg002(Fact002); |
| 272 | static BenchRegistry gReg003(Fact003); |
| 273 | |
robertphillips@google.com | 6bb99e3 | 2012-05-09 15:48:31 +0000 | [diff] [blame] | 274 | static SkBenchmark* Fact004(void* p) { return SkNEW_ARGS(NestedAAClipBench, (p, false)); } |
| 275 | static SkBenchmark* Fact005(void* p) { return SkNEW_ARGS(NestedAAClipBench, (p, true)); } |
| 276 | |
| 277 | static BenchRegistry gReg004(Fact004); |
| 278 | static BenchRegistry gReg005(Fact005); |