xref: /aosp_15_r20/external/skia/src/gpu/ganesh/GrResourceCache.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
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 
8 #ifndef GrResourceCache_DEFINED
9 #define GrResourceCache_DEFINED
10 
11 #include "include/core/SkRefCnt.h"
12 #include "include/core/SkTypes.h"
13 #include "include/gpu/ganesh/GrDirectContext.h"
14 #include "include/private/base/SkDebug.h"
15 #include "include/private/base/SkTArray.h"
16 #include "include/private/base/SkTDArray.h"
17 #include "include/private/base/SkTo.h"
18 #include "include/private/gpu/ganesh/GrTypesPriv.h"
19 #include "src/base/SkTDPQueue.h"
20 #include "src/core/SkMessageBus.h"
21 #include "src/core/SkTDynamicHash.h"
22 #include "src/core/SkTMultiMap.h"
23 #include "src/gpu/GpuTypesPriv.h"
24 #include "src/gpu/ResourceKey.h"
25 #include "src/gpu/ganesh/GrGpuResource.h"
26 #include "src/gpu/ganesh/GrGpuResourceCacheAccess.h"
27 #include "src/gpu/ganesh/GrGpuResourcePriv.h"
28 
29 #include <cstddef>
30 #include <cstdint>
31 #include <functional>
32 #include <type_traits>
33 #include <utility>
34 
35 class GrProxyProvider;
36 class GrSurface;
37 class GrThreadSafeCache;
38 class SkString;
39 class SkTraceMemoryDump;
40 enum class GrPurgeResourceOptions;
41 namespace skgpu {
42 class SingleOwner;
43 }
44 
45 /**
46  * Manages the lifetime of all GrGpuResource instances.
47  *
48  * Resources may have optionally have two types of keys:
49  *      1) A scratch key. This is for resources whose allocations are cached but not their contents.
50  *         Multiple resources can share the same scratch key. This is so a caller can have two
51  *         resource instances with the same properties (e.g. multipass rendering that ping-pongs
52  *         between two temporary surfaces). The scratch key is set at resource creation time and
53  *         should never change. Resources need not have a scratch key.
54  *      2) A unique key. This key's meaning is specific to the domain that created the key. Only one
55  *         resource may have a given unique key. The unique key can be set, cleared, or changed
56  *         anytime after resource creation.
57  *
58  * A unique key always takes precedence over a scratch key when a resource has both types of keys.
59  * If a resource has neither key type then it will be deleted as soon as the last reference to it
60  * is dropped.
61  */
62 class GrResourceCache {
63 public:
64     GrResourceCache(skgpu::SingleOwner* owner,
65                     GrDirectContext::DirectContextID owningContextID,
66                     uint32_t familyID);
67     ~GrResourceCache();
68 
69     /**
70      * This is used to safely return a resource to the cache when the owner may be on another
71      * thread from GrDirectContext. If the context still exists then using this method ensures that
72      * the resource is received by the cache for processing (recycling or destruction) on the
73      * context's thread.
74      *
75      * This is templated as it is rather than simply taking sk_sp<GrGpuResource> in order to enforce
76      * that the caller passes an rvalue. If the caller doesn't move its ref into this function
77      * then it will retain ownership, defeating the purpose. (Note that sk_sp<GrGpuResource>&&
78      * doesn't work either because calling with sk_sp<GrSpecificResource> will create a temporary
79      * sk_sp<GrGpuResource> which is an rvalue).
80      */
81     template<typename T>
82     static std::enable_if_t<std::is_base_of_v<GrGpuResource, T>, void>
ReturnResourceFromThread(sk_sp<T> && resource,GrDirectContext::DirectContextID id)83     ReturnResourceFromThread(sk_sp<T>&& resource, GrDirectContext::DirectContextID id) {
84         UnrefResourceMessage msg(std::move(resource), id);
85         UnrefResourceMessage::Bus::Post(std::move(msg));
86     }
87 
88     // Default maximum number of bytes of gpu memory of budgeted resources in the cache.
89     static const size_t kDefaultMaxSize             = 256 * (1 << 20);
90 
91     /** Used to access functionality needed by GrGpuResource for lifetime management. */
92     class ResourceAccess;
93     ResourceAccess resourceAccess();
94 
95     /** Unique ID of the owning GrContext. */
contextUniqueID()96     uint32_t contextUniqueID() const { return fContextUniqueID; }
97 
98     /** Sets the max gpu memory byte size of the cache. */
99     void setLimit(size_t bytes);
100 
101     /**
102      * Returns the number of resources.
103      */
getResourceCount()104     int getResourceCount() const {
105         return fPurgeableQueue.count() + fNonpurgeableResources.size();
106     }
107 
108     /**
109      * Returns the number of resources that count against the budget.
110      */
getBudgetedResourceCount()111     int getBudgetedResourceCount() const { return fBudgetedCount; }
112 
113     /**
114      * Returns the number of bytes consumed by resources.
115      */
getResourceBytes()116     size_t getResourceBytes() const { return fBytes; }
117 
118     /**
119      * Returns the number of bytes held by unlocked resources which are available for purging.
120      */
getPurgeableBytes()121     size_t getPurgeableBytes() const { return fPurgeableBytes; }
122 
123     /**
124      * Returns the number of bytes consumed by budgeted resources.
125      */
getBudgetedResourceBytes()126     size_t getBudgetedResourceBytes() const { return fBudgetedBytes; }
127 
128     /**
129      * Returns the number of bytes consumed by cached resources.
130      */
getMaxResourceBytes()131     size_t getMaxResourceBytes() const { return fMaxBytes; }
132 
133     /**
134      * Abandons the backend API resources owned by all GrGpuResource objects and removes them from
135      * the cache.
136      */
137     void abandonAll();
138 
139     /**
140      * Releases the backend API resources owned by all GrGpuResource objects and removes them from
141      * the cache.
142      */
143     void releaseAll();
144 
145     /**
146      * Find a resource that matches a scratch key.
147      */
148     GrGpuResource* findAndRefScratchResource(const skgpu::ScratchKey& scratchKey);
149 
150 #ifdef SK_DEBUG
151     // This is not particularly fast and only used for validation, so debug only.
countScratchEntriesForKey(const skgpu::ScratchKey & scratchKey)152     int countScratchEntriesForKey(const skgpu::ScratchKey& scratchKey) const {
153         return fScratchMap.countForKey(scratchKey);
154     }
155 #endif
156 
157     /**
158      * Find a resource that matches a unique key.
159      */
findAndRefUniqueResource(const skgpu::UniqueKey & key)160     GrGpuResource* findAndRefUniqueResource(const skgpu::UniqueKey& key) {
161         GrGpuResource* resource = fUniqueHash.find(key);
162         if (resource) {
163             this->refAndMakeResourceMRU(resource);
164         }
165         return resource;
166     }
167 
168     /**
169      * Query whether a unique key exists in the cache.
170      */
hasUniqueKey(const skgpu::UniqueKey & key)171     bool hasUniqueKey(const skgpu::UniqueKey& key) const {
172         return SkToBool(fUniqueHash.find(key));
173     }
174 
175     /** Purges resources to become under budget and processes resources with invalidated unique
176         keys. */
177     void purgeAsNeeded();
178 
179     // Purge unlocked resources. If 'opts' is kScratchResourcesOnly, the purgeable resources
180     // containing persistent data are spared. If it is kAllResources then all purgeable resources
181     // will be deleted.
purgeUnlockedResources(GrPurgeResourceOptions opts)182     void purgeUnlockedResources(GrPurgeResourceOptions opts) {
183         this->purgeUnlockedResources(/*purgeTime=*/nullptr, opts);
184     }
185 
186     // Purge unlocked resources not used since the passed point in time. If 'opts' is
187     // kScratchResourcesOnly, the purgeable resources containing persistent data are spared.
188     // If it is kAllResources then all purgeable resources older than 'purgeTime' will be deleted.
purgeResourcesNotUsedSince(skgpu::StdSteadyClock::time_point purgeTime,GrPurgeResourceOptions opts)189     void purgeResourcesNotUsedSince(skgpu::StdSteadyClock::time_point purgeTime,
190                                     GrPurgeResourceOptions opts) {
191         this->purgeUnlockedResources(&purgeTime, opts);
192     }
193 
194     /** If it's possible to purge enough resources to get the provided amount of budget
195         headroom, do so and return true. If it's not possible, do nothing and return false.
196      */
197     bool purgeToMakeHeadroom(size_t desiredHeadroomBytes);
198 
overBudget()199     bool overBudget() const { return fBudgetedBytes > fMaxBytes; }
200 
201     /**
202      * Purge unlocked resources from the cache until the the provided byte count has been reached
203      * or we have purged all unlocked resources. The default policy is to purge in LRU order, but
204      * can be overridden to prefer purging scratch resources (in LRU order) prior to purging other
205      * resource types.
206      *
207      * @param maxBytesToPurge the desired number of bytes to be purged.
208      * @param preferScratchResources If true scratch resources will be purged prior to other
209      *                               resource types.
210      */
211     void purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources);
212 
213     /** Returns true if the cache would like a flush to occur in order to make more resources
214         purgeable. */
215     bool requestsFlush() const;
216 
217 #if GR_CACHE_STATS
218     struct Stats {
219         int fTotal;
220         int fNumPurgeable;
221         int fNumNonPurgeable;
222 
223         int fScratch;
224         int fWrapped;
225         size_t fUnbudgetedSize;
226 
StatsStats227         Stats() { this->reset(); }
228 
resetStats229         void reset() {
230             fTotal = 0;
231             fNumPurgeable = 0;
232             fNumNonPurgeable = 0;
233             fScratch = 0;
234             fWrapped = 0;
235             fUnbudgetedSize = 0;
236         }
237 
updateStats238         void update(GrGpuResource* resource) {
239             if (resource->cacheAccess().isScratch()) {
240                 ++fScratch;
241             }
242             if (resource->resourcePriv().refsWrappedObjects()) {
243                 ++fWrapped;
244             }
245             if (GrBudgetedType::kBudgeted != resource->resourcePriv().budgetedType()) {
246                 fUnbudgetedSize += resource->gpuMemorySize();
247             }
248         }
249     };
250 
251     void getStats(Stats*) const;
252 
253 #if defined(GPU_TEST_UTILS)
254     void dumpStats(SkString*) const;
255 
256     void dumpStatsKeyValuePairs(
257             skia_private::TArray<SkString>* keys, skia_private::TArray<double>* value) const;
258 #endif
259 
260 #endif // GR_CACHE_STATS
261 
262 #if defined(GPU_TEST_UTILS)
263     int countUniqueKeysWithTag(const char* tag) const;
264 
265     void changeTimestamp(uint32_t newTimestamp);
266 
267     void visitSurfaces(const std::function<void(const GrSurface*, bool purgeable)>&) const;
268 #endif
269 
270     // Enumerates all cached resources and dumps their details to traceMemoryDump.
271     void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const;
272 
setProxyProvider(GrProxyProvider * proxyProvider)273     void setProxyProvider(GrProxyProvider* proxyProvider) { fProxyProvider = proxyProvider; }
setThreadSafeCache(GrThreadSafeCache * threadSafeCache)274     void setThreadSafeCache(GrThreadSafeCache* threadSafeCache) {
275         fThreadSafeCache = threadSafeCache;
276     }
277 
278     // It'd be nice if this could be private but SkMessageBus relies on macros to define types that
279     // require this to be public.
280     class UnrefResourceMessage {
281     public:
recipient()282         GrDirectContext::DirectContextID recipient() const { return fRecipient; }
283 
284         UnrefResourceMessage(UnrefResourceMessage&&) = default;
285         UnrefResourceMessage& operator=(UnrefResourceMessage&&) = default;
286 
287     private:
288         friend class GrResourceCache;
289 
290         using Bus = SkMessageBus<UnrefResourceMessage,
291                                  GrDirectContext::DirectContextID,
292                                  /*AllowCopyableMessage=*/false>;
293 
UnrefResourceMessage(sk_sp<GrGpuResource> && resource,GrDirectContext::DirectContextID recipient)294         UnrefResourceMessage(sk_sp<GrGpuResource>&& resource,
295                              GrDirectContext::DirectContextID recipient)
296                 : fResource(std::move(resource)), fRecipient(recipient) {}
297 
298         UnrefResourceMessage(const UnrefResourceMessage&) = delete;
299         UnrefResourceMessage& operator=(const UnrefResourceMessage&) = delete;
300 
301         sk_sp<GrGpuResource> fResource;
302         GrDirectContext::DirectContextID fRecipient;
303     };
304 
305 private:
306     ///////////////////////////////////////////////////////////////////////////
307     /// @name Methods accessible via ResourceAccess
308     ////
309     void insertResource(GrGpuResource*);
310     void removeResource(GrGpuResource*);
311     void notifyARefCntReachedZero(GrGpuResource*, GrGpuResource::LastRemovedRef);
312     void changeUniqueKey(GrGpuResource*, const skgpu::UniqueKey&);
313     void removeUniqueKey(GrGpuResource*);
314     void willRemoveScratchKey(const GrGpuResource*);
315     void didChangeBudgetStatus(GrGpuResource*);
316     void refResource(GrGpuResource* resource);
317     /// @}
318 
319     void refAndMakeResourceMRU(GrGpuResource*);
320     void processFreedGpuResources();
321     void addToNonpurgeableArray(GrGpuResource*);
322     void removeFromNonpurgeableArray(GrGpuResource*);
323 
wouldFit(size_t bytes)324     bool wouldFit(size_t bytes) const { return fBudgetedBytes+bytes <= fMaxBytes; }
325 
326     uint32_t getNextTimestamp();
327 
328     void purgeUnlockedResources(const skgpu::StdSteadyClock::time_point* purgeTime,
329                                 GrPurgeResourceOptions opts);
330 
331 #ifdef SK_DEBUG
332     bool isInCache(const GrGpuResource* r) const;
333     void validate() const;
334 #else
validate()335     void validate() const {}
336 #endif
337 
338     class AutoValidate;
339 
340     struct ScratchMapTraits {
GetKeyScratchMapTraits341         static const skgpu::ScratchKey& GetKey(const GrGpuResource& r) {
342             return r.resourcePriv().getScratchKey();
343         }
344 
HashScratchMapTraits345         static uint32_t Hash(const skgpu::ScratchKey& key) { return key.hash(); }
OnFreeScratchMapTraits346         static void OnFree(GrGpuResource*) { }
347     };
348     typedef SkTMultiMap<GrGpuResource, skgpu::ScratchKey, ScratchMapTraits> ScratchMap;
349 
350     struct UniqueHashTraits {
GetKeyUniqueHashTraits351         static const skgpu::UniqueKey& GetKey(const GrGpuResource& r) { return r.getUniqueKey(); }
352 
HashUniqueHashTraits353         static uint32_t Hash(const skgpu::UniqueKey& key) { return key.hash(); }
354     };
355     typedef SkTDynamicHash<GrGpuResource, skgpu::UniqueKey, UniqueHashTraits> UniqueHash;
356 
CompareTimestamp(GrGpuResource * const & a,GrGpuResource * const & b)357     static bool CompareTimestamp(GrGpuResource* const& a, GrGpuResource* const& b) {
358         return a->cacheAccess().timestamp() < b->cacheAccess().timestamp();
359     }
360 
AccessResourceIndex(GrGpuResource * const & res)361     static int* AccessResourceIndex(GrGpuResource* const& res) {
362         return res->cacheAccess().accessCacheIndex();
363     }
364 
365     typedef SkMessageBus<skgpu::UniqueKeyInvalidatedMessage, uint32_t>::Inbox InvalidUniqueKeyInbox;
366     typedef SkTDPQueue<GrGpuResource*, CompareTimestamp, AccessResourceIndex> PurgeableQueue;
367     typedef SkTDArray<GrGpuResource*> ResourceArray;
368 
369     GrProxyProvider*                    fProxyProvider = nullptr;
370     GrThreadSafeCache*                  fThreadSafeCache = nullptr;
371 
372     // Whenever a resource is added to the cache or the result of a cache lookup, fTimestamp is
373     // assigned as the resource's timestamp and then incremented. fPurgeableQueue orders the
374     // purgeable resources by this value, and thus is used to purge resources in LRU order.
375     uint32_t                            fTimestamp = 0;
376     PurgeableQueue                      fPurgeableQueue;
377     ResourceArray                       fNonpurgeableResources;
378 
379     // This map holds all resources that can be used as scratch resources.
380     ScratchMap                          fScratchMap;
381     // This holds all resources that have unique keys.
382     UniqueHash                          fUniqueHash;
383 
384     // our budget, used in purgeAsNeeded()
385     size_t                              fMaxBytes = kDefaultMaxSize;
386 
387 #if GR_CACHE_STATS
388     int                                 fHighWaterCount = 0;
389     size_t                              fHighWaterBytes = 0;
390     int                                 fBudgetedHighWaterCount = 0;
391     size_t                              fBudgetedHighWaterBytes = 0;
392 #endif
393 
394     // our current stats for all resources
395     SkDEBUGCODE(int                     fCount = 0;)
396     size_t                              fBytes = 0;
397 
398     // our current stats for resources that count against the budget
399     int                                 fBudgetedCount = 0;
400     size_t                              fBudgetedBytes = 0;
401     size_t                              fPurgeableBytes = 0;
402     int                                 fNumBudgetedResourcesFlushWillMakePurgeable = 0;
403 
404     InvalidUniqueKeyInbox               fInvalidUniqueKeyInbox;
405     UnrefResourceMessage::Bus::Inbox    fUnrefResourceInbox;
406 
407     GrDirectContext::DirectContextID    fOwningContextID;
408     uint32_t                            fContextUniqueID = SK_InvalidUniqueID;
409     skgpu::SingleOwner*                 fSingleOwner = nullptr;
410 
411     // This resource is allowed to be in the nonpurgeable array for the sake of validate() because
412     // we're in the midst of converting it to purgeable status.
413     SkDEBUGCODE(GrGpuResource*          fNewlyPurgeableResourceForValidation = nullptr;)
414 };
415 
416 class GrResourceCache::ResourceAccess {
417 private:
ResourceAccess(GrResourceCache * cache)418     ResourceAccess(GrResourceCache* cache) : fCache(cache) { }
ResourceAccess(const ResourceAccess & that)419     ResourceAccess(const ResourceAccess& that) : fCache(that.fCache) { }
420     ResourceAccess& operator=(const ResourceAccess&) = delete;
421 
422     /**
423      * Insert a resource into the cache.
424      */
insertResource(GrGpuResource * resource)425     void insertResource(GrGpuResource* resource) { fCache->insertResource(resource); }
426 
427     /**
428      * Removes a resource from the cache.
429      */
removeResource(GrGpuResource * resource)430     void removeResource(GrGpuResource* resource) { fCache->removeResource(resource); }
431 
432     /**
433      * Adds a ref to a resource with proper tracking if the resource has 0 refs prior to
434      * adding the ref.
435      */
refResource(GrGpuResource * resource)436     void refResource(GrGpuResource* resource) { fCache->refResource(resource); }
437 
438     /**
439      * Notifications that should be sent to the cache when the ref/io cnt status of resources
440      * changes.
441      */
442     enum RefNotificationFlags {
443         /** All types of refs on the resource have reached zero. */
444         kAllCntsReachedZero_RefNotificationFlag = 0x1,
445         /** The normal (not pending IO type) ref cnt has reached zero. */
446         kRefCntReachedZero_RefNotificationFlag  = 0x2,
447     };
448     /**
449      * Called by GrGpuResources when they detect one of their ref cnts have reached zero. This may
450      * either be the main ref or the command buffer usage ref.
451      */
notifyARefCntReachedZero(GrGpuResource * resource,GrGpuResource::LastRemovedRef removedRef)452     void notifyARefCntReachedZero(GrGpuResource* resource,
453                                   GrGpuResource::LastRemovedRef removedRef) {
454         fCache->notifyARefCntReachedZero(resource, removedRef);
455     }
456 
457     /**
458      * Called by GrGpuResources to change their unique keys.
459      */
changeUniqueKey(GrGpuResource * resource,const skgpu::UniqueKey & newKey)460     void changeUniqueKey(GrGpuResource* resource, const skgpu::UniqueKey& newKey) {
461          fCache->changeUniqueKey(resource, newKey);
462     }
463 
464     /**
465      * Called by a GrGpuResource to remove its unique key.
466      */
removeUniqueKey(GrGpuResource * resource)467     void removeUniqueKey(GrGpuResource* resource) { fCache->removeUniqueKey(resource); }
468 
469     /**
470      * Called by a GrGpuResource when it removes its scratch key.
471      */
willRemoveScratchKey(const GrGpuResource * resource)472     void willRemoveScratchKey(const GrGpuResource* resource) {
473         fCache->willRemoveScratchKey(resource);
474     }
475 
476     /**
477      * Called by GrGpuResources when they change from budgeted to unbudgeted or vice versa.
478      */
didChangeBudgetStatus(GrGpuResource * resource)479     void didChangeBudgetStatus(GrGpuResource* resource) { fCache->didChangeBudgetStatus(resource); }
480 
481     // No taking addresses of this type.
482     const ResourceAccess* operator&() const;
483     ResourceAccess* operator&();
484 
485     GrResourceCache* fCache;
486 
487     friend class GrGpuResource; // To access all the proxy inline methods.
488     friend class GrResourceCache; // To create this type.
489 };
490 
SkShouldPostMessageToBus(const GrResourceCache::UnrefResourceMessage & msg,GrDirectContext::DirectContextID potentialRecipient)491 static inline bool SkShouldPostMessageToBus(const GrResourceCache::UnrefResourceMessage& msg,
492                                             GrDirectContext::DirectContextID potentialRecipient) {
493     return potentialRecipient == msg.recipient();
494 }
495 
resourceAccess()496 inline GrResourceCache::ResourceAccess GrResourceCache::resourceAccess() {
497     return ResourceAccess(this);
498 }
499 
500 #endif
501