Debian Codegraph @

bench/ClipStrategyBench.cpp

raw
1 /*
2 * Copyright 2017 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
13 class ClipStrategyBench : public Benchmark {
14 public:
15 enum class Mode {
16 kClipPath,
17 kMask,
18 };
19
20 ClipStrategyBench(Mode mode, size_t count)
21 : fMode(mode)
22 , fCount(count)
23 , fName("clip_strategy_"){
24
25 if (fMode == Mode::kClipPath) {
26 fName.append("path_");
27 SkPathBuilder builder;
28 this->forEachClipCircle([&](float x, float y, float r) {
29 builder.addCircle(x, y, r);
30 });
31 fClipPath = builder.detach();
32 } else {
33 fName.append("mask_");
34 }
35 fName.appendf("%zu", count);
36 }
37
38 ~ClipStrategyBench() override = default;
39
40 protected:
41 const char* onGetName() override {
42 return fName.c_str();
43 }
44
45 void onDraw(int loops, SkCanvas* canvas) override {
46 SkPaint p, srcIn;
47 p.setAntiAlias(true);
48 srcIn.setBlendMode(SkBlendMode::kSrcIn);
49
50 for (int i = 0; i < loops; ++i) {
51 SkAutoCanvasRestore acr(canvas, false);
52
53 if (fMode == Mode::kClipPath) {
54 canvas->save();
55 canvas->clipPath(fClipPath, true);
56 } else {
57 canvas->saveLayer(nullptr, nullptr);
58 this->forEachClipCircle([&](float x, float y, float r) {
59 canvas->drawCircle(x, y, r, p);
60 });
61 canvas->saveLayer(nullptr, &srcIn);
62 }
63 canvas->drawColor(SK_ColorGREEN);
64 }
65 }
66
67 private:
68 template <typename Func>
69 void forEachClipCircle(Func&& func) {
70 auto q = static_cast<float>(this->getSize().width()) / (fCount + 1);
71 for (size_t i = 1; i <= fCount; ++i) {
72 auto x = q * i;
73 func(x, x, q / 2);
74 }
75 }
76
77 Mode fMode;
78 size_t fCount;
79 SkString fName;
80 SkPath fClipPath;
81
82 using INHERITED = Benchmark;
83 };
84
85 DEF_BENCH( return new ClipStrategyBench(ClipStrategyBench::Mode::kClipPath, 1 );)
86 DEF_BENCH( return new ClipStrategyBench(ClipStrategyBench::Mode::kClipPath, 5 );)
87 DEF_BENCH( return new ClipStrategyBench(ClipStrategyBench::Mode::kClipPath, 10 );)
88 DEF_BENCH( return new ClipStrategyBench(ClipStrategyBench::Mode::kClipPath, 100);)
89
90 DEF_BENCH( return new ClipStrategyBench(ClipStrategyBench::Mode::kMask, 1 );)
91 DEF_BENCH( return new ClipStrategyBench(ClipStrategyBench::Mode::kMask, 5 );)
92 DEF_BENCH( return new ClipStrategyBench(ClipStrategyBench::Mode::kMask, 10 );)
93 DEF_BENCH( return new ClipStrategyBench(ClipStrategyBench::Mode::kMask, 100);)