epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 11 | #ifndef GrResourceCache_DEFINED |
| 12 | #define GrResourceCache_DEFINED |
| 13 | |
robertphillips@google.com | 5955202 | 2012-08-31 13:07:37 +0000 | [diff] [blame] | 14 | #include "GrConfig.h" |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 15 | #include "GrTypes.h" |
mtklein@google.com | 4c2af74 | 2013-10-21 21:04:06 +0000 | [diff] [blame] | 16 | #include "GrTHashTable.h" |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 17 | #include "GrBinHashKey.h" |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame^] | 18 | #include "SkMessageBus.h" |
bsalomon@google.com | 42619d8 | 2012-12-03 14:54:59 +0000 | [diff] [blame] | 19 | #include "SkTInternalLList.h" |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 20 | |
| 21 | class GrResource; |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 22 | class GrResourceEntry; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 23 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 24 | class GrResourceKey { |
| 25 | public: |
| 26 | enum { |
| 27 | kHashBits = 7, |
| 28 | kHashCount = 1 << kHashBits, |
| 29 | kHashMask = kHashCount - 1 |
| 30 | }; |
| 31 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 32 | static GrCacheID::Domain ScratchDomain() { |
| 33 | static const GrCacheID::Domain gDomain = GrCacheID::GenerateDomain(); |
| 34 | return gDomain; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 35 | } |
| 36 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 37 | /** Uniquely identifies the GrResource subclass in the key to avoid collisions |
| 38 | across resource types. */ |
| 39 | typedef uint8_t ResourceType; |
| 40 | |
| 41 | /** Flags set by the GrResource subclass. */ |
| 42 | typedef uint8_t ResourceFlags; |
| 43 | |
| 44 | /** Generate a unique ResourceType */ |
| 45 | static ResourceType GenerateResourceType(); |
| 46 | |
| 47 | /** Creates a key for resource */ |
| 48 | GrResourceKey(const GrCacheID& id, ResourceType type, ResourceFlags flags) { |
| 49 | this->init(id.getDomain(), id.getKey(), type, flags); |
| 50 | }; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 51 | |
| 52 | GrResourceKey(const GrResourceKey& src) { |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 53 | fKey = src.fKey; |
| 54 | } |
| 55 | |
| 56 | GrResourceKey() { |
| 57 | fKey.fHashedKey.reset(); |
| 58 | } |
| 59 | |
| 60 | void reset(const GrCacheID& id, ResourceType type, ResourceFlags flags) { |
| 61 | this->init(id.getDomain(), id.getKey(), type, flags); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | //!< returns hash value [0..kHashMask] for the key |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 65 | int getHash() const { |
| 66 | return fKey.fHashedKey.getHash() & kHashMask; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 67 | } |
| 68 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 69 | bool isScratch() const { |
| 70 | return ScratchDomain() == |
| 71 | *reinterpret_cast<const GrCacheID::Domain*>(fKey.fHashedKey.getData() + |
| 72 | kCacheIDDomainOffset); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 73 | } |
| 74 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 75 | ResourceType getResourceType() const { |
| 76 | return *reinterpret_cast<const ResourceType*>(fKey.fHashedKey.getData() + |
| 77 | kResourceTypeOffset); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 78 | } |
| 79 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 80 | ResourceFlags getResourceFlags() const { |
| 81 | return *reinterpret_cast<const ResourceFlags*>(fKey.fHashedKey.getData() + |
| 82 | kResourceFlagsOffset); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 83 | } |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 84 | |
| 85 | int compare(const GrResourceKey& other) const { |
| 86 | return fKey.fHashedKey.compare(other.fKey.fHashedKey); |
| 87 | } |
| 88 | |
| 89 | static bool LT(const GrResourceKey& a, const GrResourceKey& b) { |
| 90 | return a.compare(b) < 0; |
| 91 | } |
| 92 | |
| 93 | static bool EQ(const GrResourceKey& a, const GrResourceKey& b) { |
| 94 | return 0 == a.compare(b); |
| 95 | } |
| 96 | |
| 97 | inline static bool LT(const GrResourceEntry& entry, const GrResourceKey& key); |
| 98 | inline static bool EQ(const GrResourceEntry& entry, const GrResourceKey& key); |
| 99 | inline static bool LT(const GrResourceEntry& a, const GrResourceEntry& b); |
| 100 | inline static bool EQ(const GrResourceEntry& a, const GrResourceEntry& b); |
| 101 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 102 | private: |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 103 | enum { |
| 104 | kCacheIDKeyOffset = 0, |
| 105 | kCacheIDDomainOffset = kCacheIDKeyOffset + sizeof(GrCacheID::Key), |
| 106 | kResourceTypeOffset = kCacheIDDomainOffset + sizeof(GrCacheID::Domain), |
| 107 | kResourceFlagsOffset = kResourceTypeOffset + sizeof(ResourceType), |
| 108 | kPadOffset = kResourceFlagsOffset + sizeof(ResourceFlags), |
| 109 | kKeySize = SkAlign4(kPadOffset), |
| 110 | kPadSize = kKeySize - kPadOffset |
| 111 | }; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 112 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 113 | void init(const GrCacheID::Domain domain, |
| 114 | const GrCacheID::Key& key, |
| 115 | ResourceType type, |
| 116 | ResourceFlags flags) { |
| 117 | union { |
| 118 | uint8_t fKey8[kKeySize]; |
| 119 | uint32_t fKey32[kKeySize / 4]; |
| 120 | } keyData; |
skia.committer@gmail.com | 2859eb7 | 2012-12-21 02:01:28 +0000 | [diff] [blame] | 121 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 122 | uint8_t* k = keyData.fKey8; |
| 123 | memcpy(k + kCacheIDKeyOffset, key.fData8, sizeof(GrCacheID::Key)); |
| 124 | memcpy(k + kCacheIDDomainOffset, &domain, sizeof(GrCacheID::Domain)); |
| 125 | memcpy(k + kResourceTypeOffset, &type, sizeof(ResourceType)); |
| 126 | memcpy(k + kResourceFlagsOffset, &flags, sizeof(ResourceFlags)); |
| 127 | memset(k + kPadOffset, 0, kPadSize); |
| 128 | fKey.fHashedKey.setKeyData(keyData.fKey32); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 129 | } |
| 130 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 131 | struct Key; |
| 132 | typedef GrTBinHashKey<Key, kKeySize> HashedKey; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 133 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 134 | struct Key { |
| 135 | int compare(const HashedKey& hashedKey) const { |
sugoi@google.com | ff57afc | 2013-02-25 20:42:44 +0000 | [diff] [blame] | 136 | return fHashedKey.compare(hashedKey); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 137 | } |
skia.committer@gmail.com | 2859eb7 | 2012-12-21 02:01:28 +0000 | [diff] [blame] | 138 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 139 | HashedKey fHashedKey; |
| 140 | }; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 141 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 142 | Key fKey; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 143 | }; |
| 144 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame^] | 145 | // The cache listens for these messages to purge junk resources proactively. |
| 146 | struct GrResourceInvalidatedMessage { |
| 147 | GrResourceKey key; |
| 148 | }; |
| 149 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 150 | /////////////////////////////////////////////////////////////////////////////// |
| 151 | |
| 152 | class GrResourceEntry { |
| 153 | public: |
| 154 | GrResource* resource() const { return fResource; } |
| 155 | const GrResourceKey& key() const { return fKey; } |
| 156 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 157 | #ifdef SK_DEBUG |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 158 | void validate() const; |
| 159 | #else |
| 160 | void validate() const {} |
| 161 | #endif |
| 162 | |
| 163 | private: |
| 164 | GrResourceEntry(const GrResourceKey& key, GrResource* resource); |
| 165 | ~GrResourceEntry(); |
| 166 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 167 | GrResourceKey fKey; |
| 168 | GrResource* fResource; |
| 169 | |
bsalomon@google.com | 42619d8 | 2012-12-03 14:54:59 +0000 | [diff] [blame] | 170 | // we're a linked list |
| 171 | SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrResourceEntry); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 172 | |
| 173 | friend class GrResourceCache; |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 174 | friend class GrDLinkedList; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 177 | bool GrResourceKey::LT(const GrResourceEntry& entry, const GrResourceKey& key) { |
| 178 | return LT(entry.key(), key); |
| 179 | } |
| 180 | |
| 181 | bool GrResourceKey::EQ(const GrResourceEntry& entry, const GrResourceKey& key) { |
| 182 | return EQ(entry.key(), key); |
| 183 | } |
| 184 | |
| 185 | bool GrResourceKey::LT(const GrResourceEntry& a, const GrResourceEntry& b) { |
| 186 | return LT(a.key(), b.key()); |
| 187 | } |
| 188 | |
| 189 | bool GrResourceKey::EQ(const GrResourceEntry& a, const GrResourceEntry& b) { |
| 190 | return EQ(a.key(), b.key()); |
| 191 | } |
| 192 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 193 | /////////////////////////////////////////////////////////////////////////////// |
| 194 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 195 | /** |
| 196 | * Cache of GrResource objects. |
| 197 | * |
| 198 | * These have a corresponding GrResourceKey, built from 128bits identifying the |
| 199 | * resource. |
| 200 | * |
| 201 | * The cache stores the entries in a double-linked list, which is its LRU. |
| 202 | * When an entry is "locked" (i.e. given to the caller), it is moved to the |
| 203 | * head of the list. If/when we must purge some of the entries, we walk the |
| 204 | * list backwards from the tail, since those are the least recently used. |
| 205 | * |
| 206 | * For fast searches, we maintain a sorted array (based on the GrResourceKey) |
| 207 | * which we can bsearch. When a new entry is added, it is inserted into this |
| 208 | * array. |
| 209 | * |
| 210 | * For even faster searches, a hash is computed from the Key. If there is |
| 211 | * a collision between two keys with the same hash, we fall back on the |
| 212 | * bsearch, and update the hash to reflect the most recent Key requested. |
bsalomon@google.com | 76202b8 | 2013-05-10 19:08:22 +0000 | [diff] [blame] | 213 | * |
| 214 | * It is a goal to make the GrResourceCache the central repository and bookkeeper |
| 215 | * of all resources. It should replace the linked list of GrResources that |
| 216 | * GrGpu uses to call abandon/release. |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 217 | */ |
| 218 | class GrResourceCache { |
| 219 | public: |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 220 | GrResourceCache(int maxCount, size_t maxBytes); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 221 | ~GrResourceCache(); |
| 222 | |
| 223 | /** |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 224 | * Return the current resource cache limits. |
| 225 | * |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 226 | * @param maxResource If non-null, returns maximum number of resources |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 227 | * that can be held in the cache. |
| 228 | * @param maxBytes If non-null, returns maximum number of bytes of |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 229 | * gpu memory that can be held in the cache. |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 230 | */ |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 231 | void getLimits(int* maxResources, size_t* maxBytes) const; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 232 | |
| 233 | /** |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 234 | * Specify the resource cache limits. If the current cache exceeds either |
| 235 | * of these, it will be purged (LRU) to keep the cache within these limits. |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 236 | * |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 237 | * @param maxResources The maximum number of resources that can be held in |
| 238 | * the cache. |
| 239 | * @param maxBytes The maximum number of bytes of resource memory that |
| 240 | * can be held in the cache. |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 241 | */ |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 242 | void setLimits(int maxResources, size_t maxResourceBytes); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 243 | |
| 244 | /** |
commit-bot@chromium.org | cae27fe | 2013-07-10 10:14:35 +0000 | [diff] [blame] | 245 | * The callback function used by the cache when it is still over budget |
skia.committer@gmail.com | de2e4e8 | 2013-07-11 07:01:01 +0000 | [diff] [blame] | 246 | * after a purge. The passed in 'data' is the same 'data' handed to |
commit-bot@chromium.org | cae27fe | 2013-07-10 10:14:35 +0000 | [diff] [blame] | 247 | * setOverbudgetCallback. The callback returns true if some resources |
| 248 | * have been freed. |
| 249 | */ |
| 250 | typedef bool (*PFOverbudgetCB)(void* data); |
| 251 | |
| 252 | /** |
| 253 | * Set the callback the cache should use when it is still over budget |
skia.committer@gmail.com | de2e4e8 | 2013-07-11 07:01:01 +0000 | [diff] [blame] | 254 | * after a purge. The 'data' provided here will be passed back to the |
skia.committer@gmail.com | 1f3c738 | 2013-07-20 07:00:58 +0000 | [diff] [blame] | 255 | * callback. Note that the cache will attempt to purge any resources newly |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 256 | * freed by the callback. |
commit-bot@chromium.org | cae27fe | 2013-07-10 10:14:35 +0000 | [diff] [blame] | 257 | */ |
| 258 | void setOverbudgetCallback(PFOverbudgetCB overbudgetCB, void* data) { |
| 259 | fOverbudgetCB = overbudgetCB; |
| 260 | fOverbudgetData = data; |
| 261 | } |
| 262 | |
| 263 | /** |
twiz@google.com | 05e7024 | 2012-01-27 19:12:00 +0000 | [diff] [blame] | 264 | * Returns the number of bytes consumed by cached resources. |
| 265 | */ |
| 266 | size_t getCachedResourceBytes() const { return fEntryBytes; } |
| 267 | |
robertphillips@google.com | 209a114 | 2012-10-31 12:25:21 +0000 | [diff] [blame] | 268 | // For a found or added resource to be completely exclusive to the caller |
| 269 | // both the kNoOtherOwners and kHide flags need to be specified |
| 270 | enum OwnershipFlags { |
| 271 | kNoOtherOwners_OwnershipFlag = 0x1, // found/added resource has no other owners |
| 272 | kHide_OwnershipFlag = 0x2 // found/added resource is hidden from future 'find's |
| 273 | }; |
| 274 | |
twiz@google.com | 05e7024 | 2012-01-27 19:12:00 +0000 | [diff] [blame] | 275 | /** |
robertphillips@google.com | a9b0623 | 2012-08-30 11:06:31 +0000 | [diff] [blame] | 276 | * Search for an entry with the same Key. If found, return it. |
| 277 | * If not found, return null. |
skia.committer@gmail.com | f3dc199 | 2012-11-01 02:01:27 +0000 | [diff] [blame] | 278 | * If ownershipFlags includes kNoOtherOwners and a resource is returned |
robertphillips@google.com | 209a114 | 2012-10-31 12:25:21 +0000 | [diff] [blame] | 279 | * then that resource has no other refs to it. |
| 280 | * If ownershipFlags includes kHide and a resource is returned then that |
| 281 | * resource will not be returned from future 'find' calls until it is |
| 282 | * 'freed' (and recycled) or makeNonExclusive is called. |
| 283 | * For a resource to be completely exclusive to a caller both kNoOtherOwners |
| 284 | * and kHide must be specified. |
robertphillips@google.com | a9b0623 | 2012-08-30 11:06:31 +0000 | [diff] [blame] | 285 | */ |
skia.committer@gmail.com | f3dc199 | 2012-11-01 02:01:27 +0000 | [diff] [blame] | 286 | GrResource* find(const GrResourceKey& key, |
robertphillips@google.com | 209a114 | 2012-10-31 12:25:21 +0000 | [diff] [blame] | 287 | uint32_t ownershipFlags = 0); |
robertphillips@google.com | a9b0623 | 2012-08-30 11:06:31 +0000 | [diff] [blame] | 288 | |
| 289 | /** |
skia.committer@gmail.com | f3dc199 | 2012-11-01 02:01:27 +0000 | [diff] [blame] | 290 | * Add the new resource to the cache (by creating a new cache entry based |
| 291 | * on the provided key and resource). |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 292 | * |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 293 | * Ownership of the resource is transferred to the resource cache, |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 294 | * which will unref() it when it is purged or deleted. |
robertphillips@google.com | 209a114 | 2012-10-31 12:25:21 +0000 | [diff] [blame] | 295 | * |
| 296 | * If ownershipFlags includes kHide, subsequent calls to 'find' will not |
skia.committer@gmail.com | f3dc199 | 2012-11-01 02:01:27 +0000 | [diff] [blame] | 297 | * return 'resource' until it is 'freed' (and recycled) or makeNonExclusive |
robertphillips@google.com | 209a114 | 2012-10-31 12:25:21 +0000 | [diff] [blame] | 298 | * is called. |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 299 | */ |
skia.committer@gmail.com | f3dc199 | 2012-11-01 02:01:27 +0000 | [diff] [blame] | 300 | void addResource(const GrResourceKey& key, |
robertphillips@google.com | 209a114 | 2012-10-31 12:25:21 +0000 | [diff] [blame] | 301 | GrResource* resource, |
| 302 | uint32_t ownershipFlags = 0); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 303 | |
| 304 | /** |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 305 | * Determines if the cache contains an entry matching a key. If a matching |
| 306 | * entry exists but was detached then it will not be found. |
| 307 | */ |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 308 | bool hasKey(const GrResourceKey& key) const { return NULL != fCache.find(key); } |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 309 | |
| 310 | /** |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 311 | * Hide 'entry' so that future searches will not find it. Such |
| 312 | * hidden entries will not be purged. The entry still counts against |
| 313 | * the cache's budget and should be made non-exclusive when exclusive access |
| 314 | * is no longer needed. |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 315 | */ |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 316 | void makeExclusive(GrResourceEntry* entry); |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 317 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 318 | /** |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 319 | * Restore 'entry' so that it can be found by future searches. 'entry' |
| 320 | * will also be purgeable (provided its lock count is now 0.) |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 321 | */ |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 322 | void makeNonExclusive(GrResourceEntry* entry); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 323 | |
| 324 | /** |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 325 | * Remove a resource from the cache and delete it! |
| 326 | */ |
| 327 | void deleteResource(GrResourceEntry* entry); |
| 328 | |
| 329 | /** |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 330 | * Removes every resource in the cache that isn't locked. |
| 331 | */ |
| 332 | void purgeAllUnlocked(); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 333 | |
robertphillips@google.com | 50a035d | 2012-09-07 19:44:33 +0000 | [diff] [blame] | 334 | /** |
| 335 | * Allow cache to purge unused resources to obey resource limitations |
robertphillips@google.com | 9fbcad0 | 2012-09-09 14:44:15 +0000 | [diff] [blame] | 336 | * Note: this entry point will be hidden (again) once totally ref-driven |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 337 | * cache maintenance is implemented. Note that the overbudget callback |
| 338 | * will be called if the initial purge doesn't get the cache under |
| 339 | * its budget. |
robertphillips@google.com | 41d2532 | 2013-07-18 17:12:57 +0000 | [diff] [blame] | 340 | * |
| 341 | * extraCount and extraBytes are added to the current resource allocation |
| 342 | * to make sure enough room is available for future additions (e.g, |
| 343 | * 10MB across 10 textures is about to be added). |
robertphillips@google.com | 50a035d | 2012-09-07 19:44:33 +0000 | [diff] [blame] | 344 | */ |
robertphillips@google.com | 41d2532 | 2013-07-18 17:12:57 +0000 | [diff] [blame] | 345 | void purgeAsNeeded(int extraCount = 0, size_t extraBytes = 0); |
robertphillips@google.com | 50a035d | 2012-09-07 19:44:33 +0000 | [diff] [blame] | 346 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 347 | #ifdef SK_DEBUG |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 348 | void validate() const; |
| 349 | #else |
| 350 | void validate() const {} |
| 351 | #endif |
| 352 | |
robertphillips@google.com | 5955202 | 2012-08-31 13:07:37 +0000 | [diff] [blame] | 353 | #if GR_CACHE_STATS |
robertphillips@google.com | 9fbcad0 | 2012-09-09 14:44:15 +0000 | [diff] [blame] | 354 | void printStats(); |
robertphillips@google.com | 5955202 | 2012-08-31 13:07:37 +0000 | [diff] [blame] | 355 | #endif |
| 356 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 357 | private: |
robertphillips@google.com | 209a114 | 2012-10-31 12:25:21 +0000 | [diff] [blame] | 358 | enum BudgetBehaviors { |
| 359 | kAccountFor_BudgetBehavior, |
| 360 | kIgnore_BudgetBehavior |
| 361 | }; |
| 362 | |
| 363 | void internalDetach(GrResourceEntry*, BudgetBehaviors behavior = kAccountFor_BudgetBehavior); |
| 364 | void attachToHead(GrResourceEntry*, BudgetBehaviors behavior = kAccountFor_BudgetBehavior); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 365 | |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 366 | void removeInvalidResource(GrResourceEntry* entry); |
| 367 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 368 | GrTHashTable<GrResourceEntry, GrResourceKey, 8> fCache; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 369 | |
bsalomon@google.com | 42619d8 | 2012-12-03 14:54:59 +0000 | [diff] [blame] | 370 | // We're an internal doubly linked list |
| 371 | typedef SkTInternalLList<GrResourceEntry> EntryList; |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 372 | EntryList fList; |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 373 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 374 | #ifdef SK_DEBUG |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 375 | // These objects cannot be returned by a search |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 376 | EntryList fExclusiveList; |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 377 | #endif |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 378 | |
| 379 | // our budget, used in purgeAsNeeded() |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 380 | int fMaxCount; |
| 381 | size_t fMaxBytes; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 382 | |
| 383 | // our current stats, related to our budget |
robertphillips@google.com | 5955202 | 2012-08-31 13:07:37 +0000 | [diff] [blame] | 384 | #if GR_CACHE_STATS |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 385 | int fHighWaterEntryCount; |
| 386 | size_t fHighWaterEntryBytes; |
| 387 | int fHighWaterClientDetachedCount; |
| 388 | size_t fHighWaterClientDetachedBytes; |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame] | 389 | #endif |
| 390 | |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 391 | int fEntryCount; |
| 392 | size_t fEntryBytes; |
| 393 | int fClientDetachedCount; |
| 394 | size_t fClientDetachedBytes; |
robertphillips@google.com | 386319e | 2012-06-27 14:59:18 +0000 | [diff] [blame] | 395 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 396 | // prevents recursive purging |
commit-bot@chromium.org | cae27fe | 2013-07-10 10:14:35 +0000 | [diff] [blame] | 397 | bool fPurging; |
| 398 | |
| 399 | PFOverbudgetCB fOverbudgetCB; |
| 400 | void* fOverbudgetData; |
| 401 | |
robertphillips@google.com | 41d2532 | 2013-07-18 17:12:57 +0000 | [diff] [blame] | 402 | void internalPurge(int extraCount, size_t extraBytes); |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 403 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame^] | 404 | // Listen for messages that a resource has been invalidated and purge cached junk proactively. |
| 405 | SkMessageBus<GrResourceInvalidatedMessage>::Inbox fInvalidationInbox; |
| 406 | void purgeInvalidated(); |
| 407 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 408 | #ifdef SK_DEBUG |
bsalomon@google.com | 42619d8 | 2012-12-03 14:54:59 +0000 | [diff] [blame] | 409 | static size_t countBytes(const SkTInternalLList<GrResourceEntry>& list); |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 410 | #endif |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 411 | }; |
| 412 | |
| 413 | /////////////////////////////////////////////////////////////////////////////// |
| 414 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 415 | #ifdef SK_DEBUG |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 416 | class GrAutoResourceCacheValidate { |
| 417 | public: |
| 418 | GrAutoResourceCacheValidate(GrResourceCache* cache) : fCache(cache) { |
| 419 | cache->validate(); |
| 420 | } |
| 421 | ~GrAutoResourceCacheValidate() { |
| 422 | fCache->validate(); |
| 423 | } |
| 424 | private: |
| 425 | GrResourceCache* fCache; |
| 426 | }; |
| 427 | #else |
| 428 | class GrAutoResourceCacheValidate { |
| 429 | public: |
| 430 | GrAutoResourceCacheValidate(GrResourceCache*) {} |
| 431 | }; |
| 432 | #endif |
| 433 | |
| 434 | #endif |