blob: 394fdb49cc5a5ba9679b45872e43e41faeb34309 [file] [log] [blame]
reed@google.com57c49572011-10-31 14:33:35 +00001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#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.com6d62df42012-05-07 18:07:36 +000015
16////////////////////////////////////////////////////////////////////////////////
17// This bench tests out AA/BW clipping via canvas' clipPath and clipRect calls
tfarinaf168b862014-06-19 12:32:29 -070018class AAClipBench : public Benchmark {
robertphillips@google.com6d62df42012-05-07 18:07:36 +000019 SkString fName;
20 SkPath fClipPath;
21 SkRect fClipRect;
22 SkRect fDrawRect;
23 bool fDoPath;
24 bool fDoAA;
25
robertphillips@google.com6d62df42012-05-07 18:07:36 +000026public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000027 AAClipBench(bool doPath, bool doAA)
28 : fDoPath(doPath)
robertphillips@google.com6bb99e32012-05-09 15:48:31 +000029 , fDoAA(doAA) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +000030
31 fName.printf("aaclip_%s_%s",
32 doPath ? "path" : "rect",
33 doAA ? "AA" : "BW");
34
Mike Reed92b33352019-08-24 19:39:13 -040035 fClipRect.setLTRB(10.5f, 10.5f, 50.5f, 50.5f);
Mike Reed4241f5e2019-09-14 19:13:23 +000036 fClipPath.addRoundRect(fClipRect, SkIntToScalar(10), SkIntToScalar(10));
Mike Reed92b33352019-08-24 19:39:13 -040037 fDrawRect.setWH(100, 100);
robertphillips@google.com6d62df42012-05-07 18:07:36 +000038
39 SkASSERT(fClipPath.isConvex());
40 }
41
42protected:
Brian Salomond0072812020-07-21 17:03:56 -040043 const char* onGetName() override { return fName.c_str(); }
44 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips@google.com6d62df42012-05-07 18:07:36 +000045
46 SkPaint paint;
47 this->setupPaint(&paint);
48
commit-bot@chromium.org33614712013-12-03 18:17:16 +000049 for (int i = 0; i < loops; ++i) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +000050 // jostle the clip regions each time to prevent caching
51 fClipRect.offset((i % 2) == 0 ? SkIntToScalar(10) : SkIntToScalar(-10), 0);
52 fClipPath.reset();
Mike Reed4241f5e2019-09-14 19:13:23 +000053 fClipPath.addRoundRect(fClipRect,
54 SkIntToScalar(5), SkIntToScalar(5));
robertphillips@google.com6d62df42012-05-07 18:07:36 +000055 SkASSERT(fClipPath.isConvex());
56
57 canvas->save();
58#if 1
59 if (fDoPath) {
Michael Ludwige4b79692020-09-16 13:55:05 -040060 canvas->clipPath(fClipPath, SkClipOp::kIntersect, fDoAA);
robertphillips@google.com6d62df42012-05-07 18:07:36 +000061 } else {
Michael Ludwige4b79692020-09-16 13:55:05 -040062 canvas->clipRect(fClipRect, SkClipOp::kIntersect, fDoAA);
robertphillips@google.com6d62df42012-05-07 18:07:36 +000063 }
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 }
79private:
John Stiles7571f9e2020-09-02 22:42:33 -040080 using INHERITED = Benchmark;
robertphillips@google.com6d62df42012-05-07 18:07:36 +000081};
82
83////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6bb99e32012-05-09 15:48:31 +000084// This bench tests out nested clip stacks. It is intended to simulate
85// how WebKit nests clips.
tfarinaf168b862014-06-19 12:32:29 -070086class NestedAAClipBench : public Benchmark {
robertphillips@google.com6bb99e32012-05-09 15:48:31 +000087 SkString fName;
88 bool fDoAA;
89 SkRect fDrawRect;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000090 SkRandom fRandom;
robertphillips@google.com6bb99e32012-05-09 15:48:31 +000091
robertphillips@google.comb1af07a2012-05-09 16:27:10 +000092 static const int kNestingDepth = 3;
robertphillips@google.com6bb99e32012-05-09 15:48:31 +000093 static const int kImageSize = 400;
94
95 SkPoint fSizes[kNestingDepth+1];
96
97public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000098 NestedAAClipBench(bool doAA) : fDoAA(doAA) {
robertphillips@google.com6bb99e32012-05-09 15:48:31 +000099 fName.printf("nested_aaclip_%s", doAA ? "AA" : "BW");
100
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000101 fDrawRect = SkRect::MakeLTRB(0, 0,
102 SkIntToScalar(kImageSize),
robertphillips@google.com6bb99e32012-05-09 15:48:31 +0000103 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
112protected:
Brian Salomond0072812020-07-21 17:03:56 -0400113 const char* onGetName() override { return fName.c_str(); }
robertphillips@google.com6bb99e32012-05-09 15:48:31 +0000114
115
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000116 void recurse(SkCanvas* canvas,
robertphillips@google.com6bb99e32012-05-09 15:48:31 +0000117 int depth,
118 const SkPoint& offset) {
119
120 canvas->save();
121
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000122 SkRect temp = SkRect::MakeLTRB(0, 0,
robertphillips@google.com6bb99e32012-05-09 15:48:31 +0000123 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 Ludwige4b79692020-09-16 13:55:05 -0400130 canvas->clipPath(path, SkClipOp::kIntersect, fDoAA);
robertphillips@google.com6bb99e32012-05-09 15:48:31 +0000131
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 Salomond0072812020-07-21 17:03:56 -0400156 void onDraw(int loops, SkCanvas* canvas) override {
robertphillips@google.com6bb99e32012-05-09 15:48:31 +0000157
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000158 for (int i = 0; i < loops; ++i) {
robertphillips@google.com6bb99e32012-05-09 15:48:31 +0000159 SkPoint offset = SkPoint::Make(0, 0);
160 this->recurse(canvas, 0, offset);
161 }
162 }
163
164private:
John Stiles7571f9e2020-09-02 22:42:33 -0400165 using INHERITED = Benchmark;
robertphillips@google.com6bb99e32012-05-09 15:48:31 +0000166};
167
168////////////////////////////////////////////////////////////////////////////////
tfarinaf168b862014-06-19 12:32:29 -0700169class AAClipBuilderBench : public Benchmark {
reed@google.com57c49572011-10-31 14:33:35 +0000170 SkString fName;
171 SkPath fPath;
172 SkRect fRect;
Michael Ludwig8c9c1852021-09-14 13:16:42 -0400173 SkIRect fBounds;
reed@google.com57c49572011-10-31 14:33:35 +0000174 bool fDoPath;
175 bool fDoAA;
176
reed@google.com57c49572011-10-31 14:33:35 +0000177public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000178 AAClipBuilderBench(bool doPath, bool doAA) {
reed@google.com57c49572011-10-31 14:33:35 +0000179 fDoPath = doPath;
180 fDoAA = doAA;
181
182 fName.printf("aaclip_build_%s_%s", doPath ? "path" : "rect",
183 doAA ? "AA" : "BW");
Michael Ludwig8c9c1852021-09-14 13:16:42 -0400184 fBounds = {0, 0, 640, 480};
185 fRect.set(fBounds);
reed@google.com57c49572011-10-31 14:33:35 +0000186 fRect.inset(SK_Scalar1/4, SK_Scalar1/4);
187 fPath.addRoundRect(fRect, SkIntToScalar(20), SkIntToScalar(20));
188 }
189
190protected:
Brian Salomond0072812020-07-21 17:03:56 -0400191 const char* onGetName() override { return fName.c_str(); }
192 void onDraw(int loops, SkCanvas*) override {
reed@google.com57c49572011-10-31 14:33:35 +0000193 SkPaint paint;
194 this->setupPaint(&paint);
195
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000196 for (int i = 0; i < loops; ++i) {
reed@google.com57c49572011-10-31 14:33:35 +0000197 SkAAClip clip;
198 if (fDoPath) {
Michael Ludwig8c9c1852021-09-14 13:16:42 -0400199 clip.setPath(fPath, fBounds, fDoAA);
reed@google.com57c49572011-10-31 14:33:35 +0000200 } else {
Michael Ludwig8c9c1852021-09-14 13:16:42 -0400201 if (fDoAA) {
202 clip.setPath(SkPath::Rect(fRect), fBounds, fDoAA);
203 } else {
204 clip.setRect(fBounds);
205 }
reed@google.com57c49572011-10-31 14:33:35 +0000206 }
207 }
208 }
209private:
John Stiles7571f9e2020-09-02 22:42:33 -0400210 using INHERITED = Benchmark;
reed@google.com57c49572011-10-31 14:33:35 +0000211};
212
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000213////////////////////////////////////////////////////////////////////////////////
tfarinaf168b862014-06-19 12:32:29 -0700214class AAClipRegionBench : public Benchmark {
reed@google.coma069c8f2011-11-28 19:54:56 +0000215public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000216 AAClipRegionBench() {
reed@google.coma069c8f2011-11-28 19:54:56 +0000217 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 Reed7d34dc72019-11-26 12:17:17 -0500222 path.setFillType(SkPathFillType::kEvenOdd);
reed@google.coma069c8f2011-11-28 19:54:56 +0000223
224 SkIRect bounds;
225 path.getBounds().roundOut(&bounds);
226 fRegion.setPath(path, SkRegion(bounds));
227 }
228
229protected:
Brian Salomond0072812020-07-21 17:03:56 -0400230 const char* onGetName() override { return "aaclip_setregion"; }
231 void onDraw(int loops, SkCanvas*) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000232 for (int i = 0; i < loops; ++i) {
reed@google.coma069c8f2011-11-28 19:54:56 +0000233 SkAAClip clip;
234 clip.setRegion(fRegion);
235 }
236 }
237
238private:
reed@google.coma069c8f2011-11-28 19:54:56 +0000239 SkRegion fRegion;
John Stiles7571f9e2020-09-02 22:42:33 -0400240 using INHERITED = Benchmark;
reed@google.coma069c8f2011-11-28 19:54:56 +0000241};
242
robertphillips@google.com6d62df42012-05-07 18:07:36 +0000243////////////////////////////////////////////////////////////////////////////////
reed@google.com57c49572011-10-31 14:33:35 +0000244
halcanary385fe4d2015-08-26 13:07:48 -0700245DEF_BENCH(return new AAClipBuilderBench(false, false);)
246DEF_BENCH(return new AAClipBuilderBench(false, true);)
247DEF_BENCH(return new AAClipBuilderBench(true, false);)
248DEF_BENCH(return new AAClipBuilderBench(true, true);)
249DEF_BENCH(return new AAClipRegionBench();)
250DEF_BENCH(return new AAClipBench(false, false);)
251DEF_BENCH(return new AAClipBench(false, true);)
252DEF_BENCH(return new AAClipBench(true, false);)
253DEF_BENCH(return new AAClipBench(true, true);)
254DEF_BENCH(return new NestedAAClipBench(false);)
255DEF_BENCH(return new NestedAAClipBench(true);)