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