blob: c36634cc921c334aadb5416c5954ad7abc497fbc [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
bsalomon0ea80f42015-02-11 10:49:59 -08008#include "GrResourceCache.h"
Brian Salomon614c1a82018-12-19 15:42:06 -05009#include <atomic>
robertphillips28a838e2016-06-23 14:07:00 -070010#include "GrCaps.h"
bsalomon3582d3e2015-02-13 14:20:05 -080011#include "GrGpuResourceCacheAccess.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050012#include "GrProxyProvider.h"
Brian Salomon614c1a82018-12-19 15:42:06 -050013#include "GrSingleOwner.h"
Robert Phillipsae7d3f32017-09-21 08:26:08 -040014#include "GrTexture.h"
15#include "GrTextureProxyCacheAccess.h"
hendrikw876c3132015-03-04 10:33:49 -080016#include "GrTracing.h"
Brian Salomon876a0172019-03-08 11:12:14 -050017#include "SkExchange.h"
bsalomon71cb0c22014-11-14 12:10:14 -080018#include "SkGr.h"
19#include "SkMessageBus.h"
mtklein4e976072016-08-08 09:06:27 -070020#include "SkOpts.h"
Brian Osmane3f543c2018-11-02 12:52:10 -040021#include "SkRandom.h"
Brian Salomon614c1a82018-12-19 15:42:06 -050022#include "SkScopeExit.h"
bsalomonddf30e62015-02-19 11:38:44 -080023#include "SkTSort.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040024#include "SkTo.h"
bsalomon71cb0c22014-11-14 12:10:14 -080025
bsalomon8718aaf2015-02-19 07:24:21 -080026DECLARE_SKMESSAGEBUS_MESSAGE(GrUniqueKeyInvalidatedMessage);
bsalomon71cb0c22014-11-14 12:10:14 -080027
Brian Osman13dddce2017-05-09 13:19:50 -040028DECLARE_SKMESSAGEBUS_MESSAGE(GrGpuResourceFreedMessage);
29
Brian Salomon8f8995a2018-10-15 14:32:15 -040030#define ASSERT_SINGLE_OWNER \
31 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);)
32
bsalomon71cb0c22014-11-14 12:10:14 -080033//////////////////////////////////////////////////////////////////////////////
34
bsalomon7775c852014-12-30 12:50:52 -080035GrScratchKey::ResourceType GrScratchKey::GenerateResourceType() {
Mike Klein0ec1c572018-12-04 11:52:51 -050036 static std::atomic<int32_t> nextType{INHERITED::kInvalidDomain + 1};
bsalomonfe369ee2014-11-10 11:59:06 -080037
Mike Klein0ec1c572018-12-04 11:52:51 -050038 int32_t type = nextType++;
Ben Wagner9bc36fd2018-06-15 14:23:36 -040039 if (type > SkTo<int32_t>(UINT16_MAX)) {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040040 SK_ABORT("Too many Resource Types");
bsalomon71cb0c22014-11-14 12:10:14 -080041 }
42
43 return static_cast<ResourceType>(type);
44}
45
bsalomon8718aaf2015-02-19 07:24:21 -080046GrUniqueKey::Domain GrUniqueKey::GenerateDomain() {
Mike Klein0ec1c572018-12-04 11:52:51 -050047 static std::atomic<int32_t> nextDomain{INHERITED::kInvalidDomain + 1};
bsalomon7775c852014-12-30 12:50:52 -080048
Mike Klein0ec1c572018-12-04 11:52:51 -050049 int32_t domain = nextDomain++;
Ben Wagner397ee0e2018-06-15 15:13:26 -040050 if (domain > SkTo<int32_t>(UINT16_MAX)) {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040051 SK_ABORT("Too many GrUniqueKey Domains");
bsalomon7775c852014-12-30 12:50:52 -080052 }
bsalomon24db3b12015-01-23 04:24:04 -080053
54 return static_cast<Domain>(domain);
55}
bsalomon3f324322015-04-08 11:01:54 -070056
bsalomon24db3b12015-01-23 04:24:04 -080057uint32_t GrResourceKeyHash(const uint32_t* data, size_t size) {
mtklein4e976072016-08-08 09:06:27 -070058 return SkOpts::hash(data, size);
bsalomon7775c852014-12-30 12:50:52 -080059}
60
bsalomonfe369ee2014-11-10 11:59:06 -080061//////////////////////////////////////////////////////////////////////////////
62
bsalomon0ea80f42015-02-11 10:49:59 -080063class GrResourceCache::AutoValidate : ::SkNoncopyable {
bsalomon71cb0c22014-11-14 12:10:14 -080064public:
bsalomon0ea80f42015-02-11 10:49:59 -080065 AutoValidate(GrResourceCache* cache) : fCache(cache) { cache->validate(); }
bsalomon71cb0c22014-11-14 12:10:14 -080066 ~AutoValidate() { fCache->validate(); }
67private:
bsalomon0ea80f42015-02-11 10:49:59 -080068 GrResourceCache* fCache;
bsalomon71cb0c22014-11-14 12:10:14 -080069};
70
Brian Salomon876a0172019-03-08 11:12:14 -050071//////////////////////////////////////////////////////////////////////////////
72
73inline GrResourceCache::ResourceAwaitingUnref::ResourceAwaitingUnref() = default;
74
75inline GrResourceCache::ResourceAwaitingUnref::ResourceAwaitingUnref(GrGpuResource* resource)
76 : fResource(resource), fNumUnrefs(1) {}
77
78inline GrResourceCache::ResourceAwaitingUnref::ResourceAwaitingUnref(ResourceAwaitingUnref&& that) {
79 fResource = skstd::exchange(that.fResource, nullptr);
80 fNumUnrefs = skstd::exchange(that.fNumUnrefs, 0);
81}
82
83inline GrResourceCache::ResourceAwaitingUnref& GrResourceCache::ResourceAwaitingUnref::operator=(
84 ResourceAwaitingUnref&& that) {
85 fResource = skstd::exchange(that.fResource, nullptr);
86 fNumUnrefs = skstd::exchange(that.fNumUnrefs, 0);
87 return *this;
88}
89
90inline GrResourceCache::ResourceAwaitingUnref::~ResourceAwaitingUnref() {
91 if (fResource) {
92 for (int i = 0; i < fNumUnrefs; ++i) {
93 fResource->unref();
94 }
95 }
96}
97
98inline void GrResourceCache::ResourceAwaitingUnref::addRef() { ++fNumUnrefs; }
99
100inline void GrResourceCache::ResourceAwaitingUnref::unref() {
101 SkASSERT(fNumUnrefs > 0);
102 fResource->unref();
103 --fNumUnrefs;
104}
105
106inline bool GrResourceCache::ResourceAwaitingUnref::finished() { return !fNumUnrefs; }
107
108//////////////////////////////////////////////////////////////////////////////
robertphillipsee843b22016-10-04 05:30:20 -0700109
Brian Salomon8f8995a2018-10-15 14:32:15 -0400110GrResourceCache::GrResourceCache(const GrCaps* caps, GrSingleOwner* singleOwner,
111 uint32_t contextUniqueID)
Brian Salomon238069b2018-07-11 15:58:57 -0400112 : fProxyProvider(nullptr)
113 , fTimestamp(0)
114 , fMaxCount(kDefaultMaxCount)
115 , fMaxBytes(kDefaultMaxSize)
bsalomon71cb0c22014-11-14 12:10:14 -0800116#if GR_CACHE_STATS
Brian Salomon238069b2018-07-11 15:58:57 -0400117 , fHighWaterCount(0)
118 , fHighWaterBytes(0)
119 , fBudgetedHighWaterCount(0)
120 , fBudgetedHighWaterBytes(0)
bsalomon71cb0c22014-11-14 12:10:14 -0800121#endif
Brian Salomon238069b2018-07-11 15:58:57 -0400122 , fBytes(0)
123 , fBudgetedCount(0)
124 , fBudgetedBytes(0)
125 , fPurgeableBytes(0)
Brian Salomon238069b2018-07-11 15:58:57 -0400126 , fInvalidUniqueKeyInbox(contextUniqueID)
127 , fFreedGpuResourceInbox(contextUniqueID)
128 , fContextUniqueID(contextUniqueID)
Brian Salomon8f8995a2018-10-15 14:32:15 -0400129 , fSingleOwner(singleOwner)
Brian Salomon238069b2018-07-11 15:58:57 -0400130 , fPreferVRAMUseOverFlushes(caps->preferVRAMUseOverFlushes()) {
131 SkASSERT(contextUniqueID != SK_InvalidUniqueID);
bsalomonf320e042015-02-17 15:09:34 -0800132 SkDEBUGCODE(fCount = 0;)
halcanary96fcdcc2015-08-27 07:41:13 -0700133 SkDEBUGCODE(fNewlyPurgeableResourceForValidation = nullptr;)
bsalomon71cb0c22014-11-14 12:10:14 -0800134}
135
bsalomon0ea80f42015-02-11 10:49:59 -0800136GrResourceCache::~GrResourceCache() {
bsalomonc8dc1f72014-08-21 13:02:13 -0700137 this->releaseAll();
138}
139
Brian Salomon43b882b2018-09-07 16:29:14 -0400140void GrResourceCache::setLimits(int count, size_t bytes) {
bsalomon71cb0c22014-11-14 12:10:14 -0800141 fMaxCount = count;
142 fMaxBytes = bytes;
143 this->purgeAsNeeded();
144}
145
bsalomon0ea80f42015-02-11 10:49:59 -0800146void GrResourceCache::insertResource(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400147 ASSERT_SINGLE_OWNER
bsalomon49f085d2014-09-05 13:34:00 -0700148 SkASSERT(resource);
bsalomon16961262014-08-26 14:01:07 -0700149 SkASSERT(!this->isInCache(resource));
bsalomonf320e042015-02-17 15:09:34 -0800150 SkASSERT(!resource->wasDestroyed());
Brian Salomon614c1a82018-12-19 15:42:06 -0500151 SkASSERT(!resource->resourcePriv().isPurgeable());
bsalomonddf30e62015-02-19 11:38:44 -0800152
153 // We must set the timestamp before adding to the array in case the timestamp wraps and we wind
154 // up iterating over all the resources that already have timestamps.
155 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
156
bsalomonf320e042015-02-17 15:09:34 -0800157 this->addToNonpurgeableArray(resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800158
bsalomondace19e2014-11-17 07:34:06 -0800159 size_t size = resource->gpuMemorySize();
bsalomonf320e042015-02-17 15:09:34 -0800160 SkDEBUGCODE(++fCount;)
bsalomon84c8e622014-11-17 09:33:27 -0800161 fBytes += size;
bsalomon82b1d622014-11-14 13:59:57 -0800162#if GR_CACHE_STATS
bsalomonf320e042015-02-17 15:09:34 -0800163 fHighWaterCount = SkTMax(this->getResourceCount(), fHighWaterCount);
bsalomon82b1d622014-11-14 13:59:57 -0800164 fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes);
165#endif
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500166 if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
bsalomondace19e2014-11-17 07:34:06 -0800167 ++fBudgetedCount;
168 fBudgetedBytes += size;
Brian Osman39c08ac2017-07-26 09:36:09 -0400169 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800170 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomondace19e2014-11-17 07:34:06 -0800171#if GR_CACHE_STATS
172 fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount);
173 fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes);
174#endif
175 }
robertphillipsc4ed6842016-05-24 14:17:12 -0700176 if (resource->resourcePriv().getScratchKey().isValid() &&
177 !resource->getUniqueKey().isValid()) {
kkinnunen2e6055b2016-04-22 01:48:29 -0700178 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
bsalomon3582d3e2015-02-13 14:20:05 -0800179 fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
bsalomon744998e2014-08-28 09:54:34 -0700180 }
bsalomon9f2d1572015-02-17 11:47:40 -0800181
bsalomon71cb0c22014-11-14 12:10:14 -0800182 this->purgeAsNeeded();
bsalomonc8dc1f72014-08-21 13:02:13 -0700183}
184
bsalomon0ea80f42015-02-11 10:49:59 -0800185void GrResourceCache::removeResource(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400186 ASSERT_SINGLE_OWNER
bsalomon9f2d1572015-02-17 11:47:40 -0800187 this->validate();
bsalomon16961262014-08-26 14:01:07 -0700188 SkASSERT(this->isInCache(resource));
bsalomondace19e2014-11-17 07:34:06 -0800189
Derek Sollenbergeree479142017-05-24 11:41:33 -0400190 size_t size = resource->gpuMemorySize();
Brian Salomon614c1a82018-12-19 15:42:06 -0500191 if (resource->resourcePriv().isPurgeable()) {
bsalomon9f2d1572015-02-17 11:47:40 -0800192 fPurgeableQueue.remove(resource);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400193 fPurgeableBytes -= size;
bsalomonf320e042015-02-17 15:09:34 -0800194 } else {
195 this->removeFromNonpurgeableArray(resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800196 }
197
bsalomonf320e042015-02-17 15:09:34 -0800198 SkDEBUGCODE(--fCount;)
bsalomondace19e2014-11-17 07:34:06 -0800199 fBytes -= size;
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500200 if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
bsalomondace19e2014-11-17 07:34:06 -0800201 --fBudgetedCount;
202 fBudgetedBytes -= size;
Brian Osman39c08ac2017-07-26 09:36:09 -0400203 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800204 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomondace19e2014-11-17 07:34:06 -0800205 }
206
robertphillipsc4ed6842016-05-24 14:17:12 -0700207 if (resource->resourcePriv().getScratchKey().isValid() &&
208 !resource->getUniqueKey().isValid()) {
bsalomon3582d3e2015-02-13 14:20:05 -0800209 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
bsalomon744998e2014-08-28 09:54:34 -0700210 }
bsalomon8718aaf2015-02-19 07:24:21 -0800211 if (resource->getUniqueKey().isValid()) {
212 fUniqueHash.remove(resource->getUniqueKey());
bsalomon8b79d232014-11-10 10:19:06 -0800213 }
bsalomonb436ed62014-11-17 12:15:56 -0800214 this->validate();
bsalomonc8dc1f72014-08-21 13:02:13 -0700215}
216
bsalomon0ea80f42015-02-11 10:49:59 -0800217void GrResourceCache::abandonAll() {
bsalomon71cb0c22014-11-14 12:10:14 -0800218 AutoValidate av(this);
219
Brian Salomon876a0172019-03-08 11:12:14 -0500220 // We need to make sure to free any resources that were waiting on a free message but never
221 // received one.
222 fResourcesAwaitingUnref.reset();
Greg Danielb2acf0a2018-09-12 09:17:11 -0400223
bsalomonf320e042015-02-17 15:09:34 -0800224 while (fNonpurgeableResources.count()) {
225 GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
226 SkASSERT(!back->wasDestroyed());
227 back->cacheAccess().abandon();
bsalomonc8dc1f72014-08-21 13:02:13 -0700228 }
bsalomonf320e042015-02-17 15:09:34 -0800229
230 while (fPurgeableQueue.count()) {
231 GrGpuResource* top = fPurgeableQueue.peek();
232 SkASSERT(!top->wasDestroyed());
233 top->cacheAccess().abandon();
234 }
235
bsalomon744998e2014-08-28 09:54:34 -0700236 SkASSERT(!fScratchMap.count());
bsalomon8718aaf2015-02-19 07:24:21 -0800237 SkASSERT(!fUniqueHash.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700238 SkASSERT(!fCount);
bsalomonf320e042015-02-17 15:09:34 -0800239 SkASSERT(!this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800240 SkASSERT(!fBytes);
241 SkASSERT(!fBudgetedCount);
242 SkASSERT(!fBudgetedBytes);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400243 SkASSERT(!fPurgeableBytes);
Brian Salomon876a0172019-03-08 11:12:14 -0500244 SkASSERT(!fResourcesAwaitingUnref.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700245}
246
bsalomon0ea80f42015-02-11 10:49:59 -0800247void GrResourceCache::releaseAll() {
bsalomon71cb0c22014-11-14 12:10:14 -0800248 AutoValidate av(this);
249
Brian Osman13dddce2017-05-09 13:19:50 -0400250 this->processFreedGpuResources();
251
Greg Danielc27eb722018-08-10 09:48:08 -0400252 // We need to make sure to free any resources that were waiting on a free message but never
253 // received one.
Brian Salomon876a0172019-03-08 11:12:14 -0500254 fResourcesAwaitingUnref.reset();
Greg Danielc27eb722018-08-10 09:48:08 -0400255
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500256 SkASSERT(fProxyProvider); // better have called setProxyProvider
Robert Phillips3ec95732017-09-29 15:10:39 -0400257 // We must remove the uniqueKeys from the proxies here. While they possess a uniqueKey
258 // they also have a raw pointer back to this class (which is presumably going away)!
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500259 fProxyProvider->removeAllUniqueKeys();
Robert Phillips45a6f142017-09-29 09:49:41 -0400260
Brian Salomon614c1a82018-12-19 15:42:06 -0500261 while (fNonpurgeableResources.count()) {
bsalomonf320e042015-02-17 15:09:34 -0800262 GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
263 SkASSERT(!back->wasDestroyed());
264 back->cacheAccess().release();
bsalomonc8dc1f72014-08-21 13:02:13 -0700265 }
bsalomonf320e042015-02-17 15:09:34 -0800266
267 while (fPurgeableQueue.count()) {
268 GrGpuResource* top = fPurgeableQueue.peek();
269 SkASSERT(!top->wasDestroyed());
270 top->cacheAccess().release();
271 }
272
bsalomon744998e2014-08-28 09:54:34 -0700273 SkASSERT(!fScratchMap.count());
bsalomon8718aaf2015-02-19 07:24:21 -0800274 SkASSERT(!fUniqueHash.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700275 SkASSERT(!fCount);
bsalomonf320e042015-02-17 15:09:34 -0800276 SkASSERT(!this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800277 SkASSERT(!fBytes);
278 SkASSERT(!fBudgetedCount);
279 SkASSERT(!fBudgetedBytes);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400280 SkASSERT(!fPurgeableBytes);
Brian Salomon876a0172019-03-08 11:12:14 -0500281 SkASSERT(!fResourcesAwaitingUnref.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700282}
bsalomonbcf0a522014-10-08 08:40:09 -0700283
bsalomon0ea80f42015-02-11 10:49:59 -0800284class GrResourceCache::AvailableForScratchUse {
bsalomonbcf0a522014-10-08 08:40:09 -0700285public:
bsalomon000f8292014-10-15 19:04:14 -0700286 AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendingIO) { }
bsalomonbcf0a522014-10-08 08:40:09 -0700287
288 bool operator()(const GrGpuResource* resource) const {
robertphillipsc4ed6842016-05-24 14:17:12 -0700289 SkASSERT(!resource->getUniqueKey().isValid() &&
290 resource->resourcePriv().getScratchKey().isValid());
bsalomon12299ab2014-11-14 13:33:09 -0800291 if (resource->internalHasRef() || !resource->cacheAccess().isScratch()) {
bsalomon000f8292014-10-15 19:04:14 -0700292 return false;
bsalomonbcf0a522014-10-08 08:40:09 -0700293 }
bsalomon000f8292014-10-15 19:04:14 -0700294 return !fRejectPendingIO || !resource->internalHasPendingIO();
bsalomonbcf0a522014-10-08 08:40:09 -0700295 }
bsalomon1e2530b2014-10-09 09:57:18 -0700296
bsalomonbcf0a522014-10-08 08:40:09 -0700297private:
bsalomon000f8292014-10-15 19:04:14 -0700298 bool fRejectPendingIO;
bsalomonbcf0a522014-10-08 08:40:09 -0700299};
300
bsalomon0ea80f42015-02-11 10:49:59 -0800301GrGpuResource* GrResourceCache::findAndRefScratchResource(const GrScratchKey& scratchKey,
robertphillips6e83ac72015-08-13 05:19:14 -0700302 size_t resourceSize,
Chris Daltond004e0b2018-09-27 09:28:03 -0600303 ScratchFlags flags) {
bsalomon7775c852014-12-30 12:50:52 -0800304 SkASSERT(scratchKey.isValid());
robertphillipsee843b22016-10-04 05:30:20 -0700305
bsalomon71cb0c22014-11-14 12:10:14 -0800306 GrGpuResource* resource;
Chris Daltond004e0b2018-09-27 09:28:03 -0600307 if (flags & (ScratchFlags::kPreferNoPendingIO | ScratchFlags::kRequireNoPendingIO)) {
bsalomon71cb0c22014-11-14 12:10:14 -0800308 resource = fScratchMap.find(scratchKey, AvailableForScratchUse(true));
bsalomon000f8292014-10-15 19:04:14 -0700309 if (resource) {
bsalomon9f2d1572015-02-17 11:47:40 -0800310 this->refAndMakeResourceMRU(resource);
bsalomonb436ed62014-11-17 12:15:56 -0800311 this->validate();
312 return resource;
Chris Daltond004e0b2018-09-27 09:28:03 -0600313 } else if (flags & ScratchFlags::kRequireNoPendingIO) {
halcanary96fcdcc2015-08-27 07:41:13 -0700314 return nullptr;
bsalomon000f8292014-10-15 19:04:14 -0700315 }
robertphillips63926682015-08-20 09:39:02 -0700316 // We would prefer to consume more available VRAM rather than flushing
317 // immediately, but on ANGLE this can lead to starving of the GPU.
318 if (fPreferVRAMUseOverFlushes && this->wouldFit(resourceSize)) {
robertphillips6e83ac72015-08-13 05:19:14 -0700319 // kPrefer is specified, we didn't find a resource without pending io,
robertphillips63926682015-08-20 09:39:02 -0700320 // but there is still space in our budget for the resource so force
321 // the caller to allocate a new resource.
halcanary96fcdcc2015-08-27 07:41:13 -0700322 return nullptr;
robertphillips6e83ac72015-08-13 05:19:14 -0700323 }
bsalomon000f8292014-10-15 19:04:14 -0700324 }
bsalomon71cb0c22014-11-14 12:10:14 -0800325 resource = fScratchMap.find(scratchKey, AvailableForScratchUse(false));
326 if (resource) {
bsalomon9f2d1572015-02-17 11:47:40 -0800327 this->refAndMakeResourceMRU(resource);
bsalomonb436ed62014-11-17 12:15:56 -0800328 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800329 }
330 return resource;
bsalomonbcf0a522014-10-08 08:40:09 -0700331}
bsalomon8b79d232014-11-10 10:19:06 -0800332
bsalomon0ea80f42015-02-11 10:49:59 -0800333void GrResourceCache::willRemoveScratchKey(const GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400334 ASSERT_SINGLE_OWNER
bsalomon3582d3e2015-02-13 14:20:05 -0800335 SkASSERT(resource->resourcePriv().getScratchKey().isValid());
robertphillipsc4ed6842016-05-24 14:17:12 -0700336 if (!resource->getUniqueKey().isValid()) {
337 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
338 }
bsalomon10e23ca2014-11-25 05:52:06 -0800339}
340
bsalomonf99e9612015-02-19 08:24:16 -0800341void GrResourceCache::removeUniqueKey(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400342 ASSERT_SINGLE_OWNER
bsalomon3f324322015-04-08 11:01:54 -0700343 // Someone has a ref to this resource in order to have removed the key. When the ref count
344 // reaches zero we will get a ref cnt notification and figure out what to do with it.
bsalomonf99e9612015-02-19 08:24:16 -0800345 if (resource->getUniqueKey().isValid()) {
346 SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
347 fUniqueHash.remove(resource->getUniqueKey());
348 }
349 resource->cacheAccess().removeUniqueKey();
robertphillipsc4ed6842016-05-24 14:17:12 -0700350 if (resource->resourcePriv().getScratchKey().isValid()) {
351 fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
352 }
353
Brian Salomon9bc76d92019-01-24 12:18:33 -0500354 // Removing a unique key from a kUnbudgetedCacheable resource would make the resource
355 // require purging. However, the resource must be ref'ed to get here and therefore can't
356 // be purgeable. We'll purge it when the refs reach zero.
357 SkASSERT(!resource->resourcePriv().isPurgeable());
bsalomonf99e9612015-02-19 08:24:16 -0800358 this->validate();
bsalomon23e619c2015-02-06 11:54:28 -0800359}
360
bsalomonf99e9612015-02-19 08:24:16 -0800361void GrResourceCache::changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400362 ASSERT_SINGLE_OWNER
bsalomon8b79d232014-11-10 10:19:06 -0800363 SkASSERT(resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800364 SkASSERT(this->isInCache(resource));
bsalomon8b79d232014-11-10 10:19:06 -0800365
bsalomonf99e9612015-02-19 08:24:16 -0800366 // If another resource has the new key, remove its key then install the key on this resource.
367 if (newKey.isValid()) {
Greg Daniel0d537802017-09-08 11:44:14 -0400368 if (GrGpuResource* old = fUniqueHash.find(newKey)) {
369 // If the old resource using the key is purgeable and is unreachable, then remove it.
Brian Salomon614c1a82018-12-19 15:42:06 -0500370 if (!old->resourcePriv().getScratchKey().isValid() &&
371 old->resourcePriv().isPurgeable()) {
Greg Daniel0d537802017-09-08 11:44:14 -0400372 old->cacheAccess().release();
373 } else {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500374 // removeUniqueKey expects an external owner of the resource.
375 this->removeUniqueKey(sk_ref_sp(old).get());
Greg Daniel0d537802017-09-08 11:44:14 -0400376 }
377 }
378 SkASSERT(nullptr == fUniqueHash.find(newKey));
379
robertphillipsc4ed6842016-05-24 14:17:12 -0700380 // Remove the entry for this resource if it already has a unique key.
381 if (resource->getUniqueKey().isValid()) {
382 SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
383 fUniqueHash.remove(resource->getUniqueKey());
384 SkASSERT(nullptr == fUniqueHash.find(resource->getUniqueKey()));
385 } else {
386 // 'resource' didn't have a valid unique key before so it is switching sides. Remove it
387 // from the ScratchMap
388 if (resource->resourcePriv().getScratchKey().isValid()) {
389 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
390 }
391 }
392
bsalomonf99e9612015-02-19 08:24:16 -0800393 resource->cacheAccess().setUniqueKey(newKey);
394 fUniqueHash.add(resource);
395 } else {
robertphillipsc4ed6842016-05-24 14:17:12 -0700396 this->removeUniqueKey(resource);
bsalomonf99e9612015-02-19 08:24:16 -0800397 }
398
bsalomon71cb0c22014-11-14 12:10:14 -0800399 this->validate();
bsalomon8b79d232014-11-10 10:19:06 -0800400}
bsalomon71cb0c22014-11-14 12:10:14 -0800401
bsalomon9f2d1572015-02-17 11:47:40 -0800402void GrResourceCache::refAndMakeResourceMRU(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400403 ASSERT_SINGLE_OWNER
bsalomon71cb0c22014-11-14 12:10:14 -0800404 SkASSERT(resource);
405 SkASSERT(this->isInCache(resource));
bsalomonddf30e62015-02-19 11:38:44 -0800406
Brian Salomon614c1a82018-12-19 15:42:06 -0500407 if (resource->resourcePriv().isPurgeable()) {
bsalomon9f2d1572015-02-17 11:47:40 -0800408 // It's about to become unpurgeable.
Derek Sollenbergeree479142017-05-24 11:41:33 -0400409 fPurgeableBytes -= resource->gpuMemorySize();
bsalomon9f2d1572015-02-17 11:47:40 -0800410 fPurgeableQueue.remove(resource);
bsalomonf320e042015-02-17 15:09:34 -0800411 this->addToNonpurgeableArray(resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800412 }
413 resource->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);
bsalomon3f324322015-04-08 11:01:54 -0700440 }
441
442 if (!SkToBool(ResourceAccess::kAllCntsReachedZero_RefNotificationFlag & flags)) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500443 SkASSERT(!resource->resourcePriv().isPurgeable());
bsalomon3f324322015-04-08 11:01:54 -0700444 return;
445 }
446
Brian Salomon9bc76d92019-01-24 12:18:33 -0500447 if (!resource->resourcePriv().isPurgeable()) {
448 this->validate();
449 return;
450 }
451
bsalomonf320e042015-02-17 15:09:34 -0800452 this->removeFromNonpurgeableArray(resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800453 fPurgeableQueue.insert(resource);
Brian Salomon5e150852017-03-22 14:53:13 -0400454 resource->cacheAccess().setTimeWhenResourceBecomePurgeable();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400455 fPurgeableBytes += resource->gpuMemorySize();
bsalomon71cb0c22014-11-14 12:10:14 -0800456
Greg Daniel303e83e2018-09-10 14:10:19 -0400457 bool hasUniqueKey = resource->getUniqueKey().isValid();
458
Brian Salomon9bc76d92019-01-24 12:18:33 -0500459 GrBudgetedType budgetedType = resource->resourcePriv().budgetedType();
Brian Salomon614c1a82018-12-19 15:42:06 -0500460
Brian Salomon9bc76d92019-01-24 12:18:33 -0500461 if (budgetedType == GrBudgetedType::kBudgeted) {
462 // Purge the resource immediately if we're over budget
463 // Also purge if the resource has neither a valid scratch key nor a unique key.
464 bool hasKey = resource->resourcePriv().getScratchKey().isValid() || hasUniqueKey;
465 if (!this->overBudget() && hasKey) {
466 return;
467 }
468 } else {
469 // We keep unbudgeted resources with a unique key in the purgeable queue of the cache so
470 // they can be reused again by the image connected to the unique key.
471 if (hasUniqueKey && budgetedType == GrBudgetedType::kUnbudgetedCacheable) {
472 return;
473 }
474 // Check whether this resource could still be used as a scratch resource.
475 if (!resource->resourcePriv().refsWrappedObjects() &&
476 resource->resourcePriv().getScratchKey().isValid()) {
477 // We won't purge an existing resource to make room for this one.
478 if (fBudgetedCount < fMaxCount &&
479 fBudgetedBytes + resource->gpuMemorySize() <= fMaxBytes) {
480 resource->resourcePriv().makeBudgeted();
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500481 return;
482 }
bsalomonc2f35b72015-01-23 07:19:22 -0800483 }
bsalomonc2f35b72015-01-23 07:19:22 -0800484 }
Brian Salomon9bc76d92019-01-24 12:18:33 -0500485
bsalomonf320e042015-02-17 15:09:34 -0800486 SkDEBUGCODE(int beforeCount = this->getResourceCount();)
bsalomon9f2d1572015-02-17 11:47:40 -0800487 resource->cacheAccess().release();
488 // We should at least free this resource, perhaps dependent resources as well.
bsalomonf320e042015-02-17 15:09:34 -0800489 SkASSERT(this->getResourceCount() < beforeCount);
bsalomon71cb0c22014-11-14 12:10:14 -0800490 this->validate();
491}
492
bsalomon0ea80f42015-02-11 10:49:59 -0800493void GrResourceCache::didChangeBudgetStatus(GrGpuResource* resource) {
Brian Salomon8f8995a2018-10-15 14:32:15 -0400494 ASSERT_SINGLE_OWNER
bsalomon84c8e622014-11-17 09:33:27 -0800495 SkASSERT(resource);
496 SkASSERT(this->isInCache(resource));
497
498 size_t size = resource->gpuMemorySize();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500499 // Changing from BudgetedType::kUnbudgetedCacheable to another budgeted type could make
500 // resource become purgeable. However, we should never allow that transition. Wrapped
501 // resources are the only resources that can be in that state and they aren't allowed to
502 // transition from one budgeted state to another.
503 SkDEBUGCODE(bool wasPurgeable = resource->resourcePriv().isPurgeable());
504 if (resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
bsalomon84c8e622014-11-17 09:33:27 -0800505 ++fBudgetedCount;
506 fBudgetedBytes += size;
bsalomonafe30052015-01-16 07:32:33 -0800507#if GR_CACHE_STATS
508 fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes);
509 fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount);
510#endif
bsalomon84c8e622014-11-17 09:33:27 -0800511 this->purgeAsNeeded();
512 } else {
Brian Salomon9bc76d92019-01-24 12:18:33 -0500513 SkASSERT(resource->resourcePriv().budgetedType() != GrBudgetedType::kUnbudgetedCacheable);
bsalomon84c8e622014-11-17 09:33:27 -0800514 --fBudgetedCount;
515 fBudgetedBytes -= size;
516 }
Brian Salomon9bc76d92019-01-24 12:18:33 -0500517 SkASSERT(wasPurgeable == resource->resourcePriv().isPurgeable());
Brian Osman39c08ac2017-07-26 09:36:09 -0400518 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800519 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomon84c8e622014-11-17 09:33:27 -0800520
521 this->validate();
522}
523
robertphillipsee843b22016-10-04 05:30:20 -0700524void GrResourceCache::purgeAsNeeded() {
bsalomon3f324322015-04-08 11:01:54 -0700525 SkTArray<GrUniqueKeyInvalidatedMessage> invalidKeyMsgs;
526 fInvalidUniqueKeyInbox.poll(&invalidKeyMsgs);
527 if (invalidKeyMsgs.count()) {
Robert Phillips427966a2018-12-20 17:20:43 -0500528 SkASSERT(fProxyProvider);
529
530 for (int i = 0; i < invalidKeyMsgs.count(); ++i) {
531 fProxyProvider->processInvalidUniqueKey(invalidKeyMsgs[i].key(), nullptr,
532 GrProxyProvider::InvalidateGPUResource::kYes);
533 SkASSERT(!this->findAndRefUniqueResource(invalidKeyMsgs[i].key()));
534 }
bsalomon3f324322015-04-08 11:01:54 -0700535 }
bsalomon71cb0c22014-11-14 12:10:14 -0800536
Brian Osman13dddce2017-05-09 13:19:50 -0400537 this->processFreedGpuResources();
538
bsalomon3f324322015-04-08 11:01:54 -0700539 bool stillOverbudget = this->overBudget();
540 while (stillOverbudget && fPurgeableQueue.count()) {
robertphillipsee843b22016-10-04 05:30:20 -0700541 GrGpuResource* resource = fPurgeableQueue.peek();
Brian Salomon614c1a82018-12-19 15:42:06 -0500542 SkASSERT(resource->resourcePriv().isPurgeable());
bsalomon9f2d1572015-02-17 11:47:40 -0800543 resource->cacheAccess().release();
bsalomon3f324322015-04-08 11:01:54 -0700544 stillOverbudget = this->overBudget();
bsalomon9f2d1572015-02-17 11:47:40 -0800545 }
bsalomon71cb0c22014-11-14 12:10:14 -0800546
bsalomonb436ed62014-11-17 12:15:56 -0800547 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800548}
549
Robert Phillips6eba0632018-03-28 12:25:42 -0400550void GrResourceCache::purgeUnlockedResources(bool scratchResourcesOnly) {
551 if (!scratchResourcesOnly) {
552 // We could disable maintaining the heap property here, but it would add a lot of
553 // complexity. Moreover, this is rarely called.
554 while (fPurgeableQueue.count()) {
555 GrGpuResource* resource = fPurgeableQueue.peek();
Brian Salomon614c1a82018-12-19 15:42:06 -0500556 SkASSERT(resource->resourcePriv().isPurgeable());
Robert Phillips6eba0632018-03-28 12:25:42 -0400557 resource->cacheAccess().release();
558 }
559 } else {
560 // Sort the queue
561 fPurgeableQueue.sort();
562
563 // Make a list of the scratch resources to delete
564 SkTDArray<GrGpuResource*> scratchResources;
565 for (int i = 0; i < fPurgeableQueue.count(); i++) {
566 GrGpuResource* resource = fPurgeableQueue.at(i);
Brian Salomon614c1a82018-12-19 15:42:06 -0500567 SkASSERT(resource->resourcePriv().isPurgeable());
Robert Phillips6eba0632018-03-28 12:25:42 -0400568 if (!resource->getUniqueKey().isValid()) {
569 *scratchResources.append() = resource;
570 }
571 }
572
573 // Delete the scratch resources. This must be done as a separate pass
574 // to avoid messing up the sorted order of the queue
575 for (int i = 0; i < scratchResources.count(); i++) {
576 scratchResources.getAt(i)->cacheAccess().release();
577 }
bsalomon9f2d1572015-02-17 11:47:40 -0800578 }
bsalomon71cb0c22014-11-14 12:10:14 -0800579
bsalomonb436ed62014-11-17 12:15:56 -0800580 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800581}
582
Brian Salomon5e150852017-03-22 14:53:13 -0400583void GrResourceCache::purgeResourcesNotUsedSince(GrStdSteadyClock::time_point purgeTime) {
584 while (fPurgeableQueue.count()) {
585 const GrStdSteadyClock::time_point resourceTime =
586 fPurgeableQueue.peek()->cacheAccess().timeWhenResourceBecamePurgeable();
587 if (resourceTime >= purgeTime) {
588 // Resources were given both LRU timestamps and tagged with a frame number when
589 // they first became purgeable. The LRU timestamp won't change again until the
590 // resource is made non-purgeable again. So, at this point all the remaining
591 // resources in the timestamp-sorted queue will have a frame number >= to this
592 // one.
593 break;
594 }
595 GrGpuResource* resource = fPurgeableQueue.peek();
Brian Salomon614c1a82018-12-19 15:42:06 -0500596 SkASSERT(resource->resourcePriv().isPurgeable());
Brian Salomon5e150852017-03-22 14:53:13 -0400597 resource->cacheAccess().release();
598 }
599}
600
Derek Sollenberger5480a182017-05-25 16:43:59 -0400601void GrResourceCache::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
602
603 const size_t tmpByteBudget = SkTMax((size_t)0, fBytes - bytesToPurge);
604 bool stillOverbudget = tmpByteBudget < fBytes;
605
606 if (preferScratchResources && bytesToPurge < fPurgeableBytes) {
607 // Sort the queue
608 fPurgeableQueue.sort();
609
610 // Make a list of the scratch resources to delete
611 SkTDArray<GrGpuResource*> scratchResources;
612 size_t scratchByteCount = 0;
613 for (int i = 0; i < fPurgeableQueue.count() && stillOverbudget; i++) {
614 GrGpuResource* resource = fPurgeableQueue.at(i);
Brian Salomon614c1a82018-12-19 15:42:06 -0500615 SkASSERT(resource->resourcePriv().isPurgeable());
Derek Sollenberger5480a182017-05-25 16:43:59 -0400616 if (!resource->getUniqueKey().isValid()) {
617 *scratchResources.append() = resource;
618 scratchByteCount += resource->gpuMemorySize();
619 stillOverbudget = tmpByteBudget < fBytes - scratchByteCount;
620 }
621 }
622
623 // Delete the scratch resources. This must be done as a separate pass
624 // to avoid messing up the sorted order of the queue
625 for (int i = 0; i < scratchResources.count(); i++) {
626 scratchResources.getAt(i)->cacheAccess().release();
627 }
628 stillOverbudget = tmpByteBudget < fBytes;
629
630 this->validate();
631 }
632
633 // Purge any remaining resources in LRU order
634 if (stillOverbudget) {
635 const size_t cachedByteCount = fMaxBytes;
636 fMaxBytes = tmpByteBudget;
637 this->purgeAsNeeded();
638 fMaxBytes = cachedByteCount;
639 }
640}
641
Brian Salomon876a0172019-03-08 11:12:14 -0500642void GrResourceCache::insertDelayedResourceUnref(GrGpuResource* resource) {
Brian Osman13dddce2017-05-09 13:19:50 -0400643 resource->ref();
Brian Salomon876a0172019-03-08 11:12:14 -0500644 uint32_t id = resource->uniqueID().asUInt();
645 if (auto* data = fResourcesAwaitingUnref.find(id)) {
646 data->addRef();
647 } else {
648 fResourcesAwaitingUnref.set(id, {resource});
649 }
Brian Osman13dddce2017-05-09 13:19:50 -0400650}
651
652void GrResourceCache::processFreedGpuResources() {
653 SkTArray<GrGpuResourceFreedMessage> msgs;
654 fFreedGpuResourceInbox.poll(&msgs);
655 for (int i = 0; i < msgs.count(); ++i) {
Brian Salomon238069b2018-07-11 15:58:57 -0400656 SkASSERT(msgs[i].fOwningUniqueID == fContextUniqueID);
Brian Salomon876a0172019-03-08 11:12:14 -0500657 uint32_t id = msgs[i].fResource->uniqueID().asUInt();
658 ResourceAwaitingUnref* info = fResourcesAwaitingUnref.find(id);
Greg Danielb2acf0a2018-09-12 09:17:11 -0400659 // If we called release or abandon on the GrContext we will have already released our ref on
660 // the GrGpuResource. If then the message arrives before the actual GrContext gets destroyed
661 // we will try to process the message when we destroy the GrContext. This protects us from
662 // trying to unref the resource twice.
Brian Salomon876a0172019-03-08 11:12:14 -0500663 if (info) {
664 info->unref();
665 if (info->finished()) {
666 fResourcesAwaitingUnref.remove(id);
667 }
Greg Danielb2acf0a2018-09-12 09:17:11 -0400668 }
Brian Osman13dddce2017-05-09 13:19:50 -0400669 }
670}
671
bsalomonf320e042015-02-17 15:09:34 -0800672void GrResourceCache::addToNonpurgeableArray(GrGpuResource* resource) {
673 int index = fNonpurgeableResources.count();
674 *fNonpurgeableResources.append() = resource;
675 *resource->cacheAccess().accessCacheIndex() = index;
676}
677
678void GrResourceCache::removeFromNonpurgeableArray(GrGpuResource* resource) {
679 int* index = resource->cacheAccess().accessCacheIndex();
680 // Fill the whole we will create in the array with the tail object, adjust its index, and
681 // then pop the array
682 GrGpuResource* tail = *(fNonpurgeableResources.end() - 1);
683 SkASSERT(fNonpurgeableResources[*index] == resource);
684 fNonpurgeableResources[*index] = tail;
685 *tail->cacheAccess().accessCacheIndex() = *index;
686 fNonpurgeableResources.pop();
687 SkDEBUGCODE(*index = -1);
688}
689
bsalomonddf30e62015-02-19 11:38:44 -0800690uint32_t GrResourceCache::getNextTimestamp() {
691 // If we wrap then all the existing resources will appear older than any resources that get
692 // a timestamp after the wrap.
693 if (0 == fTimestamp) {
694 int count = this->getResourceCount();
695 if (count) {
696 // Reset all the timestamps. We sort the resources by timestamp and then assign
697 // sequential timestamps beginning with 0. This is O(n*lg(n)) but it should be extremely
698 // rare.
699 SkTDArray<GrGpuResource*> sortedPurgeableResources;
700 sortedPurgeableResources.setReserve(fPurgeableQueue.count());
701
702 while (fPurgeableQueue.count()) {
703 *sortedPurgeableResources.append() = fPurgeableQueue.peek();
704 fPurgeableQueue.pop();
705 }
robertphillipsee843b22016-10-04 05:30:20 -0700706
bsalomone2e87f32016-09-22 12:42:11 -0700707 SkTQSort(fNonpurgeableResources.begin(), fNonpurgeableResources.end() - 1,
708 CompareTimestamp);
bsalomonddf30e62015-02-19 11:38:44 -0800709
710 // Pick resources out of the purgeable and non-purgeable arrays based on lowest
711 // timestamp and assign new timestamps.
712 int currP = 0;
713 int currNP = 0;
714 while (currP < sortedPurgeableResources.count() &&
mtklein56da0252015-11-16 11:16:23 -0800715 currNP < fNonpurgeableResources.count()) {
bsalomonddf30e62015-02-19 11:38:44 -0800716 uint32_t tsP = sortedPurgeableResources[currP]->cacheAccess().timestamp();
717 uint32_t tsNP = fNonpurgeableResources[currNP]->cacheAccess().timestamp();
718 SkASSERT(tsP != tsNP);
719 if (tsP < tsNP) {
720 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
721 } else {
722 // Correct the index in the nonpurgeable array stored on the resource post-sort.
723 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
724 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
725 }
726 }
727
728 // The above loop ended when we hit the end of one array. Finish the other one.
729 while (currP < sortedPurgeableResources.count()) {
730 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
731 }
732 while (currNP < fNonpurgeableResources.count()) {
733 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
734 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
735 }
736
737 // Rebuild the queue.
738 for (int i = 0; i < sortedPurgeableResources.count(); ++i) {
739 fPurgeableQueue.insert(sortedPurgeableResources[i]);
740 }
741
742 this->validate();
743 SkASSERT(count == this->getResourceCount());
744
745 // count should be the next timestamp we return.
746 SkASSERT(fTimestamp == SkToU32(count));
mtklein56da0252015-11-16 11:16:23 -0800747 }
bsalomonddf30e62015-02-19 11:38:44 -0800748 }
749 return fTimestamp++;
750}
751
ericrk0a5fa482015-09-15 14:16:10 -0700752void GrResourceCache::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
753 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
754 fNonpurgeableResources[i]->dumpMemoryStatistics(traceMemoryDump);
755 }
756 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
757 fPurgeableQueue.at(i)->dumpMemoryStatistics(traceMemoryDump);
758 }
759}
760
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500761#if GR_CACHE_STATS
762void GrResourceCache::getStats(Stats* stats) const {
763 stats->reset();
764
765 stats->fTotal = this->getResourceCount();
766 stats->fNumNonPurgeable = fNonpurgeableResources.count();
767 stats->fNumPurgeable = fPurgeableQueue.count();
768
769 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
770 stats->update(fNonpurgeableResources[i]);
771 }
772 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
773 stats->update(fPurgeableQueue.at(i));
774 }
775}
776
777#if GR_TEST_UTILS
778void GrResourceCache::dumpStats(SkString* out) const {
779 this->validate();
780
781 Stats stats;
782
783 this->getStats(&stats);
784
785 float countUtilization = (100.f * fBudgetedCount) / fMaxCount;
786 float byteUtilization = (100.f * fBudgetedBytes) / fMaxBytes;
787
788 out->appendf("Budget: %d items %d bytes\n", fMaxCount, (int)fMaxBytes);
789 out->appendf("\t\tEntry Count: current %d"
790 " (%d budgeted, %d wrapped, %d locked, %d scratch %.2g%% full), high %d\n",
791 stats.fTotal, fBudgetedCount, stats.fWrapped, stats.fNumNonPurgeable,
792 stats.fScratch, countUtilization, fHighWaterCount);
793 out->appendf("\t\tEntry Bytes: current %d (budgeted %d, %.2g%% full, %d unbudgeted) high %d\n",
794 SkToInt(fBytes), SkToInt(fBudgetedBytes), byteUtilization,
795 SkToInt(stats.fUnbudgetedSize), SkToInt(fHighWaterBytes));
796}
797
798void GrResourceCache::dumpStatsKeyValuePairs(SkTArray<SkString>* keys,
799 SkTArray<double>* values) const {
800 this->validate();
801
802 Stats stats;
803 this->getStats(&stats);
804
805 keys->push_back(SkString("gpu_cache_purgable_entries")); values->push_back(stats.fNumPurgeable);
806}
807#endif
808
809#endif
810
bsalomon71cb0c22014-11-14 12:10:14 -0800811#ifdef SK_DEBUG
bsalomon0ea80f42015-02-11 10:49:59 -0800812void GrResourceCache::validate() const {
bsalomonc2f35b72015-01-23 07:19:22 -0800813 // Reduce the frequency of validations for large resource counts.
814 static SkRandom gRandom;
815 int mask = (SkNextPow2(fCount + 1) >> 5) - 1;
816 if (~mask && (gRandom.nextU() & mask)) {
817 return;
818 }
819
bsalomonf320e042015-02-17 15:09:34 -0800820 struct Stats {
821 size_t fBytes;
822 int fBudgetedCount;
823 size_t fBudgetedBytes;
824 int fLocked;
825 int fScratch;
826 int fCouldBeScratch;
827 int fContent;
828 const ScratchMap* fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800829 const UniqueHash* fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800830
bsalomonf320e042015-02-17 15:09:34 -0800831 Stats(const GrResourceCache* cache) {
832 memset(this, 0, sizeof(*this));
833 fScratchMap = &cache->fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800834 fUniqueHash = &cache->fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800835 }
836
bsalomonf320e042015-02-17 15:09:34 -0800837 void update(GrGpuResource* resource) {
838 fBytes += resource->gpuMemorySize();
bsalomondace19e2014-11-17 07:34:06 -0800839
Brian Salomon614c1a82018-12-19 15:42:06 -0500840 if (!resource->resourcePriv().isPurgeable()) {
bsalomonf320e042015-02-17 15:09:34 -0800841 ++fLocked;
842 }
bsalomon9f2d1572015-02-17 11:47:40 -0800843
robertphillipsc4ed6842016-05-24 14:17:12 -0700844 const GrScratchKey& scratchKey = resource->resourcePriv().getScratchKey();
845 const GrUniqueKey& uniqueKey = resource->getUniqueKey();
846
bsalomonf320e042015-02-17 15:09:34 -0800847 if (resource->cacheAccess().isScratch()) {
robertphillipsc4ed6842016-05-24 14:17:12 -0700848 SkASSERT(!uniqueKey.isValid());
bsalomonf320e042015-02-17 15:09:34 -0800849 ++fScratch;
robertphillipsc4ed6842016-05-24 14:17:12 -0700850 SkASSERT(fScratchMap->countForKey(scratchKey));
kkinnunen2e6055b2016-04-22 01:48:29 -0700851 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
robertphillipsc4ed6842016-05-24 14:17:12 -0700852 } else if (scratchKey.isValid()) {
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500853 SkASSERT(GrBudgetedType::kBudgeted != resource->resourcePriv().budgetedType() ||
robertphillipsc4ed6842016-05-24 14:17:12 -0700854 uniqueKey.isValid());
855 if (!uniqueKey.isValid()) {
mtklein4e976072016-08-08 09:06:27 -0700856 ++fCouldBeScratch;
robertphillipsc4ed6842016-05-24 14:17:12 -0700857 SkASSERT(fScratchMap->countForKey(scratchKey));
858 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700859 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
bsalomonf320e042015-02-17 15:09:34 -0800860 }
bsalomon8718aaf2015-02-19 07:24:21 -0800861 if (uniqueKey.isValid()) {
bsalomonf320e042015-02-17 15:09:34 -0800862 ++fContent;
bsalomon8718aaf2015-02-19 07:24:21 -0800863 SkASSERT(fUniqueHash->find(uniqueKey) == resource);
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500864 SkASSERT(GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType() ||
Brian Osman0562eb92017-05-08 11:16:39 -0400865 resource->resourcePriv().refsWrappedObjects());
robertphillipsc4ed6842016-05-24 14:17:12 -0700866
867 if (scratchKey.isValid()) {
868 SkASSERT(!fScratchMap->has(resource, scratchKey));
869 }
bsalomonf320e042015-02-17 15:09:34 -0800870 }
871
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500872 if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
bsalomonf320e042015-02-17 15:09:34 -0800873 ++fBudgetedCount;
874 fBudgetedBytes += resource->gpuMemorySize();
875 }
bsalomon9f2d1572015-02-17 11:47:40 -0800876 }
bsalomonf320e042015-02-17 15:09:34 -0800877 };
878
robertphillipsc4ed6842016-05-24 14:17:12 -0700879 {
880 ScratchMap::ConstIter iter(&fScratchMap);
881
882 int count = 0;
883 for ( ; !iter.done(); ++iter) {
884 const GrGpuResource* resource = *iter;
885 SkASSERT(resource->resourcePriv().getScratchKey().isValid());
886 SkASSERT(!resource->getUniqueKey().isValid());
887 count++;
888 }
889 SkASSERT(count == fScratchMap.count()); // ensure the iterator is working correctly
890 }
891
bsalomonf320e042015-02-17 15:09:34 -0800892 Stats stats(this);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400893 size_t purgeableBytes = 0;
bsalomonf320e042015-02-17 15:09:34 -0800894
895 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500896 SkASSERT(!fNonpurgeableResources[i]->resourcePriv().isPurgeable() ||
bsalomon3f324322015-04-08 11:01:54 -0700897 fNewlyPurgeableResourceForValidation == fNonpurgeableResources[i]);
bsalomonf320e042015-02-17 15:09:34 -0800898 SkASSERT(*fNonpurgeableResources[i]->cacheAccess().accessCacheIndex() == i);
899 SkASSERT(!fNonpurgeableResources[i]->wasDestroyed());
900 stats.update(fNonpurgeableResources[i]);
bsalomon71cb0c22014-11-14 12:10:14 -0800901 }
bsalomon9f2d1572015-02-17 11:47:40 -0800902 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500903 SkASSERT(fPurgeableQueue.at(i)->resourcePriv().isPurgeable());
bsalomonf320e042015-02-17 15:09:34 -0800904 SkASSERT(*fPurgeableQueue.at(i)->cacheAccess().accessCacheIndex() == i);
905 SkASSERT(!fPurgeableQueue.at(i)->wasDestroyed());
906 stats.update(fPurgeableQueue.at(i));
Derek Sollenbergeree479142017-05-24 11:41:33 -0400907 purgeableBytes += fPurgeableQueue.at(i)->gpuMemorySize();
bsalomon9f2d1572015-02-17 11:47:40 -0800908 }
909
bsalomonf320e042015-02-17 15:09:34 -0800910 SkASSERT(fCount == this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800911 SkASSERT(fBudgetedCount <= fCount);
bsalomonf320e042015-02-17 15:09:34 -0800912 SkASSERT(fBudgetedBytes <= fBytes);
913 SkASSERT(stats.fBytes == fBytes);
914 SkASSERT(stats.fBudgetedBytes == fBudgetedBytes);
915 SkASSERT(stats.fBudgetedCount == fBudgetedCount);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400916 SkASSERT(purgeableBytes == fPurgeableBytes);
bsalomon71cb0c22014-11-14 12:10:14 -0800917#if GR_CACHE_STATS
bsalomondace19e2014-11-17 07:34:06 -0800918 SkASSERT(fBudgetedHighWaterCount <= fHighWaterCount);
919 SkASSERT(fBudgetedHighWaterBytes <= fHighWaterBytes);
bsalomonf320e042015-02-17 15:09:34 -0800920 SkASSERT(fBytes <= fHighWaterBytes);
921 SkASSERT(fCount <= fHighWaterCount);
922 SkASSERT(fBudgetedBytes <= fBudgetedHighWaterBytes);
923 SkASSERT(fBudgetedCount <= fBudgetedHighWaterCount);
bsalomon71cb0c22014-11-14 12:10:14 -0800924#endif
bsalomon8718aaf2015-02-19 07:24:21 -0800925 SkASSERT(stats.fContent == fUniqueHash.count());
bsalomonf320e042015-02-17 15:09:34 -0800926 SkASSERT(stats.fScratch + stats.fCouldBeScratch == fScratchMap.count());
bsalomon71cb0c22014-11-14 12:10:14 -0800927
bsalomon3f324322015-04-08 11:01:54 -0700928 // This assertion is not currently valid because we can be in recursive notifyCntReachedZero()
bsalomon12299ab2014-11-14 13:33:09 -0800929 // calls. This will be fixed when subresource registration is explicit.
bsalomondace19e2014-11-17 07:34:06 -0800930 // bool overBudget = budgetedBytes > fMaxBytes || budgetedCount > fMaxCount;
bsalomon12299ab2014-11-14 13:33:09 -0800931 // SkASSERT(!overBudget || locked == count || fPurging);
bsalomon71cb0c22014-11-14 12:10:14 -0800932}
bsalomonf320e042015-02-17 15:09:34 -0800933
934bool GrResourceCache::isInCache(const GrGpuResource* resource) const {
935 int index = *resource->cacheAccess().accessCacheIndex();
936 if (index < 0) {
937 return false;
938 }
939 if (index < fPurgeableQueue.count() && fPurgeableQueue.at(index) == resource) {
940 return true;
941 }
942 if (index < fNonpurgeableResources.count() && fNonpurgeableResources[index] == resource) {
943 return true;
944 }
945 SkDEBUGFAIL("Resource index should be -1 or the resource should be in the cache.");
946 return false;
947}
948
bsalomon71cb0c22014-11-14 12:10:14 -0800949#endif