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