blob: a249ae4ac82c2195ce2e559978cf057ce0a8377a [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
reedf3182eb2015-11-17 08:12:19 -080089// This swizzles SkColor into the same component order as SkPMColor, but does not actually
90// "pre" multiply the color components.
91//
92// This allows us to map directly to Sk4f, and eventually scale down to bytes to output a
93// SkPMColor from the floats, without having to swizzle each time.
94//
95static uint32_t SkSwizzle_Color_to_PMColor(SkColor c) {
96 return SkPackARGB32NoCheck(SkColorGetA(c), SkColorGetR(c), SkColorGetG(c), SkColorGetB(c));
97}
98
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000099SkLinearGradient::LinearGradientContext::LinearGradientContext(
reedf3182eb2015-11-17 08:12:19 -0800100 const SkLinearGradient& shader, const ContextRec& ctx)
101 : INHERITED(shader, ctx)
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000102{
reedf3182eb2015-11-17 08:12:19 -0800103 // setup for Sk4f
fmalita748d6202016-05-11 11:39:58 -0700104 const int count = shader.fColorCount;
105 SkASSERT(count > 1);
106
reedf3182eb2015-11-17 08:12:19 -0800107 fRecs.setCount(count);
108 Rec* rec = fRecs.begin();
109 if (shader.fOrigPos) {
110 rec[0].fPos = 0;
111 SkDEBUGCODE(rec[0].fPosScale = SK_FloatNaN;) // should never get used
112 for (int i = 1; i < count; ++i) {
113 rec[i].fPos = SkTPin(shader.fOrigPos[i], rec[i - 1].fPos, 1.0f);
lsalzmanf2b86622016-01-22 14:03:02 -0800114 float diff = rec[i].fPos - rec[i - 1].fPos;
115 if (diff > 0) {
116 rec[i].fPosScale = 1.0f / diff;
117 } else {
118 rec[i].fPosScale = 0;
119 }
reedf3182eb2015-11-17 08:12:19 -0800120 }
reedf3182eb2015-11-17 08:12:19 -0800121 } else {
122 // no pos specified, so we compute evenly spaced values
123 const float scale = float(count - 1);
fmalita748d6202016-05-11 11:39:58 -0700124 const float invScale = 1.0f / scale;
reedf3182eb2015-11-17 08:12:19 -0800125 for (int i = 0; i < count; ++i) {
126 rec[i].fPos = i * invScale;
127 rec[i].fPosScale = scale;
128 }
129 }
fmalita748d6202016-05-11 11:39:58 -0700130 rec[count - 1].fPos = 1; // overwrite the last value just to be sure we end at 1.0
reedf3182eb2015-11-17 08:12:19 -0800131
132 fApplyAlphaAfterInterp = true;
133 if ((shader.getGradFlags() & SkGradientShader::kInterpolateColorsInPremul_Flag) ||
134 shader.colorsAreOpaque())
135 {
136 fApplyAlphaAfterInterp = false;
137 }
138
139 if (fApplyAlphaAfterInterp) {
140 // Our fColor values are in PMColor order, but are still unpremultiplied, allowing us to
141 // interpolate in unpremultiplied space first, and then scale by alpha right before we
142 // convert to SkPMColor bytes.
143 const float paintAlpha = ctx.fPaint->getAlpha() * kInv255Float;
144 const Sk4f scale(1, 1, 1, paintAlpha);
145 for (int i = 0; i < count; ++i) {
146 uint32_t c = SkSwizzle_Color_to_PMColor(shader.fOrigColors[i]);
mtklein507ef6d2016-01-31 08:02:47 -0800147 rec[i].fColor = SkNx_cast<float>(Sk4b::Load(&c)) * scale;
reedf3182eb2015-11-17 08:12:19 -0800148 if (i > 0) {
149 SkASSERT(rec[i - 1].fPos <= rec[i].fPos);
150 }
151 }
152 } else {
153 // Our fColor values are premultiplied, so converting to SkPMColor is just a matter
154 // of converting the floats down to bytes.
155 unsigned alphaScale = ctx.fPaint->getAlpha() + (ctx.fPaint->getAlpha() >> 7);
156 for (int i = 0; i < count; ++i) {
157 SkPMColor pmc = SkPreMultiplyColor(shader.fOrigColors[i]);
158 pmc = SkAlphaMulQ(pmc, alphaScale);
mtklein507ef6d2016-01-31 08:02:47 -0800159 rec[i].fColor = SkNx_cast<float>(Sk4b::Load(&pmc));
reedf3182eb2015-11-17 08:12:19 -0800160 if (i > 0) {
161 SkASSERT(rec[i - 1].fPos <= rec[i].fPos);
162 }
163 }
164 }
rileya@google.com589708b2012-07-26 20:04:23 +0000165}
166
167#define NO_CHECK_ITER \
168 do { \
reedcaf7e932014-12-18 12:43:08 -0800169 unsigned fi = SkGradFixedToFixed(fx) >> SkGradientShaderBase::kCache32Shift; \
rileya@google.com589708b2012-07-26 20:04:23 +0000170 SkASSERT(fi <= 0xFF); \
171 fx += dx; \
172 *dstC++ = cache[toggle + fi]; \
reed@google.com55853db2013-02-01 19:34:59 +0000173 toggle = next_dither_toggle(toggle); \
rileya@google.com589708b2012-07-26 20:04:23 +0000174 } while (0)
175
176namespace {
177
reedcaf7e932014-12-18 12:43:08 -0800178typedef void (*LinearShadeProc)(TileProc proc, SkGradFixed dx, SkGradFixed fx,
rileya@google.com589708b2012-07-26 20:04:23 +0000179 SkPMColor* dstC, const SkPMColor* cache,
180 int toggle, int count);
181
rileya@google.com589708b2012-07-26 20:04:23 +0000182// Linear interpolation (lerp) is unnecessary if there are no sharp
183// discontinuities in the gradient - which must be true if there are
184// only 2 colors - but it's cheap.
reedcaf7e932014-12-18 12:43:08 -0800185void shadeSpan_linear_vertical_lerp(TileProc proc, SkGradFixed dx, SkGradFixed fx,
rileya@google.com589708b2012-07-26 20:04:23 +0000186 SkPMColor* SK_RESTRICT dstC,
187 const SkPMColor* SK_RESTRICT cache,
188 int toggle, int count) {
189 // We're a vertical gradient, so no change in a span.
190 // If colors change sharply across the gradient, dithering is
191 // insufficient (it subsamples the color space) and we need to lerp.
reedcaf7e932014-12-18 12:43:08 -0800192 unsigned fullIndex = proc(SkGradFixedToFixed(fx));
reed@google.com3c2102c2013-02-01 12:59:40 +0000193 unsigned fi = fullIndex >> SkGradientShaderBase::kCache32Shift;
194 unsigned remainder = fullIndex & ((1 << SkGradientShaderBase::kCache32Shift) - 1);
skia.committer@gmail.com9dde0182013-02-04 12:57:42 +0000195
reed@google.com3c2102c2013-02-01 12:59:40 +0000196 int index0 = fi + toggle;
197 int index1 = index0;
198 if (fi < SkGradientShaderBase::kCache32Count - 1) {
199 index1 += 1;
200 }
201 SkPMColor lerp = SkFastFourByteInterp(cache[index1], cache[index0], remainder);
202 index0 ^= SkGradientShaderBase::kDitherStride32;
203 index1 ^= SkGradientShaderBase::kDitherStride32;
204 SkPMColor dlerp = SkFastFourByteInterp(cache[index1], cache[index0], remainder);
rileya@google.com589708b2012-07-26 20:04:23 +0000205 sk_memset32_dither(dstC, lerp, dlerp, count);
206}
207
reedcaf7e932014-12-18 12:43:08 -0800208void shadeSpan_linear_clamp(TileProc proc, SkGradFixed dx, SkGradFixed fx,
rileya@google.com589708b2012-07-26 20:04:23 +0000209 SkPMColor* SK_RESTRICT dstC,
210 const SkPMColor* SK_RESTRICT cache,
211 int toggle, int count) {
212 SkClampRange range;
reed@google.com3c2102c2013-02-01 12:59:40 +0000213 range.init(fx, dx, count, 0, SkGradientShaderBase::kCache32Count - 1);
reed9d91eb32015-01-28 11:44:48 -0800214 range.validate(count);
rileya@google.com589708b2012-07-26 20:04:23 +0000215
216 if ((count = range.fCount0) > 0) {
217 sk_memset32_dither(dstC,
218 cache[toggle + range.fV0],
reed@google.com55853db2013-02-01 19:34:59 +0000219 cache[next_dither_toggle(toggle) + range.fV0],
rileya@google.com589708b2012-07-26 20:04:23 +0000220 count);
221 dstC += count;
222 }
223 if ((count = range.fCount1) > 0) {
224 int unroll = count >> 3;
225 fx = range.fFx1;
226 for (int i = 0; i < unroll; i++) {
227 NO_CHECK_ITER; NO_CHECK_ITER;
228 NO_CHECK_ITER; NO_CHECK_ITER;
229 NO_CHECK_ITER; NO_CHECK_ITER;
230 NO_CHECK_ITER; NO_CHECK_ITER;
231 }
232 if ((count &= 7) > 0) {
233 do {
234 NO_CHECK_ITER;
235 } while (--count != 0);
236 }
237 }
238 if ((count = range.fCount2) > 0) {
239 sk_memset32_dither(dstC,
240 cache[toggle + range.fV1],
reed@google.com55853db2013-02-01 19:34:59 +0000241 cache[next_dither_toggle(toggle) + range.fV1],
rileya@google.com589708b2012-07-26 20:04:23 +0000242 count);
243 }
244}
245
reedcaf7e932014-12-18 12:43:08 -0800246void shadeSpan_linear_mirror(TileProc proc, SkGradFixed dx, SkGradFixed fx,
rileya@google.com589708b2012-07-26 20:04:23 +0000247 SkPMColor* SK_RESTRICT dstC,
248 const SkPMColor* SK_RESTRICT cache,
249 int toggle, int count) {
250 do {
reedcaf7e932014-12-18 12:43:08 -0800251 unsigned fi = mirror_8bits(SkGradFixedToFixed(fx) >> 8);
rileya@google.com589708b2012-07-26 20:04:23 +0000252 SkASSERT(fi <= 0xFF);
253 fx += dx;
254 *dstC++ = cache[toggle + fi];
reed@google.com55853db2013-02-01 19:34:59 +0000255 toggle = next_dither_toggle(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000256 } while (--count != 0);
257}
258
reedcaf7e932014-12-18 12:43:08 -0800259void shadeSpan_linear_repeat(TileProc proc, SkGradFixed dx, SkGradFixed fx,
rileya@google.com589708b2012-07-26 20:04:23 +0000260 SkPMColor* SK_RESTRICT dstC,
261 const SkPMColor* SK_RESTRICT cache,
262 int toggle, int count) {
263 do {
reedcaf7e932014-12-18 12:43:08 -0800264 unsigned fi = repeat_8bits(SkGradFixedToFixed(fx) >> 8);
rileya@google.com589708b2012-07-26 20:04:23 +0000265 SkASSERT(fi <= 0xFF);
266 fx += dx;
267 *dstC++ = cache[toggle + fi];
reed@google.com55853db2013-02-01 19:34:59 +0000268 toggle = next_dither_toggle(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000269 } while (--count != 0);
270}
271
272}
273
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000274void SkLinearGradient::LinearGradientContext::shadeSpan(int x, int y, SkPMColor* SK_RESTRICT dstC,
275 int count) {
rileya@google.com589708b2012-07-26 20:04:23 +0000276 SkASSERT(count > 0);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000277 const SkLinearGradient& linearGradient = static_cast<const SkLinearGradient&>(fShader);
278
fmalitac2e0ac42015-12-03 09:15:25 -0800279// Only use the Sk4f impl when known to be fast.
280#if defined(SKNX_IS_FAST)
reedf3182eb2015-11-17 08:12:19 -0800281 if (SkShader::kClamp_TileMode == linearGradient.fTileMode &&
282 kLinear_MatrixClass == fDstToIndexClass)
283 {
284 this->shade4_clamp(x, y, dstC, count);
285 return;
286 }
287#endif
288
rileya@google.com589708b2012-07-26 20:04:23 +0000289 SkPoint srcPt;
290 SkMatrix::MapXYProc dstProc = fDstToIndexProc;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000291 TileProc proc = linearGradient.fTileProc;
292 const SkPMColor* SK_RESTRICT cache = fCache->getCache32();
reed@google.com55853db2013-02-01 19:34:59 +0000293 int toggle = init_dither_toggle(x, y);
rileya@google.com589708b2012-07-26 20:04:23 +0000294
295 if (fDstToIndexClass != kPerspective_MatrixClass) {
296 dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf,
297 SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
reedcaf7e932014-12-18 12:43:08 -0800298 SkGradFixed dx, fx = SkScalarToGradFixed(srcPt.fX);
rileya@google.com589708b2012-07-26 20:04:23 +0000299
300 if (fDstToIndexClass == kFixedStepInX_MatrixClass) {
benjaminwagner8e175562016-02-16 10:09:40 -0800301 const auto step = fDstToIndex.fixedStepInX(SkIntToScalar(y));
reedcaf7e932014-12-18 12:43:08 -0800302 // todo: do we need a real/high-precision value for dx here?
benjaminwagner8e175562016-02-16 10:09:40 -0800303 dx = SkScalarToGradFixed(step.fX);
rileya@google.com589708b2012-07-26 20:04:23 +0000304 } else {
305 SkASSERT(fDstToIndexClass == kLinear_MatrixClass);
reedcaf7e932014-12-18 12:43:08 -0800306 dx = SkScalarToGradFixed(fDstToIndex.getScaleX());
rileya@google.com589708b2012-07-26 20:04:23 +0000307 }
308
309 LinearShadeProc shadeProc = shadeSpan_linear_repeat;
reed@google.com53009ba2013-02-07 20:28:49 +0000310 if (0 == dx) {
rileya@google.com589708b2012-07-26 20:04:23 +0000311 shadeProc = shadeSpan_linear_vertical_lerp;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000312 } else if (SkShader::kClamp_TileMode == linearGradient.fTileMode) {
rileya@google.com589708b2012-07-26 20:04:23 +0000313 shadeProc = shadeSpan_linear_clamp;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000314 } else if (SkShader::kMirror_TileMode == linearGradient.fTileMode) {
rileya@google.com589708b2012-07-26 20:04:23 +0000315 shadeProc = shadeSpan_linear_mirror;
316 } else {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000317 SkASSERT(SkShader::kRepeat_TileMode == linearGradient.fTileMode);
rileya@google.com589708b2012-07-26 20:04:23 +0000318 }
319 (*shadeProc)(proc, dx, fx, dstC, cache, toggle, count);
320 } else {
321 SkScalar dstX = SkIntToScalar(x);
322 SkScalar dstY = SkIntToScalar(y);
323 do {
324 dstProc(fDstToIndex, dstX, dstY, &srcPt);
325 unsigned fi = proc(SkScalarToFixed(srcPt.fX));
326 SkASSERT(fi <= 0xFFFF);
327 *dstC++ = cache[toggle + (fi >> kCache32Shift)];
reed@google.com55853db2013-02-01 19:34:59 +0000328 toggle = next_dither_toggle(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000329 dstX += SK_Scalar1;
330 } while (--count != 0);
331 }
332}
333
rileya@google.com589708b2012-07-26 20:04:23 +0000334SkShader::GradientType SkLinearGradient::asAGradient(GradientInfo* info) const {
335 if (info) {
336 commonAsAGradient(info);
337 info->fPoint[0] = fStart;
338 info->fPoint[1] = fEnd;
339 }
340 return kLinear_GradientType;
341}
342
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000343#if SK_SUPPORT_GPU
344
brianosmanb9c51372016-09-15 11:09:45 -0700345#include "GrColorSpaceXform.h"
egdaniel7ea439b2015-12-03 09:20:44 -0800346#include "glsl/GrGLSLCaps.h"
347#include "glsl/GrGLSLFragmentShaderBuilder.h"
dandov9de5b512014-06-10 14:38:28 -0700348#include "SkGr.h"
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000349
rileya@google.comd7cc6512012-07-27 14:00:39 +0000350/////////////////////////////////////////////////////////////////////
351
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000352class GrLinearGradient : public GrGradientEffect {
353public:
fmenozzi55d318d2016-08-09 08:05:57 -0700354 class GLSLLinearProcessor;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000355
brianosman9557c272016-09-15 06:59:15 -0700356 static sk_sp<GrFragmentProcessor> Make(const CreateArgs& args) {
357 return sk_sp<GrFragmentProcessor>(new GrLinearGradient(args));
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000358 }
359
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000360 virtual ~GrLinearGradient() { }
361
mtklein36352bf2015-03-25 18:17:31 -0700362 const char* name() const override { return "Linear Gradient"; }
joshualitteb2a6762014-12-04 11:35:33 -0800363
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000364private:
brianosman9557c272016-09-15 06:59:15 -0700365 GrLinearGradient(const CreateArgs& args)
366 : INHERITED(args) {
joshualitteb2a6762014-12-04 11:35:33 -0800367 this->initClassID<GrLinearGradient>();
368 }
wangyix4b3050b2015-08-04 07:59:37 -0700369
fmenozzi55d318d2016-08-09 08:05:57 -0700370 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
wangyixb1daa862015-08-18 11:29:31 -0700371
egdaniel57d3b032015-11-13 11:57:27 -0800372 virtual void onGetGLSLProcessorKey(const GrGLSLCaps& caps,
fmenozzi55d318d2016-08-09 08:05:57 -0700373 GrProcessorKeyBuilder* b) const override;
wangyix4b3050b2015-08-04 07:59:37 -0700374
joshualittb0a8a372014-09-23 09:50:21 -0700375 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000376
377 typedef GrGradientEffect INHERITED;
378};
379
380/////////////////////////////////////////////////////////////////////
381
fmenozzi55d318d2016-08-09 08:05:57 -0700382class GrLinearGradient::GLSLLinearProcessor : public GrGradientEffect::GLSLProcessor {
383public:
384 GLSLLinearProcessor(const GrProcessor&) {}
385
386 virtual ~GLSLLinearProcessor() { }
387
388 virtual void emitCode(EmitArgs&) override;
389
390 static void GenKey(const GrProcessor& processor, const GrGLSLCaps&, GrProcessorKeyBuilder* b) {
391 b->add32(GenBaseGradientKey(processor));
392 }
393
394private:
395 typedef GrGradientEffect::GLSLProcessor INHERITED;
396};
397
398/////////////////////////////////////////////////////////////////////
399
400GrGLSLFragmentProcessor* GrLinearGradient::onCreateGLSLInstance() const {
401 return new GrLinearGradient::GLSLLinearProcessor(*this);
402}
403
404void GrLinearGradient::onGetGLSLProcessorKey(const GrGLSLCaps& caps,
405 GrProcessorKeyBuilder* b) const {
406 GrLinearGradient::GLSLLinearProcessor::GenKey(*this, caps, b);
407}
408
409/////////////////////////////////////////////////////////////////////
410
joshualittb0a8a372014-09-23 09:50:21 -0700411GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrLinearGradient);
bsalomon@google.comd4726202012-08-03 14:34:46 +0000412
bungeman06ca8ec2016-06-09 08:01:03 -0700413sk_sp<GrFragmentProcessor> GrLinearGradient::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700414 SkPoint points[] = {{d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()},
415 {d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()}};
bsalomon@google.comd4726202012-08-03 14:34:46 +0000416
417 SkColor colors[kMaxRandomGradientColors];
418 SkScalar stopsArray[kMaxRandomGradientColors];
419 SkScalar* stops = stopsArray;
420 SkShader::TileMode tm;
joshualitt0067ff52015-07-08 14:26:19 -0700421 int colorCount = RandomGradientParams(d->fRandom, colors, &stops, &tm);
reed8a21c9f2016-03-08 18:50:00 -0800422 auto shader = SkGradientShader::MakeLinear(points, colors, stops, colorCount, tm);
brianosman839345d2016-07-22 11:04:53 -0700423 SkMatrix viewMatrix = GrTest::TestMatrix(d->fRandom);
Brian Osman0d9dfe92016-10-03 15:24:44 -0400424 auto dstColorSpace = GrTest::TestColorSpace(d->fRandom);
brianosman839345d2016-07-22 11:04:53 -0700425 sk_sp<GrFragmentProcessor> fp = shader->asFragmentProcessor(SkShader::AsFPArgs(
Brian Osman0d9dfe92016-10-03 15:24:44 -0400426 d->fContext, &viewMatrix, NULL, kNone_SkFilterQuality, dstColorSpace.get(),
brianosman1638c0d2016-07-25 05:12:53 -0700427 SkSourceGammaTreatment::kRespect));
bsalomonc21b09e2015-08-28 18:46:56 -0700428 GrAlwaysAssert(fp);
joshualittb0a8a372014-09-23 09:50:21 -0700429 return fp;
bsalomon@google.comd4726202012-08-03 14:34:46 +0000430}
431
432/////////////////////////////////////////////////////////////////////
433
fmenozzi55d318d2016-08-09 08:05:57 -0700434void GrLinearGradient::GLSLLinearProcessor::emitCode(EmitArgs& args) {
wangyix7c157a92015-07-22 15:08:53 -0700435 const GrLinearGradient& ge = args.fFp.cast<GrLinearGradient>();
egdaniel7ea439b2015-12-03 09:20:44 -0800436 this->emitUniforms(args.fUniformHandler, ge);
bsalomon1a1aa932016-09-12 09:30:36 -0700437 SkString t = args.fFragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000438 t.append(".x");
egdaniel7ea439b2015-12-03 09:20:44 -0800439 this->emitColor(args.fFragBuilder,
440 args.fUniformHandler,
egdaniela2e3e0f2015-11-19 07:23:45 -0800441 args.fGLSLCaps,
fmenozzi55d318d2016-08-09 08:05:57 -0700442 ge,
443 t.c_str(),
egdaniel4ca2e602015-11-18 08:01:26 -0800444 args.fOutputColor,
445 args.fInputColor,
cdalton3f6f76f2016-04-11 12:18:09 -0700446 args.fTexSamplers);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000447}
448
449/////////////////////////////////////////////////////////////////////
450
brianosman839345d2016-07-22 11:04:53 -0700451sk_sp<GrFragmentProcessor> SkLinearGradient::asFragmentProcessor(const AsFPArgs& args) const {
452 SkASSERT(args.fContext);
mtklein3f3b3d02014-12-01 11:47:08 -0800453
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000454 SkMatrix matrix;
bsalomon@google.comf94b3a42012-10-31 18:09:01 +0000455 if (!this->getLocalMatrix().invert(&matrix)) {
bsalomonc21b09e2015-08-28 18:46:56 -0700456 return nullptr;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000457 }
brianosman839345d2016-07-22 11:04:53 -0700458 if (args.fLocalMatrix) {
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000459 SkMatrix inv;
brianosman839345d2016-07-22 11:04:53 -0700460 if (!args.fLocalMatrix->invert(&inv)) {
bsalomonc21b09e2015-08-28 18:46:56 -0700461 return nullptr;
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000462 }
463 matrix.postConcat(inv);
464 }
bsalomon@google.comf94b3a42012-10-31 18:09:01 +0000465 matrix.postConcat(fPtsToUnit);
mtklein3f3b3d02014-12-01 11:47:08 -0800466
brianosmanb9c51372016-09-15 11:09:45 -0700467 sk_sp<GrColorSpaceXform> colorSpaceXform = GrColorSpaceXform::Make(fColorSpace.get(),
468 args.fDstColorSpace);
brianosman9557c272016-09-15 06:59:15 -0700469 sk_sp<GrFragmentProcessor> inner(GrLinearGradient::Make(
brianosmanb9c51372016-09-15 11:09:45 -0700470 GrGradientEffect::CreateArgs(args.fContext, this, &matrix, fTileMode,
471 std::move(colorSpaceXform), SkToBool(args.fDstColorSpace))));
bungeman06ca8ec2016-06-09 08:01:03 -0700472 return GrFragmentProcessor::MulOutputByInputAlpha(std::move(inner));
rileya@google.comd7cc6512012-07-27 14:00:39 +0000473}
474
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000475
476#endif
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000477
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000478#ifndef SK_IGNORE_TO_STRING
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000479void SkLinearGradient::toString(SkString* str) const {
480 str->append("SkLinearGradient (");
481
482 str->appendf("start: (%f, %f)", fStart.fX, fStart.fY);
483 str->appendf(" end: (%f, %f) ", fEnd.fX, fEnd.fY);
484
485 this->INHERITED::toString(str);
486
487 str->append(")");
488}
489#endif
reedf3182eb2015-11-17 08:12:19 -0800490
491///////////////////////////////////////////////////////////////////////////////////////////////////
492
493#include "SkNx.h"
494
495static const SkLinearGradient::LinearGradientContext::Rec*
496find_forward(const SkLinearGradient::LinearGradientContext::Rec rec[], float tiledX) {
497 SkASSERT(tiledX >= 0 && tiledX <= 1);
498
499 SkASSERT(rec[0].fPos >= 0 && rec[0].fPos <= 1);
500 SkASSERT(rec[1].fPos >= 0 && rec[1].fPos <= 1);
501 SkASSERT(rec[0].fPos <= rec[1].fPos);
502 rec += 1;
lsalzmanf2b86622016-01-22 14:03:02 -0800503 while (rec->fPos < tiledX || rec->fPosScale == 0) {
reedf3182eb2015-11-17 08:12:19 -0800504 SkASSERT(rec[0].fPos >= 0 && rec[0].fPos <= 1);
505 SkASSERT(rec[1].fPos >= 0 && rec[1].fPos <= 1);
506 SkASSERT(rec[0].fPos <= rec[1].fPos);
507 rec += 1;
508 }
509 return rec - 1;
510}
511
512static const SkLinearGradient::LinearGradientContext::Rec*
513find_backward(const SkLinearGradient::LinearGradientContext::Rec rec[], float tiledX) {
514 SkASSERT(tiledX >= 0 && tiledX <= 1);
515
516 SkASSERT(rec[0].fPos >= 0 && rec[0].fPos <= 1);
517 SkASSERT(rec[1].fPos >= 0 && rec[1].fPos <= 1);
518 SkASSERT(rec[0].fPos <= rec[1].fPos);
lsalzmanf2b86622016-01-22 14:03:02 -0800519 while (tiledX < rec->fPos || rec[1].fPosScale == 0) {
reedf3182eb2015-11-17 08:12:19 -0800520 rec -= 1;
521 SkASSERT(rec[0].fPos >= 0 && rec[0].fPos <= 1);
522 SkASSERT(rec[1].fPos >= 0 && rec[1].fPos <= 1);
523 SkASSERT(rec[0].fPos <= rec[1].fPos);
524 }
525 return rec;
526}
527
528template <bool apply_alpha> SkPMColor trunc_from_255(const Sk4f& x) {
529 SkPMColor c;
mtklein507ef6d2016-01-31 08:02:47 -0800530 SkNx_cast<uint8_t>(x).store(&c);
reedf3182eb2015-11-17 08:12:19 -0800531 if (apply_alpha) {
532 c = SkPreMultiplyARGB(SkGetPackedA32(c), SkGetPackedR32(c),
533 SkGetPackedG32(c), SkGetPackedB32(c));
534 }
535 return c;
536}
537
538template <bool apply_alpha> void fill(SkPMColor dst[], int count,
539 const Sk4f& c4, const Sk4f& c4other) {
540 sk_memset32_dither(dst, trunc_from_255<apply_alpha>(c4),
541 trunc_from_255<apply_alpha>(c4other), count);
542}
543
544template <bool apply_alpha> void fill(SkPMColor dst[], int count, const Sk4f& c4) {
545 // Assumes that c4 does not need to be dithered.
546 sk_memset32(dst, trunc_from_255<apply_alpha>(c4), count);
547}
548
549/*
550 * TODOs
551 *
552 * - tilemodes
553 * - interp before or after premul
554 * - perspective
555 * - optimizations
556 * - use fixed (32bit or 16bit) instead of floats?
557 */
558
559static Sk4f lerp_color(float fx, const SkLinearGradient::LinearGradientContext::Rec* rec) {
reedde3aac82015-11-22 13:00:04 -0800560 SkASSERT(fx >= rec[0].fPos);
561 SkASSERT(fx <= rec[1].fPos);
562
reedf3182eb2015-11-17 08:12:19 -0800563 const float p0 = rec[0].fPos;
564 const Sk4f c0 = rec[0].fColor;
565 const Sk4f c1 = rec[1].fColor;
566 const Sk4f diffc = c1 - c0;
567 const float scale = rec[1].fPosScale;
568 const float t = (fx - p0) * scale;
569 return c0 + Sk4f(t) * diffc;
570}
571
572template <bool apply_alpha> void ramp(SkPMColor dstC[], int n, const Sk4f& c, const Sk4f& dc,
573 const Sk4f& dither0, const Sk4f& dither1) {
574 Sk4f dc2 = dc + dc;
575 Sk4f dc4 = dc2 + dc2;
576 Sk4f cd0 = c + dither0;
577 Sk4f cd1 = c + dc + dither1;
578 Sk4f cd2 = cd0 + dc2;
579 Sk4f cd3 = cd1 + dc2;
580 while (n >= 4) {
mtklein9db43ac2015-12-01 07:10:21 -0800581 if (!apply_alpha) {
mtklein6f37b4a2015-12-14 11:25:18 -0800582 Sk4f_ToBytes((uint8_t*)dstC, cd0, cd1, cd2, cd3);
mtklein9db43ac2015-12-01 07:10:21 -0800583 dstC += 4;
584 } else {
585 *dstC++ = trunc_from_255<apply_alpha>(cd0);
586 *dstC++ = trunc_from_255<apply_alpha>(cd1);
587 *dstC++ = trunc_from_255<apply_alpha>(cd2);
588 *dstC++ = trunc_from_255<apply_alpha>(cd3);
589 }
reedf3182eb2015-11-17 08:12:19 -0800590 cd0 = cd0 + dc4;
591 cd1 = cd1 + dc4;
592 cd2 = cd2 + dc4;
593 cd3 = cd3 + dc4;
594 n -= 4;
595 }
596 if (n & 2) {
597 *dstC++ = trunc_from_255<apply_alpha>(cd0);
598 *dstC++ = trunc_from_255<apply_alpha>(cd1);
599 cd0 = cd0 + dc2;
600 }
601 if (n & 1) {
602 *dstC++ = trunc_from_255<apply_alpha>(cd0);
603 }
604}
605
606template <bool apply_alpha, bool dx_is_pos>
607void SkLinearGradient::LinearGradientContext::shade4_dx_clamp(SkPMColor dstC[], int count,
608 float fx, float dx, float invDx,
609 const float dither[2]) {
610 Sk4f dither0(dither[0]);
611 Sk4f dither1(dither[1]);
612 const Rec* rec = fRecs.begin();
613
614 const Sk4f dx4 = Sk4f(dx);
615 SkDEBUGCODE(SkPMColor* endDstC = dstC + count;)
616
617 if (dx_is_pos) {
618 if (fx < 0) {
fmalita7b38e3c2016-05-26 11:13:52 -0700619 // count is guaranteed to be positive, but the first arg may overflow int32 after
620 // increment => casting to uint32 ensures correct clamping.
fmalita7dcb1312016-05-26 18:10:24 -0700621 int n = SkTMin<uint32_t>(static_cast<uint32_t>(SkFloatToIntFloor(-fx * invDx)) + 1,
622 count);
fmalita7b38e3c2016-05-26 11:13:52 -0700623 SkASSERT(n > 0);
reedf3182eb2015-11-17 08:12:19 -0800624 fill<apply_alpha>(dstC, n, rec[0].fColor);
625 count -= n;
626 dstC += n;
627 fx += n * dx;
628 SkASSERT(0 == count || fx >= 0);
629 if (n & 1) {
630 SkTSwap(dither0, dither1);
631 }
632 }
633 } else { // dx < 0
634 if (fx > 1) {
fmalita7b38e3c2016-05-26 11:13:52 -0700635 // count is guaranteed to be positive, but the first arg may overflow int32 after
636 // increment => casting to uint32 ensures correct clamping.
fmalita7dcb1312016-05-26 18:10:24 -0700637 int n = SkTMin<uint32_t>(static_cast<uint32_t>(SkFloatToIntFloor((1 - fx) * invDx)) + 1,
638 count);
fmalita7b38e3c2016-05-26 11:13:52 -0700639 SkASSERT(n > 0);
reedf3182eb2015-11-17 08:12:19 -0800640 fill<apply_alpha>(dstC, n, rec[fRecs.count() - 1].fColor);
641 count -= n;
642 dstC += n;
643 fx += n * dx;
644 SkASSERT(0 == count || fx <= 1);
645 if (n & 1) {
646 SkTSwap(dither0, dither1);
647 }
648 }
649 }
650 SkASSERT(count >= 0);
651
652 const Rec* r;
653 if (dx_is_pos) {
654 r = fRecs.begin(); // start at the beginning
655 } else {
656 r = fRecs.begin() + fRecs.count() - 2; // start at the end
657 }
658
659 while (count > 0) {
660 if (dx_is_pos) {
661 if (fx >= 1) {
662 fill<apply_alpha>(dstC, count, rec[fRecs.count() - 1].fColor);
663 return;
664 }
665 } else { // dx < 0
666 if (fx <= 0) {
667 fill<apply_alpha>(dstC, count, rec[0].fColor);
668 return;
669 }
670 }
671
672 if (dx_is_pos) {
673 r = find_forward(r, fx);
674 } else {
675 r = find_backward(r, fx);
676 }
677 SkASSERT(r >= fRecs.begin() && r < fRecs.begin() + fRecs.count() - 1);
678
679 const float p0 = r[0].fPos;
680 const Sk4f c0 = r[0].fColor;
681 const float p1 = r[1].fPos;
682 const Sk4f diffc = Sk4f(r[1].fColor) - c0;
683 const float scale = r[1].fPosScale;
684 const float t = (fx - p0) * scale;
685 const Sk4f c = c0 + Sk4f(t) * diffc;
686 const Sk4f dc = diffc * dx4 * Sk4f(scale);
687
688 int n;
689 if (dx_is_pos) {
690 n = SkTMin((int)((p1 - fx) * invDx) + 1, count);
691 } else {
692 n = SkTMin((int)((p0 - fx) * invDx) + 1, count);
693 }
694
695 fx += n * dx;
reedaeab8ea2016-01-05 10:01:38 -0800696 // fx should now outside of the p0..p1 interval. However, due to float precision loss,
697 // its possible that fx is slightly too small/large, so we clamp it.
reedf3182eb2015-11-17 08:12:19 -0800698 if (dx_is_pos) {
reedaeab8ea2016-01-05 10:01:38 -0800699 fx = SkTMax(fx, p1);
reedf3182eb2015-11-17 08:12:19 -0800700 } else {
reedaeab8ea2016-01-05 10:01:38 -0800701 fx = SkTMin(fx, p0);
reedf3182eb2015-11-17 08:12:19 -0800702 }
703
704 ramp<apply_alpha>(dstC, n, c, dc, dither0, dither1);
705 dstC += n;
706 SkASSERT(dstC <= endDstC);
mtklein9db43ac2015-12-01 07:10:21 -0800707
reedf3182eb2015-11-17 08:12:19 -0800708 if (n & 1) {
709 SkTSwap(dither0, dither1);
710 }
reedaeab8ea2016-01-05 10:01:38 -0800711
712 count -= n;
713 SkASSERT(count >= 0);
reedf3182eb2015-11-17 08:12:19 -0800714 }
715}
716
717void SkLinearGradient::LinearGradientContext::shade4_clamp(int x, int y, SkPMColor dstC[],
718 int count) {
719 SkASSERT(count > 0);
720 SkASSERT(kLinear_MatrixClass == fDstToIndexClass);
721
722 SkPoint srcPt;
723 fDstToIndexProc(fDstToIndex, x + SK_ScalarHalf, y + SK_ScalarHalf, &srcPt);
724 float fx = srcPt.x();
725 const float dx = fDstToIndex.getScaleX();
726
727 // Default our dither bias values to 1/2, (rounding), which is no dithering
728 float dither0 = 0.5f;
729 float dither1 = 0.5f;
730 if (fDither) {
731 const float ditherCell[] = {
732 1/8.0f, 5/8.0f,
733 7/8.0f, 3/8.0f,
734 };
735 const int rowIndex = (y & 1) << 1;
736 dither0 = ditherCell[rowIndex];
737 dither1 = ditherCell[rowIndex + 1];
738 if (x & 1) {
739 SkTSwap(dither0, dither1);
740 }
741 }
742 const float dither[2] = { dither0, dither1 };
743 const float invDx = 1 / dx;
744
fmalita2b469132015-11-23 08:30:23 -0800745 if (SkScalarNearlyZero(dx * count)) { // gradient is vertical
reedde3aac82015-11-22 13:00:04 -0800746 const float pinFx = SkTPin(fx, 0.0f, 1.0f);
747 Sk4f c = lerp_color(pinFx, find_forward(fRecs.begin(), pinFx));
reedf3182eb2015-11-17 08:12:19 -0800748 if (fApplyAlphaAfterInterp) {
749 fill<true>(dstC, count, c + dither0, c + dither1);
750 } else {
751 fill<false>(dstC, count, c + dither0, c + dither1);
752 }
753 return;
754 }
755
756 if (dx > 0) {
757 if (fApplyAlphaAfterInterp) {
758 this->shade4_dx_clamp<true, true>(dstC, count, fx, dx, invDx, dither);
759 } else {
760 this->shade4_dx_clamp<false, true>(dstC, count, fx, dx, invDx, dither);
761 }
762 } else {
763 if (fApplyAlphaAfterInterp) {
764 this->shade4_dx_clamp<true, false>(dstC, count, fx, dx, invDx, dither);
765 } else {
766 this->shade4_dx_clamp<false, false>(dstC, count, fx, dx, invDx, dither);
767 }
768 }
769}