blob: 2d1adc51679aca69aab155dcb47eb0ac25292f12 [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 "ResourceCache.h"
20#include "Caches.h"
21
22namespace android {
John Recka35778c72014-11-06 09:45:10 -080023
John Recka35778c72014-11-06 09:45:10 -080024using namespace uirenderer;
25ANDROID_SINGLETON_STATIC_INSTANCE(ResourceCache);
John Recka35778c72014-11-06 09:45:10 -080026
Chet Haase5c13d892010-10-08 08:37:55 -070027namespace uirenderer {
28
29///////////////////////////////////////////////////////////////////////////////
30// Resource cache
31///////////////////////////////////////////////////////////////////////////////
32
33void ResourceCache::logCache() {
Steve Block5baa3a62011-12-20 16:23:08 +000034 ALOGD("ResourceCache: cacheReport:");
Chet Haase5c13d892010-10-08 08:37:55 -070035 for (size_t i = 0; i < mCache->size(); ++i) {
36 ResourceReference* ref = mCache->valueAt(i);
Ashok Bhatf5df7002014-03-25 20:51:35 +000037 ALOGD(" ResourceCache: mCache(%zu): resource, ref = 0x%p, 0x%p",
Chet Haase5c13d892010-10-08 08:37:55 -070038 i, mCache->keyAt(i), mCache->valueAt(i));
Derek Sollenberger3d4eed72014-12-04 15:20:29 -050039 ALOGD(" ResourceCache: mCache(%zu): refCount, destroyed, type = %d, %d, %d",
40 i, ref->refCount, ref->destroyed, ref->resourceType);
Chet Haase5c13d892010-10-08 08:37:55 -070041 }
42}
43
44ResourceCache::ResourceCache() {
Chet Haasee7d22952010-11-11 16:30:16 -080045 Mutex::Autolock _l(mLock);
Chris Craikd218a922014-01-02 17:13:34 -080046 mCache = new KeyedVector<const void*, ResourceReference*>();
Chet Haase5c13d892010-10-08 08:37:55 -070047}
48
49ResourceCache::~ResourceCache() {
Chet Haasee7d22952010-11-11 16:30:16 -080050 Mutex::Autolock _l(mLock);
Chet Haase5c13d892010-10-08 08:37:55 -070051 delete mCache;
52}
53
Romain Guy58ecc202012-09-07 11:58:36 -070054void ResourceCache::lock() {
55 mLock.lock();
56}
57
58void ResourceCache::unlock() {
59 mLock.unlock();
60}
61
Derek Sollenberger3d4eed72014-12-04 15:20:29 -050062const SkBitmap* ResourceCache::insert(const SkBitmap* bitmapResource) {
63 Mutex::Autolock _l(mLock);
64
65 BitmapKey bitmapKey(bitmapResource);
66 ssize_t index = mBitmapCache.indexOfKey(bitmapKey);
67 if (index == NAME_NOT_FOUND) {
68 SkBitmap* cachedBitmap = new SkBitmap(*bitmapResource);
69 index = mBitmapCache.add(bitmapKey, cachedBitmap);
70 return cachedBitmap;
71 }
72
73 mBitmapCache.keyAt(index).mRefCount++;
74 return mBitmapCache.valueAt(index);
75}
76
Chet Haase5c13d892010-10-08 08:37:55 -070077void ResourceCache::incrementRefcount(void* resource, ResourceType resourceType) {
Chet Haasee7d22952010-11-11 16:30:16 -080078 Mutex::Autolock _l(mLock);
Romain Guy58ecc202012-09-07 11:58:36 -070079 incrementRefcountLocked(resource, resourceType);
Chet Haase5c13d892010-10-08 08:37:55 -070080}
81
Chris Craikd218a922014-01-02 17:13:34 -080082void ResourceCache::incrementRefcount(const SkPath* pathResource) {
Romain Guy49c5fc02012-05-15 11:10:01 -070083 incrementRefcount((void*) pathResource, kPath);
Chet Haase5a7e8282011-02-04 12:50:55 -080084}
85
Chris Craikd218a922014-01-02 17:13:34 -080086void ResourceCache::incrementRefcount(const Res_png_9patch* patchResource) {
Romain Guye3b0a012013-06-26 15:45:41 -070087 incrementRefcount((void*) patchResource, kNinePatch);
88}
89
Romain Guy58ecc202012-09-07 11:58:36 -070090void ResourceCache::incrementRefcountLocked(void* resource, ResourceType resourceType) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -070091 ssize_t index = mCache->indexOfKey(resource);
Chris Craikd41c4d82015-01-05 15:51:13 -080092 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : nullptr;
93 if (ref == nullptr || mCache->size() == 0) {
Romain Guy58ecc202012-09-07 11:58:36 -070094 ref = new ResourceReference(resourceType);
95 mCache->add(resource, ref);
Chet Haase5c13d892010-10-08 08:37:55 -070096 }
Romain Guy58ecc202012-09-07 11:58:36 -070097 ref->refCount++;
98}
99
Romain Guy58ecc202012-09-07 11:58:36 -0700100void ResourceCache::decrementRefcount(void* resource) {
101 Mutex::Autolock _l(mLock);
102 decrementRefcountLocked(resource);
Chet Haase5c13d892010-10-08 08:37:55 -0700103}
104
Chris Craikd218a922014-01-02 17:13:34 -0800105void ResourceCache::decrementRefcount(const SkBitmap* bitmapResource) {
Derek Sollenberger3d4eed72014-12-04 15:20:29 -0500106 Mutex::Autolock _l(mLock);
107 decrementRefcountLocked(bitmapResource);
Chet Haase5c13d892010-10-08 08:37:55 -0700108}
109
Chris Craikd218a922014-01-02 17:13:34 -0800110void ResourceCache::decrementRefcount(const SkPath* pathResource) {
Chet Haase5a7e8282011-02-04 12:50:55 -0800111 decrementRefcount((void*) pathResource);
112}
113
Chris Craikd218a922014-01-02 17:13:34 -0800114void ResourceCache::decrementRefcount(const Res_png_9patch* patchResource) {
Romain Guye3b0a012013-06-26 15:45:41 -0700115 decrementRefcount((void*) patchResource);
116}
117
Romain Guy58ecc202012-09-07 11:58:36 -0700118void ResourceCache::decrementRefcountLocked(void* resource) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700119 ssize_t index = mCache->indexOfKey(resource);
Chris Craikd41c4d82015-01-05 15:51:13 -0800120 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : nullptr;
121 if (ref == nullptr) {
Romain Guy58ecc202012-09-07 11:58:36 -0700122 // 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 -0700123 return;
124 }
Romain Guy58ecc202012-09-07 11:58:36 -0700125 ref->refCount--;
Chet Haase5c13d892010-10-08 08:37:55 -0700126 if (ref->refCount == 0) {
Romain Guy97dc9172012-09-23 17:46:45 -0700127 deleteResourceReferenceLocked(resource, ref);
Chet Haase5c13d892010-10-08 08:37:55 -0700128 }
129}
130
Chris Craikd218a922014-01-02 17:13:34 -0800131void ResourceCache::decrementRefcountLocked(const SkBitmap* bitmapResource) {
Derek Sollenberger3d4eed72014-12-04 15:20:29 -0500132 BitmapKey bitmapKey(bitmapResource);
133 ssize_t index = mBitmapCache.indexOfKey(bitmapKey);
134
135 LOG_ALWAYS_FATAL_IF(index == NAME_NOT_FOUND,
136 "Decrementing the reference of an untracked Bitmap");
137
138 const BitmapKey& cacheEntry = mBitmapCache.keyAt(index);
139 if (cacheEntry.mRefCount == 1) {
140 // delete the bitmap and remove it from the cache
141 delete mBitmapCache.valueAt(index);
142 mBitmapCache.removeItemsAt(index);
143 } else {
144 cacheEntry.mRefCount--;
145 }
Romain Guy58ecc202012-09-07 11:58:36 -0700146}
147
Chris Craikd218a922014-01-02 17:13:34 -0800148void ResourceCache::decrementRefcountLocked(const SkPath* pathResource) {
Romain Guy58ecc202012-09-07 11:58:36 -0700149 decrementRefcountLocked((void*) pathResource);
150}
151
Chris Craikd218a922014-01-02 17:13:34 -0800152void ResourceCache::decrementRefcountLocked(const Res_png_9patch* patchResource) {
Romain Guye3b0a012013-06-26 15:45:41 -0700153 decrementRefcountLocked((void*) patchResource);
154}
155
Chet Haase5a7e8282011-02-04 12:50:55 -0800156void ResourceCache::destructor(SkPath* resource) {
157 Mutex::Autolock _l(mLock);
Romain Guy58ecc202012-09-07 11:58:36 -0700158 destructorLocked(resource);
159}
160
161void ResourceCache::destructorLocked(SkPath* resource) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700162 ssize_t index = mCache->indexOfKey(resource);
Chris Craikd41c4d82015-01-05 15:51:13 -0800163 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : nullptr;
164 if (ref == nullptr) {
Chet Haase5a7e8282011-02-04 12:50:55 -0800165 // If we're not tracking this resource, just delete it
166 if (Caches::hasInstance()) {
167 Caches::getInstance().pathCache.removeDeferred(resource);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900168 } else {
169 delete resource;
Chet Haase5a7e8282011-02-04 12:50:55 -0800170 }
Chet Haase5a7e8282011-02-04 12:50:55 -0800171 return;
172 }
173 ref->destroyed = true;
174 if (ref->refCount == 0) {
Romain Guy97dc9172012-09-23 17:46:45 -0700175 deleteResourceReferenceLocked(resource, ref);
Chet Haase5a7e8282011-02-04 12:50:55 -0800176 }
177}
178
Romain Guye3b0a012013-06-26 15:45:41 -0700179void ResourceCache::destructor(Res_png_9patch* resource) {
180 Mutex::Autolock _l(mLock);
181 destructorLocked(resource);
182}
183
184void ResourceCache::destructorLocked(Res_png_9patch* resource) {
185 ssize_t index = mCache->indexOfKey(resource);
Chris Craikd41c4d82015-01-05 15:51:13 -0800186 ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : nullptr;
187 if (ref == nullptr) {
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900188 // If we're not tracking this resource, just delete it
Romain Guye3b0a012013-06-26 15:45:41 -0700189 if (Caches::hasInstance()) {
190 Caches::getInstance().patchCache.removeDeferred(resource);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900191 } else {
192 // A Res_png_9patch is actually an array of byte that's larger
193 // than sizeof(Res_png_9patch). It must be freed as an array.
194 delete[] (int8_t*) resource;
Romain Guye3b0a012013-06-26 15:45:41 -0700195 }
Romain Guye3b0a012013-06-26 15:45:41 -0700196 return;
197 }
198 ref->destroyed = true;
199 if (ref->refCount == 0) {
200 deleteResourceReferenceLocked(resource, ref);
201 }
202}
203
Chet Haase547e6652012-10-22 15:07:26 -0700204/**
Chet Haasee7d22952010-11-11 16:30:16 -0800205 * This method should only be called while the mLock mutex is held (that mutex is grabbed
206 * by the various destructor() and recycle() methods which call this method).
207 */
Chris Craikd218a922014-01-02 17:13:34 -0800208void ResourceCache::deleteResourceReferenceLocked(const void* resource, ResourceReference* ref) {
John Reck0e89e2b2014-10-31 14:49:06 -0700209 if (ref->destroyed) {
Chet Haase5c13d892010-10-08 08:37:55 -0700210 switch (ref->resourceType) {
Romain Guyd586ad92011-06-22 16:14:36 -0700211 case kPath: {
212 SkPath* path = (SkPath*) resource;
Chet Haase5a7e8282011-02-04 12:50:55 -0800213 if (Caches::hasInstance()) {
214 Caches::getInstance().pathCache.removeDeferred(path);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900215 } else {
216 delete path;
Chet Haase5a7e8282011-02-04 12:50:55 -0800217 }
Chet Haase5a7e8282011-02-04 12:50:55 -0800218 }
219 break;
Romain Guye3b0a012013-06-26 15:45:41 -0700220 case kNinePatch: {
221 if (Caches::hasInstance()) {
222 Caches::getInstance().patchCache.removeDeferred((Res_png_9patch*) resource);
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900223 } else {
224 // A Res_png_9patch is actually an array of byte that's larger
225 // than sizeof(Res_png_9patch). It must be freed as an array.
226 int8_t* patch = (int8_t*) resource;
227 delete[] patch;
Romain Guye3b0a012013-06-26 15:45:41 -0700228 }
Romain Guye3b0a012013-06-26 15:45:41 -0700229 }
230 break;
Chet Haase5c13d892010-10-08 08:37:55 -0700231 }
232 }
233 mCache->removeItem(resource);
234 delete ref;
235}
236
Derek Sollenberger3d4eed72014-12-04 15:20:29 -0500237///////////////////////////////////////////////////////////////////////////////
238// Bitmap Key
239///////////////////////////////////////////////////////////////////////////////
240
241void BitmapKey::operator=(const BitmapKey& other) {
242 this->mRefCount = other.mRefCount;
243 this->mBitmapDimensions = other.mBitmapDimensions;
244 this->mPixelRefOrigin = other.mPixelRefOrigin;
245 this->mPixelRefStableID = other.mPixelRefStableID;
246}
247
248bool BitmapKey::operator==(const BitmapKey& other) const {
249 return mPixelRefStableID == other.mPixelRefStableID &&
250 mPixelRefOrigin == other.mPixelRefOrigin &&
251 mBitmapDimensions == other.mBitmapDimensions;
252}
253
254bool BitmapKey::operator<(const BitmapKey& other) const {
255 if (mPixelRefStableID != other.mPixelRefStableID) {
256 return mPixelRefStableID < other.mPixelRefStableID;
257 }
258 if (mPixelRefOrigin.x() != other.mPixelRefOrigin.x()) {
259 return mPixelRefOrigin.x() < other.mPixelRefOrigin.x();
260 }
261 if (mPixelRefOrigin.y() != other.mPixelRefOrigin.y()) {
262 return mPixelRefOrigin.y() < other.mPixelRefOrigin.y();
263 }
264 if (mBitmapDimensions.width() != other.mBitmapDimensions.width()) {
265 return mBitmapDimensions.width() < other.mBitmapDimensions.width();
266 }
267 return mBitmapDimensions.height() < other.mBitmapDimensions.height();
268}
269
Chet Haase5c13d892010-10-08 08:37:55 -0700270}; // namespace uirenderer
271}; // namespace android