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 <stack>
12
13 #include "include/core/SkRefCnt.h"
14 #include "include/gpu/GrGpuResource.h"
15 #include "include/private/GrResourceKey.h"
16 #include "include/private/SkTArray.h"
17 #include "include/private/SkTHash.h"
18 #include "src/core/SkMessageBus.h"
19 #include "src/core/SkTDPQueue.h"
20 #include "src/core/SkTInternalLList.h"
21 #include "src/core/SkTMultiMap.h"
22 #include "src/gpu/GrGpuResourceCacheAccess.h"
23 #include "src/gpu/GrGpuResourcePriv.h"
24
25 class GrCaps;
26 class GrProxyProvider;
27 class SkString;
28 class SkTraceMemoryDump;
29 class GrSingleOwner;
30
31 struct GrGpuResourceFreedMessage {
32 GrGpuResource* fResource;
33 uint32_t fOwningUniqueID;
34 };
35
SkShouldPostMessageToBus(const GrGpuResourceFreedMessage & msg,uint32_t msgBusUniqueID)36 static inline bool SkShouldPostMessageToBus(
37 const GrGpuResourceFreedMessage& msg, uint32_t msgBusUniqueID) {
38 // The inbox's ID is the unique ID of the owning GrContext.
39 return msgBusUniqueID == msg.fOwningUniqueID;
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(const GrCaps*, GrSingleOwner* owner, uint32_t contextUniqueID);
62 ~GrResourceCache();
63
64 // Default maximum number of budgeted resources in the cache.
65 static const int kDefaultMaxCount = 2 * (1 << 12);
66 // Default maximum number of bytes of gpu memory of budgeted resources in the cache.
67 static const size_t kDefaultMaxSize = 96 * (1 << 20);
68
69 /** Used to access functionality needed by GrGpuResource for lifetime management. */
70 class ResourceAccess;
71 ResourceAccess resourceAccess();
72
73 /**
74 * Get current resource tag for gpu cache recycle.
75 */
76 GrGpuResourceTag getCurrentGrResourceTag() const;
77
78 /**
79 * Set current resource tag for gpu cache recycle.
80 */
81 void setCurrentGrResourceTag(const GrGpuResourceTag tag);
82
83 /** Unique ID of the owning GrContext. */
contextUniqueID()84 uint32_t contextUniqueID() const { return fContextUniqueID; }
85
86 /** Sets the cache limits in terms of number of resources and max gpu memory byte size. */
87 void setLimits(int count, size_t bytes);
88
89 /**
90 * Returns the number of resources.
91 */
getResourceCount()92 int getResourceCount() const {
93 return fPurgeableQueue.count() + fNonpurgeableResources.count();
94 }
95
96 /**
97 * Returns the number of resources that count against the budget.
98 */
getBudgetedResourceCount()99 int getBudgetedResourceCount() const { return fBudgetedCount; }
100
101 /**
102 * Returns the number of bytes consumed by resources.
103 */
getResourceBytes()104 size_t getResourceBytes() const { return fBytes; }
105
106 /**
107 * Returns the number of bytes held by unlocked reosources which are available for purging.
108 */
getPurgeableBytes()109 size_t getPurgeableBytes() const { return fPurgeableBytes; }
110
111 /**
112 * Returns the number of bytes consumed by budgeted resources.
113 */
getBudgetedResourceBytes()114 size_t getBudgetedResourceBytes() const { return fBudgetedBytes; }
115
116 /**
117 * Returns the cached resources count budget.
118 */
getMaxResourceCount()119 int getMaxResourceCount() const { return fMaxCount; }
120
121 /**
122 * Returns the number of bytes consumed by cached resources.
123 */
getMaxResourceBytes()124 size_t getMaxResourceBytes() const { return fMaxBytes; }
125
126 /**
127 * Abandons the backend API resources owned by all GrGpuResource objects and removes them from
128 * the cache.
129 */
130 void abandonAll();
131
132 /**
133 * Releases the backend API resources owned by all GrGpuResource objects and removes them from
134 * the cache.
135 */
136 void releaseAll();
137
138 /**
139 * Releases GrGpuResource objects and removes them from the cache by tag.
140 */
141 void releaseByTag(const GrGpuResourceTag tag);
142
143 enum class ScratchFlags {
144 kNone = 0,
145 /** Preferentially returns scratch resources with no pending IO. */
146 kPreferNoPendingIO = 0x1,
147 /** Will not return any resources that match but have pending IO. */
148 kRequireNoPendingIO = 0x2,
149 };
150
151 /**
152 * Find a resource that matches a scratch key.
153 */
154 GrGpuResource* findAndRefScratchResource(const GrScratchKey& scratchKey, size_t resourceSize,
155 ScratchFlags);
156
157 #ifdef SK_DEBUG
158 // This is not particularly fast and only used for validation, so debug only.
countScratchEntriesForKey(const GrScratchKey & scratchKey)159 int countScratchEntriesForKey(const GrScratchKey& scratchKey) const {
160 return fScratchMap.countForKey(scratchKey);
161 }
162 #endif
163
164 /**
165 * Find a resource that matches a unique key.
166 */
findAndRefUniqueResource(const GrUniqueKey & key)167 GrGpuResource* findAndRefUniqueResource(const GrUniqueKey& key) {
168 GrGpuResource* resource = fUniqueHash.find(key);
169 if (resource) {
170 this->refAndMakeResourceMRU(resource);
171 }
172 return resource;
173 }
174
175 /**
176 * Query whether a unique key exists in the cache.
177 */
hasUniqueKey(const GrUniqueKey & key)178 bool hasUniqueKey(const GrUniqueKey& key) const {
179 return SkToBool(fUniqueHash.find(key));
180 }
181
182 /** Purges resources to become under budget and processes resources with invalidated unique
183 keys. */
184 void purgeAsNeeded();
185
186 /** Purges all resources that don't have external owners. */
purgeAllUnlocked()187 void purgeAllUnlocked() { this->purgeUnlockedResources(false); }
188
189 // Purge unlocked resources. If 'scratchResourcesOnly' is true the purgeable resources
190 // containing persistent data are spared. If it is false then all purgeable resources will
191 // be deleted.
192 void purgeUnlockedResources(bool scratchResourcesOnly);
193 void purgeUnlockedResourcesByTag(bool scratchResourcesOnly, const GrGpuResourceTag tag);
194
195 /** Purge all resources not used since the passed in time. */
196 void purgeResourcesNotUsedSince(GrStdSteadyClock::time_point);
197
overBudget()198 bool overBudget() const { return fBudgetedBytes > fMaxBytes || fBudgetedCount > fMaxCount; }
199
200 /**
201 * Purge unlocked resources from the cache until the the provided byte count has been reached
202 * or we have purged all unlocked resources. The default policy is to purge in LRU order, but
203 * can be overridden to prefer purging scratch resources (in LRU order) prior to purging other
204 * resource types.
205 *
206 * @param maxBytesToPurge the desired number of bytes to be purged.
207 * @param preferScratchResources If true scratch resources will be purged prior to other
208 * resource types.
209 */
210 void purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources);
211
212 /** Returns true if the cache would like a flush to occur in order to make more resources
213 purgeable. */
214 bool requestsFlush() const;
215
216 /** Maintain a ref to this resource until we receive a GrGpuResourceFreedMessage. */
217 void insertDelayedResourceUnref(GrGpuResource* resource);
218
219 #if GR_CACHE_STATS
220 struct Stats {
221 int fTotal;
222 int fNumPurgeable;
223 int fNumNonPurgeable;
224
225 int fScratch;
226 int fWrapped;
227 size_t fUnbudgetedSize;
228
StatsStats229 Stats() { this->reset(); }
230
resetStats231 void reset() {
232 fTotal = 0;
233 fNumPurgeable = 0;
234 fNumNonPurgeable = 0;
235 fScratch = 0;
236 fWrapped = 0;
237 fUnbudgetedSize = 0;
238 }
239
updateStats240 void update(GrGpuResource* resource) {
241 if (resource->cacheAccess().isScratch()) {
242 ++fScratch;
243 }
244 if (resource->resourcePriv().refsWrappedObjects()) {
245 ++fWrapped;
246 }
247 if (GrBudgetedType::kBudgeted != resource->resourcePriv().budgetedType()) {
248 fUnbudgetedSize += resource->gpuMemorySize();
249 }
250 }
251 };
252
253 void getStats(Stats*) const;
254
255 #if GR_TEST_UTILS
256 void dumpStats(SkString*) const;
257
258 void dumpStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* value) const;
259 #endif
260
261 #endif
262
263 #ifdef SK_DEBUG
264 int countUniqueKeysWithTag(const char* tag) const;
265 #endif
266
267 // This function is for unit testing and is only defined in test tools.
268 void changeTimestamp(uint32_t newTimestamp);
269
270 // Enumerates all cached resources and dumps their details to traceMemoryDump.
271 void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const;
272 void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump, GrGpuResourceTag tag) const;
273
setProxyProvider(GrProxyProvider * proxyProvider)274 void setProxyProvider(GrProxyProvider* proxyProvider) { fProxyProvider = proxyProvider; }
275
276 private:
277 ///////////////////////////////////////////////////////////////////////////
278 /// @name Methods accessible via ResourceAccess
279 ////
280 void insertResource(GrGpuResource*);
281 void removeResource(GrGpuResource*);
282 void notifyCntReachedZero(GrGpuResource*, uint32_t flags);
283 void changeUniqueKey(GrGpuResource*, const GrUniqueKey&);
284 void removeUniqueKey(GrGpuResource*);
285 void willRemoveScratchKey(const GrGpuResource*);
286 void didChangeBudgetStatus(GrGpuResource*);
287 void refResource(GrGpuResource* resource);
288 /// @}
289
290 void refAndMakeResourceMRU(GrGpuResource*);
291 void processFreedGpuResources();
292 void addToNonpurgeableArray(GrGpuResource*);
293 void removeFromNonpurgeableArray(GrGpuResource*);
294
wouldFit(size_t bytes)295 bool wouldFit(size_t bytes) {
296 return fBudgetedBytes+bytes <= fMaxBytes && fBudgetedCount+1 <= fMaxCount;
297 }
298
299 uint32_t getNextTimestamp();
300
301 #ifdef SK_DEBUG
302 bool isInCache(const GrGpuResource* r) const;
303 void validate() const;
304 #else
validate()305 void validate() const {}
306 #endif
307
308 class AutoValidate;
309
310 class AvailableForScratchUse;
311
312 struct ScratchMapTraits {
GetKeyScratchMapTraits313 static const GrScratchKey& GetKey(const GrGpuResource& r) {
314 return r.resourcePriv().getScratchKey();
315 }
316
HashScratchMapTraits317 static uint32_t Hash(const GrScratchKey& key) { return key.hash(); }
OnFreeScratchMapTraits318 static void OnFree(GrGpuResource*) { }
319 };
320 typedef SkTMultiMap<GrGpuResource, GrScratchKey, ScratchMapTraits> ScratchMap;
321
322 struct UniqueHashTraits {
GetKeyUniqueHashTraits323 static const GrUniqueKey& GetKey(const GrGpuResource& r) { return r.getUniqueKey(); }
324
HashUniqueHashTraits325 static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); }
326 };
327 typedef SkTDynamicHash<GrGpuResource, GrUniqueKey, UniqueHashTraits> UniqueHash;
328
329 class ResourceAwaitingUnref {
330 public:
331 ResourceAwaitingUnref();
332 ResourceAwaitingUnref(GrGpuResource* resource);
333 ResourceAwaitingUnref(const ResourceAwaitingUnref&) = delete;
334 ResourceAwaitingUnref& operator=(const ResourceAwaitingUnref&) = delete;
335 ResourceAwaitingUnref(ResourceAwaitingUnref&&);
336 ResourceAwaitingUnref& operator=(ResourceAwaitingUnref&&);
337 ~ResourceAwaitingUnref();
338 void addRef();
339 void unref();
340 bool finished();
341
342 private:
343 GrGpuResource* fResource = nullptr;
344 int fNumUnrefs = 0;
345 };
346 using ReourcesAwaitingUnref = SkTHashMap<uint32_t, ResourceAwaitingUnref>;
347
CompareTimestamp(GrGpuResource * const & a,GrGpuResource * const & b)348 static bool CompareTimestamp(GrGpuResource* const& a, GrGpuResource* const& b) {
349 return a->cacheAccess().timestamp() < b->cacheAccess().timestamp();
350 }
351
AccessResourceIndex(GrGpuResource * const & res)352 static int* AccessResourceIndex(GrGpuResource* const& res) {
353 return res->cacheAccess().accessCacheIndex();
354 }
355
356 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage>::Inbox InvalidUniqueKeyInbox;
357 typedef SkMessageBus<GrGpuResourceFreedMessage>::Inbox FreedGpuResourceInbox;
358 typedef SkTDPQueue<GrGpuResource*, CompareTimestamp, AccessResourceIndex> PurgeableQueue;
359 typedef SkTDArray<GrGpuResource*> ResourceArray;
360
361 GrProxyProvider* fProxyProvider = nullptr;
362 // Whenever a resource is added to the cache or the result of a cache lookup, fTimestamp is
363 // assigned as the resource's timestamp and then incremented. fPurgeableQueue orders the
364 // purgeable resources by this value, and thus is used to purge resources in LRU order.
365 uint32_t fTimestamp = 0;
366 PurgeableQueue fPurgeableQueue;
367 ResourceArray fNonpurgeableResources;
368
369 // This map holds all resources that can be used as scratch resources.
370 ScratchMap fScratchMap;
371 // This holds all resources that have unique keys.
372 UniqueHash fUniqueHash;
373
374 // our budget, used in purgeAsNeeded()
375 int fMaxCount = kDefaultMaxCount;
376 size_t fMaxBytes = kDefaultMaxSize;
377
378 #if GR_CACHE_STATS
379 int fHighWaterCount = 0;
380 size_t fHighWaterBytes = 0;
381 int fBudgetedHighWaterCount = 0;
382 size_t fBudgetedHighWaterBytes = 0;
383 #endif
384
385 // our current stats for all resources
386 SkDEBUGCODE(int fCount = 0;)
387 size_t fBytes = 0;
388
389 // our current stats for resources that count against the budget
390 int fBudgetedCount = 0;
391 size_t fBudgetedBytes = 0;
392 size_t fPurgeableBytes = 0;
393 int fNumBudgetedResourcesFlushWillMakePurgeable = 0;
394
395 InvalidUniqueKeyInbox fInvalidUniqueKeyInbox;
396 FreedGpuResourceInbox fFreedGpuResourceInbox;
397 ReourcesAwaitingUnref fResourcesAwaitingUnref;
398
399 uint32_t fContextUniqueID = SK_InvalidUniqueID;
400 GrSingleOwner* fSingleOwner = nullptr;
401
402 // This resource is allowed to be in the nonpurgeable array for the sake of validate() because
403 // we're in the midst of converting it to purgeable status.
404 SkDEBUGCODE(GrGpuResource* fNewlyPurgeableResourceForValidation = nullptr;)
405
406 bool fPreferVRAMUseOverFlushes = false;
407
408 // Indicates the cached resource tags.
409 std::stack<GrGpuResourceTag> grResourceTagCacheStack;
410 };
411
412 GR_MAKE_BITFIELD_CLASS_OPS(GrResourceCache::ScratchFlags);
413
414 class GrResourceCache::ResourceAccess {
415 private:
ResourceAccess(GrResourceCache * cache)416 ResourceAccess(GrResourceCache* cache) : fCache(cache) { }
ResourceAccess(const ResourceAccess & that)417 ResourceAccess(const ResourceAccess& that) : fCache(that.fCache) { }
418 ResourceAccess& operator=(const ResourceAccess&); // unimpl
419
420 /**
421 * Insert a resource into the cache.
422 */
insertResource(GrGpuResource * resource)423 void insertResource(GrGpuResource* resource) { fCache->insertResource(resource); }
424
425 /**
426 * Removes a resource from the cache.
427 */
removeResource(GrGpuResource * resource)428 void removeResource(GrGpuResource* resource) { fCache->removeResource(resource); }
429
430 /**
431 * Adds a ref to a resource with proper tracking if the resource has 0 refs prior to
432 * adding the ref.
433 */
refResource(GrGpuResource * resource)434 void refResource(GrGpuResource* resource) { fCache->refResource(resource); }
435
436 /**
437 * Get current resource tag for gpu cache recycle.
438 */
getCurrentGrResourceTag()439 GrGpuResourceTag getCurrentGrResourceTag() const { return fCache->getCurrentGrResourceTag(); }
440
441 /**
442 * Notifications that should be sent to the cache when the ref/io cnt status of resources
443 * changes.
444 */
445 enum RefNotificationFlags {
446 /** All types of refs on the resource have reached zero. */
447 kAllCntsReachedZero_RefNotificationFlag = 0x1,
448 /** The normal (not pending IO type) ref cnt has reached zero. */
449 kRefCntReachedZero_RefNotificationFlag = 0x2,
450 };
451 /**
452 * Called by GrGpuResources when they detect that their ref/io cnts have reached zero. When the
453 * normal ref cnt reaches zero the flags that are set should be:
454 * a) kRefCntReachedZero if a pending IO cnt is still non-zero.
455 * b) (kRefCntReachedZero | kAllCntsReachedZero) when all pending IO cnts are also zero.
456 * kAllCntsReachedZero is set by itself if a pending IO cnt is decremented to zero and all the
457 * the other cnts are already zero.
458 */
notifyCntReachedZero(GrGpuResource * resource,uint32_t flags)459 void notifyCntReachedZero(GrGpuResource* resource, uint32_t flags) {
460 fCache->notifyCntReachedZero(resource, flags);
461 }
462
463 /**
464 * Called by GrGpuResources to change their unique keys.
465 */
changeUniqueKey(GrGpuResource * resource,const GrUniqueKey & newKey)466 void changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) {
467 fCache->changeUniqueKey(resource, newKey);
468 }
469
470 /**
471 * Called by a GrGpuResource to remove its unique key.
472 */
removeUniqueKey(GrGpuResource * resource)473 void removeUniqueKey(GrGpuResource* resource) { fCache->removeUniqueKey(resource); }
474
475 /**
476 * Called by a GrGpuResource when it removes its scratch key.
477 */
willRemoveScratchKey(const GrGpuResource * resource)478 void willRemoveScratchKey(const GrGpuResource* resource) {
479 fCache->willRemoveScratchKey(resource);
480 }
481
482 /**
483 * Called by GrGpuResources when they change from budgeted to unbudgeted or vice versa.
484 */
didChangeBudgetStatus(GrGpuResource * resource)485 void didChangeBudgetStatus(GrGpuResource* resource) { fCache->didChangeBudgetStatus(resource); }
486
487 // No taking addresses of this type.
488 const ResourceAccess* operator&() const;
489 ResourceAccess* operator&();
490
491 GrResourceCache* fCache;
492
493 friend class GrGpuResource; // To access all the proxy inline methods.
494 friend class GrResourceCache; // To create this type.
495 };
496
resourceAccess()497 inline GrResourceCache::ResourceAccess GrResourceCache::resourceAccess() {
498 return ResourceAccess(this);
499 }
500
501 #endif
502