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 | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 264 | // Wrapped resources should never have a key. |
| 265 | SkASSERT(noKey || !resource->cacheAccess().isWrapped()); |
| 266 | |
| 267 | // And purge if the resource is wrapped |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 268 | if (overBudget || noKey) { |
| 269 | SkDEBUGCODE(int beforeCount = fCount;) |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 270 | resource->cacheAccess().release(); |
| 271 | // We should at least free this resource, perhaps dependent resources as well. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 272 | SkASSERT(fCount < beforeCount); |
| 273 | } |
| 274 | |
| 275 | this->validate(); |
| 276 | } |
| 277 | |
| 278 | void GrResourceCache2::didChangeGpuMemorySize(const GrGpuResource* resource, size_t oldSize) { |
| 279 | // SkASSERT(!fPurging); GrPathRange increases size during flush. :( |
| 280 | SkASSERT(resource); |
| 281 | SkASSERT(this->isInCache(resource)); |
| 282 | |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 283 | ptrdiff_t delta = resource->gpuMemorySize() - oldSize; |
| 284 | |
| 285 | fBytes += delta; |
bsalomon | 82b1d62 | 2014-11-14 13:59:57 -0800 | [diff] [blame] | 286 | #if GR_CACHE_STATS |
| 287 | fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes); |
| 288 | #endif |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 289 | if (resource->cacheAccess().isBudgeted()) { |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 290 | fBudgetedBytes += delta; |
| 291 | #if GR_CACHE_STATS |
| 292 | fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes); |
| 293 | #endif |
| 294 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 295 | |
| 296 | this->purgeAsNeeded(); |
| 297 | this->validate(); |
| 298 | } |
| 299 | |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 300 | void GrResourceCache2::didChangeBudgetStatus(GrGpuResource* resource) { |
| 301 | SkASSERT(!fPurging); |
| 302 | SkASSERT(resource); |
| 303 | SkASSERT(this->isInCache(resource)); |
| 304 | |
| 305 | size_t size = resource->gpuMemorySize(); |
| 306 | |
| 307 | if (resource->cacheAccess().isBudgeted()) { |
| 308 | ++fBudgetedCount; |
| 309 | fBudgetedBytes += size; |
| 310 | this->purgeAsNeeded(); |
| 311 | } else { |
| 312 | --fBudgetedCount; |
| 313 | fBudgetedBytes -= size; |
| 314 | } |
| 315 | |
| 316 | this->validate(); |
| 317 | } |
| 318 | |
| 319 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 320 | void GrResourceCache2::internalPurgeAsNeeded() { |
| 321 | SkASSERT(!fPurging); |
| 322 | SkASSERT(!fNewlyPurgableResourceWhilePurging); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 323 | SkASSERT(fBudgetedCount > fMaxCount || fBudgetedBytes > fMaxBytes); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 324 | |
| 325 | fPurging = true; |
| 326 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 327 | bool overBudget = true; |
| 328 | do { |
| 329 | fNewlyPurgableResourceWhilePurging = false; |
| 330 | ResourceList::Iter resourceIter; |
| 331 | GrGpuResource* resource = resourceIter.init(fResources, |
| 332 | ResourceList::Iter::kTail_IterStart); |
| 333 | |
| 334 | while (resource) { |
| 335 | GrGpuResource* prev = resourceIter.prev(); |
| 336 | if (resource->isPurgable()) { |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 337 | resource->cacheAccess().release(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 338 | } |
| 339 | resource = prev; |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 340 | if (fBudgetedCount <= fMaxCount && fBudgetedBytes <= fMaxBytes) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 341 | overBudget = false; |
| 342 | resource = NULL; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | if (!fNewlyPurgableResourceWhilePurging && overBudget && fOverBudgetCB) { |
| 347 | // Despite the purge we're still over budget. Call our over budget callback. |
| 348 | (*fOverBudgetCB)(fOverBudgetData); |
| 349 | } |
| 350 | } while (overBudget && fNewlyPurgableResourceWhilePurging); |
| 351 | |
| 352 | fNewlyPurgableResourceWhilePurging = false; |
| 353 | fPurging = false; |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 354 | this->validate(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | void GrResourceCache2::purgeAllUnlocked() { |
| 358 | SkASSERT(!fPurging); |
| 359 | SkASSERT(!fNewlyPurgableResourceWhilePurging); |
| 360 | |
| 361 | fPurging = true; |
| 362 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 363 | do { |
| 364 | fNewlyPurgableResourceWhilePurging = false; |
| 365 | ResourceList::Iter resourceIter; |
| 366 | GrGpuResource* resource = |
| 367 | resourceIter.init(fResources, ResourceList::Iter::kTail_IterStart); |
| 368 | |
| 369 | while (resource) { |
| 370 | GrGpuResource* prev = resourceIter.prev(); |
| 371 | if (resource->isPurgable()) { |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 372 | resource->cacheAccess().release(); |
| 373 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 374 | resource = prev; |
| 375 | } |
| 376 | |
| 377 | if (!fNewlyPurgableResourceWhilePurging && fCount && fOverBudgetCB) { |
| 378 | (*fOverBudgetCB)(fOverBudgetData); |
| 379 | } |
| 380 | } while (fNewlyPurgableResourceWhilePurging); |
| 381 | fPurging = false; |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 382 | this->validate(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | #ifdef SK_DEBUG |
| 386 | void GrResourceCache2::validate() const { |
| 387 | size_t bytes = 0; |
| 388 | int count = 0; |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 389 | int budgetedCount = 0; |
| 390 | size_t budgetedBytes = 0; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 391 | int locked = 0; |
| 392 | int scratch = 0; |
| 393 | int couldBeScratch = 0; |
| 394 | int content = 0; |
| 395 | |
| 396 | ResourceList::Iter iter; |
| 397 | GrGpuResource* resource = iter.init(fResources, ResourceList::Iter::kHead_IterStart); |
| 398 | for ( ; resource; resource = iter.next()) { |
| 399 | bytes += resource->gpuMemorySize(); |
| 400 | ++count; |
| 401 | |
| 402 | if (!resource->isPurgable()) { |
| 403 | ++locked; |
| 404 | } |
| 405 | |
| 406 | if (resource->cacheAccess().isScratch()) { |
| 407 | SkASSERT(NULL == resource->cacheAccess().getContentKey()); |
| 408 | ++scratch; |
| 409 | SkASSERT(fScratchMap.countForKey(resource->cacheAccess().getScratchKey())); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 410 | SkASSERT(!resource->cacheAccess().isWrapped()); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 411 | } else if (resource->cacheAccess().getScratchKey().isValid()) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 412 | SkASSERT(NULL != resource->cacheAccess().getContentKey()); |
| 413 | ++couldBeScratch; |
| 414 | SkASSERT(fScratchMap.countForKey(resource->cacheAccess().getScratchKey())); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 415 | SkASSERT(!resource->cacheAccess().isWrapped()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | if (const GrResourceKey* contentKey = resource->cacheAccess().getContentKey()) { |
| 419 | ++content; |
| 420 | SkASSERT(fContentHash.find(*contentKey) == resource); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 421 | SkASSERT(!resource->cacheAccess().isWrapped()); |
| 422 | } |
| 423 | |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 424 | if (resource->cacheAccess().isBudgeted()) { |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 425 | ++budgetedCount; |
| 426 | budgetedBytes += resource->gpuMemorySize(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 430 | SkASSERT(fBudgetedCount <= fCount); |
| 431 | SkASSERT(fBudgetedBytes <= fBudgetedBytes); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 432 | SkASSERT(bytes == fBytes); |
| 433 | SkASSERT(count == fCount); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 434 | SkASSERT(budgetedBytes == fBudgetedBytes); |
| 435 | SkASSERT(budgetedCount == fBudgetedCount); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 436 | #if GR_CACHE_STATS |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 437 | SkASSERT(fBudgetedHighWaterCount <= fHighWaterCount); |
| 438 | SkASSERT(fBudgetedHighWaterBytes <= fHighWaterBytes); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 439 | SkASSERT(bytes <= fHighWaterBytes); |
| 440 | SkASSERT(count <= fHighWaterCount); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 441 | SkASSERT(budgetedBytes <= fBudgetedHighWaterBytes); |
| 442 | SkASSERT(budgetedCount <= fBudgetedHighWaterCount); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 443 | #endif |
| 444 | SkASSERT(content == fContentHash.count()); |
| 445 | SkASSERT(scratch + couldBeScratch == fScratchMap.count()); |
| 446 | |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 447 | // This assertion is not currently valid because we can be in recursive notifyIsPurgable() |
| 448 | // calls. This will be fixed when subresource registration is explicit. |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 449 | // bool overBudget = budgetedBytes > fMaxBytes || budgetedCount > fMaxCount; |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 450 | // SkASSERT(!overBudget || locked == count || fPurging); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 451 | } |
| 452 | #endif |
| 453 | |
| 454 | #if GR_CACHE_STATS |
| 455 | void GrResourceCache2::printStats() const { |
| 456 | this->validate(); |
| 457 | |
| 458 | int locked = 0; |
| 459 | int scratch = 0; |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 460 | int wrapped = 0; |
| 461 | size_t unbudgetedSize = 0; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 462 | |
| 463 | ResourceList::Iter iter; |
| 464 | GrGpuResource* resource = iter.init(fResources, ResourceList::Iter::kHead_IterStart); |
| 465 | |
| 466 | for ( ; resource; resource = iter.next()) { |
| 467 | if (!resource->isPurgable()) { |
| 468 | ++locked; |
| 469 | } |
| 470 | if (resource->cacheAccess().isScratch()) { |
| 471 | ++scratch; |
| 472 | } |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 473 | if (resource->cacheAccess().isWrapped()) { |
| 474 | ++wrapped; |
| 475 | } |
| 476 | if (!resource->cacheAccess().isBudgeted()) { |
| 477 | unbudgetedSize += resource->gpuMemorySize(); |
| 478 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 479 | } |
| 480 | |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 481 | float countUtilization = (100.f * fBudgetedCount) / fMaxCount; |
| 482 | float byteUtilization = (100.f * fBudgetedBytes) / fMaxBytes; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 483 | |
| 484 | SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 485 | SkDebugf("\t\tEntry Count: current %d" |
| 486 | " (%d budgeted, %d wrapped, %d locked, %d scratch %.2g%% full), high %d\n", |
| 487 | fCount, fBudgetedCount, wrapped, locked, scratch, countUtilization, fHighWaterCount); |
| 488 | SkDebugf("\t\tEntry Bytes: current %d (budgeted %d, %.2g%% full, %d unbudgeted) high %d\n", |
| 489 | fBytes, fBudgetedBytes, byteUtilization, unbudgetedSize, fHighWaterBytes); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | #endif |