diff options
author | Owen Lin <owenlin@google.com> | 2012-06-28 17:12:20 +0800 |
---|---|---|
committer | Owen Lin <owenlin@google.com> | 2012-07-03 18:05:47 +0800 |
commit | df4763cf8752fb1babc7244fc2be33ed87d3b072 (patch) | |
tree | 46e4206a429da568320f59a3391e5f22eb4c8b7d /src/com/android/gallery3d/data | |
parent | ca7d9bfb42c1d5037bcecdde30eb836755140b03 (diff) | |
download | android_packages_apps_Gallery2-df4763cf8752fb1babc7244fc2be33ed87d3b072.tar.gz android_packages_apps_Gallery2-df4763cf8752fb1babc7244fc2be33ed87d3b072.tar.bz2 android_packages_apps_Gallery2-df4763cf8752fb1babc7244fc2be33ed87d3b072.zip |
Don't reuse bitmap before HoneyComb.
Change-Id: I0df9bf0a8f43e28ed25b436d4c584dc3ceb43c1d
Diffstat (limited to 'src/com/android/gallery3d/data')
-rw-r--r-- | src/com/android/gallery3d/data/BitmapPool.java | 69 | ||||
-rw-r--r-- | src/com/android/gallery3d/data/DecodeUtils.java | 79 | ||||
-rw-r--r-- | src/com/android/gallery3d/data/ImageCacheRequest.java | 10 | ||||
-rw-r--r-- | src/com/android/gallery3d/data/LocalImage.java | 3 | ||||
-rw-r--r-- | src/com/android/gallery3d/data/MediaItem.java | 14 |
5 files changed, 99 insertions, 76 deletions
diff --git a/src/com/android/gallery3d/data/BitmapPool.java b/src/com/android/gallery3d/data/BitmapPool.java index 0fbd84ef5..1151dded7 100644 --- a/src/com/android/gallery3d/data/BitmapPool.java +++ b/src/com/android/gallery3d/data/BitmapPool.java @@ -17,14 +17,9 @@ package com.android.gallery3d.data; import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.graphics.BitmapFactory.Options; import com.android.gallery3d.common.Utils; -import com.android.gallery3d.ui.Log; -import com.android.gallery3d.util.ThreadPool.JobContext; -import java.io.FileDescriptor; import java.util.ArrayList; public class BitmapPool { @@ -94,67 +89,7 @@ public class BitmapPool { mPool.clear(); } - private Bitmap findCachedBitmap(JobContext jc, - byte[] data, int offset, int length, Options options) { - if (mOneSize) return getBitmap(); - DecodeUtils.decodeBounds(jc, data, offset, length, options); - return getBitmap(options.outWidth, options.outHeight); - } - - private Bitmap findCachedBitmap(JobContext jc, - FileDescriptor fileDescriptor, Options options) { - if (mOneSize) return getBitmap(); - DecodeUtils.decodeBounds(jc, fileDescriptor, options); - return getBitmap(options.outWidth, options.outHeight); - } - - public Bitmap decode(JobContext jc, - byte[] data, int offset, int length, BitmapFactory.Options options) { - if (options == null) options = new BitmapFactory.Options(); - if (options.inSampleSize < 1) options.inSampleSize = 1; - options.inPreferredConfig = Bitmap.Config.ARGB_8888; - options.inBitmap = (options.inSampleSize == 1) - ? findCachedBitmap(jc, data, offset, length, options) : null; - try { - Bitmap bitmap = DecodeUtils.decode(jc, data, offset, length, options); - if (options.inBitmap != null && options.inBitmap != bitmap) { - recycle(options.inBitmap); - options.inBitmap = null; - } - return bitmap; - } catch (IllegalArgumentException e) { - if (options.inBitmap == null) throw e; - - Log.w(TAG, "decode fail with a given bitmap, try decode to a new bitmap"); - recycle(options.inBitmap); - options.inBitmap = null; - return DecodeUtils.decode(jc, data, offset, length, options); - } - } - - // This is the same as the method above except the source data comes - // from a file descriptor instead of a byte array. - public Bitmap decode(JobContext jc, - FileDescriptor fileDescriptor, Options options) { - if (options == null) options = new BitmapFactory.Options(); - if (options.inSampleSize < 1) options.inSampleSize = 1; - options.inPreferredConfig = Bitmap.Config.ARGB_8888; - options.inBitmap = (options.inSampleSize == 1) - ? findCachedBitmap(jc, fileDescriptor, options) : null; - try { - Bitmap bitmap = DecodeUtils.decode(jc, fileDescriptor, options); - if (options.inBitmap != null&& options.inBitmap != bitmap) { - recycle(options.inBitmap); - options.inBitmap = null; - } - return bitmap; - } catch (IllegalArgumentException e) { - if (options.inBitmap == null) throw e; - - Log.w(TAG, "decode fail with a given bitmap, try decode to a new bitmap"); - recycle(options.inBitmap); - options.inBitmap = null; - return DecodeUtils.decode(jc, fileDescriptor, options); - } + public boolean isOneSize() { + return mOneSize; } } diff --git a/src/com/android/gallery3d/data/DecodeUtils.java b/src/com/android/gallery3d/data/DecodeUtils.java index e51dc3fa9..773af74bb 100644 --- a/src/com/android/gallery3d/data/DecodeUtils.java +++ b/src/com/android/gallery3d/data/DecodeUtils.java @@ -16,15 +16,18 @@ package com.android.gallery3d.data; +import android.annotation.TargetApi; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; import android.graphics.BitmapFactory.Options; import android.graphics.BitmapRegionDecoder; +import android.os.Build; import android.util.FloatMath; import com.android.gallery3d.common.BitmapUtils; import com.android.gallery3d.common.Utils; +import com.android.gallery3d.ui.Log; import com.android.gallery3d.util.ThreadPool.CancelListener; import com.android.gallery3d.util.ThreadPool.JobContext; @@ -33,7 +36,7 @@ import java.io.FileInputStream; import java.io.InputStream; public class DecodeUtils { - private static final String TAG = "DecodeService"; + private static final String TAG = "DecodeUtils"; private static class DecodeCanceller implements CancelListener { Options mOptions; @@ -230,4 +233,78 @@ public class DecodeUtils { return null; } } + + @TargetApi(Build.VERSION_CODES.HONEYCOMB) + public static Bitmap decode(JobContext jc, byte[] data, int offset, + int length, BitmapFactory.Options options, BitmapPool pool) { + if (pool == null) { + return decode(jc, data, offset, length, options); + } + + if (options == null) options = new BitmapFactory.Options(); + if (options.inSampleSize < 1) options.inSampleSize = 1; + options.inPreferredConfig = Bitmap.Config.ARGB_8888; + options.inBitmap = (options.inSampleSize == 1) + ? findCachedBitmap(pool, jc, data, offset, length, options) : null; + try { + Bitmap bitmap = decode(jc, data, offset, length, options); + if (options.inBitmap != null && options.inBitmap != bitmap) { + pool.recycle(options.inBitmap); + options.inBitmap = null; + } + return bitmap; + } catch (IllegalArgumentException e) { + if (options.inBitmap == null) throw e; + + Log.w(TAG, "decode fail with a given bitmap, try decode to a new bitmap"); + pool.recycle(options.inBitmap); + options.inBitmap = null; + return decode(jc, data, offset, length, options); + } + } + + // This is the same as the method above except the source data comes + // from a file descriptor instead of a byte array. + @TargetApi(Build.VERSION_CODES.HONEYCOMB) + public static Bitmap decode(JobContext jc, + FileDescriptor fileDescriptor, Options options, BitmapPool pool) { + if (pool == null) { + return decode(jc, fileDescriptor, options); + } + + if (options == null) options = new BitmapFactory.Options(); + if (options.inSampleSize < 1) options.inSampleSize = 1; + options.inPreferredConfig = Bitmap.Config.ARGB_8888; + options.inBitmap = (options.inSampleSize == 1) + ? findCachedBitmap(pool, jc, fileDescriptor, options) : null; + try { + Bitmap bitmap = DecodeUtils.decode(jc, fileDescriptor, options); + if (options.inBitmap != null && options.inBitmap != bitmap) { + pool.recycle(options.inBitmap); + options.inBitmap = null; + } + return bitmap; + } catch (IllegalArgumentException e) { + if (options.inBitmap == null) throw e; + + Log.w(TAG, "decode fail with a given bitmap, try decode to a new bitmap"); + pool.recycle(options.inBitmap); + options.inBitmap = null; + return decode(jc, fileDescriptor, options); + } + } + + private static Bitmap findCachedBitmap(BitmapPool pool, JobContext jc, + byte[] data, int offset, int length, Options options) { + if (pool.isOneSize()) return pool.getBitmap(); + decodeBounds(jc, data, offset, length, options); + return pool.getBitmap(options.outWidth, options.outHeight); + } + + private static Bitmap findCachedBitmap(BitmapPool pool, JobContext jc, + FileDescriptor fileDescriptor, Options options) { + if (pool.isOneSize()) return pool.getBitmap(); + decodeBounds(jc, fileDescriptor, options); + return pool.getBitmap(options.outWidth, options.outHeight); + } } diff --git a/src/com/android/gallery3d/data/ImageCacheRequest.java b/src/com/android/gallery3d/data/ImageCacheRequest.java index 81660c915..b977b1016 100644 --- a/src/com/android/gallery3d/data/ImageCacheRequest.java +++ b/src/com/android/gallery3d/data/ImageCacheRequest.java @@ -57,11 +57,13 @@ abstract class ImageCacheRequest implements Job<Bitmap> { options.inPreferredConfig = Bitmap.Config.ARGB_8888; Bitmap bitmap; if (mType == MediaItem.TYPE_MICROTHUMBNAIL) { - bitmap = MediaItem.getMicroThumbPool().decode(jc, - buffer.data, buffer.offset, buffer.length, options); + bitmap = DecodeUtils.decode(jc, + buffer.data, buffer.offset, buffer.length, options, + MediaItem.getMicroThumbPool()); } else { - bitmap = MediaItem.getThumbPool().decode(jc, - buffer.data, buffer.offset, buffer.length, options); + bitmap = DecodeUtils.decode(jc, + buffer.data, buffer.offset, buffer.length, options, + MediaItem.getThumbPool()); } if (bitmap == null && !jc.isCancelled()) { Log.w(TAG, "decode cached failed " + debugTag); diff --git a/src/com/android/gallery3d/data/LocalImage.java b/src/com/android/gallery3d/data/LocalImage.java index 316e32469..c432ab44d 100644 --- a/src/com/android/gallery3d/data/LocalImage.java +++ b/src/com/android/gallery3d/data/LocalImage.java @@ -25,6 +25,7 @@ import android.graphics.BitmapFactory; import android.graphics.BitmapRegionDecoder; import android.media.ExifInterface; import android.net.Uri; +import android.os.Build; import android.provider.MediaStore.Images; import android.provider.MediaStore.Images.ImageColumns; import android.provider.MediaStore.MediaColumns; @@ -85,7 +86,7 @@ public class LocalImage extends LocalMediaItem { updateWidthAndHeightProjection(); } - @TargetApi(ApiHelper.VERSION_CODES.JELLY_BEAN) + @TargetApi(Build.VERSION_CODES.JELLY_BEAN) private static void updateWidthAndHeightProjection() { if (ApiHelper.HAS_MEDIA_COLUMNS_WIDTH_AND_HEIGHT) { PROJECTION[INDEX_WIDTH] = MediaColumns.WIDTH; diff --git a/src/com/android/gallery3d/data/MediaItem.java b/src/com/android/gallery3d/data/MediaItem.java index f0f1af434..15bb17623 100644 --- a/src/com/android/gallery3d/data/MediaItem.java +++ b/src/com/android/gallery3d/data/MediaItem.java @@ -19,6 +19,7 @@ package com.android.gallery3d.data; import android.graphics.Bitmap; import android.graphics.BitmapRegionDecoder; +import com.android.gallery3d.common.ApiHelper; import com.android.gallery3d.ui.ScreenNail; import com.android.gallery3d.util.ThreadPool.Job; @@ -42,9 +43,16 @@ public abstract class MediaItem extends MediaObject { private static final int BYTESBUFFE_POOL_SIZE = 4; private static final int BYTESBUFFER_SIZE = 200 * 1024; - private static final BitmapPool sMicroThumbPool = new BitmapPool( - MICROTHUMBNAIL_TARGET_SIZE, MICROTHUMBNAIL_TARGET_SIZE, 16); - private static final BitmapPool sThumbPool = new BitmapPool(4); + private static final BitmapPool sMicroThumbPool = + ApiHelper.HAS_REUSING_BITMAP_IN_BITMAP_FACTORY + ? new BitmapPool(MICROTHUMBNAIL_TARGET_SIZE, MICROTHUMBNAIL_TARGET_SIZE, 16) + : null; + + private static final BitmapPool sThumbPool = + ApiHelper.HAS_REUSING_BITMAP_IN_BITMAP_FACTORY + ? new BitmapPool(4) + : null; + private static final BytesBufferPool sMicroThumbBufferPool = new BytesBufferPool(BYTESBUFFE_POOL_SIZE, BYTESBUFFER_SIZE); |