blob: f6bae725dc13c8537a0bed1b935bbd277d033b1d [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
65SkLinearGradient::SkLinearGradient(const SkPoint pts[2],
rmistry@google.comfbfcd562012-08-23 18:09:54 +000066 const SkColor colors[],
67 const SkScalar pos[],
rileya@google.com589708b2012-07-26 20:04:23 +000068 int colorCount,
rmistry@google.comfbfcd562012-08-23 18:09:54 +000069 SkShader::TileMode mode,
rileya@google.com589708b2012-07-26 20:04:23 +000070 SkUnitMapper* mapper)
71 : SkGradientShaderBase(colors, pos, colorCount, mode, mapper)
72 , fStart(pts[0])
73 , fEnd(pts[1]) {
74 pts_to_unit_matrix(pts, &fPtsToUnit);
75}
76
77SkLinearGradient::SkLinearGradient(SkFlattenableReadBuffer& buffer)
78 : INHERITED(buffer)
79 , fStart(buffer.readPoint())
80 , fEnd(buffer.readPoint()) {
81}
82
83void SkLinearGradient::flatten(SkFlattenableWriteBuffer& buffer) const {
84 this->INHERITED::flatten(buffer);
85 buffer.writePoint(fStart);
86 buffer.writePoint(fEnd);
87}
88
89bool SkLinearGradient::setContext(const SkBitmap& device, const SkPaint& paint,
90 const SkMatrix& matrix) {
91 if (!this->INHERITED::setContext(device, paint, matrix)) {
92 return false;
93 }
94
95 unsigned mask = SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask;
96 if ((fDstToIndex.getType() & ~mask) == 0) {
reed@google.com60040292013-02-04 18:21:23 +000097#ifdef SK_IGNORE_GRADIENT_DITHER_FIX
rileya@google.com589708b2012-07-26 20:04:23 +000098 fFlags |= SkShader::kConstInY32_Flag;
reed@google.com60040292013-02-04 18:21:23 +000099#else
100 // when we dither, we are (usually) not const-in-Y
101#endif
rileya@google.com589708b2012-07-26 20:04:23 +0000102 if ((fFlags & SkShader::kHasSpan16_Flag) && !paint.isDither()) {
103 // only claim this if we do have a 16bit mode (i.e. none of our
104 // colors have alpha), and if we are not dithering (which obviously
105 // is not const in Y).
106 fFlags |= SkShader::kConstInY16_Flag;
107 }
108 }
109 return true;
110}
111
112#define NO_CHECK_ITER \
113 do { \
114 unsigned fi = fx >> SkGradientShaderBase::kCache32Shift; \
115 SkASSERT(fi <= 0xFF); \
116 fx += dx; \
117 *dstC++ = cache[toggle + fi]; \
reed@google.com55853db2013-02-01 19:34:59 +0000118 toggle = next_dither_toggle(toggle); \
rileya@google.com589708b2012-07-26 20:04:23 +0000119 } while (0)
120
121namespace {
122
123typedef void (*LinearShadeProc)(TileProc proc, SkFixed dx, SkFixed fx,
124 SkPMColor* dstC, const SkPMColor* cache,
125 int toggle, int count);
126
rileya@google.com589708b2012-07-26 20:04:23 +0000127// Linear interpolation (lerp) is unnecessary if there are no sharp
128// discontinuities in the gradient - which must be true if there are
129// only 2 colors - but it's cheap.
130void shadeSpan_linear_vertical_lerp(TileProc proc, SkFixed dx, SkFixed fx,
131 SkPMColor* SK_RESTRICT dstC,
132 const SkPMColor* SK_RESTRICT cache,
133 int toggle, int count) {
134 // We're a vertical gradient, so no change in a span.
135 // If colors change sharply across the gradient, dithering is
136 // insufficient (it subsamples the color space) and we need to lerp.
137 unsigned fullIndex = proc(fx);
reed@google.com3c2102c2013-02-01 12:59:40 +0000138 unsigned fi = fullIndex >> SkGradientShaderBase::kCache32Shift;
139 unsigned remainder = fullIndex & ((1 << SkGradientShaderBase::kCache32Shift) - 1);
skia.committer@gmail.com9dde0182013-02-04 12:57:42 +0000140
reed@google.com3c2102c2013-02-01 12:59:40 +0000141 int index0 = fi + toggle;
142 int index1 = index0;
143 if (fi < SkGradientShaderBase::kCache32Count - 1) {
144 index1 += 1;
145 }
146 SkPMColor lerp = SkFastFourByteInterp(cache[index1], cache[index0], remainder);
147 index0 ^= SkGradientShaderBase::kDitherStride32;
148 index1 ^= SkGradientShaderBase::kDitherStride32;
149 SkPMColor dlerp = SkFastFourByteInterp(cache[index1], cache[index0], remainder);
rileya@google.com589708b2012-07-26 20:04:23 +0000150 sk_memset32_dither(dstC, lerp, dlerp, count);
151}
152
153void shadeSpan_linear_clamp(TileProc proc, SkFixed dx, SkFixed fx,
154 SkPMColor* SK_RESTRICT dstC,
155 const SkPMColor* SK_RESTRICT cache,
156 int toggle, int count) {
157 SkClampRange range;
reed@google.com3c2102c2013-02-01 12:59:40 +0000158 range.init(fx, dx, count, 0, SkGradientShaderBase::kCache32Count - 1);
rileya@google.com589708b2012-07-26 20:04:23 +0000159
160 if ((count = range.fCount0) > 0) {
161 sk_memset32_dither(dstC,
162 cache[toggle + range.fV0],
reed@google.com55853db2013-02-01 19:34:59 +0000163 cache[next_dither_toggle(toggle) + range.fV0],
rileya@google.com589708b2012-07-26 20:04:23 +0000164 count);
165 dstC += count;
166 }
167 if ((count = range.fCount1) > 0) {
168 int unroll = count >> 3;
169 fx = range.fFx1;
170 for (int i = 0; i < unroll; i++) {
171 NO_CHECK_ITER; NO_CHECK_ITER;
172 NO_CHECK_ITER; NO_CHECK_ITER;
173 NO_CHECK_ITER; NO_CHECK_ITER;
174 NO_CHECK_ITER; NO_CHECK_ITER;
175 }
176 if ((count &= 7) > 0) {
177 do {
178 NO_CHECK_ITER;
179 } while (--count != 0);
180 }
181 }
182 if ((count = range.fCount2) > 0) {
183 sk_memset32_dither(dstC,
184 cache[toggle + range.fV1],
reed@google.com55853db2013-02-01 19:34:59 +0000185 cache[next_dither_toggle(toggle) + range.fV1],
rileya@google.com589708b2012-07-26 20:04:23 +0000186 count);
187 }
188}
189
190void shadeSpan_linear_mirror(TileProc proc, SkFixed dx, SkFixed fx,
191 SkPMColor* SK_RESTRICT dstC,
192 const SkPMColor* SK_RESTRICT cache,
193 int toggle, int count) {
194 do {
195 unsigned fi = mirror_8bits(fx >> 8);
196 SkASSERT(fi <= 0xFF);
197 fx += dx;
198 *dstC++ = cache[toggle + fi];
reed@google.com55853db2013-02-01 19:34:59 +0000199 toggle = next_dither_toggle(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000200 } while (--count != 0);
201}
202
203void shadeSpan_linear_repeat(TileProc proc, SkFixed dx, SkFixed fx,
204 SkPMColor* SK_RESTRICT dstC,
205 const SkPMColor* SK_RESTRICT cache,
206 int toggle, int count) {
207 do {
208 unsigned fi = repeat_8bits(fx >> 8);
209 SkASSERT(fi <= 0xFF);
210 fx += dx;
211 *dstC++ = cache[toggle + fi];
reed@google.com55853db2013-02-01 19:34:59 +0000212 toggle = next_dither_toggle(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000213 } while (--count != 0);
214}
215
216}
217
218void SkLinearGradient::shadeSpan(int x, int y, SkPMColor* SK_RESTRICT dstC,
219 int count) {
220 SkASSERT(count > 0);
221
222 SkPoint srcPt;
223 SkMatrix::MapXYProc dstProc = fDstToIndexProc;
224 TileProc proc = fTileProc;
225 const SkPMColor* SK_RESTRICT cache = this->getCache32();
reed@google.com55853db2013-02-01 19:34:59 +0000226 int toggle = init_dither_toggle(x, y);
rileya@google.com589708b2012-07-26 20:04:23 +0000227
228 if (fDstToIndexClass != kPerspective_MatrixClass) {
229 dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf,
230 SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
231 SkFixed dx, fx = SkScalarToFixed(srcPt.fX);
232
233 if (fDstToIndexClass == kFixedStepInX_MatrixClass) {
234 SkFixed dxStorage[1];
235 (void)fDstToIndex.fixedStepInX(SkIntToScalar(y), dxStorage, NULL);
236 dx = dxStorage[0];
237 } else {
238 SkASSERT(fDstToIndexClass == kLinear_MatrixClass);
239 dx = SkScalarToFixed(fDstToIndex.getScaleX());
240 }
241
242 LinearShadeProc shadeProc = shadeSpan_linear_repeat;
reed@google.com53009ba2013-02-07 20:28:49 +0000243 if (0 == dx) {
rileya@google.com589708b2012-07-26 20:04:23 +0000244 shadeProc = shadeSpan_linear_vertical_lerp;
rileya@google.com589708b2012-07-26 20:04:23 +0000245 } else if (SkShader::kClamp_TileMode == fTileMode) {
246 shadeProc = shadeSpan_linear_clamp;
247 } else if (SkShader::kMirror_TileMode == fTileMode) {
248 shadeProc = shadeSpan_linear_mirror;
249 } else {
250 SkASSERT(SkShader::kRepeat_TileMode == fTileMode);
251 }
252 (*shadeProc)(proc, dx, fx, dstC, cache, toggle, count);
253 } else {
254 SkScalar dstX = SkIntToScalar(x);
255 SkScalar dstY = SkIntToScalar(y);
256 do {
257 dstProc(fDstToIndex, dstX, dstY, &srcPt);
258 unsigned fi = proc(SkScalarToFixed(srcPt.fX));
259 SkASSERT(fi <= 0xFFFF);
260 *dstC++ = cache[toggle + (fi >> kCache32Shift)];
reed@google.com55853db2013-02-01 19:34:59 +0000261 toggle = next_dither_toggle(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000262 dstX += SK_Scalar1;
263 } while (--count != 0);
264 }
265}
266
267SkShader::BitmapType SkLinearGradient::asABitmap(SkBitmap* bitmap,
268 SkMatrix* matrix,
269 TileMode xy[]) const {
270 if (bitmap) {
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000271 this->getGradientTableBitmap(bitmap);
rileya@google.com589708b2012-07-26 20:04:23 +0000272 }
273 if (matrix) {
274 matrix->preConcat(fPtsToUnit);
275 }
276 if (xy) {
277 xy[0] = fTileMode;
278 xy[1] = kClamp_TileMode;
279 }
280 return kLinear_BitmapType;
281}
282
283SkShader::GradientType SkLinearGradient::asAGradient(GradientInfo* info) const {
284 if (info) {
285 commonAsAGradient(info);
286 info->fPoint[0] = fStart;
287 info->fPoint[1] = fEnd;
288 }
289 return kLinear_GradientType;
290}
291
rileya@google.com589708b2012-07-26 20:04:23 +0000292static void dither_memset16(uint16_t dst[], uint16_t value, uint16_t other,
293 int count) {
294 if (reinterpret_cast<uintptr_t>(dst) & 2) {
295 *dst++ = value;
296 count -= 1;
297 SkTSwap(value, other);
298 }
299
300 sk_memset32((uint32_t*)dst, (value << 16) | other, count >> 1);
301
302 if (count & 1) {
303 dst[count - 1] = value;
304 }
305}
306
307#define NO_CHECK_ITER_16 \
308 do { \
309 unsigned fi = fx >> SkGradientShaderBase::kCache16Shift; \
310 SkASSERT(fi < SkGradientShaderBase::kCache16Count); \
311 fx += dx; \
312 *dstC++ = cache[toggle + fi]; \
reed@google.com55853db2013-02-01 19:34:59 +0000313 toggle = next_dither_toggle16(toggle); \
rileya@google.com589708b2012-07-26 20:04:23 +0000314 } while (0)
315
316namespace {
317
318typedef void (*LinearShade16Proc)(TileProc proc, SkFixed dx, SkFixed fx,
319 uint16_t* dstC, const uint16_t* cache,
320 int toggle, int count);
321
322void shadeSpan16_linear_vertical(TileProc proc, SkFixed dx, SkFixed fx,
323 uint16_t* SK_RESTRICT dstC,
324 const uint16_t* SK_RESTRICT cache,
325 int toggle, int count) {
326 // we're a vertical gradient, so no change in a span
327 unsigned fi = proc(fx) >> SkGradientShaderBase::kCache16Shift;
328 SkASSERT(fi < SkGradientShaderBase::kCache16Count);
329 dither_memset16(dstC, cache[toggle + fi],
reed@google.com55853db2013-02-01 19:34:59 +0000330 cache[next_dither_toggle16(toggle) + fi], count);
rileya@google.com589708b2012-07-26 20:04:23 +0000331}
332
333void shadeSpan16_linear_clamp(TileProc proc, SkFixed dx, SkFixed fx,
334 uint16_t* SK_RESTRICT dstC,
335 const uint16_t* SK_RESTRICT cache,
336 int toggle, int count) {
337 SkClampRange range;
reed@google.com3c2102c2013-02-01 12:59:40 +0000338 range.init(fx, dx, count, 0, SkGradientShaderBase::kCache32Count - 1);
rileya@google.com589708b2012-07-26 20:04:23 +0000339
340 if ((count = range.fCount0) > 0) {
341 dither_memset16(dstC,
342 cache[toggle + range.fV0],
reed@google.com55853db2013-02-01 19:34:59 +0000343 cache[next_dither_toggle16(toggle) + range.fV0],
rileya@google.com589708b2012-07-26 20:04:23 +0000344 count);
345 dstC += count;
346 }
347 if ((count = range.fCount1) > 0) {
348 int unroll = count >> 3;
349 fx = range.fFx1;
350 for (int i = 0; i < unroll; i++) {
351 NO_CHECK_ITER_16; NO_CHECK_ITER_16;
352 NO_CHECK_ITER_16; NO_CHECK_ITER_16;
353 NO_CHECK_ITER_16; NO_CHECK_ITER_16;
354 NO_CHECK_ITER_16; NO_CHECK_ITER_16;
355 }
356 if ((count &= 7) > 0) {
357 do {
358 NO_CHECK_ITER_16;
359 } while (--count != 0);
360 }
361 }
362 if ((count = range.fCount2) > 0) {
363 dither_memset16(dstC,
364 cache[toggle + range.fV1],
reed@google.com55853db2013-02-01 19:34:59 +0000365 cache[next_dither_toggle16(toggle) + range.fV1],
rileya@google.com589708b2012-07-26 20:04:23 +0000366 count);
367 }
368}
369
370void shadeSpan16_linear_mirror(TileProc proc, SkFixed dx, SkFixed fx,
371 uint16_t* SK_RESTRICT dstC,
372 const uint16_t* SK_RESTRICT cache,
373 int toggle, int count) {
374 do {
375 unsigned fi = mirror_bits(fx >> SkGradientShaderBase::kCache16Shift,
376 SkGradientShaderBase::kCache16Bits);
377 SkASSERT(fi < SkGradientShaderBase::kCache16Count);
378 fx += dx;
379 *dstC++ = cache[toggle + fi];
reed@google.com55853db2013-02-01 19:34:59 +0000380 toggle = next_dither_toggle16(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000381 } while (--count != 0);
382}
383
384void shadeSpan16_linear_repeat(TileProc proc, SkFixed dx, SkFixed fx,
385 uint16_t* SK_RESTRICT dstC,
386 const uint16_t* SK_RESTRICT cache,
387 int toggle, int count) {
388 do {
389 unsigned fi = repeat_bits(fx >> SkGradientShaderBase::kCache16Shift,
390 SkGradientShaderBase::kCache16Bits);
391 SkASSERT(fi < SkGradientShaderBase::kCache16Count);
392 fx += dx;
393 *dstC++ = cache[toggle + fi];
reed@google.com55853db2013-02-01 19:34:59 +0000394 toggle = next_dither_toggle16(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000395 } while (--count != 0);
396}
397}
398
399void SkLinearGradient::shadeSpan16(int x, int y,
400 uint16_t* SK_RESTRICT dstC, int count) {
401 SkASSERT(count > 0);
402
403 SkPoint srcPt;
404 SkMatrix::MapXYProc dstProc = fDstToIndexProc;
405 TileProc proc = fTileProc;
406 const uint16_t* SK_RESTRICT cache = this->getCache16();
reed@google.com55853db2013-02-01 19:34:59 +0000407 int toggle = init_dither_toggle16(x, y);
rileya@google.com589708b2012-07-26 20:04:23 +0000408
409 if (fDstToIndexClass != kPerspective_MatrixClass) {
410 dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf,
411 SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
412 SkFixed dx, fx = SkScalarToFixed(srcPt.fX);
413
414 if (fDstToIndexClass == kFixedStepInX_MatrixClass) {
415 SkFixed dxStorage[1];
416 (void)fDstToIndex.fixedStepInX(SkIntToScalar(y), dxStorage, NULL);
417 dx = dxStorage[0];
418 } else {
419 SkASSERT(fDstToIndexClass == kLinear_MatrixClass);
420 dx = SkScalarToFixed(fDstToIndex.getScaleX());
421 }
422
423 LinearShade16Proc shadeProc = shadeSpan16_linear_repeat;
424 if (SkFixedNearlyZero(dx)) {
425 shadeProc = shadeSpan16_linear_vertical;
426 } else if (SkShader::kClamp_TileMode == fTileMode) {
427 shadeProc = shadeSpan16_linear_clamp;
428 } else if (SkShader::kMirror_TileMode == fTileMode) {
429 shadeProc = shadeSpan16_linear_mirror;
430 } else {
431 SkASSERT(SkShader::kRepeat_TileMode == fTileMode);
432 }
433 (*shadeProc)(proc, dx, fx, dstC, cache, toggle, count);
434 } else {
435 SkScalar dstX = SkIntToScalar(x);
436 SkScalar dstY = SkIntToScalar(y);
437 do {
438 dstProc(fDstToIndex, dstX, dstY, &srcPt);
439 unsigned fi = proc(SkScalarToFixed(srcPt.fX));
440 SkASSERT(fi <= 0xFFFF);
441
442 int index = fi >> kCache16Shift;
443 *dstC++ = cache[toggle + index];
reed@google.com55853db2013-02-01 19:34:59 +0000444 toggle = next_dither_toggle16(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000445
446 dstX += SK_Scalar1;
447 } while (--count != 0);
448 }
449}
450
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000451#if SK_SUPPORT_GPU
452
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000453#include "GrTBackendEffectFactory.h"
454
rileya@google.comd7cc6512012-07-27 14:00:39 +0000455/////////////////////////////////////////////////////////////////////
456
bsalomon@google.com0707c292012-10-25 21:45:42 +0000457class GrGLLinearGradient : public GrGLGradientEffect {
rileya@google.comd7cc6512012-07-27 14:00:39 +0000458public:
459
bsalomon@google.com6340a412013-01-22 19:55:59 +0000460 GrGLLinearGradient(const GrBackendEffectFactory& factory, const GrEffectRef&)
rileya@google.comd7cc6512012-07-27 14:00:39 +0000461 : INHERITED (factory) { }
462
463 virtual ~GrGLLinearGradient() { }
464
bsalomon@google.comf78df332012-10-29 12:43:38 +0000465 virtual void emitCode(GrGLShaderBuilder*,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000466 const GrEffectStage&,
bsalomon@google.comf78df332012-10-29 12:43:38 +0000467 EffectKey,
468 const char* vertexCoords,
469 const char* outputColor,
470 const char* inputColor,
471 const TextureSamplerArray&) SK_OVERRIDE;
472
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000473 static EffectKey GenKey(const GrEffectStage& stage, const GrGLCaps&) {
474 return GenMatrixKey(stage);
475 }
rileya@google.comd7cc6512012-07-27 14:00:39 +0000476
477private:
478
bsalomon@google.com0707c292012-10-25 21:45:42 +0000479 typedef GrGLGradientEffect INHERITED;
rileya@google.comd7cc6512012-07-27 14:00:39 +0000480};
481
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000482/////////////////////////////////////////////////////////////////////
483
484class GrLinearGradient : public GrGradientEffect {
485public:
486
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000487 static GrEffectRef* Create(GrContext* ctx,
488 const SkLinearGradient& shader,
489 const SkMatrix& matrix,
490 SkShader::TileMode tm) {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000491 AutoEffectUnref effect(SkNEW_ARGS(GrLinearGradient, (ctx, shader, matrix, tm)));
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000492 return CreateEffectRef(effect);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000493 }
494
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000495 virtual ~GrLinearGradient() { }
496
497 static const char* Name() { return "Linear Gradient"; }
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000498 const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
499 return GrTBackendEffectFactory<GrLinearGradient>::getInstance();
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000500 }
501
bsalomon@google.com422e81a2012-10-25 14:11:03 +0000502 typedef GrGLLinearGradient GLEffect;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000503
504private:
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000505 GrLinearGradient(GrContext* ctx,
506 const SkLinearGradient& shader,
507 const SkMatrix& matrix,
508 SkShader::TileMode tm)
509 : INHERITED(ctx, shader, matrix, tm) { }
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000510 GR_DECLARE_EFFECT_TEST;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000511
512 typedef GrGradientEffect INHERITED;
513};
514
515/////////////////////////////////////////////////////////////////////
516
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000517GR_DEFINE_EFFECT_TEST(GrLinearGradient);
bsalomon@google.comd4726202012-08-03 14:34:46 +0000518
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000519GrEffectRef* GrLinearGradient::TestCreate(SkRandom* random,
520 GrContext* context,
521 GrTexture**) {
bsalomon@google.comd4726202012-08-03 14:34:46 +0000522 SkPoint points[] = {{random->nextUScalar1(), random->nextUScalar1()},
523 {random->nextUScalar1(), random->nextUScalar1()}};
524
525 SkColor colors[kMaxRandomGradientColors];
526 SkScalar stopsArray[kMaxRandomGradientColors];
527 SkScalar* stops = stopsArray;
528 SkShader::TileMode tm;
529 int colorCount = RandomGradientParams(random, colors, &stops, &tm);
530 SkAutoTUnref<SkShader> shader(SkGradientShader::CreateLinear(points,
531 colors, stops, colorCount,
532 tm));
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000533 SkPaint paint;
534 return shader->asNewEffect(context, paint);
bsalomon@google.comd4726202012-08-03 14:34:46 +0000535}
536
537/////////////////////////////////////////////////////////////////////
538
bsalomon@google.comf78df332012-10-29 12:43:38 +0000539void GrGLLinearGradient::emitCode(GrGLShaderBuilder* builder,
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000540 const GrEffectStage& stage,
541 EffectKey key,
bsalomon@google.comf78df332012-10-29 12:43:38 +0000542 const char* vertexCoords,
543 const char* outputColor,
544 const char* inputColor,
545 const TextureSamplerArray& samplers) {
546 this->emitYCoordUniform(builder);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000547 const char* coords;
548 this->setupMatrix(builder, key, vertexCoords, &coords);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000549 SkString t;
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000550 t.append(coords);
551 t.append(".x");
bsalomon@google.comf06df1b2012-09-06 20:22:31 +0000552 this->emitColorLookup(builder, t.c_str(), outputColor, inputColor, samplers[0]);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000553}
554
555/////////////////////////////////////////////////////////////////////
556
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000557GrEffectRef* SkLinearGradient::asNewEffect(GrContext* context, const SkPaint&) const {
bsalomon@google.com00835cc2013-01-14 17:07:22 +0000558 SkASSERT(NULL != context);
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000559 SkMatrix matrix;
bsalomon@google.comf94b3a42012-10-31 18:09:01 +0000560 if (!this->getLocalMatrix().invert(&matrix)) {
humper@google.com84831ac2013-01-14 22:09:54 +0000561 return NULL;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000562 }
bsalomon@google.comf94b3a42012-10-31 18:09:01 +0000563 matrix.postConcat(fPtsToUnit);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000564 return GrLinearGradient::Create(context, *this, matrix, fTileMode);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000565}
566
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000567#else
568
bsalomon@google.com5d2cd202013-01-16 15:31:06 +0000569GrEffectRef* SkLinearGradient::asNewEffect(GrContext*, const SkPaint&) const {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000570 SkDEBUGFAIL("Should not call in GPU-less build");
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000571 return NULL;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000572}
573
574#endif
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000575
576#ifdef SK_DEVELOPER
577void SkLinearGradient::toString(SkString* str) const {
578 str->append("SkLinearGradient (");
579
580 str->appendf("start: (%f, %f)", fStart.fX, fStart.fY);
581 str->appendf(" end: (%f, %f) ", fEnd.fX, fEnd.fY);
582
583 this->INHERITED::toString(str);
584
585 str->append(")");
586}
587#endif