Flutter Engine
The Flutter Engine
RectBench.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"
15#include "src/base/SkRandom.h"
17
18static DEFINE_double(strokeWidth, -1.0, "If set, use this stroke width in RectBench.");
19
20class RectBench : public Benchmark {
21public:
23 enum {
24 W = 640,
25 H = 480,
26 N = 300,
27 };
30 bool fAA;
32
33 RectBench(int shift, int stroke = 0, bool aa = true, bool perspective = false)
34 : fShift(shift)
36 , fAA(aa)
37 , fPerspective(perspective) {}
38
39 const char* computeName(const char root[]) {
40 fBaseName.printf("%s_%d", root, fShift);
41 if (fStroke > 0) {
42 fBaseName.appendf("_stroke_%d", fStroke);
43 }
44 if (fAA) {
45 fBaseName.appendf("_aa");
46 } else {
47 fBaseName.appendf("_bw");
48 }
49 if (fPerspective) {
50 fBaseName.appendf("_persp");
51 }
52 return fBaseName.c_str();
53 }
54
55protected:
56
57 virtual void drawThisRect(SkCanvas* c, const SkRect& r, const SkPaint& p) {
58 c->drawRect(r, p);
59 }
60
61 const char* onGetName() override { return computeName("rects"); }
62
63 void onDelayedSetup() override {
64 SkRandom rand;
65 const SkScalar offset = SK_Scalar1/3;
66 for (int i = 0; i < N; i++) {
67 int x = rand.nextU() % W;
68 int y = rand.nextU() % H;
69 int w = rand.nextU() % W;
70 int h = rand.nextU() % H;
71 w >>= fShift;
72 h >>= fShift;
73 x -= w/2;
74 y -= h/2;
78 fColors[i] = rand.nextU() | 0xFF808080;
79 }
80 }
81
82 void onDraw(int loops, SkCanvas* canvas) override {
84 if (fStroke > 0) {
86 paint.setStrokeWidth(SkIntToScalar(fStroke));
87 }
88 if (fPerspective) {
89 // Apply some fixed perspective to change how ops may draw the rects
90 SkMatrix perspective;
91 perspective.setIdentity();
92 perspective.setPerspX(1e-4f);
93 perspective.setPerspY(1e-3f);
94 perspective.setSkewX(0.1f);
95 canvas->concat(perspective);
96 }
97 for (int i = 0; i < loops; i++) {
98 paint.setColor(fColors[i % N]);
99 this->setupPaint(&paint);
100 this->drawThisRect(canvas, fRects[i % N], paint);
101 }
102 }
103
104 void setupPaint(SkPaint* paint) override {
105 this->INHERITED::setupPaint(paint);
106 paint->setAntiAlias(fAA);
107 }
108
109private:
110 SkString fBaseName;
111 using INHERITED = Benchmark;
112};
113
115public:
117 fMode = SkBlendMode::kSrc;
118 }
119
120protected:
121 void setupPaint(SkPaint* paint) override {
122 this->INHERITED::setupPaint(paint);
123 // srcmode is most interesting when we're not opaque
124 paint->setAlpha(0x80);
125 paint->setBlendMode(fMode);
126 }
127
128 const char* onGetName() override {
129 fName.set(this->INHERITED::onGetName());
130 fName.prepend("srcmode_");
131 return fName.c_str();
132 }
133
134private:
135 SkBlendMode fMode;
136 SkString fName;
137
138 using INHERITED = RectBench;
139};
140
142public:
144
145protected:
146 void setupPaint(SkPaint* paint) override {
147 this->INHERITED::setupPaint(paint);
148 // draw non opaque rect
149 paint->setAlpha(0x80);
150 }
151
152 const char* onGetName() override {
153 fName.set(this->INHERITED::onGetName());
154 fName.prepend("transparent_");
155 return fName.c_str();
156 }
157
158private:
159 SkString fName;
160 using INHERITED = RectBench;
161};
162
163// Adds a shader to the paint that requires local coordinates to be used
165public:
166 LocalCoordsRectBench(bool aa, bool perspective = false) : INHERITED(1, 0, aa, perspective) { }
167
168protected:
169 void onDelayedSetup() override {
171 // Create the shader once, so that isn't included in the timing
172 SkPoint pts[2] = { {0.f, 0.f}, {50.f, 50.f} };
174 fShader = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
175 }
176
177 void setupPaint(SkPaint* paint) override {
178 this->INHERITED::setupPaint(paint);
179 paint->setShader(fShader);
180 }
181
182 const char* onGetName() override {
183 fName.set(this->INHERITED::onGetName());
184 fName.append("_localcoords");
185 return fName.c_str();
186 }
187
188private:
189 SkString fName;
190 sk_sp<SkShader> fShader;
191
192 using INHERITED = RectBench;
193};
194
195
196class OvalBench : public RectBench {
197public:
198 OvalBench(int shift, int stroke = 0) : RectBench(shift, stroke) {}
199protected:
200 void drawThisRect(SkCanvas* c, const SkRect& r, const SkPaint& p) override {
201 c->drawOval(r, p);
202 }
203 const char* onGetName() override { return computeName("ovals"); }
204};
205
206class RRectBench : public RectBench {
207public:
208 RRectBench(int shift, int stroke = 0) : RectBench(shift, stroke) {}
209protected:
210 void drawThisRect(SkCanvas* c, const SkRect& r, const SkPaint& p) override {
211 c->drawRoundRect(r, r.width() / 4, r.height() / 4, p);
212 }
213 const char* onGetName() override { return computeName("rrects"); }
214};
215
216class PointsBench : public RectBench {
217public:
219
221 : RectBench(2)
222 , fMode(mode) {
223 fName = name;
224 }
225
226protected:
227 void onDraw(int loops, SkCanvas* canvas) override {
228 SkScalar gSizes[] = {
229 SkIntToScalar(7), 0
230 };
231 size_t sizes = std::size(gSizes);
232
233 if (FLAGS_strokeWidth >= 0) {
234 gSizes[0] = (SkScalar)FLAGS_strokeWidth;
235 sizes = 1;
236 }
237
239 paint.setStrokeCap(SkPaint::kRound_Cap);
240
241 for (int loop = 0; loop < loops; loop++) {
242 for (size_t i = 0; i < sizes; i++) {
243 paint.setStrokeWidth(gSizes[i]);
244 this->setupPaint(&paint);
245 canvas->drawPoints(fMode, N * 2, reinterpret_cast<SkPoint*>(fRects), paint);
246 paint.setColor(fColors[i % N]);
247 }
248 }
249 }
250 const char* onGetName() override { return fName.c_str(); }
251
252private:
253 SkString fName;
254
255};
256
257/*******************************************************************************
258 * to bench BlitMask [Opaque, Black, color, shader]
259 *******************************************************************************/
260
261class BlitMaskBench : public RectBench {
262public:
268 };
270
272 BlitMaskBench::kMaskType type, const char* name) :
273 RectBench(2), fMode(mode), _type(type) {
274 fName = name;
275 }
276
277protected:
278 void onDraw(int loops, SkCanvas* canvas) override {
279 SkScalar gSizes[] = {
281 };
282 size_t sizes = std::size(gSizes);
283
284 if (FLAGS_strokeWidth >= 0) {
285 gSizes[0] = (SkScalar)FLAGS_strokeWidth;
286 sizes = 1;
287 }
288 SkRandom rand;
289 SkColor color = 0xFF000000;
290 U8CPU alpha = 0xFF;
292 paint.setStrokeCap(SkPaint::kRound_Cap);
293 if (_type == KMaskShader) {
294 SkBitmap srcBM;
295 srcBM.allocN32Pixels(10, 1);
296 srcBM.eraseColor(0xFF00FF00);
297
298 paint.setShader(srcBM.makeShader(SkSamplingOptions()));
299 }
300 for (int loop = 0; loop < loops; loop++) {
301 for (size_t i = 0; i < sizes; i++) {
302 switch (_type) {
303 case kMaskOpaque:
304 color = fColors[i];
305 alpha = 0xFF;
306 break;
307 case kMaskBlack:
308 alpha = 0xFF;
309 color = 0xFF000000;
310 break;
311 case kMaskColor:
312 color = fColors[i];
313 alpha = rand.nextU() & 255;
314 break;
315 case KMaskShader:
316 break;
317 }
318 paint.setStrokeWidth(gSizes[i]);
319 this->setupPaint(&paint);
320 paint.setColor(color);
321 paint.setAlpha(alpha);
322 canvas->drawPoints(fMode, N * 2, reinterpret_cast<SkPoint*>(fRects), paint);
323 }
324 }
325 }
326 const char* onGetName() override { return fName.c_str(); }
327
328private:
329 using INHERITED = RectBench;
330 kMaskType _type;
331 SkString fName;
332};
333
334// AA rects
335DEF_BENCH(return new RectBench(1, 0, true);)
336DEF_BENCH(return new RectBench(1, 4, true);)
337DEF_BENCH(return new RectBench(3, 0, true);)
338DEF_BENCH(return new RectBench(3, 4, true);)
339// Non-AA rects
340DEF_BENCH(return new RectBench(1, 0, false);)
341DEF_BENCH(return new RectBench(1, 4, false);)
342DEF_BENCH(return new RectBench(3, 0, false);)
343DEF_BENCH(return new RectBench(3, 4, false);)
344
345DEF_BENCH(return new OvalBench(1);)
346DEF_BENCH(return new OvalBench(3);)
347DEF_BENCH(return new OvalBench(1, 4);)
348DEF_BENCH(return new OvalBench(3, 4);)
349DEF_BENCH(return new RRectBench(1);)
350DEF_BENCH(return new RRectBench(1, 4);)
351DEF_BENCH(return new RRectBench(3);)
352DEF_BENCH(return new RRectBench(3, 4);)
356
357DEF_BENCH(return new SrcModeRectBench();)
358
359DEF_BENCH(return new TransparentRectBench();)
360
361DEF_BENCH(return new LocalCoordsRectBench(true);)
362DEF_BENCH(return new LocalCoordsRectBench(false);)
363
364// Perspective rects
365DEF_BENCH(return new RectBench(1, 0, true, true);)
366DEF_BENCH(return new RectBench(1, 0, false, true);)
367DEF_BENCH(return new LocalCoordsRectBench(true, true);)
368DEF_BENCH(return new LocalCoordsRectBench(false, true);)
369
370/* init the blitmask bench
371 */
374 "maskopaque");)
377 "maskblack");)
380 "maskcolor");)
383 "maskshader");)
#define DEF_BENCH(code)
Definition: Benchmark.h:20
static const int strokeWidth
Definition: BlurTest.cpp:60
static DEFINE_double(strokeWidth, -1.0, "If set, use this stroke width in RectBench.")
SkBlendMode
Definition: SkBlendMode.h:38
unsigned U8CPU
Definition: SkCPUTypes.h:18
uint32_t SkColor
Definition: SkColor.h:37
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
#define SK_Scalar1
Definition: SkScalar.h:18
#define SkIntToScalar(x)
Definition: SkScalar.h:57
GLenum type
virtual void setupPaint(SkPaint *paint)
Definition: Benchmark.cpp:55
BlitMaskBench(SkCanvas::PointMode mode, BlitMaskBench::kMaskType type, const char *name)
Definition: RectBench.cpp:271
void onDraw(int loops, SkCanvas *canvas) override
Definition: RectBench.cpp:278
const char * onGetName() override
Definition: RectBench.cpp:326
SkCanvas::PointMode fMode
Definition: RectBench.cpp:269
LocalCoordsRectBench(bool aa, bool perspective=false)
Definition: RectBench.cpp:166
const char * onGetName() override
Definition: RectBench.cpp:182
void onDelayedSetup() override
Definition: RectBench.cpp:169
void setupPaint(SkPaint *paint) override
Definition: RectBench.cpp:177
void drawThisRect(SkCanvas *c, const SkRect &r, const SkPaint &p) override
Definition: RectBench.cpp:200
const char * onGetName() override
Definition: RectBench.cpp:203
OvalBench(int shift, int stroke=0)
Definition: RectBench.cpp:198
const char * onGetName() override
Definition: RectBench.cpp:250
PointsBench(SkCanvas::PointMode mode, const char *name)
Definition: RectBench.cpp:220
SkCanvas::PointMode fMode
Definition: RectBench.cpp:218
void onDraw(int loops, SkCanvas *canvas) override
Definition: RectBench.cpp:227
void drawThisRect(SkCanvas *c, const SkRect &r, const SkPaint &p) override
Definition: RectBench.cpp:210
RRectBench(int shift, int stroke=0)
Definition: RectBench.cpp:208
const char * onGetName() override
Definition: RectBench.cpp:213
const char * onGetName() override
Definition: RectBench.cpp:61
RectBench(int shift, int stroke=0, bool aa=true, bool perspective=false)
Definition: RectBench.cpp:33
void onDraw(int loops, SkCanvas *canvas) override
Definition: RectBench.cpp:82
int fShift
Definition: RectBench.cpp:22
virtual void drawThisRect(SkCanvas *c, const SkRect &r, const SkPaint &p)
Definition: RectBench.cpp:57
int fStroke
Definition: RectBench.cpp:22
bool fPerspective
Definition: RectBench.cpp:31
const char * computeName(const char root[])
Definition: RectBench.cpp:39
void onDelayedSetup() override
Definition: RectBench.cpp:63
SkColor fColors[N]
Definition: RectBench.cpp:29
bool fAA
Definition: RectBench.cpp:30
SkRect fRects[N]
Definition: RectBench.cpp:28
void setupPaint(SkPaint *paint) override
Definition: RectBench.cpp:104
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition: SkBitmap.cpp:669
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition: SkBitmap.cpp:232
void eraseColor(SkColor4f) const
Definition: SkBitmap.cpp:442
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void drawOval(const SkRect &oval, const SkPaint &paint)
Definition: SkCanvas.cpp:1698
void drawPoints(PointMode mode, size_t count, const SkPoint pts[], const SkPaint &paint)
Definition: SkCanvas.cpp:1710
void drawRoundRect(const SkRect &rect, SkScalar rx, SkScalar ry, const SkPaint &paint)
Definition: SkCanvas.cpp:2717
void concat(const SkMatrix &matrix)
Definition: SkCanvas.cpp:1318
@ kLines_PointMode
draw each pair of points as a line segment
Definition: SkCanvas.h:1242
@ kPolygon_PointMode
draw the array of points as a open polygon
Definition: SkCanvas.h:1243
@ kPoints_PointMode
draw each point separately
Definition: SkCanvas.h:1241
static sk_sp< SkShader > MakeLinear(const SkPoint pts[2], const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
SkMatrix & setSkewX(SkScalar v)
Definition: SkMatrix.h:518
SkMatrix & setPerspX(SkScalar v)
Definition: SkMatrix.h:537
SkMatrix & setPerspY(SkScalar v)
Definition: SkMatrix.h:544
SkMatrix & setIdentity()
Definition: SkMatrix.h:626
@ kRound_Cap
adds circle
Definition: SkPaint.h:335
@ kStroke_Style
set to stroke geometry
Definition: SkPaint.h:194
uint32_t nextU()
Definition: SkRandom.h:42
void printf(const char format[],...) SK_PRINTF_LIKE(2
Definition: SkString.cpp:534
void set(const SkString &src)
Definition: SkString.h:186
void append(const char text[])
Definition: SkString.h:203
void prepend(const char text[])
Definition: SkString.h:215
const char * c_str() const
Definition: SkString.h:133
void void void appendf(const char format[],...) SK_PRINTF_LIKE(2
Definition: SkString.cpp:550
const char * onGetName() override
Definition: RectBench.cpp:128
void setupPaint(SkPaint *paint) override
Definition: RectBench.cpp:121
const char * onGetName() override
Definition: RectBench.cpp:152
void setupPaint(SkPaint *paint) override
Definition: RectBench.cpp:146
const Paint & paint
Definition: color_source.cc:38
DlColor color
float SkScalar
Definition: extension.cpp:12
double y
double x
PODArray< SkColor > colors
Definition: SkRecords.h:276
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive mode
Definition: switches.h:228
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
string root
Definition: scale_cpu.py:20
SkScalar w
SkScalar h
SeparatedVector2 offset
Definition: SkMD5.cpp:130
void setXYWH(float x, float y, float width, float height)
Definition: SkRect.h:931
void offset(float dx, float dy)
Definition: SkRect.h:1016
constexpr float height() const
Definition: SkRect.h:769
constexpr float width() const
Definition: SkRect.h:762