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> |
Derek Sollenberger | 3d4eed7 | 2014-12-04 15:20:29 -0500 | [diff] [blame] | 23 | #include <SkPixelRef.h> |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 24 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 25 | #include <utils/KeyedVector.h> |
John Reck | a35778c7 | 2014-11-06 09:45:10 -0800 | [diff] [blame] | 26 | #include <utils/Singleton.h> |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 27 | |
| 28 | #include <androidfw/ResourceTypes.h> |
| 29 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 30 | namespace android { |
| 31 | namespace uirenderer { |
| 32 | |
Tom Hudson | 2dc236b | 2014-10-15 15:46:42 -0400 | [diff] [blame] | 33 | class Layer; |
| 34 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 35 | /** |
| 36 | * Type of Resource being cached |
| 37 | */ |
| 38 | enum ResourceType { |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 39 | kNinePatch, |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | class ResourceReference { |
| 43 | public: |
Chih-Hung Hsieh | faecb78 | 2016-07-21 11:23:06 -0700 | [diff] [blame] | 44 | explicit ResourceReference(ResourceType type) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 45 | refCount = 0; |
| 46 | destroyed = false; |
| 47 | resourceType = type; |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | int refCount; |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 51 | bool destroyed; |
| 52 | ResourceType resourceType; |
| 53 | }; |
| 54 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 55 | class ANDROID_API ResourceCache : public Singleton<ResourceCache> { |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 56 | ResourceCache(); |
| 57 | ~ResourceCache(); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 58 | |
John Reck | a35778c7 | 2014-11-06 09:45:10 -0800 | [diff] [blame] | 59 | friend class Singleton<ResourceCache>; |
| 60 | |
| 61 | public: |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 62 | /** |
| 63 | * When using these two methods, make sure to only invoke the *Locked() |
| 64 | * variants of increment/decrementRefcount(), recyle() and destructor() |
| 65 | */ |
| 66 | void lock(); |
| 67 | void unlock(); |
| 68 | |
Derek Sollenberger | 3d4eed7 | 2014-12-04 15:20:29 -0500 | [diff] [blame] | 69 | void incrementRefcount(const Res_png_9patch* resource); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 70 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 71 | void decrementRefcount(const Res_png_9patch* resource); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 72 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 73 | void decrementRefcountLocked(const Res_png_9patch* resource); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 74 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 75 | void destructor(Res_png_9patch* resource); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 76 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 77 | void destructorLocked(Res_png_9patch* resource); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 78 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 79 | private: |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 80 | void deleteResourceReferenceLocked(const void* resource, ResourceReference* ref); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 81 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 82 | void incrementRefcount(void* resource, ResourceType resourceType); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 83 | void incrementRefcountLocked(void* resource, ResourceType resourceType); |
| 84 | |
| 85 | void decrementRefcount(void* resource); |
| 86 | void decrementRefcountLocked(void* resource); |
| 87 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 88 | void logCache(); |
Chet Haase | e7d2295 | 2010-11-11 16:30:16 -0800 | [diff] [blame] | 89 | |
| 90 | /** |
| 91 | * Used to increment, decrement, and destroy. Incrementing is generally accessed on the UI |
| 92 | * thread, but destroying resources may be called from the GC thread, the finalizer thread, |
| 93 | * or a reference queue finalization thread. |
| 94 | */ |
| 95 | mutable Mutex mLock; |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 96 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 97 | KeyedVector<const void*, ResourceReference*>* mCache; |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 100 | }; // namespace uirenderer |
| 101 | }; // namespace android |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 102 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 103 | #endif // ANDROID_HWUI_RESOURCE_CACHE_H |