blob: 49b98fd86e4a4bd9409ba48edc463b19d1380797 [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 Malita63f717d2017-05-08 09:53:11 -0400354 const Sk4f dc = DstTraits<dstType, premul>::load(fInterval->fCg);
355 fCc = DstTraits<dstType, premul>::load(fInterval->fCb) + dc * Sk4f(t);
356 fDcDx = dc * fDx;
357 fZeroRamp = fIsVertical || (dc == 0).allTrue();
fmalitabc590c02016-02-22 09:12:33 -0800358 }
359
fmalitaafac5812016-11-01 13:41:34 -0700360 void init_average_props() {
361 fAdvX = SK_ScalarInfinity;
362 fZeroRamp = true;
363 fDcDx = 0;
364 fCc = Sk4f(0);
365
366 // TODO: precompute the average at interval setup time?
367 for (const auto* i = fFirstInterval; i <= fLastInterval; ++i) {
368 // Each interval contributes its average color to the total/weighted average:
369 //
Florin Malitacf20f782017-04-07 14:56:14 -0400370 // C = (c0 + c1) / 2 = (Cb + Cg * t0 + Cb + Cg * t1) / 2 = Cb + Cg *(t0 + t1) / 2
fmalitaafac5812016-11-01 13:41:34 -0700371 //
Florin Malitacf20f782017-04-07 14:56:14 -0400372 // Avg += C * (t1 - t0)
fmalitaafac5812016-11-01 13:41:34 -0700373 //
Florin Malita63f717d2017-05-08 09:53:11 -0400374 const auto c = DstTraits<dstType, premul>::load(i->fCb)
375 + DstTraits<dstType, premul>::load(i->fCg) * (i->fT0 + i->fT1) * 0.5f;
Florin Malitacf20f782017-04-07 14:56:14 -0400376 fCc = fCc + c * (i->fT1 - i->fT0);
fmalitaafac5812016-11-01 13:41:34 -0700377 }
378 }
379
Florin Malitada4545b2017-03-23 17:04:54 -0400380 const Sk4fGradientInterval* next_interval(const Sk4fGradientInterval* i) const {
fmalitabc590c02016-02-22 09:12:33 -0800381 SkASSERT(i >= fFirstInterval);
382 SkASSERT(i <= fLastInterval);
383 i++;
384
385 if (tileMode == kClamp_TileMode) {
386 SkASSERT(i <= fLastInterval);
387 return i;
388 }
389
390 return (i <= fLastInterval) ? i : fFirstInterval;
391 }
392
393 SkScalar advance_interval(SkScalar advX) {
394 SkASSERT(advX >= fAdvX);
395
396 do {
397 advX -= fAdvX;
398 fInterval = this->next_interval(fInterval);
Florin Malitacf20f782017-04-07 14:56:14 -0400399 fAdvX = (fInterval->fT1 - fInterval->fT0) / fDx;
fmalitabc590c02016-02-22 09:12:33 -0800400 SkASSERT(fAdvX > 0);
401 } while (advX >= fAdvX);
402
Florin Malitacf20f782017-04-07 14:56:14 -0400403 compute_interval_props(fInterval->fT0);
fmalitabc590c02016-02-22 09:12:33 -0800404
405 SkASSERT(advX >= 0);
406 return advX;
407 }
408
fmalitabc590c02016-02-22 09:12:33 -0800409 // Current interval properties.
fmalitabc590c02016-02-22 09:12:33 -0800410 Sk4f fDcDx; // dst color gradient (dc/dx)
411 Sk4f fCc; // current color, interpolated in dst
412 SkScalar fAdvX; // remaining interval advance in dst
413 bool fZeroRamp; // current interval color grad is 0
414
Florin Malitada4545b2017-03-23 17:04:54 -0400415 const Sk4fGradientInterval* fFirstInterval;
416 const Sk4fGradientInterval* fLastInterval;
417 const Sk4fGradientInterval* fInterval; // current interval
418 const SkScalar fDx; // 'dx' for consistency with other impls; actually dt/dx
419 const bool fIsVertical;
fmalitabc590c02016-02-22 09:12:33 -0800420};
fmalita7e6fcf82016-03-10 11:18:43 -0800421
422void SkLinearGradient::
423LinearGradient4fContext::mapTs(int x, int y, SkScalar ts[], int count) const {
424 SkASSERT(count > 0);
425 SkASSERT(fDstToPosClass != kLinear_MatrixClass);
426
427 SkScalar sx = x + SK_ScalarHalf;
428 const SkScalar sy = y + SK_ScalarHalf;
429 SkPoint pt;
430
431 if (fDstToPosClass != kPerspective_MatrixClass) {
432 // kLinear_MatrixClass, kFixedStepInX_MatrixClass => fixed dt per scanline
433 const SkScalar dtdx = fDstToPos.fixedStepInX(sy).x();
434 fDstToPosProc(fDstToPos, sx, sy, &pt);
435
436 const Sk4f dtdx4 = Sk4f(4 * dtdx);
437 Sk4f t4 = Sk4f(pt.x() + 0 * dtdx,
438 pt.x() + 1 * dtdx,
439 pt.x() + 2 * dtdx,
440 pt.x() + 3 * dtdx);
441
442 while (count >= 4) {
443 t4.store(ts);
444 t4 = t4 + dtdx4;
445 ts += 4;
446 count -= 4;
447 }
448
449 if (count & 2) {
450 *ts++ = t4[0];
451 *ts++ = t4[1];
452 t4 = SkNx_shuffle<2, 0, 1, 3>(t4);
453 }
454
455 if (count & 1) {
456 *ts++ = t4[0];
457 }
458 } else {
459 for (int i = 0; i < count; ++i) {
460 fDstToPosProc(fDstToPos, sx, sy, &pt);
Florin Malita52bab302017-02-08 17:03:56 -0500461 // Perspective may yield NaN values.
462 // Short of a better idea, drop to 0.
463 ts[i] = SkScalarIsNaN(pt.x()) ? 0 : pt.x();
fmalita7e6fcf82016-03-10 11:18:43 -0800464 sx += SK_Scalar1;
465 }
466 }
467}
fmalitaa928b282016-03-18 10:28:23 -0700468
reed58fc94e2016-03-18 12:42:26 -0700469bool SkLinearGradient::LinearGradient4fContext::onChooseBlitProcs(const SkImageInfo& info,
470 BlitState* state) {
Mike Reed6a015542016-11-09 10:38:09 -0500471 if (state->fMode != SkBlendMode::kSrc &&
472 !(state->fMode == SkBlendMode::kSrcOver && (fFlags & kOpaqueAlpha_Flag))) {
reed58fc94e2016-03-18 12:42:26 -0700473 return false;
fmalitaa928b282016-03-18 10:28:23 -0700474 }
475
476 switch (info.colorType()) {
477 case kN32_SkColorType:
reed58fc94e2016-03-18 12:42:26 -0700478 state->fBlitBW = D32_BlitBW;
479 return true;
fmalitaa928b282016-03-18 10:28:23 -0700480 case kRGBA_F16_SkColorType:
reed58fc94e2016-03-18 12:42:26 -0700481 state->fBlitBW = D64_BlitBW;
482 return true;
fmalitaa928b282016-03-18 10:28:23 -0700483 default:
reed58fc94e2016-03-18 12:42:26 -0700484 return false;
fmalitaa928b282016-03-18 10:28:23 -0700485 }
486}
487
488void SkLinearGradient::
reed58fc94e2016-03-18 12:42:26 -0700489LinearGradient4fContext::D32_BlitBW(BlitState* state, int x, int y, const SkPixmap& dst,
490 int count) {
fmalitaa928b282016-03-18 10:28:23 -0700491 // FIXME: ignoring coverage for now
492 const LinearGradient4fContext* ctx =
493 static_cast<const LinearGradient4fContext*>(state->fCtx);
494
reeddabe5d32016-06-21 10:28:14 -0700495 if (!dst.info().gammaCloseToSRGB()) {
fmalitaa928b282016-03-18 10:28:23 -0700496 if (ctx->fColorsArePremul) {
fmalitadc6c9bf2016-03-21 13:16:51 -0700497 ctx->shadePremulSpan<DstType::L32, ApplyPremul::False>(
fmalitaa928b282016-03-18 10:28:23 -0700498 x, y, dst.writable_addr32(x, y), count);
499 } else {
fmalitadc6c9bf2016-03-21 13:16:51 -0700500 ctx->shadePremulSpan<DstType::L32, ApplyPremul::True>(
fmalitaa928b282016-03-18 10:28:23 -0700501 x, y, dst.writable_addr32(x, y), count);
502 }
503 } else {
504 if (ctx->fColorsArePremul) {
fmalitadc6c9bf2016-03-21 13:16:51 -0700505 ctx->shadePremulSpan<DstType::S32, 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::S32, ApplyPremul::True>(
fmalitaa928b282016-03-18 10:28:23 -0700509 x, y, dst.writable_addr32(x, y), count);
510 }
511 }
512}
513
514void SkLinearGradient::
reed58fc94e2016-03-18 12:42:26 -0700515LinearGradient4fContext::D64_BlitBW(BlitState* state, int x, int y, const SkPixmap& dst,
516 int count) {
fmalitaa928b282016-03-18 10:28:23 -0700517 // FIXME: ignoring coverage for now
518 const LinearGradient4fContext* ctx =
519 static_cast<const LinearGradient4fContext*>(state->fCtx);
520
521 if (ctx->fColorsArePremul) {
fmalitadc6c9bf2016-03-21 13:16:51 -0700522 ctx->shadePremulSpan<DstType::F16, ApplyPremul::False>(
fmalitaa928b282016-03-18 10:28:23 -0700523 x, y, dst.writable_addr64(x, y), count);
524 } else {
fmalitadc6c9bf2016-03-21 13:16:51 -0700525 ctx->shadePremulSpan<DstType::F16, ApplyPremul::True>(
fmalitaa928b282016-03-18 10:28:23 -0700526 x, y, dst.writable_addr64(x, y), count);
527 }
528}