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