blob: 91c0e2095593c79fc5109699b51f9adcfd37e405 [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"
rileya@google.com589708b2012-07-26 20:04:23 +00009#include "SkLinearGradient.h"
bungeman06ca8ec2016-06-09 08:01:03 -070010#include "SkRefCnt.h"
rileya@google.com589708b2012-07-26 20:04:23 +000011
fmalitabc590c02016-02-22 09:12:33 -080012// define to test the 4f gradient path
fmalita55430a62016-02-23 13:26:28 -080013// #define FORCE_4F_CONTEXT
fmalitabc590c02016-02-22 09:12:33 -080014
reedf3182eb2015-11-17 08:12:19 -080015static const float kInv255Float = 1.0f / 255;
16
rileya@google.com589708b2012-07-26 20:04:23 +000017static inline int repeat_8bits(int x) {
18 return x & 0xFF;
19}
20
rileya@google.com589708b2012-07-26 20:04:23 +000021static inline int mirror_8bits(int x) {
rileya@google.com589708b2012-07-26 20:04:23 +000022 if (x & 256) {
23 x = ~x;
24 }
25 return x & 255;
rileya@google.com589708b2012-07-26 20:04:23 +000026}
27
mtkleincc695fe2014-12-10 10:29:19 -080028static SkMatrix pts_to_unit_matrix(const SkPoint pts[2]) {
rileya@google.com589708b2012-07-26 20:04:23 +000029 SkVector vec = pts[1] - pts[0];
30 SkScalar mag = vec.length();
31 SkScalar inv = mag ? SkScalarInvert(mag) : 0;
32
33 vec.scale(inv);
mtkleincc695fe2014-12-10 10:29:19 -080034 SkMatrix matrix;
35 matrix.setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY);
36 matrix.postTranslate(-pts[0].fX, -pts[0].fY);
37 matrix.postScale(inv, inv);
38 return matrix;
rileya@google.com589708b2012-07-26 20:04:23 +000039}
40
fmalita55430a62016-02-23 13:26:28 -080041static bool use_4f_context(const SkShader::ContextRec& rec, uint32_t flags) {
42#ifdef FORCE_4F_CONTEXT
fmalitabc590c02016-02-22 09:12:33 -080043 return true;
44#else
fmalita55430a62016-02-23 13:26:28 -080045 return rec.fPreferredDstType == SkShader::ContextRec::kPM4f_DstType
46 || SkToBool(flags & SkLinearGradient::kForce4fContext_PrivateFlag);
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
reed773ceda2016-03-03 18:18:25 -080077size_t SkLinearGradient::onContextSize(const ContextRec& rec) const {
fmalita55430a62016-02-23 13:26:28 -080078 return use_4f_context(rec, fGradFlags)
reeda0cee5f2016-03-04 07:38:11 -080079 ? sizeof(LinearGradient4fContext)
80 : sizeof(LinearGradientContext);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000081}
82
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +000083SkShader::Context* SkLinearGradient::onCreateContext(const ContextRec& rec, void* storage) const {
fmalita55430a62016-02-23 13:26:28 -080084 return use_4f_context(rec, fGradFlags)
fmalita088e21b2016-10-05 09:28:42 -070085 ? CheckedCreateContext<LinearGradient4fContext>(storage, *this, rec)
86 : CheckedCreateContext< LinearGradientContext>(storage, *this, rec);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000087}
88
Florin Malitac86e4702017-01-20 08:41:34 -050089// For now, only a 2-stop raster pipeline specialization.
90//
91// Stages:
92//
93// * matrix (map dst -> grad space)
94// * clamp/repeat/mirror (tiling)
95// * linear_gradient_2stops (lerp c0/c1)
96// * optional premul
97//
98bool SkLinearGradient::onAppendStages(SkRasterPipeline* p, SkColorSpace* cs, SkArenaAlloc* alloc,
99 const SkMatrix& ctm, const SkPaint& paint) const {
100 if (fColorCount > 2) {
101 return false;
102 }
103
104 // Local matrix not supported currently. Remove once we have a generic RP wrapper.
105 if (!getLocalMatrix().isIdentity()) {
106 return false;
107 }
108
109 SkASSERT(fColorCount == 2);
110 SkASSERT(fOrigPos == nullptr || (fOrigPos[0] == 0 && fOrigPos[1] == 1));
111
112 SkMatrix dstToPts;
113 if (!ctm.invert(&dstToPts)) {
114 return false;
115 }
116
117 const auto dstToUnit = SkMatrix::Concat(fPtsToUnit, dstToPts);
118
119 auto* m = alloc->makeArrayDefault<float>(9);
120 if (dstToUnit.asAffine(m)) {
121 // TODO: mapping y is not needed; split the matrix stages to save some math?
122 p->append(SkRasterPipeline::matrix_2x3, m);
123 } else {
124 dstToUnit.get9(m);
125 p->append(SkRasterPipeline::matrix_perspective, m);
126 }
127
128 // TODO: clamp/repeat/mirror const 1f stages?
129 auto* limit = alloc->make<float>(1.0f);
130
131 switch (fTileMode) {
132 case kClamp_TileMode:
133 *limit += 0.5f; // why is clamp_x offsetting its limit?
134 p->append(SkRasterPipeline::clamp_x, limit);
135 break;
136 case kMirror_TileMode: p->append(SkRasterPipeline::mirror_x, limit); break;
137 case kRepeat_TileMode: p->append(SkRasterPipeline::repeat_x, limit); break;
138 }
139
140 const bool premulGrad = fGradFlags & SkGradientShader::kInterpolateColorsInPremul_Flag;
141 const SkColor4f c0 = to_colorspace(fOrigColors4f[0], fColorSpace.get(), cs),
142 c1 = to_colorspace(fOrigColors4f[1], fColorSpace.get(), cs);
143 const SkPM4f pmc0 = premulGrad ? c0.premul() : SkPM4f::From4f(Sk4f::Load(&c0)),
144 pmc1 = premulGrad ? c1.premul() : SkPM4f::From4f(Sk4f::Load(&c1));
145
146 auto* c0_and_dc = alloc->makeArrayDefault<SkPM4f>(2);
147 c0_and_dc[0] = pmc0;
148 c0_and_dc[1] = SkPM4f::From4f(pmc1.to4f() - pmc0.to4f());
149
150 p->append(SkRasterPipeline::linear_gradient_2stops, c0_and_dc);
151
152 if (!premulGrad && !this->colorsAreOpaque()) {
153 p->append(SkRasterPipeline::premul);
154 }
155
156 return true;
157}
158
reedf3182eb2015-11-17 08:12:19 -0800159// This swizzles SkColor into the same component order as SkPMColor, but does not actually
160// "pre" multiply the color components.
161//
162// This allows us to map directly to Sk4f, and eventually scale down to bytes to output a
163// SkPMColor from the floats, without having to swizzle each time.
164//
165static uint32_t SkSwizzle_Color_to_PMColor(SkColor c) {
166 return SkPackARGB32NoCheck(SkColorGetA(c), SkColorGetR(c), SkColorGetG(c), SkColorGetB(c));
167}
168
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000169SkLinearGradient::LinearGradientContext::LinearGradientContext(
reedf3182eb2015-11-17 08:12:19 -0800170 const SkLinearGradient& shader, const ContextRec& ctx)
171 : INHERITED(shader, ctx)
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000172{
reedf3182eb2015-11-17 08:12:19 -0800173 // setup for Sk4f
fmalita748d6202016-05-11 11:39:58 -0700174 const int count = shader.fColorCount;
175 SkASSERT(count > 1);
176
reedf3182eb2015-11-17 08:12:19 -0800177 fRecs.setCount(count);
178 Rec* rec = fRecs.begin();
179 if (shader.fOrigPos) {
180 rec[0].fPos = 0;
181 SkDEBUGCODE(rec[0].fPosScale = SK_FloatNaN;) // should never get used
182 for (int i = 1; i < count; ++i) {
183 rec[i].fPos = SkTPin(shader.fOrigPos[i], rec[i - 1].fPos, 1.0f);
lsalzmanf2b86622016-01-22 14:03:02 -0800184 float diff = rec[i].fPos - rec[i - 1].fPos;
185 if (diff > 0) {
186 rec[i].fPosScale = 1.0f / diff;
187 } else {
188 rec[i].fPosScale = 0;
189 }
reedf3182eb2015-11-17 08:12:19 -0800190 }
reedf3182eb2015-11-17 08:12:19 -0800191 } else {
192 // no pos specified, so we compute evenly spaced values
193 const float scale = float(count - 1);
fmalita748d6202016-05-11 11:39:58 -0700194 const float invScale = 1.0f / scale;
reedf3182eb2015-11-17 08:12:19 -0800195 for (int i = 0; i < count; ++i) {
196 rec[i].fPos = i * invScale;
197 rec[i].fPosScale = scale;
198 }
199 }
fmalita748d6202016-05-11 11:39:58 -0700200 rec[count - 1].fPos = 1; // overwrite the last value just to be sure we end at 1.0
reedf3182eb2015-11-17 08:12:19 -0800201
202 fApplyAlphaAfterInterp = true;
203 if ((shader.getGradFlags() & SkGradientShader::kInterpolateColorsInPremul_Flag) ||
204 shader.colorsAreOpaque())
205 {
206 fApplyAlphaAfterInterp = false;
207 }
208
209 if (fApplyAlphaAfterInterp) {
210 // Our fColor values are in PMColor order, but are still unpremultiplied, allowing us to
211 // interpolate in unpremultiplied space first, and then scale by alpha right before we
212 // convert to SkPMColor bytes.
213 const float paintAlpha = ctx.fPaint->getAlpha() * kInv255Float;
214 const Sk4f scale(1, 1, 1, paintAlpha);
215 for (int i = 0; i < count; ++i) {
216 uint32_t c = SkSwizzle_Color_to_PMColor(shader.fOrigColors[i]);
mtklein507ef6d2016-01-31 08:02:47 -0800217 rec[i].fColor = SkNx_cast<float>(Sk4b::Load(&c)) * scale;
reedf3182eb2015-11-17 08:12:19 -0800218 if (i > 0) {
219 SkASSERT(rec[i - 1].fPos <= rec[i].fPos);
220 }
221 }
222 } else {
223 // Our fColor values are premultiplied, so converting to SkPMColor is just a matter
224 // of converting the floats down to bytes.
225 unsigned alphaScale = ctx.fPaint->getAlpha() + (ctx.fPaint->getAlpha() >> 7);
226 for (int i = 0; i < count; ++i) {
227 SkPMColor pmc = SkPreMultiplyColor(shader.fOrigColors[i]);
228 pmc = SkAlphaMulQ(pmc, alphaScale);
mtklein507ef6d2016-01-31 08:02:47 -0800229 rec[i].fColor = SkNx_cast<float>(Sk4b::Load(&pmc));
reedf3182eb2015-11-17 08:12:19 -0800230 if (i > 0) {
231 SkASSERT(rec[i - 1].fPos <= rec[i].fPos);
232 }
233 }
234 }
rileya@google.com589708b2012-07-26 20:04:23 +0000235}
236
237#define NO_CHECK_ITER \
238 do { \
reedcaf7e932014-12-18 12:43:08 -0800239 unsigned fi = SkGradFixedToFixed(fx) >> SkGradientShaderBase::kCache32Shift; \
rileya@google.com589708b2012-07-26 20:04:23 +0000240 SkASSERT(fi <= 0xFF); \
241 fx += dx; \
242 *dstC++ = cache[toggle + fi]; \
reed@google.com55853db2013-02-01 19:34:59 +0000243 toggle = next_dither_toggle(toggle); \
rileya@google.com589708b2012-07-26 20:04:23 +0000244 } while (0)
245
246namespace {
247
reedcaf7e932014-12-18 12:43:08 -0800248typedef void (*LinearShadeProc)(TileProc proc, SkGradFixed dx, SkGradFixed fx,
rileya@google.com589708b2012-07-26 20:04:23 +0000249 SkPMColor* dstC, const SkPMColor* cache,
250 int toggle, int count);
251
rileya@google.com589708b2012-07-26 20:04:23 +0000252// Linear interpolation (lerp) is unnecessary if there are no sharp
253// discontinuities in the gradient - which must be true if there are
254// only 2 colors - but it's cheap.
reedcaf7e932014-12-18 12:43:08 -0800255void shadeSpan_linear_vertical_lerp(TileProc proc, SkGradFixed dx, SkGradFixed fx,
rileya@google.com589708b2012-07-26 20:04:23 +0000256 SkPMColor* SK_RESTRICT dstC,
257 const SkPMColor* SK_RESTRICT cache,
258 int toggle, int count) {
259 // We're a vertical gradient, so no change in a span.
260 // If colors change sharply across the gradient, dithering is
261 // insufficient (it subsamples the color space) and we need to lerp.
reedcaf7e932014-12-18 12:43:08 -0800262 unsigned fullIndex = proc(SkGradFixedToFixed(fx));
reed@google.com3c2102c2013-02-01 12:59:40 +0000263 unsigned fi = fullIndex >> SkGradientShaderBase::kCache32Shift;
264 unsigned remainder = fullIndex & ((1 << SkGradientShaderBase::kCache32Shift) - 1);
skia.committer@gmail.com9dde0182013-02-04 12:57:42 +0000265
reed@google.com3c2102c2013-02-01 12:59:40 +0000266 int index0 = fi + toggle;
267 int index1 = index0;
268 if (fi < SkGradientShaderBase::kCache32Count - 1) {
269 index1 += 1;
270 }
271 SkPMColor lerp = SkFastFourByteInterp(cache[index1], cache[index0], remainder);
272 index0 ^= SkGradientShaderBase::kDitherStride32;
273 index1 ^= SkGradientShaderBase::kDitherStride32;
274 SkPMColor dlerp = SkFastFourByteInterp(cache[index1], cache[index0], remainder);
rileya@google.com589708b2012-07-26 20:04:23 +0000275 sk_memset32_dither(dstC, lerp, dlerp, count);
276}
277
reedcaf7e932014-12-18 12:43:08 -0800278void shadeSpan_linear_clamp(TileProc proc, SkGradFixed dx, SkGradFixed fx,
rileya@google.com589708b2012-07-26 20:04:23 +0000279 SkPMColor* SK_RESTRICT dstC,
280 const SkPMColor* SK_RESTRICT cache,
281 int toggle, int count) {
282 SkClampRange range;
reed@google.com3c2102c2013-02-01 12:59:40 +0000283 range.init(fx, dx, count, 0, SkGradientShaderBase::kCache32Count - 1);
reed9d91eb32015-01-28 11:44:48 -0800284 range.validate(count);
rileya@google.com589708b2012-07-26 20:04:23 +0000285
286 if ((count = range.fCount0) > 0) {
287 sk_memset32_dither(dstC,
288 cache[toggle + range.fV0],
reed@google.com55853db2013-02-01 19:34:59 +0000289 cache[next_dither_toggle(toggle) + range.fV0],
rileya@google.com589708b2012-07-26 20:04:23 +0000290 count);
291 dstC += count;
292 }
293 if ((count = range.fCount1) > 0) {
294 int unroll = count >> 3;
295 fx = range.fFx1;
296 for (int i = 0; i < unroll; i++) {
297 NO_CHECK_ITER; NO_CHECK_ITER;
298 NO_CHECK_ITER; NO_CHECK_ITER;
299 NO_CHECK_ITER; NO_CHECK_ITER;
300 NO_CHECK_ITER; NO_CHECK_ITER;
301 }
302 if ((count &= 7) > 0) {
303 do {
304 NO_CHECK_ITER;
305 } while (--count != 0);
306 }
307 }
308 if ((count = range.fCount2) > 0) {
309 sk_memset32_dither(dstC,
310 cache[toggle + range.fV1],
reed@google.com55853db2013-02-01 19:34:59 +0000311 cache[next_dither_toggle(toggle) + range.fV1],
rileya@google.com589708b2012-07-26 20:04:23 +0000312 count);
313 }
314}
315
reedcaf7e932014-12-18 12:43:08 -0800316void shadeSpan_linear_mirror(TileProc proc, SkGradFixed dx, SkGradFixed fx,
rileya@google.com589708b2012-07-26 20:04:23 +0000317 SkPMColor* SK_RESTRICT dstC,
318 const SkPMColor* SK_RESTRICT cache,
319 int toggle, int count) {
320 do {
reedcaf7e932014-12-18 12:43:08 -0800321 unsigned fi = mirror_8bits(SkGradFixedToFixed(fx) >> 8);
rileya@google.com589708b2012-07-26 20:04:23 +0000322 SkASSERT(fi <= 0xFF);
323 fx += dx;
324 *dstC++ = cache[toggle + fi];
reed@google.com55853db2013-02-01 19:34:59 +0000325 toggle = next_dither_toggle(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000326 } while (--count != 0);
327}
328
reedcaf7e932014-12-18 12:43:08 -0800329void shadeSpan_linear_repeat(TileProc proc, SkGradFixed dx, SkGradFixed fx,
rileya@google.com589708b2012-07-26 20:04:23 +0000330 SkPMColor* SK_RESTRICT dstC,
331 const SkPMColor* SK_RESTRICT cache,
332 int toggle, int count) {
333 do {
reedcaf7e932014-12-18 12:43:08 -0800334 unsigned fi = repeat_8bits(SkGradFixedToFixed(fx) >> 8);
rileya@google.com589708b2012-07-26 20:04:23 +0000335 SkASSERT(fi <= 0xFF);
336 fx += dx;
337 *dstC++ = cache[toggle + fi];
reed@google.com55853db2013-02-01 19:34:59 +0000338 toggle = next_dither_toggle(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000339 } while (--count != 0);
340}
341
342}
343
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000344void SkLinearGradient::LinearGradientContext::shadeSpan(int x, int y, SkPMColor* SK_RESTRICT dstC,
345 int count) {
rileya@google.com589708b2012-07-26 20:04:23 +0000346 SkASSERT(count > 0);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000347 const SkLinearGradient& linearGradient = static_cast<const SkLinearGradient&>(fShader);
348
reedf3182eb2015-11-17 08:12:19 -0800349 if (SkShader::kClamp_TileMode == linearGradient.fTileMode &&
350 kLinear_MatrixClass == fDstToIndexClass)
351 {
352 this->shade4_clamp(x, y, dstC, count);
353 return;
354 }
reedf3182eb2015-11-17 08:12:19 -0800355
rileya@google.com589708b2012-07-26 20:04:23 +0000356 SkPoint srcPt;
357 SkMatrix::MapXYProc dstProc = fDstToIndexProc;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000358 TileProc proc = linearGradient.fTileProc;
359 const SkPMColor* SK_RESTRICT cache = fCache->getCache32();
reed@google.com55853db2013-02-01 19:34:59 +0000360 int toggle = init_dither_toggle(x, y);
rileya@google.com589708b2012-07-26 20:04:23 +0000361
362 if (fDstToIndexClass != kPerspective_MatrixClass) {
363 dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf,
364 SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
reedcaf7e932014-12-18 12:43:08 -0800365 SkGradFixed dx, fx = SkScalarToGradFixed(srcPt.fX);
rileya@google.com589708b2012-07-26 20:04:23 +0000366
367 if (fDstToIndexClass == kFixedStepInX_MatrixClass) {
benjaminwagner8e175562016-02-16 10:09:40 -0800368 const auto step = fDstToIndex.fixedStepInX(SkIntToScalar(y));
reedcaf7e932014-12-18 12:43:08 -0800369 // todo: do we need a real/high-precision value for dx here?
benjaminwagner8e175562016-02-16 10:09:40 -0800370 dx = SkScalarToGradFixed(step.fX);
rileya@google.com589708b2012-07-26 20:04:23 +0000371 } else {
372 SkASSERT(fDstToIndexClass == kLinear_MatrixClass);
reedcaf7e932014-12-18 12:43:08 -0800373 dx = SkScalarToGradFixed(fDstToIndex.getScaleX());
rileya@google.com589708b2012-07-26 20:04:23 +0000374 }
375
376 LinearShadeProc shadeProc = shadeSpan_linear_repeat;
reed@google.com53009ba2013-02-07 20:28:49 +0000377 if (0 == dx) {
rileya@google.com589708b2012-07-26 20:04:23 +0000378 shadeProc = shadeSpan_linear_vertical_lerp;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000379 } else if (SkShader::kClamp_TileMode == linearGradient.fTileMode) {
rileya@google.com589708b2012-07-26 20:04:23 +0000380 shadeProc = shadeSpan_linear_clamp;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000381 } else if (SkShader::kMirror_TileMode == linearGradient.fTileMode) {
rileya@google.com589708b2012-07-26 20:04:23 +0000382 shadeProc = shadeSpan_linear_mirror;
383 } else {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000384 SkASSERT(SkShader::kRepeat_TileMode == linearGradient.fTileMode);
rileya@google.com589708b2012-07-26 20:04:23 +0000385 }
386 (*shadeProc)(proc, dx, fx, dstC, cache, toggle, count);
387 } else {
388 SkScalar dstX = SkIntToScalar(x);
389 SkScalar dstY = SkIntToScalar(y);
390 do {
391 dstProc(fDstToIndex, dstX, dstY, &srcPt);
392 unsigned fi = proc(SkScalarToFixed(srcPt.fX));
393 SkASSERT(fi <= 0xFFFF);
394 *dstC++ = cache[toggle + (fi >> kCache32Shift)];
reed@google.com55853db2013-02-01 19:34:59 +0000395 toggle = next_dither_toggle(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000396 dstX += SK_Scalar1;
397 } while (--count != 0);
398 }
399}
400
rileya@google.com589708b2012-07-26 20:04:23 +0000401SkShader::GradientType SkLinearGradient::asAGradient(GradientInfo* info) const {
402 if (info) {
403 commonAsAGradient(info);
404 info->fPoint[0] = fStart;
405 info->fPoint[1] = fEnd;
406 }
407 return kLinear_GradientType;
408}
409
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000410#if SK_SUPPORT_GPU
411
brianosmanb9c51372016-09-15 11:09:45 -0700412#include "GrColorSpaceXform.h"
Brian Salomon94efbf52016-11-29 13:43:05 -0500413#include "GrShaderCaps.h"
egdaniel7ea439b2015-12-03 09:20:44 -0800414#include "glsl/GrGLSLFragmentShaderBuilder.h"
dandov9de5b512014-06-10 14:38:28 -0700415#include "SkGr.h"
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000416
rileya@google.comd7cc6512012-07-27 14:00:39 +0000417/////////////////////////////////////////////////////////////////////
418
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000419class GrLinearGradient : public GrGradientEffect {
420public:
fmenozzi55d318d2016-08-09 08:05:57 -0700421 class GLSLLinearProcessor;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000422
brianosman9557c272016-09-15 06:59:15 -0700423 static sk_sp<GrFragmentProcessor> Make(const CreateArgs& args) {
424 return sk_sp<GrFragmentProcessor>(new GrLinearGradient(args));
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000425 }
426
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000427 virtual ~GrLinearGradient() { }
428
mtklein36352bf2015-03-25 18:17:31 -0700429 const char* name() const override { return "Linear Gradient"; }
joshualitteb2a6762014-12-04 11:35:33 -0800430
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000431private:
brianosman9557c272016-09-15 06:59:15 -0700432 GrLinearGradient(const CreateArgs& args)
433 : INHERITED(args) {
joshualitteb2a6762014-12-04 11:35:33 -0800434 this->initClassID<GrLinearGradient>();
435 }
wangyix4b3050b2015-08-04 07:59:37 -0700436
fmenozzi55d318d2016-08-09 08:05:57 -0700437 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
wangyixb1daa862015-08-18 11:29:31 -0700438
Brian Salomon94efbf52016-11-29 13:43:05 -0500439 virtual void onGetGLSLProcessorKey(const GrShaderCaps& caps,
fmenozzi55d318d2016-08-09 08:05:57 -0700440 GrProcessorKeyBuilder* b) const override;
wangyix4b3050b2015-08-04 07:59:37 -0700441
joshualittb0a8a372014-09-23 09:50:21 -0700442 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000443
444 typedef GrGradientEffect INHERITED;
445};
446
447/////////////////////////////////////////////////////////////////////
448
fmenozzi55d318d2016-08-09 08:05:57 -0700449class GrLinearGradient::GLSLLinearProcessor : public GrGradientEffect::GLSLProcessor {
450public:
451 GLSLLinearProcessor(const GrProcessor&) {}
452
453 virtual ~GLSLLinearProcessor() { }
454
455 virtual void emitCode(EmitArgs&) override;
456
Brian Salomon94efbf52016-11-29 13:43:05 -0500457 static void GenKey(const GrProcessor& processor, const GrShaderCaps&, GrProcessorKeyBuilder* b) {
fmenozzi55d318d2016-08-09 08:05:57 -0700458 b->add32(GenBaseGradientKey(processor));
459 }
460
461private:
462 typedef GrGradientEffect::GLSLProcessor INHERITED;
463};
464
465/////////////////////////////////////////////////////////////////////
466
467GrGLSLFragmentProcessor* GrLinearGradient::onCreateGLSLInstance() const {
468 return new GrLinearGradient::GLSLLinearProcessor(*this);
469}
470
Brian Salomon94efbf52016-11-29 13:43:05 -0500471void GrLinearGradient::onGetGLSLProcessorKey(const GrShaderCaps& caps,
fmenozzi55d318d2016-08-09 08:05:57 -0700472 GrProcessorKeyBuilder* b) const {
473 GrLinearGradient::GLSLLinearProcessor::GenKey(*this, caps, b);
474}
475
476/////////////////////////////////////////////////////////////////////
477
joshualittb0a8a372014-09-23 09:50:21 -0700478GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrLinearGradient);
bsalomon@google.comd4726202012-08-03 14:34:46 +0000479
bungeman06ca8ec2016-06-09 08:01:03 -0700480sk_sp<GrFragmentProcessor> GrLinearGradient::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700481 SkPoint points[] = {{d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()},
482 {d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()}};
bsalomon@google.comd4726202012-08-03 14:34:46 +0000483
Brian Osman3f748602016-10-03 18:29:03 -0400484 RandomGradientParams params(d->fRandom);
Brian Osmana2196532016-10-17 12:48:13 -0400485 auto shader = params.fUseColors4f ?
486 SkGradientShader::MakeLinear(points, params.fColors4f, params.fColorSpace, params.fStops,
487 params.fColorCount, params.fTileMode) :
488 SkGradientShader::MakeLinear(points, params.fColors, params.fStops,
489 params.fColorCount, params.fTileMode);
Brian Osman9f532a32016-10-19 11:12:09 -0400490 GrTest::TestAsFPArgs asFPArgs(d);
491 sk_sp<GrFragmentProcessor> fp = shader->asFragmentProcessor(asFPArgs.args());
bsalomonc21b09e2015-08-28 18:46:56 -0700492 GrAlwaysAssert(fp);
joshualittb0a8a372014-09-23 09:50:21 -0700493 return fp;
bsalomon@google.comd4726202012-08-03 14:34:46 +0000494}
495
496/////////////////////////////////////////////////////////////////////
497
fmenozzi55d318d2016-08-09 08:05:57 -0700498void GrLinearGradient::GLSLLinearProcessor::emitCode(EmitArgs& args) {
wangyix7c157a92015-07-22 15:08:53 -0700499 const GrLinearGradient& ge = args.fFp.cast<GrLinearGradient>();
egdaniel7ea439b2015-12-03 09:20:44 -0800500 this->emitUniforms(args.fUniformHandler, ge);
bsalomon1a1aa932016-09-12 09:30:36 -0700501 SkString t = args.fFragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000502 t.append(".x");
egdaniel7ea439b2015-12-03 09:20:44 -0800503 this->emitColor(args.fFragBuilder,
504 args.fUniformHandler,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500505 args.fShaderCaps,
fmenozzi55d318d2016-08-09 08:05:57 -0700506 ge,
507 t.c_str(),
egdaniel4ca2e602015-11-18 08:01:26 -0800508 args.fOutputColor,
509 args.fInputColor,
cdalton3f6f76f2016-04-11 12:18:09 -0700510 args.fTexSamplers);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000511}
512
513/////////////////////////////////////////////////////////////////////
514
brianosman839345d2016-07-22 11:04:53 -0700515sk_sp<GrFragmentProcessor> SkLinearGradient::asFragmentProcessor(const AsFPArgs& args) const {
516 SkASSERT(args.fContext);
mtklein3f3b3d02014-12-01 11:47:08 -0800517
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000518 SkMatrix matrix;
bsalomon@google.comf94b3a42012-10-31 18:09:01 +0000519 if (!this->getLocalMatrix().invert(&matrix)) {
bsalomonc21b09e2015-08-28 18:46:56 -0700520 return nullptr;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000521 }
brianosman839345d2016-07-22 11:04:53 -0700522 if (args.fLocalMatrix) {
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000523 SkMatrix inv;
brianosman839345d2016-07-22 11:04:53 -0700524 if (!args.fLocalMatrix->invert(&inv)) {
bsalomonc21b09e2015-08-28 18:46:56 -0700525 return nullptr;
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000526 }
527 matrix.postConcat(inv);
528 }
bsalomon@google.comf94b3a42012-10-31 18:09:01 +0000529 matrix.postConcat(fPtsToUnit);
mtklein3f3b3d02014-12-01 11:47:08 -0800530
brianosmanb9c51372016-09-15 11:09:45 -0700531 sk_sp<GrColorSpaceXform> colorSpaceXform = GrColorSpaceXform::Make(fColorSpace.get(),
532 args.fDstColorSpace);
brianosman9557c272016-09-15 06:59:15 -0700533 sk_sp<GrFragmentProcessor> inner(GrLinearGradient::Make(
brianosmanb9c51372016-09-15 11:09:45 -0700534 GrGradientEffect::CreateArgs(args.fContext, this, &matrix, fTileMode,
535 std::move(colorSpaceXform), SkToBool(args.fDstColorSpace))));
bungeman06ca8ec2016-06-09 08:01:03 -0700536 return GrFragmentProcessor::MulOutputByInputAlpha(std::move(inner));
rileya@google.comd7cc6512012-07-27 14:00:39 +0000537}
538
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000539
540#endif
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000541
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000542#ifndef SK_IGNORE_TO_STRING
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000543void SkLinearGradient::toString(SkString* str) const {
544 str->append("SkLinearGradient (");
545
546 str->appendf("start: (%f, %f)", fStart.fX, fStart.fY);
547 str->appendf(" end: (%f, %f) ", fEnd.fX, fEnd.fY);
548
549 this->INHERITED::toString(str);
550
551 str->append(")");
552}
553#endif
reedf3182eb2015-11-17 08:12:19 -0800554
555///////////////////////////////////////////////////////////////////////////////////////////////////
556
557#include "SkNx.h"
558
559static const SkLinearGradient::LinearGradientContext::Rec*
560find_forward(const SkLinearGradient::LinearGradientContext::Rec rec[], float tiledX) {
561 SkASSERT(tiledX >= 0 && tiledX <= 1);
562
563 SkASSERT(rec[0].fPos >= 0 && rec[0].fPos <= 1);
564 SkASSERT(rec[1].fPos >= 0 && rec[1].fPos <= 1);
565 SkASSERT(rec[0].fPos <= rec[1].fPos);
566 rec += 1;
lsalzmanf2b86622016-01-22 14:03:02 -0800567 while (rec->fPos < tiledX || rec->fPosScale == 0) {
reedf3182eb2015-11-17 08:12:19 -0800568 SkASSERT(rec[0].fPos >= 0 && rec[0].fPos <= 1);
569 SkASSERT(rec[1].fPos >= 0 && rec[1].fPos <= 1);
570 SkASSERT(rec[0].fPos <= rec[1].fPos);
571 rec += 1;
572 }
573 return rec - 1;
574}
575
576static const SkLinearGradient::LinearGradientContext::Rec*
577find_backward(const SkLinearGradient::LinearGradientContext::Rec rec[], float tiledX) {
578 SkASSERT(tiledX >= 0 && tiledX <= 1);
579
580 SkASSERT(rec[0].fPos >= 0 && rec[0].fPos <= 1);
581 SkASSERT(rec[1].fPos >= 0 && rec[1].fPos <= 1);
582 SkASSERT(rec[0].fPos <= rec[1].fPos);
lsalzmanf2b86622016-01-22 14:03:02 -0800583 while (tiledX < rec->fPos || rec[1].fPosScale == 0) {
reedf3182eb2015-11-17 08:12:19 -0800584 rec -= 1;
585 SkASSERT(rec[0].fPos >= 0 && rec[0].fPos <= 1);
586 SkASSERT(rec[1].fPos >= 0 && rec[1].fPos <= 1);
587 SkASSERT(rec[0].fPos <= rec[1].fPos);
588 }
589 return rec;
590}
591
fmalita0ce4f232016-12-06 08:57:47 -0800592// As an optimization, we can apply the dither bias before interpolation -- but only when
593// operating in premul space (apply_alpha == false). When apply_alpha == true, we must
594// defer the bias application until after premul.
595//
596// The following two helpers encapsulate this logic: pre_bias is called before interpolation,
597// and effects the bias when apply_alpha == false, while post_bias is called after premul and
598// effects the bias for the apply_alpha == true case.
599
600template <bool apply_alpha>
601Sk4f pre_bias(const Sk4f& x, const Sk4f& bias) {
fmalita0ce4f232016-12-06 08:57:47 -0800602 return apply_alpha ? x : x + bias;
fmalita0ce4f232016-12-06 08:57:47 -0800603}
604
605template <bool apply_alpha>
606Sk4f post_bias(const Sk4f& x, const Sk4f& bias) {
fmalita0ce4f232016-12-06 08:57:47 -0800607 return apply_alpha ? x + bias : x;
fmalita0ce4f232016-12-06 08:57:47 -0800608}
609
610template <bool apply_alpha> SkPMColor trunc_from_255(const Sk4f& x, const Sk4f& bias) {
reedf3182eb2015-11-17 08:12:19 -0800611 SkPMColor c;
fmalita0ce4f232016-12-06 08:57:47 -0800612 Sk4f c4f255 = x;
613 if (apply_alpha) {
614 const float scale = x[SkPM4f::A] * (1 / 255.f);
615 c4f255 *= Sk4f(scale, scale, scale, 1);
616 }
617 SkNx_cast<uint8_t>(post_bias<apply_alpha>(c4f255, bias)).store(&c);
Florin Malitaaaa6d772016-12-07 14:57:04 -0500618
reedf3182eb2015-11-17 08:12:19 -0800619 return c;
620}
621
622template <bool apply_alpha> void fill(SkPMColor dst[], int count,
fmalita0ce4f232016-12-06 08:57:47 -0800623 const Sk4f& c4, const Sk4f& bias0, const Sk4f& bias1) {
624 const SkPMColor c0 = trunc_from_255<apply_alpha>(pre_bias<apply_alpha>(c4, bias0), bias0);
625 const SkPMColor c1 = trunc_from_255<apply_alpha>(pre_bias<apply_alpha>(c4, bias1), bias1);
626 sk_memset32_dither(dst, c0, c1, count);
reedf3182eb2015-11-17 08:12:19 -0800627}
628
629template <bool apply_alpha> void fill(SkPMColor dst[], int count, const Sk4f& c4) {
630 // Assumes that c4 does not need to be dithered.
fmalita0ce4f232016-12-06 08:57:47 -0800631 sk_memset32(dst, trunc_from_255<apply_alpha>(c4, 0), count);
reedf3182eb2015-11-17 08:12:19 -0800632}
633
634/*
635 * TODOs
636 *
637 * - tilemodes
638 * - interp before or after premul
639 * - perspective
640 * - optimizations
641 * - use fixed (32bit or 16bit) instead of floats?
642 */
643
644static Sk4f lerp_color(float fx, const SkLinearGradient::LinearGradientContext::Rec* rec) {
reedde3aac82015-11-22 13:00:04 -0800645 SkASSERT(fx >= rec[0].fPos);
646 SkASSERT(fx <= rec[1].fPos);
647
reedf3182eb2015-11-17 08:12:19 -0800648 const float p0 = rec[0].fPos;
649 const Sk4f c0 = rec[0].fColor;
650 const Sk4f c1 = rec[1].fColor;
651 const Sk4f diffc = c1 - c0;
652 const float scale = rec[1].fPosScale;
653 const float t = (fx - p0) * scale;
654 return c0 + Sk4f(t) * diffc;
655}
656
657template <bool apply_alpha> void ramp(SkPMColor dstC[], int n, const Sk4f& c, const Sk4f& dc,
658 const Sk4f& dither0, const Sk4f& dither1) {
659 Sk4f dc2 = dc + dc;
660 Sk4f dc4 = dc2 + dc2;
fmalita0ce4f232016-12-06 08:57:47 -0800661 Sk4f cd0 = pre_bias<apply_alpha>(c , dither0);
662 Sk4f cd1 = pre_bias<apply_alpha>(c + dc, dither1);
reedf3182eb2015-11-17 08:12:19 -0800663 Sk4f cd2 = cd0 + dc2;
664 Sk4f cd3 = cd1 + dc2;
665 while (n >= 4) {
mtklein9db43ac2015-12-01 07:10:21 -0800666 if (!apply_alpha) {
mtklein6f37b4a2015-12-14 11:25:18 -0800667 Sk4f_ToBytes((uint8_t*)dstC, cd0, cd1, cd2, cd3);
mtklein9db43ac2015-12-01 07:10:21 -0800668 dstC += 4;
669 } else {
fmalita0ce4f232016-12-06 08:57:47 -0800670 *dstC++ = trunc_from_255<apply_alpha>(cd0, dither0);
671 *dstC++ = trunc_from_255<apply_alpha>(cd1, dither1);
672 *dstC++ = trunc_from_255<apply_alpha>(cd2, dither0);
673 *dstC++ = trunc_from_255<apply_alpha>(cd3, dither1);
mtklein9db43ac2015-12-01 07:10:21 -0800674 }
reedf3182eb2015-11-17 08:12:19 -0800675 cd0 = cd0 + dc4;
676 cd1 = cd1 + dc4;
677 cd2 = cd2 + dc4;
678 cd3 = cd3 + dc4;
679 n -= 4;
680 }
681 if (n & 2) {
fmalita0ce4f232016-12-06 08:57:47 -0800682 *dstC++ = trunc_from_255<apply_alpha>(cd0, dither0);
683 *dstC++ = trunc_from_255<apply_alpha>(cd1, dither1);
reedf3182eb2015-11-17 08:12:19 -0800684 cd0 = cd0 + dc2;
685 }
686 if (n & 1) {
fmalita0ce4f232016-12-06 08:57:47 -0800687 *dstC++ = trunc_from_255<apply_alpha>(cd0, dither0);
reedf3182eb2015-11-17 08:12:19 -0800688 }
689}
690
691template <bool apply_alpha, bool dx_is_pos>
692void SkLinearGradient::LinearGradientContext::shade4_dx_clamp(SkPMColor dstC[], int count,
693 float fx, float dx, float invDx,
694 const float dither[2]) {
695 Sk4f dither0(dither[0]);
696 Sk4f dither1(dither[1]);
697 const Rec* rec = fRecs.begin();
698
699 const Sk4f dx4 = Sk4f(dx);
700 SkDEBUGCODE(SkPMColor* endDstC = dstC + count;)
701
702 if (dx_is_pos) {
703 if (fx < 0) {
fmalita7b38e3c2016-05-26 11:13:52 -0700704 // count is guaranteed to be positive, but the first arg may overflow int32 after
705 // increment => casting to uint32 ensures correct clamping.
fmalita7dcb1312016-05-26 18:10:24 -0700706 int n = SkTMin<uint32_t>(static_cast<uint32_t>(SkFloatToIntFloor(-fx * invDx)) + 1,
707 count);
fmalita7b38e3c2016-05-26 11:13:52 -0700708 SkASSERT(n > 0);
reedf3182eb2015-11-17 08:12:19 -0800709 fill<apply_alpha>(dstC, n, rec[0].fColor);
710 count -= n;
711 dstC += n;
712 fx += n * dx;
713 SkASSERT(0 == count || fx >= 0);
714 if (n & 1) {
715 SkTSwap(dither0, dither1);
716 }
717 }
718 } else { // dx < 0
719 if (fx > 1) {
fmalita7b38e3c2016-05-26 11:13:52 -0700720 // count is guaranteed to be positive, but the first arg may overflow int32 after
721 // increment => casting to uint32 ensures correct clamping.
fmalita7dcb1312016-05-26 18:10:24 -0700722 int n = SkTMin<uint32_t>(static_cast<uint32_t>(SkFloatToIntFloor((1 - fx) * invDx)) + 1,
723 count);
fmalita7b38e3c2016-05-26 11:13:52 -0700724 SkASSERT(n > 0);
reedf3182eb2015-11-17 08:12:19 -0800725 fill<apply_alpha>(dstC, n, rec[fRecs.count() - 1].fColor);
726 count -= n;
727 dstC += n;
728 fx += n * dx;
729 SkASSERT(0 == count || fx <= 1);
730 if (n & 1) {
731 SkTSwap(dither0, dither1);
732 }
733 }
734 }
735 SkASSERT(count >= 0);
736
737 const Rec* r;
738 if (dx_is_pos) {
739 r = fRecs.begin(); // start at the beginning
740 } else {
741 r = fRecs.begin() + fRecs.count() - 2; // start at the end
742 }
743
744 while (count > 0) {
745 if (dx_is_pos) {
746 if (fx >= 1) {
747 fill<apply_alpha>(dstC, count, rec[fRecs.count() - 1].fColor);
748 return;
749 }
750 } else { // dx < 0
751 if (fx <= 0) {
752 fill<apply_alpha>(dstC, count, rec[0].fColor);
753 return;
754 }
755 }
756
757 if (dx_is_pos) {
758 r = find_forward(r, fx);
759 } else {
760 r = find_backward(r, fx);
761 }
762 SkASSERT(r >= fRecs.begin() && r < fRecs.begin() + fRecs.count() - 1);
763
764 const float p0 = r[0].fPos;
765 const Sk4f c0 = r[0].fColor;
766 const float p1 = r[1].fPos;
767 const Sk4f diffc = Sk4f(r[1].fColor) - c0;
768 const float scale = r[1].fPosScale;
769 const float t = (fx - p0) * scale;
770 const Sk4f c = c0 + Sk4f(t) * diffc;
771 const Sk4f dc = diffc * dx4 * Sk4f(scale);
772
773 int n;
774 if (dx_is_pos) {
775 n = SkTMin((int)((p1 - fx) * invDx) + 1, count);
776 } else {
777 n = SkTMin((int)((p0 - fx) * invDx) + 1, count);
778 }
779
780 fx += n * dx;
reedaeab8ea2016-01-05 10:01:38 -0800781 // fx should now outside of the p0..p1 interval. However, due to float precision loss,
782 // its possible that fx is slightly too small/large, so we clamp it.
reedf3182eb2015-11-17 08:12:19 -0800783 if (dx_is_pos) {
reedaeab8ea2016-01-05 10:01:38 -0800784 fx = SkTMax(fx, p1);
reedf3182eb2015-11-17 08:12:19 -0800785 } else {
reedaeab8ea2016-01-05 10:01:38 -0800786 fx = SkTMin(fx, p0);
reedf3182eb2015-11-17 08:12:19 -0800787 }
788
789 ramp<apply_alpha>(dstC, n, c, dc, dither0, dither1);
790 dstC += n;
791 SkASSERT(dstC <= endDstC);
mtklein9db43ac2015-12-01 07:10:21 -0800792
reedf3182eb2015-11-17 08:12:19 -0800793 if (n & 1) {
794 SkTSwap(dither0, dither1);
795 }
reedaeab8ea2016-01-05 10:01:38 -0800796
797 count -= n;
798 SkASSERT(count >= 0);
reedf3182eb2015-11-17 08:12:19 -0800799 }
800}
801
802void SkLinearGradient::LinearGradientContext::shade4_clamp(int x, int y, SkPMColor dstC[],
803 int count) {
804 SkASSERT(count > 0);
805 SkASSERT(kLinear_MatrixClass == fDstToIndexClass);
806
807 SkPoint srcPt;
808 fDstToIndexProc(fDstToIndex, x + SK_ScalarHalf, y + SK_ScalarHalf, &srcPt);
809 float fx = srcPt.x();
810 const float dx = fDstToIndex.getScaleX();
811
812 // Default our dither bias values to 1/2, (rounding), which is no dithering
813 float dither0 = 0.5f;
814 float dither1 = 0.5f;
815 if (fDither) {
816 const float ditherCell[] = {
817 1/8.0f, 5/8.0f,
818 7/8.0f, 3/8.0f,
819 };
820 const int rowIndex = (y & 1) << 1;
821 dither0 = ditherCell[rowIndex];
822 dither1 = ditherCell[rowIndex + 1];
823 if (x & 1) {
824 SkTSwap(dither0, dither1);
825 }
826 }
827 const float dither[2] = { dither0, dither1 };
reedf3182eb2015-11-17 08:12:19 -0800828
fmalita2b469132015-11-23 08:30:23 -0800829 if (SkScalarNearlyZero(dx * count)) { // gradient is vertical
reedde3aac82015-11-22 13:00:04 -0800830 const float pinFx = SkTPin(fx, 0.0f, 1.0f);
831 Sk4f c = lerp_color(pinFx, find_forward(fRecs.begin(), pinFx));
reedf3182eb2015-11-17 08:12:19 -0800832 if (fApplyAlphaAfterInterp) {
fmalita0ce4f232016-12-06 08:57:47 -0800833 fill<true>(dstC, count, c, dither0, dither1);
reedf3182eb2015-11-17 08:12:19 -0800834 } else {
fmalita0ce4f232016-12-06 08:57:47 -0800835 fill<false>(dstC, count, c, dither0, dither1);
reedf3182eb2015-11-17 08:12:19 -0800836 }
837 return;
838 }
839
James Zern44e91c92016-11-09 19:22:46 -0800840 SkASSERT(0.f != dx);
841 const float invDx = 1 / dx;
reedf3182eb2015-11-17 08:12:19 -0800842 if (dx > 0) {
843 if (fApplyAlphaAfterInterp) {
844 this->shade4_dx_clamp<true, true>(dstC, count, fx, dx, invDx, dither);
845 } else {
846 this->shade4_dx_clamp<false, true>(dstC, count, fx, dx, invDx, dither);
847 }
848 } else {
849 if (fApplyAlphaAfterInterp) {
850 this->shade4_dx_clamp<true, false>(dstC, count, fx, dx, invDx, dither);
851 } else {
852 this->shade4_dx_clamp<false, false>(dstC, count, fx, dx, invDx, dither);
853 }
854 }
855}