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