blob: c948864d85965111498b082207bafbc42087c97e [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
bsalomon744998e2014-08-28 09:54:34 -070011#include "GrGpuResource.h"
bsalomon9f2d1572015-02-17 11:47:40 -080012#include "GrGpuResourceCacheAccess.h"
bsalomon3582d3e2015-02-13 14:20:05 -080013#include "GrGpuResourcePriv.h"
bsalomon744998e2014-08-28 09:54:34 -070014#include "GrResourceKey.h"
bsalomon23e619c2015-02-06 11:54:28 -080015#include "SkMessageBus.h"
bsalomon8b79d232014-11-10 10:19:06 -080016#include "SkRefCnt.h"
bsalomon23e619c2015-02-06 11:54:28 -080017#include "SkTArray.h"
bsalomon9f2d1572015-02-17 11:47:40 -080018#include "SkTDPQueue.h"
bsalomonc8dc1f72014-08-21 13:02:13 -070019#include "SkTInternalLList.h"
bsalomon744998e2014-08-28 09:54:34 -070020#include "SkTMultiMap.h"
bsalomonc8dc1f72014-08-21 13:02:13 -070021
robertphillips63926682015-08-20 09:39:02 -070022class GrCaps;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050023class GrProxyProvider;
mtkleinb9eb4ac2015-02-02 18:26:03 -080024class SkString;
ericrk0a5fa482015-09-15 14:16:10 -070025class SkTraceMemoryDump;
Brian Salomon8f8995a2018-10-15 14:32:15 -040026class GrSingleOwner;
mtkleinb9eb4ac2015-02-02 18:26:03 -080027
Brian Osman13dddce2017-05-09 13:19:50 -040028struct GrGpuResourceFreedMessage {
29 GrGpuResource* fResource;
30 uint32_t fOwningUniqueID;
31};
32
Chris Dalton9a986cf2018-10-18 15:27:59 -060033static inline bool SkShouldPostMessageToBus(
34 const GrGpuResourceFreedMessage& msg, uint32_t msgBusUniqueID) {
35 // The inbox's ID is the unique ID of the owning GrContext.
36 return msgBusUniqueID == msg.fOwningUniqueID;
37}
38
bsalomonc8dc1f72014-08-21 13:02:13 -070039/**
bsalomon71cb0c22014-11-14 12:10:14 -080040 * Manages the lifetime of all GrGpuResource instances.
41 *
42 * Resources may have optionally have two types of keys:
43 * 1) A scratch key. This is for resources whose allocations are cached but not their contents.
44 * Multiple resources can share the same scratch key. This is so a caller can have two
bsalomon84c8e622014-11-17 09:33:27 -080045 * resource instances with the same properties (e.g. multipass rendering that ping-pongs
bsalomon8718aaf2015-02-19 07:24:21 -080046 * between two temporary surfaces). The scratch key is set at resource creation time and
bsalomon71cb0c22014-11-14 12:10:14 -080047 * should never change. Resources need not have a scratch key.
bsalomon8718aaf2015-02-19 07:24:21 -080048 * 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 -080049 * resource may have a given unique key. The unique key can be set, cleared, or changed
50 * anytime after resource creation.
51 *
bsalomon8718aaf2015-02-19 07:24:21 -080052 * A unique key always takes precedence over a scratch key when a resource has both types of keys.
bsalomon71cb0c22014-11-14 12:10:14 -080053 * 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 -080054 * is dropped.
bsalomonc8dc1f72014-08-21 13:02:13 -070055 */
bsalomon0ea80f42015-02-11 10:49:59 -080056class GrResourceCache {
bsalomonc8dc1f72014-08-21 13:02:13 -070057public:
Brian Salomon8f8995a2018-10-15 14:32:15 -040058 GrResourceCache(const GrCaps*, GrSingleOwner* owner, uint32_t contextUniqueID);
bsalomon0ea80f42015-02-11 10:49:59 -080059 ~GrResourceCache();
bsalomonc8dc1f72014-08-21 13:02:13 -070060
bsalomon3f324322015-04-08 11:01:54 -070061 // Default maximum number of budgeted resources in the cache.
62 static const int kDefaultMaxCount = 2 * (1 << 12);
63 // Default maximum number of bytes of gpu memory of budgeted resources in the cache.
64 static const size_t kDefaultMaxSize = 96 * (1 << 20);
bsalomon3f324322015-04-08 11:01:54 -070065
bsalomon71cb0c22014-11-14 12:10:14 -080066 /** Used to access functionality needed by GrGpuResource for lifetime management. */
67 class ResourceAccess;
68 ResourceAccess resourceAccess();
bsalomonc8dc1f72014-08-21 13:02:13 -070069
Brian Salomon238069b2018-07-11 15:58:57 -040070 /** Unique ID of the owning GrContext. */
71 uint32_t contextUniqueID() const { return fContextUniqueID; }
72
Brian Salomon43b882b2018-09-07 16:29:14 -040073 /** Sets the cache limits in terms of number of resources and max gpu memory byte size. */
74 void setLimits(int count, size_t bytes);
bsalomonc8dc1f72014-08-21 13:02:13 -070075
bsalomon71cb0c22014-11-14 12:10:14 -080076 /**
bsalomondace19e2014-11-17 07:34:06 -080077 * Returns the number of resources.
bsalomon71cb0c22014-11-14 12:10:14 -080078 */
bsalomonf320e042015-02-17 15:09:34 -080079 int getResourceCount() const {
80 return fPurgeableQueue.count() + fNonpurgeableResources.count();
81 }
bsalomon8b79d232014-11-10 10:19:06 -080082
bsalomon71cb0c22014-11-14 12:10:14 -080083 /**
bsalomondace19e2014-11-17 07:34:06 -080084 * Returns the number of resources that count against the budget.
85 */
86 int getBudgetedResourceCount() const { return fBudgetedCount; }
87
88 /**
89 * Returns the number of bytes consumed by resources.
bsalomon71cb0c22014-11-14 12:10:14 -080090 */
91 size_t getResourceBytes() const { return fBytes; }
92
93 /**
Derek Sollenbergeree479142017-05-24 11:41:33 -040094 * Returns the number of bytes held by unlocked reosources which are available for purging.
95 */
96 size_t getPurgeableBytes() const { return fPurgeableBytes; }
97
98 /**
bsalomondace19e2014-11-17 07:34:06 -080099 * Returns the number of bytes consumed by budgeted resources.
100 */
101 size_t getBudgetedResourceBytes() const { return fBudgetedBytes; }
102
103 /**
bsalomon71cb0c22014-11-14 12:10:14 -0800104 * Returns the cached resources count budget.
105 */
106 int getMaxResourceCount() const { return fMaxCount; }
107
108 /**
109 * Returns the number of bytes consumed by cached resources.
110 */
111 size_t getMaxResourceBytes() const { return fMaxBytes; }
112
113 /**
114 * Abandons 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 abandonAll();
118
bsalomon71cb0c22014-11-14 12:10:14 -0800119 /**
120 * Releases the backend API resources owned by all GrGpuResource objects and removes them from
121 * the cache.
122 */
bsalomonc8dc1f72014-08-21 13:02:13 -0700123 void releaseAll();
124
Chris Daltond004e0b2018-09-27 09:28:03 -0600125 enum class ScratchFlags {
126 kNone = 0,
bsalomon000f8292014-10-15 19:04:14 -0700127 /** Preferentially returns scratch resources with no pending IO. */
Chris Daltond004e0b2018-09-27 09:28:03 -0600128 kPreferNoPendingIO = 0x1,
bsalomon000f8292014-10-15 19:04:14 -0700129 /** Will not return any resources that match but have pending IO. */
Chris Daltond004e0b2018-09-27 09:28:03 -0600130 kRequireNoPendingIO = 0x2,
bsalomon000f8292014-10-15 19:04:14 -0700131 };
bsalomon71cb0c22014-11-14 12:10:14 -0800132
133 /**
134 * Find a resource that matches a scratch key.
135 */
Chris Daltond004e0b2018-09-27 09:28:03 -0600136 GrGpuResource* findAndRefScratchResource(const GrScratchKey& scratchKey, size_t resourceSize,
137 ScratchFlags);
halcanary9d524f22016-03-29 09:03:52 -0700138
bsalomon8b79d232014-11-10 10:19:06 -0800139#ifdef SK_DEBUG
140 // This is not particularly fast and only used for validation, so debug only.
bsalomon7775c852014-12-30 12:50:52 -0800141 int countScratchEntriesForKey(const GrScratchKey& scratchKey) const {
bsalomon8b79d232014-11-10 10:19:06 -0800142 return fScratchMap.countForKey(scratchKey);
143 }
144#endif
145
bsalomon71cb0c22014-11-14 12:10:14 -0800146 /**
bsalomon8718aaf2015-02-19 07:24:21 -0800147 * Find a resource that matches a unique key.
bsalomon71cb0c22014-11-14 12:10:14 -0800148 */
robertphillipsee843b22016-10-04 05:30:20 -0700149 GrGpuResource* findAndRefUniqueResource(const GrUniqueKey& key) {
150 GrGpuResource* resource = fUniqueHash.find(key);
151 if (resource) {
152 this->refAndMakeResourceMRU(resource);
153 }
154 return resource;
155 }
bsalomon8b79d232014-11-10 10:19:06 -0800156
Greg Danielcd871402017-09-26 12:49:26 -0400157 /**
bsalomon8718aaf2015-02-19 07:24:21 -0800158 * Query whether a unique key exists in the cache.
bsalomon71cb0c22014-11-14 12:10:14 -0800159 */
bsalomon8718aaf2015-02-19 07:24:21 -0800160 bool hasUniqueKey(const GrUniqueKey& key) const {
161 return SkToBool(fUniqueHash.find(key));
bsalomon8b79d232014-11-10 10:19:06 -0800162 }
bsalomonbcf0a522014-10-08 08:40:09 -0700163
bsalomon8718aaf2015-02-19 07:24:21 -0800164 /** Purges resources to become under budget and processes resources with invalidated unique
bsalomon23e619c2015-02-06 11:54:28 -0800165 keys. */
robertphillipsee843b22016-10-04 05:30:20 -0700166 void purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800167
bsalomon71cb0c22014-11-14 12:10:14 -0800168 /** Purges all resources that don't have external owners. */
Robert Phillips6eba0632018-03-28 12:25:42 -0400169 void purgeAllUnlocked() { this->purgeUnlockedResources(false); }
170
171 // Purge unlocked resources. If 'scratchResourcesOnly' is true the purgeable resources
172 // containing persistent data are spared. If it is false then all purgeable resources will
173 // be deleted.
174 void purgeUnlockedResources(bool scratchResourcesOnly);
bsalomon71cb0c22014-11-14 12:10:14 -0800175
Brian Salomon5e150852017-03-22 14:53:13 -0400176 /** Purge all resources not used since the passed in time. */
177 void purgeResourcesNotUsedSince(GrStdSteadyClock::time_point);
178
Robert Phillipseafd48a2017-11-16 07:52:08 -0500179 bool overBudget() const { return fBudgetedBytes > fMaxBytes || fBudgetedCount > fMaxCount; }
180
Derek Sollenberger5480a182017-05-25 16:43:59 -0400181 /**
182 * Purge unlocked resources from the cache until the the provided byte count has been reached
183 * or we have purged all unlocked resources. The default policy is to purge in LRU order, but
184 * can be overridden to prefer purging scratch resources (in LRU order) prior to purging other
185 * resource types.
186 *
187 * @param maxBytesToPurge the desired number of bytes to be purged.
188 * @param preferScratchResources If true scratch resources will be purged prior to other
189 * resource types.
190 */
191 void purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources);
192
bsalomonb77a9072016-09-07 10:02:04 -0700193 /** Returns true if the cache would like a flush to occur in order to make more resources
194 purgeable. */
Brian Salomon57d2beab2018-09-10 09:35:41 -0400195 bool requestsFlush() const { return this->overBudget() && !fPurgeableQueue.count(); }
bsalomon71cb0c22014-11-14 12:10:14 -0800196
Brian Osman13dddce2017-05-09 13:19:50 -0400197 /** Maintain a ref to this resource until we receive a GrGpuResourceFreedMessage. */
198 void insertCrossContextGpuResource(GrGpuResource* resource);
199
robertphillips60029a52015-11-09 13:51:06 -0800200#if GR_CACHE_STATS
201 struct Stats {
202 int fTotal;
203 int fNumPurgeable;
204 int fNumNonPurgeable;
205
206 int fScratch;
kkinnunen2e6055b2016-04-22 01:48:29 -0700207 int fWrapped;
robertphillips60029a52015-11-09 13:51:06 -0800208 size_t fUnbudgetedSize;
209
210 Stats() { this->reset(); }
211
212 void reset() {
213 fTotal = 0;
214 fNumPurgeable = 0;
215 fNumNonPurgeable = 0;
216 fScratch = 0;
kkinnunen2e6055b2016-04-22 01:48:29 -0700217 fWrapped = 0;
robertphillips60029a52015-11-09 13:51:06 -0800218 fUnbudgetedSize = 0;
219 }
220
221 void update(GrGpuResource* resource) {
222 if (resource->cacheAccess().isScratch()) {
223 ++fScratch;
224 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700225 if (resource->resourcePriv().refsWrappedObjects()) {
226 ++fWrapped;
robertphillips60029a52015-11-09 13:51:06 -0800227 }
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500228 if (GrBudgetedType::kBudgeted != resource->resourcePriv().budgetedType()) {
robertphillips60029a52015-11-09 13:51:06 -0800229 fUnbudgetedSize += resource->gpuMemorySize();
230 }
231 }
232 };
233
234 void getStats(Stats*) const;
235
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500236#if GR_TEST_UTILS
mtkleinb9eb4ac2015-02-02 18:26:03 -0800237 void dumpStats(SkString*) const;
joshualittdc5685a2015-12-02 14:08:25 -0800238
239 void dumpStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* value) const;
bsalomon71cb0c22014-11-14 12:10:14 -0800240#endif
241
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500242#endif
243
Brian Salomon1090da62017-01-06 12:04:19 -0500244#ifdef SK_DEBUG
245 int countUniqueKeysWithTag(const char* tag) const;
246#endif
247
bsalomonddf30e62015-02-19 11:38:44 -0800248 // This function is for unit testing and is only defined in test tools.
249 void changeTimestamp(uint32_t newTimestamp);
250
ericrk0a5fa482015-09-15 14:16:10 -0700251 // Enumerates all cached resources and dumps their details to traceMemoryDump.
252 void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const;
253
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500254 void setProxyProvider(GrProxyProvider* proxyProvider) { fProxyProvider = proxyProvider; }
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400255
bsalomonc8dc1f72014-08-21 13:02:13 -0700256private:
bsalomon71cb0c22014-11-14 12:10:14 -0800257 ///////////////////////////////////////////////////////////////////////////
258 /// @name Methods accessible via ResourceAccess
259 ////
260 void insertResource(GrGpuResource*);
261 void removeResource(GrGpuResource*);
bsalomon3f324322015-04-08 11:01:54 -0700262 void notifyCntReachedZero(GrGpuResource*, uint32_t flags);
bsalomonf99e9612015-02-19 08:24:16 -0800263 void changeUniqueKey(GrGpuResource*, const GrUniqueKey&);
264 void removeUniqueKey(GrGpuResource*);
bsalomon10e23ca2014-11-25 05:52:06 -0800265 void willRemoveScratchKey(const GrGpuResource*);
bsalomon84c8e622014-11-17 09:33:27 -0800266 void didChangeBudgetStatus(GrGpuResource*);
bsalomon9f2d1572015-02-17 11:47:40 -0800267 void refAndMakeResourceMRU(GrGpuResource*);
bsalomon71cb0c22014-11-14 12:10:14 -0800268 /// @}
269
Brian Osman13dddce2017-05-09 13:19:50 -0400270 void processFreedGpuResources();
bsalomonf320e042015-02-17 15:09:34 -0800271 void addToNonpurgeableArray(GrGpuResource*);
272 void removeFromNonpurgeableArray(GrGpuResource*);
bsalomon71cb0c22014-11-14 12:10:14 -0800273
robertphillips6e83ac72015-08-13 05:19:14 -0700274 bool wouldFit(size_t bytes) {
halcanary9d524f22016-03-29 09:03:52 -0700275 return fBudgetedBytes+bytes <= fMaxBytes && fBudgetedCount+1 <= fMaxCount;
robertphillips6e83ac72015-08-13 05:19:14 -0700276 }
277
bsalomonddf30e62015-02-19 11:38:44 -0800278 uint32_t getNextTimestamp();
279
bsalomon16961262014-08-26 14:01:07 -0700280#ifdef SK_DEBUG
bsalomonf320e042015-02-17 15:09:34 -0800281 bool isInCache(const GrGpuResource* r) const;
bsalomon71cb0c22014-11-14 12:10:14 -0800282 void validate() const;
283#else
284 void validate() const {}
bsalomon16961262014-08-26 14:01:07 -0700285#endif
286
bsalomon71cb0c22014-11-14 12:10:14 -0800287 class AutoValidate;
288
bsalomonbcf0a522014-10-08 08:40:09 -0700289 class AvailableForScratchUse;
bsalomon744998e2014-08-28 09:54:34 -0700290
robertphillipsee843b22016-10-04 05:30:20 -0700291 struct ScratchMapTraits {
bsalomon7775c852014-12-30 12:50:52 -0800292 static const GrScratchKey& GetKey(const GrGpuResource& r) {
bsalomon3582d3e2015-02-13 14:20:05 -0800293 return r.resourcePriv().getScratchKey();
bsalomon744998e2014-08-28 09:54:34 -0700294 }
robertphillipsee843b22016-10-04 05:30:20 -0700295
296 static uint32_t Hash(const GrScratchKey& key) { return key.hash(); }
Robert Phillipsf8e25022017-11-08 15:24:31 -0500297 static void OnFree(GrGpuResource*) { }
bsalomon744998e2014-08-28 09:54:34 -0700298 };
bsalomon7775c852014-12-30 12:50:52 -0800299 typedef SkTMultiMap<GrGpuResource, GrScratchKey, ScratchMapTraits> ScratchMap;
bsalomon744998e2014-08-28 09:54:34 -0700300
robertphillipsee843b22016-10-04 05:30:20 -0700301 struct UniqueHashTraits {
bsalomon8718aaf2015-02-19 07:24:21 -0800302 static const GrUniqueKey& GetKey(const GrGpuResource& r) { return r.getUniqueKey(); }
robertphillipsee843b22016-10-04 05:30:20 -0700303
304 static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); }
bsalomon8b79d232014-11-10 10:19:06 -0800305 };
bsalomon8718aaf2015-02-19 07:24:21 -0800306 typedef SkTDynamicHash<GrGpuResource, GrUniqueKey, UniqueHashTraits> UniqueHash;
bsalomon8b79d232014-11-10 10:19:06 -0800307
bsalomon9f2d1572015-02-17 11:47:40 -0800308 static bool CompareTimestamp(GrGpuResource* const& a, GrGpuResource* const& b) {
309 return a->cacheAccess().timestamp() < b->cacheAccess().timestamp();
310 }
bsalomon23e619c2015-02-06 11:54:28 -0800311
bsalomon9f2d1572015-02-17 11:47:40 -0800312 static int* AccessResourceIndex(GrGpuResource* const& res) {
313 return res->cacheAccess().accessCacheIndex();
314 }
315
bsalomon8718aaf2015-02-19 07:24:21 -0800316 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage>::Inbox InvalidUniqueKeyInbox;
Brian Osman13dddce2017-05-09 13:19:50 -0400317 typedef SkMessageBus<GrGpuResourceFreedMessage>::Inbox FreedGpuResourceInbox;
bsalomon9f2d1572015-02-17 11:47:40 -0800318 typedef SkTDPQueue<GrGpuResource*, CompareTimestamp, AccessResourceIndex> PurgeableQueue;
bsalomonf320e042015-02-17 15:09:34 -0800319 typedef SkTDArray<GrGpuResource*> ResourceArray;
bsalomon9f2d1572015-02-17 11:47:40 -0800320
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500321 GrProxyProvider* fProxyProvider;
bsalomon9f2d1572015-02-17 11:47:40 -0800322 // Whenever a resource is added to the cache or the result of a cache lookup, fTimestamp is
323 // assigned as the resource's timestamp and then incremented. fPurgeableQueue orders the
324 // purgeable resources by this value, and thus is used to purge resources in LRU order.
325 uint32_t fTimestamp;
326 PurgeableQueue fPurgeableQueue;
bsalomonf320e042015-02-17 15:09:34 -0800327 ResourceArray fNonpurgeableResources;
bsalomon9f2d1572015-02-17 11:47:40 -0800328
bsalomon744998e2014-08-28 09:54:34 -0700329 // This map holds all resources that can be used as scratch resources.
bsalomon8b79d232014-11-10 10:19:06 -0800330 ScratchMap fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800331 // This holds all resources that have unique keys.
332 UniqueHash fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800333
334 // our budget, used in purgeAsNeeded()
335 int fMaxCount;
336 size_t fMaxBytes;
337
338#if GR_CACHE_STATS
339 int fHighWaterCount;
340 size_t fHighWaterBytes;
bsalomondace19e2014-11-17 07:34:06 -0800341 int fBudgetedHighWaterCount;
342 size_t fBudgetedHighWaterBytes;
bsalomon71cb0c22014-11-14 12:10:14 -0800343#endif
344
bsalomondace19e2014-11-17 07:34:06 -0800345 // our current stats for all resources
bsalomonf320e042015-02-17 15:09:34 -0800346 SkDEBUGCODE(int fCount;)
bsalomon71cb0c22014-11-14 12:10:14 -0800347 size_t fBytes;
348
bsalomondace19e2014-11-17 07:34:06 -0800349 // our current stats for resources that count against the budget
350 int fBudgetedCount;
351 size_t fBudgetedBytes;
Derek Sollenbergeree479142017-05-24 11:41:33 -0400352 size_t fPurgeableBytes;
bsalomondace19e2014-11-17 07:34:06 -0800353
bsalomon8718aaf2015-02-19 07:24:21 -0800354 InvalidUniqueKeyInbox fInvalidUniqueKeyInbox;
Brian Osman13dddce2017-05-09 13:19:50 -0400355 FreedGpuResourceInbox fFreedGpuResourceInbox;
356
Greg Danielc27eb722018-08-10 09:48:08 -0400357 SkTDArray<GrGpuResource*> fResourcesWaitingForFreeMsg;
358
Brian Osman13dddce2017-05-09 13:19:50 -0400359 uint32_t fContextUniqueID;
Brian Salomon8f8995a2018-10-15 14:32:15 -0400360 GrSingleOwner* fSingleOwner;
bsalomon3f324322015-04-08 11:01:54 -0700361
362 // This resource is allowed to be in the nonpurgeable array for the sake of validate() because
363 // we're in the midst of converting it to purgeable status.
364 SkDEBUGCODE(GrGpuResource* fNewlyPurgeableResourceForValidation;)
robertphillips63926682015-08-20 09:39:02 -0700365
366 bool fPreferVRAMUseOverFlushes;
bsalomonc8dc1f72014-08-21 13:02:13 -0700367};
368
Chris Daltond004e0b2018-09-27 09:28:03 -0600369GR_MAKE_BITFIELD_CLASS_OPS(GrResourceCache::ScratchFlags);
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 /**
bsalomon3f324322015-04-08 11:01:54 -0700388 * Notifications that should be sent to the cache when the ref/io cnt status of resources
389 * changes.
bsalomon71cb0c22014-11-14 12:10:14 -0800390 */
bsalomon3f324322015-04-08 11:01:54 -0700391 enum RefNotificationFlags {
392 /** All types of refs on the resource have reached zero. */
393 kAllCntsReachedZero_RefNotificationFlag = 0x1,
394 /** The normal (not pending IO type) ref cnt has reached zero. */
395 kRefCntReachedZero_RefNotificationFlag = 0x2,
396 };
397 /**
398 * Called by GrGpuResources when they detect that their ref/io cnts have reached zero. When the
399 * normal ref cnt reaches zero the flags that are set should be:
400 * a) kRefCntReachedZero if a pending IO cnt is still non-zero.
401 * b) (kRefCntReachedZero | kAllCntsReachedZero) when all pending IO cnts are also zero.
402 * kAllCntsReachedZero is set by itself if a pending IO cnt is decremented to zero and all the
403 * the other cnts are already zero.
404 */
405 void notifyCntReachedZero(GrGpuResource* resource, uint32_t flags) {
406 fCache->notifyCntReachedZero(resource, flags);
407 }
bsalomon71cb0c22014-11-14 12:10:14 -0800408
409 /**
bsalomonf99e9612015-02-19 08:24:16 -0800410 * Called by GrGpuResources to change their unique keys.
bsalomon71cb0c22014-11-14 12:10:14 -0800411 */
bsalomonf99e9612015-02-19 08:24:16 -0800412 void changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) {
413 fCache->changeUniqueKey(resource, newKey);
414 }
bsalomon71cb0c22014-11-14 12:10:14 -0800415
bsalomon10e23ca2014-11-25 05:52:06 -0800416 /**
bsalomonf99e9612015-02-19 08:24:16 -0800417 * Called by a GrGpuResource to remove its unique key.
bsalomon23e619c2015-02-06 11:54:28 -0800418 */
bsalomonf99e9612015-02-19 08:24:16 -0800419 void removeUniqueKey(GrGpuResource* resource) { fCache->removeUniqueKey(resource); }
bsalomon23e619c2015-02-06 11:54:28 -0800420
421 /**
422 * Called by a GrGpuResource when it removes its scratch key.
bsalomon10e23ca2014-11-25 05:52:06 -0800423 */
424 void willRemoveScratchKey(const GrGpuResource* resource) {
425 fCache->willRemoveScratchKey(resource);
426 }
bsalomon84c8e622014-11-17 09:33:27 -0800427
428 /**
429 * Called by GrGpuResources when they change from budgeted to unbudgeted or vice versa.
430 */
431 void didChangeBudgetStatus(GrGpuResource* resource) { fCache->didChangeBudgetStatus(resource); }
432
bsalomon71cb0c22014-11-14 12:10:14 -0800433 // No taking addresses of this type.
434 const ResourceAccess* operator&() const;
435 ResourceAccess* operator&();
436
bsalomon0ea80f42015-02-11 10:49:59 -0800437 GrResourceCache* fCache;
bsalomon71cb0c22014-11-14 12:10:14 -0800438
439 friend class GrGpuResource; // To access all the proxy inline methods.
bsalomon0ea80f42015-02-11 10:49:59 -0800440 friend class GrResourceCache; // To create this type.
bsalomon71cb0c22014-11-14 12:10:14 -0800441};
442
bsalomon0ea80f42015-02-11 10:49:59 -0800443inline GrResourceCache::ResourceAccess GrResourceCache::resourceAccess() {
bsalomon71cb0c22014-11-14 12:10:14 -0800444 return ResourceAccess(this);
445}
446
bsalomonc8dc1f72014-08-21 13:02:13 -0700447#endif