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; | |
13 | |
14 SkPMColor pack_color(SkColor c, bool premul) { | |
15 return premul | |
16 ? SkPreMultiplyColor(c) | |
17 : SkPackARGB32NoCheck(SkColorGetA(c), SkColorGetR(c), SkColorGetG(c), Sk
ColorGetB(c)); | |
18 } | |
19 | |
20 // true when x is in [k1,k2) | 12 // true when x is in [k1,k2) |
21 bool in_range(SkScalar x, SkScalar k1, SkScalar k2) { | 13 bool in_range(SkScalar x, SkScalar k1, SkScalar k2) { |
22 SkASSERT(k1 != k2); | 14 SkASSERT(k1 != k2); |
23 return (k1 < k2) | 15 return (k1 < k2) |
24 ? (x >= k1 && x < k2) | 16 ? (x >= k1 && x < k2) |
25 : (x >= k2 && x < k1); | 17 : (x >= k2 && x < k1); |
26 } | 18 } |
27 | 19 |
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 | |
105 } // anonymous namespace | 20 } // anonymous namespace |
106 | 21 |
107 SkGradientShaderBase::GradientShaderBase4fContext:: | 22 SkGradientShaderBase::GradientShaderBase4fContext:: |
108 Interval::Interval(SkPMColor c0, SkScalar p0, | 23 Interval::Interval(SkPMColor c0, SkScalar p0, |
109 SkPMColor c1, SkScalar p1, | 24 SkPMColor c1, SkScalar p1, |
110 const Sk4f& componentScale) | 25 const Sk4f& componentScale) |
111 : fP0(p0) | 26 : fP0(p0) |
112 , fP1(p1) | 27 , fP1(p1) |
113 , fZeroRamp(c0 == c1) { | 28 , fZeroRamp(c0 == c1) { |
114 SkASSERT(p0 != p1); | 29 SkASSERT(p0 != p1); |
(...skipping 25 matching lines...) Expand all Loading... |
140 GradientShaderBase4fContext::GradientShaderBase4fContext(const SkGradientShaderB
ase& shader, | 55 GradientShaderBase4fContext::GradientShaderBase4fContext(const SkGradientShaderB
ase& shader, |
141 const ContextRec& rec) | 56 const ContextRec& rec) |
142 : INHERITED(shader, rec) | 57 : INHERITED(shader, rec) |
143 , fFlags(this->INHERITED::getFlags()) | 58 , fFlags(this->INHERITED::getFlags()) |
144 #ifdef SK_SUPPORT_LEGACY_GRADIENT_DITHERING | 59 #ifdef SK_SUPPORT_LEGACY_GRADIENT_DITHERING |
145 , fDither(true) | 60 , fDither(true) |
146 #else | 61 #else |
147 , fDither(rec.fPaint->isDither()) | 62 , fDither(rec.fPaint->isDither()) |
148 #endif | 63 #endif |
149 { | 64 { |
150 // The main job here is to build an interval list. Intervals are a differen
t | |
151 // representation of the color stops data, optimized for efficient scan line | |
152 // access during shading. | |
153 // | |
154 // [{P0,C0} , {P1,C1}) [{P1,C2} , {P2,c3}) ... [{Pn,C2n} , {Pn+1,C2n+1}) | |
155 // | |
156 // The list is sorted in increasing dst order, i.e. X(Pk) < X(Pk+1). This | |
157 // allows us to always traverse left->right when iterating over a scan line. | |
158 // It also means that the interval order matches the color stops when dx >=
0, | |
159 // and is the inverse (pos, colors, order are flipped) when dx < 0. | |
160 // | |
161 // Note: the current representation duplicates pos data; we could refactor t
o | |
162 // avoid this if interval storage size becomes a concern. | |
163 // | |
164 // Aside from reordering, we also perform two more pre-processing steps at | |
165 // this stage: | |
166 // | |
167 // 1) scale the color components depending on paint alpha and the requeste
d | |
168 // interpolation space (note: the interval color storage is SkPM4f, but | |
169 // that doesn't necessarily mean the colors are premultiplied; that | |
170 // property is tracked in fColorsArePremul) | |
171 // | |
172 // 2) inject synthetic intervals to support tiling. | |
173 // | |
174 // * for kRepeat, no extra intervals are needed - the iterator just | |
175 // wraps around at the end: | |
176 // | |
177 // ->[P0,P1)->..[Pn-1,Pn)-> | |
178 // | |
179 // * for kClamp, we add two "infinite" intervals before/after: | |
180 // | |
181 // [-/+inf , P0)->[P0 , P1)->..[Pn-1 , Pn)->[Pn , +/-inf) | |
182 // | |
183 // (the iterator should never run off the end in this mode) | |
184 // | |
185 // * for kMirror, we extend the range to [0..2] and add a flipped | |
186 // interval series - then the iterator operates just as in the | |
187 // kRepeat case: | |
188 // | |
189 // ->[P0,P1)->..[Pn-1,Pn)->[2 - Pn,2 - Pn-1)->..[2 - P1,2 - P0)-> | |
190 // | |
191 // TODO: investigate collapsing intervals << 1px. | |
192 | |
193 const SkMatrix& inverse = this->getTotalInverse(); | 65 const SkMatrix& inverse = this->getTotalInverse(); |
194 fDstToPos.setConcat(shader.fPtsToUnit, inverse); | 66 fDstToPos.setConcat(shader.fPtsToUnit, inverse); |
195 fDstToPosProc = fDstToPos.getMapXYProc(); | 67 fDstToPosProc = fDstToPos.getMapXYProc(); |
196 fDstToPosClass = static_cast<uint8_t>(INHERITED::ComputeMatrixClass(fDstToPo
s)); | 68 fDstToPosClass = static_cast<uint8_t>(INHERITED::ComputeMatrixClass(fDstToPo
s)); |
197 | 69 |
198 if (shader.fColorsAreOpaque && this->getPaintAlpha() == SK_AlphaOPAQUE) { | 70 if (shader.fColorsAreOpaque && this->getPaintAlpha() == SK_AlphaOPAQUE) { |
199 fFlags |= kOpaqueAlpha_Flag; | 71 fFlags |= kOpaqueAlpha_Flag; |
200 } | 72 } |
201 | 73 |
202 fColorsArePremul = | 74 fColorsArePremul = |
203 (shader.fGradFlags & SkGradientShader::kInterpolateColorsInPremul_Flag) | 75 (shader.fGradFlags & SkGradientShader::kInterpolateColorsInPremul_Flag) |
204 || shader.fColorsAreOpaque; | 76 || shader.fColorsAreOpaque; |
205 | |
206 const float paintAlpha = rec.fPaint->getAlpha() * kInv255Float; | |
207 const Sk4f componentScale = fColorsArePremul | |
208 ? Sk4f(paintAlpha * kInv255Float) | |
209 : Sk4f(kInv255Float, kInv255Float, kInv255Float, paintAlpha * kInv255Flo
at); | |
210 | |
211 SkASSERT(shader.fColorCount > 1); | |
212 SkASSERT(shader.fOrigColors); | |
213 | |
214 const bool dx_is_pos = fDstToPos.getScaleX() >= 0; | |
215 const int first_index = dx_is_pos ? 0 : shader.fColorCount - 1; | |
216 const int last_index = shader.fColorCount - 1 - first_index; | |
217 const SkScalar first_pos = dx_is_pos ? 0 : SK_Scalar1; | |
218 const SkScalar last_pos = 1 - first_pos; | |
219 | |
220 if (shader.fTileMode == SkShader::kClamp_TileMode) { | |
221 // synthetic edge interval: -/+inf .. P0 | |
222 const SkPMColor clamp_color = pack_color(shader.fOrigColors[first_index]
, | |
223 fColorsArePremul); | |
224 const SkScalar clamp_pos = dx_is_pos ? SK_ScalarMin : SK_ScalarMax; | |
225 fIntervals.emplace_back(clamp_color, clamp_pos, | |
226 clamp_color, first_pos, | |
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); | |
231 } | |
232 | |
233 const IntervalBuilder builder(shader.fOrigColors, | |
234 shader.fOrigPos, | |
235 shader.fColorCount, | |
236 !dx_is_pos); | |
237 builder.build([this, &componentScale] (SkColor c0, SkColor c1, SkScalar p0,
SkScalar p1) { | |
238 SkASSERT(fIntervals.empty() || fIntervals.back().fP1 == p0); | |
239 | |
240 fIntervals.emplace_back(pack_color(c0, fColorsArePremul), | |
241 p0, | |
242 pack_color(c1, fColorsArePremul), | |
243 p1, | |
244 componentScale); | |
245 }); | |
246 | |
247 if (shader.fTileMode == SkShader::kClamp_TileMode) { | |
248 // synthetic edge interval: Pn .. +/-inf | |
249 const SkPMColor clamp_color = | |
250 pack_color(shader.fOrigColors[last_index], fColorsArePremul); | |
251 const SkScalar clamp_pos = dx_is_pos ? SK_ScalarMax : SK_ScalarMin; | |
252 fIntervals.emplace_back(clamp_color, last_pos, | |
253 clamp_color, clamp_pos, | |
254 componentScale); | |
255 } else if (shader.fTileMode == SkShader::kMirror_TileMode && dx_is_pos) { | |
256 // synthetic mirror intervals injected after main intervals: [1 .. 2) | |
257 addMirrorIntervals(shader, componentScale, dx_is_pos); | |
258 } | |
259 | |
260 SkASSERT(fIntervals.count() > 0); | |
261 fCachedInterval = fIntervals.begin(); | |
262 } | 77 } |
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 | |
283 const SkGradientShaderBase::GradientShaderBase4fContext::Interval* | |
284 SkGradientShaderBase:: | |
285 GradientShaderBase4fContext::findInterval(SkScalar fx) const { | |
286 SkASSERT(in_range(fx, fIntervals.front().fP0, fIntervals.back().fP1)); | |
287 | |
288 if (1) { | |
289 // Linear search, using the last scanline interval as a starting point. | |
290 SkASSERT(fCachedInterval >= fIntervals.begin()); | |
291 SkASSERT(fCachedInterval < fIntervals.end()); | |
292 const int search_dir = fDstToPos.getScaleX() >= 0 ? 1 : -1; | |
293 while (!in_range(fx, fCachedInterval->fP0, fCachedInterval->fP1)) { | |
294 fCachedInterval += search_dir; | |
295 if (fCachedInterval >= fIntervals.end()) { | |
296 fCachedInterval = fIntervals.begin(); | |
297 } else if (fCachedInterval < fIntervals.begin()) { | |
298 fCachedInterval = fIntervals.end() - 1; | |
299 } | |
300 } | |
301 return fCachedInterval; | |
302 } else { | |
303 // Binary search. Seems less effective than linear + caching. | |
304 const Interval* i0 = fIntervals.begin(); | |
305 const Interval* i1 = fIntervals.end() - 1; | |
306 | |
307 while (i0 != i1) { | |
308 SkASSERT(i0 < i1); | |
309 SkASSERT(in_range(fx, i0->fP0, i1->fP1)); | |
310 | |
311 const Interval* i = i0 + ((i1 - i0) >> 1); | |
312 | |
313 if (in_range(fx, i0->fP0, i->fP1)) { | |
314 i1 = i; | |
315 } else { | |
316 SkASSERT(in_range(fx, i->fP1, i1->fP1)); | |
317 i0 = i + 1; | |
318 } | |
319 } | |
320 | |
321 SkASSERT(in_range(fx, i0->fP0, i0->fP1)); | |
322 return i0; | |
323 } | |
324 } | |
OLD | NEW |