• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ResourceCache.h"
18 #include "Caches.h"
19 
20 namespace android {
21 
22 using namespace uirenderer;
23 ANDROID_SINGLETON_STATIC_INSTANCE(ResourceCache);
24 
25 namespace uirenderer {
26 
27 ///////////////////////////////////////////////////////////////////////////////
28 // Resource cache
29 ///////////////////////////////////////////////////////////////////////////////
30 
logCache()31 void ResourceCache::logCache() {
32     ALOGD("ResourceCache: cacheReport:");
33     for (size_t i = 0; i < mCache->size(); ++i) {
34         ResourceReference* ref = mCache->valueAt(i);
35         ALOGD("  ResourceCache: mCache(%zu): resource, ref = 0x%p, 0x%p",
36                 i, mCache->keyAt(i), mCache->valueAt(i));
37         ALOGD("  ResourceCache: mCache(%zu): refCount, destroyed, type = %d, %d, %d",
38                 i, ref->refCount, ref->destroyed, ref->resourceType);
39     }
40 }
41 
ResourceCache()42 ResourceCache::ResourceCache() {
43     Mutex::Autolock _l(mLock);
44     mCache = new KeyedVector<const void*, ResourceReference*>();
45 }
46 
~ResourceCache()47 ResourceCache::~ResourceCache() {
48     Mutex::Autolock _l(mLock);
49     delete mCache;
50 }
51 
lock()52 void ResourceCache::lock() {
53     mLock.lock();
54 }
55 
unlock()56 void ResourceCache::unlock() {
57     mLock.unlock();
58 }
59 
incrementRefcount(void * resource,ResourceType resourceType)60 void ResourceCache::incrementRefcount(void* resource, ResourceType resourceType) {
61     Mutex::Autolock _l(mLock);
62     incrementRefcountLocked(resource, resourceType);
63 }
64 
incrementRefcount(const Res_png_9patch * patchResource)65 void ResourceCache::incrementRefcount(const Res_png_9patch* patchResource) {
66     incrementRefcount((void*) patchResource, kNinePatch);
67 }
68 
incrementRefcountLocked(void * resource,ResourceType resourceType)69 void ResourceCache::incrementRefcountLocked(void* resource, ResourceType resourceType) {
70     ssize_t index = mCache->indexOfKey(resource);
71     ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : nullptr;
72     if (ref == nullptr || mCache->size() == 0) {
73         ref = new ResourceReference(resourceType);
74         mCache->add(resource, ref);
75     }
76     ref->refCount++;
77 }
78 
decrementRefcount(void * resource)79 void ResourceCache::decrementRefcount(void* resource) {
80     Mutex::Autolock _l(mLock);
81     decrementRefcountLocked(resource);
82 }
83 
decrementRefcount(const Res_png_9patch * patchResource)84 void ResourceCache::decrementRefcount(const Res_png_9patch* patchResource) {
85     decrementRefcount((void*) patchResource);
86 }
87 
decrementRefcountLocked(void * resource)88 void ResourceCache::decrementRefcountLocked(void* resource) {
89     ssize_t index = mCache->indexOfKey(resource);
90     ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : nullptr;
91     if (ref == nullptr) {
92         // Should not get here - shouldn't get a call to decrement if we're not yet tracking it
93         return;
94     }
95     ref->refCount--;
96     if (ref->refCount == 0) {
97         deleteResourceReferenceLocked(resource, ref);
98     }
99 }
100 
decrementRefcountLocked(const Res_png_9patch * patchResource)101 void ResourceCache::decrementRefcountLocked(const Res_png_9patch* patchResource) {
102     decrementRefcountLocked((void*) patchResource);
103 }
104 
destructor(Res_png_9patch * resource)105 void ResourceCache::destructor(Res_png_9patch* resource) {
106     Mutex::Autolock _l(mLock);
107     destructorLocked(resource);
108 }
109 
destructorLocked(Res_png_9patch * resource)110 void ResourceCache::destructorLocked(Res_png_9patch* resource) {
111     ssize_t index = mCache->indexOfKey(resource);
112     ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : nullptr;
113     if (ref == nullptr) {
114         // If we're not tracking this resource, just delete it
115         if (Caches::hasInstance()) {
116             Caches::getInstance().patchCache.removeDeferred(resource);
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;
121         }
122         return;
123     }
124     ref->destroyed = true;
125     if (ref->refCount == 0) {
126         deleteResourceReferenceLocked(resource, ref);
127     }
128 }
129 
130 /**
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  */
deleteResourceReferenceLocked(const void * resource,ResourceReference * ref)134 void ResourceCache::deleteResourceReferenceLocked(const void* resource, ResourceReference* ref) {
135     if (ref->destroyed) {
136         switch (ref->resourceType) {
137             case kNinePatch: {
138                 if (Caches::hasInstance()) {
139                     Caches::getInstance().patchCache.removeDeferred((Res_png_9patch*) resource);
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;
145                 }
146             }
147             break;
148         }
149     }
150     mCache->removeItem(resource);
151     delete ref;
152 }
153 
154 }; // namespace uirenderer
155 }; // namespace android
156