make allocPixels throw on failure

BUG=skia:
R=mtklein@google.com, fmalita@google.com, fmalita@chromium.org

Author: reed@google.com

Review URL: https://codereview.chromium.org/510423005
diff --git a/gyp/skia_for_chromium_defines.gypi b/gyp/skia_for_chromium_defines.gypi
index ac4a19f..636fd87 100644
--- a/gyp/skia_for_chromium_defines.gypi
+++ b/gyp/skia_for_chromium_defines.gypi
@@ -13,6 +13,7 @@
     # If these become 'permanent', they should be moved into skia_common.gypi
     #
     'skia_for_chromium_defines': [
+      'SK_SUPPORT_LEGACY_ALLOCPIXELS_BOOL',
       'SK_IGNORE_PROPER_FRACTIONAL_SCALING',
       'SK_SUPPORT_LEGACY_PICTURE_CLONE',
       'SK_SUPPORT_LEGACY_GETDEVICE',
diff --git a/include/core/SkBitmap.h b/include/core/SkBitmap.h
index 13a7e30..c58c78d 100644
--- a/include/core/SkBitmap.h
+++ b/include/core/SkBitmap.h
@@ -14,6 +14,16 @@
 #include "SkPoint.h"
 #include "SkRefCnt.h"
 
+#ifdef SK_SUPPORT_LEGACY_ALLOCPIXELS_BOOL
+    #define SK_ALLOCPIXELS_RETURN_TYPE  bool
+    #define SK_ALLOCPIXELS_RETURN_TRUE  return true
+    #define SK_ALLOCPIXELS_RETURN_FAIL  return false
+#else
+    #define SK_ALLOCPIXELS_RETURN_TYPE  void
+    #define SK_ALLOCPIXELS_RETURN_TRUE  return
+    #define SK_ALLOCPIXELS_RETURN_FAIL  sk_throw()
+#endif
+
 struct SkMask;
 struct SkIRect;
 struct SkRect;
@@ -218,7 +228,15 @@
      *  a colortable, then ColorTable must be non-null, and will be ref'd.
      *  On failure, the bitmap will be set to empty and return false.
      */
-    bool allocPixels(const SkImageInfo&, SkPixelRefFactory*, SkColorTable*);
+    bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo&, SkPixelRefFactory*, SkColorTable*);
+
+    SK_ALLOCPIXELS_RETURN_TYPE allocPixels(const SkImageInfo& info, SkPixelRefFactory* factory,
+                                           SkColorTable* ctable) {
+        if (!this->tryAllocPixels(info, factory, ctable)) {
+            SK_ALLOCPIXELS_RETURN_FAIL;
+        }
+        SK_ALLOCPIXELS_RETURN_TRUE;
+    }
 
     /**
      *  Allocate the bitmap's pixels to match the requested image info and
@@ -228,26 +246,39 @@
      *  the pixel size specified by info.colorType()) then false is returned
      *  and the bitmap is set to empty.
      */
-    bool allocPixels(const SkImageInfo& info, size_t rowBytes);
+    bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo& info, size_t rowBytes);
 
