blob: 70d117a3622bbfd535e0f409fa56f309f62f1071 [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
17#include <SkPixelRef.h>
18#include "ResourceCache.h"
19#include "Caches.h"
20
21namespace android {
22namespace uirenderer {
23
24///////////////////////////////////////////////////////////////////////////////
25// Resource cache
26///////////////////////////////////////////////////////////////////////////////
27
28void ResourceCache::logCache() {
29 LOGD("ResourceCache: cacheReport:");
30 for (size_t i = 0; i < mCache->size(); ++i) {
31 ResourceReference* ref = mCache->valueAt(i);
32 LOGD(" ResourceCache: mCache(%d): resource, ref = 0x%p, 0x%p",
33 i, mCache->keyAt(i), mCache->valueAt(i));
34 LOGD(" ResourceCache: mCache(%d): refCount, recycled, destroyed, type = %d, %d, %d, %d",
35 i, ref->refCount, ref->recycled, ref->destroyed, ref->resourceType);
36 }
37}
38
39ResourceCache::ResourceCache() {
Chet Haasee7d22952010-11-11 16:30:16 -080040 Mutex::Autolock _l(mLock);
Chet Haase5c13d892010-10-08 08:37:55 -070041 mCache = new KeyedVector<void *, ResourceReference *>();
42}
43
44ResourceCache::~ResourceCache() {
Chet Haasee7d22952010-11-11 16:30:16 -080045 Mutex::Autolock _l(mLock);
Chet Haase5c13d892010-10-08 08:37:55 -070046 delete mCache;
47}
48
49void ResourceCache::incrementRefcount(void* resource, ResourceType resourceType) {
Chet Haasee7d22952010-11-11 16:30:16 -080050 Mutex::Autolock _l(mLock);
Chet Haase5c13d892010-10-08 08:37:55 -070051 for (size_t i = 0; i < mCache->size(); ++i) {
52 void* ref = mCache->valueAt(i);
53 }
54 ResourceReference* ref = mCache->indexOfKey(resource) >= 0 ? mCache->valueFor(resource) : NULL;
55 if (ref == NULL || mCache->size() == 0) {
56 ref = new ResourceReference(resourceType);
57 mCache->add(resource, ref);
58 }
59 ref->refCount++;
60}
61
62void ResourceCache::incrementRefcount(SkBitmap* bitmapResource) {
Patrick Dubroyf890fab2010-12-19 16:47:17 -080063 bitmapResource->pixelRef()->safeRef();
Chet Haase5c13d892010-10-08 08:37:55 -070064 bitmapResource->getColorTable()->safeRef();
65 incrementRefcount((void*)bitmapResource, kBitmap);
66}
67
Chet Haase5c13d892010-10-08 08:37:55 -070068void ResourceCache::incrementRefcount(SkiaShader* shaderResource) {
69 shaderResource->getSkShader()->safeRef();
Romain Guy43ccf462011-01-14 18:51:01 -080070 incrementRefcount((void*) shaderResource, kShader);
Chet Haase5c13d892010-10-08 08:37:55 -070071}
72
Chet Haasead93c2b2010-10-22 16:17:12 -070073void ResourceCache::incrementRefcount(SkiaColorFilter* filterResource) {
74 filterResource->getSkColorFilter()->safeRef();
Romain Guy43ccf462011-01-14 18:51:01 -080075 incrementRefcount((void*) filterResource, kColorFilter);
Chet Haasead93c2b2010-10-22 16:17:12 -070076}
77
Chet Haase5c13d892010-10-08 08:37:55 -070078void ResourceCache::decrementRefcount(void* resource) {
Chet Haasee7d22952010-11-11 16:30:16 -080079 Mutex::Autolock _l(mLock);
Chet Haase5c13d892010-10-08 08:37:55 -070080 ResourceReference* ref = mCache->indexOfKey(resource) >= 0 ? mCache->valueFor(resource) : NULL;
81 if (ref == NULL) {
82 // Should not get here - shouldn't get a call to decrement if we're not yet tracking it
83 return;
84 }
85 ref->refCount--;
86 if (ref->refCount == 0) {
87 deleteResourceReference(resource, ref);
88 }
89}
90
91void ResourceCache::decrementRefcount(SkBitmap* bitmapResource) {
Patrick Dubroyf890fab2010-12-19 16:47:17 -080092 bitmapResource->pixelRef()->safeUnref();
Chet Haase5c13d892010-10-08 08:37:55 -070093 bitmapResource->getColorTable()->safeUnref();
Romain Guy43ccf462011-01-14 18:51:01 -080094 decrementRefcount((void*) bitmapResource);
Chet Haase5c13d892010-10-08 08:37:55 -070095}
96
97void ResourceCache::decrementRefcount(SkiaShader* shaderResource) {
98 shaderResource->getSkShader()->safeUnref();
Romain Guy43ccf462011-01-14 18:51:01 -080099 decrementRefcount((void*) shaderResource);
Chet Haase5c13d892010-10-08 08:37:55 -0700100}
101
Chet Haasead93c2b2010-10-22 16:17:12 -0700102void ResourceCache::decrementRefcount(SkiaColorFilter* filterResource) {
103 filterResource->getSkColorFilter()->safeUnref();
Romain Guy43ccf462011-01-14 18:51:01 -0800104 decrementRefcount((void*) filterResource);
Chet Haasead93c2b2010-10-22 16:17:12 -0700105}
106
Chet Haase5c13d892010-10-08 08:37:55 -0700107void ResourceCache::recycle(SkBitmap* resource) {
Chet Haasee7d22952010-11-11 16:30:16 -0800108 Mutex::Autolock _l(mLock);
Chet Haase5c13d892010-10-08 08:37:55 -0700109 if (mCache->indexOfKey(resource) < 0) {
110 // not tracking this resource; just recycle the pixel data
111 resource->setPixels(NULL, NULL);
112 return;
113 }
Chet Haase5c13d892010-10-08 08:37:55 -0700114 ResourceReference* ref = mCache->indexOfKey(resource) >= 0 ? mCache->valueFor(resource) : NULL;
115 if (ref == NULL) {
116 // Should not get here - shouldn't get a call to recycle if we're not yet tracking it
117 return;
118 }
119 ref->recycled = true;
120 if (ref->refCount == 0) {
121 deleteResourceReference(resource, ref);
122 }
123}
124
125void ResourceCache::destructor(SkBitmap* resource) {
Chet Haasee7d22952010-11-11 16:30:16 -0800126 Mutex::Autolock _l(mLock);
Chet Haase5c13d892010-10-08 08:37:55 -0700127 ResourceReference* ref = mCache->indexOfKey(resource) >= 0 ? mCache->valueFor(resource) : NULL;
128 if (ref == NULL) {
129 // If we're not tracking this resource, just delete it
130 if (Caches::hasInstance()) {
Romain Guyfe48f652010-11-11 15:36:56 -0800131 Caches::getInstance().textureCache.removeDeferred(resource);
Chet Haase5c13d892010-10-08 08:37:55 -0700132 }
133 delete resource;
134 return;
135 }
136 ref->destroyed = true;
137 if (ref->refCount == 0) {
138 deleteResourceReference(resource, ref);
139 return;
140 }
141}
142
Chet Haase5c13d892010-10-08 08:37:55 -0700143void ResourceCache::destructor(SkiaShader* resource) {
Chet Haasee7d22952010-11-11 16:30:16 -0800144 Mutex::Autolock _l(mLock);
Chet Haase5c13d892010-10-08 08:37:55 -0700145 ResourceReference* ref = mCache->indexOfKey(resource) >= 0 ? mCache->valueFor(resource) : NULL;
146 if (ref == NULL) {
147 // If we're not tracking this resource, just delete it
148 if (Caches::hasInstance()) {
Romain Guyfe48f652010-11-11 15:36:56 -0800149 Caches::getInstance().gradientCache.removeDeferred(resource->getSkShader());
Chet Haase5c13d892010-10-08 08:37:55 -0700150 }
151 delete resource;
152 return;
153 }
154 ref->destroyed = true;
155 if (ref->refCount == 0) {
156 deleteResourceReference(resource, ref);
157 return;
158 }
159}
160
Chet Haasead93c2b2010-10-22 16:17:12 -0700161void ResourceCache::destructor(SkiaColorFilter* resource) {
Chet Haasee7d22952010-11-11 16:30:16 -0800162 Mutex::Autolock _l(mLock);
Chet Haasead93c2b2010-10-22 16:17:12 -0700163 ResourceReference* ref = mCache->indexOfKey(resource) >= 0 ? mCache->valueFor(resource) : NULL;
164 if (ref == NULL) {
165 // If we're not tracking this resource, just delete it
166 delete resource;
167 return;
168 }
169 ref->destroyed = true;
170 if (ref->refCount == 0) {
171 deleteResourceReference(resource, ref);
172 return;
173 }
174}
175
Chet Haasee7d22952010-11-11 16:30:16 -0800176/**
177 * This method should only be called while the mLock mutex is held (that mutex is grabbed
178 * by the various destructor() and recycle() methods which call this method).
179 */
Chet Haase5c13d892010-10-08 08:37:55 -0700180void ResourceCache::deleteResourceReference(void* resource, ResourceReference* ref) {
181 if (ref->recycled && ref->resourceType == kBitmap) {
182 ((SkBitmap*) resource)->setPixels(NULL, NULL);
183 }
184 if (ref->destroyed) {
185 switch (ref->resourceType) {
186 case kBitmap:
187 {
188 SkBitmap* bitmap = (SkBitmap*)resource;
189 if (Caches::hasInstance()) {
Romain Guyfe48f652010-11-11 15:36:56 -0800190 Caches::getInstance().textureCache.removeDeferred(bitmap);
Chet Haase5c13d892010-10-08 08:37:55 -0700191 }
192 delete bitmap;
193 }
194 break;
Chet Haase5c13d892010-10-08 08:37:55 -0700195 case kShader:
Chet Haasead93c2b2010-10-22 16:17:12 -0700196 {
Chet Haase5c13d892010-10-08 08:37:55 -0700197 SkiaShader* shader = (SkiaShader*)resource;
198 if (Caches::hasInstance()) {
Romain Guyfe48f652010-11-11 15:36:56 -0800199 Caches::getInstance().gradientCache.removeDeferred(shader->getSkShader());
Chet Haase5c13d892010-10-08 08:37:55 -0700200 }
201 delete shader;
Chet Haasead93c2b2010-10-22 16:17:12 -0700202 }
203 break;
204 case kColorFilter:
205 {
206 SkiaColorFilter* filter = (SkiaColorFilter*)resource;
207 delete filter;
208 }
209 break;
Chet Haase5c13d892010-10-08 08:37:55 -0700210 }
211 }
212 mCache->removeItem(resource);
213 delete ref;
214}
215
216}; // namespace uirenderer
217}; // namespace android