2929#include " CreateJavaOutputStreamAdaptor.h"
3030#include " Utils.h"
3131#include " JNIHelp.h"
32+ #include " SkTScopedPtr.h"
3233
3334#include < android_runtime/AndroidRuntime.h>
3435#include " android_util_Binder.h"
@@ -180,7 +181,8 @@ static jobject nativeNewInstanceFromAsset(JNIEnv* env, jobject clazz,
180181 * reportSizeToVM not supported
181182 */
182183static jobject nativeDecodeRegion (JNIEnv* env, jobject, SkBitmapRegionDecoder *brd,
183- int start_x, int start_y, int width, int height, jobject options) {
184+ int start_x, int start_y, int width, int height, jobject options) {
185+ jobject tileBitmap = NULL ;
184186 SkImageDecoder *decoder = brd->getDecoder ();
185187 int sampleSize = 1 ;
186188 SkBitmap::Config prefConfig = SkBitmap::kNo_Config ;
@@ -199,12 +201,12 @@ static jobject nativeDecodeRegion(JNIEnv* env, jobject, SkBitmapRegionDecoder *b
199201 doDither = env->GetBooleanField (options, gOptions_ditherFieldID );
200202 preferQualityOverSpeed = env->GetBooleanField (options,
201203 gOptions_preferQualityOverSpeedFieldID );
204+ // Get the bitmap for re-use if it exists.
205+ tileBitmap = env->GetObjectField (options, gOptions_bitmapFieldID );
202206 }
203207
204208 decoder->setDitherImage (doDither);
205209 decoder->setPreferQualityOverSpeed (preferQualityOverSpeed);
206- SkBitmap* bitmap = new SkBitmap;
207- SkAutoTDelete<SkBitmap> adb (bitmap);
208210 AutoDecoderCancel adc (options, decoder);
209211
210212 // To fix the race condition in case "requestCancelDecode"
@@ -219,6 +221,17 @@ static jobject nativeDecodeRegion(JNIEnv* env, jobject, SkBitmapRegionDecoder *b
219221 region.fTop = start_y;
220222 region.fRight = start_x + width;
221223 region.fBottom = start_y + height;
224+ SkBitmap* bitmap = NULL ;
225+ SkTScopedPtr<SkBitmap> adb;
226+
227+ if (tileBitmap != NULL ) {
228+ // Re-use bitmap.
229+ bitmap = GraphicsJNI::getNativeBitmap (env, tileBitmap);
230+ }
231+ if (bitmap == NULL ) {
232+ bitmap = new SkBitmap;
233+ adb.reset (bitmap);
234+ }
222235
223236 if (!brd->decodeRegion (bitmap, region, prefConfig, sampleSize)) {
224237 return nullObjectReturn (" decoder->decodeRegion returned false" );
@@ -235,12 +248,12 @@ static jobject nativeDecodeRegion(JNIEnv* env, jobject, SkBitmapRegionDecoder *b
235248 getMimeTypeString (env, decoder->getFormat ()));
236249 }
237250
238- // detach bitmap from its autodeleter, since we want to own it now
239- adb.detach ();
251+ if (tileBitmap != NULL ) {
252+ return tileBitmap;
253+ }
240254
241- SkPixelRef* pr = bitmap->pixelRef ();
242- // promise we will never change our pixels (great for sharing and pictures)
243- pr->setImmutable ();
255+ // detach bitmap from its autodeleter, since we want to own it now
256+ adb.release ();
244257
245258 JavaPixelAllocator* allocator = (JavaPixelAllocator*) decoder->getAllocator ();
246259 jbyteArray buff = allocator->getStorageObjAndReset ();
0 commit comments