blob: 93b8c8358f77e26e6ffe2b7862554b6126f4af97 [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"
15#include "SkMallocPixelRef.h"
16#include "SkUnitMapper.h"
17#include "SkUtils.h"
18#include "SkTemplates.h"
19#include "SkBitmapCache.h"
20#include "SkShader.h"
21#include "GrSamplerState.h"
22#include "SkGr.h"
23#include "gl/GrGLProgramStage.h"
24
25#ifndef SK_DISABLE_DITHER_32BIT_GRADIENT
26 #define USE_DITHER_32BIT_GRADIENT
27#endif
28
29static void sk_memset32_dither(uint32_t dst[], uint32_t v0, uint32_t v1,
30 int count) {
31 if (count > 0) {
32 if (v0 == v1) {
33 sk_memset32(dst, v0, count);
34 } else {
35 int pairs = count >> 1;
36 for (int i = 0; i < pairs; i++) {
37 *dst++ = v0;
38 *dst++ = v1;
39 }
40 if (count & 1) {
41 *dst = v0;
42 }
43 }
44 }
45}
46
47// Clamp
48
49static SkFixed clamp_tileproc(SkFixed x) {
50 return SkClampMax(x, 0xFFFF);
51}
52
53// Repeat
54
55static SkFixed repeat_tileproc(SkFixed x) {
56 return x & 0xFFFF;
57}
58
59// Mirror
60
61// Visual Studio 2010 (MSC_VER=1600) optimizes bit-shift code incorrectly.
62// See http://code.google.com/p/skia/issues/detail?id=472
63#if defined(_MSC_VER) && (_MSC_VER >= 1600)
64#pragma optimize("", off)
65#endif
66
67static inline SkFixed mirror_tileproc(SkFixed x) {
68 int s = x << 15 >> 31;
69 return (x ^ s) & 0xFFFF;
70}
71
72#if defined(_MSC_VER) && (_MSC_VER >= 1600)
73#pragma optimize("", on)
74#endif
75
76///////////////////////////////////////////////////////////////////////////////
77
78typedef SkFixed (*TileProc)(SkFixed);
79
80///////////////////////////////////////////////////////////////////////////////
81
82static const TileProc gTileProcs[] = {
83 clamp_tileproc,
84 repeat_tileproc,
85 mirror_tileproc
86};
87
88///////////////////////////////////////////////////////////////////////////////
89
90class SkGradientShaderBase : public SkShader {
91public:
92 SkGradientShaderBase(const SkColor colors[], const SkScalar pos[],
93 int colorCount, SkShader::TileMode mode, SkUnitMapper* mapper);
94 virtual ~SkGradientShaderBase();
95
96 // overrides
97 virtual bool setContext(const SkBitmap&, const SkPaint&, const SkMatrix&) SK_OVERRIDE;
98 virtual uint32_t getFlags() SK_OVERRIDE { return fFlags; }
99 virtual bool isOpaque() const SK_OVERRIDE;
100
101 void getGradientTableBitmap(SkBitmap*) const;
102
103 enum {
104 /// Seems like enough for visual accuracy. TODO: if pos[] deserves
105 /// it, use a larger cache.
106 kCache16Bits = 8,
107 kGradient16Length = (1 << kCache16Bits),
108 /// Each cache gets 1 extra entry at the end so we don't have to
109 /// test for end-of-cache in lerps. This is also the value used
110 /// to stride *writes* into the dither cache; it must not be zero.
111 /// Total space for a cache is 2x kCache16Count entries: one
112 /// regular cache, one for dithering.
113 kCache16Count = kGradient16Length + 1,
114 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,
120 kGradient32Length = (1 << kCache32Bits),
121 /// Each cache gets 1 extra entry at the end so we don't have to
122 /// test for end-of-cache in lerps. This is also the value used
123 /// to stride *writes* into the dither cache; it must not be zero.
124 /// Total space for a cache is 2x kCache32Count entries: one
125 /// regular cache, one for dithering.
126 kCache32Count = kGradient32Length + 1,
127 kCache32Shift = 16 - kCache32Bits,
128 kSqrt32Shift = 8 - kCache32Bits,
129
130 /// This value is used to *read* the dither cache; it may be 0
131 /// if dithering is disabled.
132#ifdef USE_DITHER_32BIT_GRADIENT
133 kDitherStride32 = kCache32Count,
134#else
135 kDitherStride32 = 0,
136#endif
137 kDitherStride16 = kCache16Count,
138 kLerpRemainderMask32 = (1 << (16 - kCache32Bits)) - 1
139 };
140
141
142protected:
143 SkGradientShaderBase(SkFlattenableReadBuffer& );
144 virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
145
146 SkUnitMapper* fMapper;
147 SkMatrix fPtsToUnit; // set by subclass
148 SkMatrix fDstToIndex;
149 SkMatrix::MapXYProc fDstToIndexProc;
150 TileMode fTileMode;
151 TileProc fTileProc;
152 int fColorCount;
153 uint8_t fDstToIndexClass;
154 uint8_t fFlags;
155
156 struct Rec {
157 SkFixed fPos; // 0...1
158 uint32_t fScale; // (1 << 24) / range
159 };
160 Rec* fRecs;
161
162 const uint16_t* getCache16() const;
163 const SkPMColor* getCache32() const;
164
165 void commonAsAGradient(GradientInfo*) const;
166
167private:
168 enum {
169 kColorStorageCount = 4, // more than this many colors, and we'll use sk_malloc for the space
170
171 kStorageSize = kColorStorageCount * (sizeof(SkColor) + sizeof(Rec))
172 };
173 SkColor fStorage[(kStorageSize + 3) >> 2];
174 SkColor* fOrigColors; // original colors, before modulation by paint in setContext
175 bool fColorsAreOpaque;
176
177 mutable uint16_t* fCache16; // working ptr. If this is NULL, we need to recompute the cache values
178 mutable SkPMColor* fCache32; // working ptr. If this is NULL, we need to recompute the cache values
179
180 mutable uint16_t* fCache16Storage; // storage for fCache16, allocated on demand
181 mutable SkMallocPixelRef* fCache32PixelRef;
182 mutable unsigned fCacheAlpha; // the alpha value we used when we computed the cache. larger than 8bits so we can store uninitialized value
183
184 static void Build16bitCache(uint16_t[], SkColor c0, SkColor c1, int count);
185 static void Build32bitCache(SkPMColor[], SkColor c0, SkColor c1, int count,
186 U8CPU alpha);
187 void setCacheAlpha(U8CPU alpha) const;
188 void initCommon();
189
190 typedef SkShader INHERITED;
191};
192
193///////////////////////////////////////////////////////////////////////////////
194
195class GrSamplerState;
196class GrProgramStageFactory;
197
198/*
199 * The intepretation of the texture matrix depends on the sample mode. The
200 * texture matrix is applied both when the texture coordinates are explicit
201 * and when vertex positions are used as texture coordinates. In the latter
202 * case the texture matrix is applied to the pre-view-matrix position
203 * values.
204 *
205 * Normal SampleMode
206 * The post-matrix texture coordinates are in normalize space with (0,0) at
207 * the top-left and (1,1) at the bottom right.
208 * RadialGradient
209 * The matrix specifies the radial gradient parameters.
210 * (0,0) in the post-matrix space is center of the radial gradient.
211 * Radial2Gradient
212 * Matrix transforms to space where first circle is centered at the
213 * origin. The second circle will be centered (x, 0) where x may be
214 * 0 and is provided by setRadial2Params. The post-matrix space is
215 * normalized such that 1 is the second radius - first radius.
216 * SweepGradient
217 * The angle from the origin of texture coordinates in post-matrix space
218 * determines the gradient value.
219 */
220
221// Base class for Gr gradient effects
222class GrGradientEffect : public GrCustomStage {
223public:
224
225 // FIXME: This constructor is only used in GrGpuGL_unittest.cpp, and should
226 // be removed once an alternative testing setup has been devised.
227 GrGradientEffect(GrTexture* texture);
228 GrGradientEffect(GrContext* ctx, const SkGradientShaderBase& shader,
229 GrSamplerState* sampler);
230
231 virtual ~GrGradientEffect();
232
233 unsigned int numTextures() const;
234 GrTexture* texture(unsigned int index) const;
235
236 bool useTexture() const { return fUseTexture; }
237
238private:
239
240 GrTexture* fTexture;
241 bool fUseTexture;
242
243 typedef GrCustomStage INHERITED;
244
245};
246
247///////////////////////////////////////////////////////////////////////////////
248
249// Base class for GL gradient custom stages
250class GrGLGradientStage : public GrGLProgramStage {
251public:
252
253 GrGLGradientStage(const GrProgramStageFactory& factory);
254 virtual ~GrGLGradientStage();
255
256 // emit code that gets a fragment's color from an expression for t; for now
257 // this always uses the texture, but for simpler cases we'll be able to lerp
258 void emitColorLookup(GrGLShaderBuilder* builder, const char* t,
259 const char* outputColor, const char* samplerName);
260
261private:
262
263 typedef GrGLProgramStage INHERITED;
264};
265
266#endif
267