OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "Sk4fGradientBase.h" | 8 #include "Sk4fGradientBase.h" |
9 | 9 |
10 #include <functional> | |
11 | |
10 namespace { | 12 namespace { |
11 | 13 |
12 const float kInv255Float = 1.0f / 255; | 14 const float kInv255Float = 1.0f / 255; |
13 | 15 |
14 SkPMColor pack_color(SkColor c, bool premul) { | 16 SkPMColor pack_color(SkColor c, bool premul) { |
15 return premul | 17 return premul |
16 ? SkPreMultiplyColor(c) | 18 ? SkPreMultiplyColor(c) |
17 : SkPackARGB32NoCheck(SkColorGetA(c), SkColorGetR(c), SkColorGetG(c), Sk ColorGetB(c)); | 19 : SkPackARGB32NoCheck(SkColorGetA(c), SkColorGetR(c), SkColorGetG(c), Sk ColorGetB(c)); |
18 } | 20 } |
19 | 21 |
20 // true when x is in [k1,k2) | 22 // true when x is in [k1,k2) |
21 bool in_range(SkScalar x, SkScalar k1, SkScalar k2) { | 23 bool in_range(SkScalar x, SkScalar k1, SkScalar k2) { |
22 SkASSERT(k1 != k2); | 24 SkASSERT(k1 != k2); |
23 return (k1 < k2) | 25 return (k1 < k2) |
24 ? (x >= k1 && x < k2) | 26 ? (x >= k1 && x < k2) |
25 : (x >= k2 && x < k1); | 27 : (x >= k2 && x < k1); |
26 } | 28 } |
27 | 29 |
30 class IntervalBuilder { | |
31 public: | |
32 IntervalBuilder(const SkColor* colors, const SkScalar* pos, int count, bool reverse) | |
33 : fColors(colors) | |
34 , fPos(pos) | |
35 , fCount(count) | |
36 , fFirstPos(reverse ? SK_Scalar1 : 0) | |
37 , fBegin(reverse ? count - 1 : 0) | |
38 , fAdvance(reverse ? -1 : 1) { | |
39 SkASSERT(colors); | |
40 SkASSERT(count > 1); | |
41 } | |
42 | |
43 void build(std::function<void(SkColor, SkColor, SkScalar, SkScalar)> | |
herb_g
2016/03/01 21:50:37
I would use:
template <Func>
void build(Func func)
f(malita)
2016/03/01 22:26:23
Done.
| |
44 func) const { | |
45 if (!fPos) { | |
46 buildImplicitPos(func); | |
herb_g
2016/03/01 21:50:37
this->
f(malita)
2016/03/01 22:26:23
Done.
| |
47 return; | |
48 } | |
49 | |
50 const int end = fBegin + fAdvance * (fCount - 1); | |
51 const SkScalar last_pos = 1 - fFirstPos; | |
herb_g
2016/03/01 21:50:37
lastPos
f(malita)
2016/03/01 22:26:23
Done.
| |
52 int prev = fBegin; | |
53 SkScalar prev_pos = fFirstPos; | |
herb_g
2016/03/01 21:50:37
prevPos
f(malita)
2016/03/01 22:26:23
Done.
| |
54 | |
55 do { | |
56 const int curr = prev + fAdvance; | |
57 SkASSERT(curr >= 0 && curr < fCount); | |
58 | |
59 // TODO: this sanitization should be done in SkGradientShaderBase | |
60 const SkScalar curr_pos = (fAdvance > 0) | |
herb_g
2016/03/01 21:50:36
currPos, here and other places
f(malita)
2016/03/01 22:26:23
Done.
| |
61 ? SkTPin(fPos[curr], prev_pos, last_pos) | |
62 : SkTPin(fPos[curr], last_pos, prev_pos); | |
63 | |
64 if (curr_pos != prev_pos) { | |
65 SkASSERT((curr_pos - prev_pos > 0) == (fAdvance > 0)); | |
66 func(fColors[prev], fColors[curr], prev_pos, curr_pos); | |
67 } | |
68 | |
69 prev = curr; | |
70 prev_pos = curr_pos; | |
71 } while (prev != end); | |
72 } | |
73 | |
74 private: | |
75 void buildImplicitPos(std::function<void(SkColor, SkColor, SkScalar, SkScala r)> | |
herb_g
2016/03/01 21:50:36
What is the difference between buildImplicitPos an
f(malita)
2016/03/01 22:26:23
build() handles the case of explicit color stop po
| |
76 func) const { | |
77 const SkScalar dt = fAdvance * SK_Scalar1 / (fCount - 1); | |
78 const int end = fBegin + fAdvance * (fCount - 2); | |
79 int prev = fBegin; | |
80 SkScalar prev_pos = fFirstPos; | |
81 | |
82 while (prev != end) { | |
83 const int curr = prev + fAdvance; | |
84 SkASSERT(curr >= 0 && curr < fCount); | |
85 | |
86 const SkScalar curr_pos = prev_pos + dt; | |
87 func(fColors[prev], fColors[curr], prev_pos, curr_pos); | |
88 prev = curr; | |
89 prev_pos = curr_pos; | |
90 } | |
91 | |
92 // emit the last interval explicitly, to avoid end pos precision issues | |
93 func(fColors[prev], fColors[prev + fAdvance], prev_pos, 1 - fFirstPos); | |
94 } | |
95 | |
96 const SkColor* fColors; | |
97 const SkScalar* fPos; | |
98 const int fCount; | |
99 const SkScalar fFirstPos; | |
100 const int fBegin; | |
101 const int fAdvance; | |
102 }; | |
103 | |
28 } // anonymous namespace | 104 } // anonymous namespace |
29 | 105 |
30 SkGradientShaderBase::GradientShaderBase4fContext:: | 106 SkGradientShaderBase::GradientShaderBase4fContext:: |
31 Interval::Interval(SkPMColor c0, SkScalar p0, | 107 Interval::Interval(SkPMColor c0, SkScalar p0, |
32 SkPMColor c1, SkScalar p1, | 108 SkPMColor c1, SkScalar p1, |
33 const Sk4f& componentScale) | 109 const Sk4f& componentScale) |
34 : fP0(p0) | 110 : fP0(p0) |
35 , fP1(p1) | 111 , fP1(p1) |
36 , fZeroRamp(c0 == c1) { | 112 , fZeroRamp(c0 == c1) { |
37 SkASSERT(p0 != p1); | 113 SkASSERT(p0 != p1); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
127 || shader.fColorsAreOpaque; | 203 || shader.fColorsAreOpaque; |
128 | 204 |
129 const float paintAlpha = rec.fPaint->getAlpha() * kInv255Float; | 205 const float paintAlpha = rec.fPaint->getAlpha() * kInv255Float; |
130 const Sk4f componentScale = fColorsArePremul | 206 const Sk4f componentScale = fColorsArePremul |
131 ? Sk4f(paintAlpha * kInv255Float) | 207 ? Sk4f(paintAlpha * kInv255Float) |
132 : Sk4f(kInv255Float, kInv255Float, kInv255Float, paintAlpha * kInv255Flo at); | 208 : Sk4f(kInv255Float, kInv255Float, kInv255Float, paintAlpha * kInv255Flo at); |
133 | 209 |
134 SkASSERT(shader.fColorCount > 1); | 210 SkASSERT(shader.fColorCount > 1); |
135 SkASSERT(shader.fOrigColors); | 211 SkASSERT(shader.fOrigColors); |
136 | 212 |
137 int direction = 1; | |
138 int first_index = 0; | |
139 int last_index = shader.fColorCount - 1; | |
140 SkScalar first_pos = 0; | |
141 SkScalar last_pos = 1; | |
142 const bool dx_is_pos = fDstToPos.getScaleX() >= 0; | 213 const bool dx_is_pos = fDstToPos.getScaleX() >= 0; |
143 if (!dx_is_pos) { | 214 const int first_index = dx_is_pos ? 0 : shader.fColorCount - 1; |
herb_g
2016/03/01 21:50:37
camelCase
| |
144 direction = -direction; | 215 const int last_index = shader.fColorCount - 1 - first_index; |
145 SkTSwap(first_index, last_index); | 216 const SkScalar first_pos = dx_is_pos ? 0 : SK_Scalar1; |
146 SkTSwap(first_pos, last_pos); | 217 const SkScalar last_pos = 1 - first_pos; |
147 } | |
148 | 218 |
149 if (shader.fTileMode == SkShader::kClamp_TileMode) { | 219 if (shader.fTileMode == SkShader::kClamp_TileMode) { |
150 // synthetic edge interval: -/+inf .. P0) | 220 // synthetic edge interval: -/+inf .. P0 |
151 const SkPMColor clamp_color = pack_color(shader.fOrigColors[first_index] , | 221 const SkPMColor clamp_color = pack_color(shader.fOrigColors[first_index] , |
152 fColorsArePremul); | 222 fColorsArePremul); |
153 const SkScalar clamp_pos = dx_is_pos ? SK_ScalarMin : SK_ScalarMax; | 223 const SkScalar clamp_pos = dx_is_pos ? SK_ScalarMin : SK_ScalarMax; |
154 fIntervals.emplace_back(clamp_color, clamp_pos, | 224 fIntervals.emplace_back(clamp_color, clamp_pos, |
155 clamp_color, first_pos, | 225 clamp_color, first_pos, |
156 componentScale); | 226 componentScale); |
227 } else if (shader.fTileMode == SkShader::kMirror_TileMode && !dx_is_pos) { | |
228 // synthetic mirror intervals injected before main intervals: (2 .. 1] | |
229 addMirrorIntervals(shader, componentScale, dx_is_pos); | |
157 } | 230 } |
158 | 231 |
159 int prev = first_index; | 232 const IntervalBuilder builder(shader.fOrigColors, |
160 int curr = prev + direction; | 233 shader.fOrigPos, |
161 SkScalar prev_pos = first_pos; | 234 shader.fColorCount, |
162 if (shader.fOrigPos) { | 235 !dx_is_pos); |
163 // explicit positions | 236 builder.build([this, &componentScale] (SkColor c0, SkColor c1, SkScalar p0, SkScalar p1) { |
164 do { | 237 SkASSERT(fIntervals.empty() || fIntervals.back().fP1 == p0); |
165 // TODO: this sanitization should be done in SkGradientShaderBase | |
166 const SkScalar curr_pos = (dx_is_pos) | |
167 ? SkTPin(shader.fOrigPos[curr], prev_pos, last_pos) | |
168 : SkTPin(shader.fOrigPos[curr], last_pos, prev_pos); | |
169 if (curr_pos != prev_pos) { | |
170 fIntervals.emplace_back( | |
171 pack_color(shader.fOrigColors[prev], fColorsArePremul), | |
172 prev_pos, | |
173 pack_color(shader.fOrigColors[curr], fColorsArePremul), | |
174 curr_pos, | |
175 componentScale); | |
176 } | |
177 prev = curr; | |
178 prev_pos = curr_pos; | |
179 curr += direction; | |
180 } while (prev != last_index); | |
181 } else { | |
182 // implicit positions | |
183 const SkScalar dt = direction * SK_Scalar1 / (shader.fColorCount - 1); | |
184 do { | |
185 const SkScalar curr_pos = prev_pos + dt; | |
186 fIntervals.emplace_back( | |
187 pack_color(shader.fOrigColors[prev], fColorsArePremul), | |
188 prev_pos, | |
189 pack_color(shader.fOrigColors[curr], fColorsArePremul), | |
190 curr_pos, | |
191 componentScale); | |
192 | 238 |
193 prev = curr; | 239 fIntervals.emplace_back(pack_color(c0, fColorsArePremul), |
194 prev_pos = curr_pos; | 240 p0, |
195 curr += direction; | 241 pack_color(c1, fColorsArePremul), |
196 } while (prev != last_index); | 242 p1, |
197 // pin the last pos to maintain accurate [0,1] pos coverage. | 243 componentScale); |
198 fIntervals.back().fP1 = last_pos; | 244 }); |
199 } | |
200 | 245 |
201 if (shader.fTileMode == SkShader::kClamp_TileMode) { | 246 if (shader.fTileMode == SkShader::kClamp_TileMode) { |
202 // synthetic edge interval: Pn .. +/-inf | 247 // synthetic edge interval: Pn .. +/-inf |
203 const SkPMColor clamp_color = | 248 const SkPMColor clamp_color = |
204 pack_color(shader.fOrigColors[last_index], fColorsArePremul); | 249 pack_color(shader.fOrigColors[last_index], fColorsArePremul); |
205 const SkScalar clamp_pos = dx_is_pos ? SK_ScalarMax : SK_ScalarMin; | 250 const SkScalar clamp_pos = dx_is_pos ? SK_ScalarMax : SK_ScalarMin; |
206 fIntervals.emplace_back(clamp_color, last_pos, | 251 fIntervals.emplace_back(clamp_color, last_pos, |
207 clamp_color, clamp_pos, | 252 clamp_color, clamp_pos, |
208 componentScale); | 253 componentScale); |
209 } else if (shader.fTileMode == SkShader::kMirror_TileMode) { | 254 } else if (shader.fTileMode == SkShader::kMirror_TileMode && dx_is_pos) { |
210 const int count = fIntervals.count(); | 255 // synthetic mirror intervals injected after main intervals: [1 .. 2) |
211 // synthetic flipped intervals in [1 .. 2) | 256 addMirrorIntervals(shader, componentScale, dx_is_pos); |
212 for (int i = count - 1; i >= 0; --i) { | |
213 const Interval& interval = fIntervals[i]; | |
214 const SkScalar p0 = interval.fP0; | |
215 const SkScalar p1 = interval.fP1; | |
216 Sk4f dc = Sk4f::Load(interval.fDc.fVec); | |
217 Sk4f c = Sk4f::Load(interval.fC0.fVec) + dc * Sk4f(p1 - p0); | |
218 fIntervals.emplace_back(c, dc * Sk4f(-1), 2 - p1, 2 - p0); | |
219 } | |
220 | |
221 if (!dx_is_pos) { | |
222 // When dx is negative, our initial invervals are in (1..0] order. | |
223 // The loop above appends their flipped counterparts, pivoted in 2: (1..0](2..1] | |
224 // To achieve the expected monotonic interval order, we need to | |
225 // swap the two halves: (2..1](1..0] | |
226 // TODO: we can probably avoid this late swap with some additional l ogic during | |
227 // the initial interval buildup. | |
228 SkASSERT(fIntervals.count() == count * 2) | |
229 for (int i = 0; i < count; ++i) { | |
230 SkTSwap(fIntervals[i], fIntervals[count + i]); | |
231 } | |
232 } | |
233 } | 257 } |
234 | 258 |
235 SkASSERT(fIntervals.count() > 0); | 259 SkASSERT(fIntervals.count() > 0); |
236 fCachedInterval = fIntervals.begin(); | 260 fCachedInterval = fIntervals.begin(); |
237 } | 261 } |
238 | 262 |
263 void SkGradientShaderBase:: | |
264 GradientShaderBase4fContext::addMirrorIntervals(const SkGradientShaderBase& shad er, | |
265 const Sk4f& componentScale, bool dx_is_pos) { | |
266 // Iterates in reverse order (vs main interval builder) and adds intervals r eflected in 2. | |
267 const IntervalBuilder builder(shader.fOrigColors, | |
268 shader.fOrigPos, | |
269 shader.fColorCount, | |
270 dx_is_pos); | |
271 builder.build([this, &componentScale] (SkColor c0, SkColor c1, SkScalar p0, SkScalar p1) { | |
272 SkASSERT(fIntervals.empty() || fIntervals.back().fP1 == 2 - p0); | |
273 | |
274 fIntervals.emplace_back(pack_color(c0, fColorsArePremul), | |
275 2 - p0, | |
276 pack_color(c1, fColorsArePremul), | |
277 2 - p1, | |
278 componentScale); | |
279 }); | |
280 } | |
281 | |
239 const SkGradientShaderBase::GradientShaderBase4fContext::Interval* | 282 const SkGradientShaderBase::GradientShaderBase4fContext::Interval* |
240 SkGradientShaderBase:: | 283 SkGradientShaderBase:: |
241 GradientShaderBase4fContext::findInterval(SkScalar fx) const { | 284 GradientShaderBase4fContext::findInterval(SkScalar fx) const { |
242 SkASSERT(in_range(fx, fIntervals.front().fP0, fIntervals.back().fP1)); | 285 SkASSERT(in_range(fx, fIntervals.front().fP0, fIntervals.back().fP1)); |
243 | 286 |
244 if (1) { | 287 if (1) { |
245 // Linear search, using the last scanline interval as a starting point. | 288 // Linear search, using the last scanline interval as a starting point. |
246 SkASSERT(fCachedInterval >= fIntervals.begin()); | 289 SkASSERT(fCachedInterval >= fIntervals.begin()); |
247 SkASSERT(fCachedInterval < fIntervals.end()); | 290 SkASSERT(fCachedInterval < fIntervals.end()); |
248 const int search_dir = fDstToPos.getScaleX() >= 0 ? 1 : -1; | 291 const int search_dir = fDstToPos.getScaleX() >= 0 ? 1 : -1; |
(...skipping 22 matching lines...) Expand all Loading... | |
271 } else { | 314 } else { |
272 SkASSERT(in_range(fx, i->fP1, i1->fP1)); | 315 SkASSERT(in_range(fx, i->fP1, i1->fP1)); |
273 i0 = i + 1; | 316 i0 = i + 1; |
274 } | 317 } |
275 } | 318 } |
276 | 319 |
277 SkASSERT(in_range(fx, i0->fP0, i0->fP1)); | 320 SkASSERT(in_range(fx, i0->fP0, i0->fP1)); |
278 return i0; | 321 return i0; |
279 } | 322 } |
280 } | 323 } |
OLD | NEW |