blob: 0a074e9ae488fdfa5b3063555efb60afbcd176fb [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 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
Brian Salomon876a0172019-03-08 11:12:14 -0500204 // We need to make sure to free any resources that were waiting on a free message but never
205 // received one.
Robert Phillipsddc21482019-10-16 14:30:09 -0400206 fTexturesAwaitingUnref.reset();
Greg Danielb2acf0a2018-09-12 09:17:11 -0400207
bsalomonf320e042015-02-17 15:09:34 -0800208 while (fNonpurgeableResources.count()) {
209 GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
210 SkASSERT(!back->wasDestroyed());
211 back->cacheAccess().abandon();
bsalomonc8dc1f72014-08-21 13:02:13 -0700212 }
bsalomonf320e042015-02-17 15:09:34 -0800213
214 while (fPurgeableQueue.count()) {
215 GrGpuResource* top = fPurgeableQueue.peek();
216 SkASSERT(!top->wasDestroyed());
217 top->cacheAccess().abandon();
218 }
219
Robert Phillipseb999bc2020-11-03 08:41:47 -0500220 fThreadSafeCache->dropAllRefs();
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) {
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 &&
912 !fNonpurgeableResources[i]->cacheAccess().hasRef() &&
913 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