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 | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 187 | GrResource* GrResourceCache::findAndLock(const GrResourceKey& key, |
| 188 | LockType type) { |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 189 | GrAutoResourceCacheValidate atcv(this); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 190 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 191 | GrResourceEntry* entry = fCache.find(key); |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 192 | if (NULL == entry) { |
| 193 | return NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 194 | } |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 195 | |
| 196 | this->internalDetach(entry, false); |
| 197 | // mark the entry as "busy" so it doesn't get purged |
| 198 | // do this between detach and attach for locked count tracking |
| 199 | if (kNested_LockType == type || !entry->isLocked()) { |
| 200 | entry->lock(); |
| 201 | } |
| 202 | this->attachToHead(entry, false); |
| 203 | |
| 204 | return entry->fResource; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 205 | } |
| 206 | |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 207 | bool GrResourceCache::hasKey(const GrResourceKey& key) const { |
| 208 | return NULL != fCache.find(key); |
| 209 | } |
| 210 | |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 211 | GrResourceEntry* GrResourceCache::create(const GrResourceKey& key, |
| 212 | GrResource* resource, |
robertphillips@google.com | 386319e | 2012-06-27 14:59:18 +0000 | [diff] [blame] | 213 | bool lock, |
| 214 | bool clientReattach) { |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 215 | // we don't expect to create new resources during a purge. In theory |
| 216 | // this could cause purgeAsNeeded() into an infinite loop (e.g. |
| 217 | // each resource destroyed creates and locks 2 resources and |
| 218 | // unlocks 1 thereby causing a new purge). |
| 219 | GrAssert(!fPurging); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 220 | GrAutoResourceCacheValidate atcv(this); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 221 | |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 222 | GrResourceEntry* entry = SkNEW_ARGS(GrResourceEntry, (key, resource)); |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 223 | resource->setCacheEntry(entry); |
| 224 | |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 225 | if (lock) { |
| 226 | // mark the entry as "busy" so it doesn't get purged |
| 227 | // do this before attach for locked count tracking |
| 228 | entry->lock(); |
| 229 | } |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 230 | |
robertphillips@google.com | 386319e | 2012-06-27 14:59:18 +0000 | [diff] [blame] | 231 | this->attachToHead(entry, clientReattach); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 232 | fCache.insert(key, entry); |
| 233 | |
| 234 | #if GR_DUMP_TEXTURE_UPLOAD |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 235 | GrPrintf("--- add resource to cache %p, count=%d bytes= %d %d\n", |
| 236 | entry, fEntryCount, resource->sizeInBytes(), fEntryBytes); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 237 | #endif |
| 238 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 239 | this->purgeAsNeeded(); |
| 240 | return entry; |
| 241 | } |
| 242 | |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 243 | void GrResourceCache::createAndLock(const GrResourceKey& key, |
| 244 | GrResource* resource) { |
| 245 | GrAssert(NULL == resource->getCacheEntry()); |
| 246 | this->create(key, resource, true, false); |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | void GrResourceCache::attach(const GrResourceKey& key, |
| 250 | GrResource* resource) { |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 251 | GrAssert(NULL == resource->getCacheEntry()); |
robertphillips@google.com | 386319e | 2012-06-27 14:59:18 +0000 | [diff] [blame] | 252 | this->create(key, resource, false, true); |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 253 | } |
| 254 | |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 255 | void GrResourceCache::makeExclusive(GrResourceEntry* entry) { |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 256 | GrAutoResourceCacheValidate atcv(this); |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 257 | |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 258 | this->internalDetach(entry, true); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 259 | fCache.remove(entry->fKey, entry); |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 260 | |
| 261 | #if GR_DEBUG |
| 262 | fExclusiveList.addToHead(entry); |
| 263 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 264 | } |
| 265 | |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 266 | void GrResourceCache::removeInvalidResource(GrResourceEntry* entry) { |
| 267 | // If the resource went invalid while it was detached then purge it |
| 268 | // This can happen when a 3D context was lost, |
| 269 | // the client called GrContext::contextDestroyed() to notify Gr, |
| 270 | // and then later an SkGpuDevice's destructor releases its backing |
| 271 | // texture (which was invalidated at contextDestroyed time). |
| 272 | fClientDetachedCount -= 1; |
| 273 | fEntryCount -= 1; |
| 274 | size_t size = entry->resource()->sizeInBytes(); |
| 275 | fClientDetachedBytes -= size; |
| 276 | fEntryBytes -= size; |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 277 | } |
| 278 | |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 279 | void GrResourceCache::makeNonExclusive(GrResourceEntry* entry) { |
bsalomon@google.com | 6087975 | 2011-09-15 20:43:53 +0000 | [diff] [blame] | 280 | GrAutoResourceCacheValidate atcv(this); |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 281 | |
| 282 | #if GR_DEBUG |
| 283 | fExclusiveList.remove(entry); |
| 284 | #endif |
| 285 | |
bsalomon@google.com | 6087975 | 2011-09-15 20:43:53 +0000 | [diff] [blame] | 286 | if (entry->resource()->isValid()) { |
| 287 | attachToHead(entry, true); |
| 288 | fCache.insert(entry->key(), entry); |
| 289 | } else { |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 290 | this->removeInvalidResource(entry); |
bsalomon@google.com | 6087975 | 2011-09-15 20:43:53 +0000 | [diff] [blame] | 291 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 292 | } |
| 293 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 294 | void GrResourceCache::unlock(GrResourceEntry* entry) { |
| 295 | GrAutoResourceCacheValidate atcv(this); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 296 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 297 | GrAssert(entry); |
| 298 | GrAssert(entry->isLocked()); |
| 299 | GrAssert(fCache.find(entry->key())); |
| 300 | |
| 301 | entry->unlock(); |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 302 | if (!entry->isLocked()) { |
| 303 | ++fUnlockedEntryCount; |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame] | 304 | #if GR_DEBUG |
| 305 | if (fHighWaterUnlockedEntryCount < fUnlockedEntryCount) { |
| 306 | fHighWaterUnlockedEntryCount = fUnlockedEntryCount; |
| 307 | } |
| 308 | #endif |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 309 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 310 | this->purgeAsNeeded(); |
| 311 | } |
| 312 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 313 | /** |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 314 | * Destroying a resource may potentially trigger the unlock of additional |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 315 | * resources which in turn will trigger a nested purge. We block the nested |
| 316 | * purge using the fPurging variable. However, the initial purge will keep |
| 317 | * looping until either all resources in the cache are unlocked or we've met |
| 318 | * the budget. There is an assertion in createAndLock to check against a |
| 319 | * resource's destructor inserting new resources into the cache. If these |
| 320 | * new resources were unlocked before purgeAsNeeded completed it could |
| 321 | * potentially make purgeAsNeeded loop infinitely. |
| 322 | */ |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 323 | void GrResourceCache::purgeAsNeeded() { |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 324 | if (!fPurging) { |
| 325 | fPurging = true; |
| 326 | bool withinBudget = false; |
| 327 | do { |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame^] | 328 | EntryList::Iter iter; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 329 | |
| 330 | // Note: the following code relies on the fact that the |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 331 | // doubly linked list doesn't invalidate its data/pointers |
| 332 | // outside of the specific area where a deletion occurs (e.g., |
| 333 | // in internalDetach) |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame^] | 334 | GrResourceEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart); |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 335 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 336 | while (entry && fUnlockedEntryCount) { |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 337 | GrAutoResourceCacheValidate atcv(this); |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 338 | if (fEntryCount <= fMaxCount && fEntryBytes <= fMaxBytes) { |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 339 | withinBudget = true; |
| 340 | break; |
| 341 | } |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 342 | |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 343 | GrResourceEntry* prev = iter.prev(); |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 344 | if (!entry->isLocked()) { |
| 345 | // remove from our cache |
| 346 | fCache.remove(entry->fKey, entry); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 347 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 348 | // remove from our llist |
| 349 | this->internalDetach(entry, false); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 350 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 351 | #if GR_DUMP_TEXTURE_UPLOAD |
| 352 | GrPrintf("--- ~resource from cache %p [%d %d]\n", |
| 353 | entry->resource(), |
| 354 | entry->resource()->width(), |
| 355 | entry->resource()->height()); |
| 356 | #endif |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 357 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 358 | delete entry; |
| 359 | } |
| 360 | entry = prev; |
| 361 | } |
| 362 | } while (!withinBudget && fUnlockedEntryCount); |
| 363 | fPurging = false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame^] | 367 | void GrResourceCache::purgeAllUnlocked() { |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 368 | GrAutoResourceCacheValidate atcv(this); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 369 | |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 370 | // we can have one GrResource holding a lock on another |
| 371 | // so we don't want to just do a simple loop kicking each |
| 372 | // entry out. Instead change the budget and purge. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 373 | |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 374 | int savedMaxBytes = fMaxBytes; |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 375 | int savedMaxCount = fMaxCount; |
| 376 | fMaxBytes = (size_t) -1; |
| 377 | fMaxCount = 0; |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 378 | this->purgeAsNeeded(); |
| 379 | |
twiz@google.com | 0ec107f | 2012-02-21 19:15:53 +0000 | [diff] [blame] | 380 | #if GR_DEBUG |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 381 | GrAssert(fExclusiveList.countEntries() == fClientDetachedCount); |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 382 | GrAssert(countBytes(fExclusiveList) == fClientDetachedBytes); |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 383 | GrAssert(!fUnlockedEntryCount); |
twiz@google.com | 0ec107f | 2012-02-21 19:15:53 +0000 | [diff] [blame] | 384 | if (!fCache.count()) { |
| 385 | // Items may have been detached from the cache (such as the backing |
| 386 | // texture for an SkGpuDevice). The above purge would not have removed |
| 387 | // them. |
| 388 | GrAssert(fEntryCount == fClientDetachedCount); |
| 389 | GrAssert(fEntryBytes == fClientDetachedBytes); |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 390 | GrAssert(fList.isEmpty()); |
twiz@google.com | 0ec107f | 2012-02-21 19:15:53 +0000 | [diff] [blame] | 391 | } |
| 392 | #endif |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 393 | |
| 394 | fMaxBytes = savedMaxBytes; |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 395 | fMaxCount = savedMaxCount; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | /////////////////////////////////////////////////////////////////////////////// |
| 399 | |
| 400 | #if GR_DEBUG |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame^] | 401 | size_t GrResourceCache::countBytes(const EntryList& list) { |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 402 | size_t bytes = 0; |
| 403 | |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame^] | 404 | EntryList::Iter iter; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 405 | |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame^] | 406 | const GrResourceEntry* entry = iter.init(const_cast<EntryList&>(list), |
| 407 | EntryList::Iter::kTail_IterStart); |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 408 | |
| 409 | for ( ; NULL != entry; entry = iter.prev()) { |
| 410 | bytes += entry->resource()->sizeInBytes(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 411 | } |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 412 | return bytes; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 413 | } |
| 414 | |
reed@google.com | b89a643 | 2011-02-07 13:20:30 +0000 | [diff] [blame] | 415 | static bool both_zero_or_nonzero(int count, size_t bytes) { |
| 416 | return (count == 0 && bytes == 0) || (count > 0 && bytes > 0); |
| 417 | } |
reed@google.com | b89a643 | 2011-02-07 13:20:30 +0000 | [diff] [blame] | 418 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 419 | void GrResourceCache::validate() const { |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 420 | fList.validate(); |
| 421 | fExclusiveList.validate(); |
reed@google.com | b89a643 | 2011-02-07 13:20:30 +0000 | [diff] [blame] | 422 | GrAssert(both_zero_or_nonzero(fEntryCount, fEntryBytes)); |
| 423 | GrAssert(both_zero_or_nonzero(fClientDetachedCount, fClientDetachedBytes)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 424 | GrAssert(fClientDetachedBytes <= fEntryBytes); |
| 425 | GrAssert(fClientDetachedCount <= fEntryCount); |
| 426 | GrAssert((fEntryCount - fClientDetachedCount) == fCache.count()); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 427 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 428 | fCache.validate(); |
| 429 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 430 | int count = 0; |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 431 | int unlockCount = 0; |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 432 | |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame^] | 433 | EntryList::Iter iter; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 434 | |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame^] | 435 | const GrResourceEntry* entry = iter.init(const_cast<EntryList&>(fList), |
| 436 | EntryList::Iter::kHead_IterStart); |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 437 | |
| 438 | for ( ; NULL != entry; entry = iter.next()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 439 | entry->validate(); |
| 440 | GrAssert(fCache.find(entry->key())); |
| 441 | count += 1; |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 442 | if (!entry->isLocked()) { |
| 443 | unlockCount += 1; |
| 444 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 445 | } |
| 446 | GrAssert(count == fEntryCount - fClientDetachedCount); |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 447 | |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 448 | size_t bytes = countBytes(fList); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 449 | GrAssert(bytes == fEntryBytes - fClientDetachedBytes); |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 450 | |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 451 | bytes = countBytes(fExclusiveList); |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 452 | GrAssert(bytes == fClientDetachedBytes); |
| 453 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 454 | GrAssert(unlockCount == fUnlockedEntryCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 455 | |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 456 | GrAssert(fList.countEntries() == fEntryCount - fClientDetachedCount); |
| 457 | |
| 458 | GrAssert(fExclusiveList.countEntries() == fClientDetachedCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 459 | } |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame] | 460 | |
| 461 | void GrResourceCache::printStats() const { |
| 462 | SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes); |
| 463 | SkDebugf("\t\tEntry Count: current %d high %d\n", |
| 464 | fEntryCount, fHighWaterEntryCount); |
| 465 | SkDebugf("\t\tUnlocked Entry Count: current %d high %d\n", |
| 466 | fUnlockedEntryCount, fHighWaterUnlockedEntryCount); |
| 467 | SkDebugf("\t\tEntry Bytes: current %d high %d\n", |
| 468 | fEntryBytes, fHighWaterEntryBytes); |
| 469 | SkDebugf("\t\tDetached Entry Count: current %d high %d\n", |
| 470 | fClientDetachedCount, fHighWaterClientDetachedCount); |
| 471 | SkDebugf("\t\tDetached Bytes: current %d high %d\n", |
| 472 | fClientDetachedBytes, fHighWaterClientDetachedBytes); |
| 473 | } |
| 474 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 475 | #endif |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 476 | |
| 477 | /////////////////////////////////////////////////////////////////////////////// |