blob: e1706a34833624a3c5b712ca706168945039a071 [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#ifndef GrResourceCache_DEFINED
9#define GrResourceCache_DEFINED
bsalomonc8dc1f72014-08-21 13:02:13 -070010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkRefCnt.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/private/GrResourceKey.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/private/SkTArray.h"
14#include "include/private/SkTHash.h"
Ben Wagner21bca282019-05-15 10:15:52 -040015#include "src/core/SkMessageBus.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/core/SkTDPQueue.h"
Ben Wagner729a23f2019-05-17 16:29:34 -040017#include "src/core/SkTInternalLList.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/core/SkTMultiMap.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000019#include "src/gpu/GrGpuResource.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrGpuResourceCacheAccess.h"
21#include "src/gpu/GrGpuResourcePriv.h"
bsalomonc8dc1f72014-08-21 13:02:13 -070022
robertphillips63926682015-08-20 09:39:02 -070023class GrCaps;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050024class GrProxyProvider;
mtkleinb9eb4ac2015-02-02 18:26:03 -080025class SkString;
ericrk0a5fa482015-09-15 14:16:10 -070026class SkTraceMemoryDump;
Brian Salomon8f8995a2018-10-15 14:32:15 -040027class GrSingleOwner;
Greg Daniel7c902112020-03-06 13:07:10 -050028class GrTexture;
Robert Phillipsd464feb2020-10-08 11:00:02 -040029class GrThreadSafeCache;
mtkleinb9eb4ac2015-02-02 18:26:03 -080030
Robert Phillipsddc21482019-10-16 14:30:09 -040031struct GrTextureFreedMessage {
32 GrTexture* fTexture;
Brian Osman13dddce2017-05-09 13:19:50 -040033 uint32_t fOwningUniqueID;
34};
35
Chris Dalton9a986cf2018-10-18 15:27:59 -060036static inline bool SkShouldPostMessageToBus(
Robert Phillipsddc21482019-10-16 14:30:09 -040037 const GrTextureFreedMessage& msg, uint32_t msgBusUniqueID) {
Chris Dalton9a986cf2018-10-18 15:27:59 -060038 // The inbox's ID is the unique ID of the owning GrContext.
39 return msgBusUniqueID == msg.fOwningUniqueID;
40}
41
bsalomonc8dc1f72014-08-21 13:02:13 -070042/**
bsalomon71cb0c22014-11-14 12:10:14 -080043 * Manages the lifetime of all GrGpuResource instances.
44 *
45 * Resources may have optionally have two types of keys:
46 * 1) A scratch key. This is for resources whose allocations are cached but not their contents.
47 * Multiple resources can share the same scratch key. This is so a caller can have two
bsalomon84c8e622014-11-17 09:33:27 -080048 * resource instances with the same properties (e.g. multipass rendering that ping-pongs
bsalomon8718aaf2015-02-19 07:24:21 -080049 * between two temporary surfaces). The scratch key is set at resource creation time and
bsalomon71cb0c22014-11-14 12:10:14 -080050 * should never change. Resources need not have a scratch key.
bsalomon8718aaf2015-02-19 07:24:21 -080051 * 2) A unique key. This key's meaning is specific to the domain that created the key. Only one
bsalomonf99e9612015-02-19 08:24:16 -080052 * resource may have a given unique key. The unique key can be set, cleared, or changed
53 * anytime after resource creation.
54 *
bsalomon8718aaf2015-02-19 07:24:21 -080055 * A unique key always takes precedence over a scratch key when a resource has both types of keys.
bsalomon71cb0c22014-11-14 12:10:14 -080056 * If a resource has neither key type then it will be deleted as soon as the last reference to it
bsalomon8718aaf2015-02-19 07:24:21 -080057 * is dropped.
bsalomonc8dc1f72014-08-21 13:02:13 -070058 */
bsalomon0ea80f42015-02-11 10:49:59 -080059class GrResourceCache {
bsalomonc8dc1f72014-08-21 13:02:13 -070060public:
Brian Salomonbe1084b2021-01-26 13:29:30 -050061 GrResourceCache(GrSingleOwner* owner, uint32_t contextUniqueID);
bsalomon0ea80f42015-02-11 10:49:59 -080062 ~GrResourceCache();
bsalomonc8dc1f72014-08-21 13:02:13 -070063
bsalomon3f324322015-04-08 11:01:54 -070064 // Default maximum number of bytes of gpu memory of budgeted resources in the cache.
Chris Dalton062d2d92020-05-27 14:04:15 -060065 static const size_t kDefaultMaxSize = 256 * (1 << 20);
bsalomon3f324322015-04-08 11:01:54 -070066
bsalomon71cb0c22014-11-14 12:10:14 -080067 /** Used to access functionality needed by GrGpuResource for lifetime management. */
68 class ResourceAccess;
69 ResourceAccess resourceAccess();
bsalomonc8dc1f72014-08-21 13:02:13 -070070
Brian Salomon238069b2018-07-11 15:58:57 -040071 /** Unique ID of the owning GrContext. */
72 uint32_t contextUniqueID() const { return fContextUniqueID; }
73
Robert Phillipscf39f372019-09-03 10:29:20 -040074 /** Sets the max gpu memory byte size of the cache. */
75 void setLimit(size_t bytes);
bsalomonc8dc1f72014-08-21 13:02:13 -070076
bsalomon71cb0c22014-11-14 12:10:14 -080077 /**
bsalomondace19e2014-11-17 07:34:06 -080078 * Returns the number of resources.
bsalomon71cb0c22014-11-14 12:10:14 -080079 */
bsalomonf320e042015-02-17 15:09:34 -080080 int getResourceCount() const {
81 return fPurgeableQueue.count() + fNonpurgeableResources.count();
82 }
bsalomon8b79d232014-11-10 10:19:06 -080083
bsalomon71cb0c22014-11-14 12:10:14 -080084 /**
bsalomondace19e2014-11-17 07:34:06 -080085 * Returns the number of resources that count against the budget.
86 */
87 int getBudgetedResourceCount() const { return fBudgetedCount; }
88
89 /**
90 * Returns the number of bytes consumed by resources.
bsalomon71cb0c22014-11-14 12:10:14 -080091 */
92 size_t getResourceBytes() const { return fBytes; }
93
94 /**
Robert Phillips12d06a32020-09-16 12:31:34 -040095 * Returns the number of bytes held by unlocked resources which are available for purging.
Derek Sollenbergeree479142017-05-24 11:41:33 -040096 */
97 size_t getPurgeableBytes() const { return fPurgeableBytes; }
98
99 /**
bsalomondace19e2014-11-17 07:34:06 -0800100 * Returns the number of bytes consumed by budgeted resources.
101 */
102 size_t getBudgetedResourceBytes() const { return fBudgetedBytes; }
103
104 /**
bsalomon71cb0c22014-11-14 12:10:14 -0800105 * Returns the number of bytes consumed by cached resources.
106 */
107 size_t getMaxResourceBytes() const { return fMaxBytes; }
108
109 /**
110 * Abandons the backend API resources owned by all GrGpuResource objects and removes them from
111 * the cache.
112 */
bsalomonc8dc1f72014-08-21 13:02:13 -0700113 void abandonAll();
114
bsalomon71cb0c22014-11-14 12:10:14 -0800115 /**
116 * Releases the backend API resources owned by all GrGpuResource objects and removes them from
117 * the cache.
118 */
bsalomonc8dc1f72014-08-21 13:02:13 -0700119 void releaseAll();
120
bsalomon71cb0c22014-11-14 12:10:14 -0800121 /**
122 * Find a resource that matches a scratch key.
123 */
Robert Phillipsaee18c92019-09-06 11:48:27 -0400124 GrGpuResource* findAndRefScratchResource(const GrScratchKey& scratchKey);
halcanary9d524f22016-03-29 09:03:52 -0700125
bsalomon8b79d232014-11-10 10:19:06 -0800126#ifdef SK_DEBUG
127 // This is not particularly fast and only used for validation, so debug only.
bsalomon7775c852014-12-30 12:50:52 -0800128 int countScratchEntriesForKey(const GrScratchKey& scratchKey) const {
bsalomon8b79d232014-11-10 10:19:06 -0800129 return fScratchMap.countForKey(scratchKey);
130 }
131#endif
132
bsalomon71cb0c22014-11-14 12:10:14 -0800133 /**
bsalomon8718aaf2015-02-19 07:24:21 -0800134 * Find a resource that matches a unique key.
bsalomon71cb0c22014-11-14 12:10:14 -0800135 */
robertphillipsee843b22016-10-04 05:30:20 -0700136 GrGpuResource* findAndRefUniqueResource(const GrUniqueKey& key) {
137 GrGpuResource* resource = fUniqueHash.find(key);
138 if (resource) {
139 this->refAndMakeResourceMRU(resource);
140 }
141 return resource;
142 }
bsalomon8b79d232014-11-10 10:19:06 -0800143
Greg Danielcd871402017-09-26 12:49:26 -0400144 /**
bsalomon8718aaf2015-02-19 07:24:21 -0800145 * Query whether a unique key exists in the cache.
bsalomon71cb0c22014-11-14 12:10:14 -0800146 */
bsalomon8718aaf2015-02-19 07:24:21 -0800147 bool hasUniqueKey(const GrUniqueKey& key) const {
148 return SkToBool(fUniqueHash.find(key));
bsalomon8b79d232014-11-10 10:19:06 -0800149 }
bsalomonbcf0a522014-10-08 08:40:09 -0700150
bsalomon8718aaf2015-02-19 07:24:21 -0800151 /** Purges resources to become under budget and processes resources with invalidated unique
bsalomon23e619c2015-02-06 11:54:28 -0800152 keys. */
robertphillipsee843b22016-10-04 05:30:20 -0700153 void purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800154
bsalomon71cb0c22014-11-14 12:10:14 -0800155 /** Purges all resources that don't have external owners. */
Robert Phillips6eba0632018-03-28 12:25:42 -0400156 void purgeAllUnlocked() { this->purgeUnlockedResources(false); }
157
158 // Purge unlocked resources. If 'scratchResourcesOnly' is true the purgeable resources
159 // containing persistent data are spared. If it is false then all purgeable resources will
160 // be deleted.
161 void purgeUnlockedResources(bool scratchResourcesOnly);
bsalomon71cb0c22014-11-14 12:10:14 -0800162
Brian Salomon5e150852017-03-22 14:53:13 -0400163 /** Purge all resources not used since the passed in time. */
164 void purgeResourcesNotUsedSince(GrStdSteadyClock::time_point);
165
Robert Phillipscf39f372019-09-03 10:29:20 -0400166 bool overBudget() const { return fBudgetedBytes > fMaxBytes; }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500167
Derek Sollenberger5480a182017-05-25 16:43:59 -0400168 /**
169 * Purge unlocked resources from the cache until the the provided byte count has been reached
170 * or we have purged all unlocked resources. The default policy is to purge in LRU order, but
171 * can be overridden to prefer purging scratch resources (in LRU order) prior to purging other
172 * resource types.
173 *
174 * @param maxBytesToPurge the desired number of bytes to be purged.
175 * @param preferScratchResources If true scratch resources will be purged prior to other
176 * resource types.
177 */
178 void purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources);
179
bsalomonb77a9072016-09-07 10:02:04 -0700180 /** Returns true if the cache would like a flush to occur in order to make more resources
181 purgeable. */
Brian Salomon8cefa3e2019-04-04 11:39:55 -0400182 bool requestsFlush() const;
bsalomon71cb0c22014-11-14 12:10:14 -0800183
Robert Phillipsddc21482019-10-16 14:30:09 -0400184 /** Maintain a ref to this texture until we receive a GrTextureFreedMessage. */
185 void insertDelayedTextureUnref(GrTexture*);
Brian Osman13dddce2017-05-09 13:19:50 -0400186
robertphillips60029a52015-11-09 13:51:06 -0800187#if GR_CACHE_STATS
188 struct Stats {
189 int fTotal;
190 int fNumPurgeable;
191 int fNumNonPurgeable;
192
193 int fScratch;
kkinnunen2e6055b2016-04-22 01:48:29 -0700194 int fWrapped;
robertphillips60029a52015-11-09 13:51:06 -0800195 size_t fUnbudgetedSize;
196
197 Stats() { this->reset(); }
198
199 void reset() {
200 fTotal = 0;
201 fNumPurgeable = 0;
202 fNumNonPurgeable = 0;
203 fScratch = 0;
kkinnunen2e6055b2016-04-22 01:48:29 -0700204 fWrapped = 0;
robertphillips60029a52015-11-09 13:51:06 -0800205 fUnbudgetedSize = 0;
206 }
207
208 void update(GrGpuResource* resource) {
209 if (resource->cacheAccess().isScratch()) {
210 ++fScratch;
211 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700212 if (resource->resourcePriv().refsWrappedObjects()) {
213 ++fWrapped;
robertphillips60029a52015-11-09 13:51:06 -0800214 }
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500215 if (GrBudgetedType::kBudgeted != resource->resourcePriv().budgetedType()) {
robertphillips60029a52015-11-09 13:51:06 -0800216 fUnbudgetedSize += resource->gpuMemorySize();
217 }
218 }
219 };
220
221 void getStats(Stats*) const;
222
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500223#if GR_TEST_UTILS
mtkleinb9eb4ac2015-02-02 18:26:03 -0800224 void dumpStats(SkString*) const;
joshualittdc5685a2015-12-02 14:08:25 -0800225
226 void dumpStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* value) const;
bsalomon71cb0c22014-11-14 12:10:14 -0800227#endif
228
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500229#endif
230
Brian Salomon1090da62017-01-06 12:04:19 -0500231#ifdef SK_DEBUG
232 int countUniqueKeysWithTag(const char* tag) const;
233#endif
234
bsalomonddf30e62015-02-19 11:38:44 -0800235 // This function is for unit testing and is only defined in test tools.
236 void changeTimestamp(uint32_t newTimestamp);
237
ericrk0a5fa482015-09-15 14:16:10 -0700238 // Enumerates all cached resources and dumps their details to traceMemoryDump.
239 void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const;
240
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500241 void setProxyProvider(GrProxyProvider* proxyProvider) { fProxyProvider = proxyProvider; }
Robert Phillipsd464feb2020-10-08 11:00:02 -0400242 void setThreadSafeCache(GrThreadSafeCache* threadSafeCache) {
243 fThreadSafeCache = threadSafeCache;
Robert Phillips12d06a32020-09-16 12:31:34 -0400244 }
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400245
bsalomonc8dc1f72014-08-21 13:02:13 -0700246private:
bsalomon71cb0c22014-11-14 12:10:14 -0800247 ///////////////////////////////////////////////////////////////////////////
248 /// @name Methods accessible via ResourceAccess
249 ////
250 void insertResource(GrGpuResource*);
251 void removeResource(GrGpuResource*);
Greg Danielda642612021-02-09 18:04:02 -0500252 void notifyARefCntReachedZero(GrGpuResource*, GrGpuResource::LastRemovedRef);
bsalomonf99e9612015-02-19 08:24:16 -0800253 void changeUniqueKey(GrGpuResource*, const GrUniqueKey&);
254 void removeUniqueKey(GrGpuResource*);
bsalomon10e23ca2014-11-25 05:52:06 -0800255 void willRemoveScratchKey(const GrGpuResource*);
bsalomon84c8e622014-11-17 09:33:27 -0800256 void didChangeBudgetStatus(GrGpuResource*);
Brian Salomon2c791fc2019-04-02 11:52:03 -0400257 void refResource(GrGpuResource* resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800258 /// @}
259
Brian Salomon2c791fc2019-04-02 11:52:03 -0400260 void refAndMakeResourceMRU(GrGpuResource*);
Brian Osman13dddce2017-05-09 13:19:50 -0400261 void processFreedGpuResources();
bsalomonf320e042015-02-17 15:09:34 -0800262 void addToNonpurgeableArray(GrGpuResource*);
263 void removeFromNonpurgeableArray(GrGpuResource*);
bsalomon71cb0c22014-11-14 12:10:14 -0800264
Robert Phillipscf39f372019-09-03 10:29:20 -0400265 bool wouldFit(size_t bytes) const { return fBudgetedBytes+bytes <= fMaxBytes; }
robertphillips6e83ac72015-08-13 05:19:14 -0700266
bsalomonddf30e62015-02-19 11:38:44 -0800267 uint32_t getNextTimestamp();
268
bsalomon16961262014-08-26 14:01:07 -0700269#ifdef SK_DEBUG
bsalomonf320e042015-02-17 15:09:34 -0800270 bool isInCache(const GrGpuResource* r) const;
bsalomon71cb0c22014-11-14 12:10:14 -0800271 void validate() const;
272#else
273 void validate() const {}
bsalomon16961262014-08-26 14:01:07 -0700274#endif
275
bsalomon71cb0c22014-11-14 12:10:14 -0800276 class AutoValidate;
277
bsalomonbcf0a522014-10-08 08:40:09 -0700278 class AvailableForScratchUse;
bsalomon744998e2014-08-28 09:54:34 -0700279
robertphillipsee843b22016-10-04 05:30:20 -0700280 struct ScratchMapTraits {
bsalomon7775c852014-12-30 12:50:52 -0800281 static const GrScratchKey& GetKey(const GrGpuResource& r) {
bsalomon3582d3e2015-02-13 14:20:05 -0800282 return r.resourcePriv().getScratchKey();
bsalomon744998e2014-08-28 09:54:34 -0700283 }
robertphillipsee843b22016-10-04 05:30:20 -0700284
285 static uint32_t Hash(const GrScratchKey& key) { return key.hash(); }
Robert Phillipsf8e25022017-11-08 15:24:31 -0500286 static void OnFree(GrGpuResource*) { }
bsalomon744998e2014-08-28 09:54:34 -0700287 };
bsalomon7775c852014-12-30 12:50:52 -0800288 typedef SkTMultiMap<GrGpuResource, GrScratchKey, ScratchMapTraits> ScratchMap;
bsalomon744998e2014-08-28 09:54:34 -0700289
robertphillipsee843b22016-10-04 05:30:20 -0700290 struct UniqueHashTraits {
bsalomon8718aaf2015-02-19 07:24:21 -0800291 static const GrUniqueKey& GetKey(const GrGpuResource& r) { return r.getUniqueKey(); }
robertphillipsee843b22016-10-04 05:30:20 -0700292
293 static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); }
bsalomon8b79d232014-11-10 10:19:06 -0800294 };
bsalomon8718aaf2015-02-19 07:24:21 -0800295 typedef SkTDynamicHash<GrGpuResource, GrUniqueKey, UniqueHashTraits> UniqueHash;
bsalomon8b79d232014-11-10 10:19:06 -0800296
Robert Phillipsddc21482019-10-16 14:30:09 -0400297 class TextureAwaitingUnref {
Brian Salomon876a0172019-03-08 11:12:14 -0500298 public:
Robert Phillipsddc21482019-10-16 14:30:09 -0400299 TextureAwaitingUnref();
300 TextureAwaitingUnref(GrTexture* texture);
301 TextureAwaitingUnref(const TextureAwaitingUnref&) = delete;
302 TextureAwaitingUnref& operator=(const TextureAwaitingUnref&) = delete;
303 TextureAwaitingUnref(TextureAwaitingUnref&&);
304 TextureAwaitingUnref& operator=(TextureAwaitingUnref&&);
305 ~TextureAwaitingUnref();
Brian Salomon876a0172019-03-08 11:12:14 -0500306 void addRef();
307 void unref();
308 bool finished();
309
310 private:
Robert Phillipsddc21482019-10-16 14:30:09 -0400311 GrTexture* fTexture = nullptr;
Brian Salomon876a0172019-03-08 11:12:14 -0500312 int fNumUnrefs = 0;
313 };
Robert Phillipsddc21482019-10-16 14:30:09 -0400314 using TexturesAwaitingUnref = SkTHashMap<uint32_t, TextureAwaitingUnref>;
Brian Salomon876a0172019-03-08 11:12:14 -0500315
bsalomon9f2d1572015-02-17 11:47:40 -0800316 static bool CompareTimestamp(GrGpuResource* const& a, GrGpuResource* const& b) {
317 return a->cacheAccess().timestamp() < b->cacheAccess().timestamp();
318 }
bsalomon23e619c2015-02-06 11:54:28 -0800319
bsalomon9f2d1572015-02-17 11:47:40 -0800320 static int* AccessResourceIndex(GrGpuResource* const& res) {
321 return res->cacheAccess().accessCacheIndex();
322 }
323
Robert Phillipse7a959d2021-03-11 14:44:42 -0500324 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage, uint32_t>::Inbox InvalidUniqueKeyInbox;
325 typedef SkMessageBus<GrTextureFreedMessage, uint32_t>::Inbox FreedTextureInbox;
bsalomon9f2d1572015-02-17 11:47:40 -0800326 typedef SkTDPQueue<GrGpuResource*, CompareTimestamp, AccessResourceIndex> PurgeableQueue;
bsalomonf320e042015-02-17 15:09:34 -0800327 typedef SkTDArray<GrGpuResource*> ResourceArray;
bsalomon9f2d1572015-02-17 11:47:40 -0800328
Brian Salomon2c791fc2019-04-02 11:52:03 -0400329 GrProxyProvider* fProxyProvider = nullptr;
Robert Phillipsd464feb2020-10-08 11:00:02 -0400330 GrThreadSafeCache* fThreadSafeCache = nullptr;
Robert Phillips12d06a32020-09-16 12:31:34 -0400331
bsalomon9f2d1572015-02-17 11:47:40 -0800332 // Whenever a resource is added to the cache or the result of a cache lookup, fTimestamp is
333 // assigned as the resource's timestamp and then incremented. fPurgeableQueue orders the
334 // purgeable resources by this value, and thus is used to purge resources in LRU order.
Brian Salomon2c791fc2019-04-02 11:52:03 -0400335 uint32_t fTimestamp = 0;
bsalomon9f2d1572015-02-17 11:47:40 -0800336 PurgeableQueue fPurgeableQueue;
bsalomonf320e042015-02-17 15:09:34 -0800337 ResourceArray fNonpurgeableResources;
bsalomon9f2d1572015-02-17 11:47:40 -0800338
bsalomon744998e2014-08-28 09:54:34 -0700339 // This map holds all resources that can be used as scratch resources.
bsalomon8b79d232014-11-10 10:19:06 -0800340 ScratchMap fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800341 // This holds all resources that have unique keys.
342 UniqueHash fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800343
344 // our budget, used in purgeAsNeeded()
Brian Salomon2c791fc2019-04-02 11:52:03 -0400345 size_t fMaxBytes = kDefaultMaxSize;
bsalomon71cb0c22014-11-14 12:10:14 -0800346
347#if GR_CACHE_STATS
Brian Salomon2c791fc2019-04-02 11:52:03 -0400348 int fHighWaterCount = 0;
349 size_t fHighWaterBytes = 0;
350 int fBudgetedHighWaterCount = 0;
351 size_t fBudgetedHighWaterBytes = 0;
bsalomon71cb0c22014-11-14 12:10:14 -0800352#endif
353
bsalomondace19e2014-11-17 07:34:06 -0800354 // our current stats for all resources
Brian Salomon2c791fc2019-04-02 11:52:03 -0400355 SkDEBUGCODE(int fCount = 0;)
356 size_t fBytes = 0;
bsalomon71cb0c22014-11-14 12:10:14 -0800357
bsalomondace19e2014-11-17 07:34:06 -0800358 // our current stats for resources that count against the budget
Brian Salomon2c791fc2019-04-02 11:52:03 -0400359 int fBudgetedCount = 0;
360 size_t fBudgetedBytes = 0;
361 size_t fPurgeableBytes = 0;
362 int fNumBudgetedResourcesFlushWillMakePurgeable = 0;
bsalomondace19e2014-11-17 07:34:06 -0800363
bsalomon8718aaf2015-02-19 07:24:21 -0800364 InvalidUniqueKeyInbox fInvalidUniqueKeyInbox;
Robert Phillipsddc21482019-10-16 14:30:09 -0400365 FreedTextureInbox fFreedTextureInbox;
366 TexturesAwaitingUnref fTexturesAwaitingUnref;
Greg Danielc27eb722018-08-10 09:48:08 -0400367
Brian Salomon2c791fc2019-04-02 11:52:03 -0400368 uint32_t fContextUniqueID = SK_InvalidUniqueID;
369 GrSingleOwner* fSingleOwner = nullptr;
bsalomon3f324322015-04-08 11:01:54 -0700370
371 // This resource is allowed to be in the nonpurgeable array for the sake of validate() because
372 // we're in the midst of converting it to purgeable status.
Brian Salomon2c791fc2019-04-02 11:52:03 -0400373 SkDEBUGCODE(GrGpuResource* fNewlyPurgeableResourceForValidation = nullptr;)
bsalomonc8dc1f72014-08-21 13:02:13 -0700374};
375
bsalomon0ea80f42015-02-11 10:49:59 -0800376class GrResourceCache::ResourceAccess {
bsalomon71cb0c22014-11-14 12:10:14 -0800377private:
bsalomon0ea80f42015-02-11 10:49:59 -0800378 ResourceAccess(GrResourceCache* cache) : fCache(cache) { }
bsalomon71cb0c22014-11-14 12:10:14 -0800379 ResourceAccess(const ResourceAccess& that) : fCache(that.fCache) { }
John Stilesb35c3b82020-08-06 19:58:52 -0400380 ResourceAccess& operator=(const ResourceAccess&) = delete;
bsalomon71cb0c22014-11-14 12:10:14 -0800381
382 /**
383 * Insert a resource into the cache.
384 */
385 void insertResource(GrGpuResource* resource) { fCache->insertResource(resource); }
386
387 /**
388 * Removes a resource from the cache.
389 */
390 void removeResource(GrGpuResource* resource) { fCache->removeResource(resource); }
391
392 /**
Brian Salomon2c791fc2019-04-02 11:52:03 -0400393 * Adds a ref to a resource with proper tracking if the resource has 0 refs prior to
394 * adding the ref.
395 */
396 void refResource(GrGpuResource* resource) { fCache->refResource(resource); }
397
398 /**
bsalomon3f324322015-04-08 11:01:54 -0700399 * Notifications that should be sent to the cache when the ref/io cnt status of resources
400 * changes.
bsalomon71cb0c22014-11-14 12:10:14 -0800401 */
bsalomon3f324322015-04-08 11:01:54 -0700402 enum RefNotificationFlags {
403 /** All types of refs on the resource have reached zero. */
404 kAllCntsReachedZero_RefNotificationFlag = 0x1,
405 /** The normal (not pending IO type) ref cnt has reached zero. */
406 kRefCntReachedZero_RefNotificationFlag = 0x2,
407 };
408 /**
Greg Danielda642612021-02-09 18:04:02 -0500409 * Called by GrGpuResources when they detect one of their ref cnts have reached zero. This may
410 * either be the main ref or the command buffer usage ref.
bsalomon3f324322015-04-08 11:01:54 -0700411 */
Greg Danielda642612021-02-09 18:04:02 -0500412 void notifyARefCntReachedZero(GrGpuResource* resource,
413 GrGpuResource::LastRemovedRef removedRef) {
414 fCache->notifyARefCntReachedZero(resource, removedRef);
bsalomon3f324322015-04-08 11:01:54 -0700415 }
bsalomon71cb0c22014-11-14 12:10:14 -0800416
417 /**
bsalomonf99e9612015-02-19 08:24:16 -0800418 * Called by GrGpuResources to change their unique keys.
bsalomon71cb0c22014-11-14 12:10:14 -0800419 */
bsalomonf99e9612015-02-19 08:24:16 -0800420 void changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) {
421 fCache->changeUniqueKey(resource, newKey);
422 }
bsalomon71cb0c22014-11-14 12:10:14 -0800423
bsalomon10e23ca2014-11-25 05:52:06 -0800424 /**
bsalomonf99e9612015-02-19 08:24:16 -0800425 * Called by a GrGpuResource to remove its unique key.
bsalomon23e619c2015-02-06 11:54:28 -0800426 */
bsalomonf99e9612015-02-19 08:24:16 -0800427 void removeUniqueKey(GrGpuResource* resource) { fCache->removeUniqueKey(resource); }
bsalomon23e619c2015-02-06 11:54:28 -0800428
429 /**
430 * Called by a GrGpuResource when it removes its scratch key.
bsalomon10e23ca2014-11-25 05:52:06 -0800431 */
432 void willRemoveScratchKey(const GrGpuResource* resource) {
433 fCache->willRemoveScratchKey(resource);
434 }
bsalomon84c8e622014-11-17 09:33:27 -0800435
436 /**
437 * Called by GrGpuResources when they change from budgeted to unbudgeted or vice versa.
438 */
439 void didChangeBudgetStatus(GrGpuResource* resource) { fCache->didChangeBudgetStatus(resource); }
440
bsalomon71cb0c22014-11-14 12:10:14 -0800441 // No taking addresses of this type.
442 const ResourceAccess* operator&() const;
443 ResourceAccess* operator&();
444
bsalomon0ea80f42015-02-11 10:49:59 -0800445 GrResourceCache* fCache;
bsalomon71cb0c22014-11-14 12:10:14 -0800446
447 friend class GrGpuResource; // To access all the proxy inline methods.
bsalomon0ea80f42015-02-11 10:49:59 -0800448 friend class GrResourceCache; // To create this type.
bsalomon71cb0c22014-11-14 12:10:14 -0800449};
450
bsalomon0ea80f42015-02-11 10:49:59 -0800451inline GrResourceCache::ResourceAccess GrResourceCache::resourceAccess() {
bsalomon71cb0c22014-11-14 12:10:14 -0800452 return ResourceAccess(this);
453}
454
bsalomonc8dc1f72014-08-21 13:02:13 -0700455#endif