blob: cfa0c96246c7995bb8b2fa9dc50c16a0e0242788 [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>
Adlai Hollere1c8a382021-04-08 15:30:12 -040010#include <vector>
Robert Phillips4e105e22020-07-16 09:18:50 -040011#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/private/GrSingleOwner.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/private/SkTo.h"
14#include "include/utils/SkRandom.h"
Ben Wagner21bca282019-05-15 10:15:52 -040015#include "src/core/SkMessageBus.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/core/SkOpts.h"
17#include "src/core/SkScopeExit.h"
John Stiles6e9ead92020-07-14 00:13:51 +000018#include "src/core/SkTSort.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrCaps.h"
Adlai Hollera0693042020-10-14 11:23:11 -040020#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrGpuResourceCacheAccess.h"
22#include "src/gpu/GrProxyProvider.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000023#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrTextureProxyCacheAccess.h"
Robert Phillipsd464feb2020-10-08 11:00:02 -040025#include "src/gpu/GrThreadSafeCache.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/GrTracing.h"
27#include "src/gpu/SkGr.h"
bsalomon71cb0c22014-11-14 12:10:14 -080028
Robert Phillipse7a959d2021-03-11 14:44:42 -050029DECLARE_SKMESSAGEBUS_MESSAGE(GrUniqueKeyInvalidatedMessage, uint32_t, true);
bsalomon71cb0c22014-11-14 12:10:14 -080030
Robert Phillipsd074b622021-03-15 08:49:24 -040031DECLARE_SKMESSAGEBUS_MESSAGE(GrTextureFreedMessage, GrDirectContext::DirectContextID, true);
Brian Osman13dddce2017-05-09 13:19:50 -040032
Adlai Holler33dbd652020-06-01 12:35:42 -040033#define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(fSingleOwner)
Brian Salomon8f8995a2018-10-15 14:32:15 -040034
bsalomon71cb0c22014-11-14 12:10:14 -080035//////////////////////////////////////////////////////////////////////////////
36
bsalomon7775c852014-12-30 12:50:52 -080037GrScratchKey::ResourceType GrScratchKey::GenerateResourceType() {
Mike Klein0ec1c572018-12-04 11:52:51 -050038 static std::atomic<int32_t> nextType{INHERITED::kInvalidDomain + 1};
bsalomonfe369ee2014-11-10 11:59:06 -080039
Adlai Holler4888cda2020-11-06 16:37:37 -050040 int32_t type = nextType.fetch_add(1, std::memory_order_relaxed);
Ben Wagner9bc36fd2018-06-15 14:23:36 -040041 if (type > SkTo<int32_t>(UINT16_MAX)) {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040042 SK_ABORT("Too many Resource Types");
bsalomon71cb0c22014-11-14 12:10:14 -080043 }
44
45 return static_cast<ResourceType>(type);
46}
47
bsalomon8718aaf2015-02-19 07:24:21 -080048GrUniqueKey::Domain GrUniqueKey::GenerateDomain() {
Mike Klein0ec1c572018-12-04 11:52:51 -050049 static std::atomic<int32_t> nextDomain{INHERITED::kInvalidDomain + 1};
bsalomon7775c852014-12-30 12:50:52 -080050
Adlai Holler4888cda2020-11-06 16:37:37 -050051 int32_t domain = nextDomain.fetch_add(1, std::memory_order_relaxed);
Ben Wagner397ee0e2018-06-15 15:13:26 -040052 if (domain > SkTo<int32_t>(UINT16_MAX)) {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040053 SK_ABORT("Too many GrUniqueKey Domains");
bsalomon7775c852014-12-30 12:50:52 -080054 }
bsalomon24db3b12015-01-23 04:24:04 -080055
56 return static_cast<Domain>(domain);
57}
bsalomon3f324322015-04-08 11:01:54 -070058
bsalomon24db3b12015-01-23 04:24:04 -080059uint32_t GrResourceKeyHash(const uint32_t* data, size_t size) {
mtklein4e976072016-08-08 09:06:27 -070060 return SkOpts::hash(data, size);
bsalomon7775c852014-12-30 12:50:52 -080061}
62
bsalomonfe369ee2014-11-10 11:59:06 -080063//////////////////////////////////////////////////////////////////////////////
64
bsalomon0ea80f42015-02-11 10:49:59 -080065class GrResourceCache::AutoValidate : ::SkNoncopyable {
bsalomon71cb0c22014-11-14 12:10:14 -080066public:
bsalomon0ea80f42015-02-11 10:49:59 -080067 AutoValidate(GrResourceCache* cache) : fCache(cache) { cache->validate(); }
bsalomon71cb0c22014-11-14 12:10:14 -080068 ~AutoValidate() { fCache->validate(); }
69private:
bsalomon0ea80f42015-02-11 10:49:59 -080070 GrResourceCache* fCache;
bsalomon71cb0c22014-11-14 12:10:14 -080071};
72
Brian Salomon876a0172019-03-08 11:12:14 -050073//////////////////////////////////////////////////////////////////////////////
74
Robert Phillipsddc21482019-10-16 14:30:09 -040075inline GrResourceCache::TextureAwaitingUnref::TextureAwaitingUnref() = default;
Brian Salomon876a0172019-03-08 11:12:14 -050076
Robert Phillipsddc21482019-10-16 14:30:09 -040077inline GrResourceCache::TextureAwaitingUnref::TextureAwaitingUnref(GrTexture* texture)
78 : fTexture(texture), fNumUnrefs(1) {}
Brian Salomon876a0172019-03-08 11:12:14 -050079
Robert Phillipsddc21482019-10-16 14:30:09 -040080inline GrResourceCache::TextureAwaitingUnref::TextureAwaitingUnref(TextureAwaitingUnref&& that) {
Adlai Holler5ba50af2020-04-29 21:11:14 -040081 fTexture = std::exchange(that.fTexture, nullptr);
82 fNumUnrefs = std::exchange(that.fNumUnrefs, 0);
Brian Salomon876a0172019-03-08 11:12:14 -050083}
84
Robert Phillipsddc21482019-10-16 14:30:09 -040085inline GrResourceCache::TextureAwaitingUnref& GrResourceCache::TextureAwaitingUnref::operator=(
86 TextureAwaitingUnref&& that) {
Adlai Holler5ba50af2020-04-29 21:11:14 -040087 fTexture = std::exchange(that.fTexture, nullptr);
88 fNumUnrefs = std::exchange(that.fNumUnrefs, 0);
Brian Salomon876a0172019-03-08 11:12:14 -050089 return *this;
90}
91
Robert Phillipsddc21482019-10-16 14:30:09 -040092inline GrResourceCache::TextureAwaitingUnref::~TextureAwaitingUnref() {
93 if (fTexture) {
Brian Salomon876a0172019-03-08 11:12:14 -050094 for (int i = 0; i < fNumUnrefs; ++i) {
Robert Phillipsddc21482019-10-16 14:30:09 -040095 fTexture->unref();
Brian Salomon876a0172019-03-08 11:12:14 -050096 }
97 }
98}
99
Robert Phillipsddc21482019-10-16 14:30:09 -0400100inline void GrResourceCache::TextureAwaitingUnref::TextureAwaitingUnref::addRef() { ++fNumUnrefs; }
Brian Salomon876a0172019-03-08 11:12:14 -0500101
Robert Phillipsddc21482019-10-16 14:30:09 -0400102inline void GrResourceCache::TextureAwaitingUnref::unref() {
Brian Salomon876a0172019-03-08 11:12:14 -0500103 SkASSERT(fNumUnrefs > 0);
Robert Phillipsddc21482019-10-16 14:30:09 -0400104 fTexture->unref();
Brian Salomon876a0172019-03-08 11:12:14 -0500105 --fNumUnrefs;
106}
107
Robert Phillipsddc21482019-10-16 14:30:09 -0400108inline bool GrResourceCache::TextureAwaitingUnref::finished() { return !fNumUnrefs; }
Brian Salomon876a0172019-03-08 11:12:14 -0500109
110//////////////////////////////////////////////////////////////////////////////
robertphillipsee843b22016-10-04 05:30:20 -0700111
Robert Phillipsd074b622021-03-15 08:49:24 -0400112GrResourceCache::GrResourceCache(GrSingleOwner* singleOwner,
113 GrDirectContext::DirectContextID owningContextID,
114 uint32_t familyID)
115 : fInvalidUniqueKeyInbox(familyID)
116 , fFreedTextureInbox(owningContextID)
117 , fOwningContextID(owningContextID)
118 , fContextUniqueID(familyID)
Brian Salomonbe1084b2021-01-26 13:29:30 -0500119 , fSingleOwner(singleOwner) {
Robert Phillipsd074b622021-03-15 08:49:24 -0400120 SkASSERT(owningContextID.isValid());
121 SkASSERT(familyID != SK_InvalidUniqueID);
bsalomon71cb0c22014-11-14 12:10:14 -0800122}
123
bsalomon0ea80f42015-02-11 10:49:59 -0800124GrResourceCache::~GrResourceCache() {
bsalomonc8dc1f72014-08-21 13:02:13 -0700125 this->releaseAll();
126}
127
Robert Phillipscf39f372019-09-03 10:29:20 -0400128void GrResourceCache::setLimit(size_t bytes) {
bsalomon71cb0c22014-11-14 12:10:14 -0800129 fMaxBytes = bytes;
130 this->purgeAsNeeded();
131}
132
bsalomon0ea80f42015-02-11 10:49:59 -0800133void GrResourceCache::insertResource(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400134 ASSERT_SINGLE_OWNER
bsalomon49f085d2014-09-05 13:34:00 -0700135 SkASSERT(resource);
bsalomon16961262014-08-26 14:01:07 -0700136 SkASSERT(!this->isInCache(resource));
bsalomonf320e042015-02-17 15:09:34 -0800137 SkASSERT(!resource->wasDestroyed());
Brian Salomon614c1a82018-12-19 15:42:06 -0500138 SkASSERT(!resource->resourcePriv().isPurgeable());
bsalomonddf30e62015-02-19 11:38:44 -0800139
140 // We must set the timestamp before adding to the array in case the timestamp wraps and we wind
141 // up iterating over all the resources that already have timestamps.
142 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
143
bsalomonf320e042015-02-17 15:09:34 -0800144 this->addToNonpurgeableArray(resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800145
bsalomondace19e2014-11-17 07:34:06 -0800146 size_t size = resource->gpuMemorySize();
bsalomonf320e042015-02-17 15:09:34 -0800147 SkDEBUGCODE(++fCount;)
bsalomon84c8e622014-11-17 09:33:27 -0800148 fBytes += size;
bsalomon82b1d622014-11-14 13:59:57 -0800149#if GR_CACHE_STATS
Brian Osman788b9162020-02-07 10:36:46 -0500150 fHighWaterCount = std::max(this->getResourceCount(), fHighWaterCount);
151 fHighWaterBytes = std::max(fBytes, fHighWaterBytes);
bsalomon82b1d622014-11-14 13:59:57 -0800152#endif
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500153 if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
bsalomondace19e2014-11-17 07:34:06 -0800154 ++fBudgetedCount;
155 fBudgetedBytes += size;
Brian Osman39c08ac2017-07-26 09:36:09 -0400156 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800157 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomondace19e2014-11-17 07:34:06 -0800158#if GR_CACHE_STATS
Brian Osman788b9162020-02-07 10:36:46 -0500159 fBudgetedHighWaterCount = std::max(fBudgetedCount, fBudgetedHighWaterCount);
160 fBudgetedHighWaterBytes = std::max(fBudgetedBytes, fBudgetedHighWaterBytes);
bsalomondace19e2014-11-17 07:34:06 -0800161#endif
162 }
Greg Danielda642612021-02-09 18:04:02 -0500163 SkASSERT(!resource->cacheAccess().isUsableAsScratch());
bsalomon71cb0c22014-11-14 12:10:14 -0800164 this->purgeAsNeeded();
bsalomonc8dc1f72014-08-21 13:02:13 -0700165}
166
bsalomon0ea80f42015-02-11 10:49:59 -0800167void GrResourceCache::removeResource(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400168 ASSERT_SINGLE_OWNER
bsalomon9f2d1572015-02-17 11:47:40 -0800169 this->validate();
bsalomon16961262014-08-26 14:01:07 -0700170 SkASSERT(this->isInCache(resource));
bsalomondace19e2014-11-17 07:34:06 -0800171
Derek Sollenbergeree479142017-05-24 11:41:33 -0400172 size_t size = resource->gpuMemorySize();
Brian Salomon614c1a82018-12-19 15:42:06 -0500173 if (resource->resourcePriv().isPurgeable()) {
bsalomon9f2d1572015-02-17 11:47:40 -0800174 fPurgeableQueue.remove(resource);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400175 fPurgeableBytes -= size;
bsalomonf320e042015-02-17 15:09:34 -0800176 } else {
177 this->removeFromNonpurgeableArray(resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800178 }
179
bsalomonf320e042015-02-17 15:09:34 -0800180 SkDEBUGCODE(--fCount;)
bsalomondace19e2014-11-17 07:34:06 -0800181 fBytes -= size;
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500182 if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
bsalomondace19e2014-11-17 07:34:06 -0800183 --fBudgetedCount;
184 fBudgetedBytes -= size;
Brian Osman39c08ac2017-07-26 09:36:09 -0400185 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800186 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomondace19e2014-11-17 07:34:06 -0800187 }
188
Greg Danielda642612021-02-09 18:04:02 -0500189 if (resource->cacheAccess().isUsableAsScratch()) {
bsalomon3582d3e2015-02-13 14:20:05 -0800190 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
bsalomon744998e2014-08-28 09:54:34 -0700191 }
bsalomon8718aaf2015-02-19 07:24:21 -0800192 if (resource->getUniqueKey().isValid()) {
193 fUniqueHash.remove(resource->getUniqueKey());
bsalomon8b79d232014-11-10 10:19:06 -0800194 }
bsalomonb436ed62014-11-17 12:15:56 -0800195 this->validate();
bsalomonc8dc1f72014-08-21 13:02:13 -0700196}
197
bsalomon0ea80f42015-02-11 10:49:59 -0800198void GrResourceCache::abandonAll() {
bsalomon71cb0c22014-11-14 12:10:14 -0800199 AutoValidate av(this);
200
Brian Salomon876a0172019-03-08 11:12:14 -0500201 // We need to make sure to free any resources that were waiting on a free message but never
202 // received one.
Robert Phillipsddc21482019-10-16 14:30:09 -0400203 fTexturesAwaitingUnref.reset();
Greg Danielb2acf0a2018-09-12 09:17:11 -0400204
bsalomonf320e042015-02-17 15:09:34 -0800205 while (fNonpurgeableResources.count()) {
206 GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
207 SkASSERT(!back->wasDestroyed());
208 back->cacheAccess().abandon();
bsalomonc8dc1f72014-08-21 13:02:13 -0700209 }
bsalomonf320e042015-02-17 15:09:34 -0800210
211 while (fPurgeableQueue.count()) {
212 GrGpuResource* top = fPurgeableQueue.peek();
213 SkASSERT(!top->wasDestroyed());
214 top->cacheAccess().abandon();
215 }
216
Robert Phillipseb999bc2020-11-03 08:41:47 -0500217 fThreadSafeCache->dropAllRefs();
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
Robert Phillipsd464feb2020-10-08 11:00:02 -0400233 fThreadSafeCache->dropAllRefs();
Robert Phillips12d06a32020-09-16 12:31:34 -0400234
Brian Osman13dddce2017-05-09 13:19:50 -0400235 this->processFreedGpuResources();
236
Greg Danielc27eb722018-08-10 09:48:08 -0400237 // We need to make sure to free any resources that were waiting on a free message but never
238 // received one.
Robert Phillipsddc21482019-10-16 14:30:09 -0400239 fTexturesAwaitingUnref.reset();
Greg Danielc27eb722018-08-10 09:48:08 -0400240
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500241 SkASSERT(fProxyProvider); // better have called setProxyProvider
Robert Phillipsd464feb2020-10-08 11:00:02 -0400242 SkASSERT(fThreadSafeCache); // better have called setThreadSafeCache too
Robert Phillips12d06a32020-09-16 12:31:34 -0400243
Robert Phillips3ec95732017-09-29 15:10:39 -0400244 // We must remove the uniqueKeys from the proxies here. While they possess a uniqueKey
245 // they also have a raw pointer back to this class (which is presumably going away)!
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500246 fProxyProvider->removeAllUniqueKeys();
Robert Phillips45a6f142017-09-29 09:49:41 -0400247
Brian Salomon614c1a82018-12-19 15:42:06 -0500248 while (fNonpurgeableResources.count()) {
bsalomonf320e042015-02-17 15:09:34 -0800249 GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
250 SkASSERT(!back->wasDestroyed());
251 back->cacheAccess().release();
bsalomonc8dc1f72014-08-21 13:02:13 -0700252 }
bsalomonf320e042015-02-17 15:09:34 -0800253
254 while (fPurgeableQueue.count()) {
255 GrGpuResource* top = fPurgeableQueue.peek();
256 SkASSERT(!top->wasDestroyed());
257 top->cacheAccess().release();
258 }
259
bsalomon744998e2014-08-28 09:54:34 -0700260 SkASSERT(!fScratchMap.count());
bsalomon8718aaf2015-02-19 07:24:21 -0800261 SkASSERT(!fUniqueHash.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700262 SkASSERT(!fCount);
bsalomonf320e042015-02-17 15:09:34 -0800263 SkASSERT(!this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800264 SkASSERT(!fBytes);
265 SkASSERT(!fBudgetedCount);
266 SkASSERT(!fBudgetedBytes);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400267 SkASSERT(!fPurgeableBytes);
Robert Phillipsddc21482019-10-16 14:30:09 -0400268 SkASSERT(!fTexturesAwaitingUnref.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700269}
bsalomonbcf0a522014-10-08 08:40:09 -0700270
Brian Salomon2c791fc2019-04-02 11:52:03 -0400271void GrResourceCache::refResource(GrGpuResource* resource) {
272 SkASSERT(resource);
273 SkASSERT(resource->getContext()->priv().getResourceCache() == this);
274 if (resource->cacheAccess().hasRef()) {
275 resource->ref();
276 } else {
277 this->refAndMakeResourceMRU(resource);
278 }
279 this->validate();
280}
281
bsalomon0ea80f42015-02-11 10:49:59 -0800282class GrResourceCache::AvailableForScratchUse {
bsalomonbcf0a522014-10-08 08:40:09 -0700283public:
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400284 AvailableForScratchUse() { }
bsalomonbcf0a522014-10-08 08:40:09 -0700285
286 bool operator()(const GrGpuResource* resource) const {
Greg Danielda642612021-02-09 18:04:02 -0500287 // Everything that is in the scratch map should be usable as a
288 // scratch resource.
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400289 return true;
bsalomonbcf0a522014-10-08 08:40:09 -0700290 }
bsalomonbcf0a522014-10-08 08:40:09 -0700291};
292
Robert Phillipsaee18c92019-09-06 11:48:27 -0400293GrGpuResource* GrResourceCache::findAndRefScratchResource(const GrScratchKey& scratchKey) {
bsalomon7775c852014-12-30 12:50:52 -0800294 SkASSERT(scratchKey.isValid());
robertphillipsee843b22016-10-04 05:30:20 -0700295
Robert Phillipsaee18c92019-09-06 11:48:27 -0400296 GrGpuResource* resource = fScratchMap.find(scratchKey, AvailableForScratchUse());
bsalomon71cb0c22014-11-14 12:10:14 -0800297 if (resource) {
Greg Danielda642612021-02-09 18:04:02 -0500298 fScratchMap.remove(scratchKey, 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());
Greg Danielda642612021-02-09 18:04:02 -0500308 if (resource->cacheAccess().isUsableAsScratch()) {
robertphillipsc4ed6842016-05-24 14:17:12 -0700309 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();
Greg Danielda642612021-02-09 18:04:02 -0500322 if (resource->cacheAccess().isUsableAsScratch()) {
robertphillipsc4ed6842016-05-24 14:17:12 -0700323 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
Greg Danielda642612021-02-09 18:04:02 -0500359 // from the ScratchMap. The isUsableAsScratch call depends on us not adding the new
360 // unique key until after this check.
361 if (resource->cacheAccess().isUsableAsScratch()) {
robertphillipsc4ed6842016-05-24 14:17:12 -0700362 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);
Greg Daniel1fd8ac82020-12-11 11:22:01 -0500385 } else if (!resource->cacheAccess().hasRefOrCommandBufferUsage() &&
Brian Salomon2c791fc2019-04-02 11:52:03 -0400386 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
Greg Danielda642612021-02-09 18:04:02 -0500396void GrResourceCache::notifyARefCntReachedZero(GrGpuResource* resource,
397 GrGpuResource::LastRemovedRef removedRef) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400398 ASSERT_SINGLE_OWNER
bsalomon71cb0c22014-11-14 12:10:14 -0800399 SkASSERT(resource);
bsalomon3f324322015-04-08 11:01:54 -0700400 SkASSERT(!resource->wasDestroyed());
bsalomon71cb0c22014-11-14 12:10:14 -0800401 SkASSERT(this->isInCache(resource));
bsalomon3f324322015-04-08 11:01:54 -0700402 // This resource should always be in the nonpurgeable array when this function is called. It
403 // will be moved to the queue if it is newly purgeable.
404 SkASSERT(fNonpurgeableResources[*resource->cacheAccess().accessCacheIndex()] == resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800405
Greg Danielda642612021-02-09 18:04:02 -0500406 if (removedRef == GrGpuResource::LastRemovedRef::kMainRef) {
407 if (resource->cacheAccess().isUsableAsScratch()) {
408 fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
409 }
410 }
411
412 if (resource->cacheAccess().hasRefOrCommandBufferUsage()) {
413 this->validate();
414 return;
415 }
416
bsalomon3f324322015-04-08 11:01:54 -0700417#ifdef SK_DEBUG
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400418 // When the timestamp overflows validate() is called. validate() checks that resources in
419 // the nonpurgeable array are indeed not purgeable. However, the movement from the array to
420 // the purgeable queue happens just below in this function. So we mark it as an exception.
421 if (resource->resourcePriv().isPurgeable()) {
422 fNewlyPurgeableResourceForValidation = resource;
bsalomon3f324322015-04-08 11:01:54 -0700423 }
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400424#endif
425 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
426 SkDEBUGCODE(fNewlyPurgeableResourceForValidation = nullptr);
bsalomon3f324322015-04-08 11:01:54 -0700427
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400428 if (!resource->resourcePriv().isPurgeable() &&
429 resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
430 ++fNumBudgetedResourcesFlushWillMakePurgeable;
bsalomon3f324322015-04-08 11:01:54 -0700431 }
432
Brian Salomon9bc76d92019-01-24 12:18:33 -0500433 if (!resource->resourcePriv().isPurgeable()) {
434 this->validate();
435 return;
436 }
437
bsalomonf320e042015-02-17 15:09:34 -0800438 this->removeFromNonpurgeableArray(resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800439 fPurgeableQueue.insert(resource);
Brian Salomon5e150852017-03-22 14:53:13 -0400440 resource->cacheAccess().setTimeWhenResourceBecomePurgeable();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400441 fPurgeableBytes += resource->gpuMemorySize();
bsalomon71cb0c22014-11-14 12:10:14 -0800442
Greg Daniel303e83e2018-09-10 14:10:19 -0400443 bool hasUniqueKey = resource->getUniqueKey().isValid();
444
Brian Salomon9bc76d92019-01-24 12:18:33 -0500445 GrBudgetedType budgetedType = resource->resourcePriv().budgetedType();
Brian Salomon614c1a82018-12-19 15:42:06 -0500446
Brian Salomon9bc76d92019-01-24 12:18:33 -0500447 if (budgetedType == GrBudgetedType::kBudgeted) {
448 // Purge the resource immediately if we're over budget
449 // Also purge if the resource has neither a valid scratch key nor a unique key.
450 bool hasKey = resource->resourcePriv().getScratchKey().isValid() || hasUniqueKey;
451 if (!this->overBudget() && hasKey) {
452 return;
453 }
454 } else {
455 // We keep unbudgeted resources with a unique key in the purgeable queue of the cache so
456 // they can be reused again by the image connected to the unique key.
457 if (hasUniqueKey && budgetedType == GrBudgetedType::kUnbudgetedCacheable) {
458 return;
459 }
460 // Check whether this resource could still be used as a scratch resource.
461 if (!resource->resourcePriv().refsWrappedObjects() &&
462 resource->resourcePriv().getScratchKey().isValid()) {
463 // We won't purge an existing resource to make room for this one.
Robert Phillipscf39f372019-09-03 10:29:20 -0400464 if (this->wouldFit(resource->gpuMemorySize())) {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500465 resource->resourcePriv().makeBudgeted();
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500466 return;
467 }
bsalomonc2f35b72015-01-23 07:19:22 -0800468 }
bsalomonc2f35b72015-01-23 07:19:22 -0800469 }
Brian Salomon9bc76d92019-01-24 12:18:33 -0500470
bsalomonf320e042015-02-17 15:09:34 -0800471 SkDEBUGCODE(int beforeCount = this->getResourceCount();)
bsalomon9f2d1572015-02-17 11:47:40 -0800472 resource->cacheAccess().release();
473 // We should at least free this resource, perhaps dependent resources as well.
bsalomonf320e042015-02-17 15:09:34 -0800474 SkASSERT(this->getResourceCount() < beforeCount);
bsalomon71cb0c22014-11-14 12:10:14 -0800475 this->validate();
476}
477
bsalomon0ea80f42015-02-11 10:49:59 -0800478void GrResourceCache::didChangeBudgetStatus(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400479 ASSERT_SINGLE_OWNER
bsalomon84c8e622014-11-17 09:33:27 -0800480 SkASSERT(resource);
481 SkASSERT(this->isInCache(resource));
482
483 size_t size = resource->gpuMemorySize();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500484 // Changing from BudgetedType::kUnbudgetedCacheable to another budgeted type could make
485 // resource become purgeable. However, we should never allow that transition. Wrapped
486 // resources are the only resources that can be in that state and they aren't allowed to
487 // transition from one budgeted state to another.
488 SkDEBUGCODE(bool wasPurgeable = resource->resourcePriv().isPurgeable());
489 if (resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
bsalomon84c8e622014-11-17 09:33:27 -0800490 ++fBudgetedCount;
491 fBudgetedBytes += size;
bsalomonafe30052015-01-16 07:32:33 -0800492#if GR_CACHE_STATS
Brian Osman788b9162020-02-07 10:36:46 -0500493 fBudgetedHighWaterBytes = std::max(fBudgetedBytes, fBudgetedHighWaterBytes);
494 fBudgetedHighWaterCount = std::max(fBudgetedCount, fBudgetedHighWaterCount);
bsalomonafe30052015-01-16 07:32:33 -0800495#endif
Greg Daniel1fd8ac82020-12-11 11:22:01 -0500496 if (!resource->resourcePriv().isPurgeable() &&
497 !resource->cacheAccess().hasRefOrCommandBufferUsage()) {
Brian Salomon2c791fc2019-04-02 11:52:03 -0400498 ++fNumBudgetedResourcesFlushWillMakePurgeable;
499 }
Greg Danielda642612021-02-09 18:04:02 -0500500 if (resource->cacheAccess().isUsableAsScratch()) {
501 fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
502 }
bsalomon84c8e622014-11-17 09:33:27 -0800503 this->purgeAsNeeded();
504 } else {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500505 SkASSERT(resource->resourcePriv().budgetedType() != GrBudgetedType::kUnbudgetedCacheable);
bsalomon84c8e622014-11-17 09:33:27 -0800506 --fBudgetedCount;
507 fBudgetedBytes -= size;
Greg Daniel1fd8ac82020-12-11 11:22:01 -0500508 if (!resource->resourcePriv().isPurgeable() &&
509 !resource->cacheAccess().hasRefOrCommandBufferUsage()) {
Brian Salomon2c791fc2019-04-02 11:52:03 -0400510 --fNumBudgetedResourcesFlushWillMakePurgeable;
511 }
Greg Danielda642612021-02-09 18:04:02 -0500512 if (!resource->cacheAccess().hasRef() && !resource->getUniqueKey().isValid() &&
513 resource->resourcePriv().getScratchKey().isValid()) {
514 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
515 }
bsalomon84c8e622014-11-17 09:33:27 -0800516 }
Brian Salomon9bc76d92019-01-24 12:18:33 -0500517 SkASSERT(wasPurgeable == resource->resourcePriv().isPurgeable());
Brian Osman39c08ac2017-07-26 09:36:09 -0400518 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800519 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomon84c8e622014-11-17 09:33:27 -0800520
521 this->validate();
522}
523
robertphillipsee843b22016-10-04 05:30:20 -0700524void GrResourceCache::purgeAsNeeded() {
bsalomon3f324322015-04-08 11:01:54 -0700525 SkTArray<GrUniqueKeyInvalidatedMessage> invalidKeyMsgs;
526 fInvalidUniqueKeyInbox.poll(&invalidKeyMsgs);
527 if (invalidKeyMsgs.count()) {
Robert Phillips427966a2018-12-20 17:20:43 -0500528 SkASSERT(fProxyProvider);
529
530 for (int i = 0; i < invalidKeyMsgs.count(); ++i) {
Robert Phillips83c38a82020-10-28 14:57:53 -0400531 if (invalidKeyMsgs[i].inThreadSafeCache()) {
532 fThreadSafeCache->remove(invalidKeyMsgs[i].key());
533 SkASSERT(!fThreadSafeCache->has(invalidKeyMsgs[i].key()));
534 } else {
535 fProxyProvider->processInvalidUniqueKey(
536 invalidKeyMsgs[i].key(), nullptr,
Robert Phillips427966a2018-12-20 17:20:43 -0500537 GrProxyProvider::InvalidateGPUResource::kYes);
Robert Phillips83c38a82020-10-28 14:57:53 -0400538 SkASSERT(!this->findAndRefUniqueResource(invalidKeyMsgs[i].key()));
539 }
Robert Phillips427966a2018-12-20 17:20:43 -0500540 }
bsalomon3f324322015-04-08 11:01:54 -0700541 }
bsalomon71cb0c22014-11-14 12:10:14 -0800542
Brian Osman13dddce2017-05-09 13:19:50 -0400543 this->processFreedGpuResources();
544
bsalomon3f324322015-04-08 11:01:54 -0700545 bool stillOverbudget = this->overBudget();
546 while (stillOverbudget && fPurgeableQueue.count()) {
robertphillipsee843b22016-10-04 05:30:20 -0700547 GrGpuResource* resource = fPurgeableQueue.peek();
Brian Salomon614c1a82018-12-19 15:42:06 -0500548 SkASSERT(resource->resourcePriv().isPurgeable());
bsalomon9f2d1572015-02-17 11:47:40 -0800549 resource->cacheAccess().release();
bsalomon3f324322015-04-08 11:01:54 -0700550 stillOverbudget = this->overBudget();
bsalomon9f2d1572015-02-17 11:47:40 -0800551 }
bsalomon71cb0c22014-11-14 12:10:14 -0800552
Robert Phillips12d06a32020-09-16 12:31:34 -0400553 if (stillOverbudget) {
Robert Phillipsd464feb2020-10-08 11:00:02 -0400554 fThreadSafeCache->dropUniqueRefs(this);
Robert Phillips12d06a32020-09-16 12:31:34 -0400555
Robert Phillipsb25ce712021-06-29 07:36:55 -0400556 stillOverbudget = this->overBudget();
Robert Phillips12d06a32020-09-16 12:31:34 -0400557 while (stillOverbudget && fPurgeableQueue.count()) {
558 GrGpuResource* resource = fPurgeableQueue.peek();
559 SkASSERT(resource->resourcePriv().isPurgeable());
560 resource->cacheAccess().release();
561 stillOverbudget = this->overBudget();
562 }
563 }
564
bsalomonb436ed62014-11-17 12:15:56 -0800565 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800566}
567
Michael Ludwig9d1cc052021-06-09 20:49:48 -0400568void GrResourceCache::purgeUnlockedResources(const GrStdSteadyClock::time_point* purgeTime,
569 bool scratchResourcesOnly) {
Robert Phillips12d06a32020-09-16 12:31:34 -0400570
Robert Phillips6eba0632018-03-28 12:25:42 -0400571 if (!scratchResourcesOnly) {
Michael Ludwig9d1cc052021-06-09 20:49:48 -0400572 if (purgeTime) {
573 fThreadSafeCache->dropUniqueRefsOlderThan(*purgeTime);
574 } else {
575 fThreadSafeCache->dropUniqueRefs(nullptr);
576 }
Robert Phillips331699c2020-09-22 15:20:01 -0400577
Robert Phillips6eba0632018-03-28 12:25:42 -0400578 // We could disable maintaining the heap property here, but it would add a lot of
579 // complexity. Moreover, this is rarely called.
580 while (fPurgeableQueue.count()) {
581 GrGpuResource* resource = fPurgeableQueue.peek();
Michael Ludwig9d1cc052021-06-09 20:49:48 -0400582
583 const GrStdSteadyClock::time_point resourceTime =
584 resource->cacheAccess().timeWhenResourceBecamePurgeable();
585 if (purgeTime && resourceTime >= *purgeTime) {
586 // Resources were given both LRU timestamps and tagged with a frame number when
587 // they first became purgeable. The LRU timestamp won't change again until the
588 // resource is made non-purgeable again. So, at this point all the remaining
589 // resources in the timestamp-sorted queue will have a frame number >= to this
590 // one.
591 break;
592 }
593
Brian Salomon614c1a82018-12-19 15:42:06 -0500594 SkASSERT(resource->resourcePriv().isPurgeable());
Robert Phillips6eba0632018-03-28 12:25:42 -0400595 resource->cacheAccess().release();
596 }
597 } else {
Michael Ludwig9d1cc052021-06-09 20:49:48 -0400598 // Early out if the very first item is too new to purge to avoid sorting the queue when
599 // nothing will be deleted.
600 if (purgeTime && fPurgeableQueue.count() &&
601 fPurgeableQueue.peek()->cacheAccess().timeWhenResourceBecamePurgeable() >= *purgeTime) {
602 return;
603 }
604
Robert Phillips6eba0632018-03-28 12:25:42 -0400605 // Sort the queue
606 fPurgeableQueue.sort();
607
608 // Make a list of the scratch resources to delete
609 SkTDArray<GrGpuResource*> scratchResources;
610 for (int i = 0; i < fPurgeableQueue.count(); i++) {
611 GrGpuResource* resource = fPurgeableQueue.at(i);
Michael Ludwig9d1cc052021-06-09 20:49:48 -0400612
613 const GrStdSteadyClock::time_point resourceTime =
614 resource->cacheAccess().timeWhenResourceBecamePurgeable();
615 if (purgeTime && resourceTime >= *purgeTime) {
616 // scratch or not, all later iterations will be too recently used to purge.
617 break;
618 }
Brian Salomon614c1a82018-12-19 15:42:06 -0500619 SkASSERT(resource->resourcePriv().isPurgeable());
Robert Phillips6eba0632018-03-28 12:25:42 -0400620 if (!resource->getUniqueKey().isValid()) {
621 *scratchResources.append() = resource;
622 }
623 }
624
625 // Delete the scratch resources. This must be done as a separate pass
626 // to avoid messing up the sorted order of the queue
627 for (int i = 0; i < scratchResources.count(); i++) {
628 scratchResources.getAt(i)->cacheAccess().release();
629 }
bsalomon9f2d1572015-02-17 11:47:40 -0800630 }
bsalomon71cb0c22014-11-14 12:10:14 -0800631
bsalomonb436ed62014-11-17 12:15:56 -0800632 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800633}
634
Adlai Hollere1c8a382021-04-08 15:30:12 -0400635bool GrResourceCache::purgeToMakeHeadroom(size_t desiredHeadroomBytes) {
636 AutoValidate av(this);
Adlai Hollercd2f96d2021-04-09 17:58:14 -0400637 if (desiredHeadroomBytes > fMaxBytes) {
638 return false;
639 }
Adlai Hollere1c8a382021-04-08 15:30:12 -0400640 if (this->wouldFit(desiredHeadroomBytes)) {
641 return true;
642 }
643 fPurgeableQueue.sort();
644
Adlai Hollercd2f96d2021-04-09 17:58:14 -0400645 size_t projectedBudget = fBudgetedBytes;
Adlai Hollere1c8a382021-04-08 15:30:12 -0400646 int purgeCnt = 0;
647 for (int i = 0; i < fPurgeableQueue.count(); i++) {
648 GrGpuResource* resource = fPurgeableQueue.at(i);
Adlai Hollercd2f96d2021-04-09 17:58:14 -0400649 if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
650 projectedBudget -= resource->gpuMemorySize();
651 }
652 if (projectedBudget + desiredHeadroomBytes <= fMaxBytes) {
Adlai Hollere1c8a382021-04-08 15:30:12 -0400653 purgeCnt = i + 1;
654 break;
655 }
656 }
Adlai Hollercd2f96d2021-04-09 17:58:14 -0400657 if (purgeCnt == 0) {
Adlai Hollere1c8a382021-04-08 15:30:12 -0400658 return false;
659 }
660
661 // Success! Release the resources.
662 // Copy to array first so we don't mess with the queue.
663 std::vector<GrGpuResource*> resources;
664 resources.reserve(purgeCnt);
665 for (int i = 0; i < purgeCnt; i++) {
666 resources.push_back(fPurgeableQueue.at(i));
667 }
668 for (GrGpuResource* resource : resources) {
669 resource->cacheAccess().release();
670 }
671 return true;
672}
673
Derek Sollenberger5480a182017-05-25 16:43:59 -0400674void GrResourceCache::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
675
Brian Osman788b9162020-02-07 10:36:46 -0500676 const size_t tmpByteBudget = std::max((size_t)0, fBytes - bytesToPurge);
Derek Sollenberger5480a182017-05-25 16:43:59 -0400677 bool stillOverbudget = tmpByteBudget < fBytes;
678
679 if (preferScratchResources && bytesToPurge < fPurgeableBytes) {
680 // Sort the queue
681 fPurgeableQueue.sort();
682
683 // Make a list of the scratch resources to delete
684 SkTDArray<GrGpuResource*> scratchResources;
685 size_t scratchByteCount = 0;
686 for (int i = 0; i < fPurgeableQueue.count() && stillOverbudget; i++) {
687 GrGpuResource* resource = fPurgeableQueue.at(i);
Brian Salomon614c1a82018-12-19 15:42:06 -0500688 SkASSERT(resource->resourcePriv().isPurgeable());
Derek Sollenberger5480a182017-05-25 16:43:59 -0400689 if (!resource->getUniqueKey().isValid()) {
690 *scratchResources.append() = resource;
691 scratchByteCount += resource->gpuMemorySize();
692 stillOverbudget = tmpByteBudget < fBytes - scratchByteCount;
693 }
694 }
695
696 // Delete the scratch resources. This must be done as a separate pass
697 // to avoid messing up the sorted order of the queue
698 for (int i = 0; i < scratchResources.count(); i++) {
699 scratchResources.getAt(i)->cacheAccess().release();
700 }
701 stillOverbudget = tmpByteBudget < fBytes;
702
703 this->validate();
704 }
705
706 // Purge any remaining resources in LRU order
707 if (stillOverbudget) {
708 const size_t cachedByteCount = fMaxBytes;
709 fMaxBytes = tmpByteBudget;
710 this->purgeAsNeeded();
711 fMaxBytes = cachedByteCount;
712 }
713}
Robert Phillips12d06a32020-09-16 12:31:34 -0400714
Brian Salomon8cefa3e2019-04-04 11:39:55 -0400715bool GrResourceCache::requestsFlush() const {
716 return this->overBudget() && !fPurgeableQueue.count() &&
717 fNumBudgetedResourcesFlushWillMakePurgeable > 0;
718}
719
Robert Phillipsddc21482019-10-16 14:30:09 -0400720void GrResourceCache::insertDelayedTextureUnref(GrTexture* texture) {
721 texture->ref();
722 uint32_t id = texture->uniqueID().asUInt();
723 if (auto* data = fTexturesAwaitingUnref.find(id)) {
Brian Salomon876a0172019-03-08 11:12:14 -0500724 data->addRef();
725 } else {
Robert Phillipsddc21482019-10-16 14:30:09 -0400726 fTexturesAwaitingUnref.set(id, {texture});
Brian Salomon876a0172019-03-08 11:12:14 -0500727 }
Brian Osman13dddce2017-05-09 13:19:50 -0400728}
729
730void GrResourceCache::processFreedGpuResources() {
Robert Phillips1dfc77c2019-10-16 16:39:45 -0400731 if (!fTexturesAwaitingUnref.count()) {
Robert Phillips12d06a32020-09-16 12:31:34 -0400732 return;
Robert Phillips1dfc77c2019-10-16 16:39:45 -0400733 }
734
Robert Phillipsddc21482019-10-16 14:30:09 -0400735 SkTArray<GrTextureFreedMessage> msgs;
736 fFreedTextureInbox.poll(&msgs);
Brian Osman13dddce2017-05-09 13:19:50 -0400737 for (int i = 0; i < msgs.count(); ++i) {
Robert Phillipsd074b622021-03-15 08:49:24 -0400738 SkASSERT(msgs[i].fIntendedRecipient == fOwningContextID);
Robert Phillipsddc21482019-10-16 14:30:09 -0400739 uint32_t id = msgs[i].fTexture->uniqueID().asUInt();
740 TextureAwaitingUnref* info = fTexturesAwaitingUnref.find(id);
Greg Daniel1a5d2d52019-12-04 11:14:29 -0500741 // If the GrContext was released or abandoned then fTexturesAwaitingUnref should have been
742 // empty and we would have returned early above. Thus, any texture from a message should be
743 // in the list of fTexturesAwaitingUnref.
744 SkASSERT(info);
745 info->unref();
746 if (info->finished()) {
747 fTexturesAwaitingUnref.remove(id);
Greg Danielb2acf0a2018-09-12 09:17:11 -0400748 }
Brian Osman13dddce2017-05-09 13:19:50 -0400749 }
750}
751
bsalomonf320e042015-02-17 15:09:34 -0800752void GrResourceCache::addToNonpurgeableArray(GrGpuResource* resource) {
753 int index = fNonpurgeableResources.count();
754 *fNonpurgeableResources.append() = resource;
755 *resource->cacheAccess().accessCacheIndex() = index;
756}
757
758void GrResourceCache::removeFromNonpurgeableArray(GrGpuResource* resource) {
759 int* index = resource->cacheAccess().accessCacheIndex();
Adlai Holler9555f292020-10-09 09:41:14 -0400760 // Fill the hole we will create in the array with the tail object, adjust its index, and
bsalomonf320e042015-02-17 15:09:34 -0800761 // then pop the array
762 GrGpuResource* tail = *(fNonpurgeableResources.end() - 1);
763 SkASSERT(fNonpurgeableResources[*index] == resource);
764 fNonpurgeableResources[*index] = tail;
765 *tail->cacheAccess().accessCacheIndex() = *index;
766 fNonpurgeableResources.pop();
767 SkDEBUGCODE(*index = -1);
768}
769
bsalomonddf30e62015-02-19 11:38:44 -0800770uint32_t GrResourceCache::getNextTimestamp() {
771 // If we wrap then all the existing resources will appear older than any resources that get
772 // a timestamp after the wrap.
773 if (0 == fTimestamp) {
774 int count = this->getResourceCount();
775 if (count) {
776 // Reset all the timestamps. We sort the resources by timestamp and then assign
777 // sequential timestamps beginning with 0. This is O(n*lg(n)) but it should be extremely
778 // rare.
779 SkTDArray<GrGpuResource*> sortedPurgeableResources;
780 sortedPurgeableResources.setReserve(fPurgeableQueue.count());
781
782 while (fPurgeableQueue.count()) {
783 *sortedPurgeableResources.append() = fPurgeableQueue.peek();
784 fPurgeableQueue.pop();
785 }
robertphillipsee843b22016-10-04 05:30:20 -0700786
John Stiles886a9042020-07-14 16:28:33 -0400787 SkTQSort(fNonpurgeableResources.begin(), fNonpurgeableResources.end(),
John Stiles6e9ead92020-07-14 00:13:51 +0000788 CompareTimestamp);
bsalomonddf30e62015-02-19 11:38:44 -0800789
790 // Pick resources out of the purgeable and non-purgeable arrays based on lowest
791 // timestamp and assign new timestamps.
792 int currP = 0;
793 int currNP = 0;
794 while (currP < sortedPurgeableResources.count() &&
mtklein56da0252015-11-16 11:16:23 -0800795 currNP < fNonpurgeableResources.count()) {
bsalomonddf30e62015-02-19 11:38:44 -0800796 uint32_t tsP = sortedPurgeableResources[currP]->cacheAccess().timestamp();
797 uint32_t tsNP = fNonpurgeableResources[currNP]->cacheAccess().timestamp();
798 SkASSERT(tsP != tsNP);
799 if (tsP < tsNP) {
800 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
801 } else {
802 // Correct the index in the nonpurgeable array stored on the resource post-sort.
803 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
804 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
805 }
806 }
807
808 // The above loop ended when we hit the end of one array. Finish the other one.
809 while (currP < sortedPurgeableResources.count()) {
810 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
811 }
812 while (currNP < fNonpurgeableResources.count()) {
813 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
814 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
815 }
816
817 // Rebuild the queue.
818 for (int i = 0; i < sortedPurgeableResources.count(); ++i) {
819 fPurgeableQueue.insert(sortedPurgeableResources[i]);
820 }
821
822 this->validate();
823 SkASSERT(count == this->getResourceCount());
824
825 // count should be the next timestamp we return.
826 SkASSERT(fTimestamp == SkToU32(count));
mtklein56da0252015-11-16 11:16:23 -0800827 }
bsalomonddf30e62015-02-19 11:38:44 -0800828 }
829 return fTimestamp++;
830}
831
ericrk0a5fa482015-09-15 14:16:10 -0700832void GrResourceCache::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
833 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
834 fNonpurgeableResources[i]->dumpMemoryStatistics(traceMemoryDump);
835 }
836 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
837 fPurgeableQueue.at(i)->dumpMemoryStatistics(traceMemoryDump);
838 }
839}
840
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500841#if GR_CACHE_STATS
842void GrResourceCache::getStats(Stats* stats) const {
843 stats->reset();
844
845 stats->fTotal = this->getResourceCount();
846 stats->fNumNonPurgeable = fNonpurgeableResources.count();
847 stats->fNumPurgeable = fPurgeableQueue.count();
848
849 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
850 stats->update(fNonpurgeableResources[i]);
851 }
852 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
853 stats->update(fPurgeableQueue.at(i));
854 }
855}
856
857#if GR_TEST_UTILS
858void GrResourceCache::dumpStats(SkString* out) const {
859 this->validate();
860
861 Stats stats;
862
863 this->getStats(&stats);
864
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500865 float byteUtilization = (100.f * fBudgetedBytes) / fMaxBytes;
866
Robert Phillipscf39f372019-09-03 10:29:20 -0400867 out->appendf("Budget: %d bytes\n", (int)fMaxBytes);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500868 out->appendf("\t\tEntry Count: current %d"
Robert Phillipscf39f372019-09-03 10:29:20 -0400869 " (%d budgeted, %d wrapped, %d locked, %d scratch), high %d\n",
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500870 stats.fTotal, fBudgetedCount, stats.fWrapped, stats.fNumNonPurgeable,
Robert Phillipscf39f372019-09-03 10:29:20 -0400871 stats.fScratch, fHighWaterCount);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500872 out->appendf("\t\tEntry Bytes: current %d (budgeted %d, %.2g%% full, %d unbudgeted) high %d\n",
873 SkToInt(fBytes), SkToInt(fBudgetedBytes), byteUtilization,
874 SkToInt(stats.fUnbudgetedSize), SkToInt(fHighWaterBytes));
875}
876
877void GrResourceCache::dumpStatsKeyValuePairs(SkTArray<SkString>* keys,
878 SkTArray<double>* values) const {
879 this->validate();
880
881 Stats stats;
882 this->getStats(&stats);
883
884 keys->push_back(SkString("gpu_cache_purgable_entries")); values->push_back(stats.fNumPurgeable);
885}
Robert Phillips72354b02021-07-06 13:43:56 -0400886#endif // GR_TEST_UTILS
887#endif // GR_CACHE_STATS
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500888
bsalomon71cb0c22014-11-14 12:10:14 -0800889#ifdef SK_DEBUG
bsalomon0ea80f42015-02-11 10:49:59 -0800890void GrResourceCache::validate() const {
bsalomonc2f35b72015-01-23 07:19:22 -0800891 // Reduce the frequency of validations for large resource counts.
892 static SkRandom gRandom;
893 int mask = (SkNextPow2(fCount + 1) >> 5) - 1;
894 if (~mask && (gRandom.nextU() & mask)) {
895 return;
896 }
897
bsalomonf320e042015-02-17 15:09:34 -0800898 struct Stats {
899 size_t fBytes;
900 int fBudgetedCount;
901 size_t fBudgetedBytes;
902 int fLocked;
903 int fScratch;
904 int fCouldBeScratch;
905 int fContent;
906 const ScratchMap* fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800907 const UniqueHash* fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800908
bsalomonf320e042015-02-17 15:09:34 -0800909 Stats(const GrResourceCache* cache) {
910 memset(this, 0, sizeof(*this));
911 fScratchMap = &cache->fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800912 fUniqueHash = &cache->fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800913 }
914
bsalomonf320e042015-02-17 15:09:34 -0800915 void update(GrGpuResource* resource) {
916 fBytes += resource->gpuMemorySize();
bsalomondace19e2014-11-17 07:34:06 -0800917
Brian Salomon614c1a82018-12-19 15:42:06 -0500918 if (!resource->resourcePriv().isPurgeable()) {
bsalomonf320e042015-02-17 15:09:34 -0800919 ++fLocked;
920 }
bsalomon9f2d1572015-02-17 11:47:40 -0800921
robertphillipsc4ed6842016-05-24 14:17:12 -0700922 const GrScratchKey& scratchKey = resource->resourcePriv().getScratchKey();
923 const GrUniqueKey& uniqueKey = resource->getUniqueKey();
924
Greg Danielda642612021-02-09 18:04:02 -0500925 if (resource->cacheAccess().isUsableAsScratch()) {
robertphillipsc4ed6842016-05-24 14:17:12 -0700926 SkASSERT(!uniqueKey.isValid());
Greg Danielda642612021-02-09 18:04:02 -0500927 SkASSERT(GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType());
928 SkASSERT(!resource->cacheAccess().hasRef());
bsalomonf320e042015-02-17 15:09:34 -0800929 ++fScratch;
robertphillipsc4ed6842016-05-24 14:17:12 -0700930 SkASSERT(fScratchMap->countForKey(scratchKey));
kkinnunen2e6055b2016-04-22 01:48:29 -0700931 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
robertphillipsc4ed6842016-05-24 14:17:12 -0700932 } else if (scratchKey.isValid()) {
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500933 SkASSERT(GrBudgetedType::kBudgeted != resource->resourcePriv().budgetedType() ||
Greg Danielda642612021-02-09 18:04:02 -0500934 uniqueKey.isValid() || resource->cacheAccess().hasRef());
kkinnunen2e6055b2016-04-22 01:48:29 -0700935 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
Greg Danielda642612021-02-09 18:04:02 -0500936 SkASSERT(!fScratchMap->has(resource, scratchKey));
bsalomonf320e042015-02-17 15:09:34 -0800937 }
bsalomon8718aaf2015-02-19 07:24:21 -0800938 if (uniqueKey.isValid()) {
bsalomonf320e042015-02-17 15:09:34 -0800939 ++fContent;
bsalomon8718aaf2015-02-19 07:24:21 -0800940 SkASSERT(fUniqueHash->find(uniqueKey) == resource);
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500941 SkASSERT(GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType() ||
Brian Osman0562eb92017-05-08 11:16:39 -0400942 resource->resourcePriv().refsWrappedObjects());
bsalomonf320e042015-02-17 15:09:34 -0800943 }
944
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500945 if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
bsalomonf320e042015-02-17 15:09:34 -0800946 ++fBudgetedCount;
947 fBudgetedBytes += resource->gpuMemorySize();
948 }
bsalomon9f2d1572015-02-17 11:47:40 -0800949 }
bsalomonf320e042015-02-17 15:09:34 -0800950 };
951
robertphillipsc4ed6842016-05-24 14:17:12 -0700952 {
robertphillipsc4ed6842016-05-24 14:17:12 -0700953 int count = 0;
Mike Kleincff63962020-03-14 16:22:45 -0500954 fScratchMap.foreach([&](const GrGpuResource& resource) {
Greg Danielda642612021-02-09 18:04:02 -0500955 SkASSERT(resource.cacheAccess().isUsableAsScratch());
robertphillipsc4ed6842016-05-24 14:17:12 -0700956 count++;
Mike Kleincff63962020-03-14 16:22:45 -0500957 });
958 SkASSERT(count == fScratchMap.count());
robertphillipsc4ed6842016-05-24 14:17:12 -0700959 }
960
bsalomonf320e042015-02-17 15:09:34 -0800961 Stats stats(this);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400962 size_t purgeableBytes = 0;
Brian Salomon2c791fc2019-04-02 11:52:03 -0400963 int numBudgetedResourcesFlushWillMakePurgeable = 0;
bsalomonf320e042015-02-17 15:09:34 -0800964
965 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500966 SkASSERT(!fNonpurgeableResources[i]->resourcePriv().isPurgeable() ||
bsalomon3f324322015-04-08 11:01:54 -0700967 fNewlyPurgeableResourceForValidation == fNonpurgeableResources[i]);
bsalomonf320e042015-02-17 15:09:34 -0800968 SkASSERT(*fNonpurgeableResources[i]->cacheAccess().accessCacheIndex() == i);
969 SkASSERT(!fNonpurgeableResources[i]->wasDestroyed());
Brian Salomon2c791fc2019-04-02 11:52:03 -0400970 if (fNonpurgeableResources[i]->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted &&
Greg Daniel1fd8ac82020-12-11 11:22:01 -0500971 !fNonpurgeableResources[i]->cacheAccess().hasRefOrCommandBufferUsage() &&
Brian Salomon2c791fc2019-04-02 11:52:03 -0400972 fNewlyPurgeableResourceForValidation != fNonpurgeableResources[i]) {
Brian Salomon2c791fc2019-04-02 11:52:03 -0400973 ++numBudgetedResourcesFlushWillMakePurgeable;
974 }
bsalomonf320e042015-02-17 15:09:34 -0800975 stats.update(fNonpurgeableResources[i]);
bsalomon71cb0c22014-11-14 12:10:14 -0800976 }
bsalomon9f2d1572015-02-17 11:47:40 -0800977 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500978 SkASSERT(fPurgeableQueue.at(i)->resourcePriv().isPurgeable());
bsalomonf320e042015-02-17 15:09:34 -0800979 SkASSERT(*fPurgeableQueue.at(i)->cacheAccess().accessCacheIndex() == i);
980 SkASSERT(!fPurgeableQueue.at(i)->wasDestroyed());
981 stats.update(fPurgeableQueue.at(i));
Derek Sollenbergeree479142017-05-24 11:41:33 -0400982 purgeableBytes += fPurgeableQueue.at(i)->gpuMemorySize();
bsalomon9f2d1572015-02-17 11:47:40 -0800983 }
984
bsalomonf320e042015-02-17 15:09:34 -0800985 SkASSERT(fCount == this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800986 SkASSERT(fBudgetedCount <= fCount);
bsalomonf320e042015-02-17 15:09:34 -0800987 SkASSERT(fBudgetedBytes <= fBytes);
988 SkASSERT(stats.fBytes == fBytes);
Brian Salomon2c791fc2019-04-02 11:52:03 -0400989 SkASSERT(fNumBudgetedResourcesFlushWillMakePurgeable ==
990 numBudgetedResourcesFlushWillMakePurgeable);
bsalomonf320e042015-02-17 15:09:34 -0800991 SkASSERT(stats.fBudgetedBytes == fBudgetedBytes);
992 SkASSERT(stats.fBudgetedCount == fBudgetedCount);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400993 SkASSERT(purgeableBytes == fPurgeableBytes);
bsalomon71cb0c22014-11-14 12:10:14 -0800994#if GR_CACHE_STATS
bsalomondace19e2014-11-17 07:34:06 -0800995 SkASSERT(fBudgetedHighWaterCount <= fHighWaterCount);
996 SkASSERT(fBudgetedHighWaterBytes <= fHighWaterBytes);
bsalomonf320e042015-02-17 15:09:34 -0800997 SkASSERT(fBytes <= fHighWaterBytes);
998 SkASSERT(fCount <= fHighWaterCount);
999 SkASSERT(fBudgetedBytes <= fBudgetedHighWaterBytes);
1000 SkASSERT(fBudgetedCount <= fBudgetedHighWaterCount);
bsalomon71cb0c22014-11-14 12:10:14 -08001001#endif
bsalomon8718aaf2015-02-19 07:24:21 -08001002 SkASSERT(stats.fContent == fUniqueHash.count());
Greg Danielda642612021-02-09 18:04:02 -05001003 SkASSERT(stats.fScratch == fScratchMap.count());
bsalomon71cb0c22014-11-14 12:10:14 -08001004
bsalomon3f324322015-04-08 11:01:54 -07001005 // This assertion is not currently valid because we can be in recursive notifyCntReachedZero()
bsalomon12299ab2014-11-14 13:33:09 -08001006 // calls. This will be fixed when subresource registration is explicit.
bsalomondace19e2014-11-17 07:34:06 -08001007 // bool overBudget = budgetedBytes > fMaxBytes || budgetedCount > fMaxCount;
bsalomon12299ab2014-11-14 13:33:09 -08001008 // SkASSERT(!overBudget || locked == count || fPurging);
bsalomon71cb0c22014-11-14 12:10:14 -08001009}
bsalomonf320e042015-02-17 15:09:34 -08001010
1011bool GrResourceCache::isInCache(const GrGpuResource* resource) const {
1012 int index = *resource->cacheAccess().accessCacheIndex();
1013 if (index < 0) {
1014 return false;
1015 }
1016 if (index < fPurgeableQueue.count() && fPurgeableQueue.at(index) == resource) {
1017 return true;
1018 }
1019 if (index < fNonpurgeableResources.count() && fNonpurgeableResources[index] == resource) {
1020 return true;
1021 }
1022 SkDEBUGFAIL("Resource index should be -1 or the resource should be in the cache.");
1023 return false;
1024}
1025
Robert Phillips72354b02021-07-06 13:43:56 -04001026#endif // SK_DEBUG
1027
1028#if GR_TEST_UTILS
1029
1030int GrResourceCache::countUniqueKeysWithTag(const char* tag) const {
1031 int count = 0;
1032 fUniqueHash.foreach([&](const GrGpuResource& resource){
1033 if (0 == strcmp(tag, resource.getUniqueKey().tag())) {
1034 ++count;
1035 }
1036 });
1037 return count;
1038}
1039
1040void GrResourceCache::changeTimestamp(uint32_t newTimestamp) {
1041 fTimestamp = newTimestamp;
1042}
1043
1044#endif // GR_TEST_UTILS