ResourceCache.cpp revision 7c103a36f60b690e3fe83c40210e1cb0c76bba43
1/* 2 * Copyright (C) 2010 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 17#define LOG_TAG "OpenGLRenderer" 18 19#include "ResourceCache.h" 20#include "Caches.h" 21 22namespace android { 23 24using namespace uirenderer; 25ANDROID_SINGLETON_STATIC_INSTANCE(ResourceCache); 26 27namespace uirenderer { 28 29/////////////////////////////////////////////////////////////////////////////// 30// Resource cache 31/////////////////////////////////////////////////////////////////////////////// 32 33void ResourceCache::logCache() { 34 ALOGD("ResourceCache: cacheReport:"); 35 for (size_t i = 0; i < mCache->size(); ++i) { 36 ResourceReference* ref = mCache->valueAt(i); 37 ALOGD(" ResourceCache: mCache(%zu): resource, ref = 0x%p, 0x%p", 38 i, mCache->keyAt(i), mCache->valueAt(i)); 39 ALOGD(" ResourceCache: mCache(%zu): refCount, destroyed, type = %d, %d, %d", 40 i, ref->refCount, ref->destroyed, ref->resourceType); 41 } 42} 43 44ResourceCache::ResourceCache() { 45 Mutex::Autolock _l(mLock); 46 mCache = new KeyedVector<const void*, ResourceReference*>(); 47} 48 49ResourceCache::~ResourceCache() { 50 Mutex::Autolock _l(mLock); 51 delete mCache; 52} 53 54void ResourceCache::lock() { 55 mLock.lock(); 56} 57 58void ResourceCache::unlock() { 59 mLock.unlock(); 60} 61 62const SkBitmap* ResourceCache::insert(const SkBitmap& bitmapResource) { 63 Mutex::Autolock _l(mLock); 64 65 BitmapKey bitmapKey(bitmapResource); 66 ssize_t index = mBitmapCache.indexOfKey(bitmapKey); 67 if (index == NAME_NOT_FOUND) { 68 SkBitmap* cachedBitmap = new SkBitmap(bitmapResource); 69 index = mBitmapCache.add(bitmapKey, cachedBitmap); 70 return cachedBitmap; 71 } 72 73 mBitmapCache.keyAt(index).mRefCount++; 74 return mBitmapCache.valueAt(index); 75} 76 77void ResourceCache::incrementRefcount(void* resource, ResourceType resourceType) { 78 Mutex::Autolock _l(mLock); 79 incrementRefcountLocked(resource, resourceType); 80} 81 82void ResourceCache::incrementRefcount(const Res_png_9patch* patchResource) { 83 incrementRefcount((void*) patchResource, kNinePatch); 84} 85 86void ResourceCache::incrementRefcountLocked(void* resource, ResourceType resourceType) { 87 ssize_t index = mCache->indexOfKey(resource); 88 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : nullptr; 89 if (ref == nullptr || mCache->size() == 0) { 90 ref = new ResourceReference(resourceType); 91 mCache->add(resource, ref); 92 } 93 ref->refCount++; 94} 95 96void ResourceCache::decrementRefcount(void* resource) { 97 Mutex::Autolock _l(mLock); 98 decrementRefcountLocked(resource); 99} 100 101void ResourceCache::decrementRefcount(const SkBitmap* bitmapResource) { 102 Mutex::Autolock _l(mLock); 103 decrementRefcountLocked(bitmapResource); 104} 105 106void ResourceCache::decrementRefcount(const Res_png_9patch* patchResource) { 107 decrementRefcount((void*) patchResource); 108} 109 110void ResourceCache::decrementRefcountLocked(void* resource) { 111 ssize_t index = mCache->indexOfKey(resource); 112 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : nullptr; 113 if (ref == nullptr) { 114 // Should not get here - shouldn't get a call to decrement if we're not yet tracking it 115 return; 116 } 117 ref->refCount--; 118 if (ref->refCount == 0) { 119 deleteResourceReferenceLocked(resource, ref); 120 } 121} 122 123void ResourceCache::decrementRefcountLocked(const SkBitmap* bitmapResource) { 124 BitmapKey bitmapKey(*bitmapResource); 125 ssize_t index = mBitmapCache.indexOfKey(bitmapKey); 126 127 LOG_ALWAYS_FATAL_IF(index == NAME_NOT_FOUND, 128 "Decrementing the reference of an untracked Bitmap"); 129 130 const BitmapKey& cacheEntry = mBitmapCache.keyAt(index); 131 if (cacheEntry.mRefCount == 1) { 132 // delete the bitmap and remove it from the cache 133 delete mBitmapCache.valueAt(index); 134 mBitmapCache.removeItemsAt(index); 135 } else { 136 cacheEntry.mRefCount--; 137 } 138} 139 140void ResourceCache::decrementRefcountLocked(const Res_png_9patch* patchResource) { 141 decrementRefcountLocked((void*) patchResource); 142} 143 144void ResourceCache::destructor(Res_png_9patch* resource) { 145 Mutex::Autolock _l(mLock); 146 destructorLocked(resource); 147} 148 149void ResourceCache::destructorLocked(Res_png_9patch* resource) { 150 ssize_t index = mCache->indexOfKey(resource); 151 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : nullptr; 152 if (ref == nullptr) { 153 // If we're not tracking this resource, just delete it 154 if (Caches::hasInstance()) { 155 Caches::getInstance().patchCache.removeDeferred(resource); 156 } else { 157 // A Res_png_9patch is actually an array of byte that's larger 158 // than sizeof(Res_png_9patch). It must be freed as an array. 159 delete[] (int8_t*) resource; 160 } 161 return; 162 } 163 ref->destroyed = true; 164 if (ref->refCount == 0) { 165 deleteResourceReferenceLocked(resource, ref); 166 } 167} 168 169/** 170 * This method should only be called while the mLock mutex is held (that mutex is grabbed 171 * by the various destructor() and recycle() methods which call this method). 172 */ 173void ResourceCache::deleteResourceReferenceLocked(const void* resource, ResourceReference* ref) { 174 if (ref->destroyed) { 175 switch (ref->resourceType) { 176 case kNinePatch: { 177 if (Caches::hasInstance()) { 178 Caches::getInstance().patchCache.removeDeferred((Res_png_9patch*) resource); 179 } else { 180 // A Res_png_9patch is actually an array of byte that's larger 181 // than sizeof(Res_png_9patch). It must be freed as an array. 182 int8_t* patch = (int8_t*) resource; 183 delete[] patch; 184 } 185 } 186 break; 187 } 188 } 189 mCache->removeItem(resource); 190 delete ref; 191} 192 193/////////////////////////////////////////////////////////////////////////////// 194// Bitmap Key 195/////////////////////////////////////////////////////////////////////////////// 196 197void BitmapKey::operator=(const BitmapKey& other) { 198 this->mRefCount = other.mRefCount; 199 this->mBitmapDimensions = other.mBitmapDimensions; 200 this->mPixelRefOrigin = other.mPixelRefOrigin; 201 this->mPixelRefStableID = other.mPixelRefStableID; 202} 203 204bool BitmapKey::operator==(const BitmapKey& other) const { 205 return mPixelRefStableID == other.mPixelRefStableID && 206 mPixelRefOrigin == other.mPixelRefOrigin && 207 mBitmapDimensions == other.mBitmapDimensions; 208} 209 210bool BitmapKey::operator<(const BitmapKey& other) const { 211 if (mPixelRefStableID != other.mPixelRefStableID) { 212 return mPixelRefStableID < other.mPixelRefStableID; 213 } 214 if (mPixelRefOrigin.x() != other.mPixelRefOrigin.x()) { 215 return mPixelRefOrigin.x() < other.mPixelRefOrigin.x(); 216 } 217 if (mPixelRefOrigin.y() != other.mPixelRefOrigin.y()) { 218 return mPixelRefOrigin.y() < other.mPixelRefOrigin.y(); 219 } 220 if (mBitmapDimensions.width() != other.mBitmapDimensions.width()) { 221 return mBitmapDimensions.width() < other.mBitmapDimensions.width(); 222 } 223 return mBitmapDimensions.height() < other.mBitmapDimensions.height(); 224} 225 226}; // namespace uirenderer 227}; // namespace android 228