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