blob: b9dbf1b87b062c193c49d7ab696ea6533aec2a8b [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:
85 SkGradientShaderBase(const SkColor colors[], const SkScalar pos[],
86 int colorCount, SkShader::TileMode mode, SkUnitMapper* mapper);
87 virtual ~SkGradientShaderBase();
88
rileya@google.com1c6d64b2012-07-27 15:49:05 +000089 virtual bool setContext(const SkBitmap&, const SkPaint&, const SkMatrix&) SK_OVERRIDE;
90 virtual uint32_t getFlags() SK_OVERRIDE { return fFlags; }
91 virtual bool isOpaque() const SK_OVERRIDE;
92
93 void getGradientTableBitmap(SkBitmap*) const;
94
95 enum {
96 /// Seems like enough for visual accuracy. TODO: if pos[] deserves
97 /// it, use a larger cache.
98 kCache16Bits = 8,
reed@google.com3c2102c2013-02-01 12:59:40 +000099 kCache16Count = (1 << kCache16Bits),
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000100 kCache16Shift = 16 - kCache16Bits,
101 kSqrt16Shift = 8 - kCache16Bits,
102
103 /// Seems like enough for visual accuracy. TODO: if pos[] deserves
104 /// it, use a larger cache.
105 kCache32Bits = 8,
reed@google.com60040292013-02-04 18:21:23 +0000106 kCache32Count = (1 << kCache32Bits),
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000107 kCache32Shift = 16 - kCache32Bits,
108 kSqrt32Shift = 8 - kCache32Bits,
109
110 /// This value is used to *read* the dither cache; it may be 0
111 /// if dithering is disabled.
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000112 kDitherStride32 = kCache32Count,
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000113 kDitherStride16 = kCache16Count,
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000114 };
115
116
117protected:
118 SkGradientShaderBase(SkFlattenableReadBuffer& );
119 virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000120 SK_DEVELOPER_TO_STRING()
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000121
122 SkUnitMapper* fMapper;
123 SkMatrix fPtsToUnit; // set by subclass
124 SkMatrix fDstToIndex;
125 SkMatrix::MapXYProc fDstToIndexProc;
126 TileMode fTileMode;
127 TileProc fTileProc;
128 int fColorCount;
129 uint8_t fDstToIndexClass;
130 uint8_t fFlags;
131
132 struct Rec {
133 SkFixed fPos; // 0...1
134 uint32_t fScale; // (1 << 24) / range
135 };
136 Rec* fRecs;
137
138 const uint16_t* getCache16() const;
139 const SkPMColor* getCache32() const;
140
141 void commonAsAGradient(GradientInfo*) const;
142
143private:
144 enum {
145 kColorStorageCount = 4, // more than this many colors, and we'll use sk_malloc for the space
146
147 kStorageSize = kColorStorageCount * (sizeof(SkColor) + sizeof(Rec))
148 };
149 SkColor fStorage[(kStorageSize + 3) >> 2];
150 SkColor* fOrigColors; // original colors, before modulation by paint in setContext
151 bool fColorsAreOpaque;
152
153 mutable uint16_t* fCache16; // working ptr. If this is NULL, we need to recompute the cache values
154 mutable SkPMColor* fCache32; // working ptr. If this is NULL, we need to recompute the cache values
155
156 mutable uint16_t* fCache16Storage; // storage for fCache16, allocated on demand
157 mutable SkMallocPixelRef* fCache32PixelRef;
158 mutable unsigned fCacheAlpha; // the alpha value we used when we computed the cache. larger than 8bits so we can store uninitialized value
159
160 static void Build16bitCache(uint16_t[], SkColor c0, SkColor c1, int count);
161 static void Build32bitCache(SkPMColor[], SkColor c0, SkColor c1, int count,
162 U8CPU alpha);
163 void setCacheAlpha(U8CPU alpha) const;
164 void initCommon();
165
166 typedef SkShader INHERITED;
167};
168
reed@google.com55853db2013-02-01 19:34:59 +0000169static inline int init_dither_toggle(int x, int y) {
reed@google.com60040292013-02-04 18:21:23 +0000170 x &= 1;
171 y = (y & 1) << 1;
172 return (x | y) * SkGradientShaderBase::kDitherStride32;
reed@google.com55853db2013-02-01 19:34:59 +0000173}
174
175static inline int next_dither_toggle(int toggle) {
176 return toggle ^ SkGradientShaderBase::kDitherStride32;
177}
178
179static inline int init_dither_toggle16(int x, int y) {
180 return ((x ^ y) & 1) * SkGradientShaderBase::kDitherStride16;
181}
182
183static inline int next_dither_toggle16(int toggle) {
184 return toggle ^ SkGradientShaderBase::kDitherStride16;
185}
186
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000187///////////////////////////////////////////////////////////////////////////////
188
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000189#if SK_SUPPORT_GPU
190
bsalomon@google.comd698f772012-10-25 13:22:00 +0000191#include "gl/GrGLEffect.h"
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000192#include "gl/GrGLEffectMatrix.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000193
bsalomon@google.com08283af2012-10-26 13:01:20 +0000194class GrEffectStage;
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000195class GrBackendEffectFactory;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000196
197/*
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000198 * The interpretation of the texture matrix depends on the sample mode. The
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000199 * texture matrix is applied both when the texture coordinates are explicit
200 * and when vertex positions are used as texture coordinates. In the latter
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000201 * case the texture matrix is applied to the pre-view-matrix position
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000202 * values.
203 *
204 * Normal SampleMode
205 * The post-matrix texture coordinates are in normalize space with (0,0) at
206 * the top-left and (1,1) at the bottom right.
207 * RadialGradient
208 * The matrix specifies the radial gradient parameters.
209 * (0,0) in the post-matrix space is center of the radial gradient.
210 * Radial2Gradient
211 * Matrix transforms to space where first circle is centered at the
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000212 * origin. The second circle will be centered (x, 0) where x may be
213 * 0 and is provided by setRadial2Params. The post-matrix space is
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000214 * normalized such that 1 is the second radius - first radius.
215 * SweepGradient
216 * The angle from the origin of texture coordinates in post-matrix space
217 * determines the gradient value.
218 */
219
rileya@google.comb3e50f22012-08-20 17:43:08 +0000220 class GrTextureStripAtlas;
221
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000222// Base class for Gr gradient effects
bsalomon@google.coma469c282012-10-24 18:28:34 +0000223class GrGradientEffect : public GrEffect {
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000224public:
225
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000226 GrGradientEffect(GrContext* ctx,
227 const SkGradientShaderBase& shader,
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000228 const SkMatrix& matrix,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000229 SkShader::TileMode tileMode);
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000230
231 virtual ~GrGradientEffect();
232
rileya@google.comb3e50f22012-08-20 17:43:08 +0000233 bool useAtlas() const { return SkToBool(-1 != fRow); }
bsalomon@google.com81712882012-11-01 17:12:34 +0000234 SkScalar getYCoord() const { return fYCoord; };
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000235 const SkMatrix& getMatrix() const { return fMatrix;}
rileya@google.comb3e50f22012-08-20 17:43:08 +0000236
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000237 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;
bsalomon@google.com371e1052013-01-11 21:08:55 +0000238
bsalomon@google.comd4726202012-08-03 14:34:46 +0000239protected:
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000240
bsalomon@google.comd4726202012-08-03 14:34:46 +0000241 /** Populates a pair of arrays with colors and stop info to construct a random gradient.
242 The function decides whether stop values should be used or not. The return value indicates
243 the number of colors, which will be capped by kMaxRandomGradientColors. colors should be
244 sized to be at least kMaxRandomGradientColors. stops is a pointer to an array of at least
245 size kMaxRandomGradientColors. It may be updated to NULL, indicating that NULL should be
246 passed to the gradient factory rather than the array.
247 */
248 static const int kMaxRandomGradientColors = 4;
bsalomon@google.com73a96942013-02-13 16:31:19 +0000249 static int RandomGradientParams(SkMWCRandom* r,
bsalomon@google.comd4726202012-08-03 14:34:46 +0000250 SkColor colors[kMaxRandomGradientColors],
251 SkScalar** stops,
252 SkShader::TileMode* tm);
253
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000254 virtual bool onIsEqual(const GrEffect& effect) const SK_OVERRIDE;
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000255
bsalomon@google.comd4726202012-08-03 14:34:46 +0000256private:
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000257
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000258 GrTextureAccess fTextureAccess;
bsalomon@google.com81712882012-11-01 17:12:34 +0000259 SkScalar fYCoord;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000260 GrTextureStripAtlas* fAtlas;
261 int fRow;
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000262 SkMatrix fMatrix;
bsalomon@google.com371e1052013-01-11 21:08:55 +0000263 bool fIsOpaque;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000264
bsalomon@google.coma469c282012-10-24 18:28:34 +0000265 typedef GrEffect INHERITED;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000266
267};
268
269///////////////////////////////////////////////////////////////////////////////
270
bsalomon@google.com8ea78d82012-10-24 20:11:30 +0000271// Base class for GL gradient effects
bsalomon@google.comf78df332012-10-29 12:43:38 +0000272class GrGLGradientEffect : public GrGLEffect {
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000273public:
bsalomon@google.com0707c292012-10-25 21:45:42 +0000274 GrGLGradientEffect(const GrBackendEffectFactory& factory);
275 virtual ~GrGLGradientEffect();
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000276
bsalomon@google.comc7818882013-03-20 19:19:53 +0000277 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000278
bsalomon@google.comf78df332012-10-29 12:43:38 +0000279protected:
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000280 /**
281 * Subclasses must reserve the lower kMatrixKeyBitCnt of their key for use by
282 * GrGLGradientEffect.
283 */
284 enum {
285 kMatrixKeyBitCnt = GrGLEffectMatrix::kKeyBits,
286 kMatrixKeyMask = (1 << kMatrixKeyBitCnt) - 1,
287 };
288
289 /**
290 * Subclasses must call this. It will return a value restricted to the lower kMatrixKeyBitCnt
291 * bits.
292 */
bsalomon@google.comc7818882013-03-20 19:19:53 +0000293 static EffectKey GenMatrixKey(const GrDrawEffect&);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000294
295 /**
296 * Inserts code to implement the GrGradientEffect's matrix. This should be called before a
297 * subclass emits its own code. The name of the 2D coords is output via fsCoordName and already
298 * incorporates any perspective division. The caller can also optionally retrieve the name of
299 * the varying inserted in the VS and its type, which may be either vec2f or vec3f depending
300 * upon whether the matrix has perspective or not. It is not necessary to mask the key before
301 * calling.
302 */
303 void setupMatrix(GrGLShaderBuilder* builder,
304 EffectKey key,
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000305 const char** fsCoordName,
306 const char** vsVaryingName = NULL,
307 GrSLType* vsVaryingType = NULL);
308
bsalomon@google.comf78df332012-10-29 12:43:38 +0000309 // Emits the uniform used as the y-coord to texture samples in derived classes. Subclasses
310 // should call this method from their emitCode().
311 void emitYCoordUniform(GrGLShaderBuilder* builder);
312
313 // emit code that gets a fragment's color from an expression for t; for now this always uses the
314 // texture, but for simpler cases we'll be able to lerp. Subclasses should call this method from
315 // their emitCode().
bsalomon@google.com868a8e72012-08-30 19:11:34 +0000316 void emitColorLookup(GrGLShaderBuilder* builder,
317 const char* gradientTValue,
318 const char* outputColor,
319 const char* inputColor,
bsalomon@google.comf06df1b2012-09-06 20:22:31 +0000320 const GrGLShaderBuilder::TextureSampler&);
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000321
322private:
bsalomon@google.comc7818882013-03-20 19:19:53 +0000323 static const GrEffect::CoordsType kCoordsType = GrEffect::kLocal_CoordsType;
324
bsalomon@google.com81712882012-11-01 17:12:34 +0000325 SkScalar fCachedYCoord;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000326 GrGLUniformManager::UniformHandle fFSYUni;
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000327 GrGLEffectMatrix fEffectMatrix;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000328
bsalomon@google.comf78df332012-10-29 12:43:38 +0000329 typedef GrGLEffect INHERITED;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000330};
331
332#endif
333
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000334#endif