blob: 8a48a6e6c04c287c99133cc416681a1e369a64ca [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 <SkPixelRef.h>
20#include "ResourceCache.h"
21#include "Caches.h"
22
23namespace android {
John Recka35778c72014-11-06 09:45:10 -080024
John Recka35778c72014-11-06 09:45:10 -080025using namespace uirenderer;
26ANDROID_SINGLETON_STATIC_INSTANCE(ResourceCache);
John Recka35778c72014-11-06 09:45:10 -080027
Chet Haase5c13d892010-10-08 08:37:55 -070028namespace uirenderer {
29
30///////////////////////////////////////////////////////////////////////////////
31// Resource cache
32///////////////////////////////////////////////////////////////////////////////
33
34void ResourceCache::logCache() {
Steve Block5baa3a62011-12-20 16:23:08 +000035 ALOGD("ResourceCache: cacheReport:");
Chet Haase5c13d892010-10-08 08:37:55 -070036 for (size_t i = 0; i < mCache->size(); ++i) {
37 ResourceReference* ref = mCache->valueAt(i);
Ashok Bhatf5df7002014-03-25 20:51:35 +000038 ALOGD(" ResourceCache: mCache(%zu): resource, ref = 0x%p, 0x%p",
Chet Haase5c13d892010-10-08 08:37:55 -070039 i, mCache->keyAt(i), mCache->valueAt(i));
Ashok Bhatf5df7002014-03-25 20:51:35 +000040 ALOGD(" ResourceCache: mCache(%zu): refCount, recycled, destroyed, type = %d, %d, %d, %d",
Chet Haase5c13d892010-10-08 08:37:55 -070041 i, ref->refCount, ref->recycled, ref->destroyed, ref->resourceType);
42 }
43}
44
45ResourceCache::ResourceCache() {
Chet Haasee7d22952010-11-11 16:30:16 -080046 Mutex::Autolock _l(mLock);
Chris Craikd218a922014-01-02 17:13:34 -080047 mCache = new KeyedVector<const void*, ResourceReference*>();
Chet Haase5c13d892010-10-08 08:37:55 -070048}
49
50ResourceCache::~ResourceCache() {
Chet Haasee7d22952010-11-11 16:30:16 -080051 Mutex::Autolock _l(mLock);
Chet Haase5c13d892010-10-08 08:37:55 -070052 delete mCache;
53}
54
Romain Guy58ecc202012-09-07 11:58:36 -070055void ResourceCache::lock() {
56 mLock.lock();
57}
58
59void ResourceCache::unlock() {
60 mLock.unlock();
61}
62
Chet Haase5c13d892010-10-08 08:37:55 -070063void ResourceCache::incrementRefcount(void* resource, ResourceType resourceType) {
Chet Haasee7d22952010-11-11 16:30:16 -080064 Mutex::Autolock _l(mLock);
Romain Guy58ecc202012-09-07 11:58:36 -070065 incrementRefcountLocked(resource, resourceType);
Chet Haase5c13d892010-10-08 08:37:55 -070066}
67
Chris Craikd218a922014-01-02 17:13:34 -080068void ResourceCache::incrementRefcount(const SkBitmap* bitmapResource) {
Romain Guy49c5fc02012-05-15 11:10:01 -070069 incrementRefcount((void*) bitmapResource, kBitmap);
Chet Haase5c13d892010-10-08 08:37:55 -070070}
71
Chris Craikd218a922014-01-02 17:13:34 -080072void ResourceCache::incrementRefcount(const SkPath* pathResource) {
Romain Guy49c5fc02012-05-15 11:10:01 -070073 incrementRefcount((void*) pathResource, kPath);
Chet Haase5a7e8282011-02-04 12:50:55 -080074}
75
Chris Craikd218a922014-01-02 17:13:34 -080076void ResourceCache::incrementRefcount(const Res_png_9patch* patchResource) {
Romain Guye3b0a012013-06-26 15:45:41 -070077 incrementRefcount((void*) patchResource, kNinePatch);
78}
79
Romain Guy58ecc202012-09-07 11:58:36 -070080void ResourceCache::incrementRefcountLocked(void* resource, ResourceType resourceType) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -070081 ssize_t index = mCache->indexOfKey(resource);
82 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
Romain Guy58ecc202012-09-07 11:58:36 -070083 if (ref == NULL || mCache->size() == 0) {
84 ref = new ResourceReference(resourceType);
85 mCache->add(resource, ref);
Chet Haase5c13d892010-10-08 08:37:55 -070086 }
Romain Guy58ecc202012-09-07 11:58:36 -070087 ref->refCount++;
88}
89
Chris Craikd218a922014-01-02 17:13:34 -080090void ResourceCache::incrementRefcountLocked(const SkBitmap* bitmapResource) {
Romain Guy58ecc202012-09-07 11:58:36 -070091 incrementRefcountLocked((void*) bitmapResource, kBitmap);
92}
93
Chris Craikd218a922014-01-02 17:13:34 -080094void ResourceCache::incrementRefcountLocked(const SkPath* pathResource) {
Romain Guy58ecc202012-09-07 11:58:36 -070095 incrementRefcountLocked((void*) pathResource, kPath);
96}
97
Chris Craikd218a922014-01-02 17:13:34 -080098void ResourceCache::incrementRefcountLocked(const Res_png_9patch* patchResource) {
Romain Guye3b0a012013-06-26 15:45:41 -070099 incrementRefcountLocked((void*) patchResource, kNinePatch);
100}
101
Romain Guy58ecc202012-09-07 11:58:36 -0700102void ResourceCache::decrementRefcount(void* resource) {
103 Mutex::Autolock _l(mLock);
104 decrementRefcountLocked(resource);
Chet Haase5c13d892010-10-08 08:37:55 -0700105}
106
Chris Craikd218a922014-01-02 17:13:34 -0800107void ResourceCache::decrementRefcount(const SkBitmap* bitmapResource) {
Romain Guy43ccf462011-01-14 18:51:01 -0800108 decrementRefcount((void*) bitmapResource);
Chet Haase5c13d892010-10-08 08:37:55 -0700109}
110
Chris Craikd218a922014-01-02 17:13:34 -0800111void ResourceCache::decrementRefcount(const SkPath* pathResource) {
Chet Haase5a7e8282011-02-04 12:50:55 -0800112 decrementRefcount((void*) pathResource);
113}
114
Chris Craikd218a922014-01-02 17:13:34 -0800115void ResourceCache::decrementRefcount(const Res_png_9patch* patchResource) {
Romain Guye3b0a012013-06-26 15:45:41 -0700116 decrementRefcount((void*) patchResource);
117}
118
Romain Guy58ecc202012-09-07 11:58:36 -0700119void ResourceCache::decrementRefcountLocked(void* resource) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700120 ssize_t index = mCache->indexOfKey(resource);
Romain Guy58ecc202012-09-07 11:58:36 -0700121 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
Chet Haase5c13d892010-10-08 08:37:55 -0700122 if (ref == NULL) {
Romain Guy58ecc202012-09-07 11:58:36 -0700123 // 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 -0700124 return;
125 }
Romain Guy58ecc202012-09-07 11:58:36 -0700126 ref->refCount--;
Chet Haase5c13d892010-10-08 08:37:55 -0700127 if (ref->refCount == 0) {
Romain Guy97dc9172012-09-23 17:46:45 -0700128 deleteResourceReferenceLocked(resource, ref);
Chet Haase5c13d892010-10-08 08:37:55 -0700129 }
130}
131
Chris Craikd218a922014-01-02 17:13:34 -0800132void ResourceCache::decrementRefcountLocked(const SkBitmap* bitmapResource) {
Romain Guy58ecc202012-09-07 11:58:36 -0700133 decrementRefcountLocked((void*) bitmapResource);
134}
135
Chris Craikd218a922014-01-02 17:13:34 -0800136void ResourceCache::decrementRefcountLocked(const SkPath* pathResource) {
Romain Guy58ecc202012-09-07 11:58:36 -0700137 decrementRefcountLocked((void*) pathResource);
138}
139
Chris Craikd218a922014-01-02 17:13:34 -0800140void ResourceCache::decrementRefcountLocked(const Res_png_9patch* patchResource) {
Romain Guye3b0a012013-06-26 15:45:41 -0700141 decrementRefcountLocked((void*) patchResource);
142}
143
Chet Haase5a7e8282011-02-04 12:50:55 -0800144void ResourceCache::destructor(SkPath* resource) {
145 Mutex::Autolock _l(mLock);
Romain Guy58ecc202012-09-07 11:58:36 -0700146 destructorLocked(resource);
147}
148
149void ResourceCache::destructorLocked(SkPath* resource) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700150 ssize_t index = mCache->indexOfKey(resource);
151 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
Chet Haase5a7e8282011-02-04 12:50:55 -0800152 if (ref == NULL) {
153 // If we're not tracking this resource, just delete it
154 if (Caches::hasInstance()) {
155 Caches::getInstance().pathCache.removeDeferred(resource);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900156 } else {
157 delete resource;
Chet Haase5a7e8282011-02-04 12:50:55 -0800158 }
Chet Haase5a7e8282011-02-04 12:50:55 -0800159 return;
160 }
161 ref->destroyed = true;
162 if (ref->refCount == 0) {
Romain Guy97dc9172012-09-23 17:46:45 -0700163 deleteResourceReferenceLocked(resource, ref);
Chet Haase5a7e8282011-02-04 12:50:55 -0800164 }
165}
166
Chris Craikd218a922014-01-02 17:13:34 -0800167void ResourceCache::destructor(const SkBitmap* resource) {
Chet Haasee7d22952010-11-11 16:30:16 -0800168 Mutex::Autolock _l(mLock);
Romain Guy58ecc202012-09-07 11:58:36 -0700169 destructorLocked(resource);
170}
171
Chris Craikd218a922014-01-02 17:13:34 -0800172void ResourceCache::destructorLocked(const SkBitmap* resource) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700173 ssize_t index = mCache->indexOfKey(resource);
174 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
Chet Haase5c13d892010-10-08 08:37:55 -0700175 if (ref == NULL) {
176 // If we're not tracking this resource, just delete it
177 if (Caches::hasInstance()) {
John Reck71d08a02014-11-24 15:21:28 -0800178 Caches::getInstance().textureCache.releaseTexture(resource);
Chet Haase5c13d892010-10-08 08:37:55 -0700179 }
John Reck71d08a02014-11-24 15:21:28 -0800180 delete resource;
Chet Haase5c13d892010-10-08 08:37:55 -0700181 return;
182 }
183 ref->destroyed = true;
184 if (ref->refCount == 0) {
Romain Guy97dc9172012-09-23 17:46:45 -0700185 deleteResourceReferenceLocked(resource, ref);
Chet Haase5c13d892010-10-08 08:37:55 -0700186 }
187}
188
Romain Guye3b0a012013-06-26 15:45:41 -0700189void ResourceCache::destructor(Res_png_9patch* resource) {
190 Mutex::Autolock _l(mLock);
191 destructorLocked(resource);
192}
193
194void ResourceCache::destructorLocked(Res_png_9patch* resource) {
195 ssize_t index = mCache->indexOfKey(resource);
196 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
197 if (ref == NULL) {
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900198 // If we're not tracking this resource, just delete it
Romain Guye3b0a012013-06-26 15:45:41 -0700199 if (Caches::hasInstance()) {
200 Caches::getInstance().patchCache.removeDeferred(resource);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900201 } else {
202 // A Res_png_9patch is actually an array of byte that's larger
203 // than sizeof(Res_png_9patch). It must be freed as an array.
204 delete[] (int8_t*) resource;
Romain Guye3b0a012013-06-26 15:45:41 -0700205 }
Romain Guye3b0a012013-06-26 15:45:41 -0700206 return;
207 }
208 ref->destroyed = true;
209 if (ref->refCount == 0) {
210 deleteResourceReferenceLocked(resource, ref);
211 }
212}
213
Chet Haase547e6652012-10-22 15:07:26 -0700214/**
215 * Return value indicates whether resource was actually recycled, which happens when RefCnt
216 * reaches 0.
217 */
218bool ResourceCache::recycle(SkBitmap* resource) {
Romain Guy58ecc202012-09-07 11:58:36 -0700219 Mutex::Autolock _l(mLock);
Chet Haase547e6652012-10-22 15:07:26 -0700220 return recycleLocked(resource);
Romain Guy58ecc202012-09-07 11:58:36 -0700221}
222
Chet Haase547e6652012-10-22 15:07:26 -0700223/**
224 * Return value indicates whether resource was actually recycled, which happens when RefCnt
225 * reaches 0.
226 */
227bool ResourceCache::recycleLocked(SkBitmap* resource) {
Romain Guy58ecc202012-09-07 11:58:36 -0700228 ssize_t index = mCache->indexOfKey(resource);
229 if (index < 0) {
John Reck71d08a02014-11-24 15:21:28 -0800230 if (Caches::hasInstance()) {
231 Caches::getInstance().textureCache.releaseTexture(resource);
232 }
Romain Guy58ecc202012-09-07 11:58:36 -0700233 // not tracking this resource; just recycle the pixel data
234 resource->setPixels(NULL, NULL);
Chet Haase547e6652012-10-22 15:07:26 -0700235 return true;
Romain Guy58ecc202012-09-07 11:58:36 -0700236 }
237 ResourceReference* ref = mCache->valueAt(index);
238 if (ref == NULL) {
239 // Should not get here - shouldn't get a call to recycle if we're not yet tracking it
Chet Haase547e6652012-10-22 15:07:26 -0700240 return true;
Romain Guy58ecc202012-09-07 11:58:36 -0700241 }
242 ref->recycled = true;
243 if (ref->refCount == 0) {
Romain Guy97dc9172012-09-23 17:46:45 -0700244 deleteResourceReferenceLocked(resource, ref);
Chet Haase547e6652012-10-22 15:07:26 -0700245 return true;
Romain Guy58ecc202012-09-07 11:58:36 -0700246 }
Chet Haase547e6652012-10-22 15:07:26 -0700247 // Still referring to resource, don't recycle yet
248 return false;
Romain Guy58ecc202012-09-07 11:58:36 -0700249}
250
Chet Haasee7d22952010-11-11 16:30:16 -0800251/**
252 * This method should only be called while the mLock mutex is held (that mutex is grabbed
253 * by the various destructor() and recycle() methods which call this method).
254 */
Chris Craikd218a922014-01-02 17:13:34 -0800255void ResourceCache::deleteResourceReferenceLocked(const void* resource, ResourceReference* ref) {
Chet Haase5c13d892010-10-08 08:37:55 -0700256 if (ref->recycled && ref->resourceType == kBitmap) {
John Reck71d08a02014-11-24 15:21:28 -0800257 SkBitmap* bitmap = (SkBitmap*) resource;
258 if (Caches::hasInstance()) {
259 Caches::getInstance().textureCache.releaseTexture(bitmap);
260 }
261 bitmap->setPixels(NULL, NULL);
Chet Haase5c13d892010-10-08 08:37:55 -0700262 }
John Reck0e89e2b2014-10-31 14:49:06 -0700263 if (ref->destroyed) {
Chet Haase5c13d892010-10-08 08:37:55 -0700264 switch (ref->resourceType) {
Romain Guyd586ad92011-06-22 16:14:36 -0700265 case kBitmap: {
266 SkBitmap* bitmap = (SkBitmap*) resource;
Chet Haase5c13d892010-10-08 08:37:55 -0700267 if (Caches::hasInstance()) {
John Reck71d08a02014-11-24 15:21:28 -0800268 Caches::getInstance().textureCache.releaseTexture(bitmap);
Chet Haase5c13d892010-10-08 08:37:55 -0700269 }
John Reck71d08a02014-11-24 15:21:28 -0800270 delete bitmap;
Chet Haase5c13d892010-10-08 08:37:55 -0700271 }
272 break;
Romain Guyd586ad92011-06-22 16:14:36 -0700273 case kPath: {
274 SkPath* path = (SkPath*) resource;
Chet Haase5a7e8282011-02-04 12:50:55 -0800275 if (Caches::hasInstance()) {
276 Caches::getInstance().pathCache.removeDeferred(path);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900277 } else {
278 delete path;
Chet Haase5a7e8282011-02-04 12:50:55 -0800279 }
Chet Haase5a7e8282011-02-04 12:50:55 -0800280 }
281 break;
Romain Guye3b0a012013-06-26 15:45:41 -0700282 case kNinePatch: {
283 if (Caches::hasInstance()) {
284 Caches::getInstance().patchCache.removeDeferred((Res_png_9patch*) resource);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900285 } else {
286 // A Res_png_9patch is actually an array of byte that's larger
287 // than sizeof(Res_png_9patch). It must be freed as an array.
288 int8_t* patch = (int8_t*) resource;
289 delete[] patch;
Romain Guye3b0a012013-06-26 15:45:41 -0700290 }
Romain Guye3b0a012013-06-26 15:45:41 -0700291 }
292 break;
Chet Haase5c13d892010-10-08 08:37:55 -0700293 }
294 }
295 mCache->removeItem(resource);
296 delete ref;
297}
298
299}; // namespace uirenderer
300}; // namespace android