blob: a5826486cac9b9bb8f06427f2096a61527e65e1b [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"
Brian Salomon876a0172019-03-08 11:12:14 -050019#include "SkTHash.h"
bsalomonc8dc1f72014-08-21 13:02:13 -070020#include "SkTInternalLList.h"
bsalomon744998e2014-08-28 09:54:34 -070021#include "SkTMultiMap.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 budgeted resources in the cache.
63 static const int kDefaultMaxCount = 2 * (1 << 12);
64 // Default maximum number of bytes of gpu memory of budgeted resources in the cache.
65 static const size_t kDefaultMaxSize = 96 * (1 << 20);
bsalomon3f324322015-04-08 11:01:54 -070066
bsalomon71cb0c22014-11-14 12:10:14 -080067 /** Used to access functionality needed by GrGpuResource for lifetime management. */
68 class ResourceAccess;
69 ResourceAccess resourceAccess();
bsalomonc8dc1f72014-08-21 13:02:13 -070070
Brian Salomon238069b2018-07-11 15:58:57 -040071 /** Unique ID of the owning GrContext. */
72 uint32_t contextUniqueID() const { return fContextUniqueID; }
73
Brian Salomon43b882b2018-09-07 16:29:14 -040074 /** Sets the cache limits in terms of number of resources and max gpu memory byte size. */
75 void setLimits(int count, size_t bytes);
bsalomonc8dc1f72014-08-21 13:02:13 -070076
bsalomon71cb0c22014-11-14 12:10:14 -080077 /**
bsalomondace19e2014-11-17 07:34:06 -080078 * Returns the number of resources.
bsalomon71cb0c22014-11-14 12:10:14 -080079 */
bsalomonf320e042015-02-17 15:09:34 -080080 int getResourceCount() const {
81 return fPurgeableQueue.count() + fNonpurgeableResources.count();
82 }
bsalomon8b79d232014-11-10 10:19:06 -080083
bsalomon71cb0c22014-11-14 12:10:14 -080084 /**
bsalomondace19e2014-11-17 07:34:06 -080085 * Returns the number of resources that count against the budget.
86 */
87 int getBudgetedResourceCount() const { return fBudgetedCount; }
88
89 /**
90 * Returns the number of bytes consumed by resources.
bsalomon71cb0c22014-11-14 12:10:14 -080091 */
92 size_t getResourceBytes() const { return fBytes; }
93
94 /**
Derek Sollenbergeree479142017-05-24 11:41:33 -040095 * Returns the number of bytes held by unlocked reosources which are available for purging.
96 */
97 size_t getPurgeableBytes() const { return fPurgeableBytes; }
98
99 /**
bsalomondace19e2014-11-17 07:34:06 -0800100 * Returns the number of bytes consumed by budgeted resources.
101 */
102 size_t getBudgetedResourceBytes() const { return fBudgetedBytes; }
103
104 /**
bsalomon71cb0c22014-11-14 12:10:14 -0800105 * Returns the cached resources count budget.
106 */
107 int getMaxResourceCount() const { return fMaxCount; }
108
109 /**
110 * Returns the number of bytes consumed by cached resources.
111 */
112 size_t getMaxResourceBytes() const { return fMaxBytes; }
113
114 /**
115 * Abandons the backend API resources owned by all GrGpuResource objects and removes them from
116 * the cache.
117 */
bsalomonc8dc1f72014-08-21 13:02:13 -0700118 void abandonAll();
119
bsalomon71cb0c22014-11-14 12:10:14 -0800120 /**
121 * Releases the backend API resources owned by all GrGpuResource objects and removes them from
122 * the cache.
123 */
bsalomonc8dc1f72014-08-21 13:02:13 -0700124 void releaseAll();
125
Chris Daltond004e0b2018-09-27 09:28:03 -0600126 enum class ScratchFlags {
127 kNone = 0,
bsalomon000f8292014-10-15 19:04:14 -0700128 /** Preferentially returns scratch resources with no pending IO. */
Chris Daltond004e0b2018-09-27 09:28:03 -0600129 kPreferNoPendingIO = 0x1,
bsalomon000f8292014-10-15 19:04:14 -0700130 /** Will not return any resources that match but have pending IO. */
Chris Daltond004e0b2018-09-27 09:28:03 -0600131 kRequireNoPendingIO = 0x2,
bsalomon000f8292014-10-15 19:04:14 -0700132 };
bsalomon71cb0c22014-11-14 12:10:14 -0800133
134 /**
135 * Find a resource that matches a scratch key.
136 */
Chris Daltond004e0b2018-09-27 09:28:03 -0600137 GrGpuResource* findAndRefScratchResource(const GrScratchKey& scratchKey, size_t resourceSize,
138 ScratchFlags);
halcanary9d524f22016-03-29 09:03:52 -0700139
bsalomon8b79d232014-11-10 10:19:06 -0800140#ifdef SK_DEBUG
141 // This is not particularly fast and only used for validation, so debug only.
bsalomon7775c852014-12-30 12:50:52 -0800142 int countScratchEntriesForKey(const GrScratchKey& scratchKey) const {
bsalomon8b79d232014-11-10 10:19:06 -0800143 return fScratchMap.countForKey(scratchKey);
144 }
145#endif
146
bsalomon71cb0c22014-11-14 12:10:14 -0800147 /**
bsalomon8718aaf2015-02-19 07:24:21 -0800148 * Find a resource that matches a unique key.
bsalomon71cb0c22014-11-14 12:10:14 -0800149 */
robertphillipsee843b22016-10-04 05:30:20 -0700150 GrGpuResource* findAndRefUniqueResource(const GrUniqueKey& key) {
151 GrGpuResource* resource = fUniqueHash.find(key);
152 if (resource) {
153 this->refAndMakeResourceMRU(resource);
154 }
155 return resource;
156 }
bsalomon8b79d232014-11-10 10:19:06 -0800157
Greg Danielcd871402017-09-26 12:49:26 -0400158 /**
bsalomon8718aaf2015-02-19 07:24:21 -0800159 * Query whether a unique key exists in the cache.
bsalomon71cb0c22014-11-14 12:10:14 -0800160 */
bsalomon8718aaf2015-02-19 07:24:21 -0800161 bool hasUniqueKey(const GrUniqueKey& key) const {
162 return SkToBool(fUniqueHash.find(key));
bsalomon8b79d232014-11-10 10:19:06 -0800163 }
bsalomonbcf0a522014-10-08 08:40:09 -0700164
bsalomon8718aaf2015-02-19 07:24:21 -0800165 /** Purges resources to become under budget and processes resources with invalidated unique
bsalomon23e619c2015-02-06 11:54:28 -0800166 keys. */
robertphillipsee843b22016-10-04 05:30:20 -0700167 void purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800168
bsalomon71cb0c22014-11-14 12:10:14 -0800169 /** Purges all resources that don't have external owners. */
Robert Phillips6eba0632018-03-28 12:25:42 -0400170 void purgeAllUnlocked() { this->purgeUnlockedResources(false); }
171
172 // Purge unlocked resources. If 'scratchResourcesOnly' is true the purgeable resources
173 // containing persistent data are spared. If it is false then all purgeable resources will
174 // be deleted.
175 void purgeUnlockedResources(bool scratchResourcesOnly);
bsalomon71cb0c22014-11-14 12:10:14 -0800176
Brian Salomon5e150852017-03-22 14:53:13 -0400177 /** Purge all resources not used since the passed in time. */
178 void purgeResourcesNotUsedSince(GrStdSteadyClock::time_point);
179
Robert Phillipseafd48a2017-11-16 07:52:08 -0500180 bool overBudget() const { return fBudgetedBytes > fMaxBytes || fBudgetedCount > fMaxCount; }
181
Derek Sollenberger5480a182017-05-25 16:43:59 -0400182 /**
183 * Purge unlocked resources from the cache until the the provided byte count has been reached
184 * or we have purged all unlocked resources. The default policy is to purge in LRU order, but
185 * can be overridden to prefer purging scratch resources (in LRU order) prior to purging other
186 * resource types.
187 *
188 * @param maxBytesToPurge the desired number of bytes to be purged.
189 * @param preferScratchResources If true scratch resources will be purged prior to other
190 * resource types.
191 */
192 void purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources);
193
bsalomonb77a9072016-09-07 10:02:04 -0700194 /** Returns true if the cache would like a flush to occur in order to make more resources
195 purgeable. */
Brian Salomon8cefa3e2019-04-04 11:39:55 -0400196 bool requestsFlush() const;
bsalomon71cb0c22014-11-14 12:10:14 -0800197
Brian Osman13dddce2017-05-09 13:19:50 -0400198 /** Maintain a ref to this resource until we receive a GrGpuResourceFreedMessage. */
Brian Salomon876a0172019-03-08 11:12:14 -0500199 void insertDelayedResourceUnref(GrGpuResource* resource);
Brian Osman13dddce2017-05-09 13:19:50 -0400200
robertphillips60029a52015-11-09 13:51:06 -0800201#if GR_CACHE_STATS
202 struct Stats {
203 int fTotal;
204 int fNumPurgeable;
205 int fNumNonPurgeable;
206
207 int fScratch;
kkinnunen2e6055b2016-04-22 01:48:29 -0700208 int fWrapped;
robertphillips60029a52015-11-09 13:51:06 -0800209 size_t fUnbudgetedSize;
210
211 Stats() { this->reset(); }
212
213 void reset() {
214 fTotal = 0;
215 fNumPurgeable = 0;
216 fNumNonPurgeable = 0;
217 fScratch = 0;
kkinnunen2e6055b2016-04-22 01:48:29 -0700218 fWrapped = 0;
robertphillips60029a52015-11-09 13:51:06 -0800219 fUnbudgetedSize = 0;
220 }
221
222 void update(GrGpuResource* resource) {
223 if (resource->cacheAccess().isScratch()) {
224 ++fScratch;
225 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700226 if (resource->resourcePriv().refsWrappedObjects()) {
227 ++fWrapped;
robertphillips60029a52015-11-09 13:51:06 -0800228 }
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500229 if (GrBudgetedType::kBudgeted != resource->resourcePriv().budgetedType()) {
robertphillips60029a52015-11-09 13:51:06 -0800230 fUnbudgetedSize += resource->gpuMemorySize();
231 }
232 }
233 };
234
235 void getStats(Stats*) const;
236
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500237#if GR_TEST_UTILS
mtkleinb9eb4ac2015-02-02 18:26:03 -0800238 void dumpStats(SkString*) const;
joshualittdc5685a2015-12-02 14:08:25 -0800239
240 void dumpStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* value) const;
bsalomon71cb0c22014-11-14 12:10:14 -0800241#endif
242
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500243#endif
244
Brian Salomon1090da62017-01-06 12:04:19 -0500245#ifdef SK_DEBUG
246 int countUniqueKeysWithTag(const char* tag) const;
247#endif
248
bsalomonddf30e62015-02-19 11:38:44 -0800249 // This function is for unit testing and is only defined in test tools.
250 void changeTimestamp(uint32_t newTimestamp);
251
ericrk0a5fa482015-09-15 14:16:10 -0700252 // Enumerates all cached resources and dumps their details to traceMemoryDump.
253 void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const;
254
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500255 void setProxyProvider(GrProxyProvider* proxyProvider) { fProxyProvider = proxyProvider; }
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400256
bsalomonc8dc1f72014-08-21 13:02:13 -0700257private:
bsalomon71cb0c22014-11-14 12:10:14 -0800258 ///////////////////////////////////////////////////////////////////////////
259 /// @name Methods accessible via ResourceAccess
260 ////
261 void insertResource(GrGpuResource*);
262 void removeResource(GrGpuResource*);
bsalomon3f324322015-04-08 11:01:54 -0700263 void notifyCntReachedZero(GrGpuResource*, uint32_t flags);
bsalomonf99e9612015-02-19 08:24:16 -0800264 void changeUniqueKey(GrGpuResource*, const GrUniqueKey&);
265 void removeUniqueKey(GrGpuResource*);
bsalomon10e23ca2014-11-25 05:52:06 -0800266 void willRemoveScratchKey(const GrGpuResource*);
bsalomon84c8e622014-11-17 09:33:27 -0800267 void didChangeBudgetStatus(GrGpuResource*);
Brian Salomon2c791fc2019-04-02 11:52:03 -0400268 void refResource(GrGpuResource* resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800269 /// @}
270
Brian Salomon2c791fc2019-04-02 11:52:03 -0400271 void refAndMakeResourceMRU(GrGpuResource*);
Brian Osman13dddce2017-05-09 13:19:50 -0400272 void processFreedGpuResources();
bsalomonf320e042015-02-17 15:09:34 -0800273 void addToNonpurgeableArray(GrGpuResource*);
274 void removeFromNonpurgeableArray(GrGpuResource*);
bsalomon71cb0c22014-11-14 12:10:14 -0800275
robertphillips6e83ac72015-08-13 05:19:14 -0700276 bool wouldFit(size_t bytes) {
halcanary9d524f22016-03-29 09:03:52 -0700277 return fBudgetedBytes+bytes <= fMaxBytes && fBudgetedCount+1 <= fMaxCount;
robertphillips6e83ac72015-08-13 05:19:14 -0700278 }
279
bsalomonddf30e62015-02-19 11:38:44 -0800280 uint32_t getNextTimestamp();
281
bsalomon16961262014-08-26 14:01:07 -0700282#ifdef SK_DEBUG
bsalomonf320e042015-02-17 15:09:34 -0800283 bool isInCache(const GrGpuResource* r) const;
bsalomon71cb0c22014-11-14 12:10:14 -0800284 void validate() const;
285#else
286 void validate() const {}
bsalomon16961262014-08-26 14:01:07 -0700287#endif
288
bsalomon71cb0c22014-11-14 12:10:14 -0800289 class AutoValidate;
290
bsalomonbcf0a522014-10-08 08:40:09 -0700291 class AvailableForScratchUse;
bsalomon744998e2014-08-28 09:54:34 -0700292
robertphillipsee843b22016-10-04 05:30:20 -0700293 struct ScratchMapTraits {
bsalomon7775c852014-12-30 12:50:52 -0800294 static const GrScratchKey& GetKey(const GrGpuResource& r) {
bsalomon3582d3e2015-02-13 14:20:05 -0800295 return r.resourcePriv().getScratchKey();
bsalomon744998e2014-08-28 09:54:34 -0700296 }
robertphillipsee843b22016-10-04 05:30:20 -0700297
298 static uint32_t Hash(const GrScratchKey& key) { return key.hash(); }
Robert Phillipsf8e25022017-11-08 15:24:31 -0500299 static void OnFree(GrGpuResource*) { }
bsalomon744998e2014-08-28 09:54:34 -0700300 };
bsalomon7775c852014-12-30 12:50:52 -0800301 typedef SkTMultiMap<GrGpuResource, GrScratchKey, ScratchMapTraits> ScratchMap;
bsalomon744998e2014-08-28 09:54:34 -0700302
robertphillipsee843b22016-10-04 05:30:20 -0700303 struct UniqueHashTraits {
bsalomon8718aaf2015-02-19 07:24:21 -0800304 static const GrUniqueKey& GetKey(const GrGpuResource& r) { return r.getUniqueKey(); }
robertphillipsee843b22016-10-04 05:30:20 -0700305
306 static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); }
bsalomon8b79d232014-11-10 10:19:06 -0800307 };
bsalomon8718aaf2015-02-19 07:24:21 -0800308 typedef SkTDynamicHash<GrGpuResource, GrUniqueKey, UniqueHashTraits> UniqueHash;
bsalomon8b79d232014-11-10 10:19:06 -0800309
Brian Salomon876a0172019-03-08 11:12:14 -0500310 class ResourceAwaitingUnref {
311 public:
312 ResourceAwaitingUnref();
313 ResourceAwaitingUnref(GrGpuResource* resource);
314 ResourceAwaitingUnref(const ResourceAwaitingUnref&) = delete;
315 ResourceAwaitingUnref& operator=(const ResourceAwaitingUnref&) = delete;
316 ResourceAwaitingUnref(ResourceAwaitingUnref&&);
317 ResourceAwaitingUnref& operator=(ResourceAwaitingUnref&&);
318 ~ResourceAwaitingUnref();
319 void addRef();
320 void unref();
321 bool finished();
322
323 private:
324 GrGpuResource* fResource = nullptr;
325 int fNumUnrefs = 0;
326 };
327 using ReourcesAwaitingUnref = SkTHashMap<uint32_t, ResourceAwaitingUnref>;
328
bsalomon9f2d1572015-02-17 11:47:40 -0800329 static bool CompareTimestamp(GrGpuResource* const& a, GrGpuResource* const& b) {
330 return a->cacheAccess().timestamp() < b->cacheAccess().timestamp();
331 }
bsalomon23e619c2015-02-06 11:54:28 -0800332
bsalomon9f2d1572015-02-17 11:47:40 -0800333 static int* AccessResourceIndex(GrGpuResource* const& res) {
334 return res->cacheAccess().accessCacheIndex();
335 }
336
bsalomon8718aaf2015-02-19 07:24:21 -0800337 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage>::Inbox InvalidUniqueKeyInbox;
Brian Osman13dddce2017-05-09 13:19:50 -0400338 typedef SkMessageBus<GrGpuResourceFreedMessage>::Inbox FreedGpuResourceInbox;
bsalomon9f2d1572015-02-17 11:47:40 -0800339 typedef SkTDPQueue<GrGpuResource*, CompareTimestamp, AccessResourceIndex> PurgeableQueue;
bsalomonf320e042015-02-17 15:09:34 -0800340 typedef SkTDArray<GrGpuResource*> ResourceArray;
bsalomon9f2d1572015-02-17 11:47:40 -0800341
Brian Salomon2c791fc2019-04-02 11:52:03 -0400342 GrProxyProvider* fProxyProvider = nullptr;
bsalomon9f2d1572015-02-17 11:47:40 -0800343 // Whenever a resource is added to the cache or the result of a cache lookup, fTimestamp is
344 // assigned as the resource's timestamp and then incremented. fPurgeableQueue orders the
345 // purgeable resources by this value, and thus is used to purge resources in LRU order.
Brian Salomon2c791fc2019-04-02 11:52:03 -0400346 uint32_t fTimestamp = 0;
bsalomon9f2d1572015-02-17 11:47:40 -0800347 PurgeableQueue fPurgeableQueue;
bsalomonf320e042015-02-17 15:09:34 -0800348 ResourceArray fNonpurgeableResources;
bsalomon9f2d1572015-02-17 11:47:40 -0800349
bsalomon744998e2014-08-28 09:54:34 -0700350 // This map holds all resources that can be used as scratch resources.
bsalomon8b79d232014-11-10 10:19:06 -0800351 ScratchMap fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800352 // This holds all resources that have unique keys.
353 UniqueHash fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800354
355 // our budget, used in purgeAsNeeded()
Brian Salomon2c791fc2019-04-02 11:52:03 -0400356 int fMaxCount = kDefaultMaxCount;
357 size_t fMaxBytes = kDefaultMaxSize;
bsalomon71cb0c22014-11-14 12:10:14 -0800358
359#if GR_CACHE_STATS
Brian Salomon2c791fc2019-04-02 11:52:03 -0400360 int fHighWaterCount = 0;
361 size_t fHighWaterBytes = 0;
362 int fBudgetedHighWaterCount = 0;
363 size_t fBudgetedHighWaterBytes = 0;
bsalomon71cb0c22014-11-14 12:10:14 -0800364#endif
365
bsalomondace19e2014-11-17 07:34:06 -0800366 // our current stats for all resources
Brian Salomon2c791fc2019-04-02 11:52:03 -0400367 SkDEBUGCODE(int fCount = 0;)
368 size_t fBytes = 0;
bsalomon71cb0c22014-11-14 12:10:14 -0800369
bsalomondace19e2014-11-17 07:34:06 -0800370 // our current stats for resources that count against the budget
Brian Salomon2c791fc2019-04-02 11:52:03 -0400371 int fBudgetedCount = 0;
372 size_t fBudgetedBytes = 0;
373 size_t fPurgeableBytes = 0;
374 int fNumBudgetedResourcesFlushWillMakePurgeable = 0;
bsalomondace19e2014-11-17 07:34:06 -0800375
bsalomon8718aaf2015-02-19 07:24:21 -0800376 InvalidUniqueKeyInbox fInvalidUniqueKeyInbox;
Brian Osman13dddce2017-05-09 13:19:50 -0400377 FreedGpuResourceInbox fFreedGpuResourceInbox;
Brian Salomon876a0172019-03-08 11:12:14 -0500378 ReourcesAwaitingUnref fResourcesAwaitingUnref;
Greg Danielc27eb722018-08-10 09:48:08 -0400379
Brian Salomon2c791fc2019-04-02 11:52:03 -0400380 uint32_t fContextUniqueID = SK_InvalidUniqueID;
381 GrSingleOwner* fSingleOwner = nullptr;
bsalomon3f324322015-04-08 11:01:54 -0700382
383 // This resource is allowed to be in the nonpurgeable array for the sake of validate() because
384 // we're in the midst of converting it to purgeable status.
Brian Salomon2c791fc2019-04-02 11:52:03 -0400385 SkDEBUGCODE(GrGpuResource* fNewlyPurgeableResourceForValidation = nullptr;)
robertphillips63926682015-08-20 09:39:02 -0700386
Brian Salomon2c791fc2019-04-02 11:52:03 -0400387 bool fPreferVRAMUseOverFlushes = false;
bsalomonc8dc1f72014-08-21 13:02:13 -0700388};
389
Chris Daltond004e0b2018-09-27 09:28:03 -0600390GR_MAKE_BITFIELD_CLASS_OPS(GrResourceCache::ScratchFlags);
391
bsalomon0ea80f42015-02-11 10:49:59 -0800392class GrResourceCache::ResourceAccess {
bsalomon71cb0c22014-11-14 12:10:14 -0800393private:
bsalomon0ea80f42015-02-11 10:49:59 -0800394 ResourceAccess(GrResourceCache* cache) : fCache(cache) { }
bsalomon71cb0c22014-11-14 12:10:14 -0800395 ResourceAccess(const ResourceAccess& that) : fCache(that.fCache) { }
396 ResourceAccess& operator=(const ResourceAccess&); // unimpl
397
398 /**
399 * Insert a resource into the cache.
400 */
401 void insertResource(GrGpuResource* resource) { fCache->insertResource(resource); }
402
403 /**
404 * Removes a resource from the cache.
405 */
406 void removeResource(GrGpuResource* resource) { fCache->removeResource(resource); }
407
408 /**
Brian Salomon2c791fc2019-04-02 11:52:03 -0400409 * Adds a ref to a resource with proper tracking if the resource has 0 refs prior to
410 * adding the ref.
411 */
412 void refResource(GrGpuResource* resource) { fCache->refResource(resource); }
413
414 /**
bsalomon3f324322015-04-08 11:01:54 -0700415 * Notifications that should be sent to the cache when the ref/io cnt status of resources
416 * changes.
bsalomon71cb0c22014-11-14 12:10:14 -0800417 */
bsalomon3f324322015-04-08 11:01:54 -0700418 enum RefNotificationFlags {
419 /** All types of refs on the resource have reached zero. */
420 kAllCntsReachedZero_RefNotificationFlag = 0x1,
421 /** The normal (not pending IO type) ref cnt has reached zero. */
422 kRefCntReachedZero_RefNotificationFlag = 0x2,
423 };
424 /**
425 * Called by GrGpuResources when they detect that their ref/io cnts have reached zero. When the
426 * normal ref cnt reaches zero the flags that are set should be:
427 * a) kRefCntReachedZero if a pending IO cnt is still non-zero.
428 * b) (kRefCntReachedZero | kAllCntsReachedZero) when all pending IO cnts are also zero.
429 * kAllCntsReachedZero is set by itself if a pending IO cnt is decremented to zero and all the
430 * the other cnts are already zero.
431 */
432 void notifyCntReachedZero(GrGpuResource* resource, uint32_t flags) {
433 fCache->notifyCntReachedZero(resource, flags);
434 }
bsalomon71cb0c22014-11-14 12:10:14 -0800435
436 /**
bsalomonf99e9612015-02-19 08:24:16 -0800437 * Called by GrGpuResources to change their unique keys.
bsalomon71cb0c22014-11-14 12:10:14 -0800438 */
bsalomonf99e9612015-02-19 08:24:16 -0800439 void changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) {
440 fCache->changeUniqueKey(resource, newKey);
441 }
bsalomon71cb0c22014-11-14 12:10:14 -0800442
bsalomon10e23ca2014-11-25 05:52:06 -0800443 /**
bsalomonf99e9612015-02-19 08:24:16 -0800444 * Called by a GrGpuResource to remove its unique key.
bsalomon23e619c2015-02-06 11:54:28 -0800445 */
bsalomonf99e9612015-02-19 08:24:16 -0800446 void removeUniqueKey(GrGpuResource* resource) { fCache->removeUniqueKey(resource); }
bsalomon23e619c2015-02-06 11:54:28 -0800447
448 /**
449 * Called by a GrGpuResource when it removes its scratch key.
bsalomon10e23ca2014-11-25 05:52:06 -0800450 */
451 void willRemoveScratchKey(const GrGpuResource* resource) {
452 fCache->willRemoveScratchKey(resource);
453 }
bsalomon84c8e622014-11-17 09:33:27 -0800454
455 /**
456 * Called by GrGpuResources when they change from budgeted to unbudgeted or vice versa.
457 */
458 void didChangeBudgetStatus(GrGpuResource* resource) { fCache->didChangeBudgetStatus(resource); }
459
bsalomon71cb0c22014-11-14 12:10:14 -0800460 // No taking addresses of this type.
461 const ResourceAccess* operator&() const;
462 ResourceAccess* operator&();
463
bsalomon0ea80f42015-02-11 10:49:59 -0800464 GrResourceCache* fCache;
bsalomon71cb0c22014-11-14 12:10:14 -0800465
466 friend class GrGpuResource; // To access all the proxy inline methods.
bsalomon0ea80f42015-02-11 10:49:59 -0800467 friend class GrResourceCache; // To create this type.
bsalomon71cb0c22014-11-14 12:10:14 -0800468};
469
bsalomon0ea80f42015-02-11 10:49:59 -0800470inline GrResourceCache::ResourceAccess GrResourceCache::resourceAccess() {
bsalomon71cb0c22014-11-14 12:10:14 -0800471 return ResourceAccess(this);
472}
473
bsalomonc8dc1f72014-08-21 13:02:13 -0700474#endif