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