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