blob: 12d4928ad78c5e59963d009d5047fa89eb6a9e33 [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
25#ifdef USE_OPENGL_RENDERER
26using namespace uirenderer;
27ANDROID_SINGLETON_STATIC_INSTANCE(ResourceCache);
28#endif
29
Chet Haase5c13d892010-10-08 08:37:55 -070030namespace uirenderer {
31
32///////////////////////////////////////////////////////////////////////////////
33// Resource cache
34///////////////////////////////////////////////////////////////////////////////
35
36void ResourceCache::logCache() {
Steve Block5baa3a62011-12-20 16:23:08 +000037 ALOGD("ResourceCache: cacheReport:");
Chet Haase5c13d892010-10-08 08:37:55 -070038 for (size_t i = 0; i < mCache->size(); ++i) {
39 ResourceReference* ref = mCache->valueAt(i);
Ashok Bhatf5df7002014-03-25 20:51:35 +000040 ALOGD(" ResourceCache: mCache(%zu): resource, ref = 0x%p, 0x%p",
Chet Haase5c13d892010-10-08 08:37:55 -070041 i, mCache->keyAt(i), mCache->valueAt(i));
Ashok Bhatf5df7002014-03-25 20:51:35 +000042 ALOGD(" ResourceCache: mCache(%zu): refCount, recycled, destroyed, type = %d, %d, %d, %d",
Chet Haase5c13d892010-10-08 08:37:55 -070043 i, ref->refCount, ref->recycled, ref->destroyed, ref->resourceType);
44 }
45}
46
47ResourceCache::ResourceCache() {
Chet Haasee7d22952010-11-11 16:30:16 -080048 Mutex::Autolock _l(mLock);
Chris Craikd218a922014-01-02 17:13:34 -080049 mCache = new KeyedVector<const void*, ResourceReference*>();
Chet Haase5c13d892010-10-08 08:37:55 -070050}
51
52ResourceCache::~ResourceCache() {
Chet Haasee7d22952010-11-11 16:30:16 -080053 Mutex::Autolock _l(mLock);
Chet Haase5c13d892010-10-08 08:37:55 -070054 delete mCache;
55}
56
Romain Guy58ecc202012-09-07 11:58:36 -070057void ResourceCache::lock() {
58 mLock.lock();
59}
60
61void ResourceCache::unlock() {
62 mLock.unlock();
63}
64
Chet Haase5c13d892010-10-08 08:37:55 -070065void ResourceCache::incrementRefcount(void* resource, ResourceType resourceType) {
Chet Haasee7d22952010-11-11 16:30:16 -080066 Mutex::Autolock _l(mLock);
Romain Guy58ecc202012-09-07 11:58:36 -070067 incrementRefcountLocked(resource, resourceType);
Chet Haase5c13d892010-10-08 08:37:55 -070068}
69
Chris Craikd218a922014-01-02 17:13:34 -080070void ResourceCache::incrementRefcount(const SkBitmap* bitmapResource) {
Chris Craik0c20c382013-07-02 10:48:54 -070071 bitmapResource->pixelRef()->globalRef();
Derek Sollenberger6062c592011-02-22 13:55:04 -050072 SkSafeRef(bitmapResource->getColorTable());
Romain Guy49c5fc02012-05-15 11:10:01 -070073 incrementRefcount((void*) bitmapResource, kBitmap);
Chet Haase5c13d892010-10-08 08:37:55 -070074}
75
Chris Craikd218a922014-01-02 17:13:34 -080076void ResourceCache::incrementRefcount(const SkPath* pathResource) {
Romain Guy49c5fc02012-05-15 11:10:01 -070077 incrementRefcount((void*) pathResource, kPath);
Chet Haase5a7e8282011-02-04 12:50:55 -080078}
79
Chris Craikd218a922014-01-02 17:13:34 -080080void ResourceCache::incrementRefcount(const Res_png_9patch* patchResource) {
Romain Guye3b0a012013-06-26 15:45:41 -070081 incrementRefcount((void*) patchResource, kNinePatch);
82}
83
Romain Guy58ecc202012-09-07 11:58:36 -070084void ResourceCache::incrementRefcountLocked(void* resource, ResourceType resourceType) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -070085 ssize_t index = mCache->indexOfKey(resource);
86 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
Romain Guy58ecc202012-09-07 11:58:36 -070087 if (ref == NULL || mCache->size() == 0) {
88 ref = new ResourceReference(resourceType);
89 mCache->add(resource, ref);
Chet Haase5c13d892010-10-08 08:37:55 -070090 }
Romain Guy58ecc202012-09-07 11:58:36 -070091 ref->refCount++;
92}
93
Chris Craikd218a922014-01-02 17:13:34 -080094void ResourceCache::incrementRefcountLocked(const SkBitmap* bitmapResource) {
Chris Craik0c20c382013-07-02 10:48:54 -070095 bitmapResource->pixelRef()->globalRef();
Romain Guy58ecc202012-09-07 11:58:36 -070096 SkSafeRef(bitmapResource->getColorTable());
97 incrementRefcountLocked((void*) bitmapResource, kBitmap);
98}
99
Chris Craikd218a922014-01-02 17:13:34 -0800100void ResourceCache::incrementRefcountLocked(const SkPath* pathResource) {
Romain Guy58ecc202012-09-07 11:58:36 -0700101 incrementRefcountLocked((void*) pathResource, kPath);
102}
103
Chris Craikd218a922014-01-02 17:13:34 -0800104void ResourceCache::incrementRefcountLocked(const Res_png_9patch* patchResource) {
Romain Guye3b0a012013-06-26 15:45:41 -0700105 incrementRefcountLocked((void*) patchResource, kNinePatch);
106}
107
Romain Guy58ecc202012-09-07 11:58:36 -0700108void ResourceCache::decrementRefcount(void* resource) {
109 Mutex::Autolock _l(mLock);
110 decrementRefcountLocked(resource);
Chet Haase5c13d892010-10-08 08:37:55 -0700111}
112
Chris Craikd218a922014-01-02 17:13:34 -0800113void ResourceCache::decrementRefcount(const SkBitmap* bitmapResource) {
Chris Craik0c20c382013-07-02 10:48:54 -0700114 bitmapResource->pixelRef()->globalUnref();
Derek Sollenberger6062c592011-02-22 13:55:04 -0500115 SkSafeUnref(bitmapResource->getColorTable());
Romain Guy43ccf462011-01-14 18:51:01 -0800116 decrementRefcount((void*) bitmapResource);
Chet Haase5c13d892010-10-08 08:37:55 -0700117}
118
Chris Craikd218a922014-01-02 17:13:34 -0800119void ResourceCache::decrementRefcount(const SkPath* pathResource) {
Chet Haase5a7e8282011-02-04 12:50:55 -0800120 decrementRefcount((void*) pathResource);
121}
122
Chris Craikd218a922014-01-02 17:13:34 -0800123void ResourceCache::decrementRefcount(const Res_png_9patch* patchResource) {
Romain Guye3b0a012013-06-26 15:45:41 -0700124 decrementRefcount((void*) patchResource);
125}
126
Romain Guy58ecc202012-09-07 11:58:36 -0700127void ResourceCache::decrementRefcountLocked(void* resource) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700128 ssize_t index = mCache->indexOfKey(resource);
Romain Guy58ecc202012-09-07 11:58:36 -0700129 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
Chet Haase5c13d892010-10-08 08:37:55 -0700130 if (ref == NULL) {
Romain Guy58ecc202012-09-07 11:58:36 -0700131 // 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 -0700132 return;
133 }
Romain Guy58ecc202012-09-07 11:58:36 -0700134 ref->refCount--;
Chet Haase5c13d892010-10-08 08:37:55 -0700135 if (ref->refCount == 0) {
Romain Guy97dc9172012-09-23 17:46:45 -0700136 deleteResourceReferenceLocked(resource, ref);
Chet Haase5c13d892010-10-08 08:37:55 -0700137 }
138}
139
Chris Craikd218a922014-01-02 17:13:34 -0800140void ResourceCache::decrementRefcountLocked(const SkBitmap* bitmapResource) {
Chris Craik0c20c382013-07-02 10:48:54 -0700141 bitmapResource->pixelRef()->globalUnref();
Romain Guy58ecc202012-09-07 11:58:36 -0700142 SkSafeUnref(bitmapResource->getColorTable());
143 decrementRefcountLocked((void*) bitmapResource);
144}
145
Chris Craikd218a922014-01-02 17:13:34 -0800146void ResourceCache::decrementRefcountLocked(const SkPath* pathResource) {
Romain Guy58ecc202012-09-07 11:58:36 -0700147 decrementRefcountLocked((void*) pathResource);
148}
149
Chris Craikd218a922014-01-02 17:13:34 -0800150void ResourceCache::decrementRefcountLocked(const Res_png_9patch* patchResource) {
Romain Guye3b0a012013-06-26 15:45:41 -0700151 decrementRefcountLocked((void*) patchResource);
152}
153
Chet Haase5a7e8282011-02-04 12:50:55 -0800154void ResourceCache::destructor(SkPath* resource) {
155 Mutex::Autolock _l(mLock);
Romain Guy58ecc202012-09-07 11:58:36 -0700156 destructorLocked(resource);
157}
158
159void ResourceCache::destructorLocked(SkPath* resource) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700160 ssize_t index = mCache->indexOfKey(resource);
161 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
Chet Haase5a7e8282011-02-04 12:50:55 -0800162 if (ref == NULL) {
163 // If we're not tracking this resource, just delete it
164 if (Caches::hasInstance()) {
165 Caches::getInstance().pathCache.removeDeferred(resource);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900166 } else {
167 delete resource;
Chet Haase5a7e8282011-02-04 12:50:55 -0800168 }
Chet Haase5a7e8282011-02-04 12:50:55 -0800169 return;
170 }
171 ref->destroyed = true;
172 if (ref->refCount == 0) {
Romain Guy97dc9172012-09-23 17:46:45 -0700173 deleteResourceReferenceLocked(resource, ref);
Chet Haase5a7e8282011-02-04 12:50:55 -0800174 }
175}
176
Chris Craikd218a922014-01-02 17:13:34 -0800177void ResourceCache::destructor(const SkBitmap* resource) {
Chet Haasee7d22952010-11-11 16:30:16 -0800178 Mutex::Autolock _l(mLock);
Romain Guy58ecc202012-09-07 11:58:36 -0700179 destructorLocked(resource);
180}
181
Chris Craikd218a922014-01-02 17:13:34 -0800182void ResourceCache::destructorLocked(const SkBitmap* resource) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700183 ssize_t index = mCache->indexOfKey(resource);
184 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
Chet Haase5c13d892010-10-08 08:37:55 -0700185 if (ref == NULL) {
186 // If we're not tracking this resource, just delete it
187 if (Caches::hasInstance()) {
Romain Guyfe48f652010-11-11 15:36:56 -0800188 Caches::getInstance().textureCache.removeDeferred(resource);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900189 } else {
190 delete resource;
Chet Haase5c13d892010-10-08 08:37:55 -0700191 }
Chet Haase5c13d892010-10-08 08:37:55 -0700192 return;
193 }
194 ref->destroyed = true;
195 if (ref->refCount == 0) {
Romain Guy97dc9172012-09-23 17:46:45 -0700196 deleteResourceReferenceLocked(resource, ref);
Chet Haase5c13d892010-10-08 08:37:55 -0700197 }
198}
199
Romain Guye3b0a012013-06-26 15:45:41 -0700200void ResourceCache::destructor(Res_png_9patch* resource) {
201 Mutex::Autolock _l(mLock);
202 destructorLocked(resource);
203}
204
205void ResourceCache::destructorLocked(Res_png_9patch* resource) {
206 ssize_t index = mCache->indexOfKey(resource);
207 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
208 if (ref == NULL) {
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900209 // If we're not tracking this resource, just delete it
Romain Guye3b0a012013-06-26 15:45:41 -0700210 if (Caches::hasInstance()) {
211 Caches::getInstance().patchCache.removeDeferred(resource);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900212 } else {
213 // A Res_png_9patch is actually an array of byte that's larger
214 // than sizeof(Res_png_9patch). It must be freed as an array.
215 delete[] (int8_t*) resource;
Romain Guye3b0a012013-06-26 15:45:41 -0700216 }
Romain Guye3b0a012013-06-26 15:45:41 -0700217 return;
218 }
219 ref->destroyed = true;
220 if (ref->refCount == 0) {
221 deleteResourceReferenceLocked(resource, ref);
222 }
223}
224
Chet Haase547e6652012-10-22 15:07:26 -0700225/**
226 * Return value indicates whether resource was actually recycled, which happens when RefCnt
227 * reaches 0.
228 */
229bool ResourceCache::recycle(SkBitmap* resource) {
Romain Guy58ecc202012-09-07 11:58:36 -0700230 Mutex::Autolock _l(mLock);
Chet Haase547e6652012-10-22 15:07:26 -0700231 return recycleLocked(resource);
Romain Guy58ecc202012-09-07 11:58:36 -0700232}
233
Chet Haase547e6652012-10-22 15:07:26 -0700234/**
235 * Return value indicates whether resource was actually recycled, which happens when RefCnt
236 * reaches 0.
237 */
238bool ResourceCache::recycleLocked(SkBitmap* resource) {
Romain Guy58ecc202012-09-07 11:58:36 -0700239 ssize_t index = mCache->indexOfKey(resource);
240 if (index < 0) {
241 // not tracking this resource; just recycle the pixel data
242 resource->setPixels(NULL, NULL);
Chet Haase547e6652012-10-22 15:07:26 -0700243 return true;
Romain Guy58ecc202012-09-07 11:58:36 -0700244 }
245 ResourceReference* ref = mCache->valueAt(index);
246 if (ref == NULL) {
247 // 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 -0700248 return true;
Romain Guy58ecc202012-09-07 11:58:36 -0700249 }
250 ref->recycled = true;
251 if (ref->refCount == 0) {
Romain Guy97dc9172012-09-23 17:46:45 -0700252 deleteResourceReferenceLocked(resource, ref);
Chet Haase547e6652012-10-22 15:07:26 -0700253 return true;
Romain Guy58ecc202012-09-07 11:58:36 -0700254 }
Chet Haase547e6652012-10-22 15:07:26 -0700255 // Still referring to resource, don't recycle yet
256 return false;
Romain Guy58ecc202012-09-07 11:58:36 -0700257}
258
Chet Haasee7d22952010-11-11 16:30:16 -0800259/**
260 * This method should only be called while the mLock mutex is held (that mutex is grabbed
261 * by the various destructor() and recycle() methods which call this method).
262 */
Chris Craikd218a922014-01-02 17:13:34 -0800263void ResourceCache::deleteResourceReferenceLocked(const void* resource, ResourceReference* ref) {
Chet Haase5c13d892010-10-08 08:37:55 -0700264 if (ref->recycled && ref->resourceType == kBitmap) {
265 ((SkBitmap*) resource)->setPixels(NULL, NULL);
266 }
John Reck0e89e2b2014-10-31 14:49:06 -0700267 if (ref->destroyed) {
Chet Haase5c13d892010-10-08 08:37:55 -0700268 switch (ref->resourceType) {
Romain Guyd586ad92011-06-22 16:14:36 -0700269 case kBitmap: {
270 SkBitmap* bitmap = (SkBitmap*) resource;
Chet Haase5c13d892010-10-08 08:37:55 -0700271 if (Caches::hasInstance()) {
Romain Guyfe48f652010-11-11 15:36:56 -0800272 Caches::getInstance().textureCache.removeDeferred(bitmap);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900273 } else {
274 delete bitmap;
Chet Haase5c13d892010-10-08 08:37:55 -0700275 }
Chet Haase5c13d892010-10-08 08:37:55 -0700276 }
277 break;
Romain Guyd586ad92011-06-22 16:14:36 -0700278 case kPath: {
279 SkPath* path = (SkPath*) resource;
Chet Haase5a7e8282011-02-04 12:50:55 -0800280 if (Caches::hasInstance()) {
281 Caches::getInstance().pathCache.removeDeferred(path);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900282 } else {
283 delete path;
Chet Haase5a7e8282011-02-04 12:50:55 -0800284 }
Chet Haase5a7e8282011-02-04 12:50:55 -0800285 }
286 break;
Romain Guye3b0a012013-06-26 15:45:41 -0700287 case kNinePatch: {
288 if (Caches::hasInstance()) {
289 Caches::getInstance().patchCache.removeDeferred((Res_png_9patch*) resource);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900290 } else {
291 // A Res_png_9patch is actually an array of byte that's larger
292 // than sizeof(Res_png_9patch). It must be freed as an array.
293 int8_t* patch = (int8_t*) resource;
294 delete[] patch;
Romain Guye3b0a012013-06-26 15:45:41 -0700295 }
Romain Guye3b0a012013-06-26 15:45:41 -0700296 }
297 break;
Chet Haase5c13d892010-10-08 08:37:55 -0700298 }
299 }
300 mCache->removeItem(resource);
301 delete ref;
302}
303
304}; // namespace uirenderer
305}; // namespace android