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 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 10 | #include "GrResourceCache.h" |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 11 | #include "GrGpuResourceCacheAccess.h" |
hendrikw | 876c313 | 2015-03-04 10:33:49 -0800 | [diff] [blame] | 12 | #include "GrTracing.h" |
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" |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 16 | #include "SkTSort.h" |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 17 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 18 | DECLARE_SKMESSAGEBUS_MESSAGE(GrUniqueKeyInvalidatedMessage); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 19 | |
| 20 | ////////////////////////////////////////////////////////////////////////////// |
| 21 | |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 22 | GrScratchKey::ResourceType GrScratchKey::GenerateResourceType() { |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 23 | static int32_t gType = INHERITED::kInvalidDomain + 1; |
bsalomon | fe369ee | 2014-11-10 11:59:06 -0800 | [diff] [blame] | 24 | |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 25 | int32_t type = sk_atomic_inc(&gType); |
robertphillips | 9790a7b | 2015-01-05 12:29:15 -0800 | [diff] [blame] | 26 | if (type > SK_MaxU16) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 27 | SkFAIL("Too many Resource Types"); |
| 28 | } |
| 29 | |
| 30 | return static_cast<ResourceType>(type); |
| 31 | } |
| 32 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 33 | GrUniqueKey::Domain GrUniqueKey::GenerateDomain() { |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 34 | static int32_t gDomain = INHERITED::kInvalidDomain + 1; |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 35 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 36 | int32_t domain = sk_atomic_inc(&gDomain); |
kkinnunen | 016dffb | 2015-01-23 06:43:05 -0800 | [diff] [blame] | 37 | if (domain > SK_MaxU16) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 38 | SkFAIL("Too many GrUniqueKey Domains"); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 39 | } |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 40 | |
| 41 | return static_cast<Domain>(domain); |
| 42 | } |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 43 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 44 | uint32_t GrResourceKeyHash(const uint32_t* data, size_t size) { |
mtklein | 56da025 | 2015-11-16 11:16:23 -0800 | [diff] [blame] | 45 | return SkChecksum::Murmur3(data, size); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 46 | } |
| 47 | |
bsalomon | fe369ee | 2014-11-10 11:59:06 -0800 | [diff] [blame] | 48 | ////////////////////////////////////////////////////////////////////////////// |
| 49 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 50 | class GrResourceCache::AutoValidate : ::SkNoncopyable { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 51 | public: |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 52 | AutoValidate(GrResourceCache* cache) : fCache(cache) { cache->validate(); } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 53 | ~AutoValidate() { fCache->validate(); } |
| 54 | private: |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 55 | GrResourceCache* fCache; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | ////////////////////////////////////////////////////////////////////////////// |
| 59 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 60 | |
robertphillips | 6392668 | 2015-08-20 09:39:02 -0700 | [diff] [blame] | 61 | GrResourceCache::GrResourceCache(const GrCaps* caps) |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 62 | : fTimestamp(0) |
| 63 | , fMaxCount(kDefaultMaxCount) |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 64 | , fMaxBytes(kDefaultMaxSize) |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 65 | , fMaxUnusedFlushes(kDefaultMaxUnusedFlushes) |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 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 |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 72 | , fBytes(0) |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 73 | , fBudgetedCount(0) |
| 74 | , fBudgetedBytes(0) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 75 | , fOverBudgetCB(nullptr) |
| 76 | , fOverBudgetData(nullptr) |
| 77 | , fFlushTimestamps(nullptr) |
robertphillips | 6392668 | 2015-08-20 09:39:02 -0700 | [diff] [blame] | 78 | , fLastFlushTimestampIndex(0) |
| 79 | , fPreferVRAMUseOverFlushes(caps->preferVRAMUseOverFlushes()) { |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 80 | SkDEBUGCODE(fCount = 0;) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 81 | SkDEBUGCODE(fNewlyPurgeableResourceForValidation = nullptr;) |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 82 | this->resetFlushTimestamps(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 83 | } |
| 84 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 85 | GrResourceCache::~GrResourceCache() { |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 86 | this->releaseAll(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 87 | delete[] fFlushTimestamps; |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 88 | } |
| 89 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 90 | void GrResourceCache::setLimits(int count, size_t bytes, int maxUnusedFlushes) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 91 | fMaxCount = count; |
| 92 | fMaxBytes = bytes; |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 93 | fMaxUnusedFlushes = maxUnusedFlushes; |
| 94 | this->resetFlushTimestamps(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 95 | this->purgeAsNeeded(); |
| 96 | } |
| 97 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 98 | void GrResourceCache::resetFlushTimestamps() { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 99 | delete[] fFlushTimestamps; |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 100 | |
| 101 | // We assume this number is a power of two when wrapping indices into the timestamp array. |
| 102 | fMaxUnusedFlushes = SkNextPow2(fMaxUnusedFlushes); |
| 103 | |
| 104 | // Since our implementation is to store the timestamps of the last fMaxUnusedFlushes flush calls |
| 105 | // we just turn the feature off if that array would be large. |
| 106 | static const int kMaxSupportedTimestampHistory = 128; |
| 107 | |
| 108 | if (fMaxUnusedFlushes > kMaxSupportedTimestampHistory) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 109 | fFlushTimestamps = nullptr; |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 110 | return; |
| 111 | } |
| 112 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 113 | fFlushTimestamps = new uint32_t[fMaxUnusedFlushes]; |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 114 | fLastFlushTimestampIndex = 0; |
| 115 | // Set all the historical flush timestamps to initially be at the beginning of time (timestamp |
| 116 | // 0). |
| 117 | sk_bzero(fFlushTimestamps, fMaxUnusedFlushes * sizeof(uint32_t)); |
| 118 | } |
| 119 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 120 | void GrResourceCache::insertResource(GrGpuResource* resource) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 121 | SkASSERT(resource); |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 122 | SkASSERT(!this->isInCache(resource)); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 123 | SkASSERT(!resource->wasDestroyed()); |
| 124 | SkASSERT(!resource->isPurgeable()); |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 125 | |
| 126 | // We must set the timestamp before adding to the array in case the timestamp wraps and we wind |
| 127 | // up iterating over all the resources that already have timestamps. |
| 128 | resource->cacheAccess().setTimestamp(this->getNextTimestamp()); |
| 129 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 130 | this->addToNonpurgeableArray(resource); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 131 | |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 132 | size_t size = resource->gpuMemorySize(); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 133 | SkDEBUGCODE(++fCount;) |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 134 | fBytes += size; |
bsalomon | 82b1d62 | 2014-11-14 13:59:57 -0800 | [diff] [blame] | 135 | #if GR_CACHE_STATS |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 136 | fHighWaterCount = SkTMax(this->getResourceCount(), fHighWaterCount); |
bsalomon | 82b1d62 | 2014-11-14 13:59:57 -0800 | [diff] [blame] | 137 | fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes); |
| 138 | #endif |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame^] | 139 | if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) { |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 140 | ++fBudgetedCount; |
| 141 | fBudgetedBytes += size; |
hendrikw | 876c313 | 2015-03-04 10:33:49 -0800 | [diff] [blame] | 142 | TRACE_COUNTER2(TRACE_DISABLED_BY_DEFAULT("skia.gpu.cache"), "skia budget", "used", |
| 143 | fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 144 | #if GR_CACHE_STATS |
| 145 | fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount); |
| 146 | fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes); |
| 147 | #endif |
| 148 | } |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 149 | if (resource->resourcePriv().getScratchKey().isValid()) { |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 150 | SkASSERT(!resource->cacheAccess().isExternal()); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 151 | fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 152 | } |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 153 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 154 | this->purgeAsNeeded(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 155 | } |
| 156 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 157 | void GrResourceCache::removeResource(GrGpuResource* resource) { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 158 | this->validate(); |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 159 | SkASSERT(this->isInCache(resource)); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 160 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 161 | if (resource->isPurgeable()) { |
| 162 | fPurgeableQueue.remove(resource); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 163 | } else { |
| 164 | this->removeFromNonpurgeableArray(resource); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 165 | } |
| 166 | |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 167 | size_t size = resource->gpuMemorySize(); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 168 | SkDEBUGCODE(--fCount;) |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 169 | fBytes -= size; |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame^] | 170 | if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) { |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 171 | --fBudgetedCount; |
| 172 | fBudgetedBytes -= size; |
hendrikw | 876c313 | 2015-03-04 10:33:49 -0800 | [diff] [blame] | 173 | TRACE_COUNTER2(TRACE_DISABLED_BY_DEFAULT("skia.gpu.cache"), "skia budget", "used", |
| 174 | fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 175 | } |
| 176 | |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 177 | if (resource->resourcePriv().getScratchKey().isValid()) { |
| 178 | fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 179 | } |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 180 | if (resource->getUniqueKey().isValid()) { |
| 181 | fUniqueHash.remove(resource->getUniqueKey()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 182 | } |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 183 | this->validate(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 184 | } |
| 185 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 186 | void GrResourceCache::abandonAll() { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 187 | AutoValidate av(this); |
| 188 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 189 | while (fNonpurgeableResources.count()) { |
| 190 | GrGpuResource* back = *(fNonpurgeableResources.end() - 1); |
| 191 | SkASSERT(!back->wasDestroyed()); |
| 192 | back->cacheAccess().abandon(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 193 | } |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 194 | |
| 195 | while (fPurgeableQueue.count()) { |
| 196 | GrGpuResource* top = fPurgeableQueue.peek(); |
| 197 | SkASSERT(!top->wasDestroyed()); |
| 198 | top->cacheAccess().abandon(); |
| 199 | } |
| 200 | |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 201 | SkASSERT(!fScratchMap.count()); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 202 | SkASSERT(!fUniqueHash.count()); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 203 | SkASSERT(!fCount); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 204 | SkASSERT(!this->getResourceCount()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 205 | SkASSERT(!fBytes); |
| 206 | SkASSERT(!fBudgetedCount); |
| 207 | SkASSERT(!fBudgetedBytes); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 208 | } |
| 209 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 210 | void GrResourceCache::releaseAll() { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 211 | AutoValidate av(this); |
| 212 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 213 | while(fNonpurgeableResources.count()) { |
| 214 | GrGpuResource* back = *(fNonpurgeableResources.end() - 1); |
| 215 | SkASSERT(!back->wasDestroyed()); |
| 216 | back->cacheAccess().release(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 217 | } |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 218 | |
| 219 | while (fPurgeableQueue.count()) { |
| 220 | GrGpuResource* top = fPurgeableQueue.peek(); |
| 221 | SkASSERT(!top->wasDestroyed()); |
| 222 | top->cacheAccess().release(); |
| 223 | } |
| 224 | |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 225 | SkASSERT(!fScratchMap.count()); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 226 | SkASSERT(!fUniqueHash.count()); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 227 | SkASSERT(!fCount); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 228 | SkASSERT(!this->getResourceCount()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 229 | SkASSERT(!fBytes); |
| 230 | SkASSERT(!fBudgetedCount); |
| 231 | SkASSERT(!fBudgetedBytes); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 232 | } |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 233 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 234 | class GrResourceCache::AvailableForScratchUse { |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 235 | public: |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 236 | AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendingIO) { } |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 237 | |
| 238 | bool operator()(const GrGpuResource* resource) const { |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 239 | if (resource->internalHasRef() || !resource->cacheAccess().isScratch()) { |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 240 | return false; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 241 | } |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 242 | return !fRejectPendingIO || !resource->internalHasPendingIO(); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 243 | } |
bsalomon | 1e2530b | 2014-10-09 09:57:18 -0700 | [diff] [blame] | 244 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 245 | private: |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 246 | bool fRejectPendingIO; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 247 | }; |
| 248 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 249 | GrGpuResource* GrResourceCache::findAndRefScratchResource(const GrScratchKey& scratchKey, |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 250 | size_t resourceSize, |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 251 | uint32_t flags) { |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 252 | SkASSERT(scratchKey.isValid()); |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 253 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 254 | GrGpuResource* resource; |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 255 | if (flags & (kPreferNoPendingIO_ScratchFlag | kRequireNoPendingIO_ScratchFlag)) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 256 | resource = fScratchMap.find(scratchKey, AvailableForScratchUse(true)); |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 257 | if (resource) { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 258 | this->refAndMakeResourceMRU(resource); |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 259 | this->validate(); |
| 260 | return resource; |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 261 | } else if (flags & kRequireNoPendingIO_ScratchFlag) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 262 | return nullptr; |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 263 | } |
robertphillips | 6392668 | 2015-08-20 09:39:02 -0700 | [diff] [blame] | 264 | // We would prefer to consume more available VRAM rather than flushing |
| 265 | // immediately, but on ANGLE this can lead to starving of the GPU. |
| 266 | if (fPreferVRAMUseOverFlushes && this->wouldFit(resourceSize)) { |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 267 | // kPrefer is specified, we didn't find a resource without pending io, |
robertphillips | 6392668 | 2015-08-20 09:39:02 -0700 | [diff] [blame] | 268 | // but there is still space in our budget for the resource so force |
| 269 | // the caller to allocate a new resource. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 270 | return nullptr; |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 271 | } |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 272 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 273 | resource = fScratchMap.find(scratchKey, AvailableForScratchUse(false)); |
| 274 | if (resource) { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 275 | this->refAndMakeResourceMRU(resource); |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 276 | this->validate(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 277 | } |
| 278 | return resource; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 279 | } |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 280 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 281 | void GrResourceCache::willRemoveScratchKey(const GrGpuResource* resource) { |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 282 | SkASSERT(resource->resourcePriv().getScratchKey().isValid()); |
| 283 | fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 284 | } |
| 285 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 286 | void GrResourceCache::removeUniqueKey(GrGpuResource* resource) { |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 287 | // Someone has a ref to this resource in order to have removed the key. When the ref count |
| 288 | // reaches zero we will get a ref cnt notification and figure out what to do with it. |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 289 | if (resource->getUniqueKey().isValid()) { |
| 290 | SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey())); |
| 291 | fUniqueHash.remove(resource->getUniqueKey()); |
| 292 | } |
| 293 | resource->cacheAccess().removeUniqueKey(); |
| 294 | this->validate(); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 295 | } |
| 296 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 297 | void GrResourceCache::changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 298 | SkASSERT(resource); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 299 | SkASSERT(this->isInCache(resource)); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 300 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 301 | // Remove the entry for this resource if it already has a unique key. |
| 302 | if (resource->getUniqueKey().isValid()) { |
| 303 | SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey())); |
| 304 | fUniqueHash.remove(resource->getUniqueKey()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 305 | SkASSERT(nullptr == fUniqueHash.find(resource->getUniqueKey())); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 306 | } |
| 307 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 308 | // If another resource has the new key, remove its key then install the key on this resource. |
| 309 | if (newKey.isValid()) { |
| 310 | if (GrGpuResource* old = fUniqueHash.find(newKey)) { |
| 311 | // If the old resource using the key is purgeable and is unreachable, then remove it. |
| 312 | if (!old->resourcePriv().getScratchKey().isValid() && old->isPurgeable()) { |
| 313 | // release may call validate() which will assert that resource is in fUniqueHash |
| 314 | // if it has a valid key. So in debug reset the key here before we assign it. |
| 315 | SkDEBUGCODE(resource->cacheAccess().removeUniqueKey();) |
| 316 | old->cacheAccess().release(); |
| 317 | } else { |
| 318 | fUniqueHash.remove(newKey); |
| 319 | old->cacheAccess().removeUniqueKey(); |
| 320 | } |
| 321 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 322 | SkASSERT(nullptr == fUniqueHash.find(newKey)); |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 323 | resource->cacheAccess().setUniqueKey(newKey); |
| 324 | fUniqueHash.add(resource); |
| 325 | } else { |
| 326 | resource->cacheAccess().removeUniqueKey(); |
| 327 | } |
| 328 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 329 | this->validate(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 330 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 331 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 332 | void GrResourceCache::refAndMakeResourceMRU(GrGpuResource* resource) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 333 | SkASSERT(resource); |
| 334 | SkASSERT(this->isInCache(resource)); |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 335 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 336 | if (resource->isPurgeable()) { |
| 337 | // It's about to become unpurgeable. |
| 338 | fPurgeableQueue.remove(resource); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 339 | this->addToNonpurgeableArray(resource); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 340 | } |
| 341 | resource->ref(); |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 342 | |
| 343 | resource->cacheAccess().setTimestamp(this->getNextTimestamp()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 344 | this->validate(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 345 | } |
| 346 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 347 | void GrResourceCache::notifyCntReachedZero(GrGpuResource* resource, uint32_t flags) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 348 | SkASSERT(resource); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 349 | SkASSERT(!resource->wasDestroyed()); |
| 350 | SkASSERT(flags); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 351 | SkASSERT(this->isInCache(resource)); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 352 | // This resource should always be in the nonpurgeable array when this function is called. It |
| 353 | // will be moved to the queue if it is newly purgeable. |
| 354 | SkASSERT(fNonpurgeableResources[*resource->cacheAccess().accessCacheIndex()] == resource); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 355 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 356 | if (SkToBool(ResourceAccess::kRefCntReachedZero_RefNotificationFlag & flags)) { |
| 357 | #ifdef SK_DEBUG |
| 358 | // When the timestamp overflows validate() is called. validate() checks that resources in |
| 359 | // the nonpurgeable array are indeed not purgeable. However, the movement from the array to |
| 360 | // the purgeable queue happens just below in this function. So we mark it as an exception. |
| 361 | if (resource->isPurgeable()) { |
| 362 | fNewlyPurgeableResourceForValidation = resource; |
| 363 | } |
| 364 | #endif |
| 365 | resource->cacheAccess().setTimestamp(this->getNextTimestamp()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 366 | SkDEBUGCODE(fNewlyPurgeableResourceForValidation = nullptr); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | if (!SkToBool(ResourceAccess::kAllCntsReachedZero_RefNotificationFlag & flags)) { |
| 370 | SkASSERT(!resource->isPurgeable()); |
| 371 | return; |
| 372 | } |
| 373 | |
| 374 | SkASSERT(resource->isPurgeable()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 375 | this->removeFromNonpurgeableArray(resource); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 376 | fPurgeableQueue.insert(resource); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 377 | |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame^] | 378 | if (SkBudgeted::kNo == resource->resourcePriv().isBudgeted()) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 379 | // Check whether this resource could still be used as a scratch resource. |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 380 | if (!resource->cacheAccess().isExternal() && |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 381 | resource->resourcePriv().getScratchKey().isValid()) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 382 | // We won't purge an existing resource to make room for this one. |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 383 | if (fBudgetedCount < fMaxCount && |
| 384 | fBudgetedBytes + resource->gpuMemorySize() <= fMaxBytes) { |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 385 | resource->resourcePriv().makeBudgeted(); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 386 | return; |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 387 | } |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 388 | } |
| 389 | } else { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 390 | // Purge the resource immediately if we're over budget |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 391 | // Also purge if the resource has neither a valid scratch key nor a unique key. |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 392 | bool noKey = !resource->resourcePriv().getScratchKey().isValid() && |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 393 | !resource->getUniqueKey().isValid(); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 394 | if (!this->overBudget() && !noKey) { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 395 | return; |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 396 | } |
| 397 | } |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 398 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 399 | SkDEBUGCODE(int beforeCount = this->getResourceCount();) |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 400 | resource->cacheAccess().release(); |
| 401 | // We should at least free this resource, perhaps dependent resources as well. |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 402 | SkASSERT(this->getResourceCount() < beforeCount); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 403 | this->validate(); |
| 404 | } |
| 405 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 406 | void GrResourceCache::didChangeGpuMemorySize(const GrGpuResource* resource, size_t oldSize) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 407 | // SkASSERT(!fPurging); GrPathRange increases size during flush. :( |
| 408 | SkASSERT(resource); |
| 409 | SkASSERT(this->isInCache(resource)); |
| 410 | |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 411 | ptrdiff_t delta = resource->gpuMemorySize() - oldSize; |
| 412 | |
| 413 | fBytes += delta; |
bsalomon | 82b1d62 | 2014-11-14 13:59:57 -0800 | [diff] [blame] | 414 | #if GR_CACHE_STATS |
| 415 | fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes); |
| 416 | #endif |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame^] | 417 | if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) { |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 418 | fBudgetedBytes += delta; |
hendrikw | 876c313 | 2015-03-04 10:33:49 -0800 | [diff] [blame] | 419 | TRACE_COUNTER2(TRACE_DISABLED_BY_DEFAULT("skia.gpu.cache"), "skia budget", "used", |
| 420 | fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 421 | #if GR_CACHE_STATS |
| 422 | fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes); |
| 423 | #endif |
| 424 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 425 | |
| 426 | this->purgeAsNeeded(); |
| 427 | this->validate(); |
| 428 | } |
| 429 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 430 | void GrResourceCache::didChangeBudgetStatus(GrGpuResource* resource) { |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 431 | SkASSERT(resource); |
| 432 | SkASSERT(this->isInCache(resource)); |
| 433 | |
| 434 | size_t size = resource->gpuMemorySize(); |
| 435 | |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame^] | 436 | if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) { |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 437 | ++fBudgetedCount; |
| 438 | fBudgetedBytes += size; |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 439 | #if GR_CACHE_STATS |
| 440 | fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes); |
| 441 | fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount); |
| 442 | #endif |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 443 | this->purgeAsNeeded(); |
| 444 | } else { |
| 445 | --fBudgetedCount; |
| 446 | fBudgetedBytes -= size; |
| 447 | } |
hendrikw | 876c313 | 2015-03-04 10:33:49 -0800 | [diff] [blame] | 448 | TRACE_COUNTER2(TRACE_DISABLED_BY_DEFAULT("skia.gpu.cache"), "skia budget", "used", |
| 449 | fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 450 | |
| 451 | this->validate(); |
| 452 | } |
| 453 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 454 | void GrResourceCache::purgeAsNeeded() { |
| 455 | SkTArray<GrUniqueKeyInvalidatedMessage> invalidKeyMsgs; |
| 456 | fInvalidUniqueKeyInbox.poll(&invalidKeyMsgs); |
| 457 | if (invalidKeyMsgs.count()) { |
| 458 | this->processInvalidUniqueKeys(invalidKeyMsgs); |
| 459 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 460 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 461 | if (fFlushTimestamps) { |
| 462 | // Assuming kNumFlushesToDeleteUnusedResource is a power of 2. |
| 463 | SkASSERT(SkIsPow2(fMaxUnusedFlushes)); |
| 464 | int oldestFlushIndex = (fLastFlushTimestampIndex + 1) & (fMaxUnusedFlushes - 1); |
| 465 | |
| 466 | uint32_t oldestAllowedTimestamp = fFlushTimestamps[oldestFlushIndex]; |
| 467 | while (fPurgeableQueue.count()) { |
| 468 | uint32_t oldestResourceTimestamp = fPurgeableQueue.peek()->cacheAccess().timestamp(); |
| 469 | if (oldestAllowedTimestamp < oldestResourceTimestamp) { |
| 470 | break; |
| 471 | } |
| 472 | GrGpuResource* resource = fPurgeableQueue.peek(); |
| 473 | SkASSERT(resource->isPurgeable()); |
| 474 | resource->cacheAccess().release(); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | bool stillOverbudget = this->overBudget(); |
| 479 | while (stillOverbudget && fPurgeableQueue.count()) { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 480 | GrGpuResource* resource = fPurgeableQueue.peek(); |
| 481 | SkASSERT(resource->isPurgeable()); |
| 482 | resource->cacheAccess().release(); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 483 | stillOverbudget = this->overBudget(); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 484 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 485 | |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 486 | this->validate(); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 487 | |
| 488 | if (stillOverbudget) { |
| 489 | // Despite the purge we're still over budget. Call our over budget callback. If this frees |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 490 | // any resources then we'll get notified and take appropriate action. |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 491 | (*fOverBudgetCB)(fOverBudgetData); |
| 492 | this->validate(); |
| 493 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 494 | } |
| 495 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 496 | void GrResourceCache::purgeAllUnlocked() { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 497 | // We could disable maintaining the heap property here, but it would add a lot of complexity. |
| 498 | // Moreover, this is rarely called. |
| 499 | while (fPurgeableQueue.count()) { |
| 500 | GrGpuResource* resource = fPurgeableQueue.peek(); |
| 501 | SkASSERT(resource->isPurgeable()); |
| 502 | resource->cacheAccess().release(); |
| 503 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 504 | |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 505 | this->validate(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 506 | } |
| 507 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 508 | void GrResourceCache::processInvalidUniqueKeys( |
| 509 | const SkTArray<GrUniqueKeyInvalidatedMessage>& msgs) { |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 510 | for (int i = 0; i < msgs.count(); ++i) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 511 | GrGpuResource* resource = this->findAndRefUniqueResource(msgs[i].key()); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 512 | if (resource) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 513 | resource->resourcePriv().removeUniqueKey(); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 514 | resource->unref(); // If this resource is now purgeable, the cache will be notified. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | } |
| 518 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 519 | void GrResourceCache::addToNonpurgeableArray(GrGpuResource* resource) { |
| 520 | int index = fNonpurgeableResources.count(); |
| 521 | *fNonpurgeableResources.append() = resource; |
| 522 | *resource->cacheAccess().accessCacheIndex() = index; |
| 523 | } |
| 524 | |
| 525 | void GrResourceCache::removeFromNonpurgeableArray(GrGpuResource* resource) { |
| 526 | int* index = resource->cacheAccess().accessCacheIndex(); |
| 527 | // Fill the whole we will create in the array with the tail object, adjust its index, and |
| 528 | // then pop the array |
| 529 | GrGpuResource* tail = *(fNonpurgeableResources.end() - 1); |
| 530 | SkASSERT(fNonpurgeableResources[*index] == resource); |
| 531 | fNonpurgeableResources[*index] = tail; |
| 532 | *tail->cacheAccess().accessCacheIndex() = *index; |
| 533 | fNonpurgeableResources.pop(); |
| 534 | SkDEBUGCODE(*index = -1); |
| 535 | } |
| 536 | |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 537 | uint32_t GrResourceCache::getNextTimestamp() { |
| 538 | // If we wrap then all the existing resources will appear older than any resources that get |
| 539 | // a timestamp after the wrap. |
| 540 | if (0 == fTimestamp) { |
| 541 | int count = this->getResourceCount(); |
| 542 | if (count) { |
| 543 | // Reset all the timestamps. We sort the resources by timestamp and then assign |
| 544 | // sequential timestamps beginning with 0. This is O(n*lg(n)) but it should be extremely |
| 545 | // rare. |
| 546 | SkTDArray<GrGpuResource*> sortedPurgeableResources; |
| 547 | sortedPurgeableResources.setReserve(fPurgeableQueue.count()); |
| 548 | |
| 549 | while (fPurgeableQueue.count()) { |
| 550 | *sortedPurgeableResources.append() = fPurgeableQueue.peek(); |
| 551 | fPurgeableQueue.pop(); |
| 552 | } |
| 553 | |
| 554 | struct Less { |
| 555 | bool operator()(GrGpuResource* a, GrGpuResource* b) { |
| 556 | return CompareTimestamp(a,b); |
| 557 | } |
| 558 | }; |
| 559 | Less less; |
| 560 | SkTQSort(fNonpurgeableResources.begin(), fNonpurgeableResources.end() - 1, less); |
| 561 | |
| 562 | // Pick resources out of the purgeable and non-purgeable arrays based on lowest |
| 563 | // timestamp and assign new timestamps. |
| 564 | int currP = 0; |
| 565 | int currNP = 0; |
| 566 | while (currP < sortedPurgeableResources.count() && |
mtklein | 56da025 | 2015-11-16 11:16:23 -0800 | [diff] [blame] | 567 | currNP < fNonpurgeableResources.count()) { |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 568 | uint32_t tsP = sortedPurgeableResources[currP]->cacheAccess().timestamp(); |
| 569 | uint32_t tsNP = fNonpurgeableResources[currNP]->cacheAccess().timestamp(); |
| 570 | SkASSERT(tsP != tsNP); |
| 571 | if (tsP < tsNP) { |
| 572 | sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++); |
| 573 | } else { |
| 574 | // Correct the index in the nonpurgeable array stored on the resource post-sort. |
| 575 | *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP; |
| 576 | fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | // The above loop ended when we hit the end of one array. Finish the other one. |
| 581 | while (currP < sortedPurgeableResources.count()) { |
| 582 | sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++); |
| 583 | } |
| 584 | while (currNP < fNonpurgeableResources.count()) { |
| 585 | *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP; |
| 586 | fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++); |
| 587 | } |
| 588 | |
| 589 | // Rebuild the queue. |
| 590 | for (int i = 0; i < sortedPurgeableResources.count(); ++i) { |
| 591 | fPurgeableQueue.insert(sortedPurgeableResources[i]); |
| 592 | } |
| 593 | |
| 594 | this->validate(); |
| 595 | SkASSERT(count == this->getResourceCount()); |
| 596 | |
| 597 | // count should be the next timestamp we return. |
| 598 | SkASSERT(fTimestamp == SkToU32(count)); |
mtklein | 56da025 | 2015-11-16 11:16:23 -0800 | [diff] [blame] | 599 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 600 | // The historical timestamps of flushes are now invalid. |
| 601 | this->resetFlushTimestamps(); |
mtklein | 56da025 | 2015-11-16 11:16:23 -0800 | [diff] [blame] | 602 | } |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 603 | } |
| 604 | return fTimestamp++; |
| 605 | } |
| 606 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 607 | void GrResourceCache::notifyFlushOccurred() { |
| 608 | if (fFlushTimestamps) { |
| 609 | SkASSERT(SkIsPow2(fMaxUnusedFlushes)); |
| 610 | fLastFlushTimestampIndex = (fLastFlushTimestampIndex + 1) & (fMaxUnusedFlushes - 1); |
| 611 | // get the timestamp before accessing fFlushTimestamps because getNextTimestamp will |
| 612 | // reallocate fFlushTimestamps on timestamp overflow. |
| 613 | uint32_t timestamp = this->getNextTimestamp(); |
| 614 | fFlushTimestamps[fLastFlushTimestampIndex] = timestamp; |
| 615 | this->purgeAsNeeded(); |
| 616 | } |
| 617 | } |
| 618 | |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 619 | void GrResourceCache::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const { |
| 620 | for (int i = 0; i < fNonpurgeableResources.count(); ++i) { |
| 621 | fNonpurgeableResources[i]->dumpMemoryStatistics(traceMemoryDump); |
| 622 | } |
| 623 | for (int i = 0; i < fPurgeableQueue.count(); ++i) { |
| 624 | fPurgeableQueue.at(i)->dumpMemoryStatistics(traceMemoryDump); |
| 625 | } |
| 626 | } |
| 627 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 628 | #ifdef SK_DEBUG |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 629 | void GrResourceCache::validate() const { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 630 | // Reduce the frequency of validations for large resource counts. |
| 631 | static SkRandom gRandom; |
| 632 | int mask = (SkNextPow2(fCount + 1) >> 5) - 1; |
| 633 | if (~mask && (gRandom.nextU() & mask)) { |
| 634 | return; |
| 635 | } |
| 636 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 637 | struct Stats { |
| 638 | size_t fBytes; |
| 639 | int fBudgetedCount; |
| 640 | size_t fBudgetedBytes; |
| 641 | int fLocked; |
| 642 | int fScratch; |
| 643 | int fCouldBeScratch; |
| 644 | int fContent; |
| 645 | const ScratchMap* fScratchMap; |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 646 | const UniqueHash* fUniqueHash; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 647 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 648 | Stats(const GrResourceCache* cache) { |
| 649 | memset(this, 0, sizeof(*this)); |
| 650 | fScratchMap = &cache->fScratchMap; |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 651 | fUniqueHash = &cache->fUniqueHash; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 652 | } |
| 653 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 654 | void update(GrGpuResource* resource) { |
| 655 | fBytes += resource->gpuMemorySize(); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 656 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 657 | if (!resource->isPurgeable()) { |
| 658 | ++fLocked; |
| 659 | } |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 660 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 661 | if (resource->cacheAccess().isScratch()) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 662 | SkASSERT(!resource->getUniqueKey().isValid()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 663 | ++fScratch; |
| 664 | SkASSERT(fScratchMap->countForKey(resource->resourcePriv().getScratchKey())); |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 665 | SkASSERT(!resource->cacheAccess().isExternal()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 666 | } else if (resource->resourcePriv().getScratchKey().isValid()) { |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame^] | 667 | SkASSERT(SkBudgeted::kNo == resource->resourcePriv().isBudgeted() || |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 668 | resource->getUniqueKey().isValid()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 669 | ++fCouldBeScratch; |
| 670 | SkASSERT(fScratchMap->countForKey(resource->resourcePriv().getScratchKey())); |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 671 | SkASSERT(!resource->cacheAccess().isExternal()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 672 | } |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 673 | const GrUniqueKey& uniqueKey = resource->getUniqueKey(); |
| 674 | if (uniqueKey.isValid()) { |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 675 | ++fContent; |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 676 | SkASSERT(fUniqueHash->find(uniqueKey) == resource); |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 677 | SkASSERT(!resource->cacheAccess().isExternal()); |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame^] | 678 | SkASSERT(SkBudgeted::kYes == resource->resourcePriv().isBudgeted()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 679 | } |
| 680 | |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame^] | 681 | if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) { |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 682 | ++fBudgetedCount; |
| 683 | fBudgetedBytes += resource->gpuMemorySize(); |
| 684 | } |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 685 | } |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 686 | }; |
| 687 | |
| 688 | Stats stats(this); |
| 689 | |
| 690 | for (int i = 0; i < fNonpurgeableResources.count(); ++i) { |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 691 | SkASSERT(!fNonpurgeableResources[i]->isPurgeable() || |
| 692 | fNewlyPurgeableResourceForValidation == fNonpurgeableResources[i]); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 693 | SkASSERT(*fNonpurgeableResources[i]->cacheAccess().accessCacheIndex() == i); |
| 694 | SkASSERT(!fNonpurgeableResources[i]->wasDestroyed()); |
| 695 | stats.update(fNonpurgeableResources[i]); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 696 | } |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 697 | for (int i = 0; i < fPurgeableQueue.count(); ++i) { |
| 698 | SkASSERT(fPurgeableQueue.at(i)->isPurgeable()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 699 | SkASSERT(*fPurgeableQueue.at(i)->cacheAccess().accessCacheIndex() == i); |
| 700 | SkASSERT(!fPurgeableQueue.at(i)->wasDestroyed()); |
| 701 | stats.update(fPurgeableQueue.at(i)); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 702 | } |
| 703 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 704 | SkASSERT(fCount == this->getResourceCount()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 705 | SkASSERT(fBudgetedCount <= fCount); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 706 | SkASSERT(fBudgetedBytes <= fBytes); |
| 707 | SkASSERT(stats.fBytes == fBytes); |
| 708 | SkASSERT(stats.fBudgetedBytes == fBudgetedBytes); |
| 709 | SkASSERT(stats.fBudgetedCount == fBudgetedCount); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 710 | #if GR_CACHE_STATS |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 711 | SkASSERT(fBudgetedHighWaterCount <= fHighWaterCount); |
| 712 | SkASSERT(fBudgetedHighWaterBytes <= fHighWaterBytes); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 713 | SkASSERT(fBytes <= fHighWaterBytes); |
| 714 | SkASSERT(fCount <= fHighWaterCount); |
| 715 | SkASSERT(fBudgetedBytes <= fBudgetedHighWaterBytes); |
| 716 | SkASSERT(fBudgetedCount <= fBudgetedHighWaterCount); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 717 | #endif |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 718 | SkASSERT(stats.fContent == fUniqueHash.count()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 719 | SkASSERT(stats.fScratch + stats.fCouldBeScratch == fScratchMap.count()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 720 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 721 | // This assertion is not currently valid because we can be in recursive notifyCntReachedZero() |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 722 | // calls. This will be fixed when subresource registration is explicit. |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 723 | // bool overBudget = budgetedBytes > fMaxBytes || budgetedCount > fMaxCount; |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 724 | // SkASSERT(!overBudget || locked == count || fPurging); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 725 | } |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 726 | |
| 727 | bool GrResourceCache::isInCache(const GrGpuResource* resource) const { |
| 728 | int index = *resource->cacheAccess().accessCacheIndex(); |
| 729 | if (index < 0) { |
| 730 | return false; |
| 731 | } |
| 732 | if (index < fPurgeableQueue.count() && fPurgeableQueue.at(index) == resource) { |
| 733 | return true; |
| 734 | } |
| 735 | if (index < fNonpurgeableResources.count() && fNonpurgeableResources[index] == resource) { |
| 736 | return true; |
| 737 | } |
| 738 | SkDEBUGFAIL("Resource index should be -1 or the resource should be in the cache."); |
| 739 | return false; |
| 740 | } |
| 741 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 742 | #endif |