BitmapPool.java revision 7817979db0c52ffeacb951625b1e821eba303285
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.gallery3d.data;
18
19import android.graphics.Bitmap;
20
21import com.android.gallery3d.common.Utils;
22
23import java.util.ArrayList;
24
25public class BitmapPool {
26    @SuppressWarnings("unused")
27    private static final String TAG = "BitmapPool";
28
29    private final ArrayList<Bitmap> mPool;
30    private final int mPoolLimit;
31
32    // mOneSize is true if the pool can only cache Bitmap with one size.
33    private final boolean mOneSize;
34    private final int mWidth, mHeight;  // only used if mOneSize is true
35
36    // Construct a BitmapPool which caches bitmap with the specified size.
37    public BitmapPool(int width, int height, int poolLimit) {
38        mWidth = width;
39        mHeight = height;
40        mPoolLimit = poolLimit;
41        mPool = new ArrayList<Bitmap>(poolLimit);
42        mOneSize = true;
43    }
44
45    // Construct a BitmapPool which caches bitmap with any size;
46    public BitmapPool(int poolLimit) {
47        mWidth = -1;
48        mHeight = -1;
49        mPoolLimit = poolLimit;
50        mPool = new ArrayList<Bitmap>(poolLimit);
51        mOneSize = false;
52    }
53
54    // Get a Bitmap from the pool.
55    public synchronized Bitmap getBitmap() {
56        Utils.assertTrue(mOneSize);
57        int size = mPool.size();
58        return size > 0 ? mPool.remove(size - 1) : null;
59    }
60
61    // Get a Bitmap from the pool with the specified size.
62    public synchronized Bitmap getBitmap(int width, int height) {
63        Utils.assertTrue(!mOneSize);
64        for (int i = mPool.size() - 1; i >= 0; i--) {
65            Bitmap b = mPool.get(i);
66            if (b.getWidth() == width && b.getHeight() == height) {
67                return mPool.remove(i);
68            }
69        }
70        return null;
71    }
72
73    // Put a Bitmap into the pool, if the Bitmap has a proper size. Otherwise
74    // the Bitmap will be recycled. If the pool is full, an old Bitmap will be
75    // recycled.
76    public void recycle(Bitmap bitmap) {
77        if (bitmap == null) return;
78        if (mOneSize && ((bitmap.getWidth() != mWidth) ||
79                (bitmap.getHeight() != mHeight))) {
80            bitmap.recycle();
81            return;
82        }
83        synchronized (this) {
84            if (mPool.size() >= mPoolLimit) mPool.remove(0);
85            mPool.add(bitmap);
86        }
87    }
88
89    public synchronized void clear() {
90        mPool.clear();
91    }
92
93    public boolean isOneSize() {
94        return mOneSize;
95    }
96}
97