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