Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 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 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_HWUI_RESOURCE_CACHE_H |
| 18 | #define ANDROID_HWUI_RESOURCE_CACHE_H |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 19 | |
Romain Guy | 7953745 | 2011-10-12 13:48:51 -0700 | [diff] [blame] | 20 | #include <cutils/compiler.h> |
| 21 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 22 | #include <SkBitmap.h> |
Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 23 | #include <SkiaColorFilter.h> |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 24 | #include <SkiaShader.h> |
| 25 | #include <utils/KeyedVector.h> |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 26 | #include "Layer.h" |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | namespace uirenderer { |
| 30 | |
| 31 | /** |
| 32 | * Type of Resource being cached |
| 33 | */ |
| 34 | enum ResourceType { |
| 35 | kBitmap, |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 36 | kShader, |
Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 37 | kColorFilter, |
Chet Haase | 5a7e828 | 2011-02-04 12:50:55 -0800 | [diff] [blame] | 38 | kPath, |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 39 | kLayer |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | class ResourceReference { |
| 43 | public: |
| 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 | |
Romain Guy | 7953745 | 2011-10-12 13:48:51 -0700 | [diff] [blame] | 56 | class ANDROID_API ResourceCache { |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 57 | public: |
| 58 | ResourceCache(); |
| 59 | ~ResourceCache(); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 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 | |
Chet Haase | 5a7e828 | 2011-02-04 12:50:55 -0800 | [diff] [blame] | 68 | void incrementRefcount(SkPath* resource); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 69 | void incrementRefcount(SkBitmap* resource); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 70 | void incrementRefcount(SkiaShader* resource); |
Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 71 | void incrementRefcount(SkiaColorFilter* resource); |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 72 | void incrementRefcount(Layer* resource); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 73 | |
| 74 | void incrementRefcountLocked(SkPath* resource); |
| 75 | void incrementRefcountLocked(SkBitmap* resource); |
| 76 | void incrementRefcountLocked(SkiaShader* resource); |
| 77 | void incrementRefcountLocked(SkiaColorFilter* resource); |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 78 | void incrementRefcountLocked(Layer* resource); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 79 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 80 | void decrementRefcount(SkBitmap* resource); |
Chet Haase | 5a7e828 | 2011-02-04 12:50:55 -0800 | [diff] [blame] | 81 | void decrementRefcount(SkPath* resource); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 82 | void decrementRefcount(SkiaShader* resource); |
Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 83 | void decrementRefcount(SkiaColorFilter* resource); |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 84 | void decrementRefcount(Layer* resource); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 85 | |
| 86 | void decrementRefcountLocked(SkBitmap* resource); |
| 87 | void decrementRefcountLocked(SkPath* resource); |
| 88 | void decrementRefcountLocked(SkiaShader* resource); |
| 89 | void decrementRefcountLocked(SkiaColorFilter* resource); |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 90 | void decrementRefcountLocked(Layer* resource); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 91 | |
Chet Haase | 5a7e828 | 2011-02-04 12:50:55 -0800 | [diff] [blame] | 92 | void destructor(SkPath* resource); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 93 | void destructor(SkBitmap* resource); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 94 | void destructor(SkiaShader* resource); |
Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 95 | void destructor(SkiaColorFilter* resource); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 96 | |
| 97 | void destructorLocked(SkPath* resource); |
| 98 | void destructorLocked(SkBitmap* resource); |
| 99 | void destructorLocked(SkiaShader* resource); |
| 100 | void destructorLocked(SkiaColorFilter* resource); |
| 101 | |
Chet Haase | 547e665 | 2012-10-22 15:07:26 -0700 | [diff] [blame] | 102 | bool recycle(SkBitmap* resource); |
| 103 | bool recycleLocked(SkBitmap* resource); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 104 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 105 | private: |
Romain Guy | 97dc917 | 2012-09-23 17:46:45 -0700 | [diff] [blame] | 106 | void deleteResourceReferenceLocked(void* resource, ResourceReference* ref); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 107 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 108 | void incrementRefcount(void* resource, ResourceType resourceType); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 109 | void incrementRefcountLocked(void* resource, ResourceType resourceType); |
| 110 | |
| 111 | void decrementRefcount(void* resource); |
| 112 | void decrementRefcountLocked(void* resource); |
| 113 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 114 | void logCache(); |
Chet Haase | e7d2295 | 2010-11-11 16:30:16 -0800 | [diff] [blame] | 115 | |
| 116 | /** |
| 117 | * Used to increment, decrement, and destroy. Incrementing is generally accessed on the UI |
| 118 | * thread, but destroying resources may be called from the GC thread, the finalizer thread, |
| 119 | * or a reference queue finalization thread. |
| 120 | */ |
| 121 | mutable Mutex mLock; |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 122 | |
| 123 | KeyedVector<void*, ResourceReference*>* mCache; |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | }; // namespace uirenderer |
| 127 | }; // namespace android |
| 128 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 129 | #endif // ANDROID_HWUI_RESOURCE_CACHE_H |