From 2a3d754549abc4b55e6cfc2d0c986d29782b2492 Mon Sep 17 00:00:00 2001 From: Bryan Mawhinney Date: Wed, 3 Nov 2010 17:23:57 +0000 Subject: Avoid copying byte arrays when just decoding bounds. Currently, if a caller specifies both "purgeable" and "just decode bounds" options when passing a byte array to decode, we create an unnecessary copy of the byte array. This is probably not common, but we may as well avoid the copy. Change-Id: I27e573b0e1fb8f8516729882a84efa02b6da08a5 --- core/jni/android/graphics/BitmapFactory.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/core/jni/android/graphics/BitmapFactory.cpp b/core/jni/android/graphics/BitmapFactory.cpp index 90a024387ed2..3b2406ce40d4 100644 --- a/core/jni/android/graphics/BitmapFactory.cpp +++ b/core/jni/android/graphics/BitmapFactory.cpp @@ -146,6 +146,11 @@ static bool optionsShareable(JNIEnv* env, jobject options) { env->GetBooleanField(options, gOptions_shareableFieldID); } +static bool optionsJustBounds(JNIEnv* env, jobject options) { + return options != NULL && + env->GetBooleanField(options, gOptions_justBoundsFieldID); +} + static bool optionsReportSizeToVM(JNIEnv* env, jobject options) { return NULL == options || !env->GetBooleanField(options, gOptions_nativeAllocFieldID); @@ -183,7 +188,7 @@ static jobject doDecode(JNIEnv* env, SkStream* stream, jobject padding, if (NULL != options) { sampleSize = env->GetIntField(options, gOptions_sampleSizeFieldID); - if (env->GetBooleanField(options, gOptions_justBoundsFieldID)) { + if (optionsJustBounds(env, options)) { mode = SkImageDecoder::kDecodeBounds_Mode; } // initialize these, in case we fail later on @@ -415,13 +420,14 @@ static jobject nativeDecodeByteArray(JNIEnv* env, jobject, jbyteArray byteArray, /* If optionsShareable() we could decide to just wrap the java array and share it, but that means adding a globalref to the java array object and managing its lifetime. For now we just always copy the array's data - if optionsPurgeable(). + if optionsPurgeable(), unless we're just decoding bounds. */ + bool purgeable = optionsPurgeable(env, options) + && !optionsJustBounds(env, options); AutoJavaByteArray ar(env, byteArray); - SkStream* stream = new SkMemoryStream(ar.ptr() + offset, length, - optionsPurgeable(env, options)); + SkStream* stream = new SkMemoryStream(ar.ptr() + offset, length, purgeable); SkAutoUnref aur(stream); - return doDecode(env, stream, NULL, options, true); + return doDecode(env, stream, NULL, options, purgeable); } static void nativeRequestCancel(JNIEnv*, jobject joptions) { -- cgit v1.2.3-59-g8ed1b