blob: f6378db092b7e0e6d5c9ba259d6af6cc8433b4a4 [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"
19#include "src/gpu/GrContextPriv.h"
20#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
Mike Klein0ec1c572018-12-04 11:52:51 -050039 int32_t type = nextType++;
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
Mike Klein0ec1c572018-12-04 11:52:51 -050050 int32_t domain = nextDomain++;
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 Salomon8f8995a2018-10-15 14:32:15 -0400111GrResourceCache::GrResourceCache(const GrCaps* caps, GrSingleOwner* singleOwner,
112 uint32_t contextUniqueID)
Brian Salomon2c791fc2019-04-02 11:52:03 -0400113 : fInvalidUniqueKeyInbox(contextUniqueID)
Robert Phillipsddc21482019-10-16 14:30:09 -0400114 , fFreedTextureInbox(contextUniqueID)
Brian Salomon238069b2018-07-11 15:58:57 -0400115 , fContextUniqueID(contextUniqueID)
Brian Salomon8f8995a2018-10-15 14:32:15 -0400116 , fSingleOwner(singleOwner)
Brian Salomon238069b2018-07-11 15:58:57 -0400117 , fPreferVRAMUseOverFlushes(caps->preferVRAMUseOverFlushes()) {
118 SkASSERT(contextUniqueID != SK_InvalidUniqueID);
bsalomon71cb0c22014-11-14 12:10:14 -0800119}
120
bsalomon0ea80f42015-02-11 10:49:59 -0800121GrResourceCache::~GrResourceCache() {
bsalomonc8dc1f72014-08-21 13:02:13 -0700122 this->releaseAll();
123}
124
Robert Phillipscf39f372019-09-03 10:29:20 -0400125void GrResourceCache::setLimit(size_t bytes) {
bsalomon71cb0c22014-11-14 12:10:14 -0800126 fMaxBytes = bytes;
127 this->purgeAsNeeded();
128}
129
bsalomon0ea80f42015-02-11 10:49:59 -0800130void GrResourceCache::insertResource(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400131 ASSERT_SINGLE_OWNER
bsalomon49f085d2014-09-05 13:34:00 -0700132 SkASSERT(resource);
bsalomon16961262014-08-26 14:01:07 -0700133 SkASSERT(!this->isInCache(resource));
bsalomonf320e042015-02-17 15:09:34 -0800134 SkASSERT(!resource->wasDestroyed());
Brian Salomon614c1a82018-12-19 15:42:06 -0500135 SkASSERT(!resource->resourcePriv().isPurgeable());
bsalomonddf30e62015-02-19 11:38:44 -0800136
137 // We must set the timestamp before adding to the array in case the timestamp wraps and we wind
138 // up iterating over all the resources that already have timestamps.
139 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
140
bsalomonf320e042015-02-17 15:09:34 -0800141 this->addToNonpurgeableArray(resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800142
bsalomondace19e2014-11-17 07:34:06 -0800143 size_t size = resource->gpuMemorySize();
bsalomonf320e042015-02-17 15:09:34 -0800144 SkDEBUGCODE(++fCount;)
bsalomon84c8e622014-11-17 09:33:27 -0800145 fBytes += size;
bsalomon82b1d622014-11-14 13:59:57 -0800146#if GR_CACHE_STATS
Brian Osman788b9162020-02-07 10:36:46 -0500147 fHighWaterCount = std::max(this->getResourceCount(), fHighWaterCount);
148 fHighWaterBytes = std::max(fBytes, fHighWaterBytes);
bsalomon82b1d622014-11-14 13:59:57 -0800149#endif
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500150 if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
bsalomondace19e2014-11-17 07:34:06 -0800151 ++fBudgetedCount;
152 fBudgetedBytes += size;
Brian Osman39c08ac2017-07-26 09:36:09 -0400153 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800154 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomondace19e2014-11-17 07:34:06 -0800155#if GR_CACHE_STATS
Brian Osman788b9162020-02-07 10:36:46 -0500156 fBudgetedHighWaterCount = std::max(fBudgetedCount, fBudgetedHighWaterCount);
157 fBudgetedHighWaterBytes = std::max(fBudgetedBytes, fBudgetedHighWaterBytes);
bsalomondace19e2014-11-17 07:34:06 -0800158#endif
159 }
robertphillipsc4ed6842016-05-24 14:17:12 -0700160 if (resource->resourcePriv().getScratchKey().isValid() &&
161 !resource->getUniqueKey().isValid()) {
kkinnunen2e6055b2016-04-22 01:48:29 -0700162 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
bsalomon3582d3e2015-02-13 14:20:05 -0800163 fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
bsalomon744998e2014-08-28 09:54:34 -0700164 }
bsalomon9f2d1572015-02-17 11:47:40 -0800165
bsalomon71cb0c22014-11-14 12:10:14 -0800166 this->purgeAsNeeded();
bsalomonc8dc1f72014-08-21 13:02:13 -0700167}
168
bsalomon0ea80f42015-02-11 10:49:59 -0800169void GrResourceCache::removeResource(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400170 ASSERT_SINGLE_OWNER
bsalomon9f2d1572015-02-17 11:47:40 -0800171 this->validate();
bsalomon16961262014-08-26 14:01:07 -0700172 SkASSERT(this->isInCache(resource));
bsalomondace19e2014-11-17 07:34:06 -0800173
Derek Sollenbergeree479142017-05-24 11:41:33 -0400174 size_t size = resource->gpuMemorySize();
Brian Salomon614c1a82018-12-19 15:42:06 -0500175 if (resource->resourcePriv().isPurgeable()) {
bsalomon9f2d1572015-02-17 11:47:40 -0800176 fPurgeableQueue.remove(resource);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400177 fPurgeableBytes -= size;
bsalomonf320e042015-02-17 15:09:34 -0800178 } else {
179 this->removeFromNonpurgeableArray(resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800180 }
181
bsalomonf320e042015-02-17 15:09:34 -0800182 SkDEBUGCODE(--fCount;)
bsalomondace19e2014-11-17 07:34:06 -0800183 fBytes -= size;
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500184 if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
bsalomondace19e2014-11-17 07:34:06 -0800185 --fBudgetedCount;
186 fBudgetedBytes -= size;
Brian Osman39c08ac2017-07-26 09:36:09 -0400187 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800188 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomondace19e2014-11-17 07:34:06 -0800189 }
190
robertphillipsc4ed6842016-05-24 14:17:12 -0700191 if (resource->resourcePriv().getScratchKey().isValid() &&
192 !resource->getUniqueKey().isValid()) {
bsalomon3582d3e2015-02-13 14:20:05 -0800193 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
bsalomon744998e2014-08-28 09:54:34 -0700194 }
bsalomon8718aaf2015-02-19 07:24:21 -0800195 if (resource->getUniqueKey().isValid()) {
196 fUniqueHash.remove(resource->getUniqueKey());
bsalomon8b79d232014-11-10 10:19:06 -0800197 }
bsalomonb436ed62014-11-17 12:15:56 -0800198 this->validate();
bsalomonc8dc1f72014-08-21 13:02:13 -0700199}
200
bsalomon0ea80f42015-02-11 10:49:59 -0800201void GrResourceCache::abandonAll() {
bsalomon71cb0c22014-11-14 12:10:14 -0800202 AutoValidate av(this);
203
Robert Phillipsd464feb2020-10-08 11:00:02 -0400204 fThreadSafeCache->dropAllRefs();
Robert Phillips12d06a32020-09-16 12:31:34 -0400205
Brian Salomon876a0172019-03-08 11:12:14 -0500206 // We need to make sure to free any resources that were waiting on a free message but never
207 // received one.
Robert Phillipsddc21482019-10-16 14:30:09 -0400208 fTexturesAwaitingUnref.reset();
Greg Danielb2acf0a2018-09-12 09:17:11 -0400209
bsalomonf320e042015-02-17 15:09:34 -0800210 while (fNonpurgeableResources.count()) {
211 GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
212 SkASSERT(!back->wasDestroyed());
213 back->cacheAccess().abandon();
bsalomonc8dc1f72014-08-21 13:02:13 -0700214 }
bsalomonf320e042015-02-17 15:09:34 -0800215
216 while (fPurgeableQueue.count()) {
217 GrGpuResource* top = fPurgeableQueue.peek();
218 SkASSERT(!top->wasDestroyed());
219 top->cacheAccess().abandon();
220 }
221
bsalomon744998e2014-08-28 09:54:34 -0700222 SkASSERT(!fScratchMap.count());
bsalomon8718aaf2015-02-19 07:24:21 -0800223 SkASSERT(!fUniqueHash.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700224 SkASSERT(!fCount);
bsalomonf320e042015-02-17 15:09:34 -0800225 SkASSERT(!this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800226 SkASSERT(!fBytes);
227 SkASSERT(!fBudgetedCount);
228 SkASSERT(!fBudgetedBytes);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400229 SkASSERT(!fPurgeableBytes);
Robert Phillipsddc21482019-10-16 14:30:09 -0400230 SkASSERT(!fTexturesAwaitingUnref.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700231}
232
bsalomon0ea80f42015-02-11 10:49:59 -0800233void GrResourceCache::releaseAll() {
bsalomon71cb0c22014-11-14 12:10:14 -0800234 AutoValidate av(this);
235
Robert Phillipsd464feb2020-10-08 11:00:02 -0400236 fThreadSafeCache->dropAllRefs();
Robert Phillips12d06a32020-09-16 12:31:34 -0400237
Brian Osman13dddce2017-05-09 13:19:50 -0400238 this->processFreedGpuResources();
239
Greg Danielc27eb722018-08-10 09:48:08 -0400240 // We need to make sure to free any resources that were waiting on a free message but never
241 // received one.
Robert Phillipsddc21482019-10-16 14:30:09 -0400242 fTexturesAwaitingUnref.reset();
Greg Danielc27eb722018-08-10 09:48:08 -0400243
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500244 SkASSERT(fProxyProvider); // better have called setProxyProvider
Robert Phillipsd464feb2020-10-08 11:00:02 -0400245 SkASSERT(fThreadSafeCache); // better have called setThreadSafeCache too
Robert Phillips12d06a32020-09-16 12:31:34 -0400246
Robert Phillips3ec95732017-09-29 15:10:39 -0400247 // We must remove the uniqueKeys from the proxies here. While they possess a uniqueKey
248 // they also have a raw pointer back to this class (which is presumably going away)!
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500249 fProxyProvider->removeAllUniqueKeys();
Robert Phillips45a6f142017-09-29 09:49:41 -0400250
Brian Salomon614c1a82018-12-19 15:42:06 -0500251 while (fNonpurgeableResources.count()) {
bsalomonf320e042015-02-17 15:09:34 -0800252 GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
253 SkASSERT(!back->wasDestroyed());
254 back->cacheAccess().release();
bsalomonc8dc1f72014-08-21 13:02:13 -0700255 }
bsalomonf320e042015-02-17 15:09:34 -0800256
257 while (fPurgeableQueue.count()) {
258 GrGpuResource* top = fPurgeableQueue.peek();
259 SkASSERT(!top->wasDestroyed());
260 top->cacheAccess().release();
261 }
262
bsalomon744998e2014-08-28 09:54:34 -0700263 SkASSERT(!fScratchMap.count());
bsalomon8718aaf2015-02-19 07:24:21 -0800264 SkASSERT(!fUniqueHash.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700265 SkASSERT(!fCount);
bsalomonf320e042015-02-17 15:09:34 -0800266 SkASSERT(!this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800267 SkASSERT(!fBytes);
268 SkASSERT(!fBudgetedCount);
269 SkASSERT(!fBudgetedBytes);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400270 SkASSERT(!fPurgeableBytes);
Robert Phillipsddc21482019-10-16 14:30:09 -0400271 SkASSERT(!fTexturesAwaitingUnref.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700272}
bsalomonbcf0a522014-10-08 08:40:09 -0700273
Brian Salomon2c791fc2019-04-02 11:52:03 -0400274void GrResourceCache::refResource(GrGpuResource* resource) {
275 SkASSERT(resource);
276 SkASSERT(resource->getContext()->priv().getResourceCache() == this);
277 if (resource->cacheAccess().hasRef()) {
278 resource->ref();
279 } else {
280 this->refAndMakeResourceMRU(resource);
281 }
282 this->validate();
283}
284
bsalomon0ea80f42015-02-11 10:49:59 -0800285class GrResourceCache::AvailableForScratchUse {
bsalomonbcf0a522014-10-08 08:40:09 -0700286public:
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400287 AvailableForScratchUse() { }
bsalomonbcf0a522014-10-08 08:40:09 -0700288
289 bool operator()(const GrGpuResource* resource) const {
robertphillipsc4ed6842016-05-24 14:17:12 -0700290 SkASSERT(!resource->getUniqueKey().isValid() &&
291 resource->resourcePriv().getScratchKey().isValid());
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400292
Robert Phillipsaee18c92019-09-06 11:48:27 -0400293 // isScratch() also tests that the resource is budgeted.
bsalomon12299ab2014-11-14 13:33:09 -0800294 if (resource->internalHasRef() || !resource->cacheAccess().isScratch()) {
bsalomon000f8292014-10-15 19:04:14 -0700295 return false;
bsalomonbcf0a522014-10-08 08:40:09 -0700296 }
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400297 return true;
bsalomonbcf0a522014-10-08 08:40:09 -0700298 }
bsalomonbcf0a522014-10-08 08:40:09 -0700299};
300
Robert Phillipsaee18c92019-09-06 11:48:27 -0400301GrGpuResource* GrResourceCache::findAndRefScratchResource(const GrScratchKey& scratchKey) {
bsalomon7775c852014-12-30 12:50:52 -0800302 SkASSERT(scratchKey.isValid());
robertphillipsee843b22016-10-04 05:30:20 -0700303
Robert Phillipsaee18c92019-09-06 11:48:27 -0400304 GrGpuResource* resource = fScratchMap.find(scratchKey, AvailableForScratchUse());
bsalomon71cb0c22014-11-14 12:10:14 -0800305 if (resource) {
bsalomon9f2d1572015-02-17 11:47:40 -0800306 this->refAndMakeResourceMRU(resource);
bsalomonb436ed62014-11-17 12:15:56 -0800307 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800308 }
309 return resource;
bsalomonbcf0a522014-10-08 08:40:09 -0700310}
bsalomon8b79d232014-11-10 10:19:06 -0800311
bsalomon0ea80f42015-02-11 10:49:59 -0800312void GrResourceCache::willRemoveScratchKey(const GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400313 ASSERT_SINGLE_OWNER
bsalomon3582d3e2015-02-13 14:20:05 -0800314 SkASSERT(resource->resourcePriv().getScratchKey().isValid());
robertphillipsc4ed6842016-05-24 14:17:12 -0700315 if (!resource->getUniqueKey().isValid()) {
316 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
317 }
bsalomon10e23ca2014-11-25 05:52:06 -0800318}
319
bsalomonf99e9612015-02-19 08:24:16 -0800320void GrResourceCache::removeUniqueKey(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400321 ASSERT_SINGLE_OWNER
bsalomon3f324322015-04-08 11:01:54 -0700322 // Someone has a ref to this resource in order to have removed the key. When the ref count
323 // reaches zero we will get a ref cnt notification and figure out what to do with it.
bsalomonf99e9612015-02-19 08:24:16 -0800324 if (resource->getUniqueKey().isValid()) {
325 SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
326 fUniqueHash.remove(resource->getUniqueKey());
327 }
328 resource->cacheAccess().removeUniqueKey();
robertphillipsc4ed6842016-05-24 14:17:12 -0700329 if (resource->resourcePriv().getScratchKey().isValid()) {
330 fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
331 }
332
Brian Salomon9bc76d92019-01-24 12:18:33 -0500333 // Removing a unique key from a kUnbudgetedCacheable resource would make the resource
334 // require purging. However, the resource must be ref'ed to get here and therefore can't
335 // be purgeable. We'll purge it when the refs reach zero.
336 SkASSERT(!resource->resourcePriv().isPurgeable());
bsalomonf99e9612015-02-19 08:24:16 -0800337 this->validate();
bsalomon23e619c2015-02-06 11:54:28 -0800338}
339
bsalomonf99e9612015-02-19 08:24:16 -0800340void GrResourceCache::changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400341 ASSERT_SINGLE_OWNER
bsalomon8b79d232014-11-10 10:19:06 -0800342 SkASSERT(resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800343 SkASSERT(this->isInCache(resource));
bsalomon8b79d232014-11-10 10:19:06 -0800344
bsalomonf99e9612015-02-19 08:24:16 -0800345 // If another resource has the new key, remove its key then install the key on this resource.
346 if (newKey.isValid()) {
Greg Daniel0d537802017-09-08 11:44:14 -0400347 if (GrGpuResource* old = fUniqueHash.find(newKey)) {
348 // If the old resource using the key is purgeable and is unreachable, then remove it.
Brian Salomon614c1a82018-12-19 15:42:06 -0500349 if (!old->resourcePriv().getScratchKey().isValid() &&
350 old->resourcePriv().isPurgeable()) {
Greg Daniel0d537802017-09-08 11:44:14 -0400351 old->cacheAccess().release();
352 } else {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500353 // removeUniqueKey expects an external owner of the resource.
354 this->removeUniqueKey(sk_ref_sp(old).get());
Greg Daniel0d537802017-09-08 11:44:14 -0400355 }
356 }
357 SkASSERT(nullptr == fUniqueHash.find(newKey));
358
robertphillipsc4ed6842016-05-24 14:17:12 -0700359 // Remove the entry for this resource if it already has a unique key.
360 if (resource->getUniqueKey().isValid()) {
361 SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
362 fUniqueHash.remove(resource->getUniqueKey());
363 SkASSERT(nullptr == fUniqueHash.find(resource->getUniqueKey()));
364 } else {
365 // 'resource' didn't have a valid unique key before so it is switching sides. Remove it
366 // from the ScratchMap
367 if (resource->resourcePriv().getScratchKey().isValid()) {
368 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
369 }
370 }
371
bsalomonf99e9612015-02-19 08:24:16 -0800372 resource->cacheAccess().setUniqueKey(newKey);
373 fUniqueHash.add(resource);
374 } else {
robertphillipsc4ed6842016-05-24 14:17:12 -0700375 this->removeUniqueKey(resource);
bsalomonf99e9612015-02-19 08:24:16 -0800376 }
377
bsalomon71cb0c22014-11-14 12:10:14 -0800378 this->validate();
bsalomon8b79d232014-11-10 10:19:06 -0800379}
bsalomon71cb0c22014-11-14 12:10:14 -0800380
bsalomon9f2d1572015-02-17 11:47:40 -0800381void GrResourceCache::refAndMakeResourceMRU(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400382 ASSERT_SINGLE_OWNER
bsalomon71cb0c22014-11-14 12:10:14 -0800383 SkASSERT(resource);
384 SkASSERT(this->isInCache(resource));
bsalomonddf30e62015-02-19 11:38:44 -0800385
Brian Salomon614c1a82018-12-19 15:42:06 -0500386 if (resource->resourcePriv().isPurgeable()) {
bsalomon9f2d1572015-02-17 11:47:40 -0800387 // It's about to become unpurgeable.
Derek Sollenbergeree479142017-05-24 11:41:33 -0400388 fPurgeableBytes -= resource->gpuMemorySize();
bsalomon9f2d1572015-02-17 11:47:40 -0800389 fPurgeableQueue.remove(resource);
bsalomonf320e042015-02-17 15:09:34 -0800390 this->addToNonpurgeableArray(resource);
Brian Salomon2c791fc2019-04-02 11:52:03 -0400391 } else if (!resource->cacheAccess().hasRef() &&
392 resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
393 SkASSERT(fNumBudgetedResourcesFlushWillMakePurgeable > 0);
394 fNumBudgetedResourcesFlushWillMakePurgeable--;
bsalomon9f2d1572015-02-17 11:47:40 -0800395 }
Brian Salomon01ceae92019-04-02 11:49:54 -0400396 resource->cacheAccess().ref();
bsalomonddf30e62015-02-19 11:38:44 -0800397
398 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
bsalomonf320e042015-02-17 15:09:34 -0800399 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800400}
401
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400402void GrResourceCache::notifyRefCntReachedZero(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400403 ASSERT_SINGLE_OWNER
bsalomon71cb0c22014-11-14 12:10:14 -0800404 SkASSERT(resource);
bsalomon3f324322015-04-08 11:01:54 -0700405 SkASSERT(!resource->wasDestroyed());
bsalomon71cb0c22014-11-14 12:10:14 -0800406 SkASSERT(this->isInCache(resource));
bsalomon3f324322015-04-08 11:01:54 -0700407 // This resource should always be in the nonpurgeable array when this function is called. It
408 // will be moved to the queue if it is newly purgeable.
409 SkASSERT(fNonpurgeableResources[*resource->cacheAccess().accessCacheIndex()] == resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800410
bsalomon3f324322015-04-08 11:01:54 -0700411#ifdef SK_DEBUG
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400412 // When the timestamp overflows validate() is called. validate() checks that resources in
413 // the nonpurgeable array are indeed not purgeable. However, the movement from the array to
414 // the purgeable queue happens just below in this function. So we mark it as an exception.
415 if (resource->resourcePriv().isPurgeable()) {
416 fNewlyPurgeableResourceForValidation = resource;
bsalomon3f324322015-04-08 11:01:54 -0700417 }
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400418#endif
419 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
420 SkDEBUGCODE(fNewlyPurgeableResourceForValidation = nullptr);
bsalomon3f324322015-04-08 11:01:54 -0700421
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400422 if (!resource->resourcePriv().isPurgeable() &&
423 resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
424 ++fNumBudgetedResourcesFlushWillMakePurgeable;
bsalomon3f324322015-04-08 11:01:54 -0700425 }
426
Brian Salomon9bc76d92019-01-24 12:18:33 -0500427 if (!resource->resourcePriv().isPurgeable()) {
428 this->validate();
429 return;
430 }
431
bsalomonf320e042015-02-17 15:09:34 -0800432 this->removeFromNonpurgeableArray(resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800433 fPurgeableQueue.insert(resource);
Brian Salomon5e150852017-03-22 14:53:13 -0400434 resource->cacheAccess().setTimeWhenResourceBecomePurgeable();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400435 fPurgeableBytes += resource->gpuMemorySize();
bsalomon71cb0c22014-11-14 12:10:14 -0800436
Greg Daniel303e83e2018-09-10 14:10:19 -0400437 bool hasUniqueKey = resource->getUniqueKey().isValid();
438
Brian Salomon9bc76d92019-01-24 12:18:33 -0500439 GrBudgetedType budgetedType = resource->resourcePriv().budgetedType();
Brian Salomon614c1a82018-12-19 15:42:06 -0500440
Brian Salomon9bc76d92019-01-24 12:18:33 -0500441 if (budgetedType == GrBudgetedType::kBudgeted) {
442 // Purge the resource immediately if we're over budget
443 // Also purge if the resource has neither a valid scratch key nor a unique key.
444 bool hasKey = resource->resourcePriv().getScratchKey().isValid() || hasUniqueKey;
445 if (!this->overBudget() && hasKey) {
446 return;
447 }
448 } else {
449 // We keep unbudgeted resources with a unique key in the purgeable queue of the cache so
450 // they can be reused again by the image connected to the unique key.
451 if (hasUniqueKey && budgetedType == GrBudgetedType::kUnbudgetedCacheable) {
452 return;
453 }
454 // Check whether this resource could still be used as a scratch resource.
455 if (!resource->resourcePriv().refsWrappedObjects() &&
456 resource->resourcePriv().getScratchKey().isValid()) {
457 // We won't purge an existing resource to make room for this one.
Robert Phillipscf39f372019-09-03 10:29:20 -0400458 if (this->wouldFit(resource->gpuMemorySize())) {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500459 resource->resourcePriv().makeBudgeted();
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500460 return;
461 }
bsalomonc2f35b72015-01-23 07:19:22 -0800462 }
bsalomonc2f35b72015-01-23 07:19:22 -0800463 }
Brian Salomon9bc76d92019-01-24 12:18:33 -0500464
bsalomonf320e042015-02-17 15:09:34 -0800465 SkDEBUGCODE(int beforeCount = this->getResourceCount();)
bsalomon9f2d1572015-02-17 11:47:40 -0800466 resource->cacheAccess().release();
467 // We should at least free this resource, perhaps dependent resources as well.
bsalomonf320e042015-02-17 15:09:34 -0800468 SkASSERT(this->getResourceCount() < beforeCount);
bsalomon71cb0c22014-11-14 12:10:14 -0800469 this->validate();
470}
471
bsalomon0ea80f42015-02-11 10:49:59 -0800472void GrResourceCache::didChangeBudgetStatus(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400473 ASSERT_SINGLE_OWNER
bsalomon84c8e622014-11-17 09:33:27 -0800474 SkASSERT(resource);
475 SkASSERT(this->isInCache(resource));
476
477 size_t size = resource->gpuMemorySize();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500478 // Changing from BudgetedType::kUnbudgetedCacheable to another budgeted type could make
479 // resource become purgeable. However, we should never allow that transition. Wrapped
480 // resources are the only resources that can be in that state and they aren't allowed to
481 // transition from one budgeted state to another.
482 SkDEBUGCODE(bool wasPurgeable = resource->resourcePriv().isPurgeable());
483 if (resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
bsalomon84c8e622014-11-17 09:33:27 -0800484 ++fBudgetedCount;
485 fBudgetedBytes += size;
bsalomonafe30052015-01-16 07:32:33 -0800486#if GR_CACHE_STATS
Brian Osman788b9162020-02-07 10:36:46 -0500487 fBudgetedHighWaterBytes = std::max(fBudgetedBytes, fBudgetedHighWaterBytes);
488 fBudgetedHighWaterCount = std::max(fBudgetedCount, fBudgetedHighWaterCount);
bsalomonafe30052015-01-16 07:32:33 -0800489#endif
Brian Salomon2c791fc2019-04-02 11:52:03 -0400490 if (!resource->resourcePriv().isPurgeable() && !resource->cacheAccess().hasRef()) {
491 ++fNumBudgetedResourcesFlushWillMakePurgeable;
492 }
bsalomon84c8e622014-11-17 09:33:27 -0800493 this->purgeAsNeeded();
494 } else {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500495 SkASSERT(resource->resourcePriv().budgetedType() != GrBudgetedType::kUnbudgetedCacheable);
bsalomon84c8e622014-11-17 09:33:27 -0800496 --fBudgetedCount;
497 fBudgetedBytes -= size;
Brian Salomon2c791fc2019-04-02 11:52:03 -0400498 if (!resource->resourcePriv().isPurgeable() && !resource->cacheAccess().hasRef()) {
499 --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) {
516 fProxyProvider->processInvalidUniqueKey(invalidKeyMsgs[i].key(), nullptr,
517 GrProxyProvider::InvalidateGPUResource::kYes);
518 SkASSERT(!this->findAndRefUniqueResource(invalidKeyMsgs[i].key()));
519 }
bsalomon3f324322015-04-08 11:01:54 -0700520 }
bsalomon71cb0c22014-11-14 12:10:14 -0800521
Brian Osman13dddce2017-05-09 13:19:50 -0400522 this->processFreedGpuResources();
523
bsalomon3f324322015-04-08 11:01:54 -0700524 bool stillOverbudget = this->overBudget();
525 while (stillOverbudget && fPurgeableQueue.count()) {
robertphillipsee843b22016-10-04 05:30:20 -0700526 GrGpuResource* resource = fPurgeableQueue.peek();
Brian Salomon614c1a82018-12-19 15:42:06 -0500527 SkASSERT(resource->resourcePriv().isPurgeable());
bsalomon9f2d1572015-02-17 11:47:40 -0800528 resource->cacheAccess().release();
bsalomon3f324322015-04-08 11:01:54 -0700529 stillOverbudget = this->overBudget();
bsalomon9f2d1572015-02-17 11:47:40 -0800530 }
bsalomon71cb0c22014-11-14 12:10:14 -0800531
Robert Phillips12d06a32020-09-16 12:31:34 -0400532 if (stillOverbudget) {
Robert Phillipsd464feb2020-10-08 11:00:02 -0400533 fThreadSafeCache->dropUniqueRefs(this);
Robert Phillips12d06a32020-09-16 12:31:34 -0400534
535 while (stillOverbudget && fPurgeableQueue.count()) {
536 GrGpuResource* resource = fPurgeableQueue.peek();
537 SkASSERT(resource->resourcePriv().isPurgeable());
538 resource->cacheAccess().release();
539 stillOverbudget = this->overBudget();
540 }
541 }
542
bsalomonb436ed62014-11-17 12:15:56 -0800543 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800544}
545
Robert Phillips6eba0632018-03-28 12:25:42 -0400546void GrResourceCache::purgeUnlockedResources(bool scratchResourcesOnly) {
Robert Phillips12d06a32020-09-16 12:31:34 -0400547
Robert Phillips6eba0632018-03-28 12:25:42 -0400548 if (!scratchResourcesOnly) {
Robert Phillipsd464feb2020-10-08 11:00:02 -0400549 fThreadSafeCache->dropUniqueRefs(nullptr);
Robert Phillips331699c2020-09-22 15:20:01 -0400550
Robert Phillips6eba0632018-03-28 12:25:42 -0400551 // We could disable maintaining the heap property here, but it would add a lot of
552 // complexity. Moreover, this is rarely called.
553 while (fPurgeableQueue.count()) {
554 GrGpuResource* resource = fPurgeableQueue.peek();
Brian Salomon614c1a82018-12-19 15:42:06 -0500555 SkASSERT(resource->resourcePriv().isPurgeable());
Robert Phillips6eba0632018-03-28 12:25:42 -0400556 resource->cacheAccess().release();
557 }
558 } else {
559 // Sort the queue
560 fPurgeableQueue.sort();
561
562 // Make a list of the scratch resources to delete
563 SkTDArray<GrGpuResource*> scratchResources;
564 for (int i = 0; i < fPurgeableQueue.count(); i++) {
565 GrGpuResource* resource = fPurgeableQueue.at(i);
Brian Salomon614c1a82018-12-19 15:42:06 -0500566 SkASSERT(resource->resourcePriv().isPurgeable());
Robert Phillips6eba0632018-03-28 12:25:42 -0400567 if (!resource->getUniqueKey().isValid()) {
568 *scratchResources.append() = resource;
569 }
570 }
571
572 // Delete the scratch resources. This must be done as a separate pass
573 // to avoid messing up the sorted order of the queue
574 for (int i = 0; i < scratchResources.count(); i++) {
575 scratchResources.getAt(i)->cacheAccess().release();
576 }
bsalomon9f2d1572015-02-17 11:47:40 -0800577 }
bsalomon71cb0c22014-11-14 12:10:14 -0800578
bsalomonb436ed62014-11-17 12:15:56 -0800579 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800580}
581
Brian Salomon5e150852017-03-22 14:53:13 -0400582void GrResourceCache::purgeResourcesNotUsedSince(GrStdSteadyClock::time_point purgeTime) {
Robert Phillipsd464feb2020-10-08 11:00:02 -0400583 fThreadSafeCache->dropUniqueRefsOlderThan(purgeTime);
Robert Phillipsc2fe1642020-09-22 17:34:51 -0400584
Brian Salomon5e150852017-03-22 14:53:13 -0400585 while (fPurgeableQueue.count()) {
586 const GrStdSteadyClock::time_point resourceTime =
587 fPurgeableQueue.peek()->cacheAccess().timeWhenResourceBecamePurgeable();
588 if (resourceTime >= purgeTime) {
589 // Resources were given both LRU timestamps and tagged with a frame number when
590 // they first became purgeable. The LRU timestamp won't change again until the
591 // resource is made non-purgeable again. So, at this point all the remaining
592 // resources in the timestamp-sorted queue will have a frame number >= to this
593 // one.
594 break;
595 }
596 GrGpuResource* resource = fPurgeableQueue.peek();
Brian Salomon614c1a82018-12-19 15:42:06 -0500597 SkASSERT(resource->resourcePriv().isPurgeable());
Brian Salomon5e150852017-03-22 14:53:13 -0400598 resource->cacheAccess().release();
599 }
600}
601
Derek Sollenberger5480a182017-05-25 16:43:59 -0400602void GrResourceCache::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
603
Brian Osman788b9162020-02-07 10:36:46 -0500604 const size_t tmpByteBudget = std::max((size_t)0, fBytes - bytesToPurge);
Derek Sollenberger5480a182017-05-25 16:43:59 -0400605 bool stillOverbudget = tmpByteBudget < fBytes;
606
607 if (preferScratchResources && bytesToPurge < fPurgeableBytes) {
608 // Sort the queue
609 fPurgeableQueue.sort();
610
611 // Make a list of the scratch resources to delete
612 SkTDArray<GrGpuResource*> scratchResources;
613 size_t scratchByteCount = 0;
614 for (int i = 0; i < fPurgeableQueue.count() && stillOverbudget; i++) {
615 GrGpuResource* resource = fPurgeableQueue.at(i);
Brian Salomon614c1a82018-12-19 15:42:06 -0500616 SkASSERT(resource->resourcePriv().isPurgeable());
Derek Sollenberger5480a182017-05-25 16:43:59 -0400617 if (!resource->getUniqueKey().isValid()) {
618 *scratchResources.append() = resource;
619 scratchByteCount += resource->gpuMemorySize();
620 stillOverbudget = tmpByteBudget < fBytes - scratchByteCount;
621 }
622 }
623
624 // Delete the scratch resources. This must be done as a separate pass
625 // to avoid messing up the sorted order of the queue
626 for (int i = 0; i < scratchResources.count(); i++) {
627 scratchResources.getAt(i)->cacheAccess().release();
628 }
629 stillOverbudget = tmpByteBudget < fBytes;
630
631 this->validate();
632 }
633
634 // Purge any remaining resources in LRU order
635 if (stillOverbudget) {
636 const size_t cachedByteCount = fMaxBytes;
637 fMaxBytes = tmpByteBudget;
638 this->purgeAsNeeded();
639 fMaxBytes = cachedByteCount;
640 }
641}
Robert Phillips12d06a32020-09-16 12:31:34 -0400642
Brian Salomon8cefa3e2019-04-04 11:39:55 -0400643bool GrResourceCache::requestsFlush() const {
644 return this->overBudget() && !fPurgeableQueue.count() &&
645 fNumBudgetedResourcesFlushWillMakePurgeable > 0;
646}
647
Robert Phillipsddc21482019-10-16 14:30:09 -0400648void GrResourceCache::insertDelayedTextureUnref(GrTexture* texture) {
649 texture->ref();
650 uint32_t id = texture->uniqueID().asUInt();
651 if (auto* data = fTexturesAwaitingUnref.find(id)) {
Brian Salomon876a0172019-03-08 11:12:14 -0500652 data->addRef();
653 } else {
Robert Phillipsddc21482019-10-16 14:30:09 -0400654 fTexturesAwaitingUnref.set(id, {texture});
Brian Salomon876a0172019-03-08 11:12:14 -0500655 }
Brian Osman13dddce2017-05-09 13:19:50 -0400656}
657
658void GrResourceCache::processFreedGpuResources() {
Robert Phillips1dfc77c2019-10-16 16:39:45 -0400659 if (!fTexturesAwaitingUnref.count()) {
Robert Phillips12d06a32020-09-16 12:31:34 -0400660 return;
Robert Phillips1dfc77c2019-10-16 16:39:45 -0400661 }
662
Robert Phillipsddc21482019-10-16 14:30:09 -0400663 SkTArray<GrTextureFreedMessage> msgs;
664 fFreedTextureInbox.poll(&msgs);
Brian Osman13dddce2017-05-09 13:19:50 -0400665 for (int i = 0; i < msgs.count(); ++i) {
Brian Salomon238069b2018-07-11 15:58:57 -0400666 SkASSERT(msgs[i].fOwningUniqueID == fContextUniqueID);
Robert Phillipsddc21482019-10-16 14:30:09 -0400667 uint32_t id = msgs[i].fTexture->uniqueID().asUInt();
668 TextureAwaitingUnref* info = fTexturesAwaitingUnref.find(id);
Greg Daniel1a5d2d52019-12-04 11:14:29 -0500669 // If the GrContext was released or abandoned then fTexturesAwaitingUnref should have been
670 // empty and we would have returned early above. Thus, any texture from a message should be
671 // in the list of fTexturesAwaitingUnref.
672 SkASSERT(info);
673 info->unref();
674 if (info->finished()) {
675 fTexturesAwaitingUnref.remove(id);
Greg Danielb2acf0a2018-09-12 09:17:11 -0400676 }
Brian Osman13dddce2017-05-09 13:19:50 -0400677 }
678}
679
bsalomonf320e042015-02-17 15:09:34 -0800680void GrResourceCache::addToNonpurgeableArray(GrGpuResource* resource) {
681 int index = fNonpurgeableResources.count();
682 *fNonpurgeableResources.append() = resource;
683 *resource->cacheAccess().accessCacheIndex() = index;
684}
685
686void GrResourceCache::removeFromNonpurgeableArray(GrGpuResource* resource) {
687 int* index = resource->cacheAccess().accessCacheIndex();
688 // Fill the whole we will create in the array with the tail object, adjust its index, and
689 // then pop the array
690 GrGpuResource* tail = *(fNonpurgeableResources.end() - 1);
691 SkASSERT(fNonpurgeableResources[*index] == resource);
692 fNonpurgeableResources[*index] = tail;
693 *tail->cacheAccess().accessCacheIndex() = *index;
694 fNonpurgeableResources.pop();
695 SkDEBUGCODE(*index = -1);
696}
697
bsalomonddf30e62015-02-19 11:38:44 -0800698uint32_t GrResourceCache::getNextTimestamp() {
699 // If we wrap then all the existing resources will appear older than any resources that get
700 // a timestamp after the wrap.
701 if (0 == fTimestamp) {
702 int count = this->getResourceCount();
703 if (count) {
704 // Reset all the timestamps. We sort the resources by timestamp and then assign
705 // sequential timestamps beginning with 0. This is O(n*lg(n)) but it should be extremely
706 // rare.
707 SkTDArray<GrGpuResource*> sortedPurgeableResources;
708 sortedPurgeableResources.setReserve(fPurgeableQueue.count());
709
710 while (fPurgeableQueue.count()) {
711 *sortedPurgeableResources.append() = fPurgeableQueue.peek();
712 fPurgeableQueue.pop();
713 }
robertphillipsee843b22016-10-04 05:30:20 -0700714
John Stiles886a9042020-07-14 16:28:33 -0400715 SkTQSort(fNonpurgeableResources.begin(), fNonpurgeableResources.end(),
John Stiles6e9ead92020-07-14 00:13:51 +0000716 CompareTimestamp);
bsalomonddf30e62015-02-19 11:38:44 -0800717
718 // Pick resources out of the purgeable and non-purgeable arrays based on lowest
719 // timestamp and assign new timestamps.
720 int currP = 0;
721 int currNP = 0;
722 while (currP < sortedPurgeableResources.count() &&
mtklein56da0252015-11-16 11:16:23 -0800723 currNP < fNonpurgeableResources.count()) {
bsalomonddf30e62015-02-19 11:38:44 -0800724 uint32_t tsP = sortedPurgeableResources[currP]->cacheAccess().timestamp();
725 uint32_t tsNP = fNonpurgeableResources[currNP]->cacheAccess().timestamp();
726 SkASSERT(tsP != tsNP);
727 if (tsP < tsNP) {
728 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
729 } else {
730 // Correct the index in the nonpurgeable array stored on the resource post-sort.
731 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
732 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
733 }
734 }
735
736 // The above loop ended when we hit the end of one array. Finish the other one.
737 while (currP < sortedPurgeableResources.count()) {
738 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
739 }
740 while (currNP < fNonpurgeableResources.count()) {
741 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
742 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
743 }
744
745 // Rebuild the queue.
746 for (int i = 0; i < sortedPurgeableResources.count(); ++i) {
747 fPurgeableQueue.insert(sortedPurgeableResources[i]);
748 }
749
750 this->validate();
751 SkASSERT(count == this->getResourceCount());
752
753 // count should be the next timestamp we return.
754 SkASSERT(fTimestamp == SkToU32(count));
mtklein56da0252015-11-16 11:16:23 -0800755 }
bsalomonddf30e62015-02-19 11:38:44 -0800756 }
757 return fTimestamp++;
758}
759
ericrk0a5fa482015-09-15 14:16:10 -0700760void GrResourceCache::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
761 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
762 fNonpurgeableResources[i]->dumpMemoryStatistics(traceMemoryDump);
763 }
764 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
765 fPurgeableQueue.at(i)->dumpMemoryStatistics(traceMemoryDump);
766 }
767}
768
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500769#if GR_CACHE_STATS
770void GrResourceCache::getStats(Stats* stats) const {
771 stats->reset();
772
773 stats->fTotal = this->getResourceCount();
774 stats->fNumNonPurgeable = fNonpurgeableResources.count();
775 stats->fNumPurgeable = fPurgeableQueue.count();
776
777 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
778 stats->update(fNonpurgeableResources[i]);
779 }
780 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
781 stats->update(fPurgeableQueue.at(i));
782 }
783}
784
785#if GR_TEST_UTILS
786void GrResourceCache::dumpStats(SkString* out) const {
787 this->validate();
788
789 Stats stats;
790
791 this->getStats(&stats);
792
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500793 float byteUtilization = (100.f * fBudgetedBytes) / fMaxBytes;
794
Robert Phillipscf39f372019-09-03 10:29:20 -0400795 out->appendf("Budget: %d bytes\n", (int)fMaxBytes);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500796 out->appendf("\t\tEntry Count: current %d"
Robert Phillipscf39f372019-09-03 10:29:20 -0400797 " (%d budgeted, %d wrapped, %d locked, %d scratch), high %d\n",
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500798 stats.fTotal, fBudgetedCount, stats.fWrapped, stats.fNumNonPurgeable,
Robert Phillipscf39f372019-09-03 10:29:20 -0400799 stats.fScratch, fHighWaterCount);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500800 out->appendf("\t\tEntry Bytes: current %d (budgeted %d, %.2g%% full, %d unbudgeted) high %d\n",
801 SkToInt(fBytes), SkToInt(fBudgetedBytes), byteUtilization,
802 SkToInt(stats.fUnbudgetedSize), SkToInt(fHighWaterBytes));
803}
804
805void GrResourceCache::dumpStatsKeyValuePairs(SkTArray<SkString>* keys,
806 SkTArray<double>* values) const {
807 this->validate();
808
809 Stats stats;
810 this->getStats(&stats);
811
812 keys->push_back(SkString("gpu_cache_purgable_entries")); values->push_back(stats.fNumPurgeable);
813}
814#endif
815
816#endif
817
bsalomon71cb0c22014-11-14 12:10:14 -0800818#ifdef SK_DEBUG
bsalomon0ea80f42015-02-11 10:49:59 -0800819void GrResourceCache::validate() const {
bsalomonc2f35b72015-01-23 07:19:22 -0800820 // Reduce the frequency of validations for large resource counts.
821 static SkRandom gRandom;
822 int mask = (SkNextPow2(fCount + 1) >> 5) - 1;
823 if (~mask && (gRandom.nextU() & mask)) {
824 return;
825 }
826
bsalomonf320e042015-02-17 15:09:34 -0800827 struct Stats {
828 size_t fBytes;
829 int fBudgetedCount;
830 size_t fBudgetedBytes;
831 int fLocked;
832 int fScratch;
833 int fCouldBeScratch;
834 int fContent;
835 const ScratchMap* fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800836 const UniqueHash* fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800837
bsalomonf320e042015-02-17 15:09:34 -0800838 Stats(const GrResourceCache* cache) {
839 memset(this, 0, sizeof(*this));
840 fScratchMap = &cache->fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800841 fUniqueHash = &cache->fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800842 }
843
bsalomonf320e042015-02-17 15:09:34 -0800844 void update(GrGpuResource* resource) {
845 fBytes += resource->gpuMemorySize();
bsalomondace19e2014-11-17 07:34:06 -0800846
Brian Salomon614c1a82018-12-19 15:42:06 -0500847 if (!resource->resourcePriv().isPurgeable()) {
bsalomonf320e042015-02-17 15:09:34 -0800848 ++fLocked;
849 }
bsalomon9f2d1572015-02-17 11:47:40 -0800850
robertphillipsc4ed6842016-05-24 14:17:12 -0700851 const GrScratchKey& scratchKey = resource->resourcePriv().getScratchKey();
852 const GrUniqueKey& uniqueKey = resource->getUniqueKey();
853
bsalomonf320e042015-02-17 15:09:34 -0800854 if (resource->cacheAccess().isScratch()) {
robertphillipsc4ed6842016-05-24 14:17:12 -0700855 SkASSERT(!uniqueKey.isValid());
bsalomonf320e042015-02-17 15:09:34 -0800856 ++fScratch;
robertphillipsc4ed6842016-05-24 14:17:12 -0700857 SkASSERT(fScratchMap->countForKey(scratchKey));
kkinnunen2e6055b2016-04-22 01:48:29 -0700858 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
robertphillipsc4ed6842016-05-24 14:17:12 -0700859 } else if (scratchKey.isValid()) {
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500860 SkASSERT(GrBudgetedType::kBudgeted != resource->resourcePriv().budgetedType() ||
robertphillipsc4ed6842016-05-24 14:17:12 -0700861 uniqueKey.isValid());
862 if (!uniqueKey.isValid()) {
mtklein4e976072016-08-08 09:06:27 -0700863 ++fCouldBeScratch;
robertphillipsc4ed6842016-05-24 14:17:12 -0700864 SkASSERT(fScratchMap->countForKey(scratchKey));
865 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700866 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
bsalomonf320e042015-02-17 15:09:34 -0800867 }
bsalomon8718aaf2015-02-19 07:24:21 -0800868 if (uniqueKey.isValid()) {
bsalomonf320e042015-02-17 15:09:34 -0800869 ++fContent;
bsalomon8718aaf2015-02-19 07:24:21 -0800870 SkASSERT(fUniqueHash->find(uniqueKey) == resource);
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500871 SkASSERT(GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType() ||
Brian Osman0562eb92017-05-08 11:16:39 -0400872 resource->resourcePriv().refsWrappedObjects());
robertphillipsc4ed6842016-05-24 14:17:12 -0700873
874 if (scratchKey.isValid()) {
875 SkASSERT(!fScratchMap->has(resource, scratchKey));
876 }
bsalomonf320e042015-02-17 15:09:34 -0800877 }
878
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500879 if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
bsalomonf320e042015-02-17 15:09:34 -0800880 ++fBudgetedCount;
881 fBudgetedBytes += resource->gpuMemorySize();
882 }
bsalomon9f2d1572015-02-17 11:47:40 -0800883 }
bsalomonf320e042015-02-17 15:09:34 -0800884 };
885
robertphillipsc4ed6842016-05-24 14:17:12 -0700886 {
robertphillipsc4ed6842016-05-24 14:17:12 -0700887 int count = 0;
Mike Kleincff63962020-03-14 16:22:45 -0500888 fScratchMap.foreach([&](const GrGpuResource& resource) {
889 SkASSERT(resource.resourcePriv().getScratchKey().isValid());
890 SkASSERT(!resource.getUniqueKey().isValid());
robertphillipsc4ed6842016-05-24 14:17:12 -0700891 count++;
Mike Kleincff63962020-03-14 16:22:45 -0500892 });
893 SkASSERT(count == fScratchMap.count());
robertphillipsc4ed6842016-05-24 14:17:12 -0700894 }
895
bsalomonf320e042015-02-17 15:09:34 -0800896 Stats stats(this);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400897 size_t purgeableBytes = 0;
Brian Salomon2c791fc2019-04-02 11:52:03 -0400898 int numBudgetedResourcesFlushWillMakePurgeable = 0;
bsalomonf320e042015-02-17 15:09:34 -0800899
900 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500901 SkASSERT(!fNonpurgeableResources[i]->resourcePriv().isPurgeable() ||
bsalomon3f324322015-04-08 11:01:54 -0700902 fNewlyPurgeableResourceForValidation == fNonpurgeableResources[i]);
bsalomonf320e042015-02-17 15:09:34 -0800903 SkASSERT(*fNonpurgeableResources[i]->cacheAccess().accessCacheIndex() == i);
904 SkASSERT(!fNonpurgeableResources[i]->wasDestroyed());
Brian Salomon2c791fc2019-04-02 11:52:03 -0400905 if (fNonpurgeableResources[i]->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted &&
906 !fNonpurgeableResources[i]->cacheAccess().hasRef() &&
907 fNewlyPurgeableResourceForValidation != fNonpurgeableResources[i]) {
Brian Salomon2c791fc2019-04-02 11:52:03 -0400908 ++numBudgetedResourcesFlushWillMakePurgeable;
909 }
bsalomonf320e042015-02-17 15:09:34 -0800910 stats.update(fNonpurgeableResources[i]);
bsalomon71cb0c22014-11-14 12:10:14 -0800911 }
bsalomon9f2d1572015-02-17 11:47:40 -0800912 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500913 SkASSERT(fPurgeableQueue.at(i)->resourcePriv().isPurgeable());
bsalomonf320e042015-02-17 15:09:34 -0800914 SkASSERT(*fPurgeableQueue.at(i)->cacheAccess().accessCacheIndex() == i);
915 SkASSERT(!fPurgeableQueue.at(i)->wasDestroyed());
916 stats.update(fPurgeableQueue.at(i));
Derek Sollenbergeree479142017-05-24 11:41:33 -0400917 purgeableBytes += fPurgeableQueue.at(i)->gpuMemorySize();
bsalomon9f2d1572015-02-17 11:47:40 -0800918 }
919
bsalomonf320e042015-02-17 15:09:34 -0800920 SkASSERT(fCount == this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800921 SkASSERT(fBudgetedCount <= fCount);
bsalomonf320e042015-02-17 15:09:34 -0800922 SkASSERT(fBudgetedBytes <= fBytes);
923 SkASSERT(stats.fBytes == fBytes);
Brian Salomon2c791fc2019-04-02 11:52:03 -0400924 SkASSERT(fNumBudgetedResourcesFlushWillMakePurgeable ==
925 numBudgetedResourcesFlushWillMakePurgeable);
bsalomonf320e042015-02-17 15:09:34 -0800926 SkASSERT(stats.fBudgetedBytes == fBudgetedBytes);
927 SkASSERT(stats.fBudgetedCount == fBudgetedCount);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400928 SkASSERT(purgeableBytes == fPurgeableBytes);
bsalomon71cb0c22014-11-14 12:10:14 -0800929#if GR_CACHE_STATS
bsalomondace19e2014-11-17 07:34:06 -0800930 SkASSERT(fBudgetedHighWaterCount <= fHighWaterCount);
931 SkASSERT(fBudgetedHighWaterBytes <= fHighWaterBytes);
bsalomonf320e042015-02-17 15:09:34 -0800932 SkASSERT(fBytes <= fHighWaterBytes);
933 SkASSERT(fCount <= fHighWaterCount);
934 SkASSERT(fBudgetedBytes <= fBudgetedHighWaterBytes);
935 SkASSERT(fBudgetedCount <= fBudgetedHighWaterCount);
bsalomon71cb0c22014-11-14 12:10:14 -0800936#endif
bsalomon8718aaf2015-02-19 07:24:21 -0800937 SkASSERT(stats.fContent == fUniqueHash.count());
bsalomonf320e042015-02-17 15:09:34 -0800938 SkASSERT(stats.fScratch + stats.fCouldBeScratch == fScratchMap.count());
bsalomon71cb0c22014-11-14 12:10:14 -0800939
bsalomon3f324322015-04-08 11:01:54 -0700940 // This assertion is not currently valid because we can be in recursive notifyCntReachedZero()
bsalomon12299ab2014-11-14 13:33:09 -0800941 // calls. This will be fixed when subresource registration is explicit.
bsalomondace19e2014-11-17 07:34:06 -0800942 // bool overBudget = budgetedBytes > fMaxBytes || budgetedCount > fMaxCount;
bsalomon12299ab2014-11-14 13:33:09 -0800943 // SkASSERT(!overBudget || locked == count || fPurging);
bsalomon71cb0c22014-11-14 12:10:14 -0800944}
bsalomonf320e042015-02-17 15:09:34 -0800945
946bool GrResourceCache::isInCache(const GrGpuResource* resource) const {
947 int index = *resource->cacheAccess().accessCacheIndex();
948 if (index < 0) {
949 return false;
950 }
951 if (index < fPurgeableQueue.count() && fPurgeableQueue.at(index) == resource) {
952 return true;
953 }
954 if (index < fNonpurgeableResources.count() && fNonpurgeableResources[index] == resource) {
955 return true;
956 }
957 SkDEBUGFAIL("Resource index should be -1 or the resource should be in the cache.");
958 return false;
959}
960
bsalomon71cb0c22014-11-14 12:10:14 -0800961#endif