epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2010 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. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +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 | #include "GrResourceCache.h" |
| 12 | #include "GrResource.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 13 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 14 | GrResourceEntry::GrResourceEntry(const GrResourceKey& key, GrResource* resource) |
| 15 | : fKey(key), fResource(resource) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 16 | fLockCount = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 17 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 18 | // we assume ownership of the resource, and will unref it when we die |
| 19 | GrAssert(resource); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 20 | } |
| 21 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 22 | GrResourceEntry::~GrResourceEntry() { |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 23 | fResource->setCacheEntry(NULL); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 24 | fResource->unref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | #if GR_DEBUG |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 28 | void GrResourceEntry::validate() const { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 29 | GrAssert(fLockCount >= 0); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 30 | GrAssert(fResource); |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 31 | GrAssert(fResource->getCacheEntry() == this); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 32 | fResource->validate(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 33 | } |
| 34 | #endif |
| 35 | |
| 36 | /////////////////////////////////////////////////////////////////////////////// |
| 37 | |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 38 | class GrResourceCache::Key { |
| 39 | typedef GrResourceEntry T; |
| 40 | |
| 41 | const GrResourceKey& fKey; |
| 42 | public: |
| 43 | Key(const GrResourceKey& key) : fKey(key) {} |
| 44 | |
| 45 | uint32_t getHash() const { return fKey.hashIndex(); } |
| 46 | |
| 47 | static bool LT(const T& entry, const Key& key) { |
| 48 | return entry.key() < key.fKey; |
| 49 | } |
| 50 | static bool EQ(const T& entry, const Key& key) { |
| 51 | return entry.key() == key.fKey; |
| 52 | } |
| 53 | #if GR_DEBUG |
| 54 | static uint32_t GetHash(const T& entry) { |
| 55 | return entry.key().hashIndex(); |
| 56 | } |
| 57 | static bool LT(const T& a, const T& b) { |
| 58 | return a.key() < b.key(); |
| 59 | } |
| 60 | static bool EQ(const T& a, const T& b) { |
| 61 | return a.key() == b.key(); |
| 62 | } |
| 63 | #endif |
| 64 | }; |
| 65 | |
| 66 | /////////////////////////////////////////////////////////////////////////////// |
| 67 | |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 68 | GrResourceCache::GrResourceCache(int maxCount, size_t maxBytes) : |
| 69 | fMaxCount(maxCount), |
| 70 | fMaxBytes(maxBytes) { |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame] | 71 | #if GR_DEBUG |
| 72 | fHighWaterEntryCount = 0; |
| 73 | fHighWaterUnlockedEntryCount = 0; |
| 74 | fHighWaterEntryBytes = 0; |
| 75 | fHighWaterClientDetachedCount = 0; |
| 76 | fHighWaterClientDetachedBytes = 0; |
| 77 | #endif |
| 78 | |
| 79 | fEntryCount = 0; |
| 80 | fUnlockedEntryCount = 0; |
| 81 | fEntryBytes = 0; |
| 82 | fClientDetachedCount = 0; |
| 83 | fClientDetachedBytes = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 84 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 85 | fPurging = false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 86 | } |
| 87 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 88 | GrResourceCache::~GrResourceCache() { |
| 89 | GrAutoResourceCacheValidate atcv(this); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 90 | |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 91 | EntryList::Iter iter; |
| 92 | |
| 93 | // Unlike the removeAll, here we really remove everything, including locked resources. |
| 94 | while (GrResourceEntry* entry = fList.head()) { |
| 95 | GrAutoResourceCacheValidate atcv(this); |
| 96 | |
| 97 | // remove from our cache |
| 98 | fCache.remove(entry->fKey, entry); |
| 99 | |
| 100 | // remove from our llist |
| 101 | this->internalDetach(entry, false); |
| 102 | |
| 103 | delete entry; |
| 104 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 105 | } |
| 106 | |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 107 | void GrResourceCache::getLimits(int* maxResources, size_t* maxResourceBytes) const{ |
| 108 | if (maxResources) { |
| 109 | *maxResources = fMaxCount; |
| 110 | } |
| 111 | if (maxResourceBytes) { |
| 112 | *maxResourceBytes = fMaxBytes; |
| 113 | } |
| 114 | } |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 115 | |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 116 | void GrResourceCache::setLimits(int maxResources, size_t maxResourceBytes) { |
| 117 | bool smaller = (maxResources < fMaxCount) || (maxResourceBytes < fMaxBytes); |
| 118 | |
| 119 | fMaxCount = maxResources; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 120 | fMaxBytes = maxResourceBytes; |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 121 | |
| 122 | if (smaller) { |
| 123 | this->purgeAsNeeded(); |
| 124 | } |
| 125 | } |
| 126 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 127 | void GrResourceCache::internalDetach(GrResourceEntry* entry, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 128 | bool clientDetach) { |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 129 | fList.remove(entry); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 130 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 131 | if (!entry->isLocked()) { |
| 132 | --fUnlockedEntryCount; |
| 133 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 134 | |
| 135 | // update our stats |
| 136 | if (clientDetach) { |
| 137 | fClientDetachedCount += 1; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 138 | fClientDetachedBytes += entry->resource()->sizeInBytes(); |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame] | 139 | |
| 140 | #if GR_DEBUG |
| 141 | if (fHighWaterClientDetachedCount < fClientDetachedCount) { |
| 142 | fHighWaterClientDetachedCount = fClientDetachedCount; |
| 143 | } |
| 144 | if (fHighWaterClientDetachedBytes < fClientDetachedBytes) { |
| 145 | fHighWaterClientDetachedBytes = fClientDetachedBytes; |
| 146 | } |
| 147 | #endif |
| 148 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 149 | } else { |
| 150 | fEntryCount -= 1; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 151 | fEntryBytes -= entry->resource()->sizeInBytes(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 155 | void GrResourceCache::attachToHead(GrResourceEntry* entry, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 156 | bool clientReattach) { |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 157 | fList.addToHead(entry); |
| 158 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 159 | if (!entry->isLocked()) { |
| 160 | ++fUnlockedEntryCount; |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame] | 161 | #if GR_DEBUG |
| 162 | if (fHighWaterUnlockedEntryCount < fUnlockedEntryCount) { |
| 163 | fHighWaterUnlockedEntryCount = fUnlockedEntryCount; |
| 164 | } |
| 165 | #endif |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 166 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 167 | |
| 168 | // update our stats |
| 169 | if (clientReattach) { |
| 170 | fClientDetachedCount -= 1; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 171 | fClientDetachedBytes -= entry->resource()->sizeInBytes(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 172 | } else { |
| 173 | fEntryCount += 1; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 174 | fEntryBytes += entry->resource()->sizeInBytes(); |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame] | 175 | |
| 176 | #if GR_DEBUG |
| 177 | if (fHighWaterEntryCount < fEntryCount) { |
| 178 | fHighWaterEntryCount = fEntryCount; |
| 179 | } |
| 180 | if (fHighWaterEntryBytes < fEntryBytes) { |
| 181 | fHighWaterEntryBytes = fEntryBytes; |
| 182 | } |
| 183 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
robertphillips@google.com | a9b0623 | 2012-08-30 11:06:31 +0000 | [diff] [blame^] | 187 | GrResource* GrResourceCache::find(const GrResourceKey& key) { |
| 188 | GrAutoResourceCacheValidate atcv(this); |
| 189 | |
| 190 | GrResourceEntry* entry = fCache.find(key); |
| 191 | if (NULL == entry) { |
| 192 | return NULL; |
| 193 | } |
| 194 | |
| 195 | return entry->fResource; |
| 196 | } |
| 197 | |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 198 | GrResource* GrResourceCache::findAndLock(const GrResourceKey& key, |
| 199 | LockType type) { |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 200 | GrAutoResourceCacheValidate atcv(this); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 201 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 202 | GrResourceEntry* entry = fCache.find(key); |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 203 | if (NULL == entry) { |
| 204 | return NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 205 | } |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 206 | |
| 207 | this->internalDetach(entry, false); |
| 208 | // mark the entry as "busy" so it doesn't get purged |
| 209 | // do this between detach and attach for locked count tracking |
| 210 | if (kNested_LockType == type || !entry->isLocked()) { |
| 211 | entry->lock(); |
| 212 | } |
| 213 | this->attachToHead(entry, false); |
| 214 | |
| 215 | return entry->fResource; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 216 | } |
| 217 | |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 218 | bool GrResourceCache::hasKey(const GrResourceKey& key) const { |
| 219 | return NULL != fCache.find(key); |
| 220 | } |
| 221 | |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 222 | GrResourceEntry* GrResourceCache::create(const GrResourceKey& key, |
| 223 | GrResource* resource, |
robertphillips@google.com | 386319e | 2012-06-27 14:59:18 +0000 | [diff] [blame] | 224 | bool lock, |
| 225 | bool clientReattach) { |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 226 | // we don't expect to create new resources during a purge. In theory |
| 227 | // this could cause purgeAsNeeded() into an infinite loop (e.g. |
| 228 | // each resource destroyed creates and locks 2 resources and |
| 229 | // unlocks 1 thereby causing a new purge). |
| 230 | GrAssert(!fPurging); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 231 | GrAutoResourceCacheValidate atcv(this); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 232 | |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 233 | GrResourceEntry* entry = SkNEW_ARGS(GrResourceEntry, (key, resource)); |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 234 | resource->setCacheEntry(entry); |
| 235 | |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 236 | if (lock) { |
| 237 | // mark the entry as "busy" so it doesn't get purged |
| 238 | // do this before attach for locked count tracking |
| 239 | entry->lock(); |
| 240 | } |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 241 | |
robertphillips@google.com | 386319e | 2012-06-27 14:59:18 +0000 | [diff] [blame] | 242 | this->attachToHead(entry, clientReattach); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 243 | fCache.insert(key, entry); |
| 244 | |
| 245 | #if GR_DUMP_TEXTURE_UPLOAD |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 246 | GrPrintf("--- add resource to cache %p, count=%d bytes= %d %d\n", |
| 247 | entry, fEntryCount, resource->sizeInBytes(), fEntryBytes); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 248 | #endif |
| 249 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 250 | this->purgeAsNeeded(); |
| 251 | return entry; |
| 252 | } |
| 253 | |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 254 | void GrResourceCache::createAndLock(const GrResourceKey& key, |
| 255 | GrResource* resource) { |
| 256 | GrAssert(NULL == resource->getCacheEntry()); |
| 257 | this->create(key, resource, true, false); |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | void GrResourceCache::attach(const GrResourceKey& key, |
| 261 | GrResource* resource) { |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 262 | GrAssert(NULL == resource->getCacheEntry()); |
robertphillips@google.com | 386319e | 2012-06-27 14:59:18 +0000 | [diff] [blame] | 263 | this->create(key, resource, false, true); |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 264 | } |
| 265 | |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 266 | void GrResourceCache::makeExclusive(GrResourceEntry* entry) { |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 267 | GrAutoResourceCacheValidate atcv(this); |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 268 | |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 269 | this->internalDetach(entry, true); |
robertphillips@google.com | a9b0623 | 2012-08-30 11:06:31 +0000 | [diff] [blame^] | 270 | fCache.remove(entry->key(), entry); |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 271 | |
| 272 | #if GR_DEBUG |
| 273 | fExclusiveList.addToHead(entry); |
| 274 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 275 | } |
| 276 | |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 277 | void GrResourceCache::removeInvalidResource(GrResourceEntry* entry) { |
| 278 | // If the resource went invalid while it was detached then purge it |
| 279 | // This can happen when a 3D context was lost, |
| 280 | // the client called GrContext::contextDestroyed() to notify Gr, |
| 281 | // and then later an SkGpuDevice's destructor releases its backing |
| 282 | // texture (which was invalidated at contextDestroyed time). |
| 283 | fClientDetachedCount -= 1; |
| 284 | fEntryCount -= 1; |
| 285 | size_t size = entry->resource()->sizeInBytes(); |
| 286 | fClientDetachedBytes -= size; |
| 287 | fEntryBytes -= size; |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 288 | } |
| 289 | |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 290 | void GrResourceCache::makeNonExclusive(GrResourceEntry* entry) { |
bsalomon@google.com | 6087975 | 2011-09-15 20:43:53 +0000 | [diff] [blame] | 291 | GrAutoResourceCacheValidate atcv(this); |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 292 | |
| 293 | #if GR_DEBUG |
| 294 | fExclusiveList.remove(entry); |
| 295 | #endif |
| 296 | |
bsalomon@google.com | 6087975 | 2011-09-15 20:43:53 +0000 | [diff] [blame] | 297 | if (entry->resource()->isValid()) { |
| 298 | attachToHead(entry, true); |
| 299 | fCache.insert(entry->key(), entry); |
| 300 | } else { |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 301 | this->removeInvalidResource(entry); |
bsalomon@google.com | 6087975 | 2011-09-15 20:43:53 +0000 | [diff] [blame] | 302 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 303 | } |
| 304 | |
robertphillips@google.com | a9b0623 | 2012-08-30 11:06:31 +0000 | [diff] [blame^] | 305 | void GrResourceCache::lock(GrResourceEntry* entry) { |
| 306 | GrAutoResourceCacheValidate atcv(this); |
| 307 | |
| 308 | GrAssert(entry); |
| 309 | GrAssert(fCache.find(entry->key())); |
| 310 | |
| 311 | if (!entry->isLocked()) { |
| 312 | --fUnlockedEntryCount; |
| 313 | } |
| 314 | |
| 315 | entry->lock(); |
| 316 | } |
| 317 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 318 | void GrResourceCache::unlock(GrResourceEntry* entry) { |
| 319 | GrAutoResourceCacheValidate atcv(this); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 320 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 321 | GrAssert(entry); |
| 322 | GrAssert(entry->isLocked()); |
| 323 | GrAssert(fCache.find(entry->key())); |
| 324 | |
| 325 | entry->unlock(); |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 326 | if (!entry->isLocked()) { |
| 327 | ++fUnlockedEntryCount; |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame] | 328 | #if GR_DEBUG |
| 329 | if (fHighWaterUnlockedEntryCount < fUnlockedEntryCount) { |
| 330 | fHighWaterUnlockedEntryCount = fUnlockedEntryCount; |
| 331 | } |
| 332 | #endif |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 333 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 334 | this->purgeAsNeeded(); |
| 335 | } |
| 336 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 337 | /** |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 338 | * Destroying a resource may potentially trigger the unlock of additional |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 339 | * resources which in turn will trigger a nested purge. We block the nested |
| 340 | * purge using the fPurging variable. However, the initial purge will keep |
| 341 | * looping until either all resources in the cache are unlocked or we've met |
| 342 | * the budget. There is an assertion in createAndLock to check against a |
| 343 | * resource's destructor inserting new resources into the cache. If these |
| 344 | * new resources were unlocked before purgeAsNeeded completed it could |
| 345 | * potentially make purgeAsNeeded loop infinitely. |
| 346 | */ |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 347 | void GrResourceCache::purgeAsNeeded() { |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 348 | if (!fPurging) { |
| 349 | fPurging = true; |
| 350 | bool withinBudget = false; |
| 351 | do { |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 352 | EntryList::Iter iter; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 353 | |
| 354 | // Note: the following code relies on the fact that the |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 355 | // doubly linked list doesn't invalidate its data/pointers |
| 356 | // outside of the specific area where a deletion occurs (e.g., |
| 357 | // in internalDetach) |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 358 | GrResourceEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart); |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 359 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 360 | while (entry && fUnlockedEntryCount) { |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 361 | GrAutoResourceCacheValidate atcv(this); |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 362 | if (fEntryCount <= fMaxCount && fEntryBytes <= fMaxBytes) { |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 363 | withinBudget = true; |
| 364 | break; |
| 365 | } |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 366 | |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 367 | GrResourceEntry* prev = iter.prev(); |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 368 | if (!entry->isLocked()) { |
| 369 | // remove from our cache |
robertphillips@google.com | a9b0623 | 2012-08-30 11:06:31 +0000 | [diff] [blame^] | 370 | fCache.remove(entry->key(), entry); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 371 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 372 | // remove from our llist |
| 373 | this->internalDetach(entry, false); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 374 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 375 | #if GR_DUMP_TEXTURE_UPLOAD |
| 376 | GrPrintf("--- ~resource from cache %p [%d %d]\n", |
| 377 | entry->resource(), |
| 378 | entry->resource()->width(), |
| 379 | entry->resource()->height()); |
| 380 | #endif |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 381 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 382 | delete entry; |
| 383 | } |
| 384 | entry = prev; |
| 385 | } |
| 386 | } while (!withinBudget && fUnlockedEntryCount); |
| 387 | fPurging = false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 391 | void GrResourceCache::purgeAllUnlocked() { |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 392 | GrAutoResourceCacheValidate atcv(this); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 393 | |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 394 | // we can have one GrResource holding a lock on another |
| 395 | // so we don't want to just do a simple loop kicking each |
| 396 | // entry out. Instead change the budget and purge. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 397 | |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 398 | int savedMaxBytes = fMaxBytes; |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 399 | int savedMaxCount = fMaxCount; |
| 400 | fMaxBytes = (size_t) -1; |
| 401 | fMaxCount = 0; |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 402 | this->purgeAsNeeded(); |
| 403 | |
twiz@google.com | 0ec107f | 2012-02-21 19:15:53 +0000 | [diff] [blame] | 404 | #if GR_DEBUG |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 405 | GrAssert(fExclusiveList.countEntries() == fClientDetachedCount); |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 406 | GrAssert(countBytes(fExclusiveList) == fClientDetachedBytes); |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 407 | GrAssert(!fUnlockedEntryCount); |
twiz@google.com | 0ec107f | 2012-02-21 19:15:53 +0000 | [diff] [blame] | 408 | if (!fCache.count()) { |
| 409 | // Items may have been detached from the cache (such as the backing |
| 410 | // texture for an SkGpuDevice). The above purge would not have removed |
| 411 | // them. |
| 412 | GrAssert(fEntryCount == fClientDetachedCount); |
| 413 | GrAssert(fEntryBytes == fClientDetachedBytes); |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 414 | GrAssert(fList.isEmpty()); |
twiz@google.com | 0ec107f | 2012-02-21 19:15:53 +0000 | [diff] [blame] | 415 | } |
| 416 | #endif |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 417 | |
| 418 | fMaxBytes = savedMaxBytes; |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 419 | fMaxCount = savedMaxCount; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | /////////////////////////////////////////////////////////////////////////////// |
| 423 | |
| 424 | #if GR_DEBUG |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 425 | size_t GrResourceCache::countBytes(const EntryList& list) { |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 426 | size_t bytes = 0; |
| 427 | |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 428 | EntryList::Iter iter; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 429 | |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 430 | const GrResourceEntry* entry = iter.init(const_cast<EntryList&>(list), |
| 431 | EntryList::Iter::kTail_IterStart); |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 432 | |
| 433 | for ( ; NULL != entry; entry = iter.prev()) { |
| 434 | bytes += entry->resource()->sizeInBytes(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 435 | } |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 436 | return bytes; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 437 | } |
| 438 | |
reed@google.com | b89a643 | 2011-02-07 13:20:30 +0000 | [diff] [blame] | 439 | static bool both_zero_or_nonzero(int count, size_t bytes) { |
| 440 | return (count == 0 && bytes == 0) || (count > 0 && bytes > 0); |
| 441 | } |
reed@google.com | b89a643 | 2011-02-07 13:20:30 +0000 | [diff] [blame] | 442 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 443 | void GrResourceCache::validate() const { |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 444 | fList.validate(); |
| 445 | fExclusiveList.validate(); |
reed@google.com | b89a643 | 2011-02-07 13:20:30 +0000 | [diff] [blame] | 446 | GrAssert(both_zero_or_nonzero(fEntryCount, fEntryBytes)); |
| 447 | GrAssert(both_zero_or_nonzero(fClientDetachedCount, fClientDetachedBytes)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 448 | GrAssert(fClientDetachedBytes <= fEntryBytes); |
| 449 | GrAssert(fClientDetachedCount <= fEntryCount); |
| 450 | GrAssert((fEntryCount - fClientDetachedCount) == fCache.count()); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 451 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 452 | fCache.validate(); |
| 453 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 454 | int count = 0; |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 455 | int unlockCount = 0; |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 456 | |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 457 | EntryList::Iter iter; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 458 | |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 459 | const GrResourceEntry* entry = iter.init(const_cast<EntryList&>(fList), |
| 460 | EntryList::Iter::kHead_IterStart); |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 461 | |
| 462 | for ( ; NULL != entry; entry = iter.next()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 463 | entry->validate(); |
| 464 | GrAssert(fCache.find(entry->key())); |
| 465 | count += 1; |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 466 | if (!entry->isLocked()) { |
| 467 | unlockCount += 1; |
| 468 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 469 | } |
| 470 | GrAssert(count == fEntryCount - fClientDetachedCount); |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 471 | |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 472 | size_t bytes = countBytes(fList); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 473 | GrAssert(bytes == fEntryBytes - fClientDetachedBytes); |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 474 | |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 475 | bytes = countBytes(fExclusiveList); |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 476 | GrAssert(bytes == fClientDetachedBytes); |
| 477 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 478 | GrAssert(unlockCount == fUnlockedEntryCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 479 | |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 480 | GrAssert(fList.countEntries() == fEntryCount - fClientDetachedCount); |
| 481 | |
| 482 | GrAssert(fExclusiveList.countEntries() == fClientDetachedCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 483 | } |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame] | 484 | |
| 485 | void GrResourceCache::printStats() const { |
| 486 | SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes); |
| 487 | SkDebugf("\t\tEntry Count: current %d high %d\n", |
| 488 | fEntryCount, fHighWaterEntryCount); |
| 489 | SkDebugf("\t\tUnlocked Entry Count: current %d high %d\n", |
| 490 | fUnlockedEntryCount, fHighWaterUnlockedEntryCount); |
| 491 | SkDebugf("\t\tEntry Bytes: current %d high %d\n", |
| 492 | fEntryBytes, fHighWaterEntryBytes); |
| 493 | SkDebugf("\t\tDetached Entry Count: current %d high %d\n", |
| 494 | fClientDetachedCount, fHighWaterClientDetachedCount); |
| 495 | SkDebugf("\t\tDetached Bytes: current %d high %d\n", |
| 496 | fClientDetachedBytes, fHighWaterClientDetachedBytes); |
| 497 | } |
| 498 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 499 | #endif |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 500 | |
| 501 | /////////////////////////////////////////////////////////////////////////////// |