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