OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #include "SkTransparentShader.h" | 10 #include "SkTransparentShader.h" |
11 #include "SkColorPriv.h" | 11 #include "SkColorPriv.h" |
12 #include "SkString.h" | 12 #include "SkString.h" |
13 | 13 |
14 SkShader::Context* SkTransparentShader::createContext(const SkBitmap& device, | 14 bool SkTransparentShader::setContext(const SkBitmap& device, |
15 const SkPaint& paint, | 15 const SkPaint& paint, |
16 const SkMatrix& matrix, | 16 const SkMatrix& matrix) { |
17 void* storage) const { | 17 fDevice = &device; |
18 if (!this->validContext(device, paint, matrix)) { | 18 fAlpha = paint.getAlpha(); |
19 return NULL; | |
20 } | |
21 | 19 |
22 return SkNEW_PLACEMENT_ARGS(storage, TransparentShaderContext, (*this, devic
e, paint, matrix)); | 20 return this->INHERITED::setContext(device, paint, matrix); |
23 } | 21 } |
24 | 22 |
25 size_t SkTransparentShader::contextSize() const { | 23 uint32_t SkTransparentShader::getFlags() { |
26 return sizeof(TransparentShaderContext); | |
27 } | |
28 | |
29 SkTransparentShader::TransparentShaderContext::TransparentShaderContext( | |
30 const SkTransparentShader& shader, const SkBitmap& device, | |
31 const SkPaint& paint, const SkMatrix& matrix) | |
32 : INHERITED(shader, device, paint, matrix) | |
33 , fDevice(&device) {} | |
34 | |
35 SkTransparentShader::TransparentShaderContext::~TransparentShaderContext() {} | |
36 | |
37 uint32_t SkTransparentShader::TransparentShaderContext::getFlags() const { | |
38 uint32_t flags = this->INHERITED::getFlags(); | 24 uint32_t flags = this->INHERITED::getFlags(); |
39 | 25 |
40 switch (fDevice->colorType()) { | 26 switch (fDevice->colorType()) { |
41 case kRGB_565_SkColorType: | 27 case kRGB_565_SkColorType: |
42 flags |= kHasSpan16_Flag; | 28 flags |= kHasSpan16_Flag; |
43 if (this->getPaintAlpha() == 255) | 29 if (fAlpha == 255) |
44 flags |= kOpaqueAlpha_Flag; | 30 flags |= kOpaqueAlpha_Flag; |
45 break; | 31 break; |
46 case kN32_SkColorType: | 32 case kN32_SkColorType: |
47 if (this->getPaintAlpha() == 255 && fDevice->isOpaque()) | 33 if (fAlpha == 255 && fDevice->isOpaque()) |
48 flags |= kOpaqueAlpha_Flag; | 34 flags |= kOpaqueAlpha_Flag; |
49 break; | 35 break; |
50 default: | 36 default: |
51 break; | 37 break; |
52 } | 38 } |
53 return flags; | 39 return flags; |
54 } | 40 } |
55 | 41 |
56 void SkTransparentShader::TransparentShaderContext::shadeSpan(int x, int y, SkPM
Color span[], | 42 void SkTransparentShader::shadeSpan(int x, int y, SkPMColor span[], int count) { |
57 int count) { | 43 unsigned scale = SkAlpha255To256(fAlpha); |
58 unsigned scale = SkAlpha255To256(this->getPaintAlpha()); | |
59 | 44 |
60 switch (fDevice->colorType()) { | 45 switch (fDevice->colorType()) { |
61 case kN32_SkColorType: | 46 case kN32_SkColorType: |
62 if (scale == 256) { | 47 if (scale == 256) { |
63 SkPMColor* src = fDevice->getAddr32(x, y); | 48 SkPMColor* src = fDevice->getAddr32(x, y); |
64 if (src != span) { | 49 if (src != span) { |
65 memcpy(span, src, count * sizeof(SkPMColor)); | 50 memcpy(span, src, count * sizeof(SkPMColor)); |
66 } | 51 } |
67 } else { | 52 } else { |
68 const SkPMColor* src = fDevice->getAddr32(x, y); | 53 const SkPMColor* src = fDevice->getAddr32(x, y); |
69 for (int i = count - 1; i >= 0; --i) { | 54 for (int i = count - 1; i >= 0; --i) { |
70 span[i] = SkAlphaMulQ(src[i], scale); | 55 span[i] = SkAlphaMulQ(src[i], scale); |
71 } | 56 } |
72 } | 57 } |
73 break; | 58 break; |
74 case kRGB_565_SkColorType: { | 59 case kRGB_565_SkColorType: { |
75 const uint16_t* src = fDevice->getAddr16(x, y); | 60 const uint16_t* src = fDevice->getAddr16(x, y); |
76 if (scale == 256) { | 61 if (scale == 256) { |
77 for (int i = count - 1; i >= 0; --i) { | 62 for (int i = count - 1; i >= 0; --i) { |
78 span[i] = SkPixel16ToPixel32(src[i]); | 63 span[i] = SkPixel16ToPixel32(src[i]); |
79 } | 64 } |
80 } else { | 65 } else { |
81 unsigned alpha = this->getPaintAlpha(); | 66 unsigned alpha = fAlpha; |
82 for (int i = count - 1; i >= 0; --i) { | 67 for (int i = count - 1; i >= 0; --i) { |
83 uint16_t c = src[i]; | 68 uint16_t c = src[i]; |
84 unsigned r = SkPacked16ToR32(c); | 69 unsigned r = SkPacked16ToR32(c); |
85 unsigned g = SkPacked16ToG32(c); | 70 unsigned g = SkPacked16ToG32(c); |
86 unsigned b = SkPacked16ToB32(c); | 71 unsigned b = SkPacked16ToB32(c); |
87 | 72 |
88 span[i] = SkPackARGB32( alpha, | 73 span[i] = SkPackARGB32( alpha, |
89 SkAlphaMul(r, scale), | 74 SkAlphaMul(r, scale), |
90 SkAlphaMul(g, scale), | 75 SkAlphaMul(g, scale), |
91 SkAlphaMul(b, scale)); | 76 SkAlphaMul(b, scale)); |
(...skipping 13 matching lines...) Expand all Loading... |
105 } | 90 } |
106 } | 91 } |
107 break; | 92 break; |
108 } | 93 } |
109 default: | 94 default: |
110 SkDEBUGFAIL("colorType not supported as a destination device"); | 95 SkDEBUGFAIL("colorType not supported as a destination device"); |
111 break; | 96 break; |
112 } | 97 } |
113 } | 98 } |
114 | 99 |
115 void SkTransparentShader::TransparentShaderContext::shadeSpan16(int x, int y, ui
nt16_t span[], | 100 void SkTransparentShader::shadeSpan16(int x, int y, uint16_t span[], int count)
{ |
116 int count) { | |
117 SkASSERT(fDevice->colorType() == kRGB_565_SkColorType); | 101 SkASSERT(fDevice->colorType() == kRGB_565_SkColorType); |
118 | 102 |
119 uint16_t* src = fDevice->getAddr16(x, y); | 103 uint16_t* src = fDevice->getAddr16(x, y); |
120 if (src != span) { | 104 if (src != span) { |
121 memcpy(span, src, count << 1); | 105 memcpy(span, src, count << 1); |
122 } | 106 } |
123 } | 107 } |
124 | 108 |
125 #ifndef SK_IGNORE_TO_STRING | 109 #ifndef SK_IGNORE_TO_STRING |
126 void SkTransparentShader::toString(SkString* str) const { | 110 void SkTransparentShader::toString(SkString* str) const { |
127 str->append("SkTransparentShader: ("); | 111 str->append("SkTransparentShader: ("); |
128 | 112 |
129 this->INHERITED::toString(str); | 113 this->INHERITED::toString(str); |
130 | 114 |
131 str->append(")"); | 115 str->append(")"); |
132 } | 116 } |
133 #endif | 117 #endif |
OLD | NEW |