GalleryBitmapPool.java revision f52ceba89962829aa12f5caba131580e8da85880
1/* 2 * Copyright (C) 2013 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.photos.data; 18 19import android.graphics.Bitmap; 20import android.graphics.Point; 21import android.util.Pools.Pool; 22import android.util.Pools.SimplePool; 23 24import com.android.photos.data.SparseArrayBitmapPool.Node; 25 26public class GalleryBitmapPool { 27 28 private static final int CAPACITY_BYTES = 20971520; 29 private static final int POOL_INDEX_NONE = -1; 30 private static final int POOL_INDEX_SQUARE = 0; 31 private static final int POOL_INDEX_PHOTO = 1; 32 private static final int POOL_INDEX_MISC = 2; 33 34 private static final Point[] COMMON_PHOTO_ASPECT_RATIOS = 35 { new Point(4, 3), new Point(3, 2), new Point(16, 9) }; 36 37 private int mCapacityBytes; 38 private SparseArrayBitmapPool [] mPools; 39 private Pool<Node> mSharedNodePool = new SimplePool<Node>(128); 40 41 private GalleryBitmapPool(int capacityBytes) { 42 mPools = new SparseArrayBitmapPool[3]; 43 mPools[POOL_INDEX_SQUARE] = new SparseArrayBitmapPool(capacityBytes / 3, mSharedNodePool); 44 mPools[POOL_INDEX_PHOTO] = new SparseArrayBitmapPool(capacityBytes / 3, mSharedNodePool); 45 mPools[POOL_INDEX_MISC] = new SparseArrayBitmapPool(capacityBytes / 3, mSharedNodePool); 46 mCapacityBytes = capacityBytes; 47 } 48 49 private static GalleryBitmapPool sInstance; 50 public static GalleryBitmapPool getInstance() { 51 if (sInstance == null) { 52 sInstance = new GalleryBitmapPool(CAPACITY_BYTES); 53 } 54 return sInstance; 55 } 56 57 private SparseArrayBitmapPool getPoolForDimensions(int width, int height) { 58 int index = getPoolIndexForDimensions(width, height); 59 if (index == POOL_INDEX_NONE) { 60 return null; 61 } else { 62 return mPools[index]; 63 } 64 } 65 66 private int getPoolIndexForDimensions(int width, int height) { 67 if (width <= 0 || height <= 0) { 68 return POOL_INDEX_NONE; 69 } 70 if (width == height) { 71 return POOL_INDEX_SQUARE; 72 } 73 int min, max; 74 if (width > height) { 75 min = height; 76 max = width; 77 } else { 78 min = width; 79 max = height; 80 } 81 for (Point ar : COMMON_PHOTO_ASPECT_RATIOS) { 82 if (min * ar.x == max * ar.y) { 83 return POOL_INDEX_PHOTO; 84 } 85 } 86 return POOL_INDEX_MISC; 87 } 88 89 public synchronized int getCapacity() { 90 return mCapacityBytes; 91 } 92 93 public synchronized int getSize() { 94 int total = 0; 95 for (SparseArrayBitmapPool p : mPools) { 96 total += p.getSize(); 97 } 98 return total; 99 } 100 101 public Bitmap get(int width, int height) { 102 SparseArrayBitmapPool pool = getPoolForDimensions(width, height); 103 if (pool == null) { 104 return null; 105 } else { 106 return pool.get(width, height); 107 } 108 } 109 110 public boolean put(Bitmap b) { 111 if (b == null) { 112 return false; 113 } 114 SparseArrayBitmapPool pool = getPoolForDimensions(b.getWidth(), b.getHeight()); 115 if (pool == null) { 116 b.recycle(); 117 return false; 118 } else { 119 return pool.put(b); 120 } 121 } 122 123 public void clear() { 124 for (SparseArrayBitmapPool p : mPools) { 125 p.clear(); 126 } 127 } 128} 129