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 |
| 97 | this->internalDetach(entry, false); |
| 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, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 124 | bool clientDetach) { |
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 |
| 128 | if (clientDetach) { |
| 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 { |
| 142 | fEntryCount -= 1; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 143 | fEntryBytes -= entry->resource()->sizeInBytes(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 147 | void GrResourceCache::attachToHead(GrResourceEntry* entry, |
robertphillips@google.com | d07cb0c | 2012-08-30 19:22:29 +0000 | [diff] [blame] | 148 | bool clientReattach) { |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 149 | fList.addToHead(entry); |
| 150 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 151 | // update our stats |
| 152 | if (clientReattach) { |
| 153 | fClientDetachedCount -= 1; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 154 | fClientDetachedBytes -= entry->resource()->sizeInBytes(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 155 | } else { |
| 156 | fEntryCount += 1; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 157 | fEntryBytes += entry->resource()->sizeInBytes(); |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame] | 158 | |
robertphillips@google.com | 5955202 | 2012-08-31 13:07:37 +0000 | [diff] [blame] | 159 | #if GR_CACHE_STATS |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame] | 160 | if (fHighWaterEntryCount < fEntryCount) { |
| 161 | fHighWaterEntryCount = fEntryCount; |
| 162 | } |
| 163 | if (fHighWaterEntryBytes < fEntryBytes) { |
| 164 | fHighWaterEntryBytes = fEntryBytes; |
| 165 | } |
| 166 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
robertphillips@google.com | a9b0623 | 2012-08-30 11:06:31 +0000 | [diff] [blame] | 170 | GrResource* GrResourceCache::find(const GrResourceKey& key) { |
| 171 | GrAutoResourceCacheValidate atcv(this); |
| 172 | |
| 173 | GrResourceEntry* entry = fCache.find(key); |
| 174 | if (NULL == entry) { |
| 175 | return NULL; |
| 176 | } |
| 177 | |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 178 | this->internalDetach(entry, false); |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 179 | this->attachToHead(entry, false); |
| 180 | |
| 181 | return entry->fResource; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 182 | } |
| 183 | |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 184 | bool GrResourceCache::hasKey(const GrResourceKey& key) const { |
| 185 | return NULL != fCache.find(key); |
| 186 | } |
| 187 | |
robertphillips@google.com | d07cb0c | 2012-08-30 19:22:29 +0000 | [diff] [blame] | 188 | void GrResourceCache::create(const GrResourceKey& key, GrResource* resource) { |
| 189 | GrAssert(NULL == resource->getCacheEntry()); |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 190 | // we don't expect to create new resources during a purge. In theory |
| 191 | // this could cause purgeAsNeeded() into an infinite loop (e.g. |
| 192 | // each resource destroyed creates and locks 2 resources and |
| 193 | // unlocks 1 thereby causing a new purge). |
| 194 | GrAssert(!fPurging); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 195 | GrAutoResourceCacheValidate atcv(this); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 196 | |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 197 | GrResourceEntry* entry = SkNEW_ARGS(GrResourceEntry, (key, resource)); |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 198 | resource->setCacheEntry(entry); |
| 199 | |
robertphillips@google.com | d07cb0c | 2012-08-30 19:22:29 +0000 | [diff] [blame] | 200 | this->attachToHead(entry, false); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 201 | fCache.insert(key, entry); |
| 202 | |
| 203 | #if GR_DUMP_TEXTURE_UPLOAD |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 204 | GrPrintf("--- add resource to cache %p, count=%d bytes= %d %d\n", |
| 205 | entry, fEntryCount, resource->sizeInBytes(), fEntryBytes); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 206 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 207 | } |
| 208 | |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 209 | void GrResourceCache::makeExclusive(GrResourceEntry* entry) { |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 210 | GrAutoResourceCacheValidate atcv(this); |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 211 | |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 212 | this->internalDetach(entry, true); |
robertphillips@google.com | a9b0623 | 2012-08-30 11:06:31 +0000 | [diff] [blame] | 213 | fCache.remove(entry->key(), entry); |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 214 | |
| 215 | #if GR_DEBUG |
| 216 | fExclusiveList.addToHead(entry); |
| 217 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 218 | } |
| 219 | |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 220 | void GrResourceCache::removeInvalidResource(GrResourceEntry* entry) { |
| 221 | // If the resource went invalid while it was detached then purge it |
| 222 | // This can happen when a 3D context was lost, |
| 223 | // the client called GrContext::contextDestroyed() to notify Gr, |
| 224 | // and then later an SkGpuDevice's destructor releases its backing |
| 225 | // texture (which was invalidated at contextDestroyed time). |
| 226 | fClientDetachedCount -= 1; |
| 227 | fEntryCount -= 1; |
| 228 | size_t size = entry->resource()->sizeInBytes(); |
| 229 | fClientDetachedBytes -= size; |
| 230 | fEntryBytes -= size; |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 231 | } |
| 232 | |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 233 | void GrResourceCache::makeNonExclusive(GrResourceEntry* entry) { |
bsalomon@google.com | 6087975 | 2011-09-15 20:43:53 +0000 | [diff] [blame] | 234 | GrAutoResourceCacheValidate atcv(this); |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 235 | |
| 236 | #if GR_DEBUG |
| 237 | fExclusiveList.remove(entry); |
| 238 | #endif |
| 239 | |
bsalomon@google.com | 6087975 | 2011-09-15 20:43:53 +0000 | [diff] [blame] | 240 | if (entry->resource()->isValid()) { |
| 241 | attachToHead(entry, true); |
| 242 | fCache.insert(entry->key(), entry); |
| 243 | } else { |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 244 | this->removeInvalidResource(entry); |
bsalomon@google.com | 6087975 | 2011-09-15 20:43:53 +0000 | [diff] [blame] | 245 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 246 | } |
| 247 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 248 | /** |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 249 | * Destroying a resource may potentially trigger the unlock of additional |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 250 | * resources which in turn will trigger a nested purge. We block the nested |
| 251 | * purge using the fPurging variable. However, the initial purge will keep |
| 252 | * looping until either all resources in the cache are unlocked or we've met |
| 253 | * the budget. There is an assertion in createAndLock to check against a |
| 254 | * resource's destructor inserting new resources into the cache. If these |
| 255 | * new resources were unlocked before purgeAsNeeded completed it could |
| 256 | * potentially make purgeAsNeeded loop infinitely. |
| 257 | */ |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 258 | void GrResourceCache::purgeAsNeeded() { |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 259 | if (!fPurging) { |
| 260 | fPurging = true; |
| 261 | bool withinBudget = false; |
robertphillips@google.com | 9fbcad0 | 2012-09-09 14:44:15 +0000 | [diff] [blame] | 262 | bool changed = false; |
robertphillips@google.com | 6fc9518 | 2012-09-04 13:36:31 +0000 | [diff] [blame] | 263 | |
| 264 | // The purging process is repeated several times since one pass |
| 265 | // may free up other resources |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 266 | do { |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 267 | EntryList::Iter iter; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 268 | |
robertphillips@google.com | 9fbcad0 | 2012-09-09 14:44:15 +0000 | [diff] [blame] | 269 | changed = false; |
robertphillips@google.com | 6fc9518 | 2012-09-04 13:36:31 +0000 | [diff] [blame] | 270 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 271 | // Note: the following code relies on the fact that the |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 272 | // doubly linked list doesn't invalidate its data/pointers |
| 273 | // outside of the specific area where a deletion occurs (e.g., |
| 274 | // in internalDetach) |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 275 | GrResourceEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart); |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 276 | |
robertphillips@google.com | 9fbcad0 | 2012-09-09 14:44:15 +0000 | [diff] [blame] | 277 | while (NULL != entry) { |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 278 | GrAutoResourceCacheValidate atcv(this); |
robertphillips@google.com | 9fbcad0 | 2012-09-09 14:44:15 +0000 | [diff] [blame] | 279 | |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 280 | if (fEntryCount <= fMaxCount && fEntryBytes <= fMaxBytes) { |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 281 | withinBudget = true; |
| 282 | break; |
| 283 | } |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 284 | |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 285 | GrResourceEntry* prev = iter.prev(); |
robertphillips@google.com | 9fbcad0 | 2012-09-09 14:44:15 +0000 | [diff] [blame] | 286 | if (1 == entry->fResource->getRefCnt()) { |
| 287 | changed = true; |
| 288 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 289 | // remove from our cache |
robertphillips@google.com | a9b0623 | 2012-08-30 11:06:31 +0000 | [diff] [blame] | 290 | fCache.remove(entry->key(), entry); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 291 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 292 | // remove from our llist |
| 293 | this->internalDetach(entry, false); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 294 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 295 | #if GR_DUMP_TEXTURE_UPLOAD |
| 296 | GrPrintf("--- ~resource from cache %p [%d %d]\n", |
| 297 | entry->resource(), |
| 298 | entry->resource()->width(), |
| 299 | entry->resource()->height()); |
| 300 | #endif |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 301 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 302 | delete entry; |
| 303 | } |
| 304 | entry = prev; |
| 305 | } |
robertphillips@google.com | 9fbcad0 | 2012-09-09 14:44:15 +0000 | [diff] [blame] | 306 | } while (!withinBudget && changed); |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 307 | fPurging = false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 311 | void GrResourceCache::purgeAllUnlocked() { |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 312 | GrAutoResourceCacheValidate atcv(this); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 313 | |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 314 | // we can have one GrResource holding a lock on another |
| 315 | // so we don't want to just do a simple loop kicking each |
| 316 | // entry out. Instead change the budget and purge. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 317 | |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 318 | int savedMaxBytes = fMaxBytes; |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 319 | int savedMaxCount = fMaxCount; |
| 320 | fMaxBytes = (size_t) -1; |
| 321 | fMaxCount = 0; |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 322 | this->purgeAsNeeded(); |
| 323 | |
twiz@google.com | 0ec107f | 2012-02-21 19:15:53 +0000 | [diff] [blame] | 324 | #if GR_DEBUG |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 325 | GrAssert(fExclusiveList.countEntries() == fClientDetachedCount); |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 326 | GrAssert(countBytes(fExclusiveList) == fClientDetachedBytes); |
twiz@google.com | 0ec107f | 2012-02-21 19:15:53 +0000 | [diff] [blame] | 327 | if (!fCache.count()) { |
| 328 | // Items may have been detached from the cache (such as the backing |
| 329 | // texture for an SkGpuDevice). The above purge would not have removed |
| 330 | // them. |
| 331 | GrAssert(fEntryCount == fClientDetachedCount); |
| 332 | GrAssert(fEntryBytes == fClientDetachedBytes); |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 333 | GrAssert(fList.isEmpty()); |
twiz@google.com | 0ec107f | 2012-02-21 19:15:53 +0000 | [diff] [blame] | 334 | } |
| 335 | #endif |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 336 | |
| 337 | fMaxBytes = savedMaxBytes; |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 338 | fMaxCount = savedMaxCount; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | /////////////////////////////////////////////////////////////////////////////// |
| 342 | |
| 343 | #if GR_DEBUG |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 344 | size_t GrResourceCache::countBytes(const EntryList& list) { |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 345 | size_t bytes = 0; |
| 346 | |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 347 | EntryList::Iter iter; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 348 | |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 349 | const GrResourceEntry* entry = iter.init(const_cast<EntryList&>(list), |
| 350 | EntryList::Iter::kTail_IterStart); |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 351 | |
| 352 | for ( ; NULL != entry; entry = iter.prev()) { |
| 353 | bytes += entry->resource()->sizeInBytes(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 354 | } |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 355 | return bytes; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 356 | } |
| 357 | |
reed@google.com | b89a643 | 2011-02-07 13:20:30 +0000 | [diff] [blame] | 358 | static bool both_zero_or_nonzero(int count, size_t bytes) { |
| 359 | return (count == 0 && bytes == 0) || (count > 0 && bytes > 0); |
| 360 | } |
reed@google.com | b89a643 | 2011-02-07 13:20:30 +0000 | [diff] [blame] | 361 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 362 | void GrResourceCache::validate() const { |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 363 | fList.validate(); |
| 364 | fExclusiveList.validate(); |
reed@google.com | b89a643 | 2011-02-07 13:20:30 +0000 | [diff] [blame] | 365 | GrAssert(both_zero_or_nonzero(fEntryCount, fEntryBytes)); |
| 366 | GrAssert(both_zero_or_nonzero(fClientDetachedCount, fClientDetachedBytes)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 367 | GrAssert(fClientDetachedBytes <= fEntryBytes); |
| 368 | GrAssert(fClientDetachedCount <= fEntryCount); |
| 369 | GrAssert((fEntryCount - fClientDetachedCount) == fCache.count()); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 370 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 371 | fCache.validate(); |
| 372 | |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 373 | |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 374 | EntryList::Iter iter; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 375 | |
robertphillips@google.com | 9fbcad0 | 2012-09-09 14:44:15 +0000 | [diff] [blame] | 376 | // check that the exclusively held entries are okay |
robertphillips@google.com | d07cb0c | 2012-08-30 19:22:29 +0000 | [diff] [blame] | 377 | const GrResourceEntry* entry = iter.init(const_cast<EntryList&>(fExclusiveList), |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 378 | EntryList::Iter::kHead_IterStart); |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 379 | |
| 380 | for ( ; NULL != entry; entry = iter.next()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 381 | entry->validate(); |
robertphillips@google.com | d07cb0c | 2012-08-30 19:22:29 +0000 | [diff] [blame] | 382 | } |
| 383 | |
robertphillips@google.com | 9fbcad0 | 2012-09-09 14:44:15 +0000 | [diff] [blame] | 384 | // check that the shareable entries are okay |
robertphillips@google.com | d07cb0c | 2012-08-30 19:22:29 +0000 | [diff] [blame] | 385 | entry = iter.init(const_cast<EntryList&>(fList), EntryList::Iter::kHead_IterStart); |
| 386 | |
| 387 | int count = 0; |
robertphillips@google.com | d07cb0c | 2012-08-30 19:22:29 +0000 | [diff] [blame] | 388 | for ( ; NULL != entry; entry = iter.next()) { |
| 389 | entry->validate(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 390 | GrAssert(fCache.find(entry->key())); |
| 391 | count += 1; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 392 | } |
| 393 | GrAssert(count == fEntryCount - fClientDetachedCount); |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 394 | |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 395 | size_t bytes = countBytes(fList); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 396 | GrAssert(bytes == fEntryBytes - fClientDetachedBytes); |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 397 | |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 398 | bytes = countBytes(fExclusiveList); |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 399 | GrAssert(bytes == fClientDetachedBytes); |
| 400 | |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 401 | GrAssert(fList.countEntries() == fEntryCount - fClientDetachedCount); |
| 402 | |
| 403 | GrAssert(fExclusiveList.countEntries() == fClientDetachedCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 404 | } |
robertphillips@google.com | 5955202 | 2012-08-31 13:07:37 +0000 | [diff] [blame] | 405 | #endif // GR_DEBUG |
| 406 | |
| 407 | #if GR_CACHE_STATS |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame] | 408 | |
robertphillips@google.com | 9fbcad0 | 2012-09-09 14:44:15 +0000 | [diff] [blame] | 409 | void GrResourceCache::printStats() { |
| 410 | int locked = 0; |
| 411 | |
| 412 | EntryList::Iter iter; |
| 413 | |
| 414 | GrResourceEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart); |
| 415 | |
| 416 | for ( ; NULL != entry; entry = iter.prev()) { |
| 417 | if (entry->fResource->getRefCnt() > 1) { |
| 418 | ++locked; |
| 419 | } |
| 420 | } |
| 421 | |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame] | 422 | SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes); |
robertphillips@google.com | 9fbcad0 | 2012-09-09 14:44:15 +0000 | [diff] [blame] | 423 | SkDebugf("\t\tEntry Count: current %d (%d locked) high %d\n", |
| 424 | fEntryCount, locked, fHighWaterEntryCount); |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame] | 425 | SkDebugf("\t\tEntry Bytes: current %d high %d\n", |
| 426 | fEntryBytes, fHighWaterEntryBytes); |
| 427 | SkDebugf("\t\tDetached Entry Count: current %d high %d\n", |
| 428 | fClientDetachedCount, fHighWaterClientDetachedCount); |
| 429 | SkDebugf("\t\tDetached Bytes: current %d high %d\n", |
| 430 | fClientDetachedBytes, fHighWaterClientDetachedBytes); |
| 431 | } |
| 432 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 433 | #endif |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 434 | |
| 435 | /////////////////////////////////////////////////////////////////////////////// |