blob: efbdb49a6d0b9e31e2527248270be2aa7b360dbe [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
bsalomon71cb0c22014-11-14 12:10:14 -0800119 /**
120 * Find a resource that matches a scratch key.
121 */
Robert Phillipsaee18c92019-09-06 11:48:27 -0400122 GrGpuResource* findAndRefScratchResource(const GrScratchKey& scratchKey);
halcanary9d524f22016-03-29 09:03:52 -0700123
bsalomon8b79d232014-11-10 10:19:06 -0800124#ifdef SK_DEBUG
125 // This is not particularly fast and only used for validation, so debug only.
bsalomon7775c852014-12-30 12:50:52 -0800126 int countScratchEntriesForKey(const GrScratchKey& scratchKey) const {
bsalomon8b79d232014-11-10 10:19:06 -0800127 return fScratchMap.countForKey(scratchKey);
128 }
129#endif
130
bsalomon71cb0c22014-11-14 12:10:14 -0800131 /**
bsalomon8718aaf2015-02-19 07:24:21 -0800132 * Find a resource that matches a unique key.
bsalomon71cb0c22014-11-14 12:10:14 -0800133 */
robertphillipsee843b22016-10-04 05:30:20 -0700134 GrGpuResource* findAndRefUniqueResource(const GrUniqueKey& key) {
135 GrGpuResource* resource = fUniqueHash.find(key);
136 if (resource) {
137 this->refAndMakeResourceMRU(resource);
138 }
139 return resource;
140 }
bsalomon8b79d232014-11-10 10:19:06 -0800141
Greg Danielcd871402017-09-26 12:49:26 -0400142 /**
bsalomon8718aaf2015-02-19 07:24:21 -0800143 * Query whether a unique key exists in the cache.
bsalomon71cb0c22014-11-14 12:10:14 -0800144 */
bsalomon8718aaf2015-02-19 07:24:21 -0800145 bool hasUniqueKey(const GrUniqueKey& key) const {
146 return SkToBool(fUniqueHash.find(key));
bsalomon8b79d232014-11-10 10:19:06 -0800147 }
bsalomonbcf0a522014-10-08 08:40:09 -0700148
bsalomon8718aaf2015-02-19 07:24:21 -0800149 /** Purges resources to become under budget and processes resources with invalidated unique
bsalomon23e619c2015-02-06 11:54:28 -0800150 keys. */
robertphillipsee843b22016-10-04 05:30:20 -0700151 void purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800152
bsalomon71cb0c22014-11-14 12:10:14 -0800153 /** Purges all resources that don't have external owners. */
Robert Phillips6eba0632018-03-28 12:25:42 -0400154 void purgeAllUnlocked() { this->purgeUnlockedResources(false); }
155
156 // Purge unlocked resources. If 'scratchResourcesOnly' is true the purgeable resources
157 // containing persistent data are spared. If it is false then all purgeable resources will
158 // be deleted.
159 void purgeUnlockedResources(bool scratchResourcesOnly);
bsalomon71cb0c22014-11-14 12:10:14 -0800160
Brian Salomon5e150852017-03-22 14:53:13 -0400161 /** Purge all resources not used since the passed in time. */
162 void purgeResourcesNotUsedSince(GrStdSteadyClock::time_point);
163
Robert Phillipscf39f372019-09-03 10:29:20 -0400164 bool overBudget() const { return fBudgetedBytes > fMaxBytes; }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500165
Derek Sollenberger5480a182017-05-25 16:43:59 -0400166 /**
167 * Purge unlocked resources from the cache until the the provided byte count has been reached
168 * or we have purged all unlocked resources. The default policy is to purge in LRU order, but
169 * can be overridden to prefer purging scratch resources (in LRU order) prior to purging other
170 * resource types.
171 *
172 * @param maxBytesToPurge the desired number of bytes to be purged.
173 * @param preferScratchResources If true scratch resources will be purged prior to other
174 * resource types.
175 */
176 void purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources);
177
bsalomonb77a9072016-09-07 10:02:04 -0700178 /** Returns true if the cache would like a flush to occur in order to make more resources
179 purgeable. */
Brian Salomon8cefa3e2019-04-04 11:39:55 -0400180 bool requestsFlush() const;
bsalomon71cb0c22014-11-14 12:10:14 -0800181
Brian Osman13dddce2017-05-09 13:19:50 -0400182 /** Maintain a ref to this resource until we receive a GrGpuResourceFreedMessage. */
Brian Salomon876a0172019-03-08 11:12:14 -0500183 void insertDelayedResourceUnref(GrGpuResource* resource);
Brian Osman13dddce2017-05-09 13:19:50 -0400184
robertphillips60029a52015-11-09 13:51:06 -0800185#if GR_CACHE_STATS
186 struct Stats {
187 int fTotal;
188 int fNumPurgeable;
189 int fNumNonPurgeable;
190
191 int fScratch;
kkinnunen2e6055b2016-04-22 01:48:29 -0700192 int fWrapped;
robertphillips60029a52015-11-09 13:51:06 -0800193 size_t fUnbudgetedSize;
194
195 Stats() { this->reset(); }
196
197 void reset() {
198 fTotal = 0;
199 fNumPurgeable = 0;
200 fNumNonPurgeable = 0;
201 fScratch = 0;
kkinnunen2e6055b2016-04-22 01:48:29 -0700202 fWrapped = 0;
robertphillips60029a52015-11-09 13:51:06 -0800203 fUnbudgetedSize = 0;
204 }
205
206 void update(GrGpuResource* resource) {
207 if (resource->cacheAccess().isScratch()) {
208 ++fScratch;
209 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700210 if (resource->resourcePriv().refsWrappedObjects()) {
211 ++fWrapped;
robertphillips60029a52015-11-09 13:51:06 -0800212 }
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500213 if (GrBudgetedType::kBudgeted != resource->resourcePriv().budgetedType()) {
robertphillips60029a52015-11-09 13:51:06 -0800214 fUnbudgetedSize += resource->gpuMemorySize();
215 }
216 }
217 };
218
219 void getStats(Stats*) const;
220
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500221#if GR_TEST_UTILS
mtkleinb9eb4ac2015-02-02 18:26:03 -0800222 void dumpStats(SkString*) const;
joshualittdc5685a2015-12-02 14:08:25 -0800223
224 void dumpStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* value) const;
bsalomon71cb0c22014-11-14 12:10:14 -0800225#endif
226
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500227#endif
228
Brian Salomon1090da62017-01-06 12:04:19 -0500229#ifdef SK_DEBUG
230 int countUniqueKeysWithTag(const char* tag) const;
231#endif
232
bsalomonddf30e62015-02-19 11:38:44 -0800233 // This function is for unit testing and is only defined in test tools.
234 void changeTimestamp(uint32_t newTimestamp);
235
ericrk0a5fa482015-09-15 14:16:10 -0700236 // Enumerates all cached resources and dumps their details to traceMemoryDump.
237 void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const;
238
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500239 void setProxyProvider(GrProxyProvider* proxyProvider) { fProxyProvider = proxyProvider; }
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400240
bsalomonc8dc1f72014-08-21 13:02:13 -0700241private:
bsalomon71cb0c22014-11-14 12:10:14 -0800242 ///////////////////////////////////////////////////////////////////////////
243 /// @name Methods accessible via ResourceAccess
244 ////
245 void insertResource(GrGpuResource*);
246 void removeResource(GrGpuResource*);
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400247 void notifyRefCntReachedZero(GrGpuResource*);
bsalomonf99e9612015-02-19 08:24:16 -0800248 void changeUniqueKey(GrGpuResource*, const GrUniqueKey&);
249 void removeUniqueKey(GrGpuResource*);
bsalomon10e23ca2014-11-25 05:52:06 -0800250 void willRemoveScratchKey(const GrGpuResource*);
bsalomon84c8e622014-11-17 09:33:27 -0800251 void didChangeBudgetStatus(GrGpuResource*);
Brian Salomon2c791fc2019-04-02 11:52:03 -0400252 void refResource(GrGpuResource* resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800253 /// @}
254
Brian Salomon2c791fc2019-04-02 11:52:03 -0400255 void refAndMakeResourceMRU(GrGpuResource*);
Brian Osman13dddce2017-05-09 13:19:50 -0400256 void processFreedGpuResources();
bsalomonf320e042015-02-17 15:09:34 -0800257 void addToNonpurgeableArray(GrGpuResource*);
258 void removeFromNonpurgeableArray(GrGpuResource*);
bsalomon71cb0c22014-11-14 12:10:14 -0800259
Robert Phillipscf39f372019-09-03 10:29:20 -0400260 bool wouldFit(size_t bytes) const { return fBudgetedBytes+bytes <= fMaxBytes; }
robertphillips6e83ac72015-08-13 05:19:14 -0700261
bsalomonddf30e62015-02-19 11:38:44 -0800262 uint32_t getNextTimestamp();
263
bsalomon16961262014-08-26 14:01:07 -0700264#ifdef SK_DEBUG
bsalomonf320e042015-02-17 15:09:34 -0800265 bool isInCache(const GrGpuResource* r) const;
bsalomon71cb0c22014-11-14 12:10:14 -0800266 void validate() const;
267#else
268 void validate() const {}
bsalomon16961262014-08-26 14:01:07 -0700269#endif
270
bsalomon71cb0c22014-11-14 12:10:14 -0800271 class AutoValidate;
272
bsalomonbcf0a522014-10-08 08:40:09 -0700273 class AvailableForScratchUse;
bsalomon744998e2014-08-28 09:54:34 -0700274
robertphillipsee843b22016-10-04 05:30:20 -0700275 struct ScratchMapTraits {
bsalomon7775c852014-12-30 12:50:52 -0800276 static const GrScratchKey& GetKey(const GrGpuResource& r) {
bsalomon3582d3e2015-02-13 14:20:05 -0800277 return r.resourcePriv().getScratchKey();
bsalomon744998e2014-08-28 09:54:34 -0700278 }
robertphillipsee843b22016-10-04 05:30:20 -0700279
280 static uint32_t Hash(const GrScratchKey& key) { return key.hash(); }
Robert Phillipsf8e25022017-11-08 15:24:31 -0500281 static void OnFree(GrGpuResource*) { }
bsalomon744998e2014-08-28 09:54:34 -0700282 };
bsalomon7775c852014-12-30 12:50:52 -0800283 typedef SkTMultiMap<GrGpuResource, GrScratchKey, ScratchMapTraits> ScratchMap;
bsalomon744998e2014-08-28 09:54:34 -0700284
robertphillipsee843b22016-10-04 05:30:20 -0700285 struct UniqueHashTraits {
bsalomon8718aaf2015-02-19 07:24:21 -0800286 static const GrUniqueKey& GetKey(const GrGpuResource& r) { return r.getUniqueKey(); }
robertphillipsee843b22016-10-04 05:30:20 -0700287
288 static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); }
bsalomon8b79d232014-11-10 10:19:06 -0800289 };
bsalomon8718aaf2015-02-19 07:24:21 -0800290 typedef SkTDynamicHash<GrGpuResource, GrUniqueKey, UniqueHashTraits> UniqueHash;
bsalomon8b79d232014-11-10 10:19:06 -0800291
Brian Salomon876a0172019-03-08 11:12:14 -0500292 class ResourceAwaitingUnref {
293 public:
294 ResourceAwaitingUnref();
295 ResourceAwaitingUnref(GrGpuResource* resource);
296 ResourceAwaitingUnref(const ResourceAwaitingUnref&) = delete;
297 ResourceAwaitingUnref& operator=(const ResourceAwaitingUnref&) = delete;
298 ResourceAwaitingUnref(ResourceAwaitingUnref&&);
299 ResourceAwaitingUnref& operator=(ResourceAwaitingUnref&&);
300 ~ResourceAwaitingUnref();
301 void addRef();
302 void unref();
303 bool finished();
304
305 private:
306 GrGpuResource* fResource = nullptr;
307 int fNumUnrefs = 0;
308 };
309 using ReourcesAwaitingUnref = SkTHashMap<uint32_t, ResourceAwaitingUnref>;
310
bsalomon9f2d1572015-02-17 11:47:40 -0800311 static bool CompareTimestamp(GrGpuResource* const& a, GrGpuResource* const& b) {
312 return a->cacheAccess().timestamp() < b->cacheAccess().timestamp();
313 }
bsalomon23e619c2015-02-06 11:54:28 -0800314
bsalomon9f2d1572015-02-17 11:47:40 -0800315 static int* AccessResourceIndex(GrGpuResource* const& res) {
316 return res->cacheAccess().accessCacheIndex();
317 }
318
bsalomon8718aaf2015-02-19 07:24:21 -0800319 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage>::Inbox InvalidUniqueKeyInbox;
Brian Osman13dddce2017-05-09 13:19:50 -0400320 typedef SkMessageBus<GrGpuResourceFreedMessage>::Inbox FreedGpuResourceInbox;
bsalomon9f2d1572015-02-17 11:47:40 -0800321 typedef SkTDPQueue<GrGpuResource*, CompareTimestamp, AccessResourceIndex> PurgeableQueue;
bsalomonf320e042015-02-17 15:09:34 -0800322 typedef SkTDArray<GrGpuResource*> ResourceArray;
bsalomon9f2d1572015-02-17 11:47:40 -0800323
Brian Salomon2c791fc2019-04-02 11:52:03 -0400324 GrProxyProvider* fProxyProvider = nullptr;
bsalomon9f2d1572015-02-17 11:47:40 -0800325 // Whenever a resource is added to the cache or the result of a cache lookup, fTimestamp is
326 // assigned as the resource's timestamp and then incremented. fPurgeableQueue orders the
327 // purgeable resources by this value, and thus is used to purge resources in LRU order.
Brian Salomon2c791fc2019-04-02 11:52:03 -0400328 uint32_t fTimestamp = 0;
bsalomon9f2d1572015-02-17 11:47:40 -0800329 PurgeableQueue fPurgeableQueue;
bsalomonf320e042015-02-17 15:09:34 -0800330 ResourceArray fNonpurgeableResources;
bsalomon9f2d1572015-02-17 11:47:40 -0800331
bsalomon744998e2014-08-28 09:54:34 -0700332 // This map holds all resources that can be used as scratch resources.
bsalomon8b79d232014-11-10 10:19:06 -0800333 ScratchMap fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800334 // This holds all resources that have unique keys.
335 UniqueHash fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800336
337 // our budget, used in purgeAsNeeded()
Brian Salomon2c791fc2019-04-02 11:52:03 -0400338 size_t fMaxBytes = kDefaultMaxSize;
bsalomon71cb0c22014-11-14 12:10:14 -0800339
340#if GR_CACHE_STATS
Brian Salomon2c791fc2019-04-02 11:52:03 -0400341 int fHighWaterCount = 0;
342 size_t fHighWaterBytes = 0;
343 int fBudgetedHighWaterCount = 0;
344 size_t fBudgetedHighWaterBytes = 0;
bsalomon71cb0c22014-11-14 12:10:14 -0800345#endif
346
bsalomondace19e2014-11-17 07:34:06 -0800347 // our current stats for all resources
Brian Salomon2c791fc2019-04-02 11:52:03 -0400348 SkDEBUGCODE(int fCount = 0;)
349 size_t fBytes = 0;
bsalomon71cb0c22014-11-14 12:10:14 -0800350
bsalomondace19e2014-11-17 07:34:06 -0800351 // our current stats for resources that count against the budget
Brian Salomon2c791fc2019-04-02 11:52:03 -0400352 int fBudgetedCount = 0;
353 size_t fBudgetedBytes = 0;
354 size_t fPurgeableBytes = 0;
355 int fNumBudgetedResourcesFlushWillMakePurgeable = 0;
bsalomondace19e2014-11-17 07:34:06 -0800356
bsalomon8718aaf2015-02-19 07:24:21 -0800357 InvalidUniqueKeyInbox fInvalidUniqueKeyInbox;
Brian Osman13dddce2017-05-09 13:19:50 -0400358 FreedGpuResourceInbox fFreedGpuResourceInbox;
Brian Salomon876a0172019-03-08 11:12:14 -0500359 ReourcesAwaitingUnref fResourcesAwaitingUnref;
Greg Danielc27eb722018-08-10 09:48:08 -0400360
Brian Salomon2c791fc2019-04-02 11:52:03 -0400361 uint32_t fContextUniqueID = SK_InvalidUniqueID;
362 GrSingleOwner* fSingleOwner = nullptr;
bsalomon3f324322015-04-08 11:01:54 -0700363
364 // This resource is allowed to be in the nonpurgeable array for the sake of validate() because
365 // we're in the midst of converting it to purgeable status.
Brian Salomon2c791fc2019-04-02 11:52:03 -0400366 SkDEBUGCODE(GrGpuResource* fNewlyPurgeableResourceForValidation = nullptr;)
robertphillips63926682015-08-20 09:39:02 -0700367
Brian Salomon2c791fc2019-04-02 11:52:03 -0400368 bool fPreferVRAMUseOverFlushes = false;
bsalomonc8dc1f72014-08-21 13:02:13 -0700369};
370
bsalomon0ea80f42015-02-11 10:49:59 -0800371class GrResourceCache::ResourceAccess {
bsalomon71cb0c22014-11-14 12:10:14 -0800372private:
bsalomon0ea80f42015-02-11 10:49:59 -0800373 ResourceAccess(GrResourceCache* cache) : fCache(cache) { }
bsalomon71cb0c22014-11-14 12:10:14 -0800374 ResourceAccess(const ResourceAccess& that) : fCache(that.fCache) { }
375 ResourceAccess& operator=(const ResourceAccess&); // unimpl
376
377 /**
378 * Insert a resource into the cache.
379 */
380 void insertResource(GrGpuResource* resource) { fCache->insertResource(resource); }
381
382 /**
383 * Removes a resource from the cache.
384 */
385 void removeResource(GrGpuResource* resource) { fCache->removeResource(resource); }
386
387 /**
Brian Salomon2c791fc2019-04-02 11:52:03 -0400388 * Adds a ref to a resource with proper tracking if the resource has 0 refs prior to
389 * adding the ref.
390 */
391 void refResource(GrGpuResource* resource) { fCache->refResource(resource); }
392
393 /**
bsalomon3f324322015-04-08 11:01:54 -0700394 * Notifications that should be sent to the cache when the ref/io cnt status of resources
395 * changes.
bsalomon71cb0c22014-11-14 12:10:14 -0800396 */
bsalomon3f324322015-04-08 11:01:54 -0700397 enum RefNotificationFlags {
398 /** All types of refs on the resource have reached zero. */
399 kAllCntsReachedZero_RefNotificationFlag = 0x1,
400 /** The normal (not pending IO type) ref cnt has reached zero. */
401 kRefCntReachedZero_RefNotificationFlag = 0x2,
402 };
403 /**
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400404 * Called by GrGpuResources when they detect that their ref cnt has reached zero.
bsalomon3f324322015-04-08 11:01:54 -0700405 */
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400406 void notifyRefCntReachedZero(GrGpuResource* resource) {
407 fCache->notifyRefCntReachedZero(resource);
bsalomon3f324322015-04-08 11:01:54 -0700408 }
bsalomon71cb0c22014-11-14 12:10:14 -0800409
410 /**
bsalomonf99e9612015-02-19 08:24:16 -0800411 * Called by GrGpuResources to change their unique keys.
bsalomon71cb0c22014-11-14 12:10:14 -0800412 */
bsalomonf99e9612015-02-19 08:24:16 -0800413 void changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) {
414 fCache->changeUniqueKey(resource, newKey);
415 }
bsalomon71cb0c22014-11-14 12:10:14 -0800416
bsalomon10e23ca2014-11-25 05:52:06 -0800417 /**
bsalomonf99e9612015-02-19 08:24:16 -0800418 * Called by a GrGpuResource to remove its unique key.
bsalomon23e619c2015-02-06 11:54:28 -0800419 */
bsalomonf99e9612015-02-19 08:24:16 -0800420 void removeUniqueKey(GrGpuResource* resource) { fCache->removeUniqueKey(resource); }
bsalomon23e619c2015-02-06 11:54:28 -0800421
422 /**
423 * Called by a GrGpuResource when it removes its scratch key.
bsalomon10e23ca2014-11-25 05:52:06 -0800424 */
425 void willRemoveScratchKey(const GrGpuResource* resource) {
426 fCache->willRemoveScratchKey(resource);
427 }
bsalomon84c8e622014-11-17 09:33:27 -0800428
429 /**
430 * Called by GrGpuResources when they change from budgeted to unbudgeted or vice versa.
431 */
432 void didChangeBudgetStatus(GrGpuResource* resource) { fCache->didChangeBudgetStatus(resource); }
433
bsalomon71cb0c22014-11-14 12:10:14 -0800434 // No taking addresses of this type.
435 const ResourceAccess* operator&() const;
436 ResourceAccess* operator&();
437
bsalomon0ea80f42015-02-11 10:49:59 -0800438 GrResourceCache* fCache;
bsalomon71cb0c22014-11-14 12:10:14 -0800439
440 friend class GrGpuResource; // To access all the proxy inline methods.
bsalomon0ea80f42015-02-11 10:49:59 -0800441 friend class GrResourceCache; // To create this type.
bsalomon71cb0c22014-11-14 12:10:14 -0800442};
443
bsalomon0ea80f42015-02-11 10:49:59 -0800444inline GrResourceCache::ResourceAccess GrResourceCache::resourceAccess() {
bsalomon71cb0c22014-11-14 12:10:14 -0800445 return ResourceAccess(this);
446}
447
bsalomonc8dc1f72014-08-21 13:02:13 -0700448#endif