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