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