OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2006 The Android Open Source Project | 2 * Copyright 2006 The Android Open Source Project |
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 #include "SkBitmapProcShader.h" | 8 #include "SkBitmapProcShader.h" |
9 #include "SkReadBuffer.h" | 9 #include "SkReadBuffer.h" |
10 #include "SkMallocPixelRef.h" | 10 #include "SkMallocPixelRef.h" |
11 #include "SkPaint.h" | 11 #include "SkPaint.h" |
12 #include "SkPicture.h" | 12 #include "SkPicture.h" |
13 #include "SkPictureShader.h" | 13 #include "SkPictureShader.h" |
14 #include "SkScalar.h" | 14 #include "SkScalar.h" |
15 #include "SkShader.h" | 15 #include "SkShader.h" |
16 #include "SkWriteBuffer.h" | 16 #include "SkWriteBuffer.h" |
17 | 17 |
18 SkShader::SkShader() { | 18 SkShader::SkShader() { |
19 fLocalMatrix.reset(); | 19 fLocalMatrix.reset(); |
| 20 SkDEBUGCODE(fInSetContext = false;) |
20 } | 21 } |
21 | 22 |
22 SkShader::SkShader(SkReadBuffer& buffer) | 23 SkShader::SkShader(SkReadBuffer& buffer) |
23 : INHERITED(buffer) { | 24 : INHERITED(buffer) { |
24 if (buffer.readBool()) { | 25 if (buffer.readBool()) { |
25 buffer.readMatrix(&fLocalMatrix); | 26 buffer.readMatrix(&fLocalMatrix); |
26 } else { | 27 } else { |
27 fLocalMatrix.reset(); | 28 fLocalMatrix.reset(); |
28 } | 29 } |
| 30 |
| 31 SkDEBUGCODE(fInSetContext = false;) |
29 } | 32 } |
30 | 33 |
31 SkShader::~SkShader() { | 34 SkShader::~SkShader() { |
| 35 SkASSERT(!fInSetContext); |
32 } | 36 } |
33 | 37 |
34 void SkShader::flatten(SkWriteBuffer& buffer) const { | 38 void SkShader::flatten(SkWriteBuffer& buffer) const { |
35 this->INHERITED::flatten(buffer); | 39 this->INHERITED::flatten(buffer); |
36 bool hasLocalM = this->hasLocalMatrix(); | 40 bool hasLocalM = this->hasLocalMatrix(); |
37 buffer.writeBool(hasLocalM); | 41 buffer.writeBool(hasLocalM); |
38 if (hasLocalM) { | 42 if (hasLocalM) { |
39 buffer.writeMatrix(fLocalMatrix); | 43 buffer.writeMatrix(fLocalMatrix); |
40 } | 44 } |
41 } | 45 } |
42 | 46 |
43 bool SkShader::computeTotalInverse(const SkMatrix& matrix, SkMatrix* totalInvers
e) const { | 47 bool SkShader::setContext(const SkBitmap& device, |
| 48 const SkPaint& paint, |
| 49 const SkMatrix& matrix) { |
| 50 SkASSERT(!this->setContextHasBeenCalled()); |
| 51 |
44 const SkMatrix* m = &matrix; | 52 const SkMatrix* m = &matrix; |
45 SkMatrix total; | 53 SkMatrix total; |
46 | 54 |
| 55 fPaintAlpha = paint.getAlpha(); |
47 if (this->hasLocalMatrix()) { | 56 if (this->hasLocalMatrix()) { |
48 total.setConcat(matrix, this->getLocalMatrix()); | 57 total.setConcat(matrix, this->getLocalMatrix()); |
49 m = &total; | 58 m = &total; |
50 } | 59 } |
51 | 60 if (m->invert(&fTotalInverse)) { |
52 return m->invert(totalInverse); | 61 fTotalInverseClass = (uint8_t)ComputeMatrixClass(fTotalInverse); |
| 62 SkDEBUGCODE(fInSetContext = true;) |
| 63 return true; |
| 64 } |
| 65 return false; |
53 } | 66 } |
54 | 67 |
55 bool SkShader::validContext(const SkBitmap& device, | 68 void SkShader::endContext() { |
56 const SkPaint& paint, | 69 SkASSERT(fInSetContext); |
57 const SkMatrix& matrix, | 70 SkDEBUGCODE(fInSetContext = false;) |
58 SkMatrix* totalInverse) const { | |
59 return this->computeTotalInverse(matrix, totalInverse); | |
60 } | 71 } |
61 | 72 |
62 SkShader::Context::Context(const SkShader& shader, const SkBitmap& device, | 73 SkShader::ShadeProc SkShader::asAShadeProc(void** ctx) { |
63 const SkPaint& paint, const SkMatrix& matrix) | |
64 : fShader(shader) | |
65 { | |
66 SkASSERT(fShader.validContext(device, paint, matrix)); | |
67 | |
68 // Because the context parameters must be valid at this point, we know that
the matrix is | |
69 // invertible. | |
70 SkAssertResult(fShader.computeTotalInverse(matrix, &fTotalInverse)); | |
71 fTotalInverseClass = (uint8_t)ComputeMatrixClass(fTotalInverse); | |
72 | |
73 fPaintAlpha = paint.getAlpha(); | |
74 } | |
75 | |
76 SkShader::Context::~Context() {} | |
77 | |
78 SkShader::Context::ShadeProc SkShader::Context::asAShadeProc(void** ctx) { | |
79 return NULL; | 74 return NULL; |
80 } | 75 } |
81 | 76 |
82 #include "SkColorPriv.h" | 77 #include "SkColorPriv.h" |
83 | 78 |
84 void SkShader::Context::shadeSpan16(int x, int y, uint16_t span16[], int count)
{ | 79 void SkShader::shadeSpan16(int x, int y, uint16_t span16[], int count) { |
85 SkASSERT(span16); | 80 SkASSERT(span16); |
86 SkASSERT(count > 0); | 81 SkASSERT(count > 0); |
87 SkASSERT(this->canCallShadeSpan16()); | 82 SkASSERT(this->canCallShadeSpan16()); |
88 | 83 |
89 // basically, if we get here, the subclass screwed up | 84 // basically, if we get here, the subclass screwed up |
90 SkDEBUGFAIL("kHasSpan16 flag is set, but shadeSpan16() not implemented"); | 85 SkDEBUGFAIL("kHasSpan16 flag is set, but shadeSpan16() not implemented"); |
91 } | 86 } |
92 | 87 |
93 #define kTempColorQuadCount 6 // balance between speed (larger) and saving sta
ck-space | 88 #define kTempColorQuadCount 6 // balance between speed (larger) and saving sta
ck-space |
94 #define kTempColorCount (kTempColorQuadCount << 2) | 89 #define kTempColorCount (kTempColorQuadCount << 2) |
95 | 90 |
96 #ifdef SK_CPU_BENDIAN | 91 #ifdef SK_CPU_BENDIAN |
97 #define SkU32BitShiftToByteOffset(shift) (3 - ((shift) >> 3)) | 92 #define SkU32BitShiftToByteOffset(shift) (3 - ((shift) >> 3)) |
98 #else | 93 #else |
99 #define SkU32BitShiftToByteOffset(shift) ((shift) >> 3) | 94 #define SkU32BitShiftToByteOffset(shift) ((shift) >> 3) |
100 #endif | 95 #endif |
101 | 96 |
102 void SkShader::Context::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count)
{ | 97 void SkShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) { |
103 SkASSERT(count > 0); | 98 SkASSERT(count > 0); |
104 | 99 |
105 SkPMColor colors[kTempColorCount]; | 100 SkPMColor colors[kTempColorCount]; |
106 | 101 |
107 while ((count -= kTempColorCount) >= 0) { | 102 while ((count -= kTempColorCount) >= 0) { |
108 this->shadeSpan(x, y, colors, kTempColorCount); | 103 this->shadeSpan(x, y, colors, kTempColorCount); |
109 x += kTempColorCount; | 104 x += kTempColorCount; |
110 | 105 |
111 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset
(SK_A32_SHIFT); | 106 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset
(SK_A32_SHIFT); |
112 int quads = kTempColorQuadCount; | 107 int quads = kTempColorQuadCount; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 | 141 |
147 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset
(SK_A32_SHIFT); | 142 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset
(SK_A32_SHIFT); |
148 do { | 143 do { |
149 *alpha++ = *srcA; | 144 *alpha++ = *srcA; |
150 srcA += 4; | 145 srcA += 4; |
151 } while (--n != 0); | 146 } while (--n != 0); |
152 } while (count > 0); | 147 } while (count > 0); |
153 #endif | 148 #endif |
154 } | 149 } |
155 | 150 |
156 SkShader::Context::MatrixClass SkShader::Context::ComputeMatrixClass(const SkMat
rix& mat) { | 151 SkShader::MatrixClass SkShader::ComputeMatrixClass(const SkMatrix& mat) { |
157 MatrixClass mc = kLinear_MatrixClass; | 152 MatrixClass mc = kLinear_MatrixClass; |
158 | 153 |
159 if (mat.hasPerspective()) { | 154 if (mat.hasPerspective()) { |
160 if (mat.fixedStepInX(0, NULL, NULL)) { | 155 if (mat.fixedStepInX(0, NULL, NULL)) { |
161 mc = kFixedStepInX_MatrixClass; | 156 mc = kFixedStepInX_MatrixClass; |
162 } else { | 157 } else { |
163 mc = kPerspective_MatrixClass; | 158 mc = kPerspective_MatrixClass; |
164 } | 159 } |
165 } | 160 } |
166 return mc; | 161 return mc; |
167 } | 162 } |
168 | 163 |
169 ////////////////////////////////////////////////////////////////////////////// | 164 ////////////////////////////////////////////////////////////////////////////// |
170 | 165 |
171 SkShader::BitmapType SkShader::asABitmap(SkBitmap*, SkMatrix*, TileMode*) const
{ | 166 SkShader::BitmapType SkShader::asABitmap(SkBitmap*, SkMatrix*, |
| 167 TileMode*) const { |
172 return kNone_BitmapType; | 168 return kNone_BitmapType; |
173 } | 169 } |
174 | 170 |
175 SkShader::GradientType SkShader::asAGradient(GradientInfo* info) const { | 171 SkShader::GradientType SkShader::asAGradient(GradientInfo* info) const { |
176 return kNone_GradientType; | 172 return kNone_GradientType; |
177 } | 173 } |
178 | 174 |
179 GrEffectRef* SkShader::asNewEffect(GrContext*, const SkPaint&) const { | 175 GrEffectRef* SkShader::asNewEffect(GrContext*, const SkPaint&) const { |
180 return NULL; | 176 return NULL; |
181 } | 177 } |
(...skipping 14 matching lines...) Expand all Loading... |
196 this->getLocalMatrix().toString(str); | 192 this->getLocalMatrix().toString(str); |
197 } | 193 } |
198 } | 194 } |
199 #endif | 195 #endif |
200 | 196 |
201 ////////////////////////////////////////////////////////////////////////////// | 197 ////////////////////////////////////////////////////////////////////////////// |
202 | 198 |
203 #include "SkColorShader.h" | 199 #include "SkColorShader.h" |
204 #include "SkUtils.h" | 200 #include "SkUtils.h" |
205 | 201 |
206 SkColorShader::SkColorShader() | 202 SkColorShader::SkColorShader() { |
207 : fColor() | 203 fFlags = 0; |
208 , fInheritColor(true) { | 204 fInheritColor = true; |
209 } | 205 } |
210 | 206 |
211 SkColorShader::SkColorShader(SkColor c) | 207 SkColorShader::SkColorShader(SkColor c) { |
212 : fColor(c) | 208 fFlags = 0; |
213 , fInheritColor(false) { | 209 fColor = c; |
| 210 fInheritColor = false; |
214 } | 211 } |
215 | 212 |
| 213 SkColorShader::~SkColorShader() {} |
| 214 |
216 bool SkColorShader::isOpaque() const { | 215 bool SkColorShader::isOpaque() const { |
217 if (fInheritColor) { | 216 if (fInheritColor) { |
218 return true; // using paint's alpha | 217 return true; // using paint's alpha |
219 } | 218 } |
220 return SkColorGetA(fColor) == 255; | 219 return SkColorGetA(fColor) == 255; |
221 } | 220 } |
222 | 221 |
223 SkColorShader::SkColorShader(SkReadBuffer& b) : INHERITED(b) { | 222 SkColorShader::SkColorShader(SkReadBuffer& b) : INHERITED(b) { |
| 223 fFlags = 0; // computed in setContext |
| 224 |
224 fInheritColor = b.readBool(); | 225 fInheritColor = b.readBool(); |
225 if (fInheritColor) { | 226 if (fInheritColor) { |
226 return; | 227 return; |
227 } | 228 } |
228 fColor = b.readColor(); | 229 fColor = b.readColor(); |
229 } | 230 } |
230 | 231 |
231 void SkColorShader::flatten(SkWriteBuffer& buffer) const { | 232 void SkColorShader::flatten(SkWriteBuffer& buffer) const { |
232 this->INHERITED::flatten(buffer); | 233 this->INHERITED::flatten(buffer); |
233 buffer.writeBool(fInheritColor); | 234 buffer.writeBool(fInheritColor); |
234 if (fInheritColor) { | 235 if (fInheritColor) { |
235 return; | 236 return; |
236 } | 237 } |
237 buffer.writeColor(fColor); | 238 buffer.writeColor(fColor); |
238 } | 239 } |
239 | 240 |
240 uint32_t SkColorShader::ColorShaderContext::getFlags() const { | 241 uint32_t SkColorShader::getFlags() { |
241 return fFlags; | 242 return fFlags; |
242 } | 243 } |
243 | 244 |
244 uint8_t SkColorShader::ColorShaderContext::getSpan16Alpha() const { | 245 uint8_t SkColorShader::getSpan16Alpha() const { |
245 return SkGetPackedA32(fPMColor); | 246 return SkGetPackedA32(fPMColor); |
246 } | 247 } |
247 | 248 |
248 SkShader::Context* SkColorShader::createContext(const SkBitmap& device, const Sk
Paint& paint, | 249 bool SkColorShader::setContext(const SkBitmap& device, const SkPaint& paint, |
249 const SkMatrix& matrix, void* st
orage) const { | 250 const SkMatrix& matrix) { |
250 if (!this->validContext(device, paint, matrix)) { | 251 if (!this->INHERITED::setContext(device, paint, matrix)) { |
251 return NULL; | 252 return false; |
252 } | 253 } |
253 | 254 |
254 return SkNEW_PLACEMENT_ARGS(storage, ColorShaderContext, (*this, device, pai
nt, matrix)); | |
255 } | |
256 | |
257 SkColorShader::ColorShaderContext::ColorShaderContext(const SkColorShader& shade
r, | |
258 const SkBitmap& device, | |
259 const SkPaint& paint, | |
260 const SkMatrix& matrix) | |
261 : INHERITED(shader, device, paint, matrix) | |
262 { | |
263 unsigned a; | 255 unsigned a; |
264 | 256 |
265 SkColor color; | 257 if (fInheritColor) { |
266 if (shader.fInheritColor) { | 258 fColor = paint.getColor(); |
267 color = paint.getColor(); | 259 a = SkColorGetA(fColor); |
268 a = SkColorGetA(color); | |
269 } else { | 260 } else { |
270 color = shader.fColor; | 261 a = SkAlphaMul(SkColorGetA(fColor), SkAlpha255To256(paint.getAlpha())); |
271 a = SkAlphaMul(SkColorGetA(color), SkAlpha255To256(paint.getAlpha())); | |
272 } | 262 } |
273 | 263 |
274 unsigned r = SkColorGetR(color); | 264 unsigned r = SkColorGetR(fColor); |
275 unsigned g = SkColorGetG(color); | 265 unsigned g = SkColorGetG(fColor); |
276 unsigned b = SkColorGetB(color); | 266 unsigned b = SkColorGetB(fColor); |
277 | 267 |
278 // we want this before we apply any alpha | 268 // we want this before we apply any alpha |
279 fColor16 = SkPack888ToRGB16(r, g, b); | 269 fColor16 = SkPack888ToRGB16(r, g, b); |
280 | 270 |
281 if (a != 255) { | 271 if (a != 255) { |
282 r = SkMulDiv255Round(r, a); | 272 r = SkMulDiv255Round(r, a); |
283 g = SkMulDiv255Round(g, a); | 273 g = SkMulDiv255Round(g, a); |
284 b = SkMulDiv255Round(b, a); | 274 b = SkMulDiv255Round(b, a); |
285 } | 275 } |
286 fPMColor = SkPackARGB32(a, r, g, b); | 276 fPMColor = SkPackARGB32(a, r, g, b); |
287 | 277 |
288 fFlags = kConstInY32_Flag; | 278 fFlags = kConstInY32_Flag; |
289 if (255 == a) { | 279 if (255 == a) { |
290 fFlags |= kOpaqueAlpha_Flag; | 280 fFlags |= kOpaqueAlpha_Flag; |
291 if (paint.isDither() == false) { | 281 if (paint.isDither() == false) { |
292 fFlags |= kHasSpan16_Flag; | 282 fFlags |= kHasSpan16_Flag; |
293 } | 283 } |
294 } | 284 } |
| 285 |
| 286 return true; |
295 } | 287 } |
296 | 288 |
297 void SkColorShader::ColorShaderContext::shadeSpan(int x, int y, SkPMColor span[]
, int count) { | 289 void SkColorShader::shadeSpan(int x, int y, SkPMColor span[], int count) { |
298 sk_memset32(span, fPMColor, count); | 290 sk_memset32(span, fPMColor, count); |
299 } | 291 } |
300 | 292 |
301 void SkColorShader::ColorShaderContext::shadeSpan16(int x, int y, uint16_t span[
], int count) { | 293 void SkColorShader::shadeSpan16(int x, int y, uint16_t span[], int count) { |
302 sk_memset16(span, fColor16, count); | 294 sk_memset16(span, fColor16, count); |
303 } | 295 } |
304 | 296 |
305 void SkColorShader::ColorShaderContext::shadeSpanAlpha(int x, int y, uint8_t alp
ha[], int count) { | 297 void SkColorShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) { |
306 memset(alpha, SkGetPackedA32(fPMColor), count); | 298 memset(alpha, SkGetPackedA32(fPMColor), count); |
307 } | 299 } |
308 | 300 |
309 // if we had a asAColor method, that would be more efficient... | 301 // if we had a asAColor method, that would be more efficient... |
310 SkShader::BitmapType SkColorShader::asABitmap(SkBitmap* bitmap, SkMatrix* matrix
, | 302 SkShader::BitmapType SkColorShader::asABitmap(SkBitmap* bitmap, SkMatrix* matrix
, |
311 TileMode modes[]) const { | 303 TileMode modes[]) const { |
312 return kNone_BitmapType; | 304 return kNone_BitmapType; |
313 } | 305 } |
314 | 306 |
315 SkShader::GradientType SkColorShader::asAGradient(GradientInfo* info) const { | 307 SkShader::GradientType SkColorShader::asAGradient(GradientInfo* info) const { |
(...skipping 19 matching lines...) Expand all Loading... |
335 } | 327 } |
336 | 328 |
337 this->INHERITED::toString(str); | 329 this->INHERITED::toString(str); |
338 | 330 |
339 str->append(")"); | 331 str->append(")"); |
340 } | 332 } |
341 #endif | 333 #endif |
342 | 334 |
343 /////////////////////////////////////////////////////////////////////////////// | 335 /////////////////////////////////////////////////////////////////////////////// |
344 | 336 |
345 #ifndef SK_IGNORE_TO_STRING | |
346 #include "SkEmptyShader.h" | 337 #include "SkEmptyShader.h" |
347 | 338 |
| 339 uint32_t SkEmptyShader::getFlags() { return 0; } |
| 340 uint8_t SkEmptyShader::getSpan16Alpha() const { return 0; } |
| 341 |
| 342 bool SkEmptyShader::setContext(const SkBitmap&, const SkPaint&, |
| 343 const SkMatrix&) { return false; } |
| 344 |
| 345 void SkEmptyShader::shadeSpan(int x, int y, SkPMColor span[], int count) { |
| 346 SkDEBUGFAIL("should never get called, since setContext() returned false"); |
| 347 } |
| 348 |
| 349 void SkEmptyShader::shadeSpan16(int x, int y, uint16_t span[], int count) { |
| 350 SkDEBUGFAIL("should never get called, since setContext() returned false"); |
| 351 } |
| 352 |
| 353 void SkEmptyShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) { |
| 354 SkDEBUGFAIL("should never get called, since setContext() returned false"); |
| 355 } |
| 356 |
| 357 #ifndef SK_IGNORE_TO_STRING |
348 void SkEmptyShader::toString(SkString* str) const { | 358 void SkEmptyShader::toString(SkString* str) const { |
349 str->append("SkEmptyShader: ("); | 359 str->append("SkEmptyShader: ("); |
350 | 360 |
351 this->INHERITED::toString(str); | 361 this->INHERITED::toString(str); |
352 | 362 |
353 str->append(")"); | 363 str->append(")"); |
354 } | 364 } |
355 #endif | 365 #endif |
OLD | NEW |