-    /**
-     *  Allocate a pixelref to match the specified image info, using the default
-     *  allocator.
-     *  On success, the bitmap's pixels will be "locked", and return true.
-     *  On failure, the bitmap will be set to empty and return false.
-     */
-    bool allocPixels(const SkImageInfo& info) {
+    SK_ALLOCPIXELS_RETURN_TYPE allocPixels(const SkImageInfo& info, size_t rowBytes) {
+        if (!this->tryAllocPixels(info, rowBytes)) {
+            SK_ALLOCPIXELS_RETURN_FAIL;
+        }
+        SK_ALLOCPIXELS_RETURN_TRUE;
+    }
+
+    bool SK_WARN_UNUSED_RESULT tryAllocPixels(const SkImageInfo& info) {
+        return this->tryAllocPixels(info, info.minRowBytes());
+    }
+
+    SK_ALLOCPIXELS_RETURN_TYPE allocPixels(const SkImageInfo& info) {
         return this->allocPixels(info, info.minRowBytes());
     }
 
-    bool allocN32Pixels(int width, int height, bool isOpaque = false) {
+    bool SK_WARN_UNUSED_RESULT tryAllocN32Pixels(int width, int height, bool isOpaque = false) {
+        SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
+        if (isOpaque) {
+            info.fAlphaType = kOpaque_SkAlphaType;
+        }
+        return this->tryAllocPixels(info);
+    }
+    
+    SK_ALLOCPIXELS_RETURN_TYPE allocN32Pixels(int width, int height, bool isOpaque = false) {
         SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
         if (isOpaque) {
             info.fAlphaType = kOpaque_SkAlphaType;
         }
         return this->allocPixels(info);
     }
-
+    
     /**
      *  Install a pixelref that wraps the specified pixels and rowBytes, and
      *  optional ReleaseProc and context. When the pixels are no longer
@@ -320,7 +351,11 @@
         @return true if the allocation succeeds. If not the pixelref field of
                      the bitmap will be unchanged.
     */
-    bool allocPixels(SkColorTable* ctable = NULL) {
+    bool SK_WARN_UNUSED_RESULT tryAllocPixels(SkColorTable* ctable = NULL) {
+        return this->tryAllocPixels(NULL, ctable);
+    }
+
+    SK_ALLOCPIXELS_RETURN_TYPE allocPixels(SkColorTable* ctable = NULL) {
         return this->allocPixels(NULL, ctable);
     }
 
@@ -342,7 +377,14 @@
         @return true if the allocation succeeds. If not the pixelref field of
                      the bitmap will be unchanged.
     */
-    bool allocPixels(Allocator* allocator, SkColorTable* ctable);
+    bool SK_WARN_UNUSED_RESULT tryAllocPixels(Allocator* allocator, SkColorTable* ctable);
+
+    SK_ALLOCPIXELS_RETURN_TYPE allocPixels(Allocator* allocator, SkColorTable* ctable) {
+        if (!this->tryAllocPixels(allocator, ctable)) {
+            SK_ALLOCPIXELS_RETURN_FAIL;
+        }
+        SK_ALLOCPIXELS_RETURN_TRUE;
+    }
 
     /**
      *  Return the current pixelref object or NULL if there is none. This does
diff --git a/samplecode/SampleApp.cpp b/samplecode/SampleApp.cpp
index 34d1b97..e8d6d99 100644
--- a/samplecode/SampleApp.cpp
+++ b/samplecode/SampleApp.cpp
@@ -999,7 +999,7 @@
 
 static SkBitmap capture_bitmap(SkCanvas* canvas) {
     SkBitmap bm;
-    if (bm.allocPixels(canvas->imageInfo())) {
+    if (bm.tryAllocPixels(canvas->imageInfo())) {
         canvas->readPixels(&bm, 0, 0);
     }
     return bm;
diff --git a/samplecode/SampleFilterFuzz.cpp b/samplecode/SampleFilterFuzz.cpp
index 8aa48ed..f4157cd 100644
--- a/samplecode/SampleFilterFuzz.cpp
+++ b/samplecode/SampleFilterFuzz.cpp
@@ -153,7 +153,7 @@
     do {
         info = SkImageInfo::Make(kBitmapSize, kBitmapSize, rand_colortype(),
                                  kPremul_SkAlphaType);
-    } while (!valid_for_raster_canvas(info) || !bitmap->allocPixels(info));
+    } while (!valid_for_raster_canvas(info) || !bitmap->tryAllocPixels(info));
 }
 
 static void make_g_bitmap(SkBitmap& bitmap) {
diff --git a/samplecode/SampleUnpremul.cpp b/samplecode/SampleUnpremul.cpp
index bfe69e0..992444d 100644
--- a/samplecode/SampleUnpremul.cpp
+++ b/samplecode/SampleUnpremul.cpp
@@ -128,11 +128,7 @@
             // Copy it to a bitmap which can be drawn, converting
             // to premultiplied:
             SkBitmap bm;
-            if (!bm.allocN32Pixels(fBitmap.width(), fBitmap.height())) {
-                SkString errMsg("allocPixels failed");
-                canvas->drawText(errMsg.c_str(), errMsg.size(), 0, height, paint);
-                return;
-            }
+            bm.allocN32Pixels(fBitmap.width(), fBitmap.height());
             for (int i = 0; i < fBitmap.width(); ++i) {
                 for (int j = 0; j < fBitmap.height(); ++j) {
                     *bm.getAddr32(i, j) = premultiply_unpmcolor(*fBitmap.getAddr32(i, j));
diff --git a/src/core/SkBitmap.cpp b/src/core/SkBitmap.cpp
index d83199d..928a4b7 100644
--- a/src/core/SkBitmap.cpp
+++ b/src/core/SkBitmap.cpp
@@ -268,7 +268,7 @@
     SkDEBUGCODE(this->validate();)
 }
 
-bool SkBitmap::allocPixels(Allocator* allocator, SkColorTable* ctable) {
+bool SkBitmap::tryAllocPixels(Allocator* allocator, SkColorTable* ctable) {
     HeapAllocator stdalloc;
 
     if (NULL == allocator) {
@@ -279,7 +279,7 @@
 
 ///////////////////////////////////////////////////////////////////////////////
 
-bool SkBitmap::allocPixels(const SkImageInfo& requestedInfo, size_t rowBytes) {
+bool SkBitmap::tryAllocPixels(const SkImageInfo& requestedInfo, size_t rowBytes) {
     if (kIndex_8_SkColorType == requestedInfo.colorType()) {
         return reset_return_false(this);
     }
@@ -308,8 +308,8 @@
     return true;
 }
 
-bool SkBitmap::allocPixels(const SkImageInfo& requestedInfo, SkPixelRefFactory* factory,
-                           SkColorTable* ctable) {
+bool SkBitmap::tryAllocPixels(const SkImageInfo& requestedInfo, SkPixelRefFactory* factory,
+                                SkColorTable* ctable) {
     if (kIndex_8_SkColorType == requestedInfo.fColorType && NULL == ctable) {
         return reset_return_false(this);
     }
@@ -950,7 +950,7 @@
         // TODO: can we just ref() the src colortable? Is it reentrant-safe?
         ctable.reset(SkNEW_ARGS(SkColorTable, (*src->getColorTable())));
     }
-    if (!tmpDst.allocPixels(alloc, ctable)) {
+    if (!tmpDst.tryAllocPixels(alloc, ctable)) {
         return false;
     }
 
@@ -1122,7 +1122,7 @@
     } else {
     NO_FILTER_CASE:
         tmpBitmap.setInfo(SkImageInfo::MakeA8(this->width(), this->height()), srcM.fRowBytes);
-        if (!tmpBitmap.allocPixels(allocator, NULL)) {
+        if (!tmpBitmap.tryAllocPixels(allocator, NULL)) {
             // Allocation of pixels for alpha bitmap failed.
             SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
                     tmpBitmap.width(), tmpBitmap.height());
@@ -1146,7 +1146,7 @@
 
     tmpBitmap.setInfo(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.height()),
                       dstM.fRowBytes);
-    if (!tmpBitmap.allocPixels(allocator, NULL)) {
+    if (!tmpBitmap.tryAllocPixels(allocator, NULL)) {
         // Allocation of pixels for alpha bitmap failed.
         SkDebugf("extractAlpha failed to allocate (%d,%d) alpha bitmap\n",
                 tmpBitmap.width(), tmpBitmap.height());
diff --git a/src/core/SkBitmapDevice.cpp b/src/core/SkBitmapDevice.cpp
index 5f205ce..2287864 100644
--- a/src/core/SkBitmapDevice.cpp
+++ b/src/core/SkBitmapDevice.cpp
@@ -82,7 +82,7 @@
             return NULL;
         }
     } else {
-        if (!bitmap.allocPixels(info)) {
+        if (!bitmap.tryAllocPixels(info)) {
             return NULL;
         }
         if (!bitmap.info().isOpaque()) {
diff --git a/src/core/SkCanvas.cpp b/src/core/SkCanvas.cpp
index 717878c..f071fc9 100644
--- a/src/core/SkCanvas.cpp
+++ b/src/core/SkCanvas.cpp
@@ -568,7 +568,7 @@
 
     bool weAllocated = false;
     if (NULL == bitmap->pixelRef()) {
-        if (!bitmap->allocPixels()) {
+        if (!bitmap->tryAllocPixels()) {
             return false;
         }
         weAllocated = true;
@@ -594,7 +594,7 @@
         return false;
     }
 
-    if (!bitmap->allocN32Pixels(r.width(), r.height())) {
+    if (!bitmap->tryAllocN32Pixels(r.width(), r.height())) {
         // bitmap will already be reset.
         return false;
     }
@@ -1006,7 +1006,7 @@
     fAddr = canvas->peekPixels(&fInfo, &fRowBytes);
     if (NULL == fAddr) {
         fInfo = canvas->imageInfo();
-        if (kUnknown_SkColorType == fInfo.colorType() || !fBitmap.allocPixels(fInfo)) {
+        if (kUnknown_SkColorType == fInfo.colorType() || !fBitmap.tryAllocPixels(fInfo)) {
             return; // failure, fAddr is NULL
         }
         if (!canvas->readPixels(&fBitmap, 0, 0)) {
@@ -2535,7 +2535,7 @@
     }
 
     SkBitmap bitmap;
-    if (!bitmap.allocPixels(info)) {
+    if (!bitmap.tryAllocPixels(info)) {
         return NULL;
     }
 
diff --git a/src/core/SkPictureShader.cpp b/src/core/SkPictureShader.cpp
index 73ab170..a7b5412 100644
--- a/src/core/SkPictureShader.cpp
+++ b/src/core/SkPictureShader.cpp
@@ -107,7 +107,7 @@
 
     if (!fCachedBitmapShader || tileScale != fCachedTileScale) {
         SkBitmap bm;
-        if (!bm.allocN32Pixels(tileSize.width(), tileSize.height())) {
+        if (!bm.tryAllocN32Pixels(tileSize.width(), tileSize.height())) {
             return NULL;
         }
         bm.eraseColor(SK_ColorTRANSPARENT);
diff --git a/src/core/SkScalerContext.cpp b/src/core/SkScalerContext.cpp
index db3c0fb..4d7f362 100644
--- a/src/core/SkScalerContext.cpp
+++ b/src/core/SkScalerContext.cpp
@@ -437,7 +437,7 @@
     SkBitmap bm;
 
     if (0 == dstRB) {
-        if (!bm.allocPixels(info)) {
+        if (!bm.tryAllocPixels(info)) {
             // can't allocate offscreen, so empty the mask and return
             sk_bzero(mask.fImage, mask.computeImageSize());
             return;
diff --git a/src/effects/SkAlphaThresholdFilter.cpp b/src/effects/SkAlphaThresholdFilter.cpp
index ce35ba2..261640f 100644
--- a/src/effects/SkAlphaThresholdFilter.cpp
+++ b/src/effects/SkAlphaThresholdFilter.cpp
@@ -343,7 +343,7 @@
         return false;
     }
 
-    if (!dst->allocPixels(src.info())) {
+    if (!dst->tryAllocPixels(src.info())) {
         return false;
     }
 
diff --git a/src/effects/SkBlurImageFilter.cpp b/src/effects/SkBlurImageFilter.cpp
index d082098..8590400 100644
--- a/src/effects/SkBlurImageFilter.cpp
+++ b/src/effects/SkBlurImageFilter.cpp
@@ -172,7 +172,7 @@
         return false;
     }
 
-    if (!dst->allocPixels(src.info().makeWH(srcBounds.width(), srcBounds.height()))) {
+    if (!dst->tryAllocPixels(src.info().makeWH(srcBounds.width(), srcBounds.height()))) {
         return false;
     }
     dst->getBounds(&dstBounds);
@@ -199,7 +199,7 @@
     }
 
     SkBitmap temp;
-    if (!temp.allocPixels(dst->info())) {
+    if (!temp.tryAllocPixels(dst->info())) {
         return false;
     }
 
diff --git a/src/effects/SkDisplacementMapEffect.cpp b/src/effects/SkDisplacementMapEffect.cpp
index 5d15b4f..bb71aa1 100644
--- a/src/effects/SkDisplacementMapEffect.cpp
+++ b/src/effects/SkDisplacementMapEffect.cpp
@@ -254,7 +254,7 @@
         return false;
     }
 
-    if (!dst->allocPixels(color.info().makeWH(bounds.width(), bounds.height()))) {
+    if (!dst->tryAllocPixels(color.info().makeWH(bounds.width(), bounds.height()))) {
         return false;
     }
 
diff --git a/src/effects/SkLightingImageFilter.cpp b/src/effects/SkLightingImageFilter.cpp
index 69e2760..1eb2a36 100644
--- a/src/effects/SkLightingImageFilter.cpp
+++ b/src/effects/SkLightingImageFilter.cpp
@@ -1016,7 +1016,7 @@
         return false;
     }
 
-    if (!dst->allocPixels(src.info().makeWH(bounds.width(), bounds.height()))) {
+    if (!dst->tryAllocPixels(src.info().makeWH(bounds.width(), bounds.height()))) {
         return false;
     }
 
@@ -1134,7 +1134,7 @@
         return false;
     }
 
-    if (!dst->allocPixels(src.info().makeWH(bounds.width(), bounds.height()))) {
+    if (!dst->tryAllocPixels(src.info().makeWH(bounds.width(), bounds.height()))) {
         return false;
     }
 
diff --git a/src/effects/SkMagnifierImageFilter.cpp b/src/effects/SkMagnifierImageFilter.cpp
index 70e95f6..9293f7f 100644
--- a/src/effects/SkMagnifierImageFilter.cpp
+++ b/src/effects/SkMagnifierImageFilter.cpp
@@ -321,7 +321,7 @@
       return false;
     }
 
-    if (!dst->allocPixels(src.info())) {
+    if (!dst->tryAllocPixels(src.info())) {
         return false;
     }
 
diff --git a/src/effects/SkMatrixConvolutionImageFilter.cpp b/src/effects/SkMatrixConvolutionImageFilter.cpp
index aa8b804..c8959ee 100644
--- a/src/effects/SkMatrixConvolutionImageFilter.cpp
+++ b/src/effects/SkMatrixConvolutionImageFilter.cpp
@@ -265,7 +265,7 @@
         return SkBitmap();
     }
     SkBitmap result;
-    if (!result.allocPixels(src.info())) {
+    if (!result.tryAllocPixels(src.info())) {
         return SkBitmap();
     }
     for (int y = 0; y < src.height(); ++y) {
@@ -307,7 +307,7 @@
         return false;
     }
 
-    if (!result->allocPixels(src.info().makeWH(bounds.width(), bounds.height()))) {
+    if (!result->tryAllocPixels(src.info().makeWH(bounds.width(), bounds.height()))) {
         return false;
     }
 
diff --git a/src/effects/SkMorphologyImageFilter.cpp b/src/effects/SkMorphologyImageFilter.cpp
index 2b9c91e..c6c470c 100644
--- a/src/effects/SkMorphologyImageFilter.cpp
+++ b/src/effects/SkMorphologyImageFilter.cpp
@@ -166,7 +166,7 @@
         return false;
     }
 
-    if (!dst->allocPixels(src.info().makeWH(bounds.width(), bounds.height()))) {
+    if (!dst->tryAllocPixels(src.info().makeWH(bounds.width(), bounds.height()))) {
         return false;
     }
 
@@ -191,7 +191,7 @@
     }
 
     SkBitmap temp;
-    if (!temp.allocPixels(dst->info())) {
+    if (!temp.tryAllocPixels(dst->info())) {
         return false;
     }
 
diff --git a/src/gpu/GrSWMaskHelper.cpp b/src/gpu/GrSWMaskHelper.cpp
index b7b2d1a..b51bae4 100644
--- a/src/gpu/GrSWMaskHelper.cpp
+++ b/src/gpu/GrSWMaskHelper.cpp
@@ -205,7 +205,7 @@
     // allocate the pixels for a bitmap
     const SkImageInfo bmImageInfo = SkImageInfo::MakeA8(cmpWidth, cmpHeight);
     if (kBlitter_CompressionMode != fCompressionMode) {
-        if (!fBM.allocPixels(bmImageInfo)) {
+        if (!fBM.tryAllocPixels(bmImageInfo)) {
             return false;
         }
 
diff --git a/src/gpu/GrSurface.cpp b/src/gpu/GrSurface.cpp
index a07fe67..54497fe 100644
--- a/src/gpu/GrSurface.cpp
+++ b/src/gpu/GrSurface.cpp
@@ -25,8 +25,7 @@
 
 bool GrSurface::savePixels(const char* filename) {
     SkBitmap bm;
-    if (!bm.allocPixels(SkImageInfo::MakeN32Premul(this->width(),
-                                                   this->height()))) {
+    if (!bm.tryAllocPixels(SkImageInfo::MakeN32Premul(this->width(), this->height()))) {
         return false;
     }
 
diff --git a/src/gpu/SkGrPixelRef.cpp b/src/gpu/SkGrPixelRef.cpp
index d378032..2371ea7 100644
--- a/src/gpu/SkGrPixelRef.cpp
+++ b/src/gpu/SkGrPixelRef.cpp
@@ -170,7 +170,7 @@
         top = 0;
         height = this->info().fHeight;
     }
-    if (!dst->allocPixels(SkImageInfo::MakeN32Premul(width, height))) {
+    if (!dst->tryAllocN32Pixels(width, height)) {
         SkDebugf("SkGrPixelRef::onReadPixels failed to alloc bitmap for result!\n");
         return false;
     }
diff --git a/src/image/SkImage.cpp b/src/image/SkImage.cpp
index 795b842..a606384 100644
--- a/src/image/SkImage.cpp
+++ b/src/image/SkImage.cpp
@@ -117,9 +117,8 @@
             return false;
         }
     } else {
-        const SkImageInfo info = SkImageInfo::MakeN32Premul(subset.width(), subset.height());
         SkBitmap tmp;
-        if (!tmp.allocPixels(info)) {
+        if (!tmp.tryAllocN32Pixels(subset.width(), subset.height())) {
             return false;
         }
         *bitmap = tmp;
diff --git a/src/images/SkDecodingImageGenerator.cpp b/src/images/SkDecodingImageGenerator.cpp
index dfa093b..5c66c94 100644
--- a/src/images/SkDecodingImageGenerator.cpp
+++ b/src/images/SkDecodingImageGenerator.cpp
@@ -69,7 +69,7 @@
     virtual bool allocPixelRef(SkBitmap* bm, SkColorTable* ct) {
         if (NULL == fTarget || !equal_modulo_alpha(fInfo, bm->info())) {
             // Call default allocator.
-            return bm->allocPixels(NULL, ct);
+            return bm->tryAllocPixels(NULL, ct);
         }
 
         // TODO(halcanary): verify that all callers of this function
diff --git a/src/images/SkImageDecoder.cpp b/src/images/SkImageDecoder.cpp
index ed2ad16..4a5e734 100644
--- a/src/images/SkImageDecoder.cpp
+++ b/src/images/SkImageDecoder.cpp
@@ -133,7 +133,7 @@
 
 bool SkImageDecoder::allocPixelRef(SkBitmap* bitmap,
                                    SkColorTable* ctable) const {
-    return bitmap->allocPixels(fAllocator, ctable);
+    return bitmap->tryAllocPixels(fAllocator, ctable);
 }
 
 ///////////////////////////////////////////////////////////////////////////////
diff --git a/src/images/SkImageDecoder_libjpeg.cpp b/src/images/SkImageDecoder_libjpeg.cpp
index 9b93716..99401e6 100644
--- a/src/images/SkImageDecoder_libjpeg.cpp
+++ b/src/images/SkImageDecoder_libjpeg.cpp
@@ -852,7 +852,7 @@
             return return_false(*cinfo, bitmap, "allocPixelRef");
         }
     } else {
-        if (!bitmap.allocPixels()) {
+        if (!bitmap.tryAllocPixels()) {
             return return_false(*cinfo, bitmap, "allocPixels");
         }
     }
diff --git a/src/images/SkImageDecoder_libpng.cpp b/src/images/SkImageDecoder_libpng.cpp
index 01b7c69..b69f879 100644
--- a/src/images/SkImageDecoder_libpng.cpp
+++ b/src/images/SkImageDecoder_libpng.cpp
@@ -823,7 +823,7 @@
             return false;
         }
     } else {
-        if (!decodedBitmap.allocPixels(NULL, needColorTable ? colorTable : NULL)) {
+        if (!decodedBitmap.tryAllocPixels(NULL, needColorTable ? colorTable : NULL)) {
             return false;
         }
     }
diff --git a/src/images/SkImageDecoder_libwebp.cpp b/src/images/SkImageDecoder_libwebp.cpp
index f7cfa8b..f32587d 100644
--- a/src/images/SkImageDecoder_libwebp.cpp
+++ b/src/images/SkImageDecoder_libwebp.cpp
@@ -375,7 +375,7 @@
         // alloc from native heap if it is a temp bitmap. (prevent GC)
         bool allocResult = (bitmap == decodedBitmap)
                                ? allocPixelRef(bitmap, NULL)
-                               : bitmap->allocPixels();
+                               : bitmap->tryAllocPixels();
         if (!allocResult) {
             return return_false(*decodedBitmap, "allocPixelRef");
         }
diff --git a/src/images/SkMovie_gif.cpp b/src/images/SkMovie_gif.cpp
index decefd5..3810db5 100644
--- a/src/images/SkMovie_gif.cpp
+++ b/src/images/SkMovie_gif.cpp
@@ -364,11 +364,11 @@
         startIndex = 0;
 
         // create bitmap
-        if (!bm->allocPixels(SkImageInfo::MakeN32Premul(width, height))) {
+        if (!bm->tryAllocN32Pixels(width, height)) {
             return false;
         }
         // create bitmap for backup
-        if (!fBackup.allocPixels(SkImageInfo::MakeN32Premul(width, height))) {
+        if (!fBackup.tryAllocN32Pixels(width, height)) {
             return false;
         }
     } else if (startIndex > fCurrIndex) {
diff --git a/src/lazy/SkCachingPixelRef.cpp b/src/lazy/SkCachingPixelRef.cpp
index 5643188..bd67532 100644
--- a/src/lazy/SkCachingPixelRef.cpp
+++ b/src/lazy/SkCachingPixelRef.cpp
@@ -46,7 +46,7 @@
     const SkImageInfo& info = this->info();
     if (!SkBitmapCache::Find(this->getGenerationID(), info.fWidth, info.fHeight, &fLockedBitmap)) {
         // Cache has been purged, must re-decode.
-        if (!fLockedBitmap.allocPixels(info, fRowBytes)) {
+        if (!fLockedBitmap.tryAllocPixels(info, fRowBytes)) {
             fErrorInDecoding = true;
             return false;
         }
diff --git a/src/lazy/SkDiscardablePixelRef.cpp b/src/lazy/SkDiscardablePixelRef.cpp
index a86c3de..ec8d5ea 100644
--- a/src/lazy/SkDiscardablePixelRef.cpp
+++ b/src/lazy/SkDiscardablePixelRef.cpp
@@ -110,7 +110,7 @@
 
     SkASSERT(info.colorType() != kUnknown_SkColorType);
     if (dst->empty()) {  // Use a normal pixelref.
-        return dst->allocPixels();
+        return dst->tryAllocPixels();
     }
     SkAutoTUnref<SkDiscardablePixelRef> ref(
         SkNEW_ARGS(SkDiscardablePixelRef,
diff --git a/src/pdf/SkPDFDevice.cpp b/src/pdf/SkPDFDevice.cpp
index ec07a8f..f4ea694 100644
--- a/src/pdf/SkPDFDevice.cpp
+++ b/src/pdf/SkPDFDevice.cpp
@@ -2120,7 +2120,7 @@
 
         const int w = SkScalarCeilToInt(physicalPerspectiveOutline.getBounds().width());
         const int h = SkScalarCeilToInt(physicalPerspectiveOutline.getBounds().height());
-        if (!perspectiveBitmap.allocPixels(SkImageInfo::MakeN32Premul(w, h))) {
+        if (!perspectiveBitmap.tryAllocN32Pixels(w, h)) {
             return;
         }
         perspectiveBitmap.eraseColor(SK_ColorTRANSPARENT);
diff --git a/src/pdf/SkPDFImage.cpp b/src/pdf/SkPDFImage.cpp
index 4946051..0c9b741 100644
--- a/src/pdf/SkPDFImage.cpp
+++ b/src/pdf/SkPDFImage.cpp
@@ -375,8 +375,7 @@
 static SkBitmap unpremultiply_bitmap(const SkBitmap& bitmap,
                                      const SkIRect& srcRect) {
     SkBitmap outBitmap;
-    SkAssertResult(outBitmap.allocPixels(
-            bitmap.info().makeWH(srcRect.width(), srcRect.height())));
+    outBitmap.allocPixels(bitmap.info().makeWH(srcRect.width(), srcRect.height()));
     int dstRow = 0;
 
     SkAutoLockPixels outBitmapPixelLock(outBitmap);
diff --git a/src/utils/SkPDFRasterizer.cpp b/src/utils/SkPDFRasterizer.cpp
index 6663480..1cb792f 100644
--- a/src/utils/SkPDFRasterizer.cpp
+++ b/src/utils/SkPDFRasterizer.cpp
@@ -50,7 +50,7 @@
   char *imgData = image.data();
 
   SkBitmap bitmap;
-  if (!bitmap.allocPixels(SkImageInfo::MakeN32Premul(width, height))) {
+  if (!bitmap.tryAllocN32Pixels(width, height)) {
     return false;
   }
   bitmap.eraseColor(SK_ColorWHITE);
diff --git a/src/utils/mac/SkCreateCGImageRef.cpp b/src/utils/mac/SkCreateCGImageRef.cpp
index 01b774d..14a24d8 100644
--- a/src/utils/mac/SkCreateCGImageRef.cpp
+++ b/src/utils/mac/SkCreateCGImageRef.cpp
@@ -221,7 +221,7 @@
     int h = (int)CGRectGetHeight(bounds);
 
     SkBitmap bitmap;
-    if (!bitmap.allocPixels(SkImageInfo::MakeN32Premul(w, h))) {
+    if (!bitmap.tryAllocN32Pixels(w, h)) {
         return false;
     }
     bitmap.eraseColor(SK_ColorWHITE);
@@ -287,7 +287,7 @@
     SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
 
     SkBitmap tmp;
-    if (!tmp.allocPixels(info)) {
+    if (!tmp.tryAllocPixels(info)) {
         return false;
     }
 
diff --git a/tests/ARGBImageEncoderTest.cpp b/tests/ARGBImageEncoderTest.cpp
index 18f315f..4d16f4c 100644
--- a/tests/ARGBImageEncoderTest.cpp
+++ b/tests/ARGBImageEncoderTest.cpp
@@ -35,9 +35,8 @@
         // A bitmap that should generate the above bytes:
         SkBitmap bitmap;
         {
-            bool success = bitmap.allocPixels(SkImageInfo::Make(kWidth, kHeight,
-                                                        gColorTypes[ctIndex], kOpaque_SkAlphaType));
-            REPORTER_ASSERT(reporter, success);
+            bitmap.allocPixels(SkImageInfo::Make(kWidth, kHeight, gColorTypes[ctIndex],
+                                                 kOpaque_SkAlphaType));
             bitmap.eraseColor(SK_ColorBLUE);
             // Change rows [0,1] from blue to [red,green].
             SkCanvas canvas(bitmap);
diff --git a/tests/BitmapCopyTest.cpp b/tests/BitmapCopyTest.cpp
index 4a49a6b..6a20668 100644
--- a/tests/BitmapCopyTest.cpp
+++ b/tests/BitmapCopyTest.cpp
@@ -387,14 +387,16 @@
                 ct = init_ctable(kPremul_SkAlphaType);
             }
 
+            int localSubW;
             if (isExtracted[copyCase]) { // A larger image to extract from.
-                src.allocPixels(SkImageInfo::Make(2 * subW + 1, subH,
-                                                  gPairs[i].fColorType,
-                                                  kPremul_SkAlphaType));
+                localSubW = 2 * subW + 1;
             } else { // Tests expect a 2x2 bitmap, so make smaller.
-                src.allocPixels(SkImageInfo::Make(subW, subH,
-                                                  gPairs[i].fColorType,
-                                                  kPremul_SkAlphaType));
+                localSubW = subW;
+            }
+            // could fail if we pass kIndex_8 for the colortype
+            if (src.tryAllocPixels(SkImageInfo::Make(localSubW, subH, gPairs[i].fColorType,
+                                                     kPremul_SkAlphaType))) {
+                // failure is fine, as we will notice later on
             }
             SkSafeUnref(ct);
 
diff --git a/tests/BitmapHasherTest.cpp b/tests/BitmapHasherTest.cpp
index e39439a..3b51706 100644
--- a/tests/BitmapHasherTest.cpp
+++ b/tests/BitmapHasherTest.cpp
@@ -17,8 +17,7 @@
 // Fill in bitmap with test data.
 static void CreateTestBitmap(SkBitmap* bitmap, int width, int height,
                              SkColor color, skiatest::Reporter* reporter) {
-    SkImageInfo info = SkImageInfo::MakeN32(width, height, kOpaque_SkAlphaType);
-    REPORTER_ASSERT(reporter, bitmap->allocPixels(info));
+    bitmap->allocN32Pixels(width, height, kOpaque_SkAlphaType);
     bitmap->eraseColor(color);
 }
 
diff --git a/tests/BitmapTest.cpp b/tests/BitmapTest.cpp
index 42ed884..4826b83 100644
--- a/tests/BitmapTest.cpp
+++ b/tests/BitmapTest.cpp
@@ -75,7 +75,7 @@
             bool setConf = bm.setInfo(SkImageInfo::MakeN32Premul(width, height));
             REPORTER_ASSERT(reporter, setConf);
             if (setConf) {
-                REPORTER_ASSERT(reporter, bm.allocPixels(NULL));
+                bm.allocPixels();
             }
             REPORTER_ASSERT(reporter, SkToBool(width & height) != bm.empty());
         }
diff --git a/tests/CachedDecodingPixelRefTest.cpp b/tests/CachedDecodingPixelRefTest.cpp
index 65623d0..7c63a0e 100644
--- a/tests/CachedDecodingPixelRefTest.cpp
+++ b/tests/CachedDecodingPixelRefTest.cpp
@@ -348,8 +348,7 @@
         REPORTER_ASSERT(r, TestImageGenerator::Height() == image->height());
 
         SkBitmap bitmap;
-        SkAssertResult(bitmap.allocN32Pixels(TestImageGenerator::Width(),
-                                             TestImageGenerator::Height()));
+        bitmap.allocN32Pixels(TestImageGenerator::Width(), TestImageGenerator::Height());
         SkCanvas canvas(bitmap);
         const SkColor kDefaultColor = 0xffabcdef;
         canvas.clear(kDefaultColor);
diff --git a/tests/DrawBitmapRectTest.cpp b/tests/DrawBitmapRectTest.cpp
index 6dca98b..720155c 100644
--- a/tests/DrawBitmapRectTest.cpp
+++ b/tests/DrawBitmapRectTest.cpp
@@ -190,7 +190,9 @@
     c.concat(matrix);
 
     SkBitmap bm;
-    bm.allocN32Pixels(width, height);
+    if (bm.tryAllocN32Pixels(width, height)) {
+        // allow this to fail silently, to test the code downstream
+    }
     bm.eraseColor(SK_ColorRED);
 
     matrix.setAll(0.0078740157f,
diff --git a/tests/ImageDecodingTest.cpp b/tests/ImageDecodingTest.cpp
index 8838e75..4d26167 100644
--- a/tests/ImageDecodingTest.cpp
+++ b/tests/ImageDecodingTest.cpp
@@ -770,7 +770,7 @@
             fSize = 0;
             return true;
         }
-        return bm->allocPixels(NULL, ct);
+        return bm->tryAllocPixels(NULL, ct);
     }
     bool ready() { return fPixels != NULL; }
 private:
diff --git a/tests/KtxTest.cpp b/tests/KtxTest.cpp
index bddd09a..32dacd5 100644
--- a/tests/KtxTest.cpp
+++ b/tests/KtxTest.cpp
@@ -28,8 +28,7 @@
     SkRandom rand(0x1005cbad);
 
     SkBitmap bm8888;
-    bool pixelsAllocated = bm8888.allocN32Pixels(128, 128);
-    REPORTER_ASSERT(reporter, pixelsAllocated);
+    bm8888.allocN32Pixels(128, 128);
 
     uint8_t *pixels = reinterpret_cast<uint8_t*>(bm8888.getPixels());
     REPORTER_ASSERT(reporter, NULL != pixels);
diff --git a/tests/PathOpsSkpClipTest.cpp b/tests/PathOpsSkpClipTest.cpp
index d413a08..cdc3c1f 100755
--- a/tests/PathOpsSkpClipTest.cpp
+++ b/tests/PathOpsSkpClipTest.cpp
@@ -482,8 +482,7 @@
         do {
             int dimX = SkScalarCeilToInt(width / fScale);
             int dimY = SkScalarCeilToInt(height / fScale);
-            if (oldBitmap.allocN32Pixels(dimX, dimY) &&
-                opBitmap.allocN32Pixels(dimX, dimY)) {
+            if (oldBitmap.tryAllocN32Pixels(dimX, dimY) && opBitmap.tryAllocN32Pixels(dimX, dimY)) {
                 break;
             }
             SkDebugf("-%d-", fScale);
diff --git a/tests/ReadPixelsTest.cpp b/tests/ReadPixelsTest.cpp
index d0bf903..77aac1f 100644
--- a/tests/ReadPixelsTest.cpp
+++ b/tests/ReadPixelsTest.cpp
@@ -96,9 +96,7 @@
 static void fillCanvas(SkCanvas* canvas) {
     static SkBitmap bmp;
     if (bmp.isNull()) {
-        SkDEBUGCODE(bool alloc =) bmp.allocN32Pixels(DEV_W, DEV_H);
-        SkASSERT(alloc);
-        SkAutoLockPixels alp(bmp);
+        bmp.allocN32Pixels(DEV_W, DEV_H);
         intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels());
         for (int y = 0; y < DEV_H; ++y) {
             for (int x = 0; x < DEV_W; ++x) {
diff --git a/tests/SerializationTest.cpp b/tests/SerializationTest.cpp
index 4e11f12..450f94f 100644
--- a/tests/SerializationTest.cpp
+++ b/tests/SerializationTest.cpp
@@ -335,14 +335,12 @@
     compare_bitmaps(reporter, origBitmap, destBitmap);
 }
 
-static bool setup_bitmap_for_canvas(SkBitmap* bitmap) {
-    SkImageInfo info = SkImageInfo::Make(
-        kBitmapSize, kBitmapSize, kN32_SkColorType, kPremul_SkAlphaType);
-    return bitmap->allocPixels(info);
+static void setup_bitmap_for_canvas(SkBitmap* bitmap) {
+    bitmap->allocN32Pixels(kBitmapSize, kBitmapSize);
 }
 
-static bool make_checkerboard_bitmap(SkBitmap& bitmap) {
-    bool success = setup_bitmap_for_canvas(&bitmap);
+static void make_checkerboard_bitmap(SkBitmap& bitmap) {
+    setup_bitmap_for_canvas(&bitmap);
 
     SkCanvas canvas(bitmap);
     canvas.clear(0x00000000);
@@ -363,14 +361,12 @@
             canvas.restore();
         }
     }
-
-    return success;
 }
 
-static bool draw_something(SkCanvas* canvas) {
+static void draw_something(SkCanvas* canvas) {
     SkPaint paint;
     SkBitmap bitmap;
-    bool success = make_checkerboard_bitmap(bitmap);
+    make_checkerboard_bitmap(bitmap);
 
     canvas->save();
     canvas->scale(0.5f, 0.5f);
@@ -389,8 +385,6 @@
     paint.setColor(SK_ColorBLACK);
     paint.setTextSize(SkIntToScalar(kBitmapSize/3));
     canvas->drawText("Picture", 7, SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), paint);
-
-    return success;
 }
 
 DEF_TEST(Serialization, reporter) {
@@ -481,10 +475,9 @@
     // Test simple SkPicture serialization
     {
         SkPictureRecorder recorder;
-        bool didDraw = draw_something(recorder.beginRecording(SkIntToScalar(kBitmapSize), 
-                                                              SkIntToScalar(kBitmapSize), 
-                                                              NULL, 0));
-        REPORTER_ASSERT(reporter, didDraw);
+        draw_something(recorder.beginRecording(SkIntToScalar(kBitmapSize),
+                                               SkIntToScalar(kBitmapSize),
+                                               NULL, 0));
         SkAutoTUnref<SkPicture> pict(recorder.endRecording());
 
         // Serialize picture
diff --git a/tests/SkResourceCacheTest.cpp b/tests/SkResourceCacheTest.cpp
index b71c744..454f8c2 100644
--- a/tests/SkResourceCacheTest.cpp
+++ b/tests/SkResourceCacheTest.cpp
@@ -27,7 +27,7 @@
     SkAutoTUnref<SkCanvas> canvas(
             SkCanvas::NewRasterN32(kCanvasSize, kCanvasSize));
     SkBitmap bitmap;
-    SkAssertResult(bitmap.allocN32Pixels(kBitmapSize, kBitmapSize));
+    bitmap.allocN32Pixels(kBitmapSize, kBitmapSize);
     bitmap.eraseColor(0xFFFFFFFF);
     SkScalar scale = SkIntToScalar(kScale);
     SkScalar scaledSize = SkIntToScalar(kBitmapSize) * scale;
diff --git a/tests/TextureCompressionTest.cpp b/tests/TextureCompressionTest.cpp
index da7a87b..b93cabb 100644
--- a/tests/TextureCompressionTest.cpp
+++ b/tests/TextureCompressionTest.cpp
@@ -58,8 +58,7 @@
     bool setInfoSuccess = bitmap.setInfo(info);
     REPORTER_ASSERT(reporter, setInfoSuccess);
 
-    bool allocPixelsSuccess = bitmap.allocPixels(info);
-    REPORTER_ASSERT(reporter, allocPixelsSuccess);
+    bitmap.allocPixels(info);
     bitmap.unlockPixels();
     
     for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) {
@@ -92,8 +91,7 @@
     bool setInfoSuccess = bitmap.setInfo(info);
     REPORTER_ASSERT(reporter, setInfoSuccess);
 
-    bool allocPixelsSuccess = bitmap.allocPixels(info);
-    REPORTER_ASSERT(reporter, allocPixelsSuccess);
+    bitmap.allocPixels(info);
     bitmap.unlockPixels();
 
     for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) {
@@ -128,8 +126,7 @@
     bool setInfoSuccess = bitmap.setInfo(info);
     REPORTER_ASSERT(reporter, setInfoSuccess);
 
-    bool allocPixelsSuccess = bitmap.allocPixels(info);
-    REPORTER_ASSERT(reporter, allocPixelsSuccess);
+    bitmap.allocPixels(info);
     bitmap.unlockPixels();
 
     // Populate bitmap
@@ -215,8 +212,7 @@
     bool setInfoSuccess = bitmap.setInfo(info);
     REPORTER_ASSERT(reporter, setInfoSuccess);
 
-    bool allocPixelsSuccess = bitmap.allocPixels(info);
-    REPORTER_ASSERT(reporter, allocPixelsSuccess);
+    bitmap.allocPixels(info);
     bitmap.unlockPixels();
 
     int latcDimX, latcDimY;
diff --git a/tests/WritePixelsTest.cpp b/tests/WritePixelsTest.cpp
index 2a8d059..f47c67b 100644
--- a/tests/WritePixelsTest.cpp
+++ b/tests/WritePixelsTest.cpp
@@ -115,8 +115,7 @@
 static void fillCanvas(SkCanvas* canvas) {
     SkBitmap bmp;
     if (bmp.isNull()) {
-        SkDEBUGCODE(bool alloc = ) bmp.allocN32Pixels(DEV_W, DEV_H);
-        SkASSERT(alloc);
+        bmp.allocN32Pixels(DEV_W, DEV_H);
         for (int y = 0; y < DEV_H; ++y) {
             for (int x = 0; x < DEV_W; ++x) {
                 *bmp.getAddr32(x, y) = getCanvasColor(x, y);