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 | |
| 17 | #include <SkPixelRef.h> |
| 18 | #include "ResourceCache.h" |
| 19 | #include "Caches.h" |
| 20 | |
| 21 | namespace android { |
| 22 | namespace uirenderer { |
| 23 | |
| 24 | /////////////////////////////////////////////////////////////////////////////// |
| 25 | // Resource cache |
| 26 | /////////////////////////////////////////////////////////////////////////////// |
| 27 | |
| 28 | void 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 | |
| 39 | ResourceCache::ResourceCache() { |
Chet Haase | e7d2295 | 2010-11-11 16:30:16 -0800 | [diff] [blame] | 40 | Mutex::Autolock _l(mLock); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 41 | mCache = new KeyedVector<void *, ResourceReference *>(); |
| 42 | } |
| 43 | |
| 44 | ResourceCache::~ResourceCache() { |
Chet Haase | e7d2295 | 2010-11-11 16:30:16 -0800 | [diff] [blame] | 45 | Mutex::Autolock _l(mLock); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 46 | delete mCache; |
| 47 | } |
| 48 | |
| 49 | void ResourceCache::incrementRefcount(void* resource, ResourceType resourceType) { |
Chet Haase | e7d2295 | 2010-11-11 16:30:16 -0800 | [diff] [blame] | 50 | Mutex::Autolock _l(mLock); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 51 | 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 | |
| 62 | void ResourceCache::incrementRefcount(SkBitmap* bitmapResource) { |
Patrick Dubroy | f890fab | 2010-12-19 16:47:17 -0800 | [diff] [blame] | 63 | bitmapResource->pixelRef()->safeRef(); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 64 | bitmapResource->getColorTable()->safeRef(); |
| 65 | incrementRefcount((void*)bitmapResource, kBitmap); |
| 66 | } |
| 67 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 68 | void ResourceCache::incrementRefcount(SkiaShader* shaderResource) { |
| 69 | shaderResource->getSkShader()->safeRef(); |
Romain Guy | 43ccf46 | 2011-01-14 18:51:01 -0800 | [diff] [blame] | 70 | incrementRefcount((void*) shaderResource, kShader); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 73 | void ResourceCache::incrementRefcount(SkiaColorFilter* filterResource) { |
| 74 | filterResource->getSkColorFilter()->safeRef(); |
Romain Guy | 43ccf46 | 2011-01-14 18:51:01 -0800 | [diff] [blame] | 75 | incrementRefcount((void*) filterResource, kColorFilter); |
Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 78 | void ResourceCache::decrementRefcount(void* resource) { |
Chet Haase | e7d2295 | 2010-11-11 16:30:16 -0800 | [diff] [blame] | 79 | Mutex::Autolock _l(mLock); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 80 | 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 | |
| 91 | void ResourceCache::decrementRefcount(SkBitmap* bitmapResource) { |
Patrick Dubroy | f890fab | 2010-12-19 16:47:17 -0800 | [diff] [blame] | 92 | bitmapResource->pixelRef()->safeUnref(); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 93 | bitmapResource->getColorTable()->safeUnref(); |
Romain Guy | 43ccf46 | 2011-01-14 18:51:01 -0800 | [diff] [blame] | 94 | decrementRefcount((void*) bitmapResource); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void ResourceCache::decrementRefcount(SkiaShader* shaderResource) { |
| 98 | shaderResource->getSkShader()->safeUnref(); |
Romain Guy | 43ccf46 | 2011-01-14 18:51:01 -0800 | [diff] [blame] | 99 | decrementRefcount((void*) shaderResource); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 102 | void ResourceCache::decrementRefcount(SkiaColorFilter* filterResource) { |
| 103 | filterResource->getSkColorFilter()->safeUnref(); |
Romain Guy | 43ccf46 | 2011-01-14 18:51:01 -0800 | [diff] [blame] | 104 | decrementRefcount((void*) filterResource); |
Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 107 | void ResourceCache::recycle(SkBitmap* resource) { |
Chet Haase | e7d2295 | 2010-11-11 16:30:16 -0800 | [diff] [blame] | 108 | Mutex::Autolock _l(mLock); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 109 | if (mCache->indexOfKey(resource) < 0) { |
| 110 | // not tracking this resource; just recycle the pixel data |
| 111 | resource->setPixels(NULL, NULL); |
| 112 | return; |
| 113 | } |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 114 | 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 | |
| 125 | void ResourceCache::destructor(SkBitmap* resource) { |
Chet Haase | e7d2295 | 2010-11-11 16:30:16 -0800 | [diff] [blame] | 126 | Mutex::Autolock _l(mLock); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 127 | 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 Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 131 | Caches::getInstance().textureCache.removeDeferred(resource); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 132 | } |
| 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 Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 143 | void ResourceCache::destructor(SkiaShader* resource) { |
Chet Haase | e7d2295 | 2010-11-11 16:30:16 -0800 | [diff] [blame] | 144 | Mutex::Autolock _l(mLock); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 145 | 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 Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 149 | Caches::getInstance().gradientCache.removeDeferred(resource->getSkShader()); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 150 | } |
| 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 Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 161 | void ResourceCache::destructor(SkiaColorFilter* resource) { |
Chet Haase | e7d2295 | 2010-11-11 16:30:16 -0800 | [diff] [blame] | 162 | Mutex::Autolock _l(mLock); |
Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 163 | 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 Haase | e7d2295 | 2010-11-11 16:30:16 -0800 | [diff] [blame] | 176 | /** |
| 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 Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 180 | void 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 Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 190 | Caches::getInstance().textureCache.removeDeferred(bitmap); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 191 | } |
| 192 | delete bitmap; |
| 193 | } |
| 194 | break; |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 195 | case kShader: |
Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 196 | { |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 197 | SkiaShader* shader = (SkiaShader*)resource; |
| 198 | if (Caches::hasInstance()) { |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 199 | Caches::getInstance().gradientCache.removeDeferred(shader->getSkShader()); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 200 | } |
| 201 | delete shader; |
Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 202 | } |
| 203 | break; |
| 204 | case kColorFilter: |
| 205 | { |
| 206 | SkiaColorFilter* filter = (SkiaColorFilter*)resource; |
| 207 | delete filter; |
| 208 | } |
| 209 | break; |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | mCache->removeItem(resource); |
| 213 | delete ref; |
| 214 | } |
| 215 | |
| 216 | }; // namespace uirenderer |
| 217 | }; // namespace android |