aboutsummaryrefslogtreecommitdiffhomepage
path: root/bench/HardStopGradientBench_SpecialHardStops.cpp
blob: e987901c67f17e29b8662dfc8c26118ababc0fa4 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * Copyright 2016 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 "SkShader.h"
#include "SkGradientShader.h"
#include "SkString.h"
#include "SkColor.h"
#include "SkPaint.h"

enum class Kind {
    k001,
    k011,
    kCentered,
};

const char* kindstr(Kind k) {
    switch (k) {
        case Kind::k001:
            return "001";
        case Kind::k011:
            return "011";
        case Kind::kCentered:
            return "centered";
        default:
            return "Invalid kind";
    }
}

class HardStopGradientBench_SpecialHardStops : public Benchmark {
public:
    HardStopGradientBench_SpecialHardStops(int w, int h, Kind kind) {
        fW    = w;
        fH    = h;
        fKind = kind;

        fName.printf("hardstop_special_%03dx%03d_%s", fW, fH, kindstr(fKind));
    }

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

    SkIPoint onGetSize() override {
        return SkIPoint::Make(fW, fH);
    }

    void onPreDraw(SkCanvas* canvas) override {
        SkPoint points[2] = {
            SkPoint::Make(   0.0f, fH/2.0f),
            SkPoint::Make(fW+2.0f, fH/2.0f),
        };

        SkColor colors[4] = {
            SK_ColorRED,
            SK_ColorGREEN,
            SK_ColorBLUE,
            SK_ColorYELLOW,
        };

        SkScalar pos_001[3] = {
            0.0f,
            0.0f,
            1.0f,
        };

        SkScalar pos_011[3] = {
            0.0f,
            1.0f,
            1.0f,
        };

        SkScalar pos_centered[4] = {
            0.0f,
            0.5f,
            0.5f,
            1.0f,
        };

        SkScalar* positions = fKind == Kind::k001 ? pos_001 :
                              fKind == Kind::k011 ? pos_011 :
                                                    pos_centered;

        int count = fKind == Kind::kCentered ? 4 : 3;

        fPaint.setShader(SkGradientShader::MakeLinear(points,
                                                      colors,
                                                      positions,
                                                      count,
                                                      SkShader::kClamp_TileMode,
                                                      0,
                                                      nullptr));
    }

    void onDraw(int loops, SkCanvas* canvas) override {
        for (int i = 0; i < loops; i++) {
            canvas->drawPaint(fPaint);
        }
    }

private:
    SkString fName;
    int      fW;
    int      fH;
    Kind     fKind;
    SkPaint  fPaint;
};

DEF_BENCH(return new HardStopGradientBench_SpecialHardStops(100, 100, Kind::k001););
DEF_BENCH(return new HardStopGradientBench_SpecialHardStops(200, 200, Kind::k001););
DEF_BENCH(return new HardStopGradientBench_SpecialHardStops(300, 300, Kind::k001););
DEF_BENCH(return new HardStopGradientBench_SpecialHardStops(400, 400, Kind::k001););
DEF_BENCH(return new HardStopGradientBench_SpecialHardStops(500, 500, Kind::k001););

DEF_BENCH(return new HardStopGradientBench_SpecialHardStops(100, 100, Kind::k011););
DEF_BENCH(return new HardStopGradientBench_SpecialHardStops(200, 200, Kind::k011););
DEF_BENCH(return new HardStopGradientBench_SpecialHardStops(300, 300, Kind::k011););
DEF_BENCH(return new HardStopGradientBench_SpecialHardStops(400, 400, Kind::k011););
DEF_BENCH(return new HardStopGradientBench_SpecialHardStops(500, 500, Kind::k011););

DEF_BENCH(return new HardStopGradientBench_SpecialHardStops(100, 100, Kind::kCentered););
DEF_BENCH(return new HardStopGradientBench_SpecialHardStops(200, 200, Kind::kCentered););
DEF_BENCH(return new HardStopGradientBench_SpecialHardStops(300, 300, Kind::kCentered););
DEF_BENCH(return new HardStopGradientBench_SpecialHardStops(400, 400, Kind::kCentered););
DEF_BENCH(return new HardStopGradientBench_SpecialHardStops(500, 500, Kind::kCentered););