OLD | NEW |
---|---|
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
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 #include "SkColorPriv.h" | 8 #include "SkColorPriv.h" |
9 #include "SkReadBuffer.h" | 9 #include "SkReadBuffer.h" |
10 #include "SkWriteBuffer.h" | 10 #include "SkWriteBuffer.h" |
(...skipping 16 matching lines...) Expand all Loading... | |
27 return true; | 27 return true; |
28 default: | 28 default: |
29 break; | 29 break; |
30 } | 30 } |
31 return false; | 31 return false; |
32 } | 32 } |
33 | 33 |
34 SkBitmapProcShader::SkBitmapProcShader(const SkBitmap& src, | 34 SkBitmapProcShader::SkBitmapProcShader(const SkBitmap& src, |
35 TileMode tmx, TileMode tmy) { | 35 TileMode tmx, TileMode tmy) { |
36 fRawBitmap = src; | 36 fRawBitmap = src; |
37 fState.fTileModeX = (uint8_t)tmx; | 37 fTileModeX = (uint8_t)tmx; |
38 fState.fTileModeY = (uint8_t)tmy; | 38 fTileModeY = (uint8_t)tmy; |
39 fFlags = 0; // computed in setContext | |
40 } | 39 } |
41 | 40 |
42 SkBitmapProcShader::SkBitmapProcShader(SkReadBuffer& buffer) | 41 SkBitmapProcShader::SkBitmapProcShader(SkReadBuffer& buffer) |
43 : INHERITED(buffer) { | 42 : INHERITED(buffer) { |
44 buffer.readBitmap(&fRawBitmap); | 43 buffer.readBitmap(&fRawBitmap); |
45 fRawBitmap.setImmutable(); | 44 fRawBitmap.setImmutable(); |
46 fState.fTileModeX = buffer.readUInt(); | 45 fTileModeX = buffer.readUInt(); |
47 fState.fTileModeY = buffer.readUInt(); | 46 fTileModeY = buffer.readUInt(); |
48 fFlags = 0; // computed in setContext | |
49 } | 47 } |
50 | 48 |
51 SkShader::BitmapType SkBitmapProcShader::asABitmap(SkBitmap* texture, | 49 SkShader::BitmapType SkBitmapProcShader::asABitmap(SkBitmap* texture, |
52 SkMatrix* texM, | 50 SkMatrix* texM, |
53 TileMode xy[]) const { | 51 TileMode xy[]) const { |
54 if (texture) { | 52 if (texture) { |
55 *texture = fRawBitmap; | 53 *texture = fRawBitmap; |
56 } | 54 } |
57 if (texM) { | 55 if (texM) { |
58 texM->reset(); | 56 texM->reset(); |
59 } | 57 } |
60 if (xy) { | 58 if (xy) { |
61 xy[0] = (TileMode)fState.fTileModeX; | 59 xy[0] = (TileMode)fTileModeX; |
62 xy[1] = (TileMode)fState.fTileModeY; | 60 xy[1] = (TileMode)fTileModeY; |
63 } | 61 } |
64 return kDefault_BitmapType; | 62 return kDefault_BitmapType; |
65 } | 63 } |
66 | 64 |
67 void SkBitmapProcShader::flatten(SkWriteBuffer& buffer) const { | 65 void SkBitmapProcShader::flatten(SkWriteBuffer& buffer) const { |
68 this->INHERITED::flatten(buffer); | 66 this->INHERITED::flatten(buffer); |
69 | 67 |
70 buffer.writeBitmap(fRawBitmap); | 68 buffer.writeBitmap(fRawBitmap); |
71 buffer.writeUInt(fState.fTileModeX); | 69 buffer.writeUInt(fTileModeX); |
72 buffer.writeUInt(fState.fTileModeY); | 70 buffer.writeUInt(fTileModeY); |
73 } | 71 } |
74 | 72 |
75 static bool only_scale_and_translate(const SkMatrix& matrix) { | 73 static bool only_scale_and_translate(const SkMatrix& matrix) { |
76 unsigned mask = SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask; | 74 unsigned mask = SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask; |
77 return (matrix.getType() & ~mask) == 0; | 75 return (matrix.getType() & ~mask) == 0; |
78 } | 76 } |
79 | 77 |
80 bool SkBitmapProcShader::isOpaque() const { | 78 bool SkBitmapProcShader::isOpaque() const { |
81 return fRawBitmap.isOpaque(); | 79 return fRawBitmap.isOpaque(); |
82 } | 80 } |
83 | 81 |
84 static bool valid_for_drawing(const SkBitmap& bm) { | 82 static bool valid_for_drawing(const SkBitmap& bm) { |
85 if (0 == bm.width() || 0 == bm.height()) { | 83 if (0 == bm.width() || 0 == bm.height()) { |
86 return false; // nothing to draw | 84 return false; // nothing to draw |
87 } | 85 } |
88 if (NULL == bm.pixelRef()) { | 86 if (NULL == bm.pixelRef()) { |
89 return false; // no pixels to read | 87 return false; // no pixels to read |
90 } | 88 } |
91 if (kIndex_8_SkColorType == bm.colorType()) { | 89 if (kIndex_8_SkColorType == bm.colorType()) { |
92 // ugh, I have to lock-pixels to inspect the colortable | 90 // ugh, I have to lock-pixels to inspect the colortable |
93 SkAutoLockPixels alp(bm); | 91 SkAutoLockPixels alp(bm); |
94 if (!bm.getColorTable()) { | 92 if (!bm.getColorTable()) { |
95 return false; | 93 return false; |
96 } | 94 } |
97 } | 95 } |
98 return true; | 96 return true; |
99 } | 97 } |
100 | 98 |
101 bool SkBitmapProcShader::setContext(const SkBitmap& device, | 99 bool SkBitmapProcShader::validInternal(const SkBitmap& device, |
102 const SkPaint& paint, | 100 const SkPaint& paint, |
103 const SkMatrix& matrix) { | 101 const SkMatrix& matrix, |
102 SkMatrix* totalInverse, | |
103 SkBitmapProcState* state) const { | |
104 if (!fRawBitmap.getTexture() && !valid_for_drawing(fRawBitmap)) { | 104 if (!fRawBitmap.getTexture() && !valid_for_drawing(fRawBitmap)) { |
105 return false; | 105 return false; |
106 } | 106 } |
107 | 107 |
108 // do this first, so we have a correct inverse matrix | 108 // Make sure we can use totalInverse as a cache. |
109 if (!this->INHERITED::setContext(device, paint, matrix)) { | 109 SkMatrix totalInverseLocal; |
110 if (NULL == totalInverse) { | |
111 totalInverse = &totalInverseLocal; | |
112 } | |
113 | |
114 // Do this first, so we know the matrix can be inverted. | |
115 if (!this->INHERITED::validContext(device, paint, matrix, totalInverse)) { | |
110 return false; | 116 return false; |
111 } | 117 } |
112 | 118 |
113 fState.fOrigBitmap = fRawBitmap; | 119 SkASSERT(state); |
114 if (!fState.chooseProcs(this->getTotalInverse(), paint)) { | 120 state->fTileModeX = fTileModeX; |
115 this->INHERITED::endContext(); | 121 state->fTileModeY = fTileModeY; |
116 return false; | 122 state->fOrigBitmap = fRawBitmap; |
123 return state->chooseProcs(*totalInverse, paint); | |
124 } | |
125 | |
126 bool SkBitmapProcShader::validContext(const SkBitmap& device, | |
127 const SkPaint& paint, | |
128 const SkMatrix& matrix, | |
129 SkMatrix* totalInverse) const { | |
130 SkBitmapProcState state; | |
131 return this->validInternal(device, paint, matrix, totalInverse, &state); | |
132 } | |
133 | |
134 SkShader::Context* SkBitmapProcShader::createContext(const SkBitmap& device, con st SkPaint& paint, | |
135 const SkMatrix& matrix, voi d* storage) const { | |
136 void* stateStorage = (char*)storage + sizeof(BitmapProcShaderContext); | |
137 SkBitmapProcState* state = SkNEW_PLACEMENT(stateStorage, SkBitmapProcState); | |
138 if (!this->validInternal(device, paint, matrix, NULL, state)) { | |
139 state->~SkBitmapProcState(); | |
140 return NULL; | |
117 } | 141 } |
118 | 142 |
119 const SkBitmap& bitmap = *fState.fBitmap; | 143 return SkNEW_PLACEMENT_ARGS(storage, BitmapProcShaderContext, |
144 (*this, device, paint, matrix, state)); | |
145 } | |
146 | |
147 size_t SkBitmapProcShader::contextSize() const { | |
148 // The SkBitmapProcState is stored outside of the context object, with the c ontext holding | |
149 // a pointer to it. | |
150 return sizeof(BitmapProcShaderContext) + sizeof(SkBitmapProcState); | |
151 } | |
152 | |
153 SkBitmapProcShader::BitmapProcShaderContext::BitmapProcShaderContext( | |
154 const SkBitmapProcShader& shader, const SkBitmap& device, | |
155 const SkPaint& paint, const SkMatrix& matrix, SkBitmapProcState* state) | |
156 : INHERITED(shader, device, paint, matrix), fState(state) | |
157 { | |
158 const SkBitmap& bitmap = *fState->fBitmap; | |
120 bool bitmapIsOpaque = bitmap.isOpaque(); | 159 bool bitmapIsOpaque = bitmap.isOpaque(); |
121 | 160 |
122 // update fFlags | 161 // update fFlags |
123 uint32_t flags = 0; | 162 uint32_t flags = 0; |
124 if (bitmapIsOpaque && (255 == this->getPaintAlpha())) { | 163 if (bitmapIsOpaque && (255 == this->getPaintAlpha())) { |
125 flags |= kOpaqueAlpha_Flag; | 164 flags |= kOpaqueAlpha_Flag; |
126 } | 165 } |
127 | 166 |
128 switch (bitmap.colorType()) { | 167 switch (bitmap.colorType()) { |
129 case kRGB_565_SkColorType: | 168 case kRGB_565_SkColorType: |
(...skipping 20 matching lines...) Expand all Loading... | |
150 // if we're only 1-pixel high, and we don't rotate, then we can claim this | 189 // if we're only 1-pixel high, and we don't rotate, then we can claim this |
151 if (1 == bitmap.height() && | 190 if (1 == bitmap.height() && |
152 only_scale_and_translate(this->getTotalInverse())) { | 191 only_scale_and_translate(this->getTotalInverse())) { |
153 flags |= kConstInY32_Flag; | 192 flags |= kConstInY32_Flag; |
154 if (flags & kHasSpan16_Flag) { | 193 if (flags & kHasSpan16_Flag) { |
155 flags |= kConstInY16_Flag; | 194 flags |= kConstInY16_Flag; |
156 } | 195 } |
157 } | 196 } |
158 | 197 |
159 fFlags = flags; | 198 fFlags = flags; |
160 return true; | |
161 } | 199 } |
162 | 200 |
163 void SkBitmapProcShader::endContext() { | 201 SkBitmapProcShader::BitmapProcShaderContext::~BitmapProcShaderContext() { |
164 fState.endContext(); | 202 fState->~SkBitmapProcState(); |
scroggo
2014/04/01 18:02:41
A comment explaining why we treat it this way woul
Dominik Grewe
2014/04/02 18:00:48
Done.
| |
165 this->INHERITED::endContext(); | |
166 } | 203 } |
167 | 204 |
168 #define BUF_MAX 128 | 205 #define BUF_MAX 128 |
169 | 206 |
170 #define TEST_BUFFER_OVERRITEx | 207 #define TEST_BUFFER_OVERRITEx |
171 | 208 |
172 #ifdef TEST_BUFFER_OVERRITE | 209 #ifdef TEST_BUFFER_OVERRITE |
173 #define TEST_BUFFER_EXTRA 32 | 210 #define TEST_BUFFER_EXTRA 32 |
174 #define TEST_PATTERN 0x88888888 | 211 #define TEST_PATTERN 0x88888888 |
175 #else | 212 #else |
176 #define TEST_BUFFER_EXTRA 0 | 213 #define TEST_BUFFER_EXTRA 0 |
177 #endif | 214 #endif |
178 | 215 |
179 void SkBitmapProcShader::shadeSpan(int x, int y, SkPMColor dstC[], int count) { | 216 void SkBitmapProcShader::BitmapProcShaderContext::shadeSpan(int x, int y, SkPMCo lor dstC[], |
180 const SkBitmapProcState& state = fState; | 217 int count) { |
218 const SkBitmapProcState& state = *fState; | |
181 if (state.getShaderProc32()) { | 219 if (state.getShaderProc32()) { |
182 state.getShaderProc32()(state, x, y, dstC, count); | 220 state.getShaderProc32()(state, x, y, dstC, count); |
183 return; | 221 return; |
184 } | 222 } |
185 | 223 |
186 uint32_t buffer[BUF_MAX + TEST_BUFFER_EXTRA]; | 224 uint32_t buffer[BUF_MAX + TEST_BUFFER_EXTRA]; |
187 SkBitmapProcState::MatrixProc mproc = state.getMatrixProc(); | 225 SkBitmapProcState::MatrixProc mproc = state.getMatrixProc(); |
188 SkBitmapProcState::SampleProc32 sproc = state.getSampleProc32(); | 226 SkBitmapProcState::SampleProc32 sproc = state.getSampleProc32(); |
189 int max = fState.maxCountForBufferSize(sizeof(buffer[0]) * BUF_MAX); | 227 int max = state.maxCountForBufferSize(sizeof(buffer[0]) * BUF_MAX); |
190 | 228 |
191 SkASSERT(state.fBitmap->getPixels()); | 229 SkASSERT(state.fBitmap->getPixels()); |
192 SkASSERT(state.fBitmap->pixelRef() == NULL || | 230 SkASSERT(state.fBitmap->pixelRef() == NULL || |
193 state.fBitmap->pixelRef()->isLocked()); | 231 state.fBitmap->pixelRef()->isLocked()); |
194 | 232 |
195 for (;;) { | 233 for (;;) { |
196 int n = count; | 234 int n = count; |
197 if (n > max) { | 235 if (n > max) { |
198 n = max; | 236 n = max; |
199 } | 237 } |
(...skipping 13 matching lines...) Expand all Loading... | |
213 | 251 |
214 if ((count -= n) == 0) { | 252 if ((count -= n) == 0) { |
215 break; | 253 break; |
216 } | 254 } |
217 SkASSERT(count > 0); | 255 SkASSERT(count > 0); |
218 x += n; | 256 x += n; |
219 dstC += n; | 257 dstC += n; |
220 } | 258 } |
221 } | 259 } |
222 | 260 |
223 SkShader::ShadeProc SkBitmapProcShader::asAShadeProc(void** ctx) { | 261 SkShader::Context::ShadeProc SkBitmapProcShader::BitmapProcShaderContext::asASha deProc(void** ctx) { |
224 if (fState.getShaderProc32()) { | 262 if (fState->getShaderProc32()) { |
225 *ctx = &fState; | 263 *ctx = fState; |
226 return (ShadeProc)fState.getShaderProc32(); | 264 return (ShadeProc)fState->getShaderProc32(); |
227 } | 265 } |
228 return NULL; | 266 return NULL; |
229 } | 267 } |
230 | 268 |
231 void SkBitmapProcShader::shadeSpan16(int x, int y, uint16_t dstC[], int count) { | 269 void SkBitmapProcShader::BitmapProcShaderContext::shadeSpan16(int x, int y, uint 16_t dstC[], |
232 const SkBitmapProcState& state = fState; | 270 int count) { |
271 const SkBitmapProcState& state = *fState; | |
233 if (state.getShaderProc16()) { | 272 if (state.getShaderProc16()) { |
234 state.getShaderProc16()(state, x, y, dstC, count); | 273 state.getShaderProc16()(state, x, y, dstC, count); |
235 return; | 274 return; |
236 } | 275 } |
237 | 276 |
238 uint32_t buffer[BUF_MAX]; | 277 uint32_t buffer[BUF_MAX]; |
239 SkBitmapProcState::MatrixProc mproc = state.getMatrixProc(); | 278 SkBitmapProcState::MatrixProc mproc = state.getMatrixProc(); |
240 SkBitmapProcState::SampleProc16 sproc = state.getSampleProc16(); | 279 SkBitmapProcState::SampleProc16 sproc = state.getSampleProc16(); |
241 int max = fState.maxCountForBufferSize(sizeof(buffer)); | 280 int max = state.maxCountForBufferSize(sizeof(buffer)); |
242 | 281 |
243 SkASSERT(state.fBitmap->getPixels()); | 282 SkASSERT(state.fBitmap->getPixels()); |
244 SkASSERT(state.fBitmap->pixelRef() == NULL || | 283 SkASSERT(state.fBitmap->pixelRef() == NULL || |
245 state.fBitmap->pixelRef()->isLocked()); | 284 state.fBitmap->pixelRef()->isLocked()); |
246 | 285 |
247 for (;;) { | 286 for (;;) { |
248 int n = count; | 287 int n = count; |
249 if (n > max) { | 288 if (n > max) { |
250 n = max; | 289 n = max; |
251 } | 290 } |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
335 | 374 |
336 #ifdef SK_DEVELOPER | 375 #ifdef SK_DEVELOPER |
337 void SkBitmapProcShader::toString(SkString* str) const { | 376 void SkBitmapProcShader::toString(SkString* str) const { |
338 static const char* gTileModeName[SkShader::kTileModeCount] = { | 377 static const char* gTileModeName[SkShader::kTileModeCount] = { |
339 "clamp", "repeat", "mirror" | 378 "clamp", "repeat", "mirror" |
340 }; | 379 }; |
341 | 380 |
342 str->append("BitmapShader: ("); | 381 str->append("BitmapShader: ("); |
343 | 382 |
344 str->appendf("(%s, %s)", | 383 str->appendf("(%s, %s)", |
345 gTileModeName[fState.fTileModeX], | 384 gTileModeName[fTileModeX], |
346 gTileModeName[fState.fTileModeY]); | 385 gTileModeName[fTileModeY]); |
347 | 386 |
348 str->append(" "); | 387 str->append(" "); |
349 fRawBitmap.toString(str); | 388 fRawBitmap.toString(str); |
350 | 389 |
351 this->INHERITED::toString(str); | 390 this->INHERITED::toString(str); |
352 | 391 |
353 str->append(")"); | 392 str->append(")"); |
354 } | 393 } |
355 #endif | 394 #endif |
356 | 395 |
(...skipping 20 matching lines...) Expand all Loading... | |
377 SkMatrix matrix; | 416 SkMatrix matrix; |
378 matrix.setIDiv(fRawBitmap.width(), fRawBitmap.height()); | 417 matrix.setIDiv(fRawBitmap.width(), fRawBitmap.height()); |
379 | 418 |
380 SkMatrix lmInverse; | 419 SkMatrix lmInverse; |
381 if (!this->getLocalMatrix().invert(&lmInverse)) { | 420 if (!this->getLocalMatrix().invert(&lmInverse)) { |
382 return NULL; | 421 return NULL; |
383 } | 422 } |
384 matrix.preConcat(lmInverse); | 423 matrix.preConcat(lmInverse); |
385 | 424 |
386 SkShader::TileMode tm[] = { | 425 SkShader::TileMode tm[] = { |
387 (TileMode)fState.fTileModeX, | 426 (TileMode)fTileModeX, |
388 (TileMode)fState.fTileModeY, | 427 (TileMode)fTileModeY, |
389 }; | 428 }; |
390 | 429 |
391 // Must set wrap and filter on the sampler before requesting a texture. In t wo places below | 430 // Must set wrap and filter on the sampler before requesting a texture. In t wo places below |
392 // we check the matrix scale factors to determine how to interpret the filte r quality setting. | 431 // we check the matrix scale factors to determine how to interpret the filte r quality setting. |
393 // This completely ignores the complexity of the drawVertices case where exp licit local coords | 432 // This completely ignores the complexity of the drawVertices case where exp licit local coords |
394 // are provided by the caller. | 433 // are provided by the caller. |
395 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel(); | 434 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel(); |
396 GrTextureParams::FilterMode textureFilterMode; | 435 GrTextureParams::FilterMode textureFilterMode; |
397 switch(paintFilterLevel) { | 436 switch(paintFilterLevel) { |
398 case SkPaint::kNone_FilterLevel: | 437 case SkPaint::kNone_FilterLevel: |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
444 GrEffectRef* effect = NULL; | 483 GrEffectRef* effect = NULL; |
445 if (paintFilterLevel == SkPaint::kHigh_FilterLevel) { | 484 if (paintFilterLevel == SkPaint::kHigh_FilterLevel) { |
446 effect = GrBicubicEffect::Create(texture, matrix, tm); | 485 effect = GrBicubicEffect::Create(texture, matrix, tm); |
447 } else { | 486 } else { |
448 effect = GrSimpleTextureEffect::Create(texture, matrix, params); | 487 effect = GrSimpleTextureEffect::Create(texture, matrix, params); |
449 } | 488 } |
450 GrUnlockAndUnrefCachedBitmapTexture(texture); | 489 GrUnlockAndUnrefCachedBitmapTexture(texture); |
451 return effect; | 490 return effect; |
452 } | 491 } |
453 #endif | 492 #endif |
OLD | NEW |