summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/data
diff options
context:
space:
mode:
authorBobby Georgescu <georgescu@google.com>2013-02-19 15:49:14 -0800
committerBobby Georgescu <georgescu@google.com>2013-02-22 12:56:23 -0800
commitf52ceba89962829aa12f5caba131580e8da85880 (patch)
treec2b59028ff12abcbfe4552ecb7cb528d795385cc /src/com/android/gallery3d/data
parenta96e8879473c9f9c937a5f14dbbd35579b4d0952 (diff)
downloadandroid_packages_apps_Gallery2-f52ceba89962829aa12f5caba131580e8da85880.tar.gz
android_packages_apps_Gallery2-f52ceba89962829aa12f5caba131580e8da85880.tar.bz2
android_packages_apps_Gallery2-f52ceba89962829aa12f5caba131580e8da85880.zip
Replace various BitmapPools with a smarter unified pool
Make all of gallery use a single shared pool, and pave the way for making the pool more adaptive based on the current workload. Change-Id: Ia32561ad50b1b9716ebe2fd32a7bf02737685dac
Diffstat (limited to 'src/com/android/gallery3d/data')
-rw-r--r--src/com/android/gallery3d/data/BitmapPool.java96
-rw-r--r--src/com/android/gallery3d/data/DecodeUtils.java43
-rw-r--r--src/com/android/gallery3d/data/ImageCacheRequest.java10
-rw-r--r--src/com/android/gallery3d/data/MediaItem.java24
4 files changed, 21 insertions, 152 deletions
diff --git a/src/com/android/gallery3d/data/BitmapPool.java b/src/com/android/gallery3d/data/BitmapPool.java
deleted file mode 100644
index 5bc6d672b..000000000
--- a/src/com/android/gallery3d/data/BitmapPool.java
+++ /dev/null
@@ -1,96 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.gallery3d.data;
-
-import android.graphics.Bitmap;
-
-import com.android.gallery3d.common.Utils;
-
-import java.util.ArrayList;
-
-public class BitmapPool {
- @SuppressWarnings("unused")
- private static final String TAG = "BitmapPool";
-
- private final ArrayList<Bitmap> mPool;
- private final int mPoolLimit;
-
- // mOneSize is true if the pool can only cache Bitmap with one size.
- private final boolean mOneSize;
- private final int mWidth, mHeight; // only used if mOneSize is true
-
- // Construct a BitmapPool which caches bitmap with the specified size.
- public BitmapPool(int width, int height, int poolLimit) {
- mWidth = width;
- mHeight = height;
- mPoolLimit = poolLimit;
- mPool = new ArrayList<Bitmap>(poolLimit);
- mOneSize = true;
- }
-
- // Construct a BitmapPool which caches bitmap with any size;
- public BitmapPool(int poolLimit) {
- mWidth = -1;
- mHeight = -1;
- mPoolLimit = poolLimit;
- mPool = new ArrayList<Bitmap>(poolLimit);
- mOneSize = false;
- }
-
- // Get a Bitmap from the pool.
- public synchronized Bitmap getBitmap() {
- Utils.assertTrue(mOneSize);
- int size = mPool.size();
- return size > 0 ? mPool.remove(size - 1) : null;
- }
-
- // Get a Bitmap from the pool with the specified size.
- public synchronized Bitmap getBitmap(int width, int height) {
- Utils.assertTrue(!mOneSize);
- for (int i = mPool.size() - 1; i >= 0; i--) {
- Bitmap b = mPool.get(i);
- if (b.getWidth() == width && b.getHeight() == height) {
- return mPool.remove(i);
- }
- }
- return null;
- }
-
- // Put a Bitmap into the pool, if the Bitmap has a proper size. Otherwise
- // the Bitmap will be recycled. If the pool is full, an old Bitmap will be
- // recycled.
- public void recycle(Bitmap bitmap) {
- if (bitmap == null) return;
- if (mOneSize && ((bitmap.getWidth() != mWidth) ||
- (bitmap.getHeight() != mHeight))) {
- bitmap.recycle();
- return;
- }
- synchronized (this) {
- if (mPool.size() >= mPoolLimit) mPool.remove(0);
- mPool.add(bitmap);
- }
- }
-
- public synchronized void clear() {
- mPool.clear();
- }
-
- public boolean isOneSize() {
- return mOneSize;
- }
-}
diff --git a/src/com/android/gallery3d/data/DecodeUtils.java b/src/com/android/gallery3d/data/DecodeUtils.java
index 4d3c99653..fa709157d 100644
--- a/src/com/android/gallery3d/data/DecodeUtils.java
+++ b/src/com/android/gallery3d/data/DecodeUtils.java
@@ -28,6 +28,7 @@ import android.util.FloatMath;
import com.android.gallery3d.common.ApiHelper;
import com.android.gallery3d.common.BitmapUtils;
import com.android.gallery3d.common.Utils;
+import com.android.photos.data.GalleryBitmapPool;
import com.android.gallery3d.ui.Log;
import com.android.gallery3d.util.ThreadPool.CancelListener;
import com.android.gallery3d.util.ThreadPool.JobContext;
@@ -246,21 +247,17 @@ public class DecodeUtils {
}
@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);
- }
-
+ public static Bitmap decodeUsingPool(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(pool, jc, data, offset, length, options) : null;
+ ? findCachedBitmap(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);
+ GalleryBitmapPool.getInstance().put(options.inBitmap);
options.inBitmap = null;
}
return bitmap;
@@ -268,7 +265,7 @@ public class DecodeUtils {
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);
+ GalleryBitmapPool.getInstance().put(options.inBitmap);
options.inBitmap = null;
return decode(jc, data, offset, length, options);
}
@@ -277,21 +274,17 @@ public class DecodeUtils {
// 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);
- }
-
+ public static Bitmap decodeUsingPool(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(pool, jc, fileDescriptor, options) : null;
+ ? findCachedBitmap(jc, fileDescriptor, options) : null;
try {
Bitmap bitmap = DecodeUtils.decode(jc, fileDescriptor, options);
if (options.inBitmap != null && options.inBitmap != bitmap) {
- pool.recycle(options.inBitmap);
+ GalleryBitmapPool.getInstance().put(options.inBitmap);
options.inBitmap = null;
}
return bitmap;
@@ -299,23 +292,21 @@ public class DecodeUtils {
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);
+ GalleryBitmapPool.getInstance().put(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();
+ private static Bitmap findCachedBitmap(JobContext jc, byte[] data,
+ int offset, int length, Options options) {
decodeBounds(jc, data, offset, length, options);
- return pool.getBitmap(options.outWidth, options.outHeight);
+ return GalleryBitmapPool.getInstance().get(options.outWidth, options.outHeight);
}
- private static Bitmap findCachedBitmap(BitmapPool pool, JobContext jc,
- FileDescriptor fileDescriptor, Options options) {
- if (pool.isOneSize()) return pool.getBitmap();
+ private static Bitmap findCachedBitmap(JobContext jc, FileDescriptor fileDescriptor,
+ Options options) {
decodeBounds(jc, fileDescriptor, options);
- return pool.getBitmap(options.outWidth, options.outHeight);
+ return GalleryBitmapPool.getInstance().get(options.outWidth, options.outHeight);
}
}
diff --git a/src/com/android/gallery3d/data/ImageCacheRequest.java b/src/com/android/gallery3d/data/ImageCacheRequest.java
index 3f937e365..475614962 100644
--- a/src/com/android/gallery3d/data/ImageCacheRequest.java
+++ b/src/com/android/gallery3d/data/ImageCacheRequest.java
@@ -60,13 +60,11 @@ abstract class ImageCacheRequest implements Job<Bitmap> {
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap;
if (mType == MediaItem.TYPE_MICROTHUMBNAIL) {
- bitmap = DecodeUtils.decode(jc,
- buffer.data, buffer.offset, buffer.length, options,
- MediaItem.getMicroThumbPool());
+ bitmap = DecodeUtils.decodeUsingPool(jc,
+ buffer.data, buffer.offset, buffer.length, options);
} else {
- bitmap = DecodeUtils.decode(jc,
- buffer.data, buffer.offset, buffer.length, options,
- MediaItem.getThumbPool());
+ bitmap = DecodeUtils.decodeUsingPool(jc,
+ buffer.data, buffer.offset, buffer.length, options);
}
if (bitmap == null && !jc.isCancelled()) {
Log.w(TAG, "decode cached failed " + debugTag());
diff --git a/src/com/android/gallery3d/data/MediaItem.java b/src/com/android/gallery3d/data/MediaItem.java
index 19084d41e..59ea86551 100644
--- a/src/com/android/gallery3d/data/MediaItem.java
+++ b/src/com/android/gallery3d/data/MediaItem.java
@@ -42,15 +42,10 @@ public abstract class MediaItem extends MediaObject {
private static final int BYTESBUFFER_SIZE = 200 * 1024;
private static int sMicrothumbnailTargetSize = 200;
- private static BitmapPool sMicroThumbPool;
private static final BytesBufferPool sMicroThumbBufferPool =
new BytesBufferPool(BYTESBUFFE_POOL_SIZE, BYTESBUFFER_SIZE);
private static int sThumbnailTargetSize = 640;
- private static final BitmapPool sThumbPool =
- ApiHelper.HAS_REUSING_BITMAP_IN_BITMAP_FACTORY
- ? new BitmapPool(4)
- : null;
// TODO: fix default value for latlng and change this.
public static final double INVALID_LATLNG = 0f;
@@ -126,33 +121,14 @@ public abstract class MediaItem extends MediaObject {
}
}
- public static BitmapPool getMicroThumbPool() {
- if (ApiHelper.HAS_REUSING_BITMAP_IN_BITMAP_FACTORY && sMicroThumbPool == null) {
- initializeMicroThumbPool();
- }
- return sMicroThumbPool;
- }
-
- public static BitmapPool getThumbPool() {
- return sThumbPool;
- }
-
public static BytesBufferPool getBytesBufferPool() {
return sMicroThumbBufferPool;
}
- private static void initializeMicroThumbPool() {
- sMicroThumbPool =
- ApiHelper.HAS_REUSING_BITMAP_IN_BITMAP_FACTORY
- ? new BitmapPool(sMicrothumbnailTargetSize, sMicrothumbnailTargetSize, 16)
- : null;
- }
-
public static void setThumbnailSizes(int size, int microSize) {
sThumbnailTargetSize = size;
if (sMicrothumbnailTargetSize != microSize) {
sMicrothumbnailTargetSize = microSize;
- initializeMicroThumbPool();
}
}
}