blob: 524db769663273137a03799f0a292c94a532e973 [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) {
97 fFlags |= SkShader::kConstInY32_Flag;
98 if ((fFlags & SkShader::kHasSpan16_Flag) && !paint.isDither()) {
99 // only claim this if we do have a 16bit mode (i.e. none of our
100 // colors have alpha), and if we are not dithering (which obviously
101 // is not const in Y).
102 fFlags |= SkShader::kConstInY16_Flag;
103 }
104 }
105 return true;
106}
107
108#define NO_CHECK_ITER \
109 do { \
110 unsigned fi = fx >> SkGradientShaderBase::kCache32Shift; \
111 SkASSERT(fi <= 0xFF); \
112 fx += dx; \
113 *dstC++ = cache[toggle + fi]; \
114 toggle ^= SkGradientShaderBase::kDitherStride32; \
115 } while (0)
116
117namespace {
118
119typedef void (*LinearShadeProc)(TileProc proc, SkFixed dx, SkFixed fx,
120 SkPMColor* dstC, const SkPMColor* cache,
121 int toggle, int count);
122
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000123// This function is deprecated, and will be replaced by
rileya@google.com589708b2012-07-26 20:04:23 +0000124// shadeSpan_linear_vertical_lerp() once Chrome has been weaned off of it.
125void shadeSpan_linear_vertical(TileProc proc, SkFixed dx, SkFixed fx,
126 SkPMColor* SK_RESTRICT dstC,
127 const SkPMColor* SK_RESTRICT cache,
128 int toggle, int count) {
129 // We're a vertical gradient, so no change in a span.
130 // If colors change sharply across the gradient, dithering is
131 // insufficient (it subsamples the color space) and we need to lerp.
132 unsigned fullIndex = proc(fx);
133 unsigned fi = fullIndex >> (16 - SkGradientShaderBase::kCache32Bits);
134 sk_memset32_dither(dstC,
135 cache[toggle + fi],
136 cache[(toggle ^ SkGradientShaderBase::kDitherStride32) + fi],
137 count);
138}
139
140// Linear interpolation (lerp) is unnecessary if there are no sharp
141// discontinuities in the gradient - which must be true if there are
142// only 2 colors - but it's cheap.
143void shadeSpan_linear_vertical_lerp(TileProc proc, SkFixed dx, SkFixed fx,
144 SkPMColor* SK_RESTRICT dstC,
145 const SkPMColor* SK_RESTRICT cache,
146 int toggle, int count) {
147 // We're a vertical gradient, so no change in a span.
148 // If colors change sharply across the gradient, dithering is
149 // insufficient (it subsamples the color space) and we need to lerp.
150 unsigned fullIndex = proc(fx);
151 unsigned fi = fullIndex >> (16 - SkGradientShaderBase::kCache32Bits);
152 unsigned remainder = fullIndex & SkGradientShaderBase::kLerpRemainderMask32;
153 SkPMColor lerp =
154 SkFastFourByteInterp(
155 cache[toggle + fi + 1],
156 cache[toggle + fi], remainder);
157 SkPMColor dlerp =
158 SkFastFourByteInterp(
159 cache[(toggle ^ SkGradientShaderBase::kDitherStride32) + fi + 1],
160 cache[(toggle ^ SkGradientShaderBase::kDitherStride32) + fi], remainder);
161 sk_memset32_dither(dstC, lerp, dlerp, count);
162}
163
164void shadeSpan_linear_clamp(TileProc proc, SkFixed dx, SkFixed fx,
165 SkPMColor* SK_RESTRICT dstC,
166 const SkPMColor* SK_RESTRICT cache,
167 int toggle, int count) {
168 SkClampRange range;
169 range.init(fx, dx, count, 0, SkGradientShaderBase::kGradient32Length);
170
171 if ((count = range.fCount0) > 0) {
172 sk_memset32_dither(dstC,
173 cache[toggle + range.fV0],
174 cache[(toggle ^ SkGradientShaderBase::kDitherStride32) + range.fV0],
175 count);
176 dstC += count;
177 }
178 if ((count = range.fCount1) > 0) {
179 int unroll = count >> 3;
180 fx = range.fFx1;
181 for (int i = 0; i < unroll; i++) {
182 NO_CHECK_ITER; NO_CHECK_ITER;
183 NO_CHECK_ITER; NO_CHECK_ITER;
184 NO_CHECK_ITER; NO_CHECK_ITER;
185 NO_CHECK_ITER; NO_CHECK_ITER;
186 }
187 if ((count &= 7) > 0) {
188 do {
189 NO_CHECK_ITER;
190 } while (--count != 0);
191 }
192 }
193 if ((count = range.fCount2) > 0) {
194 sk_memset32_dither(dstC,
195 cache[toggle + range.fV1],
196 cache[(toggle ^ SkGradientShaderBase::kDitherStride32) + range.fV1],
197 count);
198 }
199}
200
201void shadeSpan_linear_mirror(TileProc proc, SkFixed dx, SkFixed fx,
202 SkPMColor* SK_RESTRICT dstC,
203 const SkPMColor* SK_RESTRICT cache,
204 int toggle, int count) {
205 do {
206 unsigned fi = mirror_8bits(fx >> 8);
207 SkASSERT(fi <= 0xFF);
208 fx += dx;
209 *dstC++ = cache[toggle + fi];
210 toggle ^= SkGradientShaderBase::kDitherStride32;
211 } while (--count != 0);
212}
213
214void shadeSpan_linear_repeat(TileProc proc, SkFixed dx, SkFixed fx,
215 SkPMColor* SK_RESTRICT dstC,
216 const SkPMColor* SK_RESTRICT cache,
217 int toggle, int count) {
218 do {
219 unsigned fi = repeat_8bits(fx >> 8);
220 SkASSERT(fi <= 0xFF);
221 fx += dx;
222 *dstC++ = cache[toggle + fi];
223 toggle ^= SkGradientShaderBase::kDitherStride32;
224 } while (--count != 0);
225}
226
227}
228
229void SkLinearGradient::shadeSpan(int x, int y, SkPMColor* SK_RESTRICT dstC,
230 int count) {
231 SkASSERT(count > 0);
232
233 SkPoint srcPt;
234 SkMatrix::MapXYProc dstProc = fDstToIndexProc;
235 TileProc proc = fTileProc;
236 const SkPMColor* SK_RESTRICT cache = this->getCache32();
237#ifdef USE_DITHER_32BIT_GRADIENT
238 int toggle = ((x ^ y) & 1) * kDitherStride32;
239#else
240 int toggle = 0;
241#endif
242
243 if (fDstToIndexClass != kPerspective_MatrixClass) {
244 dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf,
245 SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
246 SkFixed dx, fx = SkScalarToFixed(srcPt.fX);
247
248 if (fDstToIndexClass == kFixedStepInX_MatrixClass) {
249 SkFixed dxStorage[1];
250 (void)fDstToIndex.fixedStepInX(SkIntToScalar(y), dxStorage, NULL);
251 dx = dxStorage[0];
252 } else {
253 SkASSERT(fDstToIndexClass == kLinear_MatrixClass);
254 dx = SkScalarToFixed(fDstToIndex.getScaleX());
255 }
256
257 LinearShadeProc shadeProc = shadeSpan_linear_repeat;
258 if (SkFixedNearlyZero(dx)) {
259#ifdef SK_SIMPLE_TWOCOLOR_VERTICAL_GRADIENTS
260 if (fColorCount > 2) {
261 shadeProc = shadeSpan_linear_vertical_lerp;
262 } else {
263 shadeProc = shadeSpan_linear_vertical;
264 }
265#else
266 shadeProc = shadeSpan_linear_vertical_lerp;
267#endif
268 } else if (SkShader::kClamp_TileMode == fTileMode) {
269 shadeProc = shadeSpan_linear_clamp;
270 } else if (SkShader::kMirror_TileMode == fTileMode) {
271 shadeProc = shadeSpan_linear_mirror;
272 } else {
273 SkASSERT(SkShader::kRepeat_TileMode == fTileMode);
274 }
275 (*shadeProc)(proc, dx, fx, dstC, cache, toggle, count);
276 } else {
277 SkScalar dstX = SkIntToScalar(x);
278 SkScalar dstY = SkIntToScalar(y);
279 do {
280 dstProc(fDstToIndex, dstX, dstY, &srcPt);
281 unsigned fi = proc(SkScalarToFixed(srcPt.fX));
282 SkASSERT(fi <= 0xFFFF);
283 *dstC++ = cache[toggle + (fi >> kCache32Shift)];
284 toggle ^= SkGradientShaderBase::kDitherStride32;
285 dstX += SK_Scalar1;
286 } while (--count != 0);
287 }
288}
289
290SkShader::BitmapType SkLinearGradient::asABitmap(SkBitmap* bitmap,
291 SkMatrix* matrix,
292 TileMode xy[]) const {
293 if (bitmap) {
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000294 this->getGradientTableBitmap(bitmap);
rileya@google.com589708b2012-07-26 20:04:23 +0000295 }
296 if (matrix) {
297 matrix->preConcat(fPtsToUnit);
298 }
299 if (xy) {
300 xy[0] = fTileMode;
301 xy[1] = kClamp_TileMode;
302 }
303 return kLinear_BitmapType;
304}
305
306SkShader::GradientType SkLinearGradient::asAGradient(GradientInfo* info) const {
307 if (info) {
308 commonAsAGradient(info);
309 info->fPoint[0] = fStart;
310 info->fPoint[1] = fEnd;
311 }
312 return kLinear_GradientType;
313}
314
rileya@google.com589708b2012-07-26 20:04:23 +0000315static void dither_memset16(uint16_t dst[], uint16_t value, uint16_t other,
316 int count) {
317 if (reinterpret_cast<uintptr_t>(dst) & 2) {
318 *dst++ = value;
319 count -= 1;
320 SkTSwap(value, other);
321 }
322
323 sk_memset32((uint32_t*)dst, (value << 16) | other, count >> 1);
324
325 if (count & 1) {
326 dst[count - 1] = value;
327 }
328}
329
330#define NO_CHECK_ITER_16 \
331 do { \
332 unsigned fi = fx >> SkGradientShaderBase::kCache16Shift; \
333 SkASSERT(fi < SkGradientShaderBase::kCache16Count); \
334 fx += dx; \
335 *dstC++ = cache[toggle + fi]; \
336 toggle ^= SkGradientShaderBase::kDitherStride16; \
337 } while (0)
338
339namespace {
340
341typedef void (*LinearShade16Proc)(TileProc proc, SkFixed dx, SkFixed fx,
342 uint16_t* dstC, const uint16_t* cache,
343 int toggle, int count);
344
345void shadeSpan16_linear_vertical(TileProc proc, SkFixed dx, SkFixed fx,
346 uint16_t* SK_RESTRICT dstC,
347 const uint16_t* SK_RESTRICT cache,
348 int toggle, int count) {
349 // we're a vertical gradient, so no change in a span
350 unsigned fi = proc(fx) >> SkGradientShaderBase::kCache16Shift;
351 SkASSERT(fi < SkGradientShaderBase::kCache16Count);
352 dither_memset16(dstC, cache[toggle + fi],
353 cache[(toggle ^ SkGradientShaderBase::kDitherStride16) + fi], count);
354
355}
356
357void shadeSpan16_linear_clamp(TileProc proc, SkFixed dx, SkFixed fx,
358 uint16_t* SK_RESTRICT dstC,
359 const uint16_t* SK_RESTRICT cache,
360 int toggle, int count) {
361 SkClampRange range;
362 range.init(fx, dx, count, 0, SkGradientShaderBase::kGradient16Length);
363
364 if ((count = range.fCount0) > 0) {
365 dither_memset16(dstC,
366 cache[toggle + range.fV0],
367 cache[(toggle ^ SkGradientShaderBase::kDitherStride16) + range.fV0],
368 count);
369 dstC += count;
370 }
371 if ((count = range.fCount1) > 0) {
372 int unroll = count >> 3;
373 fx = range.fFx1;
374 for (int i = 0; i < unroll; i++) {
375 NO_CHECK_ITER_16; NO_CHECK_ITER_16;
376 NO_CHECK_ITER_16; NO_CHECK_ITER_16;
377 NO_CHECK_ITER_16; NO_CHECK_ITER_16;
378 NO_CHECK_ITER_16; NO_CHECK_ITER_16;
379 }
380 if ((count &= 7) > 0) {
381 do {
382 NO_CHECK_ITER_16;
383 } while (--count != 0);
384 }
385 }
386 if ((count = range.fCount2) > 0) {
387 dither_memset16(dstC,
388 cache[toggle + range.fV1],
389 cache[(toggle ^ SkGradientShaderBase::kDitherStride16) + range.fV1],
390 count);
391 }
392}
393
394void shadeSpan16_linear_mirror(TileProc proc, SkFixed dx, SkFixed fx,
395 uint16_t* SK_RESTRICT dstC,
396 const uint16_t* SK_RESTRICT cache,
397 int toggle, int count) {
398 do {
399 unsigned fi = mirror_bits(fx >> SkGradientShaderBase::kCache16Shift,
400 SkGradientShaderBase::kCache16Bits);
401 SkASSERT(fi < SkGradientShaderBase::kCache16Count);
402 fx += dx;
403 *dstC++ = cache[toggle + fi];
404 toggle ^= SkGradientShaderBase::kDitherStride16;
405 } while (--count != 0);
406}
407
408void shadeSpan16_linear_repeat(TileProc proc, SkFixed dx, SkFixed fx,
409 uint16_t* SK_RESTRICT dstC,
410 const uint16_t* SK_RESTRICT cache,
411 int toggle, int count) {
412 do {
413 unsigned fi = repeat_bits(fx >> SkGradientShaderBase::kCache16Shift,
414 SkGradientShaderBase::kCache16Bits);
415 SkASSERT(fi < SkGradientShaderBase::kCache16Count);
416 fx += dx;
417 *dstC++ = cache[toggle + fi];
418 toggle ^= SkGradientShaderBase::kDitherStride16;
419 } while (--count != 0);
420}
421}
422
423void SkLinearGradient::shadeSpan16(int x, int y,
424 uint16_t* SK_RESTRICT dstC, int count) {
425 SkASSERT(count > 0);
426
427 SkPoint srcPt;
428 SkMatrix::MapXYProc dstProc = fDstToIndexProc;
429 TileProc proc = fTileProc;
430 const uint16_t* SK_RESTRICT cache = this->getCache16();
431 int toggle = ((x ^ y) & 1) * kDitherStride16;
432
433 if (fDstToIndexClass != kPerspective_MatrixClass) {
434 dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf,
435 SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
436 SkFixed dx, fx = SkScalarToFixed(srcPt.fX);
437
438 if (fDstToIndexClass == kFixedStepInX_MatrixClass) {
439 SkFixed dxStorage[1];
440 (void)fDstToIndex.fixedStepInX(SkIntToScalar(y), dxStorage, NULL);
441 dx = dxStorage[0];
442 } else {
443 SkASSERT(fDstToIndexClass == kLinear_MatrixClass);
444 dx = SkScalarToFixed(fDstToIndex.getScaleX());
445 }
446
447 LinearShade16Proc shadeProc = shadeSpan16_linear_repeat;
448 if (SkFixedNearlyZero(dx)) {
449 shadeProc = shadeSpan16_linear_vertical;
450 } else if (SkShader::kClamp_TileMode == fTileMode) {
451 shadeProc = shadeSpan16_linear_clamp;
452 } else if (SkShader::kMirror_TileMode == fTileMode) {
453 shadeProc = shadeSpan16_linear_mirror;
454 } else {
455 SkASSERT(SkShader::kRepeat_TileMode == fTileMode);
456 }
457 (*shadeProc)(proc, dx, fx, dstC, cache, toggle, count);
458 } else {
459 SkScalar dstX = SkIntToScalar(x);
460 SkScalar dstY = SkIntToScalar(y);
461 do {
462 dstProc(fDstToIndex, dstX, dstY, &srcPt);
463 unsigned fi = proc(SkScalarToFixed(srcPt.fX));
464 SkASSERT(fi <= 0xFFFF);
465
466 int index = fi >> kCache16Shift;
467 *dstC++ = cache[toggle + index];
468 toggle ^= SkGradientShaderBase::kDitherStride16;
469
470 dstX += SK_Scalar1;
471 } while (--count != 0);
472 }
473}
474
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000475#if SK_SUPPORT_GPU
476
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000477#include "GrTBackendEffectFactory.h"
478
rileya@google.comd7cc6512012-07-27 14:00:39 +0000479/////////////////////////////////////////////////////////////////////
480
bsalomon@google.com0707c292012-10-25 21:45:42 +0000481class GrGLLinearGradient : public GrGLGradientEffect {
rileya@google.comd7cc6512012-07-27 14:00:39 +0000482public:
483
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000484 GrGLLinearGradient(const GrBackendEffectFactory& factory,
bsalomon@google.coma469c282012-10-24 18:28:34 +0000485 const GrEffect&)
rileya@google.comd7cc6512012-07-27 14:00:39 +0000486 : INHERITED (factory) { }
487
488 virtual ~GrGLLinearGradient() { }
489
bsalomon@google.comf78df332012-10-29 12:43:38 +0000490 virtual void emitCode(GrGLShaderBuilder*,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000491 const GrEffectStage&,
bsalomon@google.comf78df332012-10-29 12:43:38 +0000492 EffectKey,
493 const char* vertexCoords,
494 const char* outputColor,
495 const char* inputColor,
496 const TextureSamplerArray&) SK_OVERRIDE;
497
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000498 static EffectKey GenKey(const GrEffectStage&, const GrGLCaps& caps) { return 0; }
rileya@google.comd7cc6512012-07-27 14:00:39 +0000499
500private:
501
bsalomon@google.com0707c292012-10-25 21:45:42 +0000502 typedef GrGLGradientEffect INHERITED;
rileya@google.comd7cc6512012-07-27 14:00:39 +0000503};
504
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000505/////////////////////////////////////////////////////////////////////
506
507class GrLinearGradient : public GrGradientEffect {
508public:
509
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000510 GrLinearGradient(GrContext* ctx, const SkLinearGradient& shader, SkShader::TileMode tm)
511 : INHERITED(ctx, shader, tm) { }
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000512 virtual ~GrLinearGradient() { }
513
514 static const char* Name() { return "Linear Gradient"; }
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000515 const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
516 return GrTBackendEffectFactory<GrLinearGradient>::getInstance();
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000517 }
518
bsalomon@google.com422e81a2012-10-25 14:11:03 +0000519 typedef GrGLLinearGradient GLEffect;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000520
521private:
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000522 GR_DECLARE_EFFECT_TEST;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000523
524 typedef GrGradientEffect INHERITED;
525};
526
527/////////////////////////////////////////////////////////////////////
528
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000529GR_DEFINE_EFFECT_TEST(GrLinearGradient);
bsalomon@google.comd4726202012-08-03 14:34:46 +0000530
bsalomon@google.coma469c282012-10-24 18:28:34 +0000531GrEffect* GrLinearGradient::TestCreate(SkRandom* random,
532 GrContext* context,
533 GrTexture**) {
bsalomon@google.comd4726202012-08-03 14:34:46 +0000534 SkPoint points[] = {{random->nextUScalar1(), random->nextUScalar1()},
535 {random->nextUScalar1(), random->nextUScalar1()}};
536
537 SkColor colors[kMaxRandomGradientColors];
538 SkScalar stopsArray[kMaxRandomGradientColors];
539 SkScalar* stops = stopsArray;
540 SkShader::TileMode tm;
541 int colorCount = RandomGradientParams(random, colors, &stops, &tm);
542 SkAutoTUnref<SkShader> shader(SkGradientShader::CreateLinear(points,
543 colors, stops, colorCount,
544 tm));
bsalomon@google.com08283af2012-10-26 13:01:20 +0000545 GrEffectStage stage;
546 shader->asNewEffect(context, &stage);
547 GrAssert(NULL != stage.getEffect());
bsalomon@google.com8ea78d82012-10-24 20:11:30 +0000548 // const_cast and ref is a hack! Will remove when asNewEffect returns GrEffect*
bsalomon@google.com08283af2012-10-26 13:01:20 +0000549 stage.getEffect()->ref();
550 return const_cast<GrEffect*>(stage.getEffect());
bsalomon@google.comd4726202012-08-03 14:34:46 +0000551}
552
553/////////////////////////////////////////////////////////////////////
554
bsalomon@google.comf78df332012-10-29 12:43:38 +0000555void GrGLLinearGradient::emitCode(GrGLShaderBuilder* builder,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000556 const GrEffectStage&,
bsalomon@google.comf78df332012-10-29 12:43:38 +0000557 EffectKey,
558 const char* vertexCoords,
559 const char* outputColor,
560 const char* inputColor,
561 const TextureSamplerArray& samplers) {
562 this->emitYCoordUniform(builder);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000563 SkString t;
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +0000564 t.printf("%s.x", builder->defaultTexCoordsName());
bsalomon@google.comf06df1b2012-09-06 20:22:31 +0000565 this->emitColorLookup(builder, t.c_str(), outputColor, inputColor, samplers[0]);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000566}
567
568/////////////////////////////////////////////////////////////////////
569
bsalomon@google.com08283af2012-10-26 13:01:20 +0000570bool SkLinearGradient::asNewEffect(GrContext* context, GrEffectStage* stage) const {
571 SkASSERT(NULL != context && NULL != stage);
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000572
bsalomon@google.com021fc732012-10-25 12:47:42 +0000573 SkAutoTUnref<GrEffect> effect(SkNEW_ARGS(GrLinearGradient, (context, *this, fTileMode)));
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000574
575 SkMatrix matrix;
576 if (this->getLocalMatrix(&matrix)) {
577 if (!matrix.invert(&matrix)) {
578 return false;
579 }
580 matrix.postConcat(fPtsToUnit);
bsalomon@google.com08283af2012-10-26 13:01:20 +0000581 stage->setEffect(effect, matrix);
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000582 } else {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000583 stage->setEffect(effect, fPtsToUnit);
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000584 }
skia.committer@gmail.com20c301b2012-10-17 02:01:13 +0000585
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000586 return true;
rileya@google.comd7cc6512012-07-27 14:00:39 +0000587}
588
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000589#else
590
bsalomon@google.com08283af2012-10-26 13:01:20 +0000591bool SkLinearGradient::asNewEffect(GrContext*, GrEffectStage*) const {
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000592 SkDEBUGFAIL("Should not call in GPU-less build");
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000593 return false;
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000594}
595
596#endif