blob: 1d3598e90b86e67fb63a8ff9c72fd0075b8aa3d5 [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;
mtkleinb9eb4ac2015-02-02 18:26:03 -080026
Brian Osman13dddce2017-05-09 13:19:50 -040027struct GrGpuResourceFreedMessage {
28 GrGpuResource* fResource;
29 uint32_t fOwningUniqueID;
Brian Salomon238069b2018-07-11 15:58:57 -040030 bool shouldSend(uint32_t inboxID) const {
31 // The inbox's ID is the unique ID of the owning GrContext.
32 return inboxID == fOwningUniqueID;
33 }
Brian Osman13dddce2017-05-09 13:19:50 -040034};
35
bsalomonc8dc1f72014-08-21 13:02:13 -070036/**
bsalomon71cb0c22014-11-14 12:10:14 -080037 * Manages the lifetime of all GrGpuResource instances.
38 *
39 * Resources may have optionally have two types of keys:
40 * 1) A scratch key. This is for resources whose allocations are cached but not their contents.
41 * Multiple resources can share the same scratch key. This is so a caller can have two
bsalomon84c8e622014-11-17 09:33:27 -080042 * resource instances with the same properties (e.g. multipass rendering that ping-pongs
bsalomon8718aaf2015-02-19 07:24:21 -080043 * between two temporary surfaces). The scratch key is set at resource creation time and
bsalomon71cb0c22014-11-14 12:10:14 -080044 * should never change. Resources need not have a scratch key.
bsalomon8718aaf2015-02-19 07:24:21 -080045 * 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 -080046 * resource may have a given unique key. The unique key can be set, cleared, or changed
47 * anytime after resource creation.
48 *
bsalomon8718aaf2015-02-19 07:24:21 -080049 * A unique key always takes precedence over a scratch key when a resource has both types of keys.
bsalomon71cb0c22014-11-14 12:10:14 -080050 * 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 -080051 * is dropped.
bsalomonc8dc1f72014-08-21 13:02:13 -070052 */
bsalomon0ea80f42015-02-11 10:49:59 -080053class GrResourceCache {
bsalomonc8dc1f72014-08-21 13:02:13 -070054public:
Robert Phillips1afd4cd2018-01-08 13:40:32 -050055 GrResourceCache(const GrCaps*, uint32_t contextUniqueID);
bsalomon0ea80f42015-02-11 10:49:59 -080056 ~GrResourceCache();
bsalomonc8dc1f72014-08-21 13:02:13 -070057
bsalomon3f324322015-04-08 11:01:54 -070058 // Default maximum number of budgeted resources in the cache.
59 static const int kDefaultMaxCount = 2 * (1 << 12);
60 // Default maximum number of bytes of gpu memory of budgeted resources in the cache.
61 static const size_t kDefaultMaxSize = 96 * (1 << 20);
bsalomon3f324322015-04-08 11:01:54 -070062
bsalomon71cb0c22014-11-14 12:10:14 -080063 /** Used to access functionality needed by GrGpuResource for lifetime management. */
64 class ResourceAccess;
65 ResourceAccess resourceAccess();
bsalomonc8dc1f72014-08-21 13:02:13 -070066
Brian Salomon238069b2018-07-11 15:58:57 -040067 /** Unique ID of the owning GrContext. */
68 uint32_t contextUniqueID() const { return fContextUniqueID; }
69
Brian Salomon43b882b2018-09-07 16:29:14 -040070 /** Sets the cache limits in terms of number of resources and max gpu memory byte size. */
71 void setLimits(int count, size_t bytes);
bsalomonc8dc1f72014-08-21 13:02:13 -070072
bsalomon71cb0c22014-11-14 12:10:14 -080073 /**
bsalomondace19e2014-11-17 07:34:06 -080074 * Returns the number of resources.
bsalomon71cb0c22014-11-14 12:10:14 -080075 */
bsalomonf320e042015-02-17 15:09:34 -080076 int getResourceCount() const {
77 return fPurgeableQueue.count() + fNonpurgeableResources.count();
78 }
bsalomon8b79d232014-11-10 10:19:06 -080079
bsalomon71cb0c22014-11-14 12:10:14 -080080 /**
bsalomondace19e2014-11-17 07:34:06 -080081 * Returns the number of resources that count against the budget.
82 */
83 int getBudgetedResourceCount() const { return fBudgetedCount; }
84
85 /**
86 * Returns the number of bytes consumed by resources.
bsalomon71cb0c22014-11-14 12:10:14 -080087 */
88 size_t getResourceBytes() const { return fBytes; }
89
90 /**
Derek Sollenbergeree479142017-05-24 11:41:33 -040091 * Returns the number of bytes held by unlocked reosources which are available for purging.
92 */
93 size_t getPurgeableBytes() const { return fPurgeableBytes; }
94
95 /**
bsalomondace19e2014-11-17 07:34:06 -080096 * Returns the number of bytes consumed by budgeted resources.
97 */
98 size_t getBudgetedResourceBytes() const { return fBudgetedBytes; }
99
100 /**
bsalomon71cb0c22014-11-14 12:10:14 -0800101 * Returns the cached resources count budget.
102 */
103 int getMaxResourceCount() const { return fMaxCount; }
104
105 /**
106 * Returns the number of bytes consumed by cached resources.
107 */
108 size_t getMaxResourceBytes() const { return fMaxBytes; }
109
110 /**
111 * Abandons the backend API resources owned by all GrGpuResource objects and removes them from
112 * the cache.
113 */
bsalomonc8dc1f72014-08-21 13:02:13 -0700114 void abandonAll();
115
bsalomon71cb0c22014-11-14 12:10:14 -0800116 /**
117 * Releases the backend API resources owned by all GrGpuResource objects and removes them from
118 * the cache.
119 */
bsalomonc8dc1f72014-08-21 13:02:13 -0700120 void releaseAll();
121
Chris Daltond004e0b2018-09-27 09:28:03 -0600122 enum class ScratchFlags {
123 kNone = 0,
bsalomon000f8292014-10-15 19:04:14 -0700124 /** Preferentially returns scratch resources with no pending IO. */
Chris Daltond004e0b2018-09-27 09:28:03 -0600125 kPreferNoPendingIO = 0x1,
bsalomon000f8292014-10-15 19:04:14 -0700126 /** Will not return any resources that match but have pending IO. */
Chris Daltond004e0b2018-09-27 09:28:03 -0600127 kRequireNoPendingIO = 0x2,
bsalomon000f8292014-10-15 19:04:14 -0700128 };
bsalomon71cb0c22014-11-14 12:10:14 -0800129
130 /**
131 * Find a resource that matches a scratch key.
132 */
Chris Daltond004e0b2018-09-27 09:28:03 -0600133 GrGpuResource* findAndRefScratchResource(const GrScratchKey& scratchKey, size_t resourceSize,
134 ScratchFlags);
halcanary9d524f22016-03-29 09:03:52 -0700135
bsalomon8b79d232014-11-10 10:19:06 -0800136#ifdef SK_DEBUG
137 // This is not particularly fast and only used for validation, so debug only.
bsalomon7775c852014-12-30 12:50:52 -0800138 int countScratchEntriesForKey(const GrScratchKey& scratchKey) const {
bsalomon8b79d232014-11-10 10:19:06 -0800139 return fScratchMap.countForKey(scratchKey);
140 }
141#endif
142
bsalomon71cb0c22014-11-14 12:10:14 -0800143 /**
bsalomon8718aaf2015-02-19 07:24:21 -0800144 * Find a resource that matches a unique key.
bsalomon71cb0c22014-11-14 12:10:14 -0800145 */
robertphillipsee843b22016-10-04 05:30:20 -0700146 GrGpuResource* findAndRefUniqueResource(const GrUniqueKey& key) {
147 GrGpuResource* resource = fUniqueHash.find(key);
148 if (resource) {
149 this->refAndMakeResourceMRU(resource);
150 }
151 return resource;
152 }
bsalomon8b79d232014-11-10 10:19:06 -0800153
Greg Danielcd871402017-09-26 12:49:26 -0400154 /**
bsalomon8718aaf2015-02-19 07:24:21 -0800155 * Query whether a unique key exists in the cache.
bsalomon71cb0c22014-11-14 12:10:14 -0800156 */
bsalomon8718aaf2015-02-19 07:24:21 -0800157 bool hasUniqueKey(const GrUniqueKey& key) const {
158 return SkToBool(fUniqueHash.find(key));
bsalomon8b79d232014-11-10 10:19:06 -0800159 }
bsalomonbcf0a522014-10-08 08:40:09 -0700160
bsalomon8718aaf2015-02-19 07:24:21 -0800161 /** Purges resources to become under budget and processes resources with invalidated unique
bsalomon23e619c2015-02-06 11:54:28 -0800162 keys. */
robertphillipsee843b22016-10-04 05:30:20 -0700163 void purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800164
bsalomon71cb0c22014-11-14 12:10:14 -0800165 /** Purges all resources that don't have external owners. */
Robert Phillips6eba0632018-03-28 12:25:42 -0400166 void purgeAllUnlocked() { this->purgeUnlockedResources(false); }
167
168 // Purge unlocked resources. If 'scratchResourcesOnly' is true the purgeable resources
169 // containing persistent data are spared. If it is false then all purgeable resources will
170 // be deleted.
171 void purgeUnlockedResources(bool scratchResourcesOnly);
bsalomon71cb0c22014-11-14 12:10:14 -0800172
Brian Salomon5e150852017-03-22 14:53:13 -0400173 /** Purge all resources not used since the passed in time. */
174 void purgeResourcesNotUsedSince(GrStdSteadyClock::time_point);
175
Robert Phillipseafd48a2017-11-16 07:52:08 -0500176 bool overBudget() const { return fBudgetedBytes > fMaxBytes || fBudgetedCount > fMaxCount; }
177
Derek Sollenberger5480a182017-05-25 16:43:59 -0400178 /**
179 * Purge unlocked resources from the cache until the the provided byte count has been reached
180 * or we have purged all unlocked resources. The default policy is to purge in LRU order, but
181 * can be overridden to prefer purging scratch resources (in LRU order) prior to purging other
182 * resource types.
183 *
184 * @param maxBytesToPurge the desired number of bytes to be purged.
185 * @param preferScratchResources If true scratch resources will be purged prior to other
186 * resource types.
187 */
188 void purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources);
189
bsalomonb77a9072016-09-07 10:02:04 -0700190 /** Returns true if the cache would like a flush to occur in order to make more resources
191 purgeable. */
Brian Salomon57d2bea2018-09-10 09:35:41 -0400192 bool requestsFlush() const { return this->overBudget() && !fPurgeableQueue.count(); }
bsalomon71cb0c22014-11-14 12:10:14 -0800193
Brian Osman13dddce2017-05-09 13:19:50 -0400194 /** Maintain a ref to this resource until we receive a GrGpuResourceFreedMessage. */
195 void insertCrossContextGpuResource(GrGpuResource* resource);
196
robertphillips60029a52015-11-09 13:51:06 -0800197#if GR_CACHE_STATS
198 struct Stats {
199 int fTotal;
200 int fNumPurgeable;
201 int fNumNonPurgeable;
202
203 int fScratch;
kkinnunen2e6055b2016-04-22 01:48:29 -0700204 int fWrapped;
robertphillips60029a52015-11-09 13:51:06 -0800205 size_t fUnbudgetedSize;
206
207 Stats() { this->reset(); }
208
209 void reset() {
210 fTotal = 0;
211 fNumPurgeable = 0;
212 fNumNonPurgeable = 0;
213 fScratch = 0;
kkinnunen2e6055b2016-04-22 01:48:29 -0700214 fWrapped = 0;
robertphillips60029a52015-11-09 13:51:06 -0800215 fUnbudgetedSize = 0;
216 }
217
218 void update(GrGpuResource* resource) {
219 if (resource->cacheAccess().isScratch()) {
220 ++fScratch;
221 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700222 if (resource->resourcePriv().refsWrappedObjects()) {
223 ++fWrapped;
robertphillips60029a52015-11-09 13:51:06 -0800224 }
bsalomon5ec26ae2016-02-25 08:33:02 -0800225 if (SkBudgeted::kNo == resource->resourcePriv().isBudgeted()) {
robertphillips60029a52015-11-09 13:51:06 -0800226 fUnbudgetedSize += resource->gpuMemorySize();
227 }
228 }
229 };
230
231 void getStats(Stats*) const;
232
mtkleinb9eb4ac2015-02-02 18:26:03 -0800233 void dumpStats(SkString*) const;
joshualittdc5685a2015-12-02 14:08:25 -0800234
235 void dumpStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* value) const;
bsalomon71cb0c22014-11-14 12:10:14 -0800236#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*);
bsalomon3f324322015-04-08 11:01:54 -0700256 void notifyCntReachedZero(GrGpuResource*, uint32_t flags);
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*);
bsalomon9f2d1572015-02-17 11:47:40 -0800261 void refAndMakeResourceMRU(GrGpuResource*);
bsalomon71cb0c22014-11-14 12:10:14 -0800262 /// @}
263
bsalomon8718aaf2015-02-19 07:24:21 -0800264 void processInvalidUniqueKeys(const SkTArray<GrUniqueKeyInvalidatedMessage>&);
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
robertphillips6e83ac72015-08-13 05:19:14 -0700269 bool wouldFit(size_t bytes) {
halcanary9d524f22016-03-29 09:03:52 -0700270 return fBudgetedBytes+bytes <= fMaxBytes && fBudgetedCount+1 <= fMaxCount;
robertphillips6e83ac72015-08-13 05:19:14 -0700271 }
272
bsalomonddf30e62015-02-19 11:38:44 -0800273 uint32_t getNextTimestamp();
274
bsalomon16961262014-08-26 14:01:07 -0700275#ifdef SK_DEBUG
bsalomonf320e042015-02-17 15:09:34 -0800276 bool isInCache(const GrGpuResource* r) const;
bsalomon71cb0c22014-11-14 12:10:14 -0800277 void validate() const;
278#else
279 void validate() const {}
bsalomon16961262014-08-26 14:01:07 -0700280#endif
281
bsalomon71cb0c22014-11-14 12:10:14 -0800282 class AutoValidate;
283
bsalomonbcf0a522014-10-08 08:40:09 -0700284 class AvailableForScratchUse;
bsalomon744998e2014-08-28 09:54:34 -0700285
robertphillipsee843b22016-10-04 05:30:20 -0700286 struct ScratchMapTraits {
bsalomon7775c852014-12-30 12:50:52 -0800287 static const GrScratchKey& GetKey(const GrGpuResource& r) {
bsalomon3582d3e2015-02-13 14:20:05 -0800288 return r.resourcePriv().getScratchKey();
bsalomon744998e2014-08-28 09:54:34 -0700289 }
robertphillipsee843b22016-10-04 05:30:20 -0700290
291 static uint32_t Hash(const GrScratchKey& key) { return key.hash(); }
Robert Phillipsf8e25022017-11-08 15:24:31 -0500292 static void OnFree(GrGpuResource*) { }
bsalomon744998e2014-08-28 09:54:34 -0700293 };
bsalomon7775c852014-12-30 12:50:52 -0800294 typedef SkTMultiMap<GrGpuResource, GrScratchKey, ScratchMapTraits> ScratchMap;
bsalomon744998e2014-08-28 09:54:34 -0700295
robertphillipsee843b22016-10-04 05:30:20 -0700296 struct UniqueHashTraits {
bsalomon8718aaf2015-02-19 07:24:21 -0800297 static const GrUniqueKey& GetKey(const GrGpuResource& r) { return r.getUniqueKey(); }
robertphillipsee843b22016-10-04 05:30:20 -0700298
299 static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); }
bsalomon8b79d232014-11-10 10:19:06 -0800300 };
bsalomon8718aaf2015-02-19 07:24:21 -0800301 typedef SkTDynamicHash<GrGpuResource, GrUniqueKey, UniqueHashTraits> UniqueHash;
bsalomon8b79d232014-11-10 10:19:06 -0800302
bsalomon9f2d1572015-02-17 11:47:40 -0800303 static bool CompareTimestamp(GrGpuResource* const& a, GrGpuResource* const& b) {
304 return a->cacheAccess().timestamp() < b->cacheAccess().timestamp();
305 }
bsalomon23e619c2015-02-06 11:54:28 -0800306
bsalomon9f2d1572015-02-17 11:47:40 -0800307 static int* AccessResourceIndex(GrGpuResource* const& res) {
308 return res->cacheAccess().accessCacheIndex();
309 }
310
bsalomon8718aaf2015-02-19 07:24:21 -0800311 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage>::Inbox InvalidUniqueKeyInbox;
Brian Osman13dddce2017-05-09 13:19:50 -0400312 typedef SkMessageBus<GrGpuResourceFreedMessage>::Inbox FreedGpuResourceInbox;
bsalomon9f2d1572015-02-17 11:47:40 -0800313 typedef SkTDPQueue<GrGpuResource*, CompareTimestamp, AccessResourceIndex> PurgeableQueue;
bsalomonf320e042015-02-17 15:09:34 -0800314 typedef SkTDArray<GrGpuResource*> ResourceArray;
bsalomon9f2d1572015-02-17 11:47:40 -0800315
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500316 GrProxyProvider* fProxyProvider;
bsalomon9f2d1572015-02-17 11:47:40 -0800317 // Whenever a resource is added to the cache or the result of a cache lookup, fTimestamp is
318 // assigned as the resource's timestamp and then incremented. fPurgeableQueue orders the
319 // purgeable resources by this value, and thus is used to purge resources in LRU order.
320 uint32_t fTimestamp;
321 PurgeableQueue fPurgeableQueue;
bsalomonf320e042015-02-17 15:09:34 -0800322 ResourceArray fNonpurgeableResources;
bsalomon9f2d1572015-02-17 11:47:40 -0800323
bsalomon744998e2014-08-28 09:54:34 -0700324 // This map holds all resources that can be used as scratch resources.
bsalomon8b79d232014-11-10 10:19:06 -0800325 ScratchMap fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800326 // This holds all resources that have unique keys.
327 UniqueHash fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800328
329 // our budget, used in purgeAsNeeded()
330 int fMaxCount;
331 size_t fMaxBytes;
332
333#if GR_CACHE_STATS
334 int fHighWaterCount;
335 size_t fHighWaterBytes;
bsalomondace19e2014-11-17 07:34:06 -0800336 int fBudgetedHighWaterCount;
337 size_t fBudgetedHighWaterBytes;
bsalomon71cb0c22014-11-14 12:10:14 -0800338#endif
339
bsalomondace19e2014-11-17 07:34:06 -0800340 // our current stats for all resources
bsalomonf320e042015-02-17 15:09:34 -0800341 SkDEBUGCODE(int fCount;)
bsalomon71cb0c22014-11-14 12:10:14 -0800342 size_t fBytes;
343
bsalomondace19e2014-11-17 07:34:06 -0800344 // our current stats for resources that count against the budget
345 int fBudgetedCount;
346 size_t fBudgetedBytes;
Derek Sollenbergeree479142017-05-24 11:41:33 -0400347 size_t fPurgeableBytes;
bsalomondace19e2014-11-17 07:34:06 -0800348
bsalomon8718aaf2015-02-19 07:24:21 -0800349 InvalidUniqueKeyInbox fInvalidUniqueKeyInbox;
Brian Osman13dddce2017-05-09 13:19:50 -0400350 FreedGpuResourceInbox fFreedGpuResourceInbox;
351
Greg Danielc27eb722018-08-10 09:48:08 -0400352 SkTDArray<GrGpuResource*> fResourcesWaitingForFreeMsg;
353
Brian Osman13dddce2017-05-09 13:19:50 -0400354 uint32_t fContextUniqueID;
bsalomon3f324322015-04-08 11:01:54 -0700355
356 // This resource is allowed to be in the nonpurgeable array for the sake of validate() because
357 // we're in the midst of converting it to purgeable status.
358 SkDEBUGCODE(GrGpuResource* fNewlyPurgeableResourceForValidation;)
robertphillips63926682015-08-20 09:39:02 -0700359
360 bool fPreferVRAMUseOverFlushes;
bsalomonc8dc1f72014-08-21 13:02:13 -0700361};
362
Chris Daltond004e0b2018-09-27 09:28:03 -0600363GR_MAKE_BITFIELD_CLASS_OPS(GrResourceCache::ScratchFlags);
364
bsalomon0ea80f42015-02-11 10:49:59 -0800365class GrResourceCache::ResourceAccess {
bsalomon71cb0c22014-11-14 12:10:14 -0800366private:
bsalomon0ea80f42015-02-11 10:49:59 -0800367 ResourceAccess(GrResourceCache* cache) : fCache(cache) { }
bsalomon71cb0c22014-11-14 12:10:14 -0800368 ResourceAccess(const ResourceAccess& that) : fCache(that.fCache) { }
369 ResourceAccess& operator=(const ResourceAccess&); // unimpl
370
371 /**
372 * Insert a resource into the cache.
373 */
374 void insertResource(GrGpuResource* resource) { fCache->insertResource(resource); }
375
376 /**
377 * Removes a resource from the cache.
378 */
379 void removeResource(GrGpuResource* resource) { fCache->removeResource(resource); }
380
381 /**
bsalomon3f324322015-04-08 11:01:54 -0700382 * Notifications that should be sent to the cache when the ref/io cnt status of resources
383 * changes.
bsalomon71cb0c22014-11-14 12:10:14 -0800384 */
bsalomon3f324322015-04-08 11:01:54 -0700385 enum RefNotificationFlags {
386 /** All types of refs on the resource have reached zero. */
387 kAllCntsReachedZero_RefNotificationFlag = 0x1,
388 /** The normal (not pending IO type) ref cnt has reached zero. */
389 kRefCntReachedZero_RefNotificationFlag = 0x2,
390 };
391 /**
392 * Called by GrGpuResources when they detect that their ref/io cnts have reached zero. When the
393 * normal ref cnt reaches zero the flags that are set should be:
394 * a) kRefCntReachedZero if a pending IO cnt is still non-zero.
395 * b) (kRefCntReachedZero | kAllCntsReachedZero) when all pending IO cnts are also zero.
396 * kAllCntsReachedZero is set by itself if a pending IO cnt is decremented to zero and all the
397 * the other cnts are already zero.
398 */
399 void notifyCntReachedZero(GrGpuResource* resource, uint32_t flags) {
400 fCache->notifyCntReachedZero(resource, flags);
401 }
bsalomon71cb0c22014-11-14 12:10:14 -0800402
403 /**
bsalomonf99e9612015-02-19 08:24:16 -0800404 * Called by GrGpuResources to change their unique keys.
bsalomon71cb0c22014-11-14 12:10:14 -0800405 */
bsalomonf99e9612015-02-19 08:24:16 -0800406 void changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) {
407 fCache->changeUniqueKey(resource, newKey);
408 }
bsalomon71cb0c22014-11-14 12:10:14 -0800409
bsalomon10e23ca2014-11-25 05:52:06 -0800410 /**
bsalomonf99e9612015-02-19 08:24:16 -0800411 * Called by a GrGpuResource to remove its unique key.
bsalomon23e619c2015-02-06 11:54:28 -0800412 */
bsalomonf99e9612015-02-19 08:24:16 -0800413 void removeUniqueKey(GrGpuResource* resource) { fCache->removeUniqueKey(resource); }
bsalomon23e619c2015-02-06 11:54:28 -0800414
415 /**
416 * Called by a GrGpuResource when it removes its scratch key.
bsalomon10e23ca2014-11-25 05:52:06 -0800417 */
418 void willRemoveScratchKey(const GrGpuResource* resource) {
419 fCache->willRemoveScratchKey(resource);
420 }
bsalomon84c8e622014-11-17 09:33:27 -0800421
422 /**
423 * Called by GrGpuResources when they change from budgeted to unbudgeted or vice versa.
424 */
425 void didChangeBudgetStatus(GrGpuResource* resource) { fCache->didChangeBudgetStatus(resource); }
426
bsalomon71cb0c22014-11-14 12:10:14 -0800427 // No taking addresses of this type.
428 const ResourceAccess* operator&() const;
429 ResourceAccess* operator&();
430
bsalomon0ea80f42015-02-11 10:49:59 -0800431 GrResourceCache* fCache;
bsalomon71cb0c22014-11-14 12:10:14 -0800432
433 friend class GrGpuResource; // To access all the proxy inline methods.
bsalomon0ea80f42015-02-11 10:49:59 -0800434 friend class GrResourceCache; // To create this type.
bsalomon71cb0c22014-11-14 12:10:14 -0800435};
436
bsalomon0ea80f42015-02-11 10:49:59 -0800437inline GrResourceCache::ResourceAccess GrResourceCache::resourceAccess() {
bsalomon71cb0c22014-11-14 12:10:14 -0800438 return ResourceAccess(this);
439}
440
bsalomonc8dc1f72014-08-21 13:02:13 -0700441#endif