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