From 800e1ff0b9eaff0b02ce711bb7f22f2149141c9e Mon Sep 17 00:00:00 2001 From: Eric Hassold Date: Tue, 22 Feb 2011 11:56:07 -0800 Subject: Check for pixels allocation failure Standard memory allocator in Skia abort when an allocation request fails. However, when allocating in managed heap, need to check for allocation failure in allocPixels(). This add checks for all allocPixels calls in Skia Bitmap code. Bug: 3418381 Change-Id: I3c4ac403a3c073d64a689b29d3baccdc7fc90e1a --- include/core/SkBitmap.h | 13 +++++++++++++ src/core/SkBitmap.cpp | 20 ++++++++++++++++---- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/include/core/SkBitmap.h b/include/core/SkBitmap.h index 1def198..84d069c 100644 --- a/include/core/SkBitmap.h +++ b/include/core/SkBitmap.h @@ -404,6 +404,19 @@ public: this->extractAlpha(dst, paint, NULL, offset); } + /** Set dst to contain alpha layer of this bitmap. If destination bitmap + fails to be initialized, e.g. because allocator can't allocate pixels + for it, dst will be resetted (zero width and height, no pixels). + + @param dst The bitmap to be filled with alpha layer + @param paint The paint to draw with + @param allocator Allocator used to allocate the pixelref for the dst + bitmap. If this is null, the standard HeapAllocator + will be used. + @param offset If not null, it is set to top-left coordinate to position + the returned bitmap so that it visually lines up with the + original + */ void extractAlpha(SkBitmap* dst, const SkPaint* paint, Allocator* allocator, SkIPoint* offset) const; diff --git a/src/core/SkBitmap.cpp b/src/core/SkBitmap.cpp index e7d9537..0dc4ddc 100644 --- a/src/core/SkBitmap.cpp +++ b/src/core/SkBitmap.cpp @@ -1138,8 +1138,19 @@ void SkBitmap::extractAlpha(SkBitmap* dst, const SkPaint* paint, NO_FILTER_CASE: dst->setConfig(SkBitmap::kA8_Config, this->width(), this->height(), srcM.fRowBytes); - dst->allocPixels(allocator, NULL); - GetBitmapAlpha(*this, dst->getAddr8(0, 0), srcM.fRowBytes); + if (dst->allocPixels(allocator, NULL)) { + GetBitmapAlpha(*this, dst->getAddr8(0, 0), srcM.fRowBytes); + if (offset) { + offset->set(0, 0); + } + return; + } + ALLOC_ERR: + // Allocation of pixels for alpha bitmap failed. + SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n", + dst->width(), dst->height()); + // When pixels can't be allocated, reset destination bitmap + dst->reset(); if (offset) { offset->set(0, 0); } @@ -1157,7 +1168,9 @@ void SkBitmap::extractAlpha(SkBitmap* dst, const SkPaint* paint, dst->setConfig(SkBitmap::kA8_Config, dstM.fBounds.width(), dstM.fBounds.height(), dstM.fRowBytes); - dst->allocPixels(allocator, NULL); + if (!dst->allocPixels(allocator, NULL)) { + goto ALLOC_ERR; + } memcpy(dst->getPixels(), dstM.fImage, dstM.computeImageSize()); if (offset) { offset->set(dstM.fBounds.fLeft, dstM.fBounds.fTop); @@ -1341,4 +1354,3 @@ void SkBitmap::validate() const { #endif } #endif - -- cgit v1.1