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" |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 12 | #include "GrGpuResource.h" |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame^] | 13 | #include "GrTexturePriv.h" |
| 14 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 15 | |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 16 | DECLARE_SKMESSAGEBUS_MESSAGE(GrResourceInvalidatedMessage); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 17 | |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 18 | /////////////////////////////////////////////////////////////////////////////// |
| 19 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 20 | void GrGpuResource::didChangeGpuMemorySize() const { |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 21 | if (this->isInCache()) { |
| 22 | fCacheEntry->didChangeResourceSize(); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | /////////////////////////////////////////////////////////////////////////////// |
| 27 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 28 | GrResourceKey::ResourceType GrResourceKey::GenerateResourceType() { |
| 29 | static int32_t gNextType = 0; |
| 30 | |
| 31 | int32_t type = sk_atomic_inc(&gNextType); |
| 32 | if (type >= (1 << 8 * sizeof(ResourceType))) { |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 33 | SkFAIL("Too many Resource Types"); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | return static_cast<ResourceType>(type); |
| 37 | } |
| 38 | |
| 39 | /////////////////////////////////////////////////////////////////////////////// |
| 40 | |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 41 | GrResourceCacheEntry::GrResourceCacheEntry(GrResourceCache* resourceCache, |
| 42 | const GrResourceKey& key, |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 43 | GrGpuResource* resource) |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 44 | : fResourceCache(resourceCache), |
| 45 | fKey(key), |
| 46 | fResource(resource), |
| 47 | fCachedSize(resource->gpuMemorySize()), |
| 48 | fIsExclusive(false) { |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 49 | // we assume ownership of the resource, and will unref it when we die |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 50 | SkASSERT(resource); |
skia.committer@gmail.com | 6c77816 | 2012-09-06 02:01:13 +0000 | [diff] [blame] | 51 | resource->ref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 52 | } |
| 53 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 54 | GrResourceCacheEntry::~GrResourceCacheEntry() { |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 55 | fResource->setCacheEntry(NULL); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 56 | fResource->unref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 57 | } |
| 58 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 59 | #ifdef SK_DEBUG |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 60 | void GrResourceCacheEntry::validate() const { |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 61 | SkASSERT(fResourceCache); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 62 | SkASSERT(fResource); |
| 63 | SkASSERT(fResource->getCacheEntry() == this); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 64 | SkASSERT(fResource->gpuMemorySize() == fCachedSize); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 65 | fResource->validate(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 66 | } |
| 67 | #endif |
| 68 | |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 69 | void GrResourceCacheEntry::didChangeResourceSize() { |
| 70 | size_t oldSize = fCachedSize; |
| 71 | fCachedSize = fResource->gpuMemorySize(); |
| 72 | if (fCachedSize > oldSize) { |
| 73 | fResourceCache->didIncreaseResourceSize(this, fCachedSize - oldSize); |
| 74 | } else if (fCachedSize < oldSize) { |
| 75 | fResourceCache->didDecreaseResourceSize(this, oldSize - fCachedSize); |
| 76 | } |
| 77 | } |
| 78 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 79 | /////////////////////////////////////////////////////////////////////////////// |
| 80 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame^] | 81 | GrResourceCache::GrResourceCache(const GrDrawTargetCaps* caps, int maxCount, size_t maxBytes) |
| 82 | : fMaxCount(maxCount) |
| 83 | , fMaxBytes(maxBytes) |
| 84 | , fCaps(SkRef(caps)) { |
robertphillips@google.com | 5955202 | 2012-08-31 13:07:37 +0000 | [diff] [blame] | 85 | #if GR_CACHE_STATS |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame] | 86 | fHighWaterEntryCount = 0; |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame] | 87 | fHighWaterEntryBytes = 0; |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame] | 88 | #endif |
| 89 | |
| 90 | fEntryCount = 0; |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame] | 91 | fEntryBytes = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 92 | |
commit-bot@chromium.org | cae27fe | 2013-07-10 10:14:35 +0000 | [diff] [blame] | 93 | fPurging = false; |
| 94 | |
| 95 | fOverbudgetCB = NULL; |
| 96 | fOverbudgetData = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 97 | } |
| 98 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 99 | GrResourceCache::~GrResourceCache() { |
| 100 | GrAutoResourceCacheValidate atcv(this); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 101 | |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 102 | EntryList::Iter iter; |
| 103 | |
| 104 | // Unlike the removeAll, here we really remove everything, including locked resources. |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 105 | while (GrResourceCacheEntry* entry = fList.head()) { |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 106 | GrAutoResourceCacheValidate atcv(this); |
| 107 | |
| 108 | // remove from our cache |
| 109 | fCache.remove(entry->fKey, entry); |
| 110 | |
| 111 | // remove from our llist |
robertphillips@google.com | 209a114 | 2012-10-31 12:25:21 +0000 | [diff] [blame] | 112 | this->internalDetach(entry); |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 113 | |
| 114 | delete entry; |
| 115 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 116 | } |
| 117 | |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 118 | void GrResourceCache::getLimits(int* maxResources, size_t* maxResourceBytes) const{ |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 119 | if (maxResources) { |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 120 | *maxResources = fMaxCount; |
| 121 | } |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 122 | if (maxResourceBytes) { |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 123 | *maxResourceBytes = fMaxBytes; |
| 124 | } |
| 125 | } |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 126 | |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 127 | void GrResourceCache::setLimits(int maxResources, size_t maxResourceBytes) { |
| 128 | bool smaller = (maxResources < fMaxCount) || (maxResourceBytes < fMaxBytes); |
| 129 | |
| 130 | fMaxCount = maxResources; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 131 | fMaxBytes = maxResourceBytes; |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 132 | |
| 133 | if (smaller) { |
| 134 | this->purgeAsNeeded(); |
| 135 | } |
| 136 | } |
| 137 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame^] | 138 | void GrResourceCache::internalDetach(GrResourceCacheEntry* entry) { |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 139 | fList.remove(entry); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame^] | 140 | fEntryCount -= 1; |
| 141 | fEntryBytes -= entry->fCachedSize; |
Brian Salomon | 9323b8b | 2014-10-07 15:07:38 -0400 | [diff] [blame] | 142 | } |
| 143 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame^] | 144 | void GrResourceCache::attachToHead(GrResourceCacheEntry* entry) { |
Brian Salomon | 9323b8b | 2014-10-07 15:07:38 -0400 | [diff] [blame] | 145 | fList.addToHead(entry); |
| 146 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame^] | 147 | fEntryCount += 1; |
| 148 | fEntryBytes += entry->fCachedSize; |
Brian Salomon | 9323b8b | 2014-10-07 15:07:38 -0400 | [diff] [blame] | 149 | |
| 150 | #if GR_CACHE_STATS |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame^] | 151 | if (fHighWaterEntryCount < fEntryCount) { |
| 152 | fHighWaterEntryCount = fEntryCount; |
Brian Salomon | 9323b8b | 2014-10-07 15:07:38 -0400 | [diff] [blame] | 153 | } |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame^] | 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 | |
robertphillips@google.com | 209a114 | 2012-10-31 12:25:21 +0000 | [diff] [blame] | 160 | // This functor just searches for an entry with only a single ref (from |
| 161 | // the texture cache itself). Presumably in this situation no one else |
| 162 | // is relying on the texture. |
| 163 | class GrTFindUnreffedFunctor { |
| 164 | public: |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 165 | bool operator()(const GrResourceCacheEntry* entry) const { |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame^] | 166 | return entry->resource()->isPurgable(); |
robertphillips@google.com | 209a114 | 2012-10-31 12:25:21 +0000 | [diff] [blame] | 167 | } |
| 168 | }; |
| 169 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame^] | 170 | |
| 171 | void GrResourceCache::makeResourceMRU(GrGpuResource* resource) { |
| 172 | GrResourceCacheEntry* entry = resource->getCacheEntry(); |
| 173 | if (entry) { |
| 174 | this->internalDetach(entry); |
| 175 | this->attachToHead(entry); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | void GrResourceCache::notifyPurgable(const GrGpuResource* resource) { |
| 180 | // Remove scratch textures from the cache the moment they become purgeable if |
| 181 | // scratch texture reuse is turned off. |
| 182 | SkASSERT(resource->getCacheEntry()); |
| 183 | if (resource->getCacheEntry()->key().getResourceType() == GrTexturePriv::ResourceType() && |
| 184 | resource->fIsScratch && |
| 185 | !fCaps->reuseScratchTextures() && |
| 186 | !(static_cast<const GrTexture*>(resource)->desc().fFlags & |
| 187 | kRenderTarget_GrTextureFlagBit)) { |
| 188 | this->deleteResource(resource->getCacheEntry()); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | GrGpuResource* GrResourceCache::find(const GrResourceKey& key) { |
robertphillips@google.com | a9b0623 | 2012-08-30 11:06:31 +0000 | [diff] [blame] | 193 | GrAutoResourceCacheValidate atcv(this); |
| 194 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 195 | GrResourceCacheEntry* entry = NULL; |
robertphillips@google.com | 209a114 | 2012-10-31 12:25:21 +0000 | [diff] [blame] | 196 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame^] | 197 | entry = fCache.find(key); |
robertphillips@google.com | 209a114 | 2012-10-31 12:25:21 +0000 | [diff] [blame] | 198 | |
robertphillips@google.com | a9b0623 | 2012-08-30 11:06:31 +0000 | [diff] [blame] | 199 | if (NULL == entry) { |
| 200 | return NULL; |
| 201 | } |
| 202 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame^] | 203 | // Make this resource MRU |
| 204 | this->internalDetach(entry); |
| 205 | this->attachToHead(entry); |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 206 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame^] | 207 | // GrResourceCache2 is responsible for scratch resources. |
| 208 | SkASSERT(GrGpuResource::kNo_IsScratch == entry->resource()->fIsScratch); |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 209 | return entry->fResource; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 210 | } |
| 211 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame^] | 212 | void GrResourceCache::addResource(const GrResourceKey& key, GrGpuResource* resource) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 213 | SkASSERT(NULL == resource->getCacheEntry()); |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 214 | // we don't expect to create new resources during a purge. In theory |
| 215 | // this could cause purgeAsNeeded() into an infinite loop (e.g. |
| 216 | // each resource destroyed creates and locks 2 resources and |
| 217 | // unlocks 1 thereby causing a new purge). |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 218 | SkASSERT(!fPurging); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 219 | GrAutoResourceCacheValidate atcv(this); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 220 | |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 221 | GrResourceCacheEntry* entry = SkNEW_ARGS(GrResourceCacheEntry, (this, key, resource)); |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 222 | resource->setCacheEntry(entry); |
| 223 | |
robertphillips@google.com | 209a114 | 2012-10-31 12:25:21 +0000 | [diff] [blame] | 224 | this->attachToHead(entry); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 225 | fCache.insert(key, entry); |
Brian Salomon | 9323b8b | 2014-10-07 15:07:38 -0400 | [diff] [blame] | 226 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame^] | 227 | this->purgeAsNeeded(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 228 | } |
| 229 | |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 230 | void GrResourceCache::didIncreaseResourceSize(const GrResourceCacheEntry* entry, size_t amountInc) { |
| 231 | fEntryBytes += amountInc; |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 232 | this->purgeAsNeeded(); |
| 233 | } |
| 234 | |
| 235 | void GrResourceCache::didDecreaseResourceSize(const GrResourceCacheEntry* entry, size_t amountDec) { |
| 236 | fEntryBytes -= amountDec; |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 237 | #ifdef SK_DEBUG |
| 238 | this->validate(); |
| 239 | #endif |
| 240 | } |
| 241 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 242 | /** |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 243 | * Destroying a resource may potentially trigger the unlock of additional |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 244 | * resources which in turn will trigger a nested purge. We block the nested |
| 245 | * purge using the fPurging variable. However, the initial purge will keep |
| 246 | * looping until either all resources in the cache are unlocked or we've met |
| 247 | * the budget. There is an assertion in createAndLock to check against a |
| 248 | * resource's destructor inserting new resources into the cache. If these |
| 249 | * new resources were unlocked before purgeAsNeeded completed it could |
| 250 | * potentially make purgeAsNeeded loop infinitely. |
robertphillips@google.com | 41d2532 | 2013-07-18 17:12:57 +0000 | [diff] [blame] | 251 | * |
| 252 | * extraCount and extraBytes are added to the current resource totals to account |
| 253 | * for incoming resources (e.g., GrContext is about to add 10MB split between |
| 254 | * 10 textures). |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 255 | */ |
robertphillips@google.com | 41d2532 | 2013-07-18 17:12:57 +0000 | [diff] [blame] | 256 | void GrResourceCache::purgeAsNeeded(int extraCount, size_t extraBytes) { |
commit-bot@chromium.org | cae27fe | 2013-07-10 10:14:35 +0000 | [diff] [blame] | 257 | if (fPurging) { |
| 258 | return; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 259 | } |
commit-bot@chromium.org | cae27fe | 2013-07-10 10:14:35 +0000 | [diff] [blame] | 260 | |
| 261 | fPurging = true; |
| 262 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 263 | this->purgeInvalidated(); |
| 264 | |
robertphillips@google.com | 41d2532 | 2013-07-18 17:12:57 +0000 | [diff] [blame] | 265 | this->internalPurge(extraCount, extraBytes); |
skia.committer@gmail.com | a799198 | 2013-07-19 07:00:57 +0000 | [diff] [blame] | 266 | if (((fEntryCount+extraCount) > fMaxCount || |
robertphillips@google.com | 41d2532 | 2013-07-18 17:12:57 +0000 | [diff] [blame] | 267 | (fEntryBytes+extraBytes) > fMaxBytes) && |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 268 | fOverbudgetCB) { |
commit-bot@chromium.org | cae27fe | 2013-07-10 10:14:35 +0000 | [diff] [blame] | 269 | // Despite the purge we're still over budget. See if Ganesh can |
| 270 | // release some resources and purge again. |
| 271 | if ((*fOverbudgetCB)(fOverbudgetData)) { |
robertphillips@google.com | 41d2532 | 2013-07-18 17:12:57 +0000 | [diff] [blame] | 272 | this->internalPurge(extraCount, extraBytes); |
commit-bot@chromium.org | cae27fe | 2013-07-10 10:14:35 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
| 276 | fPurging = false; |
| 277 | } |
| 278 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 279 | void GrResourceCache::purgeInvalidated() { |
| 280 | SkTDArray<GrResourceInvalidatedMessage> invalidated; |
| 281 | fInvalidationInbox.poll(&invalidated); |
| 282 | |
| 283 | for (int i = 0; i < invalidated.count(); i++) { |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 284 | while (GrResourceCacheEntry* entry = fCache.find(invalidated[i].key, GrTFindUnreffedFunctor())) { |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 285 | this->deleteResource(entry); |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 290 | void GrResourceCache::deleteResource(GrResourceCacheEntry* entry) { |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame^] | 291 | SkASSERT(entry->fResource->isPurgable()); |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 292 | |
| 293 | // remove from our cache |
| 294 | fCache.remove(entry->key(), entry); |
| 295 | |
| 296 | // remove from our llist |
| 297 | this->internalDetach(entry); |
| 298 | delete entry; |
| 299 | } |
| 300 | |
robertphillips@google.com | 41d2532 | 2013-07-18 17:12:57 +0000 | [diff] [blame] | 301 | void GrResourceCache::internalPurge(int extraCount, size_t extraBytes) { |
commit-bot@chromium.org | cae27fe | 2013-07-10 10:14:35 +0000 | [diff] [blame] | 302 | SkASSERT(fPurging); |
| 303 | |
| 304 | bool withinBudget = false; |
| 305 | bool changed = false; |
| 306 | |
| 307 | // The purging process is repeated several times since one pass |
| 308 | // may free up other resources |
| 309 | do { |
| 310 | EntryList::Iter iter; |
| 311 | |
| 312 | changed = false; |
| 313 | |
| 314 | // Note: the following code relies on the fact that the |
| 315 | // doubly linked list doesn't invalidate its data/pointers |
| 316 | // outside of the specific area where a deletion occurs (e.g., |
| 317 | // in internalDetach) |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 318 | GrResourceCacheEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart); |
commit-bot@chromium.org | cae27fe | 2013-07-10 10:14:35 +0000 | [diff] [blame] | 319 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 320 | while (entry) { |
commit-bot@chromium.org | cae27fe | 2013-07-10 10:14:35 +0000 | [diff] [blame] | 321 | GrAutoResourceCacheValidate atcv(this); |
| 322 | |
skia.committer@gmail.com | a799198 | 2013-07-19 07:00:57 +0000 | [diff] [blame] | 323 | if ((fEntryCount+extraCount) <= fMaxCount && |
robertphillips@google.com | 41d2532 | 2013-07-18 17:12:57 +0000 | [diff] [blame] | 324 | (fEntryBytes+extraBytes) <= fMaxBytes) { |
commit-bot@chromium.org | cae27fe | 2013-07-10 10:14:35 +0000 | [diff] [blame] | 325 | withinBudget = true; |
| 326 | break; |
| 327 | } |
| 328 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 329 | GrResourceCacheEntry* prev = iter.prev(); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame^] | 330 | if (entry->fResource->isPurgable()) { |
commit-bot@chromium.org | cae27fe | 2013-07-10 10:14:35 +0000 | [diff] [blame] | 331 | changed = true; |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 332 | this->deleteResource(entry); |
commit-bot@chromium.org | cae27fe | 2013-07-10 10:14:35 +0000 | [diff] [blame] | 333 | } |
| 334 | entry = prev; |
| 335 | } |
| 336 | } while (!withinBudget && changed); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 337 | } |
| 338 | |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 339 | void GrResourceCache::purgeAllUnlocked() { |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 340 | GrAutoResourceCacheValidate atcv(this); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 341 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 342 | // we can have one GrCacheable holding a lock on another |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 343 | // so we don't want to just do a simple loop kicking each |
| 344 | // entry out. Instead change the budget and purge. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 345 | |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 346 | size_t savedMaxBytes = fMaxBytes; |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 347 | int savedMaxCount = fMaxCount; |
| 348 | fMaxBytes = (size_t) -1; |
| 349 | fMaxCount = 0; |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 350 | this->purgeAsNeeded(); |
| 351 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 352 | #ifdef SK_DEBUG |
twiz@google.com | 0ec107f | 2012-02-21 19:15:53 +0000 | [diff] [blame] | 353 | if (!fCache.count()) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 354 | SkASSERT(fList.isEmpty()); |
twiz@google.com | 0ec107f | 2012-02-21 19:15:53 +0000 | [diff] [blame] | 355 | } |
| 356 | #endif |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 357 | |
| 358 | fMaxBytes = savedMaxBytes; |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 359 | fMaxCount = savedMaxCount; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /////////////////////////////////////////////////////////////////////////////// |
| 363 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 364 | #ifdef SK_DEBUG |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 365 | size_t GrResourceCache::countBytes(const EntryList& list) { |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 366 | size_t bytes = 0; |
| 367 | |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 368 | EntryList::Iter iter; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 369 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 370 | const GrResourceCacheEntry* entry = iter.init(const_cast<EntryList&>(list), |
| 371 | EntryList::Iter::kTail_IterStart); |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 372 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 373 | for ( ; entry; entry = iter.prev()) { |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 374 | bytes += entry->resource()->gpuMemorySize(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 375 | } |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 376 | return bytes; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 377 | } |
| 378 | |
reed@google.com | b89a643 | 2011-02-07 13:20:30 +0000 | [diff] [blame] | 379 | static bool both_zero_or_nonzero(int count, size_t bytes) { |
| 380 | return (count == 0 && bytes == 0) || (count > 0 && bytes > 0); |
| 381 | } |
reed@google.com | b89a643 | 2011-02-07 13:20:30 +0000 | [diff] [blame] | 382 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 383 | void GrResourceCache::validate() const { |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 384 | fList.validate(); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 385 | SkASSERT(both_zero_or_nonzero(fEntryCount, fEntryBytes)); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame^] | 386 | SkASSERT(fEntryCount == fCache.count()); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 387 | |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 388 | EntryList::Iter iter; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 389 | |
Brian Salomon | 9323b8b | 2014-10-07 15:07:38 -0400 | [diff] [blame] | 390 | // check that the shareable entries are okay |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame^] | 391 | const GrResourceCacheEntry* entry = iter.init(const_cast<EntryList&>(fList), |
| 392 | EntryList::Iter::kHead_IterStart); |
Brian Salomon | 9323b8b | 2014-10-07 15:07:38 -0400 | [diff] [blame] | 393 | |
robertphillips@google.com | d07cb0c | 2012-08-30 19:22:29 +0000 | [diff] [blame] | 394 | int count = 0; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 395 | for ( ; entry; entry = iter.next()) { |
robertphillips@google.com | d07cb0c | 2012-08-30 19:22:29 +0000 | [diff] [blame] | 396 | entry->validate(); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 397 | SkASSERT(fCache.find(entry->key())); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 398 | count += 1; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 399 | } |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame^] | 400 | SkASSERT(count == fEntryCount); |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 401 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame^] | 402 | size_t bytes = this->countBytes(fList); |
| 403 | SkASSERT(bytes == fEntryBytes); |
| 404 | SkASSERT(fList.countEntries() == fEntryCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 405 | } |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 406 | #endif // SK_DEBUG |
robertphillips@google.com | 5955202 | 2012-08-31 13:07:37 +0000 | [diff] [blame] | 407 | |
| 408 | #if GR_CACHE_STATS |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame] | 409 | |
robertphillips@google.com | 9fbcad0 | 2012-09-09 14:44:15 +0000 | [diff] [blame] | 410 | void GrResourceCache::printStats() { |
| 411 | int locked = 0; |
| 412 | |
| 413 | EntryList::Iter iter; |
| 414 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 415 | GrResourceCacheEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart); |
robertphillips@google.com | 9fbcad0 | 2012-09-09 14:44:15 +0000 | [diff] [blame] | 416 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 417 | for ( ; entry; entry = iter.prev()) { |
robertphillips@google.com | 9fbcad0 | 2012-09-09 14:44:15 +0000 | [diff] [blame] | 418 | if (entry->fResource->getRefCnt() > 1) { |
| 419 | ++locked; |
| 420 | } |
| 421 | } |
| 422 | |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame] | 423 | SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes); |
robertphillips@google.com | 9fbcad0 | 2012-09-09 14:44:15 +0000 | [diff] [blame] | 424 | SkDebugf("\t\tEntry Count: current %d (%d locked) high %d\n", |
| 425 | fEntryCount, locked, fHighWaterEntryCount); |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame] | 426 | SkDebugf("\t\tEntry Bytes: current %d high %d\n", |
| 427 | fEntryBytes, fHighWaterEntryBytes); |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame] | 428 | } |
| 429 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 430 | #endif |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 431 | |
| 432 | /////////////////////////////////////////////////////////////////////////////// |