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