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