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