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