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/GrGpuResource.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/GrGpuResourceCacheAccess.h"
21 #include "src/gpu/GrGpuResourcePriv.h"
22
23 class GrCaps;
24 class GrProxyProvider;
25 class SkString;
26 class SkTraceMemoryDump;
27 class GrSingleOwner;
28
29 struct GrGpuResourceFreedMessage {
30 GrGpuResource* fResource;
31 uint32_t fOwningUniqueID;
32 };
33
SkShouldPostMessageToBus(const GrGpuResourceFreedMessage & msg,uint32_t msgBusUniqueID)34 static inline bool SkShouldPostMessageToBus(
35 const GrGpuResourceFreedMessage& msg, uint32_t msgBusUniqueID) {
36 // The inbox's ID is the unique ID of the owning GrContext.
37 return msgBusUniqueID == msg.fOwningUniqueID;
38 }
39
40 /**
41 * Manages the lifetime of all GrGpuResource instances.
42 *
43 * Resources may have optionally have two types of keys:
44 * 1) A scratch key. This is for resources whose allocations are cached but not their contents.
45 * Multiple resources can share the same scratch key. This is so a caller can have two
46 * resource instances with the same properties (e.g. multipass rendering that ping-pongs
47 * between two temporary surfaces). The scratch key is set at resource creation time and
48 * should never change. Resources need not have a scratch key.
49 * 2) A unique key. This key's meaning is specific to the domain that created the key. Only one
50 * resource may have a given unique key. The unique key can be set, cleared, or changed
51 * anytime after resource creation.
52 *
53 * A unique key always takes precedence over a scratch key when a resource has both types of keys.
54 * If a resource has neither key type then it will be deleted as soon as the last reference to it
55 * is dropped.
56 */
57 class GrResourceCache {
58 public:
59 GrResourceCache(const GrCaps*, GrSingleOwner* owner, uint32_t contextUniqueID);
60 ~GrResourceCache();
61
62 // Default maximum number of budgeted resources in the cache.
63 static const int kDefaultMaxCount = 2 * (1 << 12);
64 // Default maximum number of bytes of gpu memory of budgeted resources in the cache.
65 static const size_t kDefaultMaxSize = 96 * (1 << 20);
66
67 /** Used to access functionality needed by GrGpuResource for lifetime management. */
68 class ResourceAccess;
69 ResourceAccess resourceAccess();
70
71 /** Unique ID of the owning GrContext. */
contextUniqueID()72 uint32_t contextUniqueID() const { return fContextUniqueID; }
73
74 /** Sets the cache limits in terms of number of resources and max gpu memory byte size. */
75 void setLimits(int count, size_t bytes);
76
77 /**
78 * Returns the number of resources.
79 */
getResourceCount()80 int getResourceCount() const {
81 return fPurgeableQueue.count() + fNonpurgeableResources.count();
82 }
83
84 /**
85 * Returns the number of resources that count against the budget.
86 */
getBudgetedResourceCount()87 int getBudgetedResourceCount() const { return fBudgetedCount; }
88
89 /**
90 * Returns the number of bytes consumed by resources.
91 */
getResourceBytes()92 size_t getResourceBytes() const { return fBytes; }
93
94 /**
95 * Returns the number of bytes held by unlocked reosources which are available for purging.
96 */
getPurgeableBytes()97 size_t getPurgeableBytes() const { return fPurgeableBytes; }
98
99 /**
100 * Returns the number of bytes consumed by budgeted resources.
101 */
getBudgetedResourceBytes()102 size_t getBudgetedResourceBytes() const { return fBudgetedBytes; }
103
104 /**
105 * Returns the cached resources count budget.
106 */
getMaxResourceCount()107 int getMaxResourceCount() const { return fMaxCount; }
108
109 /**
110 * Returns the number of bytes consumed by cached resources.
111 */
getMaxResourceBytes()112 size_t getMaxResourceBytes() const { return fMaxBytes; }
113
114 /**
115 * Abandons the backend API resources owned by all GrGpuResource objects and removes them from
116 * the cache.
117 */
118 void abandonAll();
119
120 /**
121 * Releases the backend API resources owned by all GrGpuResource objects and removes them from
122 * the cache.
123 */
124 void releaseAll();
125
126 enum class ScratchFlags {
127 kNone = 0,
128 /** Preferentially returns scratch resources with no pending IO. */
129 kPreferNoPendingIO = 0x1,
130 /** Will not return any resources that match but have pending IO. */
131 kRequireNoPendingIO = 0x2,
132 };
133
134 /**
135 * Find a resource that matches a scratch key.
136 */
137 GrGpuResource* findAndRefScratchResource(const GrScratchKey& scratchKey, size_t resourceSize,
138 ScratchFlags);
139
140 #ifdef SK_DEBUG
141 // This is not particularly fast and only used for validation, so debug only.
countScratchEntriesForKey(const GrScratchKey & scratchKey)142 int countScratchEntriesForKey(const GrScratchKey& scratchKey) const {
143 return fScratchMap.countForKey(scratchKey);
144 }
145 #endif
146
147 /**
148 * Find a resource that matches a unique key.
149 */
findAndRefUniqueResource(const GrUniqueKey & key)150 GrGpuResource* findAndRefUniqueResource(const GrUniqueKey& key) {
151 GrGpuResource* resource = fUniqueHash.find(key);
152 if (resource) {
153 this->refAndMakeResourceMRU(resource);
154 }
155 return resource;
156 }
157
158 /**
159 * Query whether a unique key exists in the cache.
160 */
hasUniqueKey(const GrUniqueKey & key)161 bool hasUniqueKey(const GrUniqueKey& key) const {
162 return SkToBool(fUniqueHash.find(key));
163 }
164
165 /** Purges resources to become under budget and processes resources with invalidated unique
166 keys. */
167 void purgeAsNeeded();
168
169 /** Purges all resources that don't have external owners. */
purgeAllUnlocked()170 void purgeAllUnlocked() { this->purgeUnlockedResources(false); }
171
172 // Purge unlocked resources. If 'scratchResourcesOnly' is true the purgeable resources
173 // containing persistent data are spared. If it is false then all purgeable resources will
174 // be deleted.
175 void purgeUnlockedResources(bool scratchResourcesOnly);
176
177 /** Purge all resources not used since the passed in time. */
178 void purgeResourcesNotUsedSince(GrStdSteadyClock::time_point);
179
overBudget()180 bool overBudget() const { return fBudgetedBytes > fMaxBytes || fBudgetedCount > fMaxCount; }
181
182 /**
183 * Purge unlocked resources from the cache until the the provided byte count has been reached
184 * or we have purged all unlocked resources. The default policy is to purge in LRU order, but
185 * can be overridden to prefer purging scratch resources (in LRU order) prior to purging other
186 * resource types.
187 *
188 * @param maxBytesToPurge the desired number of bytes to be purged.
189 * @param preferScratchResources If true scratch resources will be purged prior to other
190 * resource types.
191 */
192 void purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources);
193
194 /** Returns true if the cache would like a flush to occur in order to make more resources
195 purgeable. */
196 bool requestsFlush() const;
197
198 /** Maintain a ref to this resource until we receive a GrGpuResourceFreedMessage. */
199 void insertDelayedResourceUnref(GrGpuResource* resource);
200
201 #if GR_CACHE_STATS
202 struct Stats {
203 int fTotal;
204 int fNumPurgeable;
205 int fNumNonPurgeable;
206
207 int fScratch;
208 int fWrapped;
209 size_t fUnbudgetedSize;
210
StatsStats211 Stats() { this->reset(); }
212
resetStats213 void reset() {
214 fTotal = 0;
215 fNumPurgeable = 0;
216 fNumNonPurgeable = 0;
217 fScratch = 0;
218 fWrapped = 0;
219 fUnbudgetedSize = 0;
220 }
221
updateStats222 void update(GrGpuResource* resource) {
223 if (resource->cacheAccess().isScratch()) {
224 ++fScratch;
225 }
226 if (resource->resourcePriv().refsWrappedObjects()) {
227 ++fWrapped;
228 }
229 if (GrBudgetedType::kBudgeted != resource->resourcePriv().budgetedType()) {
230 fUnbudgetedSize += resource->gpuMemorySize();
231 }
232 }
233 };
234
235 void getStats(Stats*) const;
236
237 #if GR_TEST_UTILS
238 void dumpStats(SkString*) const;
239
240 void dumpStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* value) const;
241 #endif
242
243 #endif
244
245 #ifdef SK_DEBUG
246 int countUniqueKeysWithTag(const char* tag) const;
247 #endif
248
249 // This function is for unit testing and is only defined in test tools.
250 void changeTimestamp(uint32_t newTimestamp);
251
252 // Enumerates all cached resources and dumps their details to traceMemoryDump.
253 void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const;
254
setProxyProvider(GrProxyProvider * proxyProvider)255 void setProxyProvider(GrProxyProvider* proxyProvider) { fProxyProvider = proxyProvider; }
256
257 private:
258 ///////////////////////////////////////////////////////////////////////////
259 /// @name Methods accessible via ResourceAccess
260 ////
261 void insertResource(GrGpuResource*);
262 void removeResource(GrGpuResource*);
263 void notifyCntReachedZero(GrGpuResource*, uint32_t flags);
264 void changeUniqueKey(GrGpuResource*, const GrUniqueKey&);
265 void removeUniqueKey(GrGpuResource*);
266 void willRemoveScratchKey(const GrGpuResource*);
267 void didChangeBudgetStatus(GrGpuResource*);
268 void refResource(GrGpuResource* resource);
269 /// @}
270
271 void refAndMakeResourceMRU(GrGpuResource*);
272 void processFreedGpuResources();
273 void addToNonpurgeableArray(GrGpuResource*);
274 void removeFromNonpurgeableArray(GrGpuResource*);
275
wouldFit(size_t bytes)276 bool wouldFit(size_t bytes) {
277 return fBudgetedBytes+bytes <= fMaxBytes && fBudgetedCount+1 <= fMaxCount;
278 }
279
280 uint32_t getNextTimestamp();
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 ResourceAwaitingUnref {
311 public:
312 ResourceAwaitingUnref();
313 ResourceAwaitingUnref(GrGpuResource* resource);
314 ResourceAwaitingUnref(const ResourceAwaitingUnref&) = delete;
315 ResourceAwaitingUnref& operator=(const ResourceAwaitingUnref&) = delete;
316 ResourceAwaitingUnref(ResourceAwaitingUnref&&);
317 ResourceAwaitingUnref& operator=(ResourceAwaitingUnref&&);
318 ~ResourceAwaitingUnref();
319 void addRef();
320 void unref();
321 bool finished();
322
323 private:
324 GrGpuResource* fResource = nullptr;
325 int fNumUnrefs = 0;
326 };
327 using ReourcesAwaitingUnref = SkTHashMap<uint32_t, ResourceAwaitingUnref>;
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 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage>::Inbox InvalidUniqueKeyInbox;
338 typedef SkMessageBus<GrGpuResourceFreedMessage>::Inbox FreedGpuResourceInbox;
339 typedef SkTDPQueue<GrGpuResource*, CompareTimestamp, AccessResourceIndex> PurgeableQueue;
340 typedef SkTDArray<GrGpuResource*> ResourceArray;
341
342 GrProxyProvider* fProxyProvider = nullptr;
343 // Whenever a resource is added to the cache or the result of a cache lookup, fTimestamp is
344 // assigned as the resource's timestamp and then incremented. fPurgeableQueue orders the
345 // purgeable resources by this value, and thus is used to purge resources in LRU order.
346 uint32_t fTimestamp = 0;
347 PurgeableQueue fPurgeableQueue;
348 ResourceArray fNonpurgeableResources;
349
350 // This map holds all resources that can be used as scratch resources.
351 ScratchMap fScratchMap;
352 // This holds all resources that have unique keys.
353 UniqueHash fUniqueHash;
354
355 // our budget, used in purgeAsNeeded()
356 int fMaxCount = kDefaultMaxCount;
357 size_t fMaxBytes = kDefaultMaxSize;
358
359 #if GR_CACHE_STATS
360 int fHighWaterCount = 0;
361 size_t fHighWaterBytes = 0;
362 int fBudgetedHighWaterCount = 0;
363 size_t fBudgetedHighWaterBytes = 0;
364 #endif
365
366 // our current stats for all resources
367 SkDEBUGCODE(int fCount = 0;)
368 size_t fBytes = 0;
369
370 // our current stats for resources that count against the budget
371 int fBudgetedCount = 0;
372 size_t fBudgetedBytes = 0;
373 size_t fPurgeableBytes = 0;
374 int fNumBudgetedResourcesFlushWillMakePurgeable = 0;
375
376 InvalidUniqueKeyInbox fInvalidUniqueKeyInbox;
377 FreedGpuResourceInbox fFreedGpuResourceInbox;
378 ReourcesAwaitingUnref fResourcesAwaitingUnref;
379
380 uint32_t fContextUniqueID = SK_InvalidUniqueID;
381 GrSingleOwner* fSingleOwner = nullptr;
382
383 // This resource is allowed to be in the nonpurgeable array for the sake of validate() because
384 // we're in the midst of converting it to purgeable status.
385 SkDEBUGCODE(GrGpuResource* fNewlyPurgeableResourceForValidation = nullptr;)
386
387 bool fPreferVRAMUseOverFlushes = false;
388 };
389
390 GR_MAKE_BITFIELD_CLASS_OPS(GrResourceCache::ScratchFlags);
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&); // unimpl
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 that their ref/io cnts have reached zero. When the
426 * normal ref cnt reaches zero the flags that are set should be:
427 * a) kRefCntReachedZero if a pending IO cnt is still non-zero.
428 * b) (kRefCntReachedZero | kAllCntsReachedZero) when all pending IO cnts are also zero.
429 * kAllCntsReachedZero is set by itself if a pending IO cnt is decremented to zero and all the
430 * the other cnts are already zero.
431 */
notifyCntReachedZero(GrGpuResource * resource,uint32_t flags)432 void notifyCntReachedZero(GrGpuResource* resource, uint32_t flags) {
433 fCache->notifyCntReachedZero(resource, flags);
434 }
435
436 /**
437 * Called by GrGpuResources to change their unique keys.
438 */
changeUniqueKey(GrGpuResource * resource,const GrUniqueKey & newKey)439 void changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) {
440 fCache->changeUniqueKey(resource, newKey);
441 }
442
443 /**
444 * Called by a GrGpuResource to remove its unique key.
445 */
removeUniqueKey(GrGpuResource * resource)446 void removeUniqueKey(GrGpuResource* resource) { fCache->removeUniqueKey(resource); }
447
448 /**
449 * Called by a GrGpuResource when it removes its scratch key.
450 */
willRemoveScratchKey(const GrGpuResource * resource)451 void willRemoveScratchKey(const GrGpuResource* resource) {
452 fCache->willRemoveScratchKey(resource);
453 }
454
455 /**
456 * Called by GrGpuResources when they change from budgeted to unbudgeted or vice versa.
457 */
didChangeBudgetStatus(GrGpuResource * resource)458 void didChangeBudgetStatus(GrGpuResource* resource) { fCache->didChangeBudgetStatus(resource); }
459
460 // No taking addresses of this type.
461 const ResourceAccess* operator&() const;
462 ResourceAccess* operator&();
463
464 GrResourceCache* fCache;
465
466 friend class GrGpuResource; // To access all the proxy inline methods.
467 friend class GrResourceCache; // To create this type.
468 };
469
resourceAccess()470 inline GrResourceCache::ResourceAccess GrResourceCache::resourceAccess() {
471 return ResourceAccess(this);
472 }
473
474 #endif
475