blob: c1748a3173e3eefb275f9128de59d40d1ff8a5f8 [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
bsalomon8718aaf2015-02-19 07:24:21 -080028DECLARE_SKMESSAGEBUS_MESSAGE(GrUniqueKeyInvalidatedMessage);
bsalomon71cb0c22014-11-14 12:10:14 -080029
Robert Phillipsddc21482019-10-16 14:30:09 -040030DECLARE_SKMESSAGEBUS_MESSAGE(GrTextureFreedMessage);
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
Brian Salomonbe1084b2021-01-26 13:29:30 -0500111GrResourceCache::GrResourceCache(GrSingleOwner* singleOwner, uint32_t contextUniqueID)
Brian Salomon2c791fc2019-04-02 11:52:03 -0400112 : fInvalidUniqueKeyInbox(contextUniqueID)
Robert Phillipsddc21482019-10-16 14:30:09 -0400113 , fFreedTextureInbox(contextUniqueID)
Brian Salomon238069b2018-07-11 15:58:57 -0400114 , fContextUniqueID(contextUniqueID)
Brian Salomonbe1084b2021-01-26 13:29:30 -0500115 , fSingleOwner(singleOwner) {
Brian Salomon238069b2018-07-11 15:58:57 -0400116 SkASSERT(contextUniqueID != SK_InvalidUniqueID);
bsalomon71cb0c22014-11-14 12:10:14 -0800117}
118
bsalomon0ea80f42015-02-11 10:49:59 -0800119GrResourceCache::~GrResourceCache() {
bsalomonc8dc1f72014-08-21 13:02:13 -0700120 this->releaseAll();
121}
122
Robert Phillipscf39f372019-09-03 10:29:20 -0400123void GrResourceCache::setLimit(size_t bytes) {
bsalomon71cb0c22014-11-14 12:10:14 -0800124 fMaxBytes = bytes;
125 this->purgeAsNeeded();
126}
127
bsalomon0ea80f42015-02-11 10:49:59 -0800128void GrResourceCache::insertResource(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400129 ASSERT_SINGLE_OWNER
bsalomon49f085d2014-09-05 13:34:00 -0700130 SkASSERT(resource);
bsalomon16961262014-08-26 14:01:07 -0700131 SkASSERT(!this->isInCache(resource));
bsalomonf320e042015-02-17 15:09:34 -0800132 SkASSERT(!resource->wasDestroyed());
Brian Salomon614c1a82018-12-19 15:42:06 -0500133 SkASSERT(!resource->resourcePriv().isPurgeable());
bsalomonddf30e62015-02-19 11:38:44 -0800134
135 // We must set the timestamp before adding to the array in case the timestamp wraps and we wind
136 // up iterating over all the resources that already have timestamps.
137 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
138
bsalomonf320e042015-02-17 15:09:34 -0800139 this->addToNonpurgeableArray(resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800140
bsalomondace19e2014-11-17 07:34:06 -0800141 size_t size = resource->gpuMemorySize();
bsalomonf320e042015-02-17 15:09:34 -0800142 SkDEBUGCODE(++fCount;)
bsalomon84c8e622014-11-17 09:33:27 -0800143 fBytes += size;
bsalomon82b1d622014-11-14 13:59:57 -0800144#if GR_CACHE_STATS
Brian Osman788b9162020-02-07 10:36:46 -0500145 fHighWaterCount = std::max(this->getResourceCount(), fHighWaterCount);
146 fHighWaterBytes = std::max(fBytes, fHighWaterBytes);
bsalomon82b1d622014-11-14 13:59:57 -0800147#endif
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500148 if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
bsalomondace19e2014-11-17 07:34:06 -0800149 ++fBudgetedCount;
150 fBudgetedBytes += size;
Brian Osman39c08ac2017-07-26 09:36:09 -0400151 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800152 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomondace19e2014-11-17 07:34:06 -0800153#if GR_CACHE_STATS
Brian Osman788b9162020-02-07 10:36:46 -0500154 fBudgetedHighWaterCount = std::max(fBudgetedCount, fBudgetedHighWaterCount);
155 fBudgetedHighWaterBytes = std::max(fBudgetedBytes, fBudgetedHighWaterBytes);
bsalomondace19e2014-11-17 07:34:06 -0800156#endif
157 }
robertphillipsc4ed6842016-05-24 14:17:12 -0700158 if (resource->resourcePriv().getScratchKey().isValid() &&
159 !resource->getUniqueKey().isValid()) {
kkinnunen2e6055b2016-04-22 01:48:29 -0700160 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
bsalomon3582d3e2015-02-13 14:20:05 -0800161 fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
bsalomon744998e2014-08-28 09:54:34 -0700162 }
bsalomon9f2d1572015-02-17 11:47:40 -0800163
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
robertphillipsc4ed6842016-05-24 14:17:12 -0700189 if (resource->resourcePriv().getScratchKey().isValid() &&
190 !resource->getUniqueKey().isValid()) {
bsalomon3582d3e2015-02-13 14:20:05 -0800191 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
bsalomon744998e2014-08-28 09:54:34 -0700192 }
bsalomon8718aaf2015-02-19 07:24:21 -0800193 if (resource->getUniqueKey().isValid()) {
194 fUniqueHash.remove(resource->getUniqueKey());
bsalomon8b79d232014-11-10 10:19:06 -0800195 }
bsalomonb436ed62014-11-17 12:15:56 -0800196 this->validate();
bsalomonc8dc1f72014-08-21 13:02:13 -0700197}
198
bsalomon0ea80f42015-02-11 10:49:59 -0800199void GrResourceCache::abandonAll() {
bsalomon71cb0c22014-11-14 12:10:14 -0800200 AutoValidate av(this);
201
Brian Salomon876a0172019-03-08 11:12:14 -0500202 // We need to make sure to free any resources that were waiting on a free message but never
203 // received one.
Robert Phillipsddc21482019-10-16 14:30:09 -0400204 fTexturesAwaitingUnref.reset();
Greg Danielb2acf0a2018-09-12 09:17:11 -0400205
bsalomonf320e042015-02-17 15:09:34 -0800206 while (fNonpurgeableResources.count()) {
207 GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
208 SkASSERT(!back->wasDestroyed());
209 back->cacheAccess().abandon();
bsalomonc8dc1f72014-08-21 13:02:13 -0700210 }
bsalomonf320e042015-02-17 15:09:34 -0800211
212 while (fPurgeableQueue.count()) {
213 GrGpuResource* top = fPurgeableQueue.peek();
214 SkASSERT(!top->wasDestroyed());
215 top->cacheAccess().abandon();
216 }
217
Robert Phillipseb999bc2020-11-03 08:41:47 -0500218 fThreadSafeCache->dropAllRefs();
219
bsalomon744998e2014-08-28 09:54:34 -0700220 SkASSERT(!fScratchMap.count());
bsalomon8718aaf2015-02-19 07:24:21 -0800221 SkASSERT(!fUniqueHash.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700222 SkASSERT(!fCount);
bsalomonf320e042015-02-17 15:09:34 -0800223 SkASSERT(!this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800224 SkASSERT(!fBytes);
225 SkASSERT(!fBudgetedCount);
226 SkASSERT(!fBudgetedBytes);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400227 SkASSERT(!fPurgeableBytes);
Robert Phillipsddc21482019-10-16 14:30:09 -0400228 SkASSERT(!fTexturesAwaitingUnref.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700229}
230
bsalomon0ea80f42015-02-11 10:49:59 -0800231void GrResourceCache::releaseAll() {
bsalomon71cb0c22014-11-14 12:10:14 -0800232 AutoValidate av(this);
233
Robert Phillipsd464feb2020-10-08 11:00:02 -0400234 fThreadSafeCache->dropAllRefs();
Robert Phillips12d06a32020-09-16 12:31:34 -0400235
Brian Osman13dddce2017-05-09 13:19:50 -0400236 this->processFreedGpuResources();
237
Greg Danielc27eb722018-08-10 09:48:08 -0400238 // We need to make sure to free any resources that were waiting on a free message but never
239 // received one.
Robert Phillipsddc21482019-10-16 14:30:09 -0400240 fTexturesAwaitingUnref.reset();
Greg Danielc27eb722018-08-10 09:48:08 -0400241
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500242 SkASSERT(fProxyProvider); // better have called setProxyProvider
Robert Phillipsd464feb2020-10-08 11:00:02 -0400243 SkASSERT(fThreadSafeCache); // better have called setThreadSafeCache too
Robert Phillips12d06a32020-09-16 12:31:34 -0400244
Robert Phillips3ec95732017-09-29 15:10:39 -0400245 // We must remove the uniqueKeys from the proxies here. While they possess a uniqueKey
246 // they also have a raw pointer back to this class (which is presumably going away)!
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500247 fProxyProvider->removeAllUniqueKeys();
Robert Phillips45a6f142017-09-29 09:49:41 -0400248
Brian Salomon614c1a82018-12-19 15:42:06 -0500249 while (fNonpurgeableResources.count()) {
bsalomonf320e042015-02-17 15:09:34 -0800250 GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
251 SkASSERT(!back->wasDestroyed());
252 back->cacheAccess().release();
bsalomonc8dc1f72014-08-21 13:02:13 -0700253 }
bsalomonf320e042015-02-17 15:09:34 -0800254
255 while (fPurgeableQueue.count()) {
256 GrGpuResource* top = fPurgeableQueue.peek();
257 SkASSERT(!top->wasDestroyed());
258 top->cacheAccess().release();
259 }
260
bsalomon744998e2014-08-28 09:54:34 -0700261 SkASSERT(!fScratchMap.count());
bsalomon8718aaf2015-02-19 07:24:21 -0800262 SkASSERT(!fUniqueHash.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700263 SkASSERT(!fCount);
bsalomonf320e042015-02-17 15:09:34 -0800264 SkASSERT(!this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800265 SkASSERT(!fBytes);
266 SkASSERT(!fBudgetedCount);
267 SkASSERT(!fBudgetedBytes);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400268 SkASSERT(!fPurgeableBytes);
Robert Phillipsddc21482019-10-16 14:30:09 -0400269 SkASSERT(!fTexturesAwaitingUnref.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700270}
bsalomonbcf0a522014-10-08 08:40:09 -0700271
Brian Salomon2c791fc2019-04-02 11:52:03 -0400272void GrResourceCache::refResource(GrGpuResource* resource) {
273 SkASSERT(resource);
274 SkASSERT(resource->getContext()->priv().getResourceCache() == this);
275 if (resource->cacheAccess().hasRef()) {
276 resource->ref();
277 } else {
278 this->refAndMakeResourceMRU(resource);
279 }
280 this->validate();
281}
282
bsalomon0ea80f42015-02-11 10:49:59 -0800283class GrResourceCache::AvailableForScratchUse {
bsalomonbcf0a522014-10-08 08:40:09 -0700284public:
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400285 AvailableForScratchUse() { }
bsalomonbcf0a522014-10-08 08:40:09 -0700286
287 bool operator()(const GrGpuResource* resource) const {
robertphillipsc4ed6842016-05-24 14:17:12 -0700288 SkASSERT(!resource->getUniqueKey().isValid() &&
289 resource->resourcePriv().getScratchKey().isValid());
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400290
Robert Phillipsaee18c92019-09-06 11:48:27 -0400291 // isScratch() also tests that the resource is budgeted.
bsalomon12299ab2014-11-14 13:33:09 -0800292 if (resource->internalHasRef() || !resource->cacheAccess().isScratch()) {
bsalomon000f8292014-10-15 19:04:14 -0700293 return false;
bsalomonbcf0a522014-10-08 08:40:09 -0700294 }
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400295 return true;
bsalomonbcf0a522014-10-08 08:40:09 -0700296 }
bsalomonbcf0a522014-10-08 08:40:09 -0700297};
298
Robert Phillipsaee18c92019-09-06 11:48:27 -0400299GrGpuResource* GrResourceCache::findAndRefScratchResource(const GrScratchKey& scratchKey) {
bsalomon7775c852014-12-30 12:50:52 -0800300 SkASSERT(scratchKey.isValid());
robertphillipsee843b22016-10-04 05:30:20 -0700301
Robert Phillipsaee18c92019-09-06 11:48:27 -0400302 GrGpuResource* resource = fScratchMap.find(scratchKey, AvailableForScratchUse());
bsalomon71cb0c22014-11-14 12:10:14 -0800303 if (resource) {
bsalomon9f2d1572015-02-17 11:47:40 -0800304 this->refAndMakeResourceMRU(resource);
bsalomonb436ed62014-11-17 12:15:56 -0800305 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800306 }
307 return resource;
bsalomonbcf0a522014-10-08 08:40:09 -0700308}
bsalomon8b79d232014-11-10 10:19:06 -0800309
bsalomon0ea80f42015-02-11 10:49:59 -0800310void GrResourceCache::willRemoveScratchKey(const GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400311 ASSERT_SINGLE_OWNER
bsalomon3582d3e2015-02-13 14:20:05 -0800312 SkASSERT(resource->resourcePriv().getScratchKey().isValid());
robertphillipsc4ed6842016-05-24 14:17:12 -0700313 if (!resource->getUniqueKey().isValid()) {
314 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
315 }
bsalomon10e23ca2014-11-25 05:52:06 -0800316}
317
bsalomonf99e9612015-02-19 08:24:16 -0800318void GrResourceCache::removeUniqueKey(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400319 ASSERT_SINGLE_OWNER
bsalomon3f324322015-04-08 11:01:54 -0700320 // Someone has a ref to this resource in order to have removed the key. When the ref count
321 // reaches zero we will get a ref cnt notification and figure out what to do with it.
bsalomonf99e9612015-02-19 08:24:16 -0800322 if (resource->getUniqueKey().isValid()) {
323 SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
324 fUniqueHash.remove(resource->getUniqueKey());
325 }
326 resource->cacheAccess().removeUniqueKey();
robertphillipsc4ed6842016-05-24 14:17:12 -0700327 if (resource->resourcePriv().getScratchKey().isValid()) {
328 fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
329 }
330
Brian Salomon9bc76d92019-01-24 12:18:33 -0500331 // Removing a unique key from a kUnbudgetedCacheable resource would make the resource
332 // require purging. However, the resource must be ref'ed to get here and therefore can't
333 // be purgeable. We'll purge it when the refs reach zero.
334 SkASSERT(!resource->resourcePriv().isPurgeable());
bsalomonf99e9612015-02-19 08:24:16 -0800335 this->validate();
bsalomon23e619c2015-02-06 11:54:28 -0800336}
337
bsalomonf99e9612015-02-19 08:24:16 -0800338void GrResourceCache::changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400339 ASSERT_SINGLE_OWNER
bsalomon8b79d232014-11-10 10:19:06 -0800340 SkASSERT(resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800341 SkASSERT(this->isInCache(resource));
bsalomon8b79d232014-11-10 10:19:06 -0800342
bsalomonf99e9612015-02-19 08:24:16 -0800343 // If another resource has the new key, remove its key then install the key on this resource.
344 if (newKey.isValid()) {
Greg Daniel0d537802017-09-08 11:44:14 -0400345 if (GrGpuResource* old = fUniqueHash.find(newKey)) {
346 // If the old resource using the key is purgeable and is unreachable, then remove it.
Brian Salomon614c1a82018-12-19 15:42:06 -0500347 if (!old->resourcePriv().getScratchKey().isValid() &&
348 old->resourcePriv().isPurgeable()) {
Greg Daniel0d537802017-09-08 11:44:14 -0400349 old->cacheAccess().release();
350 } else {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500351 // removeUniqueKey expects an external owner of the resource.
352 this->removeUniqueKey(sk_ref_sp(old).get());
Greg Daniel0d537802017-09-08 11:44:14 -0400353 }
354 }
355 SkASSERT(nullptr == fUniqueHash.find(newKey));
356
robertphillipsc4ed6842016-05-24 14:17:12 -0700357 // Remove the entry for this resource if it already has a unique key.
358 if (resource->getUniqueKey().isValid()) {
359 SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
360 fUniqueHash.remove(resource->getUniqueKey());
361 SkASSERT(nullptr == fUniqueHash.find(resource->getUniqueKey()));
362 } else {
363 // 'resource' didn't have a valid unique key before so it is switching sides. Remove it
364 // from the ScratchMap
365 if (resource->resourcePriv().getScratchKey().isValid()) {
366 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
367 }
368 }
369
bsalomonf99e9612015-02-19 08:24:16 -0800370 resource->cacheAccess().setUniqueKey(newKey);
371 fUniqueHash.add(resource);
372 } else {
robertphillipsc4ed6842016-05-24 14:17:12 -0700373 this->removeUniqueKey(resource);
bsalomonf99e9612015-02-19 08:24:16 -0800374 }
375
bsalomon71cb0c22014-11-14 12:10:14 -0800376 this->validate();
bsalomon8b79d232014-11-10 10:19:06 -0800377}
bsalomon71cb0c22014-11-14 12:10:14 -0800378
bsalomon9f2d1572015-02-17 11:47:40 -0800379void GrResourceCache::refAndMakeResourceMRU(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400380 ASSERT_SINGLE_OWNER
bsalomon71cb0c22014-11-14 12:10:14 -0800381 SkASSERT(resource);
382 SkASSERT(this->isInCache(resource));
bsalomonddf30e62015-02-19 11:38:44 -0800383
Brian Salomon614c1a82018-12-19 15:42:06 -0500384 if (resource->resourcePriv().isPurgeable()) {
bsalomon9f2d1572015-02-17 11:47:40 -0800385 // It's about to become unpurgeable.
Derek Sollenbergeree479142017-05-24 11:41:33 -0400386 fPurgeableBytes -= resource->gpuMemorySize();
bsalomon9f2d1572015-02-17 11:47:40 -0800387 fPurgeableQueue.remove(resource);
bsalomonf320e042015-02-17 15:09:34 -0800388 this->addToNonpurgeableArray(resource);
Greg Daniel1fd8ac82020-12-11 11:22:01 -0500389 } else if (!resource->cacheAccess().hasRefOrCommandBufferUsage() &&
Brian Salomon2c791fc2019-04-02 11:52:03 -0400390 resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
391 SkASSERT(fNumBudgetedResourcesFlushWillMakePurgeable > 0);
392 fNumBudgetedResourcesFlushWillMakePurgeable--;
bsalomon9f2d1572015-02-17 11:47:40 -0800393 }
Brian Salomon01ceae92019-04-02 11:49:54 -0400394 resource->cacheAccess().ref();
bsalomonddf30e62015-02-19 11:38:44 -0800395
396 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
bsalomonf320e042015-02-17 15:09:34 -0800397 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800398}
399
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400400void GrResourceCache::notifyRefCntReachedZero(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400401 ASSERT_SINGLE_OWNER
bsalomon71cb0c22014-11-14 12:10:14 -0800402 SkASSERT(resource);
bsalomon3f324322015-04-08 11:01:54 -0700403 SkASSERT(!resource->wasDestroyed());
bsalomon71cb0c22014-11-14 12:10:14 -0800404 SkASSERT(this->isInCache(resource));
bsalomon3f324322015-04-08 11:01:54 -0700405 // This resource should always be in the nonpurgeable array when this function is called. It
406 // will be moved to the queue if it is newly purgeable.
407 SkASSERT(fNonpurgeableResources[*resource->cacheAccess().accessCacheIndex()] == resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800408
bsalomon3f324322015-04-08 11:01:54 -0700409#ifdef SK_DEBUG
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400410 // When the timestamp overflows validate() is called. validate() checks that resources in
411 // the nonpurgeable array are indeed not purgeable. However, the movement from the array to
412 // the purgeable queue happens just below in this function. So we mark it as an exception.
413 if (resource->resourcePriv().isPurgeable()) {
414 fNewlyPurgeableResourceForValidation = resource;
bsalomon3f324322015-04-08 11:01:54 -0700415 }
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400416#endif
417 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
418 SkDEBUGCODE(fNewlyPurgeableResourceForValidation = nullptr);
bsalomon3f324322015-04-08 11:01:54 -0700419
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400420 if (!resource->resourcePriv().isPurgeable() &&
421 resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
422 ++fNumBudgetedResourcesFlushWillMakePurgeable;
bsalomon3f324322015-04-08 11:01:54 -0700423 }
424
Brian Salomon9bc76d92019-01-24 12:18:33 -0500425 if (!resource->resourcePriv().isPurgeable()) {
426 this->validate();
427 return;
428 }
429
bsalomonf320e042015-02-17 15:09:34 -0800430 this->removeFromNonpurgeableArray(resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800431 fPurgeableQueue.insert(resource);
Brian Salomon5e150852017-03-22 14:53:13 -0400432 resource->cacheAccess().setTimeWhenResourceBecomePurgeable();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400433 fPurgeableBytes += resource->gpuMemorySize();
bsalomon71cb0c22014-11-14 12:10:14 -0800434
Greg Daniel303e83e2018-09-10 14:10:19 -0400435 bool hasUniqueKey = resource->getUniqueKey().isValid();
436
Brian Salomon9bc76d92019-01-24 12:18:33 -0500437 GrBudgetedType budgetedType = resource->resourcePriv().budgetedType();
Brian Salomon614c1a82018-12-19 15:42:06 -0500438
Brian Salomon9bc76d92019-01-24 12:18:33 -0500439 if (budgetedType == GrBudgetedType::kBudgeted) {
440 // Purge the resource immediately if we're over budget
441 // Also purge if the resource has neither a valid scratch key nor a unique key.
442 bool hasKey = resource->resourcePriv().getScratchKey().isValid() || hasUniqueKey;
443 if (!this->overBudget() && hasKey) {
444 return;
445 }
446 } else {
447 // We keep unbudgeted resources with a unique key in the purgeable queue of the cache so
448 // they can be reused again by the image connected to the unique key.
449 if (hasUniqueKey && budgetedType == GrBudgetedType::kUnbudgetedCacheable) {
450 return;
451 }
452 // Check whether this resource could still be used as a scratch resource.
453 if (!resource->resourcePriv().refsWrappedObjects() &&
454 resource->resourcePriv().getScratchKey().isValid()) {
455 // We won't purge an existing resource to make room for this one.
Robert Phillipscf39f372019-09-03 10:29:20 -0400456 if (this->wouldFit(resource->gpuMemorySize())) {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500457 resource->resourcePriv().makeBudgeted();
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500458 return;
459 }
bsalomonc2f35b72015-01-23 07:19:22 -0800460 }
bsalomonc2f35b72015-01-23 07:19:22 -0800461 }
Brian Salomon9bc76d92019-01-24 12:18:33 -0500462
bsalomonf320e042015-02-17 15:09:34 -0800463 SkDEBUGCODE(int beforeCount = this->getResourceCount();)
bsalomon9f2d1572015-02-17 11:47:40 -0800464 resource->cacheAccess().release();
465 // We should at least free this resource, perhaps dependent resources as well.
bsalomonf320e042015-02-17 15:09:34 -0800466 SkASSERT(this->getResourceCount() < beforeCount);
bsalomon71cb0c22014-11-14 12:10:14 -0800467 this->validate();
468}
469
bsalomon0ea80f42015-02-11 10:49:59 -0800470void GrResourceCache::didChangeBudgetStatus(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400471 ASSERT_SINGLE_OWNER
bsalomon84c8e622014-11-17 09:33:27 -0800472 SkASSERT(resource);
473 SkASSERT(this->isInCache(resource));
474
475 size_t size = resource->gpuMemorySize();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500476 // Changing from BudgetedType::kUnbudgetedCacheable to another budgeted type could make
477 // resource become purgeable. However, we should never allow that transition. Wrapped
478 // resources are the only resources that can be in that state and they aren't allowed to
479 // transition from one budgeted state to another.
480 SkDEBUGCODE(bool wasPurgeable = resource->resourcePriv().isPurgeable());
481 if (resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
bsalomon84c8e622014-11-17 09:33:27 -0800482 ++fBudgetedCount;
483 fBudgetedBytes += size;
bsalomonafe30052015-01-16 07:32:33 -0800484#if GR_CACHE_STATS
Brian Osman788b9162020-02-07 10:36:46 -0500485 fBudgetedHighWaterBytes = std::max(fBudgetedBytes, fBudgetedHighWaterBytes);
486 fBudgetedHighWaterCount = std::max(fBudgetedCount, fBudgetedHighWaterCount);
bsalomonafe30052015-01-16 07:32:33 -0800487#endif
Greg Daniel1fd8ac82020-12-11 11:22:01 -0500488 if (!resource->resourcePriv().isPurgeable() &&
489 !resource->cacheAccess().hasRefOrCommandBufferUsage()) {
Brian Salomon2c791fc2019-04-02 11:52:03 -0400490 ++fNumBudgetedResourcesFlushWillMakePurgeable;
491 }
bsalomon84c8e622014-11-17 09:33:27 -0800492 this->purgeAsNeeded();
493 } else {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500494 SkASSERT(resource->resourcePriv().budgetedType() != GrBudgetedType::kUnbudgetedCacheable);
bsalomon84c8e622014-11-17 09:33:27 -0800495 --fBudgetedCount;
496 fBudgetedBytes -= size;
Greg Daniel1fd8ac82020-12-11 11:22:01 -0500497 if (!resource->resourcePriv().isPurgeable() &&
498 !resource->cacheAccess().hasRefOrCommandBufferUsage()) {
Brian Salomon2c791fc2019-04-02 11:52:03 -0400499 --fNumBudgetedResourcesFlushWillMakePurgeable;
500 }
bsalomon84c8e622014-11-17 09:33:27 -0800501 }
Brian Salomon9bc76d92019-01-24 12:18:33 -0500502 SkASSERT(wasPurgeable == resource->resourcePriv().isPurgeable());
Brian Osman39c08ac2017-07-26 09:36:09 -0400503 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800504 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomon84c8e622014-11-17 09:33:27 -0800505
506 this->validate();
507}
508
robertphillipsee843b22016-10-04 05:30:20 -0700509void GrResourceCache::purgeAsNeeded() {
bsalomon3f324322015-04-08 11:01:54 -0700510 SkTArray<GrUniqueKeyInvalidatedMessage> invalidKeyMsgs;
511 fInvalidUniqueKeyInbox.poll(&invalidKeyMsgs);
512 if (invalidKeyMsgs.count()) {
Robert Phillips427966a2018-12-20 17:20:43 -0500513 SkASSERT(fProxyProvider);
514
515 for (int i = 0; i < invalidKeyMsgs.count(); ++i) {
Robert Phillips83c38a82020-10-28 14:57:53 -0400516 if (invalidKeyMsgs[i].inThreadSafeCache()) {
517 fThreadSafeCache->remove(invalidKeyMsgs[i].key());
518 SkASSERT(!fThreadSafeCache->has(invalidKeyMsgs[i].key()));
519 } else {
520 fProxyProvider->processInvalidUniqueKey(
521 invalidKeyMsgs[i].key(), nullptr,
Robert Phillips427966a2018-12-20 17:20:43 -0500522 GrProxyProvider::InvalidateGPUResource::kYes);
Robert Phillips83c38a82020-10-28 14:57:53 -0400523 SkASSERT(!this->findAndRefUniqueResource(invalidKeyMsgs[i].key()));
524 }
Robert Phillips427966a2018-12-20 17:20:43 -0500525 }
bsalomon3f324322015-04-08 11:01:54 -0700526 }
bsalomon71cb0c22014-11-14 12:10:14 -0800527
Brian Osman13dddce2017-05-09 13:19:50 -0400528 this->processFreedGpuResources();
529
bsalomon3f324322015-04-08 11:01:54 -0700530 bool stillOverbudget = this->overBudget();
531 while (stillOverbudget && fPurgeableQueue.count()) {
robertphillipsee843b22016-10-04 05:30:20 -0700532 GrGpuResource* resource = fPurgeableQueue.peek();
Brian Salomon614c1a82018-12-19 15:42:06 -0500533 SkASSERT(resource->resourcePriv().isPurgeable());
bsalomon9f2d1572015-02-17 11:47:40 -0800534 resource->cacheAccess().release();
bsalomon3f324322015-04-08 11:01:54 -0700535 stillOverbudget = this->overBudget();
bsalomon9f2d1572015-02-17 11:47:40 -0800536 }
bsalomon71cb0c22014-11-14 12:10:14 -0800537
Robert Phillips12d06a32020-09-16 12:31:34 -0400538 if (stillOverbudget) {
Robert Phillipsd464feb2020-10-08 11:00:02 -0400539 fThreadSafeCache->dropUniqueRefs(this);
Robert Phillips12d06a32020-09-16 12:31:34 -0400540
541 while (stillOverbudget && fPurgeableQueue.count()) {
542 GrGpuResource* resource = fPurgeableQueue.peek();
543 SkASSERT(resource->resourcePriv().isPurgeable());
544 resource->cacheAccess().release();
545 stillOverbudget = this->overBudget();
546 }
547 }
548
bsalomonb436ed62014-11-17 12:15:56 -0800549 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800550}
551
Robert Phillips6eba0632018-03-28 12:25:42 -0400552void GrResourceCache::purgeUnlockedResources(bool scratchResourcesOnly) {
Robert Phillips12d06a32020-09-16 12:31:34 -0400553
Robert Phillips6eba0632018-03-28 12:25:42 -0400554 if (!scratchResourcesOnly) {
Robert Phillipsd464feb2020-10-08 11:00:02 -0400555 fThreadSafeCache->dropUniqueRefs(nullptr);
Robert Phillips331699c2020-09-22 15:20:01 -0400556
Robert Phillips6eba0632018-03-28 12:25:42 -0400557 // We could disable maintaining the heap property here, but it would add a lot of
558 // complexity. Moreover, this is rarely called.
559 while (fPurgeableQueue.count()) {
560 GrGpuResource* resource = fPurgeableQueue.peek();
Brian Salomon614c1a82018-12-19 15:42:06 -0500561 SkASSERT(resource->resourcePriv().isPurgeable());
Robert Phillips6eba0632018-03-28 12:25:42 -0400562 resource->cacheAccess().release();
563 }
564 } else {
565 // Sort the queue
566 fPurgeableQueue.sort();
567
568 // Make a list of the scratch resources to delete
569 SkTDArray<GrGpuResource*> scratchResources;
570 for (int i = 0; i < fPurgeableQueue.count(); i++) {
571 GrGpuResource* resource = fPurgeableQueue.at(i);
Brian Salomon614c1a82018-12-19 15:42:06 -0500572 SkASSERT(resource->resourcePriv().isPurgeable());
Robert Phillips6eba0632018-03-28 12:25:42 -0400573 if (!resource->getUniqueKey().isValid()) {
574 *scratchResources.append() = resource;
575 }
576 }
577
578 // Delete the scratch resources. This must be done as a separate pass
579 // to avoid messing up the sorted order of the queue
580 for (int i = 0; i < scratchResources.count(); i++) {
581 scratchResources.getAt(i)->cacheAccess().release();
582 }
bsalomon9f2d1572015-02-17 11:47:40 -0800583 }
bsalomon71cb0c22014-11-14 12:10:14 -0800584
bsalomonb436ed62014-11-17 12:15:56 -0800585 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800586}
587
Brian Salomon5e150852017-03-22 14:53:13 -0400588void GrResourceCache::purgeResourcesNotUsedSince(GrStdSteadyClock::time_point purgeTime) {
Robert Phillipsd464feb2020-10-08 11:00:02 -0400589 fThreadSafeCache->dropUniqueRefsOlderThan(purgeTime);
Robert Phillipsc2fe1642020-09-22 17:34:51 -0400590
Brian Salomon5e150852017-03-22 14:53:13 -0400591 while (fPurgeableQueue.count()) {
592 const GrStdSteadyClock::time_point resourceTime =
593 fPurgeableQueue.peek()->cacheAccess().timeWhenResourceBecamePurgeable();
594 if (resourceTime >= purgeTime) {
595 // Resources were given both LRU timestamps and tagged with a frame number when
596 // they first became purgeable. The LRU timestamp won't change again until the
597 // resource is made non-purgeable again. So, at this point all the remaining
598 // resources in the timestamp-sorted queue will have a frame number >= to this
599 // one.
600 break;
601 }
602 GrGpuResource* resource = fPurgeableQueue.peek();
Brian Salomon614c1a82018-12-19 15:42:06 -0500603 SkASSERT(resource->resourcePriv().isPurgeable());
Brian Salomon5e150852017-03-22 14:53:13 -0400604 resource->cacheAccess().release();
605 }
606}
607
Derek Sollenberger5480a182017-05-25 16:43:59 -0400608void GrResourceCache::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
609
Brian Osman788b9162020-02-07 10:36:46 -0500610 const size_t tmpByteBudget = std::max((size_t)0, fBytes - bytesToPurge);
Derek Sollenberger5480a182017-05-25 16:43:59 -0400611 bool stillOverbudget = tmpByteBudget < fBytes;
612
613 if (preferScratchResources && bytesToPurge < fPurgeableBytes) {
614 // Sort the queue
615 fPurgeableQueue.sort();
616
617 // Make a list of the scratch resources to delete
618 SkTDArray<GrGpuResource*> scratchResources;
619 size_t scratchByteCount = 0;
620 for (int i = 0; i < fPurgeableQueue.count() && stillOverbudget; i++) {
621 GrGpuResource* resource = fPurgeableQueue.at(i);
Brian Salomon614c1a82018-12-19 15:42:06 -0500622 SkASSERT(resource->resourcePriv().isPurgeable());
Derek Sollenberger5480a182017-05-25 16:43:59 -0400623 if (!resource->getUniqueKey().isValid()) {
624 *scratchResources.append() = resource;
625 scratchByteCount += resource->gpuMemorySize();
626 stillOverbudget = tmpByteBudget < fBytes - scratchByteCount;
627 }
628 }
629
630 // Delete the scratch resources. This must be done as a separate pass
631 // to avoid messing up the sorted order of the queue
632 for (int i = 0; i < scratchResources.count(); i++) {
633 scratchResources.getAt(i)->cacheAccess().release();
634 }
635 stillOverbudget = tmpByteBudget < fBytes;
636
637 this->validate();
638 }
639
640 // Purge any remaining resources in LRU order
641 if (stillOverbudget) {
642 const size_t cachedByteCount = fMaxBytes;
643 fMaxBytes = tmpByteBudget;
644 this->purgeAsNeeded();
645 fMaxBytes = cachedByteCount;
646 }
647}
Robert Phillips12d06a32020-09-16 12:31:34 -0400648
Brian Salomon8cefa3e2019-04-04 11:39:55 -0400649bool GrResourceCache::requestsFlush() const {
650 return this->overBudget() && !fPurgeableQueue.count() &&
651 fNumBudgetedResourcesFlushWillMakePurgeable > 0;
652}
653
Robert Phillipsddc21482019-10-16 14:30:09 -0400654void GrResourceCache::insertDelayedTextureUnref(GrTexture* texture) {
655 texture->ref();
656 uint32_t id = texture->uniqueID().asUInt();
657 if (auto* data = fTexturesAwaitingUnref.find(id)) {
Brian Salomon876a0172019-03-08 11:12:14 -0500658 data->addRef();
659 } else {
Robert Phillipsddc21482019-10-16 14:30:09 -0400660 fTexturesAwaitingUnref.set(id, {texture});
Brian Salomon876a0172019-03-08 11:12:14 -0500661 }
Brian Osman13dddce2017-05-09 13:19:50 -0400662}
663
664void GrResourceCache::processFreedGpuResources() {
Robert Phillips1dfc77c2019-10-16 16:39:45 -0400665 if (!fTexturesAwaitingUnref.count()) {
Robert Phillips12d06a32020-09-16 12:31:34 -0400666 return;
Robert Phillips1dfc77c2019-10-16 16:39:45 -0400667 }
668
Robert Phillipsddc21482019-10-16 14:30:09 -0400669 SkTArray<GrTextureFreedMessage> msgs;
670 fFreedTextureInbox.poll(&msgs);
Brian Osman13dddce2017-05-09 13:19:50 -0400671 for (int i = 0; i < msgs.count(); ++i) {
Brian Salomon238069b2018-07-11 15:58:57 -0400672 SkASSERT(msgs[i].fOwningUniqueID == fContextUniqueID);
Robert Phillipsddc21482019-10-16 14:30:09 -0400673 uint32_t id = msgs[i].fTexture->uniqueID().asUInt();
674 TextureAwaitingUnref* info = fTexturesAwaitingUnref.find(id);
Greg Daniel1a5d2d52019-12-04 11:14:29 -0500675 // If the GrContext was released or abandoned then fTexturesAwaitingUnref should have been
676 // empty and we would have returned early above. Thus, any texture from a message should be
677 // in the list of fTexturesAwaitingUnref.
678 SkASSERT(info);
679 info->unref();
680 if (info->finished()) {
681 fTexturesAwaitingUnref.remove(id);
Greg Danielb2acf0a2018-09-12 09:17:11 -0400682 }
Brian Osman13dddce2017-05-09 13:19:50 -0400683 }
684}
685
bsalomonf320e042015-02-17 15:09:34 -0800686void GrResourceCache::addToNonpurgeableArray(GrGpuResource* resource) {
687 int index = fNonpurgeableResources.count();
688 *fNonpurgeableResources.append() = resource;
689 *resource->cacheAccess().accessCacheIndex() = index;
690}
691
692void GrResourceCache::removeFromNonpurgeableArray(GrGpuResource* resource) {
693 int* index = resource->cacheAccess().accessCacheIndex();
Adlai Holler9555f292020-10-09 09:41:14 -0400694 // Fill the hole we will create in the array with the tail object, adjust its index, and
bsalomonf320e042015-02-17 15:09:34 -0800695 // then pop the array
696 GrGpuResource* tail = *(fNonpurgeableResources.end() - 1);
697 SkASSERT(fNonpurgeableResources[*index] == resource);
698 fNonpurgeableResources[*index] = tail;
699 *tail->cacheAccess().accessCacheIndex() = *index;
700 fNonpurgeableResources.pop();
701 SkDEBUGCODE(*index = -1);
702}
703
bsalomonddf30e62015-02-19 11:38:44 -0800704uint32_t GrResourceCache::getNextTimestamp() {
705 // If we wrap then all the existing resources will appear older than any resources that get
706 // a timestamp after the wrap.
707 if (0 == fTimestamp) {
708 int count = this->getResourceCount();
709 if (count) {
710 // Reset all the timestamps. We sort the resources by timestamp and then assign
711 // sequential timestamps beginning with 0. This is O(n*lg(n)) but it should be extremely
712 // rare.
713 SkTDArray<GrGpuResource*> sortedPurgeableResources;
714 sortedPurgeableResources.setReserve(fPurgeableQueue.count());
715
716 while (fPurgeableQueue.count()) {
717 *sortedPurgeableResources.append() = fPurgeableQueue.peek();
718 fPurgeableQueue.pop();
719 }
robertphillipsee843b22016-10-04 05:30:20 -0700720
John Stiles886a9042020-07-14 16:28:33 -0400721 SkTQSort(fNonpurgeableResources.begin(), fNonpurgeableResources.end(),
John Stiles6e9ead92020-07-14 00:13:51 +0000722 CompareTimestamp);
bsalomonddf30e62015-02-19 11:38:44 -0800723
724 // Pick resources out of the purgeable and non-purgeable arrays based on lowest
725 // timestamp and assign new timestamps.
726 int currP = 0;
727 int currNP = 0;
728 while (currP < sortedPurgeableResources.count() &&
mtklein56da0252015-11-16 11:16:23 -0800729 currNP < fNonpurgeableResources.count()) {
bsalomonddf30e62015-02-19 11:38:44 -0800730 uint32_t tsP = sortedPurgeableResources[currP]->cacheAccess().timestamp();
731 uint32_t tsNP = fNonpurgeableResources[currNP]->cacheAccess().timestamp();
732 SkASSERT(tsP != tsNP);
733 if (tsP < tsNP) {
734 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
735 } else {
736 // Correct the index in the nonpurgeable array stored on the resource post-sort.
737 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
738 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
739 }
740 }
741
742 // The above loop ended when we hit the end of one array. Finish the other one.
743 while (currP < sortedPurgeableResources.count()) {
744 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
745 }
746 while (currNP < fNonpurgeableResources.count()) {
747 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
748 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
749 }
750
751 // Rebuild the queue.
752 for (int i = 0; i < sortedPurgeableResources.count(); ++i) {
753 fPurgeableQueue.insert(sortedPurgeableResources[i]);
754 }
755
756 this->validate();
757 SkASSERT(count == this->getResourceCount());
758
759 // count should be the next timestamp we return.
760 SkASSERT(fTimestamp == SkToU32(count));
mtklein56da0252015-11-16 11:16:23 -0800761 }
bsalomonddf30e62015-02-19 11:38:44 -0800762 }
763 return fTimestamp++;
764}
765
ericrk0a5fa482015-09-15 14:16:10 -0700766void GrResourceCache::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
767 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
768 fNonpurgeableResources[i]->dumpMemoryStatistics(traceMemoryDump);
769 }
770 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
771 fPurgeableQueue.at(i)->dumpMemoryStatistics(traceMemoryDump);
772 }
773}
774
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500775#if GR_CACHE_STATS
776void GrResourceCache::getStats(Stats* stats) const {
777 stats->reset();
778
779 stats->fTotal = this->getResourceCount();
780 stats->fNumNonPurgeable = fNonpurgeableResources.count();
781 stats->fNumPurgeable = fPurgeableQueue.count();
782
783 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
784 stats->update(fNonpurgeableResources[i]);
785 }
786 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
787 stats->update(fPurgeableQueue.at(i));
788 }
789}
790
791#if GR_TEST_UTILS
792void GrResourceCache::dumpStats(SkString* out) const {
793 this->validate();
794
795 Stats stats;
796
797 this->getStats(&stats);
798
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500799 float byteUtilization = (100.f * fBudgetedBytes) / fMaxBytes;
800
Robert Phillipscf39f372019-09-03 10:29:20 -0400801 out->appendf("Budget: %d bytes\n", (int)fMaxBytes);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500802 out->appendf("\t\tEntry Count: current %d"
Robert Phillipscf39f372019-09-03 10:29:20 -0400803 " (%d budgeted, %d wrapped, %d locked, %d scratch), high %d\n",
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500804 stats.fTotal, fBudgetedCount, stats.fWrapped, stats.fNumNonPurgeable,
Robert Phillipscf39f372019-09-03 10:29:20 -0400805 stats.fScratch, fHighWaterCount);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500806 out->appendf("\t\tEntry Bytes: current %d (budgeted %d, %.2g%% full, %d unbudgeted) high %d\n",
807 SkToInt(fBytes), SkToInt(fBudgetedBytes), byteUtilization,
808 SkToInt(stats.fUnbudgetedSize), SkToInt(fHighWaterBytes));
809}
810
811void GrResourceCache::dumpStatsKeyValuePairs(SkTArray<SkString>* keys,
812 SkTArray<double>* values) const {
813 this->validate();
814
815 Stats stats;
816 this->getStats(&stats);
817
818 keys->push_back(SkString("gpu_cache_purgable_entries")); values->push_back(stats.fNumPurgeable);
819}
820#endif
821
822#endif
823
bsalomon71cb0c22014-11-14 12:10:14 -0800824#ifdef SK_DEBUG
bsalomon0ea80f42015-02-11 10:49:59 -0800825void GrResourceCache::validate() const {
bsalomonc2f35b72015-01-23 07:19:22 -0800826 // Reduce the frequency of validations for large resource counts.
827 static SkRandom gRandom;
828 int mask = (SkNextPow2(fCount + 1) >> 5) - 1;
829 if (~mask && (gRandom.nextU() & mask)) {
830 return;
831 }
832
bsalomonf320e042015-02-17 15:09:34 -0800833 struct Stats {
834 size_t fBytes;
835 int fBudgetedCount;
836 size_t fBudgetedBytes;
837 int fLocked;
838 int fScratch;
839 int fCouldBeScratch;
840 int fContent;
841 const ScratchMap* fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800842 const UniqueHash* fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800843
bsalomonf320e042015-02-17 15:09:34 -0800844 Stats(const GrResourceCache* cache) {
845 memset(this, 0, sizeof(*this));
846 fScratchMap = &cache->fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800847 fUniqueHash = &cache->fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800848 }
849
bsalomonf320e042015-02-17 15:09:34 -0800850 void update(GrGpuResource* resource) {
851 fBytes += resource->gpuMemorySize();
bsalomondace19e2014-11-17 07:34:06 -0800852
Brian Salomon614c1a82018-12-19 15:42:06 -0500853 if (!resource->resourcePriv().isPurgeable()) {
bsalomonf320e042015-02-17 15:09:34 -0800854 ++fLocked;
855 }
bsalomon9f2d1572015-02-17 11:47:40 -0800856
robertphillipsc4ed6842016-05-24 14:17:12 -0700857 const GrScratchKey& scratchKey = resource->resourcePriv().getScratchKey();
858 const GrUniqueKey& uniqueKey = resource->getUniqueKey();
859
bsalomonf320e042015-02-17 15:09:34 -0800860 if (resource->cacheAccess().isScratch()) {
robertphillipsc4ed6842016-05-24 14:17:12 -0700861 SkASSERT(!uniqueKey.isValid());
bsalomonf320e042015-02-17 15:09:34 -0800862 ++fScratch;
robertphillipsc4ed6842016-05-24 14:17:12 -0700863 SkASSERT(fScratchMap->countForKey(scratchKey));
kkinnunen2e6055b2016-04-22 01:48:29 -0700864 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
robertphillipsc4ed6842016-05-24 14:17:12 -0700865 } else if (scratchKey.isValid()) {
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500866 SkASSERT(GrBudgetedType::kBudgeted != resource->resourcePriv().budgetedType() ||
robertphillipsc4ed6842016-05-24 14:17:12 -0700867 uniqueKey.isValid());
868 if (!uniqueKey.isValid()) {
mtklein4e976072016-08-08 09:06:27 -0700869 ++fCouldBeScratch;
robertphillipsc4ed6842016-05-24 14:17:12 -0700870 SkASSERT(fScratchMap->countForKey(scratchKey));
871 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700872 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
bsalomonf320e042015-02-17 15:09:34 -0800873 }
bsalomon8718aaf2015-02-19 07:24:21 -0800874 if (uniqueKey.isValid()) {
bsalomonf320e042015-02-17 15:09:34 -0800875 ++fContent;
bsalomon8718aaf2015-02-19 07:24:21 -0800876 SkASSERT(fUniqueHash->find(uniqueKey) == resource);
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500877 SkASSERT(GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType() ||
Brian Osman0562eb92017-05-08 11:16:39 -0400878 resource->resourcePriv().refsWrappedObjects());
robertphillipsc4ed6842016-05-24 14:17:12 -0700879
880 if (scratchKey.isValid()) {
881 SkASSERT(!fScratchMap->has(resource, scratchKey));
882 }
bsalomonf320e042015-02-17 15:09:34 -0800883 }
884
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500885 if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
bsalomonf320e042015-02-17 15:09:34 -0800886 ++fBudgetedCount;
887 fBudgetedBytes += resource->gpuMemorySize();
888 }
bsalomon9f2d1572015-02-17 11:47:40 -0800889 }
bsalomonf320e042015-02-17 15:09:34 -0800890 };
891
robertphillipsc4ed6842016-05-24 14:17:12 -0700892 {
robertphillipsc4ed6842016-05-24 14:17:12 -0700893 int count = 0;
Mike Kleincff63962020-03-14 16:22:45 -0500894 fScratchMap.foreach([&](const GrGpuResource& resource) {
895 SkASSERT(resource.resourcePriv().getScratchKey().isValid());
896 SkASSERT(!resource.getUniqueKey().isValid());
robertphillipsc4ed6842016-05-24 14:17:12 -0700897 count++;
Mike Kleincff63962020-03-14 16:22:45 -0500898 });
899 SkASSERT(count == fScratchMap.count());
robertphillipsc4ed6842016-05-24 14:17:12 -0700900 }
901
bsalomonf320e042015-02-17 15:09:34 -0800902 Stats stats(this);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400903 size_t purgeableBytes = 0;
Brian Salomon2c791fc2019-04-02 11:52:03 -0400904 int numBudgetedResourcesFlushWillMakePurgeable = 0;
bsalomonf320e042015-02-17 15:09:34 -0800905
906 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500907 SkASSERT(!fNonpurgeableResources[i]->resourcePriv().isPurgeable() ||
bsalomon3f324322015-04-08 11:01:54 -0700908 fNewlyPurgeableResourceForValidation == fNonpurgeableResources[i]);
bsalomonf320e042015-02-17 15:09:34 -0800909 SkASSERT(*fNonpurgeableResources[i]->cacheAccess().accessCacheIndex() == i);
910 SkASSERT(!fNonpurgeableResources[i]->wasDestroyed());
Brian Salomon2c791fc2019-04-02 11:52:03 -0400911 if (fNonpurgeableResources[i]->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted &&
Greg Daniel1fd8ac82020-12-11 11:22:01 -0500912 !fNonpurgeableResources[i]->cacheAccess().hasRefOrCommandBufferUsage() &&
Brian Salomon2c791fc2019-04-02 11:52:03 -0400913 fNewlyPurgeableResourceForValidation != fNonpurgeableResources[i]) {
Brian Salomon2c791fc2019-04-02 11:52:03 -0400914 ++numBudgetedResourcesFlushWillMakePurgeable;
915 }
bsalomonf320e042015-02-17 15:09:34 -0800916 stats.update(fNonpurgeableResources[i]);
bsalomon71cb0c22014-11-14 12:10:14 -0800917 }
bsalomon9f2d1572015-02-17 11:47:40 -0800918 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500919 SkASSERT(fPurgeableQueue.at(i)->resourcePriv().isPurgeable());
bsalomonf320e042015-02-17 15:09:34 -0800920 SkASSERT(*fPurgeableQueue.at(i)->cacheAccess().accessCacheIndex() == i);
921 SkASSERT(!fPurgeableQueue.at(i)->wasDestroyed());
922 stats.update(fPurgeableQueue.at(i));
Derek Sollenbergeree479142017-05-24 11:41:33 -0400923 purgeableBytes += fPurgeableQueue.at(i)->gpuMemorySize();
bsalomon9f2d1572015-02-17 11:47:40 -0800924 }
925
bsalomonf320e042015-02-17 15:09:34 -0800926 SkASSERT(fCount == this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800927 SkASSERT(fBudgetedCount <= fCount);
bsalomonf320e042015-02-17 15:09:34 -0800928 SkASSERT(fBudgetedBytes <= fBytes);
929 SkASSERT(stats.fBytes == fBytes);
Brian Salomon2c791fc2019-04-02 11:52:03 -0400930 SkASSERT(fNumBudgetedResourcesFlushWillMakePurgeable ==
931 numBudgetedResourcesFlushWillMakePurgeable);
bsalomonf320e042015-02-17 15:09:34 -0800932 SkASSERT(stats.fBudgetedBytes == fBudgetedBytes);
933 SkASSERT(stats.fBudgetedCount == fBudgetedCount);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400934 SkASSERT(purgeableBytes == fPurgeableBytes);
bsalomon71cb0c22014-11-14 12:10:14 -0800935#if GR_CACHE_STATS
bsalomondace19e2014-11-17 07:34:06 -0800936 SkASSERT(fBudgetedHighWaterCount <= fHighWaterCount);
937 SkASSERT(fBudgetedHighWaterBytes <= fHighWaterBytes);
bsalomonf320e042015-02-17 15:09:34 -0800938 SkASSERT(fBytes <= fHighWaterBytes);
939 SkASSERT(fCount <= fHighWaterCount);
940 SkASSERT(fBudgetedBytes <= fBudgetedHighWaterBytes);
941 SkASSERT(fBudgetedCount <= fBudgetedHighWaterCount);
bsalomon71cb0c22014-11-14 12:10:14 -0800942#endif
bsalomon8718aaf2015-02-19 07:24:21 -0800943 SkASSERT(stats.fContent == fUniqueHash.count());
bsalomonf320e042015-02-17 15:09:34 -0800944 SkASSERT(stats.fScratch + stats.fCouldBeScratch == fScratchMap.count());
bsalomon71cb0c22014-11-14 12:10:14 -0800945
bsalomon3f324322015-04-08 11:01:54 -0700946 // This assertion is not currently valid because we can be in recursive notifyCntReachedZero()
bsalomon12299ab2014-11-14 13:33:09 -0800947 // calls. This will be fixed when subresource registration is explicit.
bsalomondace19e2014-11-17 07:34:06 -0800948 // bool overBudget = budgetedBytes > fMaxBytes || budgetedCount > fMaxCount;
bsalomon12299ab2014-11-14 13:33:09 -0800949 // SkASSERT(!overBudget || locked == count || fPurging);
bsalomon71cb0c22014-11-14 12:10:14 -0800950}
bsalomonf320e042015-02-17 15:09:34 -0800951
952bool GrResourceCache::isInCache(const GrGpuResource* resource) const {
953 int index = *resource->cacheAccess().accessCacheIndex();
954 if (index < 0) {
955 return false;
956 }
957 if (index < fPurgeableQueue.count() && fPurgeableQueue.at(index) == resource) {
958 return true;
959 }
960 if (index < fNonpurgeableResources.count() && fNonpurgeableResources[index] == resource) {
961 return true;
962 }
963 SkDEBUGFAIL("Resource index should be -1 or the resource should be in the cache.");
964 return false;
965}
966
bsalomon71cb0c22014-11-14 12:10:14 -0800967#endif