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