2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "ResourceCache.h"
22 using namespace uirenderer;
23 ANDROID_SINGLETON_STATIC_INSTANCE(ResourceCache);
25 namespace uirenderer {
27 ///////////////////////////////////////////////////////////////////////////////
29 ///////////////////////////////////////////////////////////////////////////////
31 void ResourceCache::logCache() {
32 ALOGD("ResourceCache: cacheReport:");
33 for (size_t i = 0; i < mCache->size(); ++i) {
34 ResourceReference* ref = mCache->valueAt(i);
35 ALOGD(" ResourceCache: mCache(%zu): resource, ref = 0x%p, 0x%p",
36 i, mCache->keyAt(i), mCache->valueAt(i));
37 ALOGD(" ResourceCache: mCache(%zu): refCount, destroyed, type = %d, %d, %d",
38 i, ref->refCount, ref->destroyed, ref->resourceType);
42 ResourceCache::ResourceCache() {
43 Mutex::Autolock _l(mLock);
44 mCache = new KeyedVector<const void*, ResourceReference*>();
47 ResourceCache::~ResourceCache() {
48 Mutex::Autolock _l(mLock);
52 void ResourceCache::lock() {
56 void ResourceCache::unlock() {
60 void ResourceCache::incrementRefcount(void* resource, ResourceType resourceType) {
61 Mutex::Autolock _l(mLock);
62 incrementRefcountLocked(resource, resourceType);
65 void ResourceCache::incrementRefcount(const Res_png_9patch* patchResource) {
66 incrementRefcount((void*) patchResource, kNinePatch);
69 void ResourceCache::incrementRefcountLocked(void* resource, ResourceType resourceType) {
70 ssize_t index = mCache->indexOfKey(resource);
71 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : nullptr;
72 if (ref == nullptr || mCache->size() == 0) {
73 ref = new ResourceReference(resourceType);
74 mCache->add(resource, ref);
79 void ResourceCache::decrementRefcount(void* resource) {
80 Mutex::Autolock _l(mLock);
81 decrementRefcountLocked(resource);
84 void ResourceCache::decrementRefcount(const Res_png_9patch* patchResource) {
85 decrementRefcount((void*) patchResource);
88 void ResourceCache::decrementRefcountLocked(void* resource) {
89 ssize_t index = mCache->indexOfKey(resource);
90 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : nullptr;
92 // Should not get here - shouldn't get a call to decrement if we're not yet tracking it
96 if (ref->refCount == 0) {
97 deleteResourceReferenceLocked(resource, ref);
101 void ResourceCache::decrementRefcountLocked(const Res_png_9patch* patchResource) {
102 decrementRefcountLocked((void*) patchResource);
105 void ResourceCache::destructor(Res_png_9patch* resource) {
106 Mutex::Autolock _l(mLock);
107 destructorLocked(resource);
110 void ResourceCache::destructorLocked(Res_png_9patch* resource) {
111 ssize_t index = mCache->indexOfKey(resource);
112 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : nullptr;
113 if (ref == nullptr) {
114 // If we're not tracking this resource, just delete it
115 if (Caches::hasInstance()) {
116 Caches::getInstance().patchCache.removeDeferred(resource);
118 // A Res_png_9patch is actually an array of byte that's larger
119 // than sizeof(Res_png_9patch). It must be freed as an array.
120 delete[] (int8_t*) resource;
124 ref->destroyed = true;
125 if (ref->refCount == 0) {
126 deleteResourceReferenceLocked(resource, ref);
131 * This method should only be called while the mLock mutex is held (that mutex is grabbed
132 * by the various destructor() and recycle() methods which call this method).
134 void ResourceCache::deleteResourceReferenceLocked(const void* resource, ResourceReference* ref) {
135 if (ref->destroyed) {
136 switch (ref->resourceType) {
138 if (Caches::hasInstance()) {
139 Caches::getInstance().patchCache.removeDeferred((Res_png_9patch*) resource);
141 // A Res_png_9patch is actually an array of byte that's larger
142 // than sizeof(Res_png_9patch). It must be freed as an array.
143 int8_t* patch = (int8_t*) resource;
150 mCache->removeItem(resource);
154 }; // namespace uirenderer
155 }; // namespace android