blob: 6b3c8d0edf7da43b901c41427385314ab8944abf [file] [log] [blame]
bsalomonc8dc1f72014-08-21 13:02:13 -07001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrResourceCache.h"
Brian Salomon614c1a82018-12-19 15:42:06 -05009#include <atomic>
Robert Phillips4e105e22020-07-16 09:18:50 -040010#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/private/GrSingleOwner.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/private/SkTo.h"
13#include "include/utils/SkRandom.h"
Ben Wagner21bca282019-05-15 10:15:52 -040014#include "src/core/SkMessageBus.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/core/SkOpts.h"
16#include "src/core/SkScopeExit.h"
John Stiles6e9ead92020-07-14 00:13:51 +000017#include "src/core/SkTSort.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrCaps.h"
19#include "src/gpu/GrContextPriv.h"
20#include "src/gpu/GrGpuResourceCacheAccess.h"
21#include "src/gpu/GrProxyProvider.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000022#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrTextureProxyCacheAccess.h"
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
Adlai Holler33dbd652020-06-01 12:35:42 -040031#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(fSingleOwner)
Brian Salomon8f8995a2018-10-15 14:32:15 -040032
bsalomon71cb0c22014-11-14 12:10:14 -080033//////////////////////////////////////////////////////////////////////////////
34
bsalomon7775c852014-12-30 12:50:52 -080035GrScratchKey::ResourceType GrScratchKey::GenerateResourceType() {
Mike Klein0ec1c572018-12-04 11:52:51 -050036 static std::atomic<int32_t> nextType{INHERITED::kInvalidDomain + 1};
bsalomonfe369ee2014-11-10 11:59:06 -080037
Mike Klein0ec1c572018-12-04 11:52:51 -050038 int32_t type = nextType++;
Ben Wagner9bc36fd2018-06-15 14:23:36 -040039 if (type > SkTo<int32_t>(UINT16_MAX)) {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040040 SK_ABORT("Too many Resource Types");
bsalomon71cb0c22014-11-14 12:10:14 -080041 }
42
43 return static_cast<ResourceType>(type);
44}
45
bsalomon8718aaf2015-02-19 07:24:21 -080046GrUniqueKey::Domain GrUniqueKey::GenerateDomain() {
Mike Klein0ec1c572018-12-04 11:52:51 -050047 static std::atomic<int32_t> nextDomain{INHERITED::kInvalidDomain + 1};
bsalomon7775c852014-12-30 12:50:52 -080048
Mike Klein0ec1c572018-12-04 11:52:51 -050049 int32_t domain = nextDomain++;
Ben Wagner397ee0e2018-06-15 15:13:26 -040050 if (domain > SkTo<int32_t>(UINT16_MAX)) {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040051 SK_ABORT("Too many GrUniqueKey Domains");
bsalomon7775c852014-12-30 12:50:52 -080052 }
bsalomon24db3b12015-01-23 04:24:04 -080053
54 return static_cast<Domain>(domain);
55}
bsalomon3f324322015-04-08 11:01:54 -070056
bsalomon24db3b12015-01-23 04:24:04 -080057uint32_t GrResourceKeyHash(const uint32_t* data, size_t size) {
mtklein4e976072016-08-08 09:06:27 -070058 return SkOpts::hash(data, size);
bsalomon7775c852014-12-30 12:50:52 -080059}
60
bsalomonfe369ee2014-11-10 11:59:06 -080061//////////////////////////////////////////////////////////////////////////////
62
bsalomon0ea80f42015-02-11 10:49:59 -080063class GrResourceCache::AutoValidate : ::SkNoncopyable {
bsalomon71cb0c22014-11-14 12:10:14 -080064public:
bsalomon0ea80f42015-02-11 10:49:59 -080065 AutoValidate(GrResourceCache* cache) : fCache(cache) { cache->validate(); }
bsalomon71cb0c22014-11-14 12:10:14 -080066 ~AutoValidate() { fCache->validate(); }
67private:
bsalomon0ea80f42015-02-11 10:49:59 -080068 GrResourceCache* fCache;
bsalomon71cb0c22014-11-14 12:10:14 -080069};
70
Brian Salomon876a0172019-03-08 11:12:14 -050071//////////////////////////////////////////////////////////////////////////////
72
Robert Phillipsddc21482019-10-16 14:30:09 -040073inline GrResourceCache::TextureAwaitingUnref::TextureAwaitingUnref() = default;
Brian Salomon876a0172019-03-08 11:12:14 -050074
Robert Phillipsddc21482019-10-16 14:30:09 -040075inline GrResourceCache::TextureAwaitingUnref::TextureAwaitingUnref(GrTexture* texture)
76 : fTexture(texture), fNumUnrefs(1) {}
Brian Salomon876a0172019-03-08 11:12:14 -050077
Robert Phillipsddc21482019-10-16 14:30:09 -040078inline GrResourceCache::TextureAwaitingUnref::TextureAwaitingUnref(TextureAwaitingUnref&& that) {
Adlai Holler5ba50af2020-04-29 21:11:14 -040079 fTexture = std::exchange(that.fTexture, nullptr);
80 fNumUnrefs = std::exchange(that.fNumUnrefs, 0);
Brian Salomon876a0172019-03-08 11:12:14 -050081}
82
Robert Phillipsddc21482019-10-16 14:30:09 -040083inline GrResourceCache::TextureAwaitingUnref& GrResourceCache::TextureAwaitingUnref::operator=(
84 TextureAwaitingUnref&& that) {
Adlai Holler5ba50af2020-04-29 21:11:14 -040085 fTexture = std::exchange(that.fTexture, nullptr);
86 fNumUnrefs = std::exchange(that.fNumUnrefs, 0);
Brian Salomon876a0172019-03-08 11:12:14 -050087 return *this;
88}
89
Robert Phillipsddc21482019-10-16 14:30:09 -040090inline GrResourceCache::TextureAwaitingUnref::~TextureAwaitingUnref() {
91 if (fTexture) {
Brian Salomon876a0172019-03-08 11:12:14 -050092 for (int i = 0; i < fNumUnrefs; ++i) {
Robert Phillipsddc21482019-10-16 14:30:09 -040093 fTexture->unref();
Brian Salomon876a0172019-03-08 11:12:14 -050094 }
95 }
96}
97
Robert Phillipsddc21482019-10-16 14:30:09 -040098inline void GrResourceCache::TextureAwaitingUnref::TextureAwaitingUnref::addRef() { ++fNumUnrefs; }
Brian Salomon876a0172019-03-08 11:12:14 -050099
Robert Phillipsddc21482019-10-16 14:30:09 -0400100inline void GrResourceCache::TextureAwaitingUnref::unref() {
Brian Salomon876a0172019-03-08 11:12:14 -0500101 SkASSERT(fNumUnrefs > 0);
Robert Phillipsddc21482019-10-16 14:30:09 -0400102 fTexture->unref();
Brian Salomon876a0172019-03-08 11:12:14 -0500103 --fNumUnrefs;
104}
105
Robert Phillipsddc21482019-10-16 14:30:09 -0400106inline bool GrResourceCache::TextureAwaitingUnref::finished() { return !fNumUnrefs; }
Brian Salomon876a0172019-03-08 11:12:14 -0500107
108//////////////////////////////////////////////////////////////////////////////
robertphillipsee843b22016-10-04 05:30:20 -0700109
Brian Salomon8f8995a2018-10-15 14:32:15 -0400110GrResourceCache::GrResourceCache(const GrCaps* caps, GrSingleOwner* singleOwner,
111 uint32_t contextUniqueID)
Brian Salomon2c791fc2019-04-02 11:52:03 -0400112 : fInvalidUniqueKeyInbox(contextUniqueID)
Robert Phillipsddc21482019-10-16 14:30:09 -0400113 , fFreedTextureInbox(contextUniqueID)
Brian Salomon238069b2018-07-11 15:58:57 -0400114 , fContextUniqueID(contextUniqueID)
Brian Salomon8f8995a2018-10-15 14:32:15 -0400115 , fSingleOwner(singleOwner)
Brian Salomon238069b2018-07-11 15:58:57 -0400116 , fPreferVRAMUseOverFlushes(caps->preferVRAMUseOverFlushes()) {
117 SkASSERT(contextUniqueID != SK_InvalidUniqueID);
bsalomon71cb0c22014-11-14 12:10:14 -0800118}
119
bsalomon0ea80f42015-02-11 10:49:59 -0800120GrResourceCache::~GrResourceCache() {
bsalomonc8dc1f72014-08-21 13:02:13 -0700121 this->releaseAll();
122}
123
Robert Phillipscf39f372019-09-03 10:29:20 -0400124void GrResourceCache::setLimit(size_t bytes) {
bsalomon71cb0c22014-11-14 12:10:14 -0800125 fMaxBytes = bytes;
126 this->purgeAsNeeded();
127}
128
bsalomon0ea80f42015-02-11 10:49:59 -0800129void GrResourceCache::insertResource(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400130 ASSERT_SINGLE_OWNER
bsalomon49f085d2014-09-05 13:34:00 -0700131 SkASSERT(resource);
bsalomon16961262014-08-26 14:01:07 -0700132 SkASSERT(!this->isInCache(resource));
bsalomonf320e042015-02-17 15:09:34 -0800133 SkASSERT(!resource->wasDestroyed());
Brian Salomon614c1a82018-12-19 15:42:06 -0500134 SkASSERT(!resource->resourcePriv().isPurgeable());
bsalomonddf30e62015-02-19 11:38:44 -0800135
136 // We must set the timestamp before adding to the array in case the timestamp wraps and we wind
137 // up iterating over all the resources that already have timestamps.
138 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
139
bsalomonf320e042015-02-17 15:09:34 -0800140 this->addToNonpurgeableArray(resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800141
bsalomondace19e2014-11-17 07:34:06 -0800142 size_t size = resource->gpuMemorySize();
bsalomonf320e042015-02-17 15:09:34 -0800143 SkDEBUGCODE(++fCount;)
bsalomon84c8e622014-11-17 09:33:27 -0800144 fBytes += size;
bsalomon82b1d622014-11-14 13:59:57 -0800145#if GR_CACHE_STATS
Brian Osman788b9162020-02-07 10:36:46 -0500146 fHighWaterCount = std::max(this->getResourceCount(), fHighWaterCount);
147 fHighWaterBytes = std::max(fBytes, fHighWaterBytes);
bsalomon82b1d622014-11-14 13:59:57 -0800148#endif
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500149 if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
bsalomondace19e2014-11-17 07:34:06 -0800150 ++fBudgetedCount;
151 fBudgetedBytes += size;
Brian Osman39c08ac2017-07-26 09:36:09 -0400152 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800153 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomondace19e2014-11-17 07:34:06 -0800154#if GR_CACHE_STATS
Brian Osman788b9162020-02-07 10:36:46 -0500155 fBudgetedHighWaterCount = std::max(fBudgetedCount, fBudgetedHighWaterCount);
156 fBudgetedHighWaterBytes = std::max(fBudgetedBytes, fBudgetedHighWaterBytes);
bsalomondace19e2014-11-17 07:34:06 -0800157#endif
158 }
robertphillipsc4ed6842016-05-24 14:17:12 -0700159 if (resource->resourcePriv().getScratchKey().isValid() &&
160 !resource->getUniqueKey().isValid()) {
kkinnunen2e6055b2016-04-22 01:48:29 -0700161 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
bsalomon3582d3e2015-02-13 14:20:05 -0800162 fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
bsalomon744998e2014-08-28 09:54:34 -0700163 }
bsalomon9f2d1572015-02-17 11:47:40 -0800164
bsalomon71cb0c22014-11-14 12:10:14 -0800165 this->purgeAsNeeded();
bsalomonc8dc1f72014-08-21 13:02:13 -0700166}
167
bsalomon0ea80f42015-02-11 10:49:59 -0800168void GrResourceCache::removeResource(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400169 ASSERT_SINGLE_OWNER
bsalomon9f2d1572015-02-17 11:47:40 -0800170 this->validate();
bsalomon16961262014-08-26 14:01:07 -0700171 SkASSERT(this->isInCache(resource));
bsalomondace19e2014-11-17 07:34:06 -0800172
Derek Sollenbergeree479142017-05-24 11:41:33 -0400173 size_t size = resource->gpuMemorySize();
Brian Salomon614c1a82018-12-19 15:42:06 -0500174 if (resource->resourcePriv().isPurgeable()) {
bsalomon9f2d1572015-02-17 11:47:40 -0800175 fPurgeableQueue.remove(resource);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400176 fPurgeableBytes -= size;
bsalomonf320e042015-02-17 15:09:34 -0800177 } else {
178 this->removeFromNonpurgeableArray(resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800179 }
180
bsalomonf320e042015-02-17 15:09:34 -0800181 SkDEBUGCODE(--fCount;)
bsalomondace19e2014-11-17 07:34:06 -0800182 fBytes -= size;
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500183 if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
bsalomondace19e2014-11-17 07:34:06 -0800184 --fBudgetedCount;
185 fBudgetedBytes -= size;
Brian Osman39c08ac2017-07-26 09:36:09 -0400186 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800187 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomondace19e2014-11-17 07:34:06 -0800188 }
189
robertphillipsc4ed6842016-05-24 14:17:12 -0700190 if (resource->resourcePriv().getScratchKey().isValid() &&
191 !resource->getUniqueKey().isValid()) {
bsalomon3582d3e2015-02-13 14:20:05 -0800192 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
bsalomon744998e2014-08-28 09:54:34 -0700193 }
bsalomon8718aaf2015-02-19 07:24:21 -0800194 if (resource->getUniqueKey().isValid()) {
195 fUniqueHash.remove(resource->getUniqueKey());
bsalomon8b79d232014-11-10 10:19:06 -0800196 }
bsalomonb436ed62014-11-17 12:15:56 -0800197 this->validate();
bsalomonc8dc1f72014-08-21 13:02:13 -0700198}
199
bsalomon0ea80f42015-02-11 10:49:59 -0800200void GrResourceCache::abandonAll() {
bsalomon71cb0c22014-11-14 12:10:14 -0800201 AutoValidate av(this);
202
Brian Salomon876a0172019-03-08 11:12:14 -0500203 // We need to make sure to free any resources that were waiting on a free message but never
204 // received one.
Robert Phillipsddc21482019-10-16 14:30:09 -0400205 fTexturesAwaitingUnref.reset();
Greg Danielb2acf0a2018-09-12 09:17:11 -0400206
bsalomonf320e042015-02-17 15:09:34 -0800207 while (fNonpurgeableResources.count()) {
208 GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
209 SkASSERT(!back->wasDestroyed());
210 back->cacheAccess().abandon();
bsalomonc8dc1f72014-08-21 13:02:13 -0700211 }
bsalomonf320e042015-02-17 15:09:34 -0800212
213 while (fPurgeableQueue.count()) {
214 GrGpuResource* top = fPurgeableQueue.peek();
215 SkASSERT(!top->wasDestroyed());
216 top->cacheAccess().abandon();
217 }
218
bsalomon744998e2014-08-28 09:54:34 -0700219 SkASSERT(!fScratchMap.count());
bsalomon8718aaf2015-02-19 07:24:21 -0800220 SkASSERT(!fUniqueHash.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700221 SkASSERT(!fCount);
bsalomonf320e042015-02-17 15:09:34 -0800222 SkASSERT(!this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800223 SkASSERT(!fBytes);
224 SkASSERT(!fBudgetedCount);
225 SkASSERT(!fBudgetedBytes);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400226 SkASSERT(!fPurgeableBytes);
Robert Phillipsddc21482019-10-16 14:30:09 -0400227 SkASSERT(!fTexturesAwaitingUnref.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700228}
229
bsalomon0ea80f42015-02-11 10:49:59 -0800230void GrResourceCache::releaseAll() {
bsalomon71cb0c22014-11-14 12:10:14 -0800231 AutoValidate av(this);
232
Brian Osman13dddce2017-05-09 13:19:50 -0400233 this->processFreedGpuResources();
234
Greg Danielc27eb722018-08-10 09:48:08 -0400235 // We need to make sure to free any resources that were waiting on a free message but never
236 // received one.
Robert Phillipsddc21482019-10-16 14:30:09 -0400237 fTexturesAwaitingUnref.reset();
Greg Danielc27eb722018-08-10 09:48:08 -0400238
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500239 SkASSERT(fProxyProvider); // better have called setProxyProvider
Robert Phillips3ec95732017-09-29 15:10:39 -0400240 // We must remove the uniqueKeys from the proxies here. While they possess a uniqueKey
241 // they also have a raw pointer back to this class (which is presumably going away)!
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500242 fProxyProvider->removeAllUniqueKeys();
Robert Phillips45a6f142017-09-29 09:49:41 -0400243
Brian Salomon614c1a82018-12-19 15:42:06 -0500244 while (fNonpurgeableResources.count()) {
bsalomonf320e042015-02-17 15:09:34 -0800245 GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
246 SkASSERT(!back->wasDestroyed());
247 back->cacheAccess().release();
bsalomonc8dc1f72014-08-21 13:02:13 -0700248 }
bsalomonf320e042015-02-17 15:09:34 -0800249
250 while (fPurgeableQueue.count()) {
251 GrGpuResource* top = fPurgeableQueue.peek();
252 SkASSERT(!top->wasDestroyed());
253 top->cacheAccess().release();
254 }
255
bsalomon744998e2014-08-28 09:54:34 -0700256 SkASSERT(!fScratchMap.count());
bsalomon8718aaf2015-02-19 07:24:21 -0800257 SkASSERT(!fUniqueHash.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700258 SkASSERT(!fCount);
bsalomonf320e042015-02-17 15:09:34 -0800259 SkASSERT(!this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800260 SkASSERT(!fBytes);
261 SkASSERT(!fBudgetedCount);
262 SkASSERT(!fBudgetedBytes);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400263 SkASSERT(!fPurgeableBytes);
Robert Phillipsddc21482019-10-16 14:30:09 -0400264 SkASSERT(!fTexturesAwaitingUnref.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700265}
bsalomonbcf0a522014-10-08 08:40:09 -0700266
Brian Salomon2c791fc2019-04-02 11:52:03 -0400267void GrResourceCache::refResource(GrGpuResource* resource) {
268 SkASSERT(resource);
269 SkASSERT(resource->getContext()->priv().getResourceCache() == this);
270 if (resource->cacheAccess().hasRef()) {
271 resource->ref();
272 } else {
273 this->refAndMakeResourceMRU(resource);
274 }
275 this->validate();
276}
277
bsalomon0ea80f42015-02-11 10:49:59 -0800278class GrResourceCache::AvailableForScratchUse {
bsalomonbcf0a522014-10-08 08:40:09 -0700279public:
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400280 AvailableForScratchUse() { }
bsalomonbcf0a522014-10-08 08:40:09 -0700281
282 bool operator()(const GrGpuResource* resource) const {
robertphillipsc4ed6842016-05-24 14:17:12 -0700283 SkASSERT(!resource->getUniqueKey().isValid() &&
284 resource->resourcePriv().getScratchKey().isValid());
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400285
Robert Phillipsaee18c92019-09-06 11:48:27 -0400286 // isScratch() also tests that the resource is budgeted.
bsalomon12299ab2014-11-14 13:33:09 -0800287 if (resource->internalHasRef() || !resource->cacheAccess().isScratch()) {
bsalomon000f8292014-10-15 19:04:14 -0700288 return false;
bsalomonbcf0a522014-10-08 08:40:09 -0700289 }
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400290 return true;
bsalomonbcf0a522014-10-08 08:40:09 -0700291 }
bsalomonbcf0a522014-10-08 08:40:09 -0700292};
293
Robert Phillipsaee18c92019-09-06 11:48:27 -0400294GrGpuResource* GrResourceCache::findAndRefScratchResource(const GrScratchKey& scratchKey) {
bsalomon7775c852014-12-30 12:50:52 -0800295 SkASSERT(scratchKey.isValid());
robertphillipsee843b22016-10-04 05:30:20 -0700296
Robert Phillipsaee18c92019-09-06 11:48:27 -0400297 GrGpuResource* resource = fScratchMap.find(scratchKey, AvailableForScratchUse());
bsalomon71cb0c22014-11-14 12:10:14 -0800298 if (resource) {
bsalomon9f2d1572015-02-17 11:47:40 -0800299 this->refAndMakeResourceMRU(resource);
bsalomonb436ed62014-11-17 12:15:56 -0800300 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800301 }
302 return resource;
bsalomonbcf0a522014-10-08 08:40:09 -0700303}
bsalomon8b79d232014-11-10 10:19:06 -0800304
bsalomon0ea80f42015-02-11 10:49:59 -0800305void GrResourceCache::willRemoveScratchKey(const GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400306 ASSERT_SINGLE_OWNER
bsalomon3582d3e2015-02-13 14:20:05 -0800307 SkASSERT(resource->resourcePriv().getScratchKey().isValid());
robertphillipsc4ed6842016-05-24 14:17:12 -0700308 if (!resource->getUniqueKey().isValid()) {
309 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
310 }
bsalomon10e23ca2014-11-25 05:52:06 -0800311}
312
bsalomonf99e9612015-02-19 08:24:16 -0800313void GrResourceCache::removeUniqueKey(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400314 ASSERT_SINGLE_OWNER
bsalomon3f324322015-04-08 11:01:54 -0700315 // Someone has a ref to this resource in order to have removed the key. When the ref count
316 // reaches zero we will get a ref cnt notification and figure out what to do with it.
bsalomonf99e9612015-02-19 08:24:16 -0800317 if (resource->getUniqueKey().isValid()) {
318 SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
319 fUniqueHash.remove(resource->getUniqueKey());
320 }
321 resource->cacheAccess().removeUniqueKey();
robertphillipsc4ed6842016-05-24 14:17:12 -0700322 if (resource->resourcePriv().getScratchKey().isValid()) {
323 fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
324 }
325
Brian Salomon9bc76d92019-01-24 12:18:33 -0500326 // Removing a unique key from a kUnbudgetedCacheable resource would make the resource
327 // require purging. However, the resource must be ref'ed to get here and therefore can't
328 // be purgeable. We'll purge it when the refs reach zero.
329 SkASSERT(!resource->resourcePriv().isPurgeable());
bsalomonf99e9612015-02-19 08:24:16 -0800330 this->validate();
bsalomon23e619c2015-02-06 11:54:28 -0800331}
332
bsalomonf99e9612015-02-19 08:24:16 -0800333void GrResourceCache::changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400334 ASSERT_SINGLE_OWNER
bsalomon8b79d232014-11-10 10:19:06 -0800335 SkASSERT(resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800336 SkASSERT(this->isInCache(resource));
bsalomon8b79d232014-11-10 10:19:06 -0800337
bsalomonf99e9612015-02-19 08:24:16 -0800338 // If another resource has the new key, remove its key then install the key on this resource.
339 if (newKey.isValid()) {
Greg Daniel0d537802017-09-08 11:44:14 -0400340 if (GrGpuResource* old = fUniqueHash.find(newKey)) {
341 // If the old resource using the key is purgeable and is unreachable, then remove it.
Brian Salomon614c1a82018-12-19 15:42:06 -0500342 if (!old->resourcePriv().getScratchKey().isValid() &&
343 old->resourcePriv().isPurgeable()) {
Greg Daniel0d537802017-09-08 11:44:14 -0400344 old->cacheAccess().release();
345 } else {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500346 // removeUniqueKey expects an external owner of the resource.
347 this->removeUniqueKey(sk_ref_sp(old).get());
Greg Daniel0d537802017-09-08 11:44:14 -0400348 }
349 }
350 SkASSERT(nullptr == fUniqueHash.find(newKey));
351
robertphillipsc4ed6842016-05-24 14:17:12 -0700352 // Remove the entry for this resource if it already has a unique key.
353 if (resource->getUniqueKey().isValid()) {
354 SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
355 fUniqueHash.remove(resource->getUniqueKey());
356 SkASSERT(nullptr == fUniqueHash.find(resource->getUniqueKey()));
357 } else {
358 // 'resource' didn't have a valid unique key before so it is switching sides. Remove it
359 // from the ScratchMap
360 if (resource->resourcePriv().getScratchKey().isValid()) {
361 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
362 }
363 }
364
bsalomonf99e9612015-02-19 08:24:16 -0800365 resource->cacheAccess().setUniqueKey(newKey);
366 fUniqueHash.add(resource);
367 } else {
robertphillipsc4ed6842016-05-24 14:17:12 -0700368 this->removeUniqueKey(resource);
bsalomonf99e9612015-02-19 08:24:16 -0800369 }
370
bsalomon71cb0c22014-11-14 12:10:14 -0800371 this->validate();
bsalomon8b79d232014-11-10 10:19:06 -0800372}
bsalomon71cb0c22014-11-14 12:10:14 -0800373
bsalomon9f2d1572015-02-17 11:47:40 -0800374void GrResourceCache::refAndMakeResourceMRU(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400375 ASSERT_SINGLE_OWNER
bsalomon71cb0c22014-11-14 12:10:14 -0800376 SkASSERT(resource);
377 SkASSERT(this->isInCache(resource));
bsalomonddf30e62015-02-19 11:38:44 -0800378
Brian Salomon614c1a82018-12-19 15:42:06 -0500379 if (resource->resourcePriv().isPurgeable()) {
bsalomon9f2d1572015-02-17 11:47:40 -0800380 // It's about to become unpurgeable.
Derek Sollenbergeree479142017-05-24 11:41:33 -0400381 fPurgeableBytes -= resource->gpuMemorySize();
bsalomon9f2d1572015-02-17 11:47:40 -0800382 fPurgeableQueue.remove(resource);
bsalomonf320e042015-02-17 15:09:34 -0800383 this->addToNonpurgeableArray(resource);
Brian Salomon2c791fc2019-04-02 11:52:03 -0400384 } else if (!resource->cacheAccess().hasRef() &&
385 resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
386 SkASSERT(fNumBudgetedResourcesFlushWillMakePurgeable > 0);
387 fNumBudgetedResourcesFlushWillMakePurgeable--;
bsalomon9f2d1572015-02-17 11:47:40 -0800388 }
Brian Salomon01ceae92019-04-02 11:49:54 -0400389 resource->cacheAccess().ref();
bsalomonddf30e62015-02-19 11:38:44 -0800390
391 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
bsalomonf320e042015-02-17 15:09:34 -0800392 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800393}
394
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400395void GrResourceCache::notifyRefCntReachedZero(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400396 ASSERT_SINGLE_OWNER
bsalomon71cb0c22014-11-14 12:10:14 -0800397 SkASSERT(resource);
bsalomon3f324322015-04-08 11:01:54 -0700398 SkASSERT(!resource->wasDestroyed());
bsalomon71cb0c22014-11-14 12:10:14 -0800399 SkASSERT(this->isInCache(resource));
bsalomon3f324322015-04-08 11:01:54 -0700400 // This resource should always be in the nonpurgeable array when this function is called. It
401 // will be moved to the queue if it is newly purgeable.
402 SkASSERT(fNonpurgeableResources[*resource->cacheAccess().accessCacheIndex()] == resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800403
bsalomon3f324322015-04-08 11:01:54 -0700404#ifdef SK_DEBUG
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400405 // When the timestamp overflows validate() is called. validate() checks that resources in
406 // the nonpurgeable array are indeed not purgeable. However, the movement from the array to
407 // the purgeable queue happens just below in this function. So we mark it as an exception.
408 if (resource->resourcePriv().isPurgeable()) {
409 fNewlyPurgeableResourceForValidation = resource;
bsalomon3f324322015-04-08 11:01:54 -0700410 }
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400411#endif
412 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
413 SkDEBUGCODE(fNewlyPurgeableResourceForValidation = nullptr);
bsalomon3f324322015-04-08 11:01:54 -0700414
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400415 if (!resource->resourcePriv().isPurgeable() &&
416 resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
417 ++fNumBudgetedResourcesFlushWillMakePurgeable;
bsalomon3f324322015-04-08 11:01:54 -0700418 }
419
Brian Salomon9bc76d92019-01-24 12:18:33 -0500420 if (!resource->resourcePriv().isPurgeable()) {
421 this->validate();
422 return;
423 }
424
bsalomonf320e042015-02-17 15:09:34 -0800425 this->removeFromNonpurgeableArray(resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800426 fPurgeableQueue.insert(resource);
Brian Salomon5e150852017-03-22 14:53:13 -0400427 resource->cacheAccess().setTimeWhenResourceBecomePurgeable();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400428 fPurgeableBytes += resource->gpuMemorySize();
bsalomon71cb0c22014-11-14 12:10:14 -0800429
Greg Daniel303e83e2018-09-10 14:10:19 -0400430 bool hasUniqueKey = resource->getUniqueKey().isValid();
431
Brian Salomon9bc76d92019-01-24 12:18:33 -0500432 GrBudgetedType budgetedType = resource->resourcePriv().budgetedType();
Brian Salomon614c1a82018-12-19 15:42:06 -0500433
Brian Salomon9bc76d92019-01-24 12:18:33 -0500434 if (budgetedType == GrBudgetedType::kBudgeted) {
435 // Purge the resource immediately if we're over budget
436 // Also purge if the resource has neither a valid scratch key nor a unique key.
437 bool hasKey = resource->resourcePriv().getScratchKey().isValid() || hasUniqueKey;
438 if (!this->overBudget() && hasKey) {
439 return;
440 }
441 } else {
442 // We keep unbudgeted resources with a unique key in the purgeable queue of the cache so
443 // they can be reused again by the image connected to the unique key.
444 if (hasUniqueKey && budgetedType == GrBudgetedType::kUnbudgetedCacheable) {
445 return;
446 }
447 // Check whether this resource could still be used as a scratch resource.
448 if (!resource->resourcePriv().refsWrappedObjects() &&
449 resource->resourcePriv().getScratchKey().isValid()) {
450 // We won't purge an existing resource to make room for this one.
Robert Phillipscf39f372019-09-03 10:29:20 -0400451 if (this->wouldFit(resource->gpuMemorySize())) {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500452 resource->resourcePriv().makeBudgeted();
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500453 return;
454 }
bsalomonc2f35b72015-01-23 07:19:22 -0800455 }
bsalomonc2f35b72015-01-23 07:19:22 -0800456 }
Brian Salomon9bc76d92019-01-24 12:18:33 -0500457
bsalomonf320e042015-02-17 15:09:34 -0800458 SkDEBUGCODE(int beforeCount = this->getResourceCount();)
bsalomon9f2d1572015-02-17 11:47:40 -0800459 resource->cacheAccess().release();
460 // We should at least free this resource, perhaps dependent resources as well.
bsalomonf320e042015-02-17 15:09:34 -0800461 SkASSERT(this->getResourceCount() < beforeCount);
bsalomon71cb0c22014-11-14 12:10:14 -0800462 this->validate();
463}
464
bsalomon0ea80f42015-02-11 10:49:59 -0800465void GrResourceCache::didChangeBudgetStatus(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400466 ASSERT_SINGLE_OWNER
bsalomon84c8e622014-11-17 09:33:27 -0800467 SkASSERT(resource);
468 SkASSERT(this->isInCache(resource));
469
470 size_t size = resource->gpuMemorySize();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500471 // Changing from BudgetedType::kUnbudgetedCacheable to another budgeted type could make
472 // resource become purgeable. However, we should never allow that transition. Wrapped
473 // resources are the only resources that can be in that state and they aren't allowed to
474 // transition from one budgeted state to another.
475 SkDEBUGCODE(bool wasPurgeable = resource->resourcePriv().isPurgeable());
476 if (resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
bsalomon84c8e622014-11-17 09:33:27 -0800477 ++fBudgetedCount;
478 fBudgetedBytes += size;
bsalomonafe30052015-01-16 07:32:33 -0800479#if GR_CACHE_STATS
Brian Osman788b9162020-02-07 10:36:46 -0500480 fBudgetedHighWaterBytes = std::max(fBudgetedBytes, fBudgetedHighWaterBytes);
481 fBudgetedHighWaterCount = std::max(fBudgetedCount, fBudgetedHighWaterCount);
bsalomonafe30052015-01-16 07:32:33 -0800482#endif
Brian Salomon2c791fc2019-04-02 11:52:03 -0400483 if (!resource->resourcePriv().isPurgeable() && !resource->cacheAccess().hasRef()) {
484 ++fNumBudgetedResourcesFlushWillMakePurgeable;
485 }
bsalomon84c8e622014-11-17 09:33:27 -0800486 this->purgeAsNeeded();
487 } else {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500488 SkASSERT(resource->resourcePriv().budgetedType() != GrBudgetedType::kUnbudgetedCacheable);
bsalomon84c8e622014-11-17 09:33:27 -0800489 --fBudgetedCount;
490 fBudgetedBytes -= size;
Brian Salomon2c791fc2019-04-02 11:52:03 -0400491 if (!resource->resourcePriv().isPurgeable() && !resource->cacheAccess().hasRef()) {
492 --fNumBudgetedResourcesFlushWillMakePurgeable;
493 }
bsalomon84c8e622014-11-17 09:33:27 -0800494 }
Brian Salomon9bc76d92019-01-24 12:18:33 -0500495 SkASSERT(wasPurgeable == resource->resourcePriv().isPurgeable());
Brian Osman39c08ac2017-07-26 09:36:09 -0400496 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800497 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomon84c8e622014-11-17 09:33:27 -0800498
499 this->validate();
500}
501
robertphillipsee843b22016-10-04 05:30:20 -0700502void GrResourceCache::purgeAsNeeded() {
bsalomon3f324322015-04-08 11:01:54 -0700503 SkTArray<GrUniqueKeyInvalidatedMessage> invalidKeyMsgs;
504 fInvalidUniqueKeyInbox.poll(&invalidKeyMsgs);
505 if (invalidKeyMsgs.count()) {
Robert Phillips427966a2018-12-20 17:20:43 -0500506 SkASSERT(fProxyProvider);
507
508 for (int i = 0; i < invalidKeyMsgs.count(); ++i) {
509 fProxyProvider->processInvalidUniqueKey(invalidKeyMsgs[i].key(), nullptr,
510 GrProxyProvider::InvalidateGPUResource::kYes);
511 SkASSERT(!this->findAndRefUniqueResource(invalidKeyMsgs[i].key()));
512 }
bsalomon3f324322015-04-08 11:01:54 -0700513 }
bsalomon71cb0c22014-11-14 12:10:14 -0800514
Brian Osman13dddce2017-05-09 13:19:50 -0400515 this->processFreedGpuResources();
516
bsalomon3f324322015-04-08 11:01:54 -0700517 bool stillOverbudget = this->overBudget();
518 while (stillOverbudget && fPurgeableQueue.count()) {
robertphillipsee843b22016-10-04 05:30:20 -0700519 GrGpuResource* resource = fPurgeableQueue.peek();
Brian Salomon614c1a82018-12-19 15:42:06 -0500520 SkASSERT(resource->resourcePriv().isPurgeable());
bsalomon9f2d1572015-02-17 11:47:40 -0800521 resource->cacheAccess().release();
bsalomon3f324322015-04-08 11:01:54 -0700522 stillOverbudget = this->overBudget();
bsalomon9f2d1572015-02-17 11:47:40 -0800523 }
bsalomon71cb0c22014-11-14 12:10:14 -0800524
bsalomonb436ed62014-11-17 12:15:56 -0800525 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800526}
527
Robert Phillips6eba0632018-03-28 12:25:42 -0400528void GrResourceCache::purgeUnlockedResources(bool scratchResourcesOnly) {
529 if (!scratchResourcesOnly) {
530 // We could disable maintaining the heap property here, but it would add a lot of
531 // complexity. Moreover, this is rarely called.
532 while (fPurgeableQueue.count()) {
533 GrGpuResource* resource = fPurgeableQueue.peek();
Brian Salomon614c1a82018-12-19 15:42:06 -0500534 SkASSERT(resource->resourcePriv().isPurgeable());
Robert Phillips6eba0632018-03-28 12:25:42 -0400535 resource->cacheAccess().release();
536 }
537 } else {
538 // Sort the queue
539 fPurgeableQueue.sort();
540
541 // Make a list of the scratch resources to delete
542 SkTDArray<GrGpuResource*> scratchResources;
543 for (int i = 0; i < fPurgeableQueue.count(); i++) {
544 GrGpuResource* resource = fPurgeableQueue.at(i);
Brian Salomon614c1a82018-12-19 15:42:06 -0500545 SkASSERT(resource->resourcePriv().isPurgeable());
Robert Phillips6eba0632018-03-28 12:25:42 -0400546 if (!resource->getUniqueKey().isValid()) {
547 *scratchResources.append() = resource;
548 }
549 }
550
551 // Delete the scratch resources. This must be done as a separate pass
552 // to avoid messing up the sorted order of the queue
553 for (int i = 0; i < scratchResources.count(); i++) {
554 scratchResources.getAt(i)->cacheAccess().release();
555 }
bsalomon9f2d1572015-02-17 11:47:40 -0800556 }
bsalomon71cb0c22014-11-14 12:10:14 -0800557
bsalomonb436ed62014-11-17 12:15:56 -0800558 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800559}
560
Brian Salomon5e150852017-03-22 14:53:13 -0400561void GrResourceCache::purgeResourcesNotUsedSince(GrStdSteadyClock::time_point purgeTime) {
562 while (fPurgeableQueue.count()) {
563 const GrStdSteadyClock::time_point resourceTime =
564 fPurgeableQueue.peek()->cacheAccess().timeWhenResourceBecamePurgeable();
565 if (resourceTime >= purgeTime) {
566 // Resources were given both LRU timestamps and tagged with a frame number when
567 // they first became purgeable. The LRU timestamp won't change again until the
568 // resource is made non-purgeable again. So, at this point all the remaining
569 // resources in the timestamp-sorted queue will have a frame number >= to this
570 // one.
571 break;
572 }
573 GrGpuResource* resource = fPurgeableQueue.peek();
Brian Salomon614c1a82018-12-19 15:42:06 -0500574 SkASSERT(resource->resourcePriv().isPurgeable());
Brian Salomon5e150852017-03-22 14:53:13 -0400575 resource->cacheAccess().release();
576 }
577}
578
Derek Sollenberger5480a182017-05-25 16:43:59 -0400579void GrResourceCache::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
580
Brian Osman788b9162020-02-07 10:36:46 -0500581 const size_t tmpByteBudget = std::max((size_t)0, fBytes - bytesToPurge);
Derek Sollenberger5480a182017-05-25 16:43:59 -0400582 bool stillOverbudget = tmpByteBudget < fBytes;
583
584 if (preferScratchResources && bytesToPurge < fPurgeableBytes) {
585 // Sort the queue
586 fPurgeableQueue.sort();
587
588 // Make a list of the scratch resources to delete
589 SkTDArray<GrGpuResource*> scratchResources;
590 size_t scratchByteCount = 0;
591 for (int i = 0; i < fPurgeableQueue.count() && stillOverbudget; i++) {
592 GrGpuResource* resource = fPurgeableQueue.at(i);
Brian Salomon614c1a82018-12-19 15:42:06 -0500593 SkASSERT(resource->resourcePriv().isPurgeable());
Derek Sollenberger5480a182017-05-25 16:43:59 -0400594 if (!resource->getUniqueKey().isValid()) {
595 *scratchResources.append() = resource;
596 scratchByteCount += resource->gpuMemorySize();
597 stillOverbudget = tmpByteBudget < fBytes - scratchByteCount;
598 }
599 }
600
601 // Delete the scratch resources. This must be done as a separate pass
602 // to avoid messing up the sorted order of the queue
603 for (int i = 0; i < scratchResources.count(); i++) {
604 scratchResources.getAt(i)->cacheAccess().release();
605 }
606 stillOverbudget = tmpByteBudget < fBytes;
607
608 this->validate();
609 }
610
611 // Purge any remaining resources in LRU order
612 if (stillOverbudget) {
613 const size_t cachedByteCount = fMaxBytes;
614 fMaxBytes = tmpByteBudget;
615 this->purgeAsNeeded();
616 fMaxBytes = cachedByteCount;
617 }
618}
Brian Salomon8cefa3e2019-04-04 11:39:55 -0400619bool GrResourceCache::requestsFlush() const {
620 return this->overBudget() && !fPurgeableQueue.count() &&
621 fNumBudgetedResourcesFlushWillMakePurgeable > 0;
622}
623
Derek Sollenberger5480a182017-05-25 16:43:59 -0400624
Robert Phillipsddc21482019-10-16 14:30:09 -0400625void GrResourceCache::insertDelayedTextureUnref(GrTexture* texture) {
626 texture->ref();
627 uint32_t id = texture->uniqueID().asUInt();
628 if (auto* data = fTexturesAwaitingUnref.find(id)) {
Brian Salomon876a0172019-03-08 11:12:14 -0500629 data->addRef();
630 } else {
Robert Phillipsddc21482019-10-16 14:30:09 -0400631 fTexturesAwaitingUnref.set(id, {texture});
Brian Salomon876a0172019-03-08 11:12:14 -0500632 }
Brian Osman13dddce2017-05-09 13:19:50 -0400633}
634
635void GrResourceCache::processFreedGpuResources() {
Robert Phillips1dfc77c2019-10-16 16:39:45 -0400636 if (!fTexturesAwaitingUnref.count()) {
637 return;
638 }
639
Robert Phillipsddc21482019-10-16 14:30:09 -0400640 SkTArray<GrTextureFreedMessage> msgs;
641 fFreedTextureInbox.poll(&msgs);
Brian Osman13dddce2017-05-09 13:19:50 -0400642 for (int i = 0; i < msgs.count(); ++i) {
Brian Salomon238069b2018-07-11 15:58:57 -0400643 SkASSERT(msgs[i].fOwningUniqueID == fContextUniqueID);
Robert Phillipsddc21482019-10-16 14:30:09 -0400644 uint32_t id = msgs[i].fTexture->uniqueID().asUInt();
645 TextureAwaitingUnref* info = fTexturesAwaitingUnref.find(id);
Greg Daniel1a5d2d52019-12-04 11:14:29 -0500646 // If the GrContext was released or abandoned then fTexturesAwaitingUnref should have been
647 // empty and we would have returned early above. Thus, any texture from a message should be
648 // in the list of fTexturesAwaitingUnref.
649 SkASSERT(info);
650 info->unref();
651 if (info->finished()) {
652 fTexturesAwaitingUnref.remove(id);
Greg Danielb2acf0a2018-09-12 09:17:11 -0400653 }
Brian Osman13dddce2017-05-09 13:19:50 -0400654 }
655}
656
bsalomonf320e042015-02-17 15:09:34 -0800657void GrResourceCache::addToNonpurgeableArray(GrGpuResource* resource) {
658 int index = fNonpurgeableResources.count();
659 *fNonpurgeableResources.append() = resource;
660 *resource->cacheAccess().accessCacheIndex() = index;
661}
662
663void GrResourceCache::removeFromNonpurgeableArray(GrGpuResource* resource) {
664 int* index = resource->cacheAccess().accessCacheIndex();
665 // Fill the whole we will create in the array with the tail object, adjust its index, and
666 // then pop the array
667 GrGpuResource* tail = *(fNonpurgeableResources.end() - 1);
668 SkASSERT(fNonpurgeableResources[*index] == resource);
669 fNonpurgeableResources[*index] = tail;
670 *tail->cacheAccess().accessCacheIndex() = *index;
671 fNonpurgeableResources.pop();
672 SkDEBUGCODE(*index = -1);
673}
674
bsalomonddf30e62015-02-19 11:38:44 -0800675uint32_t GrResourceCache::getNextTimestamp() {
676 // If we wrap then all the existing resources will appear older than any resources that get
677 // a timestamp after the wrap.
678 if (0 == fTimestamp) {
679 int count = this->getResourceCount();
680 if (count) {
681 // Reset all the timestamps. We sort the resources by timestamp and then assign
682 // sequential timestamps beginning with 0. This is O(n*lg(n)) but it should be extremely
683 // rare.
684 SkTDArray<GrGpuResource*> sortedPurgeableResources;
685 sortedPurgeableResources.setReserve(fPurgeableQueue.count());
686
687 while (fPurgeableQueue.count()) {
688 *sortedPurgeableResources.append() = fPurgeableQueue.peek();
689 fPurgeableQueue.pop();
690 }
robertphillipsee843b22016-10-04 05:30:20 -0700691
John Stiles886a9042020-07-14 16:28:33 -0400692 SkTQSort(fNonpurgeableResources.begin(), fNonpurgeableResources.end(),
John Stiles6e9ead92020-07-14 00:13:51 +0000693 CompareTimestamp);
bsalomonddf30e62015-02-19 11:38:44 -0800694
695 // Pick resources out of the purgeable and non-purgeable arrays based on lowest
696 // timestamp and assign new timestamps.
697 int currP = 0;
698 int currNP = 0;
699 while (currP < sortedPurgeableResources.count() &&
mtklein56da0252015-11-16 11:16:23 -0800700 currNP < fNonpurgeableResources.count()) {
bsalomonddf30e62015-02-19 11:38:44 -0800701 uint32_t tsP = sortedPurgeableResources[currP]->cacheAccess().timestamp();
702 uint32_t tsNP = fNonpurgeableResources[currNP]->cacheAccess().timestamp();
703 SkASSERT(tsP != tsNP);
704 if (tsP < tsNP) {
705 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
706 } else {
707 // Correct the index in the nonpurgeable array stored on the resource post-sort.
708 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
709 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
710 }
711 }
712
713 // The above loop ended when we hit the end of one array. Finish the other one.
714 while (currP < sortedPurgeableResources.count()) {
715 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
716 }
717 while (currNP < fNonpurgeableResources.count()) {
718 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
719 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
720 }
721
722 // Rebuild the queue.
723 for (int i = 0; i < sortedPurgeableResources.count(); ++i) {
724 fPurgeableQueue.insert(sortedPurgeableResources[i]);
725 }
726
727 this->validate();
728 SkASSERT(count == this->getResourceCount());
729
730 // count should be the next timestamp we return.
731 SkASSERT(fTimestamp == SkToU32(count));
mtklein56da0252015-11-16 11:16:23 -0800732 }
bsalomonddf30e62015-02-19 11:38:44 -0800733 }
734 return fTimestamp++;
735}
736
ericrk0a5fa482015-09-15 14:16:10 -0700737void GrResourceCache::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
738 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
739 fNonpurgeableResources[i]->dumpMemoryStatistics(traceMemoryDump);
740 }
741 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
742 fPurgeableQueue.at(i)->dumpMemoryStatistics(traceMemoryDump);
743 }
744}
745
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500746#if GR_CACHE_STATS
747void GrResourceCache::getStats(Stats* stats) const {
748 stats->reset();
749
750 stats->fTotal = this->getResourceCount();
751 stats->fNumNonPurgeable = fNonpurgeableResources.count();
752 stats->fNumPurgeable = fPurgeableQueue.count();
753
754 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
755 stats->update(fNonpurgeableResources[i]);
756 }
757 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
758 stats->update(fPurgeableQueue.at(i));
759 }
760}
761
762#if GR_TEST_UTILS
763void GrResourceCache::dumpStats(SkString* out) const {
764 this->validate();
765
766 Stats stats;
767
768 this->getStats(&stats);
769
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500770 float byteUtilization = (100.f * fBudgetedBytes) / fMaxBytes;
771
Robert Phillipscf39f372019-09-03 10:29:20 -0400772 out->appendf("Budget: %d bytes\n", (int)fMaxBytes);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500773 out->appendf("\t\tEntry Count: current %d"
Robert Phillipscf39f372019-09-03 10:29:20 -0400774 " (%d budgeted, %d wrapped, %d locked, %d scratch), high %d\n",
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500775 stats.fTotal, fBudgetedCount, stats.fWrapped, stats.fNumNonPurgeable,
Robert Phillipscf39f372019-09-03 10:29:20 -0400776 stats.fScratch, fHighWaterCount);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500777 out->appendf("\t\tEntry Bytes: current %d (budgeted %d, %.2g%% full, %d unbudgeted) high %d\n",
778 SkToInt(fBytes), SkToInt(fBudgetedBytes), byteUtilization,
779 SkToInt(stats.fUnbudgetedSize), SkToInt(fHighWaterBytes));
780}
781
782void GrResourceCache::dumpStatsKeyValuePairs(SkTArray<SkString>* keys,
783 SkTArray<double>* values) const {
784 this->validate();
785
786 Stats stats;
787 this->getStats(&stats);
788
789 keys->push_back(SkString("gpu_cache_purgable_entries")); values->push_back(stats.fNumPurgeable);
790}
791#endif
792
793#endif
794
bsalomon71cb0c22014-11-14 12:10:14 -0800795#ifdef SK_DEBUG
bsalomon0ea80f42015-02-11 10:49:59 -0800796void GrResourceCache::validate() const {
bsalomonc2f35b72015-01-23 07:19:22 -0800797 // Reduce the frequency of validations for large resource counts.
798 static SkRandom gRandom;
799 int mask = (SkNextPow2(fCount + 1) >> 5) - 1;
800 if (~mask && (gRandom.nextU() & mask)) {
801 return;
802 }
803
bsalomonf320e042015-02-17 15:09:34 -0800804 struct Stats {
805 size_t fBytes;
806 int fBudgetedCount;
807 size_t fBudgetedBytes;
808 int fLocked;
809 int fScratch;
810 int fCouldBeScratch;
811 int fContent;
812 const ScratchMap* fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800813 const UniqueHash* fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800814
bsalomonf320e042015-02-17 15:09:34 -0800815 Stats(const GrResourceCache* cache) {
816 memset(this, 0, sizeof(*this));
817 fScratchMap = &cache->fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800818 fUniqueHash = &cache->fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800819 }
820
bsalomonf320e042015-02-17 15:09:34 -0800821 void update(GrGpuResource* resource) {
822 fBytes += resource->gpuMemorySize();
bsalomondace19e2014-11-17 07:34:06 -0800823
Brian Salomon614c1a82018-12-19 15:42:06 -0500824 if (!resource->resourcePriv().isPurgeable()) {
bsalomonf320e042015-02-17 15:09:34 -0800825 ++fLocked;
826 }
bsalomon9f2d1572015-02-17 11:47:40 -0800827
robertphillipsc4ed6842016-05-24 14:17:12 -0700828 const GrScratchKey& scratchKey = resource->resourcePriv().getScratchKey();
829 const GrUniqueKey& uniqueKey = resource->getUniqueKey();
830
bsalomonf320e042015-02-17 15:09:34 -0800831 if (resource->cacheAccess().isScratch()) {
robertphillipsc4ed6842016-05-24 14:17:12 -0700832 SkASSERT(!uniqueKey.isValid());
bsalomonf320e042015-02-17 15:09:34 -0800833 ++fScratch;
robertphillipsc4ed6842016-05-24 14:17:12 -0700834 SkASSERT(fScratchMap->countForKey(scratchKey));
kkinnunen2e6055b2016-04-22 01:48:29 -0700835 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
robertphillipsc4ed6842016-05-24 14:17:12 -0700836 } else if (scratchKey.isValid()) {
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500837 SkASSERT(GrBudgetedType::kBudgeted != resource->resourcePriv().budgetedType() ||
robertphillipsc4ed6842016-05-24 14:17:12 -0700838 uniqueKey.isValid());
839 if (!uniqueKey.isValid()) {
mtklein4e976072016-08-08 09:06:27 -0700840 ++fCouldBeScratch;
robertphillipsc4ed6842016-05-24 14:17:12 -0700841 SkASSERT(fScratchMap->countForKey(scratchKey));
842 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700843 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
bsalomonf320e042015-02-17 15:09:34 -0800844 }
bsalomon8718aaf2015-02-19 07:24:21 -0800845 if (uniqueKey.isValid()) {
bsalomonf320e042015-02-17 15:09:34 -0800846 ++fContent;
bsalomon8718aaf2015-02-19 07:24:21 -0800847 SkASSERT(fUniqueHash->find(uniqueKey) == resource);
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500848 SkASSERT(GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType() ||
Brian Osman0562eb92017-05-08 11:16:39 -0400849 resource->resourcePriv().refsWrappedObjects());
robertphillipsc4ed6842016-05-24 14:17:12 -0700850
851 if (scratchKey.isValid()) {
852 SkASSERT(!fScratchMap->has(resource, scratchKey));
853 }
bsalomonf320e042015-02-17 15:09:34 -0800854 }
855
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500856 if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
bsalomonf320e042015-02-17 15:09:34 -0800857 ++fBudgetedCount;
858 fBudgetedBytes += resource->gpuMemorySize();
859 }
bsalomon9f2d1572015-02-17 11:47:40 -0800860 }
bsalomonf320e042015-02-17 15:09:34 -0800861 };
862
robertphillipsc4ed6842016-05-24 14:17:12 -0700863 {
robertphillipsc4ed6842016-05-24 14:17:12 -0700864 int count = 0;
Mike Kleincff63962020-03-14 16:22:45 -0500865 fScratchMap.foreach([&](const GrGpuResource& resource) {
866 SkASSERT(resource.resourcePriv().getScratchKey().isValid());
867 SkASSERT(!resource.getUniqueKey().isValid());
robertphillipsc4ed6842016-05-24 14:17:12 -0700868 count++;
Mike Kleincff63962020-03-14 16:22:45 -0500869 });
870 SkASSERT(count == fScratchMap.count());
robertphillipsc4ed6842016-05-24 14:17:12 -0700871 }
872
bsalomonf320e042015-02-17 15:09:34 -0800873 Stats stats(this);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400874 size_t purgeableBytes = 0;
Brian Salomon2c791fc2019-04-02 11:52:03 -0400875 int numBudgetedResourcesFlushWillMakePurgeable = 0;
bsalomonf320e042015-02-17 15:09:34 -0800876
877 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500878 SkASSERT(!fNonpurgeableResources[i]->resourcePriv().isPurgeable() ||
bsalomon3f324322015-04-08 11:01:54 -0700879 fNewlyPurgeableResourceForValidation == fNonpurgeableResources[i]);
bsalomonf320e042015-02-17 15:09:34 -0800880 SkASSERT(*fNonpurgeableResources[i]->cacheAccess().accessCacheIndex() == i);
881 SkASSERT(!fNonpurgeableResources[i]->wasDestroyed());
Brian Salomon2c791fc2019-04-02 11:52:03 -0400882 if (fNonpurgeableResources[i]->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted &&
883 !fNonpurgeableResources[i]->cacheAccess().hasRef() &&
884 fNewlyPurgeableResourceForValidation != fNonpurgeableResources[i]) {
Brian Salomon2c791fc2019-04-02 11:52:03 -0400885 ++numBudgetedResourcesFlushWillMakePurgeable;
886 }
bsalomonf320e042015-02-17 15:09:34 -0800887 stats.update(fNonpurgeableResources[i]);
bsalomon71cb0c22014-11-14 12:10:14 -0800888 }
bsalomon9f2d1572015-02-17 11:47:40 -0800889 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500890 SkASSERT(fPurgeableQueue.at(i)->resourcePriv().isPurgeable());
bsalomonf320e042015-02-17 15:09:34 -0800891 SkASSERT(*fPurgeableQueue.at(i)->cacheAccess().accessCacheIndex() == i);
892 SkASSERT(!fPurgeableQueue.at(i)->wasDestroyed());
893 stats.update(fPurgeableQueue.at(i));
Derek Sollenbergeree479142017-05-24 11:41:33 -0400894 purgeableBytes += fPurgeableQueue.at(i)->gpuMemorySize();
bsalomon9f2d1572015-02-17 11:47:40 -0800895 }
896
bsalomonf320e042015-02-17 15:09:34 -0800897 SkASSERT(fCount == this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800898 SkASSERT(fBudgetedCount <= fCount);
bsalomonf320e042015-02-17 15:09:34 -0800899 SkASSERT(fBudgetedBytes <= fBytes);
900 SkASSERT(stats.fBytes == fBytes);
Brian Salomon2c791fc2019-04-02 11:52:03 -0400901 SkASSERT(fNumBudgetedResourcesFlushWillMakePurgeable ==
902 numBudgetedResourcesFlushWillMakePurgeable);
bsalomonf320e042015-02-17 15:09:34 -0800903 SkASSERT(stats.fBudgetedBytes == fBudgetedBytes);
904 SkASSERT(stats.fBudgetedCount == fBudgetedCount);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400905 SkASSERT(purgeableBytes == fPurgeableBytes);
bsalomon71cb0c22014-11-14 12:10:14 -0800906#if GR_CACHE_STATS
bsalomondace19e2014-11-17 07:34:06 -0800907 SkASSERT(fBudgetedHighWaterCount <= fHighWaterCount);
908 SkASSERT(fBudgetedHighWaterBytes <= fHighWaterBytes);
bsalomonf320e042015-02-17 15:09:34 -0800909 SkASSERT(fBytes <= fHighWaterBytes);
910 SkASSERT(fCount <= fHighWaterCount);
911 SkASSERT(fBudgetedBytes <= fBudgetedHighWaterBytes);
912 SkASSERT(fBudgetedCount <= fBudgetedHighWaterCount);
bsalomon71cb0c22014-11-14 12:10:14 -0800913#endif
bsalomon8718aaf2015-02-19 07:24:21 -0800914 SkASSERT(stats.fContent == fUniqueHash.count());
bsalomonf320e042015-02-17 15:09:34 -0800915 SkASSERT(stats.fScratch + stats.fCouldBeScratch == fScratchMap.count());
bsalomon71cb0c22014-11-14 12:10:14 -0800916
bsalomon3f324322015-04-08 11:01:54 -0700917 // This assertion is not currently valid because we can be in recursive notifyCntReachedZero()
bsalomon12299ab2014-11-14 13:33:09 -0800918 // calls. This will be fixed when subresource registration is explicit.
bsalomondace19e2014-11-17 07:34:06 -0800919 // bool overBudget = budgetedBytes > fMaxBytes || budgetedCount > fMaxCount;
bsalomon12299ab2014-11-14 13:33:09 -0800920 // SkASSERT(!overBudget || locked == count || fPurging);
bsalomon71cb0c22014-11-14 12:10:14 -0800921}
bsalomonf320e042015-02-17 15:09:34 -0800922
923bool GrResourceCache::isInCache(const GrGpuResource* resource) const {
924 int index = *resource->cacheAccess().accessCacheIndex();
925 if (index < 0) {
926 return false;
927 }
928 if (index < fPurgeableQueue.count() && fPurgeableQueue.at(index) == resource) {
929 return true;
930 }
931 if (index < fNonpurgeableResources.count() && fNonpurgeableResources[index] == resource) {
932 return true;
933 }
934 SkDEBUGFAIL("Resource index should be -1 or the resource should be in the cache.");
935 return false;
936}
937
bsalomon71cb0c22014-11-14 12:10:14 -0800938#endif