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