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