1 /*
2 * Copyright 2016 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 "Benchmark.h"
9
10 #include "SkCanvas.h"
11 #include "SkShader.h"
12 #include "SkGradientShader.h"
13 #include "SkString.h"
14 #include "SkColor.h"
15 #include "SkPaint.h"
16
17 enum class Kind {
18 k001,
19 k011,
20 kCentered,
21 };
22
kindstr(Kind k)23 const char* kindstr(Kind k) {
24 switch (k) {
25 case Kind::k001:
26 return "001";
27 case Kind::k011:
28 return "011";
29 case Kind::kCentered:
30 return "centered";
31 default:
32 return "Invalid kind";
33 }
34 }
35
36 class HardStopGradientBench_SpecialHardStops : public Benchmark {
37 public:
HardStopGradientBench_SpecialHardStops(int w,int h,Kind kind)38 HardStopGradientBench_SpecialHardStops(int w, int h, Kind kind) {
39 fW = w;
40 fH = h;
41 fKind = kind;
42
43 fName.printf("hardstop_special_%03dx%03d_%s", fW, fH, kindstr(fKind));
44 }
45
onGetName()46 const char* onGetName() override {
47 return fName.c_str();
48 }
49
onGetSize()50 SkIPoint onGetSize() override {
51 return SkIPoint::Make(fW, fH);
52 }
53
onPreDraw(SkCanvas * canvas)54 void onPreDraw(SkCanvas* canvas) override {
55 SkPoint points[2] = {
56 SkPoint::Make( 0.0f, fH/2.0f),
57 SkPoint::Make(fW+2.0f, fH/2.0f),
58 };
59
60 SkColor colors[4] = {
61 SK_ColorRED,
62 SK_ColorGREEN,
63 SK_ColorBLUE,
64 SK_ColorYELLOW,
65 };
66
67 SkScalar pos_001[3] = {
68 0.0f,
69 0.0f,
70 1.0f,
71 };
72
73 SkScalar pos_011[3] = {
74 0.0f,
75 1.0f,
76 1.0f,
77 };
78
79 SkScalar pos_centered[4] = {
80 0.0f,
81 0.5f,
82 0.5f,
83 1.0f,
84 };
85
86 SkScalar* positions = fKind == Kind::k001 ? pos_001 :
87 fKind == Kind::k011 ? pos_011 :
88 pos_centered;
89
90 int count = fKind == Kind::kCentered ? 4 : 3;
91
92 fPaint.setShader(SkGradientShader::MakeLinear(points,
93 colors,
94 positions,
95 count,
96 SkShader::kClamp_TileMode,
97 0,
98 nullptr));
99 }
100
onDraw(int loops,SkCanvas * canvas)101 void onDraw(int loops, SkCanvas* canvas) override {
102 for (int i = 0; i < loops; i++) {
103 canvas->drawPaint(fPaint);
104 }
105 }
106
107 private:
108 SkString fName;
109 int fW;
110 int fH;
111 Kind fKind;
112 SkPaint fPaint;
113 };
114
115 DEF_BENCH(return new HardStopGradientBench_SpecialHardStops(100, 100, Kind::k001););
116 DEF_BENCH(return new HardStopGradientBench_SpecialHardStops(200, 200, Kind::k001););
117 DEF_BENCH(return new HardStopGradientBench_SpecialHardStops(300, 300, Kind::k001););
118 DEF_BENCH(return new HardStopGradientBench_SpecialHardStops(400, 400, Kind::k001););
119 DEF_BENCH(return new HardStopGradientBench_SpecialHardStops(500, 500, Kind::k001););
120
121 DEF_BENCH(return new HardStopGradientBench_SpecialHardStops(100, 100, Kind::k011););
122 DEF_BENCH(return new HardStopGradientBench_SpecialHardStops(200, 200, Kind::k011););
123 DEF_BENCH(return new HardStopGradientBench_SpecialHardStops(300, 300, Kind::k011););
124 DEF_BENCH(return new HardStopGradientBench_SpecialHardStops(400, 400, Kind::k011););
125 DEF_BENCH(return new HardStopGradientBench_SpecialHardStops(500, 500, Kind::k011););
126
127 DEF_BENCH(return new HardStopGradientBench_SpecialHardStops(100, 100, Kind::kCentered););
128 DEF_BENCH(return new HardStopGradientBench_SpecialHardStops(200, 200, Kind::kCentered););
129 DEF_BENCH(return new HardStopGradientBench_SpecialHardStops(300, 300, Kind::kCentered););
130 DEF_BENCH(return new HardStopGradientBench_SpecialHardStops(400, 400, Kind::kCentered););
131 DEF_BENCH(return new HardStopGradientBench_SpecialHardStops(500, 500, Kind::kCentered););
132