aboutsummaryrefslogtreecommitdiffhomepage
path: root/bench/ClipStrategyBench.cpp
blob: c65cf4b97d4222aaf13ba5846c97fe35e50cf045 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * Copyright 2017 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "Benchmark.h"
#include "SkCanvas.h"
#include "SkPath.h"
#include "SkPathOps.h"

class ClipStrategyBench : public Benchmark {
public:
    enum class Mode {
        kClipPath,
        kMask,
    };

    ClipStrategyBench(Mode mode, size_t count)
        : fMode(mode)
        , fCount(count)
        , fName("clip_strategy_"){

        if (fMode == Mode::kClipPath) {
            fName.append("path_");
            this->forEachClipCircle([&](float x, float y, float r) {
                fClipPath.addCircle(x, y, r);
            });
        } else {
            fName.append("mask_");
        }
        fName.appendf("%zu", count);
    }

    ~ClipStrategyBench() override = default;

protected:
    const char* onGetName() override {
        return fName.c_str();
    }

    void onDraw(int loops, SkCanvas* canvas) override {
        SkPaint p, srcIn;
        p.setAntiAlias(true);
        srcIn.setBlendMode(SkBlendMode::kSrcIn);

        for (int i = 0; i < loops; ++i) {
            SkAutoCanvasRestore acr(canvas, false);

            if (fMode == Mode::kClipPath) {
                canvas->save();
                canvas->clipPath(fClipPath, true);
            } else {
                canvas->saveLayer(nullptr, nullptr);
                this->forEachClipCircle([&](float x, float y, float r) {
                    canvas->drawCircle(x, y, r, p);
                });
                canvas->saveLayer(nullptr, &srcIn);
            }
            canvas->drawColor(SK_ColorGREEN);
        }
    }

private:
    template <typename Func>
    void forEachClipCircle(Func&& func) {
        auto q = static_cast<float>(this->getSize().x()) / (fCount + 1);
        for (size_t i = 1; i <= fCount; ++i) {
            auto x = q * i;
            func(x, x, q / 2);
        }
    }

    Mode     fMode;
    size_t   fCount;
    SkString fName;
    SkPath   fClipPath;

    typedef Benchmark INHERITED;
};

DEF_BENCH( return new ClipStrategyBench(ClipStrategyBench::Mode::kClipPath, 1  );)
DEF_BENCH( return new ClipStrategyBench(ClipStrategyBench::Mode::kClipPath, 5  );)
DEF_BENCH( return new ClipStrategyBench(ClipStrategyBench::Mode::kClipPath, 10 );)
DEF_BENCH( return new ClipStrategyBench(ClipStrategyBench::Mode::kClipPath, 100);)

DEF_BENCH( return new ClipStrategyBench(ClipStrategyBench::Mode::kMask, 1  );)
DEF_BENCH( return new ClipStrategyBench(ClipStrategyBench::Mode::kMask, 5  );)
DEF_BENCH( return new ClipStrategyBench(ClipStrategyBench::Mode::kMask, 10 );)
DEF_BENCH( return new ClipStrategyBench(ClipStrategyBench::Mode::kMask, 100);)