blob: fe900e8d03e974beb4d9f665282bf357647e86a0 [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
fmalitadc6c9bf2016-03-21 13:16:51 -070015template<DstType dstType, ApplyPremul premul>
16void ramp(const Sk4f& c, const Sk4f& dc, typename DstTraits<dstType, premul>::Type 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) {
fmalitadc6c9bf2016-03-21 13:16:51 -070028 DstTraits<dstType, 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) {
fmalitadc6c9bf2016-03-21 13:16:51 -070038 DstTraits<dstType, premul>::store(c0, dst++);
39 DstTraits<dstType, premul>::store(c1, dst++);
fmalitabc590c02016-02-22 09:12:33 -080040 c0 = c0 + dc2;
41 }
42 if (n & 1) {
fmalitadc6c9bf2016-03-21 13:16:51 -070043 DstTraits<dstType, premul>::store(c0, dst);
fmalitabc590c02016-02-22 09:12:33 -080044 }
45}
46
fmalita83aa9202016-03-23 12:28:14 -070047// Planar version of ramp (S32 no-premul only).
48template<>
49void ramp<DstType::S32, ApplyPremul::False>(const Sk4f& c, const Sk4f& dc, SkPMColor dst[], int n) {
50 SkASSERT(n > 0);
51
52 const Sk4f dc4 = dc * 4;
53 const Sk4x4f dc4x = { Sk4f(dc4[0]), Sk4f(dc4[1]), Sk4f(dc4[2]), Sk4f(dc4[3]) };
54 Sk4x4f c4x = Sk4x4f::Transpose(c, c + dc, c + dc * 2, c + dc * 3);
55
56 while (n >= 4) {
mtklein0c902472016-07-20 18:10:07 -070057 ( sk_linear_to_srgb(c4x.r) << 0
58 | sk_linear_to_srgb(c4x.g) << 8
59 | sk_linear_to_srgb(c4x.b) << 16
60 | Sk4f_round(255.0f*c4x.a) << 24).store(dst);
fmalita83aa9202016-03-23 12:28:14 -070061
62 c4x.r += dc4x.r;
63 c4x.g += dc4x.g;
64 c4x.b += dc4x.b;
65 c4x.a += dc4x.a;
66
67 dst += 4;
68 n -= 4;
69 }
70
71 if (n & 2) {
72 DstTraits<DstType::S32, ApplyPremul::False>
73 ::store(Sk4f(c4x.r[0], c4x.g[0], c4x.b[0], c4x.a[0]), dst++);
74 DstTraits<DstType::S32, ApplyPremul::False>
75 ::store(Sk4f(c4x.r[1], c4x.g[1], c4x.b[1], c4x.a[1]), dst++);
76 }
77
78 if (n & 1) {
79 DstTraits<DstType::S32, ApplyPremul::False>
80 ::store(Sk4f(c4x.r[n & 2], c4x.g[n & 2], c4x.b[n & 2], c4x.a[n & 2]), dst);
81 }
82}
83
fmalitabc590c02016-02-22 09:12:33 -080084template<SkShader::TileMode>
85SkScalar pinFx(SkScalar);
86
87template<>
88SkScalar pinFx<SkShader::kClamp_TileMode>(SkScalar fx) {
89 return fx;
90}
91
92template<>
93SkScalar pinFx<SkShader::kRepeat_TileMode>(SkScalar fx) {
Florin Malita0fdde542016-11-14 15:54:04 -050094 SkScalar f = SkScalarFraction(fx);
95 if (f < 0) {
96 f = SkTMin(f + 1, nextafterf(1, 0));
97 }
98 SkASSERT(f >= 0);
99 SkASSERT(f < 1.0f);
100 return f;
fmalitabc590c02016-02-22 09:12:33 -0800101}
102
103template<>
104SkScalar pinFx<SkShader::kMirror_TileMode>(SkScalar fx) {
Florin Malita0fdde542016-11-14 15:54:04 -0500105 SkScalar f = SkScalarMod(fx, 2.0f);
106 if (f < 0) {
107 f = SkTMin(f + 2, nextafterf(2, 0));
108 }
109 SkASSERT(f >= 0);
110 SkASSERT(f < 2.0f);
111 return f;
fmalitabc590c02016-02-22 09:12:33 -0800112}
113
Florin Malitae659c7f2017-02-09 13:46:55 -0500114// true when x is in [k1,k2], or [k2, k1] when the interval is reversed.
fmalita6d7e4e82016-09-20 06:55:16 -0700115// TODO(fmalita): hoist the reversed interval check out of this helper.
fmalita7520fc42016-03-04 11:01:24 -0800116bool in_range(SkScalar x, SkScalar k1, SkScalar k2) {
117 SkASSERT(k1 != k2);
118 return (k1 < k2)
Florin Malitae659c7f2017-02-09 13:46:55 -0500119 ? (x >= k1 && x <= k2)
120 : (x >= k2 && x <= k1);
fmalita7520fc42016-03-04 11:01:24 -0800121}
122
fmalitabc590c02016-02-22 09:12:33 -0800123} // anonymous namespace
124
125SkLinearGradient::
126LinearGradient4fContext::LinearGradient4fContext(const SkLinearGradient& shader,
127 const ContextRec& rec)
fmalita7520fc42016-03-04 11:01:24 -0800128 : INHERITED(shader, rec) {
fmalita7520fc42016-03-04 11:01:24 -0800129
fmalita7e6fcf82016-03-10 11:18:43 -0800130 // Our fast path expects interval points to be monotonically increasing in x.
Ben Wagner8a1036c2016-11-09 15:00:49 -0500131 const bool reverseIntervals = this->isFast() && std::signbit(fDstToPos.getScaleX());
Florin Malitada4545b2017-03-23 17:04:54 -0400132 fIntervals.init(shader.fOrigColors, shader.fOrigPos, shader.fColorCount, shader.fTileMode,
133 fColorsArePremul, rec.fPaint->getAlpha() * (1.0f / 255), reverseIntervals);
fmalita7520fc42016-03-04 11:01:24 -0800134
Florin Malitada4545b2017-03-23 17:04:54 -0400135 SkASSERT(fIntervals->count() > 0);
136 fCachedInterval = fIntervals->begin();
fmalita7520fc42016-03-04 11:01:24 -0800137}
138
Florin Malitada4545b2017-03-23 17:04:54 -0400139const Sk4fGradientInterval*
fmalita7520fc42016-03-04 11:01:24 -0800140SkLinearGradient::LinearGradient4fContext::findInterval(SkScalar fx) const {
Florin Malitacf20f782017-04-07 14:56:14 -0400141 SkASSERT(in_range(fx, fIntervals->front().fT0, fIntervals->back().fT1));
fmalita7520fc42016-03-04 11:01:24 -0800142
143 if (1) {
144 // Linear search, using the last scanline interval as a starting point.
Florin Malitada4545b2017-03-23 17:04:54 -0400145 SkASSERT(fCachedInterval >= fIntervals->begin());
146 SkASSERT(fCachedInterval < fIntervals->end());
fmalita7520fc42016-03-04 11:01:24 -0800147 const int search_dir = fDstToPos.getScaleX() >= 0 ? 1 : -1;
Florin Malitacf20f782017-04-07 14:56:14 -0400148 while (!in_range(fx, fCachedInterval->fT0, fCachedInterval->fT1)) {
fmalita7520fc42016-03-04 11:01:24 -0800149 fCachedInterval += search_dir;
Florin Malitada4545b2017-03-23 17:04:54 -0400150 if (fCachedInterval >= fIntervals->end()) {
151 fCachedInterval = fIntervals->begin();
152 } else if (fCachedInterval < fIntervals->begin()) {
153 fCachedInterval = fIntervals->end() - 1;
fmalita7520fc42016-03-04 11:01:24 -0800154 }
155 }
156 return fCachedInterval;
157 } else {
158 // Binary search. Seems less effective than linear + caching.
Florin Malitada4545b2017-03-23 17:04:54 -0400159 const auto* i0 = fIntervals->begin();
160 const auto* i1 = fIntervals->end() - 1;
fmalita7520fc42016-03-04 11:01:24 -0800161
162 while (i0 != i1) {
163 SkASSERT(i0 < i1);
Florin Malitacf20f782017-04-07 14:56:14 -0400164 SkASSERT(in_range(fx, i0->fT0, i1->fT1));
fmalita7520fc42016-03-04 11:01:24 -0800165
Florin Malitada4545b2017-03-23 17:04:54 -0400166 const auto* i = i0 + ((i1 - i0) >> 1);
fmalita7520fc42016-03-04 11:01:24 -0800167
Florin Malitacf20f782017-04-07 14:56:14 -0400168 if (in_range(fx, i0->fT0, i->fT1)) {
fmalita7520fc42016-03-04 11:01:24 -0800169 i1 = i;
170 } else {
Florin Malitacf20f782017-04-07 14:56:14 -0400171 SkASSERT(in_range(fx, i->fT1, i1->fT1));
fmalita7520fc42016-03-04 11:01:24 -0800172 i0 = i + 1;
173 }
174 }
175
Florin Malitacf20f782017-04-07 14:56:14 -0400176 SkASSERT(in_range(fx, i0->fT0, i0->fT1));
fmalita7520fc42016-03-04 11:01:24 -0800177 return i0;
178 }
179}
fmalitabc590c02016-02-22 09:12:33 -0800180
181void SkLinearGradient::
182LinearGradient4fContext::shadeSpan(int x, int y, SkPMColor dst[], int count) {
fmalita7e6fcf82016-03-10 11:18:43 -0800183 if (!this->isFast()) {
184 this->INHERITED::shadeSpan(x, y, dst, count);
185 return;
186 }
187
fmalitabc590c02016-02-22 09:12:33 -0800188 // TODO: plumb dithering
189 SkASSERT(count > 0);
190 if (fColorsArePremul) {
fmalitadc6c9bf2016-03-21 13:16:51 -0700191 this->shadePremulSpan<DstType::L32,
fmalitaa928b282016-03-18 10:28:23 -0700192 ApplyPremul::False>(x, y, dst, count);
fmalitabc590c02016-02-22 09:12:33 -0800193 } else {
fmalitadc6c9bf2016-03-21 13:16:51 -0700194 this->shadePremulSpan<DstType::L32,
fmalitaa928b282016-03-18 10:28:23 -0700195 ApplyPremul::True>(x, y, dst, count);
fmalitabc590c02016-02-22 09:12:33 -0800196 }
197}
198
199void SkLinearGradient::
200LinearGradient4fContext::shadeSpan4f(int x, int y, SkPM4f dst[], int count) {
fmalita7e6fcf82016-03-10 11:18:43 -0800201 if (!this->isFast()) {
202 this->INHERITED::shadeSpan4f(x, y, dst, count);
203 return;
204 }
205
fmalitabc590c02016-02-22 09:12:33 -0800206 // TONOTDO: plumb dithering
207 SkASSERT(count > 0);
208 if (fColorsArePremul) {
fmalitadc6c9bf2016-03-21 13:16:51 -0700209 this->shadePremulSpan<DstType::F32,
fmalitaa928b282016-03-18 10:28:23 -0700210 ApplyPremul::False>(x, y, dst, count);
fmalitabc590c02016-02-22 09:12:33 -0800211 } else {
fmalitadc6c9bf2016-03-21 13:16:51 -0700212 this->shadePremulSpan<DstType::F32,
fmalitaa928b282016-03-18 10:28:23 -0700213 ApplyPremul::True>(x, y, dst, count);
fmalitabc590c02016-02-22 09:12:33 -0800214 }
215}
216
fmalitadc6c9bf2016-03-21 13:16:51 -0700217template<DstType dstType, ApplyPremul premul>
fmalitabc590c02016-02-22 09:12:33 -0800218void SkLinearGradient::
219LinearGradient4fContext::shadePremulSpan(int x, int y,
fmalitadc6c9bf2016-03-21 13:16:51 -0700220 typename DstTraits<dstType, premul>::Type dst[],
fmalitabc590c02016-02-22 09:12:33 -0800221 int count) const {
222 const SkLinearGradient& shader =
223 static_cast<const SkLinearGradient&>(fShader);
224 switch (shader.fTileMode) {
225 case kClamp_TileMode:
fmalitadc6c9bf2016-03-21 13:16:51 -0700226 this->shadeSpanInternal<dstType,
fmalitaa928b282016-03-18 10:28:23 -0700227 premul,
fmalitabc590c02016-02-22 09:12:33 -0800228 kClamp_TileMode>(x, y, dst, count);
229 break;
230 case kRepeat_TileMode:
fmalitadc6c9bf2016-03-21 13:16:51 -0700231 this->shadeSpanInternal<dstType,
fmalitaa928b282016-03-18 10:28:23 -0700232 premul,
fmalitabc590c02016-02-22 09:12:33 -0800233 kRepeat_TileMode>(x, y, dst, count);
234 break;
235 case kMirror_TileMode:
fmalitadc6c9bf2016-03-21 13:16:51 -0700236 this->shadeSpanInternal<dstType,
fmalitaa928b282016-03-18 10:28:23 -0700237 premul,
fmalitabc590c02016-02-22 09:12:33 -0800238 kMirror_TileMode>(x, y, dst, count);
239 break;
240 }
241}
242
fmalitadc6c9bf2016-03-21 13:16:51 -0700243template<DstType dstType, ApplyPremul premul, SkShader::TileMode tileMode>
fmalitabc590c02016-02-22 09:12:33 -0800244void SkLinearGradient::
245LinearGradient4fContext::shadeSpanInternal(int x, int y,
fmalitadc6c9bf2016-03-21 13:16:51 -0700246 typename DstTraits<dstType, premul>::Type dst[],
fmalitabc590c02016-02-22 09:12:33 -0800247 int count) const {
248 SkPoint pt;
249 fDstToPosProc(fDstToPos,
250 x + SK_ScalarHalf,
251 y + SK_ScalarHalf,
252 &pt);
253 const SkScalar fx = pinFx<tileMode>(pt.x());
254 const SkScalar dx = fDstToPos.getScaleX();
Florin Malitada4545b2017-03-23 17:04:54 -0400255 LinearIntervalProcessor<dstType, premul, tileMode> proc(fIntervals->begin(),
256 fIntervals->end() - 1,
fmalita3a2e45a2016-10-14 08:18:24 -0700257 this->findInterval(fx),
258 fx,
259 dx,
260 SkScalarNearlyZero(dx * count));
fmalitabc590c02016-02-22 09:12:33 -0800261 while (count > 0) {
262 // What we really want here is SkTPin(advance, 1, count)
263 // but that's a significant perf hit for >> stops; investigate.
264 const int n = SkScalarTruncToInt(
265 SkTMin<SkScalar>(proc.currentAdvance() + 1, SkIntToScalar(count)));
266
267 // The current interval advance can be +inf (e.g. when reaching
268 // the clamp mode end intervals) - when that happens, we expect to
269 // a) consume all remaining count in one swoop
270 // b) return a zero color gradient
271 SkASSERT(SkScalarIsFinite(proc.currentAdvance())
272 || (n == count && proc.currentRampIsZero()));
273
274 if (proc.currentRampIsZero()) {
fmalitadc6c9bf2016-03-21 13:16:51 -0700275 DstTraits<dstType, premul>::store(proc.currentColor(),
276 dst, n);
fmalitabc590c02016-02-22 09:12:33 -0800277 } else {
fmalitadc6c9bf2016-03-21 13:16:51 -0700278 ramp<dstType, premul>(proc.currentColor(),
279 proc.currentColorGrad(),
280 dst, n);
fmalitabc590c02016-02-22 09:12:33 -0800281 }
282
283 proc.advance(SkIntToScalar(n));
284 count -= n;
285 dst += n;
286 }
287}
288
fmalita3a2e45a2016-10-14 08:18:24 -0700289template<DstType dstType, ApplyPremul premul, SkShader::TileMode tileMode>
fmalitabc590c02016-02-22 09:12:33 -0800290class SkLinearGradient::
291LinearGradient4fContext::LinearIntervalProcessor {
292public:
Florin Malitada4545b2017-03-23 17:04:54 -0400293 LinearIntervalProcessor(const Sk4fGradientInterval* firstInterval,
294 const Sk4fGradientInterval* lastInterval,
295 const Sk4fGradientInterval* i,
fmalitabc590c02016-02-22 09:12:33 -0800296 SkScalar fx,
297 SkScalar dx,
298 bool is_vertical)
Florin Malitacf20f782017-04-07 14:56:14 -0400299 : fAdvX(is_vertical ? SK_ScalarInfinity : (i->fT1 - fx) / dx)
fmalitabc590c02016-02-22 09:12:33 -0800300 , fFirstInterval(firstInterval)
301 , fLastInterval(lastInterval)
302 , fInterval(i)
303 , fDx(dx)
304 , fIsVertical(is_vertical)
305 {
fmalita6d7e4e82016-09-20 06:55:16 -0700306 SkASSERT(fAdvX >= 0);
fmalitabc590c02016-02-22 09:12:33 -0800307 SkASSERT(firstInterval <= lastInterval);
fmalitaafac5812016-11-01 13:41:34 -0700308
309 if (tileMode != kClamp_TileMode && !is_vertical) {
Florin Malitacf20f782017-04-07 14:56:14 -0400310 const auto spanX = (lastInterval->fT1 - firstInterval->fT0) / dx;
fmalitaafac5812016-11-01 13:41:34 -0700311 SkASSERT(spanX >= 0);
312
313 // If we're in a repeating tile mode and the whole gradient is compressed into a
314 // fraction of a pixel, we just use the average color in zero-ramp mode.
315 // This also avoids cases where we make no progress due to interval advances being
316 // close to zero.
317 static constexpr SkScalar kMinSpanX = .25f;
318 if (spanX < kMinSpanX) {
319 this->init_average_props();
320 return;
321 }
322 }
323
Florin Malitacf20f782017-04-07 14:56:14 -0400324 this->compute_interval_props(fx);
fmalitabc590c02016-02-22 09:12:33 -0800325 }
326
327 SkScalar currentAdvance() const {
328 SkASSERT(fAdvX >= 0);
Florin Malitacf20f782017-04-07 14:56:14 -0400329 SkASSERT(fAdvX <= (fInterval->fT1 - fInterval->fT0) / fDx || !std::isfinite(fAdvX));
fmalitabc590c02016-02-22 09:12:33 -0800330 return fAdvX;
331 }
332
333 bool currentRampIsZero() const { return fZeroRamp; }
334 const Sk4f& currentColor() const { return fCc; }
335 const Sk4f& currentColorGrad() const { return fDcDx; }
336
337 void advance(SkScalar advX) {
338 SkASSERT(advX > 0);
339 SkASSERT(fAdvX >= 0);
340
341 if (advX >= fAdvX) {
342 advX = this->advance_interval(advX);
343 }
344 SkASSERT(advX < fAdvX);
345
346 fCc = fCc + fDcDx * Sk4f(advX);
347 fAdvX -= advX;
348 }
349
350private:
351 void compute_interval_props(SkScalar t) {
Florin Malitacf20f782017-04-07 14:56:14 -0400352 SkASSERT(in_range(t, fInterval->fT0, fInterval->fT1));
353
Florin Malitada4545b2017-03-23 17:04:54 -0400354 fZeroRamp = fIsVertical || fInterval->fZeroRamp;
Florin Malitacf20f782017-04-07 14:56:14 -0400355 fCc = DstTraits<dstType, premul>::load(fInterval->fCb);
fmalita8f457592016-10-21 06:02:22 -0700356
Florin Malitada4545b2017-03-23 17:04:54 -0400357 if (fInterval->fZeroRamp) {
fmalita8f457592016-10-21 06:02:22 -0700358 fDcDx = 0;
359 } else {
Florin Malitacf20f782017-04-07 14:56:14 -0400360 const Sk4f dC = DstTraits<dstType, premul>::load(fInterval->fCg);
fmalita8f457592016-10-21 06:02:22 -0700361 fCc = fCc + dC * Sk4f(t);
362 fDcDx = dC * fDx;
363 }
fmalitabc590c02016-02-22 09:12:33 -0800364 }
365
fmalitaafac5812016-11-01 13:41:34 -0700366 void init_average_props() {
367 fAdvX = SK_ScalarInfinity;
368 fZeroRamp = true;
369 fDcDx = 0;
370 fCc = Sk4f(0);
371
372 // TODO: precompute the average at interval setup time?
373 for (const auto* i = fFirstInterval; i <= fLastInterval; ++i) {
374 // Each interval contributes its average color to the total/weighted average:
375 //
Florin Malitacf20f782017-04-07 14:56:14 -0400376 // C = (c0 + c1) / 2 = (Cb + Cg * t0 + Cb + Cg * t1) / 2 = Cb + Cg *(t0 + t1) / 2
fmalitaafac5812016-11-01 13:41:34 -0700377 //
Florin Malitacf20f782017-04-07 14:56:14 -0400378 // Avg += C * (t1 - t0)
fmalitaafac5812016-11-01 13:41:34 -0700379 //
Florin Malitacf20f782017-04-07 14:56:14 -0400380 auto c = DstTraits<dstType, premul>::load(i->fCb);
fmalitaafac5812016-11-01 13:41:34 -0700381 if (!i->fZeroRamp) {
Florin Malitacf20f782017-04-07 14:56:14 -0400382 c = c + DstTraits<dstType, premul>::load(i->fCg) * (i->fT0 + i->fT1) * 0.5f;
fmalitaafac5812016-11-01 13:41:34 -0700383 }
Florin Malitacf20f782017-04-07 14:56:14 -0400384 fCc = fCc + c * (i->fT1 - i->fT0);
fmalitaafac5812016-11-01 13:41:34 -0700385 }
386 }
387
Florin Malitada4545b2017-03-23 17:04:54 -0400388 const Sk4fGradientInterval* next_interval(const Sk4fGradientInterval* i) const {
fmalitabc590c02016-02-22 09:12:33 -0800389 SkASSERT(i >= fFirstInterval);
390 SkASSERT(i <= fLastInterval);
391 i++;
392
393 if (tileMode == kClamp_TileMode) {
394 SkASSERT(i <= fLastInterval);
395 return i;
396 }
397
398 return (i <= fLastInterval) ? i : fFirstInterval;
399 }
400
401 SkScalar advance_interval(SkScalar advX) {
402 SkASSERT(advX >= fAdvX);
403
404 do {
405 advX -= fAdvX;
406 fInterval = this->next_interval(fInterval);
Florin Malitacf20f782017-04-07 14:56:14 -0400407 fAdvX = (fInterval->fT1 - fInterval->fT0) / fDx;
fmalitabc590c02016-02-22 09:12:33 -0800408 SkASSERT(fAdvX > 0);
409 } while (advX >= fAdvX);
410
Florin Malitacf20f782017-04-07 14:56:14 -0400411 compute_interval_props(fInterval->fT0);
fmalitabc590c02016-02-22 09:12:33 -0800412
413 SkASSERT(advX >= 0);
414 return advX;
415 }
416
fmalitabc590c02016-02-22 09:12:33 -0800417 // Current interval properties.
fmalitabc590c02016-02-22 09:12:33 -0800418 Sk4f fDcDx; // dst color gradient (dc/dx)
419 Sk4f fCc; // current color, interpolated in dst
420 SkScalar fAdvX; // remaining interval advance in dst
421 bool fZeroRamp; // current interval color grad is 0
422
Florin Malitada4545b2017-03-23 17:04:54 -0400423 const Sk4fGradientInterval* fFirstInterval;
424 const Sk4fGradientInterval* fLastInterval;
425 const Sk4fGradientInterval* fInterval; // current interval
426 const SkScalar fDx; // 'dx' for consistency with other impls; actually dt/dx
427 const bool fIsVertical;
fmalitabc590c02016-02-22 09:12:33 -0800428};
fmalita7e6fcf82016-03-10 11:18:43 -0800429
430void SkLinearGradient::
431LinearGradient4fContext::mapTs(int x, int y, SkScalar ts[], int count) const {
432 SkASSERT(count > 0);
433 SkASSERT(fDstToPosClass != kLinear_MatrixClass);
434
435 SkScalar sx = x + SK_ScalarHalf;
436 const SkScalar sy = y + SK_ScalarHalf;
437 SkPoint pt;
438
439 if (fDstToPosClass != kPerspective_MatrixClass) {
440 // kLinear_MatrixClass, kFixedStepInX_MatrixClass => fixed dt per scanline
441 const SkScalar dtdx = fDstToPos.fixedStepInX(sy).x();
442 fDstToPosProc(fDstToPos, sx, sy, &pt);
443
444 const Sk4f dtdx4 = Sk4f(4 * dtdx);
445 Sk4f t4 = Sk4f(pt.x() + 0 * dtdx,
446 pt.x() + 1 * dtdx,
447 pt.x() + 2 * dtdx,
448 pt.x() + 3 * dtdx);
449
450 while (count >= 4) {
451 t4.store(ts);
452 t4 = t4 + dtdx4;
453 ts += 4;
454 count -= 4;
455 }
456
457 if (count & 2) {
458 *ts++ = t4[0];
459 *ts++ = t4[1];
460 t4 = SkNx_shuffle<2, 0, 1, 3>(t4);
461 }
462
463 if (count & 1) {
464 *ts++ = t4[0];
465 }
466 } else {
467 for (int i = 0; i < count; ++i) {
468 fDstToPosProc(fDstToPos, sx, sy, &pt);
Florin Malita52bab302017-02-08 17:03:56 -0500469 // Perspective may yield NaN values.
470 // Short of a better idea, drop to 0.
471 ts[i] = SkScalarIsNaN(pt.x()) ? 0 : pt.x();
fmalita7e6fcf82016-03-10 11:18:43 -0800472 sx += SK_Scalar1;
473 }
474 }
475}
fmalitaa928b282016-03-18 10:28:23 -0700476
reed58fc94e2016-03-18 12:42:26 -0700477bool SkLinearGradient::LinearGradient4fContext::onChooseBlitProcs(const SkImageInfo& info,
478 BlitState* state) {
Mike Reed6a015542016-11-09 10:38:09 -0500479 if (state->fMode != SkBlendMode::kSrc &&
480 !(state->fMode == SkBlendMode::kSrcOver && (fFlags & kOpaqueAlpha_Flag))) {
reed58fc94e2016-03-18 12:42:26 -0700481 return false;
fmalitaa928b282016-03-18 10:28:23 -0700482 }
483
484 switch (info.colorType()) {
485 case kN32_SkColorType:
reed58fc94e2016-03-18 12:42:26 -0700486 state->fBlitBW = D32_BlitBW;
487 return true;
fmalitaa928b282016-03-18 10:28:23 -0700488 case kRGBA_F16_SkColorType:
reed58fc94e2016-03-18 12:42:26 -0700489 state->fBlitBW = D64_BlitBW;
490 return true;
fmalitaa928b282016-03-18 10:28:23 -0700491 default:
reed58fc94e2016-03-18 12:42:26 -0700492 return false;
fmalitaa928b282016-03-18 10:28:23 -0700493 }
494}
495
496void SkLinearGradient::
reed58fc94e2016-03-18 12:42:26 -0700497LinearGradient4fContext::D32_BlitBW(BlitState* state, int x, int y, const SkPixmap& dst,
498 int count) {
fmalitaa928b282016-03-18 10:28:23 -0700499 // FIXME: ignoring coverage for now
500 const LinearGradient4fContext* ctx =
501 static_cast<const LinearGradient4fContext*>(state->fCtx);
502
reeddabe5d32016-06-21 10:28:14 -0700503 if (!dst.info().gammaCloseToSRGB()) {
fmalitaa928b282016-03-18 10:28:23 -0700504 if (ctx->fColorsArePremul) {
fmalitadc6c9bf2016-03-21 13:16:51 -0700505 ctx->shadePremulSpan<DstType::L32, ApplyPremul::False>(
fmalitaa928b282016-03-18 10:28:23 -0700506 x, y, dst.writable_addr32(x, y), count);
507 } else {
fmalitadc6c9bf2016-03-21 13:16:51 -0700508 ctx->shadePremulSpan<DstType::L32, ApplyPremul::True>(
fmalitaa928b282016-03-18 10:28:23 -0700509 x, y, dst.writable_addr32(x, y), count);
510 }
511 } else {
512 if (ctx->fColorsArePremul) {
fmalitadc6c9bf2016-03-21 13:16:51 -0700513 ctx->shadePremulSpan<DstType::S32, ApplyPremul::False>(
fmalitaa928b282016-03-18 10:28:23 -0700514 x, y, dst.writable_addr32(x, y), count);
515 } else {
fmalitadc6c9bf2016-03-21 13:16:51 -0700516 ctx->shadePremulSpan<DstType::S32, ApplyPremul::True>(
fmalitaa928b282016-03-18 10:28:23 -0700517 x, y, dst.writable_addr32(x, y), count);
518 }
519 }
520}
521
522void SkLinearGradient::
reed58fc94e2016-03-18 12:42:26 -0700523LinearGradient4fContext::D64_BlitBW(BlitState* state, int x, int y, const SkPixmap& dst,
524 int count) {
fmalitaa928b282016-03-18 10:28:23 -0700525 // FIXME: ignoring coverage for now
526 const LinearGradient4fContext* ctx =
527 static_cast<const LinearGradient4fContext*>(state->fCtx);
528
529 if (ctx->fColorsArePremul) {
fmalitadc6c9bf2016-03-21 13:16:51 -0700530 ctx->shadePremulSpan<DstType::F16, ApplyPremul::False>(
fmalitaa928b282016-03-18 10:28:23 -0700531 x, y, dst.writable_addr64(x, y), count);
532 } else {
fmalitadc6c9bf2016-03-21 13:16:51 -0700533 ctx->shadePremulSpan<DstType::F16, ApplyPremul::True>(
fmalitaa928b282016-03-18 10:28:23 -0700534 x, y, dst.writable_addr64(x, y), count);
535 }
536}