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 <SkBitmap.h> 21 #include <SkiaColorFilter.h> 22 #include <SkiaShader.h> 23 #include <utils/KeyedVector.h> 24 25 namespace android { 26 namespace uirenderer { 27 28 /** 29 * Type of Resource being cached 30 */ 31 enum ResourceType { 32 kBitmap, 33 kShader, 34 kColorFilter, 35 kPath, 36 }; 37 38 class ResourceReference { 39 public: 40 ResourceReference()41 ResourceReference() { refCount = 0; recycled = false; destroyed = false;} ResourceReference(ResourceType type)42 ResourceReference(ResourceType type) { 43 refCount = 0; recycled = false; destroyed = false; resourceType = type; 44 } 45 46 int refCount; 47 bool recycled; 48 bool destroyed; 49 ResourceType resourceType; 50 }; 51 52 class ResourceCache { 53 KeyedVector<void *, ResourceReference *>* mCache; 54 public: 55 ResourceCache(); 56 ~ResourceCache(); 57 void incrementRefcount(SkPath* resource); 58 void incrementRefcount(SkBitmap* resource); 59 void incrementRefcount(SkiaShader* resource); 60 void incrementRefcount(SkiaColorFilter* resource); 61 void incrementRefcount(const void* resource, ResourceType resourceType); 62 void decrementRefcount(void* resource); 63 void decrementRefcount(SkBitmap* resource); 64 void decrementRefcount(SkPath* resource); 65 void decrementRefcount(SkiaShader* resource); 66 void decrementRefcount(SkiaColorFilter* resource); 67 void recycle(SkBitmap* resource); 68 void destructor(SkPath* resource); 69 void destructor(SkBitmap* resource); 70 void destructor(SkiaShader* resource); 71 void destructor(SkiaColorFilter* resource); 72 private: 73 void deleteResourceReference(void* resource, ResourceReference* ref); 74 void incrementRefcount(void* resource, ResourceType resourceType); 75 void logCache(); 76 77 /** 78 * Used to increment, decrement, and destroy. Incrementing is generally accessed on the UI 79 * thread, but destroying resources may be called from the GC thread, the finalizer thread, 80 * or a reference queue finalization thread. 81 */ 82 mutable Mutex mLock; 83 }; 84 85 }; // namespace uirenderer 86 }; // namespace android 87 88 #endif // ANDROID_HWUI_RESOURCE_CACHE_H 89