ResourceCache.cpp revision 43ccf4663c822ddd435b7683cc05221f6169c6c3
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 LOGD("ResourceCache: cacheReport:"); 30 for (size_t i = 0; i < mCache->size(); ++i) { 31 ResourceReference* ref = mCache->valueAt(i); 32 LOGD(" ResourceCache: mCache(%d): resource, ref = 0x%p, 0x%p", 33 i, mCache->keyAt(i), mCache->valueAt(i)); 34 LOGD(" 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::incrementRefcount(void* resource, ResourceType resourceType) { 50 Mutex::Autolock _l(mLock); 51 for (size_t i = 0; i < mCache->size(); ++i) { 52 void* ref = mCache->valueAt(i); 53 } 54 ResourceReference* ref = mCache->indexOfKey(resource) >= 0 ? mCache->valueFor(resource) : NULL; 55 if (ref == NULL || mCache->size() == 0) { 56 ref = new ResourceReference(resourceType); 57 mCache->add(resource, ref); 58 } 59 ref->refCount++; 60} 61 62void ResourceCache::incrementRefcount(SkBitmap* bitmapResource) { 63 bitmapResource->pixelRef()->safeRef(); 64 bitmapResource->getColorTable()->safeRef(); 65 incrementRefcount((void*)bitmapResource, kBitmap); 66} 67 68void ResourceCache::incrementRefcount(SkiaShader* shaderResource) { 69 shaderResource->getSkShader()->safeRef(); 70 incrementRefcount((void*) shaderResource, kShader); 71} 72 73void ResourceCache::incrementRefcount(SkiaColorFilter* filterResource) { 74 filterResource->getSkColorFilter()->safeRef(); 75 incrementRefcount((void*) filterResource, kColorFilter); 76} 77 78void ResourceCache::decrementRefcount(void* resource) { 79 Mutex::Autolock _l(mLock); 80 ResourceReference* ref = mCache->indexOfKey(resource) >= 0 ? mCache->valueFor(resource) : NULL; 81 if (ref == NULL) { 82 // Should not get here - shouldn't get a call to decrement if we're not yet tracking it 83 return; 84 } 85 ref->refCount--; 86 if (ref->refCount == 0) { 87 deleteResourceReference(resource, ref); 88 } 89} 90 91void ResourceCache::decrementRefcount(SkBitmap* bitmapResource) { 92 bitmapResource->pixelRef()->safeUnref(); 93 bitmapResource->getColorTable()->safeUnref(); 94 decrementRefcount((void*) bitmapResource); 95} 96 97void ResourceCache::decrementRefcount(SkiaShader* shaderResource) { 98 shaderResource->getSkShader()->safeUnref(); 99 decrementRefcount((void*) shaderResource); 100} 101 102void ResourceCache::decrementRefcount(SkiaColorFilter* filterResource) { 103 filterResource->getSkColorFilter()->safeUnref(); 104 decrementRefcount((void*) filterResource); 105} 106 107void ResourceCache::recycle(SkBitmap* resource) { 108 Mutex::Autolock _l(mLock); 109 if (mCache->indexOfKey(resource) < 0) { 110 // not tracking this resource; just recycle the pixel data 111 resource->setPixels(NULL, NULL); 112 return; 113 } 114 ResourceReference* ref = mCache->indexOfKey(resource) >= 0 ? mCache->valueFor(resource) : NULL; 115 if (ref == NULL) { 116 // Should not get here - shouldn't get a call to recycle if we're not yet tracking it 117 return; 118 } 119 ref->recycled = true; 120 if (ref->refCount == 0) { 121 deleteResourceReference(resource, ref); 122 } 123} 124 125void ResourceCache::destructor(SkBitmap* resource) { 126 Mutex::Autolock _l(mLock); 127 ResourceReference* ref = mCache->indexOfKey(resource) >= 0 ? mCache->valueFor(resource) : NULL; 128 if (ref == NULL) { 129 // If we're not tracking this resource, just delete it 130 if (Caches::hasInstance()) { 131 Caches::getInstance().textureCache.removeDeferred(resource); 132 } 133 delete resource; 134 return; 135 } 136 ref->destroyed = true; 137 if (ref->refCount == 0) { 138 deleteResourceReference(resource, ref); 139 return; 140 } 141} 142 143void ResourceCache::destructor(SkiaShader* resource) { 144 Mutex::Autolock _l(mLock); 145 ResourceReference* ref = mCache->indexOfKey(resource) >= 0 ? mCache->valueFor(resource) : NULL; 146 if (ref == NULL) { 147 // If we're not tracking this resource, just delete it 148 if (Caches::hasInstance()) { 149 Caches::getInstance().gradientCache.removeDeferred(resource->getSkShader()); 150 } 151 delete resource; 152 return; 153 } 154 ref->destroyed = true; 155 if (ref->refCount == 0) { 156 deleteResourceReference(resource, ref); 157 return; 158 } 159} 160 161void ResourceCache::destructor(SkiaColorFilter* resource) { 162 Mutex::Autolock _l(mLock); 163 ResourceReference* ref = mCache->indexOfKey(resource) >= 0 ? mCache->valueFor(resource) : NULL; 164 if (ref == NULL) { 165 // If we're not tracking this resource, just delete it 166 delete resource; 167 return; 168 } 169 ref->destroyed = true; 170 if (ref->refCount == 0) { 171 deleteResourceReference(resource, ref); 172 return; 173 } 174} 175 176/** 177 * This method should only be called while the mLock mutex is held (that mutex is grabbed 178 * by the various destructor() and recycle() methods which call this method). 179 */ 180void ResourceCache::deleteResourceReference(void* resource, ResourceReference* ref) { 181 if (ref->recycled && ref->resourceType == kBitmap) { 182 ((SkBitmap*) resource)->setPixels(NULL, NULL); 183 } 184 if (ref->destroyed) { 185 switch (ref->resourceType) { 186 case kBitmap: 187 { 188 SkBitmap* bitmap = (SkBitmap*)resource; 189 if (Caches::hasInstance()) { 190 Caches::getInstance().textureCache.removeDeferred(bitmap); 191 } 192 delete bitmap; 193 } 194 break; 195 case kShader: 196 { 197 SkiaShader* shader = (SkiaShader*)resource; 198 if (Caches::hasInstance()) { 199 Caches::getInstance().gradientCache.removeDeferred(shader->getSkShader()); 200 } 201 delete shader; 202 } 203 break; 204 case kColorFilter: 205 { 206 SkiaColorFilter* filter = (SkiaColorFilter*)resource; 207 delete filter; 208 } 209 break; 210 } 211 } 212 mCache->removeItem(resource); 213 delete ref; 214} 215 216}; // namespace uirenderer 217}; // namespace android 218