blob: 57af3be262eec0e9a1ad14efd510ed5d47f366f1 [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"
15#include "src/core/SkClipOpPriv.h"
robertphillips@google.com6d62df42012-05-07 18:07:36 +000016
17////////////////////////////////////////////////////////////////////////////////
18// This bench tests out AA/BW clipping via canvas' clipPath and clipRect calls
tfarinaf168b862014-06-19 12:32:29 -070019class AAClipBench : public Benchmark {
robertphillips@google.com6d62df42012-05-07 18:07:36 +000020 SkString fName;
21 SkPath fClipPath;
22 SkRect fClipRect;
23 SkRect fDrawRect;
24 bool fDoPath;
25 bool fDoAA;
26
robertphillips@google.com6d62df42012-05-07 18:07:36 +000027public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000028 AAClipBench(bool doPath, bool doAA)
29 : fDoPath(doPath)
robertphillips@google.com6bb99e32012-05-09 15:48:31 +000030 , fDoAA(doAA) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +000031
32 fName.printf("aaclip_%s_%s",
33 doPath ? "path" : "rect",
34 doAA ? "AA" : "BW");
35
Mike Reed92b33352019-08-24 19:39:13 -040036 fClipRect.setLTRB(10.5f, 10.5f, 50.5f, 50.5f);
Mike Reed4241f5e2019-09-14 19:13:23 +000037 fClipPath.addRoundRect(fClipRect, SkIntToScalar(10), SkIntToScalar(10));
Mike Reed92b33352019-08-24 19:39:13 -040038 fDrawRect.setWH(100, 100);
robertphillips@google.com6d62df42012-05-07 18:07:36 +000039
40 SkASSERT(fClipPath.isConvex());
41 }
42
43protected:
44 virtual const char* onGetName() { return fName.c_str(); }
mtkleina1ebeb22015-10-01 09:43:39 -070045 virtual void onDraw(int loops, SkCanvas* canvas) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +000046
47 SkPaint paint;
48 this->setupPaint(&paint);
49
commit-bot@chromium.org33614712013-12-03 18:17:16 +000050 for (int i = 0; i < loops; ++i) {
robertphillips@google.com6d62df42012-05-07 18:07:36 +000051 // jostle the clip regions each time to prevent caching
52 fClipRect.offset((i % 2) == 0 ? SkIntToScalar(10) : SkIntToScalar(-10), 0);
53 fClipPath.reset();
Mike Reed4241f5e2019-09-14 19:13:23 +000054 fClipPath.addRoundRect(fClipRect,
55 SkIntToScalar(5), SkIntToScalar(5));
robertphillips@google.com6d62df42012-05-07 18:07:36 +000056 SkASSERT(fClipPath.isConvex());
57
58 canvas->save();
59#if 1
60 if (fDoPath) {
Mike Reedc1f77742016-12-09 09:00:50 -050061 canvas->clipPath(fClipPath, kReplace_SkClipOp, fDoAA);
robertphillips@google.com6d62df42012-05-07 18:07:36 +000062 } else {
Mike Reedc1f77742016-12-09 09:00:50 -050063 canvas->clipRect(fClipRect, kReplace_SkClipOp, fDoAA);
robertphillips@google.com6d62df42012-05-07 18:07:36 +000064 }
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 }
80private:
tfarinaf168b862014-06-19 12:32:29 -070081 typedef Benchmark INHERITED;
robertphillips@google.com6d62df42012-05-07 18:07:36 +000082};
83
84////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6bb99e32012-05-09 15:48:31 +000085// This bench tests out nested clip stacks. It is intended to simulate
86// how WebKit nests clips.
tfarinaf168b862014-06-19 12:32:29 -070087class NestedAAClipBench : public Benchmark {
robertphillips@google.com6bb99e32012-05-09 15:48:31 +000088 SkString fName;
89 bool fDoAA;
90 SkRect fDrawRect;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000091 SkRandom fRandom;
robertphillips@google.com6bb99e32012-05-09 15:48:31 +000092
robertphillips@google.comb1af07a2012-05-09 16:27:10 +000093 static const int kNestingDepth = 3;
robertphillips@google.com6bb99e32012-05-09 15:48:31 +000094 static const int kImageSize = 400;
95
96 SkPoint fSizes[kNestingDepth+1];
97
98public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000099 NestedAAClipBench(bool doAA) : fDoAA(doAA) {
robertphillips@google.com6bb99e32012-05-09 15:48:31 +0000100 fName.printf("nested_aaclip_%s", doAA ? "AA" : "BW");
101
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000102 fDrawRect = SkRect::MakeLTRB(0, 0,
103 SkIntToScalar(kImageSize),
robertphillips@google.com6bb99e32012-05-09 15:48:31 +0000104 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
113protected:
114 virtual const char* onGetName() { return fName.c_str(); }
115
116
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000117 void recurse(SkCanvas* canvas,
robertphillips@google.com6bb99e32012-05-09 15:48:31 +0000118 int depth,
119 const SkPoint& offset) {
120
121 canvas->save();
122
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000123 SkRect temp = SkRect::MakeLTRB(0, 0,
robertphillips@google.com6bb99e32012-05-09 15:48:31 +0000124 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
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000131 canvas->clipPath(path,
Mike Reedc1f77742016-12-09 09:00:50 -0500132 0 == depth ? kReplace_SkClipOp : kIntersect_SkClipOp,
robertphillips@google.com6bb99e32012-05-09 15:48:31 +0000133 fDoAA);
134
135 if (kNestingDepth == depth) {
136 // we only draw the draw rect at the lowest nesting level
137 SkPaint paint;
138 paint.setColor(0xff000000 | fRandom.nextU());
139 canvas->drawRect(fDrawRect, paint);
140 } else {
141 SkPoint childOffset = offset;
142 this->recurse(canvas, depth+1, childOffset);
143
144 childOffset += fSizes[depth+1];
145 this->recurse(canvas, depth+1, childOffset);
146
147 childOffset.fX = offset.fX + fSizes[depth+1].fX;
148 childOffset.fY = offset.fY;
149 this->recurse(canvas, depth+1, childOffset);
150
151 childOffset.fX = offset.fX;
152 childOffset.fY = offset.fY + fSizes[depth+1].fY;
153 this->recurse(canvas, depth+1, childOffset);
154 }
155
156 canvas->restore();
157 }
158
mtkleina1ebeb22015-10-01 09:43:39 -0700159 virtual void onDraw(int loops, SkCanvas* canvas) {
robertphillips@google.com6bb99e32012-05-09 15:48:31 +0000160
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000161 for (int i = 0; i < loops; ++i) {
robertphillips@google.com6bb99e32012-05-09 15:48:31 +0000162 SkPoint offset = SkPoint::Make(0, 0);
163 this->recurse(canvas, 0, offset);
164 }
165 }
166
167private:
tfarinaf168b862014-06-19 12:32:29 -0700168 typedef Benchmark INHERITED;
robertphillips@google.com6bb99e32012-05-09 15:48:31 +0000169};
170
171////////////////////////////////////////////////////////////////////////////////
tfarinaf168b862014-06-19 12:32:29 -0700172class AAClipBuilderBench : public Benchmark {
reed@google.com57c49572011-10-31 14:33:35 +0000173 SkString fName;
174 SkPath fPath;
175 SkRect fRect;
176 SkRegion fRegion;
177 bool fDoPath;
178 bool fDoAA;
179
reed@google.com57c49572011-10-31 14:33:35 +0000180public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000181 AAClipBuilderBench(bool doPath, bool doAA) {
reed@google.com57c49572011-10-31 14:33:35 +0000182 fDoPath = doPath;
183 fDoAA = doAA;
184
185 fName.printf("aaclip_build_%s_%s", doPath ? "path" : "rect",
186 doAA ? "AA" : "BW");
187
Mike Reed92b33352019-08-24 19:39:13 -0400188 fRegion.setRect({0, 0, 640, 480});
reed@google.com57c49572011-10-31 14:33:35 +0000189 fRect.set(fRegion.getBounds());
190 fRect.inset(SK_Scalar1/4, SK_Scalar1/4);
191 fPath.addRoundRect(fRect, SkIntToScalar(20), SkIntToScalar(20));
192 }
193
194protected:
195 virtual const char* onGetName() { return fName.c_str(); }
mtkleina1ebeb22015-10-01 09:43:39 -0700196 virtual void onDraw(int loops, SkCanvas*) {
reed@google.com57c49572011-10-31 14:33:35 +0000197 SkPaint paint;
198 this->setupPaint(&paint);
199
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000200 for (int i = 0; i < loops; ++i) {
reed@google.com57c49572011-10-31 14:33:35 +0000201 SkAAClip clip;
202 if (fDoPath) {
203 clip.setPath(fPath, &fRegion, fDoAA);
204 } else {
205 clip.setRect(fRect, fDoAA);
206 }
207 }
208 }
209private:
tfarinaf168b862014-06-19 12:32:29 -0700210 typedef Benchmark INHERITED;
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:
230 virtual const char* onGetName() { return "aaclip_setregion"; }
mtkleina1ebeb22015-10-01 09:43:39 -0700231 virtual void onDraw(int loops, SkCanvas*) {
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;
tfarinaf168b862014-06-19 12:32:29 -0700240 typedef Benchmark INHERITED;
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);)