blob: 31bd6376d5d7a6b3716a5c6cb5f3f04ffc63f5d9 [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) {
Romain Guy49c5fc02012-05-15 11:10:01 -070071 incrementRefcount((void*) bitmapResource, kBitmap);
Chet Haase5c13d892010-10-08 08:37:55 -070072}
73
Chris Craikd218a922014-01-02 17:13:34 -080074void ResourceCache::incrementRefcount(const SkPath* pathResource) {
Romain Guy49c5fc02012-05-15 11:10:01 -070075 incrementRefcount((void*) pathResource, kPath);
Chet Haase5a7e8282011-02-04 12:50:55 -080076}
77
Chris Craikd218a922014-01-02 17:13:34 -080078void ResourceCache::incrementRefcount(const Res_png_9patch* patchResource) {
Romain Guye3b0a012013-06-26 15:45:41 -070079 incrementRefcount((void*) patchResource, kNinePatch);
80}
81
Romain Guy58ecc202012-09-07 11:58:36 -070082void ResourceCache::incrementRefcountLocked(void* resource, ResourceType resourceType) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -070083 ssize_t index = mCache->indexOfKey(resource);
84 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
Romain Guy58ecc202012-09-07 11:58:36 -070085 if (ref == NULL || mCache->size() == 0) {
86 ref = new ResourceReference(resourceType);
87 mCache->add(resource, ref);
Chet Haase5c13d892010-10-08 08:37:55 -070088 }
Romain Guy58ecc202012-09-07 11:58:36 -070089 ref->refCount++;
90}
91
Chris Craikd218a922014-01-02 17:13:34 -080092void ResourceCache::incrementRefcountLocked(const SkBitmap* bitmapResource) {
Romain Guy58ecc202012-09-07 11:58:36 -070093 incrementRefcountLocked((void*) bitmapResource, kBitmap);
94}
95
Chris Craikd218a922014-01-02 17:13:34 -080096void ResourceCache::incrementRefcountLocked(const SkPath* pathResource) {
Romain Guy58ecc202012-09-07 11:58:36 -070097 incrementRefcountLocked((void*) pathResource, kPath);
98}
99
Chris Craikd218a922014-01-02 17:13:34 -0800100void ResourceCache::incrementRefcountLocked(const Res_png_9patch* patchResource) {
Romain Guye3b0a012013-06-26 15:45:41 -0700101 incrementRefcountLocked((void*) patchResource, kNinePatch);
102}
103
Romain Guy58ecc202012-09-07 11:58:36 -0700104void ResourceCache::decrementRefcount(void* resource) {
105 Mutex::Autolock _l(mLock);
106 decrementRefcountLocked(resource);
Chet Haase5c13d892010-10-08 08:37:55 -0700107}
108
Chris Craikd218a922014-01-02 17:13:34 -0800109void ResourceCache::decrementRefcount(const SkBitmap* bitmapResource) {
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) {
Romain Guy58ecc202012-09-07 11:58:36 -0700135 decrementRefcountLocked((void*) bitmapResource);
136}
137
Chris Craikd218a922014-01-02 17:13:34 -0800138void ResourceCache::decrementRefcountLocked(const SkPath* pathResource) {
Romain Guy58ecc202012-09-07 11:58:36 -0700139 decrementRefcountLocked((void*) pathResource);
140}
141
Chris Craikd218a922014-01-02 17:13:34 -0800142void ResourceCache::decrementRefcountLocked(const Res_png_9patch* patchResource) {
Romain Guye3b0a012013-06-26 15:45:41 -0700143 decrementRefcountLocked((void*) patchResource);
144}
145
Chet Haase5a7e8282011-02-04 12:50:55 -0800146void ResourceCache::destructor(SkPath* resource) {
147 Mutex::Autolock _l(mLock);
Romain Guy58ecc202012-09-07 11:58:36 -0700148 destructorLocked(resource);
149}
150
151void ResourceCache::destructorLocked(SkPath* resource) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700152 ssize_t index = mCache->indexOfKey(resource);
153 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
Chet Haase5a7e8282011-02-04 12:50:55 -0800154 if (ref == NULL) {
155 // If we're not tracking this resource, just delete it
156 if (Caches::hasInstance()) {
157 Caches::getInstance().pathCache.removeDeferred(resource);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900158 } else {
159 delete resource;
Chet Haase5a7e8282011-02-04 12:50:55 -0800160 }
Chet Haase5a7e8282011-02-04 12:50:55 -0800161 return;
162 }
163 ref->destroyed = true;
164 if (ref->refCount == 0) {
Romain Guy97dc9172012-09-23 17:46:45 -0700165 deleteResourceReferenceLocked(resource, ref);
Chet Haase5a7e8282011-02-04 12:50:55 -0800166 }
167}
168
Chris Craikd218a922014-01-02 17:13:34 -0800169void ResourceCache::destructor(const SkBitmap* resource) {
Chet Haasee7d22952010-11-11 16:30:16 -0800170 Mutex::Autolock _l(mLock);
Romain Guy58ecc202012-09-07 11:58:36 -0700171 destructorLocked(resource);
172}
173
Chris Craikd218a922014-01-02 17:13:34 -0800174void ResourceCache::destructorLocked(const SkBitmap* resource) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700175 ssize_t index = mCache->indexOfKey(resource);
176 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
Chet Haase5c13d892010-10-08 08:37:55 -0700177 if (ref == NULL) {
178 // If we're not tracking this resource, just delete it
179 if (Caches::hasInstance()) {
John Reck71d08a02014-11-24 15:21:28 -0800180 Caches::getInstance().textureCache.releaseTexture(resource);
Chet Haase5c13d892010-10-08 08:37:55 -0700181 }
John Reck71d08a02014-11-24 15:21:28 -0800182 delete resource;
Chet Haase5c13d892010-10-08 08:37:55 -0700183 return;
184 }
185 ref->destroyed = true;
186 if (ref->refCount == 0) {
Romain Guy97dc9172012-09-23 17:46:45 -0700187 deleteResourceReferenceLocked(resource, ref);
Chet Haase5c13d892010-10-08 08:37:55 -0700188 }
189}
190
Romain Guye3b0a012013-06-26 15:45:41 -0700191void ResourceCache::destructor(Res_png_9patch* resource) {
192 Mutex::Autolock _l(mLock);
193 destructorLocked(resource);
194}
195
196void ResourceCache::destructorLocked(Res_png_9patch* resource) {
197 ssize_t index = mCache->indexOfKey(resource);
198 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
199 if (ref == NULL) {
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900200 // If we're not tracking this resource, just delete it
Romain Guye3b0a012013-06-26 15:45:41 -0700201 if (Caches::hasInstance()) {
202 Caches::getInstance().patchCache.removeDeferred(resource);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900203 } else {
204 // A Res_png_9patch is actually an array of byte that's larger
205 // than sizeof(Res_png_9patch). It must be freed as an array.
206 delete[] (int8_t*) resource;
Romain Guye3b0a012013-06-26 15:45:41 -0700207 }
Romain Guye3b0a012013-06-26 15:45:41 -0700208 return;
209 }
210 ref->destroyed = true;
211 if (ref->refCount == 0) {
212 deleteResourceReferenceLocked(resource, ref);
213 }
214}
215
Chet Haase547e6652012-10-22 15:07:26 -0700216/**
217 * Return value indicates whether resource was actually recycled, which happens when RefCnt
218 * reaches 0.
219 */
220bool ResourceCache::recycle(SkBitmap* resource) {
Romain Guy58ecc202012-09-07 11:58:36 -0700221 Mutex::Autolock _l(mLock);
Chet Haase547e6652012-10-22 15:07:26 -0700222 return recycleLocked(resource);
Romain Guy58ecc202012-09-07 11:58:36 -0700223}
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::recycleLocked(SkBitmap* resource) {
Romain Guy58ecc202012-09-07 11:58:36 -0700230 ssize_t index = mCache->indexOfKey(resource);
231 if (index < 0) {
John Reck71d08a02014-11-24 15:21:28 -0800232 if (Caches::hasInstance()) {
233 Caches::getInstance().textureCache.releaseTexture(resource);
234 }
Romain Guy58ecc202012-09-07 11:58:36 -0700235 // 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) {
John Reck71d08a02014-11-24 15:21:28 -0800259 SkBitmap* bitmap = (SkBitmap*) resource;
260 if (Caches::hasInstance()) {
261 Caches::getInstance().textureCache.releaseTexture(bitmap);
262 }
263 bitmap->setPixels(NULL, NULL);
Chet Haase5c13d892010-10-08 08:37:55 -0700264 }
John Reck0e89e2b2014-10-31 14:49:06 -0700265 if (ref->destroyed) {
Chet Haase5c13d892010-10-08 08:37:55 -0700266 switch (ref->resourceType) {
Romain Guyd586ad92011-06-22 16:14:36 -0700267 case kBitmap: {
268 SkBitmap* bitmap = (SkBitmap*) resource;
Chet Haase5c13d892010-10-08 08:37:55 -0700269 if (Caches::hasInstance()) {
John Reck71d08a02014-11-24 15:21:28 -0800270 Caches::getInstance().textureCache.releaseTexture(bitmap);
Chet Haase5c13d892010-10-08 08:37:55 -0700271 }
John Reck71d08a02014-11-24 15:21:28 -0800272 delete bitmap;
Chet Haase5c13d892010-10-08 08:37:55 -0700273 }
274 break;
Romain Guyd586ad92011-06-22 16:14:36 -0700275 case kPath: {
276 SkPath* path = (SkPath*) resource;
Chet Haase5a7e8282011-02-04 12:50:55 -0800277 if (Caches::hasInstance()) {
278 Caches::getInstance().pathCache.removeDeferred(path);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900279 } else {
280 delete path;
Chet Haase5a7e8282011-02-04 12:50:55 -0800281 }
Chet Haase5a7e8282011-02-04 12:50:55 -0800282 }
283 break;
Romain Guye3b0a012013-06-26 15:45:41 -0700284 case kNinePatch: {
285 if (Caches::hasInstance()) {
286 Caches::getInstance().patchCache.removeDeferred((Res_png_9patch*) resource);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900287 } else {
288 // A Res_png_9patch is actually an array of byte that's larger
289 // than sizeof(Res_png_9patch). It must be freed as an array.
290 int8_t* patch = (int8_t*) resource;
291 delete[] patch;
Romain Guye3b0a012013-06-26 15:45:41 -0700292 }
Romain Guye3b0a012013-06-26 15:45:41 -0700293 }
294 break;
Chet Haase5c13d892010-10-08 08:37:55 -0700295 }
296 }
297 mCache->removeItem(resource);
298 delete ref;
299}
300
301}; // namespace uirenderer
302}; // namespace android