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 fFlags; | 97 uint32_t fFlags; |
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, |
173 kSqrt16Shift = 8 - kCache16Bits, | 116 kSqrt16Shift = 8 - kCache16Bits, |
174 | 117 |
175 /// Seems like enough for visual accuracy. TODO: if pos[] deserves | 118 /// Seems like enough for visual accuracy. TODO: if pos[] deserves |
176 /// it, use a larger cache. | 119 /// it, use a larger cache. |
177 kCache32Bits = 8, | 120 kCache32Bits = 8, |
178 kCache32Count = (1 << kCache32Bits), | 121 kCache32Count = (1 << kCache32Bits), |
179 kCache32Shift = 16 - kCache32Bits, | 122 kCache32Shift = 16 - kCache32Bits, |
180 kSqrt32Shift = 8 - kCache32Bits, | 123 kSqrt32Shift = 8 - kCache32Bits, |
181 | 124 |
182 /// This value is used to *read* the dither cache; it may be 0 | 125 /// This value is used to *read* the dither cache; it may be 0 |
183 /// if dithering is disabled. | 126 /// if dithering is disabled. |
184 kDitherStride32 = kCache32Count, | 127 kDitherStride32 = kCache32Count, |
185 kDitherStride16 = kCache16Count, | 128 kDitherStride16 = kCache16Count, |
186 }; | 129 }; |
187 | 130 |
| 131 |
188 protected: | 132 protected: |
189 SkGradientShaderBase(SkReadBuffer& ); | 133 SkGradientShaderBase(SkReadBuffer& ); |
190 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE; | 134 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE; |
191 SK_TO_STRING_OVERRIDE() | 135 SK_TO_STRING_OVERRIDE() |
192 | 136 |
193 SkUnitMapper* fMapper; | 137 SkUnitMapper* fMapper; |
194 SkMatrix fPtsToUnit; // set by subclass | 138 SkMatrix fPtsToUnit; // set by subclass |
| 139 SkMatrix fDstToIndex; |
| 140 SkMatrix::MapXYProc fDstToIndexProc; |
195 TileMode fTileMode; | 141 TileMode fTileMode; |
196 TileProc fTileProc; | 142 TileProc fTileProc; |
197 int fColorCount; | 143 int fColorCount; |
| 144 uint8_t fDstToIndexClass; |
| 145 uint8_t fFlags; |
198 uint8_t fGradFlags; | 146 uint8_t fGradFlags; |
199 | 147 |
200 struct Rec { | 148 struct Rec { |
201 SkFixed fPos; // 0...1 | 149 SkFixed fPos; // 0...1 |
202 uint32_t fScale; // (1 << 24) / range | 150 uint32_t fScale; // (1 << 24) / range |
203 }; | 151 }; |
204 Rec* fRecs; | 152 Rec* fRecs; |
205 | 153 |
| 154 const uint16_t* getCache16() const; |
| 155 const SkPMColor* getCache32() const; |
| 156 |
206 void commonAsAGradient(GradientInfo*) const; | 157 void commonAsAGradient(GradientInfo*) const; |
207 | 158 |
208 private: | 159 private: |
209 enum { | 160 enum { |
210 kColorStorageCount = 4, // more than this many colors, and we'll use sk_
malloc for the space | 161 kColorStorageCount = 4, // more than this many colors, and we'll use sk_
malloc for the space |
211 | 162 |
212 kStorageSize = kColorStorageCount * (sizeof(SkColor) + sizeof(Rec)) | 163 kStorageSize = kColorStorageCount * (sizeof(SkColor) + sizeof(Rec)) |
213 }; | 164 }; |
214 SkColor fStorage[(kStorageSize + 3) >> 2]; | 165 SkColor fStorage[(kStorageSize + 3) >> 2]; |
215 SkColor* fOrigColors; // original colors, before modulation by paint in c
ontext. | 166 SkColor* fOrigColors; // original colors, before modulation by paint in s
etContext |
216 bool fColorsAreOpaque; | 167 bool fColorsAreOpaque; |
217 | 168 |
218 GradientShaderCache* getCache(U8CPU alpha) const; | 169 mutable uint16_t* fCache16; // working ptr. If this is NULL, we need to
recompute the cache values |
219 mutable SkMutex fCacheMutex; | 170 mutable SkPMColor* fCache32; // working ptr. If this is NULL, we need to
recompute the cache values |
220 mutable SkAutoTUnref<GradientShaderCache> fCache; | |
221 | 171 |
| 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; |
222 void initCommon(); | 180 void initCommon(); |
223 | 181 |
224 typedef SkShader INHERITED; | 182 typedef SkShader INHERITED; |
225 }; | 183 }; |
226 | 184 |
227 static inline int init_dither_toggle(int x, int y) { | 185 static inline int init_dither_toggle(int x, int y) { |
228 x &= 1; | 186 x &= 1; |
229 y = (y & 1) << 1; | 187 y = (y & 1) << 1; |
230 return (x | y) * SkGradientShaderBase::kDitherStride32; | 188 return (x | y) * SkGradientShaderBase::kDitherStride32; |
231 } | 189 } |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
423 GrGLUniformManager::UniformHandle fColorStartUni; | 381 GrGLUniformManager::UniformHandle fColorStartUni; |
424 GrGLUniformManager::UniformHandle fColorMidUni; | 382 GrGLUniformManager::UniformHandle fColorMidUni; |
425 GrGLUniformManager::UniformHandle fColorEndUni; | 383 GrGLUniformManager::UniformHandle fColorEndUni; |
426 | 384 |
427 typedef GrGLEffect INHERITED; | 385 typedef GrGLEffect INHERITED; |
428 }; | 386 }; |
429 | 387 |
430 #endif | 388 #endif |
431 | 389 |
432 #endif | 390 #endif |
OLD | NEW |