blob: 457cfa9cfc9f43e73260f3c4b0adcbbb54c14b11 [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);
Steve Block5baa3a62011-12-20 16:23:08 +000034 ALOGD(" ResourceCache: mCache(%d): resource, ref = 0x%p, 0x%p",
Chet Haase5c13d892010-10-08 08:37:55 -070035 i, mCache->keyAt(i), mCache->valueAt(i));
Steve Block5baa3a62011-12-20 16:23:08 +000036 ALOGD(" ResourceCache: mCache(%d): 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);
196 }
197 delete resource;
198 return;
199 }
200 ref->destroyed = true;
201 if (ref->refCount == 0) {
Romain Guy97dc9172012-09-23 17:46:45 -0700202 deleteResourceReferenceLocked(resource, ref);
Chet Haase5a7e8282011-02-04 12:50:55 -0800203 }
204}
205
Chris Craikd218a922014-01-02 17:13:34 -0800206void ResourceCache::destructor(const SkBitmap* resource) {
Chet Haasee7d22952010-11-11 16:30:16 -0800207 Mutex::Autolock _l(mLock);
Romain Guy58ecc202012-09-07 11:58:36 -0700208 destructorLocked(resource);
209}
210
Chris Craikd218a922014-01-02 17:13:34 -0800211void ResourceCache::destructorLocked(const SkBitmap* resource) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700212 ssize_t index = mCache->indexOfKey(resource);
213 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
Chet Haase5c13d892010-10-08 08:37:55 -0700214 if (ref == NULL) {
215 // If we're not tracking this resource, just delete it
216 if (Caches::hasInstance()) {
Romain Guyfe48f652010-11-11 15:36:56 -0800217 Caches::getInstance().textureCache.removeDeferred(resource);
Chet Haase5c13d892010-10-08 08:37:55 -0700218 }
219 delete resource;
220 return;
221 }
222 ref->destroyed = true;
223 if (ref->refCount == 0) {
Romain Guy97dc9172012-09-23 17:46:45 -0700224 deleteResourceReferenceLocked(resource, ref);
Chet Haase5c13d892010-10-08 08:37:55 -0700225 }
226}
227
Chet Haase5c13d892010-10-08 08:37:55 -0700228void ResourceCache::destructor(SkiaShader* resource) {
Chet Haasee7d22952010-11-11 16:30:16 -0800229 Mutex::Autolock _l(mLock);
Romain Guy58ecc202012-09-07 11:58:36 -0700230 destructorLocked(resource);
231}
232
233void ResourceCache::destructorLocked(SkiaShader* resource) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700234 ssize_t index = mCache->indexOfKey(resource);
235 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
Chet Haase5c13d892010-10-08 08:37:55 -0700236 if (ref == NULL) {
237 // If we're not tracking this resource, just delete it
Chet Haase5c13d892010-10-08 08:37:55 -0700238 delete resource;
239 return;
240 }
241 ref->destroyed = true;
242 if (ref->refCount == 0) {
Romain Guy97dc9172012-09-23 17:46:45 -0700243 deleteResourceReferenceLocked(resource, ref);
Chet Haase5c13d892010-10-08 08:37:55 -0700244 }
245}
246
Romain Guye3b0a012013-06-26 15:45:41 -0700247void ResourceCache::destructor(Res_png_9patch* resource) {
248 Mutex::Autolock _l(mLock);
249 destructorLocked(resource);
250}
251
252void ResourceCache::destructorLocked(Res_png_9patch* resource) {
253 ssize_t index = mCache->indexOfKey(resource);
254 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
255 if (ref == NULL) {
256 if (Caches::hasInstance()) {
257 Caches::getInstance().patchCache.removeDeferred(resource);
258 }
259 // If we're not tracking this resource, just delete it
260 // A Res_png_9patch is actually an array of byte that's larger
261 // than sizeof(Res_png_9patch). It must be freed as an array.
262 delete[] (int8_t*) resource;
263 return;
264 }
265 ref->destroyed = true;
266 if (ref->refCount == 0) {
267 deleteResourceReferenceLocked(resource, ref);
268 }
269}
270
Chet Haase547e6652012-10-22 15:07:26 -0700271/**
272 * Return value indicates whether resource was actually recycled, which happens when RefCnt
273 * reaches 0.
274 */
275bool ResourceCache::recycle(SkBitmap* resource) {
Romain Guy58ecc202012-09-07 11:58:36 -0700276 Mutex::Autolock _l(mLock);
Chet Haase547e6652012-10-22 15:07:26 -0700277 return recycleLocked(resource);
Romain Guy58ecc202012-09-07 11:58:36 -0700278}
279
Chet Haase547e6652012-10-22 15:07:26 -0700280/**
281 * Return value indicates whether resource was actually recycled, which happens when RefCnt
282 * reaches 0.
283 */
284bool ResourceCache::recycleLocked(SkBitmap* resource) {
Romain Guy58ecc202012-09-07 11:58:36 -0700285 ssize_t index = mCache->indexOfKey(resource);
286 if (index < 0) {
287 // not tracking this resource; just recycle the pixel data
288 resource->setPixels(NULL, NULL);
Chet Haase547e6652012-10-22 15:07:26 -0700289 return true;
Romain Guy58ecc202012-09-07 11:58:36 -0700290 }
291 ResourceReference* ref = mCache->valueAt(index);
292 if (ref == NULL) {
293 // 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 -0700294 return true;
Romain Guy58ecc202012-09-07 11:58:36 -0700295 }
296 ref->recycled = true;
297 if (ref->refCount == 0) {
Romain Guy97dc9172012-09-23 17:46:45 -0700298 deleteResourceReferenceLocked(resource, ref);
Chet Haase547e6652012-10-22 15:07:26 -0700299 return true;
Romain Guy58ecc202012-09-07 11:58:36 -0700300 }
Chet Haase547e6652012-10-22 15:07:26 -0700301 // Still referring to resource, don't recycle yet
302 return false;
Romain Guy58ecc202012-09-07 11:58:36 -0700303}
304
Chet Haasee7d22952010-11-11 16:30:16 -0800305/**
306 * This method should only be called while the mLock mutex is held (that mutex is grabbed
307 * by the various destructor() and recycle() methods which call this method).
308 */
Chris Craikd218a922014-01-02 17:13:34 -0800309void ResourceCache::deleteResourceReferenceLocked(const void* resource, ResourceReference* ref) {
Chet Haase5c13d892010-10-08 08:37:55 -0700310 if (ref->recycled && ref->resourceType == kBitmap) {
311 ((SkBitmap*) resource)->setPixels(NULL, NULL);
312 }
Chet Haase603f6de2012-09-14 15:31:25 -0700313 if (ref->destroyed || ref->resourceType == kLayer) {
Chet Haase5c13d892010-10-08 08:37:55 -0700314 switch (ref->resourceType) {
Romain Guyd586ad92011-06-22 16:14:36 -0700315 case kBitmap: {
316 SkBitmap* bitmap = (SkBitmap*) resource;
Chet Haase5c13d892010-10-08 08:37:55 -0700317 if (Caches::hasInstance()) {
Romain Guyfe48f652010-11-11 15:36:56 -0800318 Caches::getInstance().textureCache.removeDeferred(bitmap);
Chet Haase5c13d892010-10-08 08:37:55 -0700319 }
320 delete bitmap;
321 }
322 break;
Romain Guyd586ad92011-06-22 16:14:36 -0700323 case kPath: {
324 SkPath* path = (SkPath*) resource;
Chet Haase5a7e8282011-02-04 12:50:55 -0800325 if (Caches::hasInstance()) {
326 Caches::getInstance().pathCache.removeDeferred(path);
327 }
328 delete path;
329 }
330 break;
Romain Guyd586ad92011-06-22 16:14:36 -0700331 case kShader: {
332 SkiaShader* shader = (SkiaShader*) resource;
Chet Haase5c13d892010-10-08 08:37:55 -0700333 delete shader;
Chet Haasead93c2b2010-10-22 16:17:12 -0700334 }
335 break;
Romain Guye3b0a012013-06-26 15:45:41 -0700336 case kNinePatch: {
337 if (Caches::hasInstance()) {
338 Caches::getInstance().patchCache.removeDeferred((Res_png_9patch*) resource);
339 }
340 // A Res_png_9patch is actually an array of byte that's larger
341 // than sizeof(Res_png_9patch). It must be freed as an array.
342 int8_t* patch = (int8_t*) resource;
343 delete[] patch;
344 }
345 break;
Chet Haase603f6de2012-09-14 15:31:25 -0700346 case kLayer: {
Dave Burke56257af2012-09-25 20:30:09 -0700347 Layer* layer = (Layer*) resource;
Mathias Agopian54643d72012-09-25 21:30:22 -0700348 Caches::getInstance().deleteLayerDeferred(layer);
Chet Haase603f6de2012-09-14 15:31:25 -0700349 }
350 break;
Chet Haase5c13d892010-10-08 08:37:55 -0700351 }
352 }
353 mCache->removeItem(resource);
354 delete ref;
355}
356
357}; // namespace uirenderer
358}; // namespace android