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