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 namespace { | 10 namespace { |
11 | 11 |
12 const float kInv255Float = 1.0f / 255; | 12 const float kInv255Float = 1.0f / 255; |
13 | 13 |
14 SkPMColor pack_color(SkColor c, bool premul) { | 14 SkPMColor pack_color(SkColor c, bool premul) { |
15 return premul | 15 return premul |
16 ? SkPreMultiplyColor(c) | 16 ? SkPreMultiplyColor(c) |
17 : SkPackARGB32NoCheck(SkColorGetA(c), SkColorGetR(c), SkColorGetG(c), Sk
ColorGetB(c)); | 17 : SkPackARGB32NoCheck(SkColorGetA(c), SkColorGetR(c), SkColorGetG(c), Sk
ColorGetB(c)); |
18 } | 18 } |
19 | 19 |
20 // true when x is in [k1,k2) | 20 // true when x is in [k1,k2) |
21 bool in_range(SkScalar x, SkScalar k1, SkScalar k2) { | 21 bool in_range(SkScalar x, SkScalar k1, SkScalar k2) { |
22 SkASSERT(k1 != k2); | 22 SkASSERT(k1 != k2); |
23 return (k1 < k2) | 23 return (k1 < k2) |
24 ? (x >= k1 && x < k2) | 24 ? (x >= k1 && x < k2) |
25 : (x >= k2 && x < k1); | 25 : (x >= k2 && x < k1); |
26 } | 26 } |
27 | 27 |
| 28 class IntervalBuilder { |
| 29 public: |
| 30 IntervalBuilder(const SkColor* colors, const SkScalar* pos, int count, bool
reverse) |
| 31 : fColors(colors) |
| 32 , fPos(pos) |
| 33 , fCount(count) |
| 34 , fFirstPos(reverse ? SK_Scalar1 : 0) |
| 35 , fBegin(reverse ? count - 1 : 0) |
| 36 , fAdvance(reverse ? -1 : 1) { |
| 37 SkASSERT(colors); |
| 38 SkASSERT(count > 1); |
| 39 } |
| 40 |
| 41 template<typename F> |
| 42 void build(F func) const { |
| 43 if (!fPos) { |
| 44 this->buildImplicitPos(func); |
| 45 return; |
| 46 } |
| 47 |
| 48 const int end = fBegin + fAdvance * (fCount - 1); |
| 49 const SkScalar lastPos = 1 - fFirstPos; |
| 50 int prev = fBegin; |
| 51 SkScalar prevPos = fFirstPos; |
| 52 |
| 53 do { |
| 54 const int curr = prev + fAdvance; |
| 55 SkASSERT(curr >= 0 && curr < fCount); |
| 56 |
| 57 // TODO: this sanitization should be done in SkGradientShaderBase |
| 58 const SkScalar currPos = (fAdvance > 0) |
| 59 ? SkTPin(fPos[curr], prevPos, lastPos) |
| 60 : SkTPin(fPos[curr], lastPos, prevPos); |
| 61 |
| 62 if (currPos != prevPos) { |
| 63 SkASSERT((currPos - prevPos > 0) == (fAdvance > 0)); |
| 64 func(fColors[prev], fColors[curr], prevPos, currPos); |
| 65 } |
| 66 |
| 67 prev = curr; |
| 68 prevPos = currPos; |
| 69 } while (prev != end); |
| 70 } |
| 71 |
| 72 private: |
| 73 template<typename F> |
| 74 void buildImplicitPos(F func) const { |
| 75 // When clients don't provide explicit color stop positions (fPos == nul
lptr), |
| 76 // the color stops are distributed evenly across the unit interval |
| 77 // (implicit positioning). |
| 78 const SkScalar dt = fAdvance * SK_Scalar1 / (fCount - 1); |
| 79 const int end = fBegin + fAdvance * (fCount - 2); |
| 80 int prev = fBegin; |
| 81 SkScalar prevPos = fFirstPos; |
| 82 |
| 83 while (prev != end) { |
| 84 const int curr = prev + fAdvance; |
| 85 SkASSERT(curr >= 0 && curr < fCount); |
| 86 |
| 87 const SkScalar currPos = prevPos + dt; |
| 88 func(fColors[prev], fColors[curr], prevPos, currPos); |
| 89 prev = curr; |
| 90 prevPos = currPos; |
| 91 } |
| 92 |
| 93 // emit the last interval with a pinned end position, to avoid precision
issues |
| 94 func(fColors[prev], fColors[prev + fAdvance], prevPos, 1 - fFirstPos); |
| 95 } |
| 96 |
| 97 const SkColor* fColors; |
| 98 const SkScalar* fPos; |
| 99 const int fCount; |
| 100 const SkScalar fFirstPos; |
| 101 const int fBegin; |
| 102 const int fAdvance; |
| 103 }; |
| 104 |
28 } // anonymous namespace | 105 } // anonymous namespace |
29 | 106 |
30 SkGradientShaderBase::GradientShaderBase4fContext:: | 107 SkGradientShaderBase::GradientShaderBase4fContext:: |
31 Interval::Interval(SkPMColor c0, SkScalar p0, | 108 Interval::Interval(SkPMColor c0, SkScalar p0, |
32 SkPMColor c1, SkScalar p1, | 109 SkPMColor c1, SkScalar p1, |
33 const Sk4f& componentScale) | 110 const Sk4f& componentScale) |
34 : fP0(p0) | 111 : fP0(p0) |
35 , fP1(p1) | 112 , fP1(p1) |
36 , fZeroRamp(c0 == c1) { | 113 , fZeroRamp(c0 == c1) { |
37 SkASSERT(p0 != p1); | 114 SkASSERT(p0 != p1); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 || shader.fColorsAreOpaque; | 204 || shader.fColorsAreOpaque; |
128 | 205 |
129 const float paintAlpha = rec.fPaint->getAlpha() * kInv255Float; | 206 const float paintAlpha = rec.fPaint->getAlpha() * kInv255Float; |
130 const Sk4f componentScale = fColorsArePremul | 207 const Sk4f componentScale = fColorsArePremul |
131 ? Sk4f(paintAlpha * kInv255Float) | 208 ? Sk4f(paintAlpha * kInv255Float) |
132 : Sk4f(kInv255Float, kInv255Float, kInv255Float, paintAlpha * kInv255Flo
at); | 209 : Sk4f(kInv255Float, kInv255Float, kInv255Float, paintAlpha * kInv255Flo
at); |
133 | 210 |
134 SkASSERT(shader.fColorCount > 1); | 211 SkASSERT(shader.fColorCount > 1); |
135 SkASSERT(shader.fOrigColors); | 212 SkASSERT(shader.fOrigColors); |
136 | 213 |
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; | 214 const bool dx_is_pos = fDstToPos.getScaleX() >= 0; |
143 if (!dx_is_pos) { | 215 const int first_index = dx_is_pos ? 0 : shader.fColorCount - 1; |
144 direction = -direction; | 216 const int last_index = shader.fColorCount - 1 - first_index; |
145 SkTSwap(first_index, last_index); | 217 const SkScalar first_pos = dx_is_pos ? 0 : SK_Scalar1; |
146 SkTSwap(first_pos, last_pos); | 218 const SkScalar last_pos = 1 - first_pos; |
147 } | |
148 | 219 |
149 if (shader.fTileMode == SkShader::kClamp_TileMode) { | 220 if (shader.fTileMode == SkShader::kClamp_TileMode) { |
150 // synthetic edge interval: -/+inf .. P0) | 221 // synthetic edge interval: -/+inf .. P0 |
151 const SkPMColor clamp_color = pack_color(shader.fOrigColors[first_index]
, | 222 const SkPMColor clamp_color = pack_color(shader.fOrigColors[first_index]
, |
152 fColorsArePremul); | 223 fColorsArePremul); |
153 const SkScalar clamp_pos = dx_is_pos ? SK_ScalarMin : SK_ScalarMax; | 224 const SkScalar clamp_pos = dx_is_pos ? SK_ScalarMin : SK_ScalarMax; |
154 fIntervals.emplace_back(clamp_color, clamp_pos, | 225 fIntervals.emplace_back(clamp_color, clamp_pos, |
155 clamp_color, first_pos, | 226 clamp_color, first_pos, |
156 componentScale); | 227 componentScale); |
| 228 } else if (shader.fTileMode == SkShader::kMirror_TileMode && !dx_is_pos) { |
| 229 // synthetic mirror intervals injected before main intervals: (2 .. 1] |
| 230 addMirrorIntervals(shader, componentScale, dx_is_pos); |
157 } | 231 } |
158 | 232 |
159 int prev = first_index; | 233 const IntervalBuilder builder(shader.fOrigColors, |
160 int curr = prev + direction; | 234 shader.fOrigPos, |
161 SkScalar prev_pos = first_pos; | 235 shader.fColorCount, |
162 if (shader.fOrigPos) { | 236 !dx_is_pos); |
163 // explicit positions | 237 builder.build([this, &componentScale] (SkColor c0, SkColor c1, SkScalar p0,
SkScalar p1) { |
164 do { | 238 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 | 239 |
193 prev = curr; | 240 fIntervals.emplace_back(pack_color(c0, fColorsArePremul), |
194 prev_pos = curr_pos; | 241 p0, |
195 curr += direction; | 242 pack_color(c1, fColorsArePremul), |
196 } while (prev != last_index); | 243 p1, |
197 // pin the last pos to maintain accurate [0,1] pos coverage. | 244 componentScale); |
198 fIntervals.back().fP1 = last_pos; | 245 }); |
199 } | |
200 | 246 |
201 if (shader.fTileMode == SkShader::kClamp_TileMode) { | 247 if (shader.fTileMode == SkShader::kClamp_TileMode) { |
202 // synthetic edge interval: Pn .. +/-inf | 248 // synthetic edge interval: Pn .. +/-inf |
203 const SkPMColor clamp_color = | 249 const SkPMColor clamp_color = |
204 pack_color(shader.fOrigColors[last_index], fColorsArePremul); | 250 pack_color(shader.fOrigColors[last_index], fColorsArePremul); |
205 const SkScalar clamp_pos = dx_is_pos ? SK_ScalarMax : SK_ScalarMin; | 251 const SkScalar clamp_pos = dx_is_pos ? SK_ScalarMax : SK_ScalarMin; |
206 fIntervals.emplace_back(clamp_color, last_pos, | 252 fIntervals.emplace_back(clamp_color, last_pos, |
207 clamp_color, clamp_pos, | 253 clamp_color, clamp_pos, |
208 componentScale); | 254 componentScale); |
209 } else if (shader.fTileMode == SkShader::kMirror_TileMode) { | 255 } else if (shader.fTileMode == SkShader::kMirror_TileMode && dx_is_pos) { |
210 const int count = fIntervals.count(); | 256 // synthetic mirror intervals injected after main intervals: [1 .. 2) |
211 // synthetic flipped intervals in [1 .. 2) | 257 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 } | 258 } |
234 | 259 |
235 SkASSERT(fIntervals.count() > 0); | 260 SkASSERT(fIntervals.count() > 0); |
236 fCachedInterval = fIntervals.begin(); | 261 fCachedInterval = fIntervals.begin(); |
237 } | 262 } |
238 | 263 |
| 264 void SkGradientShaderBase:: |
| 265 GradientShaderBase4fContext::addMirrorIntervals(const SkGradientShaderBase& shad
er, |
| 266 const Sk4f& componentScale, bool
dx_is_pos) { |
| 267 // Iterates in reverse order (vs main interval builder) and adds intervals r
eflected in 2. |
| 268 const IntervalBuilder builder(shader.fOrigColors, |
| 269 shader.fOrigPos, |
| 270 shader.fColorCount, |
| 271 dx_is_pos); |
| 272 builder.build([this, &componentScale] (SkColor c0, SkColor c1, SkScalar p0,
SkScalar p1) { |
| 273 SkASSERT(fIntervals.empty() || fIntervals.back().fP1 == 2 - p0); |
| 274 |
| 275 fIntervals.emplace_back(pack_color(c0, fColorsArePremul), |
| 276 2 - p0, |
| 277 pack_color(c1, fColorsArePremul), |
| 278 2 - p1, |
| 279 componentScale); |
| 280 }); |
| 281 } |
| 282 |
239 const SkGradientShaderBase::GradientShaderBase4fContext::Interval* | 283 const SkGradientShaderBase::GradientShaderBase4fContext::Interval* |
240 SkGradientShaderBase:: | 284 SkGradientShaderBase:: |
241 GradientShaderBase4fContext::findInterval(SkScalar fx) const { | 285 GradientShaderBase4fContext::findInterval(SkScalar fx) const { |
242 SkASSERT(in_range(fx, fIntervals.front().fP0, fIntervals.back().fP1)); | 286 SkASSERT(in_range(fx, fIntervals.front().fP0, fIntervals.back().fP1)); |
243 | 287 |
244 if (1) { | 288 if (1) { |
245 // Linear search, using the last scanline interval as a starting point. | 289 // Linear search, using the last scanline interval as a starting point. |
246 SkASSERT(fCachedInterval >= fIntervals.begin()); | 290 SkASSERT(fCachedInterval >= fIntervals.begin()); |
247 SkASSERT(fCachedInterval < fIntervals.end()); | 291 SkASSERT(fCachedInterval < fIntervals.end()); |
248 const int search_dir = fDstToPos.getScaleX() >= 0 ? 1 : -1; | 292 const int search_dir = fDstToPos.getScaleX() >= 0 ? 1 : -1; |
(...skipping 22 matching lines...) Expand all Loading... |
271 } else { | 315 } else { |
272 SkASSERT(in_range(fx, i->fP1, i1->fP1)); | 316 SkASSERT(in_range(fx, i->fP1, i1->fP1)); |
273 i0 = i + 1; | 317 i0 = i + 1; |
274 } | 318 } |
275 } | 319 } |
276 | 320 |
277 SkASSERT(in_range(fx, i0->fP0, i0->fP1)); | 321 SkASSERT(in_range(fx, i0->fP0, i0->fP1)); |
278 return i0; | 322 return i0; |
279 } | 323 } |
280 } | 324 } |
OLD | NEW |