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