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 62void ResourceCache::incrementRefcount(void* resource, ResourceType resourceType) { 63 Mutex::Autolock _l(mLock); 64 incrementRefcountLocked(resource, resourceType); 65} 66 67void ResourceCache::incrementRefcount(const Res_png_9patch* patchResource) { 68 incrementRefcount((void*) patchResource, kNinePatch); 69} 70 71void ResourceCache::incrementRefcountLocked(void* resource, ResourceType resourceType) { 72 ssize_t index = mCache->indexOfKey(resource); 73 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : nullptr; 74 if (ref == nullptr || mCache->size() == 0) { 75 ref = new ResourceReference(resourceType); 76 mCache->add(resource, ref); 77 } 78 ref->refCount++; 79} 80 81void ResourceCache::decrementRefcount(void* resource) { 82 Mutex::Autolock _l(mLock); 83 decrementRefcountLocked(resource); 84} 85 86void ResourceCache::decrementRefcount(const Res_png_9patch* patchResource) { 87 decrementRefcount((void*) patchResource); 88} 89 90void ResourceCache::decrementRefcountLocked(void* resource) { 91 ssize_t index = mCache->indexOfKey(resource); 92 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : nullptr; 93 if (ref == nullptr) { 94 // Should not get here - shouldn't get a call to decrement if we're not yet tracking it 95 return; 96 } 97 ref->refCount--; 98 if (ref->refCount == 0) { 99 deleteResourceReferenceLocked(resource, ref); 100 } 101} 102 103void ResourceCache::decrementRefcountLocked(const Res_png_9patch* patchResource) { 104 decrementRefcountLocked((void*) patchResource); 105} 106 107void ResourceCache::destructor(Res_png_9patch* resource) { 108 Mutex::Autolock _l(mLock); 109 destructorLocked(resource); 110} 111 112void ResourceCache::destructorLocked(Res_png_9patch* resource) { 113 ssize_t index = mCache->indexOfKey(resource); 114 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : nullptr; 115 if (ref == nullptr) { 116 // If we're not tracking this resource, just delete it 117 if (Caches::hasInstance()) { 118 Caches::getInstance().patchCache.removeDeferred(resource); 119 } else { 120 // A Res_png_9patch is actually an array of byte that's larger 121 // than sizeof(Res_png_9patch). It must be freed as an array. 122 delete[] (int8_t*) resource; 123 } 124 return; 125 } 126 ref->destroyed = true; 127 if (ref->refCount == 0) { 128 deleteResourceReferenceLocked(resource, ref); 129 } 130} 131 132/** 133 * This method should only be called while the mLock mutex is held (that mutex is grabbed 134 * by the various destructor() and recycle() methods which call this method). 135 */ 136void ResourceCache::deleteResourceReferenceLocked(const void* resource, ResourceReference* ref) { 137 if (ref->destroyed) { 138 switch (ref->resourceType) { 139 case kNinePatch: { 140 if (Caches::hasInstance()) { 141 Caches::getInstance().patchCache.removeDeferred((Res_png_9patch*) resource); 142 } else { 143 // A Res_png_9patch is actually an array of byte that's larger 144 // than sizeof(Res_png_9patch). It must be freed as an array. 145 int8_t* patch = (int8_t*) resource; 146 delete[] patch; 147 } 148 } 149 break; 150 } 151 } 152 mCache->removeItem(resource); 153 delete ref; 154} 155 156}; // namespace uirenderer 157}; // namespace android 158