blob: 4db77e21867d68cf1ac415ddda4f69c38801f639 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/private/GrResourceKey.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/private/SkTArray.h"
14#include "include/private/SkTHash.h"
Ben Wagner21bca282019-05-15 10:15:52 -040015#include "src/core/SkMessageBus.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/core/SkTDPQueue.h"
Ben Wagner729a23f2019-05-17 16:29:34 -040017#include "src/core/SkTInternalLList.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/core/SkTMultiMap.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000019#include "src/gpu/GrGpuResource.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#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;
Greg Daniel7c902112020-03-06 13:07:10 -050028class GrTexture;
mtkleinb9eb4ac2015-02-02 18:26:03 -080029
Robert Phillipsddc21482019-10-16 14:30:09 -040030struct GrTextureFreedMessage {
31 GrTexture* fTexture;
Brian Osman13dddce2017-05-09 13:19:50 -040032 uint32_t fOwningUniqueID;
33};
34
Chris Dalton9a986cf2018-10-18 15:27:59 -060035static inline bool SkShouldPostMessageToBus(
Robert Phillipsddc21482019-10-16 14:30:09 -040036 const GrTextureFreedMessage& msg, uint32_t msgBusUniqueID) {
Chris Dalton9a986cf2018-10-18 15:27:59 -060037 // The inbox's ID is the unique ID of the owning GrContext.
38 return msgBusUniqueID == msg.fOwningUniqueID;
39}
40
bsalomonc8dc1f72014-08-21 13:02:13 -070041/**
bsalomon71cb0c22014-11-14 12:10:14 -080042 * Manages the lifetime of all GrGpuResource instances.
43 *
44 * Resources may have optionally have two types of keys:
45 * 1) A scratch key. This is for resources whose allocations are cached but not their contents.
46 * Multiple resources can share the same scratch key. This is so a caller can have two
bsalomon84c8e622014-11-17 09:33:27 -080047 * resource instances with the same properties (e.g. multipass rendering that ping-pongs
bsalomon8718aaf2015-02-19 07:24:21 -080048 * between two temporary surfaces). The scratch key is set at resource creation time and
bsalomon71cb0c22014-11-14 12:10:14 -080049 * should never change. Resources need not have a scratch key.
bsalomon8718aaf2015-02-19 07:24:21 -080050 * 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 -080051 * resource may have a given unique key. The unique key can be set, cleared, or changed
52 * anytime after resource creation.
53 *
bsalomon8718aaf2015-02-19 07:24:21 -080054 * A unique key always takes precedence over a scratch key when a resource has both types of keys.
bsalomon71cb0c22014-11-14 12:10:14 -080055 * 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 -080056 * is dropped.
bsalomonc8dc1f72014-08-21 13:02:13 -070057 */
bsalomon0ea80f42015-02-11 10:49:59 -080058class GrResourceCache {
bsalomonc8dc1f72014-08-21 13:02:13 -070059public:
Brian Salomon8f8995a2018-10-15 14:32:15 -040060 GrResourceCache(const GrCaps*, GrSingleOwner* owner, uint32_t contextUniqueID);
bsalomon0ea80f42015-02-11 10:49:59 -080061 ~GrResourceCache();
bsalomonc8dc1f72014-08-21 13:02:13 -070062
bsalomon3f324322015-04-08 11:01:54 -070063 // 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
Robert Phillipscf39f372019-09-03 10:29:20 -040073 /** Sets the max gpu memory byte size of the cache. */
74 void setLimit(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 number of bytes consumed by cached resources.
105 */
106 size_t getMaxResourceBytes() const { return fMaxBytes; }
107
108 /**
109 * Abandons the backend API resources owned by all GrGpuResource objects and removes them from
110 * the cache.
111 */
bsalomonc8dc1f72014-08-21 13:02:13 -0700112 void abandonAll();
113
bsalomon71cb0c22014-11-14 12:10:14 -0800114 /**
115 * Releases 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 releaseAll();
119
bsalomon71cb0c22014-11-14 12:10:14 -0800120 /**
121 * Find a resource that matches a scratch key.
122 */
Robert Phillipsaee18c92019-09-06 11:48:27 -0400123 GrGpuResource* findAndRefScratchResource(const GrScratchKey& scratchKey);
halcanary9d524f22016-03-29 09:03:52 -0700124
bsalomon8b79d232014-11-10 10:19:06 -0800125#ifdef SK_DEBUG
126 // This is not particularly fast and only used for validation, so debug only.
bsalomon7775c852014-12-30 12:50:52 -0800127 int countScratchEntriesForKey(const GrScratchKey& scratchKey) const {
bsalomon8b79d232014-11-10 10:19:06 -0800128 return fScratchMap.countForKey(scratchKey);
129 }
130#endif
131
bsalomon71cb0c22014-11-14 12:10:14 -0800132 /**
bsalomon8718aaf2015-02-19 07:24:21 -0800133 * Find a resource that matches a unique key.
bsalomon71cb0c22014-11-14 12:10:14 -0800134 */
robertphillipsee843b22016-10-04 05:30:20 -0700135 GrGpuResource* findAndRefUniqueResource(const GrUniqueKey& key) {
136 GrGpuResource* resource = fUniqueHash.find(key);
137 if (resource) {
138 this->refAndMakeResourceMRU(resource);
139 }
140 return resource;
141 }
bsalomon8b79d232014-11-10 10:19:06 -0800142
Greg Danielcd871402017-09-26 12:49:26 -0400143 /**
bsalomon8718aaf2015-02-19 07:24:21 -0800144 * Query whether a unique key exists in the cache.
bsalomon71cb0c22014-11-14 12:10:14 -0800145 */
bsalomon8718aaf2015-02-19 07:24:21 -0800146 bool hasUniqueKey(const GrUniqueKey& key) const {
147 return SkToBool(fUniqueHash.find(key));
bsalomon8b79d232014-11-10 10:19:06 -0800148 }
bsalomonbcf0a522014-10-08 08:40:09 -0700149
bsalomon8718aaf2015-02-19 07:24:21 -0800150 /** Purges resources to become under budget and processes resources with invalidated unique
bsalomon23e619c2015-02-06 11:54:28 -0800151 keys. */
robertphillipsee843b22016-10-04 05:30:20 -0700152 void purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800153
bsalomon71cb0c22014-11-14 12:10:14 -0800154 /** Purges all resources that don't have external owners. */
Robert Phillips6eba0632018-03-28 12:25:42 -0400155 void purgeAllUnlocked() { this->purgeUnlockedResources(false); }
156
157 // Purge unlocked resources. If 'scratchResourcesOnly' is true the purgeable resources
158 // containing persistent data are spared. If it is false then all purgeable resources will
159 // be deleted.
160 void purgeUnlockedResources(bool scratchResourcesOnly);
bsalomon71cb0c22014-11-14 12:10:14 -0800161
Brian Salomon5e150852017-03-22 14:53:13 -0400162 /** Purge all resources not used since the passed in time. */
163 void purgeResourcesNotUsedSince(GrStdSteadyClock::time_point);
164
Robert Phillipscf39f372019-09-03 10:29:20 -0400165 bool overBudget() const { return fBudgetedBytes > fMaxBytes; }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500166
Derek Sollenberger5480a182017-05-25 16:43:59 -0400167 /**
168 * Purge unlocked resources from the cache until the the provided byte count has been reached
169 * or we have purged all unlocked resources. The default policy is to purge in LRU order, but
170 * can be overridden to prefer purging scratch resources (in LRU order) prior to purging other
171 * resource types.
172 *
173 * @param maxBytesToPurge the desired number of bytes to be purged.
174 * @param preferScratchResources If true scratch resources will be purged prior to other
175 * resource types.
176 */
177 void purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources);
178
bsalomonb77a9072016-09-07 10:02:04 -0700179 /** Returns true if the cache would like a flush to occur in order to make more resources
180 purgeable. */
Brian Salomon8cefa3e2019-04-04 11:39:55 -0400181 bool requestsFlush() const;
bsalomon71cb0c22014-11-14 12:10:14 -0800182
Robert Phillipsddc21482019-10-16 14:30:09 -0400183 /** Maintain a ref to this texture until we receive a GrTextureFreedMessage. */
184 void insertDelayedTextureUnref(GrTexture*);
Brian Osman13dddce2017-05-09 13:19:50 -0400185
robertphillips60029a52015-11-09 13:51:06 -0800186#if GR_CACHE_STATS
187 struct Stats {
188 int fTotal;
189 int fNumPurgeable;
190 int fNumNonPurgeable;
191
192 int fScratch;
kkinnunen2e6055b2016-04-22 01:48:29 -0700193 int fWrapped;
robertphillips60029a52015-11-09 13:51:06 -0800194 size_t fUnbudgetedSize;
195
196 Stats() { this->reset(); }
197
198 void reset() {
199 fTotal = 0;
200 fNumPurgeable = 0;
201 fNumNonPurgeable = 0;
202 fScratch = 0;
kkinnunen2e6055b2016-04-22 01:48:29 -0700203 fWrapped = 0;
robertphillips60029a52015-11-09 13:51:06 -0800204 fUnbudgetedSize = 0;
205 }
206
207 void update(GrGpuResource* resource) {
208 if (resource->cacheAccess().isScratch()) {
209 ++fScratch;
210 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700211 if (resource->resourcePriv().refsWrappedObjects()) {
212 ++fWrapped;
robertphillips60029a52015-11-09 13:51:06 -0800213 }
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500214 if (GrBudgetedType::kBudgeted != resource->resourcePriv().budgetedType()) {
robertphillips60029a52015-11-09 13:51:06 -0800215 fUnbudgetedSize += resource->gpuMemorySize();
216 }
217 }
218 };
219
220 void getStats(Stats*) const;
221
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500222#if GR_TEST_UTILS
mtkleinb9eb4ac2015-02-02 18:26:03 -0800223 void dumpStats(SkString*) const;
joshualittdc5685a2015-12-02 14:08:25 -0800224
225 void dumpStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* value) const;
bsalomon71cb0c22014-11-14 12:10:14 -0800226#endif
227
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500228#endif
229
Brian Salomon1090da62017-01-06 12:04:19 -0500230#ifdef SK_DEBUG
231 int countUniqueKeysWithTag(const char* tag) const;
232#endif
233
bsalomonddf30e62015-02-19 11:38:44 -0800234 // This function is for unit testing and is only defined in test tools.
235 void changeTimestamp(uint32_t newTimestamp);
236
ericrk0a5fa482015-09-15 14:16:10 -0700237 // Enumerates all cached resources and dumps their details to traceMemoryDump.
238 void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const;
239
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500240 void setProxyProvider(GrProxyProvider* proxyProvider) { fProxyProvider = proxyProvider; }
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400241
bsalomonc8dc1f72014-08-21 13:02:13 -0700242private:
bsalomon71cb0c22014-11-14 12:10:14 -0800243 ///////////////////////////////////////////////////////////////////////////
244 /// @name Methods accessible via ResourceAccess
245 ////
246 void insertResource(GrGpuResource*);
247 void removeResource(GrGpuResource*);
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400248 void notifyRefCntReachedZero(GrGpuResource*);
bsalomonf99e9612015-02-19 08:24:16 -0800249 void changeUniqueKey(GrGpuResource*, const GrUniqueKey&);
250 void removeUniqueKey(GrGpuResource*);
bsalomon10e23ca2014-11-25 05:52:06 -0800251 void willRemoveScratchKey(const GrGpuResource*);
bsalomon84c8e622014-11-17 09:33:27 -0800252 void didChangeBudgetStatus(GrGpuResource*);
Brian Salomon2c791fc2019-04-02 11:52:03 -0400253 void refResource(GrGpuResource* resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800254 /// @}
255
Brian Salomon2c791fc2019-04-02 11:52:03 -0400256 void refAndMakeResourceMRU(GrGpuResource*);
Brian Osman13dddce2017-05-09 13:19:50 -0400257 void processFreedGpuResources();
bsalomonf320e042015-02-17 15:09:34 -0800258 void addToNonpurgeableArray(GrGpuResource*);
259 void removeFromNonpurgeableArray(GrGpuResource*);
bsalomon71cb0c22014-11-14 12:10:14 -0800260
Robert Phillipscf39f372019-09-03 10:29:20 -0400261 bool wouldFit(size_t bytes) const { return fBudgetedBytes+bytes <= fMaxBytes; }
robertphillips6e83ac72015-08-13 05:19:14 -0700262
bsalomonddf30e62015-02-19 11:38:44 -0800263 uint32_t getNextTimestamp();
264
bsalomon16961262014-08-26 14:01:07 -0700265#ifdef SK_DEBUG
bsalomonf320e042015-02-17 15:09:34 -0800266 bool isInCache(const GrGpuResource* r) const;
bsalomon71cb0c22014-11-14 12:10:14 -0800267 void validate() const;
268#else
269 void validate() const {}
bsalomon16961262014-08-26 14:01:07 -0700270#endif
271
bsalomon71cb0c22014-11-14 12:10:14 -0800272 class AutoValidate;
273
bsalomonbcf0a522014-10-08 08:40:09 -0700274 class AvailableForScratchUse;
bsalomon744998e2014-08-28 09:54:34 -0700275
robertphillipsee843b22016-10-04 05:30:20 -0700276 struct ScratchMapTraits {
bsalomon7775c852014-12-30 12:50:52 -0800277 static const GrScratchKey& GetKey(const GrGpuResource& r) {
bsalomon3582d3e2015-02-13 14:20:05 -0800278 return r.resourcePriv().getScratchKey();
bsalomon744998e2014-08-28 09:54:34 -0700279 }
robertphillipsee843b22016-10-04 05:30:20 -0700280
281 static uint32_t Hash(const GrScratchKey& key) { return key.hash(); }
Robert Phillipsf8e25022017-11-08 15:24:31 -0500282 static void OnFree(GrGpuResource*) { }
bsalomon744998e2014-08-28 09:54:34 -0700283 };
bsalomon7775c852014-12-30 12:50:52 -0800284 typedef SkTMultiMap<GrGpuResource, GrScratchKey, ScratchMapTraits> ScratchMap;
bsalomon744998e2014-08-28 09:54:34 -0700285
robertphillipsee843b22016-10-04 05:30:20 -0700286 struct UniqueHashTraits {
bsalomon8718aaf2015-02-19 07:24:21 -0800287 static const GrUniqueKey& GetKey(const GrGpuResource& r) { return r.getUniqueKey(); }
robertphillipsee843b22016-10-04 05:30:20 -0700288
289 static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); }
bsalomon8b79d232014-11-10 10:19:06 -0800290 };
bsalomon8718aaf2015-02-19 07:24:21 -0800291 typedef SkTDynamicHash<GrGpuResource, GrUniqueKey, UniqueHashTraits> UniqueHash;
bsalomon8b79d232014-11-10 10:19:06 -0800292
Robert Phillipsddc21482019-10-16 14:30:09 -0400293 class TextureAwaitingUnref {
Brian Salomon876a0172019-03-08 11:12:14 -0500294 public:
Robert Phillipsddc21482019-10-16 14:30:09 -0400295 TextureAwaitingUnref();
296 TextureAwaitingUnref(GrTexture* texture);
297 TextureAwaitingUnref(const TextureAwaitingUnref&) = delete;
298 TextureAwaitingUnref& operator=(const TextureAwaitingUnref&) = delete;
299 TextureAwaitingUnref(TextureAwaitingUnref&&);
300 TextureAwaitingUnref& operator=(TextureAwaitingUnref&&);
301 ~TextureAwaitingUnref();
Brian Salomon876a0172019-03-08 11:12:14 -0500302 void addRef();
303 void unref();
304 bool finished();
305
306 private:
Robert Phillipsddc21482019-10-16 14:30:09 -0400307 GrTexture* fTexture = nullptr;
Brian Salomon876a0172019-03-08 11:12:14 -0500308 int fNumUnrefs = 0;
309 };
Robert Phillipsddc21482019-10-16 14:30:09 -0400310 using TexturesAwaitingUnref = SkTHashMap<uint32_t, TextureAwaitingUnref>;
Brian Salomon876a0172019-03-08 11:12:14 -0500311
bsalomon9f2d1572015-02-17 11:47:40 -0800312 static bool CompareTimestamp(GrGpuResource* const& a, GrGpuResource* const& b) {
313 return a->cacheAccess().timestamp() < b->cacheAccess().timestamp();
314 }
bsalomon23e619c2015-02-06 11:54:28 -0800315
bsalomon9f2d1572015-02-17 11:47:40 -0800316 static int* AccessResourceIndex(GrGpuResource* const& res) {
317 return res->cacheAccess().accessCacheIndex();
318 }
319
bsalomon8718aaf2015-02-19 07:24:21 -0800320 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage>::Inbox InvalidUniqueKeyInbox;
Robert Phillipsddc21482019-10-16 14:30:09 -0400321 typedef SkMessageBus<GrTextureFreedMessage>::Inbox FreedTextureInbox;
bsalomon9f2d1572015-02-17 11:47:40 -0800322 typedef SkTDPQueue<GrGpuResource*, CompareTimestamp, AccessResourceIndex> PurgeableQueue;
bsalomonf320e042015-02-17 15:09:34 -0800323 typedef SkTDArray<GrGpuResource*> ResourceArray;
bsalomon9f2d1572015-02-17 11:47:40 -0800324
Brian Salomon2c791fc2019-04-02 11:52:03 -0400325 GrProxyProvider* fProxyProvider = nullptr;
bsalomon9f2d1572015-02-17 11:47:40 -0800326 // Whenever a resource is added to the cache or the result of a cache lookup, fTimestamp is
327 // assigned as the resource's timestamp and then incremented. fPurgeableQueue orders the
328 // purgeable resources by this value, and thus is used to purge resources in LRU order.
Brian Salomon2c791fc2019-04-02 11:52:03 -0400329 uint32_t fTimestamp = 0;
bsalomon9f2d1572015-02-17 11:47:40 -0800330 PurgeableQueue fPurgeableQueue;
bsalomonf320e042015-02-17 15:09:34 -0800331 ResourceArray fNonpurgeableResources;
bsalomon9f2d1572015-02-17 11:47:40 -0800332
bsalomon744998e2014-08-28 09:54:34 -0700333 // This map holds all resources that can be used as scratch resources.
bsalomon8b79d232014-11-10 10:19:06 -0800334 ScratchMap fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800335 // This holds all resources that have unique keys.
336 UniqueHash fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800337
338 // our budget, used in purgeAsNeeded()
Brian Salomon2c791fc2019-04-02 11:52:03 -0400339 size_t fMaxBytes = kDefaultMaxSize;
bsalomon71cb0c22014-11-14 12:10:14 -0800340
341#if GR_CACHE_STATS
Brian Salomon2c791fc2019-04-02 11:52:03 -0400342 int fHighWaterCount = 0;
343 size_t fHighWaterBytes = 0;
344 int fBudgetedHighWaterCount = 0;
345 size_t fBudgetedHighWaterBytes = 0;
bsalomon71cb0c22014-11-14 12:10:14 -0800346#endif
347
bsalomondace19e2014-11-17 07:34:06 -0800348 // our current stats for all resources
Brian Salomon2c791fc2019-04-02 11:52:03 -0400349 SkDEBUGCODE(int fCount = 0;)
350 size_t fBytes = 0;
bsalomon71cb0c22014-11-14 12:10:14 -0800351
bsalomondace19e2014-11-17 07:34:06 -0800352 // our current stats for resources that count against the budget
Brian Salomon2c791fc2019-04-02 11:52:03 -0400353 int fBudgetedCount = 0;
354 size_t fBudgetedBytes = 0;
355 size_t fPurgeableBytes = 0;
356 int fNumBudgetedResourcesFlushWillMakePurgeable = 0;
bsalomondace19e2014-11-17 07:34:06 -0800357
bsalomon8718aaf2015-02-19 07:24:21 -0800358 InvalidUniqueKeyInbox fInvalidUniqueKeyInbox;
Robert Phillipsddc21482019-10-16 14:30:09 -0400359 FreedTextureInbox fFreedTextureInbox;
360 TexturesAwaitingUnref fTexturesAwaitingUnref;
Greg Danielc27eb722018-08-10 09:48:08 -0400361
Brian Salomon2c791fc2019-04-02 11:52:03 -0400362 uint32_t fContextUniqueID = SK_InvalidUniqueID;
363 GrSingleOwner* fSingleOwner = nullptr;
bsalomon3f324322015-04-08 11:01:54 -0700364
365 // This resource is allowed to be in the nonpurgeable array for the sake of validate() because
366 // we're in the midst of converting it to purgeable status.
Brian Salomon2c791fc2019-04-02 11:52:03 -0400367 SkDEBUGCODE(GrGpuResource* fNewlyPurgeableResourceForValidation = nullptr;)
robertphillips63926682015-08-20 09:39:02 -0700368
Brian Salomon2c791fc2019-04-02 11:52:03 -0400369 bool fPreferVRAMUseOverFlushes = false;
bsalomonc8dc1f72014-08-21 13:02:13 -0700370};
371
bsalomon0ea80f42015-02-11 10:49:59 -0800372class GrResourceCache::ResourceAccess {
bsalomon71cb0c22014-11-14 12:10:14 -0800373private:
bsalomon0ea80f42015-02-11 10:49:59 -0800374 ResourceAccess(GrResourceCache* cache) : fCache(cache) { }
bsalomon71cb0c22014-11-14 12:10:14 -0800375 ResourceAccess(const ResourceAccess& that) : fCache(that.fCache) { }
376 ResourceAccess& operator=(const ResourceAccess&); // unimpl
377
378 /**
379 * Insert a resource into the cache.
380 */
381 void insertResource(GrGpuResource* resource) { fCache->insertResource(resource); }
382
383 /**
384 * Removes a resource from the cache.
385 */
386 void removeResource(GrGpuResource* resource) { fCache->removeResource(resource); }
387
388 /**
Brian Salomon2c791fc2019-04-02 11:52:03 -0400389 * Adds a ref to a resource with proper tracking if the resource has 0 refs prior to
390 * adding the ref.
391 */
392 void refResource(GrGpuResource* resource) { fCache->refResource(resource); }
393
394 /**
bsalomon3f324322015-04-08 11:01:54 -0700395 * Notifications that should be sent to the cache when the ref/io cnt status of resources
396 * changes.
bsalomon71cb0c22014-11-14 12:10:14 -0800397 */
bsalomon3f324322015-04-08 11:01:54 -0700398 enum RefNotificationFlags {
399 /** All types of refs on the resource have reached zero. */
400 kAllCntsReachedZero_RefNotificationFlag = 0x1,
401 /** The normal (not pending IO type) ref cnt has reached zero. */
402 kRefCntReachedZero_RefNotificationFlag = 0x2,
403 };
404 /**
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400405 * Called by GrGpuResources when they detect that their ref cnt has reached zero.
bsalomon3f324322015-04-08 11:01:54 -0700406 */
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400407 void notifyRefCntReachedZero(GrGpuResource* resource) {
408 fCache->notifyRefCntReachedZero(resource);
bsalomon3f324322015-04-08 11:01:54 -0700409 }
bsalomon71cb0c22014-11-14 12:10:14 -0800410
411 /**
bsalomonf99e9612015-02-19 08:24:16 -0800412 * Called by GrGpuResources to change their unique keys.
bsalomon71cb0c22014-11-14 12:10:14 -0800413 */
bsalomonf99e9612015-02-19 08:24:16 -0800414 void changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) {
415 fCache->changeUniqueKey(resource, newKey);
416 }
bsalomon71cb0c22014-11-14 12:10:14 -0800417
bsalomon10e23ca2014-11-25 05:52:06 -0800418 /**
bsalomonf99e9612015-02-19 08:24:16 -0800419 * Called by a GrGpuResource to remove its unique key.
bsalomon23e619c2015-02-06 11:54:28 -0800420 */
bsalomonf99e9612015-02-19 08:24:16 -0800421 void removeUniqueKey(GrGpuResource* resource) { fCache->removeUniqueKey(resource); }
bsalomon23e619c2015-02-06 11:54:28 -0800422
423 /**
424 * Called by a GrGpuResource when it removes its scratch key.
bsalomon10e23ca2014-11-25 05:52:06 -0800425 */
426 void willRemoveScratchKey(const GrGpuResource* resource) {
427 fCache->willRemoveScratchKey(resource);
428 }
bsalomon84c8e622014-11-17 09:33:27 -0800429
430 /**
431 * Called by GrGpuResources when they change from budgeted to unbudgeted or vice versa.
432 */
433 void didChangeBudgetStatus(GrGpuResource* resource) { fCache->didChangeBudgetStatus(resource); }
434
bsalomon71cb0c22014-11-14 12:10:14 -0800435 // No taking addresses of this type.
436 const ResourceAccess* operator&() const;
437 ResourceAccess* operator&();
438
bsalomon0ea80f42015-02-11 10:49:59 -0800439 GrResourceCache* fCache;
bsalomon71cb0c22014-11-14 12:10:14 -0800440
441 friend class GrGpuResource; // To access all the proxy inline methods.
bsalomon0ea80f42015-02-11 10:49:59 -0800442 friend class GrResourceCache; // To create this type.
bsalomon71cb0c22014-11-14 12:10:14 -0800443};
444
bsalomon0ea80f42015-02-11 10:49:59 -0800445inline GrResourceCache::ResourceAccess GrResourceCache::resourceAccess() {
bsalomon71cb0c22014-11-14 12:10:14 -0800446 return ResourceAccess(this);
447}
448
bsalomonc8dc1f72014-08-21 13:02:13 -0700449#endif