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