blob: d978e5ada044c1f304e5818148351e0909f8a162 [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
8#include "SkLinearGradient.h"
9
10static inline int repeat_bits(int x, const int bits) {
11 return x & ((1 << bits) - 1);
12}
13
14static inline int repeat_8bits(int x) {
15 return x & 0xFF;
16}
17
18// Visual Studio 2010 (MSC_VER=1600) optimizes bit-shift code incorrectly.
19// See http://code.google.com/p/skia/issues/detail?id=472
20#if defined(_MSC_VER) && (_MSC_VER >= 1600)
21#pragma optimize("", off)
22#endif
23
24static inline int mirror_bits(int x, const int bits) {
commit-bot@chromium.org38bad322013-07-30 13:16:29 +000025 if (x & (1 << bits)) {
rileya@google.com589708b2012-07-26 20:04:23 +000026 x = ~x;
commit-bot@chromium.org38bad322013-07-30 13:16:29 +000027 }
rileya@google.com589708b2012-07-26 20:04:23 +000028 return x & ((1 << bits) - 1);
rileya@google.com589708b2012-07-26 20:04:23 +000029}
30
31static inline int mirror_8bits(int x) {
rileya@google.com589708b2012-07-26 20:04:23 +000032 if (x & 256) {
33 x = ~x;
34 }
35 return x & 255;
rileya@google.com589708b2012-07-26 20:04:23 +000036}
37
38#if defined(_MSC_VER) && (_MSC_VER >= 1600)
39#pragma optimize("", on)
40#endif
41
42static void pts_to_unit_matrix(const SkPoint pts[2], SkMatrix* matrix) {
43 SkVector vec = pts[1] - pts[0];
44 SkScalar mag = vec.length();
45 SkScalar inv = mag ? SkScalarInvert(mag) : 0;
46
47 vec.scale(inv);
48 matrix->setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY);
49 matrix->postTranslate(-pts[0].fX, -pts[0].fY);
50 matrix->postScale(inv, inv);
51}
52
53///////////////////////////////////////////////////////////////////////////////
54
reedaddf2ed2014-08-11 08:28:24 -070055SkLinearGradient::SkLinearGradient(const SkPoint pts[2], const Descriptor& desc)
56 : SkGradientShaderBase(desc)
rileya@google.com589708b2012-07-26 20:04:23 +000057 , fStart(pts[0])
reedaddf2ed2014-08-11 08:28:24 -070058 , fEnd(pts[1])
59{
rileya@google.com589708b2012-07-26 20:04:23 +000060 pts_to_unit_matrix(pts, &fPtsToUnit);
61}
62
reed9fa60da2014-08-21 07:59:51 -070063#ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000064SkLinearGradient::SkLinearGradient(SkReadBuffer& buffer)
rileya@google.com589708b2012-07-26 20:04:23 +000065 : INHERITED(buffer)
66 , fStart(buffer.readPoint())
67 , fEnd(buffer.readPoint()) {
68}
reed9fa60da2014-08-21 07:59:51 -070069#endif
70
71SkFlattenable* SkLinearGradient::CreateProc(SkReadBuffer& buffer) {
72 DescriptorScope desc;
73 if (!desc.unflatten(buffer)) {
74 return NULL;
75 }
76 SkPoint pts[2];
77 pts[0] = buffer.readPoint();
78 pts[1] = buffer.readPoint();
79 return SkGradientShader::CreateLinear(pts, desc.fColors, desc.fPos, desc.fCount,
80 desc.fTileMode, desc.fGradFlags, desc.fLocalMatrix);
81}
rileya@google.com589708b2012-07-26 20:04:23 +000082
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000083void SkLinearGradient::flatten(SkWriteBuffer& buffer) const {
rileya@google.com589708b2012-07-26 20:04:23 +000084 this->INHERITED::flatten(buffer);
85 buffer.writePoint(fStart);
86 buffer.writePoint(fEnd);
87}
88
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000089size_t SkLinearGradient::contextSize() const {
90 return sizeof(LinearGradientContext);
91}
92
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +000093SkShader::Context* SkLinearGradient::onCreateContext(const ContextRec& rec, void* storage) const {
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +000094 return SkNEW_PLACEMENT_ARGS(storage, LinearGradientContext, (*this, rec));
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000095}
96
97SkLinearGradient::LinearGradientContext::LinearGradientContext(
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +000098 const SkLinearGradient& shader, const ContextRec& rec)
99 : INHERITED(shader, rec)
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000100{
rileya@google.com589708b2012-07-26 20:04:23 +0000101 unsigned mask = SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask;
102 if ((fDstToIndex.getType() & ~mask) == 0) {
reed@google.com60040292013-02-04 18:21:23 +0000103 // when we dither, we are (usually) not const-in-Y
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000104 if ((fFlags & SkShader::kHasSpan16_Flag) && !rec.fPaint->isDither()) {
rileya@google.com589708b2012-07-26 20:04:23 +0000105 // only claim this if we do have a 16bit mode (i.e. none of our
106 // colors have alpha), and if we are not dithering (which obviously
107 // is not const in Y).
108 fFlags |= SkShader::kConstInY16_Flag;
109 }
110 }
rileya@google.com589708b2012-07-26 20:04:23 +0000111}
112
113#define NO_CHECK_ITER \
114 do { \
115 unsigned fi = fx >> SkGradientShaderBase::kCache32Shift; \
116 SkASSERT(fi <= 0xFF); \
117 fx += dx; \
118 *dstC++ = cache[toggle + fi]; \
reed@google.com55853db2013-02-01 19:34:59 +0000119 toggle = next_dither_toggle(toggle); \
rileya@google.com589708b2012-07-26 20:04:23 +0000120 } while (0)
121
122namespace {
123
124typedef void (*LinearShadeProc)(TileProc proc, SkFixed dx, SkFixed fx,
125 SkPMColor* dstC, const SkPMColor* cache,
126 int toggle, int count);
127
rileya@google.com589708b2012-07-26 20:04:23 +0000128// Linear interpolation (lerp) is unnecessary if there are no sharp
129// discontinuities in the gradient - which must be true if there are
130// only 2 colors - but it's cheap.
131void shadeSpan_linear_vertical_lerp(TileProc proc, SkFixed dx, SkFixed fx,
132 SkPMColor* SK_RESTRICT dstC,
133 const SkPMColor* SK_RESTRICT cache,
134 int toggle, int count) {
135 // We're a vertical gradient, so no change in a span.
136 // If colors change sharply across the gradient, dithering is
137 // insufficient (it subsamples the color space) and we need to lerp.
138 unsigned fullIndex = proc(fx);
reed@google.com3c2102c2013-02-01 12:59:40 +0000139 unsigned fi = fullIndex >> SkGradientShaderBase::kCache32Shift;
140 unsigned remainder = fullIndex & ((1 << SkGradientShaderBase::kCache32Shift) - 1);
skia.committer@gmail.com9dde0182013-02-04 12:57:42 +0000141
reed@google.com3c2102c2013-02-01 12:59:40 +0000142 int index0 = fi + toggle;
143 int index1 = index0;
144 if (fi < SkGradientShaderBase::kCache32Count - 1) {
145 index1 += 1;
146 }
147 SkPMColor lerp = SkFastFourByteInterp(cache[index1], cache[index0], remainder);
148 index0 ^= SkGradientShaderBase::kDitherStride32;
149 index1 ^= SkGradientShaderBase::kDitherStride32;
150 SkPMColor dlerp = SkFastFourByteInterp(cache[index1], cache[index0], remainder);
rileya@google.com589708b2012-07-26 20:04:23 +0000151 sk_memset32_dither(dstC, lerp, dlerp, count);
152}
153
154void shadeSpan_linear_clamp(TileProc proc, SkFixed dx, SkFixed fx,
155 SkPMColor* SK_RESTRICT dstC,
156 const SkPMColor* SK_RESTRICT cache,
157 int toggle, int count) {
158 SkClampRange range;
reed@google.com3c2102c2013-02-01 12:59:40 +0000159 range.init(fx, dx, count, 0, SkGradientShaderBase::kCache32Count - 1);
rileya@google.com589708b2012-07-26 20:04:23 +0000160
161 if ((count = range.fCount0) > 0) {
162 sk_memset32_dither(dstC,
163 cache[toggle + range.fV0],
reed@google.com55853db2013-02-01 19:34:59 +0000164 cache[next_dither_toggle(toggle) + range.fV0],
rileya@google.com589708b2012-07-26 20:04:23 +0000165 count);
166 dstC += count;
167 }
168 if ((count = range.fCount1) > 0) {
169 int unroll = count >> 3;
170 fx = range.fFx1;
171 for (int i = 0; i < unroll; i++) {
172 NO_CHECK_ITER; NO_CHECK_ITER;
173 NO_CHECK_ITER; NO_CHECK_ITER;
174 NO_CHECK_ITER; NO_CHECK_ITER;
175 NO_CHECK_ITER; NO_CHECK_ITER;
176 }
177 if ((count &= 7) > 0) {
178 do {
179 NO_CHECK_ITER;
180 } while (--count != 0);
181 }
182 }
183 if ((count = range.fCount2) > 0) {
184 sk_memset32_dither(dstC,
185 cache[toggle + range.fV1],
reed@google.com55853db2013-02-01 19:34:59 +0000186 cache[next_dither_toggle(toggle) + range.fV1],
rileya@google.com589708b2012-07-26 20:04:23 +0000187 count);
188 }
189}
190
191void shadeSpan_linear_mirror(TileProc proc, SkFixed dx, SkFixed fx,
192 SkPMColor* SK_RESTRICT dstC,
193 const SkPMColor* SK_RESTRICT cache,
194 int toggle, int count) {
195 do {
196 unsigned fi = mirror_8bits(fx >> 8);
197 SkASSERT(fi <= 0xFF);
198 fx += dx;
199 *dstC++ = cache[toggle + fi];
reed@google.com55853db2013-02-01 19:34:59 +0000200 toggle = next_dither_toggle(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000201 } while (--count != 0);
202}
203
204void shadeSpan_linear_repeat(TileProc proc, SkFixed dx, SkFixed fx,
205 SkPMColor* SK_RESTRICT dstC,
206 const SkPMColor* SK_RESTRICT cache,
207 int toggle, int count) {
208 do {
209 unsigned fi = repeat_8bits(fx >> 8);
210 SkASSERT(fi <= 0xFF);
211 fx += dx;
212 *dstC++ = cache[toggle + fi];
reed@google.com55853db2013-02-01 19:34:59 +0000213 toggle = next_dither_toggle(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000214 } while (--count != 0);
215}
216
217}
218
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000219void SkLinearGradient::LinearGradientContext::shadeSpan(int x, int y, SkPMColor* SK_RESTRICT dstC,
220 int count) {
rileya@google.com589708b2012-07-26 20:04:23 +0000221 SkASSERT(count > 0);
222
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000223 const SkLinearGradient& linearGradient = static_cast<const SkLinearGradient&>(fShader);
224
rileya@google.com589708b2012-07-26 20:04:23 +0000225 SkPoint srcPt;
226 SkMatrix::MapXYProc dstProc = fDstToIndexProc;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000227 TileProc proc = linearGradient.fTileProc;
228 const SkPMColor* SK_RESTRICT cache = fCache->getCache32();
reed@google.com55853db2013-02-01 19:34:59 +0000229 int toggle = init_dither_toggle(x, y);
rileya@google.com589708b2012-07-26 20:04:23 +0000230
231 if (fDstToIndexClass != kPerspective_MatrixClass) {
232 dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf,
233 SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
234 SkFixed dx, fx = SkScalarToFixed(srcPt.fX);
235
236 if (fDstToIndexClass == kFixedStepInX_MatrixClass) {
237 SkFixed dxStorage[1];
238 (void)fDstToIndex.fixedStepInX(SkIntToScalar(y), dxStorage, NULL);
239 dx = dxStorage[0];
240 } else {
241 SkASSERT(fDstToIndexClass == kLinear_MatrixClass);
242 dx = SkScalarToFixed(fDstToIndex.getScaleX());
243 }
244
245 LinearShadeProc shadeProc = shadeSpan_linear_repeat;
reed@google.com53009ba2013-02-07 20:28:49 +0000246 if (0 == dx) {
rileya@google.com589708b2012-07-26 20:04:23 +0000247 shadeProc = shadeSpan_linear_vertical_lerp;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000248 } else if (SkShader::kClamp_TileMode == linearGradient.fTileMode) {
rileya@google.com589708b2012-07-26 20:04:23 +0000249 shadeProc = shadeSpan_linear_clamp;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000250 } else if (SkShader::kMirror_TileMode == linearGradient.fTileMode) {
rileya@google.com589708b2012-07-26 20:04:23 +0000251 shadeProc = shadeSpan_linear_mirror;
252 } else {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000253 SkASSERT(SkShader::kRepeat_TileMode == linearGradient.fTileMode);
rileya@google.com589708b2012-07-26 20:04:23 +0000254 }
255 (*shadeProc)(proc, dx, fx, dstC, cache, toggle, count);
256 } else {
257 SkScalar dstX = SkIntToScalar(x);
258 SkScalar dstY = SkIntToScalar(y);
259 do {
260 dstProc(fDstToIndex, dstX, dstY, &srcPt);
261 unsigned fi = proc(SkScalarToFixed(srcPt.fX));
262 SkASSERT(fi <= 0xFFFF);
263 *dstC++ = cache[toggle + (fi >> kCache32Shift)];
reed@google.com55853db2013-02-01 19:34:59 +0000264 toggle = next_dither_toggle(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000265 dstX += SK_Scalar1;
266 } while (--count != 0);
267 }
268}
269
270SkShader::BitmapType SkLinearGradient::asABitmap(SkBitmap* bitmap,
271 SkMatrix* matrix,
272 TileMode xy[]) const {
273 if (bitmap) {
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000274 this->getGradientTableBitmap(bitmap);
rileya@google.com589708b2012-07-26 20:04:23 +0000275 }
276 if (matrix) {
277 matrix->preConcat(fPtsToUnit);
278 }
279 if (xy) {
280 xy[0] = fTileMode;
281 xy[1] = kClamp_TileMode;
282 }
283 return kLinear_BitmapType;
284}
285
286SkShader::GradientType SkLinearGradient::asAGradient(GradientInfo* info) const {
287 if (info) {
288 commonAsAGradient(info);
289 info->fPoint[0] = fStart;
290 info->fPoint[1] = fEnd;
291 }
292 return kLinear_GradientType;
293}
294
rileya@google.com589708b2012-07-26 20:04:23 +0000295static void dither_memset16(uint16_t dst[], uint16_t value, uint16_t other,
296 int count) {
297 if (reinterpret_cast<uintptr_t>(dst) & 2) {
298 *dst++ = value;
299 count -= 1;
300 SkTSwap(value, other);
301 }
302
303 sk_memset32((uint32_t*)dst, (value << 16) | other, count >> 1);
304
305 if (count & 1) {
306 dst[count - 1] = value;
307 }
308}
309
310#define NO_CHECK_ITER_16 \
311 do { \
312 unsigned fi = fx >> SkGradientShaderBase::kCache16Shift; \
313 SkASSERT(fi < SkGradientShaderBase::kCache16Count); \
314 fx += dx; \
315 *dstC++ = cache[toggle + fi]; \
reed@google.com55853db2013-02-01 19:34:59 +0000316 toggle = next_dither_toggle16(toggle); \
rileya@google.com589708b2012-07-26 20:04:23 +0000317 } while (0)
318
319namespace {
320
321typedef void (*LinearShade16Proc)(TileProc proc, SkFixed dx, SkFixed fx,
322 uint16_t* dstC, const uint16_t* cache,
323 int toggle, int count);
324
325void shadeSpan16_linear_vertical(TileProc proc, SkFixed dx, SkFixed fx,
326 uint16_t* SK_RESTRICT dstC,
327 const uint16_t* SK_RESTRICT cache,
328 int toggle, int count) {
329 // we're a vertical gradient, so no change in a span
330 unsigned fi = proc(fx) >> SkGradientShaderBase::kCache16Shift;
331 SkASSERT(fi < SkGradientShaderBase::kCache16Count);
332 dither_memset16(dstC, cache[toggle + fi],
reed@google.com55853db2013-02-01 19:34:59 +0000333 cache[next_dither_toggle16(toggle) + fi], count);
rileya@google.com589708b2012-07-26 20:04:23 +0000334}
335
336void shadeSpan16_linear_clamp(TileProc proc, SkFixed dx, SkFixed fx,
337 uint16_t* SK_RESTRICT dstC,
338 const uint16_t* SK_RESTRICT cache,
339 int toggle, int count) {
340 SkClampRange range;
reed@google.com3c2102c2013-02-01 12:59:40 +0000341 range.init(fx, dx, count, 0, SkGradientShaderBase::kCache32Count - 1);
rileya@google.com589708b2012-07-26 20:04:23 +0000342
343 if ((count = range.fCount0) > 0) {
344 dither_memset16(dstC,
345 cache[toggle + range.fV0],
reed@google.com55853db2013-02-01 19:34:59 +0000346 cache[next_dither_toggle16(toggle) + range.fV0],
rileya@google.com589708b2012-07-26 20:04:23 +0000347 count);
348 dstC += count;
349 }
350 if ((count = range.fCount1) > 0) {
351 int unroll = count >> 3;
352 fx = range.fFx1;
353 for (int i = 0; i < unroll; i++) {
354 NO_CHECK_ITER_16; NO_CHECK_ITER_16;
355 NO_CHECK_ITER_16; NO_CHECK_ITER_16;
356 NO_CHECK_ITER_16; NO_CHECK_ITER_16;
357 NO_CHECK_ITER_16; NO_CHECK_ITER_16;
358 }
359 if ((count &= 7) > 0) {
360 do {
361 NO_CHECK_ITER_16;
362 } while (--count != 0);
363 }
364 }
365 if ((count = range.fCount2) > 0) {
366 dither_memset16(dstC,
367 cache[toggle + range.fV1],
reed@google.com55853db2013-02-01 19:34:59 +0000368 cache[next_dither_toggle16(toggle) + range.fV1],
rileya@google.com589708b2012-07-26 20:04:23 +0000369 count);
370 }
371}
372
373void shadeSpan16_linear_mirror(TileProc proc, SkFixed dx, SkFixed fx,
374 uint16_t* SK_RESTRICT dstC,
375 const uint16_t* SK_RESTRICT cache,
376 int toggle, int count) {
377 do {
378 unsigned fi = mirror_bits(fx >> SkGradientShaderBase::kCache16Shift,
379 SkGradientShaderBase::kCache16Bits);
380 SkASSERT(fi < SkGradientShaderBase::kCache16Count);
381 fx += dx;
382 *dstC++ = cache[toggle + fi];
reed@google.com55853db2013-02-01 19:34:59 +0000383 toggle = next_dither_toggle16(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000384 } while (--count != 0);
385}
386
387void shadeSpan16_linear_repeat(TileProc proc, SkFixed dx, SkFixed fx,
388 uint16_t* SK_RESTRICT dstC,
389 const uint16_t* SK_RESTRICT cache,
390 int toggle, int count) {
391 do {
392 unsigned fi = repeat_bits(fx >> SkGradientShaderBase::kCache16Shift,
393 SkGradientShaderBase::kCache16Bits);
394 SkASSERT(fi < SkGradientShaderBase::kCache16Count);
395 fx += dx;
396 *dstC++ = cache[toggle + fi];
reed@google.com55853db2013-02-01 19:34:59 +0000397 toggle = next_dither_toggle16(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000398 } while (--count != 0);
399}
400}
401
reed@google.com1915fd02013-12-19 14:22:03 +0000402static bool fixed_nearly_zero(SkFixed x) {
403 return SkAbs32(x) < (SK_Fixed1 >> 12);
404}
405
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000406void SkLinearGradient::LinearGradientContext::shadeSpan16(int x, int y,
407 uint16_t* SK_RESTRICT dstC, int count) {
rileya@google.com589708b2012-07-26 20:04:23 +0000408 SkASSERT(count > 0);
409
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000410 const SkLinearGradient& linearGradient = static_cast<const SkLinearGradient&>(fShader);
411
rileya@google.com589708b2012-07-26 20:04:23 +0000412 SkPoint srcPt;
413 SkMatrix::MapXYProc dstProc = fDstToIndexProc;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000414 TileProc proc = linearGradient.fTileProc;
415 const uint16_t* SK_RESTRICT cache = fCache->getCache16();
reed@google.com55853db2013-02-01 19:34:59 +0000416 int toggle = init_dither_toggle16(x, y);
rileya@google.com589708b2012-07-26 20:04:23 +0000417
418 if (fDstToIndexClass != kPerspective_MatrixClass) {
419 dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf,
420 SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
421 SkFixed dx, fx = SkScalarToFixed(srcPt.fX);
422
423 if (fDstToIndexClass == kFixedStepInX_MatrixClass) {
424 SkFixed dxStorage[1];
425 (void)fDstToIndex.fixedStepInX(SkIntToScalar(y), dxStorage, NULL);
426 dx = dxStorage[0];
427 } else {
428 SkASSERT(fDstToIndexClass == kLinear_MatrixClass);
429 dx = SkScalarToFixed(fDstToIndex.getScaleX());
430 }
431
432 LinearShade16Proc shadeProc = shadeSpan16_linear_repeat;
reed@google.com1915fd02013-12-19 14:22:03 +0000433 if (fixed_nearly_zero(dx)) {
rileya@google.com589708b2012-07-26 20:04:23 +0000434 shadeProc = shadeSpan16_linear_vertical;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000435 } else if (SkShader::kClamp_TileMode == linearGradient.fTileMode) {
rileya@google.com589708b2012-07-26 20:04:23 +0000436 shadeProc = shadeSpan16_linear_clamp;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000437 } else if (SkShader::kMirror_TileMode == linearGradient.fTileMode) {
rileya@google.com589708b2012-07-26 20:04:23 +0000438 shadeProc = shadeSpan16_linear_mirror;
439 } else {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000440 SkASSERT(SkShader::kRepeat_TileMode == linearGradient.fTileMode);
rileya@google.com589708b2012-07-26 20:04:23 +0000441 }
442 (*shadeProc)(proc, dx, fx, dstC, cache, toggle, count);
443 } else {
444 SkScalar dstX = SkIntToScalar(x);
445 SkScalar dstY = SkIntToScalar(y);
446 do {
447 dstProc(fDstToIndex, dstX, dstY, &srcPt);
448 unsigned fi = proc(SkScalarToFixed(srcPt.fX));
449 SkASSERT(fi <= 0xFFFF);
450
451 int index = fi >> kCache16Shift;
452 *dstC++ = cache[toggle + index];
reed@google.com55853db2013-02-01 19:34:59 +0000453 toggle = next_dither_toggle16(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000454
455 dstX += SK_Scalar1;
456 } while (--count != 0);
457 }
458}
459
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000460#if SK_SUPPORT_GPU
461
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000462#include "GrTBackendEffectFactory.h"
joshualitt30ba4362014-08-21 20:18:45 -0700463#include "gl/builders/GrGLProgramBuilder.h"
dandov9de5b512014-06-10 14:38:28 -0700464#include "SkGr.h"
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000465
rileya@google.comd7cc6512012-07-27 14:00:39 +0000466/////////////////////////////////////////////////////////////////////
467
bsalomon@google.com0707c292012-10-25 21:45:42 +0000468class GrGLLinearGradient : public GrGLGradientEffect {
rileya@google.comd7cc6512012-07-27 14:00:39 +0000469public:
470
joshualitt49586be2014-09-16 08:21:41 -0700471 GrGLLinearGradient(const GrBackendEffectFactory& factory, const GrEffect&)
rileya@google.comd7cc6512012-07-27 14:00:39 +0000472 : INHERITED (factory) { }
473
474 virtual ~GrGLLinearGradient() { }
475
joshualitt30ba4362014-08-21 20:18:45 -0700476 virtual void emitCode(GrGLProgramBuilder*,
joshualitt49586be2014-09-16 08:21:41 -0700477 const GrEffect&,
bsalomon63e99f72014-07-21 08:03:14 -0700478 const GrEffectKey&,
bsalomon@google.comf78df332012-10-29 12:43:38 +0000479 const char* outputColor,
480 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000481 const TransformedCoordsArray&,
bsalomon@google.comf78df332012-10-29 12:43:38 +0000482 const TextureSamplerArray&) SK_OVERRIDE;
483
joshualitt49586be2014-09-16 08:21:41 -0700484 static void GenKey(const GrEffect& effect, const GrGLCaps&, GrEffectKeyBuilder* b) {
485 b->add32(GenBaseGradientKey(effect));
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000486 }
rileya@google.comd7cc6512012-07-27 14:00:39 +0000487
488private:
489
bsalomon@google.com0707c292012-10-25 21:45:42 +0000490 typedef GrGLGradientEffect INHERITED;
rileya@google.comd7cc6512012-07-27 14:00:39 +0000491};
492
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000493/////////////////////////////////////////////////////////////////////
494
495class GrLinearGradient : public GrGradientEffect {
496public:
497
bsalomon83d081a2014-07-08 09:56:10 -0700498 static GrEffect* Create(GrContext* ctx,
499 const SkLinearGradient& shader,
500 const SkMatrix& matrix,
501 SkShader::TileMode tm) {
bsalomon55fad7a2014-07-08 07:34:20 -0700502 return SkNEW_ARGS(GrLinearGradient, (ctx, shader, matrix, tm));
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000503 }
504
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000505 virtual ~GrLinearGradient() { }
506
507 static const char* Name() { return "Linear Gradient"; }
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000508 const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
509 return GrTBackendEffectFactory<GrLinearGradient>::getInstance();
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000510 }
511
bsalomon@google.com422e81a2012-10-25 14:11:03 +0000512 typedef GrGLLinearGradient GLEffect;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000513
514private:
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000515 GrLinearGradient(GrContext* ctx,
516 const SkLinearGradient& shader,
517 const SkMatrix& matrix,
518 SkShader::TileMode tm)
519 : INHERITED(ctx, shader, matrix, tm) { }
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000520 GR_DECLARE_EFFECT_TEST;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000521
522 typedef GrGradientEffect INHERITED;
523};
524
525/////////////////////////////////////////////////////////////////////
526
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000527GR_DEFINE_EFFECT_TEST(GrLinearGradient);
bsalomon@google.comd4726202012-08-03 14:34:46 +0000528
bsalomon83d081a2014-07-08 09:56:10 -0700529GrEffect* GrLinearGradient::TestCreate(SkRandom* random,
530 GrContext* context,
531 const GrDrawTargetCaps&,
532 GrTexture**) {
bsalomon@google.comd4726202012-08-03 14:34:46 +0000533 SkPoint points[] = {{random->nextUScalar1(), random->nextUScalar1()},
534 {random->nextUScalar1(), random->nextUScalar1()}};
535
536 SkColor colors[kMaxRandomGradientColors];
537 SkScalar stopsArray[kMaxRandomGradientColors];
538 SkScalar* stops = stopsArray;
539 SkShader::TileMode tm;
540 int colorCount = RandomGradientParams(random, colors, &stops, &tm);
541 SkAutoTUnref<SkShader> shader(SkGradientShader::CreateLinear(points,
542 colors, stops, colorCount,
543 tm));
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000544 SkPaint paint;
bsalomon83d081a2014-07-08 09:56:10 -0700545 GrColor paintColor;
546 GrEffect* effect;
547 SkAssertResult(shader->asNewEffect(context, paint, NULL, &paintColor, &effect));
dandov9de5b512014-06-10 14:38:28 -0700548 return effect;
bsalomon@google.comd4726202012-08-03 14:34:46 +0000549}
550
551/////////////////////////////////////////////////////////////////////
552
joshualitt30ba4362014-08-21 20:18:45 -0700553void GrGLLinearGradient::emitCode(GrGLProgramBuilder* builder,
joshualitt49586be2014-09-16 08:21:41 -0700554 const GrEffect&,
bsalomon63e99f72014-07-21 08:03:14 -0700555 const GrEffectKey& key,
bsalomon@google.comf78df332012-10-29 12:43:38 +0000556 const char* outputColor,
557 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000558 const TransformedCoordsArray& coords,
bsalomon@google.comf78df332012-10-29 12:43:38 +0000559 const TextureSamplerArray& samplers) {
bsalomon63e99f72014-07-21 08:03:14 -0700560 uint32_t baseKey = key.get32(0);
561 this->emitUniforms(builder, baseKey);
joshualitt30ba4362014-08-21 20:18:45 -0700562 SkString t = builder->getFragmentShaderBuilder()->ensureFSCoords2D(coords, 0);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000563 t.append(".x");
bsalomon63e99f72014-07-21 08:03:14 -0700564 this->emitColor(builder, t.c_str(), baseKey, outputColor, inputColor, samplers);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000565}
566
567/////////////////////////////////////////////////////////////////////
568
dandov9de5b512014-06-10 14:38:28 -0700569bool SkLinearGradient::asNewEffect(GrContext* context, const SkPaint& paint,
bsalomon83d081a2014-07-08 09:56:10 -0700570 const SkMatrix* localMatrix, GrColor* paintColor,
571 GrEffect** effect) const {
bsalomon49f085d2014-09-05 13:34:00 -0700572 SkASSERT(context);
dandov9de5b512014-06-10 14:38:28 -0700573
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000574 SkMatrix matrix;
bsalomon@google.comf94b3a42012-10-31 18:09:01 +0000575 if (!this->getLocalMatrix().invert(&matrix)) {
dandov9de5b512014-06-10 14:38:28 -0700576 return false;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000577 }
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000578 if (localMatrix) {
579 SkMatrix inv;
580 if (!localMatrix->invert(&inv)) {
dandov9de5b512014-06-10 14:38:28 -0700581 return false;
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000582 }
583 matrix.postConcat(inv);
584 }
bsalomon@google.comf94b3a42012-10-31 18:09:01 +0000585 matrix.postConcat(fPtsToUnit);
dandov9de5b512014-06-10 14:38:28 -0700586
bsalomon83d081a2014-07-08 09:56:10 -0700587 *paintColor = SkColor2GrColorJustAlpha(paint.getColor());
588 *effect = GrLinearGradient::Create(context, *this, matrix, fTileMode);
dandov9de5b512014-06-10 14:38:28 -0700589
590 return true;
rileya@google.comd7cc6512012-07-27 14:00:39 +0000591}
592
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000593#else
594
dandov9de5b512014-06-10 14:38:28 -0700595bool SkLinearGradient::asNewEffect(GrContext* context, const SkPaint& paint,
bsalomon83d081a2014-07-08 09:56:10 -0700596 const SkMatrix* localMatrix, GrColor* paintColor,
597 GrEffect** effect) const {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000598 SkDEBUGFAIL("Should not call in GPU-less build");
dandov9de5b512014-06-10 14:38:28 -0700599 return false;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000600}
601
602#endif
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000603
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000604#ifndef SK_IGNORE_TO_STRING
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000605void SkLinearGradient::toString(SkString* str) const {
606 str->append("SkLinearGradient (");
607
608 str->appendf("start: (%f, %f)", fStart.fX, fStart.fY);
609 str->appendf(" end: (%f, %f) ", fEnd.fX, fEnd.fY);
610
611 this->INHERITED::toString(str);
612
613 str->append(")");
614}
615#endif