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