blob: f803d5e6505001220533f2fc442a9c0a3fd13453 [file] [log] [blame]
rileya@google.com589708b2012-07-26 20:04:23 +00001/*
2 * Copyright 2012 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
fmalitabc590c02016-02-22 09:12:33 -08008#include "Sk4fLinearGradient.h"
Matt Sarett6cc6ae752017-04-18 18:29:12 -04009#include "SkColorSpaceXformer.h"
rileya@google.com589708b2012-07-26 20:04:23 +000010#include "SkLinearGradient.h"
bungeman06ca8ec2016-06-09 08:01:03 -070011#include "SkRefCnt.h"
rileya@google.com589708b2012-07-26 20:04:23 +000012
fmalitabc590c02016-02-22 09:12:33 -080013// define to test the 4f gradient path
fmalita55430a62016-02-23 13:26:28 -080014// #define FORCE_4F_CONTEXT
fmalitabc590c02016-02-22 09:12:33 -080015
reedf3182eb2015-11-17 08:12:19 -080016static const float kInv255Float = 1.0f / 255;
17
rileya@google.com589708b2012-07-26 20:04:23 +000018static inline int repeat_8bits(int x) {
19 return x & 0xFF;
20}
21
rileya@google.com589708b2012-07-26 20:04:23 +000022static inline int mirror_8bits(int x) {
rileya@google.com589708b2012-07-26 20:04:23 +000023 if (x & 256) {
24 x = ~x;
25 }
26 return x & 255;
rileya@google.com589708b2012-07-26 20:04:23 +000027}
28
mtkleincc695fe2014-12-10 10:29:19 -080029static SkMatrix pts_to_unit_matrix(const SkPoint pts[2]) {
rileya@google.com589708b2012-07-26 20:04:23 +000030 SkVector vec = pts[1] - pts[0];
31 SkScalar mag = vec.length();
32 SkScalar inv = mag ? SkScalarInvert(mag) : 0;
33
34 vec.scale(inv);
mtkleincc695fe2014-12-10 10:29:19 -080035 SkMatrix matrix;
36 matrix.setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY);
37 matrix.postTranslate(-pts[0].fX, -pts[0].fY);
38 matrix.postScale(inv, inv);
39 return matrix;
rileya@google.com589708b2012-07-26 20:04:23 +000040}
41
Florin Malita4aed1382017-05-25 10:38:07 -040042static bool use_4f_context(const SkShaderBase::ContextRec& rec, uint32_t flags) {
fmalita55430a62016-02-23 13:26:28 -080043#ifdef FORCE_4F_CONTEXT
fmalitabc590c02016-02-22 09:12:33 -080044 return true;
45#else
Florin Malitad1aedde2017-06-07 15:03:38 -040046 return rec.fPreferredDstType == SkShaderBase::ContextRec::kPM4f_DstType;
fmalitabc590c02016-02-22 09:12:33 -080047#endif
48}
49
rileya@google.com589708b2012-07-26 20:04:23 +000050///////////////////////////////////////////////////////////////////////////////
51
reedaddf2ed2014-08-11 08:28:24 -070052SkLinearGradient::SkLinearGradient(const SkPoint pts[2], const Descriptor& desc)
mtkleincc695fe2014-12-10 10:29:19 -080053 : SkGradientShaderBase(desc, pts_to_unit_matrix(pts))
rileya@google.com589708b2012-07-26 20:04:23 +000054 , fStart(pts[0])
mtkleincc695fe2014-12-10 10:29:19 -080055 , fEnd(pts[1]) {
rileya@google.com589708b2012-07-26 20:04:23 +000056}
57
reed60c9b582016-04-03 09:11:13 -070058sk_sp<SkFlattenable> SkLinearGradient::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -070059 DescriptorScope desc;
60 if (!desc.unflatten(buffer)) {
halcanary96fcdcc2015-08-27 07:41:13 -070061 return nullptr;
reed9fa60da2014-08-21 07:59:51 -070062 }
63 SkPoint pts[2];
64 pts[0] = buffer.readPoint();
65 pts[1] = buffer.readPoint();
brianosmane25d71c2016-09-28 11:27:28 -070066 return SkGradientShader::MakeLinear(pts, desc.fColors, std::move(desc.fColorSpace), desc.fPos,
67 desc.fCount, desc.fTileMode, desc.fGradFlags,
68 desc.fLocalMatrix);
reed9fa60da2014-08-21 07:59:51 -070069}
rileya@google.com589708b2012-07-26 20:04:23 +000070
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000071void SkLinearGradient::flatten(SkWriteBuffer& buffer) const {
rileya@google.com589708b2012-07-26 20:04:23 +000072 this->INHERITED::flatten(buffer);
73 buffer.writePoint(fStart);
74 buffer.writePoint(fEnd);
75}
76
Florin Malita4aed1382017-05-25 10:38:07 -040077SkShaderBase::Context* SkLinearGradient::onMakeContext(
Herb Derby83e939b2017-02-07 14:25:11 -050078 const ContextRec& rec, SkArenaAlloc* alloc) const
79{
fmalita55430a62016-02-23 13:26:28 -080080 return use_4f_context(rec, fGradFlags)
Herb Derby83e939b2017-02-07 14:25:11 -050081 ? CheckedMakeContext<LinearGradient4fContext>(alloc, *this, rec)
82 : CheckedMakeContext< LinearGradientContext>(alloc, *this, rec);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000083}
84
Florin Malita47e55a52017-06-06 12:26:54 -040085SkShaderBase::Context* SkLinearGradient::onMakeBurstPipelineContext(
86 const ContextRec& rec, SkArenaAlloc* alloc) const {
87
Florin Malitaa924dd32017-06-07 14:23:55 -040088 // Raster pipeline has a 2-stop specialization faster than our burst.
89 return fColorCount > 2 ? CheckedMakeContext<LinearGradient4fContext>(alloc, *this, rec)
90 : nullptr;
Florin Malita47e55a52017-06-06 12:26:54 -040091}
92
Mike Kleina3771842017-05-04 19:38:48 -040093bool SkLinearGradient::adjustMatrixAndAppendStages(SkArenaAlloc* alloc,
94 SkMatrix* matrix,
95 SkRasterPipeline* p) const {
96 *matrix = SkMatrix::Concat(fPtsToUnit, *matrix);
97 // If the gradient is less than a quarter of a pixel, this falls into the
98 // subpixel gradient code handled on a different path.
99 SkVector dx = matrix->mapVector(1, 0);
Mike Kleine7598532017-05-11 11:29:29 -0400100 if (dx.fX >= 4) {
101 return false;
Herb Derby7b4202d2017-04-10 10:52:34 -0400102 }
Florin Malitac86e4702017-01-20 08:41:34 -0500103 return true;
104}
105
Matt Sarett6cc6ae752017-04-18 18:29:12 -0400106sk_sp<SkShader> SkLinearGradient::onMakeColorSpace(SkColorSpaceXformer* xformer) const {
107 SkPoint pts[2] = { fStart, fEnd };
108 SkSTArray<8, SkColor> xformedColors(fColorCount);
109 xformer->apply(xformedColors.begin(), fOrigColors, fColorCount);
110 return SkGradientShader::MakeLinear(pts, xformedColors.begin(), fOrigPos, fColorCount,
111 fTileMode, fGradFlags, &this->getLocalMatrix());
112}
113
reedf3182eb2015-11-17 08:12:19 -0800114// This swizzles SkColor into the same component order as SkPMColor, but does not actually
115// "pre" multiply the color components.
116//
117// This allows us to map directly to Sk4f, and eventually scale down to bytes to output a
118// SkPMColor from the floats, without having to swizzle each time.
119//
120static uint32_t SkSwizzle_Color_to_PMColor(SkColor c) {
121 return SkPackARGB32NoCheck(SkColorGetA(c), SkColorGetR(c), SkColorGetG(c), SkColorGetB(c));
122}
123
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000124SkLinearGradient::LinearGradientContext::LinearGradientContext(
reedf3182eb2015-11-17 08:12:19 -0800125 const SkLinearGradient& shader, const ContextRec& ctx)
126 : INHERITED(shader, ctx)
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000127{
reedf3182eb2015-11-17 08:12:19 -0800128 // setup for Sk4f
fmalita748d6202016-05-11 11:39:58 -0700129 const int count = shader.fColorCount;
130 SkASSERT(count > 1);
131
reedf3182eb2015-11-17 08:12:19 -0800132 fRecs.setCount(count);
133 Rec* rec = fRecs.begin();
134 if (shader.fOrigPos) {
135 rec[0].fPos = 0;
136 SkDEBUGCODE(rec[0].fPosScale = SK_FloatNaN;) // should never get used
137 for (int i = 1; i < count; ++i) {
138 rec[i].fPos = SkTPin(shader.fOrigPos[i], rec[i - 1].fPos, 1.0f);
lsalzmanf2b86622016-01-22 14:03:02 -0800139 float diff = rec[i].fPos - rec[i - 1].fPos;
140 if (diff > 0) {
141 rec[i].fPosScale = 1.0f / diff;
142 } else {
143 rec[i].fPosScale = 0;
144 }
reedf3182eb2015-11-17 08:12:19 -0800145 }
reedf3182eb2015-11-17 08:12:19 -0800146 } else {
147 // no pos specified, so we compute evenly spaced values
148 const float scale = float(count - 1);
fmalita748d6202016-05-11 11:39:58 -0700149 const float invScale = 1.0f / scale;
reedf3182eb2015-11-17 08:12:19 -0800150 for (int i = 0; i < count; ++i) {
151 rec[i].fPos = i * invScale;
152 rec[i].fPosScale = scale;
153 }
154 }
fmalita748d6202016-05-11 11:39:58 -0700155 rec[count - 1].fPos = 1; // overwrite the last value just to be sure we end at 1.0
reedf3182eb2015-11-17 08:12:19 -0800156
157 fApplyAlphaAfterInterp = true;
158 if ((shader.getGradFlags() & SkGradientShader::kInterpolateColorsInPremul_Flag) ||
159 shader.colorsAreOpaque())
160 {
161 fApplyAlphaAfterInterp = false;
162 }
163
164 if (fApplyAlphaAfterInterp) {
165 // Our fColor values are in PMColor order, but are still unpremultiplied, allowing us to
166 // interpolate in unpremultiplied space first, and then scale by alpha right before we
167 // convert to SkPMColor bytes.
168 const float paintAlpha = ctx.fPaint->getAlpha() * kInv255Float;
169 const Sk4f scale(1, 1, 1, paintAlpha);
170 for (int i = 0; i < count; ++i) {
171 uint32_t c = SkSwizzle_Color_to_PMColor(shader.fOrigColors[i]);
mtklein507ef6d2016-01-31 08:02:47 -0800172 rec[i].fColor = SkNx_cast<float>(Sk4b::Load(&c)) * scale;
reedf3182eb2015-11-17 08:12:19 -0800173 if (i > 0) {
174 SkASSERT(rec[i - 1].fPos <= rec[i].fPos);
175 }
176 }
177 } else {
178 // Our fColor values are premultiplied, so converting to SkPMColor is just a matter
179 // of converting the floats down to bytes.
180 unsigned alphaScale = ctx.fPaint->getAlpha() + (ctx.fPaint->getAlpha() >> 7);
181 for (int i = 0; i < count; ++i) {
182 SkPMColor pmc = SkPreMultiplyColor(shader.fOrigColors[i]);
183 pmc = SkAlphaMulQ(pmc, alphaScale);
mtklein507ef6d2016-01-31 08:02:47 -0800184 rec[i].fColor = SkNx_cast<float>(Sk4b::Load(&pmc));
reedf3182eb2015-11-17 08:12:19 -0800185 if (i > 0) {
186 SkASSERT(rec[i - 1].fPos <= rec[i].fPos);
187 }
188 }
189 }
rileya@google.com589708b2012-07-26 20:04:23 +0000190}
191
192#define NO_CHECK_ITER \
193 do { \
reedcaf7e932014-12-18 12:43:08 -0800194 unsigned fi = SkGradFixedToFixed(fx) >> SkGradientShaderBase::kCache32Shift; \
rileya@google.com589708b2012-07-26 20:04:23 +0000195 SkASSERT(fi <= 0xFF); \
196 fx += dx; \
197 *dstC++ = cache[toggle + fi]; \
reed@google.com55853db2013-02-01 19:34:59 +0000198 toggle = next_dither_toggle(toggle); \
rileya@google.com589708b2012-07-26 20:04:23 +0000199 } while (0)
200
201namespace {
202
reedcaf7e932014-12-18 12:43:08 -0800203typedef void (*LinearShadeProc)(TileProc proc, SkGradFixed dx, SkGradFixed fx,
rileya@google.com589708b2012-07-26 20:04:23 +0000204 SkPMColor* dstC, const SkPMColor* cache,
205 int toggle, int count);
206
rileya@google.com589708b2012-07-26 20:04:23 +0000207// Linear interpolation (lerp) is unnecessary if there are no sharp
208// discontinuities in the gradient - which must be true if there are
209// only 2 colors - but it's cheap.
reedcaf7e932014-12-18 12:43:08 -0800210void shadeSpan_linear_vertical_lerp(TileProc proc, SkGradFixed dx, SkGradFixed fx,
rileya@google.com589708b2012-07-26 20:04:23 +0000211 SkPMColor* SK_RESTRICT dstC,
212 const SkPMColor* SK_RESTRICT cache,
213 int toggle, int count) {
214 // We're a vertical gradient, so no change in a span.
215 // If colors change sharply across the gradient, dithering is
216 // insufficient (it subsamples the color space) and we need to lerp.
reedcaf7e932014-12-18 12:43:08 -0800217 unsigned fullIndex = proc(SkGradFixedToFixed(fx));
reed@google.com3c2102c2013-02-01 12:59:40 +0000218 unsigned fi = fullIndex >> SkGradientShaderBase::kCache32Shift;
219 unsigned remainder = fullIndex & ((1 << SkGradientShaderBase::kCache32Shift) - 1);
skia.committer@gmail.com9dde0182013-02-04 12:57:42 +0000220
reed@google.com3c2102c2013-02-01 12:59:40 +0000221 int index0 = fi + toggle;
222 int index1 = index0;
223 if (fi < SkGradientShaderBase::kCache32Count - 1) {
224 index1 += 1;
225 }
226 SkPMColor lerp = SkFastFourByteInterp(cache[index1], cache[index0], remainder);
227 index0 ^= SkGradientShaderBase::kDitherStride32;
228 index1 ^= SkGradientShaderBase::kDitherStride32;
229 SkPMColor dlerp = SkFastFourByteInterp(cache[index1], cache[index0], remainder);
rileya@google.com589708b2012-07-26 20:04:23 +0000230 sk_memset32_dither(dstC, lerp, dlerp, count);
231}
232
reedcaf7e932014-12-18 12:43:08 -0800233void shadeSpan_linear_clamp(TileProc proc, SkGradFixed dx, SkGradFixed fx,
rileya@google.com589708b2012-07-26 20:04:23 +0000234 SkPMColor* SK_RESTRICT dstC,
235 const SkPMColor* SK_RESTRICT cache,
236 int toggle, int count) {
237 SkClampRange range;
reed@google.com3c2102c2013-02-01 12:59:40 +0000238 range.init(fx, dx, count, 0, SkGradientShaderBase::kCache32Count - 1);
reed9d91eb32015-01-28 11:44:48 -0800239 range.validate(count);
rileya@google.com589708b2012-07-26 20:04:23 +0000240
241 if ((count = range.fCount0) > 0) {
242 sk_memset32_dither(dstC,
243 cache[toggle + range.fV0],
reed@google.com55853db2013-02-01 19:34:59 +0000244 cache[next_dither_toggle(toggle) + range.fV0],
rileya@google.com589708b2012-07-26 20:04:23 +0000245 count);
246 dstC += count;
247 }
248 if ((count = range.fCount1) > 0) {
249 int unroll = count >> 3;
250 fx = range.fFx1;
251 for (int i = 0; i < unroll; i++) {
252 NO_CHECK_ITER; NO_CHECK_ITER;
253 NO_CHECK_ITER; NO_CHECK_ITER;
254 NO_CHECK_ITER; NO_CHECK_ITER;
255 NO_CHECK_ITER; NO_CHECK_ITER;
256 }
257 if ((count &= 7) > 0) {
258 do {
259 NO_CHECK_ITER;
260 } while (--count != 0);
261 }
262 }
263 if ((count = range.fCount2) > 0) {
264 sk_memset32_dither(dstC,
265 cache[toggle + range.fV1],
reed@google.com55853db2013-02-01 19:34:59 +0000266 cache[next_dither_toggle(toggle) + range.fV1],
rileya@google.com589708b2012-07-26 20:04:23 +0000267 count);
268 }
269}
270
reedcaf7e932014-12-18 12:43:08 -0800271void shadeSpan_linear_mirror(TileProc proc, SkGradFixed dx, SkGradFixed fx,
rileya@google.com589708b2012-07-26 20:04:23 +0000272 SkPMColor* SK_RESTRICT dstC,
273 const SkPMColor* SK_RESTRICT cache,
274 int toggle, int count) {
275 do {
reedcaf7e932014-12-18 12:43:08 -0800276 unsigned fi = mirror_8bits(SkGradFixedToFixed(fx) >> 8);
rileya@google.com589708b2012-07-26 20:04:23 +0000277 SkASSERT(fi <= 0xFF);
278 fx += dx;
279 *dstC++ = cache[toggle + fi];
reed@google.com55853db2013-02-01 19:34:59 +0000280 toggle = next_dither_toggle(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000281 } while (--count != 0);
282}
283
reedcaf7e932014-12-18 12:43:08 -0800284void shadeSpan_linear_repeat(TileProc proc, SkGradFixed dx, SkGradFixed fx,
rileya@google.com589708b2012-07-26 20:04:23 +0000285 SkPMColor* SK_RESTRICT dstC,
286 const SkPMColor* SK_RESTRICT cache,
287 int toggle, int count) {
288 do {
reedcaf7e932014-12-18 12:43:08 -0800289 unsigned fi = repeat_8bits(SkGradFixedToFixed(fx) >> 8);
rileya@google.com589708b2012-07-26 20:04:23 +0000290 SkASSERT(fi <= 0xFF);
291 fx += dx;
292 *dstC++ = cache[toggle + fi];
reed@google.com55853db2013-02-01 19:34:59 +0000293 toggle = next_dither_toggle(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000294 } while (--count != 0);
295}
296
297}
298
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000299void SkLinearGradient::LinearGradientContext::shadeSpan(int x, int y, SkPMColor* SK_RESTRICT dstC,
300 int count) {
rileya@google.com589708b2012-07-26 20:04:23 +0000301 SkASSERT(count > 0);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000302 const SkLinearGradient& linearGradient = static_cast<const SkLinearGradient&>(fShader);
303
reedf3182eb2015-11-17 08:12:19 -0800304 if (SkShader::kClamp_TileMode == linearGradient.fTileMode &&
305 kLinear_MatrixClass == fDstToIndexClass)
306 {
307 this->shade4_clamp(x, y, dstC, count);
308 return;
309 }
reedf3182eb2015-11-17 08:12:19 -0800310
rileya@google.com589708b2012-07-26 20:04:23 +0000311 SkPoint srcPt;
312 SkMatrix::MapXYProc dstProc = fDstToIndexProc;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000313 TileProc proc = linearGradient.fTileProc;
314 const SkPMColor* SK_RESTRICT cache = fCache->getCache32();
reed@google.com55853db2013-02-01 19:34:59 +0000315 int toggle = init_dither_toggle(x, y);
rileya@google.com589708b2012-07-26 20:04:23 +0000316
317 if (fDstToIndexClass != kPerspective_MatrixClass) {
318 dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf,
319 SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
Florin Malita63b61562017-02-10 10:42:49 -0500320 SkGradFixed dx, fx = SkScalarPinToGradFixed(srcPt.fX);
rileya@google.com589708b2012-07-26 20:04:23 +0000321
322 if (fDstToIndexClass == kFixedStepInX_MatrixClass) {
benjaminwagner8e175562016-02-16 10:09:40 -0800323 const auto step = fDstToIndex.fixedStepInX(SkIntToScalar(y));
reedcaf7e932014-12-18 12:43:08 -0800324 // todo: do we need a real/high-precision value for dx here?
Florin Malita63b61562017-02-10 10:42:49 -0500325 dx = SkScalarPinToGradFixed(step.fX);
rileya@google.com589708b2012-07-26 20:04:23 +0000326 } else {
327 SkASSERT(fDstToIndexClass == kLinear_MatrixClass);
Florin Malita63b61562017-02-10 10:42:49 -0500328 dx = SkScalarPinToGradFixed(fDstToIndex.getScaleX());
rileya@google.com589708b2012-07-26 20:04:23 +0000329 }
330
331 LinearShadeProc shadeProc = shadeSpan_linear_repeat;
reed@google.com53009ba2013-02-07 20:28:49 +0000332 if (0 == dx) {
rileya@google.com589708b2012-07-26 20:04:23 +0000333 shadeProc = shadeSpan_linear_vertical_lerp;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000334 } else if (SkShader::kClamp_TileMode == linearGradient.fTileMode) {
rileya@google.com589708b2012-07-26 20:04:23 +0000335 shadeProc = shadeSpan_linear_clamp;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000336 } else if (SkShader::kMirror_TileMode == linearGradient.fTileMode) {
rileya@google.com589708b2012-07-26 20:04:23 +0000337 shadeProc = shadeSpan_linear_mirror;
338 } else {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000339 SkASSERT(SkShader::kRepeat_TileMode == linearGradient.fTileMode);
rileya@google.com589708b2012-07-26 20:04:23 +0000340 }
341 (*shadeProc)(proc, dx, fx, dstC, cache, toggle, count);
342 } else {
343 SkScalar dstX = SkIntToScalar(x);
344 SkScalar dstY = SkIntToScalar(y);
345 do {
346 dstProc(fDstToIndex, dstX, dstY, &srcPt);
347 unsigned fi = proc(SkScalarToFixed(srcPt.fX));
348 SkASSERT(fi <= 0xFFFF);
349 *dstC++ = cache[toggle + (fi >> kCache32Shift)];
reed@google.com55853db2013-02-01 19:34:59 +0000350 toggle = next_dither_toggle(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000351 dstX += SK_Scalar1;
352 } while (--count != 0);
353 }
354}
355
rileya@google.com589708b2012-07-26 20:04:23 +0000356SkShader::GradientType SkLinearGradient::asAGradient(GradientInfo* info) const {
357 if (info) {
358 commonAsAGradient(info);
359 info->fPoint[0] = fStart;
360 info->fPoint[1] = fEnd;
361 }
362 return kLinear_GradientType;
363}
364
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000365#if SK_SUPPORT_GPU
366
brianosmanb9c51372016-09-15 11:09:45 -0700367#include "GrColorSpaceXform.h"
Brian Salomon94efbf52016-11-29 13:43:05 -0500368#include "GrShaderCaps.h"
egdaniel7ea439b2015-12-03 09:20:44 -0800369#include "glsl/GrGLSLFragmentShaderBuilder.h"
dandov9de5b512014-06-10 14:38:28 -0700370#include "SkGr.h"
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000371
rileya@google.comd7cc6512012-07-27 14:00:39 +0000372/////////////////////////////////////////////////////////////////////
373
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000374class GrLinearGradient : public GrGradientEffect {
375public:
fmenozzi55d318d2016-08-09 08:05:57 -0700376 class GLSLLinearProcessor;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000377
brianosman9557c272016-09-15 06:59:15 -0700378 static sk_sp<GrFragmentProcessor> Make(const CreateArgs& args) {
379 return sk_sp<GrFragmentProcessor>(new GrLinearGradient(args));
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000380 }
381
Brian Salomond3b65972017-03-22 12:05:03 -0400382 ~GrLinearGradient() override {}
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000383
mtklein36352bf2015-03-25 18:17:31 -0700384 const char* name() const override { return "Linear Gradient"; }
joshualitteb2a6762014-12-04 11:35:33 -0800385
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000386private:
Brian Salomon587e08f2017-01-27 10:59:27 -0500387 GrLinearGradient(const CreateArgs& args) : INHERITED(args, args.fShader->colorsAreOpaque()) {
joshualitteb2a6762014-12-04 11:35:33 -0800388 this->initClassID<GrLinearGradient>();
389 }
wangyix4b3050b2015-08-04 07:59:37 -0700390
fmenozzi55d318d2016-08-09 08:05:57 -0700391 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
wangyixb1daa862015-08-18 11:29:31 -0700392
Brian Salomon94efbf52016-11-29 13:43:05 -0500393 virtual void onGetGLSLProcessorKey(const GrShaderCaps& caps,
fmenozzi55d318d2016-08-09 08:05:57 -0700394 GrProcessorKeyBuilder* b) const override;
wangyix4b3050b2015-08-04 07:59:37 -0700395
joshualittb0a8a372014-09-23 09:50:21 -0700396 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000397
398 typedef GrGradientEffect INHERITED;
399};
400
401/////////////////////////////////////////////////////////////////////
402
fmenozzi55d318d2016-08-09 08:05:57 -0700403class GrLinearGradient::GLSLLinearProcessor : public GrGradientEffect::GLSLProcessor {
404public:
405 GLSLLinearProcessor(const GrProcessor&) {}
406
Brian Salomond3b65972017-03-22 12:05:03 -0400407 ~GLSLLinearProcessor() override {}
fmenozzi55d318d2016-08-09 08:05:57 -0700408
409 virtual void emitCode(EmitArgs&) override;
410
Brian Salomon94efbf52016-11-29 13:43:05 -0500411 static void GenKey(const GrProcessor& processor, const GrShaderCaps&, GrProcessorKeyBuilder* b) {
fmenozzi55d318d2016-08-09 08:05:57 -0700412 b->add32(GenBaseGradientKey(processor));
413 }
414
415private:
416 typedef GrGradientEffect::GLSLProcessor INHERITED;
417};
418
419/////////////////////////////////////////////////////////////////////
420
421GrGLSLFragmentProcessor* GrLinearGradient::onCreateGLSLInstance() const {
422 return new GrLinearGradient::GLSLLinearProcessor(*this);
423}
424
Brian Salomon94efbf52016-11-29 13:43:05 -0500425void GrLinearGradient::onGetGLSLProcessorKey(const GrShaderCaps& caps,
fmenozzi55d318d2016-08-09 08:05:57 -0700426 GrProcessorKeyBuilder* b) const {
427 GrLinearGradient::GLSLLinearProcessor::GenKey(*this, caps, b);
428}
429
430/////////////////////////////////////////////////////////////////////
431
joshualittb0a8a372014-09-23 09:50:21 -0700432GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrLinearGradient);
bsalomon@google.comd4726202012-08-03 14:34:46 +0000433
Hal Canary6f6961e2017-01-31 13:50:44 -0500434#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700435sk_sp<GrFragmentProcessor> GrLinearGradient::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700436 SkPoint points[] = {{d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()},
437 {d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()}};
bsalomon@google.comd4726202012-08-03 14:34:46 +0000438
Brian Osman3f748602016-10-03 18:29:03 -0400439 RandomGradientParams params(d->fRandom);
Brian Osmana2196532016-10-17 12:48:13 -0400440 auto shader = params.fUseColors4f ?
441 SkGradientShader::MakeLinear(points, params.fColors4f, params.fColorSpace, params.fStops,
442 params.fColorCount, params.fTileMode) :
443 SkGradientShader::MakeLinear(points, params.fColors, params.fStops,
444 params.fColorCount, params.fTileMode);
Brian Osman9f532a32016-10-19 11:12:09 -0400445 GrTest::TestAsFPArgs asFPArgs(d);
Florin Malita4aed1382017-05-25 10:38:07 -0400446 sk_sp<GrFragmentProcessor> fp = as_SB(shader)->asFragmentProcessor(asFPArgs.args());
bsalomonc21b09e2015-08-28 18:46:56 -0700447 GrAlwaysAssert(fp);
joshualittb0a8a372014-09-23 09:50:21 -0700448 return fp;
bsalomon@google.comd4726202012-08-03 14:34:46 +0000449}
Hal Canary6f6961e2017-01-31 13:50:44 -0500450#endif
bsalomon@google.comd4726202012-08-03 14:34:46 +0000451
452/////////////////////////////////////////////////////////////////////
453
fmenozzi55d318d2016-08-09 08:05:57 -0700454void GrLinearGradient::GLSLLinearProcessor::emitCode(EmitArgs& args) {
wangyix7c157a92015-07-22 15:08:53 -0700455 const GrLinearGradient& ge = args.fFp.cast<GrLinearGradient>();
egdaniel7ea439b2015-12-03 09:20:44 -0800456 this->emitUniforms(args.fUniformHandler, ge);
bsalomon1a1aa932016-09-12 09:30:36 -0700457 SkString t = args.fFragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000458 t.append(".x");
egdaniel7ea439b2015-12-03 09:20:44 -0800459 this->emitColor(args.fFragBuilder,
460 args.fUniformHandler,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500461 args.fShaderCaps,
fmenozzi55d318d2016-08-09 08:05:57 -0700462 ge,
463 t.c_str(),
egdaniel4ca2e602015-11-18 08:01:26 -0800464 args.fOutputColor,
465 args.fInputColor,
cdalton3f6f76f2016-04-11 12:18:09 -0700466 args.fTexSamplers);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000467}
468
469/////////////////////////////////////////////////////////////////////
470
brianosman839345d2016-07-22 11:04:53 -0700471sk_sp<GrFragmentProcessor> SkLinearGradient::asFragmentProcessor(const AsFPArgs& args) const {
472 SkASSERT(args.fContext);
mtklein3f3b3d02014-12-01 11:47:08 -0800473
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000474 SkMatrix matrix;
bsalomon@google.comf94b3a42012-10-31 18:09:01 +0000475 if (!this->getLocalMatrix().invert(&matrix)) {
bsalomonc21b09e2015-08-28 18:46:56 -0700476 return nullptr;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000477 }
brianosman839345d2016-07-22 11:04:53 -0700478 if (args.fLocalMatrix) {
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000479 SkMatrix inv;
brianosman839345d2016-07-22 11:04:53 -0700480 if (!args.fLocalMatrix->invert(&inv)) {
bsalomonc21b09e2015-08-28 18:46:56 -0700481 return nullptr;
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000482 }
483 matrix.postConcat(inv);
484 }
bsalomon@google.comf94b3a42012-10-31 18:09:01 +0000485 matrix.postConcat(fPtsToUnit);
mtklein3f3b3d02014-12-01 11:47:08 -0800486
brianosmanb9c51372016-09-15 11:09:45 -0700487 sk_sp<GrColorSpaceXform> colorSpaceXform = GrColorSpaceXform::Make(fColorSpace.get(),
488 args.fDstColorSpace);
brianosman9557c272016-09-15 06:59:15 -0700489 sk_sp<GrFragmentProcessor> inner(GrLinearGradient::Make(
brianosmanb9c51372016-09-15 11:09:45 -0700490 GrGradientEffect::CreateArgs(args.fContext, this, &matrix, fTileMode,
491 std::move(colorSpaceXform), SkToBool(args.fDstColorSpace))));
bungeman06ca8ec2016-06-09 08:01:03 -0700492 return GrFragmentProcessor::MulOutputByInputAlpha(std::move(inner));
rileya@google.comd7cc6512012-07-27 14:00:39 +0000493}
494
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000495
496#endif
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000497
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000498#ifndef SK_IGNORE_TO_STRING
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000499void SkLinearGradient::toString(SkString* str) const {
500 str->append("SkLinearGradient (");
501
502 str->appendf("start: (%f, %f)", fStart.fX, fStart.fY);
503 str->appendf(" end: (%f, %f) ", fEnd.fX, fEnd.fY);
504
505 this->INHERITED::toString(str);
506
507 str->append(")");
508}
509#endif
reedf3182eb2015-11-17 08:12:19 -0800510
511///////////////////////////////////////////////////////////////////////////////////////////////////
512
513#include "SkNx.h"
514
515static const SkLinearGradient::LinearGradientContext::Rec*
516find_forward(const SkLinearGradient::LinearGradientContext::Rec rec[], float tiledX) {
517 SkASSERT(tiledX >= 0 && tiledX <= 1);
518
519 SkASSERT(rec[0].fPos >= 0 && rec[0].fPos <= 1);
520 SkASSERT(rec[1].fPos >= 0 && rec[1].fPos <= 1);
521 SkASSERT(rec[0].fPos <= rec[1].fPos);
522 rec += 1;
lsalzmanf2b86622016-01-22 14:03:02 -0800523 while (rec->fPos < tiledX || rec->fPosScale == 0) {
reedf3182eb2015-11-17 08:12:19 -0800524 SkASSERT(rec[0].fPos >= 0 && rec[0].fPos <= 1);
525 SkASSERT(rec[1].fPos >= 0 && rec[1].fPos <= 1);
526 SkASSERT(rec[0].fPos <= rec[1].fPos);
527 rec += 1;
528 }
529 return rec - 1;
530}
531
532static const SkLinearGradient::LinearGradientContext::Rec*
533find_backward(const SkLinearGradient::LinearGradientContext::Rec rec[], float tiledX) {
534 SkASSERT(tiledX >= 0 && tiledX <= 1);
535
536 SkASSERT(rec[0].fPos >= 0 && rec[0].fPos <= 1);
537 SkASSERT(rec[1].fPos >= 0 && rec[1].fPos <= 1);
538 SkASSERT(rec[0].fPos <= rec[1].fPos);
lsalzmanf2b86622016-01-22 14:03:02 -0800539 while (tiledX < rec->fPos || rec[1].fPosScale == 0) {
reedf3182eb2015-11-17 08:12:19 -0800540 rec -= 1;
541 SkASSERT(rec[0].fPos >= 0 && rec[0].fPos <= 1);
542 SkASSERT(rec[1].fPos >= 0 && rec[1].fPos <= 1);
543 SkASSERT(rec[0].fPos <= rec[1].fPos);
544 }
545 return rec;
546}
547
fmalita0ce4f232016-12-06 08:57:47 -0800548// As an optimization, we can apply the dither bias before interpolation -- but only when
549// operating in premul space (apply_alpha == false). When apply_alpha == true, we must
550// defer the bias application until after premul.
551//
552// The following two helpers encapsulate this logic: pre_bias is called before interpolation,
553// and effects the bias when apply_alpha == false, while post_bias is called after premul and
554// effects the bias for the apply_alpha == true case.
555
556template <bool apply_alpha>
557Sk4f pre_bias(const Sk4f& x, const Sk4f& bias) {
fmalita0ce4f232016-12-06 08:57:47 -0800558 return apply_alpha ? x : x + bias;
fmalita0ce4f232016-12-06 08:57:47 -0800559}
560
561template <bool apply_alpha>
562Sk4f post_bias(const Sk4f& x, const Sk4f& bias) {
fmalita0ce4f232016-12-06 08:57:47 -0800563 return apply_alpha ? x + bias : x;
fmalita0ce4f232016-12-06 08:57:47 -0800564}
565
566template <bool apply_alpha> SkPMColor trunc_from_255(const Sk4f& x, const Sk4f& bias) {
reedf3182eb2015-11-17 08:12:19 -0800567 SkPMColor c;
fmalita0ce4f232016-12-06 08:57:47 -0800568 Sk4f c4f255 = x;
569 if (apply_alpha) {
Lee Salzmana8362672017-06-01 14:45:29 -0400570#ifdef SK_SUPPORT_LEGACY_GRADIENT_ALPHATRUNC
571 static constexpr float alphaScale = 1;
572#else
573 // Due to use of multiplication by the 1/255 reciprocal instead of division by 255,
574 // non-integer alpha values very close to their ceiling can push the color values
575 // above the alpha value, which will become an invalid premultiplied color. So nudge
576 // alpha up slightly by a compensating scale to keep it above the color values.
577 // To do this, multiply alpha by a number slightly greater than 1 to compensate
578 // for error in scaling from the 1/255 approximation. Since this error is then
579 // scaled by the alpha value, we need to scale the epsilon by 255 to get a safe
580 // upper bound on the error.
581 static constexpr float alphaScale = 1 + 255*std::numeric_limits<float>::epsilon();
582#endif
fmalita0ce4f232016-12-06 08:57:47 -0800583 const float scale = x[SkPM4f::A] * (1 / 255.f);
Lee Salzmana8362672017-06-01 14:45:29 -0400584 c4f255 *= Sk4f(scale, scale, scale, alphaScale);
fmalita0ce4f232016-12-06 08:57:47 -0800585 }
586 SkNx_cast<uint8_t>(post_bias<apply_alpha>(c4f255, bias)).store(&c);
Florin Malitaaaa6d772016-12-07 14:57:04 -0500587
reedf3182eb2015-11-17 08:12:19 -0800588 return c;
589}
590
591template <bool apply_alpha> void fill(SkPMColor dst[], int count,
fmalita0ce4f232016-12-06 08:57:47 -0800592 const Sk4f& c4, const Sk4f& bias0, const Sk4f& bias1) {
593 const SkPMColor c0 = trunc_from_255<apply_alpha>(pre_bias<apply_alpha>(c4, bias0), bias0);
594 const SkPMColor c1 = trunc_from_255<apply_alpha>(pre_bias<apply_alpha>(c4, bias1), bias1);
595 sk_memset32_dither(dst, c0, c1, count);
reedf3182eb2015-11-17 08:12:19 -0800596}
597
598template <bool apply_alpha> void fill(SkPMColor dst[], int count, const Sk4f& c4) {
599 // Assumes that c4 does not need to be dithered.
fmalita0ce4f232016-12-06 08:57:47 -0800600 sk_memset32(dst, trunc_from_255<apply_alpha>(c4, 0), count);
reedf3182eb2015-11-17 08:12:19 -0800601}
602
603/*
604 * TODOs
605 *
606 * - tilemodes
607 * - interp before or after premul
608 * - perspective
609 * - optimizations
610 * - use fixed (32bit or 16bit) instead of floats?
611 */
612
613static Sk4f lerp_color(float fx, const SkLinearGradient::LinearGradientContext::Rec* rec) {
reedde3aac82015-11-22 13:00:04 -0800614 SkASSERT(fx >= rec[0].fPos);
615 SkASSERT(fx <= rec[1].fPos);
616
reedf3182eb2015-11-17 08:12:19 -0800617 const float p0 = rec[0].fPos;
618 const Sk4f c0 = rec[0].fColor;
619 const Sk4f c1 = rec[1].fColor;
620 const Sk4f diffc = c1 - c0;
621 const float scale = rec[1].fPosScale;
622 const float t = (fx - p0) * scale;
623 return c0 + Sk4f(t) * diffc;
624}
625
626template <bool apply_alpha> void ramp(SkPMColor dstC[], int n, const Sk4f& c, const Sk4f& dc,
627 const Sk4f& dither0, const Sk4f& dither1) {
628 Sk4f dc2 = dc + dc;
629 Sk4f dc4 = dc2 + dc2;
fmalita0ce4f232016-12-06 08:57:47 -0800630 Sk4f cd0 = pre_bias<apply_alpha>(c , dither0);
631 Sk4f cd1 = pre_bias<apply_alpha>(c + dc, dither1);
reedf3182eb2015-11-17 08:12:19 -0800632 Sk4f cd2 = cd0 + dc2;
633 Sk4f cd3 = cd1 + dc2;
634 while (n >= 4) {
mtklein9db43ac2015-12-01 07:10:21 -0800635 if (!apply_alpha) {
mtklein6f37b4a2015-12-14 11:25:18 -0800636 Sk4f_ToBytes((uint8_t*)dstC, cd0, cd1, cd2, cd3);
mtklein9db43ac2015-12-01 07:10:21 -0800637 dstC += 4;
638 } else {
fmalita0ce4f232016-12-06 08:57:47 -0800639 *dstC++ = trunc_from_255<apply_alpha>(cd0, dither0);
640 *dstC++ = trunc_from_255<apply_alpha>(cd1, dither1);
641 *dstC++ = trunc_from_255<apply_alpha>(cd2, dither0);
642 *dstC++ = trunc_from_255<apply_alpha>(cd3, dither1);
mtklein9db43ac2015-12-01 07:10:21 -0800643 }
reedf3182eb2015-11-17 08:12:19 -0800644 cd0 = cd0 + dc4;
645 cd1 = cd1 + dc4;
646 cd2 = cd2 + dc4;
647 cd3 = cd3 + dc4;
648 n -= 4;
649 }
650 if (n & 2) {
fmalita0ce4f232016-12-06 08:57:47 -0800651 *dstC++ = trunc_from_255<apply_alpha>(cd0, dither0);
652 *dstC++ = trunc_from_255<apply_alpha>(cd1, dither1);
reedf3182eb2015-11-17 08:12:19 -0800653 cd0 = cd0 + dc2;
654 }
655 if (n & 1) {
fmalita0ce4f232016-12-06 08:57:47 -0800656 *dstC++ = trunc_from_255<apply_alpha>(cd0, dither0);
reedf3182eb2015-11-17 08:12:19 -0800657 }
658}
659
660template <bool apply_alpha, bool dx_is_pos>
661void SkLinearGradient::LinearGradientContext::shade4_dx_clamp(SkPMColor dstC[], int count,
662 float fx, float dx, float invDx,
663 const float dither[2]) {
664 Sk4f dither0(dither[0]);
665 Sk4f dither1(dither[1]);
666 const Rec* rec = fRecs.begin();
667
668 const Sk4f dx4 = Sk4f(dx);
669 SkDEBUGCODE(SkPMColor* endDstC = dstC + count;)
670
671 if (dx_is_pos) {
672 if (fx < 0) {
fmalita7b38e3c2016-05-26 11:13:52 -0700673 // count is guaranteed to be positive, but the first arg may overflow int32 after
674 // increment => casting to uint32 ensures correct clamping.
fmalita7dcb1312016-05-26 18:10:24 -0700675 int n = SkTMin<uint32_t>(static_cast<uint32_t>(SkFloatToIntFloor(-fx * invDx)) + 1,
676 count);
fmalita7b38e3c2016-05-26 11:13:52 -0700677 SkASSERT(n > 0);
reedf3182eb2015-11-17 08:12:19 -0800678 fill<apply_alpha>(dstC, n, rec[0].fColor);
679 count -= n;
680 dstC += n;
681 fx += n * dx;
682 SkASSERT(0 == count || fx >= 0);
683 if (n & 1) {
684 SkTSwap(dither0, dither1);
685 }
686 }
687 } else { // dx < 0
688 if (fx > 1) {
fmalita7b38e3c2016-05-26 11:13:52 -0700689 // count is guaranteed to be positive, but the first arg may overflow int32 after
690 // increment => casting to uint32 ensures correct clamping.
fmalita7dcb1312016-05-26 18:10:24 -0700691 int n = SkTMin<uint32_t>(static_cast<uint32_t>(SkFloatToIntFloor((1 - fx) * invDx)) + 1,
692 count);
fmalita7b38e3c2016-05-26 11:13:52 -0700693 SkASSERT(n > 0);
reedf3182eb2015-11-17 08:12:19 -0800694 fill<apply_alpha>(dstC, n, rec[fRecs.count() - 1].fColor);
695 count -= n;
696 dstC += n;
697 fx += n * dx;
698 SkASSERT(0 == count || fx <= 1);
699 if (n & 1) {
700 SkTSwap(dither0, dither1);
701 }
702 }
703 }
704 SkASSERT(count >= 0);
705
706 const Rec* r;
707 if (dx_is_pos) {
708 r = fRecs.begin(); // start at the beginning
709 } else {
710 r = fRecs.begin() + fRecs.count() - 2; // start at the end
711 }
712
713 while (count > 0) {
714 if (dx_is_pos) {
715 if (fx >= 1) {
716 fill<apply_alpha>(dstC, count, rec[fRecs.count() - 1].fColor);
717 return;
718 }
719 } else { // dx < 0
720 if (fx <= 0) {
721 fill<apply_alpha>(dstC, count, rec[0].fColor);
722 return;
723 }
724 }
725
726 if (dx_is_pos) {
727 r = find_forward(r, fx);
728 } else {
729 r = find_backward(r, fx);
730 }
731 SkASSERT(r >= fRecs.begin() && r < fRecs.begin() + fRecs.count() - 1);
732
733 const float p0 = r[0].fPos;
734 const Sk4f c0 = r[0].fColor;
735 const float p1 = r[1].fPos;
736 const Sk4f diffc = Sk4f(r[1].fColor) - c0;
737 const float scale = r[1].fPosScale;
738 const float t = (fx - p0) * scale;
739 const Sk4f c = c0 + Sk4f(t) * diffc;
740 const Sk4f dc = diffc * dx4 * Sk4f(scale);
741
742 int n;
743 if (dx_is_pos) {
744 n = SkTMin((int)((p1 - fx) * invDx) + 1, count);
745 } else {
746 n = SkTMin((int)((p0 - fx) * invDx) + 1, count);
747 }
748
749 fx += n * dx;
reedaeab8ea2016-01-05 10:01:38 -0800750 // fx should now outside of the p0..p1 interval. However, due to float precision loss,
751 // its possible that fx is slightly too small/large, so we clamp it.
reedf3182eb2015-11-17 08:12:19 -0800752 if (dx_is_pos) {
reedaeab8ea2016-01-05 10:01:38 -0800753 fx = SkTMax(fx, p1);
reedf3182eb2015-11-17 08:12:19 -0800754 } else {
reedaeab8ea2016-01-05 10:01:38 -0800755 fx = SkTMin(fx, p0);
reedf3182eb2015-11-17 08:12:19 -0800756 }
757
758 ramp<apply_alpha>(dstC, n, c, dc, dither0, dither1);
759 dstC += n;
760 SkASSERT(dstC <= endDstC);
mtklein9db43ac2015-12-01 07:10:21 -0800761
reedf3182eb2015-11-17 08:12:19 -0800762 if (n & 1) {
763 SkTSwap(dither0, dither1);
764 }
reedaeab8ea2016-01-05 10:01:38 -0800765
766 count -= n;
767 SkASSERT(count >= 0);
reedf3182eb2015-11-17 08:12:19 -0800768 }
769}
770
771void SkLinearGradient::LinearGradientContext::shade4_clamp(int x, int y, SkPMColor dstC[],
772 int count) {
773 SkASSERT(count > 0);
774 SkASSERT(kLinear_MatrixClass == fDstToIndexClass);
775
776 SkPoint srcPt;
777 fDstToIndexProc(fDstToIndex, x + SK_ScalarHalf, y + SK_ScalarHalf, &srcPt);
778 float fx = srcPt.x();
779 const float dx = fDstToIndex.getScaleX();
780
781 // Default our dither bias values to 1/2, (rounding), which is no dithering
782 float dither0 = 0.5f;
783 float dither1 = 0.5f;
784 if (fDither) {
785 const float ditherCell[] = {
786 1/8.0f, 5/8.0f,
787 7/8.0f, 3/8.0f,
788 };
789 const int rowIndex = (y & 1) << 1;
790 dither0 = ditherCell[rowIndex];
791 dither1 = ditherCell[rowIndex + 1];
792 if (x & 1) {
793 SkTSwap(dither0, dither1);
794 }
795 }
796 const float dither[2] = { dither0, dither1 };
reedf3182eb2015-11-17 08:12:19 -0800797
fmalita2b469132015-11-23 08:30:23 -0800798 if (SkScalarNearlyZero(dx * count)) { // gradient is vertical
reedde3aac82015-11-22 13:00:04 -0800799 const float pinFx = SkTPin(fx, 0.0f, 1.0f);
800 Sk4f c = lerp_color(pinFx, find_forward(fRecs.begin(), pinFx));
reedf3182eb2015-11-17 08:12:19 -0800801 if (fApplyAlphaAfterInterp) {
fmalita0ce4f232016-12-06 08:57:47 -0800802 fill<true>(dstC, count, c, dither0, dither1);
reedf3182eb2015-11-17 08:12:19 -0800803 } else {
fmalita0ce4f232016-12-06 08:57:47 -0800804 fill<false>(dstC, count, c, dither0, dither1);
reedf3182eb2015-11-17 08:12:19 -0800805 }
806 return;
807 }
808
James Zern44e91c92016-11-09 19:22:46 -0800809 SkASSERT(0.f != dx);
810 const float invDx = 1 / dx;
reedf3182eb2015-11-17 08:12:19 -0800811 if (dx > 0) {
812 if (fApplyAlphaAfterInterp) {
813 this->shade4_dx_clamp<true, true>(dstC, count, fx, dx, invDx, dither);
814 } else {
815 this->shade4_dx_clamp<false, true>(dstC, count, fx, dx, invDx, dither);
816 }
817 } else {
818 if (fApplyAlphaAfterInterp) {
819 this->shade4_dx_clamp<true, false>(dstC, count, fx, dx, invDx, dither);
820 } else {
821 this->shade4_dx_clamp<false, false>(dstC, count, fx, dx, invDx, dither);
822 }
823 }
824}