Debian Codegraph @

bench/AAClipBench.cpp

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