Debian Codegraph @

bench/RectBench.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/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkShader.h"
13 #include "include/core/SkString.h"
14 #include "include/effects/SkGradient.h"
15 #include "src/base/SkRandom.h"
16 #include "tools/flags/CommandLineFlags.h"
17
18 static DEFINE_double(strokeWidth, -1.0, "If set, use this stroke width in RectBench.");
19
20 class RectBench : public Benchmark {
21 public:
22 int fShift, fStroke;
23 enum {
24 W = 640,
25 H = 480,
26 N = 300,
27 };
28 SkRect fRects[N];
29 SkColor fColors[N];
30 bool fAA;
31 bool fPerspective;
32
33 RectBench(int shift, int stroke = 0, bool aa = true, bool perspective = false)
34 : fShift(shift)
35 , fStroke(stroke)
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
55 protected:
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;
75 fRects[i].setXYWH(SkIntToScalar(x), SkIntToScalar(y),
76 SkIntToScalar(w), SkIntToScalar(h));
77 fRects[i].offset(offset, offset);
78 fColors[i] = rand.nextU() | 0xFF808080;
79 }
80 }
81
82 void onDraw(int loops, SkCanvas* canvas) override {
83 SkPaint paint;
84 if (fStroke > 0) {
85 paint.setStyle(SkPaint::kStroke_Style);
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
109 private:
110 SkString fBaseName;
111 using INHERITED = Benchmark;
112 };
113
114 class SrcModeRectBench : public RectBench {
115 public:
116 SrcModeRectBench() : INHERITED(1, 0) {
117 fMode = SkBlendMode::kSrc;
118 }
119
120 protected:
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
134 private:
135 SkBlendMode fMode;
136 SkString fName;
137
138 using INHERITED = RectBench;
139 };
140
141 class TransparentRectBench : public RectBench {
142 public:
143 TransparentRectBench() : INHERITED(1, 0) {}
144
145 protected:
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
158 private:
159 SkString fName;
160 using INHERITED = RectBench;
161 };
162
163 // Adds a shader to the paint that requires local coordinates to be used
164 class LocalCoordsRectBench : public RectBench {
165 public:
166 LocalCoordsRectBench(bool aa, bool perspective = false) : INHERITED(1, 0, aa, perspective) { }
167
168 protected:
169 void onDelayedSetup() override {
170 this->INHERITED::onDelayedSetup();
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} };
173 SkColor4f colors[] = { SkColors::kWhite, SkColors::kBlue };
174 fShader = SkShaders::LinearGradient(pts, {{colors, {}, 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
188 private:
189 SkString fName;
190 sk_sp<SkShader> fShader;
191
192 using INHERITED = RectBench;
193 };
194
195
196 class OvalBench : public RectBench {
197 public:
198 OvalBench(int shift, int stroke = 0) : RectBench(shift, stroke) {}
199 protected:
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
206 class RRectBench : public RectBench {
207 public:
208 RRectBench(int shift, int stroke = 0) : RectBench(shift, stroke) {}
209 protected:
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
216 class PointsBench : public RectBench {
217 public:
218 SkCanvas::PointMode fMode;
219
220 PointsBench(SkCanvas::PointMode mode, const char* name)
221 : RectBench(2)
222 , fMode(mode) {
223 fName = name;
224 }
225
226 protected:
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
238 SkPaint paint;
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, {reinterpret_cast<SkPoint*>(fRects), N*2}, paint);
246 paint.setColor(fColors[i % N]);
247 }
248 }
249 }
250 const char* onGetName() override { return fName.c_str(); }
251
252 private:
253 SkString fName;
254
255 };
256
257 class HairPointsBench : public Benchmark {
258 static constexpr float W = 640;
259 static constexpr float H = 480;
260 static constexpr int N = 300;
261
262 const SkBlendMode fBM;
263 const float fAlpha;
264 std::array<SkPoint, N> fPts;
265 SkString fName;
266
267 public:
268 HairPointsBench(SkBlendMode bm, float alpha) : fBM(bm), fAlpha(alpha) {
269
270 fName.printf("hair_points_mode_%s_alpha_%g",
271 SkBlendMode_Name(bm), alpha);
272 }
273
274 protected:
275 bool isSuitableFor(Backend backend) override {
276 if (backend == Backend::kNonRendering) {
277 return false;
278 }
279
280 // seems to be a bug on graphic (mali) + src_mode
281 auto showsBug = fBM == SkBlendMode::kSrc && backend == Backend::kGraphite;
282 return !showsBug;
283 }
284
285 const char* onGetName() override { return fName.c_str(); }
286
287 void onDelayedSetup() override {
288 SkRandom rand;
289 for (auto& p : fPts) {
290 const auto x = rand.nextF() * W;
291 const auto y = rand.nextF() * H;
292 p.set(x, y);
293 }
294 }
295
296 void onDraw(int loops, SkCanvas* canvas) override {
297 SkPaint paint;
298 paint.setBlendMode(fBM);
299 paint.setAlphaf(fAlpha);
300 paint.setStrokeWidth(0); // we're hairpoints
301
302 for (int loop = 0; loop < loops; loop++) {
303 for (int i = 0; i < 1000; ++i)
304 canvas->drawPoints(SkCanvas::kPoints_PointMode, fPts, paint);
305 }
306 }
307 };
308
309
310 /*******************************************************************************
311 * to bench BlitMask [Opaque, Black, color, shader]
312 *******************************************************************************/
313
314 class BlitMaskBench : public RectBench {
315 public:
316 enum kMaskType {
317 kMaskOpaque = 0,
318 kMaskBlack,
319 kMaskColor,
320 KMaskShader
321 };
322 SkCanvas::PointMode fMode;
323
324 BlitMaskBench(SkCanvas::PointMode mode,
325 BlitMaskBench::kMaskType type, const char* name) :
326 RectBench(2), fMode(mode), _type(type) {
327 fName = name;
328 }
329
330 protected:
331 void onDraw(int loops, SkCanvas* canvas) override {
332 SkScalar gSizes[] = {
333 SkIntToScalar(13), SkIntToScalar(24)
334 };
335 size_t sizes = std::size(gSizes);
336
337 if (FLAGS_strokeWidth >= 0) {
338 gSizes[0] = (SkScalar)FLAGS_strokeWidth;
339 sizes = 1;
340 }
341 SkRandom rand;
342 SkColor color = 0xFF000000;
343 U8CPU alpha = 0xFF;
344 SkPaint paint;
345 paint.setStrokeCap(SkPaint::kRound_Cap);
346 if (_type == KMaskShader) {
347 SkBitmap srcBM;
348 srcBM.allocN32Pixels(10, 1);
349 srcBM.eraseColor(0xFF00FF00);
350
351 paint.setShader(srcBM.makeShader(SkSamplingOptions()));
352 }
353 for (int loop = 0; loop < loops; loop++) {
354 for (size_t i = 0; i < sizes; i++) {
355 switch (_type) {
356 case kMaskOpaque:
357 color = fColors[i];
358 alpha = 0xFF;
359 break;
360 case kMaskBlack:
361 alpha = 0xFF;
362 color = 0xFF000000;
363 break;
364 case kMaskColor:
365 color = fColors[i];
366 alpha = rand.nextU() & 255;
367 break;
368 case KMaskShader:
369 break;
370 }
371 paint.setStrokeWidth(gSizes[i]);
372 this->setupPaint(&paint);
373 paint.setColor(color);
374 paint.setAlpha(alpha);
375 canvas->drawPoints(fMode, {reinterpret_cast<SkPoint*>(fRects), N*2}, paint);
376 }
377 }
378 }
379 const char* onGetName() override { return fName.c_str(); }
380
381 private:
382 using INHERITED = RectBench;
383 kMaskType _type;
384 SkString fName;
385 };
386
387 // AA rects
388 DEF_BENCH(return new RectBench(1, 0, true);)
389 DEF_BENCH(return new RectBench(1, 4, true);)
390 DEF_BENCH(return new RectBench(3, 0, true);)
391 DEF_BENCH(return new RectBench(3, 4, true);)
392 // Non-AA rects
393 DEF_BENCH(return new RectBench(1, 0, false);)
394 DEF_BENCH(return new RectBench(1, 4, false);)
395 DEF_BENCH(return new RectBench(3, 0, false);)
396 DEF_BENCH(return new RectBench(3, 4, false);)
397
398 DEF_BENCH(return new OvalBench(1);)
399 DEF_BENCH(return new OvalBench(3);)
400 DEF_BENCH(return new OvalBench(1, 4);)
401 DEF_BENCH(return new OvalBench(3, 4);)
402 DEF_BENCH(return new RRectBench(1);)
403 DEF_BENCH(return new RRectBench(1, 4);)
404 DEF_BENCH(return new RRectBench(3);)
405 DEF_BENCH(return new RRectBench(3, 4);)
406
407 DEF_BENCH(return new HairPointsBench(SkBlendMode::kSrcOver, 0.5f);)
408 DEF_BENCH(return new HairPointsBench(SkBlendMode::kSrcOver, 1);)
409 DEF_BENCH(return new HairPointsBench(SkBlendMode::kSrc, 0.5f);)
410 DEF_BENCH(return new HairPointsBench(SkBlendMode::kSrc, 1);)
411
412 DEF_BENCH(return new PointsBench(SkCanvas::kPoints_PointMode, "points");)
413 DEF_BENCH(return new PointsBench(SkCanvas::kLines_PointMode, "lines");)
414 DEF_BENCH(return new PointsBench(SkCanvas::kPolygon_PointMode, "polygon");)
415
416 DEF_BENCH(return new SrcModeRectBench();)
417
418 DEF_BENCH(return new TransparentRectBench();)
419
420 DEF_BENCH(return new LocalCoordsRectBench(true);)
421 DEF_BENCH(return new LocalCoordsRectBench(false);)
422
423 // Perspective rects
424 DEF_BENCH(return new RectBench(1, 0, true, true);)
425 DEF_BENCH(return new RectBench(1, 0, false, true);)
426 DEF_BENCH(return new LocalCoordsRectBench(true, true);)
427 DEF_BENCH(return new LocalCoordsRectBench(false, true);)
428
429 /* init the blitmask bench
430 */
431 DEF_BENCH(return new BlitMaskBench(SkCanvas::kPoints_PointMode,
432 BlitMaskBench::kMaskOpaque,
433 "maskopaque");)
434 DEF_BENCH(return new BlitMaskBench(SkCanvas::kPoints_PointMode,
435 BlitMaskBench::kMaskBlack,
436 "maskblack");)
437 DEF_BENCH(return new BlitMaskBench(SkCanvas::kPoints_PointMode,
438 BlitMaskBench::kMaskColor,
439 "maskcolor");)
440 DEF_BENCH(return new BlitMaskBench(SkCanvas::kPoints_PointMode,
441 BlitMaskBench::KMaskShader,
442 "maskshader");)
443
444 class RectBoundsBench : public Benchmark {
445 SkString fName;
446 std::vector<SkPoint> fPoints;
447
448 public:
449 RectBoundsBench(size_t count)
450 : fName(SkStringPrintf("rect_bounds_%zu", count))
451 , fPoints(count)
452 {}
453
454 protected:
455
456 const char* onGetName() override {
457 return fName.c_str();
458 }
459
460 void onDelayedSetup() override {
461 SkRandom rand;
462 for (auto& p : fPoints) {
463 float x = rand.nextF();
464 float y = rand.nextF();
465 p = {x, y};
466 }
467 }
468
469 bool isSuitableFor(Backend backend) override {
470 return backend == Backend::kNonRendering;
471 }
472
473 void onDraw(int loops, SkCanvas*) override {
474 for (int i = 0; i < loops; ++i) {
475 (void)SkRect::Bounds(fPoints);
476 }
477 }
478 };
479
480 DEF_BENCH(return new RectBoundsBench(4);)
481 DEF_BENCH(return new RectBoundsBench(400);)