blob: 1b800bf1e7ac8bd73822a38d11ab0accbc19eb70 [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"
12#include "include/gpu/GrGpuResource.h"
13#include "include/private/GrResourceKey.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/private/SkTArray.h"
15#include "include/private/SkTHash.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/SkTDPQueue.h"
Ben Wagner729a23f2019-05-17 16:29:34 -040018#include "src/core/SkTInternalLList.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/core/SkTMultiMap.h"
20#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;
mtkleinb9eb4ac2015-02-02 18:26:03 -080028
Brian Osman13dddce2017-05-09 13:19:50 -040029struct GrGpuResourceFreedMessage {
30 GrGpuResource* fResource;
31 uint32_t fOwningUniqueID;
32};
33
Chris Dalton9a986cf2018-10-18 15:27:59 -060034static inline bool SkShouldPostMessageToBus(
35 const GrGpuResourceFreedMessage& msg, uint32_t msgBusUniqueID) {
36 // The inbox's ID is the unique ID of the owning GrContext.
37 return msgBusUniqueID == msg.fOwningUniqueID;
38}
39
bsalomonc8dc1f72014-08-21 13:02:13 -070040/**
bsalomon71cb0c22014-11-14 12:10:14 -080041 * Manages the lifetime of all GrGpuResource instances.
42 *
43 * Resources may have optionally have two types of keys:
44 * 1) A scratch key. This is for resources whose allocations are cached but not their contents.
45 * Multiple resources can share the same scratch key. This is so a caller can have two
bsalomon84c8e622014-11-17 09:33:27 -080046 * resource instances with the same properties (e.g. multipass rendering that ping-pongs
bsalomon8718aaf2015-02-19 07:24:21 -080047 * between two temporary surfaces). The scratch key is set at resource creation time and
bsalomon71cb0c22014-11-14 12:10:14 -080048 * should never change. Resources need not have a scratch key.
bsalomon8718aaf2015-02-19 07:24:21 -080049 * 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 -080050 * resource may have a given unique key. The unique key can be set, cleared, or changed
51 * anytime after resource creation.
52 *
bsalomon8718aaf2015-02-19 07:24:21 -080053 * A unique key always takes precedence over a scratch key when a resource has both types of keys.
bsalomon71cb0c22014-11-14 12:10:14 -080054 * 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 -080055 * is dropped.
bsalomonc8dc1f72014-08-21 13:02:13 -070056 */
bsalomon0ea80f42015-02-11 10:49:59 -080057class GrResourceCache {
bsalomonc8dc1f72014-08-21 13:02:13 -070058public:
Brian Salomon8f8995a2018-10-15 14:32:15 -040059 GrResourceCache(const GrCaps*, GrSingleOwner* owner, uint32_t contextUniqueID);
bsalomon0ea80f42015-02-11 10:49:59 -080060 ~GrResourceCache();
bsalomonc8dc1f72014-08-21 13:02:13 -070061
bsalomon3f324322015-04-08 11:01:54 -070062 // Default maximum number of bytes of gpu memory of budgeted resources in the cache.
63 static const size_t kDefaultMaxSize = 96 * (1 << 20);
bsalomon3f324322015-04-08 11:01:54 -070064
bsalomon71cb0c22014-11-14 12:10:14 -080065 /** Used to access functionality needed by GrGpuResource for lifetime management. */
66 class ResourceAccess;
67 ResourceAccess resourceAccess();
bsalomonc8dc1f72014-08-21 13:02:13 -070068
Brian Salomon238069b2018-07-11 15:58:57 -040069 /** Unique ID of the owning GrContext. */
70 uint32_t contextUniqueID() const { return fContextUniqueID; }
71
Robert Phillipscf39f372019-09-03 10:29:20 -040072 /** Sets the max gpu memory byte size of the cache. */
73 void setLimit(size_t bytes);
bsalomonc8dc1f72014-08-21 13:02:13 -070074
bsalomon71cb0c22014-11-14 12:10:14 -080075 /**
bsalomondace19e2014-11-17 07:34:06 -080076 * Returns the number of resources.
bsalomon71cb0c22014-11-14 12:10:14 -080077 */
bsalomonf320e042015-02-17 15:09:34 -080078 int getResourceCount() const {
79 return fPurgeableQueue.count() + fNonpurgeableResources.count();
80 }
bsalomon8b79d232014-11-10 10:19:06 -080081
bsalomon71cb0c22014-11-14 12:10:14 -080082 /**
bsalomondace19e2014-11-17 07:34:06 -080083 * Returns the number of resources that count against the budget.
84 */
85 int getBudgetedResourceCount() const { return fBudgetedCount; }
86
87 /**
88 * Returns the number of bytes consumed by resources.
bsalomon71cb0c22014-11-14 12:10:14 -080089 */
90 size_t getResourceBytes() const { return fBytes; }
91
92 /**
Derek Sollenbergeree479142017-05-24 11:41:33 -040093 * Returns the number of bytes held by unlocked reosources which are available for purging.
94 */
95 size_t getPurgeableBytes() const { return fPurgeableBytes; }
96
97 /**
bsalomondace19e2014-11-17 07:34:06 -080098 * Returns the number of bytes consumed by budgeted resources.
99 */
100 size_t getBudgetedResourceBytes() const { return fBudgetedBytes; }
101
102 /**
bsalomon71cb0c22014-11-14 12:10:14 -0800103 * Returns the number of bytes consumed by cached resources.
104 */
105 size_t getMaxResourceBytes() const { return fMaxBytes; }
106
107 /**
108 * Abandons the backend API resources owned by all GrGpuResource objects and removes them from
109 * the cache.
110 */
bsalomonc8dc1f72014-08-21 13:02:13 -0700111 void abandonAll();
112
bsalomon71cb0c22014-11-14 12:10:14 -0800113 /**
114 * Releases the backend API resources owned by all GrGpuResource objects and removes them from
115 * the cache.
116 */
bsalomonc8dc1f72014-08-21 13:02:13 -0700117 void releaseAll();
118
Chris Daltond004e0b2018-09-27 09:28:03 -0600119 enum class ScratchFlags {
120 kNone = 0,
bsalomon000f8292014-10-15 19:04:14 -0700121 /** Preferentially returns scratch resources with no pending IO. */
Chris Daltond004e0b2018-09-27 09:28:03 -0600122 kPreferNoPendingIO = 0x1,
bsalomon000f8292014-10-15 19:04:14 -0700123 /** Will not return any resources that match but have pending IO. */
Chris Daltond004e0b2018-09-27 09:28:03 -0600124 kRequireNoPendingIO = 0x2,
bsalomon000f8292014-10-15 19:04:14 -0700125 };
bsalomon71cb0c22014-11-14 12:10:14 -0800126
127 /**
128 * Find a resource that matches a scratch key.
129 */
Chris Daltond004e0b2018-09-27 09:28:03 -0600130 GrGpuResource* findAndRefScratchResource(const GrScratchKey& scratchKey, size_t resourceSize,
131 ScratchFlags);
halcanary9d524f22016-03-29 09:03:52 -0700132
bsalomon8b79d232014-11-10 10:19:06 -0800133#ifdef SK_DEBUG
134 // This is not particularly fast and only used for validation, so debug only.
bsalomon7775c852014-12-30 12:50:52 -0800135 int countScratchEntriesForKey(const GrScratchKey& scratchKey) const {
bsalomon8b79d232014-11-10 10:19:06 -0800136 return fScratchMap.countForKey(scratchKey);
137 }
138#endif
139
bsalomon71cb0c22014-11-14 12:10:14 -0800140 /**
bsalomon8718aaf2015-02-19 07:24:21 -0800141 * Find a resource that matches a unique key.
bsalomon71cb0c22014-11-14 12:10:14 -0800142 */
robertphillipsee843b22016-10-04 05:30:20 -0700143 GrGpuResource* findAndRefUniqueResource(const GrUniqueKey& key) {
144 GrGpuResource* resource = fUniqueHash.find(key);
145 if (resource) {
146 this->refAndMakeResourceMRU(resource);
147 }
148 return resource;
149 }
bsalomon8b79d232014-11-10 10:19:06 -0800150
Greg Danielcd871402017-09-26 12:49:26 -0400151 /**
bsalomon8718aaf2015-02-19 07:24:21 -0800152 * Query whether a unique key exists in the cache.
bsalomon71cb0c22014-11-14 12:10:14 -0800153 */
bsalomon8718aaf2015-02-19 07:24:21 -0800154 bool hasUniqueKey(const GrUniqueKey& key) const {
155 return SkToBool(fUniqueHash.find(key));
bsalomon8b79d232014-11-10 10:19:06 -0800156 }
bsalomonbcf0a522014-10-08 08:40:09 -0700157
bsalomon8718aaf2015-02-19 07:24:21 -0800158 /** Purges resources to become under budget and processes resources with invalidated unique
bsalomon23e619c2015-02-06 11:54:28 -0800159 keys. */
robertphillipsee843b22016-10-04 05:30:20 -0700160 void purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800161
bsalomon71cb0c22014-11-14 12:10:14 -0800162 /** Purges all resources that don't have external owners. */
Robert Phillips6eba0632018-03-28 12:25:42 -0400163 void purgeAllUnlocked() { this->purgeUnlockedResources(false); }
164
165 // Purge unlocked resources. If 'scratchResourcesOnly' is true the purgeable resources
166 // containing persistent data are spared. If it is false then all purgeable resources will
167 // be deleted.
168 void purgeUnlockedResources(bool scratchResourcesOnly);
bsalomon71cb0c22014-11-14 12:10:14 -0800169
Brian Salomon5e150852017-03-22 14:53:13 -0400170 /** Purge all resources not used since the passed in time. */
171 void purgeResourcesNotUsedSince(GrStdSteadyClock::time_point);
172
Robert Phillipscf39f372019-09-03 10:29:20 -0400173 bool overBudget() const { return fBudgetedBytes > fMaxBytes; }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500174
Derek Sollenberger5480a182017-05-25 16:43:59 -0400175 /**
176 * Purge unlocked resources from the cache until the the provided byte count has been reached
177 * or we have purged all unlocked resources. The default policy is to purge in LRU order, but
178 * can be overridden to prefer purging scratch resources (in LRU order) prior to purging other
179 * resource types.
180 *
181 * @param maxBytesToPurge the desired number of bytes to be purged.
182 * @param preferScratchResources If true scratch resources will be purged prior to other
183 * resource types.
184 */
185 void purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources);
186
bsalomonb77a9072016-09-07 10:02:04 -0700187 /** Returns true if the cache would like a flush to occur in order to make more resources
188 purgeable. */
Brian Salomon8cefa3e2019-04-04 11:39:55 -0400189 bool requestsFlush() const;
bsalomon71cb0c22014-11-14 12:10:14 -0800190
Brian Osman13dddce2017-05-09 13:19:50 -0400191 /** Maintain a ref to this resource until we receive a GrGpuResourceFreedMessage. */
Brian Salomon876a0172019-03-08 11:12:14 -0500192 void insertDelayedResourceUnref(GrGpuResource* resource);
Brian Osman13dddce2017-05-09 13:19:50 -0400193
robertphillips60029a52015-11-09 13:51:06 -0800194#if GR_CACHE_STATS
195 struct Stats {
196 int fTotal;
197 int fNumPurgeable;
198 int fNumNonPurgeable;
199
200 int fScratch;
kkinnunen2e6055b2016-04-22 01:48:29 -0700201 int fWrapped;
robertphillips60029a52015-11-09 13:51:06 -0800202 size_t fUnbudgetedSize;
203
204 Stats() { this->reset(); }
205
206 void reset() {
207 fTotal = 0;
208 fNumPurgeable = 0;
209 fNumNonPurgeable = 0;
210 fScratch = 0;
kkinnunen2e6055b2016-04-22 01:48:29 -0700211 fWrapped = 0;
robertphillips60029a52015-11-09 13:51:06 -0800212 fUnbudgetedSize = 0;
213 }
214
215 void update(GrGpuResource* resource) {
216 if (resource->cacheAccess().isScratch()) {
217 ++fScratch;
218 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700219 if (resource->resourcePriv().refsWrappedObjects()) {
220 ++fWrapped;
robertphillips60029a52015-11-09 13:51:06 -0800221 }
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500222 if (GrBudgetedType::kBudgeted != resource->resourcePriv().budgetedType()) {
robertphillips60029a52015-11-09 13:51:06 -0800223 fUnbudgetedSize += resource->gpuMemorySize();
224 }
225 }
226 };
227
228 void getStats(Stats*) const;
229
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500230#if GR_TEST_UTILS
mtkleinb9eb4ac2015-02-02 18:26:03 -0800231 void dumpStats(SkString*) const;
joshualittdc5685a2015-12-02 14:08:25 -0800232
233 void dumpStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* value) const;
bsalomon71cb0c22014-11-14 12:10:14 -0800234#endif
235
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500236#endif
237
Brian Salomon1090da62017-01-06 12:04:19 -0500238#ifdef SK_DEBUG
239 int countUniqueKeysWithTag(const char* tag) const;
240#endif
241
bsalomonddf30e62015-02-19 11:38:44 -0800242 // This function is for unit testing and is only defined in test tools.
243 void changeTimestamp(uint32_t newTimestamp);
244
ericrk0a5fa482015-09-15 14:16:10 -0700245 // Enumerates all cached resources and dumps their details to traceMemoryDump.
246 void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const;
247
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500248 void setProxyProvider(GrProxyProvider* proxyProvider) { fProxyProvider = proxyProvider; }
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400249
bsalomonc8dc1f72014-08-21 13:02:13 -0700250private:
bsalomon71cb0c22014-11-14 12:10:14 -0800251 ///////////////////////////////////////////////////////////////////////////
252 /// @name Methods accessible via ResourceAccess
253 ////
254 void insertResource(GrGpuResource*);
255 void removeResource(GrGpuResource*);
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400256 void notifyRefCntReachedZero(GrGpuResource*);
bsalomonf99e9612015-02-19 08:24:16 -0800257 void changeUniqueKey(GrGpuResource*, const GrUniqueKey&);
258 void removeUniqueKey(GrGpuResource*);
bsalomon10e23ca2014-11-25 05:52:06 -0800259 void willRemoveScratchKey(const GrGpuResource*);
bsalomon84c8e622014-11-17 09:33:27 -0800260 void didChangeBudgetStatus(GrGpuResource*);
Brian Salomon2c791fc2019-04-02 11:52:03 -0400261 void refResource(GrGpuResource* resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800262 /// @}
263
Brian Salomon2c791fc2019-04-02 11:52:03 -0400264 void refAndMakeResourceMRU(GrGpuResource*);
Brian Osman13dddce2017-05-09 13:19:50 -0400265 void processFreedGpuResources();
bsalomonf320e042015-02-17 15:09:34 -0800266 void addToNonpurgeableArray(GrGpuResource*);
267 void removeFromNonpurgeableArray(GrGpuResource*);
bsalomon71cb0c22014-11-14 12:10:14 -0800268
Robert Phillipscf39f372019-09-03 10:29:20 -0400269 bool wouldFit(size_t bytes) const { return fBudgetedBytes+bytes <= fMaxBytes; }
robertphillips6e83ac72015-08-13 05:19:14 -0700270
bsalomonddf30e62015-02-19 11:38:44 -0800271 uint32_t getNextTimestamp();
272
bsalomon16961262014-08-26 14:01:07 -0700273#ifdef SK_DEBUG
bsalomonf320e042015-02-17 15:09:34 -0800274 bool isInCache(const GrGpuResource* r) const;
bsalomon71cb0c22014-11-14 12:10:14 -0800275 void validate() const;
276#else
277 void validate() const {}
bsalomon16961262014-08-26 14:01:07 -0700278#endif
279
bsalomon71cb0c22014-11-14 12:10:14 -0800280 class AutoValidate;
281
bsalomonbcf0a522014-10-08 08:40:09 -0700282 class AvailableForScratchUse;
bsalomon744998e2014-08-28 09:54:34 -0700283
robertphillipsee843b22016-10-04 05:30:20 -0700284 struct ScratchMapTraits {
bsalomon7775c852014-12-30 12:50:52 -0800285 static const GrScratchKey& GetKey(const GrGpuResource& r) {
bsalomon3582d3e2015-02-13 14:20:05 -0800286 return r.resourcePriv().getScratchKey();
bsalomon744998e2014-08-28 09:54:34 -0700287 }
robertphillipsee843b22016-10-04 05:30:20 -0700288
289 static uint32_t Hash(const GrScratchKey& key) { return key.hash(); }
Robert Phillipsf8e25022017-11-08 15:24:31 -0500290 static void OnFree(GrGpuResource*) { }
bsalomon744998e2014-08-28 09:54:34 -0700291 };
bsalomon7775c852014-12-30 12:50:52 -0800292 typedef SkTMultiMap<GrGpuResource, GrScratchKey, ScratchMapTraits> ScratchMap;
bsalomon744998e2014-08-28 09:54:34 -0700293
robertphillipsee843b22016-10-04 05:30:20 -0700294 struct UniqueHashTraits {
bsalomon8718aaf2015-02-19 07:24:21 -0800295 static const GrUniqueKey& GetKey(const GrGpuResource& r) { return r.getUniqueKey(); }
robertphillipsee843b22016-10-04 05:30:20 -0700296
297 static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); }
bsalomon8b79d232014-11-10 10:19:06 -0800298 };
bsalomon8718aaf2015-02-19 07:24:21 -0800299 typedef SkTDynamicHash<GrGpuResource, GrUniqueKey, UniqueHashTraits> UniqueHash;
bsalomon8b79d232014-11-10 10:19:06 -0800300
Brian Salomon876a0172019-03-08 11:12:14 -0500301 class ResourceAwaitingUnref {
302 public:
303 ResourceAwaitingUnref();
304 ResourceAwaitingUnref(GrGpuResource* resource);
305 ResourceAwaitingUnref(const ResourceAwaitingUnref&) = delete;
306 ResourceAwaitingUnref& operator=(const ResourceAwaitingUnref&) = delete;
307 ResourceAwaitingUnref(ResourceAwaitingUnref&&);
308 ResourceAwaitingUnref& operator=(ResourceAwaitingUnref&&);
309 ~ResourceAwaitingUnref();
310 void addRef();
311 void unref();
312 bool finished();
313
314 private:
315 GrGpuResource* fResource = nullptr;
316 int fNumUnrefs = 0;
317 };
318 using ReourcesAwaitingUnref = SkTHashMap<uint32_t, ResourceAwaitingUnref>;
319
bsalomon9f2d1572015-02-17 11:47:40 -0800320 static bool CompareTimestamp(GrGpuResource* const& a, GrGpuResource* const& b) {
321 return a->cacheAccess().timestamp() < b->cacheAccess().timestamp();
322 }
bsalomon23e619c2015-02-06 11:54:28 -0800323
bsalomon9f2d1572015-02-17 11:47:40 -0800324 static int* AccessResourceIndex(GrGpuResource* const& res) {
325 return res->cacheAccess().accessCacheIndex();
326 }
327
bsalomon8718aaf2015-02-19 07:24:21 -0800328 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage>::Inbox InvalidUniqueKeyInbox;
Brian Osman13dddce2017-05-09 13:19:50 -0400329 typedef SkMessageBus<GrGpuResourceFreedMessage>::Inbox FreedGpuResourceInbox;
bsalomon9f2d1572015-02-17 11:47:40 -0800330 typedef SkTDPQueue<GrGpuResource*, CompareTimestamp, AccessResourceIndex> PurgeableQueue;
bsalomonf320e042015-02-17 15:09:34 -0800331 typedef SkTDArray<GrGpuResource*> ResourceArray;
bsalomon9f2d1572015-02-17 11:47:40 -0800332
Brian Salomon2c791fc2019-04-02 11:52:03 -0400333 GrProxyProvider* fProxyProvider = nullptr;
bsalomon9f2d1572015-02-17 11:47:40 -0800334 // Whenever a resource is added to the cache or the result of a cache lookup, fTimestamp is
335 // assigned as the resource's timestamp and then incremented. fPurgeableQueue orders the
336 // purgeable resources by this value, and thus is used to purge resources in LRU order.
Brian Salomon2c791fc2019-04-02 11:52:03 -0400337 uint32_t fTimestamp = 0;
bsalomon9f2d1572015-02-17 11:47:40 -0800338 PurgeableQueue fPurgeableQueue;
bsalomonf320e042015-02-17 15:09:34 -0800339 ResourceArray fNonpurgeableResources;
bsalomon9f2d1572015-02-17 11:47:40 -0800340
bsalomon744998e2014-08-28 09:54:34 -0700341 // This map holds all resources that can be used as scratch resources.
bsalomon8b79d232014-11-10 10:19:06 -0800342 ScratchMap fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800343 // This holds all resources that have unique keys.
344 UniqueHash fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800345
346 // our budget, used in purgeAsNeeded()
Brian Salomon2c791fc2019-04-02 11:52:03 -0400347 size_t fMaxBytes = kDefaultMaxSize;
bsalomon71cb0c22014-11-14 12:10:14 -0800348
349#if GR_CACHE_STATS
Brian Salomon2c791fc2019-04-02 11:52:03 -0400350 int fHighWaterCount = 0;
351 size_t fHighWaterBytes = 0;
352 int fBudgetedHighWaterCount = 0;
353 size_t fBudgetedHighWaterBytes = 0;
bsalomon71cb0c22014-11-14 12:10:14 -0800354#endif
355
bsalomondace19e2014-11-17 07:34:06 -0800356 // our current stats for all resources
Brian Salomon2c791fc2019-04-02 11:52:03 -0400357 SkDEBUGCODE(int fCount = 0;)
358 size_t fBytes = 0;
bsalomon71cb0c22014-11-14 12:10:14 -0800359
bsalomondace19e2014-11-17 07:34:06 -0800360 // our current stats for resources that count against the budget
Brian Salomon2c791fc2019-04-02 11:52:03 -0400361 int fBudgetedCount = 0;
362 size_t fBudgetedBytes = 0;
363 size_t fPurgeableBytes = 0;
364 int fNumBudgetedResourcesFlushWillMakePurgeable = 0;
bsalomondace19e2014-11-17 07:34:06 -0800365
bsalomon8718aaf2015-02-19 07:24:21 -0800366 InvalidUniqueKeyInbox fInvalidUniqueKeyInbox;
Brian Osman13dddce2017-05-09 13:19:50 -0400367 FreedGpuResourceInbox fFreedGpuResourceInbox;
Brian Salomon876a0172019-03-08 11:12:14 -0500368 ReourcesAwaitingUnref fResourcesAwaitingUnref;
Greg Danielc27eb722018-08-10 09:48:08 -0400369
Brian Salomon2c791fc2019-04-02 11:52:03 -0400370 uint32_t fContextUniqueID = SK_InvalidUniqueID;
371 GrSingleOwner* fSingleOwner = nullptr;
bsalomon3f324322015-04-08 11:01:54 -0700372
373 // This resource is allowed to be in the nonpurgeable array for the sake of validate() because
374 // we're in the midst of converting it to purgeable status.
Brian Salomon2c791fc2019-04-02 11:52:03 -0400375 SkDEBUGCODE(GrGpuResource* fNewlyPurgeableResourceForValidation = nullptr;)
robertphillips63926682015-08-20 09:39:02 -0700376
Brian Salomon2c791fc2019-04-02 11:52:03 -0400377 bool fPreferVRAMUseOverFlushes = false;
bsalomonc8dc1f72014-08-21 13:02:13 -0700378};
379
Chris Daltond004e0b2018-09-27 09:28:03 -0600380GR_MAKE_BITFIELD_CLASS_OPS(GrResourceCache::ScratchFlags);
381
bsalomon0ea80f42015-02-11 10:49:59 -0800382class GrResourceCache::ResourceAccess {
bsalomon71cb0c22014-11-14 12:10:14 -0800383private:
bsalomon0ea80f42015-02-11 10:49:59 -0800384 ResourceAccess(GrResourceCache* cache) : fCache(cache) { }
bsalomon71cb0c22014-11-14 12:10:14 -0800385 ResourceAccess(const ResourceAccess& that) : fCache(that.fCache) { }
386 ResourceAccess& operator=(const ResourceAccess&); // unimpl
387
388 /**
389 * Insert a resource into the cache.
390 */
391 void insertResource(GrGpuResource* resource) { fCache->insertResource(resource); }
392
393 /**
394 * Removes a resource from the cache.
395 */
396 void removeResource(GrGpuResource* resource) { fCache->removeResource(resource); }
397
398 /**
Brian Salomon2c791fc2019-04-02 11:52:03 -0400399 * Adds a ref to a resource with proper tracking if the resource has 0 refs prior to
400 * adding the ref.
401 */
402 void refResource(GrGpuResource* resource) { fCache->refResource(resource); }
403
404 /**
bsalomon3f324322015-04-08 11:01:54 -0700405 * Notifications that should be sent to the cache when the ref/io cnt status of resources
406 * changes.
bsalomon71cb0c22014-11-14 12:10:14 -0800407 */
bsalomon3f324322015-04-08 11:01:54 -0700408 enum RefNotificationFlags {
409 /** All types of refs on the resource have reached zero. */
410 kAllCntsReachedZero_RefNotificationFlag = 0x1,
411 /** The normal (not pending IO type) ref cnt has reached zero. */
412 kRefCntReachedZero_RefNotificationFlag = 0x2,
413 };
414 /**
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400415 * Called by GrGpuResources when they detect that their ref cnt has reached zero.
bsalomon3f324322015-04-08 11:01:54 -0700416 */
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400417 void notifyRefCntReachedZero(GrGpuResource* resource) {
418 fCache->notifyRefCntReachedZero(resource);
bsalomon3f324322015-04-08 11:01:54 -0700419 }
bsalomon71cb0c22014-11-14 12:10:14 -0800420
421 /**
bsalomonf99e9612015-02-19 08:24:16 -0800422 * Called by GrGpuResources to change their unique keys.
bsalomon71cb0c22014-11-14 12:10:14 -0800423 */
bsalomonf99e9612015-02-19 08:24:16 -0800424 void changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) {
425 fCache->changeUniqueKey(resource, newKey);
426 }
bsalomon71cb0c22014-11-14 12:10:14 -0800427
bsalomon10e23ca2014-11-25 05:52:06 -0800428 /**
bsalomonf99e9612015-02-19 08:24:16 -0800429 * Called by a GrGpuResource to remove its unique key.
bsalomon23e619c2015-02-06 11:54:28 -0800430 */
bsalomonf99e9612015-02-19 08:24:16 -0800431 void removeUniqueKey(GrGpuResource* resource) { fCache->removeUniqueKey(resource); }
bsalomon23e619c2015-02-06 11:54:28 -0800432
433 /**
434 * Called by a GrGpuResource when it removes its scratch key.
bsalomon10e23ca2014-11-25 05:52:06 -0800435 */
436 void willRemoveScratchKey(const GrGpuResource* resource) {
437 fCache->willRemoveScratchKey(resource);
438 }
bsalomon84c8e622014-11-17 09:33:27 -0800439
440 /**
441 * Called by GrGpuResources when they change from budgeted to unbudgeted or vice versa.
442 */
443 void didChangeBudgetStatus(GrGpuResource* resource) { fCache->didChangeBudgetStatus(resource); }
444
bsalomon71cb0c22014-11-14 12:10:14 -0800445 // No taking addresses of this type.
446 const ResourceAccess* operator&() const;
447 ResourceAccess* operator&();
448
bsalomon0ea80f42015-02-11 10:49:59 -0800449 GrResourceCache* fCache;
bsalomon71cb0c22014-11-14 12:10:14 -0800450
451 friend class GrGpuResource; // To access all the proxy inline methods.
bsalomon0ea80f42015-02-11 10:49:59 -0800452 friend class GrResourceCache; // To create this type.
bsalomon71cb0c22014-11-14 12:10:14 -0800453};
454
bsalomon0ea80f42015-02-11 10:49:59 -0800455inline GrResourceCache::ResourceAccess GrResourceCache::resourceAccess() {
bsalomon71cb0c22014-11-14 12:10:14 -0800456 return ResourceAccess(this);
457}
458
bsalomonc8dc1f72014-08-21 13:02:13 -0700459#endif