blob: d35fb1c5961719d75bb49d4180fd38cb3b919789 [file] [log] [blame]
rileya@google.com1c6d64b2012-07-27 15:49:05 +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#ifndef SkGradientShaderPriv_DEFINED
10#define SkGradientShaderPriv_DEFINED
11
12#include "SkGradientShader.h"
13#include "SkClampRange.h"
14#include "SkColorPriv.h"
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000015#include "SkFlattenableBuffers.h"
rileya@google.com1c6d64b2012-07-27 15:49:05 +000016#include "SkMallocPixelRef.h"
17#include "SkUnitMapper.h"
18#include "SkUtils.h"
19#include "SkTemplates.h"
20#include "SkBitmapCache.h"
21#include "SkShader.h"
rileya@google.com1c6d64b2012-07-27 15:49:05 +000022
23#ifndef SK_DISABLE_DITHER_32BIT_GRADIENT
24 #define USE_DITHER_32BIT_GRADIENT
25#endif
26
humper@google.com05af1af2013-01-07 16:47:43 +000027static inline void sk_memset32_dither(uint32_t dst[], uint32_t v0, uint32_t v1,
rileya@google.com1c6d64b2012-07-27 15:49:05 +000028 int count) {
29 if (count > 0) {
30 if (v0 == v1) {
31 sk_memset32(dst, v0, count);
32 } else {
33 int pairs = count >> 1;
34 for (int i = 0; i < pairs; i++) {
35 *dst++ = v0;
36 *dst++ = v1;
37 }
38 if (count & 1) {
39 *dst = v0;
40 }
41 }
42 }
43}
44
45// Clamp
46
humper@google.com05af1af2013-01-07 16:47:43 +000047static inline SkFixed clamp_tileproc(SkFixed x) {
rileya@google.com1c6d64b2012-07-27 15:49:05 +000048 return SkClampMax(x, 0xFFFF);
49}
50
51// Repeat
52
humper@google.com05af1af2013-01-07 16:47:43 +000053static inline SkFixed repeat_tileproc(SkFixed x) {
rileya@google.com1c6d64b2012-07-27 15:49:05 +000054 return x & 0xFFFF;
55}
56
57// Mirror
58
59// Visual Studio 2010 (MSC_VER=1600) optimizes bit-shift code incorrectly.
60// See http://code.google.com/p/skia/issues/detail?id=472
61#if defined(_MSC_VER) && (_MSC_VER >= 1600)
62#pragma optimize("", off)
63#endif
64
65static inline SkFixed mirror_tileproc(SkFixed x) {
66 int s = x << 15 >> 31;
67 return (x ^ s) & 0xFFFF;
68}
69
70#if defined(_MSC_VER) && (_MSC_VER >= 1600)
71#pragma optimize("", on)
72#endif
73
74///////////////////////////////////////////////////////////////////////////////
75
76typedef SkFixed (*TileProc)(SkFixed);
77
78///////////////////////////////////////////////////////////////////////////////
79
80static const TileProc gTileProcs[] = {
81 clamp_tileproc,
82 repeat_tileproc,
83 mirror_tileproc
84};
85
86///////////////////////////////////////////////////////////////////////////////
87
88class SkGradientShaderBase : public SkShader {
89public:
90 SkGradientShaderBase(const SkColor colors[], const SkScalar pos[],
91 int colorCount, SkShader::TileMode mode, SkUnitMapper* mapper);
92 virtual ~SkGradientShaderBase();
93
rileya@google.com1c6d64b2012-07-27 15:49:05 +000094 virtual bool setContext(const SkBitmap&, const SkPaint&, const SkMatrix&) SK_OVERRIDE;
95 virtual uint32_t getFlags() SK_OVERRIDE { return fFlags; }
96 virtual bool isOpaque() const SK_OVERRIDE;
97
98 void getGradientTableBitmap(SkBitmap*) const;
99
100 enum {
101 /// Seems like enough for visual accuracy. TODO: if pos[] deserves
102 /// it, use a larger cache.
103 kCache16Bits = 8,
reed@google.com3c2102c2013-02-01 12:59:40 +0000104 kCache16Count = (1 << kCache16Bits),
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000105 kCache16Shift = 16 - kCache16Bits,
106 kSqrt16Shift = 8 - kCache16Bits,
107
108 /// Seems like enough for visual accuracy. TODO: if pos[] deserves
109 /// it, use a larger cache.
110 kCache32Bits = 8,
reed@google.com3c2102c2013-02-01 12:59:40 +0000111 kCache32Count = (1 << kCache32Bits),
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000112 kCache32Shift = 16 - kCache32Bits,
113 kSqrt32Shift = 8 - kCache32Bits,
114
115 /// This value is used to *read* the dither cache; it may be 0
116 /// if dithering is disabled.
117#ifdef USE_DITHER_32BIT_GRADIENT
118 kDitherStride32 = kCache32Count,
119#else
120 kDitherStride32 = 0,
121#endif
122 kDitherStride16 = kCache16Count,
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000123 };
124
125
126protected:
127 SkGradientShaderBase(SkFlattenableReadBuffer& );
128 virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000129 SK_DEVELOPER_TO_STRING()
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000130
131 SkUnitMapper* fMapper;
132 SkMatrix fPtsToUnit; // set by subclass
133 SkMatrix fDstToIndex;
134 SkMatrix::MapXYProc fDstToIndexProc;
135 TileMode fTileMode;
136 TileProc fTileProc;
137 int fColorCount;
138 uint8_t fDstToIndexClass;
139 uint8_t fFlags;
140
141 struct Rec {
142 SkFixed fPos; // 0...1
143 uint32_t fScale; // (1 << 24) / range
144 };
145 Rec* fRecs;
146
147 const uint16_t* getCache16() const;
148 const SkPMColor* getCache32() const;
149
150 void commonAsAGradient(GradientInfo*) const;
151
152private:
153 enum {
154 kColorStorageCount = 4, // more than this many colors, and we'll use sk_malloc for the space
155
156 kStorageSize = kColorStorageCount * (sizeof(SkColor) + sizeof(Rec))
157 };
158 SkColor fStorage[(kStorageSize + 3) >> 2];
159 SkColor* fOrigColors; // original colors, before modulation by paint in setContext
160 bool fColorsAreOpaque;
161
162 mutable uint16_t* fCache16; // working ptr. If this is NULL, we need to recompute the cache values
163 mutable SkPMColor* fCache32; // working ptr. If this is NULL, we need to recompute the cache values
164
165 mutable uint16_t* fCache16Storage; // storage for fCache16, allocated on demand
166 mutable SkMallocPixelRef* fCache32PixelRef;
167 mutable unsigned fCacheAlpha; // the alpha value we used when we computed the cache. larger than 8bits so we can store uninitialized value
168
169 static void Build16bitCache(uint16_t[], SkColor c0, SkColor c1, int count);
170 static void Build32bitCache(SkPMColor[], SkColor c0, SkColor c1, int count,
171 U8CPU alpha);
172 void setCacheAlpha(U8CPU alpha) const;
173 void initCommon();
174
175 typedef SkShader INHERITED;
176};
177
178///////////////////////////////////////////////////////////////////////////////
179
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000180#if SK_SUPPORT_GPU
181
bsalomon@google.comd698f772012-10-25 13:22:00 +0000182#include "gl/GrGLEffect.h"
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000183#include "gl/GrGLEffectMatrix.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000184
bsalomon@google.com08283af2012-10-26 13:01:20 +0000185class GrEffectStage;
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000186class GrBackendEffectFactory;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000187
188/*
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000189 * The interpretation of the texture matrix depends on the sample mode. The
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000190 * texture matrix is applied both when the texture coordinates are explicit
191 * and when vertex positions are used as texture coordinates. In the latter
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000192 * case the texture matrix is applied to the pre-view-matrix position
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000193 * values.
194 *
195 * Normal SampleMode
196 * The post-matrix texture coordinates are in normalize space with (0,0) at
197 * the top-left and (1,1) at the bottom right.
198 * RadialGradient
199 * The matrix specifies the radial gradient parameters.
200 * (0,0) in the post-matrix space is center of the radial gradient.
201 * Radial2Gradient
202 * Matrix transforms to space where first circle is centered at the
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000203 * origin. The second circle will be centered (x, 0) where x may be
204 * 0 and is provided by setRadial2Params. The post-matrix space is
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000205 * normalized such that 1 is the second radius - first radius.
206 * SweepGradient
207 * The angle from the origin of texture coordinates in post-matrix space
208 * determines the gradient value.
209 */
210
rileya@google.comb3e50f22012-08-20 17:43:08 +0000211 class GrTextureStripAtlas;
212
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000213// Base class for Gr gradient effects
bsalomon@google.coma469c282012-10-24 18:28:34 +0000214class GrGradientEffect : public GrEffect {
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000215public:
216
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000217 GrGradientEffect(GrContext* ctx,
218 const SkGradientShaderBase& shader,
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000219 const SkMatrix& matrix,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000220 SkShader::TileMode tileMode);
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000221
222 virtual ~GrGradientEffect();
223
rileya@google.comb3e50f22012-08-20 17:43:08 +0000224 bool useAtlas() const { return SkToBool(-1 != fRow); }
bsalomon@google.com81712882012-11-01 17:12:34 +0000225 SkScalar getYCoord() const { return fYCoord; };
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000226 const SkMatrix& getMatrix() const { return fMatrix;}
rileya@google.comb3e50f22012-08-20 17:43:08 +0000227
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000228 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;
bsalomon@google.com371e1052013-01-11 21:08:55 +0000229
bsalomon@google.comd4726202012-08-03 14:34:46 +0000230protected:
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000231
bsalomon@google.comd4726202012-08-03 14:34:46 +0000232 /** Populates a pair of arrays with colors and stop info to construct a random gradient.
233 The function decides whether stop values should be used or not. The return value indicates
234 the number of colors, which will be capped by kMaxRandomGradientColors. colors should be
235 sized to be at least kMaxRandomGradientColors. stops is a pointer to an array of at least
236 size kMaxRandomGradientColors. It may be updated to NULL, indicating that NULL should be
237 passed to the gradient factory rather than the array.
238 */
239 static const int kMaxRandomGradientColors = 4;
240 static int RandomGradientParams(SkRandom* r,
241 SkColor colors[kMaxRandomGradientColors],
242 SkScalar** stops,
243 SkShader::TileMode* tm);
244
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000245 virtual bool onIsEqual(const GrEffect& effect) const SK_OVERRIDE;
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000246
bsalomon@google.comd4726202012-08-03 14:34:46 +0000247private:
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000248
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000249 GrTextureAccess fTextureAccess;
bsalomon@google.com81712882012-11-01 17:12:34 +0000250 SkScalar fYCoord;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000251 GrTextureStripAtlas* fAtlas;
252 int fRow;
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000253 SkMatrix fMatrix;
bsalomon@google.com371e1052013-01-11 21:08:55 +0000254 bool fIsOpaque;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000255
bsalomon@google.coma469c282012-10-24 18:28:34 +0000256 typedef GrEffect INHERITED;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000257
258};
259
260///////////////////////////////////////////////////////////////////////////////
261
bsalomon@google.com8ea78d82012-10-24 20:11:30 +0000262// Base class for GL gradient effects
bsalomon@google.comf78df332012-10-29 12:43:38 +0000263class GrGLGradientEffect : public GrGLEffect {
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000264public:
bsalomon@google.com0707c292012-10-25 21:45:42 +0000265 GrGLGradientEffect(const GrBackendEffectFactory& factory);
266 virtual ~GrGLGradientEffect();
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000267
bsalomon@google.com28a15fb2012-10-26 17:53:18 +0000268 virtual void setData(const GrGLUniformManager&, const GrEffectStage&) SK_OVERRIDE;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000269
bsalomon@google.comf78df332012-10-29 12:43:38 +0000270protected:
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000271 /**
272 * Subclasses must reserve the lower kMatrixKeyBitCnt of their key for use by
273 * GrGLGradientEffect.
274 */
275 enum {
276 kMatrixKeyBitCnt = GrGLEffectMatrix::kKeyBits,
277 kMatrixKeyMask = (1 << kMatrixKeyBitCnt) - 1,
278 };
279
280 /**
281 * Subclasses must call this. It will return a value restricted to the lower kMatrixKeyBitCnt
282 * bits.
283 */
284 static EffectKey GenMatrixKey(const GrEffectStage& s);
285
286 /**
287 * Inserts code to implement the GrGradientEffect's matrix. This should be called before a
288 * subclass emits its own code. The name of the 2D coords is output via fsCoordName and already
289 * incorporates any perspective division. The caller can also optionally retrieve the name of
290 * the varying inserted in the VS and its type, which may be either vec2f or vec3f depending
291 * upon whether the matrix has perspective or not. It is not necessary to mask the key before
292 * calling.
293 */
294 void setupMatrix(GrGLShaderBuilder* builder,
295 EffectKey key,
296 const char* vertexCoords,
297 const char** fsCoordName,
298 const char** vsVaryingName = NULL,
299 GrSLType* vsVaryingType = NULL);
300
bsalomon@google.comf78df332012-10-29 12:43:38 +0000301 // Emits the uniform used as the y-coord to texture samples in derived classes. Subclasses
302 // should call this method from their emitCode().
303 void emitYCoordUniform(GrGLShaderBuilder* builder);
304
305 // emit code that gets a fragment's color from an expression for t; for now this always uses the
306 // texture, but for simpler cases we'll be able to lerp. Subclasses should call this method from
307 // their emitCode().
bsalomon@google.com868a8e72012-08-30 19:11:34 +0000308 void emitColorLookup(GrGLShaderBuilder* builder,
309 const char* gradientTValue,
310 const char* outputColor,
311 const char* inputColor,
bsalomon@google.comf06df1b2012-09-06 20:22:31 +0000312 const GrGLShaderBuilder::TextureSampler&);
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000313
314private:
bsalomon@google.com81712882012-11-01 17:12:34 +0000315 SkScalar fCachedYCoord;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000316 GrGLUniformManager::UniformHandle fFSYUni;
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000317 GrGLEffectMatrix fEffectMatrix;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000318
bsalomon@google.comf78df332012-10-29 12:43:38 +0000319 typedef GrGLEffect INHERITED;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000320};
321
322#endif
323
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000324#endif