| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef SkGradientShaderPriv_DEFINED | 8 #ifndef SkGradientShaderPriv_DEFINED |
| 9 #define SkGradientShaderPriv_DEFINED | 9 #define SkGradientShaderPriv_DEFINED |
| 10 | 10 |
| 11 #include "SkGradientShader.h" | 11 #include "SkGradientShader.h" |
| 12 #include "SkClampRange.h" | 12 #include "SkClampRange.h" |
| 13 #include "SkColorPriv.h" | 13 #include "SkColorPriv.h" |
| 14 #include "SkReadBuffer.h" | 14 #include "SkReadBuffer.h" |
| 15 #include "SkWriteBuffer.h" | 15 #include "SkWriteBuffer.h" |
| 16 #include "SkMallocPixelRef.h" | 16 #include "SkMallocPixelRef.h" |
| 17 #include "SkUnitMapper.h" | 17 #include "SkUnitMapper.h" |
| 18 #include "SkUtils.h" | 18 #include "SkUtils.h" |
| 19 #include "SkTemplates.h" | 19 #include "SkTemplates.h" |
| 20 #include "SkBitmapCache.h" | 20 #include "SkBitmapCache.h" |
| 21 #include "SkShader.h" | 21 #include "SkShader.h" |
| 22 #include "SkOnce.h" |
| 22 | 23 |
| 23 static inline void sk_memset32_dither(uint32_t dst[], uint32_t v0, uint32_t v1, | 24 static inline void sk_memset32_dither(uint32_t dst[], uint32_t v0, uint32_t v1, |
| 24 int count) { | 25 int count) { |
| 25 if (count > 0) { | 26 if (count > 0) { |
| 26 if (v0 == v1) { | 27 if (v0 == v1) { |
| 27 sk_memset32(dst, v0, count); | 28 sk_memset32(dst, v0, count); |
| 28 } else { | 29 } else { |
| 29 int pairs = count >> 1; | 30 int pairs = count >> 1; |
| 30 for (int i = 0; i < pairs; i++) { | 31 for (int i = 0; i < pairs; i++) { |
| 31 *dst++ = v0; | 32 *dst++ = v0; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 int fCount; | 95 int fCount; |
| 95 SkShader::TileMode fTileMode; | 96 SkShader::TileMode fTileMode; |
| 96 SkUnitMapper* fMapper; | 97 SkUnitMapper* fMapper; |
| 97 uint32_t fGradFlags; | 98 uint32_t fGradFlags; |
| 98 }; | 99 }; |
| 99 | 100 |
| 100 public: | 101 public: |
| 101 SkGradientShaderBase(const Descriptor& desc); | 102 SkGradientShaderBase(const Descriptor& desc); |
| 102 virtual ~SkGradientShaderBase(); | 103 virtual ~SkGradientShaderBase(); |
| 103 | 104 |
| 104 virtual bool setContext(const SkBitmap&, const SkPaint&, const SkMatrix&) SK
_OVERRIDE; | 105 // The cache is initialized on-demand when getCache16/32 is called. |
| 105 virtual uint32_t getFlags() SK_OVERRIDE { return fFlags; } | 106 class GradientShaderCache : public SkRefCnt { |
| 107 public: |
| 108 GradientShaderCache(U8CPU alpha, const SkGradientShaderBase& shader); |
| 109 ~GradientShaderCache(); |
| 110 |
| 111 const uint16_t* getCache16(); |
| 112 const SkPMColor* getCache32(); |
| 113 |
| 114 SkMallocPixelRef* getCache32PixelRef() const { return fCache32PixelRef;
} |
| 115 |
| 116 unsigned getAlpha() const { return fCacheAlpha; } |
| 117 |
| 118 private: |
| 119 // Working pointers. If either is NULL, we need to recompute the corresp
onding cache values. |
| 120 uint16_t* fCache16; |
| 121 SkPMColor* fCache32; |
| 122 |
| 123 uint16_t* fCache16Storage; // Storage for fCache16, allocated
on demand. |
| 124 SkMallocPixelRef* fCache32PixelRef; |
| 125 const unsigned fCacheAlpha; // The alpha value we used when we
computed the cache. |
| 126 // Larger than 8bits so we can sto
re uninitialized |
| 127 // value. |
| 128 |
| 129 const SkGradientShaderBase& fShader; |
| 130 |
| 131 // Make sure we only initialize the caches once. |
| 132 bool fCache16Inited, fCache32Inited; |
| 133 SkMutex fCache16Mutex, fCache32Mutex; |
| 134 |
| 135 static void initCache16(GradientShaderCache* cache); |
| 136 static void initCache32(GradientShaderCache* cache); |
| 137 |
| 138 static void Build16bitCache(uint16_t[], SkColor c0, SkColor c1, int coun
t); |
| 139 static void Build32bitCache(SkPMColor[], SkColor c0, SkColor c1, int cou
nt, |
| 140 U8CPU alpha, uint32_t gradFlags); |
| 141 }; |
| 142 |
| 143 class GradientShaderBaseContext : public SkShader::Context { |
| 144 public: |
| 145 GradientShaderBaseContext(const SkGradientShaderBase& shader, const SkBi
tmap& device, |
| 146 const SkPaint& paint, const SkMatrix& matrix); |
| 147 ~GradientShaderBaseContext() {} |
| 148 |
| 149 virtual uint32_t getFlags() const SK_OVERRIDE { return fFlags; } |
| 150 |
| 151 protected: |
| 152 SkMatrix fDstToIndex; |
| 153 SkMatrix::MapXYProc fDstToIndexProc; |
| 154 uint8_t fDstToIndexClass; |
| 155 uint8_t fFlags; |
| 156 |
| 157 SkAutoTUnref<GradientShaderCache> fCache; |
| 158 |
| 159 private: |
| 160 typedef SkShader::Context INHERITED; |
| 161 }; |
| 162 |
| 106 virtual bool isOpaque() const SK_OVERRIDE; | 163 virtual bool isOpaque() const SK_OVERRIDE; |
| 107 | 164 |
| 108 void getGradientTableBitmap(SkBitmap*) const; | 165 void getGradientTableBitmap(SkBitmap*) const; |
| 109 | 166 |
| 110 enum { | 167 enum { |
| 111 /// Seems like enough for visual accuracy. TODO: if pos[] deserves | 168 /// Seems like enough for visual accuracy. TODO: if pos[] deserves |
| 112 /// it, use a larger cache. | 169 /// it, use a larger cache. |
| 113 kCache16Bits = 8, | 170 kCache16Bits = 8, |
| 114 kCache16Count = (1 << kCache16Bits), | 171 kCache16Count = (1 << kCache16Bits), |
| 115 kCache16Shift = 16 - kCache16Bits, | 172 kCache16Shift = 16 - kCache16Bits, |
| (...skipping 25 matching lines...) Expand all Loading... |
| 141 | 198 |
| 142 uint32_t getGradFlags() const { return fGradFlags; } | 199 uint32_t getGradFlags() const { return fGradFlags; } |
| 143 | 200 |
| 144 protected: | 201 protected: |
| 145 SkGradientShaderBase(SkReadBuffer& ); | 202 SkGradientShaderBase(SkReadBuffer& ); |
| 146 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE; | 203 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE; |
| 147 SK_TO_STRING_OVERRIDE() | 204 SK_TO_STRING_OVERRIDE() |
| 148 | 205 |
| 149 SkUnitMapper* fMapper; | 206 SkUnitMapper* fMapper; |
| 150 SkMatrix fPtsToUnit; // set by subclass | 207 SkMatrix fPtsToUnit; // set by subclass |
| 151 SkMatrix fDstToIndex; | |
| 152 SkMatrix::MapXYProc fDstToIndexProc; | |
| 153 TileMode fTileMode; | 208 TileMode fTileMode; |
| 154 TileProc fTileProc; | 209 TileProc fTileProc; |
| 155 int fColorCount; | 210 int fColorCount; |
| 156 uint8_t fDstToIndexClass; | |
| 157 uint8_t fFlags; | |
| 158 uint8_t fGradFlags; | 211 uint8_t fGradFlags; |
| 159 | 212 |
| 160 struct Rec { | 213 struct Rec { |
| 161 SkFixed fPos; // 0...1 | 214 SkFixed fPos; // 0...1 |
| 162 uint32_t fScale; // (1 << 24) / range | 215 uint32_t fScale; // (1 << 24) / range |
| 163 }; | 216 }; |
| 164 Rec* fRecs; | 217 Rec* fRecs; |
| 165 | 218 |
| 166 const uint16_t* getCache16() const; | |
| 167 const SkPMColor* getCache32() const; | |
| 168 | |
| 169 void commonAsAGradient(GradientInfo*, bool flipGrad = false) const; | 219 void commonAsAGradient(GradientInfo*, bool flipGrad = false) const; |
| 170 | 220 |
| 171 /* | 221 /* |
| 172 * Takes in pointers to gradient color and Rec info as colorSrc and recSrc r
espectively. | 222 * Takes in pointers to gradient color and Rec info as colorSrc and recSrc r
espectively. |
| 173 * Count is the number of colors in the gradient | 223 * Count is the number of colors in the gradient |
| 174 * It will then flip all the color and rec information and return in their r
espective Dst | 224 * It will then flip all the color and rec information and return in their r
espective Dst |
| 175 * pointers. It is assumed that space has already been allocated for the Dst
pointers. | 225 * pointers. It is assumed that space has already been allocated for the Dst
pointers. |
| 176 * The rec src and dst are only assumed to be valid if count > 2 | 226 * The rec src and dst are only assumed to be valid if count > 2 |
| 177 */ | 227 */ |
| 178 static void FlipGradientColors(SkColor* colorDst, Rec* recDst, | 228 static void FlipGradientColors(SkColor* colorDst, Rec* recDst, |
| 179 SkColor* colorSrc, Rec* recSrc, | 229 SkColor* colorSrc, Rec* recSrc, |
| 180 int count); | 230 int count); |
| 181 | 231 |
| 182 // V23_COMPATIBILITY_CODE | 232 // V23_COMPATIBILITY_CODE |
| 183 // Used for 2-pt conical gradients since we sort start/end cirlces by radius | 233 // Used for 2-pt conical gradients since we sort start/end cirlces by radius |
| 184 // Assumes space has already been allocated for fOrigColors | 234 // Assumes space has already been allocated for fOrigColors |
| 185 void flipGradientColors(); | 235 void flipGradientColors(); |
| 186 | 236 |
| 187 private: | 237 private: |
| 188 enum { | 238 enum { |
| 189 kColorStorageCount = 4, // more than this many colors, and we'll use sk_
malloc for the space | 239 kColorStorageCount = 4, // more than this many colors, and we'll use sk_
malloc for the space |
| 190 | 240 |
| 191 kStorageSize = kColorStorageCount * (sizeof(SkColor) + sizeof(Rec)) | 241 kStorageSize = kColorStorageCount * (sizeof(SkColor) + sizeof(Rec)) |
| 192 }; | 242 }; |
| 193 SkColor fStorage[(kStorageSize + 3) >> 2]; | 243 SkColor fStorage[(kStorageSize + 3) >> 2]; |
| 194 SkColor* fOrigColors; // original colors, before modulation by paint in s
etContext | 244 SkColor* fOrigColors; // original colors, before modulation by paint in c
ontext. |
| 195 bool fColorsAreOpaque; | 245 bool fColorsAreOpaque; |
| 196 | 246 |
| 197 mutable uint16_t* fCache16; // working ptr. If this is NULL, we need to
recompute the cache values | 247 GradientShaderCache* refCache(U8CPU alpha) const; |
| 198 mutable SkPMColor* fCache32; // working ptr. If this is NULL, we need to
recompute the cache values | 248 mutable SkMutex fCacheMutex; |
| 249 mutable SkAutoTUnref<GradientShaderCache> fCache; |
| 199 | 250 |
| 200 mutable uint16_t* fCache16Storage; // storage for fCache16, allocated o
n demand | |
| 201 mutable SkMallocPixelRef* fCache32PixelRef; | |
| 202 mutable unsigned fCacheAlpha; // the alpha value we used when we c
omputed the cache. larger than 8bits so we can store uninitialized value | |
| 203 | |
| 204 static void Build16bitCache(uint16_t[], SkColor c0, SkColor c1, int count); | |
| 205 static void Build32bitCache(SkPMColor[], SkColor c0, SkColor c1, int count, | |
| 206 U8CPU alpha, uint32_t gradFlags); | |
| 207 void setCacheAlpha(U8CPU alpha) const; | |
| 208 void initCommon(); | 251 void initCommon(); |
| 209 | 252 |
| 210 typedef SkShader INHERITED; | 253 typedef SkShader INHERITED; |
| 211 }; | 254 }; |
| 212 | 255 |
| 213 static inline int init_dither_toggle(int x, int y) { | 256 static inline int init_dither_toggle(int x, int y) { |
| 214 x &= 1; | 257 x &= 1; |
| 215 y = (y & 1) << 1; | 258 y = (y & 1) << 1; |
| 216 return (x | y) * SkGradientShaderBase::kDitherStride32; | 259 return (x | y) * SkGradientShaderBase::kDitherStride32; |
| 217 } | 260 } |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 399 GrGLUniformManager::UniformHandle fColorStartUni; | 442 GrGLUniformManager::UniformHandle fColorStartUni; |
| 400 GrGLUniformManager::UniformHandle fColorMidUni; | 443 GrGLUniformManager::UniformHandle fColorMidUni; |
| 401 GrGLUniformManager::UniformHandle fColorEndUni; | 444 GrGLUniformManager::UniformHandle fColorEndUni; |
| 402 | 445 |
| 403 typedef GrGLEffect INHERITED; | 446 typedef GrGLEffect INHERITED; |
| 404 }; | 447 }; |
| 405 | 448 |
| 406 #endif | 449 #endif |
| 407 | 450 |
| 408 #endif | 451 #endif |
| OLD | NEW |