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