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