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 | 8 |
9 #include "SkScalar.h" | 9 #include "SkScalar.h" |
10 #include "SkShader.h" | 10 #include "SkShader.h" |
(...skipping 24 matching lines...) Expand all Loading... |
35 | 35 |
36 void SkShader::flatten(SkWriteBuffer& buffer) const { | 36 void SkShader::flatten(SkWriteBuffer& buffer) const { |
37 this->INHERITED::flatten(buffer); | 37 this->INHERITED::flatten(buffer); |
38 bool hasLocalM = this->hasLocalMatrix(); | 38 bool hasLocalM = this->hasLocalMatrix(); |
39 buffer.writeBool(hasLocalM); | 39 buffer.writeBool(hasLocalM); |
40 if (hasLocalM) { | 40 if (hasLocalM) { |
41 buffer.writeMatrix(fLocalMatrix); | 41 buffer.writeMatrix(fLocalMatrix); |
42 } | 42 } |
43 } | 43 } |
44 | 44 |
45 bool SkShader::setContext(const SkBitmap& device, | 45 SkShader::Context::Context(const SkMatrix& matrix, uint8_t paintAlpha, |
46 const SkPaint& paint, | 46 SkBitmap::Config deviceConfig) |
47 const SkMatrix& matrix) { | 47 : fTotalInverse(matrix) |
| 48 , fPaintAlpha(paintAlpha) |
| 49 , fDeviceConfig(SkToU8(deviceConfig)) |
| 50 , fTotalInverseClass((uint8_t) ComputeMatrixClass(fTotalInverse)) |
| 51 , fExtraStorage(NULL) |
| 52 { |
| 53 // debugcode... |
| 54 fStorageAllocated = false; |
| 55 } |
| 56 |
| 57 SkShader::Context* SkShader::setContext(const SkBitmap& device, |
| 58 const SkPaint& paint, |
| 59 const SkMatrix& matrix) { |
48 SkASSERT(!this->setContextHasBeenCalled()); | 60 SkASSERT(!this->setContextHasBeenCalled()); |
49 | 61 |
50 const SkMatrix* m = &matrix; | 62 const SkMatrix* m = &matrix; |
51 SkMatrix total; | 63 SkMatrix total; |
52 | 64 |
53 fDeviceConfig = SkToU8(device.config()); | 65 // FIXME: Store localMatrix on paint. |
54 fPaintAlpha = paint.getAlpha(); | |
55 if (this->hasLocalMatrix()) { | 66 if (this->hasLocalMatrix()) { |
56 total.setConcat(matrix, this->getLocalMatrix()); | 67 total.setConcat(matrix, this->getLocalMatrix()); |
57 m = &total; | 68 m = &total; |
58 } | 69 } |
59 if (m->invert(&fTotalInverse)) { | 70 SkMatrix totalInverse; |
60 fTotalInverseClass = (uint8_t)ComputeMatrixClass(fTotalInverse); | 71 if (m->invert(&totalInverse)) { |
| 72 Context* context = SkNEW_ARGS(Context, (totalInverse, paint.getAlpha(),
device.config())); |
| 73 // Initialize the subclass storage space. |
| 74 context->reset(this); |
| 75 // Now call into subclasses. |
| 76 if (!this->onSetContext(context, device, paint, matrix)) { |
| 77 SkDELETE(context); |
| 78 return NULL; |
| 79 } |
61 SkDEBUGCODE(fInSetContext = true;) | 80 SkDEBUGCODE(fInSetContext = true;) |
62 return true; | 81 return context; |
63 } | 82 } |
64 return false; | 83 return NULL; |
65 } | 84 } |
66 | 85 |
67 void SkShader::endContext() { | 86 void SkShader::endContext(Context* c) { |
68 SkASSERT(fInSetContext); | 87 SkASSERT(fInSetContext); |
| 88 SkDELETE(c); |
69 SkDEBUGCODE(fInSetContext = false;) | 89 SkDEBUGCODE(fInSetContext = false;) |
70 } | 90 } |
71 | 91 |
72 SkShader::ShadeProc SkShader::asAShadeProc(void** ctx) { | 92 SkShader::ShadeProc SkShader::asAShadeProc(void** ctx) { |
73 return NULL; | 93 return NULL; |
74 } | 94 } |
75 | 95 |
76 #include "SkColorPriv.h" | 96 #include "SkColorPriv.h" |
77 | 97 |
78 void SkShader::shadeSpan16(int x, int y, uint16_t span16[], int count) { | 98 void SkShader::shadeSpan16(Context* c, int x, int y, uint16_t span16[], int coun
t) { |
79 SkASSERT(span16); | 99 SkASSERT(span16); |
80 SkASSERT(count > 0); | 100 SkASSERT(count > 0); |
81 SkASSERT(this->canCallShadeSpan16()); | 101 SkASSERT(this->canCallShadeSpan16()); |
82 | 102 |
83 // basically, if we get here, the subclass screwed up | 103 // basically, if we get here, the subclass screwed up |
84 SkDEBUGFAIL("kHasSpan16 flag is set, but shadeSpan16() not implemented"); | 104 SkDEBUGFAIL("kHasSpan16 flag is set, but shadeSpan16() not implemented"); |
85 } | 105 } |
86 | 106 |
87 #define kTempColorQuadCount 6 // balance between speed (larger) and saving sta
ck-space | 107 #define kTempColorQuadCount 6 // balance between speed (larger) and saving sta
ck-space |
88 #define kTempColorCount (kTempColorQuadCount << 2) | 108 #define kTempColorCount (kTempColorQuadCount << 2) |
89 | 109 |
90 #ifdef SK_CPU_BENDIAN | 110 #ifdef SK_CPU_BENDIAN |
91 #define SkU32BitShiftToByteOffset(shift) (3 - ((shift) >> 3)) | 111 #define SkU32BitShiftToByteOffset(shift) (3 - ((shift) >> 3)) |
92 #else | 112 #else |
93 #define SkU32BitShiftToByteOffset(shift) ((shift) >> 3) | 113 #define SkU32BitShiftToByteOffset(shift) ((shift) >> 3) |
94 #endif | 114 #endif |
95 | 115 |
96 void SkShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) { | 116 void SkShader::shadeSpanAlpha(Context* context, int x, int y, uint8_t alpha[], i
nt count) { |
97 SkASSERT(count > 0); | 117 SkASSERT(count > 0); |
98 | 118 |
99 SkPMColor colors[kTempColorCount]; | 119 SkPMColor colors[kTempColorCount]; |
100 | 120 |
101 while ((count -= kTempColorCount) >= 0) { | 121 while ((count -= kTempColorCount) >= 0) { |
102 this->shadeSpan(x, y, colors, kTempColorCount); | 122 this->shadeSpan(context, x, y, colors, kTempColorCount); |
103 x += kTempColorCount; | 123 x += kTempColorCount; |
104 | 124 |
105 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset
(SK_A32_SHIFT); | 125 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset
(SK_A32_SHIFT); |
106 int quads = kTempColorQuadCount; | 126 int quads = kTempColorQuadCount; |
107 do { | 127 do { |
108 U8CPU a0 = srcA[0]; | 128 U8CPU a0 = srcA[0]; |
109 U8CPU a1 = srcA[4]; | 129 U8CPU a1 = srcA[4]; |
110 U8CPU a2 = srcA[8]; | 130 U8CPU a2 = srcA[8]; |
111 U8CPU a3 = srcA[12]; | 131 U8CPU a3 = srcA[12]; |
112 srcA += 4*4; | 132 srcA += 4*4; |
113 *alpha++ = SkToU8(a0); | 133 *alpha++ = SkToU8(a0); |
114 *alpha++ = SkToU8(a1); | 134 *alpha++ = SkToU8(a1); |
115 *alpha++ = SkToU8(a2); | 135 *alpha++ = SkToU8(a2); |
116 *alpha++ = SkToU8(a3); | 136 *alpha++ = SkToU8(a3); |
117 } while (--quads != 0); | 137 } while (--quads != 0); |
118 } | 138 } |
119 SkASSERT(count < 0); | 139 SkASSERT(count < 0); |
120 SkASSERT(count + kTempColorCount >= 0); | 140 SkASSERT(count + kTempColorCount >= 0); |
121 if (count += kTempColorCount) { | 141 if (count += kTempColorCount) { |
122 this->shadeSpan(x, y, colors, count); | 142 this->shadeSpan(context, x, y, colors, count); |
123 | 143 |
124 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset
(SK_A32_SHIFT); | 144 const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset
(SK_A32_SHIFT); |
125 do { | 145 do { |
126 *alpha++ = *srcA; | 146 *alpha++ = *srcA; |
127 srcA += 4; | 147 srcA += 4; |
128 } while (--count != 0); | 148 } while (--count != 0); |
129 } | 149 } |
130 #if 0 | 150 #if 0 |
131 do { | 151 do { |
132 int n = count; | 152 int n = count; |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 if (fInheritColor) { | 250 if (fInheritColor) { |
231 return; | 251 return; |
232 } | 252 } |
233 buffer.writeColor(fColor); | 253 buffer.writeColor(fColor); |
234 } | 254 } |
235 | 255 |
236 uint32_t SkColorShader::getFlags() { | 256 uint32_t SkColorShader::getFlags() { |
237 return fFlags; | 257 return fFlags; |
238 } | 258 } |
239 | 259 |
240 uint8_t SkColorShader::getSpan16Alpha() const { | 260 uint8_t SkColorShader::getSpan16Alpha(Context* c) const { |
241 return SkGetPackedA32(fPMColor); | 261 return SkGetPackedA32(fPMColor); |
242 } | 262 } |
243 | 263 |
244 bool SkColorShader::setContext(const SkBitmap& device, const SkPaint& paint, | 264 bool SkColorShader::onSetContext(Context* context, const SkBitmap& device, |
245 const SkMatrix& matrix) { | 265 const SkPaint& paint, const SkMatrix& matrix) { |
246 if (!this->INHERITED::setContext(device, paint, matrix)) { | 266 if (!this->INHERITED::onSetContext(context, device, paint, matrix)) { |
247 return false; | 267 return false; |
248 } | 268 } |
249 | 269 |
250 unsigned a; | 270 unsigned a; |
251 | 271 |
252 if (fInheritColor) { | 272 if (fInheritColor) { |
253 fColor = paint.getColor(); | 273 fColor = paint.getColor(); |
254 a = SkColorGetA(fColor); | 274 a = SkColorGetA(fColor); |
255 } else { | 275 } else { |
256 a = SkAlphaMul(SkColorGetA(fColor), SkAlpha255To256(paint.getAlpha())); | 276 a = SkAlphaMul(SkColorGetA(fColor), SkAlpha255To256(paint.getAlpha())); |
(...skipping 17 matching lines...) Expand all Loading... |
274 if (255 == a) { | 294 if (255 == a) { |
275 fFlags |= kOpaqueAlpha_Flag; | 295 fFlags |= kOpaqueAlpha_Flag; |
276 if (paint.isDither() == false) { | 296 if (paint.isDither() == false) { |
277 fFlags |= kHasSpan16_Flag; | 297 fFlags |= kHasSpan16_Flag; |
278 } | 298 } |
279 } | 299 } |
280 | 300 |
281 return true; | 301 return true; |
282 } | 302 } |
283 | 303 |
284 void SkColorShader::shadeSpan(int x, int y, SkPMColor span[], int count) { | 304 void SkColorShader::shadeSpan(Context* c, int x, int y, SkPMColor span[], int co
unt) { |
285 sk_memset32(span, fPMColor, count); | 305 sk_memset32(span, fPMColor, count); |
286 } | 306 } |
287 | 307 |
288 void SkColorShader::shadeSpan16(int x, int y, uint16_t span[], int count) { | 308 void SkColorShader::shadeSpan16(Context* c, int x, int y, uint16_t span[], int c
ount) { |
289 sk_memset16(span, fColor16, count); | 309 sk_memset16(span, fColor16, count); |
290 } | 310 } |
291 | 311 |
292 void SkColorShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) { | 312 void SkColorShader::shadeSpanAlpha(Context* c, int x, int y, uint8_t alpha[], in
t count) { |
293 memset(alpha, SkGetPackedA32(fPMColor), count); | 313 memset(alpha, SkGetPackedA32(fPMColor), count); |
294 } | 314 } |
295 | 315 |
296 // if we had a asAColor method, that would be more efficient... | 316 // if we had a asAColor method, that would be more efficient... |
297 SkShader::BitmapType SkColorShader::asABitmap(SkBitmap* bitmap, SkMatrix* matrix
, | 317 SkShader::BitmapType SkColorShader::asABitmap(SkBitmap* bitmap, SkMatrix* matrix
, |
298 TileMode modes[]) const { | 318 TileMode modes[]) const { |
299 return kNone_BitmapType; | 319 return kNone_BitmapType; |
300 } | 320 } |
301 | 321 |
302 SkShader::GradientType SkColorShader::asAGradient(GradientInfo* info) const { | 322 SkShader::GradientType SkColorShader::asAGradient(GradientInfo* info) const { |
(...skipping 22 matching lines...) Expand all Loading... |
325 | 345 |
326 str->append(")"); | 346 str->append(")"); |
327 } | 347 } |
328 #endif | 348 #endif |
329 | 349 |
330 /////////////////////////////////////////////////////////////////////////////// | 350 /////////////////////////////////////////////////////////////////////////////// |
331 | 351 |
332 #include "SkEmptyShader.h" | 352 #include "SkEmptyShader.h" |
333 | 353 |
334 uint32_t SkEmptyShader::getFlags() { return 0; } | 354 uint32_t SkEmptyShader::getFlags() { return 0; } |
335 uint8_t SkEmptyShader::getSpan16Alpha() const { return 0; } | 355 uint8_t SkEmptyShader::getSpan16Alpha(Context* c) const { return 0; } |
336 | 356 |
337 bool SkEmptyShader::setContext(const SkBitmap&, const SkPaint&, | 357 bool SkEmptyShader::onSetContext(Context*, const SkBitmap&, const SkPaint&, |
338 const SkMatrix&) { return false; } | 358 const SkMatrix&) { return false; } |
339 | 359 |
340 void SkEmptyShader::shadeSpan(int x, int y, SkPMColor span[], int count) { | 360 void SkEmptyShader::shadeSpan(Context *c, int x, int y, SkPMColor span[], int co
unt) { |
341 SkDEBUGFAIL("should never get called, since setContext() returned false"); | 361 SkDEBUGFAIL("should never get called, since setContext() returned false"); |
342 } | 362 } |
343 | 363 |
344 void SkEmptyShader::shadeSpan16(int x, int y, uint16_t span[], int count) { | 364 void SkEmptyShader::shadeSpan16(Context *c, int x, int y, uint16_t span[], int c
ount) { |
345 SkDEBUGFAIL("should never get called, since setContext() returned false"); | 365 SkDEBUGFAIL("should never get called, since setContext() returned false"); |
346 } | 366 } |
347 | 367 |
348 void SkEmptyShader::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) { | 368 void SkEmptyShader::shadeSpanAlpha(Context *c, int x, int y, uint8_t alpha[], in
t count) { |
349 SkDEBUGFAIL("should never get called, since setContext() returned false"); | 369 SkDEBUGFAIL("should never get called, since setContext() returned false"); |
350 } | 370 } |
351 | 371 |
352 #ifdef SK_DEVELOPER | 372 #ifdef SK_DEVELOPER |
353 void SkEmptyShader::toString(SkString* str) const { | 373 void SkEmptyShader::toString(SkString* str) const { |
354 str->append("SkEmptyShader: ("); | 374 str->append("SkEmptyShader: ("); |
355 | 375 |
356 this->INHERITED::toString(str); | 376 this->INHERITED::toString(str); |
357 | 377 |
358 str->append(")"); | 378 str->append(")"); |
359 } | 379 } |
360 #endif | 380 #endif |
OLD | NEW |