blob: 13a3e8e68f6f377017dbba901ffd02b4a5e880d3 [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
Chet Haase5c13d892010-10-08 08:37:55 -070074void ResourceCache::incrementRefcount(SkiaShader* shaderResource) {
Derek Sollenberger6062c592011-02-22 13:55:04 -050075 SkSafeRef(shaderResource->getSkShader());
Romain Guy43ccf462011-01-14 18:51:01 -080076 incrementRefcount((void*) shaderResource, kShader);
Chet Haase5c13d892010-10-08 08:37:55 -070077}
78
Chris Craikd218a922014-01-02 17:13:34 -080079void ResourceCache::incrementRefcount(const Res_png_9patch* patchResource) {
Romain Guye3b0a012013-06-26 15:45:41 -070080 incrementRefcount((void*) patchResource, kNinePatch);
81}
82
Chet Haase603f6de2012-09-14 15:31:25 -070083void ResourceCache::incrementRefcount(Layer* layerResource) {
84 incrementRefcount((void*) layerResource, kLayer);
85}
86
Romain Guy58ecc202012-09-07 11:58:36 -070087void ResourceCache::incrementRefcountLocked(void* resource, ResourceType resourceType) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -070088 ssize_t index = mCache->indexOfKey(resource);
89 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
Romain Guy58ecc202012-09-07 11:58:36 -070090 if (ref == NULL || mCache->size() == 0) {
91 ref = new ResourceReference(resourceType);
92 mCache->add(resource, ref);
Chet Haase5c13d892010-10-08 08:37:55 -070093 }
Romain Guy58ecc202012-09-07 11:58:36 -070094 ref->refCount++;
95}
96
Chris Craikd218a922014-01-02 17:13:34 -080097void ResourceCache::incrementRefcountLocked(const SkBitmap* bitmapResource) {
Chris Craik0c20c382013-07-02 10:48:54 -070098 bitmapResource->pixelRef()->globalRef();
Romain Guy58ecc202012-09-07 11:58:36 -070099 SkSafeRef(bitmapResource->getColorTable());
100 incrementRefcountLocked((void*) bitmapResource, kBitmap);
101}
102
Chris Craikd218a922014-01-02 17:13:34 -0800103void ResourceCache::incrementRefcountLocked(const SkPath* pathResource) {
Romain Guy58ecc202012-09-07 11:58:36 -0700104 incrementRefcountLocked((void*) pathResource, kPath);
105}
106
107void ResourceCache::incrementRefcountLocked(SkiaShader* shaderResource) {
108 SkSafeRef(shaderResource->getSkShader());
109 incrementRefcountLocked((void*) shaderResource, kShader);
110}
111
Chris Craikd218a922014-01-02 17:13:34 -0800112void ResourceCache::incrementRefcountLocked(const Res_png_9patch* patchResource) {
Romain Guye3b0a012013-06-26 15:45:41 -0700113 incrementRefcountLocked((void*) patchResource, kNinePatch);
114}
115
Chet Haase603f6de2012-09-14 15:31:25 -0700116void ResourceCache::incrementRefcountLocked(Layer* layerResource) {
117 incrementRefcountLocked((void*) layerResource, kLayer);
118}
119
Romain Guy58ecc202012-09-07 11:58:36 -0700120void ResourceCache::decrementRefcount(void* resource) {
121 Mutex::Autolock _l(mLock);
122 decrementRefcountLocked(resource);
Chet Haase5c13d892010-10-08 08:37:55 -0700123}
124
Chris Craikd218a922014-01-02 17:13:34 -0800125void ResourceCache::decrementRefcount(const SkBitmap* bitmapResource) {
Chris Craik0c20c382013-07-02 10:48:54 -0700126 bitmapResource->pixelRef()->globalUnref();
Derek Sollenberger6062c592011-02-22 13:55:04 -0500127 SkSafeUnref(bitmapResource->getColorTable());
Romain Guy43ccf462011-01-14 18:51:01 -0800128 decrementRefcount((void*) bitmapResource);
Chet Haase5c13d892010-10-08 08:37:55 -0700129}
130
Chris Craikd218a922014-01-02 17:13:34 -0800131void ResourceCache::decrementRefcount(const SkPath* pathResource) {
Chet Haase5a7e8282011-02-04 12:50:55 -0800132 decrementRefcount((void*) pathResource);
133}
134
Chet Haase5c13d892010-10-08 08:37:55 -0700135void ResourceCache::decrementRefcount(SkiaShader* shaderResource) {
Derek Sollenberger6062c592011-02-22 13:55:04 -0500136 SkSafeUnref(shaderResource->getSkShader());
Romain Guy43ccf462011-01-14 18:51:01 -0800137 decrementRefcount((void*) shaderResource);
Chet Haase5c13d892010-10-08 08:37:55 -0700138}
139
Chris Craikd218a922014-01-02 17:13:34 -0800140void ResourceCache::decrementRefcount(const Res_png_9patch* patchResource) {
Romain Guye3b0a012013-06-26 15:45:41 -0700141 decrementRefcount((void*) patchResource);
142}
143
Chet Haase603f6de2012-09-14 15:31:25 -0700144void ResourceCache::decrementRefcount(Layer* layerResource) {
145 decrementRefcount((void*) layerResource);
146}
147
Romain Guy58ecc202012-09-07 11:58:36 -0700148void ResourceCache::decrementRefcountLocked(void* resource) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700149 ssize_t index = mCache->indexOfKey(resource);
Romain Guy58ecc202012-09-07 11:58:36 -0700150 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
Chet Haase5c13d892010-10-08 08:37:55 -0700151 if (ref == NULL) {
Romain Guy58ecc202012-09-07 11:58:36 -0700152 // 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 -0700153 return;
154 }
Romain Guy58ecc202012-09-07 11:58:36 -0700155 ref->refCount--;
Chet Haase5c13d892010-10-08 08:37:55 -0700156 if (ref->refCount == 0) {
Romain Guy97dc9172012-09-23 17:46:45 -0700157 deleteResourceReferenceLocked(resource, ref);
Chet Haase5c13d892010-10-08 08:37:55 -0700158 }
159}
160
Chris Craikd218a922014-01-02 17:13:34 -0800161void ResourceCache::decrementRefcountLocked(const SkBitmap* bitmapResource) {
Chris Craik0c20c382013-07-02 10:48:54 -0700162 bitmapResource->pixelRef()->globalUnref();
Romain Guy58ecc202012-09-07 11:58:36 -0700163 SkSafeUnref(bitmapResource->getColorTable());
164 decrementRefcountLocked((void*) bitmapResource);
165}
166
Chris Craikd218a922014-01-02 17:13:34 -0800167void ResourceCache::decrementRefcountLocked(const SkPath* pathResource) {
Romain Guy58ecc202012-09-07 11:58:36 -0700168 decrementRefcountLocked((void*) pathResource);
169}
170
171void ResourceCache::decrementRefcountLocked(SkiaShader* shaderResource) {
172 SkSafeUnref(shaderResource->getSkShader());
173 decrementRefcountLocked((void*) shaderResource);
174}
175
Chris Craikd218a922014-01-02 17:13:34 -0800176void ResourceCache::decrementRefcountLocked(const Res_png_9patch* patchResource) {
Romain Guye3b0a012013-06-26 15:45:41 -0700177 decrementRefcountLocked((void*) patchResource);
178}
179
Chet Haase603f6de2012-09-14 15:31:25 -0700180void ResourceCache::decrementRefcountLocked(Layer* layerResource) {
181 decrementRefcountLocked((void*) layerResource);
182}
183
Chet Haase5a7e8282011-02-04 12:50:55 -0800184void ResourceCache::destructor(SkPath* resource) {
185 Mutex::Autolock _l(mLock);
Romain Guy58ecc202012-09-07 11:58:36 -0700186 destructorLocked(resource);
187}
188
189void ResourceCache::destructorLocked(SkPath* resource) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700190 ssize_t index = mCache->indexOfKey(resource);
191 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
Chet Haase5a7e8282011-02-04 12:50:55 -0800192 if (ref == NULL) {
193 // If we're not tracking this resource, just delete it
194 if (Caches::hasInstance()) {
195 Caches::getInstance().pathCache.removeDeferred(resource);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900196 } else {
197 delete resource;
Chet Haase5a7e8282011-02-04 12:50:55 -0800198 }
Chet Haase5a7e8282011-02-04 12:50:55 -0800199 return;
200 }
201 ref->destroyed = true;
202 if (ref->refCount == 0) {
Romain Guy97dc9172012-09-23 17:46:45 -0700203 deleteResourceReferenceLocked(resource, ref);
Chet Haase5a7e8282011-02-04 12:50:55 -0800204 }
205}
206
Chris Craikd218a922014-01-02 17:13:34 -0800207void ResourceCache::destructor(const SkBitmap* resource) {
Chet Haasee7d22952010-11-11 16:30:16 -0800208 Mutex::Autolock _l(mLock);
Romain Guy58ecc202012-09-07 11:58:36 -0700209 destructorLocked(resource);
210}
211
Chris Craikd218a922014-01-02 17:13:34 -0800212void ResourceCache::destructorLocked(const SkBitmap* resource) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700213 ssize_t index = mCache->indexOfKey(resource);
214 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
Chet Haase5c13d892010-10-08 08:37:55 -0700215 if (ref == NULL) {
216 // If we're not tracking this resource, just delete it
217 if (Caches::hasInstance()) {
Romain Guyfe48f652010-11-11 15:36:56 -0800218 Caches::getInstance().textureCache.removeDeferred(resource);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900219 } else {
220 delete resource;
Chet Haase5c13d892010-10-08 08:37:55 -0700221 }
Chet Haase5c13d892010-10-08 08:37:55 -0700222 return;
223 }
224 ref->destroyed = true;
225 if (ref->refCount == 0) {
Romain Guy97dc9172012-09-23 17:46:45 -0700226 deleteResourceReferenceLocked(resource, ref);
Chet Haase5c13d892010-10-08 08:37:55 -0700227 }
228}
229
Chet Haase5c13d892010-10-08 08:37:55 -0700230void ResourceCache::destructor(SkiaShader* resource) {
Chet Haasee7d22952010-11-11 16:30:16 -0800231 Mutex::Autolock _l(mLock);
Romain Guy58ecc202012-09-07 11:58:36 -0700232 destructorLocked(resource);
233}
234
235void ResourceCache::destructorLocked(SkiaShader* resource) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700236 ssize_t index = mCache->indexOfKey(resource);
237 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
Chet Haase5c13d892010-10-08 08:37:55 -0700238 if (ref == NULL) {
239 // If we're not tracking this resource, just delete it
Chet Haase5c13d892010-10-08 08:37:55 -0700240 delete resource;
241 return;
242 }
243 ref->destroyed = true;
244 if (ref->refCount == 0) {
Romain Guy97dc9172012-09-23 17:46:45 -0700245 deleteResourceReferenceLocked(resource, ref);
Chet Haase5c13d892010-10-08 08:37:55 -0700246 }
247}
248
Romain Guye3b0a012013-06-26 15:45:41 -0700249void ResourceCache::destructor(Res_png_9patch* resource) {
250 Mutex::Autolock _l(mLock);
251 destructorLocked(resource);
252}
253
254void ResourceCache::destructorLocked(Res_png_9patch* resource) {
255 ssize_t index = mCache->indexOfKey(resource);
256 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
257 if (ref == NULL) {
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900258 // If we're not tracking this resource, just delete it
Romain Guye3b0a012013-06-26 15:45:41 -0700259 if (Caches::hasInstance()) {
260 Caches::getInstance().patchCache.removeDeferred(resource);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900261 } else {
262 // A Res_png_9patch is actually an array of byte that's larger
263 // than sizeof(Res_png_9patch). It must be freed as an array.
264 delete[] (int8_t*) resource;
Romain Guye3b0a012013-06-26 15:45:41 -0700265 }
Romain Guye3b0a012013-06-26 15:45:41 -0700266 return;
267 }
268 ref->destroyed = true;
269 if (ref->refCount == 0) {
270 deleteResourceReferenceLocked(resource, ref);
271 }
272}
273
Chet Haase547e6652012-10-22 15:07:26 -0700274/**
275 * Return value indicates whether resource was actually recycled, which happens when RefCnt
276 * reaches 0.
277 */
278bool ResourceCache::recycle(SkBitmap* resource) {
Romain Guy58ecc202012-09-07 11:58:36 -0700279 Mutex::Autolock _l(mLock);
Chet Haase547e6652012-10-22 15:07:26 -0700280 return recycleLocked(resource);
Romain Guy58ecc202012-09-07 11:58:36 -0700281}
282
Chet Haase547e6652012-10-22 15:07:26 -0700283/**
284 * Return value indicates whether resource was actually recycled, which happens when RefCnt
285 * reaches 0.
286 */
287bool ResourceCache::recycleLocked(SkBitmap* resource) {
Romain Guy58ecc202012-09-07 11:58:36 -0700288 ssize_t index = mCache->indexOfKey(resource);
289 if (index < 0) {
290 // not tracking this resource; just recycle the pixel data
291 resource->setPixels(NULL, NULL);
Chet Haase547e6652012-10-22 15:07:26 -0700292 return true;
Romain Guy58ecc202012-09-07 11:58:36 -0700293 }
294 ResourceReference* ref = mCache->valueAt(index);
295 if (ref == NULL) {
296 // 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 -0700297 return true;
Romain Guy58ecc202012-09-07 11:58:36 -0700298 }
299 ref->recycled = true;
300 if (ref->refCount == 0) {
Romain Guy97dc9172012-09-23 17:46:45 -0700301 deleteResourceReferenceLocked(resource, ref);
Chet Haase547e6652012-10-22 15:07:26 -0700302 return true;
Romain Guy58ecc202012-09-07 11:58:36 -0700303 }
Chet Haase547e6652012-10-22 15:07:26 -0700304 // Still referring to resource, don't recycle yet
305 return false;
Romain Guy58ecc202012-09-07 11:58:36 -0700306}
307
Chet Haasee7d22952010-11-11 16:30:16 -0800308/**
309 * This method should only be called while the mLock mutex is held (that mutex is grabbed
310 * by the various destructor() and recycle() methods which call this method).
311 */
Chris Craikd218a922014-01-02 17:13:34 -0800312void ResourceCache::deleteResourceReferenceLocked(const void* resource, ResourceReference* ref) {
Chet Haase5c13d892010-10-08 08:37:55 -0700313 if (ref->recycled && ref->resourceType == kBitmap) {
314 ((SkBitmap*) resource)->setPixels(NULL, NULL);
315 }
Chet Haase603f6de2012-09-14 15:31:25 -0700316 if (ref->destroyed || ref->resourceType == kLayer) {
Chet Haase5c13d892010-10-08 08:37:55 -0700317 switch (ref->resourceType) {
Romain Guyd586ad92011-06-22 16:14:36 -0700318 case kBitmap: {
319 SkBitmap* bitmap = (SkBitmap*) resource;
Chet Haase5c13d892010-10-08 08:37:55 -0700320 if (Caches::hasInstance()) {
Romain Guyfe48f652010-11-11 15:36:56 -0800321 Caches::getInstance().textureCache.removeDeferred(bitmap);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900322 } else {
323 delete bitmap;
Chet Haase5c13d892010-10-08 08:37:55 -0700324 }
Chet Haase5c13d892010-10-08 08:37:55 -0700325 }
326 break;
Romain Guyd586ad92011-06-22 16:14:36 -0700327 case kPath: {
328 SkPath* path = (SkPath*) resource;
Chet Haase5a7e8282011-02-04 12:50:55 -0800329 if (Caches::hasInstance()) {
330 Caches::getInstance().pathCache.removeDeferred(path);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900331 } else {
332 delete path;
Chet Haase5a7e8282011-02-04 12:50:55 -0800333 }
Chet Haase5a7e8282011-02-04 12:50:55 -0800334 }
335 break;
Romain Guyd586ad92011-06-22 16:14:36 -0700336 case kShader: {
337 SkiaShader* shader = (SkiaShader*) resource;
Chet Haase5c13d892010-10-08 08:37:55 -0700338 delete shader;
Chet Haasead93c2b2010-10-22 16:17:12 -0700339 }
340 break;
Romain Guye3b0a012013-06-26 15:45:41 -0700341 case kNinePatch: {
342 if (Caches::hasInstance()) {
343 Caches::getInstance().patchCache.removeDeferred((Res_png_9patch*) resource);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900344 } else {
345 // A Res_png_9patch is actually an array of byte that's larger
346 // than sizeof(Res_png_9patch). It must be freed as an array.
347 int8_t* patch = (int8_t*) resource;
348 delete[] patch;
Romain Guye3b0a012013-06-26 15:45:41 -0700349 }
Romain Guye3b0a012013-06-26 15:45:41 -0700350 }
351 break;
Chet Haase603f6de2012-09-14 15:31:25 -0700352 case kLayer: {
Dave Burke56257af2012-09-25 20:30:09 -0700353 Layer* layer = (Layer*) resource;
Mathias Agopian54643d72012-09-25 21:30:22 -0700354 Caches::getInstance().deleteLayerDeferred(layer);
Chet Haase603f6de2012-09-14 15:31:25 -0700355 }
356 break;
Chet Haase5c13d892010-10-08 08:37:55 -0700357 }
358 }
359 mCache->removeItem(resource);
360 delete ref;
361}
362
363}; // namespace uirenderer
364}; // namespace android