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/BitmapPool.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/BitmapPool.java')
-rw-r--r-- | src/com/android/gallery3d/data/BitmapPool.java | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/src/com/android/gallery3d/data/BitmapPool.java b/src/com/android/gallery3d/data/BitmapPool.java new file mode 100644 index 000000000..c52a57b0a --- /dev/null +++ b/src/com/android/gallery3d/data/BitmapPool.java @@ -0,0 +1,96 @@ +// Copyright 2012 Google Inc. All Rights Reserved. + +package com.android.gallery3d.data; + +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.BitmapFactory.Options; + +import com.android.gallery3d.ui.Log; +import com.android.gallery3d.util.ThreadPool.JobContext; + +import java.io.FileDescriptor; +import java.util.ArrayList; + +public class BitmapPool { + private static final String TAG = "BitmapPool"; + + private static final int POOL_SIZE = 16; + private final ArrayList<Bitmap> mPool = new ArrayList<Bitmap>(POOL_SIZE); + + private final int mWidth; + private final int mHeight; + + public BitmapPool(int width, int height) { + mWidth = width; + mHeight = height; + } + + public synchronized Bitmap getBitmap() { + int size = mPool.size(); + return size > 0 ? mPool.remove(size - 1) : null; + } + + public void recycle(Bitmap bitmap) { + if (bitmap == null) return; + if ((bitmap.getWidth() != mWidth) || (bitmap.getHeight() != mHeight)) { + bitmap.recycle(); + return; + } + synchronized (this) { + if (mPool.size() < POOL_SIZE) mPool.add(bitmap); + } + } + + public synchronized void clear() { + mPool.clear(); + } + + 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) ? getBitmap() : 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) ? getBitmap() : 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); + } + } +} |