blob: 329d92f0be13ec4ea95b77bfd509c6f98d6ffc56 [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 {
24namespace uirenderer {
25
26///////////////////////////////////////////////////////////////////////////////
27// Resource cache
28///////////////////////////////////////////////////////////////////////////////
29
30void ResourceCache::logCache() {
Steve Block5baa3a62011-12-20 16:23:08 +000031 ALOGD("ResourceCache: cacheReport:");
Chet Haase5c13d892010-10-08 08:37:55 -070032 for (size_t i = 0; i < mCache->size(); ++i) {
33 ResourceReference* ref = mCache->valueAt(i);
Ashok Bhatf5df7002014-03-25 20:51:35 +000034 ALOGD(" ResourceCache: mCache(%zu): resource, ref = 0x%p, 0x%p",
Chet Haase5c13d892010-10-08 08:37:55 -070035 i, mCache->keyAt(i), mCache->valueAt(i));
Ashok Bhatf5df7002014-03-25 20:51:35 +000036 ALOGD(" ResourceCache: mCache(%zu): refCount, recycled, destroyed, type = %d, %d, %d, %d",
Chet Haase5c13d892010-10-08 08:37:55 -070037 i, ref->refCount, ref->recycled, ref->destroyed, ref->resourceType);
38 }
39}
40
41ResourceCache::ResourceCache() {
Chet Haasee7d22952010-11-11 16:30:16 -080042 Mutex::Autolock _l(mLock);
Chris Craikd218a922014-01-02 17:13:34 -080043 mCache = new KeyedVector<const void*, ResourceReference*>();
Chet Haase5c13d892010-10-08 08:37:55 -070044}
45
46ResourceCache::~ResourceCache() {
Chet Haasee7d22952010-11-11 16:30:16 -080047 Mutex::Autolock _l(mLock);
Chet Haase5c13d892010-10-08 08:37:55 -070048 delete mCache;
49}
50
Romain Guy58ecc202012-09-07 11:58:36 -070051void ResourceCache::lock() {
52 mLock.lock();
53}
54
55void ResourceCache::unlock() {
56 mLock.unlock();
57}
58
Chet Haase5c13d892010-10-08 08:37:55 -070059void ResourceCache::incrementRefcount(void* resource, ResourceType resourceType) {
Chet Haasee7d22952010-11-11 16:30:16 -080060 Mutex::Autolock _l(mLock);
Romain Guy58ecc202012-09-07 11:58:36 -070061 incrementRefcountLocked(resource, resourceType);
Chet Haase5c13d892010-10-08 08:37:55 -070062}
63
Chris Craikd218a922014-01-02 17:13:34 -080064void ResourceCache::incrementRefcount(const SkBitmap* bitmapResource) {
Chris Craik0c20c382013-07-02 10:48:54 -070065 bitmapResource->pixelRef()->globalRef();
Derek Sollenberger6062c592011-02-22 13:55:04 -050066 SkSafeRef(bitmapResource->getColorTable());
Romain Guy49c5fc02012-05-15 11:10:01 -070067 incrementRefcount((void*) bitmapResource, kBitmap);
Chet Haase5c13d892010-10-08 08:37:55 -070068}
69
Chris Craikd218a922014-01-02 17:13:34 -080070void ResourceCache::incrementRefcount(const SkPath* pathResource) {
Romain Guy49c5fc02012-05-15 11:10:01 -070071 incrementRefcount((void*) pathResource, kPath);
Chet Haase5a7e8282011-02-04 12:50:55 -080072}
73
Chris Craikd218a922014-01-02 17:13:34 -080074void ResourceCache::incrementRefcount(const Res_png_9patch* patchResource) {
Romain Guye3b0a012013-06-26 15:45:41 -070075 incrementRefcount((void*) patchResource, kNinePatch);
76}
77
Romain Guy58ecc202012-09-07 11:58:36 -070078void ResourceCache::incrementRefcountLocked(void* resource, ResourceType resourceType) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -070079 ssize_t index = mCache->indexOfKey(resource);
80 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
Romain Guy58ecc202012-09-07 11:58:36 -070081 if (ref == NULL || mCache->size() == 0) {
82 ref = new ResourceReference(resourceType);
83 mCache->add(resource, ref);
Chet Haase5c13d892010-10-08 08:37:55 -070084 }
Romain Guy58ecc202012-09-07 11:58:36 -070085 ref->refCount++;
86}
87
Chris Craikd218a922014-01-02 17:13:34 -080088void ResourceCache::incrementRefcountLocked(const SkBitmap* bitmapResource) {
Chris Craik0c20c382013-07-02 10:48:54 -070089 bitmapResource->pixelRef()->globalRef();
Romain Guy58ecc202012-09-07 11:58:36 -070090 SkSafeRef(bitmapResource->getColorTable());
91 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) {
Chris Craik0c20c382013-07-02 10:48:54 -0700108 bitmapResource->pixelRef()->globalUnref();
Derek Sollenberger6062c592011-02-22 13:55:04 -0500109 SkSafeUnref(bitmapResource->getColorTable());
Romain Guy43ccf462011-01-14 18:51:01 -0800110 decrementRefcount((void*) bitmapResource);
Chet Haase5c13d892010-10-08 08:37:55 -0700111}
112
Chris Craikd218a922014-01-02 17:13:34 -0800113void ResourceCache::decrementRefcount(const SkPath* pathResource) {
Chet Haase5a7e8282011-02-04 12:50:55 -0800114 decrementRefcount((void*) pathResource);
115}
116
Chris Craikd218a922014-01-02 17:13:34 -0800117void ResourceCache::decrementRefcount(const Res_png_9patch* patchResource) {
Romain Guye3b0a012013-06-26 15:45:41 -0700118 decrementRefcount((void*) patchResource);
119}
120
Romain Guy58ecc202012-09-07 11:58:36 -0700121void ResourceCache::decrementRefcountLocked(void* resource) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700122 ssize_t index = mCache->indexOfKey(resource);
Romain Guy58ecc202012-09-07 11:58:36 -0700123 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
Chet Haase5c13d892010-10-08 08:37:55 -0700124 if (ref == NULL) {
Romain Guy58ecc202012-09-07 11:58:36 -0700125 // 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 -0700126 return;
127 }
Romain Guy58ecc202012-09-07 11:58:36 -0700128 ref->refCount--;
Chet Haase5c13d892010-10-08 08:37:55 -0700129 if (ref->refCount == 0) {
Romain Guy97dc9172012-09-23 17:46:45 -0700130 deleteResourceReferenceLocked(resource, ref);
Chet Haase5c13d892010-10-08 08:37:55 -0700131 }
132}
133
Chris Craikd218a922014-01-02 17:13:34 -0800134void ResourceCache::decrementRefcountLocked(const SkBitmap* bitmapResource) {
Chris Craik0c20c382013-07-02 10:48:54 -0700135 bitmapResource->pixelRef()->globalUnref();
Romain Guy58ecc202012-09-07 11:58:36 -0700136 SkSafeUnref(bitmapResource->getColorTable());
137 decrementRefcountLocked((void*) bitmapResource);
138}
139
Chris Craikd218a922014-01-02 17:13:34 -0800140void ResourceCache::decrementRefcountLocked(const SkPath* pathResource) {
Romain Guy58ecc202012-09-07 11:58:36 -0700141 decrementRefcountLocked((void*) pathResource);
142}
143
Chris Craikd218a922014-01-02 17:13:34 -0800144void ResourceCache::decrementRefcountLocked(const Res_png_9patch* patchResource) {
Romain Guye3b0a012013-06-26 15:45:41 -0700145 decrementRefcountLocked((void*) patchResource);
146}
147
Chet Haase5a7e8282011-02-04 12:50:55 -0800148void ResourceCache::destructor(SkPath* resource) {
149 Mutex::Autolock _l(mLock);
Romain Guy58ecc202012-09-07 11:58:36 -0700150 destructorLocked(resource);
151}
152
153void ResourceCache::destructorLocked(SkPath* resource) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700154 ssize_t index = mCache->indexOfKey(resource);
155 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
Chet Haase5a7e8282011-02-04 12:50:55 -0800156 if (ref == NULL) {
157 // If we're not tracking this resource, just delete it
158 if (Caches::hasInstance()) {
159 Caches::getInstance().pathCache.removeDeferred(resource);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900160 } else {
161 delete resource;
Chet Haase5a7e8282011-02-04 12:50:55 -0800162 }
Chet Haase5a7e8282011-02-04 12:50:55 -0800163 return;
164 }
165 ref->destroyed = true;
166 if (ref->refCount == 0) {
Romain Guy97dc9172012-09-23 17:46:45 -0700167 deleteResourceReferenceLocked(resource, ref);
Chet Haase5a7e8282011-02-04 12:50:55 -0800168 }
169}
170
Chris Craikd218a922014-01-02 17:13:34 -0800171void ResourceCache::destructor(const SkBitmap* resource) {
Chet Haasee7d22952010-11-11 16:30:16 -0800172 Mutex::Autolock _l(mLock);
Romain Guy58ecc202012-09-07 11:58:36 -0700173 destructorLocked(resource);
174}
175
Chris Craikd218a922014-01-02 17:13:34 -0800176void ResourceCache::destructorLocked(const SkBitmap* resource) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700177 ssize_t index = mCache->indexOfKey(resource);
178 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
Chet Haase5c13d892010-10-08 08:37:55 -0700179 if (ref == NULL) {
180 // If we're not tracking this resource, just delete it
181 if (Caches::hasInstance()) {
Romain Guyfe48f652010-11-11 15:36:56 -0800182 Caches::getInstance().textureCache.removeDeferred(resource);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900183 } else {
184 delete resource;
Chet Haase5c13d892010-10-08 08:37:55 -0700185 }
Chet Haase5c13d892010-10-08 08:37:55 -0700186 return;
187 }
188 ref->destroyed = true;
189 if (ref->refCount == 0) {
Romain Guy97dc9172012-09-23 17:46:45 -0700190 deleteResourceReferenceLocked(resource, ref);
Chet Haase5c13d892010-10-08 08:37:55 -0700191 }
192}
193
Romain Guye3b0a012013-06-26 15:45:41 -0700194void ResourceCache::destructor(Res_png_9patch* resource) {
195 Mutex::Autolock _l(mLock);
196 destructorLocked(resource);
197}
198
199void ResourceCache::destructorLocked(Res_png_9patch* resource) {
200 ssize_t index = mCache->indexOfKey(resource);
201 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
202 if (ref == NULL) {
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900203 // If we're not tracking this resource, just delete it
Romain Guye3b0a012013-06-26 15:45:41 -0700204 if (Caches::hasInstance()) {
205 Caches::getInstance().patchCache.removeDeferred(resource);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900206 } else {
207 // A Res_png_9patch is actually an array of byte that's larger
208 // than sizeof(Res_png_9patch). It must be freed as an array.
209 delete[] (int8_t*) resource;
Romain Guye3b0a012013-06-26 15:45:41 -0700210 }
Romain Guye3b0a012013-06-26 15:45:41 -0700211 return;
212 }
213 ref->destroyed = true;
214 if (ref->refCount == 0) {
215 deleteResourceReferenceLocked(resource, ref);
216 }
217}
218
Chet Haase547e6652012-10-22 15:07:26 -0700219/**
220 * Return value indicates whether resource was actually recycled, which happens when RefCnt
221 * reaches 0.
222 */
223bool ResourceCache::recycle(SkBitmap* resource) {
Romain Guy58ecc202012-09-07 11:58:36 -0700224 Mutex::Autolock _l(mLock);
Chet Haase547e6652012-10-22 15:07:26 -0700225 return recycleLocked(resource);
Romain Guy58ecc202012-09-07 11:58:36 -0700226}
227
Chet Haase547e6652012-10-22 15:07:26 -0700228/**
229 * Return value indicates whether resource was actually recycled, which happens when RefCnt
230 * reaches 0.
231 */
232bool ResourceCache::recycleLocked(SkBitmap* resource) {
Romain Guy58ecc202012-09-07 11:58:36 -0700233 ssize_t index = mCache->indexOfKey(resource);
234 if (index < 0) {
235 // not tracking this resource; just recycle the pixel data
236 resource->setPixels(NULL, NULL);
Chet Haase547e6652012-10-22 15:07:26 -0700237 return true;
Romain Guy58ecc202012-09-07 11:58:36 -0700238 }
239 ResourceReference* ref = mCache->valueAt(index);
240 if (ref == NULL) {
241 // 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 -0700242 return true;
Romain Guy58ecc202012-09-07 11:58:36 -0700243 }
244 ref->recycled = true;
245 if (ref->refCount == 0) {
Romain Guy97dc9172012-09-23 17:46:45 -0700246 deleteResourceReferenceLocked(resource, ref);
Chet Haase547e6652012-10-22 15:07:26 -0700247 return true;
Romain Guy58ecc202012-09-07 11:58:36 -0700248 }
Chet Haase547e6652012-10-22 15:07:26 -0700249 // Still referring to resource, don't recycle yet
250 return false;
Romain Guy58ecc202012-09-07 11:58:36 -0700251}
252
Chet Haasee7d22952010-11-11 16:30:16 -0800253/**
254 * This method should only be called while the mLock mutex is held (that mutex is grabbed
255 * by the various destructor() and recycle() methods which call this method).
256 */
Chris Craikd218a922014-01-02 17:13:34 -0800257void ResourceCache::deleteResourceReferenceLocked(const void* resource, ResourceReference* ref) {
Chet Haase5c13d892010-10-08 08:37:55 -0700258 if (ref->recycled && ref->resourceType == kBitmap) {
259 ((SkBitmap*) resource)->setPixels(NULL, NULL);
260 }
John Reck0e89e2b2014-10-31 14:49:06 -0700261 if (ref->destroyed) {
Chet Haase5c13d892010-10-08 08:37:55 -0700262 switch (ref->resourceType) {
Romain Guyd586ad92011-06-22 16:14:36 -0700263 case kBitmap: {
264 SkBitmap* bitmap = (SkBitmap*) resource;
Chet Haase5c13d892010-10-08 08:37:55 -0700265 if (Caches::hasInstance()) {
Romain Guyfe48f652010-11-11 15:36:56 -0800266 Caches::getInstance().textureCache.removeDeferred(bitmap);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900267 } else {
268 delete bitmap;
Chet Haase5c13d892010-10-08 08:37:55 -0700269 }
Chet Haase5c13d892010-10-08 08:37:55 -0700270 }
271 break;
Romain Guyd586ad92011-06-22 16:14:36 -0700272 case kPath: {
273 SkPath* path = (SkPath*) resource;
Chet Haase5a7e8282011-02-04 12:50:55 -0800274 if (Caches::hasInstance()) {
275 Caches::getInstance().pathCache.removeDeferred(path);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900276 } else {
277 delete path;
Chet Haase5a7e8282011-02-04 12:50:55 -0800278 }
Chet Haase5a7e8282011-02-04 12:50:55 -0800279 }
280 break;
Romain Guye3b0a012013-06-26 15:45:41 -0700281 case kNinePatch: {
282 if (Caches::hasInstance()) {
283 Caches::getInstance().patchCache.removeDeferred((Res_png_9patch*) resource);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900284 } else {
285 // A Res_png_9patch is actually an array of byte that's larger
286 // than sizeof(Res_png_9patch). It must be freed as an array.
287 int8_t* patch = (int8_t*) resource;
288 delete[] patch;
Romain Guye3b0a012013-06-26 15:45:41 -0700289 }
Romain Guye3b0a012013-06-26 15:45:41 -0700290 }
291 break;
Chet Haase5c13d892010-10-08 08:37:55 -0700292 }
293 }
294 mCache->removeItem(resource);
295 delete ref;
296}
297
298}; // namespace uirenderer
299}; // namespace android