ResourceCache.cpp revision 58ecc204fbcacef34806290492384677a330d4d4
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#include <SkPixelRef.h> 18#include "ResourceCache.h" 19#include "Caches.h" 20 21namespace android { 22namespace uirenderer { 23 24/////////////////////////////////////////////////////////////////////////////// 25// Resource cache 26/////////////////////////////////////////////////////////////////////////////// 27 28void ResourceCache::logCache() { 29 ALOGD("ResourceCache: cacheReport:"); 30 for (size_t i = 0; i < mCache->size(); ++i) { 31 ResourceReference* ref = mCache->valueAt(i); 32 ALOGD(" ResourceCache: mCache(%d): resource, ref = 0x%p, 0x%p", 33 i, mCache->keyAt(i), mCache->valueAt(i)); 34 ALOGD(" ResourceCache: mCache(%d): refCount, recycled, destroyed, type = %d, %d, %d, %d", 35 i, ref->refCount, ref->recycled, ref->destroyed, ref->resourceType); 36 } 37} 38 39ResourceCache::ResourceCache() { 40 Mutex::Autolock _l(mLock); 41 mCache = new KeyedVector<void*, ResourceReference*>(); 42} 43 44ResourceCache::~ResourceCache() { 45 Mutex::Autolock _l(mLock); 46 delete mCache; 47} 48 49void ResourceCache::lock() { 50 mLock.lock(); 51} 52 53void ResourceCache::unlock() { 54 mLock.unlock(); 55} 56 57void ResourceCache::incrementRefcount(void* resource, ResourceType resourceType) { 58 Mutex::Autolock _l(mLock); 59 incrementRefcountLocked(resource, resourceType); 60} 61 62void ResourceCache::incrementRefcount(SkBitmap* bitmapResource) { 63 SkSafeRef(bitmapResource->pixelRef()); 64 SkSafeRef(bitmapResource->getColorTable()); 65 incrementRefcount((void*) bitmapResource, kBitmap); 66} 67 68void ResourceCache::incrementRefcount(SkPath* pathResource) { 69 incrementRefcount((void*) pathResource, kPath); 70} 71 72void ResourceCache::incrementRefcount(SkiaShader* shaderResource) { 73 SkSafeRef(shaderResource->getSkShader()); 74 incrementRefcount((void*) shaderResource, kShader); 75} 76 77void ResourceCache::incrementRefcount(SkiaColorFilter* filterResource) { 78 SkSafeRef(filterResource->getSkColorFilter()); 79 incrementRefcount((void*) filterResource, kColorFilter); 80} 81 82void ResourceCache::incrementRefcountLocked(void* resource, ResourceType resourceType) { 83 ssize_t index = mCache->indexOfKey(resource); 84 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL; 85 if (ref == NULL || mCache->size() == 0) { 86 ref = new ResourceReference(resourceType); 87 mCache->add(resource, ref); 88 } 89 ref->refCount++; 90} 91 92void ResourceCache::incrementRefcountLocked(SkBitmap* bitmapResource) { 93 SkSafeRef(bitmapResource->pixelRef()); 94 SkSafeRef(bitmapResource->getColorTable()); 95 incrementRefcountLocked((void*) bitmapResource, kBitmap); 96} 97 98void ResourceCache::incrementRefcountLocked(SkPath* pathResource) { 99 incrementRefcountLocked((void*) pathResource, kPath); 100} 101 102void ResourceCache::incrementRefcountLocked(SkiaShader* shaderResource) { 103 SkSafeRef(shaderResource->getSkShader()); 104 incrementRefcountLocked((void*) shaderResource, kShader); 105} 106 107void ResourceCache::incrementRefcountLocked(SkiaColorFilter* filterResource) { 108 SkSafeRef(filterResource->getSkColorFilter()); 109 incrementRefcountLocked((void*) filterResource, kColorFilter); 110} 111 112void ResourceCache::decrementRefcount(void* resource) { 113 Mutex::Autolock _l(mLock); 114 decrementRefcountLocked(resource); 115} 116 117void ResourceCache::decrementRefcount(SkBitmap* bitmapResource) { 118 SkSafeUnref(bitmapResource->pixelRef()); 119 SkSafeUnref(bitmapResource->getColorTable()); 120 decrementRefcount((void*) bitmapResource); 121} 122 123void ResourceCache::decrementRefcount(SkPath* pathResource) { 124 decrementRefcount((void*) pathResource); 125} 126 127void ResourceCache::decrementRefcount(SkiaShader* shaderResource) { 128 SkSafeUnref(shaderResource->getSkShader()); 129 decrementRefcount((void*) shaderResource); 130} 131 132void ResourceCache::decrementRefcount(SkiaColorFilter* filterResource) { 133 SkSafeUnref(filterResource->getSkColorFilter()); 134 decrementRefcount((void*) filterResource); 135} 136 137void ResourceCache::decrementRefcountLocked(void* resource) { 138 ssize_t index = mCache->indexOfKey(resource); 139 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL; 140 if (ref == NULL) { 141 // Should not get here - shouldn't get a call to decrement if we're not yet tracking it 142 return; 143 } 144 ref->refCount--; 145 if (ref->refCount == 0) { 146 deleteResourceReference(resource, ref); 147 } 148} 149 150void ResourceCache::decrementRefcountLocked(SkBitmap* bitmapResource) { 151 SkSafeUnref(bitmapResource->pixelRef()); 152 SkSafeUnref(bitmapResource->getColorTable()); 153 decrementRefcountLocked((void*) bitmapResource); 154} 155 156void ResourceCache::decrementRefcountLocked(SkPath* pathResource) { 157 decrementRefcountLocked((void*) pathResource); 158} 159 160void ResourceCache::decrementRefcountLocked(SkiaShader* shaderResource) { 161 SkSafeUnref(shaderResource->getSkShader()); 162 decrementRefcountLocked((void*) shaderResource); 163} 164 165void ResourceCache::decrementRefcountLocked(SkiaColorFilter* filterResource) { 166 SkSafeUnref(filterResource->getSkColorFilter()); 167 decrementRefcountLocked((void*) filterResource); 168} 169 170void ResourceCache::destructor(SkPath* resource) { 171 Mutex::Autolock _l(mLock); 172 destructorLocked(resource); 173} 174 175void ResourceCache::destructorLocked(SkPath* resource) { 176 ssize_t index = mCache->indexOfKey(resource); 177 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL; 178 if (ref == NULL) { 179 // If we're not tracking this resource, just delete it 180 if (Caches::hasInstance()) { 181 Caches::getInstance().pathCache.removeDeferred(resource); 182 } 183 delete resource; 184 return; 185 } 186 ref->destroyed = true; 187 if (ref->refCount == 0) { 188 deleteResourceReference(resource, ref); 189 } 190} 191 192void ResourceCache::destructor(SkBitmap* resource) { 193 Mutex::Autolock _l(mLock); 194 destructorLocked(resource); 195} 196 197void ResourceCache::destructorLocked(SkBitmap* resource) { 198 ssize_t index = mCache->indexOfKey(resource); 199 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL; 200 if (ref == NULL) { 201 // If we're not tracking this resource, just delete it 202 if (Caches::hasInstance()) { 203 Caches::getInstance().textureCache.removeDeferred(resource); 204 } 205 delete resource; 206 return; 207 } 208 ref->destroyed = true; 209 if (ref->refCount == 0) { 210 deleteResourceReference(resource, ref); 211 } 212} 213 214void ResourceCache::destructor(SkiaShader* resource) { 215 Mutex::Autolock _l(mLock); 216 destructorLocked(resource); 217} 218 219void ResourceCache::destructorLocked(SkiaShader* resource) { 220 ssize_t index = mCache->indexOfKey(resource); 221 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL; 222 if (ref == NULL) { 223 // If we're not tracking this resource, just delete it 224 delete resource; 225 return; 226 } 227 ref->destroyed = true; 228 if (ref->refCount == 0) { 229 deleteResourceReference(resource, ref); 230 } 231} 232 233void ResourceCache::destructor(SkiaColorFilter* resource) { 234 Mutex::Autolock _l(mLock); 235 destructorLocked(resource); 236} 237 238void ResourceCache::destructorLocked(SkiaColorFilter* resource) { 239 ssize_t index = mCache->indexOfKey(resource); 240 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL; 241 if (ref == NULL) { 242 // If we're not tracking this resource, just delete it 243 delete resource; 244 return; 245 } 246 ref->destroyed = true; 247 if (ref->refCount == 0) { 248 deleteResourceReference(resource, ref); 249 } 250} 251 252void ResourceCache::recycle(SkBitmap* resource) { 253 Mutex::Autolock _l(mLock); 254 recycleLocked(resource); 255} 256 257void ResourceCache::recycleLocked(SkBitmap* resource) { 258 ssize_t index = mCache->indexOfKey(resource); 259 if (index < 0) { 260 // not tracking this resource; just recycle the pixel data 261 resource->setPixels(NULL, NULL); 262 return; 263 } 264 ResourceReference* ref = mCache->valueAt(index); 265 if (ref == NULL) { 266 // Should not get here - shouldn't get a call to recycle if we're not yet tracking it 267 return; 268 } 269 ref->recycled = true; 270 if (ref->refCount == 0) { 271 deleteResourceReference(resource, ref); 272 } 273} 274 275/** 276 * This method should only be called while the mLock mutex is held (that mutex is grabbed 277 * by the various destructor() and recycle() methods which call this method). 278 */ 279void ResourceCache::deleteResourceReference(void* resource, ResourceReference* ref) { 280 if (ref->recycled && ref->resourceType == kBitmap) { 281 ((SkBitmap*) resource)->setPixels(NULL, NULL); 282 } 283 if (ref->destroyed) { 284 switch (ref->resourceType) { 285 case kBitmap: { 286 SkBitmap* bitmap = (SkBitmap*) resource; 287 if (Caches::hasInstance()) { 288 Caches::getInstance().textureCache.removeDeferred(bitmap); 289 } 290 delete bitmap; 291 } 292 break; 293 case kPath: { 294 SkPath* path = (SkPath*) resource; 295 if (Caches::hasInstance()) { 296 Caches::getInstance().pathCache.removeDeferred(path); 297 } 298 delete path; 299 } 300 break; 301 case kShader: { 302 SkiaShader* shader = (SkiaShader*) resource; 303 delete shader; 304 } 305 break; 306 case kColorFilter: { 307 SkiaColorFilter* filter = (SkiaColorFilter*) resource; 308 delete filter; 309 } 310 break; 311 } 312 } 313 mCache->removeItem(resource); 314 delete ref; 315} 316 317}; // namespace uirenderer 318}; // namespace android 319