blob: cdc0e21f24bb8c1920fd547016c4a9bc8e98ac6e [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>
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/gpu/GrContext.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"
17#include "src/core/SkTSort.h"
18#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"
24#include "src/gpu/GrTracing.h"
25#include "src/gpu/SkGr.h"
bsalomon71cb0c22014-11-14 12:10:14 -080026
bsalomon8718aaf2015-02-19 07:24:21 -080027DECLARE_SKMESSAGEBUS_MESSAGE(GrUniqueKeyInvalidatedMessage);
bsalomon71cb0c22014-11-14 12:10:14 -080028
Robert Phillipsddc21482019-10-16 14:30:09 -040029DECLARE_SKMESSAGEBUS_MESSAGE(GrTextureFreedMessage);
Brian Osman13dddce2017-05-09 13:19:50 -040030
Brian Salomon8f8995a2018-10-15 14:32:15 -040031#define ASSERT_SINGLE_OWNER \
32 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);)
33
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
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
bsalomon744998e2014-08-28 09:54:34 -0700220 SkASSERT(!fScratchMap.count());
bsalomon8718aaf2015-02-19 07:24:21 -0800221 SkASSERT(!fUniqueHash.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700222 SkASSERT(!fCount);
bsalomonf320e042015-02-17 15:09:34 -0800223 SkASSERT(!this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800224 SkASSERT(!fBytes);
225 SkASSERT(!fBudgetedCount);
226 SkASSERT(!fBudgetedBytes);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400227 SkASSERT(!fPurgeableBytes);
Robert Phillipsddc21482019-10-16 14:30:09 -0400228 SkASSERT(!fTexturesAwaitingUnref.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700229}
230
bsalomon0ea80f42015-02-11 10:49:59 -0800231void GrResourceCache::releaseAll() {
bsalomon71cb0c22014-11-14 12:10:14 -0800232 AutoValidate av(this);
233
Brian Osman13dddce2017-05-09 13:19:50 -0400234 this->processFreedGpuResources();
235
Greg Danielc27eb722018-08-10 09:48:08 -0400236 // We need to make sure to free any resources that were waiting on a free message but never
237 // received one.
Robert Phillipsddc21482019-10-16 14:30:09 -0400238 fTexturesAwaitingUnref.reset();
Greg Danielc27eb722018-08-10 09:48:08 -0400239
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500240 SkASSERT(fProxyProvider); // better have called setProxyProvider
Robert Phillips3ec95732017-09-29 15:10:39 -0400241 // We must remove the uniqueKeys from the proxies here. While they possess a uniqueKey
242 // they also have a raw pointer back to this class (which is presumably going away)!
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500243 fProxyProvider->removeAllUniqueKeys();
Robert Phillips45a6f142017-09-29 09:49:41 -0400244
Brian Salomon614c1a82018-12-19 15:42:06 -0500245 while (fNonpurgeableResources.count()) {
bsalomonf320e042015-02-17 15:09:34 -0800246 GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
247 SkASSERT(!back->wasDestroyed());
248 back->cacheAccess().release();
bsalomonc8dc1f72014-08-21 13:02:13 -0700249 }
bsalomonf320e042015-02-17 15:09:34 -0800250
251 while (fPurgeableQueue.count()) {
252 GrGpuResource* top = fPurgeableQueue.peek();
253 SkASSERT(!top->wasDestroyed());
254 top->cacheAccess().release();
255 }
256
bsalomon744998e2014-08-28 09:54:34 -0700257 SkASSERT(!fScratchMap.count());
bsalomon8718aaf2015-02-19 07:24:21 -0800258 SkASSERT(!fUniqueHash.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700259 SkASSERT(!fCount);
bsalomonf320e042015-02-17 15:09:34 -0800260 SkASSERT(!this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800261 SkASSERT(!fBytes);
262 SkASSERT(!fBudgetedCount);
263 SkASSERT(!fBudgetedBytes);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400264 SkASSERT(!fPurgeableBytes);
Robert Phillipsddc21482019-10-16 14:30:09 -0400265 SkASSERT(!fTexturesAwaitingUnref.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700266}
bsalomonbcf0a522014-10-08 08:40:09 -0700267
Brian Salomon2c791fc2019-04-02 11:52:03 -0400268void GrResourceCache::refResource(GrGpuResource* resource) {
269 SkASSERT(resource);
270 SkASSERT(resource->getContext()->priv().getResourceCache() == this);
271 if (resource->cacheAccess().hasRef()) {
272 resource->ref();
273 } else {
274 this->refAndMakeResourceMRU(resource);
275 }
276 this->validate();
277}
278
bsalomon0ea80f42015-02-11 10:49:59 -0800279class GrResourceCache::AvailableForScratchUse {
bsalomonbcf0a522014-10-08 08:40:09 -0700280public:
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400281 AvailableForScratchUse() { }
bsalomonbcf0a522014-10-08 08:40:09 -0700282
283 bool operator()(const GrGpuResource* resource) const {
robertphillipsc4ed6842016-05-24 14:17:12 -0700284 SkASSERT(!resource->getUniqueKey().isValid() &&
285 resource->resourcePriv().getScratchKey().isValid());
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400286
Robert Phillipsaee18c92019-09-06 11:48:27 -0400287 // isScratch() also tests that the resource is budgeted.
bsalomon12299ab2014-11-14 13:33:09 -0800288 if (resource->internalHasRef() || !resource->cacheAccess().isScratch()) {
bsalomon000f8292014-10-15 19:04:14 -0700289 return false;
bsalomonbcf0a522014-10-08 08:40:09 -0700290 }
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400291 return true;
bsalomonbcf0a522014-10-08 08:40:09 -0700292 }
bsalomonbcf0a522014-10-08 08:40:09 -0700293};
294
Robert Phillipsaee18c92019-09-06 11:48:27 -0400295GrGpuResource* GrResourceCache::findAndRefScratchResource(const GrScratchKey& scratchKey) {
bsalomon7775c852014-12-30 12:50:52 -0800296 SkASSERT(scratchKey.isValid());
robertphillipsee843b22016-10-04 05:30:20 -0700297
Robert Phillipsaee18c92019-09-06 11:48:27 -0400298 GrGpuResource* resource = fScratchMap.find(scratchKey, AvailableForScratchUse());
bsalomon71cb0c22014-11-14 12:10:14 -0800299 if (resource) {
bsalomon9f2d1572015-02-17 11:47:40 -0800300 this->refAndMakeResourceMRU(resource);
bsalomonb436ed62014-11-17 12:15:56 -0800301 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800302 }
303 return resource;
bsalomonbcf0a522014-10-08 08:40:09 -0700304}
bsalomon8b79d232014-11-10 10:19:06 -0800305
bsalomon0ea80f42015-02-11 10:49:59 -0800306void GrResourceCache::willRemoveScratchKey(const GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400307 ASSERT_SINGLE_OWNER
bsalomon3582d3e2015-02-13 14:20:05 -0800308 SkASSERT(resource->resourcePriv().getScratchKey().isValid());
robertphillipsc4ed6842016-05-24 14:17:12 -0700309 if (!resource->getUniqueKey().isValid()) {
310 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
311 }
bsalomon10e23ca2014-11-25 05:52:06 -0800312}
313
bsalomonf99e9612015-02-19 08:24:16 -0800314void GrResourceCache::removeUniqueKey(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400315 ASSERT_SINGLE_OWNER
bsalomon3f324322015-04-08 11:01:54 -0700316 // Someone has a ref to this resource in order to have removed the key. When the ref count
317 // reaches zero we will get a ref cnt notification and figure out what to do with it.
bsalomonf99e9612015-02-19 08:24:16 -0800318 if (resource->getUniqueKey().isValid()) {
319 SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
320 fUniqueHash.remove(resource->getUniqueKey());
321 }
322 resource->cacheAccess().removeUniqueKey();
robertphillipsc4ed6842016-05-24 14:17:12 -0700323 if (resource->resourcePriv().getScratchKey().isValid()) {
324 fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
325 }
326
Brian Salomon9bc76d92019-01-24 12:18:33 -0500327 // Removing a unique key from a kUnbudgetedCacheable resource would make the resource
328 // require purging. However, the resource must be ref'ed to get here and therefore can't
329 // be purgeable. We'll purge it when the refs reach zero.
330 SkASSERT(!resource->resourcePriv().isPurgeable());
bsalomonf99e9612015-02-19 08:24:16 -0800331 this->validate();
bsalomon23e619c2015-02-06 11:54:28 -0800332}
333
bsalomonf99e9612015-02-19 08:24:16 -0800334void GrResourceCache::changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400335 ASSERT_SINGLE_OWNER
bsalomon8b79d232014-11-10 10:19:06 -0800336 SkASSERT(resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800337 SkASSERT(this->isInCache(resource));
bsalomon8b79d232014-11-10 10:19:06 -0800338
bsalomonf99e9612015-02-19 08:24:16 -0800339 // If another resource has the new key, remove its key then install the key on this resource.
340 if (newKey.isValid()) {
Greg Daniel0d537802017-09-08 11:44:14 -0400341 if (GrGpuResource* old = fUniqueHash.find(newKey)) {
342 // If the old resource using the key is purgeable and is unreachable, then remove it.
Brian Salomon614c1a82018-12-19 15:42:06 -0500343 if (!old->resourcePriv().getScratchKey().isValid() &&
344 old->resourcePriv().isPurgeable()) {
Greg Daniel0d537802017-09-08 11:44:14 -0400345 old->cacheAccess().release();
346 } else {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500347 // removeUniqueKey expects an external owner of the resource.
348 this->removeUniqueKey(sk_ref_sp(old).get());
Greg Daniel0d537802017-09-08 11:44:14 -0400349 }
350 }
351 SkASSERT(nullptr == fUniqueHash.find(newKey));
352
robertphillipsc4ed6842016-05-24 14:17:12 -0700353 // Remove the entry for this resource if it already has a unique key.
354 if (resource->getUniqueKey().isValid()) {
355 SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
356 fUniqueHash.remove(resource->getUniqueKey());
357 SkASSERT(nullptr == fUniqueHash.find(resource->getUniqueKey()));
358 } else {
359 // 'resource' didn't have a valid unique key before so it is switching sides. Remove it
360 // from the ScratchMap
361 if (resource->resourcePriv().getScratchKey().isValid()) {
362 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
363 }
364 }
365
bsalomonf99e9612015-02-19 08:24:16 -0800366 resource->cacheAccess().setUniqueKey(newKey);
367 fUniqueHash.add(resource);
368 } else {
robertphillipsc4ed6842016-05-24 14:17:12 -0700369 this->removeUniqueKey(resource);
bsalomonf99e9612015-02-19 08:24:16 -0800370 }
371
bsalomon71cb0c22014-11-14 12:10:14 -0800372 this->validate();
bsalomon8b79d232014-11-10 10:19:06 -0800373}
bsalomon71cb0c22014-11-14 12:10:14 -0800374
bsalomon9f2d1572015-02-17 11:47:40 -0800375void GrResourceCache::refAndMakeResourceMRU(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400376 ASSERT_SINGLE_OWNER
bsalomon71cb0c22014-11-14 12:10:14 -0800377 SkASSERT(resource);
378 SkASSERT(this->isInCache(resource));
bsalomonddf30e62015-02-19 11:38:44 -0800379
Brian Salomon614c1a82018-12-19 15:42:06 -0500380 if (resource->resourcePriv().isPurgeable()) {
bsalomon9f2d1572015-02-17 11:47:40 -0800381 // It's about to become unpurgeable.
Derek Sollenbergeree479142017-05-24 11:41:33 -0400382 fPurgeableBytes -= resource->gpuMemorySize();
bsalomon9f2d1572015-02-17 11:47:40 -0800383 fPurgeableQueue.remove(resource);
bsalomonf320e042015-02-17 15:09:34 -0800384 this->addToNonpurgeableArray(resource);
Brian Salomon2c791fc2019-04-02 11:52:03 -0400385 } else if (!resource->cacheAccess().hasRef() &&
386 resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
387 SkASSERT(fNumBudgetedResourcesFlushWillMakePurgeable > 0);
388 fNumBudgetedResourcesFlushWillMakePurgeable--;
bsalomon9f2d1572015-02-17 11:47:40 -0800389 }
Brian Salomon01ceae92019-04-02 11:49:54 -0400390 resource->cacheAccess().ref();
bsalomonddf30e62015-02-19 11:38:44 -0800391
392 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
bsalomonf320e042015-02-17 15:09:34 -0800393 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800394}
395
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400396void GrResourceCache::notifyRefCntReachedZero(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400397 ASSERT_SINGLE_OWNER
bsalomon71cb0c22014-11-14 12:10:14 -0800398 SkASSERT(resource);
bsalomon3f324322015-04-08 11:01:54 -0700399 SkASSERT(!resource->wasDestroyed());
bsalomon71cb0c22014-11-14 12:10:14 -0800400 SkASSERT(this->isInCache(resource));
bsalomon3f324322015-04-08 11:01:54 -0700401 // This resource should always be in the nonpurgeable array when this function is called. It
402 // will be moved to the queue if it is newly purgeable.
403 SkASSERT(fNonpurgeableResources[*resource->cacheAccess().accessCacheIndex()] == resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800404
bsalomon3f324322015-04-08 11:01:54 -0700405#ifdef SK_DEBUG
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400406 // When the timestamp overflows validate() is called. validate() checks that resources in
407 // the nonpurgeable array are indeed not purgeable. However, the movement from the array to
408 // the purgeable queue happens just below in this function. So we mark it as an exception.
409 if (resource->resourcePriv().isPurgeable()) {
410 fNewlyPurgeableResourceForValidation = resource;
bsalomon3f324322015-04-08 11:01:54 -0700411 }
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400412#endif
413 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
414 SkDEBUGCODE(fNewlyPurgeableResourceForValidation = nullptr);
bsalomon3f324322015-04-08 11:01:54 -0700415
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400416 if (!resource->resourcePriv().isPurgeable() &&
417 resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
418 ++fNumBudgetedResourcesFlushWillMakePurgeable;
bsalomon3f324322015-04-08 11:01:54 -0700419 }
420
Brian Salomon9bc76d92019-01-24 12:18:33 -0500421 if (!resource->resourcePriv().isPurgeable()) {
422 this->validate();
423 return;
424 }
425
bsalomonf320e042015-02-17 15:09:34 -0800426 this->removeFromNonpurgeableArray(resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800427 fPurgeableQueue.insert(resource);
Brian Salomon5e150852017-03-22 14:53:13 -0400428 resource->cacheAccess().setTimeWhenResourceBecomePurgeable();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400429 fPurgeableBytes += resource->gpuMemorySize();
bsalomon71cb0c22014-11-14 12:10:14 -0800430
Greg Daniel303e83e2018-09-10 14:10:19 -0400431 bool hasUniqueKey = resource->getUniqueKey().isValid();
432
Brian Salomon9bc76d92019-01-24 12:18:33 -0500433 GrBudgetedType budgetedType = resource->resourcePriv().budgetedType();
Brian Salomon614c1a82018-12-19 15:42:06 -0500434
Brian Salomon9bc76d92019-01-24 12:18:33 -0500435 if (budgetedType == GrBudgetedType::kBudgeted) {
436 // Purge the resource immediately if we're over budget
437 // Also purge if the resource has neither a valid scratch key nor a unique key.
438 bool hasKey = resource->resourcePriv().getScratchKey().isValid() || hasUniqueKey;
439 if (!this->overBudget() && hasKey) {
440 return;
441 }
442 } else {
443 // We keep unbudgeted resources with a unique key in the purgeable queue of the cache so
444 // they can be reused again by the image connected to the unique key.
445 if (hasUniqueKey && budgetedType == GrBudgetedType::kUnbudgetedCacheable) {
446 return;
447 }
448 // Check whether this resource could still be used as a scratch resource.
449 if (!resource->resourcePriv().refsWrappedObjects() &&
450 resource->resourcePriv().getScratchKey().isValid()) {
451 // We won't purge an existing resource to make room for this one.
Robert Phillipscf39f372019-09-03 10:29:20 -0400452 if (this->wouldFit(resource->gpuMemorySize())) {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500453 resource->resourcePriv().makeBudgeted();
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500454 return;
455 }
bsalomonc2f35b72015-01-23 07:19:22 -0800456 }
bsalomonc2f35b72015-01-23 07:19:22 -0800457 }
Brian Salomon9bc76d92019-01-24 12:18:33 -0500458
bsalomonf320e042015-02-17 15:09:34 -0800459 SkDEBUGCODE(int beforeCount = this->getResourceCount();)
bsalomon9f2d1572015-02-17 11:47:40 -0800460 resource->cacheAccess().release();
461 // We should at least free this resource, perhaps dependent resources as well.
bsalomonf320e042015-02-17 15:09:34 -0800462 SkASSERT(this->getResourceCount() < beforeCount);
bsalomon71cb0c22014-11-14 12:10:14 -0800463 this->validate();
464}
465
bsalomon0ea80f42015-02-11 10:49:59 -0800466void GrResourceCache::didChangeBudgetStatus(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400467 ASSERT_SINGLE_OWNER
bsalomon84c8e622014-11-17 09:33:27 -0800468 SkASSERT(resource);
469 SkASSERT(this->isInCache(resource));
470
471 size_t size = resource->gpuMemorySize();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500472 // Changing from BudgetedType::kUnbudgetedCacheable to another budgeted type could make
473 // resource become purgeable. However, we should never allow that transition. Wrapped
474 // resources are the only resources that can be in that state and they aren't allowed to
475 // transition from one budgeted state to another.
476 SkDEBUGCODE(bool wasPurgeable = resource->resourcePriv().isPurgeable());
477 if (resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
bsalomon84c8e622014-11-17 09:33:27 -0800478 ++fBudgetedCount;
479 fBudgetedBytes += size;
bsalomonafe30052015-01-16 07:32:33 -0800480#if GR_CACHE_STATS
Brian Osman788b9162020-02-07 10:36:46 -0500481 fBudgetedHighWaterBytes = std::max(fBudgetedBytes, fBudgetedHighWaterBytes);
482 fBudgetedHighWaterCount = std::max(fBudgetedCount, fBudgetedHighWaterCount);
bsalomonafe30052015-01-16 07:32:33 -0800483#endif
Brian Salomon2c791fc2019-04-02 11:52:03 -0400484 if (!resource->resourcePriv().isPurgeable() && !resource->cacheAccess().hasRef()) {
485 ++fNumBudgetedResourcesFlushWillMakePurgeable;
486 }
bsalomon84c8e622014-11-17 09:33:27 -0800487 this->purgeAsNeeded();
488 } else {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500489 SkASSERT(resource->resourcePriv().budgetedType() != GrBudgetedType::kUnbudgetedCacheable);
bsalomon84c8e622014-11-17 09:33:27 -0800490 --fBudgetedCount;
491 fBudgetedBytes -= size;
Brian Salomon2c791fc2019-04-02 11:52:03 -0400492 if (!resource->resourcePriv().isPurgeable() && !resource->cacheAccess().hasRef()) {
493 --fNumBudgetedResourcesFlushWillMakePurgeable;
494 }
bsalomon84c8e622014-11-17 09:33:27 -0800495 }
Brian Salomon9bc76d92019-01-24 12:18:33 -0500496 SkASSERT(wasPurgeable == resource->resourcePriv().isPurgeable());
Brian Osman39c08ac2017-07-26 09:36:09 -0400497 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800498 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomon84c8e622014-11-17 09:33:27 -0800499
500 this->validate();
501}
502
robertphillipsee843b22016-10-04 05:30:20 -0700503void GrResourceCache::purgeAsNeeded() {
bsalomon3f324322015-04-08 11:01:54 -0700504 SkTArray<GrUniqueKeyInvalidatedMessage> invalidKeyMsgs;
505 fInvalidUniqueKeyInbox.poll(&invalidKeyMsgs);
506 if (invalidKeyMsgs.count()) {
Robert Phillips427966a2018-12-20 17:20:43 -0500507 SkASSERT(fProxyProvider);
508
509 for (int i = 0; i < invalidKeyMsgs.count(); ++i) {
510 fProxyProvider->processInvalidUniqueKey(invalidKeyMsgs[i].key(), nullptr,
511 GrProxyProvider::InvalidateGPUResource::kYes);
512 SkASSERT(!this->findAndRefUniqueResource(invalidKeyMsgs[i].key()));
513 }
bsalomon3f324322015-04-08 11:01:54 -0700514 }
bsalomon71cb0c22014-11-14 12:10:14 -0800515
Brian Osman13dddce2017-05-09 13:19:50 -0400516 this->processFreedGpuResources();
517
bsalomon3f324322015-04-08 11:01:54 -0700518 bool stillOverbudget = this->overBudget();
519 while (stillOverbudget && fPurgeableQueue.count()) {
robertphillipsee843b22016-10-04 05:30:20 -0700520 GrGpuResource* resource = fPurgeableQueue.peek();
Brian Salomon614c1a82018-12-19 15:42:06 -0500521 SkASSERT(resource->resourcePriv().isPurgeable());
bsalomon9f2d1572015-02-17 11:47:40 -0800522 resource->cacheAccess().release();
bsalomon3f324322015-04-08 11:01:54 -0700523 stillOverbudget = this->overBudget();
bsalomon9f2d1572015-02-17 11:47:40 -0800524 }
bsalomon71cb0c22014-11-14 12:10:14 -0800525
bsalomonb436ed62014-11-17 12:15:56 -0800526 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800527}
528
Robert Phillips6eba0632018-03-28 12:25:42 -0400529void GrResourceCache::purgeUnlockedResources(bool scratchResourcesOnly) {
530 if (!scratchResourcesOnly) {
531 // We could disable maintaining the heap property here, but it would add a lot of
532 // complexity. Moreover, this is rarely called.
533 while (fPurgeableQueue.count()) {
534 GrGpuResource* resource = fPurgeableQueue.peek();
Brian Salomon614c1a82018-12-19 15:42:06 -0500535 SkASSERT(resource->resourcePriv().isPurgeable());
Robert Phillips6eba0632018-03-28 12:25:42 -0400536 resource->cacheAccess().release();
537 }
538 } else {
539 // Sort the queue
540 fPurgeableQueue.sort();
541
542 // Make a list of the scratch resources to delete
543 SkTDArray<GrGpuResource*> scratchResources;
544 for (int i = 0; i < fPurgeableQueue.count(); i++) {
545 GrGpuResource* resource = fPurgeableQueue.at(i);
Brian Salomon614c1a82018-12-19 15:42:06 -0500546 SkASSERT(resource->resourcePriv().isPurgeable());
Robert Phillips6eba0632018-03-28 12:25:42 -0400547 if (!resource->getUniqueKey().isValid()) {
548 *scratchResources.append() = resource;
549 }
550 }
551
552 // Delete the scratch resources. This must be done as a separate pass
553 // to avoid messing up the sorted order of the queue
554 for (int i = 0; i < scratchResources.count(); i++) {
555 scratchResources.getAt(i)->cacheAccess().release();
556 }
bsalomon9f2d1572015-02-17 11:47:40 -0800557 }
bsalomon71cb0c22014-11-14 12:10:14 -0800558
bsalomonb436ed62014-11-17 12:15:56 -0800559 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800560}
561
Brian Salomon5e150852017-03-22 14:53:13 -0400562void GrResourceCache::purgeResourcesNotUsedSince(GrStdSteadyClock::time_point purgeTime) {
563 while (fPurgeableQueue.count()) {
564 const GrStdSteadyClock::time_point resourceTime =
565 fPurgeableQueue.peek()->cacheAccess().timeWhenResourceBecamePurgeable();
566 if (resourceTime >= purgeTime) {
567 // Resources were given both LRU timestamps and tagged with a frame number when
568 // they first became purgeable. The LRU timestamp won't change again until the
569 // resource is made non-purgeable again. So, at this point all the remaining
570 // resources in the timestamp-sorted queue will have a frame number >= to this
571 // one.
572 break;
573 }
574 GrGpuResource* resource = fPurgeableQueue.peek();
Brian Salomon614c1a82018-12-19 15:42:06 -0500575 SkASSERT(resource->resourcePriv().isPurgeable());
Brian Salomon5e150852017-03-22 14:53:13 -0400576 resource->cacheAccess().release();
577 }
578}
579
Derek Sollenberger5480a182017-05-25 16:43:59 -0400580void GrResourceCache::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
581
Brian Osman788b9162020-02-07 10:36:46 -0500582 const size_t tmpByteBudget = std::max((size_t)0, fBytes - bytesToPurge);
Derek Sollenberger5480a182017-05-25 16:43:59 -0400583 bool stillOverbudget = tmpByteBudget < fBytes;
584
585 if (preferScratchResources && bytesToPurge < fPurgeableBytes) {
586 // Sort the queue
587 fPurgeableQueue.sort();
588
589 // Make a list of the scratch resources to delete
590 SkTDArray<GrGpuResource*> scratchResources;
591 size_t scratchByteCount = 0;
592 for (int i = 0; i < fPurgeableQueue.count() && stillOverbudget; i++) {
593 GrGpuResource* resource = fPurgeableQueue.at(i);
Brian Salomon614c1a82018-12-19 15:42:06 -0500594 SkASSERT(resource->resourcePriv().isPurgeable());
Derek Sollenberger5480a182017-05-25 16:43:59 -0400595 if (!resource->getUniqueKey().isValid()) {
596 *scratchResources.append() = resource;
597 scratchByteCount += resource->gpuMemorySize();
598 stillOverbudget = tmpByteBudget < fBytes - scratchByteCount;
599 }
600 }
601
602 // Delete the scratch resources. This must be done as a separate pass
603 // to avoid messing up the sorted order of the queue
604 for (int i = 0; i < scratchResources.count(); i++) {
605 scratchResources.getAt(i)->cacheAccess().release();
606 }
607 stillOverbudget = tmpByteBudget < fBytes;
608
609 this->validate();
610 }
611
612 // Purge any remaining resources in LRU order
613 if (stillOverbudget) {
614 const size_t cachedByteCount = fMaxBytes;
615 fMaxBytes = tmpByteBudget;
616 this->purgeAsNeeded();
617 fMaxBytes = cachedByteCount;
618 }
619}
Brian Salomon8cefa3e2019-04-04 11:39:55 -0400620bool GrResourceCache::requestsFlush() const {
621 return this->overBudget() && !fPurgeableQueue.count() &&
622 fNumBudgetedResourcesFlushWillMakePurgeable > 0;
623}
624
Derek Sollenberger5480a182017-05-25 16:43:59 -0400625
Robert Phillipsddc21482019-10-16 14:30:09 -0400626void GrResourceCache::insertDelayedTextureUnref(GrTexture* texture) {
627 texture->ref();
628 uint32_t id = texture->uniqueID().asUInt();
629 if (auto* data = fTexturesAwaitingUnref.find(id)) {
Brian Salomon876a0172019-03-08 11:12:14 -0500630 data->addRef();
631 } else {
Robert Phillipsddc21482019-10-16 14:30:09 -0400632 fTexturesAwaitingUnref.set(id, {texture});
Brian Salomon876a0172019-03-08 11:12:14 -0500633 }
Brian Osman13dddce2017-05-09 13:19:50 -0400634}
635
636void GrResourceCache::processFreedGpuResources() {
Robert Phillips1dfc77c2019-10-16 16:39:45 -0400637 if (!fTexturesAwaitingUnref.count()) {
638 return;
639 }
640
Robert Phillipsddc21482019-10-16 14:30:09 -0400641 SkTArray<GrTextureFreedMessage> msgs;
642 fFreedTextureInbox.poll(&msgs);
Brian Osman13dddce2017-05-09 13:19:50 -0400643 for (int i = 0; i < msgs.count(); ++i) {
Brian Salomon238069b2018-07-11 15:58:57 -0400644 SkASSERT(msgs[i].fOwningUniqueID == fContextUniqueID);
Robert Phillipsddc21482019-10-16 14:30:09 -0400645 uint32_t id = msgs[i].fTexture->uniqueID().asUInt();
646 TextureAwaitingUnref* info = fTexturesAwaitingUnref.find(id);
Greg Daniel1a5d2d52019-12-04 11:14:29 -0500647 // If the GrContext was released or abandoned then fTexturesAwaitingUnref should have been
648 // empty and we would have returned early above. Thus, any texture from a message should be
649 // in the list of fTexturesAwaitingUnref.
650 SkASSERT(info);
651 info->unref();
652 if (info->finished()) {
653 fTexturesAwaitingUnref.remove(id);
Greg Danielb2acf0a2018-09-12 09:17:11 -0400654 }
Brian Osman13dddce2017-05-09 13:19:50 -0400655 }
656}
657
bsalomonf320e042015-02-17 15:09:34 -0800658void GrResourceCache::addToNonpurgeableArray(GrGpuResource* resource) {
659 int index = fNonpurgeableResources.count();
660 *fNonpurgeableResources.append() = resource;
661 *resource->cacheAccess().accessCacheIndex() = index;
662}
663
664void GrResourceCache::removeFromNonpurgeableArray(GrGpuResource* resource) {
665 int* index = resource->cacheAccess().accessCacheIndex();
666 // Fill the whole we will create in the array with the tail object, adjust its index, and
667 // then pop the array
668 GrGpuResource* tail = *(fNonpurgeableResources.end() - 1);
669 SkASSERT(fNonpurgeableResources[*index] == resource);
670 fNonpurgeableResources[*index] = tail;
671 *tail->cacheAccess().accessCacheIndex() = *index;
672 fNonpurgeableResources.pop();
673 SkDEBUGCODE(*index = -1);
674}
675
bsalomonddf30e62015-02-19 11:38:44 -0800676uint32_t GrResourceCache::getNextTimestamp() {
677 // If we wrap then all the existing resources will appear older than any resources that get
678 // a timestamp after the wrap.
679 if (0 == fTimestamp) {
680 int count = this->getResourceCount();
681 if (count) {
682 // Reset all the timestamps. We sort the resources by timestamp and then assign
683 // sequential timestamps beginning with 0. This is O(n*lg(n)) but it should be extremely
684 // rare.
685 SkTDArray<GrGpuResource*> sortedPurgeableResources;
686 sortedPurgeableResources.setReserve(fPurgeableQueue.count());
687
688 while (fPurgeableQueue.count()) {
689 *sortedPurgeableResources.append() = fPurgeableQueue.peek();
690 fPurgeableQueue.pop();
691 }
robertphillipsee843b22016-10-04 05:30:20 -0700692
bsalomone2e87f32016-09-22 12:42:11 -0700693 SkTQSort(fNonpurgeableResources.begin(), fNonpurgeableResources.end() - 1,
694 CompareTimestamp);
bsalomonddf30e62015-02-19 11:38:44 -0800695
696 // Pick resources out of the purgeable and non-purgeable arrays based on lowest
697 // timestamp and assign new timestamps.
698 int currP = 0;
699 int currNP = 0;
700 while (currP < sortedPurgeableResources.count() &&
mtklein56da0252015-11-16 11:16:23 -0800701 currNP < fNonpurgeableResources.count()) {
bsalomonddf30e62015-02-19 11:38:44 -0800702 uint32_t tsP = sortedPurgeableResources[currP]->cacheAccess().timestamp();
703 uint32_t tsNP = fNonpurgeableResources[currNP]->cacheAccess().timestamp();
704 SkASSERT(tsP != tsNP);
705 if (tsP < tsNP) {
706 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
707 } else {
708 // Correct the index in the nonpurgeable array stored on the resource post-sort.
709 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
710 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
711 }
712 }
713
714 // The above loop ended when we hit the end of one array. Finish the other one.
715 while (currP < sortedPurgeableResources.count()) {
716 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
717 }
718 while (currNP < fNonpurgeableResources.count()) {
719 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
720 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
721 }
722
723 // Rebuild the queue.
724 for (int i = 0; i < sortedPurgeableResources.count(); ++i) {
725 fPurgeableQueue.insert(sortedPurgeableResources[i]);
726 }
727
728 this->validate();
729 SkASSERT(count == this->getResourceCount());
730
731 // count should be the next timestamp we return.
732 SkASSERT(fTimestamp == SkToU32(count));
mtklein56da0252015-11-16 11:16:23 -0800733 }
bsalomonddf30e62015-02-19 11:38:44 -0800734 }
735 return fTimestamp++;
736}
737
ericrk0a5fa482015-09-15 14:16:10 -0700738void GrResourceCache::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
739 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
740 fNonpurgeableResources[i]->dumpMemoryStatistics(traceMemoryDump);
741 }
742 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
743 fPurgeableQueue.at(i)->dumpMemoryStatistics(traceMemoryDump);
744 }
745}
746
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500747#if GR_CACHE_STATS
748void GrResourceCache::getStats(Stats* stats) const {
749 stats->reset();
750
751 stats->fTotal = this->getResourceCount();
752 stats->fNumNonPurgeable = fNonpurgeableResources.count();
753 stats->fNumPurgeable = fPurgeableQueue.count();
754
755 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
756 stats->update(fNonpurgeableResources[i]);
757 }
758 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
759 stats->update(fPurgeableQueue.at(i));
760 }
761}
762
763#if GR_TEST_UTILS
764void GrResourceCache::dumpStats(SkString* out) const {
765 this->validate();
766
767 Stats stats;
768
769 this->getStats(&stats);
770
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500771 float byteUtilization = (100.f * fBudgetedBytes) / fMaxBytes;
772
Robert Phillipscf39f372019-09-03 10:29:20 -0400773 out->appendf("Budget: %d bytes\n", (int)fMaxBytes);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500774 out->appendf("\t\tEntry Count: current %d"
Robert Phillipscf39f372019-09-03 10:29:20 -0400775 " (%d budgeted, %d wrapped, %d locked, %d scratch), high %d\n",
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500776 stats.fTotal, fBudgetedCount, stats.fWrapped, stats.fNumNonPurgeable,
Robert Phillipscf39f372019-09-03 10:29:20 -0400777 stats.fScratch, fHighWaterCount);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500778 out->appendf("\t\tEntry Bytes: current %d (budgeted %d, %.2g%% full, %d unbudgeted) high %d\n",
779 SkToInt(fBytes), SkToInt(fBudgetedBytes), byteUtilization,
780 SkToInt(stats.fUnbudgetedSize), SkToInt(fHighWaterBytes));
781}
782
783void GrResourceCache::dumpStatsKeyValuePairs(SkTArray<SkString>* keys,
784 SkTArray<double>* values) const {
785 this->validate();
786
787 Stats stats;
788 this->getStats(&stats);
789
790 keys->push_back(SkString("gpu_cache_purgable_entries")); values->push_back(stats.fNumPurgeable);
791}
792#endif
793
794#endif
795
bsalomon71cb0c22014-11-14 12:10:14 -0800796#ifdef SK_DEBUG
bsalomon0ea80f42015-02-11 10:49:59 -0800797void GrResourceCache::validate() const {
bsalomonc2f35b72015-01-23 07:19:22 -0800798 // Reduce the frequency of validations for large resource counts.
799 static SkRandom gRandom;
800 int mask = (SkNextPow2(fCount + 1) >> 5) - 1;
801 if (~mask && (gRandom.nextU() & mask)) {
802 return;
803 }
804
bsalomonf320e042015-02-17 15:09:34 -0800805 struct Stats {
806 size_t fBytes;
807 int fBudgetedCount;
808 size_t fBudgetedBytes;
809 int fLocked;
810 int fScratch;
811 int fCouldBeScratch;
812 int fContent;
813 const ScratchMap* fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800814 const UniqueHash* fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800815
bsalomonf320e042015-02-17 15:09:34 -0800816 Stats(const GrResourceCache* cache) {
817 memset(this, 0, sizeof(*this));
818 fScratchMap = &cache->fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800819 fUniqueHash = &cache->fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800820 }
821
bsalomonf320e042015-02-17 15:09:34 -0800822 void update(GrGpuResource* resource) {
823 fBytes += resource->gpuMemorySize();
bsalomondace19e2014-11-17 07:34:06 -0800824
Brian Salomon614c1a82018-12-19 15:42:06 -0500825 if (!resource->resourcePriv().isPurgeable()) {
bsalomonf320e042015-02-17 15:09:34 -0800826 ++fLocked;
827 }
bsalomon9f2d1572015-02-17 11:47:40 -0800828
robertphillipsc4ed6842016-05-24 14:17:12 -0700829 const GrScratchKey& scratchKey = resource->resourcePriv().getScratchKey();
830 const GrUniqueKey& uniqueKey = resource->getUniqueKey();
831
bsalomonf320e042015-02-17 15:09:34 -0800832 if (resource->cacheAccess().isScratch()) {
robertphillipsc4ed6842016-05-24 14:17:12 -0700833 SkASSERT(!uniqueKey.isValid());
bsalomonf320e042015-02-17 15:09:34 -0800834 ++fScratch;
robertphillipsc4ed6842016-05-24 14:17:12 -0700835 SkASSERT(fScratchMap->countForKey(scratchKey));
kkinnunen2e6055b2016-04-22 01:48:29 -0700836 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
robertphillipsc4ed6842016-05-24 14:17:12 -0700837 } else if (scratchKey.isValid()) {
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500838 SkASSERT(GrBudgetedType::kBudgeted != resource->resourcePriv().budgetedType() ||
robertphillipsc4ed6842016-05-24 14:17:12 -0700839 uniqueKey.isValid());
840 if (!uniqueKey.isValid()) {
mtklein4e976072016-08-08 09:06:27 -0700841 ++fCouldBeScratch;
robertphillipsc4ed6842016-05-24 14:17:12 -0700842 SkASSERT(fScratchMap->countForKey(scratchKey));
843 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700844 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
bsalomonf320e042015-02-17 15:09:34 -0800845 }
bsalomon8718aaf2015-02-19 07:24:21 -0800846 if (uniqueKey.isValid()) {
bsalomonf320e042015-02-17 15:09:34 -0800847 ++fContent;
bsalomon8718aaf2015-02-19 07:24:21 -0800848 SkASSERT(fUniqueHash->find(uniqueKey) == resource);
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500849 SkASSERT(GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType() ||
Brian Osman0562eb92017-05-08 11:16:39 -0400850 resource->resourcePriv().refsWrappedObjects());
robertphillipsc4ed6842016-05-24 14:17:12 -0700851
852 if (scratchKey.isValid()) {
853 SkASSERT(!fScratchMap->has(resource, scratchKey));
854 }
bsalomonf320e042015-02-17 15:09:34 -0800855 }
856
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500857 if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
bsalomonf320e042015-02-17 15:09:34 -0800858 ++fBudgetedCount;
859 fBudgetedBytes += resource->gpuMemorySize();
860 }
bsalomon9f2d1572015-02-17 11:47:40 -0800861 }
bsalomonf320e042015-02-17 15:09:34 -0800862 };
863
robertphillipsc4ed6842016-05-24 14:17:12 -0700864 {
robertphillipsc4ed6842016-05-24 14:17:12 -0700865 int count = 0;
Mike Kleincff63962020-03-14 16:22:45 -0500866 fScratchMap.foreach([&](const GrGpuResource& resource) {
867 SkASSERT(resource.resourcePriv().getScratchKey().isValid());
868 SkASSERT(!resource.getUniqueKey().isValid());
robertphillipsc4ed6842016-05-24 14:17:12 -0700869 count++;
Mike Kleincff63962020-03-14 16:22:45 -0500870 });
871 SkASSERT(count == fScratchMap.count());
robertphillipsc4ed6842016-05-24 14:17:12 -0700872 }
873
bsalomonf320e042015-02-17 15:09:34 -0800874 Stats stats(this);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400875 size_t purgeableBytes = 0;
Brian Salomon2c791fc2019-04-02 11:52:03 -0400876 int numBudgetedResourcesFlushWillMakePurgeable = 0;
bsalomonf320e042015-02-17 15:09:34 -0800877
878 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500879 SkASSERT(!fNonpurgeableResources[i]->resourcePriv().isPurgeable() ||
bsalomon3f324322015-04-08 11:01:54 -0700880 fNewlyPurgeableResourceForValidation == fNonpurgeableResources[i]);
bsalomonf320e042015-02-17 15:09:34 -0800881 SkASSERT(*fNonpurgeableResources[i]->cacheAccess().accessCacheIndex() == i);
882 SkASSERT(!fNonpurgeableResources[i]->wasDestroyed());
Brian Salomon2c791fc2019-04-02 11:52:03 -0400883 if (fNonpurgeableResources[i]->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted &&
884 !fNonpurgeableResources[i]->cacheAccess().hasRef() &&
885 fNewlyPurgeableResourceForValidation != fNonpurgeableResources[i]) {
Brian Salomon2c791fc2019-04-02 11:52:03 -0400886 ++numBudgetedResourcesFlushWillMakePurgeable;
887 }
bsalomonf320e042015-02-17 15:09:34 -0800888 stats.update(fNonpurgeableResources[i]);
bsalomon71cb0c22014-11-14 12:10:14 -0800889 }
bsalomon9f2d1572015-02-17 11:47:40 -0800890 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500891 SkASSERT(fPurgeableQueue.at(i)->resourcePriv().isPurgeable());
bsalomonf320e042015-02-17 15:09:34 -0800892 SkASSERT(*fPurgeableQueue.at(i)->cacheAccess().accessCacheIndex() == i);
893 SkASSERT(!fPurgeableQueue.at(i)->wasDestroyed());
894 stats.update(fPurgeableQueue.at(i));
Derek Sollenbergeree479142017-05-24 11:41:33 -0400895 purgeableBytes += fPurgeableQueue.at(i)->gpuMemorySize();
bsalomon9f2d1572015-02-17 11:47:40 -0800896 }
897
bsalomonf320e042015-02-17 15:09:34 -0800898 SkASSERT(fCount == this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800899 SkASSERT(fBudgetedCount <= fCount);
bsalomonf320e042015-02-17 15:09:34 -0800900 SkASSERT(fBudgetedBytes <= fBytes);
901 SkASSERT(stats.fBytes == fBytes);
Brian Salomon2c791fc2019-04-02 11:52:03 -0400902 SkASSERT(fNumBudgetedResourcesFlushWillMakePurgeable ==
903 numBudgetedResourcesFlushWillMakePurgeable);
bsalomonf320e042015-02-17 15:09:34 -0800904 SkASSERT(stats.fBudgetedBytes == fBudgetedBytes);
905 SkASSERT(stats.fBudgetedCount == fBudgetedCount);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400906 SkASSERT(purgeableBytes == fPurgeableBytes);
bsalomon71cb0c22014-11-14 12:10:14 -0800907#if GR_CACHE_STATS
bsalomondace19e2014-11-17 07:34:06 -0800908 SkASSERT(fBudgetedHighWaterCount <= fHighWaterCount);
909 SkASSERT(fBudgetedHighWaterBytes <= fHighWaterBytes);
bsalomonf320e042015-02-17 15:09:34 -0800910 SkASSERT(fBytes <= fHighWaterBytes);
911 SkASSERT(fCount <= fHighWaterCount);
912 SkASSERT(fBudgetedBytes <= fBudgetedHighWaterBytes);
913 SkASSERT(fBudgetedCount <= fBudgetedHighWaterCount);
bsalomon71cb0c22014-11-14 12:10:14 -0800914#endif
bsalomon8718aaf2015-02-19 07:24:21 -0800915 SkASSERT(stats.fContent == fUniqueHash.count());
bsalomonf320e042015-02-17 15:09:34 -0800916 SkASSERT(stats.fScratch + stats.fCouldBeScratch == fScratchMap.count());
bsalomon71cb0c22014-11-14 12:10:14 -0800917
bsalomon3f324322015-04-08 11:01:54 -0700918 // This assertion is not currently valid because we can be in recursive notifyCntReachedZero()
bsalomon12299ab2014-11-14 13:33:09 -0800919 // calls. This will be fixed when subresource registration is explicit.
bsalomondace19e2014-11-17 07:34:06 -0800920 // bool overBudget = budgetedBytes > fMaxBytes || budgetedCount > fMaxCount;
bsalomon12299ab2014-11-14 13:33:09 -0800921 // SkASSERT(!overBudget || locked == count || fPurging);
bsalomon71cb0c22014-11-14 12:10:14 -0800922}
bsalomonf320e042015-02-17 15:09:34 -0800923
924bool GrResourceCache::isInCache(const GrGpuResource* resource) const {
925 int index = *resource->cacheAccess().accessCacheIndex();
926 if (index < 0) {
927 return false;
928 }
929 if (index < fPurgeableQueue.count() && fPurgeableQueue.at(index) == resource) {
930 return true;
931 }
932 if (index < fNonpurgeableResources.count() && fNonpurgeableResources[index] == resource) {
933 return true;
934 }
935 SkDEBUGFAIL("Resource index should be -1 or the resource should be in the cache.");
936 return false;
937}
938
bsalomon71cb0c22014-11-14 12:10:14 -0800939#endif