blob: f7a41893db7a93826e9d5f77eafb7aea951cbfe2 [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.comd698f772012-10-25 13:22:00 +0000206#include "gl/GrGLEffect.h"
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000207#include "gl/GrGLEffectMatrix.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; };
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000250 const SkMatrix& getMatrix() const { return fMatrix;}
rileya@google.comb3e50f22012-08-20 17:43:08 +0000251
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000252 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;
bsalomon@google.com82d12232013-09-09 15:36:26 +0000253
254 enum ColorType {
255 kTwo_ColorType,
256 kThree_ColorType,
257 kTexture_ColorType
258 };
259
260 ColorType getColorType() const { return fColorType; }
261
262 enum PremulType {
263 kBeforeInterp_PremulType,
264 kAfterInterp_PremulType,
265 };
266
267 PremulType getPremulType() const { return fPremulType; }
268
269 const SkColor* getColors(int pos) const {
270 SkASSERT(fColorType != kTexture_ColorType);
271 SkASSERT((pos-1) <= fColorType);
272 return &fColors[pos];
273 }
bsalomon@google.com371e1052013-01-11 21:08:55 +0000274
bsalomon@google.comd4726202012-08-03 14:34:46 +0000275protected:
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000276
bsalomon@google.comd4726202012-08-03 14:34:46 +0000277 /** Populates a pair of arrays with colors and stop info to construct a random gradient.
278 The function decides whether stop values should be used or not. The return value indicates
279 the number of colors, which will be capped by kMaxRandomGradientColors. colors should be
280 sized to be at least kMaxRandomGradientColors. stops is a pointer to an array of at least
281 size kMaxRandomGradientColors. It may be updated to NULL, indicating that NULL should be
282 passed to the gradient factory rather than the array.
283 */
284 static const int kMaxRandomGradientColors = 4;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000285 static int RandomGradientParams(SkRandom* r,
bsalomon@google.comd4726202012-08-03 14:34:46 +0000286 SkColor colors[kMaxRandomGradientColors],
287 SkScalar** stops,
288 SkShader::TileMode* tm);
289
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000290 virtual bool onIsEqual(const GrEffect& effect) const SK_OVERRIDE;
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000291
bsalomon@google.comd4726202012-08-03 14:34:46 +0000292private:
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.com6d003d12012-09-11 15:45:20 +0000298 GrTextureAccess fTextureAccess;
bsalomon@google.com81712882012-11-01 17:12:34 +0000299 SkScalar fYCoord;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000300 GrTextureStripAtlas* fAtlas;
301 int fRow;
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000302 SkMatrix fMatrix;
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 /**
324 * Subclasses must reserve the lower kMatrixKeyBitCnt of their key for use by
325 * GrGLGradientEffect.
326 */
327 enum {
328 kMatrixKeyBitCnt = GrGLEffectMatrix::kKeyBits,
329 kMatrixKeyMask = (1 << kMatrixKeyBitCnt) - 1,
bsalomon@google.com82d12232013-09-09 15:36:26 +0000330
331 kPremulTypeKeyBitCnt = 1,
332 kPremulTypeMask = 1 << kMatrixKeyBitCnt,
333 kPremulBeforeInterpKey = kPremulTypeMask,
334
335 kTwoColorKey = 2 << (kMatrixKeyBitCnt + kPremulTypeKeyBitCnt),
336 kThreeColorKey = 3 << (kMatrixKeyBitCnt + kPremulTypeKeyBitCnt),
337 kColorKeyMask = kTwoColorKey | kThreeColorKey,
338 kColorKeyBitCnt = 2,
339
340 // Subclasses must shift any key bits they produce up by this amount
341 // and combine with the result of GenBaseGradientKey.
342 kBaseKeyBitCnt = (kMatrixKeyBitCnt + kPremulTypeKeyBitCnt + kColorKeyBitCnt)
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000343 };
344
bsalomon@google.com82d12232013-09-09 15:36:26 +0000345 static GrGradientEffect::ColorType ColorTypeFromKey(EffectKey key){
346 if (kTwoColorKey == (key & kColorKeyMask)) {
347 return GrGradientEffect::kTwo_ColorType;
348 } else if (kThreeColorKey == (key & kColorKeyMask)) {
349 return GrGradientEffect::kThree_ColorType;
350 } else {return GrGradientEffect::kTexture_ColorType;}
351 }
352
353 static GrGradientEffect::PremulType PremulTypeFromKey(EffectKey key){
354 if (kPremulBeforeInterpKey == (key & kPremulTypeMask)) {
355 return GrGradientEffect::kBeforeInterp_PremulType;
356 } else {
357 return GrGradientEffect::kAfterInterp_PremulType;
358 }
359 }
360
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000361 /**
bsalomon@google.com82d12232013-09-09 15:36:26 +0000362 * Subclasses must call this. It will return a value restricted to the lower kBaseKeyBitCnt
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000363 * bits.
364 */
bsalomon@google.com82d12232013-09-09 15:36:26 +0000365 static EffectKey GenBaseGradientKey(const GrDrawEffect&);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000366
367 /**
368 * Inserts code to implement the GrGradientEffect's matrix. This should be called before a
369 * subclass emits its own code. The name of the 2D coords is output via fsCoordName and already
370 * incorporates any perspective division. The caller can also optionally retrieve the name of
371 * the varying inserted in the VS and its type, which may be either vec2f or vec3f depending
372 * upon whether the matrix has perspective or not. It is not necessary to mask the key before
373 * calling.
374 */
375 void setupMatrix(GrGLShaderBuilder* builder,
376 EffectKey key,
commit-bot@chromium.org7ab7ca42013-08-28 15:59:13 +0000377 SkString* fsCoordName,
378 SkString* vsVaryingName = NULL,
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000379 GrSLType* vsVaryingType = NULL);
380
bsalomon@google.comf78df332012-10-29 12:43:38 +0000381 // Emits the uniform used as the y-coord to texture samples in derived classes. Subclasses
382 // should call this method from their emitCode().
bsalomon@google.com82d12232013-09-09 15:36:26 +0000383 void emitUniforms(GrGLShaderBuilder* builder, EffectKey key);
bsalomon@google.comf78df332012-10-29 12:43:38 +0000384
bsalomon@google.com82d12232013-09-09 15:36:26 +0000385
386 // emit code that gets a fragment's color from an expression for t; Has branches for 3 separate
387 // control flows inside -- 2 color gradients, 3 color symmetric gradients (both using
388 // native GLSL mix), and 4+ color gradients that use the traditional texture lookup.
389 void emitColor(GrGLShaderBuilder* builder,
390 const char* gradientTValue,
391 EffectKey key,
392 const char* outputColor,
393 const char* inputColor,
394 const GrGLShaderBuilder::TextureSamplerArray& samplers);
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000395
396private:
bsalomon@google.comc7818882013-03-20 19:19:53 +0000397 static const GrEffect::CoordsType kCoordsType = GrEffect::kLocal_CoordsType;
398
bsalomon@google.com81712882012-11-01 17:12:34 +0000399 SkScalar fCachedYCoord;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000400 GrGLUniformManager::UniformHandle fFSYUni;
bsalomon@google.com82d12232013-09-09 15:36:26 +0000401 GrGLUniformManager::UniformHandle fColorStartUni;
402 GrGLUniformManager::UniformHandle fColorMidUni;
403 GrGLUniformManager::UniformHandle fColorEndUni;
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000404 GrGLEffectMatrix fEffectMatrix;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000405
bsalomon@google.comf78df332012-10-29 12:43:38 +0000406 typedef GrGLEffect INHERITED;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000407};
408
409#endif
410
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000411#endif