blob: 4a31a8e6c31bc4a2903bf3a2b8cbc8f89845f945 [file] [log] [blame]
bsalomonc8dc1f72014-08-21 13:02:13 -07001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrResourceCache.h"
Brian Salomon614c1a82018-12-19 15:42:06 -05009#include <atomic>
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/gpu/GrContext.h"
11#include "include/gpu/GrTexture.h"
12#include "include/private/GrSingleOwner.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/private/SkTo.h"
14#include "include/utils/SkRandom.h"
15#include "src/core/SkExchange.h"
Ben Wagner21bca282019-05-15 10:15:52 -040016#include "src/core/SkMessageBus.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/core/SkOpts.h"
18#include "src/core/SkScopeExit.h"
19#include "src/core/SkTSort.h"
20#include "src/gpu/GrCaps.h"
21#include "src/gpu/GrContextPriv.h"
22#include "src/gpu/GrGpuResourceCacheAccess.h"
23#include "src/gpu/GrProxyProvider.h"
24#include "src/gpu/GrTextureProxyCacheAccess.h"
25#include "src/gpu/GrTracing.h"
26#include "src/gpu/SkGr.h"
bsalomon71cb0c22014-11-14 12:10:14 -080027
bsalomon8718aaf2015-02-19 07:24:21 -080028DECLARE_SKMESSAGEBUS_MESSAGE(GrUniqueKeyInvalidatedMessage);
bsalomon71cb0c22014-11-14 12:10:14 -080029
Brian Osman13dddce2017-05-09 13:19:50 -040030DECLARE_SKMESSAGEBUS_MESSAGE(GrGpuResourceFreedMessage);
31
Brian Salomon8f8995a2018-10-15 14:32:15 -040032#define ASSERT_SINGLE_OWNER \
33 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);)
34
bsalomon71cb0c22014-11-14 12:10:14 -080035//////////////////////////////////////////////////////////////////////////////
36
bsalomon7775c852014-12-30 12:50:52 -080037GrScratchKey::ResourceType GrScratchKey::GenerateResourceType() {
Mike Klein0ec1c572018-12-04 11:52:51 -050038 static std::atomic<int32_t> nextType{INHERITED::kInvalidDomain + 1};
bsalomonfe369ee2014-11-10 11:59:06 -080039
Mike Klein0ec1c572018-12-04 11:52:51 -050040 int32_t type = nextType++;
Ben Wagner9bc36fd2018-06-15 14:23:36 -040041 if (type > SkTo<int32_t>(UINT16_MAX)) {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040042 SK_ABORT("Too many Resource Types");
bsalomon71cb0c22014-11-14 12:10:14 -080043 }
44
45 return static_cast<ResourceType>(type);
46}
47
bsalomon8718aaf2015-02-19 07:24:21 -080048GrUniqueKey::Domain GrUniqueKey::GenerateDomain() {
Mike Klein0ec1c572018-12-04 11:52:51 -050049 static std::atomic<int32_t> nextDomain{INHERITED::kInvalidDomain + 1};
bsalomon7775c852014-12-30 12:50:52 -080050
Mike Klein0ec1c572018-12-04 11:52:51 -050051 int32_t domain = nextDomain++;
Ben Wagner397ee0e2018-06-15 15:13:26 -040052 if (domain > SkTo<int32_t>(UINT16_MAX)) {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040053 SK_ABORT("Too many GrUniqueKey Domains");
bsalomon7775c852014-12-30 12:50:52 -080054 }
bsalomon24db3b12015-01-23 04:24:04 -080055
56 return static_cast<Domain>(domain);
57}
bsalomon3f324322015-04-08 11:01:54 -070058
bsalomon24db3b12015-01-23 04:24:04 -080059uint32_t GrResourceKeyHash(const uint32_t* data, size_t size) {
mtklein4e976072016-08-08 09:06:27 -070060 return SkOpts::hash(data, size);
bsalomon7775c852014-12-30 12:50:52 -080061}
62
bsalomonfe369ee2014-11-10 11:59:06 -080063//////////////////////////////////////////////////////////////////////////////
64
bsalomon0ea80f42015-02-11 10:49:59 -080065class GrResourceCache::AutoValidate : ::SkNoncopyable {
bsalomon71cb0c22014-11-14 12:10:14 -080066public:
bsalomon0ea80f42015-02-11 10:49:59 -080067 AutoValidate(GrResourceCache* cache) : fCache(cache) { cache->validate(); }
bsalomon71cb0c22014-11-14 12:10:14 -080068 ~AutoValidate() { fCache->validate(); }
69private:
bsalomon0ea80f42015-02-11 10:49:59 -080070 GrResourceCache* fCache;
bsalomon71cb0c22014-11-14 12:10:14 -080071};
72
Brian Salomon876a0172019-03-08 11:12:14 -050073//////////////////////////////////////////////////////////////////////////////
74
75inline GrResourceCache::ResourceAwaitingUnref::ResourceAwaitingUnref() = default;
76
77inline GrResourceCache::ResourceAwaitingUnref::ResourceAwaitingUnref(GrGpuResource* resource)
78 : fResource(resource), fNumUnrefs(1) {}
79
80inline GrResourceCache::ResourceAwaitingUnref::ResourceAwaitingUnref(ResourceAwaitingUnref&& that) {
81 fResource = skstd::exchange(that.fResource, nullptr);
82 fNumUnrefs = skstd::exchange(that.fNumUnrefs, 0);
83}
84
85inline GrResourceCache::ResourceAwaitingUnref& GrResourceCache::ResourceAwaitingUnref::operator=(
86 ResourceAwaitingUnref&& that) {
87 fResource = skstd::exchange(that.fResource, nullptr);
88 fNumUnrefs = skstd::exchange(that.fNumUnrefs, 0);
89 return *this;
90}
91
92inline GrResourceCache::ResourceAwaitingUnref::~ResourceAwaitingUnref() {
93 if (fResource) {
94 for (int i = 0; i < fNumUnrefs; ++i) {
95 fResource->unref();
96 }
97 }
98}
99
100inline void GrResourceCache::ResourceAwaitingUnref::addRef() { ++fNumUnrefs; }
101
102inline void GrResourceCache::ResourceAwaitingUnref::unref() {
103 SkASSERT(fNumUnrefs > 0);
104 fResource->unref();
105 --fNumUnrefs;
106}
107
108inline bool GrResourceCache::ResourceAwaitingUnref::finished() { return !fNumUnrefs; }
109
110//////////////////////////////////////////////////////////////////////////////
robertphillipsee843b22016-10-04 05:30:20 -0700111
Brian Salomon8f8995a2018-10-15 14:32:15 -0400112GrResourceCache::GrResourceCache(const GrCaps* caps, GrSingleOwner* singleOwner,
113 uint32_t contextUniqueID)
Brian Salomon2c791fc2019-04-02 11:52:03 -0400114 : fInvalidUniqueKeyInbox(contextUniqueID)
Brian Salomon238069b2018-07-11 15:58:57 -0400115 , fFreedGpuResourceInbox(contextUniqueID)
116 , fContextUniqueID(contextUniqueID)
Brian Salomon8f8995a2018-10-15 14:32:15 -0400117 , fSingleOwner(singleOwner)
Brian Salomon238069b2018-07-11 15:58:57 -0400118 , fPreferVRAMUseOverFlushes(caps->preferVRAMUseOverFlushes()) {
119 SkASSERT(contextUniqueID != SK_InvalidUniqueID);
bsalomon71cb0c22014-11-14 12:10:14 -0800120}
121
bsalomon0ea80f42015-02-11 10:49:59 -0800122GrResourceCache::~GrResourceCache() {
bsalomonc8dc1f72014-08-21 13:02:13 -0700123 this->releaseAll();
124}
125
Robert Phillipscf39f372019-09-03 10:29:20 -0400126void GrResourceCache::setLimit(size_t bytes) {
bsalomon71cb0c22014-11-14 12:10:14 -0800127 fMaxBytes = bytes;
128 this->purgeAsNeeded();
129}
130
bsalomon0ea80f42015-02-11 10:49:59 -0800131void GrResourceCache::insertResource(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400132 ASSERT_SINGLE_OWNER
bsalomon49f085d2014-09-05 13:34:00 -0700133 SkASSERT(resource);
bsalomon16961262014-08-26 14:01:07 -0700134 SkASSERT(!this->isInCache(resource));
bsalomonf320e042015-02-17 15:09:34 -0800135 SkASSERT(!resource->wasDestroyed());
Brian Salomon614c1a82018-12-19 15:42:06 -0500136 SkASSERT(!resource->resourcePriv().isPurgeable());
bsalomonddf30e62015-02-19 11:38:44 -0800137
138 // We must set the timestamp before adding to the array in case the timestamp wraps and we wind
139 // up iterating over all the resources that already have timestamps.
140 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
141
bsalomonf320e042015-02-17 15:09:34 -0800142 this->addToNonpurgeableArray(resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800143
bsalomondace19e2014-11-17 07:34:06 -0800144 size_t size = resource->gpuMemorySize();
bsalomonf320e042015-02-17 15:09:34 -0800145 SkDEBUGCODE(++fCount;)
bsalomon84c8e622014-11-17 09:33:27 -0800146 fBytes += size;
bsalomon82b1d622014-11-14 13:59:57 -0800147#if GR_CACHE_STATS
bsalomonf320e042015-02-17 15:09:34 -0800148 fHighWaterCount = SkTMax(this->getResourceCount(), fHighWaterCount);
bsalomon82b1d622014-11-14 13:59:57 -0800149 fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes);
150#endif
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500151 if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
bsalomondace19e2014-11-17 07:34:06 -0800152 ++fBudgetedCount;
153 fBudgetedBytes += size;
Brian Osman39c08ac2017-07-26 09:36:09 -0400154 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800155 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomondace19e2014-11-17 07:34:06 -0800156#if GR_CACHE_STATS
157 fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount);
158 fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes);
159#endif
160 }
robertphillipsc4ed6842016-05-24 14:17:12 -0700161 if (resource->resourcePriv().getScratchKey().isValid() &&
162 !resource->getUniqueKey().isValid()) {
kkinnunen2e6055b2016-04-22 01:48:29 -0700163 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
bsalomon3582d3e2015-02-13 14:20:05 -0800164 fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
bsalomon744998e2014-08-28 09:54:34 -0700165 }
bsalomon9f2d1572015-02-17 11:47:40 -0800166
bsalomon71cb0c22014-11-14 12:10:14 -0800167 this->purgeAsNeeded();
bsalomonc8dc1f72014-08-21 13:02:13 -0700168}
169
bsalomon0ea80f42015-02-11 10:49:59 -0800170void GrResourceCache::removeResource(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400171 ASSERT_SINGLE_OWNER
bsalomon9f2d1572015-02-17 11:47:40 -0800172 this->validate();
bsalomon16961262014-08-26 14:01:07 -0700173 SkASSERT(this->isInCache(resource));
bsalomondace19e2014-11-17 07:34:06 -0800174
Derek Sollenbergeree479142017-05-24 11:41:33 -0400175 size_t size = resource->gpuMemorySize();
Brian Salomon614c1a82018-12-19 15:42:06 -0500176 if (resource->resourcePriv().isPurgeable()) {
bsalomon9f2d1572015-02-17 11:47:40 -0800177 fPurgeableQueue.remove(resource);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400178 fPurgeableBytes -= size;
bsalomonf320e042015-02-17 15:09:34 -0800179 } else {
180 this->removeFromNonpurgeableArray(resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800181 }
182
bsalomonf320e042015-02-17 15:09:34 -0800183 SkDEBUGCODE(--fCount;)
bsalomondace19e2014-11-17 07:34:06 -0800184 fBytes -= size;
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500185 if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
bsalomondace19e2014-11-17 07:34:06 -0800186 --fBudgetedCount;
187 fBudgetedBytes -= size;
Brian Osman39c08ac2017-07-26 09:36:09 -0400188 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800189 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomondace19e2014-11-17 07:34:06 -0800190 }
191
robertphillipsc4ed6842016-05-24 14:17:12 -0700192 if (resource->resourcePriv().getScratchKey().isValid() &&
193 !resource->getUniqueKey().isValid()) {
bsalomon3582d3e2015-02-13 14:20:05 -0800194 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
bsalomon744998e2014-08-28 09:54:34 -0700195 }
bsalomon8718aaf2015-02-19 07:24:21 -0800196 if (resource->getUniqueKey().isValid()) {
197 fUniqueHash.remove(resource->getUniqueKey());
bsalomon8b79d232014-11-10 10:19:06 -0800198 }
bsalomonb436ed62014-11-17 12:15:56 -0800199 this->validate();
bsalomonc8dc1f72014-08-21 13:02:13 -0700200}
201
bsalomon0ea80f42015-02-11 10:49:59 -0800202void GrResourceCache::abandonAll() {
bsalomon71cb0c22014-11-14 12:10:14 -0800203 AutoValidate av(this);
204
Brian Salomon876a0172019-03-08 11:12:14 -0500205 // We need to make sure to free any resources that were waiting on a free message but never
206 // received one.
207 fResourcesAwaitingUnref.reset();
Greg Danielb2acf0a2018-09-12 09:17:11 -0400208
bsalomonf320e042015-02-17 15:09:34 -0800209 while (fNonpurgeableResources.count()) {
210 GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
211 SkASSERT(!back->wasDestroyed());
212 back->cacheAccess().abandon();
bsalomonc8dc1f72014-08-21 13:02:13 -0700213 }
bsalomonf320e042015-02-17 15:09:34 -0800214
215 while (fPurgeableQueue.count()) {
216 GrGpuResource* top = fPurgeableQueue.peek();
217 SkASSERT(!top->wasDestroyed());
218 top->cacheAccess().abandon();
219 }
220
bsalomon744998e2014-08-28 09:54:34 -0700221 SkASSERT(!fScratchMap.count());
bsalomon8718aaf2015-02-19 07:24:21 -0800222 SkASSERT(!fUniqueHash.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700223 SkASSERT(!fCount);
bsalomonf320e042015-02-17 15:09:34 -0800224 SkASSERT(!this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800225 SkASSERT(!fBytes);
226 SkASSERT(!fBudgetedCount);
227 SkASSERT(!fBudgetedBytes);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400228 SkASSERT(!fPurgeableBytes);
Brian Salomon876a0172019-03-08 11:12:14 -0500229 SkASSERT(!fResourcesAwaitingUnref.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700230}
231
bsalomon0ea80f42015-02-11 10:49:59 -0800232void GrResourceCache::releaseAll() {
bsalomon71cb0c22014-11-14 12:10:14 -0800233 AutoValidate av(this);
234
Brian Osman13dddce2017-05-09 13:19:50 -0400235 this->processFreedGpuResources();
236
Greg Danielc27eb722018-08-10 09:48:08 -0400237 // We need to make sure to free any resources that were waiting on a free message but never
238 // received one.
Brian Salomon876a0172019-03-08 11:12:14 -0500239 fResourcesAwaitingUnref.reset();
Greg Danielc27eb722018-08-10 09:48:08 -0400240
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500241 SkASSERT(fProxyProvider); // better have called setProxyProvider
Robert Phillips3ec95732017-09-29 15:10:39 -0400242 // We must remove the uniqueKeys from the proxies here. While they possess a uniqueKey
243 // they also have a raw pointer back to this class (which is presumably going away)!
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500244 fProxyProvider->removeAllUniqueKeys();
Robert Phillips45a6f142017-09-29 09:49:41 -0400245
Brian Salomon614c1a82018-12-19 15:42:06 -0500246 while (fNonpurgeableResources.count()) {
bsalomonf320e042015-02-17 15:09:34 -0800247 GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
248 SkASSERT(!back->wasDestroyed());
249 back->cacheAccess().release();
bsalomonc8dc1f72014-08-21 13:02:13 -0700250 }
bsalomonf320e042015-02-17 15:09:34 -0800251
252 while (fPurgeableQueue.count()) {
253 GrGpuResource* top = fPurgeableQueue.peek();
254 SkASSERT(!top->wasDestroyed());
255 top->cacheAccess().release();
256 }
257
bsalomon744998e2014-08-28 09:54:34 -0700258 SkASSERT(!fScratchMap.count());
bsalomon8718aaf2015-02-19 07:24:21 -0800259 SkASSERT(!fUniqueHash.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700260 SkASSERT(!fCount);
bsalomonf320e042015-02-17 15:09:34 -0800261 SkASSERT(!this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800262 SkASSERT(!fBytes);
263 SkASSERT(!fBudgetedCount);
264 SkASSERT(!fBudgetedBytes);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400265 SkASSERT(!fPurgeableBytes);
Brian Salomon876a0172019-03-08 11:12:14 -0500266 SkASSERT(!fResourcesAwaitingUnref.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700267}
bsalomonbcf0a522014-10-08 08:40:09 -0700268
Brian Salomon2c791fc2019-04-02 11:52:03 -0400269void GrResourceCache::refResource(GrGpuResource* resource) {
270 SkASSERT(resource);
271 SkASSERT(resource->getContext()->priv().getResourceCache() == this);
272 if (resource->cacheAccess().hasRef()) {
273 resource->ref();
274 } else {
275 this->refAndMakeResourceMRU(resource);
276 }
277 this->validate();
278}
279
bsalomon0ea80f42015-02-11 10:49:59 -0800280class GrResourceCache::AvailableForScratchUse {
bsalomonbcf0a522014-10-08 08:40:09 -0700281public:
bsalomon000f8292014-10-15 19:04:14 -0700282 AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendingIO) { }
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());
bsalomon12299ab2014-11-14 13:33:09 -0800287 if (resource->internalHasRef() || !resource->cacheAccess().isScratch()) {
bsalomon000f8292014-10-15 19:04:14 -0700288 return false;
bsalomonbcf0a522014-10-08 08:40:09 -0700289 }
bsalomon000f8292014-10-15 19:04:14 -0700290 return !fRejectPendingIO || !resource->internalHasPendingIO();
bsalomonbcf0a522014-10-08 08:40:09 -0700291 }
bsalomon1e2530b2014-10-09 09:57:18 -0700292
bsalomonbcf0a522014-10-08 08:40:09 -0700293private:
bsalomon000f8292014-10-15 19:04:14 -0700294 bool fRejectPendingIO;
bsalomonbcf0a522014-10-08 08:40:09 -0700295};
296
bsalomon0ea80f42015-02-11 10:49:59 -0800297GrGpuResource* GrResourceCache::findAndRefScratchResource(const GrScratchKey& scratchKey,
robertphillips6e83ac72015-08-13 05:19:14 -0700298 size_t resourceSize,
Chris Daltond004e0b2018-09-27 09:28:03 -0600299 ScratchFlags flags) {
bsalomon7775c852014-12-30 12:50:52 -0800300 SkASSERT(scratchKey.isValid());
robertphillipsee843b22016-10-04 05:30:20 -0700301
bsalomon71cb0c22014-11-14 12:10:14 -0800302 GrGpuResource* resource;
Chris Daltond004e0b2018-09-27 09:28:03 -0600303 if (flags & (ScratchFlags::kPreferNoPendingIO | ScratchFlags::kRequireNoPendingIO)) {
bsalomon71cb0c22014-11-14 12:10:14 -0800304 resource = fScratchMap.find(scratchKey, AvailableForScratchUse(true));
bsalomon000f8292014-10-15 19:04:14 -0700305 if (resource) {
bsalomon9f2d1572015-02-17 11:47:40 -0800306 this->refAndMakeResourceMRU(resource);
bsalomonb436ed62014-11-17 12:15:56 -0800307 this->validate();
308 return resource;
Chris Daltond004e0b2018-09-27 09:28:03 -0600309 } else if (flags & ScratchFlags::kRequireNoPendingIO) {
halcanary96fcdcc2015-08-27 07:41:13 -0700310 return nullptr;
bsalomon000f8292014-10-15 19:04:14 -0700311 }
robertphillips63926682015-08-20 09:39:02 -0700312 // We would prefer to consume more available VRAM rather than flushing
313 // immediately, but on ANGLE this can lead to starving of the GPU.
314 if (fPreferVRAMUseOverFlushes && this->wouldFit(resourceSize)) {
robertphillips6e83ac72015-08-13 05:19:14 -0700315 // kPrefer is specified, we didn't find a resource without pending io,
robertphillips63926682015-08-20 09:39:02 -0700316 // but there is still space in our budget for the resource so force
317 // the caller to allocate a new resource.
halcanary96fcdcc2015-08-27 07:41:13 -0700318 return nullptr;
robertphillips6e83ac72015-08-13 05:19:14 -0700319 }
bsalomon000f8292014-10-15 19:04:14 -0700320 }
bsalomon71cb0c22014-11-14 12:10:14 -0800321 resource = fScratchMap.find(scratchKey, AvailableForScratchUse(false));
322 if (resource) {
bsalomon9f2d1572015-02-17 11:47:40 -0800323 this->refAndMakeResourceMRU(resource);
bsalomonb436ed62014-11-17 12:15:56 -0800324 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800325 }
326 return resource;
bsalomonbcf0a522014-10-08 08:40:09 -0700327}
bsalomon8b79d232014-11-10 10:19:06 -0800328
bsalomon0ea80f42015-02-11 10:49:59 -0800329void GrResourceCache::willRemoveScratchKey(const GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400330 ASSERT_SINGLE_OWNER
bsalomon3582d3e2015-02-13 14:20:05 -0800331 SkASSERT(resource->resourcePriv().getScratchKey().isValid());
robertphillipsc4ed6842016-05-24 14:17:12 -0700332 if (!resource->getUniqueKey().isValid()) {
333 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
334 }
bsalomon10e23ca2014-11-25 05:52:06 -0800335}
336
bsalomonf99e9612015-02-19 08:24:16 -0800337void GrResourceCache::removeUniqueKey(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400338 ASSERT_SINGLE_OWNER
bsalomon3f324322015-04-08 11:01:54 -0700339 // Someone has a ref to this resource in order to have removed the key. When the ref count
340 // reaches zero we will get a ref cnt notification and figure out what to do with it.
bsalomonf99e9612015-02-19 08:24:16 -0800341 if (resource->getUniqueKey().isValid()) {
342 SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
343 fUniqueHash.remove(resource->getUniqueKey());
344 }
345 resource->cacheAccess().removeUniqueKey();
robertphillipsc4ed6842016-05-24 14:17:12 -0700346 if (resource->resourcePriv().getScratchKey().isValid()) {
347 fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
348 }
349
Brian Salomon9bc76d92019-01-24 12:18:33 -0500350 // Removing a unique key from a kUnbudgetedCacheable resource would make the resource
351 // require purging. However, the resource must be ref'ed to get here and therefore can't
352 // be purgeable. We'll purge it when the refs reach zero.
353 SkASSERT(!resource->resourcePriv().isPurgeable());
bsalomonf99e9612015-02-19 08:24:16 -0800354 this->validate();
bsalomon23e619c2015-02-06 11:54:28 -0800355}
356
bsalomonf99e9612015-02-19 08:24:16 -0800357void GrResourceCache::changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400358 ASSERT_SINGLE_OWNER
bsalomon8b79d232014-11-10 10:19:06 -0800359 SkASSERT(resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800360 SkASSERT(this->isInCache(resource));
bsalomon8b79d232014-11-10 10:19:06 -0800361
bsalomonf99e9612015-02-19 08:24:16 -0800362 // If another resource has the new key, remove its key then install the key on this resource.
363 if (newKey.isValid()) {
Greg Daniel0d537802017-09-08 11:44:14 -0400364 if (GrGpuResource* old = fUniqueHash.find(newKey)) {
365 // If the old resource using the key is purgeable and is unreachable, then remove it.
Brian Salomon614c1a82018-12-19 15:42:06 -0500366 if (!old->resourcePriv().getScratchKey().isValid() &&
367 old->resourcePriv().isPurgeable()) {
Greg Daniel0d537802017-09-08 11:44:14 -0400368 old->cacheAccess().release();
369 } else {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500370 // removeUniqueKey expects an external owner of the resource.
371 this->removeUniqueKey(sk_ref_sp(old).get());
Greg Daniel0d537802017-09-08 11:44:14 -0400372 }
373 }
374 SkASSERT(nullptr == fUniqueHash.find(newKey));
375
robertphillipsc4ed6842016-05-24 14:17:12 -0700376 // Remove the entry for this resource if it already has a unique key.
377 if (resource->getUniqueKey().isValid()) {
378 SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
379 fUniqueHash.remove(resource->getUniqueKey());
380 SkASSERT(nullptr == fUniqueHash.find(resource->getUniqueKey()));
381 } else {
382 // 'resource' didn't have a valid unique key before so it is switching sides. Remove it
383 // from the ScratchMap
384 if (resource->resourcePriv().getScratchKey().isValid()) {
385 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
386 }
387 }
388
bsalomonf99e9612015-02-19 08:24:16 -0800389 resource->cacheAccess().setUniqueKey(newKey);
390 fUniqueHash.add(resource);
391 } else {
robertphillipsc4ed6842016-05-24 14:17:12 -0700392 this->removeUniqueKey(resource);
bsalomonf99e9612015-02-19 08:24:16 -0800393 }
394
bsalomon71cb0c22014-11-14 12:10:14 -0800395 this->validate();
bsalomon8b79d232014-11-10 10:19:06 -0800396}
bsalomon71cb0c22014-11-14 12:10:14 -0800397
bsalomon9f2d1572015-02-17 11:47:40 -0800398void GrResourceCache::refAndMakeResourceMRU(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400399 ASSERT_SINGLE_OWNER
bsalomon71cb0c22014-11-14 12:10:14 -0800400 SkASSERT(resource);
401 SkASSERT(this->isInCache(resource));
bsalomonddf30e62015-02-19 11:38:44 -0800402
Brian Salomon614c1a82018-12-19 15:42:06 -0500403 if (resource->resourcePriv().isPurgeable()) {
bsalomon9f2d1572015-02-17 11:47:40 -0800404 // It's about to become unpurgeable.
Derek Sollenbergeree479142017-05-24 11:41:33 -0400405 fPurgeableBytes -= resource->gpuMemorySize();
bsalomon9f2d1572015-02-17 11:47:40 -0800406 fPurgeableQueue.remove(resource);
bsalomonf320e042015-02-17 15:09:34 -0800407 this->addToNonpurgeableArray(resource);
Brian Salomon2c791fc2019-04-02 11:52:03 -0400408 } else if (!resource->cacheAccess().hasRef() &&
409 resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
410 SkASSERT(fNumBudgetedResourcesFlushWillMakePurgeable > 0);
411 fNumBudgetedResourcesFlushWillMakePurgeable--;
bsalomon9f2d1572015-02-17 11:47:40 -0800412 }
Brian Salomon01ceae92019-04-02 11:49:54 -0400413 resource->cacheAccess().ref();
bsalomonddf30e62015-02-19 11:38:44 -0800414
415 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
bsalomonf320e042015-02-17 15:09:34 -0800416 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800417}
418
bsalomon3f324322015-04-08 11:01:54 -0700419void GrResourceCache::notifyCntReachedZero(GrGpuResource* resource, uint32_t flags) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400420 ASSERT_SINGLE_OWNER
bsalomon71cb0c22014-11-14 12:10:14 -0800421 SkASSERT(resource);
bsalomon3f324322015-04-08 11:01:54 -0700422 SkASSERT(!resource->wasDestroyed());
423 SkASSERT(flags);
bsalomon71cb0c22014-11-14 12:10:14 -0800424 SkASSERT(this->isInCache(resource));
bsalomon3f324322015-04-08 11:01:54 -0700425 // This resource should always be in the nonpurgeable array when this function is called. It
426 // will be moved to the queue if it is newly purgeable.
427 SkASSERT(fNonpurgeableResources[*resource->cacheAccess().accessCacheIndex()] == resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800428
bsalomon3f324322015-04-08 11:01:54 -0700429 if (SkToBool(ResourceAccess::kRefCntReachedZero_RefNotificationFlag & flags)) {
430#ifdef SK_DEBUG
431 // When the timestamp overflows validate() is called. validate() checks that resources in
432 // the nonpurgeable array are indeed not purgeable. However, the movement from the array to
433 // the purgeable queue happens just below in this function. So we mark it as an exception.
Brian Salomon614c1a82018-12-19 15:42:06 -0500434 if (resource->resourcePriv().isPurgeable()) {
bsalomon3f324322015-04-08 11:01:54 -0700435 fNewlyPurgeableResourceForValidation = resource;
436 }
437#endif
438 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
halcanary96fcdcc2015-08-27 07:41:13 -0700439 SkDEBUGCODE(fNewlyPurgeableResourceForValidation = nullptr);
Brian Salomon2c791fc2019-04-02 11:52:03 -0400440 if (!resource->resourcePriv().isPurgeable() &&
441 resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
442 SkASSERT(resource->resourcePriv().hasPendingIO_debugOnly());
443 ++fNumBudgetedResourcesFlushWillMakePurgeable;
444 }
445 } else {
446 // If this is budgeted and just became purgeable by dropping the last pending IO
447 // then it clearly no longer needs a flush to become purgeable.
448 if (resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted &&
449 resource->resourcePriv().isPurgeable()) {
450 SkASSERT(fNumBudgetedResourcesFlushWillMakePurgeable > 0);
451 fNumBudgetedResourcesFlushWillMakePurgeable--;
452 }
bsalomon3f324322015-04-08 11:01:54 -0700453 }
454
455 if (!SkToBool(ResourceAccess::kAllCntsReachedZero_RefNotificationFlag & flags)) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500456 SkASSERT(!resource->resourcePriv().isPurgeable());
bsalomon3f324322015-04-08 11:01:54 -0700457 return;
458 }
459
Brian Salomon9bc76d92019-01-24 12:18:33 -0500460 if (!resource->resourcePriv().isPurgeable()) {
461 this->validate();
462 return;
463 }
464
bsalomonf320e042015-02-17 15:09:34 -0800465 this->removeFromNonpurgeableArray(resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800466 fPurgeableQueue.insert(resource);
Brian Salomon5e150852017-03-22 14:53:13 -0400467 resource->cacheAccess().setTimeWhenResourceBecomePurgeable();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400468 fPurgeableBytes += resource->gpuMemorySize();
bsalomon71cb0c22014-11-14 12:10:14 -0800469
Greg Daniel303e83e2018-09-10 14:10:19 -0400470 bool hasUniqueKey = resource->getUniqueKey().isValid();
471
Brian Salomon9bc76d92019-01-24 12:18:33 -0500472 GrBudgetedType budgetedType = resource->resourcePriv().budgetedType();
Brian Salomon614c1a82018-12-19 15:42:06 -0500473
Brian Salomon9bc76d92019-01-24 12:18:33 -0500474 if (budgetedType == GrBudgetedType::kBudgeted) {
475 // Purge the resource immediately if we're over budget
476 // Also purge if the resource has neither a valid scratch key nor a unique key.
477 bool hasKey = resource->resourcePriv().getScratchKey().isValid() || hasUniqueKey;
478 if (!this->overBudget() && hasKey) {
479 return;
480 }
481 } else {
482 // We keep unbudgeted resources with a unique key in the purgeable queue of the cache so
483 // they can be reused again by the image connected to the unique key.
484 if (hasUniqueKey && budgetedType == GrBudgetedType::kUnbudgetedCacheable) {
485 return;
486 }
487 // Check whether this resource could still be used as a scratch resource.
488 if (!resource->resourcePriv().refsWrappedObjects() &&
489 resource->resourcePriv().getScratchKey().isValid()) {
490 // We won't purge an existing resource to make room for this one.
Robert Phillipscf39f372019-09-03 10:29:20 -0400491 if (this->wouldFit(resource->gpuMemorySize())) {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500492 resource->resourcePriv().makeBudgeted();
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500493 return;
494 }
bsalomonc2f35b72015-01-23 07:19:22 -0800495 }
bsalomonc2f35b72015-01-23 07:19:22 -0800496 }
Brian Salomon9bc76d92019-01-24 12:18:33 -0500497
bsalomonf320e042015-02-17 15:09:34 -0800498 SkDEBUGCODE(int beforeCount = this->getResourceCount();)
bsalomon9f2d1572015-02-17 11:47:40 -0800499 resource->cacheAccess().release();
500 // We should at least free this resource, perhaps dependent resources as well.
bsalomonf320e042015-02-17 15:09:34 -0800501 SkASSERT(this->getResourceCount() < beforeCount);
bsalomon71cb0c22014-11-14 12:10:14 -0800502 this->validate();
503}
504
bsalomon0ea80f42015-02-11 10:49:59 -0800505void GrResourceCache::didChangeBudgetStatus(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400506 ASSERT_SINGLE_OWNER
bsalomon84c8e622014-11-17 09:33:27 -0800507 SkASSERT(resource);
508 SkASSERT(this->isInCache(resource));
509
510 size_t size = resource->gpuMemorySize();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500511 // Changing from BudgetedType::kUnbudgetedCacheable to another budgeted type could make
512 // resource become purgeable. However, we should never allow that transition. Wrapped
513 // resources are the only resources that can be in that state and they aren't allowed to
514 // transition from one budgeted state to another.
515 SkDEBUGCODE(bool wasPurgeable = resource->resourcePriv().isPurgeable());
516 if (resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
bsalomon84c8e622014-11-17 09:33:27 -0800517 ++fBudgetedCount;
518 fBudgetedBytes += size;
bsalomonafe30052015-01-16 07:32:33 -0800519#if GR_CACHE_STATS
520 fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes);
521 fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount);
522#endif
Brian Salomon2c791fc2019-04-02 11:52:03 -0400523 if (!resource->resourcePriv().isPurgeable() && !resource->cacheAccess().hasRef()) {
524 ++fNumBudgetedResourcesFlushWillMakePurgeable;
525 }
bsalomon84c8e622014-11-17 09:33:27 -0800526 this->purgeAsNeeded();
527 } else {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500528 SkASSERT(resource->resourcePriv().budgetedType() != GrBudgetedType::kUnbudgetedCacheable);
bsalomon84c8e622014-11-17 09:33:27 -0800529 --fBudgetedCount;
530 fBudgetedBytes -= size;
Brian Salomon2c791fc2019-04-02 11:52:03 -0400531 if (!resource->resourcePriv().isPurgeable() && !resource->cacheAccess().hasRef()) {
532 --fNumBudgetedResourcesFlushWillMakePurgeable;
533 }
bsalomon84c8e622014-11-17 09:33:27 -0800534 }
Brian Salomon9bc76d92019-01-24 12:18:33 -0500535 SkASSERT(wasPurgeable == resource->resourcePriv().isPurgeable());
Brian Osman39c08ac2017-07-26 09:36:09 -0400536 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800537 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomon84c8e622014-11-17 09:33:27 -0800538
539 this->validate();
540}
541
robertphillipsee843b22016-10-04 05:30:20 -0700542void GrResourceCache::purgeAsNeeded() {
bsalomon3f324322015-04-08 11:01:54 -0700543 SkTArray<GrUniqueKeyInvalidatedMessage> invalidKeyMsgs;
544 fInvalidUniqueKeyInbox.poll(&invalidKeyMsgs);
545 if (invalidKeyMsgs.count()) {
Robert Phillips427966a2018-12-20 17:20:43 -0500546 SkASSERT(fProxyProvider);
547
548 for (int i = 0; i < invalidKeyMsgs.count(); ++i) {
549 fProxyProvider->processInvalidUniqueKey(invalidKeyMsgs[i].key(), nullptr,
550 GrProxyProvider::InvalidateGPUResource::kYes);
551 SkASSERT(!this->findAndRefUniqueResource(invalidKeyMsgs[i].key()));
552 }
bsalomon3f324322015-04-08 11:01:54 -0700553 }
bsalomon71cb0c22014-11-14 12:10:14 -0800554
Brian Osman13dddce2017-05-09 13:19:50 -0400555 this->processFreedGpuResources();
556
bsalomon3f324322015-04-08 11:01:54 -0700557 bool stillOverbudget = this->overBudget();
558 while (stillOverbudget && fPurgeableQueue.count()) {
robertphillipsee843b22016-10-04 05:30:20 -0700559 GrGpuResource* resource = fPurgeableQueue.peek();
Brian Salomon614c1a82018-12-19 15:42:06 -0500560 SkASSERT(resource->resourcePriv().isPurgeable());
bsalomon9f2d1572015-02-17 11:47:40 -0800561 resource->cacheAccess().release();
bsalomon3f324322015-04-08 11:01:54 -0700562 stillOverbudget = this->overBudget();
bsalomon9f2d1572015-02-17 11:47:40 -0800563 }
bsalomon71cb0c22014-11-14 12:10:14 -0800564
bsalomonb436ed62014-11-17 12:15:56 -0800565 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800566}
567
Robert Phillips6eba0632018-03-28 12:25:42 -0400568void GrResourceCache::purgeUnlockedResources(bool scratchResourcesOnly) {
569 if (!scratchResourcesOnly) {
570 // We could disable maintaining the heap property here, but it would add a lot of
571 // complexity. Moreover, this is rarely called.
572 while (fPurgeableQueue.count()) {
573 GrGpuResource* resource = fPurgeableQueue.peek();
Brian Salomon614c1a82018-12-19 15:42:06 -0500574 SkASSERT(resource->resourcePriv().isPurgeable());
Robert Phillips6eba0632018-03-28 12:25:42 -0400575 resource->cacheAccess().release();
576 }
577 } else {
578 // Sort the queue
579 fPurgeableQueue.sort();
580
581 // Make a list of the scratch resources to delete
582 SkTDArray<GrGpuResource*> scratchResources;
583 for (int i = 0; i < fPurgeableQueue.count(); i++) {
584 GrGpuResource* resource = fPurgeableQueue.at(i);
Brian Salomon614c1a82018-12-19 15:42:06 -0500585 SkASSERT(resource->resourcePriv().isPurgeable());
Robert Phillips6eba0632018-03-28 12:25:42 -0400586 if (!resource->getUniqueKey().isValid()) {
587 *scratchResources.append() = resource;
588 }
589 }
590
591 // Delete the scratch resources. This must be done as a separate pass
592 // to avoid messing up the sorted order of the queue
593 for (int i = 0; i < scratchResources.count(); i++) {
594 scratchResources.getAt(i)->cacheAccess().release();
595 }
bsalomon9f2d1572015-02-17 11:47:40 -0800596 }
bsalomon71cb0c22014-11-14 12:10:14 -0800597
bsalomonb436ed62014-11-17 12:15:56 -0800598 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800599}
600
Brian Salomon5e150852017-03-22 14:53:13 -0400601void GrResourceCache::purgeResourcesNotUsedSince(GrStdSteadyClock::time_point purgeTime) {
602 while (fPurgeableQueue.count()) {
603 const GrStdSteadyClock::time_point resourceTime =
604 fPurgeableQueue.peek()->cacheAccess().timeWhenResourceBecamePurgeable();
605 if (resourceTime >= purgeTime) {
606 // Resources were given both LRU timestamps and tagged with a frame number when
607 // they first became purgeable. The LRU timestamp won't change again until the
608 // resource is made non-purgeable again. So, at this point all the remaining
609 // resources in the timestamp-sorted queue will have a frame number >= to this
610 // one.
611 break;
612 }
613 GrGpuResource* resource = fPurgeableQueue.peek();
Brian Salomon614c1a82018-12-19 15:42:06 -0500614 SkASSERT(resource->resourcePriv().isPurgeable());
Brian Salomon5e150852017-03-22 14:53:13 -0400615 resource->cacheAccess().release();
616 }
617}
618
Derek Sollenberger5480a182017-05-25 16:43:59 -0400619void GrResourceCache::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
620
621 const size_t tmpByteBudget = SkTMax((size_t)0, fBytes - bytesToPurge);
622 bool stillOverbudget = tmpByteBudget < fBytes;
623
624 if (preferScratchResources && bytesToPurge < fPurgeableBytes) {
625 // Sort the queue
626 fPurgeableQueue.sort();
627
628 // Make a list of the scratch resources to delete
629 SkTDArray<GrGpuResource*> scratchResources;
630 size_t scratchByteCount = 0;
631 for (int i = 0; i < fPurgeableQueue.count() && stillOverbudget; i++) {
632 GrGpuResource* resource = fPurgeableQueue.at(i);
Brian Salomon614c1a82018-12-19 15:42:06 -0500633 SkASSERT(resource->resourcePriv().isPurgeable());
Derek Sollenberger5480a182017-05-25 16:43:59 -0400634 if (!resource->getUniqueKey().isValid()) {
635 *scratchResources.append() = resource;
636 scratchByteCount += resource->gpuMemorySize();
637 stillOverbudget = tmpByteBudget < fBytes - scratchByteCount;
638 }
639 }
640
641 // Delete the scratch resources. This must be done as a separate pass
642 // to avoid messing up the sorted order of the queue
643 for (int i = 0; i < scratchResources.count(); i++) {
644 scratchResources.getAt(i)->cacheAccess().release();
645 }
646 stillOverbudget = tmpByteBudget < fBytes;
647
648 this->validate();
649 }
650
651 // Purge any remaining resources in LRU order
652 if (stillOverbudget) {
653 const size_t cachedByteCount = fMaxBytes;
654 fMaxBytes = tmpByteBudget;
655 this->purgeAsNeeded();
656 fMaxBytes = cachedByteCount;
657 }
658}
Brian Salomon8cefa3e2019-04-04 11:39:55 -0400659bool GrResourceCache::requestsFlush() const {
660 return this->overBudget() && !fPurgeableQueue.count() &&
661 fNumBudgetedResourcesFlushWillMakePurgeable > 0;
662}
663
Derek Sollenberger5480a182017-05-25 16:43:59 -0400664
Brian Salomon876a0172019-03-08 11:12:14 -0500665void GrResourceCache::insertDelayedResourceUnref(GrGpuResource* resource) {
Brian Osman13dddce2017-05-09 13:19:50 -0400666 resource->ref();
Brian Salomon876a0172019-03-08 11:12:14 -0500667 uint32_t id = resource->uniqueID().asUInt();
668 if (auto* data = fResourcesAwaitingUnref.find(id)) {
669 data->addRef();
670 } else {
671 fResourcesAwaitingUnref.set(id, {resource});
672 }
Brian Osman13dddce2017-05-09 13:19:50 -0400673}
674
675void GrResourceCache::processFreedGpuResources() {
676 SkTArray<GrGpuResourceFreedMessage> msgs;
677 fFreedGpuResourceInbox.poll(&msgs);
678 for (int i = 0; i < msgs.count(); ++i) {
Brian Salomon238069b2018-07-11 15:58:57 -0400679 SkASSERT(msgs[i].fOwningUniqueID == fContextUniqueID);
Brian Salomon876a0172019-03-08 11:12:14 -0500680 uint32_t id = msgs[i].fResource->uniqueID().asUInt();
681 ResourceAwaitingUnref* info = fResourcesAwaitingUnref.find(id);
Greg Danielb2acf0a2018-09-12 09:17:11 -0400682 // If we called release or abandon on the GrContext we will have already released our ref on
683 // the GrGpuResource. If then the message arrives before the actual GrContext gets destroyed
684 // we will try to process the message when we destroy the GrContext. This protects us from
685 // trying to unref the resource twice.
Brian Salomon876a0172019-03-08 11:12:14 -0500686 if (info) {
687 info->unref();
688 if (info->finished()) {
689 fResourcesAwaitingUnref.remove(id);
690 }
Greg Danielb2acf0a2018-09-12 09:17:11 -0400691 }
Brian Osman13dddce2017-05-09 13:19:50 -0400692 }
693}
694
bsalomonf320e042015-02-17 15:09:34 -0800695void GrResourceCache::addToNonpurgeableArray(GrGpuResource* resource) {
696 int index = fNonpurgeableResources.count();
697 *fNonpurgeableResources.append() = resource;
698 *resource->cacheAccess().accessCacheIndex() = index;
699}
700
701void GrResourceCache::removeFromNonpurgeableArray(GrGpuResource* resource) {
702 int* index = resource->cacheAccess().accessCacheIndex();
703 // Fill the whole we will create in the array with the tail object, adjust its index, and
704 // then pop the array
705 GrGpuResource* tail = *(fNonpurgeableResources.end() - 1);
706 SkASSERT(fNonpurgeableResources[*index] == resource);
707 fNonpurgeableResources[*index] = tail;
708 *tail->cacheAccess().accessCacheIndex() = *index;
709 fNonpurgeableResources.pop();
710 SkDEBUGCODE(*index = -1);
711}
712
bsalomonddf30e62015-02-19 11:38:44 -0800713uint32_t GrResourceCache::getNextTimestamp() {
714 // If we wrap then all the existing resources will appear older than any resources that get
715 // a timestamp after the wrap.
716 if (0 == fTimestamp) {
717 int count = this->getResourceCount();
718 if (count) {
719 // Reset all the timestamps. We sort the resources by timestamp and then assign
720 // sequential timestamps beginning with 0. This is O(n*lg(n)) but it should be extremely
721 // rare.
722 SkTDArray<GrGpuResource*> sortedPurgeableResources;
723 sortedPurgeableResources.setReserve(fPurgeableQueue.count());
724
725 while (fPurgeableQueue.count()) {
726 *sortedPurgeableResources.append() = fPurgeableQueue.peek();
727 fPurgeableQueue.pop();
728 }
robertphillipsee843b22016-10-04 05:30:20 -0700729
bsalomone2e87f32016-09-22 12:42:11 -0700730 SkTQSort(fNonpurgeableResources.begin(), fNonpurgeableResources.end() - 1,
731 CompareTimestamp);
bsalomonddf30e62015-02-19 11:38:44 -0800732
733 // Pick resources out of the purgeable and non-purgeable arrays based on lowest
734 // timestamp and assign new timestamps.
735 int currP = 0;
736 int currNP = 0;
737 while (currP < sortedPurgeableResources.count() &&
mtklein56da0252015-11-16 11:16:23 -0800738 currNP < fNonpurgeableResources.count()) {
bsalomonddf30e62015-02-19 11:38:44 -0800739 uint32_t tsP = sortedPurgeableResources[currP]->cacheAccess().timestamp();
740 uint32_t tsNP = fNonpurgeableResources[currNP]->cacheAccess().timestamp();
741 SkASSERT(tsP != tsNP);
742 if (tsP < tsNP) {
743 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
744 } else {
745 // Correct the index in the nonpurgeable array stored on the resource post-sort.
746 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
747 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
748 }
749 }
750
751 // The above loop ended when we hit the end of one array. Finish the other one.
752 while (currP < sortedPurgeableResources.count()) {
753 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
754 }
755 while (currNP < fNonpurgeableResources.count()) {
756 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
757 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
758 }
759
760 // Rebuild the queue.
761 for (int i = 0; i < sortedPurgeableResources.count(); ++i) {
762 fPurgeableQueue.insert(sortedPurgeableResources[i]);
763 }
764
765 this->validate();
766 SkASSERT(count == this->getResourceCount());
767
768 // count should be the next timestamp we return.
769 SkASSERT(fTimestamp == SkToU32(count));
mtklein56da0252015-11-16 11:16:23 -0800770 }
bsalomonddf30e62015-02-19 11:38:44 -0800771 }
772 return fTimestamp++;
773}
774
ericrk0a5fa482015-09-15 14:16:10 -0700775void GrResourceCache::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
776 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
777 fNonpurgeableResources[i]->dumpMemoryStatistics(traceMemoryDump);
778 }
779 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
780 fPurgeableQueue.at(i)->dumpMemoryStatistics(traceMemoryDump);
781 }
782}
783
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500784#if GR_CACHE_STATS
785void GrResourceCache::getStats(Stats* stats) const {
786 stats->reset();
787
788 stats->fTotal = this->getResourceCount();
789 stats->fNumNonPurgeable = fNonpurgeableResources.count();
790 stats->fNumPurgeable = fPurgeableQueue.count();
791
792 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
793 stats->update(fNonpurgeableResources[i]);
794 }
795 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
796 stats->update(fPurgeableQueue.at(i));
797 }
798}
799
800#if GR_TEST_UTILS
801void GrResourceCache::dumpStats(SkString* out) const {
802 this->validate();
803
804 Stats stats;
805
806 this->getStats(&stats);
807
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500808 float byteUtilization = (100.f * fBudgetedBytes) / fMaxBytes;
809
Robert Phillipscf39f372019-09-03 10:29:20 -0400810 out->appendf("Budget: %d bytes\n", (int)fMaxBytes);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500811 out->appendf("\t\tEntry Count: current %d"
Robert Phillipscf39f372019-09-03 10:29:20 -0400812 " (%d budgeted, %d wrapped, %d locked, %d scratch), high %d\n",
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500813 stats.fTotal, fBudgetedCount, stats.fWrapped, stats.fNumNonPurgeable,
Robert Phillipscf39f372019-09-03 10:29:20 -0400814 stats.fScratch, fHighWaterCount);
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500815 out->appendf("\t\tEntry Bytes: current %d (budgeted %d, %.2g%% full, %d unbudgeted) high %d\n",
816 SkToInt(fBytes), SkToInt(fBudgetedBytes), byteUtilization,
817 SkToInt(stats.fUnbudgetedSize), SkToInt(fHighWaterBytes));
818}
819
820void GrResourceCache::dumpStatsKeyValuePairs(SkTArray<SkString>* keys,
821 SkTArray<double>* values) const {
822 this->validate();
823
824 Stats stats;
825 this->getStats(&stats);
826
827 keys->push_back(SkString("gpu_cache_purgable_entries")); values->push_back(stats.fNumPurgeable);
828}
829#endif
830
831#endif
832
bsalomon71cb0c22014-11-14 12:10:14 -0800833#ifdef SK_DEBUG
bsalomon0ea80f42015-02-11 10:49:59 -0800834void GrResourceCache::validate() const {
bsalomonc2f35b72015-01-23 07:19:22 -0800835 // Reduce the frequency of validations for large resource counts.
836 static SkRandom gRandom;
837 int mask = (SkNextPow2(fCount + 1) >> 5) - 1;
838 if (~mask && (gRandom.nextU() & mask)) {
839 return;
840 }
841
bsalomonf320e042015-02-17 15:09:34 -0800842 struct Stats {
843 size_t fBytes;
844 int fBudgetedCount;
845 size_t fBudgetedBytes;
846 int fLocked;
847 int fScratch;
848 int fCouldBeScratch;
849 int fContent;
850 const ScratchMap* fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800851 const UniqueHash* fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800852
bsalomonf320e042015-02-17 15:09:34 -0800853 Stats(const GrResourceCache* cache) {
854 memset(this, 0, sizeof(*this));
855 fScratchMap = &cache->fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800856 fUniqueHash = &cache->fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800857 }
858
bsalomonf320e042015-02-17 15:09:34 -0800859 void update(GrGpuResource* resource) {
860 fBytes += resource->gpuMemorySize();
bsalomondace19e2014-11-17 07:34:06 -0800861
Robert Phillipse1efd382019-08-21 10:07:10 -0400862 // No resource should ever have pendingIO any more
863 SkASSERT(!resource->internalHasPendingIO());
864
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