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 #ifndef ANDROID_HWUI_RESOURCE_CACHE_H 18 #define ANDROID_HWUI_RESOURCE_CACHE_H 19 20 #include <cutils/compiler.h> 21 22 #include <SkBitmap.h> 23 24 #include <utils/KeyedVector.h> 25 26 #include <androidfw/ResourceTypes.h> 27 28 #include "Layer.h" 29 30 namespace android { 31 namespace uirenderer { 32 33 /** 34 * Type of Resource being cached 35 */ 36 enum ResourceType { 37 kBitmap, 38 kNinePatch, 39 kPath, 40 kLayer 41 }; 42 43 class ResourceReference { 44 public: 45 46 ResourceReference() { refCount = 0; recycled = false; destroyed = false;} 47 ResourceReference(ResourceType type) { 48 refCount = 0; recycled = false; destroyed = false; resourceType = type; 49 } 50 51 int refCount; 52 bool recycled; 53 bool destroyed; 54 ResourceType resourceType; 55 }; 56 57 class ANDROID_API ResourceCache { 58 public: 59 ResourceCache(); 60 ~ResourceCache(); 61 62 /** 63 * When using these two methods, make sure to only invoke the *Locked() 64 * variants of increment/decrementRefcount(), recyle() and destructor() 65 */ 66 void lock(); 67 void unlock(); 68 69 void incrementRefcount(const SkPath* resource); 70 void incrementRefcount(const SkBitmap* resource); 71 void incrementRefcount(const Res_png_9patch* resource); 72 void incrementRefcount(Layer* resource); 73 74 void incrementRefcountLocked(const SkPath* resource); 75 void incrementRefcountLocked(const SkBitmap* resource); 76 void incrementRefcountLocked(const Res_png_9patch* resource); 77 void incrementRefcountLocked(Layer* resource); 78 79 void decrementRefcount(const SkBitmap* resource); 80 void decrementRefcount(const SkPath* resource); 81 void decrementRefcount(const Res_png_9patch* resource); 82 void decrementRefcount(Layer* resource); 83 84 void decrementRefcountLocked(const SkBitmap* resource); 85 void decrementRefcountLocked(const SkPath* resource); 86 void decrementRefcountLocked(const Res_png_9patch* resource); 87 void decrementRefcountLocked(Layer* resource); 88 89 void destructor(SkPath* resource); 90 void destructor(const SkBitmap* resource); 91 void destructor(Res_png_9patch* resource); 92 93 void destructorLocked(SkPath* resource); 94 void destructorLocked(const SkBitmap* resource); 95 void destructorLocked(Res_png_9patch* resource); 96 97 bool recycle(SkBitmap* resource); 98 bool recycleLocked(SkBitmap* resource); 99 100 private: 101 void deleteResourceReferenceLocked(const void* resource, ResourceReference* ref); 102 103 void incrementRefcount(void* resource, ResourceType resourceType); 104 void incrementRefcountLocked(void* resource, ResourceType resourceType); 105 106 void decrementRefcount(void* resource); 107 void decrementRefcountLocked(void* resource); 108 109 void logCache(); 110 111 /** 112 * Used to increment, decrement, and destroy. Incrementing is generally accessed on the UI 113 * thread, but destroying resources may be called from the GC thread, the finalizer thread, 114 * or a reference queue finalization thread. 115 */ 116 mutable Mutex mLock; 117 118 KeyedVector<const void*, ResourceReference*>* mCache; 119 }; 120 121 }; // namespace uirenderer 122 }; // namespace android 123 124 #endif // ANDROID_HWUI_RESOURCE_CACHE_H 125