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