OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2008 The Android Open Source Project | 3 * Copyright 2008 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 "SkBitmap.h" | 10 #include "SkBitmap.h" |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 return; | 261 return; |
262 } | 262 } |
263 | 263 |
264 this->setPixelRef(pr)->unref(); | 264 this->setPixelRef(pr)->unref(); |
265 | 265 |
266 // since we're already allocated, we lockPixels right away | 266 // since we're already allocated, we lockPixels right away |
267 this->lockPixels(); | 267 this->lockPixels(); |
268 SkDEBUGCODE(this->validate();) | 268 SkDEBUGCODE(this->validate();) |
269 } | 269 } |
270 | 270 |
271 bool SkBitmap::allocPixels(Allocator* allocator, SkColorTable* ctable) { | 271 bool SkBitmap::tryAllocPixels(Allocator* allocator, SkColorTable* ctable) { |
272 HeapAllocator stdalloc; | 272 HeapAllocator stdalloc; |
273 | 273 |
274 if (NULL == allocator) { | 274 if (NULL == allocator) { |
275 allocator = &stdalloc; | 275 allocator = &stdalloc; |
276 } | 276 } |
277 return allocator->allocPixelRef(this, ctable); | 277 return allocator->allocPixelRef(this, ctable); |
278 } | 278 } |
279 | 279 |
280 /////////////////////////////////////////////////////////////////////////////// | 280 /////////////////////////////////////////////////////////////////////////////// |
281 | 281 |
282 bool SkBitmap::allocPixels(const SkImageInfo& requestedInfo, size_t rowBytes) { | 282 bool SkBitmap::tryAllocPixels(const SkImageInfo& requestedInfo, size_t rowBytes)
{ |
283 if (kIndex_8_SkColorType == requestedInfo.colorType()) { | 283 if (kIndex_8_SkColorType == requestedInfo.colorType()) { |
284 return reset_return_false(this); | 284 return reset_return_false(this); |
285 } | 285 } |
286 if (!this->setInfo(requestedInfo, rowBytes)) { | 286 if (!this->setInfo(requestedInfo, rowBytes)) { |
287 return reset_return_false(this); | 287 return reset_return_false(this); |
288 } | 288 } |
289 | 289 |
290 // setInfo may have corrected info (e.g. 565 is always opaque). | 290 // setInfo may have corrected info (e.g. 565 is always opaque). |
291 const SkImageInfo& correctedInfo = this->info(); | 291 const SkImageInfo& correctedInfo = this->info(); |
292 // setInfo may have computed a valid rowbytes if 0 were passed in | 292 // setInfo may have computed a valid rowbytes if 0 were passed in |
293 rowBytes = this->rowBytes(); | 293 rowBytes = this->rowBytes(); |
294 | 294 |
295 SkMallocPixelRef::PRFactory defaultFactory; | 295 SkMallocPixelRef::PRFactory defaultFactory; |
296 | 296 |
297 SkPixelRef* pr = defaultFactory.create(correctedInfo, rowBytes, NULL); | 297 SkPixelRef* pr = defaultFactory.create(correctedInfo, rowBytes, NULL); |
298 if (NULL == pr) { | 298 if (NULL == pr) { |
299 return reset_return_false(this); | 299 return reset_return_false(this); |
300 } | 300 } |
301 this->setPixelRef(pr)->unref(); | 301 this->setPixelRef(pr)->unref(); |
302 | 302 |
303 // TODO: lockPixels could/should return bool or void*/NULL | 303 // TODO: lockPixels could/should return bool or void*/NULL |
304 this->lockPixels(); | 304 this->lockPixels(); |
305 if (NULL == this->getPixels()) { | 305 if (NULL == this->getPixels()) { |
306 return reset_return_false(this); | 306 return reset_return_false(this); |
307 } | 307 } |
308 return true; | 308 return true; |
309 } | 309 } |
310 | 310 |
311 bool SkBitmap::allocPixels(const SkImageInfo& requestedInfo, SkPixelRefFactory*
factory, | 311 bool SkBitmap::tryAllocPixels(const SkImageInfo& requestedInfo, SkPixelRefFactor
y* factory, |
312 SkColorTable* ctable) { | 312 SkColorTable* ctable) { |
313 if (kIndex_8_SkColorType == requestedInfo.fColorType && NULL == ctable) { | 313 if (kIndex_8_SkColorType == requestedInfo.fColorType && NULL == ctable) { |
314 return reset_return_false(this); | 314 return reset_return_false(this); |
315 } | 315 } |
316 if (!this->setInfo(requestedInfo)) { | 316 if (!this->setInfo(requestedInfo)) { |
317 return reset_return_false(this); | 317 return reset_return_false(this); |
318 } | 318 } |
319 | 319 |
320 // setInfo may have corrected info (e.g. 565 is always opaque). | 320 // setInfo may have corrected info (e.g. 565 is always opaque). |
321 const SkImageInfo& correctedInfo = this->info(); | 321 const SkImageInfo& correctedInfo = this->info(); |
322 | 322 |
(...skipping 620 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
943 if (!tmpDst.setInfo(dstInfo)) { | 943 if (!tmpDst.setInfo(dstInfo)) { |
944 return false; | 944 return false; |
945 } | 945 } |
946 | 946 |
947 // allocate colortable if srcConfig == kIndex8_Config | 947 // allocate colortable if srcConfig == kIndex8_Config |
948 SkAutoTUnref<SkColorTable> ctable; | 948 SkAutoTUnref<SkColorTable> ctable; |
949 if (dstColorType == kIndex_8_SkColorType) { | 949 if (dstColorType == kIndex_8_SkColorType) { |
950 // TODO: can we just ref() the src colortable? Is it reentrant-safe? | 950 // TODO: can we just ref() the src colortable? Is it reentrant-safe? |
951 ctable.reset(SkNEW_ARGS(SkColorTable, (*src->getColorTable()))); | 951 ctable.reset(SkNEW_ARGS(SkColorTable, (*src->getColorTable()))); |
952 } | 952 } |
953 if (!tmpDst.allocPixels(alloc, ctable)) { | 953 if (!tmpDst.tryAllocPixels(alloc, ctable)) { |
954 return false; | 954 return false; |
955 } | 955 } |
956 | 956 |
957 if (!tmpDst.readyToDraw()) { | 957 if (!tmpDst.readyToDraw()) { |
958 // allocator/lock failed | 958 // allocator/lock failed |
959 return false; | 959 return false; |
960 } | 960 } |
961 | 961 |
962 // pixelRef must be non NULL or tmpDst.readyToDraw() would have | 962 // pixelRef must be non NULL or tmpDst.readyToDraw() would have |
963 // returned false. | 963 // returned false. |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1115 if (NULL != filter) { | 1115 if (NULL != filter) { |
1116 identity.reset(); | 1116 identity.reset(); |
1117 srcM.fImage = NULL; | 1117 srcM.fImage = NULL; |
1118 if (!filter->filterMask(&dstM, srcM, identity, NULL)) { | 1118 if (!filter->filterMask(&dstM, srcM, identity, NULL)) { |
1119 goto NO_FILTER_CASE; | 1119 goto NO_FILTER_CASE; |
1120 } | 1120 } |
1121 dstM.fRowBytes = SkAlign4(dstM.fBounds.width()); | 1121 dstM.fRowBytes = SkAlign4(dstM.fBounds.width()); |
1122 } else { | 1122 } else { |
1123 NO_FILTER_CASE: | 1123 NO_FILTER_CASE: |
1124 tmpBitmap.setInfo(SkImageInfo::MakeA8(this->width(), this->height()), sr
cM.fRowBytes); | 1124 tmpBitmap.setInfo(SkImageInfo::MakeA8(this->width(), this->height()), sr
cM.fRowBytes); |
1125 if (!tmpBitmap.allocPixels(allocator, NULL)) { | 1125 if (!tmpBitmap.tryAllocPixels(allocator, NULL)) { |
1126 // Allocation of pixels for alpha bitmap failed. | 1126 // Allocation of pixels for alpha bitmap failed. |
1127 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n", | 1127 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n", |
1128 tmpBitmap.width(), tmpBitmap.height()); | 1128 tmpBitmap.width(), tmpBitmap.height()); |
1129 return false; | 1129 return false; |
1130 } | 1130 } |
1131 GetBitmapAlpha(*this, tmpBitmap.getAddr8(0, 0), srcM.fRowBytes); | 1131 GetBitmapAlpha(*this, tmpBitmap.getAddr8(0, 0), srcM.fRowBytes); |
1132 if (offset) { | 1132 if (offset) { |
1133 offset->set(0, 0); | 1133 offset->set(0, 0); |
1134 } | 1134 } |
1135 tmpBitmap.swap(*dst); | 1135 tmpBitmap.swap(*dst); |
1136 return true; | 1136 return true; |
1137 } | 1137 } |
1138 srcM.fImage = SkMask::AllocImage(srcM.computeImageSize()); | 1138 srcM.fImage = SkMask::AllocImage(srcM.computeImageSize()); |
1139 SkAutoMaskFreeImage srcCleanup(srcM.fImage); | 1139 SkAutoMaskFreeImage srcCleanup(srcM.fImage); |
1140 | 1140 |
1141 GetBitmapAlpha(*this, srcM.fImage, srcM.fRowBytes); | 1141 GetBitmapAlpha(*this, srcM.fImage, srcM.fRowBytes); |
1142 if (!filter->filterMask(&dstM, srcM, identity, NULL)) { | 1142 if (!filter->filterMask(&dstM, srcM, identity, NULL)) { |
1143 goto NO_FILTER_CASE; | 1143 goto NO_FILTER_CASE; |
1144 } | 1144 } |
1145 SkAutoMaskFreeImage dstCleanup(dstM.fImage); | 1145 SkAutoMaskFreeImage dstCleanup(dstM.fImage); |
1146 | 1146 |
1147 tmpBitmap.setInfo(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.hei
ght()), | 1147 tmpBitmap.setInfo(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.hei
ght()), |
1148 dstM.fRowBytes); | 1148 dstM.fRowBytes); |
1149 if (!tmpBitmap.allocPixels(allocator, NULL)) { | 1149 if (!tmpBitmap.tryAllocPixels(allocator, NULL)) { |
1150 // Allocation of pixels for alpha bitmap failed. | 1150 // Allocation of pixels for alpha bitmap failed. |
1151 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n", | 1151 SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n", |
1152 tmpBitmap.width(), tmpBitmap.height()); | 1152 tmpBitmap.width(), tmpBitmap.height()); |
1153 return false; | 1153 return false; |
1154 } | 1154 } |
1155 memcpy(tmpBitmap.getPixels(), dstM.fImage, dstM.computeImageSize()); | 1155 memcpy(tmpBitmap.getPixels(), dstM.fImage, dstM.computeImageSize()); |
1156 if (offset) { | 1156 if (offset) { |
1157 offset->set(dstM.fBounds.fLeft, dstM.fBounds.fTop); | 1157 offset->set(dstM.fBounds.fLeft, dstM.fBounds.fTop); |
1158 } | 1158 } |
1159 SkDEBUGCODE(tmpBitmap.validate();) | 1159 SkDEBUGCODE(tmpBitmap.validate();) |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1389 /////////////////////////////////////////////////////////////////////////////// | 1389 /////////////////////////////////////////////////////////////////////////////// |
1390 | 1390 |
1391 #ifdef SK_DEBUG | 1391 #ifdef SK_DEBUG |
1392 void SkImageInfo::validate() const { | 1392 void SkImageInfo::validate() const { |
1393 SkASSERT(fWidth >= 0); | 1393 SkASSERT(fWidth >= 0); |
1394 SkASSERT(fHeight >= 0); | 1394 SkASSERT(fHeight >= 0); |
1395 SkASSERT(SkColorTypeIsValid(fColorType)); | 1395 SkASSERT(SkColorTypeIsValid(fColorType)); |
1396 SkASSERT(SkAlphaTypeIsValid(fAlphaType)); | 1396 SkASSERT(SkAlphaTypeIsValid(fAlphaType)); |
1397 } | 1397 } |
1398 #endif | 1398 #endif |
OLD | NEW |