• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/gpu/GrDirectContext.h"
13 #include "include/private/GrResourceKey.h"
14 #include "include/private/SkTArray.h"
15 #include "include/private/SkTHash.h"
16 #include "src/core/SkMessageBus.h"
17 #include "src/core/SkTDPQueue.h"
18 #include "src/core/SkTInternalLList.h"
19 #include "src/core/SkTMultiMap.h"
20 #include "src/gpu/GrGpuResource.h"
21 #include "src/gpu/GrGpuResourceCacheAccess.h"
22 #include "src/gpu/GrGpuResourcePriv.h"
23 
24 class GrCaps;
25 class GrProxyProvider;
26 class SkString;
27 class SkTraceMemoryDump;
28 class GrSingleOwner;
29 class GrTexture;
30 class GrThreadSafeCache;
31 
32 struct GrTextureFreedMessage {
33     GrTexture* fTexture;
34     GrDirectContext::DirectContextID fIntendedRecipient;
35 };
36 
SkShouldPostMessageToBus(const GrTextureFreedMessage & msg,GrDirectContext::DirectContextID potentialRecipient)37 static inline bool SkShouldPostMessageToBus(
38         const GrTextureFreedMessage& msg, GrDirectContext::DirectContextID potentialRecipient) {
39     return potentialRecipient == msg.fIntendedRecipient;
40 }
41 
42 /**
43  * 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
48  *         resource instances with the same properties (e.g. multipass rendering that ping-pongs
49  *         between two temporary surfaces). The scratch key is set at resource creation time and
50  *         should never change. Resources need not have a scratch key.
51  *      2) A unique key. This key's meaning is specific to the domain that created the key. Only one
52  *         resource may have a given unique key. The unique key can be set, cleared, or changed
53  *         anytime after resource creation.
54  *
55  * A unique key always takes precedence over a scratch key when a resource has both types of keys.
56  * If a resource has neither key type then it will be deleted as soon as the last reference to it
57  * is dropped.
58  */
59 class GrResourceCache {
60 public:
61     GrResourceCache(GrSingleOwner* owner,
62                     GrDirectContext::DirectContextID owningContextID,
63                     uint32_t familyID);
64     ~GrResourceCache();
65 
66     // Default maximum number of bytes of gpu memory of budgeted resources in the cache.
67     static const size_t kDefaultMaxSize             = 256 * (1 << 20);
68 
69     /** Used to access functionality needed by GrGpuResource for lifetime management. */
70     class ResourceAccess;
71     ResourceAccess resourceAccess();
72 
73     /** Unique ID of the owning GrContext. */
contextUniqueID()74     uint32_t contextUniqueID() const { return fContextUniqueID; }
75 
76     /** Sets the max gpu memory byte size of the cache. */
77     void setLimit(size_t bytes);
78 
79     /**
80      * Returns the number of resources.
81      */
getResourceCount()82     int getResourceCount() const {
83         return fPurgeableQueue.count() + fNonpurgeableResources.count();
84     }
85 
86     /**
87      * Returns the number of resources that count against the budget.
88      */
getBudgetedResourceCount()89     int getBudgetedResourceCount() const { return fBudgetedCount; }
90 
91     /**
92      * Returns the number of bytes consumed by resources.
93      */
getResourceBytes()94     size_t getResourceBytes() const { return fBytes; }
95 
96     /**
97      * Returns the number of bytes held by unlocked resources which are available for purging.
98      */
getPurgeableBytes()99     size_t getPurgeableBytes() const { return fPurgeableBytes; }
100 
101     /**
102      * Returns the number of bytes consumed by budgeted resources.
103      */
getBudgetedResourceBytes()104     size_t getBudgetedResourceBytes() const { return fBudgetedBytes; }
105 
106     /**
107      * Returns the number of bytes consumed by cached resources.
108      */
getMaxResourceBytes()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      */
115     void abandonAll();
116 
117     /**
118      * Releases the backend API resources owned by all GrGpuResource objects and removes them from
119      * the cache.
120      */
121     void releaseAll();
122 
123     /**
124      * Find a resource that matches a scratch key.
125      */
126     GrGpuResource* findAndRefScratchResource(const GrScratchKey& scratchKey);
127 
128 #ifdef SK_DEBUG
129     // This is not particularly fast and only used for validation, so debug only.
countScratchEntriesForKey(const GrScratchKey & scratchKey)130     int countScratchEntriesForKey(const GrScratchKey& scratchKey) const {
131         return fScratchMap.countForKey(scratchKey);
132     }
133 #endif
134 
135     /**
136      * Find a resource that matches a unique key.
137      */
findAndRefUniqueResource(const GrUniqueKey & key)138     GrGpuResource* findAndRefUniqueResource(const GrUniqueKey& key) {
139         GrGpuResource* resource = fUniqueHash.find(key);
140         if (resource) {
141             this->refAndMakeResourceMRU(resource);
142         }
143         return resource;
144     }
145 
146     /**
147      * Query whether a unique key exists in the cache.
148      */
hasUniqueKey(const GrUniqueKey & key)149     bool hasUniqueKey(const GrUniqueKey& key) const {
150         return SkToBool(fUniqueHash.find(key));
151     }
152 
153     /** Purges resources to become under budget and processes resources with invalidated unique
154         keys. */
155     void purgeAsNeeded();
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=false) {
161         this->purgeUnlockedResources(/*purgeTime=*/nullptr, scratchResourcesOnly);
162     }
163 
164     // 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     }
171 
172     /** 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 
overBudget()177     bool overBudget() const { return fBudgetedBytes > fMaxBytes; }
178 
179     /**
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 
191     /** Returns true if the cache would like a flush to occur in order to make more resources
192         purgeable. */
193     bool requestsFlush() const;
194 
195     /** Maintain a ref to this texture until we receive a GrTextureFreedMessage. */
196     void insertDelayedTextureUnref(GrTexture*);
197 
198 #if GR_CACHE_STATS
199     struct Stats {
200         int fTotal;
201         int fNumPurgeable;
202         int fNumNonPurgeable;
203 
204         int fScratch;
205         int fWrapped;
206         size_t fUnbudgetedSize;
207 
StatsStats208         Stats() { this->reset(); }
209 
resetStats210         void reset() {
211             fTotal = 0;
212             fNumPurgeable = 0;
213             fNumNonPurgeable = 0;
214             fScratch = 0;
215             fWrapped = 0;
216             fUnbudgetedSize = 0;
217         }
218 
updateStats219         void update(GrGpuResource* resource) {
220             if (resource->cacheAccess().isScratch()) {
221                 ++fScratch;
222             }
223             if (resource->resourcePriv().refsWrappedObjects()) {
224                 ++fWrapped;
225             }
226             if (GrBudgetedType::kBudgeted != resource->resourcePriv().budgetedType()) {
227                 fUnbudgetedSize += resource->gpuMemorySize();
228             }
229         }
230     };
231 
232     void getStats(Stats*) const;
233 
234 #if GR_TEST_UTILS
235     void dumpStats(SkString*) const;
236 
237     void dumpStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* value) const;
238 #endif
239 
240 #endif // GR_CACHE_STATS
241 
242 #if GR_TEST_UTILS
243     int countUniqueKeysWithTag(const char* tag) const;
244 
245     void changeTimestamp(uint32_t newTimestamp);
246 #endif
247 
248     // Enumerates all cached resources and dumps their details to traceMemoryDump.
249     void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const;
250 
setProxyProvider(GrProxyProvider * proxyProvider)251     void setProxyProvider(GrProxyProvider* proxyProvider) { fProxyProvider = proxyProvider; }
setThreadSafeCache(GrThreadSafeCache * threadSafeCache)252     void setThreadSafeCache(GrThreadSafeCache* threadSafeCache) {
253         fThreadSafeCache = threadSafeCache;
254     }
255 
256 private:
257     ///////////////////////////////////////////////////////////////////////////
258     /// @name Methods accessible via ResourceAccess
259     ////
260     void insertResource(GrGpuResource*);
261     void removeResource(GrGpuResource*);
262     void notifyARefCntReachedZero(GrGpuResource*, GrGpuResource::LastRemovedRef);
263     void changeUniqueKey(GrGpuResource*, const GrUniqueKey&);
264     void removeUniqueKey(GrGpuResource*);
265     void willRemoveScratchKey(const GrGpuResource*);
266     void didChangeBudgetStatus(GrGpuResource*);
267     void refResource(GrGpuResource* resource);
268     /// @}
269 
270     void refAndMakeResourceMRU(GrGpuResource*);
271     void processFreedGpuResources();
272     void addToNonpurgeableArray(GrGpuResource*);
273     void removeFromNonpurgeableArray(GrGpuResource*);
274 
wouldFit(size_t bytes)275     bool wouldFit(size_t bytes) const { return fBudgetedBytes+bytes <= fMaxBytes; }
276 
277     uint32_t getNextTimestamp();
278 
279     void purgeUnlockedResources(const GrStdSteadyClock::time_point* purgeTime,
280                                 bool scratchResourcesOnly);
281 
282 #ifdef SK_DEBUG
283     bool isInCache(const GrGpuResource* r) const;
284     void validate() const;
285 #else
validate()286     void validate() const {}
287 #endif
288 
289     class AutoValidate;
290 
291     class AvailableForScratchUse;
292 
293     struct ScratchMapTraits {
GetKeyScratchMapTraits294         static const GrScratchKey& GetKey(const GrGpuResource& r) {
295             return r.resourcePriv().getScratchKey();
296         }
297 
HashScratchMapTraits298         static uint32_t Hash(const GrScratchKey& key) { return key.hash(); }
OnFreeScratchMapTraits299         static void OnFree(GrGpuResource*) { }
300     };
301     typedef SkTMultiMap<GrGpuResource, GrScratchKey, ScratchMapTraits> ScratchMap;
302 
303     struct UniqueHashTraits {
GetKeyUniqueHashTraits304         static const GrUniqueKey& GetKey(const GrGpuResource& r) { return r.getUniqueKey(); }
305 
HashUniqueHashTraits306         static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); }
307     };
308     typedef SkTDynamicHash<GrGpuResource, GrUniqueKey, UniqueHashTraits> UniqueHash;
309 
310     class TextureAwaitingUnref {
311     public:
312         TextureAwaitingUnref();
313         TextureAwaitingUnref(GrTexture* texture);
314         TextureAwaitingUnref(const TextureAwaitingUnref&) = delete;
315         TextureAwaitingUnref& operator=(const TextureAwaitingUnref&) = delete;
316         TextureAwaitingUnref(TextureAwaitingUnref&&);
317         TextureAwaitingUnref& operator=(TextureAwaitingUnref&&);
318         ~TextureAwaitingUnref();
319         void addRef();
320         void unref();
321         bool finished();
322 
323     private:
324         GrTexture* fTexture = nullptr;
325         int fNumUnrefs = 0;
326     };
327     using TexturesAwaitingUnref = SkTHashMap<uint32_t, TextureAwaitingUnref>;
328 
CompareTimestamp(GrGpuResource * const & a,GrGpuResource * const & b)329     static bool CompareTimestamp(GrGpuResource* const& a, GrGpuResource* const& b) {
330         return a->cacheAccess().timestamp() < b->cacheAccess().timestamp();
331     }
332 
AccessResourceIndex(GrGpuResource * const & res)333     static int* AccessResourceIndex(GrGpuResource* const& res) {
334         return res->cacheAccess().accessCacheIndex();
335     }
336 
337     using TextureFreedMessageBus = SkMessageBus<GrTextureFreedMessage,
338                                                 GrDirectContext::DirectContextID>;
339 
340     typedef SkMessageBus<GrUniqueKeyInvalidatedMessage, uint32_t>::Inbox InvalidUniqueKeyInbox;
341     typedef SkTDPQueue<GrGpuResource*, CompareTimestamp, AccessResourceIndex> PurgeableQueue;
342     typedef SkTDArray<GrGpuResource*> ResourceArray;
343 
344     GrProxyProvider*                    fProxyProvider = nullptr;
345     GrThreadSafeCache*                  fThreadSafeCache = nullptr;
346 
347     // Whenever a resource is added to the cache or the result of a cache lookup, fTimestamp is
348     // assigned as the resource's timestamp and then incremented. fPurgeableQueue orders the
349     // purgeable resources by this value, and thus is used to purge resources in LRU order.
350     uint32_t                            fTimestamp = 0;
351     PurgeableQueue                      fPurgeableQueue;
352     ResourceArray                       fNonpurgeableResources;
353 
354     // This map holds all resources that can be used as scratch resources.
355     ScratchMap                          fScratchMap;
356     // This holds all resources that have unique keys.
357     UniqueHash                          fUniqueHash;
358 
359     // our budget, used in purgeAsNeeded()
360     size_t                              fMaxBytes = kDefaultMaxSize;
361 
362 #if GR_CACHE_STATS
363     int                                 fHighWaterCount = 0;
364     size_t                              fHighWaterBytes = 0;
365     int                                 fBudgetedHighWaterCount = 0;
366     size_t                              fBudgetedHighWaterBytes = 0;
367 #endif
368 
369     // our current stats for all resources
370     SkDEBUGCODE(int                     fCount = 0;)
371     size_t                              fBytes = 0;
372 
373     // our current stats for resources that count against the budget
374     int                                 fBudgetedCount = 0;
375     size_t                              fBudgetedBytes = 0;
376     size_t                              fPurgeableBytes = 0;
377     int                                 fNumBudgetedResourcesFlushWillMakePurgeable = 0;
378 
379     InvalidUniqueKeyInbox               fInvalidUniqueKeyInbox;
380     TextureFreedMessageBus::Inbox       fFreedTextureInbox;
381     TexturesAwaitingUnref               fTexturesAwaitingUnref;
382 
383     GrDirectContext::DirectContextID    fOwningContextID;
384     uint32_t                            fContextUniqueID = SK_InvalidUniqueID;
385     GrSingleOwner*                      fSingleOwner = nullptr;
386 
387     // This resource is allowed to be in the nonpurgeable array for the sake of validate() because
388     // we're in the midst of converting it to purgeable status.
389     SkDEBUGCODE(GrGpuResource*          fNewlyPurgeableResourceForValidation = nullptr;)
390 };
391 
392 class GrResourceCache::ResourceAccess {
393 private:
ResourceAccess(GrResourceCache * cache)394     ResourceAccess(GrResourceCache* cache) : fCache(cache) { }
ResourceAccess(const ResourceAccess & that)395     ResourceAccess(const ResourceAccess& that) : fCache(that.fCache) { }
396     ResourceAccess& operator=(const ResourceAccess&) = delete;
397 
398     /**
399      * Insert a resource into the cache.
400      */
insertResource(GrGpuResource * resource)401     void insertResource(GrGpuResource* resource) { fCache->insertResource(resource); }
402 
403     /**
404      * Removes a resource from the cache.
405      */
removeResource(GrGpuResource * resource)406     void removeResource(GrGpuResource* resource) { fCache->removeResource(resource); }
407 
408     /**
409      * Adds a ref to a resource with proper tracking if the resource has 0 refs prior to
410      * adding the ref.
411      */
refResource(GrGpuResource * resource)412     void refResource(GrGpuResource* resource) { fCache->refResource(resource); }
413 
414     /**
415      * Notifications that should be sent to the cache when the ref/io cnt status of resources
416      * changes.
417      */
418     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 one of their ref cnts have reached zero. This may
426      * either be the main ref or the command buffer usage ref.
427      */
notifyARefCntReachedZero(GrGpuResource * resource,GrGpuResource::LastRemovedRef removedRef)428     void notifyARefCntReachedZero(GrGpuResource* resource,
429                                   GrGpuResource::LastRemovedRef removedRef) {
430         fCache->notifyARefCntReachedZero(resource, removedRef);
431     }
432 
433     /**
434      * Called by GrGpuResources to change their unique keys.
435      */
changeUniqueKey(GrGpuResource * resource,const GrUniqueKey & newKey)436     void changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) {
437          fCache->changeUniqueKey(resource, newKey);
438     }
439 
440     /**
441      * Called by a GrGpuResource to remove its unique key.
442      */
removeUniqueKey(GrGpuResource * resource)443     void removeUniqueKey(GrGpuResource* resource) { fCache->removeUniqueKey(resource); }
444 
445     /**
446      * Called by a GrGpuResource when it removes its scratch key.
447      */
willRemoveScratchKey(const GrGpuResource * resource)448     void willRemoveScratchKey(const GrGpuResource* resource) {
449         fCache->willRemoveScratchKey(resource);
450     }
451 
452     /**
453      * Called by GrGpuResources when they change from budgeted to unbudgeted or vice versa.
454      */
didChangeBudgetStatus(GrGpuResource * resource)455     void didChangeBudgetStatus(GrGpuResource* resource) { fCache->didChangeBudgetStatus(resource); }
456 
457     // No taking addresses of this type.
458     const ResourceAccess* operator&() const;
459     ResourceAccess* operator&();
460 
461     GrResourceCache* fCache;
462 
463     friend class GrGpuResource; // To access all the proxy inline methods.
464     friend class GrResourceCache; // To create this type.
465 };
466 
resourceAccess()467 inline GrResourceCache::ResourceAccess GrResourceCache::resourceAccess() {
468     return ResourceAccess(this);
469 }
470 
471 #endif
472