blob: aab1779c70c8846ea5e7b6af279b9501a3c8d428 [file] [log] [blame]
fmalitabc590c02016-02-22 09:12:33 -08001/*
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 "Sk4fLinearGradient.h"
fmalita83aa9202016-03-23 12:28:14 -07009#include "Sk4x4f.h"
fmalitabc590c02016-02-22 09:12:33 -080010
Ben Wagner8a1036c2016-11-09 15:00:49 -050011#include <cmath>
12
fmalitabc590c02016-02-22 09:12:33 -080013namespace {
14
Florin Malitaae7bb042017-06-19 15:27:47 -040015template<ApplyPremul premul>
16void ramp(const Sk4f& c, const Sk4f& dc, SkPM4f dst[], int n) {
fmalitabc590c02016-02-22 09:12:33 -080017 SkASSERT(n > 0);
18
19 const Sk4f dc2 = dc + dc;
20 const Sk4f dc4 = dc2 + dc2;
21
22 Sk4f c0 = c ;
23 Sk4f c1 = c + dc;
24 Sk4f c2 = c0 + dc2;
25 Sk4f c3 = c1 + dc2;
26
27 while (n >= 4) {
Florin Malitaae7bb042017-06-19 15:27:47 -040028 DstTraits<premul>::store4x(c0, c1, c2, c3, dst);
fmalitabc590c02016-02-22 09:12:33 -080029 dst += 4;
30
31 c0 = c0 + dc4;
32 c1 = c1 + dc4;
33 c2 = c2 + dc4;
34 c3 = c3 + dc4;
35 n -= 4;
36 }
37 if (n & 2) {
Florin Malitaae7bb042017-06-19 15:27:47 -040038 DstTraits<premul>::store(c0, dst++);
39 DstTraits<premul>::store(c1, dst++);
fmalitabc590c02016-02-22 09:12:33 -080040 c0 = c0 + dc2;
41 }
42 if (n & 1) {
Florin Malitaae7bb042017-06-19 15:27:47 -040043 DstTraits<premul>::store(c0, dst);
fmalitabc590c02016-02-22 09:12:33 -080044 }
45}
46
47template<SkShader::TileMode>
48SkScalar pinFx(SkScalar);
49
50template<>
51SkScalar pinFx<SkShader::kClamp_TileMode>(SkScalar fx) {
52 return fx;
53}
54
55template<>
56SkScalar pinFx<SkShader::kRepeat_TileMode>(SkScalar fx) {
Florin Malita0fdde542016-11-14 15:54:04 -050057 SkScalar f = SkScalarFraction(fx);
58 if (f < 0) {
59 f = SkTMin(f + 1, nextafterf(1, 0));
60 }
61 SkASSERT(f >= 0);
62 SkASSERT(f < 1.0f);
63 return f;
fmalitabc590c02016-02-22 09:12:33 -080064}
65
66template<>
67SkScalar pinFx<SkShader::kMirror_TileMode>(SkScalar fx) {
Florin Malita0fdde542016-11-14 15:54:04 -050068 SkScalar f = SkScalarMod(fx, 2.0f);
69 if (f < 0) {
70 f = SkTMin(f + 2, nextafterf(2, 0));
71 }
72 SkASSERT(f >= 0);
73 SkASSERT(f < 2.0f);
74 return f;
fmalitabc590c02016-02-22 09:12:33 -080075}
76
Florin Malitae659c7f2017-02-09 13:46:55 -050077// true when x is in [k1,k2], or [k2, k1] when the interval is reversed.
fmalita6d7e4e82016-09-20 06:55:16 -070078// TODO(fmalita): hoist the reversed interval check out of this helper.
fmalita7520fc42016-03-04 11:01:24 -080079bool in_range(SkScalar x, SkScalar k1, SkScalar k2) {
80 SkASSERT(k1 != k2);
81 return (k1 < k2)
Florin Malitae659c7f2017-02-09 13:46:55 -050082 ? (x >= k1 && x <= k2)
83 : (x >= k2 && x <= k1);
fmalita7520fc42016-03-04 11:01:24 -080084}
85
fmalitabc590c02016-02-22 09:12:33 -080086} // anonymous namespace
87
88SkLinearGradient::
89LinearGradient4fContext::LinearGradient4fContext(const SkLinearGradient& shader,
90 const ContextRec& rec)
fmalita7520fc42016-03-04 11:01:24 -080091 : INHERITED(shader, rec) {
fmalita7520fc42016-03-04 11:01:24 -080092
fmalita7e6fcf82016-03-10 11:18:43 -080093 // Our fast path expects interval points to be monotonically increasing in x.
Ben Wagner8a1036c2016-11-09 15:00:49 -050094 const bool reverseIntervals = this->isFast() && std::signbit(fDstToPos.getScaleX());
Florin Malita0e36b3f2017-06-05 23:33:45 -040095 fIntervals.init(shader, rec.fDstColorSpace, shader.fTileMode,
Florin Malitada4545b2017-03-23 17:04:54 -040096 fColorsArePremul, rec.fPaint->getAlpha() * (1.0f / 255), reverseIntervals);
fmalita7520fc42016-03-04 11:01:24 -080097
Florin Malitada4545b2017-03-23 17:04:54 -040098 SkASSERT(fIntervals->count() > 0);
99 fCachedInterval = fIntervals->begin();
fmalita7520fc42016-03-04 11:01:24 -0800100}
101
Florin Malitada4545b2017-03-23 17:04:54 -0400102const Sk4fGradientInterval*
fmalita7520fc42016-03-04 11:01:24 -0800103SkLinearGradient::LinearGradient4fContext::findInterval(SkScalar fx) const {
Florin Malitacf20f782017-04-07 14:56:14 -0400104 SkASSERT(in_range(fx, fIntervals->front().fT0, fIntervals->back().fT1));
fmalita7520fc42016-03-04 11:01:24 -0800105
106 if (1) {
107 // Linear search, using the last scanline interval as a starting point.
Florin Malitada4545b2017-03-23 17:04:54 -0400108 SkASSERT(fCachedInterval >= fIntervals->begin());
109 SkASSERT(fCachedInterval < fIntervals->end());
fmalita7520fc42016-03-04 11:01:24 -0800110 const int search_dir = fDstToPos.getScaleX() >= 0 ? 1 : -1;
Florin Malitacf20f782017-04-07 14:56:14 -0400111 while (!in_range(fx, fCachedInterval->fT0, fCachedInterval->fT1)) {
fmalita7520fc42016-03-04 11:01:24 -0800112 fCachedInterval += search_dir;
Florin Malitada4545b2017-03-23 17:04:54 -0400113 if (fCachedInterval >= fIntervals->end()) {
114 fCachedInterval = fIntervals->begin();
115 } else if (fCachedInterval < fIntervals->begin()) {
116 fCachedInterval = fIntervals->end() - 1;
fmalita7520fc42016-03-04 11:01:24 -0800117 }
118 }
119 return fCachedInterval;
120 } else {
121 // Binary search. Seems less effective than linear + caching.
Florin Malitada4545b2017-03-23 17:04:54 -0400122 const auto* i0 = fIntervals->begin();
123 const auto* i1 = fIntervals->end() - 1;
fmalita7520fc42016-03-04 11:01:24 -0800124
125 while (i0 != i1) {
126 SkASSERT(i0 < i1);
Florin Malitacf20f782017-04-07 14:56:14 -0400127 SkASSERT(in_range(fx, i0->fT0, i1->fT1));
fmalita7520fc42016-03-04 11:01:24 -0800128
Florin Malitada4545b2017-03-23 17:04:54 -0400129 const auto* i = i0 + ((i1 - i0) >> 1);
fmalita7520fc42016-03-04 11:01:24 -0800130
Florin Malitacf20f782017-04-07 14:56:14 -0400131 if (in_range(fx, i0->fT0, i->fT1)) {
fmalita7520fc42016-03-04 11:01:24 -0800132 i1 = i;
133 } else {
Florin Malitacf20f782017-04-07 14:56:14 -0400134 SkASSERT(in_range(fx, i->fT1, i1->fT1));
fmalita7520fc42016-03-04 11:01:24 -0800135 i0 = i + 1;
136 }
137 }
138
Florin Malitacf20f782017-04-07 14:56:14 -0400139 SkASSERT(in_range(fx, i0->fT0, i0->fT1));
fmalita7520fc42016-03-04 11:01:24 -0800140 return i0;
141 }
142}
fmalitabc590c02016-02-22 09:12:33 -0800143
144void SkLinearGradient::
145LinearGradient4fContext::shadeSpan(int x, int y, SkPMColor dst[], int count) {
Florin Malitaae7bb042017-06-19 15:27:47 -0400146 // This impl only shades to 4f.
147 SkASSERT(false);
fmalitabc590c02016-02-22 09:12:33 -0800148}
149
150void SkLinearGradient::
151LinearGradient4fContext::shadeSpan4f(int x, int y, SkPM4f dst[], int count) {
fmalita7e6fcf82016-03-10 11:18:43 -0800152 if (!this->isFast()) {
153 this->INHERITED::shadeSpan4f(x, y, dst, count);
154 return;
155 }
156
fmalitabc590c02016-02-22 09:12:33 -0800157 SkASSERT(count > 0);
158 if (fColorsArePremul) {
Florin Malitaae7bb042017-06-19 15:27:47 -0400159 this->shadePremulSpan<ApplyPremul::False>(x, y, dst, count);
fmalitabc590c02016-02-22 09:12:33 -0800160 } else {
Florin Malitaae7bb042017-06-19 15:27:47 -0400161 this->shadePremulSpan<ApplyPremul::True>(x, y, dst, count);
fmalitabc590c02016-02-22 09:12:33 -0800162 }
163}
164
Florin Malitaae7bb042017-06-19 15:27:47 -0400165template<ApplyPremul premul>
fmalitabc590c02016-02-22 09:12:33 -0800166void SkLinearGradient::
Florin Malitaae7bb042017-06-19 15:27:47 -0400167LinearGradient4fContext::shadePremulSpan(int x, int y, SkPM4f dst[], int count) const {
fmalitabc590c02016-02-22 09:12:33 -0800168 const SkLinearGradient& shader =
169 static_cast<const SkLinearGradient&>(fShader);
170 switch (shader.fTileMode) {
171 case kClamp_TileMode:
Florin Malitaae7bb042017-06-19 15:27:47 -0400172 this->shadeSpanInternal<premul, kClamp_TileMode>(x, y, dst, count);
fmalitabc590c02016-02-22 09:12:33 -0800173 break;
174 case kRepeat_TileMode:
Florin Malitaae7bb042017-06-19 15:27:47 -0400175 this->shadeSpanInternal<premul, kRepeat_TileMode>(x, y, dst, count);
fmalitabc590c02016-02-22 09:12:33 -0800176 break;
177 case kMirror_TileMode:
Florin Malitaae7bb042017-06-19 15:27:47 -0400178 this->shadeSpanInternal<premul, kMirror_TileMode>(x, y, dst, count);
fmalitabc590c02016-02-22 09:12:33 -0800179 break;
180 }
181}
182
Florin Malitaae7bb042017-06-19 15:27:47 -0400183template<ApplyPremul premul, SkShader::TileMode tileMode>
fmalitabc590c02016-02-22 09:12:33 -0800184void SkLinearGradient::
Florin Malitaae7bb042017-06-19 15:27:47 -0400185LinearGradient4fContext::shadeSpanInternal(int x, int y, SkPM4f dst[], int count) const {
fmalitabc590c02016-02-22 09:12:33 -0800186 SkPoint pt;
187 fDstToPosProc(fDstToPos,
188 x + SK_ScalarHalf,
189 y + SK_ScalarHalf,
190 &pt);
191 const SkScalar fx = pinFx<tileMode>(pt.x());
192 const SkScalar dx = fDstToPos.getScaleX();
Florin Malitaae7bb042017-06-19 15:27:47 -0400193 LinearIntervalProcessor<premul, tileMode> proc(fIntervals->begin(),
194 fIntervals->end() - 1,
195 this->findInterval(fx),
196 fx,
197 dx,
198 SkScalarNearlyZero(dx * count));
fmalitabc590c02016-02-22 09:12:33 -0800199 while (count > 0) {
200 // What we really want here is SkTPin(advance, 1, count)
201 // but that's a significant perf hit for >> stops; investigate.
202 const int n = SkScalarTruncToInt(
203 SkTMin<SkScalar>(proc.currentAdvance() + 1, SkIntToScalar(count)));
204
205 // The current interval advance can be +inf (e.g. when reaching
206 // the clamp mode end intervals) - when that happens, we expect to
207 // a) consume all remaining count in one swoop
208 // b) return a zero color gradient
209 SkASSERT(SkScalarIsFinite(proc.currentAdvance())
210 || (n == count && proc.currentRampIsZero()));
211
212 if (proc.currentRampIsZero()) {
Florin Malitaae7bb042017-06-19 15:27:47 -0400213 DstTraits<premul>::store(proc.currentColor(), dst, n);
fmalitabc590c02016-02-22 09:12:33 -0800214 } else {
Florin Malitaae7bb042017-06-19 15:27:47 -0400215 ramp<premul>(proc.currentColor(), proc.currentColorGrad(), dst, n);
fmalitabc590c02016-02-22 09:12:33 -0800216 }
217
218 proc.advance(SkIntToScalar(n));
219 count -= n;
220 dst += n;
221 }
222}
223
Florin Malitaae7bb042017-06-19 15:27:47 -0400224template<ApplyPremul premul, SkShader::TileMode tileMode>
fmalitabc590c02016-02-22 09:12:33 -0800225class SkLinearGradient::
226LinearGradient4fContext::LinearIntervalProcessor {
227public:
Florin Malitada4545b2017-03-23 17:04:54 -0400228 LinearIntervalProcessor(const Sk4fGradientInterval* firstInterval,
229 const Sk4fGradientInterval* lastInterval,
230 const Sk4fGradientInterval* i,
fmalitabc590c02016-02-22 09:12:33 -0800231 SkScalar fx,
232 SkScalar dx,
233 bool is_vertical)
Florin Malitacf20f782017-04-07 14:56:14 -0400234 : fAdvX(is_vertical ? SK_ScalarInfinity : (i->fT1 - fx) / dx)
fmalitabc590c02016-02-22 09:12:33 -0800235 , fFirstInterval(firstInterval)
236 , fLastInterval(lastInterval)
237 , fInterval(i)
238 , fDx(dx)
239 , fIsVertical(is_vertical)
240 {
fmalita6d7e4e82016-09-20 06:55:16 -0700241 SkASSERT(fAdvX >= 0);
fmalitabc590c02016-02-22 09:12:33 -0800242 SkASSERT(firstInterval <= lastInterval);
fmalitaafac5812016-11-01 13:41:34 -0700243
244 if (tileMode != kClamp_TileMode && !is_vertical) {
Florin Malitacf20f782017-04-07 14:56:14 -0400245 const auto spanX = (lastInterval->fT1 - firstInterval->fT0) / dx;
fmalitaafac5812016-11-01 13:41:34 -0700246 SkASSERT(spanX >= 0);
247
248 // If we're in a repeating tile mode and the whole gradient is compressed into a
249 // fraction of a pixel, we just use the average color in zero-ramp mode.
250 // This also avoids cases where we make no progress due to interval advances being
251 // close to zero.
252 static constexpr SkScalar kMinSpanX = .25f;
253 if (spanX < kMinSpanX) {
254 this->init_average_props();
255 return;
256 }
257 }
258
Florin Malitacf20f782017-04-07 14:56:14 -0400259 this->compute_interval_props(fx);
fmalitabc590c02016-02-22 09:12:33 -0800260 }
261
262 SkScalar currentAdvance() const {
263 SkASSERT(fAdvX >= 0);
Florin Malitacf20f782017-04-07 14:56:14 -0400264 SkASSERT(fAdvX <= (fInterval->fT1 - fInterval->fT0) / fDx || !std::isfinite(fAdvX));
fmalitabc590c02016-02-22 09:12:33 -0800265 return fAdvX;
266 }
267
268 bool currentRampIsZero() const { return fZeroRamp; }
269 const Sk4f& currentColor() const { return fCc; }
270 const Sk4f& currentColorGrad() const { return fDcDx; }
271
272 void advance(SkScalar advX) {
273 SkASSERT(advX > 0);
274 SkASSERT(fAdvX >= 0);
275
276 if (advX >= fAdvX) {
277 advX = this->advance_interval(advX);
278 }
279 SkASSERT(advX < fAdvX);
280
281 fCc = fCc + fDcDx * Sk4f(advX);
282 fAdvX -= advX;
283 }
284
285private:
286 void compute_interval_props(SkScalar t) {
Florin Malitacf20f782017-04-07 14:56:14 -0400287 SkASSERT(in_range(t, fInterval->fT0, fInterval->fT1));
288
Florin Malitaae7bb042017-06-19 15:27:47 -0400289 const Sk4f dc = DstTraits<premul>::load(fInterval->fCg);
290 fCc = DstTraits<premul>::load(fInterval->fCb) + dc * Sk4f(t);
Florin Malita63f717d2017-05-08 09:53:11 -0400291 fDcDx = dc * fDx;
292 fZeroRamp = fIsVertical || (dc == 0).allTrue();
fmalitabc590c02016-02-22 09:12:33 -0800293 }
294
fmalitaafac5812016-11-01 13:41:34 -0700295 void init_average_props() {
296 fAdvX = SK_ScalarInfinity;
297 fZeroRamp = true;
298 fDcDx = 0;
299 fCc = Sk4f(0);
300
301 // TODO: precompute the average at interval setup time?
302 for (const auto* i = fFirstInterval; i <= fLastInterval; ++i) {
303 // Each interval contributes its average color to the total/weighted average:
304 //
Florin Malitacf20f782017-04-07 14:56:14 -0400305 // C = (c0 + c1) / 2 = (Cb + Cg * t0 + Cb + Cg * t1) / 2 = Cb + Cg *(t0 + t1) / 2
fmalitaafac5812016-11-01 13:41:34 -0700306 //
Florin Malitacf20f782017-04-07 14:56:14 -0400307 // Avg += C * (t1 - t0)
fmalitaafac5812016-11-01 13:41:34 -0700308 //
Florin Malitaae7bb042017-06-19 15:27:47 -0400309 const auto c = DstTraits<premul>::load(i->fCb)
310 + DstTraits<premul>::load(i->fCg) * (i->fT0 + i->fT1) * 0.5f;
Florin Malitacf20f782017-04-07 14:56:14 -0400311 fCc = fCc + c * (i->fT1 - i->fT0);
fmalitaafac5812016-11-01 13:41:34 -0700312 }
313 }
314
Florin Malitada4545b2017-03-23 17:04:54 -0400315 const Sk4fGradientInterval* next_interval(const Sk4fGradientInterval* i) const {
fmalitabc590c02016-02-22 09:12:33 -0800316 SkASSERT(i >= fFirstInterval);
317 SkASSERT(i <= fLastInterval);
318 i++;
319
320 if (tileMode == kClamp_TileMode) {
321 SkASSERT(i <= fLastInterval);
322 return i;
323 }
324
325 return (i <= fLastInterval) ? i : fFirstInterval;
326 }
327
328 SkScalar advance_interval(SkScalar advX) {
329 SkASSERT(advX >= fAdvX);
330
331 do {
332 advX -= fAdvX;
333 fInterval = this->next_interval(fInterval);
Florin Malitacf20f782017-04-07 14:56:14 -0400334 fAdvX = (fInterval->fT1 - fInterval->fT0) / fDx;
fmalitabc590c02016-02-22 09:12:33 -0800335 SkASSERT(fAdvX > 0);
336 } while (advX >= fAdvX);
337
Florin Malitacf20f782017-04-07 14:56:14 -0400338 compute_interval_props(fInterval->fT0);
fmalitabc590c02016-02-22 09:12:33 -0800339
340 SkASSERT(advX >= 0);
341 return advX;
342 }
343
fmalitabc590c02016-02-22 09:12:33 -0800344 // Current interval properties.
fmalitabc590c02016-02-22 09:12:33 -0800345 Sk4f fDcDx; // dst color gradient (dc/dx)
346 Sk4f fCc; // current color, interpolated in dst
347 SkScalar fAdvX; // remaining interval advance in dst
348 bool fZeroRamp; // current interval color grad is 0
349
Florin Malitada4545b2017-03-23 17:04:54 -0400350 const Sk4fGradientInterval* fFirstInterval;
351 const Sk4fGradientInterval* fLastInterval;
352 const Sk4fGradientInterval* fInterval; // current interval
353 const SkScalar fDx; // 'dx' for consistency with other impls; actually dt/dx
354 const bool fIsVertical;
fmalitabc590c02016-02-22 09:12:33 -0800355};
fmalita7e6fcf82016-03-10 11:18:43 -0800356
357void SkLinearGradient::
358LinearGradient4fContext::mapTs(int x, int y, SkScalar ts[], int count) const {
359 SkASSERT(count > 0);
360 SkASSERT(fDstToPosClass != kLinear_MatrixClass);
361
362 SkScalar sx = x + SK_ScalarHalf;
363 const SkScalar sy = y + SK_ScalarHalf;
364 SkPoint pt;
365
366 if (fDstToPosClass != kPerspective_MatrixClass) {
367 // kLinear_MatrixClass, kFixedStepInX_MatrixClass => fixed dt per scanline
368 const SkScalar dtdx = fDstToPos.fixedStepInX(sy).x();
369 fDstToPosProc(fDstToPos, sx, sy, &pt);
370
371 const Sk4f dtdx4 = Sk4f(4 * dtdx);
372 Sk4f t4 = Sk4f(pt.x() + 0 * dtdx,
373 pt.x() + 1 * dtdx,
374 pt.x() + 2 * dtdx,
375 pt.x() + 3 * dtdx);
376
377 while (count >= 4) {
378 t4.store(ts);
379 t4 = t4 + dtdx4;
380 ts += 4;
381 count -= 4;
382 }
383
384 if (count & 2) {
385 *ts++ = t4[0];
386 *ts++ = t4[1];
387 t4 = SkNx_shuffle<2, 0, 1, 3>(t4);
388 }
389
390 if (count & 1) {
391 *ts++ = t4[0];
392 }
393 } else {
394 for (int i = 0; i < count; ++i) {
395 fDstToPosProc(fDstToPos, sx, sy, &pt);
Florin Malita52bab302017-02-08 17:03:56 -0500396 // Perspective may yield NaN values.
397 // Short of a better idea, drop to 0.
398 ts[i] = SkScalarIsNaN(pt.x()) ? 0 : pt.x();
fmalita7e6fcf82016-03-10 11:18:43 -0800399 sx += SK_Scalar1;
400 }
401 }
402}