ReusableBitmap.java revision 93a35b93dc582e38ff8ee5979754a16b4bf4da0c
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright (C) 2013 The Android Open Source Project 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Licensed under the Apache License, Version 2.0 (the "License"); 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * you may not use this file except in compliance with the License. 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * You may obtain a copy of the License at 72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * http://www.apache.org/licenses/LICENSE-2.0 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Unless required by applicable law or agreed to in writing, software 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * distributed under the License is distributed on an "AS IS" BASIS, 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * See the License for the specific language governing permissions and 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * limitations under the License. 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */ 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)package com.android.bitmap; 182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import android.graphics.Bitmap; 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/** 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * A simple bitmap wrapper. Currently supports reference counting and logical width/height 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * (which may differ from a bitmap's reported width/height due to bitmap reuse). 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */ 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)public class ReusableBitmap implements Poolable { 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 27 public final Bitmap bmp; 28 private int mWidth; 29 private int mHeight; 30 private int mOrientation; 31 32 private int mRefCount = 0; 33 private final boolean mReusable; 34 35 public ReusableBitmap(final Bitmap bitmap) { 36 this(bitmap, true /* reusable */); 37 } 38 39 public ReusableBitmap(final Bitmap bitmap, final boolean reusable) { 40 bmp = bitmap; 41 mReusable = reusable; 42 } 43 44 @Override 45 public boolean isEligibleForPooling() { 46 return mReusable; 47 } 48 49 public void setLogicalWidth(int w) { 50 mWidth = w; 51 } 52 53 public void setLogicalHeight(int h) { 54 mHeight = h; 55 } 56 57 public int getLogicalWidth() { 58 return mWidth; 59 } 60 61 public int getLogicalHeight() { 62 return mHeight; 63 } 64 65 public int getOrientation() { 66 return mOrientation; 67 } 68 69 public void setOrientation(final int orientation) { 70 mOrientation = orientation; 71 } 72 73 public int getByteCount() { 74 return bmp.getByteCount(); 75 } 76 77 @Override 78 public void acquireReference() { 79 mRefCount++; 80 } 81 82 @Override 83 public void releaseReference() { 84 if (mRefCount == 0) { 85 throw new IllegalStateException(); 86 } 87 mRefCount--; 88 } 89 90 @Override 91 public int getRefCount() { 92 return mRefCount; 93 } 94 95 @Override 96 public String toString() { 97 final StringBuilder sb = new StringBuilder("["); 98 sb.append(super.toString()); 99 sb.append(" refCount="); 100 sb.append(mRefCount); 101 sb.append(" mReusable="); 102 sb.append(mReusable); 103 sb.append(" bmp="); 104 sb.append(bmp); 105 sb.append(" logicalW/H="); 106 sb.append(mWidth); 107 sb.append("/"); 108 sb.append(mHeight); 109 if (bmp != null) { 110 sb.append(" sz="); 111 sb.append(bmp.getByteCount() >> 10); 112 sb.append("KB"); 113 } 114 sb.append("]"); 115 return sb.toString(); 116 } 117 118 /** 119 * Singleton class to represent a null Bitmap. We don't want to just use a regular 120 * ReusableBitmap with a null bmp field because that will render that ReusableBitmap useless 121 * and unable to be used by another decode process. 122 */ 123 public final static class NullReusableBitmap extends ReusableBitmap { 124 private static NullReusableBitmap sInstance; 125 126 /** 127 * Get a singleton. 128 */ 129 public static NullReusableBitmap getInstance() { 130 if (sInstance == null) { 131 sInstance = new NullReusableBitmap(); 132 } 133 return sInstance; 134 } 135 136 private NullReusableBitmap() { 137 super(null /* bmp */, false /* reusable */); 138 } 139 140 @Override 141 public int getByteCount() { 142 return 0; 143 } 144 145 @Override 146 public void releaseReference() { } 147 148 @Override 149 public void acquireReference() { } 150 } 151} 152