OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 #include "SkBenchmark.h" | 8 #include "SkBenchmark.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
11 #include "SkColorPriv.h" | 11 #include "SkColorPriv.h" |
12 #include "SkGradientShader.h" | 12 #include "SkGradientShader.h" |
13 #include "SkPaint.h" | 13 #include "SkPaint.h" |
14 #include "SkShader.h" | 14 #include "SkShader.h" |
15 #include "SkString.h" | 15 #include "SkString.h" |
| 16 #include "SkUnitMapper.h" |
16 | 17 |
17 struct GradData { | 18 struct GradData { |
18 int fCount; | 19 int fCount; |
19 const SkColor* fColors; | 20 const SkColor* fColors; |
20 const SkScalar* fPos; | 21 const SkScalar* fPos; |
21 const char* fName; | 22 const char* fName; |
22 }; | 23 }; |
23 | 24 |
24 static const SkColor gColors[] = { | 25 static const SkColor gColors[] = { |
25 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK, | 26 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK, |
(...skipping 11 matching lines...) Expand all Loading... |
37 // We have several special-cases depending on the number (and spacing) of colors
, so | 38 // We have several special-cases depending on the number (and spacing) of colors
, so |
38 // try to exercise those here. | 39 // try to exercise those here. |
39 static const GradData gGradData[] = { | 40 static const GradData gGradData[] = { |
40 { 2, gColors, NULL, "" }, | 41 { 2, gColors, NULL, "" }, |
41 { 50, gColors, NULL, "_hicolor" }, // many color gradient | 42 { 50, gColors, NULL, "_hicolor" }, // many color gradient |
42 { 3, gColors, NULL, "_3color" }, | 43 { 3, gColors, NULL, "_3color" }, |
43 }; | 44 }; |
44 | 45 |
45 /// Ignores scale | 46 /// Ignores scale |
46 static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, | 47 static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, |
47 SkShader::TileMode tm, float scale) { | 48 SkShader::TileMode tm, SkUnitMapper* mapper, |
48 return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, data.fCo
unt, tm); | 49 float scale) { |
| 50 return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, |
| 51 data.fCount, tm, mapper); |
49 } | 52 } |
50 | 53 |
51 static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, | 54 static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, |
52 SkShader::TileMode tm, float scale) { | 55 SkShader::TileMode tm, SkUnitMapper* mapper, |
| 56 float scale) { |
53 SkPoint center; | 57 SkPoint center; |
54 center.set(SkScalarAve(pts[0].fX, pts[1].fX), | 58 center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
55 SkScalarAve(pts[0].fY, pts[1].fY)); | 59 SkScalarAve(pts[0].fY, pts[1].fY)); |
56 return SkGradientShader::CreateRadial(center, center.fX * scale, | 60 return SkGradientShader::CreateRadial(center, center.fX * scale, |
57 data.fColors, | 61 data.fColors, |
58 data.fPos, data.fCount, tm); | 62 data.fPos, data.fCount, tm, mapper); |
59 } | 63 } |
60 | 64 |
61 /// Ignores scale | 65 /// Ignores scale |
62 static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, | 66 static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, |
63 SkShader::TileMode tm, float scale) { | 67 SkShader::TileMode tm, SkUnitMapper* mapper, |
| 68 float scale) { |
64 SkPoint center; | 69 SkPoint center; |
65 center.set(SkScalarAve(pts[0].fX, pts[1].fX), | 70 center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
66 SkScalarAve(pts[0].fY, pts[1].fY)); | 71 SkScalarAve(pts[0].fY, pts[1].fY)); |
67 return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, | 72 return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, |
68 data.fPos, data.fCount); | 73 data.fPos, data.fCount, mapper); |
69 } | 74 } |
70 | 75 |
71 /// Ignores scale | 76 /// Ignores scale |
72 static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data, | 77 static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data, |
73 SkShader::TileMode tm, float scale) { | 78 SkShader::TileMode tm, SkUnitMapper* mapper, |
| 79 float scale) { |
74 SkPoint center0, center1; | 80 SkPoint center0, center1; |
75 center0.set(SkScalarAve(pts[0].fX, pts[1].fX), | 81 center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
76 SkScalarAve(pts[0].fY, pts[1].fY)); | 82 SkScalarAve(pts[0].fY, pts[1].fY)); |
77 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), | 83 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
78 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); | 84 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
79 return SkGradientShader::CreateTwoPointRadial( | 85 return SkGradientShader::CreateTwoPointRadial( |
80 center1, (pts[1].fX - pts[0].f
X) / 7, | 86 center1, (pts[1].fX - pts[0].f
X) / 7, |
81 center0, (pts[1].fX - pts[0].f
X) / 2, | 87 center0, (pts[1].fX - pts[0].f
X) / 2, |
82 data.fColors, data.fPos, data.
fCount, tm); | 88 data.fColors, data.fPos, data.
fCount, tm, mapper); |
83 } | 89 } |
84 | 90 |
85 /// Ignores scale | 91 /// Ignores scale |
86 static SkShader* MakeConical(const SkPoint pts[2], const GradData& data, | 92 static SkShader* MakeConical(const SkPoint pts[2], const GradData& data, |
87 SkShader::TileMode tm, float scale) { | 93 SkShader::TileMode tm, SkUnitMapper* mapper, |
| 94 float scale) { |
88 SkPoint center0, center1; | 95 SkPoint center0, center1; |
89 center0.set(SkScalarAve(pts[0].fX, pts[1].fX), | 96 center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
90 SkScalarAve(pts[0].fY, pts[1].fY)); | 97 SkScalarAve(pts[0].fY, pts[1].fY)); |
91 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), | 98 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
92 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); | 99 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
93 return SkGradientShader::CreateTwoPointConical(center1, (pts[1].fX - pts[0].
fX) / 7, | 100 return SkGradientShader::CreateTwoPointConical(center1, (pts[1].fX - pts[0].
fX) / 7, |
94 center0, (pts[1].fX - pts[0].
fX) / 2, | 101 center0, (pts[1].fX - pts[0].
fX) / 2, |
95 data.fColors, data.fPos, data
.fCount, tm); | 102 data.fColors, data.fPos, data
.fCount, tm, mapper); |
96 } | 103 } |
97 | 104 |
98 /// Ignores scale | 105 /// Ignores scale |
99 static SkShader* MakeConicalZeroRad(const SkPoint pts[2], const GradData& data, | 106 static SkShader* MakeConicalZeroRad(const SkPoint pts[2], const GradData& data, |
100 SkShader::TileMode tm, float scale) { | 107 SkShader::TileMode tm, SkUnitMapper* mapper, |
| 108 float scale) { |
101 SkPoint center0, center1; | 109 SkPoint center0, center1; |
102 center0.set(SkScalarAve(pts[0].fX, pts[1].fX), | 110 center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
103 SkScalarAve(pts[0].fY, pts[1].fY)); | 111 SkScalarAve(pts[0].fY, pts[1].fY)); |
104 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), | 112 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
105 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); | 113 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
106 return SkGradientShader::CreateTwoPointConical(center1, 0.0, | 114 return SkGradientShader::CreateTwoPointConical(center1, 0.0, |
107 center0, (pts[1].fX - pts[0].
fX) / 2, | 115 center0, (pts[1].fX - pts[0].
fX) / 2, |
108 data.fColors, data.fPos, data
.fCount, tm); | 116 data.fColors, data.fPos, data
.fCount, tm, mapper); |
109 } | 117 } |
110 | 118 |
111 /// Ignores scale | 119 /// Ignores scale |
112 static SkShader* MakeConicalOutside(const SkPoint pts[2], const GradData& data, | 120 static SkShader* MakeConicalOutside(const SkPoint pts[2], const GradData& data, |
113 SkShader::TileMode tm, float scale) { | 121 SkShader::TileMode tm, SkUnitMapper* mapper, |
| 122 float scale) { |
114 SkPoint center0, center1; | 123 SkPoint center0, center1; |
115 SkScalar radius0 = SkScalarDiv(pts[1].fX - pts[0].fX, 10); | 124 SkScalar radius0 = SkScalarDiv(pts[1].fX - pts[0].fX, 10); |
116 SkScalar radius1 = SkScalarDiv(pts[1].fX - pts[0].fX, 3); | 125 SkScalar radius1 = SkScalarDiv(pts[1].fX - pts[0].fX, 3); |
117 center0.set(pts[0].fX + radius0, pts[0].fY + radius0); | 126 center0.set(pts[0].fX + radius0, pts[0].fY + radius0); |
118 center1.set(pts[1].fX - radius1, pts[1].fY - radius1); | 127 center1.set(pts[1].fX - radius1, pts[1].fY - radius1); |
119 return SkGradientShader::CreateTwoPointConical(center0, radius0, | 128 return SkGradientShader::CreateTwoPointConical(center0, radius0, |
120 center1, radius1, | 129 center1, radius1, |
121 data.fColors, data.fPos, | 130 data.fColors, data.fPos, |
122 data.fCount, tm); | 131 data.fCount, tm, mapper); |
123 } | 132 } |
124 | 133 |
125 /// Ignores scale | 134 /// Ignores scale |
126 static SkShader* MakeConicalOutsideZeroRad(const SkPoint pts[2], const GradData&
data, | 135 static SkShader* MakeConicalOutsideZeroRad(const SkPoint pts[2], const GradData&
data, |
127 SkShader::TileMode tm, float scale) { | 136 SkShader::TileMode tm, SkUnitMapper*
mapper, |
| 137 float scale) { |
128 SkPoint center0, center1; | 138 SkPoint center0, center1; |
129 SkScalar radius0 = SkScalarDiv(pts[1].fX - pts[0].fX, 10); | 139 SkScalar radius0 = SkScalarDiv(pts[1].fX - pts[0].fX, 10); |
130 SkScalar radius1 = SkScalarDiv(pts[1].fX - pts[0].fX, 3); | 140 SkScalar radius1 = SkScalarDiv(pts[1].fX - pts[0].fX, 3); |
131 center0.set(pts[0].fX + radius0, pts[0].fY + radius0); | 141 center0.set(pts[0].fX + radius0, pts[0].fY + radius0); |
132 center1.set(pts[1].fX - radius1, pts[1].fY - radius1); | 142 center1.set(pts[1].fX - radius1, pts[1].fY - radius1); |
133 return SkGradientShader::CreateTwoPointConical(center0, 0.0, | 143 return SkGradientShader::CreateTwoPointConical(center0, 0.0, |
134 center1, radius1, | 144 center1, radius1, |
135 data.fColors, data.fPos, | 145 data.fColors, data.fPos, |
136 data.fCount, tm); | 146 data.fCount, tm, mapper); |
137 } | 147 } |
138 | 148 |
139 typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, | 149 typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, |
140 SkShader::TileMode tm, float scale); | 150 SkShader::TileMode tm, SkUnitMapper* mapper, |
| 151 float scale); |
141 | 152 |
142 static const struct { | 153 static const struct { |
143 GradMaker fMaker; | 154 GradMaker fMaker; |
144 const char* fName; | 155 const char* fName; |
145 } gGrads[] = { | 156 } gGrads[] = { |
146 { MakeLinear, "linear" }, | 157 { MakeLinear, "linear" }, |
147 { MakeRadial, "radial1" }, | 158 { MakeRadial, "radial1" }, |
148 { MakeSweep, "sweep" }, | 159 { MakeSweep, "sweep" }, |
149 { Make2Radial, "radial2" }, | 160 { Make2Radial, "radial2" }, |
150 { MakeConical, "conical" }, | 161 { MakeConical, "conical" }, |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 fName.appendf("_scale_%g", scale); | 233 fName.appendf("_scale_%g", scale); |
223 } | 234 } |
224 | 235 |
225 fName.append(data.fName); | 236 fName.append(data.fName); |
226 | 237 |
227 const SkPoint pts[2] = { | 238 const SkPoint pts[2] = { |
228 { 0, 0 }, | 239 { 0, 0 }, |
229 { SkIntToScalar(W), SkIntToScalar(H) } | 240 { SkIntToScalar(W), SkIntToScalar(H) } |
230 }; | 241 }; |
231 | 242 |
232 fShader = gGrads[gradType].fMaker(pts, data, tm, scale); | 243 fShader = gGrads[gradType].fMaker(pts, data, tm, NULL, scale); |
233 fGeomType = geomType; | 244 fGeomType = geomType; |
234 } | 245 } |
235 | 246 |
236 virtual ~GradientBench() { | 247 virtual ~GradientBench() { |
237 fShader->unref(); | 248 fShader->unref(); |
238 } | 249 } |
239 | 250 |
240 protected: | 251 protected: |
241 virtual const char* onGetName() { | 252 virtual const char* onGetName() { |
242 return fName.c_str(); | 253 return fName.c_str(); |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 canvas->drawRect(r, paint); | 356 canvas->drawRect(r, paint); |
346 } | 357 } |
347 } | 358 } |
348 | 359 |
349 private: | 360 private: |
350 typedef SkBenchmark INHERITED; | 361 typedef SkBenchmark INHERITED; |
351 }; | 362 }; |
352 | 363 |
353 DEF_BENCH( return new Gradient2Bench(false); ) | 364 DEF_BENCH( return new Gradient2Bench(false); ) |
354 DEF_BENCH( return new Gradient2Bench(true); ) | 365 DEF_BENCH( return new Gradient2Bench(true); ) |
OLD | NEW |