blob: 4bc697d9047c425889a6900307d4b5014a9122e5 [file] [log] [blame]
rileya@google.com589708b2012-07-26 20:04:23 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "SkLinearGradient.h"
10
11static inline int repeat_bits(int x, const int bits) {
12 return x & ((1 << bits) - 1);
13}
14
15static inline int repeat_8bits(int x) {
16 return x & 0xFF;
17}
18
19// Visual Studio 2010 (MSC_VER=1600) optimizes bit-shift code incorrectly.
20// See http://code.google.com/p/skia/issues/detail?id=472
21#if defined(_MSC_VER) && (_MSC_VER >= 1600)
22#pragma optimize("", off)
23#endif
24
25static inline int mirror_bits(int x, const int bits) {
26#ifdef SK_CPU_HAS_CONDITIONAL_INSTR
27 if (x & (1 << bits))
28 x = ~x;
29 return x & ((1 << bits) - 1);
30#else
31 int s = x << (31 - bits) >> 31;
32 return (x ^ s) & ((1 << bits) - 1);
33#endif
34}
35
36static inline int mirror_8bits(int x) {
37#ifdef SK_CPU_HAS_CONDITIONAL_INSTR
38 if (x & 256) {
39 x = ~x;
40 }
41 return x & 255;
42#else
43 int s = x << 23 >> 31;
44 return (x ^ s) & 0xFF;
45#endif
46}
47
48#if defined(_MSC_VER) && (_MSC_VER >= 1600)
49#pragma optimize("", on)
50#endif
51
52static void pts_to_unit_matrix(const SkPoint pts[2], SkMatrix* matrix) {
53 SkVector vec = pts[1] - pts[0];
54 SkScalar mag = vec.length();
55 SkScalar inv = mag ? SkScalarInvert(mag) : 0;
56
57 vec.scale(inv);
58 matrix->setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY);
59 matrix->postTranslate(-pts[0].fX, -pts[0].fY);
60 matrix->postScale(inv, inv);
61}
62
63///////////////////////////////////////////////////////////////////////////////
64
reed@google.com437d6eb2013-05-23 19:03:05 +000065SkLinearGradient::SkLinearGradient(const SkPoint pts[2], const Descriptor& desc)
66 : SkGradientShaderBase(desc)
rileya@google.com589708b2012-07-26 20:04:23 +000067 , fStart(pts[0])
68 , fEnd(pts[1]) {
69 pts_to_unit_matrix(pts, &fPtsToUnit);
70}
71
72SkLinearGradient::SkLinearGradient(SkFlattenableReadBuffer& buffer)
73 : INHERITED(buffer)
74 , fStart(buffer.readPoint())
75 , fEnd(buffer.readPoint()) {
76}
77
78void SkLinearGradient::flatten(SkFlattenableWriteBuffer& buffer) const {
79 this->INHERITED::flatten(buffer);
80 buffer.writePoint(fStart);
81 buffer.writePoint(fEnd);
82}
83
84bool SkLinearGradient::setContext(const SkBitmap& device, const SkPaint& paint,
85 const SkMatrix& matrix) {
86 if (!this->INHERITED::setContext(device, paint, matrix)) {
87 return false;
88 }
89
90 unsigned mask = SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask;
91 if ((fDstToIndex.getType() & ~mask) == 0) {
reed@google.com60040292013-02-04 18:21:23 +000092 // when we dither, we are (usually) not const-in-Y
rileya@google.com589708b2012-07-26 20:04:23 +000093 if ((fFlags & SkShader::kHasSpan16_Flag) && !paint.isDither()) {
94 // only claim this if we do have a 16bit mode (i.e. none of our
95 // colors have alpha), and if we are not dithering (which obviously
96 // is not const in Y).
97 fFlags |= SkShader::kConstInY16_Flag;
98 }
99 }
100 return true;
101}
102
103#define NO_CHECK_ITER \
104 do { \
105 unsigned fi = fx >> SkGradientShaderBase::kCache32Shift; \
106 SkASSERT(fi <= 0xFF); \
107 fx += dx; \
108 *dstC++ = cache[toggle + fi]; \
reed@google.com55853db2013-02-01 19:34:59 +0000109 toggle = next_dither_toggle(toggle); \
rileya@google.com589708b2012-07-26 20:04:23 +0000110 } while (0)
111
112namespace {
113
114typedef void (*LinearShadeProc)(TileProc proc, SkFixed dx, SkFixed fx,
115 SkPMColor* dstC, const SkPMColor* cache,
116 int toggle, int count);
117
rileya@google.com589708b2012-07-26 20:04:23 +0000118// Linear interpolation (lerp) is unnecessary if there are no sharp
119// discontinuities in the gradient - which must be true if there are
120// only 2 colors - but it's cheap.
121void shadeSpan_linear_vertical_lerp(TileProc proc, SkFixed dx, SkFixed fx,
122 SkPMColor* SK_RESTRICT dstC,
123 const SkPMColor* SK_RESTRICT cache,
124 int toggle, int count) {
125 // We're a vertical gradient, so no change in a span.
126 // If colors change sharply across the gradient, dithering is
127 // insufficient (it subsamples the color space) and we need to lerp.
128 unsigned fullIndex = proc(fx);
reed@google.com3c2102c2013-02-01 12:59:40 +0000129 unsigned fi = fullIndex >> SkGradientShaderBase::kCache32Shift;
130 unsigned remainder = fullIndex & ((1 << SkGradientShaderBase::kCache32Shift) - 1);
skia.committer@gmail.com9dde0182013-02-04 12:57:42 +0000131
reed@google.com3c2102c2013-02-01 12:59:40 +0000132 int index0 = fi + toggle;
133 int index1 = index0;
134 if (fi < SkGradientShaderBase::kCache32Count - 1) {
135 index1 += 1;
136 }
137 SkPMColor lerp = SkFastFourByteInterp(cache[index1], cache[index0], remainder);
138 index0 ^= SkGradientShaderBase::kDitherStride32;
139 index1 ^= SkGradientShaderBase::kDitherStride32;
140 SkPMColor dlerp = SkFastFourByteInterp(cache[index1], cache[index0], remainder);
rileya@google.com589708b2012-07-26 20:04:23 +0000141 sk_memset32_dither(dstC, lerp, dlerp, count);
142}
143
144void shadeSpan_linear_clamp(TileProc proc, SkFixed dx, SkFixed fx,
145 SkPMColor* SK_RESTRICT dstC,
146 const SkPMColor* SK_RESTRICT cache,
147 int toggle, int count) {
148 SkClampRange range;
reed@google.com3c2102c2013-02-01 12:59:40 +0000149 range.init(fx, dx, count, 0, SkGradientShaderBase::kCache32Count - 1);
rileya@google.com589708b2012-07-26 20:04:23 +0000150
151 if ((count = range.fCount0) > 0) {
152 sk_memset32_dither(dstC,
153 cache[toggle + range.fV0],
reed@google.com55853db2013-02-01 19:34:59 +0000154 cache[next_dither_toggle(toggle) + range.fV0],
rileya@google.com589708b2012-07-26 20:04:23 +0000155 count);
156 dstC += count;
157 }
158 if ((count = range.fCount1) > 0) {
159 int unroll = count >> 3;
160 fx = range.fFx1;
161 for (int i = 0; i < unroll; i++) {
162 NO_CHECK_ITER; NO_CHECK_ITER;
163 NO_CHECK_ITER; NO_CHECK_ITER;
164 NO_CHECK_ITER; NO_CHECK_ITER;
165 NO_CHECK_ITER; NO_CHECK_ITER;
166 }
167 if ((count &= 7) > 0) {
168 do {
169 NO_CHECK_ITER;
170 } while (--count != 0);
171 }
172 }
173 if ((count = range.fCount2) > 0) {
174 sk_memset32_dither(dstC,
175 cache[toggle + range.fV1],
reed@google.com55853db2013-02-01 19:34:59 +0000176 cache[next_dither_toggle(toggle) + range.fV1],
rileya@google.com589708b2012-07-26 20:04:23 +0000177 count);
178 }
179}
180
181void shadeSpan_linear_mirror(TileProc proc, SkFixed dx, SkFixed fx,
182 SkPMColor* SK_RESTRICT dstC,
183 const SkPMColor* SK_RESTRICT cache,
184 int toggle, int count) {
185 do {
186 unsigned fi = mirror_8bits(fx >> 8);
187 SkASSERT(fi <= 0xFF);
188 fx += dx;
189 *dstC++ = cache[toggle + fi];
reed@google.com55853db2013-02-01 19:34:59 +0000190 toggle = next_dither_toggle(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000191 } while (--count != 0);
192}
193
194void shadeSpan_linear_repeat(TileProc proc, SkFixed dx, SkFixed fx,
195 SkPMColor* SK_RESTRICT dstC,
196 const SkPMColor* SK_RESTRICT cache,
197 int toggle, int count) {
198 do {
199 unsigned fi = repeat_8bits(fx >> 8);
200 SkASSERT(fi <= 0xFF);
201 fx += dx;
202 *dstC++ = cache[toggle + fi];
reed@google.com55853db2013-02-01 19:34:59 +0000203 toggle = next_dither_toggle(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000204 } while (--count != 0);
205}
206
207}
208
209void SkLinearGradient::shadeSpan(int x, int y, SkPMColor* SK_RESTRICT dstC,
210 int count) {
211 SkASSERT(count > 0);
212
213 SkPoint srcPt;
214 SkMatrix::MapXYProc dstProc = fDstToIndexProc;
215 TileProc proc = fTileProc;
216 const SkPMColor* SK_RESTRICT cache = this->getCache32();
reed@google.com55853db2013-02-01 19:34:59 +0000217 int toggle = init_dither_toggle(x, y);
rileya@google.com589708b2012-07-26 20:04:23 +0000218
219 if (fDstToIndexClass != kPerspective_MatrixClass) {
220 dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf,
221 SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
222 SkFixed dx, fx = SkScalarToFixed(srcPt.fX);
223
224 if (fDstToIndexClass == kFixedStepInX_MatrixClass) {
225 SkFixed dxStorage[1];
226 (void)fDstToIndex.fixedStepInX(SkIntToScalar(y), dxStorage, NULL);
227 dx = dxStorage[0];
228 } else {
229 SkASSERT(fDstToIndexClass == kLinear_MatrixClass);
230 dx = SkScalarToFixed(fDstToIndex.getScaleX());
231 }
232
233 LinearShadeProc shadeProc = shadeSpan_linear_repeat;
reed@google.com53009ba2013-02-07 20:28:49 +0000234 if (0 == dx) {
rileya@google.com589708b2012-07-26 20:04:23 +0000235 shadeProc = shadeSpan_linear_vertical_lerp;
rileya@google.com589708b2012-07-26 20:04:23 +0000236 } else if (SkShader::kClamp_TileMode == fTileMode) {
237 shadeProc = shadeSpan_linear_clamp;
238 } else if (SkShader::kMirror_TileMode == fTileMode) {
239 shadeProc = shadeSpan_linear_mirror;
240 } else {
241 SkASSERT(SkShader::kRepeat_TileMode == fTileMode);
242 }
243 (*shadeProc)(proc, dx, fx, dstC, cache, toggle, count);
244 } else {
245 SkScalar dstX = SkIntToScalar(x);
246 SkScalar dstY = SkIntToScalar(y);
247 do {
248 dstProc(fDstToIndex, dstX, dstY, &srcPt);
249 unsigned fi = proc(SkScalarToFixed(srcPt.fX));
250 SkASSERT(fi <= 0xFFFF);
251 *dstC++ = cache[toggle + (fi >> kCache32Shift)];
reed@google.com55853db2013-02-01 19:34:59 +0000252 toggle = next_dither_toggle(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000253 dstX += SK_Scalar1;
254 } while (--count != 0);
255 }
256}
257
258SkShader::BitmapType SkLinearGradient::asABitmap(SkBitmap* bitmap,
259 SkMatrix* matrix,
260 TileMode xy[]) const {
261 if (bitmap) {
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000262 this->getGradientTableBitmap(bitmap);
rileya@google.com589708b2012-07-26 20:04:23 +0000263 }
264 if (matrix) {
265 matrix->preConcat(fPtsToUnit);
266 }
267 if (xy) {
268 xy[0] = fTileMode;
269 xy[1] = kClamp_TileMode;
270 }
271 return kLinear_BitmapType;
272}
273
274SkShader::GradientType SkLinearGradient::asAGradient(GradientInfo* info) const {
275 if (info) {
276 commonAsAGradient(info);
277 info->fPoint[0] = fStart;
278 info->fPoint[1] = fEnd;
279 }
280 return kLinear_GradientType;
281}
282
rileya@google.com589708b2012-07-26 20:04:23 +0000283static void dither_memset16(uint16_t dst[], uint16_t value, uint16_t other,
284 int count) {
285 if (reinterpret_cast<uintptr_t>(dst) & 2) {
286 *dst++ = value;
287 count -= 1;
288 SkTSwap(value, other);
289 }
290
291 sk_memset32((uint32_t*)dst, (value << 16) | other, count >> 1);
292
293 if (count & 1) {
294 dst[count - 1] = value;
295 }
296}
297
298#define NO_CHECK_ITER_16 \
299 do { \
300 unsigned fi = fx >> SkGradientShaderBase::kCache16Shift; \
301 SkASSERT(fi < SkGradientShaderBase::kCache16Count); \
302 fx += dx; \
303 *dstC++ = cache[toggle + fi]; \
reed@google.com55853db2013-02-01 19:34:59 +0000304 toggle = next_dither_toggle16(toggle); \
rileya@google.com589708b2012-07-26 20:04:23 +0000305 } while (0)
306
307namespace {
308
309typedef void (*LinearShade16Proc)(TileProc proc, SkFixed dx, SkFixed fx,
310 uint16_t* dstC, const uint16_t* cache,
311 int toggle, int count);
312
313void shadeSpan16_linear_vertical(TileProc proc, SkFixed dx, SkFixed fx,
314 uint16_t* SK_RESTRICT dstC,
315 const uint16_t* SK_RESTRICT cache,
316 int toggle, int count) {
317 // we're a vertical gradient, so no change in a span
318 unsigned fi = proc(fx) >> SkGradientShaderBase::kCache16Shift;
319 SkASSERT(fi < SkGradientShaderBase::kCache16Count);
320 dither_memset16(dstC, cache[toggle + fi],
reed@google.com55853db2013-02-01 19:34:59 +0000321 cache[next_dither_toggle16(toggle) + fi], count);
rileya@google.com589708b2012-07-26 20:04:23 +0000322}
323
324void shadeSpan16_linear_clamp(TileProc proc, SkFixed dx, SkFixed fx,
325 uint16_t* SK_RESTRICT dstC,
326 const uint16_t* SK_RESTRICT cache,
327 int toggle, int count) {
328 SkClampRange range;
reed@google.com3c2102c2013-02-01 12:59:40 +0000329 range.init(fx, dx, count, 0, SkGradientShaderBase::kCache32Count - 1);
rileya@google.com589708b2012-07-26 20:04:23 +0000330
331 if ((count = range.fCount0) > 0) {
332 dither_memset16(dstC,
333 cache[toggle + range.fV0],
reed@google.com55853db2013-02-01 19:34:59 +0000334 cache[next_dither_toggle16(toggle) + range.fV0],
rileya@google.com589708b2012-07-26 20:04:23 +0000335 count);
336 dstC += count;
337 }
338 if ((count = range.fCount1) > 0) {
339 int unroll = count >> 3;
340 fx = range.fFx1;
341 for (int i = 0; i < unroll; i++) {
342 NO_CHECK_ITER_16; NO_CHECK_ITER_16;
343 NO_CHECK_ITER_16; NO_CHECK_ITER_16;
344 NO_CHECK_ITER_16; NO_CHECK_ITER_16;
345 NO_CHECK_ITER_16; NO_CHECK_ITER_16;
346 }
347 if ((count &= 7) > 0) {
348 do {
349 NO_CHECK_ITER_16;
350 } while (--count != 0);
351 }
352 }
353 if ((count = range.fCount2) > 0) {
354 dither_memset16(dstC,
355 cache[toggle + range.fV1],
reed@google.com55853db2013-02-01 19:34:59 +0000356 cache[next_dither_toggle16(toggle) + range.fV1],
rileya@google.com589708b2012-07-26 20:04:23 +0000357 count);
358 }
359}
360
361void shadeSpan16_linear_mirror(TileProc proc, SkFixed dx, SkFixed fx,
362 uint16_t* SK_RESTRICT dstC,
363 const uint16_t* SK_RESTRICT cache,
364 int toggle, int count) {
365 do {
366 unsigned fi = mirror_bits(fx >> SkGradientShaderBase::kCache16Shift,
367 SkGradientShaderBase::kCache16Bits);
368 SkASSERT(fi < SkGradientShaderBase::kCache16Count);
369 fx += dx;
370 *dstC++ = cache[toggle + fi];
reed@google.com55853db2013-02-01 19:34:59 +0000371 toggle = next_dither_toggle16(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000372 } while (--count != 0);
373}
374
375void shadeSpan16_linear_repeat(TileProc proc, SkFixed dx, SkFixed fx,
376 uint16_t* SK_RESTRICT dstC,
377 const uint16_t* SK_RESTRICT cache,
378 int toggle, int count) {
379 do {
380 unsigned fi = repeat_bits(fx >> SkGradientShaderBase::kCache16Shift,
381 SkGradientShaderBase::kCache16Bits);
382 SkASSERT(fi < SkGradientShaderBase::kCache16Count);
383 fx += dx;
384 *dstC++ = cache[toggle + fi];
reed@google.com55853db2013-02-01 19:34:59 +0000385 toggle = next_dither_toggle16(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000386 } while (--count != 0);
387}
388}
389
390void SkLinearGradient::shadeSpan16(int x, int y,
391 uint16_t* SK_RESTRICT dstC, int count) {
392 SkASSERT(count > 0);
393
394 SkPoint srcPt;
395 SkMatrix::MapXYProc dstProc = fDstToIndexProc;
396 TileProc proc = fTileProc;
397 const uint16_t* SK_RESTRICT cache = this->getCache16();
reed@google.com55853db2013-02-01 19:34:59 +0000398 int toggle = init_dither_toggle16(x, y);
rileya@google.com589708b2012-07-26 20:04:23 +0000399
400 if (fDstToIndexClass != kPerspective_MatrixClass) {
401 dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf,
402 SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
403 SkFixed dx, fx = SkScalarToFixed(srcPt.fX);
404
405 if (fDstToIndexClass == kFixedStepInX_MatrixClass) {
406 SkFixed dxStorage[1];
407 (void)fDstToIndex.fixedStepInX(SkIntToScalar(y), dxStorage, NULL);
408 dx = dxStorage[0];
409 } else {
410 SkASSERT(fDstToIndexClass == kLinear_MatrixClass);
411 dx = SkScalarToFixed(fDstToIndex.getScaleX());
412 }
413
414 LinearShade16Proc shadeProc = shadeSpan16_linear_repeat;
415 if (SkFixedNearlyZero(dx)) {
416 shadeProc = shadeSpan16_linear_vertical;
417 } else if (SkShader::kClamp_TileMode == fTileMode) {
418 shadeProc = shadeSpan16_linear_clamp;
419 } else if (SkShader::kMirror_TileMode == fTileMode) {
420 shadeProc = shadeSpan16_linear_mirror;
421 } else {
422 SkASSERT(SkShader::kRepeat_TileMode == fTileMode);
423 }
424 (*shadeProc)(proc, dx, fx, dstC, cache, toggle, count);
425 } else {
426 SkScalar dstX = SkIntToScalar(x);
427 SkScalar dstY = SkIntToScalar(y);
428 do {
429 dstProc(fDstToIndex, dstX, dstY, &srcPt);
430 unsigned fi = proc(SkScalarToFixed(srcPt.fX));
431 SkASSERT(fi <= 0xFFFF);
432
433 int index = fi >> kCache16Shift;
434 *dstC++ = cache[toggle + index];
reed@google.com55853db2013-02-01 19:34:59 +0000435 toggle = next_dither_toggle16(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000436
437 dstX += SK_Scalar1;
438 } while (--count != 0);
439 }
440}
441
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000442#if SK_SUPPORT_GPU
443
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000444#include "GrTBackendEffectFactory.h"
445
rileya@google.comd7cc6512012-07-27 14:00:39 +0000446/////////////////////////////////////////////////////////////////////
447
bsalomon@google.com0707c292012-10-25 21:45:42 +0000448class GrGLLinearGradient : public GrGLGradientEffect {
rileya@google.comd7cc6512012-07-27 14:00:39 +0000449public:
450
bsalomon@google.comc7818882013-03-20 19:19:53 +0000451 GrGLLinearGradient(const GrBackendEffectFactory& factory, const GrDrawEffect&)
rileya@google.comd7cc6512012-07-27 14:00:39 +0000452 : INHERITED (factory) { }
453
454 virtual ~GrGLLinearGradient() { }
455
bsalomon@google.comf78df332012-10-29 12:43:38 +0000456 virtual void emitCode(GrGLShaderBuilder*,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000457 const GrDrawEffect&,
bsalomon@google.comf78df332012-10-29 12:43:38 +0000458 EffectKey,
bsalomon@google.comf78df332012-10-29 12:43:38 +0000459 const char* outputColor,
460 const char* inputColor,
461 const TextureSamplerArray&) SK_OVERRIDE;
462
bsalomon@google.comc7818882013-03-20 19:19:53 +0000463 static EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
464 return GenMatrixKey(drawEffect);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000465 }
rileya@google.comd7cc6512012-07-27 14:00:39 +0000466
467private:
468
bsalomon@google.com0707c292012-10-25 21:45:42 +0000469 typedef GrGLGradientEffect INHERITED;
rileya@google.comd7cc6512012-07-27 14:00:39 +0000470};
471
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000472/////////////////////////////////////////////////////////////////////
473
474class GrLinearGradient : public GrGradientEffect {
475public:
476
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000477 static GrEffectRef* Create(GrContext* ctx,
478 const SkLinearGradient& shader,
479 const SkMatrix& matrix,
480 SkShader::TileMode tm) {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000481 AutoEffectUnref effect(SkNEW_ARGS(GrLinearGradient, (ctx, shader, matrix, tm)));
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000482 return CreateEffectRef(effect);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000483 }
484
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000485 virtual ~GrLinearGradient() { }
486
487 static const char* Name() { return "Linear Gradient"; }
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000488 const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
489 return GrTBackendEffectFactory<GrLinearGradient>::getInstance();
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000490 }
491
bsalomon@google.com422e81a2012-10-25 14:11:03 +0000492 typedef GrGLLinearGradient GLEffect;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000493
494private:
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000495 GrLinearGradient(GrContext* ctx,
496 const SkLinearGradient& shader,
497 const SkMatrix& matrix,
498 SkShader::TileMode tm)
499 : INHERITED(ctx, shader, matrix, tm) { }
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000500 GR_DECLARE_EFFECT_TEST;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000501
502 typedef GrGradientEffect INHERITED;
503};
504
505/////////////////////////////////////////////////////////////////////
506
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000507GR_DEFINE_EFFECT_TEST(GrLinearGradient);
bsalomon@google.comd4726202012-08-03 14:34:46 +0000508
bsalomon@google.com73a96942013-02-13 16:31:19 +0000509GrEffectRef* GrLinearGradient::TestCreate(SkMWCRandom* random,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000510 GrContext* context,
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000511 const GrDrawTargetCaps&,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000512 GrTexture**) {
bsalomon@google.comd4726202012-08-03 14:34:46 +0000513 SkPoint points[] = {{random->nextUScalar1(), random->nextUScalar1()},
514 {random->nextUScalar1(), random->nextUScalar1()}};
515
516 SkColor colors[kMaxRandomGradientColors];
517 SkScalar stopsArray[kMaxRandomGradientColors];
518 SkScalar* stops = stopsArray;
519 SkShader::TileMode tm;
520 int colorCount = RandomGradientParams(random, colors, &stops, &tm);
521 SkAutoTUnref<SkShader> shader(SkGradientShader::CreateLinear(points,
522 colors, stops, colorCount,
523 tm));
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000524 SkPaint paint;
525 return shader->asNewEffect(context, paint);
bsalomon@google.comd4726202012-08-03 14:34:46 +0000526}
527
528/////////////////////////////////////////////////////////////////////
529
bsalomon@google.comf78df332012-10-29 12:43:38 +0000530void GrGLLinearGradient::emitCode(GrGLShaderBuilder* builder,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000531 const GrDrawEffect&,
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000532 EffectKey key,
bsalomon@google.comf78df332012-10-29 12:43:38 +0000533 const char* outputColor,
534 const char* inputColor,
535 const TextureSamplerArray& samplers) {
536 this->emitYCoordUniform(builder);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000537 const char* coords;
bsalomon@google.comc7818882013-03-20 19:19:53 +0000538 this->setupMatrix(builder, key, &coords);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000539 SkString t;
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000540 t.append(coords);
541 t.append(".x");
bsalomon@google.comf06df1b2012-09-06 20:22:31 +0000542 this->emitColorLookup(builder, t.c_str(), outputColor, inputColor, samplers[0]);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000543}
544
545/////////////////////////////////////////////////////////////////////
546
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000547GrEffectRef* SkLinearGradient::asNewEffect(GrContext* context, const SkPaint&) const {
bsalomon@google.com00835cc2013-01-14 17:07:22 +0000548 SkASSERT(NULL != context);
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000549 SkMatrix matrix;
bsalomon@google.comf94b3a42012-10-31 18:09:01 +0000550 if (!this->getLocalMatrix().invert(&matrix)) {
humper@google.com84831ac2013-01-14 22:09:54 +0000551 return NULL;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000552 }
bsalomon@google.comf94b3a42012-10-31 18:09:01 +0000553 matrix.postConcat(fPtsToUnit);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000554 return GrLinearGradient::Create(context, *this, matrix, fTileMode);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000555}
556
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000557#else
558
bsalomon@google.com5d2cd202013-01-16 15:31:06 +0000559GrEffectRef* SkLinearGradient::asNewEffect(GrContext*, const SkPaint&) const {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000560 SkDEBUGFAIL("Should not call in GPU-less build");
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000561 return NULL;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000562}
563
564#endif
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000565
566#ifdef SK_DEVELOPER
567void SkLinearGradient::toString(SkString* str) const {
568 str->append("SkLinearGradient (");
569
570 str->appendf("start: (%f, %f)", fStart.fX, fStart.fY);
571 str->appendf(" end: (%f, %f) ", fEnd.fX, fEnd.fY);
572
573 this->INHERITED::toString(str);
574
575 str->append(")");
576}
577#endif