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> |
Tom Hudson | 2dc236b | 2014-10-15 15:46:42 -0400 | [diff] [blame] | 23 | #include <SkPath.h> |
Derek Sollenberger | 3d4eed7 | 2014-12-04 15:20:29 -0500 | [diff] [blame] | 24 | #include <SkPixelRef.h> |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 25 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 26 | #include <utils/KeyedVector.h> |
John Reck | a35778c7 | 2014-11-06 09:45:10 -0800 | [diff] [blame] | 27 | #include <utils/Singleton.h> |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 28 | |
| 29 | #include <androidfw/ResourceTypes.h> |
| 30 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 31 | namespace android { |
| 32 | namespace uirenderer { |
| 33 | |
Tom Hudson | 2dc236b | 2014-10-15 15:46:42 -0400 | [diff] [blame] | 34 | class Layer; |
| 35 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Type of Resource being cached |
| 38 | */ |
| 39 | enum ResourceType { |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 40 | kNinePatch, |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 41 | kPath |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | class ResourceReference { |
| 45 | public: |
| 46 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 47 | ResourceReference(ResourceType type) { |
Derek Sollenberger | 3d4eed7 | 2014-12-04 15:20:29 -0500 | [diff] [blame] | 48 | refCount = 0; destroyed = false; resourceType = type; |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | int refCount; |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 52 | bool destroyed; |
| 53 | ResourceType resourceType; |
| 54 | }; |
| 55 | |
Derek Sollenberger | 3d4eed7 | 2014-12-04 15:20:29 -0500 | [diff] [blame] | 56 | class BitmapKey { |
| 57 | public: |
| 58 | BitmapKey(const SkBitmap* bitmap) |
| 59 | : mRefCount(1) |
| 60 | , mBitmapDimensions(bitmap->dimensions()) |
| 61 | , mPixelRefOrigin(bitmap->pixelRefOrigin()) |
| 62 | , mPixelRefStableID(bitmap->pixelRef()->getStableID()) { } |
| 63 | |
| 64 | void operator=(const BitmapKey& other); |
| 65 | bool operator==(const BitmapKey& other) const; |
| 66 | bool operator<(const BitmapKey& other) const; |
| 67 | |
| 68 | private: |
| 69 | // This constructor is only used by the KeyedVector implementation |
| 70 | BitmapKey() |
| 71 | : mRefCount(-1) |
| 72 | , mBitmapDimensions(SkISize::Make(0,0)) |
| 73 | , mPixelRefOrigin(SkIPoint::Make(0,0)) |
| 74 | , mPixelRefStableID(0) { } |
| 75 | |
| 76 | // reference count of all HWUI object using this bitmap |
| 77 | mutable int mRefCount; |
| 78 | |
| 79 | SkISize mBitmapDimensions; |
| 80 | SkIPoint mPixelRefOrigin; |
| 81 | uint32_t mPixelRefStableID; |
| 82 | |
| 83 | friend class ResourceCache; |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 84 | friend struct android::key_value_pair_t<BitmapKey, SkBitmap*>; |
Derek Sollenberger | 3d4eed7 | 2014-12-04 15:20:29 -0500 | [diff] [blame] | 85 | }; |
| 86 | |
John Reck | a35778c7 | 2014-11-06 09:45:10 -0800 | [diff] [blame] | 87 | class ANDROID_API ResourceCache: public Singleton<ResourceCache> { |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 88 | ResourceCache(); |
| 89 | ~ResourceCache(); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 90 | |
John Reck | a35778c7 | 2014-11-06 09:45:10 -0800 | [diff] [blame] | 91 | friend class Singleton<ResourceCache>; |
| 92 | |
| 93 | public: |
| 94 | |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 95 | /** |
| 96 | * When using these two methods, make sure to only invoke the *Locked() |
| 97 | * variants of increment/decrementRefcount(), recyle() and destructor() |
| 98 | */ |
| 99 | void lock(); |
| 100 | void unlock(); |
| 101 | |
Derek Sollenberger | 3d4eed7 | 2014-12-04 15:20:29 -0500 | [diff] [blame] | 102 | /** |
| 103 | * The cache stores a copy of the provided resource or refs an existing resource |
| 104 | * if the bitmap has previously been inserted and returns the cached copy. |
| 105 | */ |
| 106 | const SkBitmap* insert(const SkBitmap* resource); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 107 | |
Derek Sollenberger | 3d4eed7 | 2014-12-04 15:20:29 -0500 | [diff] [blame] | 108 | void incrementRefcount(const SkPath* resource); |
| 109 | void incrementRefcount(const Res_png_9patch* resource); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 110 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 111 | void decrementRefcount(const SkBitmap* resource); |
| 112 | void decrementRefcount(const SkPath* resource); |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 113 | void decrementRefcount(const Res_png_9patch* resource); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 114 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 115 | void decrementRefcountLocked(const SkBitmap* resource); |
| 116 | void decrementRefcountLocked(const SkPath* resource); |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 117 | void decrementRefcountLocked(const Res_png_9patch* resource); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 118 | |
Chet Haase | 5a7e828 | 2011-02-04 12:50:55 -0800 | [diff] [blame] | 119 | void destructor(SkPath* resource); |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 120 | void destructor(Res_png_9patch* resource); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 121 | |
| 122 | void destructorLocked(SkPath* resource); |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 123 | void destructorLocked(Res_png_9patch* resource); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 124 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 125 | private: |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 126 | void deleteResourceReferenceLocked(const void* resource, ResourceReference* ref); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 127 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 128 | void incrementRefcount(void* resource, ResourceType resourceType); |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 129 | void incrementRefcountLocked(void* resource, ResourceType resourceType); |
| 130 | |
| 131 | void decrementRefcount(void* resource); |
| 132 | void decrementRefcountLocked(void* resource); |
| 133 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 134 | void logCache(); |
Chet Haase | e7d2295 | 2010-11-11 16:30:16 -0800 | [diff] [blame] | 135 | |
| 136 | /** |
| 137 | * Used to increment, decrement, and destroy. Incrementing is generally accessed on the UI |
| 138 | * thread, but destroying resources may be called from the GC thread, the finalizer thread, |
| 139 | * or a reference queue finalization thread. |
| 140 | */ |
| 141 | mutable Mutex mLock; |
Romain Guy | 58ecc20 | 2012-09-07 11:58:36 -0700 | [diff] [blame] | 142 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 143 | KeyedVector<const void*, ResourceReference*>* mCache; |
Derek Sollenberger | 3d4eed7 | 2014-12-04 15:20:29 -0500 | [diff] [blame] | 144 | KeyedVector<BitmapKey, SkBitmap*> mBitmapCache; |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | }; // namespace uirenderer |
| 148 | }; // namespace android |
| 149 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 150 | #endif // ANDROID_HWUI_RESOURCE_CACHE_H |