2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef SkGradientShaderPriv_DEFINED
9 #define SkGradientShaderPriv_DEFINED
11 #include "SkGradientBitmapCache.h"
12 #include "SkGradientShader.h"
13 #include "SkClampRange.h"
14 #include "SkColorPriv.h"
15 #include "SkReadBuffer.h"
16 #include "SkWriteBuffer.h"
17 #include "SkMallocPixelRef.h"
19 #include "SkTemplates.h"
23 static inline void sk_memset32_dither(uint32_t dst[], uint32_t v0, uint32_t v1,
27 sk_memset32(dst, v0, count);
29 int pairs = count >> 1;
30 for (int i = 0; i < pairs; i++) {
43 static inline SkFixed clamp_tileproc(SkFixed x) {
44 return SkClampMax(x, 0xFFFF);
49 static inline SkFixed repeat_tileproc(SkFixed x) {
55 // Visual Studio 2010 (MSC_VER=1600) optimizes bit-shift code incorrectly.
56 // See http://code.google.com/p/skia/issues/detail?id=472
57 #if defined(_MSC_VER) && (_MSC_VER >= 1600)
58 #pragma optimize("", off)
61 static inline SkFixed mirror_tileproc(SkFixed x) {
62 int s = x << 15 >> 31;
63 return (x ^ s) & 0xFFFF;
66 #if defined(_MSC_VER) && (_MSC_VER >= 1600)
67 #pragma optimize("", on)
70 ///////////////////////////////////////////////////////////////////////////////
72 typedef SkFixed (*TileProc)(SkFixed);
74 ///////////////////////////////////////////////////////////////////////////////
76 static const TileProc gTileProcs[] = {
82 ///////////////////////////////////////////////////////////////////////////////
84 class SkGradientShaderBase : public SkShader {
88 sk_bzero(this, sizeof(*this));
89 fTileMode = SkShader::kClamp_TileMode;
92 const SkMatrix* fLocalMatrix;
93 const SkColor* fColors;
96 SkShader::TileMode fTileMode;
99 void flatten(SkWriteBuffer&) const;
102 class DescriptorScope : public Descriptor {
106 bool unflatten(SkReadBuffer&);
108 // fColors and fPos always point into local memory, so they can be safely mutated
110 SkColor* mutableColors() { return const_cast<SkColor*>(fColors); }
111 SkScalar* mutablePos() { return const_cast<SkScalar*>(fPos); }
117 SkColor fColorStorage[kStorageCount];
118 SkScalar fPosStorage[kStorageCount];
119 SkMatrix fLocalMatrixStorage;
120 SkAutoMalloc fDynamicStorage;
124 SkGradientShaderBase(const Descriptor& desc);
125 virtual ~SkGradientShaderBase();
127 // The cache is initialized on-demand when getCache16/32 is called.
128 class GradientShaderCache : public SkRefCnt {
130 GradientShaderCache(U8CPU alpha, const SkGradientShaderBase& shader);
131 ~GradientShaderCache();
133 const uint16_t* getCache16();
134 const SkPMColor* getCache32();
136 SkMallocPixelRef* getCache32PixelRef() const { return fCache32PixelRef; }
138 unsigned getAlpha() const { return fCacheAlpha; }
141 // Working pointers. If either is NULL, we need to recompute the corresponding cache values.
145 uint16_t* fCache16Storage; // Storage for fCache16, allocated on demand.
146 SkMallocPixelRef* fCache32PixelRef;
147 const unsigned fCacheAlpha; // The alpha value we used when we computed the cache.
148 // Larger than 8bits so we can store uninitialized
151 const SkGradientShaderBase& fShader;
153 // Make sure we only initialize the caches once.
154 bool fCache16Inited, fCache32Inited;
155 SkMutex fCache16Mutex, fCache32Mutex;
157 static void initCache16(GradientShaderCache* cache);
158 static void initCache32(GradientShaderCache* cache);
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, uint32_t gradFlags);
165 class GradientShaderBaseContext : public SkShader::Context {
167 GradientShaderBaseContext(const SkGradientShaderBase& shader, const ContextRec&);
169 virtual uint32_t getFlags() const SK_OVERRIDE { return fFlags; }
172 SkMatrix fDstToIndex;
173 SkMatrix::MapXYProc fDstToIndexProc;
174 uint8_t fDstToIndexClass;
177 SkAutoTUnref<GradientShaderCache> fCache;
180 typedef SkShader::Context INHERITED;
183 virtual bool isOpaque() const SK_OVERRIDE;
185 void getGradientTableBitmap(SkBitmap*) const;
188 /// Seems like enough for visual accuracy. TODO: if pos[] deserves
189 /// it, use a larger cache.
191 kCache16Count = (1 << kCache16Bits),
192 kCache16Shift = 16 - kCache16Bits,
193 kSqrt16Shift = 8 - kCache16Bits,
195 /// Seems like enough for visual accuracy. TODO: if pos[] deserves
196 /// it, use a larger cache.
198 kCache32Count = (1 << kCache32Bits),
199 kCache32Shift = 16 - kCache32Bits,
200 kSqrt32Shift = 8 - kCache32Bits,
202 /// This value is used to *read* the dither cache; it may be 0
203 /// if dithering is disabled.
204 kDitherStride32 = kCache32Count,
205 kDitherStride16 = kCache16Count,
210 kThree_GpuColorType, // Symmetric three color
211 kTexture_GpuColorType
214 // Determines and returns the gradient is a two color gradient, symmetric three color gradient
215 // or other (texture gradient). If it is two or symmetric three color, the colors array will
216 // also be filled with the gradient colors
217 GpuColorType getGpuColorType(SkColor colors[3]) const;
219 uint32_t getGradFlags() const { return fGradFlags; }
222 SkGradientShaderBase(SkReadBuffer& );
223 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
224 SK_TO_STRING_OVERRIDE()
226 SkMatrix fPtsToUnit; // set by subclass
233 SkFixed fPos; // 0...1
234 uint32_t fScale; // (1 << 24) / range
238 void commonAsAGradient(GradientInfo*, bool flipGrad = false) const;
240 virtual bool onAsLuminanceColor(SkColor*) const SK_OVERRIDE;
243 * Takes in pointers to gradient color and Rec info as colorSrc and recSrc respectively.
244 * Count is the number of colors in the gradient
245 * It will then flip all the color and rec information and return in their respective Dst
246 * pointers. It is assumed that space has already been allocated for the Dst pointers.
247 * The rec src and dst are only assumed to be valid if count > 2
249 static void FlipGradientColors(SkColor* colorDst, Rec* recDst,
250 SkColor* colorSrc, Rec* recSrc,
253 // V23_COMPATIBILITY_CODE
254 // Used for 2-pt conical gradients since we sort start/end cirlces by radius
255 // Assumes space has already been allocated for fOrigColors
256 void flipGradientColors();
260 kColorStorageCount = 4, // more than this many colors, and we'll use sk_malloc for the space
262 kStorageSize = kColorStorageCount * (sizeof(SkColor) + sizeof(SkScalar) + sizeof(Rec))
264 SkColor fStorage[(kStorageSize + 3) >> 2];
265 SkColor* fOrigColors; // original colors, before modulation by paint in context.
266 SkScalar* fOrigPos; // original positions
267 bool fColorsAreOpaque;
269 GradientShaderCache* refCache(U8CPU alpha) const;
270 mutable SkMutex fCacheMutex;
271 mutable SkAutoTUnref<GradientShaderCache> fCache;
275 typedef SkShader INHERITED;
278 static inline int init_dither_toggle(int x, int y) {
281 return (x | y) * SkGradientShaderBase::kDitherStride32;
284 static inline int next_dither_toggle(int toggle) {
285 return toggle ^ SkGradientShaderBase::kDitherStride32;
288 static inline int init_dither_toggle16(int x, int y) {
289 return ((x ^ y) & 1) * SkGradientShaderBase::kDitherStride16;
292 static inline int next_dither_toggle16(int toggle) {
293 return toggle ^ SkGradientShaderBase::kDitherStride16;
296 ///////////////////////////////////////////////////////////////////////////////
300 #include "GrCoordTransform.h"
301 #include "GrFragmentProcessor.h"
302 #include "gl/GrGLProcessor.h"
304 class GrFragmentStage;
305 class GrBackendProcessorFactory;
308 * The interpretation of the texture matrix depends on the sample mode. The
309 * texture matrix is applied both when the texture coordinates are explicit
310 * and when vertex positions are used as texture coordinates. In the latter
311 * case the texture matrix is applied to the pre-view-matrix position
315 * The post-matrix texture coordinates are in normalize space with (0,0) at
316 * the top-left and (1,1) at the bottom right.
318 * The matrix specifies the radial gradient parameters.
319 * (0,0) in the post-matrix space is center of the radial gradient.
321 * Matrix transforms to space where first circle is centered at the
322 * origin. The second circle will be centered (x, 0) where x may be
323 * 0 and is provided by setRadial2Params. The post-matrix space is
324 * normalized such that 1 is the second radius - first radius.
326 * The angle from the origin of texture coordinates in post-matrix space
327 * determines the gradient value.
330 class GrTextureStripAtlas;
332 // Base class for Gr gradient effects
333 class GrGradientEffect : public GrFragmentProcessor {
336 GrGradientEffect(GrContext* ctx,
337 const SkGradientShaderBase& shader,
338 const SkMatrix& matrix,
339 SkShader::TileMode tileMode);
341 virtual ~GrGradientEffect();
343 bool useAtlas() const { return SkToBool(-1 != fRow); }
344 SkScalar getYCoord() const { return fYCoord; };
346 SkGradientShaderBase::GpuColorType getColorType() const { return fColorType; }
349 kBeforeInterp_PremulType,
350 kAfterInterp_PremulType,
353 PremulType getPremulType() const { return fPremulType; }
355 const SkColor* getColors(int pos) const {
356 SkASSERT(fColorType != SkGradientShaderBase::kTexture_GpuColorType);
357 SkASSERT((pos-1) <= fColorType);
358 return &fColors[pos];
363 /** Populates a pair of arrays with colors and stop info to construct a random gradient.
364 The function decides whether stop values should be used or not. The return value indicates
365 the number of colors, which will be capped by kMaxRandomGradientColors. colors should be
366 sized to be at least kMaxRandomGradientColors. stops is a pointer to an array of at least
367 size kMaxRandomGradientColors. It may be updated to NULL, indicating that NULL should be
368 passed to the gradient factory rather than the array.
370 static const int kMaxRandomGradientColors = 4;
371 static int RandomGradientParams(SkRandom* r,
372 SkColor colors[kMaxRandomGradientColors],
374 SkShader::TileMode* tm);
376 virtual bool onIsEqual(const GrFragmentProcessor&) const SK_OVERRIDE;
378 virtual void onComputeInvariantOutput(InvariantOutput* inout) const SK_OVERRIDE;
380 const GrCoordTransform& getCoordTransform() const { return fCoordTransform; }
383 static const GrCoordSet kCoordSet = kLocal_GrCoordSet;
385 GrCoordTransform fCoordTransform;
386 GrTextureAccess fTextureAccess;
388 GrTextureStripAtlas* fAtlas;
391 SkGradientShaderBase::GpuColorType fColorType;
392 SkColor fColors[3]; // More than 3 colors we use texture
393 PremulType fPremulType; // This only changes behavior for two and three color special cases.
394 // It is already baked into to the table for texture gradients.
395 typedef GrFragmentProcessor INHERITED;
399 ///////////////////////////////////////////////////////////////////////////////
401 // Base class for GL gradient effects
402 class GrGLGradientEffect : public GrGLFragmentProcessor {
404 GrGLGradientEffect(const GrBackendProcessorFactory& factory);
405 virtual ~GrGLGradientEffect();
407 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE;
411 * Subclasses must call this. It will return a key for the part of the shader code controlled
412 * by the base class. The subclasses must stick it in their key and then pass it to the below
413 * emit* functions from their emitCode function.
415 static uint32_t GenBaseGradientKey(const GrProcessor&);
417 // Emits the uniform used as the y-coord to texture samples in derived classes. Subclasses
418 // should call this method from their emitCode().
419 void emitUniforms(GrGLFPBuilder* builder, uint32_t baseKey);
422 // emit code that gets a fragment's color from an expression for t; Has branches for 3 separate
423 // control flows inside -- 2 color gradients, 3 color symmetric gradients (both using
424 // native GLSL mix), and 4+ color gradients that use the traditional texture lookup.
425 void emitColor(GrGLFPBuilder* builder,
426 const char* gradientTValue,
428 const char* outputColor,
429 const char* inputColor,
430 const TextureSamplerArray& samplers);
434 kPremulTypeKeyBitCnt = 1,
436 kPremulBeforeInterpKey = kPremulTypeMask,
438 kTwoColorKey = 2 << kPremulTypeKeyBitCnt,
439 kThreeColorKey = 3 << kPremulTypeKeyBitCnt,
440 kColorKeyMask = kTwoColorKey | kThreeColorKey,
443 // Subclasses must shift any key bits they produce up by this amount
444 // and combine with the result of GenBaseGradientKey.
445 kBaseKeyBitCnt = (kPremulTypeKeyBitCnt + kColorKeyBitCnt)
447 GR_STATIC_ASSERT(kBaseKeyBitCnt <= 32);
449 static SkGradientShaderBase::GpuColorType ColorTypeFromKey(uint32_t baseKey){
450 if (kTwoColorKey == (baseKey & kColorKeyMask)) {
451 return SkGradientShaderBase::kTwo_GpuColorType;
452 } else if (kThreeColorKey == (baseKey & kColorKeyMask)) {
453 return SkGradientShaderBase::kThree_GpuColorType;
454 } else {return SkGradientShaderBase::kTexture_GpuColorType;}
457 static GrGradientEffect::PremulType PremulTypeFromKey(uint32_t baseKey){
458 if (kPremulBeforeInterpKey == (baseKey & kPremulTypeMask)) {
459 return GrGradientEffect::kBeforeInterp_PremulType;
461 return GrGradientEffect::kAfterInterp_PremulType;
465 SkScalar fCachedYCoord;
466 GrGLProgramDataManager::UniformHandle fFSYUni;
467 GrGLProgramDataManager::UniformHandle fColorStartUni;
468 GrGLProgramDataManager::UniformHandle fColorMidUni;
469 GrGLProgramDataManager::UniformHandle fColorEndUni;
471 typedef GrGLFragmentProcessor INHERITED;