blob: 6d9a961886ffd764dfe80b0c247c562a6d12817c [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
Robert Phillipsddc21482019-10-16 14:30:09 -040030DECLARE_SKMESSAGEBUS_MESSAGE(GrTextureFreedMessage);
Brian Osman13dddce2017-05-09 13:19:50 -040031
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
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) {
81 fTexture = skstd::exchange(that.fTexture, nullptr);
Brian Salomon876a0172019-03-08 11:12:14 -050082 fNumUnrefs = skstd::exchange(that.fNumUnrefs, 0);
83}
84
Robert Phillipsddc21482019-10-16 14:30:09 -040085inline GrResourceCache::TextureAwaitingUnref& GrResourceCache::TextureAwaitingUnref::operator=(
86 TextureAwaitingUnref&& that) {
87 fTexture = skstd::exchange(that.fTexture, nullptr);
Brian Salomon876a0172019-03-08 11:12:14 -050088 fNumUnrefs = skstd::exchange(that.fNumUnrefs, 0);
89 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
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)
Robert Phillipsddc21482019-10-16 14:30:09 -0400115 , fFreedTextureInbox(contextUniqueID)
Brian Salomon238069b2018-07-11 15:58:57 -0400116 , 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.
Robert Phillipsddc21482019-10-16 14:30:09 -0400207 fTexturesAwaitingUnref.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);
Robert Phillipsddc21482019-10-16 14:30:09 -0400229 SkASSERT(!fTexturesAwaitingUnref.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.
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 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);
Robert Phillipsddc21482019-10-16 14:30:09 -0400266 SkASSERT(!fTexturesAwaitingUnref.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
Robert Phillipsddc21482019-10-16 14:30:09 -0400627void GrResourceCache::insertDelayedTextureUnref(GrTexture* texture) {
628 texture->ref();
629 uint32_t id = texture->uniqueID().asUInt();
630 if (auto* data = fTexturesAwaitingUnref.find(id)) {
Brian Salomon876a0172019-03-08 11:12:14 -0500631 data->addRef();
632 } else {
Robert Phillipsddc21482019-10-16 14:30:09 -0400633 fTexturesAwaitingUnref.set(id, {texture});
Brian Salomon876a0172019-03-08 11:12:14 -0500634 }
Brian Osman13dddce2017-05-09 13:19:50 -0400635}
636
637void GrResourceCache::processFreedGpuResources() {
Robert Phillips1dfc77c2019-10-16 16:39:45 -0400638 if (!fTexturesAwaitingUnref.count()) {
639 return;
640 }
641
Robert Phillipsddc21482019-10-16 14:30:09 -0400642 SkTArray<GrTextureFreedMessage> msgs;
643 fFreedTextureInbox.poll(&msgs);
Brian Osman13dddce2017-05-09 13:19:50 -0400644 for (int i = 0; i < msgs.count(); ++i) {
Brian Salomon238069b2018-07-11 15:58:57 -0400645 SkASSERT(msgs[i].fOwningUniqueID == fContextUniqueID);
Robert Phillipsddc21482019-10-16 14:30:09 -0400646 uint32_t id = msgs[i].fTexture->uniqueID().asUInt();
647 TextureAwaitingUnref* info = fTexturesAwaitingUnref.find(id);
Greg Danielb2acf0a2018-09-12 09:17:11 -0400648 // If we called release or abandon on the GrContext we will have already released our ref on
649 // the GrGpuResource. If then the message arrives before the actual GrContext gets destroyed
650 // we will try to process the message when we destroy the GrContext. This protects us from
651 // trying to unref the resource twice.
Brian Salomon876a0172019-03-08 11:12:14 -0500652 if (info) {
653 info->unref();
654 if (info->finished()) {
Robert Phillipsddc21482019-10-16 14:30:09 -0400655 fTexturesAwaitingUnref.remove(id);
Brian Salomon876a0172019-03-08 11:12:14 -0500656 }
Greg Danielb2acf0a2018-09-12 09:17:11 -0400657 }
Brian Osman13dddce2017-05-09 13:19:50 -0400658 }
659}
660
bsalomonf320e042015-02-17 15:09:34 -0800661void GrResourceCache::addToNonpurgeableArray(GrGpuResource* resource) {
662 int index = fNonpurgeableResources.count();
663 *fNonpurgeableResources.append() = resource;
664 *resource->cacheAccess().accessCacheIndex() = index;
665}
666
667void GrResourceCache::removeFromNonpurgeableArray(GrGpuResource* resource) {
668 int* index = resource->cacheAccess().accessCacheIndex();
669 // Fill the whole we will create in the array with the tail object, adjust its index, and
670 // then pop the array
671 GrGpuResource* tail = *(fNonpurgeableResources.end() - 1);
672 SkASSERT(fNonpurgeableResources[*index] == resource);
673 fNonpurgeableResources[*index] = tail;
674 *tail->cacheAccess().accessCacheIndex() = *index;
675 fNonpurgeableResources.pop();
676 SkDEBUGCODE(*index = -1);
677}
678
bsalomonddf30e62015-02-19 11:38:44 -0800679uint32_t GrResourceCache::getNextTimestamp() {
680 // If we wrap then all the existing resources will appear older than any resources that get
681 // a timestamp after the wrap.
682 if (0 == fTimestamp) {
683 int count = this->getResourceCount();
684 if (count) {
685 // Reset all the timestamps. We sort the resources by timestamp and then assign
686 // sequential timestamps beginning with 0. This is O(n*lg(n)) but it should be extremely
687 // rare.
688 SkTDArray<GrGpuResource*> sortedPurgeableResources;
689 sortedPurgeableResources.setReserve(fPurgeableQueue.count());
690
691 while (fPurgeableQueue.count()) {
692 *sortedPurgeableResources.append() = fPurgeableQueue.peek();
693 fPurgeableQueue.pop();
694 }
robertphillipsee843b22016-10-04 05:30:20 -0700695
bsalomone2e87f32016-09-22 12:42:11 -0700696 SkTQSort(fNonpurgeableResources.begin(), fNonpurgeableResources.end() - 1,
697 CompareTimestamp);
bsalomonddf30e62015-02-19 11:38:44 -0800698
699 // Pick resources out of the purgeable and non-purgeable arrays based on lowest
700 // timestamp and assign new timestamps.
701 int currP = 0;
702 int currNP = 0;
703 while (currP < sortedPurgeableResources.count() &&
mtklein56da0252015-11-16 11:16:23 -0800704 currNP < fNonpurgeableResources.count()) {
bsalomonddf30e62015-02-19 11:38:44 -0800705 uint32_t tsP = sortedPurgeableResources[currP]->cacheAccess().timestamp();
706 uint32_t tsNP = fNonpurgeableResources[currNP]->cacheAccess().timestamp();
707 SkASSERT(tsP != tsNP);
708 if (tsP < tsNP) {
709 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
710 } else {
711 // Correct the index in the nonpurgeable array stored on the resource post-sort.
712 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
713 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
714 }
715 }
716
717 // The above loop ended when we hit the end of one array. Finish the other one.
718 while (currP < sortedPurgeableResources.count()) {
719 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
720 }
721 while (currNP < fNonpurgeableResources.count()) {
722 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
723 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
724 }
725
726 // Rebuild the queue.
727 for (int i = 0; i < sortedPurgeableResources.count(); ++i) {
728 fPurgeableQueue.insert(sortedPurgeableResources[i]);
729 }
730
731 this->validate();
732 SkASSERT(count == this->getResourceCount());
733
734 // count should be the next timestamp we return.
735 SkASSERT(fTimestamp == SkToU32(count));
mtklein56da0252015-11-16 11:16:23 -0800736 }
bsalomonddf30e62015-02-19 11:38:44 -0800737 }
738 return fTimestamp++;
739}
740
ericrk0a5fa482015-09-15 14:16:10 -0700741void GrResourceCache::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
742 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
743 fNonpurgeableResources[i]->dumpMemoryStatistics(traceMemoryDump);
744 }
745 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
746 fPurgeableQueue.at(i)->dumpMemoryStatistics(traceMemoryDump);
747 }
748}
749
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500750#if GR_CACHE_STATS
751void GrResourceCache::getStats(Stats* stats) const {
752 stats->reset();
753
754 stats->fTotal = this->getResourceCount();
755 stats->fNumNonPurgeable = fNonpurgeableResources.count();
756 stats->fNumPurgeable = fPurgeableQueue.count();
757
758 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
759 stats->update(fNonpurgeableResources[i]);
760 }
761 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
762 stats->update(fPurgeableQueue.at(i));
763 }
764}
765
766#if GR_TEST_UTILS
767void GrResourceCache::dumpStats(SkString* out) const {
768 this->validate();
769
770 Stats stats;
771
772 this->getStats(&stats);
773
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500774 float byteUtilization = (100.f * fBudgetedBytes) / fMaxBytes;
775
Robert Phillipscf39f372019-09-03 10:29:20 -0400776 out->appendf("Budget: %d bytes\n", (int)fMaxBytes);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500777 out->appendf("\t\tEntry Count: current %d"
Robert Phillipscf39f372019-09-03 10:29:20 -0400778 " (%d budgeted, %d wrapped, %d locked, %d scratch), high %d\n",
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500779 stats.fTotal, fBudgetedCount, stats.fWrapped, stats.fNumNonPurgeable,
Robert Phillipscf39f372019-09-03 10:29:20 -0400780 stats.fScratch, fHighWaterCount);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500781 out->appendf("\t\tEntry Bytes: current %d (budgeted %d, %.2g%% full, %d unbudgeted) high %d\n",
782 SkToInt(fBytes), SkToInt(fBudgetedBytes), byteUtilization,
783 SkToInt(stats.fUnbudgetedSize), SkToInt(fHighWaterBytes));
784}
785
786void GrResourceCache::dumpStatsKeyValuePairs(SkTArray<SkString>* keys,
787 SkTArray<double>* values) const {
788 this->validate();
789
790 Stats stats;
791 this->getStats(&stats);
792
793 keys->push_back(SkString("gpu_cache_purgable_entries")); values->push_back(stats.fNumPurgeable);
794}
795#endif
796
797#endif
798
bsalomon71cb0c22014-11-14 12:10:14 -0800799#ifdef SK_DEBUG
bsalomon0ea80f42015-02-11 10:49:59 -0800800void GrResourceCache::validate() const {
bsalomonc2f35b72015-01-23 07:19:22 -0800801 // Reduce the frequency of validations for large resource counts.
802 static SkRandom gRandom;
803 int mask = (SkNextPow2(fCount + 1) >> 5) - 1;
804 if (~mask && (gRandom.nextU() & mask)) {
805 return;
806 }
807
bsalomonf320e042015-02-17 15:09:34 -0800808 struct Stats {
809 size_t fBytes;
810 int fBudgetedCount;
811 size_t fBudgetedBytes;
812 int fLocked;
813 int fScratch;
814 int fCouldBeScratch;
815 int fContent;
816 const ScratchMap* fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800817 const UniqueHash* fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800818
bsalomonf320e042015-02-17 15:09:34 -0800819 Stats(const GrResourceCache* cache) {
820 memset(this, 0, sizeof(*this));
821 fScratchMap = &cache->fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800822 fUniqueHash = &cache->fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800823 }
824
bsalomonf320e042015-02-17 15:09:34 -0800825 void update(GrGpuResource* resource) {
826 fBytes += resource->gpuMemorySize();
bsalomondace19e2014-11-17 07:34:06 -0800827
Brian Salomon614c1a82018-12-19 15:42:06 -0500828 if (!resource->resourcePriv().isPurgeable()) {
bsalomonf320e042015-02-17 15:09:34 -0800829 ++fLocked;
830 }
bsalomon9f2d1572015-02-17 11:47:40 -0800831
robertphillipsc4ed6842016-05-24 14:17:12 -0700832 const GrScratchKey& scratchKey = resource->resourcePriv().getScratchKey();
833 const GrUniqueKey& uniqueKey = resource->getUniqueKey();
834
bsalomonf320e042015-02-17 15:09:34 -0800835 if (resource->cacheAccess().isScratch()) {
robertphillipsc4ed6842016-05-24 14:17:12 -0700836 SkASSERT(!uniqueKey.isValid());
bsalomonf320e042015-02-17 15:09:34 -0800837 ++fScratch;
robertphillipsc4ed6842016-05-24 14:17:12 -0700838 SkASSERT(fScratchMap->countForKey(scratchKey));
kkinnunen2e6055b2016-04-22 01:48:29 -0700839 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
robertphillipsc4ed6842016-05-24 14:17:12 -0700840 } else if (scratchKey.isValid()) {
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500841 SkASSERT(GrBudgetedType::kBudgeted != resource->resourcePriv().budgetedType() ||
robertphillipsc4ed6842016-05-24 14:17:12 -0700842 uniqueKey.isValid());
843 if (!uniqueKey.isValid()) {
mtklein4e976072016-08-08 09:06:27 -0700844 ++fCouldBeScratch;
robertphillipsc4ed6842016-05-24 14:17:12 -0700845 SkASSERT(fScratchMap->countForKey(scratchKey));
846 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700847 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
bsalomonf320e042015-02-17 15:09:34 -0800848 }
bsalomon8718aaf2015-02-19 07:24:21 -0800849 if (uniqueKey.isValid()) {
bsalomonf320e042015-02-17 15:09:34 -0800850 ++fContent;
bsalomon8718aaf2015-02-19 07:24:21 -0800851 SkASSERT(fUniqueHash->find(uniqueKey) == resource);
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500852 SkASSERT(GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType() ||
Brian Osman0562eb92017-05-08 11:16:39 -0400853 resource->resourcePriv().refsWrappedObjects());
robertphillipsc4ed6842016-05-24 14:17:12 -0700854
855 if (scratchKey.isValid()) {
856 SkASSERT(!fScratchMap->has(resource, scratchKey));
857 }
bsalomonf320e042015-02-17 15:09:34 -0800858 }
859
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500860 if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
bsalomonf320e042015-02-17 15:09:34 -0800861 ++fBudgetedCount;
862 fBudgetedBytes += resource->gpuMemorySize();
863 }
bsalomon9f2d1572015-02-17 11:47:40 -0800864 }
bsalomonf320e042015-02-17 15:09:34 -0800865 };
866
robertphillipsc4ed6842016-05-24 14:17:12 -0700867 {
868 ScratchMap::ConstIter iter(&fScratchMap);
869
870 int count = 0;
871 for ( ; !iter.done(); ++iter) {
872 const GrGpuResource* resource = *iter;
873 SkASSERT(resource->resourcePriv().getScratchKey().isValid());
874 SkASSERT(!resource->getUniqueKey().isValid());
875 count++;
876 }
877 SkASSERT(count == fScratchMap.count()); // ensure the iterator is working correctly
878 }
879
bsalomonf320e042015-02-17 15:09:34 -0800880 Stats stats(this);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400881 size_t purgeableBytes = 0;
Brian Salomon2c791fc2019-04-02 11:52:03 -0400882 int numBudgetedResourcesFlushWillMakePurgeable = 0;
bsalomonf320e042015-02-17 15:09:34 -0800883
884 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500885 SkASSERT(!fNonpurgeableResources[i]->resourcePriv().isPurgeable() ||
bsalomon3f324322015-04-08 11:01:54 -0700886 fNewlyPurgeableResourceForValidation == fNonpurgeableResources[i]);
bsalomonf320e042015-02-17 15:09:34 -0800887 SkASSERT(*fNonpurgeableResources[i]->cacheAccess().accessCacheIndex() == i);
888 SkASSERT(!fNonpurgeableResources[i]->wasDestroyed());
Brian Salomon2c791fc2019-04-02 11:52:03 -0400889 if (fNonpurgeableResources[i]->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted &&
890 !fNonpurgeableResources[i]->cacheAccess().hasRef() &&
891 fNewlyPurgeableResourceForValidation != fNonpurgeableResources[i]) {
Brian Salomon2c791fc2019-04-02 11:52:03 -0400892 ++numBudgetedResourcesFlushWillMakePurgeable;
893 }
bsalomonf320e042015-02-17 15:09:34 -0800894 stats.update(fNonpurgeableResources[i]);
bsalomon71cb0c22014-11-14 12:10:14 -0800895 }
bsalomon9f2d1572015-02-17 11:47:40 -0800896 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500897 SkASSERT(fPurgeableQueue.at(i)->resourcePriv().isPurgeable());
bsalomonf320e042015-02-17 15:09:34 -0800898 SkASSERT(*fPurgeableQueue.at(i)->cacheAccess().accessCacheIndex() == i);
899 SkASSERT(!fPurgeableQueue.at(i)->wasDestroyed());
900 stats.update(fPurgeableQueue.at(i));
Derek Sollenbergeree479142017-05-24 11:41:33 -0400901 purgeableBytes += fPurgeableQueue.at(i)->gpuMemorySize();
bsalomon9f2d1572015-02-17 11:47:40 -0800902 }
903
bsalomonf320e042015-02-17 15:09:34 -0800904 SkASSERT(fCount == this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800905 SkASSERT(fBudgetedCount <= fCount);
bsalomonf320e042015-02-17 15:09:34 -0800906 SkASSERT(fBudgetedBytes <= fBytes);
907 SkASSERT(stats.fBytes == fBytes);
Brian Salomon2c791fc2019-04-02 11:52:03 -0400908 SkASSERT(fNumBudgetedResourcesFlushWillMakePurgeable ==
909 numBudgetedResourcesFlushWillMakePurgeable);
bsalomonf320e042015-02-17 15:09:34 -0800910 SkASSERT(stats.fBudgetedBytes == fBudgetedBytes);
911 SkASSERT(stats.fBudgetedCount == fBudgetedCount);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400912 SkASSERT(purgeableBytes == fPurgeableBytes);
bsalomon71cb0c22014-11-14 12:10:14 -0800913#if GR_CACHE_STATS
bsalomondace19e2014-11-17 07:34:06 -0800914 SkASSERT(fBudgetedHighWaterCount <= fHighWaterCount);
915 SkASSERT(fBudgetedHighWaterBytes <= fHighWaterBytes);
bsalomonf320e042015-02-17 15:09:34 -0800916 SkASSERT(fBytes <= fHighWaterBytes);
917 SkASSERT(fCount <= fHighWaterCount);
918 SkASSERT(fBudgetedBytes <= fBudgetedHighWaterBytes);
919 SkASSERT(fBudgetedCount <= fBudgetedHighWaterCount);
bsalomon71cb0c22014-11-14 12:10:14 -0800920#endif
bsalomon8718aaf2015-02-19 07:24:21 -0800921 SkASSERT(stats.fContent == fUniqueHash.count());
bsalomonf320e042015-02-17 15:09:34 -0800922 SkASSERT(stats.fScratch + stats.fCouldBeScratch == fScratchMap.count());
bsalomon71cb0c22014-11-14 12:10:14 -0800923
bsalomon3f324322015-04-08 11:01:54 -0700924 // This assertion is not currently valid because we can be in recursive notifyCntReachedZero()
bsalomon12299ab2014-11-14 13:33:09 -0800925 // calls. This will be fixed when subresource registration is explicit.
bsalomondace19e2014-11-17 07:34:06 -0800926 // bool overBudget = budgetedBytes > fMaxBytes || budgetedCount > fMaxCount;
bsalomon12299ab2014-11-14 13:33:09 -0800927 // SkASSERT(!overBudget || locked == count || fPurging);
bsalomon71cb0c22014-11-14 12:10:14 -0800928}
bsalomonf320e042015-02-17 15:09:34 -0800929
930bool GrResourceCache::isInCache(const GrGpuResource* resource) const {
931 int index = *resource->cacheAccess().accessCacheIndex();
932 if (index < 0) {
933 return false;
934 }
935 if (index < fPurgeableQueue.count() && fPurgeableQueue.at(index) == resource) {
936 return true;
937 }
938 if (index < fNonpurgeableResources.count() && fNonpurgeableResources[index] == resource) {
939 return true;
940 }
941 SkDEBUGFAIL("Resource index should be -1 or the resource should be in the cache.");
942 return false;
943}
944
bsalomon71cb0c22014-11-14 12:10:14 -0800945#endif