1 /* 2 * Copyright 2012 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef SkGradientShaderPriv_DEFINED 9 #define SkGradientShaderPriv_DEFINED 10 11 #include "SkGradientShader.h" 12 #include "SkClampRange.h" 13 #include "SkColorPriv.h" 14 #include "SkFlattenableBuffers.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 22 static inline void sk_memset32_dither(uint32_t dst[], uint32_t v0, uint32_t v1, 23 int count) { 24 if (count > 0) { 25 if (v0 == v1) { 26 sk_memset32(dst, v0, count); 27 } else { 28 int pairs = count >> 1; 29 for (int i = 0; i < pairs; i++) { 30 *dst++ = v0; 31 *dst++ = v1; 32 } 33 if (count & 1) { 34 *dst = v0; 35 } 36 } 37 } 38 } 39 40 // Clamp 41 42 static inline SkFixed clamp_tileproc(SkFixed x) { 43 return SkClampMax(x, 0xFFFF); 44 } 45 46 // Repeat 47 48 static inline SkFixed repeat_tileproc(SkFixed x) { 49 return x & 0xFFFF; 50 } 51 52 // Mirror 53 54 // Visual Studio 2010 (MSC_VER=1600) optimizes bit-shift code incorrectly. 55 // See http://code.google.com/p/skia/issues/detail?id=472 56 #if defined(_MSC_VER) && (_MSC_VER >= 1600) 57 #pragma optimize("", off) 58 #endif 59 60 static inline SkFixed mirror_tileproc(SkFixed x) { 61 int s = x << 15 >> 31; 62 return (x ^ s) & 0xFFFF; 63 } 64 65 #if defined(_MSC_VER) && (_MSC_VER >= 1600) 66 #pragma optimize("", on) 67 #endif 68 69 /////////////////////////////////////////////////////////////////////////////// 70 71 typedef SkFixed (*TileProc)(SkFixed); 72 73 /////////////////////////////////////////////////////////////////////////////// 74 75 static const TileProc gTileProcs[] = { 76 clamp_tileproc, 77 repeat_tileproc, 78 mirror_tileproc 79 }; 80 81 /////////////////////////////////////////////////////////////////////////////// 82 83 class SkGradientShaderBase : public SkShader { 84 public: 85 struct Descriptor { 86 Descriptor() { 87 sk_bzero(this, sizeof(*this)); 88 fTileMode = SkShader::kClamp_TileMode; 89 } 90 91 const SkColor* fColors; 92 const SkScalar* fPos; 93 int fCount; 94 SkShader::TileMode fTileMode; 95 SkUnitMapper* fMapper; 96 uint32_t fFlags; 97 }; 98 99 public: 100 SkGradientShaderBase(const Descriptor& desc); 101 virtual ~SkGradientShaderBase(); 102 103 virtual bool setContext(const SkBitmap&, const SkPaint&, const SkMatrix&) SK_OVERRIDE; 104 virtual uint32_t getFlags() SK_OVERRIDE { return fFlags; } 105 virtual bool isOpaque() const SK_OVERRIDE; 106 107 void getGradientTableBitmap(SkBitmap*) const; 108 109 enum { 110 /// Seems like enough for visual accuracy. TODO: if pos[] deserves 111 /// it, use a larger cache. 112 kCache16Bits = 8, 113 kCache16Count = (1 << kCache16Bits), 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 kCache32Count = (1 << kCache32Bits), 121 kCache32Shift = 16 - kCache32Bits, 122 kSqrt32Shift = 8 - kCache32Bits, 123 124 /// This value is used to *read* the dither cache; it may be 0 125 /// if dithering is disabled. 126 kDitherStride32 = kCache32Count, 127 kDitherStride16 = kCache16Count, 128 }; 129 130 131 protected: 132 SkGradientShaderBase(SkFlattenableReadBuffer& ); 133 virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE; 134 SK_DEVELOPER_TO_STRING() 135 136 SkUnitMapper* fMapper; 137 SkMatrix fPtsToUnit; // set by subclass 138 SkMatrix fDstToIndex; 139 SkMatrix::MapXYProc fDstToIndexProc; 140 TileMode fTileMode; 141 TileProc fTileProc; 142 int fColorCount; 143 uint8_t fDstToIndexClass; 144 uint8_t fFlags; 145 uint8_t fGradFlags; 146 147 struct Rec { 148 SkFixed fPos; // 0...1 149 uint32_t fScale; // (1 << 24) / range 150 }; 151 Rec* fRecs; 152 153 const uint16_t* getCache16() const; 154 const SkPMColor* getCache32() const; 155 156 void commonAsAGradient(GradientInfo*) const; 157 158 private: 159 enum { 160 kColorStorageCount = 4, // more than this many colors, and we'll use sk_malloc for the space 161 162 kStorageSize = kColorStorageCount * (sizeof(SkColor) + sizeof(Rec)) 163 }; 164 SkColor fStorage[(kStorageSize + 3) >> 2]; 165 SkColor* fOrigColors; // original colors, before modulation by paint in setContext 166 bool fColorsAreOpaque; 167 168 mutable uint16_t* fCache16; // working ptr. If this is NULL, we need to recompute the cache values 169 mutable SkPMColor* fCache32; // working ptr. If this is NULL, we need to recompute the cache values 170 171 mutable uint16_t* fCache16Storage; // storage for fCache16, allocated on demand 172 mutable SkMallocPixelRef* fCache32PixelRef; 173 mutable unsigned fCacheAlpha; // the alpha value we used when we computed the cache. larger than 8bits so we can store uninitialized value 174 175 static void Build16bitCache(uint16_t[], SkColor c0, SkColor c1, int count); 176 static void Build32bitCache(SkPMColor[], SkColor c0, SkColor c1, int count, 177 U8CPU alpha, uint32_t gradFlags); 178 void setCacheAlpha(U8CPU alpha) const; 179 void initCommon(); 180 181 typedef SkShader INHERITED; 182 }; 183 184 static inline int init_dither_toggle(int x, int y) { 185 x &= 1; 186 y = (y & 1) << 1; 187 return (x | y) * SkGradientShaderBase::kDitherStride32; 188 } 189 190 static inline int next_dither_toggle(int toggle) { 191 return toggle ^ SkGradientShaderBase::kDitherStride32; 192 } 193 194 static inline int init_dither_toggle16(int x, int y) { 195 return ((x ^ y) & 1) * SkGradientShaderBase::kDitherStride16; 196 } 197 198 static inline int next_dither_toggle16(int toggle) { 199 return toggle ^ SkGradientShaderBase::kDitherStride16; 200 } 201 202 /////////////////////////////////////////////////////////////////////////////// 203 204 #if SK_SUPPORT_GPU 205 206 #include "gl/GrGLEffect.h" 207 #include "gl/GrGLEffectMatrix.h" 208 209 class GrEffectStage; 210 class GrBackendEffectFactory; 211 212 /* 213 * The interpretation of the texture matrix depends on the sample mode. The 214 * texture matrix is applied both when the texture coordinates are explicit 215 * and when vertex positions are used as texture coordinates. In the latter 216 * case the texture matrix is applied to the pre-view-matrix position 217 * values. 218 * 219 * Normal SampleMode 220 * The post-matrix texture coordinates are in normalize space with (0,0) at 221 * the top-left and (1,1) at the bottom right. 222 * RadialGradient 223 * The matrix specifies the radial gradient parameters. 224 * (0,0) in the post-matrix space is center of the radial gradient. 225 * Radial2Gradient 226 * Matrix transforms to space where first circle is centered at the 227 * origin. The second circle will be centered (x, 0) where x may be 228 * 0 and is provided by setRadial2Params. The post-matrix space is 229 * normalized such that 1 is the second radius - first radius. 230 * SweepGradient 231 * The angle from the origin of texture coordinates in post-matrix space 232 * determines the gradient value. 233 */ 234 235 class GrTextureStripAtlas; 236 237 // Base class for Gr gradient effects 238 class GrGradientEffect : public GrEffect { 239 public: 240 241 GrGradientEffect(GrContext* ctx, 242 const SkGradientShaderBase& shader, 243 const SkMatrix& matrix, 244 SkShader::TileMode tileMode); 245 246 virtual ~GrGradientEffect(); 247 248 bool useAtlas() const { return SkToBool(-1 != fRow); } 249 SkScalar getYCoord() const { return fYCoord; }; 250 const SkMatrix& getMatrix() const { return fMatrix;} 251 252 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE; 253 254 protected: 255 256 /** Populates a pair of arrays with colors and stop info to construct a random gradient. 257 The function decides whether stop values should be used or not. The return value indicates 258 the number of colors, which will be capped by kMaxRandomGradientColors. colors should be 259 sized to be at least kMaxRandomGradientColors. stops is a pointer to an array of at least 260 size kMaxRandomGradientColors. It may be updated to NULL, indicating that NULL should be 261 passed to the gradient factory rather than the array. 262 */ 263 static const int kMaxRandomGradientColors = 4; 264 static int RandomGradientParams(SkMWCRandom* r, 265 SkColor colors[kMaxRandomGradientColors], 266 SkScalar** stops, 267 SkShader::TileMode* tm); 268 269 virtual bool onIsEqual(const GrEffect& effect) const SK_OVERRIDE; 270 271 private: 272 273 GrTextureAccess fTextureAccess; 274 SkScalar fYCoord; 275 GrTextureStripAtlas* fAtlas; 276 int fRow; 277 SkMatrix fMatrix; 278 bool fIsOpaque; 279 280 typedef GrEffect INHERITED; 281 282 }; 283 284 /////////////////////////////////////////////////////////////////////////////// 285 286 // Base class for GL gradient effects 287 class GrGLGradientEffect : public GrGLEffect { 288 public: 289 GrGLGradientEffect(const GrBackendEffectFactory& factory); 290 virtual ~GrGLGradientEffect(); 291 292 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE; 293 294 protected: 295 /** 296 * Subclasses must reserve the lower kMatrixKeyBitCnt of their key for use by 297 * GrGLGradientEffect. 298 */ 299 enum { 300 kMatrixKeyBitCnt = GrGLEffectMatrix::kKeyBits, 301 kMatrixKeyMask = (1 << kMatrixKeyBitCnt) - 1, 302 }; 303 304 /** 305 * Subclasses must call this. It will return a value restricted to the lower kMatrixKeyBitCnt 306 * bits. 307 */ 308 static EffectKey GenMatrixKey(const GrDrawEffect&); 309 310 /** 311 * Inserts code to implement the GrGradientEffect's matrix. This should be called before a 312 * subclass emits its own code. The name of the 2D coords is output via fsCoordName and already 313 * incorporates any perspective division. The caller can also optionally retrieve the name of 314 * the varying inserted in the VS and its type, which may be either vec2f or vec3f depending 315 * upon whether the matrix has perspective or not. It is not necessary to mask the key before 316 * calling. 317 */ 318 void setupMatrix(GrGLShaderBuilder* builder, 319 EffectKey key, 320 const char** fsCoordName, 321 const char** vsVaryingName = NULL, 322 GrSLType* vsVaryingType = NULL); 323 324 // Emits the uniform used as the y-coord to texture samples in derived classes. Subclasses 325 // should call this method from their emitCode(). 326 void emitYCoordUniform(GrGLShaderBuilder* builder); 327 328 // emit code that gets a fragment's color from an expression for t; for now this always uses the 329 // texture, but for simpler cases we'll be able to lerp. Subclasses should call this method from 330 // their emitCode(). 331 void emitColorLookup(GrGLShaderBuilder* builder, 332 const char* gradientTValue, 333 const char* outputColor, 334 const char* inputColor, 335 const GrGLShaderBuilder::TextureSampler&); 336 337 private: 338 static const GrEffect::CoordsType kCoordsType = GrEffect::kLocal_CoordsType; 339 340 SkScalar fCachedYCoord; 341 GrGLUniformManager::UniformHandle fFSYUni; 342 GrGLEffectMatrix fEffectMatrix; 343 344 typedef GrGLEffect INHERITED; 345 }; 346 347 #endif 348 349 #endif 350