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