michael@0: /* michael@0: * Copyright 2012 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #ifndef SkGradientShaderPriv_DEFINED michael@0: #define SkGradientShaderPriv_DEFINED michael@0: michael@0: #include "SkGradientShader.h" michael@0: #include "SkClampRange.h" michael@0: #include "SkColorPriv.h" michael@0: #include "SkReadBuffer.h" michael@0: #include "SkWriteBuffer.h" michael@0: #include "SkMallocPixelRef.h" michael@0: #include "SkUnitMapper.h" michael@0: #include "SkUtils.h" michael@0: #include "SkTemplates.h" michael@0: #include "SkBitmapCache.h" michael@0: #include "SkShader.h" michael@0: michael@0: static inline void sk_memset32_dither(uint32_t dst[], uint32_t v0, uint32_t v1, michael@0: int count) { michael@0: if (count > 0) { michael@0: if (v0 == v1) { michael@0: sk_memset32(dst, v0, count); michael@0: } else { michael@0: int pairs = count >> 1; michael@0: for (int i = 0; i < pairs; i++) { michael@0: *dst++ = v0; michael@0: *dst++ = v1; michael@0: } michael@0: if (count & 1) { michael@0: *dst = v0; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Clamp michael@0: michael@0: static inline SkFixed clamp_tileproc(SkFixed x) { michael@0: return SkClampMax(x, 0xFFFF); michael@0: } michael@0: michael@0: // Repeat michael@0: michael@0: static inline SkFixed repeat_tileproc(SkFixed x) { michael@0: return x & 0xFFFF; michael@0: } michael@0: michael@0: // Mirror michael@0: michael@0: // Visual Studio 2010 (MSC_VER=1600) optimizes bit-shift code incorrectly. michael@0: // See http://code.google.com/p/skia/issues/detail?id=472 michael@0: #if defined(_MSC_VER) && (_MSC_VER >= 1600) michael@0: #pragma optimize("", off) michael@0: #endif michael@0: michael@0: static inline SkFixed mirror_tileproc(SkFixed x) { michael@0: int s = x << 15 >> 31; michael@0: return (x ^ s) & 0xFFFF; michael@0: } michael@0: michael@0: #if defined(_MSC_VER) && (_MSC_VER >= 1600) michael@0: #pragma optimize("", on) michael@0: #endif michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: typedef SkFixed (*TileProc)(SkFixed); michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: static const TileProc gTileProcs[] = { michael@0: clamp_tileproc, michael@0: repeat_tileproc, michael@0: mirror_tileproc michael@0: }; michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: class SkGradientShaderBase : public SkShader { michael@0: public: michael@0: struct Descriptor { michael@0: Descriptor() { michael@0: sk_bzero(this, sizeof(*this)); michael@0: fTileMode = SkShader::kClamp_TileMode; michael@0: } michael@0: michael@0: const SkColor* fColors; michael@0: const SkScalar* fPos; michael@0: int fCount; michael@0: SkShader::TileMode fTileMode; michael@0: SkUnitMapper* fMapper; michael@0: uint32_t fFlags; michael@0: }; michael@0: michael@0: public: michael@0: SkGradientShaderBase(const Descriptor& desc); michael@0: virtual ~SkGradientShaderBase(); michael@0: michael@0: virtual bool setContext(const SkBitmap&, const SkPaint&, const SkMatrix&) SK_OVERRIDE; michael@0: virtual uint32_t getFlags() SK_OVERRIDE { return fFlags; } michael@0: virtual bool isOpaque() const SK_OVERRIDE; michael@0: michael@0: void getGradientTableBitmap(SkBitmap*) const; michael@0: michael@0: enum { michael@0: /// Seems like enough for visual accuracy. TODO: if pos[] deserves michael@0: /// it, use a larger cache. michael@0: kCache16Bits = 8, michael@0: kCache16Count = (1 << kCache16Bits), michael@0: kCache16Shift = 16 - kCache16Bits, michael@0: kSqrt16Shift = 8 - kCache16Bits, michael@0: michael@0: /// Seems like enough for visual accuracy. TODO: if pos[] deserves michael@0: /// it, use a larger cache. michael@0: kCache32Bits = 8, michael@0: kCache32Count = (1 << kCache32Bits), michael@0: kCache32Shift = 16 - kCache32Bits, michael@0: kSqrt32Shift = 8 - kCache32Bits, michael@0: michael@0: /// This value is used to *read* the dither cache; it may be 0 michael@0: /// if dithering is disabled. michael@0: kDitherStride32 = kCache32Count, michael@0: kDitherStride16 = kCache16Count, michael@0: }; michael@0: michael@0: michael@0: protected: michael@0: SkGradientShaderBase(SkReadBuffer& ); michael@0: virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE; michael@0: SK_TO_STRING_OVERRIDE() michael@0: michael@0: SkUnitMapper* fMapper; michael@0: SkMatrix fPtsToUnit; // set by subclass michael@0: SkMatrix fDstToIndex; michael@0: SkMatrix::MapXYProc fDstToIndexProc; michael@0: TileMode fTileMode; michael@0: TileProc fTileProc; michael@0: int fColorCount; michael@0: uint8_t fDstToIndexClass; michael@0: uint8_t fFlags; michael@0: uint8_t fGradFlags; michael@0: michael@0: struct Rec { michael@0: SkFixed fPos; // 0...1 michael@0: uint32_t fScale; // (1 << 24) / range michael@0: }; michael@0: Rec* fRecs; michael@0: michael@0: const uint16_t* getCache16() const; michael@0: const SkPMColor* getCache32() const; michael@0: michael@0: void commonAsAGradient(GradientInfo*) const; michael@0: michael@0: private: michael@0: enum { michael@0: kColorStorageCount = 4, // more than this many colors, and we'll use sk_malloc for the space michael@0: michael@0: kStorageSize = kColorStorageCount * (sizeof(SkColor) + sizeof(Rec)) michael@0: }; michael@0: SkColor fStorage[(kStorageSize + 3) >> 2]; michael@0: SkColor* fOrigColors; // original colors, before modulation by paint in setContext michael@0: bool fColorsAreOpaque; michael@0: michael@0: mutable uint16_t* fCache16; // working ptr. If this is NULL, we need to recompute the cache values michael@0: mutable SkPMColor* fCache32; // working ptr. If this is NULL, we need to recompute the cache values michael@0: michael@0: mutable uint16_t* fCache16Storage; // storage for fCache16, allocated on demand michael@0: mutable SkMallocPixelRef* fCache32PixelRef; michael@0: mutable unsigned fCacheAlpha; // the alpha value we used when we computed the cache. larger than 8bits so we can store uninitialized value michael@0: michael@0: static void Build16bitCache(uint16_t[], SkColor c0, SkColor c1, int count); michael@0: static void Build32bitCache(SkPMColor[], SkColor c0, SkColor c1, int count, michael@0: U8CPU alpha, uint32_t gradFlags); michael@0: void setCacheAlpha(U8CPU alpha) const; michael@0: void initCommon(); michael@0: michael@0: typedef SkShader INHERITED; michael@0: }; michael@0: michael@0: static inline int init_dither_toggle(int x, int y) { michael@0: x &= 1; michael@0: y = (y & 1) << 1; michael@0: return (x | y) * SkGradientShaderBase::kDitherStride32; michael@0: } michael@0: michael@0: static inline int next_dither_toggle(int toggle) { michael@0: return toggle ^ SkGradientShaderBase::kDitherStride32; michael@0: } michael@0: michael@0: static inline int init_dither_toggle16(int x, int y) { michael@0: return ((x ^ y) & 1) * SkGradientShaderBase::kDitherStride16; michael@0: } michael@0: michael@0: static inline int next_dither_toggle16(int toggle) { michael@0: return toggle ^ SkGradientShaderBase::kDitherStride16; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #if SK_SUPPORT_GPU michael@0: michael@0: #include "GrCoordTransform.h" michael@0: #include "gl/GrGLEffect.h" michael@0: michael@0: class GrEffectStage; michael@0: class GrBackendEffectFactory; michael@0: michael@0: /* michael@0: * The interpretation of the texture matrix depends on the sample mode. The michael@0: * texture matrix is applied both when the texture coordinates are explicit michael@0: * and when vertex positions are used as texture coordinates. In the latter michael@0: * case the texture matrix is applied to the pre-view-matrix position michael@0: * values. michael@0: * michael@0: * Normal SampleMode michael@0: * The post-matrix texture coordinates are in normalize space with (0,0) at michael@0: * the top-left and (1,1) at the bottom right. michael@0: * RadialGradient michael@0: * The matrix specifies the radial gradient parameters. michael@0: * (0,0) in the post-matrix space is center of the radial gradient. michael@0: * Radial2Gradient michael@0: * Matrix transforms to space where first circle is centered at the michael@0: * origin. The second circle will be centered (x, 0) where x may be michael@0: * 0 and is provided by setRadial2Params. The post-matrix space is michael@0: * normalized such that 1 is the second radius - first radius. michael@0: * SweepGradient michael@0: * The angle from the origin of texture coordinates in post-matrix space michael@0: * determines the gradient value. michael@0: */ michael@0: michael@0: class GrTextureStripAtlas; michael@0: michael@0: // Base class for Gr gradient effects michael@0: class GrGradientEffect : public GrEffect { michael@0: public: michael@0: michael@0: GrGradientEffect(GrContext* ctx, michael@0: const SkGradientShaderBase& shader, michael@0: const SkMatrix& matrix, michael@0: SkShader::TileMode tileMode); michael@0: michael@0: virtual ~GrGradientEffect(); michael@0: michael@0: bool useAtlas() const { return SkToBool(-1 != fRow); } michael@0: SkScalar getYCoord() const { return fYCoord; }; michael@0: michael@0: virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE; michael@0: michael@0: enum ColorType { michael@0: kTwo_ColorType, michael@0: kThree_ColorType, michael@0: kTexture_ColorType michael@0: }; michael@0: michael@0: ColorType getColorType() const { return fColorType; } michael@0: michael@0: enum PremulType { michael@0: kBeforeInterp_PremulType, michael@0: kAfterInterp_PremulType, michael@0: }; michael@0: michael@0: PremulType getPremulType() const { return fPremulType; } michael@0: michael@0: const SkColor* getColors(int pos) const { michael@0: SkASSERT(fColorType != kTexture_ColorType); michael@0: SkASSERT((pos-1) <= fColorType); michael@0: return &fColors[pos]; michael@0: } michael@0: michael@0: protected: michael@0: michael@0: /** Populates a pair of arrays with colors and stop info to construct a random gradient. michael@0: The function decides whether stop values should be used or not. The return value indicates michael@0: the number of colors, which will be capped by kMaxRandomGradientColors. colors should be michael@0: sized to be at least kMaxRandomGradientColors. stops is a pointer to an array of at least michael@0: size kMaxRandomGradientColors. It may be updated to NULL, indicating that NULL should be michael@0: passed to the gradient factory rather than the array. michael@0: */ michael@0: static const int kMaxRandomGradientColors = 4; michael@0: static int RandomGradientParams(SkRandom* r, michael@0: SkColor colors[kMaxRandomGradientColors], michael@0: SkScalar** stops, michael@0: SkShader::TileMode* tm); michael@0: michael@0: virtual bool onIsEqual(const GrEffect& effect) const SK_OVERRIDE; michael@0: michael@0: const GrCoordTransform& getCoordTransform() const { return fCoordTransform; } michael@0: michael@0: private: michael@0: static const GrCoordSet kCoordSet = kLocal_GrCoordSet; michael@0: michael@0: enum { michael@0: kMaxAnalyticColors = 3 // if more colors use texture michael@0: }; michael@0: michael@0: GrCoordTransform fCoordTransform; michael@0: GrTextureAccess fTextureAccess; michael@0: SkScalar fYCoord; michael@0: GrTextureStripAtlas* fAtlas; michael@0: int fRow; michael@0: bool fIsOpaque; michael@0: ColorType fColorType; michael@0: SkColor fColors[kMaxAnalyticColors]; michael@0: PremulType fPremulType; // This only changes behavior for two and three color special cases. michael@0: // It is already baked into to the table for texture gradients. michael@0: typedef GrEffect INHERITED; michael@0: michael@0: }; michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: // Base class for GL gradient effects michael@0: class GrGLGradientEffect : public GrGLEffect { michael@0: public: michael@0: GrGLGradientEffect(const GrBackendEffectFactory& factory); michael@0: virtual ~GrGLGradientEffect(); michael@0: michael@0: virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE; michael@0: michael@0: protected: michael@0: enum { michael@0: kPremulTypeKeyBitCnt = 1, michael@0: kPremulTypeMask = 1, michael@0: kPremulBeforeInterpKey = kPremulTypeMask, michael@0: michael@0: kTwoColorKey = 2 << kPremulTypeKeyBitCnt, michael@0: kThreeColorKey = 3 << kPremulTypeKeyBitCnt, michael@0: kColorKeyMask = kTwoColorKey | kThreeColorKey, michael@0: kColorKeyBitCnt = 2, michael@0: michael@0: // Subclasses must shift any key bits they produce up by this amount michael@0: // and combine with the result of GenBaseGradientKey. michael@0: kBaseKeyBitCnt = (kPremulTypeKeyBitCnt + kColorKeyBitCnt) michael@0: }; michael@0: michael@0: static GrGradientEffect::ColorType ColorTypeFromKey(EffectKey key){ michael@0: if (kTwoColorKey == (key & kColorKeyMask)) { michael@0: return GrGradientEffect::kTwo_ColorType; michael@0: } else if (kThreeColorKey == (key & kColorKeyMask)) { michael@0: return GrGradientEffect::kThree_ColorType; michael@0: } else {return GrGradientEffect::kTexture_ColorType;} michael@0: } michael@0: michael@0: static GrGradientEffect::PremulType PremulTypeFromKey(EffectKey key){ michael@0: if (kPremulBeforeInterpKey == (key & kPremulTypeMask)) { michael@0: return GrGradientEffect::kBeforeInterp_PremulType; michael@0: } else { michael@0: return GrGradientEffect::kAfterInterp_PremulType; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Subclasses must call this. It will return a value restricted to the lower kBaseKeyBitCnt michael@0: * bits. michael@0: */ michael@0: static EffectKey GenBaseGradientKey(const GrDrawEffect&); michael@0: michael@0: // Emits the uniform used as the y-coord to texture samples in derived classes. Subclasses michael@0: // should call this method from their emitCode(). michael@0: void emitUniforms(GrGLShaderBuilder* builder, EffectKey key); michael@0: michael@0: michael@0: // emit code that gets a fragment's color from an expression for t; Has branches for 3 separate michael@0: // control flows inside -- 2 color gradients, 3 color symmetric gradients (both using michael@0: // native GLSL mix), and 4+ color gradients that use the traditional texture lookup. michael@0: void emitColor(GrGLShaderBuilder* builder, michael@0: const char* gradientTValue, michael@0: EffectKey key, michael@0: const char* outputColor, michael@0: const char* inputColor, michael@0: const TextureSamplerArray& samplers); michael@0: michael@0: private: michael@0: SkScalar fCachedYCoord; michael@0: GrGLUniformManager::UniformHandle fFSYUni; michael@0: GrGLUniformManager::UniformHandle fColorStartUni; michael@0: GrGLUniformManager::UniformHandle fColorMidUni; michael@0: GrGLUniformManager::UniformHandle fColorEndUni; michael@0: michael@0: typedef GrGLEffect INHERITED; michael@0: }; michael@0: michael@0: #endif michael@0: michael@0: #endif