blob: b26e433cfa4c4e1c29a9dbe1fc1844d1996bcecb [file] [log] [blame]
Chet Haase5c13d892010-10-08 08:37:55 -07001/*
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
Chet Haase5c13d892010-10-08 08:37:55 -070017#include "ResourceCache.h"
18#include "Caches.h"
19
20namespace android {
John Recka35778c72014-11-06 09:45:10 -080021
John Recka35778c72014-11-06 09:45:10 -080022using namespace uirenderer;
23ANDROID_SINGLETON_STATIC_INSTANCE(ResourceCache);
John Recka35778c72014-11-06 09:45:10 -080024
Chet Haase5c13d892010-10-08 08:37:55 -070025namespace uirenderer {
26
27///////////////////////////////////////////////////////////////////////////////
28// Resource cache
29///////////////////////////////////////////////////////////////////////////////
30
31void ResourceCache::logCache() {
Steve Block5baa3a62011-12-20 16:23:08 +000032 ALOGD("ResourceCache: cacheReport:");
Chet Haase5c13d892010-10-08 08:37:55 -070033 for (size_t i = 0; i < mCache->size(); ++i) {
34 ResourceReference* ref = mCache->valueAt(i);
Ashok Bhatf5df7002014-03-25 20:51:35 +000035 ALOGD(" ResourceCache: mCache(%zu): resource, ref = 0x%p, 0x%p",
Chet Haase5c13d892010-10-08 08:37:55 -070036 i, mCache->keyAt(i), mCache->valueAt(i));
Derek Sollenberger3d4eed72014-12-04 15:20:29 -050037 ALOGD(" ResourceCache: mCache(%zu): refCount, destroyed, type = %d, %d, %d",
38 i, ref->refCount, ref->destroyed, ref->resourceType);
Chet Haase5c13d892010-10-08 08:37:55 -070039 }
40}
41
42ResourceCache::ResourceCache() {
Chet Haasee7d22952010-11-11 16:30:16 -080043 Mutex::Autolock _l(mLock);
Chris Craikd218a922014-01-02 17:13:34 -080044 mCache = new KeyedVector<const void*, ResourceReference*>();
Chet Haase5c13d892010-10-08 08:37:55 -070045}
46
47ResourceCache::~ResourceCache() {
Chet Haasee7d22952010-11-11 16:30:16 -080048 Mutex::Autolock _l(mLock);
Chet Haase5c13d892010-10-08 08:37:55 -070049 delete mCache;
50}
51
Romain Guy58ecc202012-09-07 11:58:36 -070052void ResourceCache::lock() {
53 mLock.lock();
54}
55
56void ResourceCache::unlock() {
57 mLock.unlock();
58}
59
Chet Haase5c13d892010-10-08 08:37:55 -070060void ResourceCache::incrementRefcount(void* resource, ResourceType resourceType) {
Chet Haasee7d22952010-11-11 16:30:16 -080061 Mutex::Autolock _l(mLock);
Romain Guy58ecc202012-09-07 11:58:36 -070062 incrementRefcountLocked(resource, resourceType);
Chet Haase5c13d892010-10-08 08:37:55 -070063}
64
Chris Craikd218a922014-01-02 17:13:34 -080065void ResourceCache::incrementRefcount(const Res_png_9patch* patchResource) {
Romain Guye3b0a012013-06-26 15:45:41 -070066 incrementRefcount((void*) patchResource, kNinePatch);
67}
68
Romain Guy58ecc202012-09-07 11:58:36 -070069void ResourceCache::incrementRefcountLocked(void* resource, ResourceType resourceType) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -070070 ssize_t index = mCache->indexOfKey(resource);
Chris Craikd41c4d82015-01-05 15:51:13 -080071 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : nullptr;
72 if (ref == nullptr || mCache->size() == 0) {
Romain Guy58ecc202012-09-07 11:58:36 -070073 ref = new ResourceReference(resourceType);
74 mCache->add(resource, ref);
Chet Haase5c13d892010-10-08 08:37:55 -070075 }
Romain Guy58ecc202012-09-07 11:58:36 -070076 ref->refCount++;
77}
78
Romain Guy58ecc202012-09-07 11:58:36 -070079void ResourceCache::decrementRefcount(void* resource) {
80 Mutex::Autolock _l(mLock);
81 decrementRefcountLocked(resource);
Chet Haase5c13d892010-10-08 08:37:55 -070082}
83
Chris Craikd218a922014-01-02 17:13:34 -080084void ResourceCache::decrementRefcount(const Res_png_9patch* patchResource) {
Romain Guye3b0a012013-06-26 15:45:41 -070085 decrementRefcount((void*) patchResource);
86}
87
Romain Guy58ecc202012-09-07 11:58:36 -070088void ResourceCache::decrementRefcountLocked(void* resource) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -070089 ssize_t index = mCache->indexOfKey(resource);
Chris Craikd41c4d82015-01-05 15:51:13 -080090 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : nullptr;
91 if (ref == nullptr) {
Romain Guy58ecc202012-09-07 11:58:36 -070092 // Should not get here - shouldn't get a call to decrement if we're not yet tracking it
Chet Haase5c13d892010-10-08 08:37:55 -070093 return;
94 }
Romain Guy58ecc202012-09-07 11:58:36 -070095 ref->refCount--;
Chet Haase5c13d892010-10-08 08:37:55 -070096 if (ref->refCount == 0) {
Romain Guy97dc9172012-09-23 17:46:45 -070097 deleteResourceReferenceLocked(resource, ref);
Chet Haase5c13d892010-10-08 08:37:55 -070098 }
99}
100
Chris Craikd218a922014-01-02 17:13:34 -0800101void ResourceCache::decrementRefcountLocked(const Res_png_9patch* patchResource) {
Romain Guye3b0a012013-06-26 15:45:41 -0700102 decrementRefcountLocked((void*) patchResource);
103}
104
Romain Guye3b0a012013-06-26 15:45:41 -0700105void ResourceCache::destructor(Res_png_9patch* resource) {
106 Mutex::Autolock _l(mLock);
107 destructorLocked(resource);
108}
109
110void ResourceCache::destructorLocked(Res_png_9patch* resource) {
111 ssize_t index = mCache->indexOfKey(resource);
Chris Craikd41c4d82015-01-05 15:51:13 -0800112 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : nullptr;
113 if (ref == nullptr) {
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900114 // If we're not tracking this resource, just delete it
Romain Guye3b0a012013-06-26 15:45:41 -0700115 if (Caches::hasInstance()) {
116 Caches::getInstance().patchCache.removeDeferred(resource);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900117 } else {
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;
Romain Guye3b0a012013-06-26 15:45:41 -0700121 }
Romain Guye3b0a012013-06-26 15:45:41 -0700122 return;
123 }
124 ref->destroyed = true;
125 if (ref->refCount == 0) {
126 deleteResourceReferenceLocked(resource, ref);
127 }
128}
129
Chet Haase547e6652012-10-22 15:07:26 -0700130/**
Chet Haasee7d22952010-11-11 16:30:16 -0800131 * 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).
133 */
Chris Craikd218a922014-01-02 17:13:34 -0800134void ResourceCache::deleteResourceReferenceLocked(const void* resource, ResourceReference* ref) {
John Reck0e89e2b2014-10-31 14:49:06 -0700135 if (ref->destroyed) {
Chet Haase5c13d892010-10-08 08:37:55 -0700136 switch (ref->resourceType) {
Romain Guye3b0a012013-06-26 15:45:41 -0700137 case kNinePatch: {
138 if (Caches::hasInstance()) {
139 Caches::getInstance().patchCache.removeDeferred((Res_png_9patch*) resource);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900140 } else {
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;
144 delete[] patch;
Romain Guye3b0a012013-06-26 15:45:41 -0700145 }
Romain Guye3b0a012013-06-26 15:45:41 -0700146 }
147 break;
Chet Haase5c13d892010-10-08 08:37:55 -0700148 }
149 }
150 mCache->removeItem(resource);
151 delete ref;
152}
153
Chet Haase5c13d892010-10-08 08:37:55 -0700154}; // namespace uirenderer
155}; // namespace android