blob: 94211f26829aaafb7175f5a7543b53f8296a1c72 [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);
625 if (this->wouldFit(desiredHeadroomBytes)) {
626 return true;
627 }
628 fPurgeableQueue.sort();
629
630 size_t headroom = this->overBudget() ? 0 : fMaxBytes - fBudgetedBytes;
631 int purgeCnt = 0;
632 for (int i = 0; i < fPurgeableQueue.count(); i++) {
633 GrGpuResource* resource = fPurgeableQueue.at(i);
634 headroom += resource->gpuMemorySize();
635 if (headroom >= desiredHeadroomBytes) {
636 purgeCnt = i + 1;
637 break;
638 }
639 }
640 if (headroom < desiredHeadroomBytes) {
641 return false;
642 }
643
644 // Success! Release the resources.
645 // Copy to array first so we don't mess with the queue.
646 std::vector<GrGpuResource*> resources;
647 resources.reserve(purgeCnt);
648 for (int i = 0; i < purgeCnt; i++) {
649 resources.push_back(fPurgeableQueue.at(i));
650 }
651 for (GrGpuResource* resource : resources) {
652 resource->cacheAccess().release();
653 }
654 return true;
655}
656
Derek Sollenberger5480a182017-05-25 16:43:59 -0400657void GrResourceCache::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
658
Brian Osman788b9162020-02-07 10:36:46 -0500659 const size_t tmpByteBudget = std::max((size_t)0, fBytes - bytesToPurge);
Derek Sollenberger5480a182017-05-25 16:43:59 -0400660 bool stillOverbudget = tmpByteBudget < fBytes;
661
662 if (preferScratchResources && bytesToPurge < fPurgeableBytes) {
663 // Sort the queue
664 fPurgeableQueue.sort();
665
666 // Make a list of the scratch resources to delete
667 SkTDArray<GrGpuResource*> scratchResources;
668 size_t scratchByteCount = 0;
669 for (int i = 0; i < fPurgeableQueue.count() && stillOverbudget; i++) {
670 GrGpuResource* resource = fPurgeableQueue.at(i);
Brian Salomon614c1a82018-12-19 15:42:06 -0500671 SkASSERT(resource->resourcePriv().isPurgeable());
Derek Sollenberger5480a182017-05-25 16:43:59 -0400672 if (!resource->getUniqueKey().isValid()) {
673 *scratchResources.append() = resource;
674 scratchByteCount += resource->gpuMemorySize();
675 stillOverbudget = tmpByteBudget < fBytes - scratchByteCount;
676 }
677 }
678
679 // Delete the scratch resources. This must be done as a separate pass
680 // to avoid messing up the sorted order of the queue
681 for (int i = 0; i < scratchResources.count(); i++) {
682 scratchResources.getAt(i)->cacheAccess().release();
683 }
684 stillOverbudget = tmpByteBudget < fBytes;
685
686 this->validate();
687 }
688
689 // Purge any remaining resources in LRU order
690 if (stillOverbudget) {
691 const size_t cachedByteCount = fMaxBytes;
692 fMaxBytes = tmpByteBudget;
693 this->purgeAsNeeded();
694 fMaxBytes = cachedByteCount;
695 }
696}
Robert Phillips12d06a32020-09-16 12:31:34 -0400697
Brian Salomon8cefa3e2019-04-04 11:39:55 -0400698bool GrResourceCache::requestsFlush() const {
699 return this->overBudget() && !fPurgeableQueue.count() &&
700 fNumBudgetedResourcesFlushWillMakePurgeable > 0;
701}
702
Robert Phillipsddc21482019-10-16 14:30:09 -0400703void GrResourceCache::insertDelayedTextureUnref(GrTexture* texture) {
704 texture->ref();
705 uint32_t id = texture->uniqueID().asUInt();
706 if (auto* data = fTexturesAwaitingUnref.find(id)) {
Brian Salomon876a0172019-03-08 11:12:14 -0500707 data->addRef();
708 } else {
Robert Phillipsddc21482019-10-16 14:30:09 -0400709 fTexturesAwaitingUnref.set(id, {texture});
Brian Salomon876a0172019-03-08 11:12:14 -0500710 }
Brian Osman13dddce2017-05-09 13:19:50 -0400711}
712
713void GrResourceCache::processFreedGpuResources() {
Robert Phillips1dfc77c2019-10-16 16:39:45 -0400714 if (!fTexturesAwaitingUnref.count()) {
Robert Phillips12d06a32020-09-16 12:31:34 -0400715 return;
Robert Phillips1dfc77c2019-10-16 16:39:45 -0400716 }
717
Robert Phillipsddc21482019-10-16 14:30:09 -0400718 SkTArray<GrTextureFreedMessage> msgs;
719 fFreedTextureInbox.poll(&msgs);
Brian Osman13dddce2017-05-09 13:19:50 -0400720 for (int i = 0; i < msgs.count(); ++i) {
Robert Phillipsd074b622021-03-15 08:49:24 -0400721 SkASSERT(msgs[i].fIntendedRecipient == fOwningContextID);
Robert Phillipsddc21482019-10-16 14:30:09 -0400722 uint32_t id = msgs[i].fTexture->uniqueID().asUInt();
723 TextureAwaitingUnref* info = fTexturesAwaitingUnref.find(id);
Greg Daniel1a5d2d52019-12-04 11:14:29 -0500724 // If the GrContext was released or abandoned then fTexturesAwaitingUnref should have been
725 // empty and we would have returned early above. Thus, any texture from a message should be
726 // in the list of fTexturesAwaitingUnref.
727 SkASSERT(info);
728 info->unref();
729 if (info->finished()) {
730 fTexturesAwaitingUnref.remove(id);
Greg Danielb2acf0a2018-09-12 09:17:11 -0400731 }
Brian Osman13dddce2017-05-09 13:19:50 -0400732 }
733}
734
bsalomonf320e042015-02-17 15:09:34 -0800735void GrResourceCache::addToNonpurgeableArray(GrGpuResource* resource) {
736 int index = fNonpurgeableResources.count();
737 *fNonpurgeableResources.append() = resource;
738 *resource->cacheAccess().accessCacheIndex() = index;
739}
740
741void GrResourceCache::removeFromNonpurgeableArray(GrGpuResource* resource) {
742 int* index = resource->cacheAccess().accessCacheIndex();
Adlai Holler9555f292020-10-09 09:41:14 -0400743 // Fill the hole we will create in the array with the tail object, adjust its index, and
bsalomonf320e042015-02-17 15:09:34 -0800744 // then pop the array
745 GrGpuResource* tail = *(fNonpurgeableResources.end() - 1);
746 SkASSERT(fNonpurgeableResources[*index] == resource);
747 fNonpurgeableResources[*index] = tail;
748 *tail->cacheAccess().accessCacheIndex() = *index;
749 fNonpurgeableResources.pop();
750 SkDEBUGCODE(*index = -1);
751}
752
bsalomonddf30e62015-02-19 11:38:44 -0800753uint32_t GrResourceCache::getNextTimestamp() {
754 // If we wrap then all the existing resources will appear older than any resources that get
755 // a timestamp after the wrap.
756 if (0 == fTimestamp) {
757 int count = this->getResourceCount();
758 if (count) {
759 // Reset all the timestamps. We sort the resources by timestamp and then assign
760 // sequential timestamps beginning with 0. This is O(n*lg(n)) but it should be extremely
761 // rare.
762 SkTDArray<GrGpuResource*> sortedPurgeableResources;
763 sortedPurgeableResources.setReserve(fPurgeableQueue.count());
764
765 while (fPurgeableQueue.count()) {
766 *sortedPurgeableResources.append() = fPurgeableQueue.peek();
767 fPurgeableQueue.pop();
768 }
robertphillipsee843b22016-10-04 05:30:20 -0700769
John Stiles886a9042020-07-14 16:28:33 -0400770 SkTQSort(fNonpurgeableResources.begin(), fNonpurgeableResources.end(),
John Stiles6e9ead92020-07-14 00:13:51 +0000771 CompareTimestamp);
bsalomonddf30e62015-02-19 11:38:44 -0800772
773 // Pick resources out of the purgeable and non-purgeable arrays based on lowest
774 // timestamp and assign new timestamps.
775 int currP = 0;
776 int currNP = 0;
777 while (currP < sortedPurgeableResources.count() &&
mtklein56da0252015-11-16 11:16:23 -0800778 currNP < fNonpurgeableResources.count()) {
bsalomonddf30e62015-02-19 11:38:44 -0800779 uint32_t tsP = sortedPurgeableResources[currP]->cacheAccess().timestamp();
780 uint32_t tsNP = fNonpurgeableResources[currNP]->cacheAccess().timestamp();
781 SkASSERT(tsP != tsNP);
782 if (tsP < tsNP) {
783 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
784 } else {
785 // Correct the index in the nonpurgeable array stored on the resource post-sort.
786 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
787 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
788 }
789 }
790
791 // The above loop ended when we hit the end of one array. Finish the other one.
792 while (currP < sortedPurgeableResources.count()) {
793 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
794 }
795 while (currNP < fNonpurgeableResources.count()) {
796 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
797 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
798 }
799
800 // Rebuild the queue.
801 for (int i = 0; i < sortedPurgeableResources.count(); ++i) {
802 fPurgeableQueue.insert(sortedPurgeableResources[i]);
803 }
804
805 this->validate();
806 SkASSERT(count == this->getResourceCount());
807
808 // count should be the next timestamp we return.
809 SkASSERT(fTimestamp == SkToU32(count));
mtklein56da0252015-11-16 11:16:23 -0800810 }
bsalomonddf30e62015-02-19 11:38:44 -0800811 }
812 return fTimestamp++;
813}
814
ericrk0a5fa482015-09-15 14:16:10 -0700815void GrResourceCache::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
816 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
817 fNonpurgeableResources[i]->dumpMemoryStatistics(traceMemoryDump);
818 }
819 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
820 fPurgeableQueue.at(i)->dumpMemoryStatistics(traceMemoryDump);
821 }
822}
823
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500824#if GR_CACHE_STATS
825void GrResourceCache::getStats(Stats* stats) const {
826 stats->reset();
827
828 stats->fTotal = this->getResourceCount();
829 stats->fNumNonPurgeable = fNonpurgeableResources.count();
830 stats->fNumPurgeable = fPurgeableQueue.count();
831
832 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
833 stats->update(fNonpurgeableResources[i]);
834 }
835 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
836 stats->update(fPurgeableQueue.at(i));
837 }
838}
839
840#if GR_TEST_UTILS
841void GrResourceCache::dumpStats(SkString* out) const {
842 this->validate();
843
844 Stats stats;
845
846 this->getStats(&stats);
847
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500848 float byteUtilization = (100.f * fBudgetedBytes) / fMaxBytes;
849
Robert Phillipscf39f372019-09-03 10:29:20 -0400850 out->appendf("Budget: %d bytes\n", (int)fMaxBytes);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500851 out->appendf("\t\tEntry Count: current %d"
Robert Phillipscf39f372019-09-03 10:29:20 -0400852 " (%d budgeted, %d wrapped, %d locked, %d scratch), high %d\n",
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500853 stats.fTotal, fBudgetedCount, stats.fWrapped, stats.fNumNonPurgeable,
Robert Phillipscf39f372019-09-03 10:29:20 -0400854 stats.fScratch, fHighWaterCount);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500855 out->appendf("\t\tEntry Bytes: current %d (budgeted %d, %.2g%% full, %d unbudgeted) high %d\n",
856 SkToInt(fBytes), SkToInt(fBudgetedBytes), byteUtilization,
857 SkToInt(stats.fUnbudgetedSize), SkToInt(fHighWaterBytes));
858}
859
860void GrResourceCache::dumpStatsKeyValuePairs(SkTArray<SkString>* keys,
861 SkTArray<double>* values) const {
862 this->validate();
863
864 Stats stats;
865 this->getStats(&stats);
866
867 keys->push_back(SkString("gpu_cache_purgable_entries")); values->push_back(stats.fNumPurgeable);
868}
869#endif
870
871#endif
872
bsalomon71cb0c22014-11-14 12:10:14 -0800873#ifdef SK_DEBUG
bsalomon0ea80f42015-02-11 10:49:59 -0800874void GrResourceCache::validate() const {
bsalomonc2f35b72015-01-23 07:19:22 -0800875 // Reduce the frequency of validations for large resource counts.
876 static SkRandom gRandom;
877 int mask = (SkNextPow2(fCount + 1) >> 5) - 1;
878 if (~mask && (gRandom.nextU() & mask)) {
879 return;
880 }
881
bsalomonf320e042015-02-17 15:09:34 -0800882 struct Stats {
883 size_t fBytes;
884 int fBudgetedCount;
885 size_t fBudgetedBytes;
886 int fLocked;
887 int fScratch;
888 int fCouldBeScratch;
889 int fContent;
890 const ScratchMap* fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800891 const UniqueHash* fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800892
bsalomonf320e042015-02-17 15:09:34 -0800893 Stats(const GrResourceCache* cache) {
894 memset(this, 0, sizeof(*this));
895 fScratchMap = &cache->fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800896 fUniqueHash = &cache->fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800897 }
898
bsalomonf320e042015-02-17 15:09:34 -0800899 void update(GrGpuResource* resource) {
900 fBytes += resource->gpuMemorySize();
bsalomondace19e2014-11-17 07:34:06 -0800901
Brian Salomon614c1a82018-12-19 15:42:06 -0500902 if (!resource->resourcePriv().isPurgeable()) {
bsalomonf320e042015-02-17 15:09:34 -0800903 ++fLocked;
904 }
bsalomon9f2d1572015-02-17 11:47:40 -0800905
robertphillipsc4ed6842016-05-24 14:17:12 -0700906 const GrScratchKey& scratchKey = resource->resourcePriv().getScratchKey();
907 const GrUniqueKey& uniqueKey = resource->getUniqueKey();
908
Greg Danielda642612021-02-09 18:04:02 -0500909 if (resource->cacheAccess().isUsableAsScratch()) {
robertphillipsc4ed6842016-05-24 14:17:12 -0700910 SkASSERT(!uniqueKey.isValid());
Greg Danielda642612021-02-09 18:04:02 -0500911 SkASSERT(GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType());
912 SkASSERT(!resource->cacheAccess().hasRef());
bsalomonf320e042015-02-17 15:09:34 -0800913 ++fScratch;
robertphillipsc4ed6842016-05-24 14:17:12 -0700914 SkASSERT(fScratchMap->countForKey(scratchKey));
kkinnunen2e6055b2016-04-22 01:48:29 -0700915 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
robertphillipsc4ed6842016-05-24 14:17:12 -0700916 } else if (scratchKey.isValid()) {
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500917 SkASSERT(GrBudgetedType::kBudgeted != resource->resourcePriv().budgetedType() ||
Greg Danielda642612021-02-09 18:04:02 -0500918 uniqueKey.isValid() || resource->cacheAccess().hasRef());
kkinnunen2e6055b2016-04-22 01:48:29 -0700919 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
Greg Danielda642612021-02-09 18:04:02 -0500920 SkASSERT(!fScratchMap->has(resource, scratchKey));
bsalomonf320e042015-02-17 15:09:34 -0800921 }
bsalomon8718aaf2015-02-19 07:24:21 -0800922 if (uniqueKey.isValid()) {
bsalomonf320e042015-02-17 15:09:34 -0800923 ++fContent;
bsalomon8718aaf2015-02-19 07:24:21 -0800924 SkASSERT(fUniqueHash->find(uniqueKey) == resource);
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500925 SkASSERT(GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType() ||
Brian Osman0562eb92017-05-08 11:16:39 -0400926 resource->resourcePriv().refsWrappedObjects());
bsalomonf320e042015-02-17 15:09:34 -0800927 }
928
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500929 if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
bsalomonf320e042015-02-17 15:09:34 -0800930 ++fBudgetedCount;
931 fBudgetedBytes += resource->gpuMemorySize();
932 }
bsalomon9f2d1572015-02-17 11:47:40 -0800933 }
bsalomonf320e042015-02-17 15:09:34 -0800934 };
935
robertphillipsc4ed6842016-05-24 14:17:12 -0700936 {
robertphillipsc4ed6842016-05-24 14:17:12 -0700937 int count = 0;
Mike Kleincff63962020-03-14 16:22:45 -0500938 fScratchMap.foreach([&](const GrGpuResource& resource) {
Greg Danielda642612021-02-09 18:04:02 -0500939 SkASSERT(resource.cacheAccess().isUsableAsScratch());
robertphillipsc4ed6842016-05-24 14:17:12 -0700940 count++;
Mike Kleincff63962020-03-14 16:22:45 -0500941 });
942 SkASSERT(count == fScratchMap.count());
robertphillipsc4ed6842016-05-24 14:17:12 -0700943 }
944
bsalomonf320e042015-02-17 15:09:34 -0800945 Stats stats(this);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400946 size_t purgeableBytes = 0;
Brian Salomon2c791fc2019-04-02 11:52:03 -0400947 int numBudgetedResourcesFlushWillMakePurgeable = 0;
bsalomonf320e042015-02-17 15:09:34 -0800948
949 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500950 SkASSERT(!fNonpurgeableResources[i]->resourcePriv().isPurgeable() ||
bsalomon3f324322015-04-08 11:01:54 -0700951 fNewlyPurgeableResourceForValidation == fNonpurgeableResources[i]);
bsalomonf320e042015-02-17 15:09:34 -0800952 SkASSERT(*fNonpurgeableResources[i]->cacheAccess().accessCacheIndex() == i);
953 SkASSERT(!fNonpurgeableResources[i]->wasDestroyed());
Brian Salomon2c791fc2019-04-02 11:52:03 -0400954 if (fNonpurgeableResources[i]->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted &&
Greg Daniel1fd8ac82020-12-11 11:22:01 -0500955 !fNonpurgeableResources[i]->cacheAccess().hasRefOrCommandBufferUsage() &&
Brian Salomon2c791fc2019-04-02 11:52:03 -0400956 fNewlyPurgeableResourceForValidation != fNonpurgeableResources[i]) {
Brian Salomon2c791fc2019-04-02 11:52:03 -0400957 ++numBudgetedResourcesFlushWillMakePurgeable;
958 }
bsalomonf320e042015-02-17 15:09:34 -0800959 stats.update(fNonpurgeableResources[i]);
bsalomon71cb0c22014-11-14 12:10:14 -0800960 }
bsalomon9f2d1572015-02-17 11:47:40 -0800961 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500962 SkASSERT(fPurgeableQueue.at(i)->resourcePriv().isPurgeable());
bsalomonf320e042015-02-17 15:09:34 -0800963 SkASSERT(*fPurgeableQueue.at(i)->cacheAccess().accessCacheIndex() == i);
964 SkASSERT(!fPurgeableQueue.at(i)->wasDestroyed());
965 stats.update(fPurgeableQueue.at(i));
Derek Sollenbergeree479142017-05-24 11:41:33 -0400966 purgeableBytes += fPurgeableQueue.at(i)->gpuMemorySize();
bsalomon9f2d1572015-02-17 11:47:40 -0800967 }
968
bsalomonf320e042015-02-17 15:09:34 -0800969 SkASSERT(fCount == this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800970 SkASSERT(fBudgetedCount <= fCount);
bsalomonf320e042015-02-17 15:09:34 -0800971 SkASSERT(fBudgetedBytes <= fBytes);
972 SkASSERT(stats.fBytes == fBytes);
Brian Salomon2c791fc2019-04-02 11:52:03 -0400973 SkASSERT(fNumBudgetedResourcesFlushWillMakePurgeable ==
974 numBudgetedResourcesFlushWillMakePurgeable);
bsalomonf320e042015-02-17 15:09:34 -0800975 SkASSERT(stats.fBudgetedBytes == fBudgetedBytes);
976 SkASSERT(stats.fBudgetedCount == fBudgetedCount);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400977 SkASSERT(purgeableBytes == fPurgeableBytes);
bsalomon71cb0c22014-11-14 12:10:14 -0800978#if GR_CACHE_STATS
bsalomondace19e2014-11-17 07:34:06 -0800979 SkASSERT(fBudgetedHighWaterCount <= fHighWaterCount);
980 SkASSERT(fBudgetedHighWaterBytes <= fHighWaterBytes);
bsalomonf320e042015-02-17 15:09:34 -0800981 SkASSERT(fBytes <= fHighWaterBytes);
982 SkASSERT(fCount <= fHighWaterCount);
983 SkASSERT(fBudgetedBytes <= fBudgetedHighWaterBytes);
984 SkASSERT(fBudgetedCount <= fBudgetedHighWaterCount);
bsalomon71cb0c22014-11-14 12:10:14 -0800985#endif
bsalomon8718aaf2015-02-19 07:24:21 -0800986 SkASSERT(stats.fContent == fUniqueHash.count());
Greg Danielda642612021-02-09 18:04:02 -0500987 SkASSERT(stats.fScratch == fScratchMap.count());
bsalomon71cb0c22014-11-14 12:10:14 -0800988
bsalomon3f324322015-04-08 11:01:54 -0700989 // This assertion is not currently valid because we can be in recursive notifyCntReachedZero()
bsalomon12299ab2014-11-14 13:33:09 -0800990 // calls. This will be fixed when subresource registration is explicit.
bsalomondace19e2014-11-17 07:34:06 -0800991 // bool overBudget = budgetedBytes > fMaxBytes || budgetedCount > fMaxCount;
bsalomon12299ab2014-11-14 13:33:09 -0800992 // SkASSERT(!overBudget || locked == count || fPurging);
bsalomon71cb0c22014-11-14 12:10:14 -0800993}
bsalomonf320e042015-02-17 15:09:34 -0800994
995bool GrResourceCache::isInCache(const GrGpuResource* resource) const {
996 int index = *resource->cacheAccess().accessCacheIndex();
997 if (index < 0) {
998 return false;
999 }
1000 if (index < fPurgeableQueue.count() && fPurgeableQueue.at(index) == resource) {
1001 return true;
1002 }
1003 if (index < fNonpurgeableResources.count() && fNonpurgeableResources[index] == resource) {
1004 return true;
1005 }
1006 SkDEBUGFAIL("Resource index should be -1 or the resource should be in the cache.");
1007 return false;
1008}
1009
bsalomon71cb0c22014-11-14 12:10:14 -08001010#endif