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> |
| 26 | |
| 27 | namespace android { |
| 28 | namespace uirenderer { |
| 29 | |
| 30 | /** |
| 31 | * Type of Resource being cached |
| 32 | */ |
| 33 | enum ResourceType { |
| 34 | kBitmap, |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 35 | kShader, |
Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 36 | kColorFilter, |
Chet Haase | 5a7e828 | 2011-02-04 12:50:55 -0800 | [diff] [blame] | 37 | kPath, |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | class ResourceReference { |
| 41 | public: |
| 42 | |
| 43 | ResourceReference() { refCount = 0; recycled = false; destroyed = false;} |
| 44 | ResourceReference(ResourceType type) { |
| 45 | refCount = 0; recycled = false; destroyed = false; resourceType = type; |
| 46 | } |
| 47 | |
| 48 | int refCount; |
| 49 | bool recycled; |
| 50 | bool destroyed; |
| 51 | ResourceType resourceType; |
| 52 | }; |
| 53 | |
Romain Guy | 7953745 | 2011-10-12 13:48:51 -0700 | [diff] [blame] | 54 | class ANDROID_API ResourceCache { |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 55 | public: |
| 56 | ResourceCache(); |
| 57 | ~ResourceCache(); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame^] | 58 | |
| 59 | /** |
| 60 | * When using these two methods, make sure to only invoke the *Locked() |
| 61 | * variants of increment/decrementRefcount(), recyle() and destructor() |
| 62 | */ |
| 63 | void lock(); |
| 64 | void unlock(); |
| 65 | |
Chet Haase | 5a7e828 | 2011-02-04 12:50:55 -0800 | [diff] [blame] | 66 | void incrementRefcount(SkPath* resource); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 67 | void incrementRefcount(SkBitmap* resource); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 68 | void incrementRefcount(SkiaShader* resource); |
Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 69 | void incrementRefcount(SkiaColorFilter* resource); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame^] | 70 | |
| 71 | void incrementRefcountLocked(SkPath* resource); |
| 72 | void incrementRefcountLocked(SkBitmap* resource); |
| 73 | void incrementRefcountLocked(SkiaShader* resource); |
| 74 | void incrementRefcountLocked(SkiaColorFilter* resource); |
| 75 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 76 | void decrementRefcount(SkBitmap* resource); |
Chet Haase | 5a7e828 | 2011-02-04 12:50:55 -0800 | [diff] [blame] | 77 | void decrementRefcount(SkPath* resource); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 78 | void decrementRefcount(SkiaShader* resource); |
Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 79 | void decrementRefcount(SkiaColorFilter* resource); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame^] | 80 | |
| 81 | void decrementRefcountLocked(SkBitmap* resource); |
| 82 | void decrementRefcountLocked(SkPath* resource); |
| 83 | void decrementRefcountLocked(SkiaShader* resource); |
| 84 | void decrementRefcountLocked(SkiaColorFilter* resource); |
| 85 | |
Chet Haase | 5a7e828 | 2011-02-04 12:50:55 -0800 | [diff] [blame] | 86 | void destructor(SkPath* resource); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 87 | void destructor(SkBitmap* resource); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 88 | void destructor(SkiaShader* resource); |
Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 89 | void destructor(SkiaColorFilter* resource); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame^] | 90 | |
| 91 | void destructorLocked(SkPath* resource); |
| 92 | void destructorLocked(SkBitmap* resource); |
| 93 | void destructorLocked(SkiaShader* resource); |
| 94 | void destructorLocked(SkiaColorFilter* resource); |
| 95 | |
| 96 | void recycle(SkBitmap* resource); |
| 97 | void recycleLocked(SkBitmap* resource); |
| 98 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 99 | private: |
| 100 | void deleteResourceReference(void* resource, ResourceReference* ref); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame^] | 101 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 102 | void incrementRefcount(void* resource, ResourceType resourceType); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame^] | 103 | void incrementRefcountLocked(void* resource, ResourceType resourceType); |
| 104 | |
| 105 | void decrementRefcount(void* resource); |
| 106 | void decrementRefcountLocked(void* resource); |
| 107 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 108 | void logCache(); |
Chet Haase | e7d2295 | 2010-11-11 16:30:16 -0800 | [diff] [blame] | 109 | |
| 110 | /** |
| 111 | * Used to increment, decrement, and destroy. Incrementing is generally accessed on the UI |
| 112 | * thread, but destroying resources may be called from the GC thread, the finalizer thread, |
| 113 | * or a reference queue finalization thread. |
| 114 | */ |
| 115 | mutable Mutex mLock; |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame^] | 116 | |
| 117 | KeyedVector<void*, ResourceReference*>* mCache; |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | }; // namespace uirenderer |
| 121 | }; // namespace android |
| 122 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 123 | #endif // ANDROID_HWUI_RESOURCE_CACHE_H |