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 "ResourceCache.h" |
| 20 | #include "Caches.h" |
| 21 | |
| 22 | namespace android { |
John Reck | a35778c7 | 2014-11-06 09:45:10 -0800 | [diff] [blame] | 23 | |
John Reck | a35778c7 | 2014-11-06 09:45:10 -0800 | [diff] [blame] | 24 | using namespace uirenderer; |
| 25 | ANDROID_SINGLETON_STATIC_INSTANCE(ResourceCache); |
John Reck | a35778c7 | 2014-11-06 09:45:10 -0800 | [diff] [blame] | 26 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 27 | namespace uirenderer { |
| 28 | |
| 29 | /////////////////////////////////////////////////////////////////////////////// |
| 30 | // Resource cache |
| 31 | /////////////////////////////////////////////////////////////////////////////// |
| 32 | |
| 33 | void ResourceCache::logCache() { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 34 | ALOGD("ResourceCache: cacheReport:"); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 35 | for (size_t i = 0; i < mCache->size(); ++i) { |
| 36 | ResourceReference* ref = mCache->valueAt(i); |
Ashok Bhat | f5df700 | 2014-03-25 20:51:35 +0000 | [diff] [blame] | 37 | ALOGD(" ResourceCache: mCache(%zu): resource, ref = 0x%p, 0x%p", |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 38 | i, mCache->keyAt(i), mCache->valueAt(i)); |
Derek Sollenberger | 3d4eed7 | 2014-12-04 15:20:29 -0500 | [diff] [blame] | 39 | ALOGD(" ResourceCache: mCache(%zu): refCount, destroyed, type = %d, %d, %d", |
| 40 | i, ref->refCount, ref->destroyed, ref->resourceType); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
| 44 | ResourceCache::ResourceCache() { |
Chet Haase | e7d2295 | 2010-11-11 16:30:16 -0800 | [diff] [blame] | 45 | Mutex::Autolock _l(mLock); |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 46 | mCache = new KeyedVector<const void*, ResourceReference*>(); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | ResourceCache::~ResourceCache() { |
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 | delete mCache; |
| 52 | } |
| 53 | |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 54 | void ResourceCache::lock() { |
| 55 | mLock.lock(); |
| 56 | } |
| 57 | |
| 58 | void ResourceCache::unlock() { |
| 59 | mLock.unlock(); |
| 60 | } |
| 61 | |
Derek Sollenberger | 3d4eed7 | 2014-12-04 15:20:29 -0500 | [diff] [blame] | 62 | const 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 Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 77 | void ResourceCache::incrementRefcount(void* resource, ResourceType resourceType) { |
Chet Haase | e7d2295 | 2010-11-11 16:30:16 -0800 | [diff] [blame] | 78 | Mutex::Autolock _l(mLock); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 79 | incrementRefcountLocked(resource, resourceType); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 82 | void ResourceCache::incrementRefcount(const Res_png_9patch* patchResource) { |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 83 | incrementRefcount((void*) patchResource, kNinePatch); |
| 84 | } |
| 85 | |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 86 | void ResourceCache::incrementRefcountLocked(void* resource, ResourceType resourceType) { |
Romain Guy | 8dcfd5e | 2012-07-20 11:36:03 -0700 | [diff] [blame] | 87 | ssize_t index = mCache->indexOfKey(resource); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 88 | ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : nullptr; |
| 89 | if (ref == nullptr || mCache->size() == 0) { |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 90 | ref = new ResourceReference(resourceType); |
| 91 | mCache->add(resource, ref); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 92 | } |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 93 | ref->refCount++; |
| 94 | } |
| 95 | |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 96 | void ResourceCache::decrementRefcount(void* resource) { |
| 97 | Mutex::Autolock _l(mLock); |
| 98 | decrementRefcountLocked(resource); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 101 | void ResourceCache::decrementRefcount(const SkBitmap* bitmapResource) { |
Derek Sollenberger | 3d4eed7 | 2014-12-04 15:20:29 -0500 | [diff] [blame] | 102 | Mutex::Autolock _l(mLock); |
| 103 | decrementRefcountLocked(bitmapResource); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 106 | void ResourceCache::decrementRefcount(const Res_png_9patch* patchResource) { |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 107 | decrementRefcount((void*) patchResource); |
| 108 | } |
| 109 | |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 110 | void ResourceCache::decrementRefcountLocked(void* resource) { |
Romain Guy | 8dcfd5e | 2012-07-20 11:36:03 -0700 | [diff] [blame] | 111 | ssize_t index = mCache->indexOfKey(resource); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 112 | ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : nullptr; |
| 113 | if (ref == nullptr) { |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 114 | // 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] | 115 | return; |
| 116 | } |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 117 | ref->refCount--; |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 118 | if (ref->refCount == 0) { |
Romain Guy | 97dc917 | 2012-09-23 17:46:45 -0700 | [diff] [blame] | 119 | deleteResourceReferenceLocked(resource, ref); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 123 | void ResourceCache::decrementRefcountLocked(const SkBitmap* bitmapResource) { |
Derek Sollenberger | 3d4eed7 | 2014-12-04 15:20:29 -0500 | [diff] [blame] | 124 | BitmapKey bitmapKey(bitmapResource); |
| 125 | ssize_t index = mBitmapCache.indexOfKey(bitmapKey); |
| 126 | |
| 127 | LOG_ALWAYS_FATAL_IF(index == NAME_NOT_FOUND, |
| 128 | "Decrementing the reference of an untracked Bitmap"); |
| 129 | |
| 130 | const BitmapKey& cacheEntry = mBitmapCache.keyAt(index); |
| 131 | if (cacheEntry.mRefCount == 1) { |
| 132 | // delete the bitmap and remove it from the cache |
| 133 | delete mBitmapCache.valueAt(index); |
| 134 | mBitmapCache.removeItemsAt(index); |
| 135 | } else { |
| 136 | cacheEntry.mRefCount--; |
| 137 | } |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 140 | void ResourceCache::decrementRefcountLocked(const Res_png_9patch* patchResource) { |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 141 | decrementRefcountLocked((void*) patchResource); |
| 142 | } |
| 143 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 144 | void ResourceCache::destructor(Res_png_9patch* resource) { |
| 145 | Mutex::Autolock _l(mLock); |
| 146 | destructorLocked(resource); |
| 147 | } |
| 148 | |
| 149 | void ResourceCache::destructorLocked(Res_png_9patch* resource) { |
| 150 | ssize_t index = mCache->indexOfKey(resource); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 151 | ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : nullptr; |
| 152 | if (ref == nullptr) { |
Sangkyu Lee | 36fad8f | 2014-01-09 14:11:57 +0900 | [diff] [blame] | 153 | // If we're not tracking this resource, just delete it |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 154 | if (Caches::hasInstance()) { |
| 155 | Caches::getInstance().patchCache.removeDeferred(resource); |
Sangkyu Lee | 36fad8f | 2014-01-09 14:11:57 +0900 | [diff] [blame] | 156 | } else { |
| 157 | // A Res_png_9patch is actually an array of byte that's larger |
| 158 | // than sizeof(Res_png_9patch). It must be freed as an array. |
| 159 | delete[] (int8_t*) resource; |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 160 | } |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 161 | return; |
| 162 | } |
| 163 | ref->destroyed = true; |
| 164 | if (ref->refCount == 0) { |
| 165 | deleteResourceReferenceLocked(resource, ref); |
| 166 | } |
| 167 | } |
| 168 | |
Chet Haase | 547e665 | 2012-10-22 15:07:26 -0700 | [diff] [blame] | 169 | /** |
Chet Haase | e7d2295 | 2010-11-11 16:30:16 -0800 | [diff] [blame] | 170 | * This method should only be called while the mLock mutex is held (that mutex is grabbed |
| 171 | * by the various destructor() and recycle() methods which call this method). |
| 172 | */ |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 173 | void ResourceCache::deleteResourceReferenceLocked(const void* resource, ResourceReference* ref) { |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 174 | if (ref->destroyed) { |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 175 | switch (ref->resourceType) { |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 176 | case kNinePatch: { |
| 177 | if (Caches::hasInstance()) { |
| 178 | Caches::getInstance().patchCache.removeDeferred((Res_png_9patch*) resource); |
Sangkyu Lee | 36fad8f | 2014-01-09 14:11:57 +0900 | [diff] [blame] | 179 | } else { |
| 180 | // A Res_png_9patch is actually an array of byte that's larger |
| 181 | // than sizeof(Res_png_9patch). It must be freed as an array. |
| 182 | int8_t* patch = (int8_t*) resource; |
| 183 | delete[] patch; |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 184 | } |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 185 | } |
| 186 | break; |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | mCache->removeItem(resource); |
| 190 | delete ref; |
| 191 | } |
| 192 | |
Derek Sollenberger | 3d4eed7 | 2014-12-04 15:20:29 -0500 | [diff] [blame] | 193 | /////////////////////////////////////////////////////////////////////////////// |
| 194 | // Bitmap Key |
| 195 | /////////////////////////////////////////////////////////////////////////////// |
| 196 | |
| 197 | void BitmapKey::operator=(const BitmapKey& other) { |
| 198 | this->mRefCount = other.mRefCount; |
| 199 | this->mBitmapDimensions = other.mBitmapDimensions; |
| 200 | this->mPixelRefOrigin = other.mPixelRefOrigin; |
| 201 | this->mPixelRefStableID = other.mPixelRefStableID; |
| 202 | } |
| 203 | |
| 204 | bool BitmapKey::operator==(const BitmapKey& other) const { |
| 205 | return mPixelRefStableID == other.mPixelRefStableID && |
| 206 | mPixelRefOrigin == other.mPixelRefOrigin && |
| 207 | mBitmapDimensions == other.mBitmapDimensions; |
| 208 | } |
| 209 | |
| 210 | bool BitmapKey::operator<(const BitmapKey& other) const { |
| 211 | if (mPixelRefStableID != other.mPixelRefStableID) { |
| 212 | return mPixelRefStableID < other.mPixelRefStableID; |
| 213 | } |
| 214 | if (mPixelRefOrigin.x() != other.mPixelRefOrigin.x()) { |
| 215 | return mPixelRefOrigin.x() < other.mPixelRefOrigin.x(); |
| 216 | } |
| 217 | if (mPixelRefOrigin.y() != other.mPixelRefOrigin.y()) { |
| 218 | return mPixelRefOrigin.y() < other.mPixelRefOrigin.y(); |
| 219 | } |
| 220 | if (mBitmapDimensions.width() != other.mBitmapDimensions.width()) { |
| 221 | return mBitmapDimensions.width() < other.mBitmapDimensions.width(); |
| 222 | } |
| 223 | return mBitmapDimensions.height() < other.mBitmapDimensions.height(); |
| 224 | } |
| 225 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 226 | }; // namespace uirenderer |
| 227 | }; // namespace android |