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