blob: 75d81346d62aef6ef103b4321ca8a6a0a5830579 [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
Romain Guye3b0a012013-06-26 15:45:41 -070017#define LOG_TAG "OpenGLRenderer"
18
Chet Haase5c13d892010-10-08 08:37:55 -070019#include "ResourceCache.h"
20#include "Caches.h"
21
22namespace android {
John Recka35778c72014-11-06 09:45:10 -080023
John Recka35778c72014-11-06 09:45:10 -080024using namespace uirenderer;
25ANDROID_SINGLETON_STATIC_INSTANCE(ResourceCache);
John Recka35778c72014-11-06 09:45:10 -080026
Chet Haase5c13d892010-10-08 08:37:55 -070027namespace uirenderer {
28
29///////////////////////////////////////////////////////////////////////////////
30// Resource cache
31///////////////////////////////////////////////////////////////////////////////
32
33void ResourceCache::logCache() {
Steve Block5baa3a62011-12-20 16:23:08 +000034 ALOGD("ResourceCache: cacheReport:");
Chet Haase5c13d892010-10-08 08:37:55 -070035 for (size_t i = 0; i < mCache->size(); ++i) {
36 ResourceReference* ref = mCache->valueAt(i);
Ashok Bhatf5df7002014-03-25 20:51:35 +000037 ALOGD(" ResourceCache: mCache(%zu): resource, ref = 0x%p, 0x%p",
Chet Haase5c13d892010-10-08 08:37:55 -070038 i, mCache->keyAt(i), mCache->valueAt(i));
Derek Sollenberger3d4eed72014-12-04 15:20:29 -050039 ALOGD(" ResourceCache: mCache(%zu): refCount, destroyed, type = %d, %d, %d",
40 i, ref->refCount, ref->destroyed, ref->resourceType);
Chet Haase5c13d892010-10-08 08:37:55 -070041 }
42}
43
44ResourceCache::ResourceCache() {
Chet Haasee7d22952010-11-11 16:30:16 -080045 Mutex::Autolock _l(mLock);
Chris Craikd218a922014-01-02 17:13:34 -080046 mCache = new KeyedVector<const void*, ResourceReference*>();
Chet Haase5c13d892010-10-08 08:37:55 -070047}
48
49ResourceCache::~ResourceCache() {
Chet Haasee7d22952010-11-11 16:30:16 -080050 Mutex::Autolock _l(mLock);
Chet Haase5c13d892010-10-08 08:37:55 -070051 delete mCache;
52}
53
Romain Guy58ecc202012-09-07 11:58:36 -070054void ResourceCache::lock() {
55 mLock.lock();
56}
57
58void ResourceCache::unlock() {
59 mLock.unlock();
60}
61
Chet Haase5c13d892010-10-08 08:37:55 -070062void ResourceCache::incrementRefcount(void* resource, ResourceType resourceType) {
Chet Haasee7d22952010-11-11 16:30:16 -080063 Mutex::Autolock _l(mLock);
Romain Guy58ecc202012-09-07 11:58:36 -070064 incrementRefcountLocked(resource, resourceType);
Chet Haase5c13d892010-10-08 08:37:55 -070065}
66
Chris Craikd218a922014-01-02 17:13:34 -080067void ResourceCache::incrementRefcount(const Res_png_9patch* patchResource) {
Romain Guye3b0a012013-06-26 15:45:41 -070068 incrementRefcount((void*) patchResource, kNinePatch);
69}
70
Romain Guy58ecc202012-09-07 11:58:36 -070071void ResourceCache::incrementRefcountLocked(void* resource, ResourceType resourceType) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -070072 ssize_t index = mCache->indexOfKey(resource);
Chris Craikd41c4d82015-01-05 15:51:13 -080073 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : nullptr;
74 if (ref == nullptr || mCache->size() == 0) {
Romain Guy58ecc202012-09-07 11:58:36 -070075 ref = new ResourceReference(resourceType);
76 mCache->add(resource, ref);
Chet Haase5c13d892010-10-08 08:37:55 -070077 }
Romain Guy58ecc202012-09-07 11:58:36 -070078 ref->refCount++;
79}
80
Romain Guy58ecc202012-09-07 11:58:36 -070081void ResourceCache::decrementRefcount(void* resource) {
82 Mutex::Autolock _l(mLock);
83 decrementRefcountLocked(resource);
Chet Haase5c13d892010-10-08 08:37:55 -070084}
85
Chris Craikd218a922014-01-02 17:13:34 -080086void ResourceCache::decrementRefcount(const Res_png_9patch* patchResource) {
Romain Guye3b0a012013-06-26 15:45:41 -070087 decrementRefcount((void*) patchResource);
88}
89
Romain Guy58ecc202012-09-07 11:58:36 -070090void ResourceCache::decrementRefcountLocked(void* resource) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -070091 ssize_t index = mCache->indexOfKey(resource);
Chris Craikd41c4d82015-01-05 15:51:13 -080092 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : nullptr;
93 if (ref == nullptr) {
Romain Guy58ecc202012-09-07 11:58:36 -070094 // 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 -070095 return;
96 }
Romain Guy58ecc202012-09-07 11:58:36 -070097 ref->refCount--;
Chet Haase5c13d892010-10-08 08:37:55 -070098 if (ref->refCount == 0) {
Romain Guy97dc9172012-09-23 17:46:45 -070099 deleteResourceReferenceLocked(resource, ref);
Chet Haase5c13d892010-10-08 08:37:55 -0700100 }
101}
102
Chris Craikd218a922014-01-02 17:13:34 -0800103void ResourceCache::decrementRefcountLocked(const Res_png_9patch* patchResource) {
Romain Guye3b0a012013-06-26 15:45:41 -0700104 decrementRefcountLocked((void*) patchResource);
105}
106
Romain Guye3b0a012013-06-26 15:45:41 -0700107void 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);
Chris Craikd41c4d82015-01-05 15:51:13 -0800114 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : nullptr;
115 if (ref == nullptr) {
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900116 // If we're not tracking this resource, just delete it
Romain Guye3b0a012013-06-26 15:45:41 -0700117 if (Caches::hasInstance()) {
118 Caches::getInstance().patchCache.removeDeferred(resource);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900119 } 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;
Romain Guye3b0a012013-06-26 15:45:41 -0700123 }
Romain Guye3b0a012013-06-26 15:45:41 -0700124 return;
125 }
126 ref->destroyed = true;
127 if (ref->refCount == 0) {
128 deleteResourceReferenceLocked(resource, ref);
129 }
130}
131
Chet Haase547e6652012-10-22 15:07:26 -0700132/**
Chet Haasee7d22952010-11-11 16:30:16 -0800133 * 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 */
Chris Craikd218a922014-01-02 17:13:34 -0800136void ResourceCache::deleteResourceReferenceLocked(const void* resource, ResourceReference* ref) {
John Reck0e89e2b2014-10-31 14:49:06 -0700137 if (ref->destroyed) {
Chet Haase5c13d892010-10-08 08:37:55 -0700138 switch (ref->resourceType) {
Romain Guye3b0a012013-06-26 15:45:41 -0700139 case kNinePatch: {
140 if (Caches::hasInstance()) {
141 Caches::getInstance().patchCache.removeDeferred((Res_png_9patch*) resource);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900142 } 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;
Romain Guye3b0a012013-06-26 15:45:41 -0700147 }
Romain Guye3b0a012013-06-26 15:45:41 -0700148 }
149 break;
Chet Haase5c13d892010-10-08 08:37:55 -0700150 }
151 }
152 mCache->removeItem(resource);
153 delete ref;
154}
155
Chet Haase5c13d892010-10-08 08:37:55 -0700156}; // namespace uirenderer
157}; // namespace android