blob: 2d7f5c3132add48d017fd2cbb24c83ed676be990 [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"
Robert Phillipsd074b622021-03-15 08:49:24 -040012#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/private/GrResourceKey.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/private/SkTArray.h"
15#include "include/private/SkTHash.h"
Ben Wagner21bca282019-05-15 10:15:52 -040016#include "src/core/SkMessageBus.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/core/SkTDPQueue.h"
Ben Wagner729a23f2019-05-17 16:29:34 -040018#include "src/core/SkTInternalLList.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/core/SkTMultiMap.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000020#include "src/gpu/GrGpuResource.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrGpuResourceCacheAccess.h"
22#include "src/gpu/GrGpuResourcePriv.h"
bsalomonc8dc1f72014-08-21 13:02:13 -070023
robertphillips63926682015-08-20 09:39:02 -070024class GrCaps;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050025class GrProxyProvider;
mtkleinb9eb4ac2015-02-02 18:26:03 -080026class SkString;
ericrk0a5fa482015-09-15 14:16:10 -070027class SkTraceMemoryDump;
Brian Salomon8f8995a2018-10-15 14:32:15 -040028class GrSingleOwner;
Greg Daniel7c902112020-03-06 13:07:10 -050029class GrTexture;
Robert Phillipsd464feb2020-10-08 11:00:02 -040030class GrThreadSafeCache;
mtkleinb9eb4ac2015-02-02 18:26:03 -080031
Robert Phillipsddc21482019-10-16 14:30:09 -040032struct GrTextureFreedMessage {
33 GrTexture* fTexture;
Robert Phillipsd074b622021-03-15 08:49:24 -040034 GrDirectContext::DirectContextID fIntendedRecipient;
Brian Osman13dddce2017-05-09 13:19:50 -040035};
36
Chris Dalton9a986cf2018-10-18 15:27:59 -060037static inline bool SkShouldPostMessageToBus(
Robert Phillipsd074b622021-03-15 08:49:24 -040038 const GrTextureFreedMessage& msg, GrDirectContext::DirectContextID potentialRecipient) {
39 return potentialRecipient == msg.fIntendedRecipient;
Chris Dalton9a986cf2018-10-18 15:27:59 -060040}
41
bsalomonc8dc1f72014-08-21 13:02:13 -070042/**
bsalomon71cb0c22014-11-14 12:10:14 -080043 * Manages the lifetime of all GrGpuResource instances.
44 *
45 * Resources may have optionally have two types of keys:
46 * 1) A scratch key. This is for resources whose allocations are cached but not their contents.
47 * Multiple resources can share the same scratch key. This is so a caller can have two
bsalomon84c8e622014-11-17 09:33:27 -080048 * resource instances with the same properties (e.g. multipass rendering that ping-pongs
bsalomon8718aaf2015-02-19 07:24:21 -080049 * between two temporary surfaces). The scratch key is set at resource creation time and
bsalomon71cb0c22014-11-14 12:10:14 -080050 * should never change. Resources need not have a scratch key.
bsalomon8718aaf2015-02-19 07:24:21 -080051 * 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 -080052 * resource may have a given unique key. The unique key can be set, cleared, or changed
53 * anytime after resource creation.
54 *
bsalomon8718aaf2015-02-19 07:24:21 -080055 * A unique key always takes precedence over a scratch key when a resource has both types of keys.
bsalomon71cb0c22014-11-14 12:10:14 -080056 * 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 -080057 * is dropped.
bsalomonc8dc1f72014-08-21 13:02:13 -070058 */
bsalomon0ea80f42015-02-11 10:49:59 -080059class GrResourceCache {
bsalomonc8dc1f72014-08-21 13:02:13 -070060public:
Robert Phillipsd074b622021-03-15 08:49:24 -040061 GrResourceCache(GrSingleOwner* owner,
62 GrDirectContext::DirectContextID owningContextID,
63 uint32_t familyID);
bsalomon0ea80f42015-02-11 10:49:59 -080064 ~GrResourceCache();
bsalomonc8dc1f72014-08-21 13:02:13 -070065
bsalomon3f324322015-04-08 11:01:54 -070066 // Default maximum number of bytes of gpu memory of budgeted resources in the cache.
Chris Dalton062d2d92020-05-27 14:04:15 -060067 static const size_t kDefaultMaxSize = 256 * (1 << 20);
bsalomon3f324322015-04-08 11:01:54 -070068
bsalomon71cb0c22014-11-14 12:10:14 -080069 /** Used to access functionality needed by GrGpuResource for lifetime management. */
70 class ResourceAccess;
71 ResourceAccess resourceAccess();
bsalomonc8dc1f72014-08-21 13:02:13 -070072
Brian Salomon238069b2018-07-11 15:58:57 -040073 /** Unique ID of the owning GrContext. */
74 uint32_t contextUniqueID() const { return fContextUniqueID; }
75
Robert Phillipscf39f372019-09-03 10:29:20 -040076 /** Sets the max gpu memory byte size of the cache. */
77 void setLimit(size_t bytes);
bsalomonc8dc1f72014-08-21 13:02:13 -070078
bsalomon71cb0c22014-11-14 12:10:14 -080079 /**
bsalomondace19e2014-11-17 07:34:06 -080080 * Returns the number of resources.
bsalomon71cb0c22014-11-14 12:10:14 -080081 */
bsalomonf320e042015-02-17 15:09:34 -080082 int getResourceCount() const {
83 return fPurgeableQueue.count() + fNonpurgeableResources.count();
84 }
bsalomon8b79d232014-11-10 10:19:06 -080085
bsalomon71cb0c22014-11-14 12:10:14 -080086 /**
bsalomondace19e2014-11-17 07:34:06 -080087 * Returns the number of resources that count against the budget.
88 */
89 int getBudgetedResourceCount() const { return fBudgetedCount; }
90
91 /**
92 * Returns the number of bytes consumed by resources.
bsalomon71cb0c22014-11-14 12:10:14 -080093 */
94 size_t getResourceBytes() const { return fBytes; }
95
96 /**
Robert Phillips12d06a32020-09-16 12:31:34 -040097 * Returns the number of bytes held by unlocked resources which are available for purging.
Derek Sollenbergeree479142017-05-24 11:41:33 -040098 */
99 size_t getPurgeableBytes() const { return fPurgeableBytes; }
100
101 /**
bsalomondace19e2014-11-17 07:34:06 -0800102 * Returns the number of bytes consumed by budgeted resources.
103 */
104 size_t getBudgetedResourceBytes() const { return fBudgetedBytes; }
105
106 /**
bsalomon71cb0c22014-11-14 12:10:14 -0800107 * Returns the number of bytes consumed by cached resources.
108 */
109 size_t getMaxResourceBytes() const { return fMaxBytes; }
110
111 /**
112 * Abandons the backend API resources owned by all GrGpuResource objects and removes them from
113 * the cache.
114 */
bsalomonc8dc1f72014-08-21 13:02:13 -0700115 void abandonAll();
116
bsalomon71cb0c22014-11-14 12:10:14 -0800117 /**
118 * Releases the backend API resources owned by all GrGpuResource objects and removes them from
119 * the cache.
120 */
bsalomonc8dc1f72014-08-21 13:02:13 -0700121 void releaseAll();
122
bsalomon71cb0c22014-11-14 12:10:14 -0800123 /**
124 * Find a resource that matches a scratch key.
125 */
Robert Phillipsaee18c92019-09-06 11:48:27 -0400126 GrGpuResource* findAndRefScratchResource(const GrScratchKey& scratchKey);
halcanary9d524f22016-03-29 09:03:52 -0700127
bsalomon8b79d232014-11-10 10:19:06 -0800128#ifdef SK_DEBUG
129 // This is not particularly fast and only used for validation, so debug only.
bsalomon7775c852014-12-30 12:50:52 -0800130 int countScratchEntriesForKey(const GrScratchKey& scratchKey) const {
bsalomon8b79d232014-11-10 10:19:06 -0800131 return fScratchMap.countForKey(scratchKey);
132 }
133#endif
134
bsalomon71cb0c22014-11-14 12:10:14 -0800135 /**
bsalomon8718aaf2015-02-19 07:24:21 -0800136 * Find a resource that matches a unique key.
bsalomon71cb0c22014-11-14 12:10:14 -0800137 */
robertphillipsee843b22016-10-04 05:30:20 -0700138 GrGpuResource* findAndRefUniqueResource(const GrUniqueKey& key) {
139 GrGpuResource* resource = fUniqueHash.find(key);
140 if (resource) {
141 this->refAndMakeResourceMRU(resource);
142 }
143 return resource;
144 }
bsalomon8b79d232014-11-10 10:19:06 -0800145
Greg Danielcd871402017-09-26 12:49:26 -0400146 /**
bsalomon8718aaf2015-02-19 07:24:21 -0800147 * Query whether a unique key exists in the cache.
bsalomon71cb0c22014-11-14 12:10:14 -0800148 */
bsalomon8718aaf2015-02-19 07:24:21 -0800149 bool hasUniqueKey(const GrUniqueKey& key) const {
150 return SkToBool(fUniqueHash.find(key));
bsalomon8b79d232014-11-10 10:19:06 -0800151 }
bsalomonbcf0a522014-10-08 08:40:09 -0700152
bsalomon8718aaf2015-02-19 07:24:21 -0800153 /** Purges resources to become under budget and processes resources with invalidated unique
bsalomon23e619c2015-02-06 11:54:28 -0800154 keys. */
robertphillipsee843b22016-10-04 05:30:20 -0700155 void purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800156
Robert Phillips6eba0632018-03-28 12:25:42 -0400157 // 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.
Michael Ludwig9d1cc052021-06-09 20:49:48 -0400160 void purgeUnlockedResources(bool scratchResourcesOnly=false) {
161 this->purgeUnlockedResources(/*purgeTime=*/nullptr, scratchResourcesOnly);
162 }
bsalomon71cb0c22014-11-14 12:10:14 -0800163
Michael Ludwig9d1cc052021-06-09 20:49:48 -0400164 // Purge unlocked resources not used since the passed point in time. If 'scratchResourcesOnly'
165 // is true the purgeable resources containing persistent data are spared. If it is false then
166 // all purgeable resources older than 'purgeTime' will be deleted.
167 void purgeResourcesNotUsedSince(GrStdSteadyClock::time_point purgeTime,
168 bool scratchResourcesOnly=false) {
169 this->purgeUnlockedResources(&purgeTime, scratchResourcesOnly);
170 }
Brian Salomon5e150852017-03-22 14:53:13 -0400171
Adlai Hollere1c8a382021-04-08 15:30:12 -0400172 /** If it's possible to purge enough resources to get the provided amount of budget
173 headroom, do so and return true. If it's not possible, do nothing and return false.
174 */
175 bool purgeToMakeHeadroom(size_t desiredHeadroomBytes);
176
Robert Phillipscf39f372019-09-03 10:29:20 -0400177 bool overBudget() const { return fBudgetedBytes > fMaxBytes; }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500178
Derek Sollenberger5480a182017-05-25 16:43:59 -0400179 /**
180 * Purge unlocked resources from the cache until the the provided byte count has been reached
181 * or we have purged all unlocked resources. The default policy is to purge in LRU order, but
182 * can be overridden to prefer purging scratch resources (in LRU order) prior to purging other
183 * resource types.
184 *
185 * @param maxBytesToPurge the desired number of bytes to be purged.
186 * @param preferScratchResources If true scratch resources will be purged prior to other
187 * resource types.
188 */
189 void purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources);
190
bsalomonb77a9072016-09-07 10:02:04 -0700191 /** Returns true if the cache would like a flush to occur in order to make more resources
192 purgeable. */
Brian Salomon8cefa3e2019-04-04 11:39:55 -0400193 bool requestsFlush() const;
bsalomon71cb0c22014-11-14 12:10:14 -0800194
Robert Phillipsddc21482019-10-16 14:30:09 -0400195 /** Maintain a ref to this texture until we receive a GrTextureFreedMessage. */
196 void insertDelayedTextureUnref(GrTexture*);
Brian Osman13dddce2017-05-09 13:19:50 -0400197
robertphillips60029a52015-11-09 13:51:06 -0800198#if GR_CACHE_STATS
199 struct Stats {
200 int fTotal;
201 int fNumPurgeable;
202 int fNumNonPurgeable;
203
204 int fScratch;
kkinnunen2e6055b2016-04-22 01:48:29 -0700205 int fWrapped;
robertphillips60029a52015-11-09 13:51:06 -0800206 size_t fUnbudgetedSize;
207
208 Stats() { this->reset(); }
209
210 void reset() {
211 fTotal = 0;
212 fNumPurgeable = 0;
213 fNumNonPurgeable = 0;
214 fScratch = 0;
kkinnunen2e6055b2016-04-22 01:48:29 -0700215 fWrapped = 0;
robertphillips60029a52015-11-09 13:51:06 -0800216 fUnbudgetedSize = 0;
217 }
218
219 void update(GrGpuResource* resource) {
220 if (resource->cacheAccess().isScratch()) {
221 ++fScratch;
222 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700223 if (resource->resourcePriv().refsWrappedObjects()) {
224 ++fWrapped;
robertphillips60029a52015-11-09 13:51:06 -0800225 }
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500226 if (GrBudgetedType::kBudgeted != resource->resourcePriv().budgetedType()) {
robertphillips60029a52015-11-09 13:51:06 -0800227 fUnbudgetedSize += resource->gpuMemorySize();
228 }
229 }
230 };
231
232 void getStats(Stats*) const;
233
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500234#if GR_TEST_UTILS
mtkleinb9eb4ac2015-02-02 18:26:03 -0800235 void dumpStats(SkString*) const;
joshualittdc5685a2015-12-02 14:08:25 -0800236
237 void dumpStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* value) const;
bsalomon71cb0c22014-11-14 12:10:14 -0800238#endif
239
Robert Phillips72354b02021-07-06 13:43:56 -0400240#endif // GR_CACHE_STATS
Robert Phillipsdbaf3172019-02-06 15:12:53 -0500241
Robert Phillips72354b02021-07-06 13:43:56 -0400242#if GR_TEST_UTILS
Brian Salomon1090da62017-01-06 12:04:19 -0500243 int countUniqueKeysWithTag(const char* tag) const;
Brian Salomon1090da62017-01-06 12:04:19 -0500244
bsalomonddf30e62015-02-19 11:38:44 -0800245 // This function is for unit testing and is only defined in test tools.
246 void changeTimestamp(uint32_t newTimestamp);
Robert Phillips72354b02021-07-06 13:43:56 -0400247#endif
bsalomonddf30e62015-02-19 11:38:44 -0800248
ericrk0a5fa482015-09-15 14:16:10 -0700249 // Enumerates all cached resources and dumps their details to traceMemoryDump.
250 void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const;
251
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500252 void setProxyProvider(GrProxyProvider* proxyProvider) { fProxyProvider = proxyProvider; }
Robert Phillipsd464feb2020-10-08 11:00:02 -0400253 void setThreadSafeCache(GrThreadSafeCache* threadSafeCache) {
254 fThreadSafeCache = threadSafeCache;
Robert Phillips12d06a32020-09-16 12:31:34 -0400255 }
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*);
Greg Danielda642612021-02-09 18:04:02 -0500263 void notifyARefCntReachedZero(GrGpuResource*, GrGpuResource::LastRemovedRef);
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
Robert Phillipscf39f372019-09-03 10:29:20 -0400276 bool wouldFit(size_t bytes) const { return fBudgetedBytes+bytes <= fMaxBytes; }
robertphillips6e83ac72015-08-13 05:19:14 -0700277
bsalomonddf30e62015-02-19 11:38:44 -0800278 uint32_t getNextTimestamp();
279
Michael Ludwig9d1cc052021-06-09 20:49:48 -0400280 void purgeUnlockedResources(const GrStdSteadyClock::time_point* purgeTime,
281 bool scratchResourcesOnly);
282
bsalomon16961262014-08-26 14:01:07 -0700283#ifdef SK_DEBUG
bsalomonf320e042015-02-17 15:09:34 -0800284 bool isInCache(const GrGpuResource* r) const;
bsalomon71cb0c22014-11-14 12:10:14 -0800285 void validate() const;
286#else
287 void validate() const {}
bsalomon16961262014-08-26 14:01:07 -0700288#endif
289
bsalomon71cb0c22014-11-14 12:10:14 -0800290 class AutoValidate;
291
bsalomonbcf0a522014-10-08 08:40:09 -0700292 class AvailableForScratchUse;
bsalomon744998e2014-08-28 09:54:34 -0700293
robertphillipsee843b22016-10-04 05:30:20 -0700294 struct ScratchMapTraits {
bsalomon7775c852014-12-30 12:50:52 -0800295 static const GrScratchKey& GetKey(const GrGpuResource& r) {
bsalomon3582d3e2015-02-13 14:20:05 -0800296 return r.resourcePriv().getScratchKey();
bsalomon744998e2014-08-28 09:54:34 -0700297 }
robertphillipsee843b22016-10-04 05:30:20 -0700298
299 static uint32_t Hash(const GrScratchKey& key) { return key.hash(); }
Robert Phillipsf8e25022017-11-08 15:24:31 -0500300 static void OnFree(GrGpuResource*) { }
bsalomon744998e2014-08-28 09:54:34 -0700301 };
bsalomon7775c852014-12-30 12:50:52 -0800302 typedef SkTMultiMap<GrGpuResource, GrScratchKey, ScratchMapTraits> ScratchMap;
bsalomon744998e2014-08-28 09:54:34 -0700303
robertphillipsee843b22016-10-04 05:30:20 -0700304 struct UniqueHashTraits {
bsalomon8718aaf2015-02-19 07:24:21 -0800305 static const GrUniqueKey& GetKey(const GrGpuResource& r) { return r.getUniqueKey(); }
robertphillipsee843b22016-10-04 05:30:20 -0700306
307 static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); }
bsalomon8b79d232014-11-10 10:19:06 -0800308 };
bsalomon8718aaf2015-02-19 07:24:21 -0800309 typedef SkTDynamicHash<GrGpuResource, GrUniqueKey, UniqueHashTraits> UniqueHash;
bsalomon8b79d232014-11-10 10:19:06 -0800310
Robert Phillipsddc21482019-10-16 14:30:09 -0400311 class TextureAwaitingUnref {
Brian Salomon876a0172019-03-08 11:12:14 -0500312 public:
Robert Phillipsddc21482019-10-16 14:30:09 -0400313 TextureAwaitingUnref();
314 TextureAwaitingUnref(GrTexture* texture);
315 TextureAwaitingUnref(const TextureAwaitingUnref&) = delete;
316 TextureAwaitingUnref& operator=(const TextureAwaitingUnref&) = delete;
317 TextureAwaitingUnref(TextureAwaitingUnref&&);
318 TextureAwaitingUnref& operator=(TextureAwaitingUnref&&);
319 ~TextureAwaitingUnref();
Brian Salomon876a0172019-03-08 11:12:14 -0500320 void addRef();
321 void unref();
322 bool finished();
323
324 private:
Robert Phillipsddc21482019-10-16 14:30:09 -0400325 GrTexture* fTexture = nullptr;
Brian Salomon876a0172019-03-08 11:12:14 -0500326 int fNumUnrefs = 0;
327 };
Robert Phillipsddc21482019-10-16 14:30:09 -0400328 using TexturesAwaitingUnref = SkTHashMap<uint32_t, TextureAwaitingUnref>;
Brian Salomon876a0172019-03-08 11:12:14 -0500329
bsalomon9f2d1572015-02-17 11:47:40 -0800330 static bool CompareTimestamp(GrGpuResource* const& a, GrGpuResource* const& b) {
331 return a->cacheAccess().timestamp() < b->cacheAccess().timestamp();
332 }
bsalomon23e619c2015-02-06 11:54:28 -0800333
bsalomon9f2d1572015-02-17 11:47:40 -0800334 static int* AccessResourceIndex(GrGpuResource* const& res) {
335 return res->cacheAccess().accessCacheIndex();
336 }
337
Robert Phillipsd074b622021-03-15 08:49:24 -0400338 using TextureFreedMessageBus = SkMessageBus<GrTextureFreedMessage,
339 GrDirectContext::DirectContextID>;
340
Robert Phillipse7a959d2021-03-11 14:44:42 -0500341 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage, uint32_t>::Inbox InvalidUniqueKeyInbox;
bsalomon9f2d1572015-02-17 11:47:40 -0800342 typedef SkTDPQueue<GrGpuResource*, CompareTimestamp, AccessResourceIndex> PurgeableQueue;
bsalomonf320e042015-02-17 15:09:34 -0800343 typedef SkTDArray<GrGpuResource*> ResourceArray;
bsalomon9f2d1572015-02-17 11:47:40 -0800344
Brian Salomon2c791fc2019-04-02 11:52:03 -0400345 GrProxyProvider* fProxyProvider = nullptr;
Robert Phillipsd464feb2020-10-08 11:00:02 -0400346 GrThreadSafeCache* fThreadSafeCache = nullptr;
Robert Phillips12d06a32020-09-16 12:31:34 -0400347
bsalomon9f2d1572015-02-17 11:47:40 -0800348 // Whenever a resource is added to the cache or the result of a cache lookup, fTimestamp is
349 // assigned as the resource's timestamp and then incremented. fPurgeableQueue orders the
350 // purgeable resources by this value, and thus is used to purge resources in LRU order.
Brian Salomon2c791fc2019-04-02 11:52:03 -0400351 uint32_t fTimestamp = 0;
bsalomon9f2d1572015-02-17 11:47:40 -0800352 PurgeableQueue fPurgeableQueue;
bsalomonf320e042015-02-17 15:09:34 -0800353 ResourceArray fNonpurgeableResources;
bsalomon9f2d1572015-02-17 11:47:40 -0800354
bsalomon744998e2014-08-28 09:54:34 -0700355 // This map holds all resources that can be used as scratch resources.
bsalomon8b79d232014-11-10 10:19:06 -0800356 ScratchMap fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800357 // This holds all resources that have unique keys.
358 UniqueHash fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800359
360 // our budget, used in purgeAsNeeded()
Brian Salomon2c791fc2019-04-02 11:52:03 -0400361 size_t fMaxBytes = kDefaultMaxSize;
bsalomon71cb0c22014-11-14 12:10:14 -0800362
363#if GR_CACHE_STATS
Brian Salomon2c791fc2019-04-02 11:52:03 -0400364 int fHighWaterCount = 0;
365 size_t fHighWaterBytes = 0;
366 int fBudgetedHighWaterCount = 0;
367 size_t fBudgetedHighWaterBytes = 0;
bsalomon71cb0c22014-11-14 12:10:14 -0800368#endif
369
bsalomondace19e2014-11-17 07:34:06 -0800370 // our current stats for all resources
Brian Salomon2c791fc2019-04-02 11:52:03 -0400371 SkDEBUGCODE(int fCount = 0;)
372 size_t fBytes = 0;
bsalomon71cb0c22014-11-14 12:10:14 -0800373
bsalomondace19e2014-11-17 07:34:06 -0800374 // our current stats for resources that count against the budget
Brian Salomon2c791fc2019-04-02 11:52:03 -0400375 int fBudgetedCount = 0;
376 size_t fBudgetedBytes = 0;
377 size_t fPurgeableBytes = 0;
378 int fNumBudgetedResourcesFlushWillMakePurgeable = 0;
bsalomondace19e2014-11-17 07:34:06 -0800379
bsalomon8718aaf2015-02-19 07:24:21 -0800380 InvalidUniqueKeyInbox fInvalidUniqueKeyInbox;
Robert Phillipsd074b622021-03-15 08:49:24 -0400381 TextureFreedMessageBus::Inbox fFreedTextureInbox;
Robert Phillipsddc21482019-10-16 14:30:09 -0400382 TexturesAwaitingUnref fTexturesAwaitingUnref;
Greg Danielc27eb722018-08-10 09:48:08 -0400383
Robert Phillipsd074b622021-03-15 08:49:24 -0400384 GrDirectContext::DirectContextID fOwningContextID;
Brian Salomon2c791fc2019-04-02 11:52:03 -0400385 uint32_t fContextUniqueID = SK_InvalidUniqueID;
386 GrSingleOwner* fSingleOwner = nullptr;
bsalomon3f324322015-04-08 11:01:54 -0700387
388 // This resource is allowed to be in the nonpurgeable array for the sake of validate() because
389 // we're in the midst of converting it to purgeable status.
Brian Salomon2c791fc2019-04-02 11:52:03 -0400390 SkDEBUGCODE(GrGpuResource* fNewlyPurgeableResourceForValidation = nullptr;)
bsalomonc8dc1f72014-08-21 13:02:13 -0700391};
392
bsalomon0ea80f42015-02-11 10:49:59 -0800393class GrResourceCache::ResourceAccess {
bsalomon71cb0c22014-11-14 12:10:14 -0800394private:
bsalomon0ea80f42015-02-11 10:49:59 -0800395 ResourceAccess(GrResourceCache* cache) : fCache(cache) { }
bsalomon71cb0c22014-11-14 12:10:14 -0800396 ResourceAccess(const ResourceAccess& that) : fCache(that.fCache) { }
John Stilesb35c3b82020-08-06 19:58:52 -0400397 ResourceAccess& operator=(const ResourceAccess&) = delete;
bsalomon71cb0c22014-11-14 12:10:14 -0800398
399 /**
400 * Insert a resource into the cache.
401 */
402 void insertResource(GrGpuResource* resource) { fCache->insertResource(resource); }
403
404 /**
405 * Removes a resource from the cache.
406 */
407 void removeResource(GrGpuResource* resource) { fCache->removeResource(resource); }
408
409 /**
Brian Salomon2c791fc2019-04-02 11:52:03 -0400410 * Adds a ref to a resource with proper tracking if the resource has 0 refs prior to
411 * adding the ref.
412 */
413 void refResource(GrGpuResource* resource) { fCache->refResource(resource); }
414
415 /**
bsalomon3f324322015-04-08 11:01:54 -0700416 * Notifications that should be sent to the cache when the ref/io cnt status of resources
417 * changes.
bsalomon71cb0c22014-11-14 12:10:14 -0800418 */
bsalomon3f324322015-04-08 11:01:54 -0700419 enum RefNotificationFlags {
420 /** All types of refs on the resource have reached zero. */
421 kAllCntsReachedZero_RefNotificationFlag = 0x1,
422 /** The normal (not pending IO type) ref cnt has reached zero. */
423 kRefCntReachedZero_RefNotificationFlag = 0x2,
424 };
425 /**
Greg Danielda642612021-02-09 18:04:02 -0500426 * Called by GrGpuResources when they detect one of their ref cnts have reached zero. This may
427 * either be the main ref or the command buffer usage ref.
bsalomon3f324322015-04-08 11:01:54 -0700428 */
Greg Danielda642612021-02-09 18:04:02 -0500429 void notifyARefCntReachedZero(GrGpuResource* resource,
430 GrGpuResource::LastRemovedRef removedRef) {
431 fCache->notifyARefCntReachedZero(resource, removedRef);
bsalomon3f324322015-04-08 11:01:54 -0700432 }
bsalomon71cb0c22014-11-14 12:10:14 -0800433
434 /**
bsalomonf99e9612015-02-19 08:24:16 -0800435 * Called by GrGpuResources to change their unique keys.
bsalomon71cb0c22014-11-14 12:10:14 -0800436 */
bsalomonf99e9612015-02-19 08:24:16 -0800437 void changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) {
438 fCache->changeUniqueKey(resource, newKey);
439 }
bsalomon71cb0c22014-11-14 12:10:14 -0800440
bsalomon10e23ca2014-11-25 05:52:06 -0800441 /**
bsalomonf99e9612015-02-19 08:24:16 -0800442 * Called by a GrGpuResource to remove its unique key.
bsalomon23e619c2015-02-06 11:54:28 -0800443 */
bsalomonf99e9612015-02-19 08:24:16 -0800444 void removeUniqueKey(GrGpuResource* resource) { fCache->removeUniqueKey(resource); }
bsalomon23e619c2015-02-06 11:54:28 -0800445
446 /**
447 * Called by a GrGpuResource when it removes its scratch key.
bsalomon10e23ca2014-11-25 05:52:06 -0800448 */
449 void willRemoveScratchKey(const GrGpuResource* resource) {
450 fCache->willRemoveScratchKey(resource);
451 }
bsalomon84c8e622014-11-17 09:33:27 -0800452
453 /**
454 * Called by GrGpuResources when they change from budgeted to unbudgeted or vice versa.
455 */
456 void didChangeBudgetStatus(GrGpuResource* resource) { fCache->didChangeBudgetStatus(resource); }
457
bsalomon71cb0c22014-11-14 12:10:14 -0800458 // No taking addresses of this type.
459 const ResourceAccess* operator&() const;
460 ResourceAccess* operator&();
461
bsalomon0ea80f42015-02-11 10:49:59 -0800462 GrResourceCache* fCache;
bsalomon71cb0c22014-11-14 12:10:14 -0800463
464 friend class GrGpuResource; // To access all the proxy inline methods.
bsalomon0ea80f42015-02-11 10:49:59 -0800465 friend class GrResourceCache; // To create this type.
bsalomon71cb0c22014-11-14 12:10:14 -0800466};
467
bsalomon0ea80f42015-02-11 10:49:59 -0800468inline GrResourceCache::ResourceAccess GrResourceCache::resourceAccess() {
bsalomon71cb0c22014-11-14 12:10:14 -0800469 return ResourceAccess(this);
470}
471
bsalomonc8dc1f72014-08-21 13:02:13 -0700472#endif