Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 1 | /* |
| 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 Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 17 | #define LOG_TAG "OpenGLRenderer" |
| 18 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 19 | #include <SkPixelRef.h> |
| 20 | #include "ResourceCache.h" |
| 21 | #include "Caches.h" |
| 22 | |
| 23 | namespace android { |
| 24 | namespace uirenderer { |
| 25 | |
| 26 | /////////////////////////////////////////////////////////////////////////////// |
| 27 | // Resource cache |
| 28 | /////////////////////////////////////////////////////////////////////////////// |
| 29 | |
| 30 | void ResourceCache::logCache() { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 31 | ALOGD("ResourceCache: cacheReport:"); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 32 | for (size_t i = 0; i < mCache->size(); ++i) { |
| 33 | ResourceReference* ref = mCache->valueAt(i); |
Ashok Bhat | f5df700 | 2014-03-25 20:51:35 +0000 | [diff] [blame] | 34 | ALOGD(" ResourceCache: mCache(%zu): resource, ref = 0x%p, 0x%p", |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 35 | i, mCache->keyAt(i), mCache->valueAt(i)); |
Ashok Bhat | f5df700 | 2014-03-25 20:51:35 +0000 | [diff] [blame] | 36 | ALOGD(" ResourceCache: mCache(%zu): refCount, recycled, destroyed, type = %d, %d, %d, %d", |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 37 | i, ref->refCount, ref->recycled, ref->destroyed, ref->resourceType); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | ResourceCache::ResourceCache() { |
Chet Haase | e7d2295 | 2010-11-11 16:30:16 -0800 | [diff] [blame] | 42 | Mutex::Autolock _l(mLock); |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 43 | mCache = new KeyedVector<const void*, ResourceReference*>(); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | ResourceCache::~ResourceCache() { |
Chet Haase | e7d2295 | 2010-11-11 16:30:16 -0800 | [diff] [blame] | 47 | Mutex::Autolock _l(mLock); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 48 | delete mCache; |
| 49 | } |
| 50 | |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 51 | void ResourceCache::lock() { |
| 52 | mLock.lock(); |
| 53 | } |
| 54 | |
| 55 | void ResourceCache::unlock() { |
| 56 | mLock.unlock(); |
| 57 | } |
| 58 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 59 | void ResourceCache::incrementRefcount(void* resource, ResourceType resourceType) { |
Chet Haase | e7d2295 | 2010-11-11 16:30:16 -0800 | [diff] [blame] | 60 | Mutex::Autolock _l(mLock); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 61 | incrementRefcountLocked(resource, resourceType); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 64 | void ResourceCache::incrementRefcount(const SkBitmap* bitmapResource) { |
Chris Craik | 0c20c38 | 2013-07-02 10:48:54 -0700 | [diff] [blame] | 65 | bitmapResource->pixelRef()->globalRef(); |
Derek Sollenberger | 6062c59 | 2011-02-22 13:55:04 -0500 | [diff] [blame] | 66 | SkSafeRef(bitmapResource->getColorTable()); |
Romain Guy | 49c5fc0 | 2012-05-15 11:10:01 -0700 | [diff] [blame] | 67 | incrementRefcount((void*) bitmapResource, kBitmap); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 70 | void ResourceCache::incrementRefcount(const SkPath* pathResource) { |
Romain Guy | 49c5fc0 | 2012-05-15 11:10:01 -0700 | [diff] [blame] | 71 | incrementRefcount((void*) pathResource, kPath); |
Chet Haase | 5a7e828 | 2011-02-04 12:50:55 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 74 | void ResourceCache::incrementRefcount(SkiaShader* shaderResource) { |
Derek Sollenberger | 6062c59 | 2011-02-22 13:55:04 -0500 | [diff] [blame] | 75 | SkSafeRef(shaderResource->getSkShader()); |
Romain Guy | 43ccf46 | 2011-01-14 18:51:01 -0800 | [diff] [blame] | 76 | incrementRefcount((void*) shaderResource, kShader); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 79 | void ResourceCache::incrementRefcount(const Res_png_9patch* patchResource) { |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 80 | incrementRefcount((void*) patchResource, kNinePatch); |
| 81 | } |
| 82 | |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 83 | void ResourceCache::incrementRefcount(Layer* layerResource) { |
| 84 | incrementRefcount((void*) layerResource, kLayer); |
| 85 | } |
| 86 | |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 87 | void ResourceCache::incrementRefcountLocked(void* resource, ResourceType resourceType) { |
Romain Guy | 8dcfd5e | 2012-07-20 11:36:03 -0700 | [diff] [blame] | 88 | ssize_t index = mCache->indexOfKey(resource); |
| 89 | ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL; |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 90 | if (ref == NULL || mCache->size() == 0) { |
| 91 | ref = new ResourceReference(resourceType); |
| 92 | mCache->add(resource, ref); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 93 | } |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 94 | ref->refCount++; |
| 95 | } |
| 96 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 97 | void ResourceCache::incrementRefcountLocked(const SkBitmap* bitmapResource) { |
Chris Craik | 0c20c38 | 2013-07-02 10:48:54 -0700 | [diff] [blame] | 98 | bitmapResource->pixelRef()->globalRef(); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 99 | SkSafeRef(bitmapResource->getColorTable()); |
| 100 | incrementRefcountLocked((void*) bitmapResource, kBitmap); |
| 101 | } |
| 102 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 103 | void ResourceCache::incrementRefcountLocked(const SkPath* pathResource) { |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 104 | incrementRefcountLocked((void*) pathResource, kPath); |
| 105 | } |
| 106 | |
| 107 | void ResourceCache::incrementRefcountLocked(SkiaShader* shaderResource) { |
| 108 | SkSafeRef(shaderResource->getSkShader()); |
| 109 | incrementRefcountLocked((void*) shaderResource, kShader); |
| 110 | } |
| 111 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 112 | void ResourceCache::incrementRefcountLocked(const Res_png_9patch* patchResource) { |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 113 | incrementRefcountLocked((void*) patchResource, kNinePatch); |
| 114 | } |
| 115 | |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 116 | void ResourceCache::incrementRefcountLocked(Layer* layerResource) { |
| 117 | incrementRefcountLocked((void*) layerResource, kLayer); |
| 118 | } |
| 119 | |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 120 | void ResourceCache::decrementRefcount(void* resource) { |
| 121 | Mutex::Autolock _l(mLock); |
| 122 | decrementRefcountLocked(resource); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 125 | void ResourceCache::decrementRefcount(const SkBitmap* bitmapResource) { |
Chris Craik | 0c20c38 | 2013-07-02 10:48:54 -0700 | [diff] [blame] | 126 | bitmapResource->pixelRef()->globalUnref(); |
Derek Sollenberger | 6062c59 | 2011-02-22 13:55:04 -0500 | [diff] [blame] | 127 | SkSafeUnref(bitmapResource->getColorTable()); |
Romain Guy | 43ccf46 | 2011-01-14 18:51:01 -0800 | [diff] [blame] | 128 | decrementRefcount((void*) bitmapResource); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 131 | void ResourceCache::decrementRefcount(const SkPath* pathResource) { |
Chet Haase | 5a7e828 | 2011-02-04 12:50:55 -0800 | [diff] [blame] | 132 | decrementRefcount((void*) pathResource); |
| 133 | } |
| 134 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 135 | void ResourceCache::decrementRefcount(SkiaShader* shaderResource) { |
Derek Sollenberger | 6062c59 | 2011-02-22 13:55:04 -0500 | [diff] [blame] | 136 | SkSafeUnref(shaderResource->getSkShader()); |
Romain Guy | 43ccf46 | 2011-01-14 18:51:01 -0800 | [diff] [blame] | 137 | decrementRefcount((void*) shaderResource); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 140 | void ResourceCache::decrementRefcount(const Res_png_9patch* patchResource) { |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 141 | decrementRefcount((void*) patchResource); |
| 142 | } |
| 143 | |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 144 | void ResourceCache::decrementRefcount(Layer* layerResource) { |
| 145 | decrementRefcount((void*) layerResource); |
| 146 | } |
| 147 | |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 148 | void ResourceCache::decrementRefcountLocked(void* resource) { |
Romain Guy | 8dcfd5e | 2012-07-20 11:36:03 -0700 | [diff] [blame] | 149 | ssize_t index = mCache->indexOfKey(resource); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 150 | ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL; |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 151 | if (ref == NULL) { |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 152 | // Should not get here - shouldn't get a call to decrement if we're not yet tracking it |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 153 | return; |
| 154 | } |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 155 | ref->refCount--; |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 156 | if (ref->refCount == 0) { |
Romain Guy | 97dc917 | 2012-09-23 17:46:45 -0700 | [diff] [blame] | 157 | deleteResourceReferenceLocked(resource, ref); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 161 | void ResourceCache::decrementRefcountLocked(const SkBitmap* bitmapResource) { |
Chris Craik | 0c20c38 | 2013-07-02 10:48:54 -0700 | [diff] [blame] | 162 | bitmapResource->pixelRef()->globalUnref(); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 163 | SkSafeUnref(bitmapResource->getColorTable()); |
| 164 | decrementRefcountLocked((void*) bitmapResource); |
| 165 | } |
| 166 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 167 | void ResourceCache::decrementRefcountLocked(const SkPath* pathResource) { |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 168 | decrementRefcountLocked((void*) pathResource); |
| 169 | } |
| 170 | |
| 171 | void ResourceCache::decrementRefcountLocked(SkiaShader* shaderResource) { |
| 172 | SkSafeUnref(shaderResource->getSkShader()); |
| 173 | decrementRefcountLocked((void*) shaderResource); |
| 174 | } |
| 175 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 176 | void ResourceCache::decrementRefcountLocked(const Res_png_9patch* patchResource) { |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 177 | decrementRefcountLocked((void*) patchResource); |
| 178 | } |
| 179 | |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 180 | void ResourceCache::decrementRefcountLocked(Layer* layerResource) { |
| 181 | decrementRefcountLocked((void*) layerResource); |
| 182 | } |
| 183 | |
Chet Haase | 5a7e828 | 2011-02-04 12:50:55 -0800 | [diff] [blame] | 184 | void ResourceCache::destructor(SkPath* resource) { |
| 185 | Mutex::Autolock _l(mLock); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 186 | destructorLocked(resource); |
| 187 | } |
| 188 | |
| 189 | void ResourceCache::destructorLocked(SkPath* resource) { |
Romain Guy | 8dcfd5e | 2012-07-20 11:36:03 -0700 | [diff] [blame] | 190 | ssize_t index = mCache->indexOfKey(resource); |
| 191 | ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL; |
Chet Haase | 5a7e828 | 2011-02-04 12:50:55 -0800 | [diff] [blame] | 192 | 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 Lee | 36fad8f | 2014-01-09 14:11:57 +0900 | [diff] [blame] | 196 | } else { |
| 197 | delete resource; |
Chet Haase | 5a7e828 | 2011-02-04 12:50:55 -0800 | [diff] [blame] | 198 | } |
Chet Haase | 5a7e828 | 2011-02-04 12:50:55 -0800 | [diff] [blame] | 199 | return; |
| 200 | } |
| 201 | ref->destroyed = true; |
| 202 | if (ref->refCount == 0) { |
Romain Guy | 97dc917 | 2012-09-23 17:46:45 -0700 | [diff] [blame] | 203 | deleteResourceReferenceLocked(resource, ref); |
Chet Haase | 5a7e828 | 2011-02-04 12:50:55 -0800 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 207 | void ResourceCache::destructor(const SkBitmap* resource) { |
Chet Haase | e7d2295 | 2010-11-11 16:30:16 -0800 | [diff] [blame] | 208 | Mutex::Autolock _l(mLock); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 209 | destructorLocked(resource); |
| 210 | } |
| 211 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 212 | void ResourceCache::destructorLocked(const SkBitmap* resource) { |
Romain Guy | 8dcfd5e | 2012-07-20 11:36:03 -0700 | [diff] [blame] | 213 | ssize_t index = mCache->indexOfKey(resource); |
| 214 | ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL; |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 215 | if (ref == NULL) { |
| 216 | // If we're not tracking this resource, just delete it |
| 217 | if (Caches::hasInstance()) { |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 218 | Caches::getInstance().textureCache.removeDeferred(resource); |
Sangkyu Lee | 36fad8f | 2014-01-09 14:11:57 +0900 | [diff] [blame] | 219 | } else { |
| 220 | delete resource; |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 221 | } |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 222 | return; |
| 223 | } |
| 224 | ref->destroyed = true; |
| 225 | if (ref->refCount == 0) { |
Romain Guy | 97dc917 | 2012-09-23 17:46:45 -0700 | [diff] [blame] | 226 | deleteResourceReferenceLocked(resource, ref); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 230 | void ResourceCache::destructor(SkiaShader* resource) { |
Chet Haase | e7d2295 | 2010-11-11 16:30:16 -0800 | [diff] [blame] | 231 | Mutex::Autolock _l(mLock); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 232 | destructorLocked(resource); |
| 233 | } |
| 234 | |
| 235 | void ResourceCache::destructorLocked(SkiaShader* resource) { |
Romain Guy | 8dcfd5e | 2012-07-20 11:36:03 -0700 | [diff] [blame] | 236 | ssize_t index = mCache->indexOfKey(resource); |
| 237 | ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : NULL; |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 238 | if (ref == NULL) { |
| 239 | // If we're not tracking this resource, just delete it |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 240 | delete resource; |
| 241 | return; |
| 242 | } |
| 243 | ref->destroyed = true; |
| 244 | if (ref->refCount == 0) { |
Romain Guy | 97dc917 | 2012-09-23 17:46:45 -0700 | [diff] [blame] | 245 | deleteResourceReferenceLocked(resource, ref); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 249 | void ResourceCache::destructor(Res_png_9patch* resource) { |
| 250 | Mutex::Autolock _l(mLock); |
| 251 | destructorLocked(resource); |
| 252 | } |
| 253 | |
| 254 | void 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 Lee | 36fad8f | 2014-01-09 14:11:57 +0900 | [diff] [blame] | 258 | // If we're not tracking this resource, just delete it |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 259 | if (Caches::hasInstance()) { |
| 260 | Caches::getInstance().patchCache.removeDeferred(resource); |
Sangkyu Lee | 36fad8f | 2014-01-09 14:11:57 +0900 | [diff] [blame] | 261 | } 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 Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 265 | } |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 266 | return; |
| 267 | } |
| 268 | ref->destroyed = true; |
| 269 | if (ref->refCount == 0) { |
| 270 | deleteResourceReferenceLocked(resource, ref); |
| 271 | } |
| 272 | } |
| 273 | |
Chet Haase | 547e665 | 2012-10-22 15:07:26 -0700 | [diff] [blame] | 274 | /** |
| 275 | * Return value indicates whether resource was actually recycled, which happens when RefCnt |
| 276 | * reaches 0. |
| 277 | */ |
| 278 | bool ResourceCache::recycle(SkBitmap* resource) { |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 279 | Mutex::Autolock _l(mLock); |
Chet Haase | 547e665 | 2012-10-22 15:07:26 -0700 | [diff] [blame] | 280 | return recycleLocked(resource); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Chet Haase | 547e665 | 2012-10-22 15:07:26 -0700 | [diff] [blame] | 283 | /** |
| 284 | * Return value indicates whether resource was actually recycled, which happens when RefCnt |
| 285 | * reaches 0. |
| 286 | */ |
| 287 | bool ResourceCache::recycleLocked(SkBitmap* resource) { |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 288 | 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 Haase | 547e665 | 2012-10-22 15:07:26 -0700 | [diff] [blame] | 292 | return true; |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 293 | } |
| 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 Haase | 547e665 | 2012-10-22 15:07:26 -0700 | [diff] [blame] | 297 | return true; |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 298 | } |
| 299 | ref->recycled = true; |
| 300 | if (ref->refCount == 0) { |
Romain Guy | 97dc917 | 2012-09-23 17:46:45 -0700 | [diff] [blame] | 301 | deleteResourceReferenceLocked(resource, ref); |
Chet Haase | 547e665 | 2012-10-22 15:07:26 -0700 | [diff] [blame] | 302 | return true; |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 303 | } |
Chet Haase | 547e665 | 2012-10-22 15:07:26 -0700 | [diff] [blame] | 304 | // Still referring to resource, don't recycle yet |
| 305 | return false; |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Chet Haase | e7d2295 | 2010-11-11 16:30:16 -0800 | [diff] [blame] | 308 | /** |
| 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 Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 312 | void ResourceCache::deleteResourceReferenceLocked(const void* resource, ResourceReference* ref) { |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 313 | if (ref->recycled && ref->resourceType == kBitmap) { |
| 314 | ((SkBitmap*) resource)->setPixels(NULL, NULL); |
| 315 | } |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 316 | if (ref->destroyed || ref->resourceType == kLayer) { |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 317 | switch (ref->resourceType) { |
Romain Guy | d586ad9 | 2011-06-22 16:14:36 -0700 | [diff] [blame] | 318 | case kBitmap: { |
| 319 | SkBitmap* bitmap = (SkBitmap*) resource; |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 320 | if (Caches::hasInstance()) { |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 321 | Caches::getInstance().textureCache.removeDeferred(bitmap); |
Sangkyu Lee | 36fad8f | 2014-01-09 14:11:57 +0900 | [diff] [blame] | 322 | } else { |
| 323 | delete bitmap; |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 324 | } |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 325 | } |
| 326 | break; |
Romain Guy | d586ad9 | 2011-06-22 16:14:36 -0700 | [diff] [blame] | 327 | case kPath: { |
| 328 | SkPath* path = (SkPath*) resource; |
Chet Haase | 5a7e828 | 2011-02-04 12:50:55 -0800 | [diff] [blame] | 329 | if (Caches::hasInstance()) { |
| 330 | Caches::getInstance().pathCache.removeDeferred(path); |
Sangkyu Lee | 36fad8f | 2014-01-09 14:11:57 +0900 | [diff] [blame] | 331 | } else { |
| 332 | delete path; |
Chet Haase | 5a7e828 | 2011-02-04 12:50:55 -0800 | [diff] [blame] | 333 | } |
Chet Haase | 5a7e828 | 2011-02-04 12:50:55 -0800 | [diff] [blame] | 334 | } |
| 335 | break; |
Romain Guy | d586ad9 | 2011-06-22 16:14:36 -0700 | [diff] [blame] | 336 | case kShader: { |
| 337 | SkiaShader* shader = (SkiaShader*) resource; |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 338 | delete shader; |
Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 339 | } |
| 340 | break; |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 341 | case kNinePatch: { |
| 342 | if (Caches::hasInstance()) { |
| 343 | Caches::getInstance().patchCache.removeDeferred((Res_png_9patch*) resource); |
Sangkyu Lee | 36fad8f | 2014-01-09 14:11:57 +0900 | [diff] [blame] | 344 | } 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 Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 349 | } |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 350 | } |
| 351 | break; |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 352 | case kLayer: { |
Dave Burke | 56257af | 2012-09-25 20:30:09 -0700 | [diff] [blame] | 353 | Layer* layer = (Layer*) resource; |
Mathias Agopian | 54643d7 | 2012-09-25 21:30:22 -0700 | [diff] [blame] | 354 | Caches::getInstance().deleteLayerDeferred(layer); |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 355 | } |
| 356 | break; |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | mCache->removeItem(resource); |
| 360 | delete ref; |
| 361 | } |
| 362 | |
| 363 | }; // namespace uirenderer |
| 364 | }; // namespace android |