| 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 fFlags; | 98 uint32_t fFlags; |
| 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 de
mand. |
| 124 SkMallocPixelRef* fCache32PixelRef; |
| 125 unsigned fCacheAlpha; // The alpha value we used when we compu
ted the cache. |
| 126 // Larger than 8bits so we can store uni
nitialized value. |
| 127 |
| 128 const SkGradientShaderBase& fShader; |
| 129 |
| 130 // Make sure we only initialize the caches once. |
| 131 bool fCache16Inited, fCache32Inited; |
| 132 SkMutex fCache16Mutex, fCache32Mutex; |
| 133 |
| 134 static void initCache16(GradientShaderCache* cache); |
| 135 static void initCache32(GradientShaderCache* cache); |
| 136 |
| 137 static void Build16bitCache(uint16_t[], SkColor c0, SkColor c1, int coun
t); |
| 138 static void Build32bitCache(SkPMColor[], SkColor c0, SkColor c1, int cou
nt, |
| 139 U8CPU alpha, uint32_t gradFlags); |
| 140 }; |
| 141 |
| 142 class GradientShaderBaseContext : public SkShader::Context { |
| 143 public: |
| 144 GradientShaderBaseContext(const SkGradientShaderBase& shader, const SkBi
tmap& device, |
| 145 const SkPaint& paint, const SkMatrix& matrix); |
| 146 ~GradientShaderBaseContext() {} |
| 147 |
| 148 protected: |
| 149 SkMatrix fDstToIndex; |
| 150 SkMatrix::MapXYProc fDstToIndexProc; |
| 151 uint8_t fDstToIndexClass; |
| 152 uint8_t fFlags; |
| 153 |
| 154 SkAutoTUnref<GradientShaderCache> fCache; |
| 155 |
| 156 private: |
| 157 typedef SkShader::Context INHERITED; |
| 158 }; |
| 159 |
| 106 virtual bool isOpaque() const SK_OVERRIDE; | 160 virtual bool isOpaque() const SK_OVERRIDE; |
| 107 | 161 |
| 108 void getGradientTableBitmap(SkBitmap*) const; | 162 void getGradientTableBitmap(SkBitmap*) const; |
| 109 | 163 |
| 110 enum { | 164 enum { |
| 111 /// Seems like enough for visual accuracy. TODO: if pos[] deserves | 165 /// Seems like enough for visual accuracy. TODO: if pos[] deserves |
| 112 /// it, use a larger cache. | 166 /// it, use a larger cache. |
| 113 kCache16Bits = 8, | 167 kCache16Bits = 8, |
| 114 kCache16Count = (1 << kCache16Bits), | 168 kCache16Count = (1 << kCache16Bits), |
| 115 kCache16Shift = 16 - kCache16Bits, | 169 kCache16Shift = 16 - kCache16Bits, |
| 116 kSqrt16Shift = 8 - kCache16Bits, | 170 kSqrt16Shift = 8 - kCache16Bits, |
| 117 | 171 |
| 118 /// Seems like enough for visual accuracy. TODO: if pos[] deserves | 172 /// Seems like enough for visual accuracy. TODO: if pos[] deserves |
| 119 /// it, use a larger cache. | 173 /// it, use a larger cache. |
| 120 kCache32Bits = 8, | 174 kCache32Bits = 8, |
| 121 kCache32Count = (1 << kCache32Bits), | 175 kCache32Count = (1 << kCache32Bits), |
| 122 kCache32Shift = 16 - kCache32Bits, | 176 kCache32Shift = 16 - kCache32Bits, |
| 123 kSqrt32Shift = 8 - kCache32Bits, | 177 kSqrt32Shift = 8 - kCache32Bits, |
| 124 | 178 |
| 125 /// This value is used to *read* the dither cache; it may be 0 | 179 /// This value is used to *read* the dither cache; it may be 0 |
| 126 /// if dithering is disabled. | 180 /// if dithering is disabled. |
| 127 kDitherStride32 = kCache32Count, | 181 kDitherStride32 = kCache32Count, |
| 128 kDitherStride16 = kCache16Count, | 182 kDitherStride16 = kCache16Count, |
| 129 }; | 183 }; |
| 130 | 184 |
| 131 | |
| 132 protected: | 185 protected: |
| 133 SkGradientShaderBase(SkReadBuffer& ); | 186 SkGradientShaderBase(SkReadBuffer& ); |
| 134 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE; | 187 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE; |
| 135 SK_TO_STRING_OVERRIDE() | 188 SK_TO_STRING_OVERRIDE() |
| 136 | 189 |
| 137 SkUnitMapper* fMapper; | 190 SkUnitMapper* fMapper; |
| 138 SkMatrix fPtsToUnit; // set by subclass | 191 SkMatrix fPtsToUnit; // set by subclass |
| 139 SkMatrix fDstToIndex; | |
| 140 SkMatrix::MapXYProc fDstToIndexProc; | |
| 141 TileMode fTileMode; | 192 TileMode fTileMode; |
| 142 TileProc fTileProc; | 193 TileProc fTileProc; |
| 143 int fColorCount; | 194 int fColorCount; |
| 144 uint8_t fDstToIndexClass; | |
| 145 uint8_t fFlags; | |
| 146 uint8_t fGradFlags; | 195 uint8_t fGradFlags; |
| 147 | 196 |
| 148 struct Rec { | 197 struct Rec { |
| 149 SkFixed fPos; // 0...1 | 198 SkFixed fPos; // 0...1 |
| 150 uint32_t fScale; // (1 << 24) / range | 199 uint32_t fScale; // (1 << 24) / range |
| 151 }; | 200 }; |
| 152 Rec* fRecs; | 201 Rec* fRecs; |
| 153 | 202 |
| 154 const uint16_t* getCache16() const; | |
| 155 const SkPMColor* getCache32() const; | |
| 156 | |
| 157 void commonAsAGradient(GradientInfo*) const; | 203 void commonAsAGradient(GradientInfo*) const; |
| 158 | 204 |
| 159 private: | 205 private: |
| 160 enum { | 206 enum { |
| 161 kColorStorageCount = 4, // more than this many colors, and we'll use sk_
malloc for the space | 207 kColorStorageCount = 4, // more than this many colors, and we'll use sk_
malloc for the space |
| 162 | 208 |
| 163 kStorageSize = kColorStorageCount * (sizeof(SkColor) + sizeof(Rec)) | 209 kStorageSize = kColorStorageCount * (sizeof(SkColor) + sizeof(Rec)) |
| 164 }; | 210 }; |
| 165 SkColor fStorage[(kStorageSize + 3) >> 2]; | 211 SkColor fStorage[(kStorageSize + 3) >> 2]; |
| 166 SkColor* fOrigColors; // original colors, before modulation by paint in s
etContext | 212 SkColor* fOrigColors; // original colors, before modulation by paint in c
ontext. |
| 167 bool fColorsAreOpaque; | 213 bool fColorsAreOpaque; |
| 168 | 214 |
| 169 mutable uint16_t* fCache16; // working ptr. If this is NULL, we need to
recompute the cache values | 215 GradientShaderCache* getCache(U8CPU alpha) const; |
| 170 mutable SkPMColor* fCache32; // working ptr. If this is NULL, we need to
recompute the cache values | 216 mutable SkAutoTUnref<GradientShaderCache> fCache; |
| 171 | 217 |
| 172 mutable uint16_t* fCache16Storage; // storage for fCache16, allocated o
n demand | |
| 173 mutable SkMallocPixelRef* fCache32PixelRef; | |
| 174 mutable unsigned fCacheAlpha; // the alpha value we used when we c
omputed the cache. larger than 8bits so we can store uninitialized value | |
| 175 | |
| 176 static void Build16bitCache(uint16_t[], SkColor c0, SkColor c1, int count); | |
| 177 static void Build32bitCache(SkPMColor[], SkColor c0, SkColor c1, int count, | |
| 178 U8CPU alpha, uint32_t gradFlags); | |
| 179 void setCacheAlpha(U8CPU alpha) const; | |
| 180 void initCommon(); | 218 void initCommon(); |
| 181 | 219 |
| 182 typedef SkShader INHERITED; | 220 typedef SkShader INHERITED; |
| 183 }; | 221 }; |
| 184 | 222 |
| 185 static inline int init_dither_toggle(int x, int y) { | 223 static inline int init_dither_toggle(int x, int y) { |
| 186 x &= 1; | 224 x &= 1; |
| 187 y = (y & 1) << 1; | 225 y = (y & 1) << 1; |
| 188 return (x | y) * SkGradientShaderBase::kDitherStride32; | 226 return (x | y) * SkGradientShaderBase::kDitherStride32; |
| 189 } | 227 } |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 GrGLUniformManager::UniformHandle fColorStartUni; | 419 GrGLUniformManager::UniformHandle fColorStartUni; |
| 382 GrGLUniformManager::UniformHandle fColorMidUni; | 420 GrGLUniformManager::UniformHandle fColorMidUni; |
| 383 GrGLUniformManager::UniformHandle fColorEndUni; | 421 GrGLUniformManager::UniformHandle fColorEndUni; |
| 384 | 422 |
| 385 typedef GrGLEffect INHERITED; | 423 typedef GrGLEffect INHERITED; |
| 386 }; | 424 }; |
| 387 | 425 |
| 388 #endif | 426 #endif |
| 389 | 427 |
| 390 #endif | 428 #endif |
| OLD | NEW |