diff options
author | Owen Lin <owenlin@google.com> | 2012-04-09 10:15:35 +0800 |
---|---|---|
committer | Owen Lin <owenlin@google.com> | 2012-04-10 14:39:23 +0800 |
commit | d8d1cfdb80a7bcc950e8deb957a6c55ec83a68b0 (patch) | |
tree | c5a5f0dac151c4f4cec881e9862ce592343b9608 /src/com/android/gallery3d/data/ImageCacheRequest.java | |
parent | a66827d34d8c29a612787e3fa2557a53191494da (diff) | |
download | android_packages_apps_Snap-d8d1cfdb80a7bcc950e8deb957a6c55ec83a68b0.tar.gz android_packages_apps_Snap-d8d1cfdb80a7bcc950e8deb957a6c55ec83a68b0.tar.bz2 android_packages_apps_Snap-d8d1cfdb80a7bcc950e8deb957a6c55ec83a68b0.zip |
Add BytesBufferPool to prevent GC.
Change-Id: Ia8513ff380a60f102481cbf25650eca149b75064
Diffstat (limited to 'src/com/android/gallery3d/data/ImageCacheRequest.java')
-rw-r--r-- | src/com/android/gallery3d/data/ImageCacheRequest.java | 76 |
1 files changed, 40 insertions, 36 deletions
diff --git a/src/com/android/gallery3d/data/ImageCacheRequest.java b/src/com/android/gallery3d/data/ImageCacheRequest.java index d3d4f51e7..c10158bcd 100644 --- a/src/com/android/gallery3d/data/ImageCacheRequest.java +++ b/src/com/android/gallery3d/data/ImageCacheRequest.java @@ -21,7 +21,7 @@ import android.graphics.BitmapFactory; import com.android.gallery3d.app.GalleryApp; import com.android.gallery3d.common.BitmapUtils; -import com.android.gallery3d.data.ImageCacheService.ImageData; +import com.android.gallery3d.data.BytesBufferPool.BytesBuffer; import com.android.gallery3d.util.ThreadPool.Job; import com.android.gallery3d.util.ThreadPool.JobContext; @@ -41,52 +41,56 @@ abstract class ImageCacheRequest implements Job<Bitmap> { mTargetSize = targetSize; } + @Override public Bitmap run(JobContext jc) { String debugTag = mPath + "," + ((mType == MediaItem.TYPE_THUMBNAIL) ? "THUMB" : (mType == MediaItem.TYPE_MICROTHUMBNAIL) ? "MICROTHUMB" : "?"); ImageCacheService cacheService = mApplication.getImageCacheService(); - ImageData data = cacheService.getImageData(mPath, mType); - if (jc.isCancelled()) return null; - - if (data != null) { - BitmapFactory.Options options = new BitmapFactory.Options(); - options.inPreferredConfig = Bitmap.Config.ARGB_8888; - Bitmap bitmap; - if (mType == MediaItem.TYPE_MICROTHUMBNAIL) { - bitmap = MediaItem.getMicroThumbPool().decode(jc, - data.mData, data.mOffset, data.mData.length - data.mOffset, options); - } else { - bitmap = DecodeUtils.decode(jc, - data.mData, data.mOffset, data.mData.length - data.mOffset, options); - } - if (bitmap == null && !jc.isCancelled()) { - Log.w(TAG, "decode cached failed " + debugTag); - } - return bitmap; - } else { - Bitmap bitmap = onDecodeOriginal(jc, mType); + BytesBuffer buffer = MediaItem.getBytesBufferPool().get(); + try { + boolean found = cacheService.getImageData(mPath, mType, buffer); if (jc.isCancelled()) return null; - - if (bitmap == null) { - Log.w(TAG, "decode orig failed " + debugTag); - return null; - } - - if (mType == MediaItem.TYPE_MICROTHUMBNAIL) { - bitmap = BitmapUtils.resizeAndCropCenter(bitmap, mTargetSize, true); - } else { - bitmap = BitmapUtils.resizeDownBySideLength(bitmap, mTargetSize, true); + if (found) { + BitmapFactory.Options options = new BitmapFactory.Options(); + 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); + } else { + bitmap = DecodeUtils.decode(jc, + buffer.data, buffer.offset, buffer.length, options); + } + if (bitmap == null && !jc.isCancelled()) { + Log.w(TAG, "decode cached failed " + debugTag); + } + return bitmap; } - if (jc.isCancelled()) return null; + } finally { + MediaItem.getBytesBufferPool().recycle(buffer); + } + Bitmap bitmap = onDecodeOriginal(jc, mType); + if (jc.isCancelled()) return null; - byte[] array = BitmapUtils.compressToBytes(bitmap); - if (jc.isCancelled()) return null; + if (bitmap == null) { + Log.w(TAG, "decode orig failed " + debugTag); + return null; + } - cacheService.putImageData(mPath, mType, array); - return bitmap; + if (mType == MediaItem.TYPE_MICROTHUMBNAIL) { + bitmap = BitmapUtils.resizeAndCropCenter(bitmap, mTargetSize, true); + } else { + bitmap = BitmapUtils.resizeDownBySideLength(bitmap, mTargetSize, true); } + if (jc.isCancelled()) return null; + + byte[] array = BitmapUtils.compressToBytes(bitmap); + if (jc.isCancelled()) return null; + + cacheService.putImageData(mPath, mType, array); + return bitmap; } public abstract Bitmap onDecodeOriginal(JobContext jc, int targetSize); |