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> |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 23 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 24 | #include <utils/KeyedVector.h> |
John Reck | a35778c7 | 2014-11-06 09:45:10 -0800 | [diff] [blame] | 25 | #include <utils/Singleton.h> |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 26 | |
| 27 | #include <androidfw/ResourceTypes.h> |
| 28 | |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 29 | #include "Layer.h" |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | namespace uirenderer { |
| 33 | |
| 34 | /** |
| 35 | * Type of Resource being cached |
| 36 | */ |
| 37 | enum ResourceType { |
| 38 | kBitmap, |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 39 | kNinePatch, |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 40 | kPath |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | class ResourceReference { |
| 44 | public: |
| 45 | |
| 46 | ResourceReference() { refCount = 0; recycled = false; destroyed = false;} |
| 47 | ResourceReference(ResourceType type) { |
| 48 | refCount = 0; recycled = false; destroyed = false; resourceType = type; |
| 49 | } |
| 50 | |
| 51 | int refCount; |
| 52 | bool recycled; |
| 53 | bool destroyed; |
| 54 | ResourceType resourceType; |
| 55 | }; |
| 56 | |
John Reck | a35778c7 | 2014-11-06 09:45:10 -0800 | [diff] [blame] | 57 | class ANDROID_API ResourceCache: public Singleton<ResourceCache> { |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 58 | ResourceCache(); |
| 59 | ~ResourceCache(); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 60 | |
John Reck | a35778c7 | 2014-11-06 09:45:10 -0800 | [diff] [blame] | 61 | friend class Singleton<ResourceCache>; |
| 62 | |
| 63 | public: |
| 64 | |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 65 | /** |
| 66 | * When using these two methods, make sure to only invoke the *Locked() |
| 67 | * variants of increment/decrementRefcount(), recyle() and destructor() |
| 68 | */ |
| 69 | void lock(); |
| 70 | void unlock(); |
| 71 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 72 | void incrementRefcount(const SkPath* resource); |
| 73 | void incrementRefcount(const SkBitmap* resource); |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 74 | void incrementRefcount(const Res_png_9patch* resource); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 75 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 76 | void incrementRefcountLocked(const SkPath* resource); |
| 77 | void incrementRefcountLocked(const SkBitmap* resource); |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 78 | void incrementRefcountLocked(const Res_png_9patch* resource); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 79 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 80 | void decrementRefcount(const SkBitmap* resource); |
| 81 | void decrementRefcount(const SkPath* resource); |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 82 | void decrementRefcount(const Res_png_9patch* resource); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 83 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 84 | void decrementRefcountLocked(const SkBitmap* resource); |
| 85 | void decrementRefcountLocked(const SkPath* resource); |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 86 | void decrementRefcountLocked(const Res_png_9patch* resource); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 87 | |
Chet Haase | 5a7e828 | 2011-02-04 12:50:55 -0800 | [diff] [blame] | 88 | void destructor(SkPath* resource); |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 89 | void destructor(const SkBitmap* resource); |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 90 | void destructor(Res_png_9patch* resource); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 91 | |
| 92 | void destructorLocked(SkPath* resource); |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 93 | void destructorLocked(const SkBitmap* resource); |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 94 | void destructorLocked(Res_png_9patch* resource); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 95 | |
Chet Haase | 547e665 | 2012-10-22 15:07:26 -0700 | [diff] [blame] | 96 | bool recycle(SkBitmap* resource); |
| 97 | bool recycleLocked(SkBitmap* resource); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 98 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 99 | private: |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 100 | void deleteResourceReferenceLocked(const 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 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 117 | KeyedVector<const 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 |