blob: 64d470c35f506b3c7639a22cabc0b408e4232707 [file] [log] [blame]
bsalomonc8dc1f72014-08-21 13:02:13 -07001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrResourceCache.h"
Brian Salomon614c1a82018-12-19 15:42:06 -05009#include <atomic>
Adlai Hollere1c8a382021-04-08 15:30:12 -040010#include <vector>
Robert Phillips4e105e22020-07-16 09:18:50 -040011#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/private/GrSingleOwner.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/private/SkTo.h"
14#include "include/utils/SkRandom.h"
Ben Wagner21bca282019-05-15 10:15:52 -040015#include "src/core/SkMessageBus.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/core/SkOpts.h"
17#include "src/core/SkScopeExit.h"
John Stiles6e9ead92020-07-14 00:13:51 +000018#include "src/core/SkTSort.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrCaps.h"
Adlai Hollera0693042020-10-14 11:23:11 -040020#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrGpuResourceCacheAccess.h"
22#include "src/gpu/GrProxyProvider.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000023#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrTextureProxyCacheAccess.h"
Robert Phillipsd464feb2020-10-08 11:00:02 -040025#include "src/gpu/GrThreadSafeCache.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/GrTracing.h"
27#include "src/gpu/SkGr.h"
bsalomon71cb0c22014-11-14 12:10:14 -080028
Robert Phillipse7a959d2021-03-11 14:44:42 -050029DECLARE_SKMESSAGEBUS_MESSAGE(GrUniqueKeyInvalidatedMessage, uint32_t, true);
bsalomon71cb0c22014-11-14 12:10:14 -080030
Robert Phillipsd074b622021-03-15 08:49:24 -040031DECLARE_SKMESSAGEBUS_MESSAGE(GrTextureFreedMessage, GrDirectContext::DirectContextID, true);
Brian Osman13dddce2017-05-09 13:19:50 -040032
Adlai Holler33dbd652020-06-01 12:35:42 -040033#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(fSingleOwner)
Brian Salomon8f8995a2018-10-15 14:32:15 -040034
bsalomon71cb0c22014-11-14 12:10:14 -080035//////////////////////////////////////////////////////////////////////////////
36
bsalomon7775c852014-12-30 12:50:52 -080037GrScratchKey::ResourceType GrScratchKey::GenerateResourceType() {
Mike Klein0ec1c572018-12-04 11:52:51 -050038 static std::atomic<int32_t> nextType{INHERITED::kInvalidDomain + 1};
bsalomonfe369ee2014-11-10 11:59:06 -080039
Adlai Holler4888cda2020-11-06 16:37:37 -050040 int32_t type = nextType.fetch_add(1, std::memory_order_relaxed);
Ben Wagner9bc36fd2018-06-15 14:23:36 -040041 if (type > SkTo<int32_t>(UINT16_MAX)) {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040042 SK_ABORT("Too many Resource Types");
bsalomon71cb0c22014-11-14 12:10:14 -080043 }
44
45 return static_cast<ResourceType>(type);
46}
47
bsalomon8718aaf2015-02-19 07:24:21 -080048GrUniqueKey::Domain GrUniqueKey::GenerateDomain() {
Mike Klein0ec1c572018-12-04 11:52:51 -050049 static std::atomic<int32_t> nextDomain{INHERITED::kInvalidDomain + 1};
bsalomon7775c852014-12-30 12:50:52 -080050
Adlai Holler4888cda2020-11-06 16:37:37 -050051 int32_t domain = nextDomain.fetch_add(1, std::memory_order_relaxed);
Ben Wagner397ee0e2018-06-15 15:13:26 -040052 if (domain > SkTo<int32_t>(UINT16_MAX)) {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040053 SK_ABORT("Too many GrUniqueKey Domains");
bsalomon7775c852014-12-30 12:50:52 -080054 }
bsalomon24db3b12015-01-23 04:24:04 -080055
56 return static_cast<Domain>(domain);
57}
bsalomon3f324322015-04-08 11:01:54 -070058
bsalomon24db3b12015-01-23 04:24:04 -080059uint32_t GrResourceKeyHash(const uint32_t* data, size_t size) {
mtklein4e976072016-08-08 09:06:27 -070060 return SkOpts::hash(data, size);
bsalomon7775c852014-12-30 12:50:52 -080061}
62
bsalomonfe369ee2014-11-10 11:59:06 -080063//////////////////////////////////////////////////////////////////////////////
64
bsalomon0ea80f42015-02-11 10:49:59 -080065class GrResourceCache::AutoValidate : ::SkNoncopyable {
bsalomon71cb0c22014-11-14 12:10:14 -080066public:
bsalomon0ea80f42015-02-11 10:49:59 -080067 AutoValidate(GrResourceCache* cache) : fCache(cache) { cache->validate(); }
bsalomon71cb0c22014-11-14 12:10:14 -080068 ~AutoValidate() { fCache->validate(); }
69private:
bsalomon0ea80f42015-02-11 10:49:59 -080070 GrResourceCache* fCache;
bsalomon71cb0c22014-11-14 12:10:14 -080071};
72
Brian Salomon876a0172019-03-08 11:12:14 -050073//////////////////////////////////////////////////////////////////////////////
74
Robert Phillipsddc21482019-10-16 14:30:09 -040075inline GrResourceCache::TextureAwaitingUnref::TextureAwaitingUnref() = default;
Brian Salomon876a0172019-03-08 11:12:14 -050076
Robert Phillipsddc21482019-10-16 14:30:09 -040077inline GrResourceCache::TextureAwaitingUnref::TextureAwaitingUnref(GrTexture* texture)
78 : fTexture(texture), fNumUnrefs(1) {}
Brian Salomon876a0172019-03-08 11:12:14 -050079
Robert Phillipsddc21482019-10-16 14:30:09 -040080inline GrResourceCache::TextureAwaitingUnref::TextureAwaitingUnref(TextureAwaitingUnref&& that) {
Adlai Holler5ba50af2020-04-29 21:11:14 -040081 fTexture = std::exchange(that.fTexture, nullptr);
82 fNumUnrefs = std::exchange(that.fNumUnrefs, 0);
Brian Salomon876a0172019-03-08 11:12:14 -050083}
84
Robert Phillipsddc21482019-10-16 14:30:09 -040085inline GrResourceCache::TextureAwaitingUnref& GrResourceCache::TextureAwaitingUnref::operator=(
86 TextureAwaitingUnref&& that) {
Adlai Holler5ba50af2020-04-29 21:11:14 -040087 fTexture = std::exchange(that.fTexture, nullptr);
88 fNumUnrefs = std::exchange(that.fNumUnrefs, 0);
Brian Salomon876a0172019-03-08 11:12:14 -050089 return *this;
90}
91
Robert Phillipsddc21482019-10-16 14:30:09 -040092inline GrResourceCache::TextureAwaitingUnref::~TextureAwaitingUnref() {
93 if (fTexture) {
Brian Salomon876a0172019-03-08 11:12:14 -050094 for (int i = 0; i < fNumUnrefs; ++i) {
Robert Phillipsddc21482019-10-16 14:30:09 -040095 fTexture->unref();
Brian Salomon876a0172019-03-08 11:12:14 -050096 }
97 }
98}
99
Robert Phillipsddc21482019-10-16 14:30:09 -0400100inline void GrResourceCache::TextureAwaitingUnref::TextureAwaitingUnref::addRef() { ++fNumUnrefs; }
Brian Salomon876a0172019-03-08 11:12:14 -0500101
Robert Phillipsddc21482019-10-16 14:30:09 -0400102inline void GrResourceCache::TextureAwaitingUnref::unref() {
Brian Salomon876a0172019-03-08 11:12:14 -0500103 SkASSERT(fNumUnrefs > 0);
Robert Phillipsddc21482019-10-16 14:30:09 -0400104 fTexture->unref();
Brian Salomon876a0172019-03-08 11:12:14 -0500105 --fNumUnrefs;
106}
107
Robert Phillipsddc21482019-10-16 14:30:09 -0400108inline bool GrResourceCache::TextureAwaitingUnref::finished() { return !fNumUnrefs; }
Brian Salomon876a0172019-03-08 11:12:14 -0500109
110//////////////////////////////////////////////////////////////////////////////
robertphillipsee843b22016-10-04 05:30:20 -0700111
Robert Phillipsd074b622021-03-15 08:49:24 -0400112GrResourceCache::GrResourceCache(GrSingleOwner* singleOwner,
113 GrDirectContext::DirectContextID owningContextID,
114 uint32_t familyID)
115 : fInvalidUniqueKeyInbox(familyID)
116 , fFreedTextureInbox(owningContextID)
117 , fOwningContextID(owningContextID)
118 , fContextUniqueID(familyID)
Brian Salomonbe1084b2021-01-26 13:29:30 -0500119 , fSingleOwner(singleOwner) {
Robert Phillipsd074b622021-03-15 08:49:24 -0400120 SkASSERT(owningContextID.isValid());
121 SkASSERT(familyID != SK_InvalidUniqueID);
bsalomon71cb0c22014-11-14 12:10:14 -0800122}
123
bsalomon0ea80f42015-02-11 10:49:59 -0800124GrResourceCache::~GrResourceCache() {
bsalomonc8dc1f72014-08-21 13:02:13 -0700125 this->releaseAll();
126}
127
Robert Phillipscf39f372019-09-03 10:29:20 -0400128void GrResourceCache::setLimit(size_t bytes) {
bsalomon71cb0c22014-11-14 12:10:14 -0800129 fMaxBytes = bytes;
130 this->purgeAsNeeded();
131}
132
bsalomon0ea80f42015-02-11 10:49:59 -0800133void GrResourceCache::insertResource(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400134 ASSERT_SINGLE_OWNER
bsalomon49f085d2014-09-05 13:34:00 -0700135 SkASSERT(resource);
bsalomon16961262014-08-26 14:01:07 -0700136 SkASSERT(!this->isInCache(resource));
bsalomonf320e042015-02-17 15:09:34 -0800137 SkASSERT(!resource->wasDestroyed());
Brian Salomon614c1a82018-12-19 15:42:06 -0500138 SkASSERT(!resource->resourcePriv().isPurgeable());
bsalomonddf30e62015-02-19 11:38:44 -0800139
140 // We must set the timestamp before adding to the array in case the timestamp wraps and we wind
141 // up iterating over all the resources that already have timestamps.
142 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
143
bsalomonf320e042015-02-17 15:09:34 -0800144 this->addToNonpurgeableArray(resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800145
bsalomondace19e2014-11-17 07:34:06 -0800146 size_t size = resource->gpuMemorySize();
bsalomonf320e042015-02-17 15:09:34 -0800147 SkDEBUGCODE(++fCount;)
bsalomon84c8e622014-11-17 09:33:27 -0800148 fBytes += size;
bsalomon82b1d622014-11-14 13:59:57 -0800149#if GR_CACHE_STATS
Brian Osman788b9162020-02-07 10:36:46 -0500150 fHighWaterCount = std::max(this->getResourceCount(), fHighWaterCount);
151 fHighWaterBytes = std::max(fBytes, fHighWaterBytes);
bsalomon82b1d622014-11-14 13:59:57 -0800152#endif
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500153 if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
bsalomondace19e2014-11-17 07:34:06 -0800154 ++fBudgetedCount;
155 fBudgetedBytes += size;
Brian Osman39c08ac2017-07-26 09:36:09 -0400156 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800157 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomondace19e2014-11-17 07:34:06 -0800158#if GR_CACHE_STATS
Brian Osman788b9162020-02-07 10:36:46 -0500159 fBudgetedHighWaterCount = std::max(fBudgetedCount, fBudgetedHighWaterCount);
160 fBudgetedHighWaterBytes = std::max(fBudgetedBytes, fBudgetedHighWaterBytes);
bsalomondace19e2014-11-17 07:34:06 -0800161#endif
162 }
Greg Danielda642612021-02-09 18:04:02 -0500163 SkASSERT(!resource->cacheAccess().isUsableAsScratch());
bsalomon71cb0c22014-11-14 12:10:14 -0800164 this->purgeAsNeeded();
bsalomonc8dc1f72014-08-21 13:02:13 -0700165}
166
bsalomon0ea80f42015-02-11 10:49:59 -0800167void GrResourceCache::removeResource(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400168 ASSERT_SINGLE_OWNER
bsalomon9f2d1572015-02-17 11:47:40 -0800169 this->validate();
bsalomon16961262014-08-26 14:01:07 -0700170 SkASSERT(this->isInCache(resource));
bsalomondace19e2014-11-17 07:34:06 -0800171
Derek Sollenbergeree479142017-05-24 11:41:33 -0400172 size_t size = resource->gpuMemorySize();
Brian Salomon614c1a82018-12-19 15:42:06 -0500173 if (resource->resourcePriv().isPurgeable()) {
bsalomon9f2d1572015-02-17 11:47:40 -0800174 fPurgeableQueue.remove(resource);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400175 fPurgeableBytes -= size;
bsalomonf320e042015-02-17 15:09:34 -0800176 } else {
177 this->removeFromNonpurgeableArray(resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800178 }
179
bsalomonf320e042015-02-17 15:09:34 -0800180 SkDEBUGCODE(--fCount;)
bsalomondace19e2014-11-17 07:34:06 -0800181 fBytes -= size;
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500182 if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
bsalomondace19e2014-11-17 07:34:06 -0800183 --fBudgetedCount;
184 fBudgetedBytes -= size;
Brian Osman39c08ac2017-07-26 09:36:09 -0400185 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800186 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomondace19e2014-11-17 07:34:06 -0800187 }
188
Greg Danielda642612021-02-09 18:04:02 -0500189 if (resource->cacheAccess().isUsableAsScratch()) {
bsalomon3582d3e2015-02-13 14:20:05 -0800190 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
bsalomon744998e2014-08-28 09:54:34 -0700191 }
bsalomon8718aaf2015-02-19 07:24:21 -0800192 if (resource->getUniqueKey().isValid()) {
193 fUniqueHash.remove(resource->getUniqueKey());
bsalomon8b79d232014-11-10 10:19:06 -0800194 }
bsalomonb436ed62014-11-17 12:15:56 -0800195 this->validate();
bsalomonc8dc1f72014-08-21 13:02:13 -0700196}
197
bsalomon0ea80f42015-02-11 10:49:59 -0800198void GrResourceCache::abandonAll() {
bsalomon71cb0c22014-11-14 12:10:14 -0800199 AutoValidate av(this);
200
Brian Salomon876a0172019-03-08 11:12:14 -0500201 // We need to make sure to free any resources that were waiting on a free message but never
202 // received one.
Robert Phillipsddc21482019-10-16 14:30:09 -0400203 fTexturesAwaitingUnref.reset();
Greg Danielb2acf0a2018-09-12 09:17:11 -0400204
bsalomonf320e042015-02-17 15:09:34 -0800205 while (fNonpurgeableResources.count()) {
206 GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
207 SkASSERT(!back->wasDestroyed());
208 back->cacheAccess().abandon();
bsalomonc8dc1f72014-08-21 13:02:13 -0700209 }
bsalomonf320e042015-02-17 15:09:34 -0800210
211 while (fPurgeableQueue.count()) {
212 GrGpuResource* top = fPurgeableQueue.peek();
213 SkASSERT(!top->wasDestroyed());
214 top->cacheAccess().abandon();
215 }
216
Robert Phillipseb999bc2020-11-03 08:41:47 -0500217 fThreadSafeCache->dropAllRefs();
218
bsalomon744998e2014-08-28 09:54:34 -0700219 SkASSERT(!fScratchMap.count());
bsalomon8718aaf2015-02-19 07:24:21 -0800220 SkASSERT(!fUniqueHash.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700221 SkASSERT(!fCount);
bsalomonf320e042015-02-17 15:09:34 -0800222 SkASSERT(!this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800223 SkASSERT(!fBytes);
224 SkASSERT(!fBudgetedCount);
225 SkASSERT(!fBudgetedBytes);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400226 SkASSERT(!fPurgeableBytes);
Robert Phillipsddc21482019-10-16 14:30:09 -0400227 SkASSERT(!fTexturesAwaitingUnref.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700228}
229
bsalomon0ea80f42015-02-11 10:49:59 -0800230void GrResourceCache::releaseAll() {
bsalomon71cb0c22014-11-14 12:10:14 -0800231 AutoValidate av(this);
232
Robert Phillipsd464feb2020-10-08 11:00:02 -0400233 fThreadSafeCache->dropAllRefs();
Robert Phillips12d06a32020-09-16 12:31:34 -0400234
Brian Osman13dddce2017-05-09 13:19:50 -0400235 this->processFreedGpuResources();
236
Greg Danielc27eb722018-08-10 09:48:08 -0400237 // We need to make sure to free any resources that were waiting on a free message but never
238 // received one.
Robert Phillipsddc21482019-10-16 14:30:09 -0400239 fTexturesAwaitingUnref.reset();
Greg Danielc27eb722018-08-10 09:48:08 -0400240
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500241 SkASSERT(fProxyProvider); // better have called setProxyProvider
Robert Phillipsd464feb2020-10-08 11:00:02 -0400242 SkASSERT(fThreadSafeCache); // better have called setThreadSafeCache too
Robert Phillips12d06a32020-09-16 12:31:34 -0400243
Robert Phillips3ec95732017-09-29 15:10:39 -0400244 // We must remove the uniqueKeys from the proxies here. While they possess a uniqueKey
245 // they also have a raw pointer back to this class (which is presumably going away)!
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500246 fProxyProvider->removeAllUniqueKeys();
Robert Phillips45a6f142017-09-29 09:49:41 -0400247
Brian Salomon614c1a82018-12-19 15:42:06 -0500248 while (fNonpurgeableResources.count()) {
bsalomonf320e042015-02-17 15:09:34 -0800249 GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
250 SkASSERT(!back->wasDestroyed());
251 back->cacheAccess().release();
bsalomonc8dc1f72014-08-21 13:02:13 -0700252 }
bsalomonf320e042015-02-17 15:09:34 -0800253
254 while (fPurgeableQueue.count()) {
255 GrGpuResource* top = fPurgeableQueue.peek();
256 SkASSERT(!top->wasDestroyed());
257 top->cacheAccess().release();
258 }
259
bsalomon744998e2014-08-28 09:54:34 -0700260 SkASSERT(!fScratchMap.count());
bsalomon8718aaf2015-02-19 07:24:21 -0800261 SkASSERT(!fUniqueHash.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700262 SkASSERT(!fCount);
bsalomonf320e042015-02-17 15:09:34 -0800263 SkASSERT(!this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800264 SkASSERT(!fBytes);
265 SkASSERT(!fBudgetedCount);
266 SkASSERT(!fBudgetedBytes);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400267 SkASSERT(!fPurgeableBytes);
Robert Phillipsddc21482019-10-16 14:30:09 -0400268 SkASSERT(!fTexturesAwaitingUnref.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700269}
bsalomonbcf0a522014-10-08 08:40:09 -0700270
Brian Salomon2c791fc2019-04-02 11:52:03 -0400271void GrResourceCache::refResource(GrGpuResource* resource) {
272 SkASSERT(resource);
273 SkASSERT(resource->getContext()->priv().getResourceCache() == this);
274 if (resource->cacheAccess().hasRef()) {
275 resource->ref();
276 } else {
277 this->refAndMakeResourceMRU(resource);
278 }
279 this->validate();
280}
281
bsalomon0ea80f42015-02-11 10:49:59 -0800282class GrResourceCache::AvailableForScratchUse {
bsalomonbcf0a522014-10-08 08:40:09 -0700283public:
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400284 AvailableForScratchUse() { }
bsalomonbcf0a522014-10-08 08:40:09 -0700285
286 bool operator()(const GrGpuResource* resource) const {
Greg Danielda642612021-02-09 18:04:02 -0500287 // Everything that is in the scratch map should be usable as a
288 // scratch resource.
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400289 return true;
bsalomonbcf0a522014-10-08 08:40:09 -0700290 }
bsalomonbcf0a522014-10-08 08:40:09 -0700291};
292
Robert Phillipsaee18c92019-09-06 11:48:27 -0400293GrGpuResource* GrResourceCache::findAndRefScratchResource(const GrScratchKey& scratchKey) {
bsalomon7775c852014-12-30 12:50:52 -0800294 SkASSERT(scratchKey.isValid());
robertphillipsee843b22016-10-04 05:30:20 -0700295
Robert Phillipsaee18c92019-09-06 11:48:27 -0400296 GrGpuResource* resource = fScratchMap.find(scratchKey, AvailableForScratchUse());
bsalomon71cb0c22014-11-14 12:10:14 -0800297 if (resource) {
Greg Danielda642612021-02-09 18:04:02 -0500298 fScratchMap.remove(scratchKey, resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800299 this->refAndMakeResourceMRU(resource);
bsalomonb436ed62014-11-17 12:15:56 -0800300 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800301 }
302 return resource;
bsalomonbcf0a522014-10-08 08:40:09 -0700303}
bsalomon8b79d232014-11-10 10:19:06 -0800304
bsalomon0ea80f42015-02-11 10:49:59 -0800305void GrResourceCache::willRemoveScratchKey(const GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400306 ASSERT_SINGLE_OWNER
bsalomon3582d3e2015-02-13 14:20:05 -0800307 SkASSERT(resource->resourcePriv().getScratchKey().isValid());
Greg Danielda642612021-02-09 18:04:02 -0500308 if (resource->cacheAccess().isUsableAsScratch()) {
robertphillipsc4ed6842016-05-24 14:17:12 -0700309 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
310 }
bsalomon10e23ca2014-11-25 05:52:06 -0800311}
312
bsalomonf99e9612015-02-19 08:24:16 -0800313void GrResourceCache::removeUniqueKey(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400314 ASSERT_SINGLE_OWNER
bsalomon3f324322015-04-08 11:01:54 -0700315 // 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.
bsalomonf99e9612015-02-19 08:24:16 -0800317 if (resource->getUniqueKey().isValid()) {
318 SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
319 fUniqueHash.remove(resource->getUniqueKey());
320 }
321 resource->cacheAccess().removeUniqueKey();
Greg Danielda642612021-02-09 18:04:02 -0500322 if (resource->cacheAccess().isUsableAsScratch()) {
robertphillipsc4ed6842016-05-24 14:17:12 -0700323 fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
324 }
325
Brian Salomon9bc76d92019-01-24 12:18:33 -0500326 // 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());
bsalomonf99e9612015-02-19 08:24:16 -0800330 this->validate();
bsalomon23e619c2015-02-06 11:54:28 -0800331}
332
bsalomonf99e9612015-02-19 08:24:16 -0800333void GrResourceCache::changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400334 ASSERT_SINGLE_OWNER
bsalomon8b79d232014-11-10 10:19:06 -0800335 SkASSERT(resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800336 SkASSERT(this->isInCache(resource));
bsalomon8b79d232014-11-10 10:19:06 -0800337
bsalomonf99e9612015-02-19 08:24:16 -0800338 // If another resource has the new key, remove its key then install the key on this resource.
339 if (newKey.isValid()) {
Greg Daniel0d537802017-09-08 11:44:14 -0400340 if (GrGpuResource* old = fUniqueHash.find(newKey)) {
341 // If the old resource using the key is purgeable and is unreachable, then remove it.
Brian Salomon614c1a82018-12-19 15:42:06 -0500342 if (!old->resourcePriv().getScratchKey().isValid() &&
343 old->resourcePriv().isPurgeable()) {
Greg Daniel0d537802017-09-08 11:44:14 -0400344 old->cacheAccess().release();
345 } else {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500346 // removeUniqueKey expects an external owner of the resource.
347 this->removeUniqueKey(sk_ref_sp(old).get());
Greg Daniel0d537802017-09-08 11:44:14 -0400348 }
349 }
350 SkASSERT(nullptr == fUniqueHash.find(newKey));
351
robertphillipsc4ed6842016-05-24 14:17:12 -0700352 // Remove the entry for this resource if it already has a unique key.
353 if (resource->getUniqueKey().isValid()) {
354 SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
355 fUniqueHash.remove(resource->getUniqueKey());
356 SkASSERT(nullptr == fUniqueHash.find(resource->getUniqueKey()));
357 } else {
358 // 'resource' didn't have a valid unique key before so it is switching sides. Remove it
Greg Danielda642612021-02-09 18:04:02 -0500359 // from the ScratchMap. The isUsableAsScratch call depends on us not adding the new
360 // unique key until after this check.
361 if (resource->cacheAccess().isUsableAsScratch()) {
robertphillipsc4ed6842016-05-24 14:17:12 -0700362 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
363 }
364 }
365
bsalomonf99e9612015-02-19 08:24:16 -0800366 resource->cacheAccess().setUniqueKey(newKey);
367 fUniqueHash.add(resource);
368 } else {
robertphillipsc4ed6842016-05-24 14:17:12 -0700369 this->removeUniqueKey(resource);
bsalomonf99e9612015-02-19 08:24:16 -0800370 }
371
bsalomon71cb0c22014-11-14 12:10:14 -0800372 this->validate();
bsalomon8b79d232014-11-10 10:19:06 -0800373}
bsalomon71cb0c22014-11-14 12:10:14 -0800374
bsalomon9f2d1572015-02-17 11:47:40 -0800375void GrResourceCache::refAndMakeResourceMRU(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400376 ASSERT_SINGLE_OWNER
bsalomon71cb0c22014-11-14 12:10:14 -0800377 SkASSERT(resource);
378 SkASSERT(this->isInCache(resource));
bsalomonddf30e62015-02-19 11:38:44 -0800379
Brian Salomon614c1a82018-12-19 15:42:06 -0500380 if (resource->resourcePriv().isPurgeable()) {
bsalomon9f2d1572015-02-17 11:47:40 -0800381 // It's about to become unpurgeable.
Derek Sollenbergeree479142017-05-24 11:41:33 -0400382 fPurgeableBytes -= resource->gpuMemorySize();
bsalomon9f2d1572015-02-17 11:47:40 -0800383 fPurgeableQueue.remove(resource);
bsalomonf320e042015-02-17 15:09:34 -0800384 this->addToNonpurgeableArray(resource);
Greg Daniel1fd8ac82020-12-11 11:22:01 -0500385 } else if (!resource->cacheAccess().hasRefOrCommandBufferUsage() &&
Brian Salomon2c791fc2019-04-02 11:52:03 -0400386 resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
387 SkASSERT(fNumBudgetedResourcesFlushWillMakePurgeable > 0);
388 fNumBudgetedResourcesFlushWillMakePurgeable--;
bsalomon9f2d1572015-02-17 11:47:40 -0800389 }
Brian Salomon01ceae92019-04-02 11:49:54 -0400390 resource->cacheAccess().ref();
bsalomonddf30e62015-02-19 11:38:44 -0800391
392 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
bsalomonf320e042015-02-17 15:09:34 -0800393 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800394}
395
Greg Danielda642612021-02-09 18:04:02 -0500396void GrResourceCache::notifyARefCntReachedZero(GrGpuResource* resource,
397 GrGpuResource::LastRemovedRef removedRef) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400398 ASSERT_SINGLE_OWNER
bsalomon71cb0c22014-11-14 12:10:14 -0800399 SkASSERT(resource);
bsalomon3f324322015-04-08 11:01:54 -0700400 SkASSERT(!resource->wasDestroyed());
bsalomon71cb0c22014-11-14 12:10:14 -0800401 SkASSERT(this->isInCache(resource));
bsalomon3f324322015-04-08 11:01:54 -0700402 // This resource should always be in the nonpurgeable array when this function is called. It
403 // will be moved to the queue if it is newly purgeable.
404 SkASSERT(fNonpurgeableResources[*resource->cacheAccess().accessCacheIndex()] == resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800405
Greg Danielda642612021-02-09 18:04:02 -0500406 if (removedRef == GrGpuResource::LastRemovedRef::kMainRef) {
407 if (resource->cacheAccess().isUsableAsScratch()) {
408 fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
409 }
410 }
411
412 if (resource->cacheAccess().hasRefOrCommandBufferUsage()) {
413 this->validate();
414 return;
415 }
416
bsalomon3f324322015-04-08 11:01:54 -0700417#ifdef SK_DEBUG
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400418 // When the timestamp overflows validate() is called. validate() checks that resources in
419 // the nonpurgeable array are indeed not purgeable. However, the movement from the array to
420 // the purgeable queue happens just below in this function. So we mark it as an exception.
421 if (resource->resourcePriv().isPurgeable()) {
422 fNewlyPurgeableResourceForValidation = resource;
bsalomon3f324322015-04-08 11:01:54 -0700423 }
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400424#endif
425 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
426 SkDEBUGCODE(fNewlyPurgeableResourceForValidation = nullptr);
bsalomon3f324322015-04-08 11:01:54 -0700427
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400428 if (!resource->resourcePriv().isPurgeable() &&
429 resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
430 ++fNumBudgetedResourcesFlushWillMakePurgeable;
bsalomon3f324322015-04-08 11:01:54 -0700431 }
432
Brian Salomon9bc76d92019-01-24 12:18:33 -0500433 if (!resource->resourcePriv().isPurgeable()) {
434 this->validate();
435 return;
436 }
437
bsalomonf320e042015-02-17 15:09:34 -0800438 this->removeFromNonpurgeableArray(resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800439 fPurgeableQueue.insert(resource);
Brian Salomon5e150852017-03-22 14:53:13 -0400440 resource->cacheAccess().setTimeWhenResourceBecomePurgeable();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400441 fPurgeableBytes += resource->gpuMemorySize();
bsalomon71cb0c22014-11-14 12:10:14 -0800442
Greg Daniel303e83e2018-09-10 14:10:19 -0400443 bool hasUniqueKey = resource->getUniqueKey().isValid();
444
Brian Salomon9bc76d92019-01-24 12:18:33 -0500445 GrBudgetedType budgetedType = resource->resourcePriv().budgetedType();
Brian Salomon614c1a82018-12-19 15:42:06 -0500446
Brian Salomon9bc76d92019-01-24 12:18:33 -0500447 if (budgetedType == GrBudgetedType::kBudgeted) {
448 // Purge the resource immediately if we're over budget
449 // Also purge if the resource has neither a valid scratch key nor a unique key.
450 bool hasKey = resource->resourcePriv().getScratchKey().isValid() || hasUniqueKey;
451 if (!this->overBudget() && hasKey) {
452 return;
453 }
454 } else {
455 // We keep unbudgeted resources with a unique key in the purgeable queue of the cache so
456 // they can be reused again by the image connected to the unique key.
457 if (hasUniqueKey && budgetedType == GrBudgetedType::kUnbudgetedCacheable) {
458 return;
459 }
460 // Check whether this resource could still be used as a scratch resource.
461 if (!resource->resourcePriv().refsWrappedObjects() &&
462 resource->resourcePriv().getScratchKey().isValid()) {
463 // We won't purge an existing resource to make room for this one.
Robert Phillipscf39f372019-09-03 10:29:20 -0400464 if (this->wouldFit(resource->gpuMemorySize())) {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500465 resource->resourcePriv().makeBudgeted();
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500466 return;
467 }
bsalomonc2f35b72015-01-23 07:19:22 -0800468 }
bsalomonc2f35b72015-01-23 07:19:22 -0800469 }
Brian Salomon9bc76d92019-01-24 12:18:33 -0500470
bsalomonf320e042015-02-17 15:09:34 -0800471 SkDEBUGCODE(int beforeCount = this->getResourceCount();)
bsalomon9f2d1572015-02-17 11:47:40 -0800472 resource->cacheAccess().release();
473 // We should at least free this resource, perhaps dependent resources as well.
bsalomonf320e042015-02-17 15:09:34 -0800474 SkASSERT(this->getResourceCount() < beforeCount);
bsalomon71cb0c22014-11-14 12:10:14 -0800475 this->validate();
476}
477
bsalomon0ea80f42015-02-11 10:49:59 -0800478void GrResourceCache::didChangeBudgetStatus(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400479 ASSERT_SINGLE_OWNER
bsalomon84c8e622014-11-17 09:33:27 -0800480 SkASSERT(resource);
481 SkASSERT(this->isInCache(resource));
482
483 size_t size = resource->gpuMemorySize();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500484 // Changing from BudgetedType::kUnbudgetedCacheable to another budgeted type could make
485 // resource become purgeable. However, we should never allow that transition. Wrapped
486 // resources are the only resources that can be in that state and they aren't allowed to
487 // transition from one budgeted state to another.
488 SkDEBUGCODE(bool wasPurgeable = resource->resourcePriv().isPurgeable());
489 if (resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
bsalomon84c8e622014-11-17 09:33:27 -0800490 ++fBudgetedCount;
491 fBudgetedBytes += size;
bsalomonafe30052015-01-16 07:32:33 -0800492#if GR_CACHE_STATS
Brian Osman788b9162020-02-07 10:36:46 -0500493 fBudgetedHighWaterBytes = std::max(fBudgetedBytes, fBudgetedHighWaterBytes);
494 fBudgetedHighWaterCount = std::max(fBudgetedCount, fBudgetedHighWaterCount);
bsalomonafe30052015-01-16 07:32:33 -0800495#endif
Greg Daniel1fd8ac82020-12-11 11:22:01 -0500496 if (!resource->resourcePriv().isPurgeable() &&
497 !resource->cacheAccess().hasRefOrCommandBufferUsage()) {
Brian Salomon2c791fc2019-04-02 11:52:03 -0400498 ++fNumBudgetedResourcesFlushWillMakePurgeable;
499 }
Greg Danielda642612021-02-09 18:04:02 -0500500 if (resource->cacheAccess().isUsableAsScratch()) {
501 fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
502 }
bsalomon84c8e622014-11-17 09:33:27 -0800503 this->purgeAsNeeded();
504 } else {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500505 SkASSERT(resource->resourcePriv().budgetedType() != GrBudgetedType::kUnbudgetedCacheable);
bsalomon84c8e622014-11-17 09:33:27 -0800506 --fBudgetedCount;
507 fBudgetedBytes -= size;
Greg Daniel1fd8ac82020-12-11 11:22:01 -0500508 if (!resource->resourcePriv().isPurgeable() &&
509 !resource->cacheAccess().hasRefOrCommandBufferUsage()) {
Brian Salomon2c791fc2019-04-02 11:52:03 -0400510 --fNumBudgetedResourcesFlushWillMakePurgeable;
511 }
Greg Danielda642612021-02-09 18:04:02 -0500512 if (!resource->cacheAccess().hasRef() && !resource->getUniqueKey().isValid() &&
513 resource->resourcePriv().getScratchKey().isValid()) {
514 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
515 }
bsalomon84c8e622014-11-17 09:33:27 -0800516 }
Brian Salomon9bc76d92019-01-24 12:18:33 -0500517 SkASSERT(wasPurgeable == resource->resourcePriv().isPurgeable());
Brian Osman39c08ac2017-07-26 09:36:09 -0400518 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800519 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomon84c8e622014-11-17 09:33:27 -0800520
521 this->validate();
522}
523
robertphillipsee843b22016-10-04 05:30:20 -0700524void GrResourceCache::purgeAsNeeded() {
bsalomon3f324322015-04-08 11:01:54 -0700525 SkTArray<GrUniqueKeyInvalidatedMessage> invalidKeyMsgs;
526 fInvalidUniqueKeyInbox.poll(&invalidKeyMsgs);
527 if (invalidKeyMsgs.count()) {
Robert Phillips427966a2018-12-20 17:20:43 -0500528 SkASSERT(fProxyProvider);
529
530 for (int i = 0; i < invalidKeyMsgs.count(); ++i) {
Robert Phillips83c38a82020-10-28 14:57:53 -0400531 if (invalidKeyMsgs[i].inThreadSafeCache()) {
532 fThreadSafeCache->remove(invalidKeyMsgs[i].key());
533 SkASSERT(!fThreadSafeCache->has(invalidKeyMsgs[i].key()));
534 } else {
535 fProxyProvider->processInvalidUniqueKey(
536 invalidKeyMsgs[i].key(), nullptr,
Robert Phillips427966a2018-12-20 17:20:43 -0500537 GrProxyProvider::InvalidateGPUResource::kYes);
Robert Phillips83c38a82020-10-28 14:57:53 -0400538 SkASSERT(!this->findAndRefUniqueResource(invalidKeyMsgs[i].key()));
539 }
Robert Phillips427966a2018-12-20 17:20:43 -0500540 }
bsalomon3f324322015-04-08 11:01:54 -0700541 }
bsalomon71cb0c22014-11-14 12:10:14 -0800542
Brian Osman13dddce2017-05-09 13:19:50 -0400543 this->processFreedGpuResources();
544
bsalomon3f324322015-04-08 11:01:54 -0700545 bool stillOverbudget = this->overBudget();
546 while (stillOverbudget && fPurgeableQueue.count()) {
robertphillipsee843b22016-10-04 05:30:20 -0700547 GrGpuResource* resource = fPurgeableQueue.peek();
Brian Salomon614c1a82018-12-19 15:42:06 -0500548 SkASSERT(resource->resourcePriv().isPurgeable());
bsalomon9f2d1572015-02-17 11:47:40 -0800549 resource->cacheAccess().release();
bsalomon3f324322015-04-08 11:01:54 -0700550 stillOverbudget = this->overBudget();
bsalomon9f2d1572015-02-17 11:47:40 -0800551 }
bsalomon71cb0c22014-11-14 12:10:14 -0800552
Robert Phillips12d06a32020-09-16 12:31:34 -0400553 if (stillOverbudget) {
Robert Phillipsd464feb2020-10-08 11:00:02 -0400554 fThreadSafeCache->dropUniqueRefs(this);
Robert Phillips12d06a32020-09-16 12:31:34 -0400555
556 while (stillOverbudget && fPurgeableQueue.count()) {
557 GrGpuResource* resource = fPurgeableQueue.peek();
558 SkASSERT(resource->resourcePriv().isPurgeable());
559 resource->cacheAccess().release();
560 stillOverbudget = this->overBudget();
561 }
562 }
563
bsalomonb436ed62014-11-17 12:15:56 -0800564 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800565}
566
Robert Phillips6eba0632018-03-28 12:25:42 -0400567void GrResourceCache::purgeUnlockedResources(bool scratchResourcesOnly) {
Robert Phillips12d06a32020-09-16 12:31:34 -0400568
Robert Phillips6eba0632018-03-28 12:25:42 -0400569 if (!scratchResourcesOnly) {
Robert Phillipsd464feb2020-10-08 11:00:02 -0400570 fThreadSafeCache->dropUniqueRefs(nullptr);
Robert Phillips331699c2020-09-22 15:20:01 -0400571
Robert Phillips6eba0632018-03-28 12:25:42 -0400572 // We could disable maintaining the heap property here, but it would add a lot of
573 // complexity. Moreover, this is rarely called.
574 while (fPurgeableQueue.count()) {
575 GrGpuResource* resource = fPurgeableQueue.peek();
Brian Salomon614c1a82018-12-19 15:42:06 -0500576 SkASSERT(resource->resourcePriv().isPurgeable());
Robert Phillips6eba0632018-03-28 12:25:42 -0400577 resource->cacheAccess().release();
578 }
579 } else {
580 // Sort the queue
581 fPurgeableQueue.sort();
582
583 // Make a list of the scratch resources to delete
584 SkTDArray<GrGpuResource*> scratchResources;
585 for (int i = 0; i < fPurgeableQueue.count(); i++) {
586 GrGpuResource* resource = fPurgeableQueue.at(i);
Brian Salomon614c1a82018-12-19 15:42:06 -0500587 SkASSERT(resource->resourcePriv().isPurgeable());
Robert Phillips6eba0632018-03-28 12:25:42 -0400588 if (!resource->getUniqueKey().isValid()) {
589 *scratchResources.append() = resource;
590 }
591 }
592
593 // Delete the scratch resources. This must be done as a separate pass
594 // to avoid messing up the sorted order of the queue
595 for (int i = 0; i < scratchResources.count(); i++) {
596 scratchResources.getAt(i)->cacheAccess().release();
597 }
bsalomon9f2d1572015-02-17 11:47:40 -0800598 }
bsalomon71cb0c22014-11-14 12:10:14 -0800599
bsalomonb436ed62014-11-17 12:15:56 -0800600 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800601}
602
Brian Salomon5e150852017-03-22 14:53:13 -0400603void GrResourceCache::purgeResourcesNotUsedSince(GrStdSteadyClock::time_point purgeTime) {
Robert Phillipsd464feb2020-10-08 11:00:02 -0400604 fThreadSafeCache->dropUniqueRefsOlderThan(purgeTime);
Robert Phillipsc2fe1642020-09-22 17:34:51 -0400605
Brian Salomon5e150852017-03-22 14:53:13 -0400606 while (fPurgeableQueue.count()) {
607 const GrStdSteadyClock::time_point resourceTime =
608 fPurgeableQueue.peek()->cacheAccess().timeWhenResourceBecamePurgeable();
609 if (resourceTime >= purgeTime) {
610 // Resources were given both LRU timestamps and tagged with a frame number when
611 // they first became purgeable. The LRU timestamp won't change again until the
612 // resource is made non-purgeable again. So, at this point all the remaining
613 // resources in the timestamp-sorted queue will have a frame number >= to this
614 // one.
615 break;
616 }
617 GrGpuResource* resource = fPurgeableQueue.peek();
Brian Salomon614c1a82018-12-19 15:42:06 -0500618 SkASSERT(resource->resourcePriv().isPurgeable());
Brian Salomon5e150852017-03-22 14:53:13 -0400619 resource->cacheAccess().release();
620 }
621}
622
Adlai Hollere1c8a382021-04-08 15:30:12 -0400623bool GrResourceCache::purgeToMakeHeadroom(size_t desiredHeadroomBytes) {
624 AutoValidate av(this);
Adlai Hollercd2f96d2021-04-09 17:58:14 -0400625 if (desiredHeadroomBytes > fMaxBytes) {
626 return false;
627 }
Adlai Hollere1c8a382021-04-08 15:30:12 -0400628 if (this->wouldFit(desiredHeadroomBytes)) {
629 return true;
630 }
631 fPurgeableQueue.sort();
632
Adlai Hollercd2f96d2021-04-09 17:58:14 -0400633 size_t projectedBudget = fBudgetedBytes;
Adlai Hollere1c8a382021-04-08 15:30:12 -0400634 int purgeCnt = 0;
635 for (int i = 0; i < fPurgeableQueue.count(); i++) {
636 GrGpuResource* resource = fPurgeableQueue.at(i);
Adlai Hollercd2f96d2021-04-09 17:58:14 -0400637 if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
638 projectedBudget -= resource->gpuMemorySize();
639 }
640 if (projectedBudget + desiredHeadroomBytes <= fMaxBytes) {
Adlai Hollere1c8a382021-04-08 15:30:12 -0400641 purgeCnt = i + 1;
642 break;
643 }
644 }
Adlai Hollercd2f96d2021-04-09 17:58:14 -0400645 if (purgeCnt == 0) {
Adlai Hollere1c8a382021-04-08 15:30:12 -0400646 return false;
647 }
648
649 // Success! Release the resources.
650 // Copy to array first so we don't mess with the queue.
651 std::vector<GrGpuResource*> resources;
652 resources.reserve(purgeCnt);
653 for (int i = 0; i < purgeCnt; i++) {
654 resources.push_back(fPurgeableQueue.at(i));
655 }
656 for (GrGpuResource* resource : resources) {
657 resource->cacheAccess().release();
658 }
659 return true;
660}
661
Derek Sollenberger5480a182017-05-25 16:43:59 -0400662void GrResourceCache::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
663
Brian Osman788b9162020-02-07 10:36:46 -0500664 const size_t tmpByteBudget = std::max((size_t)0, fBytes - bytesToPurge);
Derek Sollenberger5480a182017-05-25 16:43:59 -0400665 bool stillOverbudget = tmpByteBudget < fBytes;
666
667 if (preferScratchResources && bytesToPurge < fPurgeableBytes) {
668 // Sort the queue
669 fPurgeableQueue.sort();
670
671 // Make a list of the scratch resources to delete
672 SkTDArray<GrGpuResource*> scratchResources;
673 size_t scratchByteCount = 0;
674 for (int i = 0; i < fPurgeableQueue.count() && stillOverbudget; i++) {
675 GrGpuResource* resource = fPurgeableQueue.at(i);
Brian Salomon614c1a82018-12-19 15:42:06 -0500676 SkASSERT(resource->resourcePriv().isPurgeable());
Derek Sollenberger5480a182017-05-25 16:43:59 -0400677 if (!resource->getUniqueKey().isValid()) {
678 *scratchResources.append() = resource;
679 scratchByteCount += resource->gpuMemorySize();
680 stillOverbudget = tmpByteBudget < fBytes - scratchByteCount;
681 }
682 }
683
684 // Delete the scratch resources. This must be done as a separate pass
685 // to avoid messing up the sorted order of the queue
686 for (int i = 0; i < scratchResources.count(); i++) {
687 scratchResources.getAt(i)->cacheAccess().release();
688 }
689 stillOverbudget = tmpByteBudget < fBytes;
690
691 this->validate();
692 }
693
694 // Purge any remaining resources in LRU order
695 if (stillOverbudget) {
696 const size_t cachedByteCount = fMaxBytes;
697 fMaxBytes = tmpByteBudget;
698 this->purgeAsNeeded();
699 fMaxBytes = cachedByteCount;
700 }
701}
Robert Phillips12d06a32020-09-16 12:31:34 -0400702
Brian Salomon8cefa3e2019-04-04 11:39:55 -0400703bool GrResourceCache::requestsFlush() const {
704 return this->overBudget() && !fPurgeableQueue.count() &&
705 fNumBudgetedResourcesFlushWillMakePurgeable > 0;
706}
707
Robert Phillipsddc21482019-10-16 14:30:09 -0400708void GrResourceCache::insertDelayedTextureUnref(GrTexture* texture) {
709 texture->ref();
710 uint32_t id = texture->uniqueID().asUInt();
711 if (auto* data = fTexturesAwaitingUnref.find(id)) {
Brian Salomon876a0172019-03-08 11:12:14 -0500712 data->addRef();
713 } else {
Robert Phillipsddc21482019-10-16 14:30:09 -0400714 fTexturesAwaitingUnref.set(id, {texture});
Brian Salomon876a0172019-03-08 11:12:14 -0500715 }
Brian Osman13dddce2017-05-09 13:19:50 -0400716}
717
718void GrResourceCache::processFreedGpuResources() {
Robert Phillips1dfc77c2019-10-16 16:39:45 -0400719 if (!fTexturesAwaitingUnref.count()) {
Robert Phillips12d06a32020-09-16 12:31:34 -0400720 return;
Robert Phillips1dfc77c2019-10-16 16:39:45 -0400721 }
722
Robert Phillipsddc21482019-10-16 14:30:09 -0400723 SkTArray<GrTextureFreedMessage> msgs;
724 fFreedTextureInbox.poll(&msgs);
Brian Osman13dddce2017-05-09 13:19:50 -0400725 for (int i = 0; i < msgs.count(); ++i) {
Robert Phillipsd074b622021-03-15 08:49:24 -0400726 SkASSERT(msgs[i].fIntendedRecipient == fOwningContextID);
Robert Phillipsddc21482019-10-16 14:30:09 -0400727 uint32_t id = msgs[i].fTexture->uniqueID().asUInt();
728 TextureAwaitingUnref* info = fTexturesAwaitingUnref.find(id);
Greg Daniel1a5d2d52019-12-04 11:14:29 -0500729 // If the GrContext was released or abandoned then fTexturesAwaitingUnref should have been
730 // empty and we would have returned early above. Thus, any texture from a message should be
731 // in the list of fTexturesAwaitingUnref.
732 SkASSERT(info);
733 info->unref();
734 if (info->finished()) {
735 fTexturesAwaitingUnref.remove(id);
Greg Danielb2acf0a2018-09-12 09:17:11 -0400736 }
Brian Osman13dddce2017-05-09 13:19:50 -0400737 }
738}
739
bsalomonf320e042015-02-17 15:09:34 -0800740void GrResourceCache::addToNonpurgeableArray(GrGpuResource* resource) {
741 int index = fNonpurgeableResources.count();
742 *fNonpurgeableResources.append() = resource;
743 *resource->cacheAccess().accessCacheIndex() = index;
744}
745
746void GrResourceCache::removeFromNonpurgeableArray(GrGpuResource* resource) {
747 int* index = resource->cacheAccess().accessCacheIndex();
Adlai Holler9555f292020-10-09 09:41:14 -0400748 // Fill the hole we will create in the array with the tail object, adjust its index, and
bsalomonf320e042015-02-17 15:09:34 -0800749 // then pop the array
750 GrGpuResource* tail = *(fNonpurgeableResources.end() - 1);
751 SkASSERT(fNonpurgeableResources[*index] == resource);
752 fNonpurgeableResources[*index] = tail;
753 *tail->cacheAccess().accessCacheIndex() = *index;
754 fNonpurgeableResources.pop();
755 SkDEBUGCODE(*index = -1);
756}
757
bsalomonddf30e62015-02-19 11:38:44 -0800758uint32_t GrResourceCache::getNextTimestamp() {
759 // If we wrap then all the existing resources will appear older than any resources that get
760 // a timestamp after the wrap.
761 if (0 == fTimestamp) {
762 int count = this->getResourceCount();
763 if (count) {
764 // Reset all the timestamps. We sort the resources by timestamp and then assign
765 // sequential timestamps beginning with 0. This is O(n*lg(n)) but it should be extremely
766 // rare.
767 SkTDArray<GrGpuResource*> sortedPurgeableResources;
768 sortedPurgeableResources.setReserve(fPurgeableQueue.count());
769
770 while (fPurgeableQueue.count()) {
771 *sortedPurgeableResources.append() = fPurgeableQueue.peek();
772 fPurgeableQueue.pop();
773 }
robertphillipsee843b22016-10-04 05:30:20 -0700774
John Stiles886a9042020-07-14 16:28:33 -0400775 SkTQSort(fNonpurgeableResources.begin(), fNonpurgeableResources.end(),
John Stiles6e9ead92020-07-14 00:13:51 +0000776 CompareTimestamp);
bsalomonddf30e62015-02-19 11:38:44 -0800777
778 // Pick resources out of the purgeable and non-purgeable arrays based on lowest
779 // timestamp and assign new timestamps.
780 int currP = 0;
781 int currNP = 0;
782 while (currP < sortedPurgeableResources.count() &&
mtklein56da0252015-11-16 11:16:23 -0800783 currNP < fNonpurgeableResources.count()) {
bsalomonddf30e62015-02-19 11:38:44 -0800784 uint32_t tsP = sortedPurgeableResources[currP]->cacheAccess().timestamp();
785 uint32_t tsNP = fNonpurgeableResources[currNP]->cacheAccess().timestamp();
786 SkASSERT(tsP != tsNP);
787 if (tsP < tsNP) {
788 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
789 } else {
790 // Correct the index in the nonpurgeable array stored on the resource post-sort.
791 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
792 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
793 }
794 }
795
796 // The above loop ended when we hit the end of one array. Finish the other one.
797 while (currP < sortedPurgeableResources.count()) {
798 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
799 }
800 while (currNP < fNonpurgeableResources.count()) {
801 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
802 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
803 }
804
805 // Rebuild the queue.
806 for (int i = 0; i < sortedPurgeableResources.count(); ++i) {
807 fPurgeableQueue.insert(sortedPurgeableResources[i]);
808 }
809
810 this->validate();
811 SkASSERT(count == this->getResourceCount());
812
813 // count should be the next timestamp we return.
814 SkASSERT(fTimestamp == SkToU32(count));
mtklein56da0252015-11-16 11:16:23 -0800815 }
bsalomonddf30e62015-02-19 11:38:44 -0800816 }
817 return fTimestamp++;
818}
819
ericrk0a5fa482015-09-15 14:16:10 -0700820void GrResourceCache::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
821 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
822 fNonpurgeableResources[i]->dumpMemoryStatistics(traceMemoryDump);
823 }
824 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
825 fPurgeableQueue.at(i)->dumpMemoryStatistics(traceMemoryDump);
826 }
827}
828
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500829#if GR_CACHE_STATS
830void GrResourceCache::getStats(Stats* stats) const {
831 stats->reset();
832
833 stats->fTotal = this->getResourceCount();
834 stats->fNumNonPurgeable = fNonpurgeableResources.count();
835 stats->fNumPurgeable = fPurgeableQueue.count();
836
837 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
838 stats->update(fNonpurgeableResources[i]);
839 }
840 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
841 stats->update(fPurgeableQueue.at(i));
842 }
843}
844
845#if GR_TEST_UTILS
846void GrResourceCache::dumpStats(SkString* out) const {
847 this->validate();
848
849 Stats stats;
850
851 this->getStats(&stats);
852
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500853 float byteUtilization = (100.f * fBudgetedBytes) / fMaxBytes;
854
Robert Phillipscf39f372019-09-03 10:29:20 -0400855 out->appendf("Budget: %d bytes\n", (int)fMaxBytes);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500856 out->appendf("\t\tEntry Count: current %d"
Robert Phillipscf39f372019-09-03 10:29:20 -0400857 " (%d budgeted, %d wrapped, %d locked, %d scratch), high %d\n",
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500858 stats.fTotal, fBudgetedCount, stats.fWrapped, stats.fNumNonPurgeable,
Robert Phillipscf39f372019-09-03 10:29:20 -0400859 stats.fScratch, fHighWaterCount);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500860 out->appendf("\t\tEntry Bytes: current %d (budgeted %d, %.2g%% full, %d unbudgeted) high %d\n",
861 SkToInt(fBytes), SkToInt(fBudgetedBytes), byteUtilization,
862 SkToInt(stats.fUnbudgetedSize), SkToInt(fHighWaterBytes));
863}
864
865void GrResourceCache::dumpStatsKeyValuePairs(SkTArray<SkString>* keys,
866 SkTArray<double>* values) const {
867 this->validate();
868
869 Stats stats;
870 this->getStats(&stats);
871
872 keys->push_back(SkString("gpu_cache_purgable_entries")); values->push_back(stats.fNumPurgeable);
873}
874#endif
875
876#endif
877
bsalomon71cb0c22014-11-14 12:10:14 -0800878#ifdef SK_DEBUG
bsalomon0ea80f42015-02-11 10:49:59 -0800879void GrResourceCache::validate() const {
bsalomonc2f35b72015-01-23 07:19:22 -0800880 // Reduce the frequency of validations for large resource counts.
881 static SkRandom gRandom;
882 int mask = (SkNextPow2(fCount + 1) >> 5) - 1;
883 if (~mask && (gRandom.nextU() & mask)) {
884 return;
885 }
886
bsalomonf320e042015-02-17 15:09:34 -0800887 struct Stats {
888 size_t fBytes;
889 int fBudgetedCount;
890 size_t fBudgetedBytes;
891 int fLocked;
892 int fScratch;
893 int fCouldBeScratch;
894 int fContent;
895 const ScratchMap* fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800896 const UniqueHash* fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800897
bsalomonf320e042015-02-17 15:09:34 -0800898 Stats(const GrResourceCache* cache) {
899 memset(this, 0, sizeof(*this));
900 fScratchMap = &cache->fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800901 fUniqueHash = &cache->fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800902 }
903
bsalomonf320e042015-02-17 15:09:34 -0800904 void update(GrGpuResource* resource) {
905 fBytes += resource->gpuMemorySize();
bsalomondace19e2014-11-17 07:34:06 -0800906
Brian Salomon614c1a82018-12-19 15:42:06 -0500907 if (!resource->resourcePriv().isPurgeable()) {
bsalomonf320e042015-02-17 15:09:34 -0800908 ++fLocked;
909 }
bsalomon9f2d1572015-02-17 11:47:40 -0800910
robertphillipsc4ed6842016-05-24 14:17:12 -0700911 const GrScratchKey& scratchKey = resource->resourcePriv().getScratchKey();
912 const GrUniqueKey& uniqueKey = resource->getUniqueKey();
913
Greg Danielda642612021-02-09 18:04:02 -0500914 if (resource->cacheAccess().isUsableAsScratch()) {
robertphillipsc4ed6842016-05-24 14:17:12 -0700915 SkASSERT(!uniqueKey.isValid());
Greg Danielda642612021-02-09 18:04:02 -0500916 SkASSERT(GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType());
917 SkASSERT(!resource->cacheAccess().hasRef());
bsalomonf320e042015-02-17 15:09:34 -0800918 ++fScratch;
robertphillipsc4ed6842016-05-24 14:17:12 -0700919 SkASSERT(fScratchMap->countForKey(scratchKey));
kkinnunen2e6055b2016-04-22 01:48:29 -0700920 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
robertphillipsc4ed6842016-05-24 14:17:12 -0700921 } else if (scratchKey.isValid()) {
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500922 SkASSERT(GrBudgetedType::kBudgeted != resource->resourcePriv().budgetedType() ||
Greg Danielda642612021-02-09 18:04:02 -0500923 uniqueKey.isValid() || resource->cacheAccess().hasRef());
kkinnunen2e6055b2016-04-22 01:48:29 -0700924 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
Greg Danielda642612021-02-09 18:04:02 -0500925 SkASSERT(!fScratchMap->has(resource, scratchKey));
bsalomonf320e042015-02-17 15:09:34 -0800926 }
bsalomon8718aaf2015-02-19 07:24:21 -0800927 if (uniqueKey.isValid()) {
bsalomonf320e042015-02-17 15:09:34 -0800928 ++fContent;
bsalomon8718aaf2015-02-19 07:24:21 -0800929 SkASSERT(fUniqueHash->find(uniqueKey) == resource);
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500930 SkASSERT(GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType() ||
Brian Osman0562eb92017-05-08 11:16:39 -0400931 resource->resourcePriv().refsWrappedObjects());
bsalomonf320e042015-02-17 15:09:34 -0800932 }
933
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500934 if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
bsalomonf320e042015-02-17 15:09:34 -0800935 ++fBudgetedCount;
936 fBudgetedBytes += resource->gpuMemorySize();
937 }
bsalomon9f2d1572015-02-17 11:47:40 -0800938 }
bsalomonf320e042015-02-17 15:09:34 -0800939 };
940
robertphillipsc4ed6842016-05-24 14:17:12 -0700941 {
robertphillipsc4ed6842016-05-24 14:17:12 -0700942 int count = 0;
Mike Kleincff63962020-03-14 16:22:45 -0500943 fScratchMap.foreach([&](const GrGpuResource& resource) {
Greg Danielda642612021-02-09 18:04:02 -0500944 SkASSERT(resource.cacheAccess().isUsableAsScratch());
robertphillipsc4ed6842016-05-24 14:17:12 -0700945 count++;
Mike Kleincff63962020-03-14 16:22:45 -0500946 });
947 SkASSERT(count == fScratchMap.count());
robertphillipsc4ed6842016-05-24 14:17:12 -0700948 }
949
bsalomonf320e042015-02-17 15:09:34 -0800950 Stats stats(this);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400951 size_t purgeableBytes = 0;
Brian Salomon2c791fc2019-04-02 11:52:03 -0400952 int numBudgetedResourcesFlushWillMakePurgeable = 0;
bsalomonf320e042015-02-17 15:09:34 -0800953
954 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500955 SkASSERT(!fNonpurgeableResources[i]->resourcePriv().isPurgeable() ||
bsalomon3f324322015-04-08 11:01:54 -0700956 fNewlyPurgeableResourceForValidation == fNonpurgeableResources[i]);
bsalomonf320e042015-02-17 15:09:34 -0800957 SkASSERT(*fNonpurgeableResources[i]->cacheAccess().accessCacheIndex() == i);
958 SkASSERT(!fNonpurgeableResources[i]->wasDestroyed());
Brian Salomon2c791fc2019-04-02 11:52:03 -0400959 if (fNonpurgeableResources[i]->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted &&
Greg Daniel1fd8ac82020-12-11 11:22:01 -0500960 !fNonpurgeableResources[i]->cacheAccess().hasRefOrCommandBufferUsage() &&
Brian Salomon2c791fc2019-04-02 11:52:03 -0400961 fNewlyPurgeableResourceForValidation != fNonpurgeableResources[i]) {
Brian Salomon2c791fc2019-04-02 11:52:03 -0400962 ++numBudgetedResourcesFlushWillMakePurgeable;
963 }
bsalomonf320e042015-02-17 15:09:34 -0800964 stats.update(fNonpurgeableResources[i]);
bsalomon71cb0c22014-11-14 12:10:14 -0800965 }
bsalomon9f2d1572015-02-17 11:47:40 -0800966 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500967 SkASSERT(fPurgeableQueue.at(i)->resourcePriv().isPurgeable());
bsalomonf320e042015-02-17 15:09:34 -0800968 SkASSERT(*fPurgeableQueue.at(i)->cacheAccess().accessCacheIndex() == i);
969 SkASSERT(!fPurgeableQueue.at(i)->wasDestroyed());
970 stats.update(fPurgeableQueue.at(i));
Derek Sollenbergeree479142017-05-24 11:41:33 -0400971 purgeableBytes += fPurgeableQueue.at(i)->gpuMemorySize();
bsalomon9f2d1572015-02-17 11:47:40 -0800972 }
973
bsalomonf320e042015-02-17 15:09:34 -0800974 SkASSERT(fCount == this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800975 SkASSERT(fBudgetedCount <= fCount);
bsalomonf320e042015-02-17 15:09:34 -0800976 SkASSERT(fBudgetedBytes <= fBytes);
977 SkASSERT(stats.fBytes == fBytes);
Brian Salomon2c791fc2019-04-02 11:52:03 -0400978 SkASSERT(fNumBudgetedResourcesFlushWillMakePurgeable ==
979 numBudgetedResourcesFlushWillMakePurgeable);
bsalomonf320e042015-02-17 15:09:34 -0800980 SkASSERT(stats.fBudgetedBytes == fBudgetedBytes);
981 SkASSERT(stats.fBudgetedCount == fBudgetedCount);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400982 SkASSERT(purgeableBytes == fPurgeableBytes);
bsalomon71cb0c22014-11-14 12:10:14 -0800983#if GR_CACHE_STATS
bsalomondace19e2014-11-17 07:34:06 -0800984 SkASSERT(fBudgetedHighWaterCount <= fHighWaterCount);
985 SkASSERT(fBudgetedHighWaterBytes <= fHighWaterBytes);
bsalomonf320e042015-02-17 15:09:34 -0800986 SkASSERT(fBytes <= fHighWaterBytes);
987 SkASSERT(fCount <= fHighWaterCount);
988 SkASSERT(fBudgetedBytes <= fBudgetedHighWaterBytes);
989 SkASSERT(fBudgetedCount <= fBudgetedHighWaterCount);
bsalomon71cb0c22014-11-14 12:10:14 -0800990#endif
bsalomon8718aaf2015-02-19 07:24:21 -0800991 SkASSERT(stats.fContent == fUniqueHash.count());
Greg Danielda642612021-02-09 18:04:02 -0500992 SkASSERT(stats.fScratch == fScratchMap.count());
bsalomon71cb0c22014-11-14 12:10:14 -0800993
bsalomon3f324322015-04-08 11:01:54 -0700994 // This assertion is not currently valid because we can be in recursive notifyCntReachedZero()
bsalomon12299ab2014-11-14 13:33:09 -0800995 // calls. This will be fixed when subresource registration is explicit.
bsalomondace19e2014-11-17 07:34:06 -0800996 // bool overBudget = budgetedBytes > fMaxBytes || budgetedCount > fMaxCount;
bsalomon12299ab2014-11-14 13:33:09 -0800997 // SkASSERT(!overBudget || locked == count || fPurging);
bsalomon71cb0c22014-11-14 12:10:14 -0800998}
bsalomonf320e042015-02-17 15:09:34 -0800999
1000bool GrResourceCache::isInCache(const GrGpuResource* resource) const {
1001 int index = *resource->cacheAccess().accessCacheIndex();
1002 if (index < 0) {
1003 return false;
1004 }
1005 if (index < fPurgeableQueue.count() && fPurgeableQueue.at(index) == resource) {
1006 return true;
1007 }
1008 if (index < fNonpurgeableResources.count() && fNonpurgeableResources[index] == resource) {
1009 return true;
1010 }
1011 SkDEBUGFAIL("Resource index should be -1 or the resource should be in the cache.");
1012 return false;
1013}
1014
bsalomon71cb0c22014-11-14 12:10:14 -08001015#endif