blob: e6655d23ca7d8a24239651c38bf2cf6ed68fafda [file] [log] [blame]
bsalomonc8dc1f72014-08-21 13:02:13 -07001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrResourceCache.h"
Brian Salomon614c1a82018-12-19 15:42:06 -05009#include <atomic>
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/gpu/GrContext.h"
11#include "include/gpu/GrTexture.h"
12#include "include/private/GrSingleOwner.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/private/SkTo.h"
14#include "include/utils/SkRandom.h"
15#include "src/core/SkExchange.h"
Ben Wagner21bca282019-05-15 10:15:52 -040016#include "src/core/SkMessageBus.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/core/SkOpts.h"
18#include "src/core/SkScopeExit.h"
19#include "src/core/SkTSort.h"
20#include "src/gpu/GrCaps.h"
21#include "src/gpu/GrContextPriv.h"
22#include "src/gpu/GrGpuResourceCacheAccess.h"
23#include "src/gpu/GrProxyProvider.h"
24#include "src/gpu/GrTextureProxyCacheAccess.h"
25#include "src/gpu/GrTracing.h"
26#include "src/gpu/SkGr.h"
bsalomon71cb0c22014-11-14 12:10:14 -080027
bsalomon8718aaf2015-02-19 07:24:21 -080028DECLARE_SKMESSAGEBUS_MESSAGE(GrUniqueKeyInvalidatedMessage);
bsalomon71cb0c22014-11-14 12:10:14 -080029
Brian Osman13dddce2017-05-09 13:19:50 -040030DECLARE_SKMESSAGEBUS_MESSAGE(GrGpuResourceFreedMessage);
31
Brian Salomon8f8995a2018-10-15 14:32:15 -040032#define ASSERT_SINGLE_OWNER \
33 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);)
34
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
Mike Klein0ec1c572018-12-04 11:52:51 -050040 int32_t type = nextType++;
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
Mike Klein0ec1c572018-12-04 11:52:51 -050051 int32_t domain = nextDomain++;
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
75inline GrResourceCache::ResourceAwaitingUnref::ResourceAwaitingUnref() = default;
76
77inline GrResourceCache::ResourceAwaitingUnref::ResourceAwaitingUnref(GrGpuResource* resource)
78 : fResource(resource), fNumUnrefs(1) {}
79
80inline GrResourceCache::ResourceAwaitingUnref::ResourceAwaitingUnref(ResourceAwaitingUnref&& that) {
81 fResource = skstd::exchange(that.fResource, nullptr);
82 fNumUnrefs = skstd::exchange(that.fNumUnrefs, 0);
83}
84
85inline GrResourceCache::ResourceAwaitingUnref& GrResourceCache::ResourceAwaitingUnref::operator=(
86 ResourceAwaitingUnref&& that) {
87 fResource = skstd::exchange(that.fResource, nullptr);
88 fNumUnrefs = skstd::exchange(that.fNumUnrefs, 0);
89 return *this;
90}
91
92inline GrResourceCache::ResourceAwaitingUnref::~ResourceAwaitingUnref() {
93 if (fResource) {
94 for (int i = 0; i < fNumUnrefs; ++i) {
95 fResource->unref();
96 }
97 }
98}
99
100inline void GrResourceCache::ResourceAwaitingUnref::addRef() { ++fNumUnrefs; }
101
102inline void GrResourceCache::ResourceAwaitingUnref::unref() {
103 SkASSERT(fNumUnrefs > 0);
104 fResource->unref();
105 --fNumUnrefs;
106}
107
108inline bool GrResourceCache::ResourceAwaitingUnref::finished() { return !fNumUnrefs; }
109
110//////////////////////////////////////////////////////////////////////////////
robertphillipsee843b22016-10-04 05:30:20 -0700111
Brian Salomon8f8995a2018-10-15 14:32:15 -0400112GrResourceCache::GrResourceCache(const GrCaps* caps, GrSingleOwner* singleOwner,
113 uint32_t contextUniqueID)
Brian Salomon2c791fc2019-04-02 11:52:03 -0400114 : fInvalidUniqueKeyInbox(contextUniqueID)
Brian Salomon238069b2018-07-11 15:58:57 -0400115 , fFreedGpuResourceInbox(contextUniqueID)
116 , fContextUniqueID(contextUniqueID)
Brian Salomon8f8995a2018-10-15 14:32:15 -0400117 , fSingleOwner(singleOwner)
Brian Salomon238069b2018-07-11 15:58:57 -0400118 , fPreferVRAMUseOverFlushes(caps->preferVRAMUseOverFlushes()) {
119 SkASSERT(contextUniqueID != SK_InvalidUniqueID);
bsalomon71cb0c22014-11-14 12:10:14 -0800120}
121
bsalomon0ea80f42015-02-11 10:49:59 -0800122GrResourceCache::~GrResourceCache() {
bsalomonc8dc1f72014-08-21 13:02:13 -0700123 this->releaseAll();
124}
125
Robert Phillipscf39f372019-09-03 10:29:20 -0400126void GrResourceCache::setLimit(size_t bytes) {
bsalomon71cb0c22014-11-14 12:10:14 -0800127 fMaxBytes = bytes;
128 this->purgeAsNeeded();
129}
130
bsalomon0ea80f42015-02-11 10:49:59 -0800131void GrResourceCache::insertResource(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400132 ASSERT_SINGLE_OWNER
bsalomon49f085d2014-09-05 13:34:00 -0700133 SkASSERT(resource);
bsalomon16961262014-08-26 14:01:07 -0700134 SkASSERT(!this->isInCache(resource));
bsalomonf320e042015-02-17 15:09:34 -0800135 SkASSERT(!resource->wasDestroyed());
Brian Salomon614c1a82018-12-19 15:42:06 -0500136 SkASSERT(!resource->resourcePriv().isPurgeable());
bsalomonddf30e62015-02-19 11:38:44 -0800137
138 // We must set the timestamp before adding to the array in case the timestamp wraps and we wind
139 // up iterating over all the resources that already have timestamps.
140 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
141
bsalomonf320e042015-02-17 15:09:34 -0800142 this->addToNonpurgeableArray(resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800143
bsalomondace19e2014-11-17 07:34:06 -0800144 size_t size = resource->gpuMemorySize();
bsalomonf320e042015-02-17 15:09:34 -0800145 SkDEBUGCODE(++fCount;)
bsalomon84c8e622014-11-17 09:33:27 -0800146 fBytes += size;
bsalomon82b1d622014-11-14 13:59:57 -0800147#if GR_CACHE_STATS
bsalomonf320e042015-02-17 15:09:34 -0800148 fHighWaterCount = SkTMax(this->getResourceCount(), fHighWaterCount);
bsalomon82b1d622014-11-14 13:59:57 -0800149 fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes);
150#endif
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500151 if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
bsalomondace19e2014-11-17 07:34:06 -0800152 ++fBudgetedCount;
153 fBudgetedBytes += size;
Brian Osman39c08ac2017-07-26 09:36:09 -0400154 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800155 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomondace19e2014-11-17 07:34:06 -0800156#if GR_CACHE_STATS
157 fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount);
158 fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes);
159#endif
160 }
robertphillipsc4ed6842016-05-24 14:17:12 -0700161 if (resource->resourcePriv().getScratchKey().isValid() &&
162 !resource->getUniqueKey().isValid()) {
kkinnunen2e6055b2016-04-22 01:48:29 -0700163 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
bsalomon3582d3e2015-02-13 14:20:05 -0800164 fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
bsalomon744998e2014-08-28 09:54:34 -0700165 }
bsalomon9f2d1572015-02-17 11:47:40 -0800166
bsalomon71cb0c22014-11-14 12:10:14 -0800167 this->purgeAsNeeded();
bsalomonc8dc1f72014-08-21 13:02:13 -0700168}
169
bsalomon0ea80f42015-02-11 10:49:59 -0800170void GrResourceCache::removeResource(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400171 ASSERT_SINGLE_OWNER
bsalomon9f2d1572015-02-17 11:47:40 -0800172 this->validate();
bsalomon16961262014-08-26 14:01:07 -0700173 SkASSERT(this->isInCache(resource));
bsalomondace19e2014-11-17 07:34:06 -0800174
Derek Sollenbergeree479142017-05-24 11:41:33 -0400175 size_t size = resource->gpuMemorySize();
Brian Salomon614c1a82018-12-19 15:42:06 -0500176 if (resource->resourcePriv().isPurgeable()) {
bsalomon9f2d1572015-02-17 11:47:40 -0800177 fPurgeableQueue.remove(resource);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400178 fPurgeableBytes -= size;
bsalomonf320e042015-02-17 15:09:34 -0800179 } else {
180 this->removeFromNonpurgeableArray(resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800181 }
182
bsalomonf320e042015-02-17 15:09:34 -0800183 SkDEBUGCODE(--fCount;)
bsalomondace19e2014-11-17 07:34:06 -0800184 fBytes -= size;
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500185 if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
bsalomondace19e2014-11-17 07:34:06 -0800186 --fBudgetedCount;
187 fBudgetedBytes -= size;
Brian Osman39c08ac2017-07-26 09:36:09 -0400188 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800189 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomondace19e2014-11-17 07:34:06 -0800190 }
191
robertphillipsc4ed6842016-05-24 14:17:12 -0700192 if (resource->resourcePriv().getScratchKey().isValid() &&
193 !resource->getUniqueKey().isValid()) {
bsalomon3582d3e2015-02-13 14:20:05 -0800194 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
bsalomon744998e2014-08-28 09:54:34 -0700195 }
bsalomon8718aaf2015-02-19 07:24:21 -0800196 if (resource->getUniqueKey().isValid()) {
197 fUniqueHash.remove(resource->getUniqueKey());
bsalomon8b79d232014-11-10 10:19:06 -0800198 }
bsalomonb436ed62014-11-17 12:15:56 -0800199 this->validate();
bsalomonc8dc1f72014-08-21 13:02:13 -0700200}
201
bsalomon0ea80f42015-02-11 10:49:59 -0800202void GrResourceCache::abandonAll() {
bsalomon71cb0c22014-11-14 12:10:14 -0800203 AutoValidate av(this);
204
Brian Salomon876a0172019-03-08 11:12:14 -0500205 // We need to make sure to free any resources that were waiting on a free message but never
206 // received one.
207 fResourcesAwaitingUnref.reset();
Greg Danielb2acf0a2018-09-12 09:17:11 -0400208
bsalomonf320e042015-02-17 15:09:34 -0800209 while (fNonpurgeableResources.count()) {
210 GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
211 SkASSERT(!back->wasDestroyed());
212 back->cacheAccess().abandon();
bsalomonc8dc1f72014-08-21 13:02:13 -0700213 }
bsalomonf320e042015-02-17 15:09:34 -0800214
215 while (fPurgeableQueue.count()) {
216 GrGpuResource* top = fPurgeableQueue.peek();
217 SkASSERT(!top->wasDestroyed());
218 top->cacheAccess().abandon();
219 }
220
bsalomon744998e2014-08-28 09:54:34 -0700221 SkASSERT(!fScratchMap.count());
bsalomon8718aaf2015-02-19 07:24:21 -0800222 SkASSERT(!fUniqueHash.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700223 SkASSERT(!fCount);
bsalomonf320e042015-02-17 15:09:34 -0800224 SkASSERT(!this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800225 SkASSERT(!fBytes);
226 SkASSERT(!fBudgetedCount);
227 SkASSERT(!fBudgetedBytes);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400228 SkASSERT(!fPurgeableBytes);
Brian Salomon876a0172019-03-08 11:12:14 -0500229 SkASSERT(!fResourcesAwaitingUnref.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700230}
231
bsalomon0ea80f42015-02-11 10:49:59 -0800232void GrResourceCache::releaseAll() {
bsalomon71cb0c22014-11-14 12:10:14 -0800233 AutoValidate av(this);
234
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.
Brian Salomon876a0172019-03-08 11:12:14 -0500239 fResourcesAwaitingUnref.reset();
Greg Danielc27eb722018-08-10 09:48:08 -0400240
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500241 SkASSERT(fProxyProvider); // better have called setProxyProvider
Robert Phillips3ec95732017-09-29 15:10:39 -0400242 // We must remove the uniqueKeys from the proxies here. While they possess a uniqueKey
243 // they also have a raw pointer back to this class (which is presumably going away)!
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500244 fProxyProvider->removeAllUniqueKeys();
Robert Phillips45a6f142017-09-29 09:49:41 -0400245
Brian Salomon614c1a82018-12-19 15:42:06 -0500246 while (fNonpurgeableResources.count()) {
bsalomonf320e042015-02-17 15:09:34 -0800247 GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
248 SkASSERT(!back->wasDestroyed());
249 back->cacheAccess().release();
bsalomonc8dc1f72014-08-21 13:02:13 -0700250 }
bsalomonf320e042015-02-17 15:09:34 -0800251
252 while (fPurgeableQueue.count()) {
253 GrGpuResource* top = fPurgeableQueue.peek();
254 SkASSERT(!top->wasDestroyed());
255 top->cacheAccess().release();
256 }
257
bsalomon744998e2014-08-28 09:54:34 -0700258 SkASSERT(!fScratchMap.count());
bsalomon8718aaf2015-02-19 07:24:21 -0800259 SkASSERT(!fUniqueHash.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700260 SkASSERT(!fCount);
bsalomonf320e042015-02-17 15:09:34 -0800261 SkASSERT(!this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800262 SkASSERT(!fBytes);
263 SkASSERT(!fBudgetedCount);
264 SkASSERT(!fBudgetedBytes);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400265 SkASSERT(!fPurgeableBytes);
Brian Salomon876a0172019-03-08 11:12:14 -0500266 SkASSERT(!fResourcesAwaitingUnref.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700267}
bsalomonbcf0a522014-10-08 08:40:09 -0700268
Brian Salomon2c791fc2019-04-02 11:52:03 -0400269void GrResourceCache::refResource(GrGpuResource* resource) {
270 SkASSERT(resource);
271 SkASSERT(resource->getContext()->priv().getResourceCache() == this);
272 if (resource->cacheAccess().hasRef()) {
273 resource->ref();
274 } else {
275 this->refAndMakeResourceMRU(resource);
276 }
277 this->validate();
278}
279
bsalomon0ea80f42015-02-11 10:49:59 -0800280class GrResourceCache::AvailableForScratchUse {
bsalomonbcf0a522014-10-08 08:40:09 -0700281public:
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400282 AvailableForScratchUse() { }
bsalomonbcf0a522014-10-08 08:40:09 -0700283
284 bool operator()(const GrGpuResource* resource) const {
robertphillipsc4ed6842016-05-24 14:17:12 -0700285 SkASSERT(!resource->getUniqueKey().isValid() &&
286 resource->resourcePriv().getScratchKey().isValid());
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400287
Robert Phillipsaee18c92019-09-06 11:48:27 -0400288 // isScratch() also tests that the resource is budgeted.
bsalomon12299ab2014-11-14 13:33:09 -0800289 if (resource->internalHasRef() || !resource->cacheAccess().isScratch()) {
bsalomon000f8292014-10-15 19:04:14 -0700290 return false;
bsalomonbcf0a522014-10-08 08:40:09 -0700291 }
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400292 return true;
bsalomonbcf0a522014-10-08 08:40:09 -0700293 }
bsalomonbcf0a522014-10-08 08:40:09 -0700294};
295
Robert Phillipsaee18c92019-09-06 11:48:27 -0400296GrGpuResource* GrResourceCache::findAndRefScratchResource(const GrScratchKey& scratchKey) {
bsalomon7775c852014-12-30 12:50:52 -0800297 SkASSERT(scratchKey.isValid());
robertphillipsee843b22016-10-04 05:30:20 -0700298
Robert Phillipsaee18c92019-09-06 11:48:27 -0400299 GrGpuResource* resource = fScratchMap.find(scratchKey, AvailableForScratchUse());
bsalomon71cb0c22014-11-14 12:10:14 -0800300 if (resource) {
bsalomon9f2d1572015-02-17 11:47:40 -0800301 this->refAndMakeResourceMRU(resource);
bsalomonb436ed62014-11-17 12:15:56 -0800302 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800303 }
304 return resource;
bsalomonbcf0a522014-10-08 08:40:09 -0700305}
bsalomon8b79d232014-11-10 10:19:06 -0800306
bsalomon0ea80f42015-02-11 10:49:59 -0800307void GrResourceCache::willRemoveScratchKey(const GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400308 ASSERT_SINGLE_OWNER
bsalomon3582d3e2015-02-13 14:20:05 -0800309 SkASSERT(resource->resourcePriv().getScratchKey().isValid());
robertphillipsc4ed6842016-05-24 14:17:12 -0700310 if (!resource->getUniqueKey().isValid()) {
311 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
312 }
bsalomon10e23ca2014-11-25 05:52:06 -0800313}
314
bsalomonf99e9612015-02-19 08:24:16 -0800315void GrResourceCache::removeUniqueKey(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400316 ASSERT_SINGLE_OWNER
bsalomon3f324322015-04-08 11:01:54 -0700317 // Someone has a ref to this resource in order to have removed the key. When the ref count
318 // reaches zero we will get a ref cnt notification and figure out what to do with it.
bsalomonf99e9612015-02-19 08:24:16 -0800319 if (resource->getUniqueKey().isValid()) {
320 SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
321 fUniqueHash.remove(resource->getUniqueKey());
322 }
323 resource->cacheAccess().removeUniqueKey();
robertphillipsc4ed6842016-05-24 14:17:12 -0700324 if (resource->resourcePriv().getScratchKey().isValid()) {
325 fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
326 }
327
Brian Salomon9bc76d92019-01-24 12:18:33 -0500328 // Removing a unique key from a kUnbudgetedCacheable resource would make the resource
329 // require purging. However, the resource must be ref'ed to get here and therefore can't
330 // be purgeable. We'll purge it when the refs reach zero.
331 SkASSERT(!resource->resourcePriv().isPurgeable());
bsalomonf99e9612015-02-19 08:24:16 -0800332 this->validate();
bsalomon23e619c2015-02-06 11:54:28 -0800333}
334
bsalomonf99e9612015-02-19 08:24:16 -0800335void GrResourceCache::changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400336 ASSERT_SINGLE_OWNER
bsalomon8b79d232014-11-10 10:19:06 -0800337 SkASSERT(resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800338 SkASSERT(this->isInCache(resource));
bsalomon8b79d232014-11-10 10:19:06 -0800339
bsalomonf99e9612015-02-19 08:24:16 -0800340 // If another resource has the new key, remove its key then install the key on this resource.
341 if (newKey.isValid()) {
Greg Daniel0d537802017-09-08 11:44:14 -0400342 if (GrGpuResource* old = fUniqueHash.find(newKey)) {
343 // If the old resource using the key is purgeable and is unreachable, then remove it.
Brian Salomon614c1a82018-12-19 15:42:06 -0500344 if (!old->resourcePriv().getScratchKey().isValid() &&
345 old->resourcePriv().isPurgeable()) {
Greg Daniel0d537802017-09-08 11:44:14 -0400346 old->cacheAccess().release();
347 } else {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500348 // removeUniqueKey expects an external owner of the resource.
349 this->removeUniqueKey(sk_ref_sp(old).get());
Greg Daniel0d537802017-09-08 11:44:14 -0400350 }
351 }
352 SkASSERT(nullptr == fUniqueHash.find(newKey));
353
robertphillipsc4ed6842016-05-24 14:17:12 -0700354 // Remove the entry for this resource if it already has a unique key.
355 if (resource->getUniqueKey().isValid()) {
356 SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
357 fUniqueHash.remove(resource->getUniqueKey());
358 SkASSERT(nullptr == fUniqueHash.find(resource->getUniqueKey()));
359 } else {
360 // 'resource' didn't have a valid unique key before so it is switching sides. Remove it
361 // from the ScratchMap
362 if (resource->resourcePriv().getScratchKey().isValid()) {
363 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
364 }
365 }
366
bsalomonf99e9612015-02-19 08:24:16 -0800367 resource->cacheAccess().setUniqueKey(newKey);
368 fUniqueHash.add(resource);
369 } else {
robertphillipsc4ed6842016-05-24 14:17:12 -0700370 this->removeUniqueKey(resource);
bsalomonf99e9612015-02-19 08:24:16 -0800371 }
372
bsalomon71cb0c22014-11-14 12:10:14 -0800373 this->validate();
bsalomon8b79d232014-11-10 10:19:06 -0800374}
bsalomon71cb0c22014-11-14 12:10:14 -0800375
bsalomon9f2d1572015-02-17 11:47:40 -0800376void GrResourceCache::refAndMakeResourceMRU(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400377 ASSERT_SINGLE_OWNER
bsalomon71cb0c22014-11-14 12:10:14 -0800378 SkASSERT(resource);
379 SkASSERT(this->isInCache(resource));
bsalomonddf30e62015-02-19 11:38:44 -0800380
Brian Salomon614c1a82018-12-19 15:42:06 -0500381 if (resource->resourcePriv().isPurgeable()) {
bsalomon9f2d1572015-02-17 11:47:40 -0800382 // It's about to become unpurgeable.
Derek Sollenbergeree479142017-05-24 11:41:33 -0400383 fPurgeableBytes -= resource->gpuMemorySize();
bsalomon9f2d1572015-02-17 11:47:40 -0800384 fPurgeableQueue.remove(resource);
bsalomonf320e042015-02-17 15:09:34 -0800385 this->addToNonpurgeableArray(resource);
Brian Salomon2c791fc2019-04-02 11:52:03 -0400386 } else if (!resource->cacheAccess().hasRef() &&
387 resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
388 SkASSERT(fNumBudgetedResourcesFlushWillMakePurgeable > 0);
389 fNumBudgetedResourcesFlushWillMakePurgeable--;
bsalomon9f2d1572015-02-17 11:47:40 -0800390 }
Brian Salomon01ceae92019-04-02 11:49:54 -0400391 resource->cacheAccess().ref();
bsalomonddf30e62015-02-19 11:38:44 -0800392
393 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
bsalomonf320e042015-02-17 15:09:34 -0800394 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800395}
396
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400397void GrResourceCache::notifyRefCntReachedZero(GrGpuResource* resource) {
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
bsalomon3f324322015-04-08 11:01:54 -0700406#ifdef SK_DEBUG
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400407 // When the timestamp overflows validate() is called. validate() checks that resources in
408 // the nonpurgeable array are indeed not purgeable. However, the movement from the array to
409 // the purgeable queue happens just below in this function. So we mark it as an exception.
410 if (resource->resourcePriv().isPurgeable()) {
411 fNewlyPurgeableResourceForValidation = resource;
bsalomon3f324322015-04-08 11:01:54 -0700412 }
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400413#endif
414 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
415 SkDEBUGCODE(fNewlyPurgeableResourceForValidation = nullptr);
bsalomon3f324322015-04-08 11:01:54 -0700416
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400417 if (!resource->resourcePriv().isPurgeable() &&
418 resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
419 ++fNumBudgetedResourcesFlushWillMakePurgeable;
bsalomon3f324322015-04-08 11:01:54 -0700420 }
421
Brian Salomon9bc76d92019-01-24 12:18:33 -0500422 if (!resource->resourcePriv().isPurgeable()) {
423 this->validate();
424 return;
425 }
426
bsalomonf320e042015-02-17 15:09:34 -0800427 this->removeFromNonpurgeableArray(resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800428 fPurgeableQueue.insert(resource);
Brian Salomon5e150852017-03-22 14:53:13 -0400429 resource->cacheAccess().setTimeWhenResourceBecomePurgeable();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400430 fPurgeableBytes += resource->gpuMemorySize();
bsalomon71cb0c22014-11-14 12:10:14 -0800431
Greg Daniel303e83e2018-09-10 14:10:19 -0400432 bool hasUniqueKey = resource->getUniqueKey().isValid();
433
Brian Salomon9bc76d92019-01-24 12:18:33 -0500434 GrBudgetedType budgetedType = resource->resourcePriv().budgetedType();
Brian Salomon614c1a82018-12-19 15:42:06 -0500435
Brian Salomon9bc76d92019-01-24 12:18:33 -0500436 if (budgetedType == GrBudgetedType::kBudgeted) {
437 // Purge the resource immediately if we're over budget
438 // Also purge if the resource has neither a valid scratch key nor a unique key.
439 bool hasKey = resource->resourcePriv().getScratchKey().isValid() || hasUniqueKey;
440 if (!this->overBudget() && hasKey) {
441 return;
442 }
443 } else {
444 // We keep unbudgeted resources with a unique key in the purgeable queue of the cache so
445 // they can be reused again by the image connected to the unique key.
446 if (hasUniqueKey && budgetedType == GrBudgetedType::kUnbudgetedCacheable) {
447 return;
448 }
449 // Check whether this resource could still be used as a scratch resource.
450 if (!resource->resourcePriv().refsWrappedObjects() &&
451 resource->resourcePriv().getScratchKey().isValid()) {
452 // We won't purge an existing resource to make room for this one.
Robert Phillipscf39f372019-09-03 10:29:20 -0400453 if (this->wouldFit(resource->gpuMemorySize())) {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500454 resource->resourcePriv().makeBudgeted();
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500455 return;
456 }
bsalomonc2f35b72015-01-23 07:19:22 -0800457 }
bsalomonc2f35b72015-01-23 07:19:22 -0800458 }
Brian Salomon9bc76d92019-01-24 12:18:33 -0500459
bsalomonf320e042015-02-17 15:09:34 -0800460 SkDEBUGCODE(int beforeCount = this->getResourceCount();)
bsalomon9f2d1572015-02-17 11:47:40 -0800461 resource->cacheAccess().release();
462 // We should at least free this resource, perhaps dependent resources as well.
bsalomonf320e042015-02-17 15:09:34 -0800463 SkASSERT(this->getResourceCount() < beforeCount);
bsalomon71cb0c22014-11-14 12:10:14 -0800464 this->validate();
465}
466
bsalomon0ea80f42015-02-11 10:49:59 -0800467void GrResourceCache::didChangeBudgetStatus(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400468 ASSERT_SINGLE_OWNER
bsalomon84c8e622014-11-17 09:33:27 -0800469 SkASSERT(resource);
470 SkASSERT(this->isInCache(resource));
471
472 size_t size = resource->gpuMemorySize();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500473 // Changing from BudgetedType::kUnbudgetedCacheable to another budgeted type could make
474 // resource become purgeable. However, we should never allow that transition. Wrapped
475 // resources are the only resources that can be in that state and they aren't allowed to
476 // transition from one budgeted state to another.
477 SkDEBUGCODE(bool wasPurgeable = resource->resourcePriv().isPurgeable());
478 if (resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
bsalomon84c8e622014-11-17 09:33:27 -0800479 ++fBudgetedCount;
480 fBudgetedBytes += size;
bsalomonafe30052015-01-16 07:32:33 -0800481#if GR_CACHE_STATS
482 fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes);
483 fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount);
484#endif
Brian Salomon2c791fc2019-04-02 11:52:03 -0400485 if (!resource->resourcePriv().isPurgeable() && !resource->cacheAccess().hasRef()) {
486 ++fNumBudgetedResourcesFlushWillMakePurgeable;
487 }
bsalomon84c8e622014-11-17 09:33:27 -0800488 this->purgeAsNeeded();
489 } else {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500490 SkASSERT(resource->resourcePriv().budgetedType() != GrBudgetedType::kUnbudgetedCacheable);
bsalomon84c8e622014-11-17 09:33:27 -0800491 --fBudgetedCount;
492 fBudgetedBytes -= size;
Brian Salomon2c791fc2019-04-02 11:52:03 -0400493 if (!resource->resourcePriv().isPurgeable() && !resource->cacheAccess().hasRef()) {
494 --fNumBudgetedResourcesFlushWillMakePurgeable;
495 }
bsalomon84c8e622014-11-17 09:33:27 -0800496 }
Brian Salomon9bc76d92019-01-24 12:18:33 -0500497 SkASSERT(wasPurgeable == resource->resourcePriv().isPurgeable());
Brian Osman39c08ac2017-07-26 09:36:09 -0400498 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800499 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomon84c8e622014-11-17 09:33:27 -0800500
501 this->validate();
502}
503
robertphillipsee843b22016-10-04 05:30:20 -0700504void GrResourceCache::purgeAsNeeded() {
bsalomon3f324322015-04-08 11:01:54 -0700505 SkTArray<GrUniqueKeyInvalidatedMessage> invalidKeyMsgs;
506 fInvalidUniqueKeyInbox.poll(&invalidKeyMsgs);
507 if (invalidKeyMsgs.count()) {
Robert Phillips427966a2018-12-20 17:20:43 -0500508 SkASSERT(fProxyProvider);
509
510 for (int i = 0; i < invalidKeyMsgs.count(); ++i) {
511 fProxyProvider->processInvalidUniqueKey(invalidKeyMsgs[i].key(), nullptr,
512 GrProxyProvider::InvalidateGPUResource::kYes);
513 SkASSERT(!this->findAndRefUniqueResource(invalidKeyMsgs[i].key()));
514 }
bsalomon3f324322015-04-08 11:01:54 -0700515 }
bsalomon71cb0c22014-11-14 12:10:14 -0800516
Brian Osman13dddce2017-05-09 13:19:50 -0400517 this->processFreedGpuResources();
518
bsalomon3f324322015-04-08 11:01:54 -0700519 bool stillOverbudget = this->overBudget();
520 while (stillOverbudget && fPurgeableQueue.count()) {
robertphillipsee843b22016-10-04 05:30:20 -0700521 GrGpuResource* resource = fPurgeableQueue.peek();
Brian Salomon614c1a82018-12-19 15:42:06 -0500522 SkASSERT(resource->resourcePriv().isPurgeable());
bsalomon9f2d1572015-02-17 11:47:40 -0800523 resource->cacheAccess().release();
bsalomon3f324322015-04-08 11:01:54 -0700524 stillOverbudget = this->overBudget();
bsalomon9f2d1572015-02-17 11:47:40 -0800525 }
bsalomon71cb0c22014-11-14 12:10:14 -0800526
bsalomonb436ed62014-11-17 12:15:56 -0800527 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800528}
529
Robert Phillips6eba0632018-03-28 12:25:42 -0400530void GrResourceCache::purgeUnlockedResources(bool scratchResourcesOnly) {
531 if (!scratchResourcesOnly) {
532 // We could disable maintaining the heap property here, but it would add a lot of
533 // complexity. Moreover, this is rarely called.
534 while (fPurgeableQueue.count()) {
535 GrGpuResource* resource = fPurgeableQueue.peek();
Brian Salomon614c1a82018-12-19 15:42:06 -0500536 SkASSERT(resource->resourcePriv().isPurgeable());
Robert Phillips6eba0632018-03-28 12:25:42 -0400537 resource->cacheAccess().release();
538 }
539 } else {
540 // Sort the queue
541 fPurgeableQueue.sort();
542
543 // Make a list of the scratch resources to delete
544 SkTDArray<GrGpuResource*> scratchResources;
545 for (int i = 0; i < fPurgeableQueue.count(); i++) {
546 GrGpuResource* resource = fPurgeableQueue.at(i);
Brian Salomon614c1a82018-12-19 15:42:06 -0500547 SkASSERT(resource->resourcePriv().isPurgeable());
Robert Phillips6eba0632018-03-28 12:25:42 -0400548 if (!resource->getUniqueKey().isValid()) {
549 *scratchResources.append() = resource;
550 }
551 }
552
553 // Delete the scratch resources. This must be done as a separate pass
554 // to avoid messing up the sorted order of the queue
555 for (int i = 0; i < scratchResources.count(); i++) {
556 scratchResources.getAt(i)->cacheAccess().release();
557 }
bsalomon9f2d1572015-02-17 11:47:40 -0800558 }
bsalomon71cb0c22014-11-14 12:10:14 -0800559
bsalomonb436ed62014-11-17 12:15:56 -0800560 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800561}
562
Brian Salomon5e150852017-03-22 14:53:13 -0400563void GrResourceCache::purgeResourcesNotUsedSince(GrStdSteadyClock::time_point purgeTime) {
564 while (fPurgeableQueue.count()) {
565 const GrStdSteadyClock::time_point resourceTime =
566 fPurgeableQueue.peek()->cacheAccess().timeWhenResourceBecamePurgeable();
567 if (resourceTime >= purgeTime) {
568 // Resources were given both LRU timestamps and tagged with a frame number when
569 // they first became purgeable. The LRU timestamp won't change again until the
570 // resource is made non-purgeable again. So, at this point all the remaining
571 // resources in the timestamp-sorted queue will have a frame number >= to this
572 // one.
573 break;
574 }
575 GrGpuResource* resource = fPurgeableQueue.peek();
Brian Salomon614c1a82018-12-19 15:42:06 -0500576 SkASSERT(resource->resourcePriv().isPurgeable());
Brian Salomon5e150852017-03-22 14:53:13 -0400577 resource->cacheAccess().release();
578 }
579}
580
Derek Sollenberger5480a182017-05-25 16:43:59 -0400581void GrResourceCache::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
582
583 const size_t tmpByteBudget = SkTMax((size_t)0, fBytes - bytesToPurge);
584 bool stillOverbudget = tmpByteBudget < fBytes;
585
586 if (preferScratchResources && bytesToPurge < fPurgeableBytes) {
587 // Sort the queue
588 fPurgeableQueue.sort();
589
590 // Make a list of the scratch resources to delete
591 SkTDArray<GrGpuResource*> scratchResources;
592 size_t scratchByteCount = 0;
593 for (int i = 0; i < fPurgeableQueue.count() && stillOverbudget; i++) {
594 GrGpuResource* resource = fPurgeableQueue.at(i);
Brian Salomon614c1a82018-12-19 15:42:06 -0500595 SkASSERT(resource->resourcePriv().isPurgeable());
Derek Sollenberger5480a182017-05-25 16:43:59 -0400596 if (!resource->getUniqueKey().isValid()) {
597 *scratchResources.append() = resource;
598 scratchByteCount += resource->gpuMemorySize();
599 stillOverbudget = tmpByteBudget < fBytes - scratchByteCount;
600 }
601 }
602
603 // Delete the scratch resources. This must be done as a separate pass
604 // to avoid messing up the sorted order of the queue
605 for (int i = 0; i < scratchResources.count(); i++) {
606 scratchResources.getAt(i)->cacheAccess().release();
607 }
608 stillOverbudget = tmpByteBudget < fBytes;
609
610 this->validate();
611 }
612
613 // Purge any remaining resources in LRU order
614 if (stillOverbudget) {
615 const size_t cachedByteCount = fMaxBytes;
616 fMaxBytes = tmpByteBudget;
617 this->purgeAsNeeded();
618 fMaxBytes = cachedByteCount;
619 }
620}
Brian Salomon8cefa3e2019-04-04 11:39:55 -0400621bool GrResourceCache::requestsFlush() const {
622 return this->overBudget() && !fPurgeableQueue.count() &&
623 fNumBudgetedResourcesFlushWillMakePurgeable > 0;
624}
625
Derek Sollenberger5480a182017-05-25 16:43:59 -0400626
Brian Salomon876a0172019-03-08 11:12:14 -0500627void GrResourceCache::insertDelayedResourceUnref(GrGpuResource* resource) {
Brian Osman13dddce2017-05-09 13:19:50 -0400628 resource->ref();
Brian Salomon876a0172019-03-08 11:12:14 -0500629 uint32_t id = resource->uniqueID().asUInt();
630 if (auto* data = fResourcesAwaitingUnref.find(id)) {
631 data->addRef();
632 } else {
633 fResourcesAwaitingUnref.set(id, {resource});
634 }
Brian Osman13dddce2017-05-09 13:19:50 -0400635}
636
637void GrResourceCache::processFreedGpuResources() {
638 SkTArray<GrGpuResourceFreedMessage> msgs;
639 fFreedGpuResourceInbox.poll(&msgs);
640 for (int i = 0; i < msgs.count(); ++i) {
Brian Salomon238069b2018-07-11 15:58:57 -0400641 SkASSERT(msgs[i].fOwningUniqueID == fContextUniqueID);
Brian Salomon876a0172019-03-08 11:12:14 -0500642 uint32_t id = msgs[i].fResource->uniqueID().asUInt();
643 ResourceAwaitingUnref* info = fResourcesAwaitingUnref.find(id);
Greg Danielb2acf0a2018-09-12 09:17:11 -0400644 // If we called release or abandon on the GrContext we will have already released our ref on
645 // the GrGpuResource. If then the message arrives before the actual GrContext gets destroyed
646 // we will try to process the message when we destroy the GrContext. This protects us from
647 // trying to unref the resource twice.
Brian Salomon876a0172019-03-08 11:12:14 -0500648 if (info) {
649 info->unref();
650 if (info->finished()) {
651 fResourcesAwaitingUnref.remove(id);
652 }
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
bsalomone2e87f32016-09-22 12:42:11 -0700692 SkTQSort(fNonpurgeableResources.begin(), fNonpurgeableResources.end() - 1,
693 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 {
864 ScratchMap::ConstIter iter(&fScratchMap);
865
866 int count = 0;
867 for ( ; !iter.done(); ++iter) {
868 const GrGpuResource* resource = *iter;
869 SkASSERT(resource->resourcePriv().getScratchKey().isValid());
870 SkASSERT(!resource->getUniqueKey().isValid());
871 count++;
872 }
873 SkASSERT(count == fScratchMap.count()); // ensure the iterator is working correctly
874 }
875
bsalomonf320e042015-02-17 15:09:34 -0800876 Stats stats(this);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400877 size_t purgeableBytes = 0;
Brian Salomon2c791fc2019-04-02 11:52:03 -0400878 int numBudgetedResourcesFlushWillMakePurgeable = 0;
bsalomonf320e042015-02-17 15:09:34 -0800879
880 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500881 SkASSERT(!fNonpurgeableResources[i]->resourcePriv().isPurgeable() ||
bsalomon3f324322015-04-08 11:01:54 -0700882 fNewlyPurgeableResourceForValidation == fNonpurgeableResources[i]);
bsalomonf320e042015-02-17 15:09:34 -0800883 SkASSERT(*fNonpurgeableResources[i]->cacheAccess().accessCacheIndex() == i);
884 SkASSERT(!fNonpurgeableResources[i]->wasDestroyed());
Brian Salomon2c791fc2019-04-02 11:52:03 -0400885 if (fNonpurgeableResources[i]->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted &&
886 !fNonpurgeableResources[i]->cacheAccess().hasRef() &&
887 fNewlyPurgeableResourceForValidation != fNonpurgeableResources[i]) {
Brian Salomon2c791fc2019-04-02 11:52:03 -0400888 ++numBudgetedResourcesFlushWillMakePurgeable;
889 }
bsalomonf320e042015-02-17 15:09:34 -0800890 stats.update(fNonpurgeableResources[i]);
bsalomon71cb0c22014-11-14 12:10:14 -0800891 }
bsalomon9f2d1572015-02-17 11:47:40 -0800892 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500893 SkASSERT(fPurgeableQueue.at(i)->resourcePriv().isPurgeable());
bsalomonf320e042015-02-17 15:09:34 -0800894 SkASSERT(*fPurgeableQueue.at(i)->cacheAccess().accessCacheIndex() == i);
895 SkASSERT(!fPurgeableQueue.at(i)->wasDestroyed());
896 stats.update(fPurgeableQueue.at(i));
Derek Sollenbergeree479142017-05-24 11:41:33 -0400897 purgeableBytes += fPurgeableQueue.at(i)->gpuMemorySize();
bsalomon9f2d1572015-02-17 11:47:40 -0800898 }
899
bsalomonf320e042015-02-17 15:09:34 -0800900 SkASSERT(fCount == this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800901 SkASSERT(fBudgetedCount <= fCount);
bsalomonf320e042015-02-17 15:09:34 -0800902 SkASSERT(fBudgetedBytes <= fBytes);
903 SkASSERT(stats.fBytes == fBytes);
Brian Salomon2c791fc2019-04-02 11:52:03 -0400904 SkASSERT(fNumBudgetedResourcesFlushWillMakePurgeable ==
905 numBudgetedResourcesFlushWillMakePurgeable);
bsalomonf320e042015-02-17 15:09:34 -0800906 SkASSERT(stats.fBudgetedBytes == fBudgetedBytes);
907 SkASSERT(stats.fBudgetedCount == fBudgetedCount);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400908 SkASSERT(purgeableBytes == fPurgeableBytes);
bsalomon71cb0c22014-11-14 12:10:14 -0800909#if GR_CACHE_STATS
bsalomondace19e2014-11-17 07:34:06 -0800910 SkASSERT(fBudgetedHighWaterCount <= fHighWaterCount);
911 SkASSERT(fBudgetedHighWaterBytes <= fHighWaterBytes);
bsalomonf320e042015-02-17 15:09:34 -0800912 SkASSERT(fBytes <= fHighWaterBytes);
913 SkASSERT(fCount <= fHighWaterCount);
914 SkASSERT(fBudgetedBytes <= fBudgetedHighWaterBytes);
915 SkASSERT(fBudgetedCount <= fBudgetedHighWaterCount);
bsalomon71cb0c22014-11-14 12:10:14 -0800916#endif
bsalomon8718aaf2015-02-19 07:24:21 -0800917 SkASSERT(stats.fContent == fUniqueHash.count());
bsalomonf320e042015-02-17 15:09:34 -0800918 SkASSERT(stats.fScratch + stats.fCouldBeScratch == fScratchMap.count());
bsalomon71cb0c22014-11-14 12:10:14 -0800919
bsalomon3f324322015-04-08 11:01:54 -0700920 // This assertion is not currently valid because we can be in recursive notifyCntReachedZero()
bsalomon12299ab2014-11-14 13:33:09 -0800921 // calls. This will be fixed when subresource registration is explicit.
bsalomondace19e2014-11-17 07:34:06 -0800922 // bool overBudget = budgetedBytes > fMaxBytes || budgetedCount > fMaxCount;
bsalomon12299ab2014-11-14 13:33:09 -0800923 // SkASSERT(!overBudget || locked == count || fPurging);
bsalomon71cb0c22014-11-14 12:10:14 -0800924}
bsalomonf320e042015-02-17 15:09:34 -0800925
926bool GrResourceCache::isInCache(const GrGpuResource* resource) const {
927 int index = *resource->cacheAccess().accessCacheIndex();
928 if (index < 0) {
929 return false;
930 }
931 if (index < fPurgeableQueue.count() && fPurgeableQueue.at(index) == resource) {
932 return true;
933 }
934 if (index < fNonpurgeableResources.count() && fNonpurgeableResources[index] == resource) {
935 return true;
936 }
937 SkDEBUGFAIL("Resource index should be -1 or the resource should be in the cache.");
938 return false;
939}
940
bsalomon71cb0c22014-11-14 12:10:14 -0800941#endif