bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2014 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. |
| 7 | */ |
| 8 | |
| 9 | |
| 10 | #include "GrResourceCache2.h" |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 11 | #include "GrGpuResource.h" |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 12 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 13 | #include "SkGr.h" |
| 14 | #include "SkMessageBus.h" |
| 15 | |
| 16 | DECLARE_SKMESSAGEBUS_MESSAGE(GrResourceInvalidatedMessage); |
| 17 | |
| 18 | ////////////////////////////////////////////////////////////////////////////// |
| 19 | |
bsalomon | fe369ee | 2014-11-10 11:59:06 -0800 | [diff] [blame] | 20 | GrResourceKey& GrResourceKey::NullScratchKey() { |
| 21 | static const GrCacheID::Key kBogusKey = { { {0} } }; |
| 22 | static GrCacheID kBogusID(ScratchDomain(), kBogusKey); |
| 23 | static GrResourceKey kNullScratchKey(kBogusID, NoneResourceType(), 0); |
| 24 | return kNullScratchKey; |
| 25 | } |
| 26 | |
| 27 | GrResourceKey::ResourceType GrResourceKey::NoneResourceType() { |
| 28 | static const ResourceType gNoneResourceType = GenerateResourceType(); |
| 29 | return gNoneResourceType; |
| 30 | } |
| 31 | |
| 32 | GrCacheID::Domain GrResourceKey::ScratchDomain() { |
| 33 | static const GrCacheID::Domain gDomain = GrCacheID::GenerateDomain(); |
| 34 | return gDomain; |
| 35 | } |
| 36 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 37 | GrResourceKey::ResourceType GrResourceKey::GenerateResourceType() { |
| 38 | static int32_t gNextType = 0; |
| 39 | |
| 40 | int32_t type = sk_atomic_inc(&gNextType); |
| 41 | if (type >= (1 << 8 * sizeof(ResourceType))) { |
| 42 | SkFAIL("Too many Resource Types"); |
| 43 | } |
| 44 | |
| 45 | return static_cast<ResourceType>(type); |
| 46 | } |
| 47 | |
bsalomon | fe369ee | 2014-11-10 11:59:06 -0800 | [diff] [blame] | 48 | ////////////////////////////////////////////////////////////////////////////// |
| 49 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 50 | class GrResourceCache2::AutoValidate : ::SkNoncopyable { |
| 51 | public: |
| 52 | AutoValidate(GrResourceCache2* cache) : fCache(cache) { cache->validate(); } |
| 53 | ~AutoValidate() { fCache->validate(); } |
| 54 | private: |
| 55 | GrResourceCache2* fCache; |
| 56 | }; |
| 57 | |
| 58 | ////////////////////////////////////////////////////////////////////////////// |
| 59 | |
| 60 | static const int kDefaultMaxCount = 2 * (1 << 10); |
| 61 | static const size_t kDefaultMaxSize = 96 * (1 << 20); |
| 62 | |
| 63 | GrResourceCache2::GrResourceCache2() |
| 64 | : fMaxCount(kDefaultMaxCount) |
| 65 | , fMaxBytes(kDefaultMaxSize) |
| 66 | #if GR_CACHE_STATS |
| 67 | , fHighWaterCount(0) |
| 68 | , fHighWaterBytes(0) |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 69 | , fBudgetedHighWaterCount(0) |
| 70 | , fBudgetedHighWaterBytes(0) |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 71 | #endif |
| 72 | , fCount(0) |
| 73 | , fBytes(0) |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 74 | , fBudgetedCount(0) |
| 75 | , fBudgetedBytes(0) |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 76 | , fPurging(false) |
| 77 | , fNewlyPurgableResourceWhilePurging(false) |
| 78 | , fOverBudgetCB(NULL) |
| 79 | , fOverBudgetData(NULL) { |
| 80 | } |
| 81 | |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 82 | GrResourceCache2::~GrResourceCache2() { |
| 83 | this->releaseAll(); |
| 84 | } |
| 85 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 86 | void GrResourceCache2::setLimits(int count, size_t bytes) { |
| 87 | fMaxCount = count; |
| 88 | fMaxBytes = bytes; |
| 89 | this->purgeAsNeeded(); |
| 90 | } |
| 91 | |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 92 | void GrResourceCache2::insertResource(GrGpuResource* resource) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 93 | SkASSERT(resource); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 94 | SkASSERT(!resource->wasDestroyed()); |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 95 | SkASSERT(!this->isInCache(resource)); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 96 | SkASSERT(!fPurging); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 97 | fResources.addToHead(resource); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 98 | |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 99 | size_t size = resource->gpuMemorySize(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 100 | ++fCount; |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 101 | fBytes += size; |
bsalomon | 82b1d62 | 2014-11-14 13:59:57 -0800 | [diff] [blame] | 102 | #if GR_CACHE_STATS |
| 103 | fHighWaterCount = SkTMax(fCount, fHighWaterCount); |
| 104 | fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes); |
| 105 | #endif |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 106 | if (resource->cacheAccess().isBudgeted()) { |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 107 | ++fBudgetedCount; |
| 108 | fBudgetedBytes += size; |
| 109 | #if GR_CACHE_STATS |
| 110 | fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount); |
| 111 | fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes); |
| 112 | #endif |
| 113 | } |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 114 | if (!resource->cacheAccess().getScratchKey().isNullScratch()) { |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 115 | SkASSERT(!resource->cacheAccess().isWrapped()); |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 116 | fScratchMap.insert(resource->cacheAccess().getScratchKey(), resource); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 117 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 118 | |
| 119 | this->purgeAsNeeded(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void GrResourceCache2::removeResource(GrGpuResource* resource) { |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 123 | SkASSERT(this->isInCache(resource)); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 124 | |
| 125 | size_t size = resource->gpuMemorySize(); |
| 126 | --fCount; |
| 127 | fBytes -= size; |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 128 | if (resource->cacheAccess().isBudgeted()) { |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 129 | --fBudgetedCount; |
| 130 | fBudgetedBytes -= size; |
| 131 | } |
| 132 | |
| 133 | fResources.remove(resource); |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 134 | if (!resource->cacheAccess().getScratchKey().isNullScratch()) { |
| 135 | fScratchMap.remove(resource->cacheAccess().getScratchKey(), resource); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 136 | } |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 137 | if (const GrResourceKey* contentKey = resource->cacheAccess().getContentKey()) { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 138 | fContentHash.remove(*contentKey); |
| 139 | } |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 140 | this->validate(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void GrResourceCache2::abandonAll() { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 144 | AutoValidate av(this); |
| 145 | |
| 146 | SkASSERT(!fPurging); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 147 | while (GrGpuResource* head = fResources.head()) { |
| 148 | SkASSERT(!head->wasDestroyed()); |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 149 | head->cacheAccess().abandon(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 150 | // abandon should have already removed this from the list. |
| 151 | SkASSERT(head != fResources.head()); |
| 152 | } |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 153 | SkASSERT(!fScratchMap.count()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 154 | SkASSERT(!fContentHash.count()); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 155 | SkASSERT(!fCount); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 156 | SkASSERT(!fBytes); |
| 157 | SkASSERT(!fBudgetedCount); |
| 158 | SkASSERT(!fBudgetedBytes); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void GrResourceCache2::releaseAll() { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 162 | AutoValidate av(this); |
| 163 | |
| 164 | SkASSERT(!fPurging); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 165 | while (GrGpuResource* head = fResources.head()) { |
| 166 | SkASSERT(!head->wasDestroyed()); |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 167 | head->cacheAccess().release(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 168 | // release should have already removed this from the list. |
| 169 | SkASSERT(head != fResources.head()); |
| 170 | } |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 171 | SkASSERT(!fScratchMap.count()); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 172 | SkASSERT(!fCount); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 173 | SkASSERT(!fBytes); |
| 174 | SkASSERT(!fBudgetedCount); |
| 175 | SkASSERT(!fBudgetedBytes); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 176 | } |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 177 | |
| 178 | class GrResourceCache2::AvailableForScratchUse { |
| 179 | public: |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 180 | AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendingIO) { } |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 181 | |
| 182 | bool operator()(const GrGpuResource* resource) const { |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 183 | if (resource->internalHasRef() || !resource->cacheAccess().isScratch()) { |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 184 | return false; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 185 | } |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 186 | return !fRejectPendingIO || !resource->internalHasPendingIO(); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 187 | } |
bsalomon | 1e2530b | 2014-10-09 09:57:18 -0700 | [diff] [blame] | 188 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 189 | private: |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 190 | bool fRejectPendingIO; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | GrGpuResource* GrResourceCache2::findAndRefScratchResource(const GrResourceKey& scratchKey, |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 194 | uint32_t flags) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 195 | SkASSERT(!fPurging); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 196 | SkASSERT(scratchKey.isScratch()); |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 197 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 198 | GrGpuResource* resource; |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 199 | if (flags & (kPreferNoPendingIO_ScratchFlag | kRequireNoPendingIO_ScratchFlag)) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 200 | resource = fScratchMap.find(scratchKey, AvailableForScratchUse(true)); |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 201 | if (resource) { |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 202 | resource->ref(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 203 | this->makeResourceMRU(resource); |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 204 | this->validate(); |
| 205 | return resource; |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 206 | } else if (flags & kRequireNoPendingIO_ScratchFlag) { |
| 207 | return NULL; |
| 208 | } |
| 209 | // TODO: fail here when kPrefer is specified, we didn't find a resource without pending io, |
| 210 | // but there is still space in our budget for the resource. |
| 211 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 212 | resource = fScratchMap.find(scratchKey, AvailableForScratchUse(false)); |
| 213 | if (resource) { |
| 214 | resource->ref(); |
| 215 | this->makeResourceMRU(resource); |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 216 | this->validate(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 217 | } |
| 218 | return resource; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 219 | } |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 220 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame^] | 221 | void GrResourceCache2::willRemoveScratchKey(const GrGpuResource* resource) { |
| 222 | SkASSERT(resource->cacheAccess().isScratch()); |
| 223 | fScratchMap.remove(resource->cacheAccess().getScratchKey(), resource); |
| 224 | } |
| 225 | |
bsalomon | 6d4488c | 2014-11-11 07:27:16 -0800 | [diff] [blame] | 226 | bool GrResourceCache2::didSetContentKey(GrGpuResource* resource) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 227 | SkASSERT(!fPurging); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 228 | SkASSERT(resource); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 229 | SkASSERT(this->isInCache(resource)); |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 230 | SkASSERT(resource->cacheAccess().getContentKey()); |
| 231 | SkASSERT(!resource->cacheAccess().getContentKey()->isScratch()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 232 | |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 233 | GrGpuResource* res = fContentHash.find(*resource->cacheAccess().getContentKey()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 234 | if (NULL != res) { |
| 235 | return false; |
| 236 | } |
| 237 | |
| 238 | fContentHash.add(resource); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 239 | this->validate(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 240 | return true; |
| 241 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 242 | |
| 243 | void GrResourceCache2::makeResourceMRU(GrGpuResource* resource) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 244 | SkASSERT(!fPurging); |
| 245 | SkASSERT(resource); |
| 246 | SkASSERT(this->isInCache(resource)); |
| 247 | fResources.remove(resource); |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 248 | fResources.addToHead(resource); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 249 | } |
| 250 | |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 251 | void GrResourceCache2::notifyPurgable(GrGpuResource* resource) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 252 | SkASSERT(resource); |
| 253 | SkASSERT(this->isInCache(resource)); |
| 254 | SkASSERT(resource->isPurgable()); |
| 255 | |
| 256 | // We can't purge if in the middle of purging because purge is iterating. Instead record |
| 257 | // that additional resources became purgable. |
| 258 | if (fPurging) { |
| 259 | fNewlyPurgableResourceWhilePurging = true; |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | // Purge the resource if we're over budget |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 264 | bool overBudget = fBudgetedCount > fMaxCount || fBudgetedBytes > fMaxBytes; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 265 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 266 | // Also purge if the resource has neither a valid scratch key nor a content key. |
| 267 | bool noKey = !resource->cacheAccess().isScratch() && |
| 268 | (NULL == resource->cacheAccess().getContentKey()); |
| 269 | |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 270 | // Wrapped resources should never have a key. |
| 271 | SkASSERT(noKey || !resource->cacheAccess().isWrapped()); |
| 272 | |
| 273 | // And purge if the resource is wrapped |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 274 | if (overBudget || noKey) { |
| 275 | SkDEBUGCODE(int beforeCount = fCount;) |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 276 | resource->cacheAccess().release(); |
| 277 | // We should at least free this resource, perhaps dependent resources as well. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 278 | SkASSERT(fCount < beforeCount); |
| 279 | } |
| 280 | |
| 281 | this->validate(); |
| 282 | } |
| 283 | |
| 284 | void GrResourceCache2::didChangeGpuMemorySize(const GrGpuResource* resource, size_t oldSize) { |
| 285 | // SkASSERT(!fPurging); GrPathRange increases size during flush. :( |
| 286 | SkASSERT(resource); |
| 287 | SkASSERT(this->isInCache(resource)); |
| 288 | |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 289 | ptrdiff_t delta = resource->gpuMemorySize() - oldSize; |
| 290 | |
| 291 | fBytes += delta; |
bsalomon | 82b1d62 | 2014-11-14 13:59:57 -0800 | [diff] [blame] | 292 | #if GR_CACHE_STATS |
| 293 | fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes); |
| 294 | #endif |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 295 | if (resource->cacheAccess().isBudgeted()) { |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 296 | fBudgetedBytes += delta; |
| 297 | #if GR_CACHE_STATS |
| 298 | fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes); |
| 299 | #endif |
| 300 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 301 | |
| 302 | this->purgeAsNeeded(); |
| 303 | this->validate(); |
| 304 | } |
| 305 | |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 306 | void GrResourceCache2::didChangeBudgetStatus(GrGpuResource* resource) { |
| 307 | SkASSERT(!fPurging); |
| 308 | SkASSERT(resource); |
| 309 | SkASSERT(this->isInCache(resource)); |
| 310 | |
| 311 | size_t size = resource->gpuMemorySize(); |
| 312 | |
| 313 | if (resource->cacheAccess().isBudgeted()) { |
| 314 | ++fBudgetedCount; |
| 315 | fBudgetedBytes += size; |
| 316 | this->purgeAsNeeded(); |
| 317 | } else { |
| 318 | --fBudgetedCount; |
| 319 | fBudgetedBytes -= size; |
| 320 | } |
| 321 | |
| 322 | this->validate(); |
| 323 | } |
| 324 | |
| 325 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 326 | void GrResourceCache2::internalPurgeAsNeeded() { |
| 327 | SkASSERT(!fPurging); |
| 328 | SkASSERT(!fNewlyPurgableResourceWhilePurging); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 329 | SkASSERT(fBudgetedCount > fMaxCount || fBudgetedBytes > fMaxBytes); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 330 | |
| 331 | fPurging = true; |
| 332 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 333 | bool overBudget = true; |
| 334 | do { |
| 335 | fNewlyPurgableResourceWhilePurging = false; |
| 336 | ResourceList::Iter resourceIter; |
| 337 | GrGpuResource* resource = resourceIter.init(fResources, |
| 338 | ResourceList::Iter::kTail_IterStart); |
| 339 | |
| 340 | while (resource) { |
| 341 | GrGpuResource* prev = resourceIter.prev(); |
| 342 | if (resource->isPurgable()) { |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 343 | resource->cacheAccess().release(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 344 | } |
| 345 | resource = prev; |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 346 | if (fBudgetedCount <= fMaxCount && fBudgetedBytes <= fMaxBytes) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 347 | overBudget = false; |
| 348 | resource = NULL; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | if (!fNewlyPurgableResourceWhilePurging && overBudget && fOverBudgetCB) { |
| 353 | // Despite the purge we're still over budget. Call our over budget callback. |
| 354 | (*fOverBudgetCB)(fOverBudgetData); |
| 355 | } |
| 356 | } while (overBudget && fNewlyPurgableResourceWhilePurging); |
| 357 | |
| 358 | fNewlyPurgableResourceWhilePurging = false; |
| 359 | fPurging = false; |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 360 | this->validate(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | void GrResourceCache2::purgeAllUnlocked() { |
| 364 | SkASSERT(!fPurging); |
| 365 | SkASSERT(!fNewlyPurgableResourceWhilePurging); |
| 366 | |
| 367 | fPurging = true; |
| 368 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 369 | do { |
| 370 | fNewlyPurgableResourceWhilePurging = false; |
| 371 | ResourceList::Iter resourceIter; |
| 372 | GrGpuResource* resource = |
| 373 | resourceIter.init(fResources, ResourceList::Iter::kTail_IterStart); |
| 374 | |
| 375 | while (resource) { |
| 376 | GrGpuResource* prev = resourceIter.prev(); |
| 377 | if (resource->isPurgable()) { |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 378 | resource->cacheAccess().release(); |
| 379 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 380 | resource = prev; |
| 381 | } |
| 382 | |
| 383 | if (!fNewlyPurgableResourceWhilePurging && fCount && fOverBudgetCB) { |
| 384 | (*fOverBudgetCB)(fOverBudgetData); |
| 385 | } |
| 386 | } while (fNewlyPurgableResourceWhilePurging); |
| 387 | fPurging = false; |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 388 | this->validate(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | #ifdef SK_DEBUG |
| 392 | void GrResourceCache2::validate() const { |
| 393 | size_t bytes = 0; |
| 394 | int count = 0; |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 395 | int budgetedCount = 0; |
| 396 | size_t budgetedBytes = 0; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 397 | int locked = 0; |
| 398 | int scratch = 0; |
| 399 | int couldBeScratch = 0; |
| 400 | int content = 0; |
| 401 | |
| 402 | ResourceList::Iter iter; |
| 403 | GrGpuResource* resource = iter.init(fResources, ResourceList::Iter::kHead_IterStart); |
| 404 | for ( ; resource; resource = iter.next()) { |
| 405 | bytes += resource->gpuMemorySize(); |
| 406 | ++count; |
| 407 | |
| 408 | if (!resource->isPurgable()) { |
| 409 | ++locked; |
| 410 | } |
| 411 | |
| 412 | if (resource->cacheAccess().isScratch()) { |
| 413 | SkASSERT(NULL == resource->cacheAccess().getContentKey()); |
| 414 | ++scratch; |
| 415 | SkASSERT(fScratchMap.countForKey(resource->cacheAccess().getScratchKey())); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 416 | SkASSERT(!resource->cacheAccess().isWrapped()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 417 | } else if (!resource->cacheAccess().getScratchKey().isNullScratch()) { |
| 418 | SkASSERT(NULL != resource->cacheAccess().getContentKey()); |
| 419 | ++couldBeScratch; |
| 420 | SkASSERT(fScratchMap.countForKey(resource->cacheAccess().getScratchKey())); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 421 | SkASSERT(!resource->cacheAccess().isWrapped()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | if (const GrResourceKey* contentKey = resource->cacheAccess().getContentKey()) { |
| 425 | ++content; |
| 426 | SkASSERT(fContentHash.find(*contentKey) == resource); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 427 | SkASSERT(!resource->cacheAccess().isWrapped()); |
| 428 | } |
| 429 | |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 430 | if (resource->cacheAccess().isBudgeted()) { |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 431 | ++budgetedCount; |
| 432 | budgetedBytes += resource->gpuMemorySize(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 436 | SkASSERT(fBudgetedCount <= fCount); |
| 437 | SkASSERT(fBudgetedBytes <= fBudgetedBytes); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 438 | SkASSERT(bytes == fBytes); |
| 439 | SkASSERT(count == fCount); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 440 | SkASSERT(budgetedBytes == fBudgetedBytes); |
| 441 | SkASSERT(budgetedCount == fBudgetedCount); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 442 | #if GR_CACHE_STATS |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 443 | SkASSERT(fBudgetedHighWaterCount <= fHighWaterCount); |
| 444 | SkASSERT(fBudgetedHighWaterBytes <= fHighWaterBytes); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 445 | SkASSERT(bytes <= fHighWaterBytes); |
| 446 | SkASSERT(count <= fHighWaterCount); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 447 | SkASSERT(budgetedBytes <= fBudgetedHighWaterBytes); |
| 448 | SkASSERT(budgetedCount <= fBudgetedHighWaterCount); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 449 | #endif |
| 450 | SkASSERT(content == fContentHash.count()); |
| 451 | SkASSERT(scratch + couldBeScratch == fScratchMap.count()); |
| 452 | |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 453 | // This assertion is not currently valid because we can be in recursive notifyIsPurgable() |
| 454 | // calls. This will be fixed when subresource registration is explicit. |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 455 | // bool overBudget = budgetedBytes > fMaxBytes || budgetedCount > fMaxCount; |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 456 | // SkASSERT(!overBudget || locked == count || fPurging); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 457 | } |
| 458 | #endif |
| 459 | |
| 460 | #if GR_CACHE_STATS |
| 461 | void GrResourceCache2::printStats() const { |
| 462 | this->validate(); |
| 463 | |
| 464 | int locked = 0; |
| 465 | int scratch = 0; |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 466 | int wrapped = 0; |
| 467 | size_t unbudgetedSize = 0; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 468 | |
| 469 | ResourceList::Iter iter; |
| 470 | GrGpuResource* resource = iter.init(fResources, ResourceList::Iter::kHead_IterStart); |
| 471 | |
| 472 | for ( ; resource; resource = iter.next()) { |
| 473 | if (!resource->isPurgable()) { |
| 474 | ++locked; |
| 475 | } |
| 476 | if (resource->cacheAccess().isScratch()) { |
| 477 | ++scratch; |
| 478 | } |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 479 | if (resource->cacheAccess().isWrapped()) { |
| 480 | ++wrapped; |
| 481 | } |
| 482 | if (!resource->cacheAccess().isBudgeted()) { |
| 483 | unbudgetedSize += resource->gpuMemorySize(); |
| 484 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 485 | } |
| 486 | |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 487 | float countUtilization = (100.f * fBudgetedCount) / fMaxCount; |
| 488 | float byteUtilization = (100.f * fBudgetedBytes) / fMaxBytes; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 489 | |
| 490 | SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 491 | SkDebugf("\t\tEntry Count: current %d" |
| 492 | " (%d budgeted, %d wrapped, %d locked, %d scratch %.2g%% full), high %d\n", |
| 493 | fCount, fBudgetedCount, wrapped, locked, scratch, countUtilization, fHighWaterCount); |
| 494 | SkDebugf("\t\tEntry Bytes: current %d (budgeted %d, %.2g%% full, %d unbudgeted) high %d\n", |
| 495 | fBytes, fBudgetedBytes, byteUtilization, unbudgetedSize, fHighWaterBytes); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | #endif |