blob: 841771644c910f9fea6ad2de3d161f4ba95dfc7d [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
27static void sk_memset32_dither(uint32_t dst[], uint32_t v0, uint32_t v1,
28 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
47static SkFixed clamp_tileproc(SkFixed x) {
48 return SkClampMax(x, 0xFFFF);
49}
50
51// Repeat
52
53static SkFixed repeat_tileproc(SkFixed x) {
54 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
94 // overrides
95 virtual bool setContext(const SkBitmap&, const SkPaint&, const SkMatrix&) SK_OVERRIDE;
96 virtual uint32_t getFlags() SK_OVERRIDE { return fFlags; }
97 virtual bool isOpaque() const SK_OVERRIDE;
98
99 void getGradientTableBitmap(SkBitmap*) const;
100
101 enum {
102 /// Seems like enough for visual accuracy. TODO: if pos[] deserves
103 /// it, use a larger cache.
104 kCache16Bits = 8,
105 kGradient16Length = (1 << kCache16Bits),
106 /// Each cache gets 1 extra entry at the end so we don't have to
107 /// test for end-of-cache in lerps. This is also the value used
108 /// to stride *writes* into the dither cache; it must not be zero.
109 /// Total space for a cache is 2x kCache16Count entries: one
110 /// regular cache, one for dithering.
111 kCache16Count = kGradient16Length + 1,
112 kCache16Shift = 16 - kCache16Bits,
113 kSqrt16Shift = 8 - kCache16Bits,
114
115 /// Seems like enough for visual accuracy. TODO: if pos[] deserves
116 /// it, use a larger cache.
117 kCache32Bits = 8,
118 kGradient32Length = (1 << kCache32Bits),
119 /// Each cache gets 1 extra entry at the end so we don't have to
120 /// test for end-of-cache in lerps. This is also the value used
121 /// to stride *writes* into the dither cache; it must not be zero.
122 /// Total space for a cache is 2x kCache32Count entries: one
123 /// regular cache, one for dithering.
124 kCache32Count = kGradient32Length + 1,
125 kCache32Shift = 16 - kCache32Bits,
126 kSqrt32Shift = 8 - kCache32Bits,
127
128 /// This value is used to *read* the dither cache; it may be 0
129 /// if dithering is disabled.
130#ifdef USE_DITHER_32BIT_GRADIENT
131 kDitherStride32 = kCache32Count,
132#else
133 kDitherStride32 = 0,
134#endif
135 kDitherStride16 = kCache16Count,
136 kLerpRemainderMask32 = (1 << (16 - kCache32Bits)) - 1
137 };
138
139
140protected:
141 SkGradientShaderBase(SkFlattenableReadBuffer& );
142 virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
143
144 SkUnitMapper* fMapper;
145 SkMatrix fPtsToUnit; // set by subclass
146 SkMatrix fDstToIndex;
147 SkMatrix::MapXYProc fDstToIndexProc;
148 TileMode fTileMode;
149 TileProc fTileProc;
150 int fColorCount;
151 uint8_t fDstToIndexClass;
152 uint8_t fFlags;
153
154 struct Rec {
155 SkFixed fPos; // 0...1
156 uint32_t fScale; // (1 << 24) / range
157 };
158 Rec* fRecs;
159
160 const uint16_t* getCache16() const;
161 const SkPMColor* getCache32() const;
162
163 void commonAsAGradient(GradientInfo*) const;
164
165private:
166 enum {
167 kColorStorageCount = 4, // more than this many colors, and we'll use sk_malloc for the space
168
169 kStorageSize = kColorStorageCount * (sizeof(SkColor) + sizeof(Rec))
170 };
171 SkColor fStorage[(kStorageSize + 3) >> 2];
172 SkColor* fOrigColors; // original colors, before modulation by paint in setContext
173 bool fColorsAreOpaque;
174
175 mutable uint16_t* fCache16; // working ptr. If this is NULL, we need to recompute the cache values
176 mutable SkPMColor* fCache32; // working ptr. If this is NULL, we need to recompute the cache values
177
178 mutable uint16_t* fCache16Storage; // storage for fCache16, allocated on demand
179 mutable SkMallocPixelRef* fCache32PixelRef;
180 mutable unsigned fCacheAlpha; // the alpha value we used when we computed the cache. larger than 8bits so we can store uninitialized value
181
182 static void Build16bitCache(uint16_t[], SkColor c0, SkColor c1, int count);
183 static void Build32bitCache(SkPMColor[], SkColor c0, SkColor c1, int count,
184 U8CPU alpha);
185 void setCacheAlpha(U8CPU alpha) const;
186 void initCommon();
187
188 typedef SkShader INHERITED;
189};
190
191///////////////////////////////////////////////////////////////////////////////
192
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000193#if SK_SUPPORT_GPU
194
bsalomon@google.comd698f772012-10-25 13:22:00 +0000195#include "gl/GrGLEffect.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000196
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000197class GrSamplerState;
198class GrProgramStageFactory;
199
200/*
201 * The intepretation of the texture matrix depends on the sample mode. The
202 * texture matrix is applied both when the texture coordinates are explicit
203 * and when vertex positions are used as texture coordinates. In the latter
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000204 * case the texture matrix is applied to the pre-view-matrix position
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000205 * values.
206 *
207 * Normal SampleMode
208 * The post-matrix texture coordinates are in normalize space with (0,0) at
209 * the top-left and (1,1) at the bottom right.
210 * RadialGradient
211 * The matrix specifies the radial gradient parameters.
212 * (0,0) in the post-matrix space is center of the radial gradient.
213 * Radial2Gradient
214 * Matrix transforms to space where first circle is centered at the
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000215 * origin. The second circle will be centered (x, 0) where x may be
216 * 0 and is provided by setRadial2Params. The post-matrix space is
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000217 * normalized such that 1 is the second radius - first radius.
218 * SweepGradient
219 * The angle from the origin of texture coordinates in post-matrix space
220 * determines the gradient value.
221 */
222
rileya@google.comb3e50f22012-08-20 17:43:08 +0000223 class GrTextureStripAtlas;
224
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000225// Base class for Gr gradient effects
bsalomon@google.coma469c282012-10-24 18:28:34 +0000226class GrGradientEffect : public GrEffect {
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000227public:
228
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000229 GrGradientEffect(GrContext* ctx,
230 const SkGradientShaderBase& shader,
231 SkShader::TileMode tileMode);
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000232
233 virtual ~GrGradientEffect();
234
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000235 virtual const GrTextureAccess& textureAccess(int index) const SK_OVERRIDE;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000236
rileya@google.comb3e50f22012-08-20 17:43:08 +0000237 bool useAtlas() const { return SkToBool(-1 != fRow); }
bsalomon@google.come6e62d12012-10-04 14:38:48 +0000238 GrScalar getYCoord() const { return fYCoord; };
rileya@google.comb3e50f22012-08-20 17:43:08 +0000239
bsalomon@google.com021fc732012-10-25 12:47:42 +0000240 virtual bool isEqual(const GrEffect& effect) const SK_OVERRIDE {
241 const GrGradientEffect& s = static_cast<const GrGradientEffect&>(effect);
242 return INHERITED::isEqual(effect) && this->useAtlas() == s.useAtlas() &&
rileya@google.comb3e50f22012-08-20 17:43:08 +0000243 fYCoord == s.getYCoord();
244 }
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000245
bsalomon@google.comd4726202012-08-03 14:34:46 +0000246protected:
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000247
bsalomon@google.comd4726202012-08-03 14:34:46 +0000248 /** Populates a pair of arrays with colors and stop info to construct a random gradient.
249 The function decides whether stop values should be used or not. The return value indicates
250 the number of colors, which will be capped by kMaxRandomGradientColors. colors should be
251 sized to be at least kMaxRandomGradientColors. stops is a pointer to an array of at least
252 size kMaxRandomGradientColors. It may be updated to NULL, indicating that NULL should be
253 passed to the gradient factory rather than the array.
254 */
255 static const int kMaxRandomGradientColors = 4;
256 static int RandomGradientParams(SkRandom* r,
257 SkColor colors[kMaxRandomGradientColors],
258 SkScalar** stops,
259 SkShader::TileMode* tm);
260
261private:
bsalomon@google.com6d003d12012-09-11 15:45:20 +0000262 GrTextureAccess fTextureAccess;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000263 GrScalar fYCoord;
264 GrTextureStripAtlas* fAtlas;
265 int fRow;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000266
bsalomon@google.coma469c282012-10-24 18:28:34 +0000267 typedef GrEffect INHERITED;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000268
269};
270
271///////////////////////////////////////////////////////////////////////////////
272
bsalomon@google.com8ea78d82012-10-24 20:11:30 +0000273// Base class for GL gradient effects
bsalomon@google.com374e7592012-10-23 17:30:45 +0000274class GrGLGradientStage : public GrGLLegacyProgramStage {
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000275public:
276
277 GrGLGradientStage(const GrProgramStageFactory& factory);
278 virtual ~GrGLGradientStage();
279
rileya@google.comb3e50f22012-08-20 17:43:08 +0000280 virtual void setupVariables(GrGLShaderBuilder* builder) SK_OVERRIDE;
bsalomon@google.coma469c282012-10-24 18:28:34 +0000281 virtual void setData(const GrGLUniformManager&, const GrEffect&) SK_OVERRIDE;
rileya@google.comb3e50f22012-08-20 17:43:08 +0000282
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000283 // emit code that gets a fragment's color from an expression for t; for now
284 // this always uses the texture, but for simpler cases we'll be able to lerp
bsalomon@google.com868a8e72012-08-30 19:11:34 +0000285 void emitColorLookup(GrGLShaderBuilder* builder,
286 const char* gradientTValue,
287 const char* outputColor,
288 const char* inputColor,
bsalomon@google.comf06df1b2012-09-06 20:22:31 +0000289 const GrGLShaderBuilder::TextureSampler&);
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000290
291private:
292
rileya@google.comb3e50f22012-08-20 17:43:08 +0000293 GrScalar fCachedYCoord;
294 GrGLUniformManager::UniformHandle fFSYUni;
295
bsalomon@google.com374e7592012-10-23 17:30:45 +0000296 typedef GrGLLegacyProgramStage INHERITED;
rileya@google.com1c6d64b2012-07-27 15:49:05 +0000297};
298
299#endif
300
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000301#endif
302