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 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 17 | #include "ResourceCache.h" |
| 18 | #include "Caches.h" |
| 19 | |
| 20 | namespace android { |
John Reck | a35778c7 | 2014-11-06 09:45:10 -0800 | [diff] [blame] | 21 | |
John Reck | a35778c7 | 2014-11-06 09:45:10 -0800 | [diff] [blame] | 22 | using namespace uirenderer; |
| 23 | ANDROID_SINGLETON_STATIC_INSTANCE(ResourceCache); |
John Reck | a35778c7 | 2014-11-06 09:45:10 -0800 | [diff] [blame] | 24 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 25 | namespace uirenderer { |
| 26 | |
| 27 | /////////////////////////////////////////////////////////////////////////////// |
| 28 | // Resource cache |
| 29 | /////////////////////////////////////////////////////////////////////////////// |
| 30 | |
| 31 | void ResourceCache::logCache() { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 32 | ALOGD("ResourceCache: cacheReport:"); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 33 | for (size_t i = 0; i < mCache->size(); ++i) { |
| 34 | ResourceReference* ref = mCache->valueAt(i); |
Ashok Bhat | f5df700 | 2014-03-25 20:51:35 +0000 | [diff] [blame] | 35 | ALOGD(" ResourceCache: mCache(%zu): resource, ref = 0x%p, 0x%p", |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 36 | i, mCache->keyAt(i), mCache->valueAt(i)); |
Derek Sollenberger | 3d4eed7 | 2014-12-04 15:20:29 -0500 | [diff] [blame] | 37 | ALOGD(" ResourceCache: mCache(%zu): refCount, destroyed, type = %d, %d, %d", |
| 38 | i, ref->refCount, ref->destroyed, ref->resourceType); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
| 42 | ResourceCache::ResourceCache() { |
Chet Haase | e7d2295 | 2010-11-11 16:30:16 -0800 | [diff] [blame] | 43 | Mutex::Autolock _l(mLock); |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 44 | mCache = new KeyedVector<const void*, ResourceReference*>(); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | ResourceCache::~ResourceCache() { |
Chet Haase | e7d2295 | 2010-11-11 16:30:16 -0800 | [diff] [blame] | 48 | Mutex::Autolock _l(mLock); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 49 | delete mCache; |
| 50 | } |
| 51 | |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 52 | void ResourceCache::lock() { |
| 53 | mLock.lock(); |
| 54 | } |
| 55 | |
| 56 | void ResourceCache::unlock() { |
| 57 | mLock.unlock(); |
| 58 | } |
| 59 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 60 | void ResourceCache::incrementRefcount(void* resource, ResourceType resourceType) { |
Chet Haase | e7d2295 | 2010-11-11 16:30:16 -0800 | [diff] [blame] | 61 | Mutex::Autolock _l(mLock); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 62 | incrementRefcountLocked(resource, resourceType); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 65 | void ResourceCache::incrementRefcount(const Res_png_9patch* patchResource) { |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 66 | incrementRefcount((void*) patchResource, kNinePatch); |
| 67 | } |
| 68 | |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 69 | void ResourceCache::incrementRefcountLocked(void* resource, ResourceType resourceType) { |
Romain Guy | 8dcfd5e | 2012-07-20 11:36:03 -0700 | [diff] [blame] | 70 | ssize_t index = mCache->indexOfKey(resource); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 71 | ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : nullptr; |
| 72 | if (ref == nullptr || mCache->size() == 0) { |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 73 | ref = new ResourceReference(resourceType); |
| 74 | mCache->add(resource, ref); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 75 | } |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 76 | ref->refCount++; |
| 77 | } |
| 78 | |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 79 | void ResourceCache::decrementRefcount(void* resource) { |
| 80 | Mutex::Autolock _l(mLock); |
| 81 | decrementRefcountLocked(resource); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 84 | void ResourceCache::decrementRefcount(const Res_png_9patch* patchResource) { |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 85 | decrementRefcount((void*) patchResource); |
| 86 | } |
| 87 | |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 88 | void ResourceCache::decrementRefcountLocked(void* resource) { |
Romain Guy | 8dcfd5e | 2012-07-20 11:36:03 -0700 | [diff] [blame] | 89 | ssize_t index = mCache->indexOfKey(resource); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 90 | ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : nullptr; |
| 91 | if (ref == nullptr) { |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 92 | // 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] | 93 | return; |
| 94 | } |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 95 | ref->refCount--; |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 96 | if (ref->refCount == 0) { |
Romain Guy | 97dc917 | 2012-09-23 17:46:45 -0700 | [diff] [blame] | 97 | deleteResourceReferenceLocked(resource, ref); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 101 | void ResourceCache::decrementRefcountLocked(const Res_png_9patch* patchResource) { |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 102 | decrementRefcountLocked((void*) patchResource); |
| 103 | } |
| 104 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 105 | void ResourceCache::destructor(Res_png_9patch* resource) { |
| 106 | Mutex::Autolock _l(mLock); |
| 107 | destructorLocked(resource); |
| 108 | } |
| 109 | |
| 110 | void ResourceCache::destructorLocked(Res_png_9patch* resource) { |
| 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) { |
Sangkyu Lee | 36fad8f | 2014-01-09 14:11:57 +0900 | [diff] [blame] | 114 | // If we're not tracking this resource, just delete it |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 115 | if (Caches::hasInstance()) { |
| 116 | Caches::getInstance().patchCache.removeDeferred(resource); |
Sangkyu Lee | 36fad8f | 2014-01-09 14:11:57 +0900 | [diff] [blame] | 117 | } else { |
| 118 | // A Res_png_9patch is actually an array of byte that's larger |
| 119 | // than sizeof(Res_png_9patch). It must be freed as an array. |
| 120 | delete[] (int8_t*) resource; |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 121 | } |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 122 | return; |
| 123 | } |
| 124 | ref->destroyed = true; |
| 125 | if (ref->refCount == 0) { |
| 126 | deleteResourceReferenceLocked(resource, ref); |
| 127 | } |
| 128 | } |
| 129 | |
Chet Haase | 547e665 | 2012-10-22 15:07:26 -0700 | [diff] [blame] | 130 | /** |
Chet Haase | e7d2295 | 2010-11-11 16:30:16 -0800 | [diff] [blame] | 131 | * This method should only be called while the mLock mutex is held (that mutex is grabbed |
| 132 | * by the various destructor() and recycle() methods which call this method). |
| 133 | */ |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 134 | void ResourceCache::deleteResourceReferenceLocked(const void* resource, ResourceReference* ref) { |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 135 | if (ref->destroyed) { |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 136 | switch (ref->resourceType) { |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 137 | case kNinePatch: { |
| 138 | if (Caches::hasInstance()) { |
| 139 | Caches::getInstance().patchCache.removeDeferred((Res_png_9patch*) resource); |
Sangkyu Lee | 36fad8f | 2014-01-09 14:11:57 +0900 | [diff] [blame] | 140 | } else { |
| 141 | // A Res_png_9patch is actually an array of byte that's larger |
| 142 | // than sizeof(Res_png_9patch). It must be freed as an array. |
| 143 | int8_t* patch = (int8_t*) resource; |
| 144 | delete[] patch; |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 145 | } |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 146 | } |
| 147 | break; |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | mCache->removeItem(resource); |
| 151 | delete ref; |
| 152 | } |
| 153 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 154 | }; // namespace uirenderer |
| 155 | }; // namespace android |