blob: 4dd0481daa2fa7bc65a7cbdd671f374a927fdd28 [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
Brian Salomon43b882b2018-09-07 16:29:14 -0400126void GrResourceCache::setLimits(int count, size_t bytes) {
bsalomon71cb0c22014-11-14 12:10:14 -0800127 fMaxCount = count;
128 fMaxBytes = bytes;
129 this->purgeAsNeeded();
130}
131
bsalomon0ea80f42015-02-11 10:49:59 -0800132void GrResourceCache::insertResource(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400133 ASSERT_SINGLE_OWNER
bsalomon49f085d2014-09-05 13:34:00 -0700134 SkASSERT(resource);
bsalomon16961262014-08-26 14:01:07 -0700135 SkASSERT(!this->isInCache(resource));
bsalomonf320e042015-02-17 15:09:34 -0800136 SkASSERT(!resource->wasDestroyed());
Brian Salomon614c1a82018-12-19 15:42:06 -0500137 SkASSERT(!resource->resourcePriv().isPurgeable());
bsalomonddf30e62015-02-19 11:38:44 -0800138
139 // We must set the timestamp before adding to the array in case the timestamp wraps and we wind
140 // up iterating over all the resources that already have timestamps.
141 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
142
bsalomonf320e042015-02-17 15:09:34 -0800143 this->addToNonpurgeableArray(resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800144
bsalomondace19e2014-11-17 07:34:06 -0800145 size_t size = resource->gpuMemorySize();
bsalomonf320e042015-02-17 15:09:34 -0800146 SkDEBUGCODE(++fCount;)
bsalomon84c8e622014-11-17 09:33:27 -0800147 fBytes += size;
bsalomon82b1d622014-11-14 13:59:57 -0800148#if GR_CACHE_STATS
bsalomonf320e042015-02-17 15:09:34 -0800149 fHighWaterCount = SkTMax(this->getResourceCount(), fHighWaterCount);
bsalomon82b1d622014-11-14 13:59:57 -0800150 fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes);
151#endif
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500152 if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
bsalomondace19e2014-11-17 07:34:06 -0800153 ++fBudgetedCount;
154 fBudgetedBytes += size;
Brian Osman39c08ac2017-07-26 09:36:09 -0400155 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800156 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomondace19e2014-11-17 07:34:06 -0800157#if GR_CACHE_STATS
158 fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount);
159 fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes);
160#endif
161 }
robertphillipsc4ed6842016-05-24 14:17:12 -0700162 if (resource->resourcePriv().getScratchKey().isValid() &&
163 !resource->getUniqueKey().isValid()) {
kkinnunen2e6055b2016-04-22 01:48:29 -0700164 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
bsalomon3582d3e2015-02-13 14:20:05 -0800165 fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
bsalomon744998e2014-08-28 09:54:34 -0700166 }
bsalomon9f2d1572015-02-17 11:47:40 -0800167
bsalomon71cb0c22014-11-14 12:10:14 -0800168 this->purgeAsNeeded();
bsalomonc8dc1f72014-08-21 13:02:13 -0700169}
170
bsalomon0ea80f42015-02-11 10:49:59 -0800171void GrResourceCache::removeResource(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400172 ASSERT_SINGLE_OWNER
bsalomon9f2d1572015-02-17 11:47:40 -0800173 this->validate();
bsalomon16961262014-08-26 14:01:07 -0700174 SkASSERT(this->isInCache(resource));
bsalomondace19e2014-11-17 07:34:06 -0800175
Derek Sollenbergeree479142017-05-24 11:41:33 -0400176 size_t size = resource->gpuMemorySize();
Brian Salomon614c1a82018-12-19 15:42:06 -0500177 if (resource->resourcePriv().isPurgeable()) {
bsalomon9f2d1572015-02-17 11:47:40 -0800178 fPurgeableQueue.remove(resource);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400179 fPurgeableBytes -= size;
bsalomonf320e042015-02-17 15:09:34 -0800180 } else {
181 this->removeFromNonpurgeableArray(resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800182 }
183
bsalomonf320e042015-02-17 15:09:34 -0800184 SkDEBUGCODE(--fCount;)
bsalomondace19e2014-11-17 07:34:06 -0800185 fBytes -= size;
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500186 if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
bsalomondace19e2014-11-17 07:34:06 -0800187 --fBudgetedCount;
188 fBudgetedBytes -= size;
Brian Osman39c08ac2017-07-26 09:36:09 -0400189 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800190 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomondace19e2014-11-17 07:34:06 -0800191 }
192
robertphillipsc4ed6842016-05-24 14:17:12 -0700193 if (resource->resourcePriv().getScratchKey().isValid() &&
194 !resource->getUniqueKey().isValid()) {
bsalomon3582d3e2015-02-13 14:20:05 -0800195 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
bsalomon744998e2014-08-28 09:54:34 -0700196 }
bsalomon8718aaf2015-02-19 07:24:21 -0800197 if (resource->getUniqueKey().isValid()) {
198 fUniqueHash.remove(resource->getUniqueKey());
bsalomon8b79d232014-11-10 10:19:06 -0800199 }
bsalomonb436ed62014-11-17 12:15:56 -0800200 this->validate();
bsalomonc8dc1f72014-08-21 13:02:13 -0700201}
202
bsalomon0ea80f42015-02-11 10:49:59 -0800203void GrResourceCache::abandonAll() {
bsalomon71cb0c22014-11-14 12:10:14 -0800204 AutoValidate av(this);
205
Brian Salomon876a0172019-03-08 11:12:14 -0500206 // We need to make sure to free any resources that were waiting on a free message but never
207 // received one.
208 fResourcesAwaitingUnref.reset();
Greg Danielb2acf0a2018-09-12 09:17:11 -0400209
bsalomonf320e042015-02-17 15:09:34 -0800210 while (fNonpurgeableResources.count()) {
211 GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
212 SkASSERT(!back->wasDestroyed());
213 back->cacheAccess().abandon();
bsalomonc8dc1f72014-08-21 13:02:13 -0700214 }
bsalomonf320e042015-02-17 15:09:34 -0800215
216 while (fPurgeableQueue.count()) {
217 GrGpuResource* top = fPurgeableQueue.peek();
218 SkASSERT(!top->wasDestroyed());
219 top->cacheAccess().abandon();
220 }
221
bsalomon744998e2014-08-28 09:54:34 -0700222 SkASSERT(!fScratchMap.count());
bsalomon8718aaf2015-02-19 07:24:21 -0800223 SkASSERT(!fUniqueHash.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700224 SkASSERT(!fCount);
bsalomonf320e042015-02-17 15:09:34 -0800225 SkASSERT(!this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800226 SkASSERT(!fBytes);
227 SkASSERT(!fBudgetedCount);
228 SkASSERT(!fBudgetedBytes);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400229 SkASSERT(!fPurgeableBytes);
Brian Salomon876a0172019-03-08 11:12:14 -0500230 SkASSERT(!fResourcesAwaitingUnref.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700231}
232
bsalomon0ea80f42015-02-11 10:49:59 -0800233void GrResourceCache::releaseAll() {
bsalomon71cb0c22014-11-14 12:10:14 -0800234 AutoValidate av(this);
235
Brian Osman13dddce2017-05-09 13:19:50 -0400236 this->processFreedGpuResources();
237
Greg Danielc27eb722018-08-10 09:48:08 -0400238 // We need to make sure to free any resources that were waiting on a free message but never
239 // received one.
Brian Salomon876a0172019-03-08 11:12:14 -0500240 fResourcesAwaitingUnref.reset();
Greg Danielc27eb722018-08-10 09:48:08 -0400241
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500242 SkASSERT(fProxyProvider); // better have called setProxyProvider
Robert Phillips3ec95732017-09-29 15:10:39 -0400243 // We must remove the uniqueKeys from the proxies here. While they possess a uniqueKey
244 // they also have a raw pointer back to this class (which is presumably going away)!
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500245 fProxyProvider->removeAllUniqueKeys();
Robert Phillips45a6f142017-09-29 09:49:41 -0400246
Brian Salomon614c1a82018-12-19 15:42:06 -0500247 while (fNonpurgeableResources.count()) {
bsalomonf320e042015-02-17 15:09:34 -0800248 GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
249 SkASSERT(!back->wasDestroyed());
250 back->cacheAccess().release();
bsalomonc8dc1f72014-08-21 13:02:13 -0700251 }
bsalomonf320e042015-02-17 15:09:34 -0800252
253 while (fPurgeableQueue.count()) {
254 GrGpuResource* top = fPurgeableQueue.peek();
255 SkASSERT(!top->wasDestroyed());
256 top->cacheAccess().release();
257 }
258
bsalomon744998e2014-08-28 09:54:34 -0700259 SkASSERT(!fScratchMap.count());
bsalomon8718aaf2015-02-19 07:24:21 -0800260 SkASSERT(!fUniqueHash.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700261 SkASSERT(!fCount);
bsalomonf320e042015-02-17 15:09:34 -0800262 SkASSERT(!this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800263 SkASSERT(!fBytes);
264 SkASSERT(!fBudgetedCount);
265 SkASSERT(!fBudgetedBytes);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400266 SkASSERT(!fPurgeableBytes);
Brian Salomon876a0172019-03-08 11:12:14 -0500267 SkASSERT(!fResourcesAwaitingUnref.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700268}
bsalomonbcf0a522014-10-08 08:40:09 -0700269
Brian Salomon2c791fc2019-04-02 11:52:03 -0400270void GrResourceCache::refResource(GrGpuResource* resource) {
271 SkASSERT(resource);
272 SkASSERT(resource->getContext()->priv().getResourceCache() == this);
273 if (resource->cacheAccess().hasRef()) {
274 resource->ref();
275 } else {
276 this->refAndMakeResourceMRU(resource);
277 }
278 this->validate();
279}
280
bsalomon0ea80f42015-02-11 10:49:59 -0800281class GrResourceCache::AvailableForScratchUse {
bsalomonbcf0a522014-10-08 08:40:09 -0700282public:
bsalomon000f8292014-10-15 19:04:14 -0700283 AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendingIO) { }
bsalomonbcf0a522014-10-08 08:40:09 -0700284
285 bool operator()(const GrGpuResource* resource) const {
robertphillipsc4ed6842016-05-24 14:17:12 -0700286 SkASSERT(!resource->getUniqueKey().isValid() &&
287 resource->resourcePriv().getScratchKey().isValid());
bsalomon12299ab2014-11-14 13:33:09 -0800288 if (resource->internalHasRef() || !resource->cacheAccess().isScratch()) {
bsalomon000f8292014-10-15 19:04:14 -0700289 return false;
bsalomonbcf0a522014-10-08 08:40:09 -0700290 }
bsalomon000f8292014-10-15 19:04:14 -0700291 return !fRejectPendingIO || !resource->internalHasPendingIO();
bsalomonbcf0a522014-10-08 08:40:09 -0700292 }
bsalomon1e2530b2014-10-09 09:57:18 -0700293
bsalomonbcf0a522014-10-08 08:40:09 -0700294private:
bsalomon000f8292014-10-15 19:04:14 -0700295 bool fRejectPendingIO;
bsalomonbcf0a522014-10-08 08:40:09 -0700296};
297
bsalomon0ea80f42015-02-11 10:49:59 -0800298GrGpuResource* GrResourceCache::findAndRefScratchResource(const GrScratchKey& scratchKey,
robertphillips6e83ac72015-08-13 05:19:14 -0700299 size_t resourceSize,
Chris Daltond004e0b2018-09-27 09:28:03 -0600300 ScratchFlags flags) {
bsalomon7775c852014-12-30 12:50:52 -0800301 SkASSERT(scratchKey.isValid());
robertphillipsee843b22016-10-04 05:30:20 -0700302
bsalomon71cb0c22014-11-14 12:10:14 -0800303 GrGpuResource* resource;
Chris Daltond004e0b2018-09-27 09:28:03 -0600304 if (flags & (ScratchFlags::kPreferNoPendingIO | ScratchFlags::kRequireNoPendingIO)) {
bsalomon71cb0c22014-11-14 12:10:14 -0800305 resource = fScratchMap.find(scratchKey, AvailableForScratchUse(true));
bsalomon000f8292014-10-15 19:04:14 -0700306 if (resource) {
bsalomon9f2d1572015-02-17 11:47:40 -0800307 this->refAndMakeResourceMRU(resource);
bsalomonb436ed62014-11-17 12:15:56 -0800308 this->validate();
309 return resource;
Chris Daltond004e0b2018-09-27 09:28:03 -0600310 } else if (flags & ScratchFlags::kRequireNoPendingIO) {
halcanary96fcdcc2015-08-27 07:41:13 -0700311 return nullptr;
bsalomon000f8292014-10-15 19:04:14 -0700312 }
robertphillips63926682015-08-20 09:39:02 -0700313 // We would prefer to consume more available VRAM rather than flushing
314 // immediately, but on ANGLE this can lead to starving of the GPU.
315 if (fPreferVRAMUseOverFlushes && this->wouldFit(resourceSize)) {
robertphillips6e83ac72015-08-13 05:19:14 -0700316 // kPrefer is specified, we didn't find a resource without pending io,
robertphillips63926682015-08-20 09:39:02 -0700317 // but there is still space in our budget for the resource so force
318 // the caller to allocate a new resource.
halcanary96fcdcc2015-08-27 07:41:13 -0700319 return nullptr;
robertphillips6e83ac72015-08-13 05:19:14 -0700320 }
bsalomon000f8292014-10-15 19:04:14 -0700321 }
bsalomon71cb0c22014-11-14 12:10:14 -0800322 resource = fScratchMap.find(scratchKey, AvailableForScratchUse(false));
323 if (resource) {
bsalomon9f2d1572015-02-17 11:47:40 -0800324 this->refAndMakeResourceMRU(resource);
bsalomonb436ed62014-11-17 12:15:56 -0800325 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800326 }
327 return resource;
bsalomonbcf0a522014-10-08 08:40:09 -0700328}
bsalomon8b79d232014-11-10 10:19:06 -0800329
bsalomon0ea80f42015-02-11 10:49:59 -0800330void GrResourceCache::willRemoveScratchKey(const GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400331 ASSERT_SINGLE_OWNER
bsalomon3582d3e2015-02-13 14:20:05 -0800332 SkASSERT(resource->resourcePriv().getScratchKey().isValid());
robertphillipsc4ed6842016-05-24 14:17:12 -0700333 if (!resource->getUniqueKey().isValid()) {
334 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
335 }
bsalomon10e23ca2014-11-25 05:52:06 -0800336}
337
bsalomonf99e9612015-02-19 08:24:16 -0800338void GrResourceCache::removeUniqueKey(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400339 ASSERT_SINGLE_OWNER
bsalomon3f324322015-04-08 11:01:54 -0700340 // Someone has a ref to this resource in order to have removed the key. When the ref count
341 // reaches zero we will get a ref cnt notification and figure out what to do with it.
bsalomonf99e9612015-02-19 08:24:16 -0800342 if (resource->getUniqueKey().isValid()) {
343 SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
344 fUniqueHash.remove(resource->getUniqueKey());
345 }
346 resource->cacheAccess().removeUniqueKey();
robertphillipsc4ed6842016-05-24 14:17:12 -0700347 if (resource->resourcePriv().getScratchKey().isValid()) {
348 fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
349 }
350
Brian Salomon9bc76d92019-01-24 12:18:33 -0500351 // Removing a unique key from a kUnbudgetedCacheable resource would make the resource
352 // require purging. However, the resource must be ref'ed to get here and therefore can't
353 // be purgeable. We'll purge it when the refs reach zero.
354 SkASSERT(!resource->resourcePriv().isPurgeable());
bsalomonf99e9612015-02-19 08:24:16 -0800355 this->validate();
bsalomon23e619c2015-02-06 11:54:28 -0800356}
357
bsalomonf99e9612015-02-19 08:24:16 -0800358void GrResourceCache::changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400359 ASSERT_SINGLE_OWNER
bsalomon8b79d232014-11-10 10:19:06 -0800360 SkASSERT(resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800361 SkASSERT(this->isInCache(resource));
bsalomon8b79d232014-11-10 10:19:06 -0800362
bsalomonf99e9612015-02-19 08:24:16 -0800363 // If another resource has the new key, remove its key then install the key on this resource.
364 if (newKey.isValid()) {
Greg Daniel0d537802017-09-08 11:44:14 -0400365 if (GrGpuResource* old = fUniqueHash.find(newKey)) {
366 // If the old resource using the key is purgeable and is unreachable, then remove it.
Brian Salomon614c1a82018-12-19 15:42:06 -0500367 if (!old->resourcePriv().getScratchKey().isValid() &&
368 old->resourcePriv().isPurgeable()) {
Greg Daniel0d537802017-09-08 11:44:14 -0400369 old->cacheAccess().release();
370 } else {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500371 // removeUniqueKey expects an external owner of the resource.
372 this->removeUniqueKey(sk_ref_sp(old).get());
Greg Daniel0d537802017-09-08 11:44:14 -0400373 }
374 }
375 SkASSERT(nullptr == fUniqueHash.find(newKey));
376
robertphillipsc4ed6842016-05-24 14:17:12 -0700377 // Remove the entry for this resource if it already has a unique key.
378 if (resource->getUniqueKey().isValid()) {
379 SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
380 fUniqueHash.remove(resource->getUniqueKey());
381 SkASSERT(nullptr == fUniqueHash.find(resource->getUniqueKey()));
382 } else {
383 // 'resource' didn't have a valid unique key before so it is switching sides. Remove it
384 // from the ScratchMap
385 if (resource->resourcePriv().getScratchKey().isValid()) {
386 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
387 }
388 }
389
bsalomonf99e9612015-02-19 08:24:16 -0800390 resource->cacheAccess().setUniqueKey(newKey);
391 fUniqueHash.add(resource);
392 } else {
robertphillipsc4ed6842016-05-24 14:17:12 -0700393 this->removeUniqueKey(resource);
bsalomonf99e9612015-02-19 08:24:16 -0800394 }
395
bsalomon71cb0c22014-11-14 12:10:14 -0800396 this->validate();
bsalomon8b79d232014-11-10 10:19:06 -0800397}
bsalomon71cb0c22014-11-14 12:10:14 -0800398
bsalomon9f2d1572015-02-17 11:47:40 -0800399void GrResourceCache::refAndMakeResourceMRU(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400400 ASSERT_SINGLE_OWNER
bsalomon71cb0c22014-11-14 12:10:14 -0800401 SkASSERT(resource);
402 SkASSERT(this->isInCache(resource));
bsalomonddf30e62015-02-19 11:38:44 -0800403
Brian Salomon614c1a82018-12-19 15:42:06 -0500404 if (resource->resourcePriv().isPurgeable()) {
bsalomon9f2d1572015-02-17 11:47:40 -0800405 // It's about to become unpurgeable.
Derek Sollenbergeree479142017-05-24 11:41:33 -0400406 fPurgeableBytes -= resource->gpuMemorySize();
bsalomon9f2d1572015-02-17 11:47:40 -0800407 fPurgeableQueue.remove(resource);
bsalomonf320e042015-02-17 15:09:34 -0800408 this->addToNonpurgeableArray(resource);
Brian Salomon2c791fc2019-04-02 11:52:03 -0400409 } else if (!resource->cacheAccess().hasRef() &&
410 resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
411 SkASSERT(fNumBudgetedResourcesFlushWillMakePurgeable > 0);
412 fNumBudgetedResourcesFlushWillMakePurgeable--;
bsalomon9f2d1572015-02-17 11:47:40 -0800413 }
Brian Salomon01ceae92019-04-02 11:49:54 -0400414 resource->cacheAccess().ref();
bsalomonddf30e62015-02-19 11:38:44 -0800415
416 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
bsalomonf320e042015-02-17 15:09:34 -0800417 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800418}
419
bsalomon3f324322015-04-08 11:01:54 -0700420void GrResourceCache::notifyCntReachedZero(GrGpuResource* resource, uint32_t flags) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400421 ASSERT_SINGLE_OWNER
bsalomon71cb0c22014-11-14 12:10:14 -0800422 SkASSERT(resource);
bsalomon3f324322015-04-08 11:01:54 -0700423 SkASSERT(!resource->wasDestroyed());
424 SkASSERT(flags);
bsalomon71cb0c22014-11-14 12:10:14 -0800425 SkASSERT(this->isInCache(resource));
bsalomon3f324322015-04-08 11:01:54 -0700426 // This resource should always be in the nonpurgeable array when this function is called. It
427 // will be moved to the queue if it is newly purgeable.
428 SkASSERT(fNonpurgeableResources[*resource->cacheAccess().accessCacheIndex()] == resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800429
bsalomon3f324322015-04-08 11:01:54 -0700430 if (SkToBool(ResourceAccess::kRefCntReachedZero_RefNotificationFlag & flags)) {
431#ifdef SK_DEBUG
432 // When the timestamp overflows validate() is called. validate() checks that resources in
433 // the nonpurgeable array are indeed not purgeable. However, the movement from the array to
434 // the purgeable queue happens just below in this function. So we mark it as an exception.
Brian Salomon614c1a82018-12-19 15:42:06 -0500435 if (resource->resourcePriv().isPurgeable()) {
bsalomon3f324322015-04-08 11:01:54 -0700436 fNewlyPurgeableResourceForValidation = resource;
437 }
438#endif
439 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
halcanary96fcdcc2015-08-27 07:41:13 -0700440 SkDEBUGCODE(fNewlyPurgeableResourceForValidation = nullptr);
Brian Salomon2c791fc2019-04-02 11:52:03 -0400441 if (!resource->resourcePriv().isPurgeable() &&
442 resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
443 SkASSERT(resource->resourcePriv().hasPendingIO_debugOnly());
444 ++fNumBudgetedResourcesFlushWillMakePurgeable;
445 }
446 } else {
447 // If this is budgeted and just became purgeable by dropping the last pending IO
448 // then it clearly no longer needs a flush to become purgeable.
449 if (resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted &&
450 resource->resourcePriv().isPurgeable()) {
451 SkASSERT(fNumBudgetedResourcesFlushWillMakePurgeable > 0);
452 fNumBudgetedResourcesFlushWillMakePurgeable--;
453 }
bsalomon3f324322015-04-08 11:01:54 -0700454 }
455
456 if (!SkToBool(ResourceAccess::kAllCntsReachedZero_RefNotificationFlag & flags)) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500457 SkASSERT(!resource->resourcePriv().isPurgeable());
bsalomon3f324322015-04-08 11:01:54 -0700458 return;
459 }
460
Brian Salomon9bc76d92019-01-24 12:18:33 -0500461 if (!resource->resourcePriv().isPurgeable()) {
462 this->validate();
463 return;
464 }
465
bsalomonf320e042015-02-17 15:09:34 -0800466 this->removeFromNonpurgeableArray(resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800467 fPurgeableQueue.insert(resource);
Brian Salomon5e150852017-03-22 14:53:13 -0400468 resource->cacheAccess().setTimeWhenResourceBecomePurgeable();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400469 fPurgeableBytes += resource->gpuMemorySize();
bsalomon71cb0c22014-11-14 12:10:14 -0800470
Greg Daniel303e83e2018-09-10 14:10:19 -0400471 bool hasUniqueKey = resource->getUniqueKey().isValid();
472
Brian Salomon9bc76d92019-01-24 12:18:33 -0500473 GrBudgetedType budgetedType = resource->resourcePriv().budgetedType();
Brian Salomon614c1a82018-12-19 15:42:06 -0500474
Brian Salomon9bc76d92019-01-24 12:18:33 -0500475 if (budgetedType == GrBudgetedType::kBudgeted) {
476 // Purge the resource immediately if we're over budget
477 // Also purge if the resource has neither a valid scratch key nor a unique key.
478 bool hasKey = resource->resourcePriv().getScratchKey().isValid() || hasUniqueKey;
479 if (!this->overBudget() && hasKey) {
480 return;
481 }
482 } else {
483 // We keep unbudgeted resources with a unique key in the purgeable queue of the cache so
484 // they can be reused again by the image connected to the unique key.
485 if (hasUniqueKey && budgetedType == GrBudgetedType::kUnbudgetedCacheable) {
486 return;
487 }
488 // Check whether this resource could still be used as a scratch resource.
489 if (!resource->resourcePriv().refsWrappedObjects() &&
490 resource->resourcePriv().getScratchKey().isValid()) {
491 // We won't purge an existing resource to make room for this one.
492 if (fBudgetedCount < fMaxCount &&
493 fBudgetedBytes + resource->gpuMemorySize() <= fMaxBytes) {
494 resource->resourcePriv().makeBudgeted();
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500495 return;
496 }
bsalomonc2f35b72015-01-23 07:19:22 -0800497 }
bsalomonc2f35b72015-01-23 07:19:22 -0800498 }
Brian Salomon9bc76d92019-01-24 12:18:33 -0500499
bsalomonf320e042015-02-17 15:09:34 -0800500 SkDEBUGCODE(int beforeCount = this->getResourceCount();)
bsalomon9f2d1572015-02-17 11:47:40 -0800501 resource->cacheAccess().release();
502 // We should at least free this resource, perhaps dependent resources as well.
bsalomonf320e042015-02-17 15:09:34 -0800503 SkASSERT(this->getResourceCount() < beforeCount);
bsalomon71cb0c22014-11-14 12:10:14 -0800504 this->validate();
505}
506
bsalomon0ea80f42015-02-11 10:49:59 -0800507void GrResourceCache::didChangeBudgetStatus(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400508 ASSERT_SINGLE_OWNER
bsalomon84c8e622014-11-17 09:33:27 -0800509 SkASSERT(resource);
510 SkASSERT(this->isInCache(resource));
511
512 size_t size = resource->gpuMemorySize();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500513 // Changing from BudgetedType::kUnbudgetedCacheable to another budgeted type could make
514 // resource become purgeable. However, we should never allow that transition. Wrapped
515 // resources are the only resources that can be in that state and they aren't allowed to
516 // transition from one budgeted state to another.
517 SkDEBUGCODE(bool wasPurgeable = resource->resourcePriv().isPurgeable());
518 if (resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
bsalomon84c8e622014-11-17 09:33:27 -0800519 ++fBudgetedCount;
520 fBudgetedBytes += size;
bsalomonafe30052015-01-16 07:32:33 -0800521#if GR_CACHE_STATS
522 fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes);
523 fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount);
524#endif
Brian Salomon2c791fc2019-04-02 11:52:03 -0400525 if (!resource->resourcePriv().isPurgeable() && !resource->cacheAccess().hasRef()) {
526 ++fNumBudgetedResourcesFlushWillMakePurgeable;
527 }
bsalomon84c8e622014-11-17 09:33:27 -0800528 this->purgeAsNeeded();
529 } else {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500530 SkASSERT(resource->resourcePriv().budgetedType() != GrBudgetedType::kUnbudgetedCacheable);
bsalomon84c8e622014-11-17 09:33:27 -0800531 --fBudgetedCount;
532 fBudgetedBytes -= size;
Brian Salomon2c791fc2019-04-02 11:52:03 -0400533 if (!resource->resourcePriv().isPurgeable() && !resource->cacheAccess().hasRef()) {
534 --fNumBudgetedResourcesFlushWillMakePurgeable;
535 }
bsalomon84c8e622014-11-17 09:33:27 -0800536 }
Brian Salomon9bc76d92019-01-24 12:18:33 -0500537 SkASSERT(wasPurgeable == resource->resourcePriv().isPurgeable());
Brian Osman39c08ac2017-07-26 09:36:09 -0400538 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800539 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomon84c8e622014-11-17 09:33:27 -0800540
541 this->validate();
542}
543
robertphillipsee843b22016-10-04 05:30:20 -0700544void GrResourceCache::purgeAsNeeded() {
bsalomon3f324322015-04-08 11:01:54 -0700545 SkTArray<GrUniqueKeyInvalidatedMessage> invalidKeyMsgs;
546 fInvalidUniqueKeyInbox.poll(&invalidKeyMsgs);
547 if (invalidKeyMsgs.count()) {
Robert Phillips427966a2018-12-20 17:20:43 -0500548 SkASSERT(fProxyProvider);
549
550 for (int i = 0; i < invalidKeyMsgs.count(); ++i) {
551 fProxyProvider->processInvalidUniqueKey(invalidKeyMsgs[i].key(), nullptr,
552 GrProxyProvider::InvalidateGPUResource::kYes);
553 SkASSERT(!this->findAndRefUniqueResource(invalidKeyMsgs[i].key()));
554 }
bsalomon3f324322015-04-08 11:01:54 -0700555 }
bsalomon71cb0c22014-11-14 12:10:14 -0800556
Brian Osman13dddce2017-05-09 13:19:50 -0400557 this->processFreedGpuResources();
558
bsalomon3f324322015-04-08 11:01:54 -0700559 bool stillOverbudget = this->overBudget();
560 while (stillOverbudget && fPurgeableQueue.count()) {
robertphillipsee843b22016-10-04 05:30:20 -0700561 GrGpuResource* resource = fPurgeableQueue.peek();
Brian Salomon614c1a82018-12-19 15:42:06 -0500562 SkASSERT(resource->resourcePriv().isPurgeable());
bsalomon9f2d1572015-02-17 11:47:40 -0800563 resource->cacheAccess().release();
bsalomon3f324322015-04-08 11:01:54 -0700564 stillOverbudget = this->overBudget();
bsalomon9f2d1572015-02-17 11:47:40 -0800565 }
bsalomon71cb0c22014-11-14 12:10:14 -0800566
bsalomonb436ed62014-11-17 12:15:56 -0800567 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800568}
569
Robert Phillips6eba0632018-03-28 12:25:42 -0400570void GrResourceCache::purgeUnlockedResources(bool scratchResourcesOnly) {
571 if (!scratchResourcesOnly) {
572 // We could disable maintaining the heap property here, but it would add a lot of
573 // complexity. Moreover, this is rarely called.
574 while (fPurgeableQueue.count()) {
575 GrGpuResource* resource = fPurgeableQueue.peek();
Brian Salomon614c1a82018-12-19 15:42:06 -0500576 SkASSERT(resource->resourcePriv().isPurgeable());
Robert Phillips6eba0632018-03-28 12:25:42 -0400577 resource->cacheAccess().release();
578 }
579 } else {
580 // Sort the queue
581 fPurgeableQueue.sort();
582
583 // Make a list of the scratch resources to delete
584 SkTDArray<GrGpuResource*> scratchResources;
585 for (int i = 0; i < fPurgeableQueue.count(); i++) {
586 GrGpuResource* resource = fPurgeableQueue.at(i);
Brian Salomon614c1a82018-12-19 15:42:06 -0500587 SkASSERT(resource->resourcePriv().isPurgeable());
Robert Phillips6eba0632018-03-28 12:25:42 -0400588 if (!resource->getUniqueKey().isValid()) {
589 *scratchResources.append() = resource;
590 }
591 }
592
593 // Delete the scratch resources. This must be done as a separate pass
594 // to avoid messing up the sorted order of the queue
595 for (int i = 0; i < scratchResources.count(); i++) {
596 scratchResources.getAt(i)->cacheAccess().release();
597 }
bsalomon9f2d1572015-02-17 11:47:40 -0800598 }
bsalomon71cb0c22014-11-14 12:10:14 -0800599
bsalomonb436ed62014-11-17 12:15:56 -0800600 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800601}
602
Brian Salomon5e150852017-03-22 14:53:13 -0400603void GrResourceCache::purgeResourcesNotUsedSince(GrStdSteadyClock::time_point purgeTime) {
604 while (fPurgeableQueue.count()) {
605 const GrStdSteadyClock::time_point resourceTime =
606 fPurgeableQueue.peek()->cacheAccess().timeWhenResourceBecamePurgeable();
607 if (resourceTime >= purgeTime) {
608 // Resources were given both LRU timestamps and tagged with a frame number when
609 // they first became purgeable. The LRU timestamp won't change again until the
610 // resource is made non-purgeable again. So, at this point all the remaining
611 // resources in the timestamp-sorted queue will have a frame number >= to this
612 // one.
613 break;
614 }
615 GrGpuResource* resource = fPurgeableQueue.peek();
Brian Salomon614c1a82018-12-19 15:42:06 -0500616 SkASSERT(resource->resourcePriv().isPurgeable());
Brian Salomon5e150852017-03-22 14:53:13 -0400617 resource->cacheAccess().release();
618 }
619}
620
Derek Sollenberger5480a182017-05-25 16:43:59 -0400621void GrResourceCache::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
622
623 const size_t tmpByteBudget = SkTMax((size_t)0, fBytes - bytesToPurge);
624 bool stillOverbudget = tmpByteBudget < fBytes;
625
626 if (preferScratchResources && bytesToPurge < fPurgeableBytes) {
627 // Sort the queue
628 fPurgeableQueue.sort();
629
630 // Make a list of the scratch resources to delete
631 SkTDArray<GrGpuResource*> scratchResources;
632 size_t scratchByteCount = 0;
633 for (int i = 0; i < fPurgeableQueue.count() && stillOverbudget; i++) {
634 GrGpuResource* resource = fPurgeableQueue.at(i);
Brian Salomon614c1a82018-12-19 15:42:06 -0500635 SkASSERT(resource->resourcePriv().isPurgeable());
Derek Sollenberger5480a182017-05-25 16:43:59 -0400636 if (!resource->getUniqueKey().isValid()) {
637 *scratchResources.append() = resource;
638 scratchByteCount += resource->gpuMemorySize();
639 stillOverbudget = tmpByteBudget < fBytes - scratchByteCount;
640 }
641 }
642
643 // Delete the scratch resources. This must be done as a separate pass
644 // to avoid messing up the sorted order of the queue
645 for (int i = 0; i < scratchResources.count(); i++) {
646 scratchResources.getAt(i)->cacheAccess().release();
647 }
648 stillOverbudget = tmpByteBudget < fBytes;
649
650 this->validate();
651 }
652
653 // Purge any remaining resources in LRU order
654 if (stillOverbudget) {
655 const size_t cachedByteCount = fMaxBytes;
656 fMaxBytes = tmpByteBudget;
657 this->purgeAsNeeded();
658 fMaxBytes = cachedByteCount;
659 }
660}
Brian Salomon8cefa3e2019-04-04 11:39:55 -0400661bool GrResourceCache::requestsFlush() const {
662 return this->overBudget() && !fPurgeableQueue.count() &&
663 fNumBudgetedResourcesFlushWillMakePurgeable > 0;
664}
665
Derek Sollenberger5480a182017-05-25 16:43:59 -0400666
Brian Salomon876a0172019-03-08 11:12:14 -0500667void GrResourceCache::insertDelayedResourceUnref(GrGpuResource* resource) {
Brian Osman13dddce2017-05-09 13:19:50 -0400668 resource->ref();
Brian Salomon876a0172019-03-08 11:12:14 -0500669 uint32_t id = resource->uniqueID().asUInt();
670 if (auto* data = fResourcesAwaitingUnref.find(id)) {
671 data->addRef();
672 } else {
673 fResourcesAwaitingUnref.set(id, {resource});
674 }
Brian Osman13dddce2017-05-09 13:19:50 -0400675}
676
677void GrResourceCache::processFreedGpuResources() {
678 SkTArray<GrGpuResourceFreedMessage> msgs;
679 fFreedGpuResourceInbox.poll(&msgs);
680 for (int i = 0; i < msgs.count(); ++i) {
Brian Salomon238069b2018-07-11 15:58:57 -0400681 SkASSERT(msgs[i].fOwningUniqueID == fContextUniqueID);
Brian Salomon876a0172019-03-08 11:12:14 -0500682 uint32_t id = msgs[i].fResource->uniqueID().asUInt();
683 ResourceAwaitingUnref* info = fResourcesAwaitingUnref.find(id);
Greg Danielb2acf0a2018-09-12 09:17:11 -0400684 // If we called release or abandon on the GrContext we will have already released our ref on
685 // the GrGpuResource. If then the message arrives before the actual GrContext gets destroyed
686 // we will try to process the message when we destroy the GrContext. This protects us from
687 // trying to unref the resource twice.
Brian Salomon876a0172019-03-08 11:12:14 -0500688 if (info) {
689 info->unref();
690 if (info->finished()) {
691 fResourcesAwaitingUnref.remove(id);
692 }
Greg Danielb2acf0a2018-09-12 09:17:11 -0400693 }
Brian Osman13dddce2017-05-09 13:19:50 -0400694 }
695}
696
bsalomonf320e042015-02-17 15:09:34 -0800697void GrResourceCache::addToNonpurgeableArray(GrGpuResource* resource) {
698 int index = fNonpurgeableResources.count();
699 *fNonpurgeableResources.append() = resource;
700 *resource->cacheAccess().accessCacheIndex() = index;
701}
702
703void GrResourceCache::removeFromNonpurgeableArray(GrGpuResource* resource) {
704 int* index = resource->cacheAccess().accessCacheIndex();
705 // Fill the whole we will create in the array with the tail object, adjust its index, and
706 // then pop the array
707 GrGpuResource* tail = *(fNonpurgeableResources.end() - 1);
708 SkASSERT(fNonpurgeableResources[*index] == resource);
709 fNonpurgeableResources[*index] = tail;
710 *tail->cacheAccess().accessCacheIndex() = *index;
711 fNonpurgeableResources.pop();
712 SkDEBUGCODE(*index = -1);
713}
714
bsalomonddf30e62015-02-19 11:38:44 -0800715uint32_t GrResourceCache::getNextTimestamp() {
716 // If we wrap then all the existing resources will appear older than any resources that get
717 // a timestamp after the wrap.
718 if (0 == fTimestamp) {
719 int count = this->getResourceCount();
720 if (count) {
721 // Reset all the timestamps. We sort the resources by timestamp and then assign
722 // sequential timestamps beginning with 0. This is O(n*lg(n)) but it should be extremely
723 // rare.
724 SkTDArray<GrGpuResource*> sortedPurgeableResources;
725 sortedPurgeableResources.setReserve(fPurgeableQueue.count());
726
727 while (fPurgeableQueue.count()) {
728 *sortedPurgeableResources.append() = fPurgeableQueue.peek();
729 fPurgeableQueue.pop();
730 }
robertphillipsee843b22016-10-04 05:30:20 -0700731
bsalomone2e87f32016-09-22 12:42:11 -0700732 SkTQSort(fNonpurgeableResources.begin(), fNonpurgeableResources.end() - 1,
733 CompareTimestamp);
bsalomonddf30e62015-02-19 11:38:44 -0800734
735 // Pick resources out of the purgeable and non-purgeable arrays based on lowest
736 // timestamp and assign new timestamps.
737 int currP = 0;
738 int currNP = 0;
739 while (currP < sortedPurgeableResources.count() &&
mtklein56da0252015-11-16 11:16:23 -0800740 currNP < fNonpurgeableResources.count()) {
bsalomonddf30e62015-02-19 11:38:44 -0800741 uint32_t tsP = sortedPurgeableResources[currP]->cacheAccess().timestamp();
742 uint32_t tsNP = fNonpurgeableResources[currNP]->cacheAccess().timestamp();
743 SkASSERT(tsP != tsNP);
744 if (tsP < tsNP) {
745 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
746 } else {
747 // Correct the index in the nonpurgeable array stored on the resource post-sort.
748 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
749 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
750 }
751 }
752
753 // The above loop ended when we hit the end of one array. Finish the other one.
754 while (currP < sortedPurgeableResources.count()) {
755 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
756 }
757 while (currNP < fNonpurgeableResources.count()) {
758 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
759 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
760 }
761
762 // Rebuild the queue.
763 for (int i = 0; i < sortedPurgeableResources.count(); ++i) {
764 fPurgeableQueue.insert(sortedPurgeableResources[i]);
765 }
766
767 this->validate();
768 SkASSERT(count == this->getResourceCount());
769
770 // count should be the next timestamp we return.
771 SkASSERT(fTimestamp == SkToU32(count));
mtklein56da0252015-11-16 11:16:23 -0800772 }
bsalomonddf30e62015-02-19 11:38:44 -0800773 }
774 return fTimestamp++;
775}
776
ericrk0a5fa482015-09-15 14:16:10 -0700777void GrResourceCache::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
778 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
779 fNonpurgeableResources[i]->dumpMemoryStatistics(traceMemoryDump);
780 }
781 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
782 fPurgeableQueue.at(i)->dumpMemoryStatistics(traceMemoryDump);
783 }
784}
785
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500786#if GR_CACHE_STATS
787void GrResourceCache::getStats(Stats* stats) const {
788 stats->reset();
789
790 stats->fTotal = this->getResourceCount();
791 stats->fNumNonPurgeable = fNonpurgeableResources.count();
792 stats->fNumPurgeable = fPurgeableQueue.count();
793
794 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
795 stats->update(fNonpurgeableResources[i]);
796 }
797 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
798 stats->update(fPurgeableQueue.at(i));
799 }
800}
801
802#if GR_TEST_UTILS
803void GrResourceCache::dumpStats(SkString* out) const {
804 this->validate();
805
806 Stats stats;
807
808 this->getStats(&stats);
809
810 float countUtilization = (100.f * fBudgetedCount) / fMaxCount;
811 float byteUtilization = (100.f * fBudgetedBytes) / fMaxBytes;
812
813 out->appendf("Budget: %d items %d bytes\n", fMaxCount, (int)fMaxBytes);
814 out->appendf("\t\tEntry Count: current %d"
815 " (%d budgeted, %d wrapped, %d locked, %d scratch %.2g%% full), high %d\n",
816 stats.fTotal, fBudgetedCount, stats.fWrapped, stats.fNumNonPurgeable,
817 stats.fScratch, countUtilization, fHighWaterCount);
818 out->appendf("\t\tEntry Bytes: current %d (budgeted %d, %.2g%% full, %d unbudgeted) high %d\n",
819 SkToInt(fBytes), SkToInt(fBudgetedBytes), byteUtilization,
820 SkToInt(stats.fUnbudgetedSize), SkToInt(fHighWaterBytes));
821}
822
823void GrResourceCache::dumpStatsKeyValuePairs(SkTArray<SkString>* keys,
824 SkTArray<double>* values) const {
825 this->validate();
826
827 Stats stats;
828 this->getStats(&stats);
829
830 keys->push_back(SkString("gpu_cache_purgable_entries")); values->push_back(stats.fNumPurgeable);
831}
832#endif
833
834#endif
835
bsalomon71cb0c22014-11-14 12:10:14 -0800836#ifdef SK_DEBUG
bsalomon0ea80f42015-02-11 10:49:59 -0800837void GrResourceCache::validate() const {
bsalomonc2f35b72015-01-23 07:19:22 -0800838 // Reduce the frequency of validations for large resource counts.
839 static SkRandom gRandom;
840 int mask = (SkNextPow2(fCount + 1) >> 5) - 1;
841 if (~mask && (gRandom.nextU() & mask)) {
842 return;
843 }
844
bsalomonf320e042015-02-17 15:09:34 -0800845 struct Stats {
846 size_t fBytes;
847 int fBudgetedCount;
848 size_t fBudgetedBytes;
849 int fLocked;
850 int fScratch;
851 int fCouldBeScratch;
852 int fContent;
853 const ScratchMap* fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800854 const UniqueHash* fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800855
bsalomonf320e042015-02-17 15:09:34 -0800856 Stats(const GrResourceCache* cache) {
857 memset(this, 0, sizeof(*this));
858 fScratchMap = &cache->fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800859 fUniqueHash = &cache->fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800860 }
861
bsalomonf320e042015-02-17 15:09:34 -0800862 void update(GrGpuResource* resource) {
863 fBytes += resource->gpuMemorySize();
bsalomondace19e2014-11-17 07:34:06 -0800864
Brian Salomon614c1a82018-12-19 15:42:06 -0500865 if (!resource->resourcePriv().isPurgeable()) {
bsalomonf320e042015-02-17 15:09:34 -0800866 ++fLocked;
867 }
bsalomon9f2d1572015-02-17 11:47:40 -0800868
robertphillipsc4ed6842016-05-24 14:17:12 -0700869 const GrScratchKey& scratchKey = resource->resourcePriv().getScratchKey();
870 const GrUniqueKey& uniqueKey = resource->getUniqueKey();
871
bsalomonf320e042015-02-17 15:09:34 -0800872 if (resource->cacheAccess().isScratch()) {
robertphillipsc4ed6842016-05-24 14:17:12 -0700873 SkASSERT(!uniqueKey.isValid());
bsalomonf320e042015-02-17 15:09:34 -0800874 ++fScratch;
robertphillipsc4ed6842016-05-24 14:17:12 -0700875 SkASSERT(fScratchMap->countForKey(scratchKey));
kkinnunen2e6055b2016-04-22 01:48:29 -0700876 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
robertphillipsc4ed6842016-05-24 14:17:12 -0700877 } else if (scratchKey.isValid()) {
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500878 SkASSERT(GrBudgetedType::kBudgeted != resource->resourcePriv().budgetedType() ||
robertphillipsc4ed6842016-05-24 14:17:12 -0700879 uniqueKey.isValid());
880 if (!uniqueKey.isValid()) {
mtklein4e976072016-08-08 09:06:27 -0700881 ++fCouldBeScratch;
robertphillipsc4ed6842016-05-24 14:17:12 -0700882 SkASSERT(fScratchMap->countForKey(scratchKey));
883 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700884 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
bsalomonf320e042015-02-17 15:09:34 -0800885 }
bsalomon8718aaf2015-02-19 07:24:21 -0800886 if (uniqueKey.isValid()) {
bsalomonf320e042015-02-17 15:09:34 -0800887 ++fContent;
bsalomon8718aaf2015-02-19 07:24:21 -0800888 SkASSERT(fUniqueHash->find(uniqueKey) == resource);
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500889 SkASSERT(GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType() ||
Brian Osman0562eb92017-05-08 11:16:39 -0400890 resource->resourcePriv().refsWrappedObjects());
robertphillipsc4ed6842016-05-24 14:17:12 -0700891
892 if (scratchKey.isValid()) {
893 SkASSERT(!fScratchMap->has(resource, scratchKey));
894 }
bsalomonf320e042015-02-17 15:09:34 -0800895 }
896
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500897 if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
bsalomonf320e042015-02-17 15:09:34 -0800898 ++fBudgetedCount;
899 fBudgetedBytes += resource->gpuMemorySize();
900 }
bsalomon9f2d1572015-02-17 11:47:40 -0800901 }
bsalomonf320e042015-02-17 15:09:34 -0800902 };
903
robertphillipsc4ed6842016-05-24 14:17:12 -0700904 {
905 ScratchMap::ConstIter iter(&fScratchMap);
906
907 int count = 0;
908 for ( ; !iter.done(); ++iter) {
909 const GrGpuResource* resource = *iter;
910 SkASSERT(resource->resourcePriv().getScratchKey().isValid());
911 SkASSERT(!resource->getUniqueKey().isValid());
912 count++;
913 }
914 SkASSERT(count == fScratchMap.count()); // ensure the iterator is working correctly
915 }
916
bsalomonf320e042015-02-17 15:09:34 -0800917 Stats stats(this);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400918 size_t purgeableBytes = 0;
Brian Salomon2c791fc2019-04-02 11:52:03 -0400919 int numBudgetedResourcesFlushWillMakePurgeable = 0;
bsalomonf320e042015-02-17 15:09:34 -0800920
921 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500922 SkASSERT(!fNonpurgeableResources[i]->resourcePriv().isPurgeable() ||
bsalomon3f324322015-04-08 11:01:54 -0700923 fNewlyPurgeableResourceForValidation == fNonpurgeableResources[i]);
bsalomonf320e042015-02-17 15:09:34 -0800924 SkASSERT(*fNonpurgeableResources[i]->cacheAccess().accessCacheIndex() == i);
925 SkASSERT(!fNonpurgeableResources[i]->wasDestroyed());
Brian Salomon2c791fc2019-04-02 11:52:03 -0400926 if (fNonpurgeableResources[i]->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted &&
927 !fNonpurgeableResources[i]->cacheAccess().hasRef() &&
928 fNewlyPurgeableResourceForValidation != fNonpurgeableResources[i]) {
929 SkASSERT(fNonpurgeableResources[i]->resourcePriv().hasPendingIO_debugOnly());
930 ++numBudgetedResourcesFlushWillMakePurgeable;
931 }
bsalomonf320e042015-02-17 15:09:34 -0800932 stats.update(fNonpurgeableResources[i]);
bsalomon71cb0c22014-11-14 12:10:14 -0800933 }
bsalomon9f2d1572015-02-17 11:47:40 -0800934 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500935 SkASSERT(fPurgeableQueue.at(i)->resourcePriv().isPurgeable());
bsalomonf320e042015-02-17 15:09:34 -0800936 SkASSERT(*fPurgeableQueue.at(i)->cacheAccess().accessCacheIndex() == i);
937 SkASSERT(!fPurgeableQueue.at(i)->wasDestroyed());
938 stats.update(fPurgeableQueue.at(i));
Derek Sollenbergeree479142017-05-24 11:41:33 -0400939 purgeableBytes += fPurgeableQueue.at(i)->gpuMemorySize();
bsalomon9f2d1572015-02-17 11:47:40 -0800940 }
941
bsalomonf320e042015-02-17 15:09:34 -0800942 SkASSERT(fCount == this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800943 SkASSERT(fBudgetedCount <= fCount);
bsalomonf320e042015-02-17 15:09:34 -0800944 SkASSERT(fBudgetedBytes <= fBytes);
945 SkASSERT(stats.fBytes == fBytes);
Brian Salomon2c791fc2019-04-02 11:52:03 -0400946 SkASSERT(fNumBudgetedResourcesFlushWillMakePurgeable ==
947 numBudgetedResourcesFlushWillMakePurgeable);
bsalomonf320e042015-02-17 15:09:34 -0800948 SkASSERT(stats.fBudgetedBytes == fBudgetedBytes);
949 SkASSERT(stats.fBudgetedCount == fBudgetedCount);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400950 SkASSERT(purgeableBytes == fPurgeableBytes);
bsalomon71cb0c22014-11-14 12:10:14 -0800951#if GR_CACHE_STATS
bsalomondace19e2014-11-17 07:34:06 -0800952 SkASSERT(fBudgetedHighWaterCount <= fHighWaterCount);
953 SkASSERT(fBudgetedHighWaterBytes <= fHighWaterBytes);
bsalomonf320e042015-02-17 15:09:34 -0800954 SkASSERT(fBytes <= fHighWaterBytes);
955 SkASSERT(fCount <= fHighWaterCount);
956 SkASSERT(fBudgetedBytes <= fBudgetedHighWaterBytes);
957 SkASSERT(fBudgetedCount <= fBudgetedHighWaterCount);
bsalomon71cb0c22014-11-14 12:10:14 -0800958#endif
bsalomon8718aaf2015-02-19 07:24:21 -0800959 SkASSERT(stats.fContent == fUniqueHash.count());
bsalomonf320e042015-02-17 15:09:34 -0800960 SkASSERT(stats.fScratch + stats.fCouldBeScratch == fScratchMap.count());
bsalomon71cb0c22014-11-14 12:10:14 -0800961
bsalomon3f324322015-04-08 11:01:54 -0700962 // This assertion is not currently valid because we can be in recursive notifyCntReachedZero()
bsalomon12299ab2014-11-14 13:33:09 -0800963 // calls. This will be fixed when subresource registration is explicit.
bsalomondace19e2014-11-17 07:34:06 -0800964 // bool overBudget = budgetedBytes > fMaxBytes || budgetedCount > fMaxCount;
bsalomon12299ab2014-11-14 13:33:09 -0800965 // SkASSERT(!overBudget || locked == count || fPurging);
bsalomon71cb0c22014-11-14 12:10:14 -0800966}
bsalomonf320e042015-02-17 15:09:34 -0800967
968bool GrResourceCache::isInCache(const GrGpuResource* resource) const {
969 int index = *resource->cacheAccess().accessCacheIndex();
970 if (index < 0) {
971 return false;
972 }
973 if (index < fPurgeableQueue.count() && fPurgeableQueue.at(index) == resource) {
974 return true;
975 }
976 if (index < fNonpurgeableResources.count() && fNonpurgeableResources[index] == resource) {
977 return true;
978 }
979 SkDEBUGFAIL("Resource index should be -1 or the resource should be in the cache.");
980 return false;
981}
982
bsalomon71cb0c22014-11-14 12:10:14 -0800983#endif