Flutter Engine
The Flutter Engine
AAClipBench.cpp
Go to the documentation of this file.
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"
10#include "include/core/SkPath.h"
13#include "src/base/SkRandom.h"
14#include "src/core/SkAAClip.h"
15
16////////////////////////////////////////////////////////////////////////////////
17// This bench tests out AA/BW clipping via canvas' clipPath and clipRect calls
18class AAClipBench : public Benchmark {
19 SkString fName;
20 SkPath fClipPath;
21 SkRect fClipRect;
22 SkRect fDrawRect;
23 bool fDoPath;
24 bool fDoAA;
25
26public:
27 AAClipBench(bool doPath, bool doAA)
28 : fDoPath(doPath)
29 , fDoAA(doAA) {
30
31 fName.printf("aaclip_%s_%s",
32 doPath ? "path" : "rect",
33 doAA ? "AA" : "BW");
34
35 fClipRect.setLTRB(10.5f, 10.5f, 50.5f, 50.5f);
36 fClipPath.addRoundRect(fClipRect, SkIntToScalar(10), SkIntToScalar(10));
37 fDrawRect.setWH(100, 100);
38
39 SkASSERT(fClipPath.isConvex());
40 }
41
42protected:
43 const char* onGetName() override { return fName.c_str(); }
44 void onDraw(int loops, SkCanvas* canvas) override {
45
47 this->setupPaint(&paint);
48
49 for (int i = 0; i < loops; ++i) {
50 // jostle the clip regions each time to prevent caching
51 fClipRect.offset((i % 2) == 0 ? SkIntToScalar(10) : SkIntToScalar(-10), 0);
52 fClipPath.reset();
53 fClipPath.addRoundRect(fClipRect,
55 SkASSERT(fClipPath.isConvex());
56
57 canvas->save();
58#if 1
59 if (fDoPath) {
60 canvas->clipPath(fClipPath, SkClipOp::kIntersect, fDoAA);
61 } else {
62 canvas->clipRect(fClipRect, SkClipOp::kIntersect, fDoAA);
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 }
79private:
80 using INHERITED = Benchmark;
81};
82
83////////////////////////////////////////////////////////////////////////////////
84// This bench tests out nested clip stacks. It is intended to simulate
85// how WebKit nests clips.
87 SkString fName;
88 bool fDoAA;
89 SkRect fDrawRect;
90 SkRandom fRandom;
91
92 static const int kNestingDepth = 3;
93 static const int kImageSize = 400;
94
95 SkPoint fSizes[kNestingDepth+1];
96
97public:
98 NestedAAClipBench(bool doAA) : fDoAA(doAA) {
99 fName.printf("nested_aaclip_%s", doAA ? "AA" : "BW");
100
101 fDrawRect = SkRect::MakeLTRB(0, 0,
102 SkIntToScalar(kImageSize),
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
112protected:
113 const char* onGetName() override { return fName.c_str(); }
114
115
116 void recurse(SkCanvas* canvas,
117 int depth,
118 const SkPoint& offset) {
119
120 canvas->save();
121
122 SkRect temp = SkRect::MakeLTRB(0, 0,
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
130 canvas->clipPath(path, SkClipOp::kIntersect, fDoAA);
131
132 if (kNestingDepth == depth) {
133 // we only draw the draw rect at the lowest nesting level
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
156 void onDraw(int loops, SkCanvas* canvas) override {
157
158 for (int i = 0; i < loops; ++i) {
160 this->recurse(canvas, 0, offset);
161 }
162 }
163
164private:
165 using INHERITED = Benchmark;
166};
167
168////////////////////////////////////////////////////////////////////////////////
170 SkString fName;
171 SkPath fPath;
172 SkRect fRect;
173 SkIRect fBounds;
174 bool fDoPath;
175 bool fDoAA;
176
177public:
178 AAClipBuilderBench(bool doPath, bool doAA) {
179 fDoPath = doPath;
180 fDoAA = doAA;
181
182 fName.printf("aaclip_build_%s_%s", doPath ? "path" : "rect",
183 doAA ? "AA" : "BW");
184 fBounds = {0, 0, 640, 480};
188 }
189
190protected:
191 const char* onGetName() override { return fName.c_str(); }
192 void onDraw(int loops, SkCanvas*) override {
194 this->setupPaint(&paint);
195
196 for (int i = 0; i < loops; ++i) {
198 if (fDoPath) {
199 clip.setPath(fPath, fBounds, fDoAA);
200 } else {
201 if (fDoAA) {
202 clip.setPath(SkPath::Rect(fRect), fBounds, fDoAA);
203 } else {
204 clip.setRect(fBounds);
205 }
206 }
207 }
208 }
209private:
210 using INHERITED = Benchmark;
211};
212
213////////////////////////////////////////////////////////////////////////////////
215public:
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
222 path.setFillType(SkPathFillType::kEvenOdd);
223
225 path.getBounds().roundOut(&bounds);
226 fRegion.setPath(path, SkRegion(bounds));
227 }
228
229protected:
230 const char* onGetName() override { return "aaclip_setregion"; }
231 void onDraw(int loops, SkCanvas*) override {
232 for (int i = 0; i < loops; ++i) {
234 clip.setRegion(fRegion);
235 }
236 }
237
238private:
239 SkRegion fRegion;
240 using INHERITED = Benchmark;
241};
242
243////////////////////////////////////////////////////////////////////////////////
244
245DEF_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);)
SkPath fPath
#define DEF_BENCH(code)
Definition: Benchmark.h:20
SkRect fRect
Definition: FillRRectOp.cpp:73
const char * fName
const SkRect fBounds
#define SkASSERT(cond)
Definition: SkAssert.h:116
static SkPath clip(const SkPath &path, const SkHalfPlane &plane)
Definition: SkPath.cpp:3892
#define SK_Scalar1
Definition: SkScalar.h:18
#define SkIntToScalar(x)
Definition: SkScalar.h:57
void onDraw(int loops, SkCanvas *canvas) override
Definition: AAClipBench.cpp:44
const char * onGetName() override
Definition: AAClipBench.cpp:43
AAClipBench(bool doPath, bool doAA)
Definition: AAClipBench.cpp:27
AAClipBuilderBench(bool doPath, bool doAA)
void onDraw(int loops, SkCanvas *) override
const char * onGetName() override
void onDraw(int loops, SkCanvas *) override
const char * onGetName() override
virtual void setupPaint(SkPaint *paint)
Definition: Benchmark.cpp:55
NestedAAClipBench(bool doAA)
Definition: AAClipBench.cpp:98
void onDraw(int loops, SkCanvas *canvas) override
void recurse(SkCanvas *canvas, int depth, const SkPoint &offset)
const char * onGetName() override
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
Definition: SkCanvas.cpp:1361
void restore()
Definition: SkCanvas.cpp:461
void clipPath(const SkPath &path, SkClipOp op, bool doAntiAlias)
Definition: SkCanvas.cpp:1456
int save()
Definition: SkCanvas.cpp:447
void drawPath(const SkPath &path, const SkPaint &paint)
Definition: SkCanvas.cpp:1747
Definition: SkPath.h:59
static SkPath Rect(const SkRect &, SkPathDirection=SkPathDirection::kCW, unsigned startIndex=0)
Definition: SkPath.cpp:3586
SkPath & reset()
Definition: SkPath.cpp:370
SkPath & addRoundRect(const SkRect &rect, SkScalar rx, SkScalar ry, SkPathDirection dir=SkPathDirection::kCW)
Definition: SkPath.cpp:1093
bool isConvex() const
Definition: SkPath.cpp:426
uint32_t nextU()
Definition: SkRandom.h:42
bool setPath(const SkPath &path, const SkRegion &clip)
const Paint & paint
Definition: color_source.cc:38
Optional< SkRect > bounds
Definition: SkRecords.h:189
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
SeparatedVector2 offset
Definition: SkRect.h:32
float fX
x-axis value
Definition: SkPoint_impl.h:164
static constexpr SkPoint Make(float x, float y)
Definition: SkPoint_impl.h:173
void set(float x, float y)
Definition: SkPoint_impl.h:200
float fY
y-axis value
Definition: SkPoint_impl.h:165
void inset(float dx, float dy)
Definition: SkRect.h:1060
void setWH(float width, float height)
Definition: SkRect.h:944
void offset(float dx, float dy)
Definition: SkRect.h:1016
void setLTRB(float left, float top, float right, float bottom)
Definition: SkRect.h:865
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition: SkRect.h:646
void set(const SkIRect &src)
Definition: SkRect.h:849