blob: d5737835e4eb49e4cad6b62d3398f75dee63f2f2 [file] [log] [blame]
rileya@google.com1c6d64b2012-07-27 15:49:05 +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#ifndef SkGradientShaderPriv_DEFINED
9#define SkGradientShaderPriv_DEFINED
10
11#include "SkGradientShader.h"
12#include "SkClampRange.h"
13#include "SkColorPriv.h"
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000014#include "SkFlattenableBuffers.h"
rileya@google.com1c6d64b2012-07-27 15:49:05 +000015#include "SkMallocPixelRef.h"
16#include "SkUnitMapper.h"
17#include "SkUtils.h"
18#include "SkTemplates.h"
19#include "SkBitmapCache.h"
20#include "SkShader.h"
rileya@google.com1c6d64b2012-07-27 15:49:05 +000021
humper@google.com05af1af2013-01-07 16:47:43 +000022static inline void sk_memset32_dither(uint32_t dst[], uint32_t v0, uint32_t v1,
rileya@google.com1c6d64b2012-07-27 15:49:05 +000023 int count) {
24 if (count > 0) {
25 if (v0 == v1) {
26 sk_memset32(dst, v0, count);
27 } else {
28 int pairs = count >> 1;
29 for (int i = 0; i < pairs; i++) {
30 *dst++ = v0;
31 *dst++ = v1;
32 }
33 if (count & 1) {
34 *dst = v0;
35 }
36 }
37 }
38}
39
40// Clamp
41
humper@google.com05af1af2013-01-07 16:47:43 +000042static inline SkFixed clamp_tileproc(SkFixed x) {
rileya@google.com1c6d64b2012-07-27 15:49:05 +000043 return SkClampMax(x, 0xFFFF);
44}
45
46// Repeat
47
humper@google.com05af1af2013-01-07 16:47:43 +000048static inline SkFixed repeat_tileproc(SkFixed x) {
rileya@google.com1c6d64b2012-07-27 15:49:05 +000049 return x & 0xFFFF;
50}
51
52// Mirror
53
54// Visual Studio 2010 (MSC_VER=1600) optimizes bit-shift code incorrectly.
55// See http://code.google.com/p/skia/issues/detail?id=472
56#if defined(_MSC_VER) && (_MSC_VER >= 1600)
57#pragma optimize("", off)
58#endif
59
60static inline SkFixed mirror_tileproc(SkFixed x) {
61 int s = x << 15 >> 31;
62 return (x ^ s) & 0xFFFF;
63}
64
65#if defined(_MSC_VER) && (_MSC_VER >= 1600)
66#pragma optimize("", on)
67#endif
68
69///////////////////////////////////////////////////////////////////////////////
70
71typedef SkFixed (*TileProc)(SkFixed);
72
73///////////////////////////////////////////////////////////////////////////////
74
75static const TileProc gTileProcs[] = {
76 clamp_tileproc,
77 repeat_tileproc,
78 mirror_tileproc
79};
80
81///////////////////////////////////////////////////////////////////////////////
82
83class SkGradientShaderBase : public SkShader {
84public:
reed@google.com437d6eb2013-05-23 19:03:05 +000085 struct Descriptor {
86 Descriptor() {
87 sk_bzero(this, sizeof(*this));
88 fTileMode = SkShader::kClamp_TileMode;
89 }
skia.committer@gmail.com3e2345a2013-05-24 07:01:26 +000090
reed@google.com437d6eb2013-05-23 19:03:05 +000091 const SkColor* fColors;
92 const SkScalar* fPos;
93 int fCount;
94 SkShader::TileMode fTileMode;
95 SkUnitMapper* fMapper;
reed@google.com3d3a8602013-05-24 14:58:44 +000096 uint32_t fFlags;
reed@google.com437d6eb2013-05-23 19:03:05 +000097 };
98
99public:
100 SkGradientShaderBase(const Descriptor& desc);
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000101 virtual ~SkGradientShaderBase();
102
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000103 virtual bool setContext(const SkBitmap&, const SkPaint&, const SkMatrix&) SK_OVERRIDE;
104 virtual uint32_t getFlags() SK_OVERRIDE { return fFlags; }
105 virtual bool isOpaque() const SK_OVERRIDE;
106
107 void getGradientTableBitmap(SkBitmap*) const;
108
109 enum {
110 /// Seems like enough for visual accuracy. TODO: if pos[] deserves
111 /// it, use a larger cache.
112 kCache16Bits = 8,
reed@google.com3c2102c2013-02-01 12:59:40 +0000113 kCache16Count = (1 << kCache16Bits),
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000114 kCache16Shift = 16 - kCache16Bits,
115 kSqrt16Shift = 8 - kCache16Bits,
116
117 /// Seems like enough for visual accuracy. TODO: if pos[] deserves
118 /// it, use a larger cache.
119 kCache32Bits = 8,
reed@google.com60040292013-02-04 18:21:23 +0000120 kCache32Count = (1 << kCache32Bits),
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000121 kCache32Shift = 16 - kCache32Bits,
122 kSqrt32Shift = 8 - kCache32Bits,
123
124 /// This value is used to *read* the dither cache; it may be 0
125 /// if dithering is disabled.
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000126 kDitherStride32 = kCache32Count,
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000127 kDitherStride16 = kCache16Count,
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000128 };
129
130
131protected:
132 SkGradientShaderBase(SkFlattenableReadBuffer& );
133 virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000134 SK_DEVELOPER_TO_STRING()
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000135
136 SkUnitMapper* fMapper;
137 SkMatrix fPtsToUnit; // set by subclass
138 SkMatrix fDstToIndex;
139 SkMatrix::MapXYProc fDstToIndexProc;
140 TileMode fTileMode;
141 TileProc fTileProc;
142 int fColorCount;
143 uint8_t fDstToIndexClass;
144 uint8_t fFlags;
reed@google.com3d3a8602013-05-24 14:58:44 +0000145 uint8_t fGradFlags;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000146
147 struct Rec {
148 SkFixed fPos; // 0...1
149 uint32_t fScale; // (1 << 24) / range
150 };
151 Rec* fRecs;
152
153 const uint16_t* getCache16() const;
154 const SkPMColor* getCache32() const;
155
156 void commonAsAGradient(GradientInfo*) const;
157
158private:
159 enum {
160 kColorStorageCount = 4, // more than this many colors, and we'll use sk_malloc for the space
161
162 kStorageSize = kColorStorageCount * (sizeof(SkColor) + sizeof(Rec))
163 };
164 SkColor fStorage[(kStorageSize + 3) >> 2];
165 SkColor* fOrigColors; // original colors, before modulation by paint in setContext
166 bool fColorsAreOpaque;
167
168 mutable uint16_t* fCache16; // working ptr. If this is NULL, we need to recompute the cache values
169 mutable SkPMColor* fCache32; // working ptr. If this is NULL, we need to recompute the cache values
170
171 mutable uint16_t* fCache16Storage; // storage for fCache16, allocated on demand
172 mutable SkMallocPixelRef* fCache32PixelRef;
173 mutable unsigned fCacheAlpha; // the alpha value we used when we computed the cache. larger than 8bits so we can store uninitialized value
174
175 static void Build16bitCache(uint16_t[], SkColor c0, SkColor c1, int count);
176 static void Build32bitCache(SkPMColor[], SkColor c0, SkColor c1, int count,
reed@google.com3d3a8602013-05-24 14:58:44 +0000177 U8CPU alpha, uint32_t gradFlags);
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000178 void setCacheAlpha(U8CPU alpha) const;
179 void initCommon();
180
181 typedef SkShader INHERITED;
182};
183
reed@google.com55853db2013-02-01 19:34:59 +0000184static inline int init_dither_toggle(int x, int y) {
reed@google.com60040292013-02-04 18:21:23 +0000185 x &= 1;
186 y = (y & 1) << 1;
187 return (x | y) * SkGradientShaderBase::kDitherStride32;
reed@google.com55853db2013-02-01 19:34:59 +0000188}
189
190static inline int next_dither_toggle(int toggle) {
191 return toggle ^ SkGradientShaderBase::kDitherStride32;
192}
193
194static inline int init_dither_toggle16(int x, int y) {
195 return ((x ^ y) & 1) * SkGradientShaderBase::kDitherStride16;
196}
197
198static inline int next_dither_toggle16(int toggle) {
199 return toggle ^ SkGradientShaderBase::kDitherStride16;
200}
201
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000202///////////////////////////////////////////////////////////////////////////////
203
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000204#if SK_SUPPORT_GPU
205
bsalomon@google.com77af6802013-10-02 13:04:56 +0000206#include "GrCoordTransform.h"
bsalomon@google.comd698f772012-10-25 13:22:00 +0000207#include "gl/GrGLEffect.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000208
bsalomon@google.com08283af2012-10-26 13:01:20 +0000209class GrEffectStage;
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000210class GrBackendEffectFactory;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000211
212/*
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000213 * The interpretation of the texture matrix depends on the sample mode. The
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000214 * texture matrix is applied both when the texture coordinates are explicit
215 * and when vertex positions are used as texture coordinates. In the latter
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000216 * case the texture matrix is applied to the pre-view-matrix position
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000217 * values.
218 *
219 * Normal SampleMode
220 * The post-matrix texture coordinates are in normalize space with (0,0) at
221 * the top-left and (1,1) at the bottom right.
222 * RadialGradient
223 * The matrix specifies the radial gradient parameters.
224 * (0,0) in the post-matrix space is center of the radial gradient.
225 * Radial2Gradient
226 * Matrix transforms to space where first circle is centered at the
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000227 * origin. The second circle will be centered (x, 0) where x may be
228 * 0 and is provided by setRadial2Params. The post-matrix space is
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000229 * normalized such that 1 is the second radius - first radius.
230 * SweepGradient
231 * The angle from the origin of texture coordinates in post-matrix space
232 * determines the gradient value.
233 */
234
rileya@google.comb3e50f22012-08-20 17:43:08 +0000235 class GrTextureStripAtlas;
236
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000237// Base class for Gr gradient effects
bsalomon@google.coma469c282012-10-24 18:28:34 +0000238class GrGradientEffect : public GrEffect {
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000239public:
240
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000241 GrGradientEffect(GrContext* ctx,
242 const SkGradientShaderBase& shader,
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000243 const SkMatrix& matrix,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000244 SkShader::TileMode tileMode);
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000245
246 virtual ~GrGradientEffect();
247
rileya@google.comb3e50f22012-08-20 17:43:08 +0000248 bool useAtlas() const { return SkToBool(-1 != fRow); }
bsalomon@google.com81712882012-11-01 17:12:34 +0000249 SkScalar getYCoord() const { return fYCoord; };
rileya@google.comb3e50f22012-08-20 17:43:08 +0000250
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000251 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;
skia.committer@gmail.com9a070f22013-09-10 07:01:44 +0000252
bsalomon@google.com82d12232013-09-09 15:36:26 +0000253 enum ColorType {
254 kTwo_ColorType,
255 kThree_ColorType,
256 kTexture_ColorType
257 };
258
259 ColorType getColorType() const { return fColorType; }
260
261 enum PremulType {
262 kBeforeInterp_PremulType,
263 kAfterInterp_PremulType,
264 };
265
266 PremulType getPremulType() const { return fPremulType; }
267
268 const SkColor* getColors(int pos) const {
269 SkASSERT(fColorType != kTexture_ColorType);
270 SkASSERT((pos-1) <= fColorType);
271 return &fColors[pos];
272 }
bsalomon@google.com371e1052013-01-11 21:08:55 +0000273
bsalomon@google.comd4726202012-08-03 14:34:46 +0000274protected:
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000275
bsalomon@google.comd4726202012-08-03 14:34:46 +0000276 /** Populates a pair of arrays with colors and stop info to construct a random gradient.
277 The function decides whether stop values should be used or not. The return value indicates
278 the number of colors, which will be capped by kMaxRandomGradientColors. colors should be
279 sized to be at least kMaxRandomGradientColors. stops is a pointer to an array of at least
280 size kMaxRandomGradientColors. It may be updated to NULL, indicating that NULL should be
281 passed to the gradient factory rather than the array.
282 */
283 static const int kMaxRandomGradientColors = 4;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000284 static int RandomGradientParams(SkRandom* r,
bsalomon@google.comd4726202012-08-03 14:34:46 +0000285 SkColor colors[kMaxRandomGradientColors],
286 SkScalar** stops,
287 SkShader::TileMode* tm);
288
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000289 virtual bool onIsEqual(const GrEffect& effect) const SK_OVERRIDE;
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000290
bsalomon@google.comd4726202012-08-03 14:34:46 +0000291private:
bsalomon@google.com77af6802013-10-02 13:04:56 +0000292 static const GrCoordSet kCoordSet = kLocal_GrCoordSet;
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000293
bsalomon@google.com82d12232013-09-09 15:36:26 +0000294 enum {
295 kMaxAnalyticColors = 3 // if more colors use texture
296 };
297
bsalomon@google.com77af6802013-10-02 13:04:56 +0000298 GrCoordTransform fCoordTransform;
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000299 GrTextureAccess fTextureAccess;
bsalomon@google.com81712882012-11-01 17:12:34 +0000300 SkScalar fYCoord;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000301 GrTextureStripAtlas* fAtlas;
302 int fRow;
bsalomon@google.com371e1052013-01-11 21:08:55 +0000303 bool fIsOpaque;
bsalomon@google.com82d12232013-09-09 15:36:26 +0000304 ColorType fColorType;
305 SkColor fColors[kMaxAnalyticColors];
306 PremulType fPremulType; // This only changes behavior for two and three color special cases.
307 // It is already baked into to the table for texture gradients.
bsalomon@google.coma469c282012-10-24 18:28:34 +0000308 typedef GrEffect INHERITED;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000309
310};
311
312///////////////////////////////////////////////////////////////////////////////
313
bsalomon@google.com8ea78d82012-10-24 20:11:30 +0000314// Base class for GL gradient effects
bsalomon@google.comf78df332012-10-29 12:43:38 +0000315class GrGLGradientEffect : public GrGLEffect {
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000316public:
bsalomon@google.com0707c292012-10-25 21:45:42 +0000317 GrGLGradientEffect(const GrBackendEffectFactory& factory);
318 virtual ~GrGLGradientEffect();
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000319
bsalomon@google.comc7818882013-03-20 19:19:53 +0000320 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000321
bsalomon@google.comf78df332012-10-29 12:43:38 +0000322protected:
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000323 enum {
bsalomon@google.com82d12232013-09-09 15:36:26 +0000324 kPremulTypeKeyBitCnt = 1,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000325 kPremulTypeMask = 1,
bsalomon@google.com82d12232013-09-09 15:36:26 +0000326 kPremulBeforeInterpKey = kPremulTypeMask,
327
bsalomon@google.com77af6802013-10-02 13:04:56 +0000328 kTwoColorKey = 2 << kPremulTypeKeyBitCnt,
329 kThreeColorKey = 3 << kPremulTypeKeyBitCnt,
bsalomon@google.com82d12232013-09-09 15:36:26 +0000330 kColorKeyMask = kTwoColorKey | kThreeColorKey,
331 kColorKeyBitCnt = 2,
332
333 // Subclasses must shift any key bits they produce up by this amount
334 // and combine with the result of GenBaseGradientKey.
bsalomon@google.com77af6802013-10-02 13:04:56 +0000335 kBaseKeyBitCnt = (kPremulTypeKeyBitCnt + kColorKeyBitCnt)
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000336 };
337
bsalomon@google.com82d12232013-09-09 15:36:26 +0000338 static GrGradientEffect::ColorType ColorTypeFromKey(EffectKey key){
339 if (kTwoColorKey == (key & kColorKeyMask)) {
340 return GrGradientEffect::kTwo_ColorType;
341 } else if (kThreeColorKey == (key & kColorKeyMask)) {
342 return GrGradientEffect::kThree_ColorType;
343 } else {return GrGradientEffect::kTexture_ColorType;}
344 }
345
346 static GrGradientEffect::PremulType PremulTypeFromKey(EffectKey key){
347 if (kPremulBeforeInterpKey == (key & kPremulTypeMask)) {
348 return GrGradientEffect::kBeforeInterp_PremulType;
349 } else {
350 return GrGradientEffect::kAfterInterp_PremulType;
351 }
352 }
353
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000354 /**
bsalomon@google.com82d12232013-09-09 15:36:26 +0000355 * Subclasses must call this. It will return a value restricted to the lower kBaseKeyBitCnt
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000356 * bits.
357 */
bsalomon@google.com82d12232013-09-09 15:36:26 +0000358 static EffectKey GenBaseGradientKey(const GrDrawEffect&);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000359
bsalomon@google.comf78df332012-10-29 12:43:38 +0000360 // Emits the uniform used as the y-coord to texture samples in derived classes. Subclasses
361 // should call this method from their emitCode().
bsalomon@google.com82d12232013-09-09 15:36:26 +0000362 void emitUniforms(GrGLShaderBuilder* builder, EffectKey key);
bsalomon@google.comf78df332012-10-29 12:43:38 +0000363
bsalomon@google.com82d12232013-09-09 15:36:26 +0000364
365 // emit code that gets a fragment's color from an expression for t; Has branches for 3 separate
366 // control flows inside -- 2 color gradients, 3 color symmetric gradients (both using
367 // native GLSL mix), and 4+ color gradients that use the traditional texture lookup.
368 void emitColor(GrGLShaderBuilder* builder,
369 const char* gradientTValue,
370 EffectKey key,
371 const char* outputColor,
372 const char* inputColor,
373 const GrGLShaderBuilder::TextureSamplerArray& samplers);
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000374
375private:
bsalomon@google.com81712882012-11-01 17:12:34 +0000376 SkScalar fCachedYCoord;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000377 GrGLUniformManager::UniformHandle fFSYUni;
bsalomon@google.com82d12232013-09-09 15:36:26 +0000378 GrGLUniformManager::UniformHandle fColorStartUni;
379 GrGLUniformManager::UniformHandle fColorMidUni;
380 GrGLUniformManager::UniformHandle fColorEndUni;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000381
bsalomon@google.comf78df332012-10-29 12:43:38 +0000382 typedef GrGLEffect INHERITED;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000383};
384
385#endif
386
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000387#endif