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