blob: 4c5cb85e4cbb1d22c98da5c52f79add901217438 [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>
Robert Phillips4e105e22020-07-16 09:18:50 -040010#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/private/GrSingleOwner.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/private/SkTo.h"
13#include "include/utils/SkRandom.h"
Ben Wagner21bca282019-05-15 10:15:52 -040014#include "src/core/SkMessageBus.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/core/SkOpts.h"
16#include "src/core/SkScopeExit.h"
John Stiles6e9ead92020-07-14 00:13:51 +000017#include "src/core/SkTSort.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrCaps.h"
Adlai Hollera0693042020-10-14 11:23:11 -040019#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrGpuResourceCacheAccess.h"
21#include "src/gpu/GrProxyProvider.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000022#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrTextureProxyCacheAccess.h"
Robert Phillipsd464feb2020-10-08 11:00:02 -040024#include "src/gpu/GrThreadSafeCache.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrTracing.h"
26#include "src/gpu/SkGr.h"
bsalomon71cb0c22014-11-14 12:10:14 -080027
Robert Phillipse7a959d2021-03-11 14:44:42 -050028DECLARE_SKMESSAGEBUS_MESSAGE(GrUniqueKeyInvalidatedMessage, uint32_t, true);
bsalomon71cb0c22014-11-14 12:10:14 -080029
Robert Phillipsd074b622021-03-15 08:49:24 -040030DECLARE_SKMESSAGEBUS_MESSAGE(GrTextureFreedMessage, GrDirectContext::DirectContextID, true);
Brian Osman13dddce2017-05-09 13:19:50 -040031
Adlai Holler33dbd652020-06-01 12:35:42 -040032#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(fSingleOwner)
Brian Salomon8f8995a2018-10-15 14:32:15 -040033
bsalomon71cb0c22014-11-14 12:10:14 -080034//////////////////////////////////////////////////////////////////////////////
35
bsalomon7775c852014-12-30 12:50:52 -080036GrScratchKey::ResourceType GrScratchKey::GenerateResourceType() {
Mike Klein0ec1c572018-12-04 11:52:51 -050037 static std::atomic<int32_t> nextType{INHERITED::kInvalidDomain + 1};
bsalomonfe369ee2014-11-10 11:59:06 -080038
Adlai Holler4888cda2020-11-06 16:37:37 -050039 int32_t type = nextType.fetch_add(1, std::memory_order_relaxed);
Ben Wagner9bc36fd2018-06-15 14:23:36 -040040 if (type > SkTo<int32_t>(UINT16_MAX)) {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040041 SK_ABORT("Too many Resource Types");
bsalomon71cb0c22014-11-14 12:10:14 -080042 }
43
44 return static_cast<ResourceType>(type);
45}
46
bsalomon8718aaf2015-02-19 07:24:21 -080047GrUniqueKey::Domain GrUniqueKey::GenerateDomain() {
Mike Klein0ec1c572018-12-04 11:52:51 -050048 static std::atomic<int32_t> nextDomain{INHERITED::kInvalidDomain + 1};
bsalomon7775c852014-12-30 12:50:52 -080049
Adlai Holler4888cda2020-11-06 16:37:37 -050050 int32_t domain = nextDomain.fetch_add(1, std::memory_order_relaxed);
Ben Wagner397ee0e2018-06-15 15:13:26 -040051 if (domain > SkTo<int32_t>(UINT16_MAX)) {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040052 SK_ABORT("Too many GrUniqueKey Domains");
bsalomon7775c852014-12-30 12:50:52 -080053 }
bsalomon24db3b12015-01-23 04:24:04 -080054
55 return static_cast<Domain>(domain);
56}
bsalomon3f324322015-04-08 11:01:54 -070057
bsalomon24db3b12015-01-23 04:24:04 -080058uint32_t GrResourceKeyHash(const uint32_t* data, size_t size) {
mtklein4e976072016-08-08 09:06:27 -070059 return SkOpts::hash(data, size);
bsalomon7775c852014-12-30 12:50:52 -080060}
61
bsalomonfe369ee2014-11-10 11:59:06 -080062//////////////////////////////////////////////////////////////////////////////
63
bsalomon0ea80f42015-02-11 10:49:59 -080064class GrResourceCache::AutoValidate : ::SkNoncopyable {
bsalomon71cb0c22014-11-14 12:10:14 -080065public:
bsalomon0ea80f42015-02-11 10:49:59 -080066 AutoValidate(GrResourceCache* cache) : fCache(cache) { cache->validate(); }
bsalomon71cb0c22014-11-14 12:10:14 -080067 ~AutoValidate() { fCache->validate(); }
68private:
bsalomon0ea80f42015-02-11 10:49:59 -080069 GrResourceCache* fCache;
bsalomon71cb0c22014-11-14 12:10:14 -080070};
71
Brian Salomon876a0172019-03-08 11:12:14 -050072//////////////////////////////////////////////////////////////////////////////
73
Robert Phillipsddc21482019-10-16 14:30:09 -040074inline GrResourceCache::TextureAwaitingUnref::TextureAwaitingUnref() = default;
Brian Salomon876a0172019-03-08 11:12:14 -050075
Robert Phillipsddc21482019-10-16 14:30:09 -040076inline GrResourceCache::TextureAwaitingUnref::TextureAwaitingUnref(GrTexture* texture)
77 : fTexture(texture), fNumUnrefs(1) {}
Brian Salomon876a0172019-03-08 11:12:14 -050078
Robert Phillipsddc21482019-10-16 14:30:09 -040079inline GrResourceCache::TextureAwaitingUnref::TextureAwaitingUnref(TextureAwaitingUnref&& that) {
Adlai Holler5ba50af2020-04-29 21:11:14 -040080 fTexture = std::exchange(that.fTexture, nullptr);
81 fNumUnrefs = std::exchange(that.fNumUnrefs, 0);
Brian Salomon876a0172019-03-08 11:12:14 -050082}
83
Robert Phillipsddc21482019-10-16 14:30:09 -040084inline GrResourceCache::TextureAwaitingUnref& GrResourceCache::TextureAwaitingUnref::operator=(
85 TextureAwaitingUnref&& that) {
Adlai Holler5ba50af2020-04-29 21:11:14 -040086 fTexture = std::exchange(that.fTexture, nullptr);
87 fNumUnrefs = std::exchange(that.fNumUnrefs, 0);
Brian Salomon876a0172019-03-08 11:12:14 -050088 return *this;
89}
90
Robert Phillipsddc21482019-10-16 14:30:09 -040091inline GrResourceCache::TextureAwaitingUnref::~TextureAwaitingUnref() {
92 if (fTexture) {
Brian Salomon876a0172019-03-08 11:12:14 -050093 for (int i = 0; i < fNumUnrefs; ++i) {
Robert Phillipsddc21482019-10-16 14:30:09 -040094 fTexture->unref();
Brian Salomon876a0172019-03-08 11:12:14 -050095 }
96 }
97}
98
Robert Phillipsddc21482019-10-16 14:30:09 -040099inline void GrResourceCache::TextureAwaitingUnref::TextureAwaitingUnref::addRef() { ++fNumUnrefs; }
Brian Salomon876a0172019-03-08 11:12:14 -0500100
Robert Phillipsddc21482019-10-16 14:30:09 -0400101inline void GrResourceCache::TextureAwaitingUnref::unref() {
Brian Salomon876a0172019-03-08 11:12:14 -0500102 SkASSERT(fNumUnrefs > 0);
Robert Phillipsddc21482019-10-16 14:30:09 -0400103 fTexture->unref();
Brian Salomon876a0172019-03-08 11:12:14 -0500104 --fNumUnrefs;
105}
106
Robert Phillipsddc21482019-10-16 14:30:09 -0400107inline bool GrResourceCache::TextureAwaitingUnref::finished() { return !fNumUnrefs; }
Brian Salomon876a0172019-03-08 11:12:14 -0500108
109//////////////////////////////////////////////////////////////////////////////
robertphillipsee843b22016-10-04 05:30:20 -0700110
Robert Phillipsd074b622021-03-15 08:49:24 -0400111GrResourceCache::GrResourceCache(GrSingleOwner* singleOwner,
112 GrDirectContext::DirectContextID owningContextID,
113 uint32_t familyID)
114 : fInvalidUniqueKeyInbox(familyID)
115 , fFreedTextureInbox(owningContextID)
116 , fOwningContextID(owningContextID)
117 , fContextUniqueID(familyID)
Brian Salomonbe1084b2021-01-26 13:29:30 -0500118 , fSingleOwner(singleOwner) {
Robert Phillipsd074b622021-03-15 08:49:24 -0400119 SkASSERT(owningContextID.isValid());
120 SkASSERT(familyID != SK_InvalidUniqueID);
bsalomon71cb0c22014-11-14 12:10:14 -0800121}
122
bsalomon0ea80f42015-02-11 10:49:59 -0800123GrResourceCache::~GrResourceCache() {
bsalomonc8dc1f72014-08-21 13:02:13 -0700124 this->releaseAll();
125}
126
Robert Phillipscf39f372019-09-03 10:29:20 -0400127void GrResourceCache::setLimit(size_t bytes) {
bsalomon71cb0c22014-11-14 12:10:14 -0800128 fMaxBytes = bytes;
129 this->purgeAsNeeded();
130}
131
bsalomon0ea80f42015-02-11 10:49:59 -0800132void GrResourceCache::insertResource(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400133 ASSERT_SINGLE_OWNER
bsalomon49f085d2014-09-05 13:34:00 -0700134 SkASSERT(resource);
bsalomon16961262014-08-26 14:01:07 -0700135 SkASSERT(!this->isInCache(resource));
bsalomonf320e042015-02-17 15:09:34 -0800136 SkASSERT(!resource->wasDestroyed());
Brian Salomon614c1a82018-12-19 15:42:06 -0500137 SkASSERT(!resource->resourcePriv().isPurgeable());
bsalomonddf30e62015-02-19 11:38:44 -0800138
139 // We must set the timestamp before adding to the array in case the timestamp wraps and we wind
140 // up iterating over all the resources that already have timestamps.
141 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
142
bsalomonf320e042015-02-17 15:09:34 -0800143 this->addToNonpurgeableArray(resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800144
bsalomondace19e2014-11-17 07:34:06 -0800145 size_t size = resource->gpuMemorySize();
bsalomonf320e042015-02-17 15:09:34 -0800146 SkDEBUGCODE(++fCount;)
bsalomon84c8e622014-11-17 09:33:27 -0800147 fBytes += size;
bsalomon82b1d622014-11-14 13:59:57 -0800148#if GR_CACHE_STATS
Brian Osman788b9162020-02-07 10:36:46 -0500149 fHighWaterCount = std::max(this->getResourceCount(), fHighWaterCount);
150 fHighWaterBytes = std::max(fBytes, fHighWaterBytes);
bsalomon82b1d622014-11-14 13:59:57 -0800151#endif
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500152 if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
bsalomondace19e2014-11-17 07:34:06 -0800153 ++fBudgetedCount;
154 fBudgetedBytes += size;
Brian Osman39c08ac2017-07-26 09:36:09 -0400155 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800156 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomondace19e2014-11-17 07:34:06 -0800157#if GR_CACHE_STATS
Brian Osman788b9162020-02-07 10:36:46 -0500158 fBudgetedHighWaterCount = std::max(fBudgetedCount, fBudgetedHighWaterCount);
159 fBudgetedHighWaterBytes = std::max(fBudgetedBytes, fBudgetedHighWaterBytes);
bsalomondace19e2014-11-17 07:34:06 -0800160#endif
161 }
Greg Danielda642612021-02-09 18:04:02 -0500162 SkASSERT(!resource->cacheAccess().isUsableAsScratch());
bsalomon71cb0c22014-11-14 12:10:14 -0800163 this->purgeAsNeeded();
bsalomonc8dc1f72014-08-21 13:02:13 -0700164}
165
bsalomon0ea80f42015-02-11 10:49:59 -0800166void GrResourceCache::removeResource(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400167 ASSERT_SINGLE_OWNER
bsalomon9f2d1572015-02-17 11:47:40 -0800168 this->validate();
bsalomon16961262014-08-26 14:01:07 -0700169 SkASSERT(this->isInCache(resource));
bsalomondace19e2014-11-17 07:34:06 -0800170
Derek Sollenbergeree479142017-05-24 11:41:33 -0400171 size_t size = resource->gpuMemorySize();
Brian Salomon614c1a82018-12-19 15:42:06 -0500172 if (resource->resourcePriv().isPurgeable()) {
bsalomon9f2d1572015-02-17 11:47:40 -0800173 fPurgeableQueue.remove(resource);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400174 fPurgeableBytes -= size;
bsalomonf320e042015-02-17 15:09:34 -0800175 } else {
176 this->removeFromNonpurgeableArray(resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800177 }
178
bsalomonf320e042015-02-17 15:09:34 -0800179 SkDEBUGCODE(--fCount;)
bsalomondace19e2014-11-17 07:34:06 -0800180 fBytes -= size;
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500181 if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
bsalomondace19e2014-11-17 07:34:06 -0800182 --fBudgetedCount;
183 fBudgetedBytes -= size;
Brian Osman39c08ac2017-07-26 09:36:09 -0400184 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800185 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomondace19e2014-11-17 07:34:06 -0800186 }
187
Greg Danielda642612021-02-09 18:04:02 -0500188 if (resource->cacheAccess().isUsableAsScratch()) {
bsalomon3582d3e2015-02-13 14:20:05 -0800189 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
bsalomon744998e2014-08-28 09:54:34 -0700190 }
bsalomon8718aaf2015-02-19 07:24:21 -0800191 if (resource->getUniqueKey().isValid()) {
192 fUniqueHash.remove(resource->getUniqueKey());
bsalomon8b79d232014-11-10 10:19:06 -0800193 }
bsalomonb436ed62014-11-17 12:15:56 -0800194 this->validate();
bsalomonc8dc1f72014-08-21 13:02:13 -0700195}
196
bsalomon0ea80f42015-02-11 10:49:59 -0800197void GrResourceCache::abandonAll() {
bsalomon71cb0c22014-11-14 12:10:14 -0800198 AutoValidate av(this);
199
Brian Salomon876a0172019-03-08 11:12:14 -0500200 // We need to make sure to free any resources that were waiting on a free message but never
201 // received one.
Robert Phillipsddc21482019-10-16 14:30:09 -0400202 fTexturesAwaitingUnref.reset();
Greg Danielb2acf0a2018-09-12 09:17:11 -0400203
bsalomonf320e042015-02-17 15:09:34 -0800204 while (fNonpurgeableResources.count()) {
205 GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
206 SkASSERT(!back->wasDestroyed());
207 back->cacheAccess().abandon();
bsalomonc8dc1f72014-08-21 13:02:13 -0700208 }
bsalomonf320e042015-02-17 15:09:34 -0800209
210 while (fPurgeableQueue.count()) {
211 GrGpuResource* top = fPurgeableQueue.peek();
212 SkASSERT(!top->wasDestroyed());
213 top->cacheAccess().abandon();
214 }
215
Robert Phillipseb999bc2020-11-03 08:41:47 -0500216 fThreadSafeCache->dropAllRefs();
217
bsalomon744998e2014-08-28 09:54:34 -0700218 SkASSERT(!fScratchMap.count());
bsalomon8718aaf2015-02-19 07:24:21 -0800219 SkASSERT(!fUniqueHash.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700220 SkASSERT(!fCount);
bsalomonf320e042015-02-17 15:09:34 -0800221 SkASSERT(!this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800222 SkASSERT(!fBytes);
223 SkASSERT(!fBudgetedCount);
224 SkASSERT(!fBudgetedBytes);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400225 SkASSERT(!fPurgeableBytes);
Robert Phillipsddc21482019-10-16 14:30:09 -0400226 SkASSERT(!fTexturesAwaitingUnref.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700227}
228
bsalomon0ea80f42015-02-11 10:49:59 -0800229void GrResourceCache::releaseAll() {
bsalomon71cb0c22014-11-14 12:10:14 -0800230 AutoValidate av(this);
231
Robert Phillipsd464feb2020-10-08 11:00:02 -0400232 fThreadSafeCache->dropAllRefs();
Robert Phillips12d06a32020-09-16 12:31:34 -0400233
Brian Osman13dddce2017-05-09 13:19:50 -0400234 this->processFreedGpuResources();
235
Greg Danielc27eb722018-08-10 09:48:08 -0400236 // We need to make sure to free any resources that were waiting on a free message but never
237 // received one.
Robert Phillipsddc21482019-10-16 14:30:09 -0400238 fTexturesAwaitingUnref.reset();
Greg Danielc27eb722018-08-10 09:48:08 -0400239
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500240 SkASSERT(fProxyProvider); // better have called setProxyProvider
Robert Phillipsd464feb2020-10-08 11:00:02 -0400241 SkASSERT(fThreadSafeCache); // better have called setThreadSafeCache too
Robert Phillips12d06a32020-09-16 12:31:34 -0400242
Robert Phillips3ec95732017-09-29 15:10:39 -0400243 // We must remove the uniqueKeys from the proxies here. While they possess a uniqueKey
244 // they also have a raw pointer back to this class (which is presumably going away)!
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500245 fProxyProvider->removeAllUniqueKeys();
Robert Phillips45a6f142017-09-29 09:49:41 -0400246
Brian Salomon614c1a82018-12-19 15:42:06 -0500247 while (fNonpurgeableResources.count()) {
bsalomonf320e042015-02-17 15:09:34 -0800248 GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
249 SkASSERT(!back->wasDestroyed());
250 back->cacheAccess().release();
bsalomonc8dc1f72014-08-21 13:02:13 -0700251 }
bsalomonf320e042015-02-17 15:09:34 -0800252
253 while (fPurgeableQueue.count()) {
254 GrGpuResource* top = fPurgeableQueue.peek();
255 SkASSERT(!top->wasDestroyed());
256 top->cacheAccess().release();
257 }
258
bsalomon744998e2014-08-28 09:54:34 -0700259 SkASSERT(!fScratchMap.count());
bsalomon8718aaf2015-02-19 07:24:21 -0800260 SkASSERT(!fUniqueHash.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700261 SkASSERT(!fCount);
bsalomonf320e042015-02-17 15:09:34 -0800262 SkASSERT(!this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800263 SkASSERT(!fBytes);
264 SkASSERT(!fBudgetedCount);
265 SkASSERT(!fBudgetedBytes);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400266 SkASSERT(!fPurgeableBytes);
Robert Phillipsddc21482019-10-16 14:30:09 -0400267 SkASSERT(!fTexturesAwaitingUnref.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700268}
bsalomonbcf0a522014-10-08 08:40:09 -0700269
Brian Salomon2c791fc2019-04-02 11:52:03 -0400270void GrResourceCache::refResource(GrGpuResource* resource) {
271 SkASSERT(resource);
272 SkASSERT(resource->getContext()->priv().getResourceCache() == this);
273 if (resource->cacheAccess().hasRef()) {
274 resource->ref();
275 } else {
276 this->refAndMakeResourceMRU(resource);
277 }
278 this->validate();
279}
280
bsalomon0ea80f42015-02-11 10:49:59 -0800281class GrResourceCache::AvailableForScratchUse {
bsalomonbcf0a522014-10-08 08:40:09 -0700282public:
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400283 AvailableForScratchUse() { }
bsalomonbcf0a522014-10-08 08:40:09 -0700284
285 bool operator()(const GrGpuResource* resource) const {
Greg Danielda642612021-02-09 18:04:02 -0500286 // Everything that is in the scratch map should be usable as a
287 // scratch resource.
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400288 return true;
bsalomonbcf0a522014-10-08 08:40:09 -0700289 }
bsalomonbcf0a522014-10-08 08:40:09 -0700290};
291
Robert Phillipsaee18c92019-09-06 11:48:27 -0400292GrGpuResource* GrResourceCache::findAndRefScratchResource(const GrScratchKey& scratchKey) {
bsalomon7775c852014-12-30 12:50:52 -0800293 SkASSERT(scratchKey.isValid());
robertphillipsee843b22016-10-04 05:30:20 -0700294
Robert Phillipsaee18c92019-09-06 11:48:27 -0400295 GrGpuResource* resource = fScratchMap.find(scratchKey, AvailableForScratchUse());
bsalomon71cb0c22014-11-14 12:10:14 -0800296 if (resource) {
Greg Danielda642612021-02-09 18:04:02 -0500297 fScratchMap.remove(scratchKey, resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800298 this->refAndMakeResourceMRU(resource);
bsalomonb436ed62014-11-17 12:15:56 -0800299 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800300 }
301 return resource;
bsalomonbcf0a522014-10-08 08:40:09 -0700302}
bsalomon8b79d232014-11-10 10:19:06 -0800303
bsalomon0ea80f42015-02-11 10:49:59 -0800304void GrResourceCache::willRemoveScratchKey(const GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400305 ASSERT_SINGLE_OWNER
bsalomon3582d3e2015-02-13 14:20:05 -0800306 SkASSERT(resource->resourcePriv().getScratchKey().isValid());
Greg Danielda642612021-02-09 18:04:02 -0500307 if (resource->cacheAccess().isUsableAsScratch()) {
robertphillipsc4ed6842016-05-24 14:17:12 -0700308 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
309 }
bsalomon10e23ca2014-11-25 05:52:06 -0800310}
311
bsalomonf99e9612015-02-19 08:24:16 -0800312void GrResourceCache::removeUniqueKey(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400313 ASSERT_SINGLE_OWNER
bsalomon3f324322015-04-08 11:01:54 -0700314 // Someone has a ref to this resource in order to have removed the key. When the ref count
315 // reaches zero we will get a ref cnt notification and figure out what to do with it.
bsalomonf99e9612015-02-19 08:24:16 -0800316 if (resource->getUniqueKey().isValid()) {
317 SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
318 fUniqueHash.remove(resource->getUniqueKey());
319 }
320 resource->cacheAccess().removeUniqueKey();
Greg Danielda642612021-02-09 18:04:02 -0500321 if (resource->cacheAccess().isUsableAsScratch()) {
robertphillipsc4ed6842016-05-24 14:17:12 -0700322 fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
323 }
324
Brian Salomon9bc76d92019-01-24 12:18:33 -0500325 // Removing a unique key from a kUnbudgetedCacheable resource would make the resource
326 // require purging. However, the resource must be ref'ed to get here and therefore can't
327 // be purgeable. We'll purge it when the refs reach zero.
328 SkASSERT(!resource->resourcePriv().isPurgeable());
bsalomonf99e9612015-02-19 08:24:16 -0800329 this->validate();
bsalomon23e619c2015-02-06 11:54:28 -0800330}
331
bsalomonf99e9612015-02-19 08:24:16 -0800332void GrResourceCache::changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400333 ASSERT_SINGLE_OWNER
bsalomon8b79d232014-11-10 10:19:06 -0800334 SkASSERT(resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800335 SkASSERT(this->isInCache(resource));
bsalomon8b79d232014-11-10 10:19:06 -0800336
bsalomonf99e9612015-02-19 08:24:16 -0800337 // If another resource has the new key, remove its key then install the key on this resource.
338 if (newKey.isValid()) {
Greg Daniel0d537802017-09-08 11:44:14 -0400339 if (GrGpuResource* old = fUniqueHash.find(newKey)) {
340 // If the old resource using the key is purgeable and is unreachable, then remove it.
Brian Salomon614c1a82018-12-19 15:42:06 -0500341 if (!old->resourcePriv().getScratchKey().isValid() &&
342 old->resourcePriv().isPurgeable()) {
Greg Daniel0d537802017-09-08 11:44:14 -0400343 old->cacheAccess().release();
344 } else {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500345 // removeUniqueKey expects an external owner of the resource.
346 this->removeUniqueKey(sk_ref_sp(old).get());
Greg Daniel0d537802017-09-08 11:44:14 -0400347 }
348 }
349 SkASSERT(nullptr == fUniqueHash.find(newKey));
350
robertphillipsc4ed6842016-05-24 14:17:12 -0700351 // Remove the entry for this resource if it already has a unique key.
352 if (resource->getUniqueKey().isValid()) {
353 SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
354 fUniqueHash.remove(resource->getUniqueKey());
355 SkASSERT(nullptr == fUniqueHash.find(resource->getUniqueKey()));
356 } else {
357 // 'resource' didn't have a valid unique key before so it is switching sides. Remove it
Greg Danielda642612021-02-09 18:04:02 -0500358 // from the ScratchMap. The isUsableAsScratch call depends on us not adding the new
359 // unique key until after this check.
360 if (resource->cacheAccess().isUsableAsScratch()) {
robertphillipsc4ed6842016-05-24 14:17:12 -0700361 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
362 }
363 }
364
bsalomonf99e9612015-02-19 08:24:16 -0800365 resource->cacheAccess().setUniqueKey(newKey);
366 fUniqueHash.add(resource);
367 } else {
robertphillipsc4ed6842016-05-24 14:17:12 -0700368 this->removeUniqueKey(resource);
bsalomonf99e9612015-02-19 08:24:16 -0800369 }
370
bsalomon71cb0c22014-11-14 12:10:14 -0800371 this->validate();
bsalomon8b79d232014-11-10 10:19:06 -0800372}
bsalomon71cb0c22014-11-14 12:10:14 -0800373
bsalomon9f2d1572015-02-17 11:47:40 -0800374void GrResourceCache::refAndMakeResourceMRU(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400375 ASSERT_SINGLE_OWNER
bsalomon71cb0c22014-11-14 12:10:14 -0800376 SkASSERT(resource);
377 SkASSERT(this->isInCache(resource));
bsalomonddf30e62015-02-19 11:38:44 -0800378
Brian Salomon614c1a82018-12-19 15:42:06 -0500379 if (resource->resourcePriv().isPurgeable()) {
bsalomon9f2d1572015-02-17 11:47:40 -0800380 // It's about to become unpurgeable.
Derek Sollenbergeree479142017-05-24 11:41:33 -0400381 fPurgeableBytes -= resource->gpuMemorySize();
bsalomon9f2d1572015-02-17 11:47:40 -0800382 fPurgeableQueue.remove(resource);
bsalomonf320e042015-02-17 15:09:34 -0800383 this->addToNonpurgeableArray(resource);
Greg Daniel1fd8ac82020-12-11 11:22:01 -0500384 } else if (!resource->cacheAccess().hasRefOrCommandBufferUsage() &&
Brian Salomon2c791fc2019-04-02 11:52:03 -0400385 resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
386 SkASSERT(fNumBudgetedResourcesFlushWillMakePurgeable > 0);
387 fNumBudgetedResourcesFlushWillMakePurgeable--;
bsalomon9f2d1572015-02-17 11:47:40 -0800388 }
Brian Salomon01ceae92019-04-02 11:49:54 -0400389 resource->cacheAccess().ref();
bsalomonddf30e62015-02-19 11:38:44 -0800390
391 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
bsalomonf320e042015-02-17 15:09:34 -0800392 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800393}
394
Greg Danielda642612021-02-09 18:04:02 -0500395void GrResourceCache::notifyARefCntReachedZero(GrGpuResource* resource,
396 GrGpuResource::LastRemovedRef removedRef) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400397 ASSERT_SINGLE_OWNER
bsalomon71cb0c22014-11-14 12:10:14 -0800398 SkASSERT(resource);
bsalomon3f324322015-04-08 11:01:54 -0700399 SkASSERT(!resource->wasDestroyed());
bsalomon71cb0c22014-11-14 12:10:14 -0800400 SkASSERT(this->isInCache(resource));
bsalomon3f324322015-04-08 11:01:54 -0700401 // This resource should always be in the nonpurgeable array when this function is called. It
402 // will be moved to the queue if it is newly purgeable.
403 SkASSERT(fNonpurgeableResources[*resource->cacheAccess().accessCacheIndex()] == resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800404
Greg Danielda642612021-02-09 18:04:02 -0500405 if (removedRef == GrGpuResource::LastRemovedRef::kMainRef) {
406 if (resource->cacheAccess().isUsableAsScratch()) {
407 fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
408 }
409 }
410
411 if (resource->cacheAccess().hasRefOrCommandBufferUsage()) {
412 this->validate();
413 return;
414 }
415
bsalomon3f324322015-04-08 11:01:54 -0700416#ifdef SK_DEBUG
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400417 // When the timestamp overflows validate() is called. validate() checks that resources in
418 // the nonpurgeable array are indeed not purgeable. However, the movement from the array to
419 // the purgeable queue happens just below in this function. So we mark it as an exception.
420 if (resource->resourcePriv().isPurgeable()) {
421 fNewlyPurgeableResourceForValidation = resource;
bsalomon3f324322015-04-08 11:01:54 -0700422 }
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400423#endif
424 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
425 SkDEBUGCODE(fNewlyPurgeableResourceForValidation = nullptr);
bsalomon3f324322015-04-08 11:01:54 -0700426
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400427 if (!resource->resourcePriv().isPurgeable() &&
428 resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
429 ++fNumBudgetedResourcesFlushWillMakePurgeable;
bsalomon3f324322015-04-08 11:01:54 -0700430 }
431
Brian Salomon9bc76d92019-01-24 12:18:33 -0500432 if (!resource->resourcePriv().isPurgeable()) {
433 this->validate();
434 return;
435 }
436
bsalomonf320e042015-02-17 15:09:34 -0800437 this->removeFromNonpurgeableArray(resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800438 fPurgeableQueue.insert(resource);
Brian Salomon5e150852017-03-22 14:53:13 -0400439 resource->cacheAccess().setTimeWhenResourceBecomePurgeable();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400440 fPurgeableBytes += resource->gpuMemorySize();
bsalomon71cb0c22014-11-14 12:10:14 -0800441
Greg Daniel303e83e2018-09-10 14:10:19 -0400442 bool hasUniqueKey = resource->getUniqueKey().isValid();
443
Brian Salomon9bc76d92019-01-24 12:18:33 -0500444 GrBudgetedType budgetedType = resource->resourcePriv().budgetedType();
Brian Salomon614c1a82018-12-19 15:42:06 -0500445
Brian Salomon9bc76d92019-01-24 12:18:33 -0500446 if (budgetedType == GrBudgetedType::kBudgeted) {
447 // Purge the resource immediately if we're over budget
448 // Also purge if the resource has neither a valid scratch key nor a unique key.
449 bool hasKey = resource->resourcePriv().getScratchKey().isValid() || hasUniqueKey;
450 if (!this->overBudget() && hasKey) {
451 return;
452 }
453 } else {
454 // We keep unbudgeted resources with a unique key in the purgeable queue of the cache so
455 // they can be reused again by the image connected to the unique key.
456 if (hasUniqueKey && budgetedType == GrBudgetedType::kUnbudgetedCacheable) {
457 return;
458 }
459 // Check whether this resource could still be used as a scratch resource.
460 if (!resource->resourcePriv().refsWrappedObjects() &&
461 resource->resourcePriv().getScratchKey().isValid()) {
462 // We won't purge an existing resource to make room for this one.
Robert Phillipscf39f372019-09-03 10:29:20 -0400463 if (this->wouldFit(resource->gpuMemorySize())) {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500464 resource->resourcePriv().makeBudgeted();
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500465 return;
466 }
bsalomonc2f35b72015-01-23 07:19:22 -0800467 }
bsalomonc2f35b72015-01-23 07:19:22 -0800468 }
Brian Salomon9bc76d92019-01-24 12:18:33 -0500469
bsalomonf320e042015-02-17 15:09:34 -0800470 SkDEBUGCODE(int beforeCount = this->getResourceCount();)
bsalomon9f2d1572015-02-17 11:47:40 -0800471 resource->cacheAccess().release();
472 // We should at least free this resource, perhaps dependent resources as well.
bsalomonf320e042015-02-17 15:09:34 -0800473 SkASSERT(this->getResourceCount() < beforeCount);
bsalomon71cb0c22014-11-14 12:10:14 -0800474 this->validate();
475}
476
bsalomon0ea80f42015-02-11 10:49:59 -0800477void GrResourceCache::didChangeBudgetStatus(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400478 ASSERT_SINGLE_OWNER
bsalomon84c8e622014-11-17 09:33:27 -0800479 SkASSERT(resource);
480 SkASSERT(this->isInCache(resource));
481
482 size_t size = resource->gpuMemorySize();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500483 // Changing from BudgetedType::kUnbudgetedCacheable to another budgeted type could make
484 // resource become purgeable. However, we should never allow that transition. Wrapped
485 // resources are the only resources that can be in that state and they aren't allowed to
486 // transition from one budgeted state to another.
487 SkDEBUGCODE(bool wasPurgeable = resource->resourcePriv().isPurgeable());
488 if (resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
bsalomon84c8e622014-11-17 09:33:27 -0800489 ++fBudgetedCount;
490 fBudgetedBytes += size;
bsalomonafe30052015-01-16 07:32:33 -0800491#if GR_CACHE_STATS
Brian Osman788b9162020-02-07 10:36:46 -0500492 fBudgetedHighWaterBytes = std::max(fBudgetedBytes, fBudgetedHighWaterBytes);
493 fBudgetedHighWaterCount = std::max(fBudgetedCount, fBudgetedHighWaterCount);
bsalomonafe30052015-01-16 07:32:33 -0800494#endif
Greg Daniel1fd8ac82020-12-11 11:22:01 -0500495 if (!resource->resourcePriv().isPurgeable() &&
496 !resource->cacheAccess().hasRefOrCommandBufferUsage()) {
Brian Salomon2c791fc2019-04-02 11:52:03 -0400497 ++fNumBudgetedResourcesFlushWillMakePurgeable;
498 }
Greg Danielda642612021-02-09 18:04:02 -0500499 if (resource->cacheAccess().isUsableAsScratch()) {
500 fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
501 }
bsalomon84c8e622014-11-17 09:33:27 -0800502 this->purgeAsNeeded();
503 } else {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500504 SkASSERT(resource->resourcePriv().budgetedType() != GrBudgetedType::kUnbudgetedCacheable);
bsalomon84c8e622014-11-17 09:33:27 -0800505 --fBudgetedCount;
506 fBudgetedBytes -= size;
Greg Daniel1fd8ac82020-12-11 11:22:01 -0500507 if (!resource->resourcePriv().isPurgeable() &&
508 !resource->cacheAccess().hasRefOrCommandBufferUsage()) {
Brian Salomon2c791fc2019-04-02 11:52:03 -0400509 --fNumBudgetedResourcesFlushWillMakePurgeable;
510 }
Greg Danielda642612021-02-09 18:04:02 -0500511 if (!resource->cacheAccess().hasRef() && !resource->getUniqueKey().isValid() &&
512 resource->resourcePriv().getScratchKey().isValid()) {
513 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
514 }
bsalomon84c8e622014-11-17 09:33:27 -0800515 }
Brian Salomon9bc76d92019-01-24 12:18:33 -0500516 SkASSERT(wasPurgeable == resource->resourcePriv().isPurgeable());
Brian Osman39c08ac2017-07-26 09:36:09 -0400517 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800518 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomon84c8e622014-11-17 09:33:27 -0800519
520 this->validate();
521}
522
robertphillipsee843b22016-10-04 05:30:20 -0700523void GrResourceCache::purgeAsNeeded() {
bsalomon3f324322015-04-08 11:01:54 -0700524 SkTArray<GrUniqueKeyInvalidatedMessage> invalidKeyMsgs;
525 fInvalidUniqueKeyInbox.poll(&invalidKeyMsgs);
526 if (invalidKeyMsgs.count()) {
Robert Phillips427966a2018-12-20 17:20:43 -0500527 SkASSERT(fProxyProvider);
528
529 for (int i = 0; i < invalidKeyMsgs.count(); ++i) {
Robert Phillips83c38a82020-10-28 14:57:53 -0400530 if (invalidKeyMsgs[i].inThreadSafeCache()) {
531 fThreadSafeCache->remove(invalidKeyMsgs[i].key());
532 SkASSERT(!fThreadSafeCache->has(invalidKeyMsgs[i].key()));
533 } else {
534 fProxyProvider->processInvalidUniqueKey(
535 invalidKeyMsgs[i].key(), nullptr,
Robert Phillips427966a2018-12-20 17:20:43 -0500536 GrProxyProvider::InvalidateGPUResource::kYes);
Robert Phillips83c38a82020-10-28 14:57:53 -0400537 SkASSERT(!this->findAndRefUniqueResource(invalidKeyMsgs[i].key()));
538 }
Robert Phillips427966a2018-12-20 17:20:43 -0500539 }
bsalomon3f324322015-04-08 11:01:54 -0700540 }
bsalomon71cb0c22014-11-14 12:10:14 -0800541
Brian Osman13dddce2017-05-09 13:19:50 -0400542 this->processFreedGpuResources();
543
bsalomon3f324322015-04-08 11:01:54 -0700544 bool stillOverbudget = this->overBudget();
545 while (stillOverbudget && fPurgeableQueue.count()) {
robertphillipsee843b22016-10-04 05:30:20 -0700546 GrGpuResource* resource = fPurgeableQueue.peek();
Brian Salomon614c1a82018-12-19 15:42:06 -0500547 SkASSERT(resource->resourcePriv().isPurgeable());
bsalomon9f2d1572015-02-17 11:47:40 -0800548 resource->cacheAccess().release();
bsalomon3f324322015-04-08 11:01:54 -0700549 stillOverbudget = this->overBudget();
bsalomon9f2d1572015-02-17 11:47:40 -0800550 }
bsalomon71cb0c22014-11-14 12:10:14 -0800551
Robert Phillips12d06a32020-09-16 12:31:34 -0400552 if (stillOverbudget) {
Robert Phillipsd464feb2020-10-08 11:00:02 -0400553 fThreadSafeCache->dropUniqueRefs(this);
Robert Phillips12d06a32020-09-16 12:31:34 -0400554
555 while (stillOverbudget && fPurgeableQueue.count()) {
556 GrGpuResource* resource = fPurgeableQueue.peek();
557 SkASSERT(resource->resourcePriv().isPurgeable());
558 resource->cacheAccess().release();
559 stillOverbudget = this->overBudget();
560 }
561 }
562
bsalomonb436ed62014-11-17 12:15:56 -0800563 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800564}
565
Robert Phillips6eba0632018-03-28 12:25:42 -0400566void GrResourceCache::purgeUnlockedResources(bool scratchResourcesOnly) {
Robert Phillips12d06a32020-09-16 12:31:34 -0400567
Robert Phillips6eba0632018-03-28 12:25:42 -0400568 if (!scratchResourcesOnly) {
Robert Phillipsd464feb2020-10-08 11:00:02 -0400569 fThreadSafeCache->dropUniqueRefs(nullptr);
Robert Phillips331699c2020-09-22 15:20:01 -0400570
Robert Phillips6eba0632018-03-28 12:25:42 -0400571 // We could disable maintaining the heap property here, but it would add a lot of
572 // complexity. Moreover, this is rarely called.
573 while (fPurgeableQueue.count()) {
574 GrGpuResource* resource = fPurgeableQueue.peek();
Brian Salomon614c1a82018-12-19 15:42:06 -0500575 SkASSERT(resource->resourcePriv().isPurgeable());
Robert Phillips6eba0632018-03-28 12:25:42 -0400576 resource->cacheAccess().release();
577 }
578 } else {
579 // Sort the queue
580 fPurgeableQueue.sort();
581
582 // Make a list of the scratch resources to delete
583 SkTDArray<GrGpuResource*> scratchResources;
584 for (int i = 0; i < fPurgeableQueue.count(); i++) {
585 GrGpuResource* resource = fPurgeableQueue.at(i);
Brian Salomon614c1a82018-12-19 15:42:06 -0500586 SkASSERT(resource->resourcePriv().isPurgeable());
Robert Phillips6eba0632018-03-28 12:25:42 -0400587 if (!resource->getUniqueKey().isValid()) {
588 *scratchResources.append() = resource;
589 }
590 }
591
592 // Delete the scratch resources. This must be done as a separate pass
593 // to avoid messing up the sorted order of the queue
594 for (int i = 0; i < scratchResources.count(); i++) {
595 scratchResources.getAt(i)->cacheAccess().release();
596 }
bsalomon9f2d1572015-02-17 11:47:40 -0800597 }
bsalomon71cb0c22014-11-14 12:10:14 -0800598
bsalomonb436ed62014-11-17 12:15:56 -0800599 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800600}
601
Brian Salomon5e150852017-03-22 14:53:13 -0400602void GrResourceCache::purgeResourcesNotUsedSince(GrStdSteadyClock::time_point purgeTime) {
Robert Phillipsd464feb2020-10-08 11:00:02 -0400603 fThreadSafeCache->dropUniqueRefsOlderThan(purgeTime);
Robert Phillipsc2fe1642020-09-22 17:34:51 -0400604
Brian Salomon5e150852017-03-22 14:53:13 -0400605 while (fPurgeableQueue.count()) {
606 const GrStdSteadyClock::time_point resourceTime =
607 fPurgeableQueue.peek()->cacheAccess().timeWhenResourceBecamePurgeable();
608 if (resourceTime >= purgeTime) {
609 // Resources were given both LRU timestamps and tagged with a frame number when
610 // they first became purgeable. The LRU timestamp won't change again until the
611 // resource is made non-purgeable again. So, at this point all the remaining
612 // resources in the timestamp-sorted queue will have a frame number >= to this
613 // one.
614 break;
615 }
616 GrGpuResource* resource = fPurgeableQueue.peek();
Brian Salomon614c1a82018-12-19 15:42:06 -0500617 SkASSERT(resource->resourcePriv().isPurgeable());
Brian Salomon5e150852017-03-22 14:53:13 -0400618 resource->cacheAccess().release();
619 }
620}
621
Derek Sollenberger5480a182017-05-25 16:43:59 -0400622void GrResourceCache::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
623
Brian Osman788b9162020-02-07 10:36:46 -0500624 const size_t tmpByteBudget = std::max((size_t)0, fBytes - bytesToPurge);
Derek Sollenberger5480a182017-05-25 16:43:59 -0400625 bool stillOverbudget = tmpByteBudget < fBytes;
626
627 if (preferScratchResources && bytesToPurge < fPurgeableBytes) {
628 // Sort the queue
629 fPurgeableQueue.sort();
630
631 // Make a list of the scratch resources to delete
632 SkTDArray<GrGpuResource*> scratchResources;
633 size_t scratchByteCount = 0;
634 for (int i = 0; i < fPurgeableQueue.count() && stillOverbudget; i++) {
635 GrGpuResource* resource = fPurgeableQueue.at(i);
Brian Salomon614c1a82018-12-19 15:42:06 -0500636 SkASSERT(resource->resourcePriv().isPurgeable());
Derek Sollenberger5480a182017-05-25 16:43:59 -0400637 if (!resource->getUniqueKey().isValid()) {
638 *scratchResources.append() = resource;
639 scratchByteCount += resource->gpuMemorySize();
640 stillOverbudget = tmpByteBudget < fBytes - scratchByteCount;
641 }
642 }
643
644 // Delete the scratch resources. This must be done as a separate pass
645 // to avoid messing up the sorted order of the queue
646 for (int i = 0; i < scratchResources.count(); i++) {
647 scratchResources.getAt(i)->cacheAccess().release();
648 }
649 stillOverbudget = tmpByteBudget < fBytes;
650
651 this->validate();
652 }
653
654 // Purge any remaining resources in LRU order
655 if (stillOverbudget) {
656 const size_t cachedByteCount = fMaxBytes;
657 fMaxBytes = tmpByteBudget;
658 this->purgeAsNeeded();
659 fMaxBytes = cachedByteCount;
660 }
661}
Robert Phillips12d06a32020-09-16 12:31:34 -0400662
Brian Salomon8cefa3e2019-04-04 11:39:55 -0400663bool GrResourceCache::requestsFlush() const {
664 return this->overBudget() && !fPurgeableQueue.count() &&
665 fNumBudgetedResourcesFlushWillMakePurgeable > 0;
666}
667
Robert Phillipsddc21482019-10-16 14:30:09 -0400668void GrResourceCache::insertDelayedTextureUnref(GrTexture* texture) {
669 texture->ref();
670 uint32_t id = texture->uniqueID().asUInt();
671 if (auto* data = fTexturesAwaitingUnref.find(id)) {
Brian Salomon876a0172019-03-08 11:12:14 -0500672 data->addRef();
673 } else {
Robert Phillipsddc21482019-10-16 14:30:09 -0400674 fTexturesAwaitingUnref.set(id, {texture});
Brian Salomon876a0172019-03-08 11:12:14 -0500675 }
Brian Osman13dddce2017-05-09 13:19:50 -0400676}
677
678void GrResourceCache::processFreedGpuResources() {
Robert Phillips1dfc77c2019-10-16 16:39:45 -0400679 if (!fTexturesAwaitingUnref.count()) {
Robert Phillips12d06a32020-09-16 12:31:34 -0400680 return;
Robert Phillips1dfc77c2019-10-16 16:39:45 -0400681 }
682
Robert Phillipsddc21482019-10-16 14:30:09 -0400683 SkTArray<GrTextureFreedMessage> msgs;
684 fFreedTextureInbox.poll(&msgs);
Brian Osman13dddce2017-05-09 13:19:50 -0400685 for (int i = 0; i < msgs.count(); ++i) {
Robert Phillipsd074b622021-03-15 08:49:24 -0400686 SkASSERT(msgs[i].fIntendedRecipient == fOwningContextID);
Robert Phillipsddc21482019-10-16 14:30:09 -0400687 uint32_t id = msgs[i].fTexture->uniqueID().asUInt();
688 TextureAwaitingUnref* info = fTexturesAwaitingUnref.find(id);
Greg Daniel1a5d2d52019-12-04 11:14:29 -0500689 // If the GrContext was released or abandoned then fTexturesAwaitingUnref should have been
690 // empty and we would have returned early above. Thus, any texture from a message should be
691 // in the list of fTexturesAwaitingUnref.
692 SkASSERT(info);
693 info->unref();
694 if (info->finished()) {
695 fTexturesAwaitingUnref.remove(id);
Greg Danielb2acf0a2018-09-12 09:17:11 -0400696 }
Brian Osman13dddce2017-05-09 13:19:50 -0400697 }
698}
699
bsalomonf320e042015-02-17 15:09:34 -0800700void GrResourceCache::addToNonpurgeableArray(GrGpuResource* resource) {
701 int index = fNonpurgeableResources.count();
702 *fNonpurgeableResources.append() = resource;
703 *resource->cacheAccess().accessCacheIndex() = index;
704}
705
706void GrResourceCache::removeFromNonpurgeableArray(GrGpuResource* resource) {
707 int* index = resource->cacheAccess().accessCacheIndex();
Adlai Holler9555f292020-10-09 09:41:14 -0400708 // Fill the hole we will create in the array with the tail object, adjust its index, and
bsalomonf320e042015-02-17 15:09:34 -0800709 // then pop the array
710 GrGpuResource* tail = *(fNonpurgeableResources.end() - 1);
711 SkASSERT(fNonpurgeableResources[*index] == resource);
712 fNonpurgeableResources[*index] = tail;
713 *tail->cacheAccess().accessCacheIndex() = *index;
714 fNonpurgeableResources.pop();
715 SkDEBUGCODE(*index = -1);
716}
717
bsalomonddf30e62015-02-19 11:38:44 -0800718uint32_t GrResourceCache::getNextTimestamp() {
719 // If we wrap then all the existing resources will appear older than any resources that get
720 // a timestamp after the wrap.
721 if (0 == fTimestamp) {
722 int count = this->getResourceCount();
723 if (count) {
724 // Reset all the timestamps. We sort the resources by timestamp and then assign
725 // sequential timestamps beginning with 0. This is O(n*lg(n)) but it should be extremely
726 // rare.
727 SkTDArray<GrGpuResource*> sortedPurgeableResources;
728 sortedPurgeableResources.setReserve(fPurgeableQueue.count());
729
730 while (fPurgeableQueue.count()) {
731 *sortedPurgeableResources.append() = fPurgeableQueue.peek();
732 fPurgeableQueue.pop();
733 }
robertphillipsee843b22016-10-04 05:30:20 -0700734
John Stiles886a9042020-07-14 16:28:33 -0400735 SkTQSort(fNonpurgeableResources.begin(), fNonpurgeableResources.end(),
John Stiles6e9ead92020-07-14 00:13:51 +0000736 CompareTimestamp);
bsalomonddf30e62015-02-19 11:38:44 -0800737
738 // Pick resources out of the purgeable and non-purgeable arrays based on lowest
739 // timestamp and assign new timestamps.
740 int currP = 0;
741 int currNP = 0;
742 while (currP < sortedPurgeableResources.count() &&
mtklein56da0252015-11-16 11:16:23 -0800743 currNP < fNonpurgeableResources.count()) {
bsalomonddf30e62015-02-19 11:38:44 -0800744 uint32_t tsP = sortedPurgeableResources[currP]->cacheAccess().timestamp();
745 uint32_t tsNP = fNonpurgeableResources[currNP]->cacheAccess().timestamp();
746 SkASSERT(tsP != tsNP);
747 if (tsP < tsNP) {
748 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
749 } else {
750 // Correct the index in the nonpurgeable array stored on the resource post-sort.
751 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
752 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
753 }
754 }
755
756 // The above loop ended when we hit the end of one array. Finish the other one.
757 while (currP < sortedPurgeableResources.count()) {
758 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
759 }
760 while (currNP < fNonpurgeableResources.count()) {
761 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
762 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
763 }
764
765 // Rebuild the queue.
766 for (int i = 0; i < sortedPurgeableResources.count(); ++i) {
767 fPurgeableQueue.insert(sortedPurgeableResources[i]);
768 }
769
770 this->validate();
771 SkASSERT(count == this->getResourceCount());
772
773 // count should be the next timestamp we return.
774 SkASSERT(fTimestamp == SkToU32(count));
mtklein56da0252015-11-16 11:16:23 -0800775 }
bsalomonddf30e62015-02-19 11:38:44 -0800776 }
777 return fTimestamp++;
778}
779
ericrk0a5fa482015-09-15 14:16:10 -0700780void GrResourceCache::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
781 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
782 fNonpurgeableResources[i]->dumpMemoryStatistics(traceMemoryDump);
783 }
784 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
785 fPurgeableQueue.at(i)->dumpMemoryStatistics(traceMemoryDump);
786 }
787}
788
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500789#if GR_CACHE_STATS
790void GrResourceCache::getStats(Stats* stats) const {
791 stats->reset();
792
793 stats->fTotal = this->getResourceCount();
794 stats->fNumNonPurgeable = fNonpurgeableResources.count();
795 stats->fNumPurgeable = fPurgeableQueue.count();
796
797 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
798 stats->update(fNonpurgeableResources[i]);
799 }
800 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
801 stats->update(fPurgeableQueue.at(i));
802 }
803}
804
805#if GR_TEST_UTILS
806void GrResourceCache::dumpStats(SkString* out) const {
807 this->validate();
808
809 Stats stats;
810
811 this->getStats(&stats);
812
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500813 float byteUtilization = (100.f * fBudgetedBytes) / fMaxBytes;
814
Robert Phillipscf39f372019-09-03 10:29:20 -0400815 out->appendf("Budget: %d bytes\n", (int)fMaxBytes);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500816 out->appendf("\t\tEntry Count: current %d"
Robert Phillipscf39f372019-09-03 10:29:20 -0400817 " (%d budgeted, %d wrapped, %d locked, %d scratch), high %d\n",
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500818 stats.fTotal, fBudgetedCount, stats.fWrapped, stats.fNumNonPurgeable,
Robert Phillipscf39f372019-09-03 10:29:20 -0400819 stats.fScratch, fHighWaterCount);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500820 out->appendf("\t\tEntry Bytes: current %d (budgeted %d, %.2g%% full, %d unbudgeted) high %d\n",
821 SkToInt(fBytes), SkToInt(fBudgetedBytes), byteUtilization,
822 SkToInt(stats.fUnbudgetedSize), SkToInt(fHighWaterBytes));
823}
824
825void GrResourceCache::dumpStatsKeyValuePairs(SkTArray<SkString>* keys,
826 SkTArray<double>* values) const {
827 this->validate();
828
829 Stats stats;
830 this->getStats(&stats);
831
832 keys->push_back(SkString("gpu_cache_purgable_entries")); values->push_back(stats.fNumPurgeable);
833}
834#endif
835
836#endif
837
bsalomon71cb0c22014-11-14 12:10:14 -0800838#ifdef SK_DEBUG
bsalomon0ea80f42015-02-11 10:49:59 -0800839void GrResourceCache::validate() const {
bsalomonc2f35b72015-01-23 07:19:22 -0800840 // Reduce the frequency of validations for large resource counts.
841 static SkRandom gRandom;
842 int mask = (SkNextPow2(fCount + 1) >> 5) - 1;
843 if (~mask && (gRandom.nextU() & mask)) {
844 return;
845 }
846
bsalomonf320e042015-02-17 15:09:34 -0800847 struct Stats {
848 size_t fBytes;
849 int fBudgetedCount;
850 size_t fBudgetedBytes;
851 int fLocked;
852 int fScratch;
853 int fCouldBeScratch;
854 int fContent;
855 const ScratchMap* fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800856 const UniqueHash* fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800857
bsalomonf320e042015-02-17 15:09:34 -0800858 Stats(const GrResourceCache* cache) {
859 memset(this, 0, sizeof(*this));
860 fScratchMap = &cache->fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800861 fUniqueHash = &cache->fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800862 }
863
bsalomonf320e042015-02-17 15:09:34 -0800864 void update(GrGpuResource* resource) {
865 fBytes += resource->gpuMemorySize();
bsalomondace19e2014-11-17 07:34:06 -0800866
Brian Salomon614c1a82018-12-19 15:42:06 -0500867 if (!resource->resourcePriv().isPurgeable()) {
bsalomonf320e042015-02-17 15:09:34 -0800868 ++fLocked;
869 }
bsalomon9f2d1572015-02-17 11:47:40 -0800870
robertphillipsc4ed6842016-05-24 14:17:12 -0700871 const GrScratchKey& scratchKey = resource->resourcePriv().getScratchKey();
872 const GrUniqueKey& uniqueKey = resource->getUniqueKey();
873
Greg Danielda642612021-02-09 18:04:02 -0500874 if (resource->cacheAccess().isUsableAsScratch()) {
robertphillipsc4ed6842016-05-24 14:17:12 -0700875 SkASSERT(!uniqueKey.isValid());
Greg Danielda642612021-02-09 18:04:02 -0500876 SkASSERT(GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType());
877 SkASSERT(!resource->cacheAccess().hasRef());
bsalomonf320e042015-02-17 15:09:34 -0800878 ++fScratch;
robertphillipsc4ed6842016-05-24 14:17:12 -0700879 SkASSERT(fScratchMap->countForKey(scratchKey));
kkinnunen2e6055b2016-04-22 01:48:29 -0700880 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
robertphillipsc4ed6842016-05-24 14:17:12 -0700881 } else if (scratchKey.isValid()) {
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500882 SkASSERT(GrBudgetedType::kBudgeted != resource->resourcePriv().budgetedType() ||
Greg Danielda642612021-02-09 18:04:02 -0500883 uniqueKey.isValid() || resource->cacheAccess().hasRef());
kkinnunen2e6055b2016-04-22 01:48:29 -0700884 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
Greg Danielda642612021-02-09 18:04:02 -0500885 SkASSERT(!fScratchMap->has(resource, scratchKey));
bsalomonf320e042015-02-17 15:09:34 -0800886 }
bsalomon8718aaf2015-02-19 07:24:21 -0800887 if (uniqueKey.isValid()) {
bsalomonf320e042015-02-17 15:09:34 -0800888 ++fContent;
bsalomon8718aaf2015-02-19 07:24:21 -0800889 SkASSERT(fUniqueHash->find(uniqueKey) == resource);
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500890 SkASSERT(GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType() ||
Brian Osman0562eb92017-05-08 11:16:39 -0400891 resource->resourcePriv().refsWrappedObjects());
bsalomonf320e042015-02-17 15:09:34 -0800892 }
893
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500894 if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
bsalomonf320e042015-02-17 15:09:34 -0800895 ++fBudgetedCount;
896 fBudgetedBytes += resource->gpuMemorySize();
897 }
bsalomon9f2d1572015-02-17 11:47:40 -0800898 }
bsalomonf320e042015-02-17 15:09:34 -0800899 };
900
robertphillipsc4ed6842016-05-24 14:17:12 -0700901 {
robertphillipsc4ed6842016-05-24 14:17:12 -0700902 int count = 0;
Mike Kleincff63962020-03-14 16:22:45 -0500903 fScratchMap.foreach([&](const GrGpuResource& resource) {
Greg Danielda642612021-02-09 18:04:02 -0500904 SkASSERT(resource.cacheAccess().isUsableAsScratch());
robertphillipsc4ed6842016-05-24 14:17:12 -0700905 count++;
Mike Kleincff63962020-03-14 16:22:45 -0500906 });
907 SkASSERT(count == fScratchMap.count());
robertphillipsc4ed6842016-05-24 14:17:12 -0700908 }
909
bsalomonf320e042015-02-17 15:09:34 -0800910 Stats stats(this);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400911 size_t purgeableBytes = 0;
Brian Salomon2c791fc2019-04-02 11:52:03 -0400912 int numBudgetedResourcesFlushWillMakePurgeable = 0;
bsalomonf320e042015-02-17 15:09:34 -0800913
914 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500915 SkASSERT(!fNonpurgeableResources[i]->resourcePriv().isPurgeable() ||
bsalomon3f324322015-04-08 11:01:54 -0700916 fNewlyPurgeableResourceForValidation == fNonpurgeableResources[i]);
bsalomonf320e042015-02-17 15:09:34 -0800917 SkASSERT(*fNonpurgeableResources[i]->cacheAccess().accessCacheIndex() == i);
918 SkASSERT(!fNonpurgeableResources[i]->wasDestroyed());
Brian Salomon2c791fc2019-04-02 11:52:03 -0400919 if (fNonpurgeableResources[i]->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted &&
Greg Daniel1fd8ac82020-12-11 11:22:01 -0500920 !fNonpurgeableResources[i]->cacheAccess().hasRefOrCommandBufferUsage() &&
Brian Salomon2c791fc2019-04-02 11:52:03 -0400921 fNewlyPurgeableResourceForValidation != fNonpurgeableResources[i]) {
Brian Salomon2c791fc2019-04-02 11:52:03 -0400922 ++numBudgetedResourcesFlushWillMakePurgeable;
923 }
bsalomonf320e042015-02-17 15:09:34 -0800924 stats.update(fNonpurgeableResources[i]);
bsalomon71cb0c22014-11-14 12:10:14 -0800925 }
bsalomon9f2d1572015-02-17 11:47:40 -0800926 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500927 SkASSERT(fPurgeableQueue.at(i)->resourcePriv().isPurgeable());
bsalomonf320e042015-02-17 15:09:34 -0800928 SkASSERT(*fPurgeableQueue.at(i)->cacheAccess().accessCacheIndex() == i);
929 SkASSERT(!fPurgeableQueue.at(i)->wasDestroyed());
930 stats.update(fPurgeableQueue.at(i));
Derek Sollenbergeree479142017-05-24 11:41:33 -0400931 purgeableBytes += fPurgeableQueue.at(i)->gpuMemorySize();
bsalomon9f2d1572015-02-17 11:47:40 -0800932 }
933
bsalomonf320e042015-02-17 15:09:34 -0800934 SkASSERT(fCount == this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800935 SkASSERT(fBudgetedCount <= fCount);
bsalomonf320e042015-02-17 15:09:34 -0800936 SkASSERT(fBudgetedBytes <= fBytes);
937 SkASSERT(stats.fBytes == fBytes);
Brian Salomon2c791fc2019-04-02 11:52:03 -0400938 SkASSERT(fNumBudgetedResourcesFlushWillMakePurgeable ==
939 numBudgetedResourcesFlushWillMakePurgeable);
bsalomonf320e042015-02-17 15:09:34 -0800940 SkASSERT(stats.fBudgetedBytes == fBudgetedBytes);
941 SkASSERT(stats.fBudgetedCount == fBudgetedCount);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400942 SkASSERT(purgeableBytes == fPurgeableBytes);
bsalomon71cb0c22014-11-14 12:10:14 -0800943#if GR_CACHE_STATS
bsalomondace19e2014-11-17 07:34:06 -0800944 SkASSERT(fBudgetedHighWaterCount <= fHighWaterCount);
945 SkASSERT(fBudgetedHighWaterBytes <= fHighWaterBytes);
bsalomonf320e042015-02-17 15:09:34 -0800946 SkASSERT(fBytes <= fHighWaterBytes);
947 SkASSERT(fCount <= fHighWaterCount);
948 SkASSERT(fBudgetedBytes <= fBudgetedHighWaterBytes);
949 SkASSERT(fBudgetedCount <= fBudgetedHighWaterCount);
bsalomon71cb0c22014-11-14 12:10:14 -0800950#endif
bsalomon8718aaf2015-02-19 07:24:21 -0800951 SkASSERT(stats.fContent == fUniqueHash.count());
Greg Danielda642612021-02-09 18:04:02 -0500952 SkASSERT(stats.fScratch == fScratchMap.count());
bsalomon71cb0c22014-11-14 12:10:14 -0800953
bsalomon3f324322015-04-08 11:01:54 -0700954 // This assertion is not currently valid because we can be in recursive notifyCntReachedZero()
bsalomon12299ab2014-11-14 13:33:09 -0800955 // calls. This will be fixed when subresource registration is explicit.
bsalomondace19e2014-11-17 07:34:06 -0800956 // bool overBudget = budgetedBytes > fMaxBytes || budgetedCount > fMaxCount;
bsalomon12299ab2014-11-14 13:33:09 -0800957 // SkASSERT(!overBudget || locked == count || fPurging);
bsalomon71cb0c22014-11-14 12:10:14 -0800958}
bsalomonf320e042015-02-17 15:09:34 -0800959
960bool GrResourceCache::isInCache(const GrGpuResource* resource) const {
961 int index = *resource->cacheAccess().accessCacheIndex();
962 if (index < 0) {
963 return false;
964 }
965 if (index < fPurgeableQueue.count() && fPurgeableQueue.at(index) == resource) {
966 return true;
967 }
968 if (index < fNonpurgeableResources.count() && fNonpurgeableResources[index] == resource) {
969 return true;
970 }
971 SkDEBUGFAIL("Resource index should be -1 or the resource should be in the cache.");
972 return false;
973}
974
bsalomon71cb0c22014-11-14 12:10:14 -0800975#endif