1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/effects/gradients/SkGradientShaderPriv.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,390 @@ 1.4 +/* 1.5 + * Copyright 2012 Google Inc. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 + 1.11 +#ifndef SkGradientShaderPriv_DEFINED 1.12 +#define SkGradientShaderPriv_DEFINED 1.13 + 1.14 +#include "SkGradientShader.h" 1.15 +#include "SkClampRange.h" 1.16 +#include "SkColorPriv.h" 1.17 +#include "SkReadBuffer.h" 1.18 +#include "SkWriteBuffer.h" 1.19 +#include "SkMallocPixelRef.h" 1.20 +#include "SkUnitMapper.h" 1.21 +#include "SkUtils.h" 1.22 +#include "SkTemplates.h" 1.23 +#include "SkBitmapCache.h" 1.24 +#include "SkShader.h" 1.25 + 1.26 +static inline void sk_memset32_dither(uint32_t dst[], uint32_t v0, uint32_t v1, 1.27 + int count) { 1.28 + if (count > 0) { 1.29 + if (v0 == v1) { 1.30 + sk_memset32(dst, v0, count); 1.31 + } else { 1.32 + int pairs = count >> 1; 1.33 + for (int i = 0; i < pairs; i++) { 1.34 + *dst++ = v0; 1.35 + *dst++ = v1; 1.36 + } 1.37 + if (count & 1) { 1.38 + *dst = v0; 1.39 + } 1.40 + } 1.41 + } 1.42 +} 1.43 + 1.44 +// Clamp 1.45 + 1.46 +static inline SkFixed clamp_tileproc(SkFixed x) { 1.47 + return SkClampMax(x, 0xFFFF); 1.48 +} 1.49 + 1.50 +// Repeat 1.51 + 1.52 +static inline SkFixed repeat_tileproc(SkFixed x) { 1.53 + return x & 0xFFFF; 1.54 +} 1.55 + 1.56 +// Mirror 1.57 + 1.58 +// Visual Studio 2010 (MSC_VER=1600) optimizes bit-shift code incorrectly. 1.59 +// See http://code.google.com/p/skia/issues/detail?id=472 1.60 +#if defined(_MSC_VER) && (_MSC_VER >= 1600) 1.61 +#pragma optimize("", off) 1.62 +#endif 1.63 + 1.64 +static inline SkFixed mirror_tileproc(SkFixed x) { 1.65 + int s = x << 15 >> 31; 1.66 + return (x ^ s) & 0xFFFF; 1.67 +} 1.68 + 1.69 +#if defined(_MSC_VER) && (_MSC_VER >= 1600) 1.70 +#pragma optimize("", on) 1.71 +#endif 1.72 + 1.73 +/////////////////////////////////////////////////////////////////////////////// 1.74 + 1.75 +typedef SkFixed (*TileProc)(SkFixed); 1.76 + 1.77 +/////////////////////////////////////////////////////////////////////////////// 1.78 + 1.79 +static const TileProc gTileProcs[] = { 1.80 + clamp_tileproc, 1.81 + repeat_tileproc, 1.82 + mirror_tileproc 1.83 +}; 1.84 + 1.85 +/////////////////////////////////////////////////////////////////////////////// 1.86 + 1.87 +class SkGradientShaderBase : public SkShader { 1.88 +public: 1.89 + struct Descriptor { 1.90 + Descriptor() { 1.91 + sk_bzero(this, sizeof(*this)); 1.92 + fTileMode = SkShader::kClamp_TileMode; 1.93 + } 1.94 + 1.95 + const SkColor* fColors; 1.96 + const SkScalar* fPos; 1.97 + int fCount; 1.98 + SkShader::TileMode fTileMode; 1.99 + SkUnitMapper* fMapper; 1.100 + uint32_t fFlags; 1.101 + }; 1.102 + 1.103 +public: 1.104 + SkGradientShaderBase(const Descriptor& desc); 1.105 + virtual ~SkGradientShaderBase(); 1.106 + 1.107 + virtual bool setContext(const SkBitmap&, const SkPaint&, const SkMatrix&) SK_OVERRIDE; 1.108 + virtual uint32_t getFlags() SK_OVERRIDE { return fFlags; } 1.109 + virtual bool isOpaque() const SK_OVERRIDE; 1.110 + 1.111 + void getGradientTableBitmap(SkBitmap*) const; 1.112 + 1.113 + enum { 1.114 + /// Seems like enough for visual accuracy. TODO: if pos[] deserves 1.115 + /// it, use a larger cache. 1.116 + kCache16Bits = 8, 1.117 + kCache16Count = (1 << kCache16Bits), 1.118 + kCache16Shift = 16 - kCache16Bits, 1.119 + kSqrt16Shift = 8 - kCache16Bits, 1.120 + 1.121 + /// Seems like enough for visual accuracy. TODO: if pos[] deserves 1.122 + /// it, use a larger cache. 1.123 + kCache32Bits = 8, 1.124 + kCache32Count = (1 << kCache32Bits), 1.125 + kCache32Shift = 16 - kCache32Bits, 1.126 + kSqrt32Shift = 8 - kCache32Bits, 1.127 + 1.128 + /// This value is used to *read* the dither cache; it may be 0 1.129 + /// if dithering is disabled. 1.130 + kDitherStride32 = kCache32Count, 1.131 + kDitherStride16 = kCache16Count, 1.132 + }; 1.133 + 1.134 + 1.135 +protected: 1.136 + SkGradientShaderBase(SkReadBuffer& ); 1.137 + virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE; 1.138 + SK_TO_STRING_OVERRIDE() 1.139 + 1.140 + SkUnitMapper* fMapper; 1.141 + SkMatrix fPtsToUnit; // set by subclass 1.142 + SkMatrix fDstToIndex; 1.143 + SkMatrix::MapXYProc fDstToIndexProc; 1.144 + TileMode fTileMode; 1.145 + TileProc fTileProc; 1.146 + int fColorCount; 1.147 + uint8_t fDstToIndexClass; 1.148 + uint8_t fFlags; 1.149 + uint8_t fGradFlags; 1.150 + 1.151 + struct Rec { 1.152 + SkFixed fPos; // 0...1 1.153 + uint32_t fScale; // (1 << 24) / range 1.154 + }; 1.155 + Rec* fRecs; 1.156 + 1.157 + const uint16_t* getCache16() const; 1.158 + const SkPMColor* getCache32() const; 1.159 + 1.160 + void commonAsAGradient(GradientInfo*) const; 1.161 + 1.162 +private: 1.163 + enum { 1.164 + kColorStorageCount = 4, // more than this many colors, and we'll use sk_malloc for the space 1.165 + 1.166 + kStorageSize = kColorStorageCount * (sizeof(SkColor) + sizeof(Rec)) 1.167 + }; 1.168 + SkColor fStorage[(kStorageSize + 3) >> 2]; 1.169 + SkColor* fOrigColors; // original colors, before modulation by paint in setContext 1.170 + bool fColorsAreOpaque; 1.171 + 1.172 + mutable uint16_t* fCache16; // working ptr. If this is NULL, we need to recompute the cache values 1.173 + mutable SkPMColor* fCache32; // working ptr. If this is NULL, we need to recompute the cache values 1.174 + 1.175 + mutable uint16_t* fCache16Storage; // storage for fCache16, allocated on demand 1.176 + mutable SkMallocPixelRef* fCache32PixelRef; 1.177 + mutable unsigned fCacheAlpha; // the alpha value we used when we computed the cache. larger than 8bits so we can store uninitialized value 1.178 + 1.179 + static void Build16bitCache(uint16_t[], SkColor c0, SkColor c1, int count); 1.180 + static void Build32bitCache(SkPMColor[], SkColor c0, SkColor c1, int count, 1.181 + U8CPU alpha, uint32_t gradFlags); 1.182 + void setCacheAlpha(U8CPU alpha) const; 1.183 + void initCommon(); 1.184 + 1.185 + typedef SkShader INHERITED; 1.186 +}; 1.187 + 1.188 +static inline int init_dither_toggle(int x, int y) { 1.189 + x &= 1; 1.190 + y = (y & 1) << 1; 1.191 + return (x | y) * SkGradientShaderBase::kDitherStride32; 1.192 +} 1.193 + 1.194 +static inline int next_dither_toggle(int toggle) { 1.195 + return toggle ^ SkGradientShaderBase::kDitherStride32; 1.196 +} 1.197 + 1.198 +static inline int init_dither_toggle16(int x, int y) { 1.199 + return ((x ^ y) & 1) * SkGradientShaderBase::kDitherStride16; 1.200 +} 1.201 + 1.202 +static inline int next_dither_toggle16(int toggle) { 1.203 + return toggle ^ SkGradientShaderBase::kDitherStride16; 1.204 +} 1.205 + 1.206 +/////////////////////////////////////////////////////////////////////////////// 1.207 + 1.208 +#if SK_SUPPORT_GPU 1.209 + 1.210 +#include "GrCoordTransform.h" 1.211 +#include "gl/GrGLEffect.h" 1.212 + 1.213 +class GrEffectStage; 1.214 +class GrBackendEffectFactory; 1.215 + 1.216 +/* 1.217 + * The interpretation of the texture matrix depends on the sample mode. The 1.218 + * texture matrix is applied both when the texture coordinates are explicit 1.219 + * and when vertex positions are used as texture coordinates. In the latter 1.220 + * case the texture matrix is applied to the pre-view-matrix position 1.221 + * values. 1.222 + * 1.223 + * Normal SampleMode 1.224 + * The post-matrix texture coordinates are in normalize space with (0,0) at 1.225 + * the top-left and (1,1) at the bottom right. 1.226 + * RadialGradient 1.227 + * The matrix specifies the radial gradient parameters. 1.228 + * (0,0) in the post-matrix space is center of the radial gradient. 1.229 + * Radial2Gradient 1.230 + * Matrix transforms to space where first circle is centered at the 1.231 + * origin. The second circle will be centered (x, 0) where x may be 1.232 + * 0 and is provided by setRadial2Params. The post-matrix space is 1.233 + * normalized such that 1 is the second radius - first radius. 1.234 + * SweepGradient 1.235 + * The angle from the origin of texture coordinates in post-matrix space 1.236 + * determines the gradient value. 1.237 + */ 1.238 + 1.239 + class GrTextureStripAtlas; 1.240 + 1.241 +// Base class for Gr gradient effects 1.242 +class GrGradientEffect : public GrEffect { 1.243 +public: 1.244 + 1.245 + GrGradientEffect(GrContext* ctx, 1.246 + const SkGradientShaderBase& shader, 1.247 + const SkMatrix& matrix, 1.248 + SkShader::TileMode tileMode); 1.249 + 1.250 + virtual ~GrGradientEffect(); 1.251 + 1.252 + bool useAtlas() const { return SkToBool(-1 != fRow); } 1.253 + SkScalar getYCoord() const { return fYCoord; }; 1.254 + 1.255 + virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE; 1.256 + 1.257 + enum ColorType { 1.258 + kTwo_ColorType, 1.259 + kThree_ColorType, 1.260 + kTexture_ColorType 1.261 + }; 1.262 + 1.263 + ColorType getColorType() const { return fColorType; } 1.264 + 1.265 + enum PremulType { 1.266 + kBeforeInterp_PremulType, 1.267 + kAfterInterp_PremulType, 1.268 + }; 1.269 + 1.270 + PremulType getPremulType() const { return fPremulType; } 1.271 + 1.272 + const SkColor* getColors(int pos) const { 1.273 + SkASSERT(fColorType != kTexture_ColorType); 1.274 + SkASSERT((pos-1) <= fColorType); 1.275 + return &fColors[pos]; 1.276 + } 1.277 + 1.278 +protected: 1.279 + 1.280 + /** Populates a pair of arrays with colors and stop info to construct a random gradient. 1.281 + The function decides whether stop values should be used or not. The return value indicates 1.282 + the number of colors, which will be capped by kMaxRandomGradientColors. colors should be 1.283 + sized to be at least kMaxRandomGradientColors. stops is a pointer to an array of at least 1.284 + size kMaxRandomGradientColors. It may be updated to NULL, indicating that NULL should be 1.285 + passed to the gradient factory rather than the array. 1.286 + */ 1.287 + static const int kMaxRandomGradientColors = 4; 1.288 + static int RandomGradientParams(SkRandom* r, 1.289 + SkColor colors[kMaxRandomGradientColors], 1.290 + SkScalar** stops, 1.291 + SkShader::TileMode* tm); 1.292 + 1.293 + virtual bool onIsEqual(const GrEffect& effect) const SK_OVERRIDE; 1.294 + 1.295 + const GrCoordTransform& getCoordTransform() const { return fCoordTransform; } 1.296 + 1.297 +private: 1.298 + static const GrCoordSet kCoordSet = kLocal_GrCoordSet; 1.299 + 1.300 + enum { 1.301 + kMaxAnalyticColors = 3 // if more colors use texture 1.302 + }; 1.303 + 1.304 + GrCoordTransform fCoordTransform; 1.305 + GrTextureAccess fTextureAccess; 1.306 + SkScalar fYCoord; 1.307 + GrTextureStripAtlas* fAtlas; 1.308 + int fRow; 1.309 + bool fIsOpaque; 1.310 + ColorType fColorType; 1.311 + SkColor fColors[kMaxAnalyticColors]; 1.312 + PremulType fPremulType; // This only changes behavior for two and three color special cases. 1.313 + // It is already baked into to the table for texture gradients. 1.314 + typedef GrEffect INHERITED; 1.315 + 1.316 +}; 1.317 + 1.318 +/////////////////////////////////////////////////////////////////////////////// 1.319 + 1.320 +// Base class for GL gradient effects 1.321 +class GrGLGradientEffect : public GrGLEffect { 1.322 +public: 1.323 + GrGLGradientEffect(const GrBackendEffectFactory& factory); 1.324 + virtual ~GrGLGradientEffect(); 1.325 + 1.326 + virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE; 1.327 + 1.328 +protected: 1.329 + enum { 1.330 + kPremulTypeKeyBitCnt = 1, 1.331 + kPremulTypeMask = 1, 1.332 + kPremulBeforeInterpKey = kPremulTypeMask, 1.333 + 1.334 + kTwoColorKey = 2 << kPremulTypeKeyBitCnt, 1.335 + kThreeColorKey = 3 << kPremulTypeKeyBitCnt, 1.336 + kColorKeyMask = kTwoColorKey | kThreeColorKey, 1.337 + kColorKeyBitCnt = 2, 1.338 + 1.339 + // Subclasses must shift any key bits they produce up by this amount 1.340 + // and combine with the result of GenBaseGradientKey. 1.341 + kBaseKeyBitCnt = (kPremulTypeKeyBitCnt + kColorKeyBitCnt) 1.342 + }; 1.343 + 1.344 + static GrGradientEffect::ColorType ColorTypeFromKey(EffectKey key){ 1.345 + if (kTwoColorKey == (key & kColorKeyMask)) { 1.346 + return GrGradientEffect::kTwo_ColorType; 1.347 + } else if (kThreeColorKey == (key & kColorKeyMask)) { 1.348 + return GrGradientEffect::kThree_ColorType; 1.349 + } else {return GrGradientEffect::kTexture_ColorType;} 1.350 + } 1.351 + 1.352 + static GrGradientEffect::PremulType PremulTypeFromKey(EffectKey key){ 1.353 + if (kPremulBeforeInterpKey == (key & kPremulTypeMask)) { 1.354 + return GrGradientEffect::kBeforeInterp_PremulType; 1.355 + } else { 1.356 + return GrGradientEffect::kAfterInterp_PremulType; 1.357 + } 1.358 + } 1.359 + 1.360 + /** 1.361 + * Subclasses must call this. It will return a value restricted to the lower kBaseKeyBitCnt 1.362 + * bits. 1.363 + */ 1.364 + static EffectKey GenBaseGradientKey(const GrDrawEffect&); 1.365 + 1.366 + // Emits the uniform used as the y-coord to texture samples in derived classes. Subclasses 1.367 + // should call this method from their emitCode(). 1.368 + void emitUniforms(GrGLShaderBuilder* builder, EffectKey key); 1.369 + 1.370 + 1.371 + // emit code that gets a fragment's color from an expression for t; Has branches for 3 separate 1.372 + // control flows inside -- 2 color gradients, 3 color symmetric gradients (both using 1.373 + // native GLSL mix), and 4+ color gradients that use the traditional texture lookup. 1.374 + void emitColor(GrGLShaderBuilder* builder, 1.375 + const char* gradientTValue, 1.376 + EffectKey key, 1.377 + const char* outputColor, 1.378 + const char* inputColor, 1.379 + const TextureSamplerArray& samplers); 1.380 + 1.381 +private: 1.382 + SkScalar fCachedYCoord; 1.383 + GrGLUniformManager::UniformHandle fFSYUni; 1.384 + GrGLUniformManager::UniformHandle fColorStartUni; 1.385 + GrGLUniformManager::UniformHandle fColorMidUni; 1.386 + GrGLUniformManager::UniformHandle fColorEndUni; 1.387 + 1.388 + typedef GrGLEffect INHERITED; 1.389 +}; 1.390 + 1.391 +#endif 1.392 + 1.393 +#endif