OLD | NEW |
---|---|
(Empty) | |
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 static const char* get_tilemode_name(SkShader::TileMode tilemode) { | |
18 switch (tilemode) { | |
19 case SkShader::kClamp_TileMode: | |
20 return "clamp"; | |
21 case SkShader::kRepeat_TileMode: | |
22 return "repeat"; | |
23 case SkShader::kMirror_TileMode: | |
24 return "mirror"; | |
25 default: | |
26 SkDEBUGFAIL("Unknown tilemode"); | |
27 return "error"; | |
28 } | |
29 } | |
30 | |
31 class HardStopGradientBench : public Benchmark { | |
32 public: | |
33 HardStopGradientBench(SkShader::TileMode tilemode, int count) { | |
34 fName.printf("hardstop_%s_%03d_colors", get_tilemode_name(tilemode), cou nt); | |
35 | |
36 fTileMode = tilemode; | |
37 fColorCount = count; | |
38 } | |
39 | |
40 const char* onGetName() override { | |
41 return fName.c_str(); | |
42 } | |
43 | |
44 SkIPoint onGetSize() override { | |
45 return SkIPoint::Make(kSize, kSize); | |
46 } | |
47 | |
48 void onPreDraw(SkCanvas* canvas) override { | |
49 // Left to right | |
50 SkPoint points[2] = { | |
51 SkPoint::Make(0, kSize/2), | |
52 SkPoint::Make(kSize-1, kSize/2), | |
53 }; | |
54 | |
55 // "Evenly spaced" colors | |
56 SkColor colors[100]; | |
bsalomon
2016/07/11 17:40:02
Why not use a dynamic array?
| |
57 for (int i = 0; i < fColorCount; i++) { | |
58 colors[i] = i * (0xffffffff / fColorCount); | |
59 } | |
60 | |
61 // Create a hard stop | |
62 SkScalar positions[100]; | |
63 positions[0] = 0.0f; | |
64 positions[1] = 0.0f; | |
65 for (int i = 2; i < fColorCount; i++) { | |
66 // Evenly spaced afterwards | |
67 positions[i] = i / (fColorCount - 1.0f); | |
68 } | |
69 | |
70 fPaint.setShader(SkGradientShader::MakeLinear(points, | |
71 colors, | |
72 positions, | |
73 fColorCount, | |
74 fTileMode, | |
75 0, | |
76 nullptr)); | |
77 } | |
78 | |
79 /* | |
80 * Draw simple linear gradient from left to right | |
81 */ | |
82 void onDraw(int loops, SkCanvas* canvas) override { | |
83 for (int i = 0; i < loops; i++) { | |
84 canvas->drawPaint(fPaint); | |
85 } | |
86 } | |
87 | |
88 private: | |
89 static const int kSize = 500; | |
90 | |
91 SkShader::TileMode fTileMode; | |
92 SkString fName; | |
93 int fColorCount; | |
94 SkPaint fPaint; | |
95 | |
96 typedef Benchmark INHERITED; | |
97 }; | |
98 | |
99 // Clamp | |
100 DEF_BENCH(return new HardStopGradientBench(SkShader::kClamp_TileMode, 5);) | |
101 DEF_BENCH(return new HardStopGradientBench(SkShader::kClamp_TileMode, 10);) | |
102 DEF_BENCH(return new HardStopGradientBench(SkShader::kClamp_TileMode, 25);) | |
103 DEF_BENCH(return new HardStopGradientBench(SkShader::kClamp_TileMode, 50);) | |
104 DEF_BENCH(return new HardStopGradientBench(SkShader::kClamp_TileMode, 100);) | |
105 | |
106 // Repeat | |
107 DEF_BENCH(return new HardStopGradientBench(SkShader::kRepeat_TileMode, 5);) | |
108 DEF_BENCH(return new HardStopGradientBench(SkShader::kRepeat_TileMode, 10);) | |
109 DEF_BENCH(return new HardStopGradientBench(SkShader::kRepeat_TileMode, 25);) | |
110 DEF_BENCH(return new HardStopGradientBench(SkShader::kRepeat_TileMode, 50);) | |
111 DEF_BENCH(return new HardStopGradientBench(SkShader::kRepeat_TileMode, 100);) | |
112 | |
113 // Mirror | |
114 DEF_BENCH(return new HardStopGradientBench(SkShader::kMirror_TileMode, 5);) | |
115 DEF_BENCH(return new HardStopGradientBench(SkShader::kMirror_TileMode, 10);) | |
116 DEF_BENCH(return new HardStopGradientBench(SkShader::kMirror_TileMode, 25);) | |
117 DEF_BENCH(return new HardStopGradientBench(SkShader::kMirror_TileMode, 50);) | |
118 DEF_BENCH(return new HardStopGradientBench(SkShader::kMirror_TileMode, 100);) | |
OLD | NEW |