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" | |
23 | 22 |
24 static inline void sk_memset32_dither(uint32_t dst[], uint32_t v0, uint32_t v1, | 23 static inline void sk_memset32_dither(uint32_t dst[], uint32_t v0, uint32_t v1, |
25 int count) { | 24 int count) { |
26 if (count > 0) { | 25 if (count > 0) { |
27 if (v0 == v1) { | 26 if (v0 == v1) { |
28 sk_memset32(dst, v0, count); | 27 sk_memset32(dst, v0, count); |
29 } else { | 28 } else { |
30 int pairs = count >> 1; | 29 int pairs = count >> 1; |
31 for (int i = 0; i < pairs; i++) { | 30 for (int i = 0; i < pairs; i++) { |
32 *dst++ = v0; | 31 *dst++ = v0; |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 int fCount; | 94 int fCount; |
96 SkShader::TileMode fTileMode; | 95 SkShader::TileMode fTileMode; |
97 SkUnitMapper* fMapper; | 96 SkUnitMapper* fMapper; |
98 uint32_t fGradFlags; | 97 uint32_t fGradFlags; |
99 }; | 98 }; |
100 | 99 |
101 public: | 100 public: |
102 SkGradientShaderBase(const Descriptor& desc); | 101 SkGradientShaderBase(const Descriptor& desc); |
103 virtual ~SkGradientShaderBase(); | 102 virtual ~SkGradientShaderBase(); |
104 | 103 |
105 // The cache is initialized on-demand when getCache16/32 is called. | 104 virtual bool setContext(const SkBitmap&, const SkPaint&, const SkMatrix&) SK
_OVERRIDE; |
106 class GradientShaderCache : public SkRefCnt { | 105 virtual uint32_t getFlags() SK_OVERRIDE { return fFlags; } |
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 | |
163 virtual bool isOpaque() const SK_OVERRIDE; | 106 virtual bool isOpaque() const SK_OVERRIDE; |
164 | 107 |
165 void getGradientTableBitmap(SkBitmap*) const; | 108 void getGradientTableBitmap(SkBitmap*) const; |
166 | 109 |
167 enum { | 110 enum { |
168 /// Seems like enough for visual accuracy. TODO: if pos[] deserves | 111 /// Seems like enough for visual accuracy. TODO: if pos[] deserves |
169 /// it, use a larger cache. | 112 /// it, use a larger cache. |
170 kCache16Bits = 8, | 113 kCache16Bits = 8, |
171 kCache16Count = (1 << kCache16Bits), | 114 kCache16Count = (1 << kCache16Bits), |
172 kCache16Shift = 16 - kCache16Bits, | 115 kCache16Shift = 16 - kCache16Bits, |
(...skipping 25 matching lines...) Expand all Loading... |
198 | 141 |
199 uint32_t getGradFlags() const { return fGradFlags; } | 142 uint32_t getGradFlags() const { return fGradFlags; } |
200 | 143 |
201 protected: | 144 protected: |
202 SkGradientShaderBase(SkReadBuffer& ); | 145 SkGradientShaderBase(SkReadBuffer& ); |
203 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE; | 146 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE; |
204 SK_TO_STRING_OVERRIDE() | 147 SK_TO_STRING_OVERRIDE() |
205 | 148 |
206 SkUnitMapper* fMapper; | 149 SkUnitMapper* fMapper; |
207 SkMatrix fPtsToUnit; // set by subclass | 150 SkMatrix fPtsToUnit; // set by subclass |
| 151 SkMatrix fDstToIndex; |
| 152 SkMatrix::MapXYProc fDstToIndexProc; |
208 TileMode fTileMode; | 153 TileMode fTileMode; |
209 TileProc fTileProc; | 154 TileProc fTileProc; |
210 int fColorCount; | 155 int fColorCount; |
| 156 uint8_t fDstToIndexClass; |
| 157 uint8_t fFlags; |
211 uint8_t fGradFlags; | 158 uint8_t fGradFlags; |
212 | 159 |
213 struct Rec { | 160 struct Rec { |
214 SkFixed fPos; // 0...1 | 161 SkFixed fPos; // 0...1 |
215 uint32_t fScale; // (1 << 24) / range | 162 uint32_t fScale; // (1 << 24) / range |
216 }; | 163 }; |
217 Rec* fRecs; | 164 Rec* fRecs; |
218 | 165 |
| 166 const uint16_t* getCache16() const; |
| 167 const SkPMColor* getCache32() const; |
| 168 |
219 void commonAsAGradient(GradientInfo*, bool flipGrad = false) const; | 169 void commonAsAGradient(GradientInfo*, bool flipGrad = false) const; |
220 | 170 |
221 /* | 171 /* |
222 * Takes in pointers to gradient color and Rec info as colorSrc and recSrc r
espectively. | 172 * Takes in pointers to gradient color and Rec info as colorSrc and recSrc r
espectively. |
223 * Count is the number of colors in the gradient | 173 * Count is the number of colors in the gradient |
224 * It will then flip all the color and rec information and return in their r
espective Dst | 174 * It will then flip all the color and rec information and return in their r
espective Dst |
225 * pointers. It is assumed that space has already been allocated for the Dst
pointers. | 175 * pointers. It is assumed that space has already been allocated for the Dst
pointers. |
226 * The rec src and dst are only assumed to be valid if count > 2 | 176 * The rec src and dst are only assumed to be valid if count > 2 |
227 */ | 177 */ |
228 static void FlipGradientColors(SkColor* colorDst, Rec* recDst, | 178 static void FlipGradientColors(SkColor* colorDst, Rec* recDst, |
229 SkColor* colorSrc, Rec* recSrc, | 179 SkColor* colorSrc, Rec* recSrc, |
230 int count); | 180 int count); |
231 | 181 |
232 // V23_COMPATIBILITY_CODE | 182 // V23_COMPATIBILITY_CODE |
233 // Used for 2-pt conical gradients since we sort start/end cirlces by radius | 183 // Used for 2-pt conical gradients since we sort start/end cirlces by radius |
234 // Assumes space has already been allocated for fOrigColors | 184 // Assumes space has already been allocated for fOrigColors |
235 void flipGradientColors(); | 185 void flipGradientColors(); |
236 | 186 |
237 private: | 187 private: |
238 enum { | 188 enum { |
239 kColorStorageCount = 4, // more than this many colors, and we'll use sk_
malloc for the space | 189 kColorStorageCount = 4, // more than this many colors, and we'll use sk_
malloc for the space |
240 | 190 |
241 kStorageSize = kColorStorageCount * (sizeof(SkColor) + sizeof(Rec)) | 191 kStorageSize = kColorStorageCount * (sizeof(SkColor) + sizeof(Rec)) |
242 }; | 192 }; |
243 SkColor fStorage[(kStorageSize + 3) >> 2]; | 193 SkColor fStorage[(kStorageSize + 3) >> 2]; |
244 SkColor* fOrigColors; // original colors, before modulation by paint in c
ontext. | 194 SkColor* fOrigColors; // original colors, before modulation by paint in s
etContext |
245 bool fColorsAreOpaque; | 195 bool fColorsAreOpaque; |
246 | 196 |
247 GradientShaderCache* refCache(U8CPU alpha) const; | 197 mutable uint16_t* fCache16; // working ptr. If this is NULL, we need to
recompute the cache values |
248 mutable SkMutex fCacheMutex; | 198 mutable SkPMColor* fCache32; // working ptr. If this is NULL, we need to
recompute the cache values |
249 mutable SkAutoTUnref<GradientShaderCache> fCache; | |
250 | 199 |
| 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; |
251 void initCommon(); | 208 void initCommon(); |
252 | 209 |
253 typedef SkShader INHERITED; | 210 typedef SkShader INHERITED; |
254 }; | 211 }; |
255 | 212 |
256 static inline int init_dither_toggle(int x, int y) { | 213 static inline int init_dither_toggle(int x, int y) { |
257 x &= 1; | 214 x &= 1; |
258 y = (y & 1) << 1; | 215 y = (y & 1) << 1; |
259 return (x | y) * SkGradientShaderBase::kDitherStride32; | 216 return (x | y) * SkGradientShaderBase::kDitherStride32; |
260 } | 217 } |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
442 GrGLUniformManager::UniformHandle fColorStartUni; | 399 GrGLUniformManager::UniformHandle fColorStartUni; |
443 GrGLUniformManager::UniformHandle fColorMidUni; | 400 GrGLUniformManager::UniformHandle fColorMidUni; |
444 GrGLUniformManager::UniformHandle fColorEndUni; | 401 GrGLUniformManager::UniformHandle fColorEndUni; |
445 | 402 |
446 typedef GrGLEffect INHERITED; | 403 typedef GrGLEffect INHERITED; |
447 }; | 404 }; |
448 | 405 |
449 #endif | 406 #endif |
450 | 407 |
451 #endif | 408 #endif |
OLD | NEW |