diff --git a/core/jni/android/graphics/BitmapRegionDecoder.cpp b/core/jni/android/graphics/BitmapRegionDecoder.cpp index 1e00b71961b17..c81db821589c6 100644 --- a/core/jni/android/graphics/BitmapRegionDecoder.cpp +++ b/core/jni/android/graphics/BitmapRegionDecoder.cpp @@ -248,8 +248,8 @@ static jobject nativeDecodeRegion(JNIEnv* env, jobject, SkBitmapRegionDecoder *b // promise we will never change our pixels (great for sharing and pictures) pr->setImmutable(); - // now create the java bitmap - jbyteArray buff = ((AndroidPixelRef*) pr)->getStorageObj(); + JavaPixelAllocator* allocator = (JavaPixelAllocator*) decoder->getAllocator(); + jbyteArray buff = allocator->getStorageObjAndReset(); return GraphicsJNI::createBitmap(env, bitmap, buff, false, NULL, -1); } diff --git a/core/jni/android/graphics/Graphics.cpp b/core/jni/android/graphics/Graphics.cpp index fc358a10a851f..770c896e2447c 100644 --- a/core/jni/android/graphics/Graphics.cpp +++ b/core/jni/android/graphics/Graphics.cpp @@ -564,7 +564,8 @@ bool GraphicsJNI::mallocPixelRef(JNIEnv* env, SkBitmap* bitmap, SkColorTable* ct JavaPixelAllocator::JavaPixelAllocator(JNIEnv* env, bool allocateInJavaHeap) : fAllocateInJavaHeap(allocateInJavaHeap), - fStorageObj(NULL) { + fStorageObj(NULL), + fAllocCount(0) { if (env->GetJavaVM(&fVM) != JNI_OK) { SkDebugf("------ [%p] env->GetJavaVM failed\n", env); sk_throw(); @@ -577,12 +578,13 @@ bool JavaPixelAllocator::allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) { // If allocating in the Java heap, only allow a single object to be // allocated for the lifetime of this object. if (fStorageObj != NULL) { - SkDebugf("ERROR: One-shot allocator has already allocated\n"); - sk_throw(); + SkDebugf("WARNING: One-shot allocator has already allocated (alloc count = %d)\n", fAllocCount); +// sk_throw(); } if (fAllocateInJavaHeap) { fStorageObj = GraphicsJNI::allocateJavaPixelRef(env, bitmap, ctable); + fAllocCount += 1; return fStorageObj != NULL; } return GraphicsJNI::mallocPixelRef(env, bitmap, ctable); diff --git a/core/jni/android/graphics/GraphicsJNI.h b/core/jni/android/graphics/GraphicsJNI.h index 505d24ebb8b96..7193b5927fca4 100644 --- a/core/jni/android/graphics/GraphicsJNI.h +++ b/core/jni/android/graphics/GraphicsJNI.h @@ -136,12 +136,28 @@ public: // overrides virtual bool allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable); - jbyteArray getStorageObj() { return fStorageObj; }; + /** Return the Java array object created for the last allocation. + * This returns a local JNI reference which the caller is responsible + * for storing appropriately (usually by passing it to the Bitmap + * constructor). + */ + jbyteArray getStorageObj() { return fStorageObj; } + + /** Same as getStorageObj(), but also resets the allocator so that it + * can allocate again. + */ + jbyteArray getStorageObjAndReset() { + jbyteArray result = fStorageObj; + fStorageObj = NULL; + fAllocCount = 0; + return result; + }; private: JavaVM* fVM; bool fAllocateInJavaHeap; jbyteArray fStorageObj; + int fAllocCount; }; class JavaMemoryUsageReporter : public SkVMMemoryReporter {