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