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 | |
| 14 | #include "GrTypes.h" |
| 15 | #include "GrTHashCache.h" |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 16 | #include "SkTDLinkedList.h" |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 17 | |
| 18 | class GrResource; |
| 19 | |
| 20 | // return true if a<b, or false if b<a |
| 21 | // |
| 22 | #define RET_IF_LT_OR_GT(a, b) \ |
| 23 | do { \ |
| 24 | if ((a) < (b)) { \ |
| 25 | return true; \ |
| 26 | } \ |
| 27 | if ((b) < (a)) { \ |
| 28 | return false; \ |
| 29 | } \ |
| 30 | } while (0) |
| 31 | |
| 32 | /** |
| 33 | * Helper class for GrResourceCache, the Key is used to identify src data for |
| 34 | * a resource. It is identified by 2 32bit data fields which can hold any |
| 35 | * data (uninterpreted by the cache) and a width/height. |
| 36 | */ |
| 37 | class GrResourceKey { |
| 38 | public: |
| 39 | enum { |
| 40 | kHashBits = 7, |
| 41 | kHashCount = 1 << kHashBits, |
| 42 | kHashMask = kHashCount - 1 |
| 43 | }; |
| 44 | |
| 45 | GrResourceKey(uint32_t p0, uint32_t p1, uint32_t p2, uint32_t p3) { |
| 46 | fP[0] = p0; |
| 47 | fP[1] = p1; |
| 48 | fP[2] = p2; |
| 49 | fP[3] = p3; |
| 50 | this->computeHashIndex(); |
| 51 | } |
| 52 | |
| 53 | GrResourceKey(uint32_t v[4]) { |
| 54 | memcpy(fP, v, 4 * sizeof(uint32_t)); |
| 55 | this->computeHashIndex(); |
| 56 | } |
| 57 | |
| 58 | GrResourceKey(const GrResourceKey& src) { |
| 59 | memcpy(fP, src.fP, 4 * sizeof(uint32_t)); |
| 60 | #if GR_DEBUG |
| 61 | this->computeHashIndex(); |
| 62 | GrAssert(fHashIndex == src.fHashIndex); |
| 63 | #endif |
| 64 | fHashIndex = src.fHashIndex; |
| 65 | } |
| 66 | |
| 67 | //!< returns hash value [0..kHashMask] for the key |
| 68 | int hashIndex() const { return fHashIndex; } |
| 69 | |
| 70 | friend bool operator==(const GrResourceKey& a, const GrResourceKey& b) { |
| 71 | GR_DEBUGASSERT(-1 != a.fHashIndex && -1 != b.fHashIndex); |
| 72 | return 0 == memcmp(a.fP, b.fP, 4 * sizeof(uint32_t)); |
| 73 | } |
| 74 | |
| 75 | friend bool operator!=(const GrResourceKey& a, const GrResourceKey& b) { |
| 76 | GR_DEBUGASSERT(-1 != a.fHashIndex && -1 != b.fHashIndex); |
| 77 | return !(a == b); |
| 78 | } |
| 79 | |
| 80 | friend bool operator<(const GrResourceKey& a, const GrResourceKey& b) { |
| 81 | RET_IF_LT_OR_GT(a.fP[0], b.fP[0]); |
| 82 | RET_IF_LT_OR_GT(a.fP[1], b.fP[1]); |
| 83 | RET_IF_LT_OR_GT(a.fP[2], b.fP[2]); |
| 84 | return a.fP[3] < b.fP[3]; |
| 85 | } |
| 86 | |
| 87 | uint32_t getValue32(int i) const { |
| 88 | GrAssert(i >=0 && i < 4); |
| 89 | return fP[i]; |
| 90 | } |
| 91 | private: |
| 92 | |
| 93 | static uint32_t rol(uint32_t x) { |
| 94 | return (x >> 24) | (x << 8); |
| 95 | } |
| 96 | static uint32_t ror(uint32_t x) { |
| 97 | return (x >> 8) | (x << 24); |
| 98 | } |
| 99 | static uint32_t rohalf(uint32_t x) { |
| 100 | return (x >> 16) | (x << 16); |
| 101 | } |
| 102 | |
| 103 | void computeHashIndex() { |
| 104 | uint32_t hash = fP[0] ^ rol(fP[1]) ^ ror(fP[2]) ^ rohalf(fP[3]); |
| 105 | // this way to mix and reduce hash to its index may have to change |
| 106 | // depending on how many bits we allocate to the index |
| 107 | hash ^= hash >> 16; |
| 108 | hash ^= hash >> 8; |
| 109 | fHashIndex = hash & kHashMask; |
| 110 | } |
| 111 | |
| 112 | uint32_t fP[4]; |
| 113 | |
| 114 | // this is computed from the fP... fields |
| 115 | int fHashIndex; |
| 116 | |
| 117 | friend class GrContext; |
| 118 | }; |
| 119 | |
| 120 | /////////////////////////////////////////////////////////////////////////////// |
| 121 | |
| 122 | class GrResourceEntry { |
| 123 | public: |
| 124 | GrResource* resource() const { return fResource; } |
| 125 | const GrResourceKey& key() const { return fKey; } |
| 126 | |
| 127 | #if GR_DEBUG |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 128 | void validate() const; |
| 129 | #else |
| 130 | void validate() const {} |
| 131 | #endif |
| 132 | |
| 133 | private: |
| 134 | GrResourceEntry(const GrResourceKey& key, GrResource* resource); |
| 135 | ~GrResourceEntry(); |
| 136 | |
| 137 | bool isLocked() const { return fLockCount != 0; } |
| 138 | void lock() { ++fLockCount; } |
| 139 | void unlock() { |
| 140 | GrAssert(fLockCount > 0); |
| 141 | --fLockCount; |
| 142 | } |
| 143 | |
| 144 | GrResourceKey fKey; |
| 145 | GrResource* fResource; |
| 146 | |
| 147 | // track if we're in use, used when we need to purge |
| 148 | // we only purge unlocked entries |
| 149 | int fLockCount; |
| 150 | |
| 151 | // we're a dlinklist |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 152 | SK_DEFINE_DLINKEDLIST_INTERFACE(GrResourceEntry); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 153 | |
| 154 | friend class GrResourceCache; |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 155 | friend class GrDLinkedList; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | /////////////////////////////////////////////////////////////////////////////// |
| 159 | |
| 160 | #include "GrTHashCache.h" |
| 161 | |
| 162 | /** |
| 163 | * Cache of GrResource objects. |
| 164 | * |
| 165 | * These have a corresponding GrResourceKey, built from 128bits identifying the |
| 166 | * resource. |
| 167 | * |
| 168 | * The cache stores the entries in a double-linked list, which is its LRU. |
| 169 | * When an entry is "locked" (i.e. given to the caller), it is moved to the |
| 170 | * head of the list. If/when we must purge some of the entries, we walk the |
| 171 | * list backwards from the tail, since those are the least recently used. |
| 172 | * |
| 173 | * For fast searches, we maintain a sorted array (based on the GrResourceKey) |
| 174 | * which we can bsearch. When a new entry is added, it is inserted into this |
| 175 | * array. |
| 176 | * |
| 177 | * For even faster searches, a hash is computed from the Key. If there is |
| 178 | * a collision between two keys with the same hash, we fall back on the |
| 179 | * bsearch, and update the hash to reflect the most recent Key requested. |
| 180 | */ |
| 181 | class GrResourceCache { |
| 182 | public: |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 183 | GrResourceCache(int maxCount, size_t maxBytes); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 184 | ~GrResourceCache(); |
| 185 | |
| 186 | /** |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 187 | * Return the current resource cache limits. |
| 188 | * |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame^] | 189 | * @param maxResource If non-null, returns maximum number of resources |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 190 | * that can be held in the cache. |
| 191 | * @param maxBytes If non-null, returns maximum number of bytes of |
| 192 | * gpu memory that can be held in the cache. |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 193 | */ |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 194 | void getLimits(int* maxResources, size_t* maxBytes) const; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 195 | |
| 196 | /** |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 197 | * Specify the resource cache limits. If the current cache exceeds either |
| 198 | * 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] | 199 | * |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 200 | * @param maxResources The maximum number of resources that can be held in |
| 201 | * the cache. |
| 202 | * @param maxBytes The maximum number of bytes of resource memory that |
| 203 | * can be held in the cache. |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 204 | */ |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 205 | void setLimits(int maxResource, size_t maxResourceBytes); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 206 | |
| 207 | /** |
twiz@google.com | 05e7024 | 2012-01-27 19:12:00 +0000 | [diff] [blame] | 208 | * Returns the number of bytes consumed by cached resources. |
| 209 | */ |
| 210 | size_t getCachedResourceBytes() const { return fEntryBytes; } |
| 211 | |
| 212 | /** |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 213 | * Controls whether locks should be nestable or not. |
| 214 | */ |
| 215 | enum LockType { |
| 216 | kNested_LockType, |
| 217 | kSingle_LockType, |
| 218 | }; |
| 219 | |
| 220 | /** |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 221 | * Search for an entry with the same Key. If found, "lock" it and return it. |
| 222 | * If not found, return null. |
| 223 | */ |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 224 | GrResource* findAndLock(const GrResourceKey&, LockType style); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 225 | |
| 226 | /** |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame^] | 227 | * Create a new cache entry, based on the provided key and resource, and |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 228 | * return it. |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 229 | * |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame^] | 230 | * Ownership of the resource is transferred to the resource cache, |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 231 | * which will unref() it when it is purged or deleted. |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 232 | */ |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 233 | void createAndLock(const GrResourceKey&, GrResource*); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 234 | |
| 235 | /** |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 236 | * Create a new cache entry, based on the provided key and resource. |
| 237 | * |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame^] | 238 | * Ownership of the resource is transferred to the resource cache, |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 239 | * which will unref() it when it is purged or deleted. |
| 240 | * |
| 241 | * Currently this entry point is only intended for textures "detached" |
| 242 | * from an AutoScratchTexture object. |
| 243 | */ |
| 244 | void attach(const GrResourceKey& key, GrResource* resource); |
| 245 | |
| 246 | /** |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 247 | * Determines if the cache contains an entry matching a key. If a matching |
| 248 | * entry exists but was detached then it will not be found. |
| 249 | */ |
| 250 | bool hasKey(const GrResourceKey& key) const; |
| 251 | |
| 252 | /** |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 253 | * Hide 'entry' so that future searches will not find it. Such |
| 254 | * hidden entries will not be purged. The entry still counts against |
| 255 | * the cache's budget and should be made non-exclusive when exclusive access |
| 256 | * is no longer needed. |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 257 | */ |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 258 | void makeExclusive(GrResourceEntry* entry); |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 259 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 260 | /** |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 261 | * Restore 'entry' so that it can be found by future searches. 'entry' |
| 262 | * will also be purgeable (provided its lock count is now 0.) |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 263 | */ |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 264 | void makeNonExclusive(GrResourceEntry* entry); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 265 | |
| 266 | /** |
| 267 | * When done with an entry, call unlock(entry) on it, which returns it to |
| 268 | * a purgable state. |
| 269 | */ |
| 270 | void unlock(GrResourceEntry*); |
| 271 | |
| 272 | void removeAll(); |
| 273 | |
| 274 | #if GR_DEBUG |
| 275 | void validate() const; |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame] | 276 | void printStats() const; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 277 | #else |
| 278 | void validate() const {} |
| 279 | #endif |
| 280 | |
| 281 | private: |
| 282 | void internalDetach(GrResourceEntry*, bool); |
| 283 | void attachToHead(GrResourceEntry*, bool); |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 284 | void purgeAsNeeded(); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 285 | |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 286 | void removeInvalidResource(GrResourceEntry* entry); |
| 287 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 288 | class Key; |
| 289 | GrTHashTable<GrResourceEntry, Key, 8> fCache; |
| 290 | |
| 291 | // manage the dlink list |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 292 | SkTDLinkedList<GrResourceEntry> fList; |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 293 | |
| 294 | #if GR_DEBUG |
| 295 | // These objects cannot be returned by a search |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 296 | SkTDLinkedList<GrResourceEntry> fExclusiveList; |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 297 | #endif |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 298 | |
| 299 | // our budget, used in purgeAsNeeded() |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 300 | int fMaxCount; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 301 | size_t fMaxBytes; |
| 302 | |
| 303 | // our current stats, related to our budget |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame] | 304 | #if GR_DEBUG |
| 305 | int fHighWaterEntryCount; |
| 306 | int fHighWaterUnlockedEntryCount; |
| 307 | size_t fHighWaterEntryBytes; |
| 308 | int fHighWaterClientDetachedCount; |
| 309 | size_t fHighWaterClientDetachedBytes; |
| 310 | #endif |
| 311 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 312 | int fEntryCount; |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 313 | int fUnlockedEntryCount; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 314 | size_t fEntryBytes; |
| 315 | int fClientDetachedCount; |
| 316 | size_t fClientDetachedBytes; |
robertphillips@google.com | 386319e | 2012-06-27 14:59:18 +0000 | [diff] [blame] | 317 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 318 | // prevents recursive purging |
| 319 | bool fPurging; |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 320 | |
| 321 | GrResourceEntry* create(const GrResourceKey& key, |
| 322 | GrResource* resource, |
robertphillips@google.com | 386319e | 2012-06-27 14:59:18 +0000 | [diff] [blame] | 323 | bool lock, |
| 324 | bool clientReattach); |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 325 | |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 326 | #if GR_DEBUG |
robertphillips@google.com | a5c43a5 | 2012-08-23 11:24:02 +0000 | [diff] [blame] | 327 | static size_t countBytes(const SkTDLinkedList<GrResourceEntry>& list); |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 328 | #endif |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 329 | }; |
| 330 | |
| 331 | /////////////////////////////////////////////////////////////////////////////// |
| 332 | |
| 333 | #if GR_DEBUG |
| 334 | class GrAutoResourceCacheValidate { |
| 335 | public: |
| 336 | GrAutoResourceCacheValidate(GrResourceCache* cache) : fCache(cache) { |
| 337 | cache->validate(); |
| 338 | } |
| 339 | ~GrAutoResourceCacheValidate() { |
| 340 | fCache->validate(); |
| 341 | } |
| 342 | private: |
| 343 | GrResourceCache* fCache; |
| 344 | }; |
| 345 | #else |
| 346 | class GrAutoResourceCacheValidate { |
| 347 | public: |
| 348 | GrAutoResourceCacheValidate(GrResourceCache*) {} |
| 349 | }; |
| 350 | #endif |
| 351 | |
| 352 | #endif |