blob: 8b553d1383d410e8bac2d2665f059fdd79014b95 [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
Chet Haase603f6de2012-09-14 15:31:25 -070078void ResourceCache::incrementRefcount(Layer* layerResource) {
79 incrementRefcount((void*) layerResource, kLayer);
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) {
Chris Craik0c20c382013-07-02 10:48:54 -070093 bitmapResource->pixelRef()->globalRef();
Romain Guy58ecc202012-09-07 11:58:36 -070094 SkSafeRef(bitmapResource->getColorTable());
95 incrementRefcountLocked((void*) bitmapResource, kBitmap);
96}
97
Chris Craikd218a922014-01-02 17:13:34 -080098void ResourceCache::incrementRefcountLocked(const SkPath* pathResource) {
Romain Guy58ecc202012-09-07 11:58:36 -070099 incrementRefcountLocked((void*) pathResource, kPath);
100}
101
Chris Craikd218a922014-01-02 17:13:34 -0800102void ResourceCache::incrementRefcountLocked(const Res_png_9patch* patchResource) {
Romain Guye3b0a012013-06-26 15:45:41 -0700103 incrementRefcountLocked((void*) patchResource, kNinePatch);
104}
105
Chet Haase603f6de2012-09-14 15:31:25 -0700106void ResourceCache::incrementRefcountLocked(Layer* layerResource) {
107 incrementRefcountLocked((void*) layerResource, kLayer);
108}
109
Romain Guy58ecc202012-09-07 11:58:36 -0700110void ResourceCache::decrementRefcount(void* resource) {
111 Mutex::Autolock _l(mLock);
112 decrementRefcountLocked(resource);
Chet Haase5c13d892010-10-08 08:37:55 -0700113}
114
Chris Craikd218a922014-01-02 17:13:34 -0800115void ResourceCache::decrementRefcount(const SkBitmap* bitmapResource) {
Chris Craik0c20c382013-07-02 10:48:54 -0700116 bitmapResource->pixelRef()->globalUnref();
Derek Sollenberger6062c592011-02-22 13:55:04 -0500117 SkSafeUnref(bitmapResource->getColorTable());
Romain Guy43ccf462011-01-14 18:51:01 -0800118 decrementRefcount((void*) bitmapResource);
Chet Haase5c13d892010-10-08 08:37:55 -0700119}
120
Chris Craikd218a922014-01-02 17:13:34 -0800121void ResourceCache::decrementRefcount(const SkPath* pathResource) {
Chet Haase5a7e8282011-02-04 12:50:55 -0800122 decrementRefcount((void*) pathResource);
123}
124
Chris Craikd218a922014-01-02 17:13:34 -0800125void ResourceCache::decrementRefcount(const Res_png_9patch* patchResource) {
Romain Guye3b0a012013-06-26 15:45:41 -0700126 decrementRefcount((void*) patchResource);
127}
128
Chet Haase603f6de2012-09-14 15:31:25 -0700129void ResourceCache::decrementRefcount(Layer* layerResource) {
130 decrementRefcount((void*) layerResource);
131}
132
Romain Guy58ecc202012-09-07 11:58:36 -0700133void ResourceCache::decrementRefcountLocked(void* resource) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700134 ssize_t index = mCache->indexOfKey(resource);
Romain Guy58ecc202012-09-07 11:58:36 -0700135 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
Chet Haase5c13d892010-10-08 08:37:55 -0700136 if (ref == NULL) {
Romain Guy58ecc202012-09-07 11:58:36 -0700137 // 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 -0700138 return;
139 }
Romain Guy58ecc202012-09-07 11:58:36 -0700140 ref->refCount--;
Chet Haase5c13d892010-10-08 08:37:55 -0700141 if (ref->refCount == 0) {
Romain Guy97dc9172012-09-23 17:46:45 -0700142 deleteResourceReferenceLocked(resource, ref);
Chet Haase5c13d892010-10-08 08:37:55 -0700143 }
144}
145
Chris Craikd218a922014-01-02 17:13:34 -0800146void ResourceCache::decrementRefcountLocked(const SkBitmap* bitmapResource) {
Chris Craik0c20c382013-07-02 10:48:54 -0700147 bitmapResource->pixelRef()->globalUnref();
Romain Guy58ecc202012-09-07 11:58:36 -0700148 SkSafeUnref(bitmapResource->getColorTable());
149 decrementRefcountLocked((void*) bitmapResource);
150}
151
Chris Craikd218a922014-01-02 17:13:34 -0800152void ResourceCache::decrementRefcountLocked(const SkPath* pathResource) {
Romain Guy58ecc202012-09-07 11:58:36 -0700153 decrementRefcountLocked((void*) pathResource);
154}
155
Chris Craikd218a922014-01-02 17:13:34 -0800156void ResourceCache::decrementRefcountLocked(const Res_png_9patch* patchResource) {
Romain Guye3b0a012013-06-26 15:45:41 -0700157 decrementRefcountLocked((void*) patchResource);
158}
159
Chet Haase603f6de2012-09-14 15:31:25 -0700160void ResourceCache::decrementRefcountLocked(Layer* layerResource) {
161 decrementRefcountLocked((void*) layerResource);
162}
163
Chet Haase5a7e8282011-02-04 12:50:55 -0800164void ResourceCache::destructor(SkPath* resource) {
165 Mutex::Autolock _l(mLock);
Romain Guy58ecc202012-09-07 11:58:36 -0700166 destructorLocked(resource);
167}
168
169void ResourceCache::destructorLocked(SkPath* resource) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700170 ssize_t index = mCache->indexOfKey(resource);
171 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
Chet Haase5a7e8282011-02-04 12:50:55 -0800172 if (ref == NULL) {
173 // If we're not tracking this resource, just delete it
174 if (Caches::hasInstance()) {
175 Caches::getInstance().pathCache.removeDeferred(resource);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900176 } else {
177 delete resource;
Chet Haase5a7e8282011-02-04 12:50:55 -0800178 }
Chet Haase5a7e8282011-02-04 12:50:55 -0800179 return;
180 }
181 ref->destroyed = true;
182 if (ref->refCount == 0) {
Romain Guy97dc9172012-09-23 17:46:45 -0700183 deleteResourceReferenceLocked(resource, ref);
Chet Haase5a7e8282011-02-04 12:50:55 -0800184 }
185}
186
Chris Craikd218a922014-01-02 17:13:34 -0800187void ResourceCache::destructor(const SkBitmap* resource) {
Chet Haasee7d22952010-11-11 16:30:16 -0800188 Mutex::Autolock _l(mLock);
Romain Guy58ecc202012-09-07 11:58:36 -0700189 destructorLocked(resource);
190}
191
Chris Craikd218a922014-01-02 17:13:34 -0800192void ResourceCache::destructorLocked(const SkBitmap* resource) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700193 ssize_t index = mCache->indexOfKey(resource);
194 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
Chet Haase5c13d892010-10-08 08:37:55 -0700195 if (ref == NULL) {
196 // If we're not tracking this resource, just delete it
197 if (Caches::hasInstance()) {
Romain Guyfe48f652010-11-11 15:36:56 -0800198 Caches::getInstance().textureCache.removeDeferred(resource);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900199 } else {
200 delete resource;
Chet Haase5c13d892010-10-08 08:37:55 -0700201 }
Chet Haase5c13d892010-10-08 08:37:55 -0700202 return;
203 }
204 ref->destroyed = true;
205 if (ref->refCount == 0) {
Romain Guy97dc9172012-09-23 17:46:45 -0700206 deleteResourceReferenceLocked(resource, ref);
Chet Haase5c13d892010-10-08 08:37:55 -0700207 }
208}
209
Romain Guye3b0a012013-06-26 15:45:41 -0700210void ResourceCache::destructor(Res_png_9patch* resource) {
211 Mutex::Autolock _l(mLock);
212 destructorLocked(resource);
213}
214
215void ResourceCache::destructorLocked(Res_png_9patch* resource) {
216 ssize_t index = mCache->indexOfKey(resource);
217 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL;
218 if (ref == NULL) {
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900219 // If we're not tracking this resource, just delete it
Romain Guye3b0a012013-06-26 15:45:41 -0700220 if (Caches::hasInstance()) {
221 Caches::getInstance().patchCache.removeDeferred(resource);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900222 } else {
223 // A Res_png_9patch is actually an array of byte that's larger
224 // than sizeof(Res_png_9patch). It must be freed as an array.
225 delete[] (int8_t*) resource;
Romain Guye3b0a012013-06-26 15:45:41 -0700226 }
Romain Guye3b0a012013-06-26 15:45:41 -0700227 return;
228 }
229 ref->destroyed = true;
230 if (ref->refCount == 0) {
231 deleteResourceReferenceLocked(resource, ref);
232 }
233}
234
Chet Haase547e6652012-10-22 15:07:26 -0700235/**
236 * Return value indicates whether resource was actually recycled, which happens when RefCnt
237 * reaches 0.
238 */
239bool ResourceCache::recycle(SkBitmap* resource) {
Romain Guy58ecc202012-09-07 11:58:36 -0700240 Mutex::Autolock _l(mLock);
Chet Haase547e6652012-10-22 15:07:26 -0700241 return recycleLocked(resource);
Romain Guy58ecc202012-09-07 11:58:36 -0700242}
243
Chet Haase547e6652012-10-22 15:07:26 -0700244/**
245 * Return value indicates whether resource was actually recycled, which happens when RefCnt
246 * reaches 0.
247 */
248bool ResourceCache::recycleLocked(SkBitmap* resource) {
Romain Guy58ecc202012-09-07 11:58:36 -0700249 ssize_t index = mCache->indexOfKey(resource);
250 if (index < 0) {
251 // not tracking this resource; just recycle the pixel data
252 resource->setPixels(NULL, NULL);
Chet Haase547e6652012-10-22 15:07:26 -0700253 return true;
Romain Guy58ecc202012-09-07 11:58:36 -0700254 }
255 ResourceReference* ref = mCache->valueAt(index);
256 if (ref == NULL) {
257 // 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 -0700258 return true;
Romain Guy58ecc202012-09-07 11:58:36 -0700259 }
260 ref->recycled = true;
261 if (ref->refCount == 0) {
Romain Guy97dc9172012-09-23 17:46:45 -0700262 deleteResourceReferenceLocked(resource, ref);
Chet Haase547e6652012-10-22 15:07:26 -0700263 return true;
Romain Guy58ecc202012-09-07 11:58:36 -0700264 }
Chet Haase547e6652012-10-22 15:07:26 -0700265 // Still referring to resource, don't recycle yet
266 return false;
Romain Guy58ecc202012-09-07 11:58:36 -0700267}
268
Chet Haasee7d22952010-11-11 16:30:16 -0800269/**
270 * This method should only be called while the mLock mutex is held (that mutex is grabbed
271 * by the various destructor() and recycle() methods which call this method).
272 */
Chris Craikd218a922014-01-02 17:13:34 -0800273void ResourceCache::deleteResourceReferenceLocked(const void* resource, ResourceReference* ref) {
Chet Haase5c13d892010-10-08 08:37:55 -0700274 if (ref->recycled && ref->resourceType == kBitmap) {
275 ((SkBitmap*) resource)->setPixels(NULL, NULL);
276 }
Chet Haase603f6de2012-09-14 15:31:25 -0700277 if (ref->destroyed || ref->resourceType == kLayer) {
Chet Haase5c13d892010-10-08 08:37:55 -0700278 switch (ref->resourceType) {
Romain Guyd586ad92011-06-22 16:14:36 -0700279 case kBitmap: {
280 SkBitmap* bitmap = (SkBitmap*) resource;
Chet Haase5c13d892010-10-08 08:37:55 -0700281 if (Caches::hasInstance()) {
Romain Guyfe48f652010-11-11 15:36:56 -0800282 Caches::getInstance().textureCache.removeDeferred(bitmap);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900283 } else {
284 delete bitmap;
Chet Haase5c13d892010-10-08 08:37:55 -0700285 }
Chet Haase5c13d892010-10-08 08:37:55 -0700286 }
287 break;
Romain Guyd586ad92011-06-22 16:14:36 -0700288 case kPath: {
289 SkPath* path = (SkPath*) resource;
Chet Haase5a7e8282011-02-04 12:50:55 -0800290 if (Caches::hasInstance()) {
291 Caches::getInstance().pathCache.removeDeferred(path);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900292 } else {
293 delete path;
Chet Haase5a7e8282011-02-04 12:50:55 -0800294 }
Chet Haase5a7e8282011-02-04 12:50:55 -0800295 }
296 break;
Romain Guye3b0a012013-06-26 15:45:41 -0700297 case kNinePatch: {
298 if (Caches::hasInstance()) {
299 Caches::getInstance().patchCache.removeDeferred((Res_png_9patch*) resource);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900300 } else {
301 // A Res_png_9patch is actually an array of byte that's larger
302 // than sizeof(Res_png_9patch). It must be freed as an array.
303 int8_t* patch = (int8_t*) resource;
304 delete[] patch;
Romain Guye3b0a012013-06-26 15:45:41 -0700305 }
Romain Guye3b0a012013-06-26 15:45:41 -0700306 }
307 break;
Chet Haase603f6de2012-09-14 15:31:25 -0700308 case kLayer: {
Dave Burke56257af2012-09-25 20:30:09 -0700309 Layer* layer = (Layer*) resource;
Mathias Agopian54643d72012-09-25 21:30:22 -0700310 Caches::getInstance().deleteLayerDeferred(layer);
Chet Haase603f6de2012-09-14 15:31:25 -0700311 }
312 break;
Chet Haase5c13d892010-10-08 08:37:55 -0700313 }
314 }
315 mCache->removeItem(resource);
316 delete ref;
317}
318
319}; // namespace uirenderer
320}; // namespace android