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, |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 249 | uint32_t flags) { |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 250 | SkASSERT(scratchKey.isValid()); |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 251 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 252 | GrGpuResource* resource; |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 253 | if (flags & (kPreferNoPendingIO_ScratchFlag | kRequireNoPendingIO_ScratchFlag)) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 254 | resource = fScratchMap.find(scratchKey, AvailableForScratchUse(true)); |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 255 | if (resource) { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 256 | this->refAndMakeResourceMRU(resource); |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 257 | this->validate(); |
| 258 | return resource; |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 259 | } else if (flags & kRequireNoPendingIO_ScratchFlag) { |
| 260 | return NULL; |
| 261 | } |
| 262 | // TODO: fail here when kPrefer is specified, we didn't find a resource without pending io, |
| 263 | // but there is still space in our budget for the resource. |
| 264 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 265 | resource = fScratchMap.find(scratchKey, AvailableForScratchUse(false)); |
| 266 | if (resource) { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 267 | this->refAndMakeResourceMRU(resource); |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 268 | this->validate(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 269 | } |
| 270 | return resource; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 271 | } |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 272 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 273 | void GrResourceCache::willRemoveScratchKey(const GrGpuResource* resource) { |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 274 | SkASSERT(resource->resourcePriv().getScratchKey().isValid()); |
| 275 | fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 276 | } |
| 277 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 278 | void GrResourceCache::removeUniqueKey(GrGpuResource* resource) { |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 279 | // Someone has a ref to this resource in order to have removed the key. When the ref count |
| 280 | // 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] | 281 | if (resource->getUniqueKey().isValid()) { |
| 282 | SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey())); |
| 283 | fUniqueHash.remove(resource->getUniqueKey()); |
| 284 | } |
| 285 | resource->cacheAccess().removeUniqueKey(); |
| 286 | this->validate(); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 287 | } |
| 288 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 289 | void GrResourceCache::changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 290 | SkASSERT(resource); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 291 | SkASSERT(this->isInCache(resource)); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 292 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 293 | // Remove the entry for this resource if it already has a unique key. |
| 294 | if (resource->getUniqueKey().isValid()) { |
| 295 | SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey())); |
| 296 | fUniqueHash.remove(resource->getUniqueKey()); |
| 297 | SkASSERT(NULL == fUniqueHash.find(resource->getUniqueKey())); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 298 | } |
| 299 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 300 | // If another resource has the new key, remove its key then install the key on this resource. |
| 301 | if (newKey.isValid()) { |
| 302 | if (GrGpuResource* old = fUniqueHash.find(newKey)) { |
| 303 | // If the old resource using the key is purgeable and is unreachable, then remove it. |
| 304 | if (!old->resourcePriv().getScratchKey().isValid() && old->isPurgeable()) { |
| 305 | // release may call validate() which will assert that resource is in fUniqueHash |
| 306 | // if it has a valid key. So in debug reset the key here before we assign it. |
| 307 | SkDEBUGCODE(resource->cacheAccess().removeUniqueKey();) |
| 308 | old->cacheAccess().release(); |
| 309 | } else { |
| 310 | fUniqueHash.remove(newKey); |
| 311 | old->cacheAccess().removeUniqueKey(); |
| 312 | } |
| 313 | } |
| 314 | SkASSERT(NULL == fUniqueHash.find(newKey)); |
| 315 | resource->cacheAccess().setUniqueKey(newKey); |
| 316 | fUniqueHash.add(resource); |
| 317 | } else { |
| 318 | resource->cacheAccess().removeUniqueKey(); |
| 319 | } |
| 320 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 321 | this->validate(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 322 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 323 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 324 | void GrResourceCache::refAndMakeResourceMRU(GrGpuResource* resource) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 325 | SkASSERT(resource); |
| 326 | SkASSERT(this->isInCache(resource)); |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 327 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 328 | if (resource->isPurgeable()) { |
| 329 | // It's about to become unpurgeable. |
| 330 | fPurgeableQueue.remove(resource); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 331 | this->addToNonpurgeableArray(resource); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 332 | } |
| 333 | resource->ref(); |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 334 | |
| 335 | resource->cacheAccess().setTimestamp(this->getNextTimestamp()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 336 | this->validate(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 337 | } |
| 338 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 339 | void GrResourceCache::notifyCntReachedZero(GrGpuResource* resource, uint32_t flags) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 340 | SkASSERT(resource); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 341 | SkASSERT(!resource->wasDestroyed()); |
| 342 | SkASSERT(flags); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 343 | SkASSERT(this->isInCache(resource)); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 344 | // This resource should always be in the nonpurgeable array when this function is called. It |
| 345 | // will be moved to the queue if it is newly purgeable. |
| 346 | SkASSERT(fNonpurgeableResources[*resource->cacheAccess().accessCacheIndex()] == resource); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 347 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 348 | if (SkToBool(ResourceAccess::kRefCntReachedZero_RefNotificationFlag & flags)) { |
| 349 | #ifdef SK_DEBUG |
| 350 | // When the timestamp overflows validate() is called. validate() checks that resources in |
| 351 | // the nonpurgeable array are indeed not purgeable. However, the movement from the array to |
| 352 | // the purgeable queue happens just below in this function. So we mark it as an exception. |
| 353 | if (resource->isPurgeable()) { |
| 354 | fNewlyPurgeableResourceForValidation = resource; |
| 355 | } |
| 356 | #endif |
| 357 | resource->cacheAccess().setTimestamp(this->getNextTimestamp()); |
| 358 | SkDEBUGCODE(fNewlyPurgeableResourceForValidation = NULL); |
| 359 | } |
| 360 | |
| 361 | if (!SkToBool(ResourceAccess::kAllCntsReachedZero_RefNotificationFlag & flags)) { |
| 362 | SkASSERT(!resource->isPurgeable()); |
| 363 | return; |
| 364 | } |
| 365 | |
| 366 | SkASSERT(resource->isPurgeable()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 367 | this->removeFromNonpurgeableArray(resource); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 368 | fPurgeableQueue.insert(resource); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 369 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 370 | if (!resource->resourcePriv().isBudgeted()) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 371 | // Check whether this resource could still be used as a scratch resource. |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 372 | if (!resource->cacheAccess().isExternal() && |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 373 | resource->resourcePriv().getScratchKey().isValid()) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 374 | // We won't purge an existing resource to make room for this one. |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 375 | if (fBudgetedCount < fMaxCount && |
| 376 | fBudgetedBytes + resource->gpuMemorySize() <= fMaxBytes) { |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 377 | resource->resourcePriv().makeBudgeted(); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 378 | return; |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 379 | } |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 380 | } |
| 381 | } else { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 382 | // Purge the resource immediately if we're over budget |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 383 | // 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] | 384 | bool noKey = !resource->resourcePriv().getScratchKey().isValid() && |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 385 | !resource->getUniqueKey().isValid(); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 386 | if (!this->overBudget() && !noKey) { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 387 | return; |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 388 | } |
| 389 | } |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 390 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 391 | SkDEBUGCODE(int beforeCount = this->getResourceCount();) |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 392 | resource->cacheAccess().release(); |
| 393 | // We should at least free this resource, perhaps dependent resources as well. |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 394 | SkASSERT(this->getResourceCount() < beforeCount); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 395 | this->validate(); |
| 396 | } |
| 397 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 398 | void GrResourceCache::didChangeGpuMemorySize(const GrGpuResource* resource, size_t oldSize) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 399 | // SkASSERT(!fPurging); GrPathRange increases size during flush. :( |
| 400 | SkASSERT(resource); |
| 401 | SkASSERT(this->isInCache(resource)); |
| 402 | |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 403 | ptrdiff_t delta = resource->gpuMemorySize() - oldSize; |
| 404 | |
| 405 | fBytes += delta; |
bsalomon | 82b1d62 | 2014-11-14 13:59:57 -0800 | [diff] [blame] | 406 | #if GR_CACHE_STATS |
| 407 | fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes); |
| 408 | #endif |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 409 | if (resource->resourcePriv().isBudgeted()) { |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 410 | fBudgetedBytes += delta; |
hendrikw | 876c313 | 2015-03-04 10:33:49 -0800 | [diff] [blame] | 411 | TRACE_COUNTER2(TRACE_DISABLED_BY_DEFAULT("skia.gpu.cache"), "skia budget", "used", |
| 412 | fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 413 | #if GR_CACHE_STATS |
| 414 | fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes); |
| 415 | #endif |
| 416 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 417 | |
| 418 | this->purgeAsNeeded(); |
| 419 | this->validate(); |
| 420 | } |
| 421 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 422 | void GrResourceCache::didChangeBudgetStatus(GrGpuResource* resource) { |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 423 | SkASSERT(resource); |
| 424 | SkASSERT(this->isInCache(resource)); |
| 425 | |
| 426 | size_t size = resource->gpuMemorySize(); |
| 427 | |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 428 | if (resource->resourcePriv().isBudgeted()) { |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 429 | ++fBudgetedCount; |
| 430 | fBudgetedBytes += size; |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 431 | #if GR_CACHE_STATS |
| 432 | fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes); |
| 433 | fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount); |
| 434 | #endif |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 435 | this->purgeAsNeeded(); |
| 436 | } else { |
| 437 | --fBudgetedCount; |
| 438 | fBudgetedBytes -= size; |
| 439 | } |
hendrikw | 876c313 | 2015-03-04 10:33:49 -0800 | [diff] [blame] | 440 | TRACE_COUNTER2(TRACE_DISABLED_BY_DEFAULT("skia.gpu.cache"), "skia budget", "used", |
| 441 | fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 442 | |
| 443 | this->validate(); |
| 444 | } |
| 445 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 446 | void GrResourceCache::purgeAsNeeded() { |
| 447 | SkTArray<GrUniqueKeyInvalidatedMessage> invalidKeyMsgs; |
| 448 | fInvalidUniqueKeyInbox.poll(&invalidKeyMsgs); |
| 449 | if (invalidKeyMsgs.count()) { |
| 450 | this->processInvalidUniqueKeys(invalidKeyMsgs); |
| 451 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 452 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 453 | if (fFlushTimestamps) { |
| 454 | // Assuming kNumFlushesToDeleteUnusedResource is a power of 2. |
| 455 | SkASSERT(SkIsPow2(fMaxUnusedFlushes)); |
| 456 | int oldestFlushIndex = (fLastFlushTimestampIndex + 1) & (fMaxUnusedFlushes - 1); |
| 457 | |
| 458 | uint32_t oldestAllowedTimestamp = fFlushTimestamps[oldestFlushIndex]; |
| 459 | while (fPurgeableQueue.count()) { |
| 460 | uint32_t oldestResourceTimestamp = fPurgeableQueue.peek()->cacheAccess().timestamp(); |
| 461 | if (oldestAllowedTimestamp < oldestResourceTimestamp) { |
| 462 | break; |
| 463 | } |
| 464 | GrGpuResource* resource = fPurgeableQueue.peek(); |
| 465 | SkASSERT(resource->isPurgeable()); |
| 466 | resource->cacheAccess().release(); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | bool stillOverbudget = this->overBudget(); |
| 471 | while (stillOverbudget && fPurgeableQueue.count()) { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 472 | GrGpuResource* resource = fPurgeableQueue.peek(); |
| 473 | SkASSERT(resource->isPurgeable()); |
| 474 | resource->cacheAccess().release(); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 475 | stillOverbudget = this->overBudget(); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 476 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 477 | |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 478 | this->validate(); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 479 | |
| 480 | if (stillOverbudget) { |
| 481 | // 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] | 482 | // any resources then we'll get notified and take appropriate action. |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 483 | (*fOverBudgetCB)(fOverBudgetData); |
| 484 | this->validate(); |
| 485 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 486 | } |
| 487 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 488 | void GrResourceCache::purgeAllUnlocked() { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 489 | // We could disable maintaining the heap property here, but it would add a lot of complexity. |
| 490 | // Moreover, this is rarely called. |
| 491 | while (fPurgeableQueue.count()) { |
| 492 | GrGpuResource* resource = fPurgeableQueue.peek(); |
| 493 | SkASSERT(resource->isPurgeable()); |
| 494 | resource->cacheAccess().release(); |
| 495 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 496 | |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 497 | this->validate(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 498 | } |
| 499 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 500 | void GrResourceCache::processInvalidUniqueKeys( |
| 501 | const SkTArray<GrUniqueKeyInvalidatedMessage>& msgs) { |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 502 | for (int i = 0; i < msgs.count(); ++i) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 503 | GrGpuResource* resource = this->findAndRefUniqueResource(msgs[i].key()); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 504 | if (resource) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 505 | resource->resourcePriv().removeUniqueKey(); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 506 | resource->unref(); // If this resource is now purgeable, the cache will be notified. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | } |
| 510 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 511 | void GrResourceCache::addToNonpurgeableArray(GrGpuResource* resource) { |
| 512 | int index = fNonpurgeableResources.count(); |
| 513 | *fNonpurgeableResources.append() = resource; |
| 514 | *resource->cacheAccess().accessCacheIndex() = index; |
| 515 | } |
| 516 | |
| 517 | void GrResourceCache::removeFromNonpurgeableArray(GrGpuResource* resource) { |
| 518 | int* index = resource->cacheAccess().accessCacheIndex(); |
| 519 | // Fill the whole we will create in the array with the tail object, adjust its index, and |
| 520 | // then pop the array |
| 521 | GrGpuResource* tail = *(fNonpurgeableResources.end() - 1); |
| 522 | SkASSERT(fNonpurgeableResources[*index] == resource); |
| 523 | fNonpurgeableResources[*index] = tail; |
| 524 | *tail->cacheAccess().accessCacheIndex() = *index; |
| 525 | fNonpurgeableResources.pop(); |
| 526 | SkDEBUGCODE(*index = -1); |
| 527 | } |
| 528 | |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 529 | uint32_t GrResourceCache::getNextTimestamp() { |
| 530 | // If we wrap then all the existing resources will appear older than any resources that get |
| 531 | // a timestamp after the wrap. |
| 532 | if (0 == fTimestamp) { |
| 533 | int count = this->getResourceCount(); |
| 534 | if (count) { |
| 535 | // Reset all the timestamps. We sort the resources by timestamp and then assign |
| 536 | // sequential timestamps beginning with 0. This is O(n*lg(n)) but it should be extremely |
| 537 | // rare. |
| 538 | SkTDArray<GrGpuResource*> sortedPurgeableResources; |
| 539 | sortedPurgeableResources.setReserve(fPurgeableQueue.count()); |
| 540 | |
| 541 | while (fPurgeableQueue.count()) { |
| 542 | *sortedPurgeableResources.append() = fPurgeableQueue.peek(); |
| 543 | fPurgeableQueue.pop(); |
| 544 | } |
| 545 | |
| 546 | struct Less { |
| 547 | bool operator()(GrGpuResource* a, GrGpuResource* b) { |
| 548 | return CompareTimestamp(a,b); |
| 549 | } |
| 550 | }; |
| 551 | Less less; |
| 552 | SkTQSort(fNonpurgeableResources.begin(), fNonpurgeableResources.end() - 1, less); |
| 553 | |
| 554 | // Pick resources out of the purgeable and non-purgeable arrays based on lowest |
| 555 | // timestamp and assign new timestamps. |
| 556 | int currP = 0; |
| 557 | int currNP = 0; |
| 558 | while (currP < sortedPurgeableResources.count() && |
| 559 | currNP < fNonpurgeableResources.count()) { |
| 560 | uint32_t tsP = sortedPurgeableResources[currP]->cacheAccess().timestamp(); |
| 561 | uint32_t tsNP = fNonpurgeableResources[currNP]->cacheAccess().timestamp(); |
| 562 | SkASSERT(tsP != tsNP); |
| 563 | if (tsP < tsNP) { |
| 564 | sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++); |
| 565 | } else { |
| 566 | // Correct the index in the nonpurgeable array stored on the resource post-sort. |
| 567 | *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP; |
| 568 | fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | // The above loop ended when we hit the end of one array. Finish the other one. |
| 573 | while (currP < sortedPurgeableResources.count()) { |
| 574 | sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++); |
| 575 | } |
| 576 | while (currNP < fNonpurgeableResources.count()) { |
| 577 | *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP; |
| 578 | fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++); |
| 579 | } |
| 580 | |
| 581 | // Rebuild the queue. |
| 582 | for (int i = 0; i < sortedPurgeableResources.count(); ++i) { |
| 583 | fPurgeableQueue.insert(sortedPurgeableResources[i]); |
| 584 | } |
| 585 | |
| 586 | this->validate(); |
| 587 | SkASSERT(count == this->getResourceCount()); |
| 588 | |
| 589 | // count should be the next timestamp we return. |
| 590 | SkASSERT(fTimestamp == SkToU32(count)); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 591 | |
| 592 | // The historical timestamps of flushes are now invalid. |
| 593 | this->resetFlushTimestamps(); |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 594 | } |
| 595 | } |
| 596 | return fTimestamp++; |
| 597 | } |
| 598 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 599 | void GrResourceCache::notifyFlushOccurred() { |
| 600 | if (fFlushTimestamps) { |
| 601 | SkASSERT(SkIsPow2(fMaxUnusedFlushes)); |
| 602 | fLastFlushTimestampIndex = (fLastFlushTimestampIndex + 1) & (fMaxUnusedFlushes - 1); |
| 603 | // get the timestamp before accessing fFlushTimestamps because getNextTimestamp will |
| 604 | // reallocate fFlushTimestamps on timestamp overflow. |
| 605 | uint32_t timestamp = this->getNextTimestamp(); |
| 606 | fFlushTimestamps[fLastFlushTimestampIndex] = timestamp; |
| 607 | this->purgeAsNeeded(); |
| 608 | } |
| 609 | } |
| 610 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 611 | #ifdef SK_DEBUG |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 612 | void GrResourceCache::validate() const { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 613 | // Reduce the frequency of validations for large resource counts. |
| 614 | static SkRandom gRandom; |
| 615 | int mask = (SkNextPow2(fCount + 1) >> 5) - 1; |
| 616 | if (~mask && (gRandom.nextU() & mask)) { |
| 617 | return; |
| 618 | } |
| 619 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 620 | struct Stats { |
| 621 | size_t fBytes; |
| 622 | int fBudgetedCount; |
| 623 | size_t fBudgetedBytes; |
| 624 | int fLocked; |
| 625 | int fScratch; |
| 626 | int fCouldBeScratch; |
| 627 | int fContent; |
| 628 | const ScratchMap* fScratchMap; |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 629 | const UniqueHash* fUniqueHash; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 630 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 631 | Stats(const GrResourceCache* cache) { |
| 632 | memset(this, 0, sizeof(*this)); |
| 633 | fScratchMap = &cache->fScratchMap; |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 634 | fUniqueHash = &cache->fUniqueHash; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 635 | } |
| 636 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 637 | void update(GrGpuResource* resource) { |
| 638 | fBytes += resource->gpuMemorySize(); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 639 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 640 | if (!resource->isPurgeable()) { |
| 641 | ++fLocked; |
| 642 | } |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 643 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 644 | if (resource->cacheAccess().isScratch()) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 645 | SkASSERT(!resource->getUniqueKey().isValid()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 646 | ++fScratch; |
| 647 | SkASSERT(fScratchMap->countForKey(resource->resourcePriv().getScratchKey())); |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 648 | SkASSERT(!resource->cacheAccess().isExternal()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 649 | } else if (resource->resourcePriv().getScratchKey().isValid()) { |
| 650 | SkASSERT(!resource->resourcePriv().isBudgeted() || |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 651 | resource->getUniqueKey().isValid()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 652 | ++fCouldBeScratch; |
| 653 | SkASSERT(fScratchMap->countForKey(resource->resourcePriv().getScratchKey())); |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 654 | SkASSERT(!resource->cacheAccess().isExternal()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 655 | } |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 656 | const GrUniqueKey& uniqueKey = resource->getUniqueKey(); |
| 657 | if (uniqueKey.isValid()) { |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 658 | ++fContent; |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 659 | SkASSERT(fUniqueHash->find(uniqueKey) == resource); |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 660 | SkASSERT(!resource->cacheAccess().isExternal()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 661 | SkASSERT(resource->resourcePriv().isBudgeted()); |
| 662 | } |
| 663 | |
| 664 | if (resource->resourcePriv().isBudgeted()) { |
| 665 | ++fBudgetedCount; |
| 666 | fBudgetedBytes += resource->gpuMemorySize(); |
| 667 | } |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 668 | } |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 669 | }; |
| 670 | |
| 671 | Stats stats(this); |
| 672 | |
| 673 | for (int i = 0; i < fNonpurgeableResources.count(); ++i) { |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 674 | SkASSERT(!fNonpurgeableResources[i]->isPurgeable() || |
| 675 | fNewlyPurgeableResourceForValidation == fNonpurgeableResources[i]); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 676 | SkASSERT(*fNonpurgeableResources[i]->cacheAccess().accessCacheIndex() == i); |
| 677 | SkASSERT(!fNonpurgeableResources[i]->wasDestroyed()); |
| 678 | stats.update(fNonpurgeableResources[i]); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 679 | } |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 680 | for (int i = 0; i < fPurgeableQueue.count(); ++i) { |
| 681 | SkASSERT(fPurgeableQueue.at(i)->isPurgeable()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 682 | SkASSERT(*fPurgeableQueue.at(i)->cacheAccess().accessCacheIndex() == i); |
| 683 | SkASSERT(!fPurgeableQueue.at(i)->wasDestroyed()); |
| 684 | stats.update(fPurgeableQueue.at(i)); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 685 | } |
| 686 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 687 | SkASSERT(fCount == this->getResourceCount()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 688 | SkASSERT(fBudgetedCount <= fCount); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 689 | SkASSERT(fBudgetedBytes <= fBytes); |
| 690 | SkASSERT(stats.fBytes == fBytes); |
| 691 | SkASSERT(stats.fBudgetedBytes == fBudgetedBytes); |
| 692 | SkASSERT(stats.fBudgetedCount == fBudgetedCount); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 693 | #if GR_CACHE_STATS |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 694 | SkASSERT(fBudgetedHighWaterCount <= fHighWaterCount); |
| 695 | SkASSERT(fBudgetedHighWaterBytes <= fHighWaterBytes); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 696 | SkASSERT(fBytes <= fHighWaterBytes); |
| 697 | SkASSERT(fCount <= fHighWaterCount); |
| 698 | SkASSERT(fBudgetedBytes <= fBudgetedHighWaterBytes); |
| 699 | SkASSERT(fBudgetedCount <= fBudgetedHighWaterCount); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 700 | #endif |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 701 | SkASSERT(stats.fContent == fUniqueHash.count()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 702 | SkASSERT(stats.fScratch + stats.fCouldBeScratch == fScratchMap.count()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 703 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 704 | // This assertion is not currently valid because we can be in recursive notifyCntReachedZero() |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 705 | // calls. This will be fixed when subresource registration is explicit. |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 706 | // bool overBudget = budgetedBytes > fMaxBytes || budgetedCount > fMaxCount; |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 707 | // SkASSERT(!overBudget || locked == count || fPurging); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 708 | } |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 709 | |
| 710 | bool GrResourceCache::isInCache(const GrGpuResource* resource) const { |
| 711 | int index = *resource->cacheAccess().accessCacheIndex(); |
| 712 | if (index < 0) { |
| 713 | return false; |
| 714 | } |
| 715 | if (index < fPurgeableQueue.count() && fPurgeableQueue.at(index) == resource) { |
| 716 | return true; |
| 717 | } |
| 718 | if (index < fNonpurgeableResources.count() && fNonpurgeableResources[index] == resource) { |
| 719 | return true; |
| 720 | } |
| 721 | SkDEBUGFAIL("Resource index should be -1 or the resource should be in the cache."); |
| 722 | return false; |
| 723 | } |
| 724 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 725 | #endif |