blob: 700b7fb13e8b445ee670e394de4aa33bf2caf9f5 [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.
Florin Malita4d41b8f2017-07-12 14:35:46 -040094 const bool reverseIntervals = 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::
fmalitabc590c02016-02-22 09:12:33 -0800145LinearGradient4fContext::shadeSpan4f(int x, int y, SkPM4f dst[], int count) {
fmalitabc590c02016-02-22 09:12:33 -0800146 SkASSERT(count > 0);
147 if (fColorsArePremul) {
Florin Malitaae7bb042017-06-19 15:27:47 -0400148 this->shadePremulSpan<ApplyPremul::False>(x, y, dst, count);
fmalitabc590c02016-02-22 09:12:33 -0800149 } else {
Florin Malitaae7bb042017-06-19 15:27:47 -0400150 this->shadePremulSpan<ApplyPremul::True>(x, y, dst, count);
fmalitabc590c02016-02-22 09:12:33 -0800151 }
152}
153
Florin Malitaae7bb042017-06-19 15:27:47 -0400154template<ApplyPremul premul>
fmalitabc590c02016-02-22 09:12:33 -0800155void SkLinearGradient::
Florin Malitaae7bb042017-06-19 15:27:47 -0400156LinearGradient4fContext::shadePremulSpan(int x, int y, SkPM4f dst[], int count) const {
fmalitabc590c02016-02-22 09:12:33 -0800157 const SkLinearGradient& shader =
158 static_cast<const SkLinearGradient&>(fShader);
159 switch (shader.fTileMode) {
160 case kClamp_TileMode:
Florin Malitaae7bb042017-06-19 15:27:47 -0400161 this->shadeSpanInternal<premul, kClamp_TileMode>(x, y, dst, count);
fmalitabc590c02016-02-22 09:12:33 -0800162 break;
163 case kRepeat_TileMode:
Florin Malitaae7bb042017-06-19 15:27:47 -0400164 this->shadeSpanInternal<premul, kRepeat_TileMode>(x, y, dst, count);
fmalitabc590c02016-02-22 09:12:33 -0800165 break;
166 case kMirror_TileMode:
Florin Malitaae7bb042017-06-19 15:27:47 -0400167 this->shadeSpanInternal<premul, kMirror_TileMode>(x, y, dst, count);
fmalitabc590c02016-02-22 09:12:33 -0800168 break;
169 }
170}
171
Florin Malitaae7bb042017-06-19 15:27:47 -0400172template<ApplyPremul premul, SkShader::TileMode tileMode>
fmalitabc590c02016-02-22 09:12:33 -0800173void SkLinearGradient::
Florin Malitaae7bb042017-06-19 15:27:47 -0400174LinearGradient4fContext::shadeSpanInternal(int x, int y, SkPM4f dst[], int count) const {
fmalitabc590c02016-02-22 09:12:33 -0800175 SkPoint pt;
176 fDstToPosProc(fDstToPos,
177 x + SK_ScalarHalf,
178 y + SK_ScalarHalf,
179 &pt);
180 const SkScalar fx = pinFx<tileMode>(pt.x());
181 const SkScalar dx = fDstToPos.getScaleX();
Florin Malitaae7bb042017-06-19 15:27:47 -0400182 LinearIntervalProcessor<premul, tileMode> proc(fIntervals->begin(),
183 fIntervals->end() - 1,
184 this->findInterval(fx),
185 fx,
186 dx,
187 SkScalarNearlyZero(dx * count));
fmalitabc590c02016-02-22 09:12:33 -0800188 while (count > 0) {
189 // What we really want here is SkTPin(advance, 1, count)
190 // but that's a significant perf hit for >> stops; investigate.
191 const int n = SkScalarTruncToInt(
192 SkTMin<SkScalar>(proc.currentAdvance() + 1, SkIntToScalar(count)));
193
194 // The current interval advance can be +inf (e.g. when reaching
195 // the clamp mode end intervals) - when that happens, we expect to
196 // a) consume all remaining count in one swoop
197 // b) return a zero color gradient
198 SkASSERT(SkScalarIsFinite(proc.currentAdvance())
199 || (n == count && proc.currentRampIsZero()));
200
201 if (proc.currentRampIsZero()) {
Florin Malitaae7bb042017-06-19 15:27:47 -0400202 DstTraits<premul>::store(proc.currentColor(), dst, n);
fmalitabc590c02016-02-22 09:12:33 -0800203 } else {
Florin Malitaae7bb042017-06-19 15:27:47 -0400204 ramp<premul>(proc.currentColor(), proc.currentColorGrad(), dst, n);
fmalitabc590c02016-02-22 09:12:33 -0800205 }
206
207 proc.advance(SkIntToScalar(n));
208 count -= n;
209 dst += n;
210 }
211}
212
Florin Malitaae7bb042017-06-19 15:27:47 -0400213template<ApplyPremul premul, SkShader::TileMode tileMode>
fmalitabc590c02016-02-22 09:12:33 -0800214class SkLinearGradient::
215LinearGradient4fContext::LinearIntervalProcessor {
216public:
Florin Malitada4545b2017-03-23 17:04:54 -0400217 LinearIntervalProcessor(const Sk4fGradientInterval* firstInterval,
218 const Sk4fGradientInterval* lastInterval,
219 const Sk4fGradientInterval* i,
fmalitabc590c02016-02-22 09:12:33 -0800220 SkScalar fx,
221 SkScalar dx,
222 bool is_vertical)
Florin Malitacf20f782017-04-07 14:56:14 -0400223 : fAdvX(is_vertical ? SK_ScalarInfinity : (i->fT1 - fx) / dx)
fmalitabc590c02016-02-22 09:12:33 -0800224 , fFirstInterval(firstInterval)
225 , fLastInterval(lastInterval)
226 , fInterval(i)
227 , fDx(dx)
228 , fIsVertical(is_vertical)
229 {
fmalita6d7e4e82016-09-20 06:55:16 -0700230 SkASSERT(fAdvX >= 0);
fmalitabc590c02016-02-22 09:12:33 -0800231 SkASSERT(firstInterval <= lastInterval);
fmalitaafac5812016-11-01 13:41:34 -0700232
233 if (tileMode != kClamp_TileMode && !is_vertical) {
Florin Malitacf20f782017-04-07 14:56:14 -0400234 const auto spanX = (lastInterval->fT1 - firstInterval->fT0) / dx;
fmalitaafac5812016-11-01 13:41:34 -0700235 SkASSERT(spanX >= 0);
236
237 // If we're in a repeating tile mode and the whole gradient is compressed into a
238 // fraction of a pixel, we just use the average color in zero-ramp mode.
239 // This also avoids cases where we make no progress due to interval advances being
240 // close to zero.
241 static constexpr SkScalar kMinSpanX = .25f;
242 if (spanX < kMinSpanX) {
243 this->init_average_props();
244 return;
245 }
246 }
247
Florin Malitacf20f782017-04-07 14:56:14 -0400248 this->compute_interval_props(fx);
fmalitabc590c02016-02-22 09:12:33 -0800249 }
250
251 SkScalar currentAdvance() const {
252 SkASSERT(fAdvX >= 0);
Florin Malitacf20f782017-04-07 14:56:14 -0400253 SkASSERT(fAdvX <= (fInterval->fT1 - fInterval->fT0) / fDx || !std::isfinite(fAdvX));
fmalitabc590c02016-02-22 09:12:33 -0800254 return fAdvX;
255 }
256
257 bool currentRampIsZero() const { return fZeroRamp; }
258 const Sk4f& currentColor() const { return fCc; }
259 const Sk4f& currentColorGrad() const { return fDcDx; }
260
261 void advance(SkScalar advX) {
262 SkASSERT(advX > 0);
263 SkASSERT(fAdvX >= 0);
264
265 if (advX >= fAdvX) {
266 advX = this->advance_interval(advX);
267 }
268 SkASSERT(advX < fAdvX);
269
270 fCc = fCc + fDcDx * Sk4f(advX);
271 fAdvX -= advX;
272 }
273
274private:
275 void compute_interval_props(SkScalar t) {
Florin Malitacf20f782017-04-07 14:56:14 -0400276 SkASSERT(in_range(t, fInterval->fT0, fInterval->fT1));
277
Florin Malitaae7bb042017-06-19 15:27:47 -0400278 const Sk4f dc = DstTraits<premul>::load(fInterval->fCg);
279 fCc = DstTraits<premul>::load(fInterval->fCb) + dc * Sk4f(t);
Florin Malita63f717d2017-05-08 09:53:11 -0400280 fDcDx = dc * fDx;
281 fZeroRamp = fIsVertical || (dc == 0).allTrue();
fmalitabc590c02016-02-22 09:12:33 -0800282 }
283
fmalitaafac5812016-11-01 13:41:34 -0700284 void init_average_props() {
285 fAdvX = SK_ScalarInfinity;
286 fZeroRamp = true;
287 fDcDx = 0;
288 fCc = Sk4f(0);
289
290 // TODO: precompute the average at interval setup time?
291 for (const auto* i = fFirstInterval; i <= fLastInterval; ++i) {
292 // Each interval contributes its average color to the total/weighted average:
293 //
Florin Malitacf20f782017-04-07 14:56:14 -0400294 // C = (c0 + c1) / 2 = (Cb + Cg * t0 + Cb + Cg * t1) / 2 = Cb + Cg *(t0 + t1) / 2
fmalitaafac5812016-11-01 13:41:34 -0700295 //
Florin Malitacf20f782017-04-07 14:56:14 -0400296 // Avg += C * (t1 - t0)
fmalitaafac5812016-11-01 13:41:34 -0700297 //
Florin Malitaae7bb042017-06-19 15:27:47 -0400298 const auto c = DstTraits<premul>::load(i->fCb)
299 + DstTraits<premul>::load(i->fCg) * (i->fT0 + i->fT1) * 0.5f;
Florin Malitacf20f782017-04-07 14:56:14 -0400300 fCc = fCc + c * (i->fT1 - i->fT0);
fmalitaafac5812016-11-01 13:41:34 -0700301 }
302 }
303
Florin Malitada4545b2017-03-23 17:04:54 -0400304 const Sk4fGradientInterval* next_interval(const Sk4fGradientInterval* i) const {
fmalitabc590c02016-02-22 09:12:33 -0800305 SkASSERT(i >= fFirstInterval);
306 SkASSERT(i <= fLastInterval);
307 i++;
308
309 if (tileMode == kClamp_TileMode) {
310 SkASSERT(i <= fLastInterval);
311 return i;
312 }
313
314 return (i <= fLastInterval) ? i : fFirstInterval;
315 }
316
317 SkScalar advance_interval(SkScalar advX) {
318 SkASSERT(advX >= fAdvX);
319
320 do {
321 advX -= fAdvX;
322 fInterval = this->next_interval(fInterval);
Florin Malitacf20f782017-04-07 14:56:14 -0400323 fAdvX = (fInterval->fT1 - fInterval->fT0) / fDx;
fmalitabc590c02016-02-22 09:12:33 -0800324 SkASSERT(fAdvX > 0);
325 } while (advX >= fAdvX);
326
Florin Malitacf20f782017-04-07 14:56:14 -0400327 compute_interval_props(fInterval->fT0);
fmalitabc590c02016-02-22 09:12:33 -0800328
329 SkASSERT(advX >= 0);
330 return advX;
331 }
332
fmalitabc590c02016-02-22 09:12:33 -0800333 // Current interval properties.
fmalitabc590c02016-02-22 09:12:33 -0800334 Sk4f fDcDx; // dst color gradient (dc/dx)
335 Sk4f fCc; // current color, interpolated in dst
336 SkScalar fAdvX; // remaining interval advance in dst
337 bool fZeroRamp; // current interval color grad is 0
338
Florin Malitada4545b2017-03-23 17:04:54 -0400339 const Sk4fGradientInterval* fFirstInterval;
340 const Sk4fGradientInterval* fLastInterval;
341 const Sk4fGradientInterval* fInterval; // current interval
342 const SkScalar fDx; // 'dx' for consistency with other impls; actually dt/dx
343 const bool fIsVertical;
fmalitabc590c02016-02-22 09:12:33 -0800344};