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