blob: 2f41bebbcec3e787e1ec3ea8dbf7944481c64529 [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 }
90
91 const SkColor* fColors;
92 const SkScalar* fPos;
93 int fCount;
94 SkShader::TileMode fTileMode;
95 SkUnitMapper* fMapper;
96 };
97
98public:
99 SkGradientShaderBase(const Descriptor& desc);
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000100 virtual ~SkGradientShaderBase();
101
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000102 virtual bool setContext(const SkBitmap&, const SkPaint&, const SkMatrix&) SK_OVERRIDE;
103 virtual uint32_t getFlags() SK_OVERRIDE { return fFlags; }
104 virtual bool isOpaque() const SK_OVERRIDE;
105
106 void getGradientTableBitmap(SkBitmap*) const;
107
108 enum {
109 /// Seems like enough for visual accuracy. TODO: if pos[] deserves
110 /// it, use a larger cache.
111 kCache16Bits = 8,
reed@google.com3c2102c2013-02-01 12:59:40 +0000112 kCache16Count = (1 << kCache16Bits),
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000113 kCache16Shift = 16 - kCache16Bits,
114 kSqrt16Shift = 8 - kCache16Bits,
115
116 /// Seems like enough for visual accuracy. TODO: if pos[] deserves
117 /// it, use a larger cache.
118 kCache32Bits = 8,
reed@google.com60040292013-02-04 18:21:23 +0000119 kCache32Count = (1 << kCache32Bits),
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000120 kCache32Shift = 16 - kCache32Bits,
121 kSqrt32Shift = 8 - kCache32Bits,
122
123 /// This value is used to *read* the dither cache; it may be 0
124 /// if dithering is disabled.
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000125 kDitherStride32 = kCache32Count,
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000126 kDitherStride16 = kCache16Count,
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000127 };
128
129
130protected:
131 SkGradientShaderBase(SkFlattenableReadBuffer& );
132 virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000133 SK_DEVELOPER_TO_STRING()
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000134
135 SkUnitMapper* fMapper;
136 SkMatrix fPtsToUnit; // set by subclass
137 SkMatrix fDstToIndex;
138 SkMatrix::MapXYProc fDstToIndexProc;
139 TileMode fTileMode;
140 TileProc fTileProc;
141 int fColorCount;
142 uint8_t fDstToIndexClass;
143 uint8_t fFlags;
144
145 struct Rec {
146 SkFixed fPos; // 0...1
147 uint32_t fScale; // (1 << 24) / range
148 };
149 Rec* fRecs;
150
151 const uint16_t* getCache16() const;
152 const SkPMColor* getCache32() const;
153
154 void commonAsAGradient(GradientInfo*) const;
155
156private:
157 enum {
158 kColorStorageCount = 4, // more than this many colors, and we'll use sk_malloc for the space
159
160 kStorageSize = kColorStorageCount * (sizeof(SkColor) + sizeof(Rec))
161 };
162 SkColor fStorage[(kStorageSize + 3) >> 2];
163 SkColor* fOrigColors; // original colors, before modulation by paint in setContext
164 bool fColorsAreOpaque;
165
166 mutable uint16_t* fCache16; // working ptr. If this is NULL, we need to recompute the cache values
167 mutable SkPMColor* fCache32; // working ptr. If this is NULL, we need to recompute the cache values
168
169 mutable uint16_t* fCache16Storage; // storage for fCache16, allocated on demand
170 mutable SkMallocPixelRef* fCache32PixelRef;
171 mutable unsigned fCacheAlpha; // the alpha value we used when we computed the cache. larger than 8bits so we can store uninitialized value
172
173 static void Build16bitCache(uint16_t[], SkColor c0, SkColor c1, int count);
174 static void Build32bitCache(SkPMColor[], SkColor c0, SkColor c1, int count,
175 U8CPU alpha);
176 void setCacheAlpha(U8CPU alpha) const;
177 void initCommon();
178
179 typedef SkShader INHERITED;
180};
181
reed@google.com55853db2013-02-01 19:34:59 +0000182static inline int init_dither_toggle(int x, int y) {
reed@google.com60040292013-02-04 18:21:23 +0000183 x &= 1;
184 y = (y & 1) << 1;
185 return (x | y) * SkGradientShaderBase::kDitherStride32;
reed@google.com55853db2013-02-01 19:34:59 +0000186}
187
188static inline int next_dither_toggle(int toggle) {
189 return toggle ^ SkGradientShaderBase::kDitherStride32;
190}
191
192static inline int init_dither_toggle16(int x, int y) {
193 return ((x ^ y) & 1) * SkGradientShaderBase::kDitherStride16;
194}
195
196static inline int next_dither_toggle16(int toggle) {
197 return toggle ^ SkGradientShaderBase::kDitherStride16;
198}
199
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000200///////////////////////////////////////////////////////////////////////////////
201
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000202#if SK_SUPPORT_GPU
203
bsalomon@google.comd698f772012-10-25 13:22:00 +0000204#include "gl/GrGLEffect.h"
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000205#include "gl/GrGLEffectMatrix.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000206
bsalomon@google.com08283af2012-10-26 13:01:20 +0000207class GrEffectStage;
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000208class GrBackendEffectFactory;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000209
210/*
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000211 * The interpretation of the texture matrix depends on the sample mode. The
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000212 * texture matrix is applied both when the texture coordinates are explicit
213 * and when vertex positions are used as texture coordinates. In the latter
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000214 * case the texture matrix is applied to the pre-view-matrix position
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000215 * values.
216 *
217 * Normal SampleMode
218 * The post-matrix texture coordinates are in normalize space with (0,0) at
219 * the top-left and (1,1) at the bottom right.
220 * RadialGradient
221 * The matrix specifies the radial gradient parameters.
222 * (0,0) in the post-matrix space is center of the radial gradient.
223 * Radial2Gradient
224 * Matrix transforms to space where first circle is centered at the
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000225 * origin. The second circle will be centered (x, 0) where x may be
226 * 0 and is provided by setRadial2Params. The post-matrix space is
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000227 * normalized such that 1 is the second radius - first radius.
228 * SweepGradient
229 * The angle from the origin of texture coordinates in post-matrix space
230 * determines the gradient value.
231 */
232
rileya@google.comb3e50f22012-08-20 17:43:08 +0000233 class GrTextureStripAtlas;
234
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000235// Base class for Gr gradient effects
bsalomon@google.coma469c282012-10-24 18:28:34 +0000236class GrGradientEffect : public GrEffect {
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000237public:
238
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000239 GrGradientEffect(GrContext* ctx,
240 const SkGradientShaderBase& shader,
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000241 const SkMatrix& matrix,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000242 SkShader::TileMode tileMode);
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000243
244 virtual ~GrGradientEffect();
245
rileya@google.comb3e50f22012-08-20 17:43:08 +0000246 bool useAtlas() const { return SkToBool(-1 != fRow); }
bsalomon@google.com81712882012-11-01 17:12:34 +0000247 SkScalar getYCoord() const { return fYCoord; };
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000248 const SkMatrix& getMatrix() const { return fMatrix;}
rileya@google.comb3e50f22012-08-20 17:43:08 +0000249
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000250 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;
bsalomon@google.com371e1052013-01-11 21:08:55 +0000251
bsalomon@google.comd4726202012-08-03 14:34:46 +0000252protected:
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000253
bsalomon@google.comd4726202012-08-03 14:34:46 +0000254 /** Populates a pair of arrays with colors and stop info to construct a random gradient.
255 The function decides whether stop values should be used or not. The return value indicates
256 the number of colors, which will be capped by kMaxRandomGradientColors. colors should be
257 sized to be at least kMaxRandomGradientColors. stops is a pointer to an array of at least
258 size kMaxRandomGradientColors. It may be updated to NULL, indicating that NULL should be
259 passed to the gradient factory rather than the array.
260 */
261 static const int kMaxRandomGradientColors = 4;
bsalomon@google.com73a96942013-02-13 16:31:19 +0000262 static int RandomGradientParams(SkMWCRandom* r,
bsalomon@google.comd4726202012-08-03 14:34:46 +0000263 SkColor colors[kMaxRandomGradientColors],
264 SkScalar** stops,
265 SkShader::TileMode* tm);
266
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000267 virtual bool onIsEqual(const GrEffect& effect) const SK_OVERRIDE;
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000268
bsalomon@google.comd4726202012-08-03 14:34:46 +0000269private:
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000270
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000271 GrTextureAccess fTextureAccess;
bsalomon@google.com81712882012-11-01 17:12:34 +0000272 SkScalar fYCoord;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000273 GrTextureStripAtlas* fAtlas;
274 int fRow;
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000275 SkMatrix fMatrix;
bsalomon@google.com371e1052013-01-11 21:08:55 +0000276 bool fIsOpaque;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000277
bsalomon@google.coma469c282012-10-24 18:28:34 +0000278 typedef GrEffect INHERITED;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000279
280};
281
282///////////////////////////////////////////////////////////////////////////////
283
bsalomon@google.com8ea78d82012-10-24 20:11:30 +0000284// Base class for GL gradient effects
bsalomon@google.comf78df332012-10-29 12:43:38 +0000285class GrGLGradientEffect : public GrGLEffect {
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000286public:
bsalomon@google.com0707c292012-10-25 21:45:42 +0000287 GrGLGradientEffect(const GrBackendEffectFactory& factory);
288 virtual ~GrGLGradientEffect();
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000289
bsalomon@google.comc7818882013-03-20 19:19:53 +0000290 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000291
bsalomon@google.comf78df332012-10-29 12:43:38 +0000292protected:
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000293 /**
294 * Subclasses must reserve the lower kMatrixKeyBitCnt of their key for use by
295 * GrGLGradientEffect.
296 */
297 enum {
298 kMatrixKeyBitCnt = GrGLEffectMatrix::kKeyBits,
299 kMatrixKeyMask = (1 << kMatrixKeyBitCnt) - 1,
300 };
301
302 /**
303 * Subclasses must call this. It will return a value restricted to the lower kMatrixKeyBitCnt
304 * bits.
305 */
bsalomon@google.comc7818882013-03-20 19:19:53 +0000306 static EffectKey GenMatrixKey(const GrDrawEffect&);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000307
308 /**
309 * Inserts code to implement the GrGradientEffect's matrix. This should be called before a
310 * subclass emits its own code. The name of the 2D coords is output via fsCoordName and already
311 * incorporates any perspective division. The caller can also optionally retrieve the name of
312 * the varying inserted in the VS and its type, which may be either vec2f or vec3f depending
313 * upon whether the matrix has perspective or not. It is not necessary to mask the key before
314 * calling.
315 */
316 void setupMatrix(GrGLShaderBuilder* builder,
317 EffectKey key,
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000318 const char** fsCoordName,
319 const char** vsVaryingName = NULL,
320 GrSLType* vsVaryingType = NULL);
321
bsalomon@google.comf78df332012-10-29 12:43:38 +0000322 // Emits the uniform used as the y-coord to texture samples in derived classes. Subclasses
323 // should call this method from their emitCode().
324 void emitYCoordUniform(GrGLShaderBuilder* builder);
325
326 // emit code that gets a fragment's color from an expression for t; for now this always uses the
327 // texture, but for simpler cases we'll be able to lerp. Subclasses should call this method from
328 // their emitCode().
bsalomon@google.com868a8e72012-08-30 19:11:34 +0000329 void emitColorLookup(GrGLShaderBuilder* builder,
330 const char* gradientTValue,
331 const char* outputColor,
332 const char* inputColor,
bsalomon@google.comf06df1b2012-09-06 20:22:31 +0000333 const GrGLShaderBuilder::TextureSampler&);
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000334
335private:
bsalomon@google.comc7818882013-03-20 19:19:53 +0000336 static const GrEffect::CoordsType kCoordsType = GrEffect::kLocal_CoordsType;
337
bsalomon@google.com81712882012-11-01 17:12:34 +0000338 SkScalar fCachedYCoord;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000339 GrGLUniformManager::UniformHandle fFSYUni;
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000340 GrGLEffectMatrix fEffectMatrix;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000341
bsalomon@google.comf78df332012-10-29 12:43:38 +0000342 typedef GrGLEffect INHERITED;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000343};
344
345#endif
346
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000347#endif