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