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
241
242 #ifdef SK_DEBUG
243 int countUniqueKeysWithTag(const char* tag) const;
244 #endif
245
246 // This function is for unit testing and is only defined in test tools.
247 void changeTimestamp(uint32_t newTimestamp);
248
249 // Enumerates all cached resources and dumps their details to traceMemoryDump.
250 void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const;
251
setProxyProvider(GrProxyProvider * proxyProvider)252 void setProxyProvider(GrProxyProvider* proxyProvider) { fProxyProvider = proxyProvider; }
setThreadSafeCache(GrThreadSafeCache * threadSafeCache)253 void setThreadSafeCache(GrThreadSafeCache* threadSafeCache) {
254 fThreadSafeCache = threadSafeCache;
255 }
256
257 private:
258 ///////////////////////////////////////////////////////////////////////////
259 /// @name Methods accessible via ResourceAccess
260 ////
261 void insertResource(GrGpuResource*);
262 void removeResource(GrGpuResource*);
263 void notifyARefCntReachedZero(GrGpuResource*, GrGpuResource::LastRemovedRef);
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) const { return fBudgetedBytes+bytes <= fMaxBytes; }
277
278 uint32_t getNextTimestamp();
279
280 void purgeUnlockedResources(const GrStdSteadyClock::time_point* purgeTime,
281 bool scratchResourcesOnly);
282
283 #ifdef SK_DEBUG
284 bool isInCache(const GrGpuResource* r) const;
285 void validate() const;
286 #else
validate()287 void validate() const {}
288 #endif
289
290 class AutoValidate;
291
292 class AvailableForScratchUse;
293
294 struct ScratchMapTraits {
GetKeyScratchMapTraits295 static const GrScratchKey& GetKey(const GrGpuResource& r) {
296 return r.resourcePriv().getScratchKey();
297 }
298
HashScratchMapTraits299 static uint32_t Hash(const GrScratchKey& key) { return key.hash(); }
OnFreeScratchMapTraits300 static void OnFree(GrGpuResource*) { }
301 };
302 typedef SkTMultiMap<GrGpuResource, GrScratchKey, ScratchMapTraits> ScratchMap;
303
304 struct UniqueHashTraits {
GetKeyUniqueHashTraits305 static const GrUniqueKey& GetKey(const GrGpuResource& r) { return r.getUniqueKey(); }
306
HashUniqueHashTraits307 static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); }
308 };
309 typedef SkTDynamicHash<GrGpuResource, GrUniqueKey, UniqueHashTraits> UniqueHash;
310
311 class TextureAwaitingUnref {
312 public:
313 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();
320 void addRef();
321 void unref();
322 bool finished();
323
324 private:
325 GrTexture* fTexture = nullptr;
326 int fNumUnrefs = 0;
327 };
328 using TexturesAwaitingUnref = SkTHashMap<uint32_t, TextureAwaitingUnref>;
329
CompareTimestamp(GrGpuResource * const & a,GrGpuResource * const & b)330 static bool CompareTimestamp(GrGpuResource* const& a, GrGpuResource* const& b) {
331 return a->cacheAccess().timestamp() < b->cacheAccess().timestamp();
332 }
333
AccessResourceIndex(GrGpuResource * const & res)334 static int* AccessResourceIndex(GrGpuResource* const& res) {
335 return res->cacheAccess().accessCacheIndex();
336 }
337
338 using TextureFreedMessageBus = SkMessageBus<GrTextureFreedMessage,
339 GrDirectContext::DirectContextID>;
340
341 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage, uint32_t>::Inbox InvalidUniqueKeyInbox;
342 typedef SkTDPQueue<GrGpuResource*, CompareTimestamp, AccessResourceIndex> PurgeableQueue;
343 typedef SkTDArray<GrGpuResource*> ResourceArray;
344
345 GrProxyProvider* fProxyProvider = nullptr;
346 GrThreadSafeCache* fThreadSafeCache = nullptr;
347
348 // 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.
351 uint32_t fTimestamp = 0;
352 PurgeableQueue fPurgeableQueue;
353 ResourceArray fNonpurgeableResources;
354
355 // This map holds all resources that can be used as scratch resources.
356 ScratchMap fScratchMap;
357 // This holds all resources that have unique keys.
358 UniqueHash fUniqueHash;
359
360 // our budget, used in purgeAsNeeded()
361 size_t fMaxBytes = kDefaultMaxSize;
362
363 #if GR_CACHE_STATS
364 int fHighWaterCount = 0;
365 size_t fHighWaterBytes = 0;
366 int fBudgetedHighWaterCount = 0;
367 size_t fBudgetedHighWaterBytes = 0;
368 #endif
369
370 // our current stats for all resources
371 SkDEBUGCODE(int fCount = 0;)
372 size_t fBytes = 0;
373
374 // our current stats for resources that count against the budget
375 int fBudgetedCount = 0;
376 size_t fBudgetedBytes = 0;
377 size_t fPurgeableBytes = 0;
378 int fNumBudgetedResourcesFlushWillMakePurgeable = 0;
379
380 InvalidUniqueKeyInbox fInvalidUniqueKeyInbox;
381 TextureFreedMessageBus::Inbox fFreedTextureInbox;
382 TexturesAwaitingUnref fTexturesAwaitingUnref;
383
384 GrDirectContext::DirectContextID fOwningContextID;
385 uint32_t fContextUniqueID = SK_InvalidUniqueID;
386 GrSingleOwner* fSingleOwner = nullptr;
387
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.
390 SkDEBUGCODE(GrGpuResource* fNewlyPurgeableResourceForValidation = nullptr;)
391 };
392
393 class GrResourceCache::ResourceAccess {
394 private:
ResourceAccess(GrResourceCache * cache)395 ResourceAccess(GrResourceCache* cache) : fCache(cache) { }
ResourceAccess(const ResourceAccess & that)396 ResourceAccess(const ResourceAccess& that) : fCache(that.fCache) { }
397 ResourceAccess& operator=(const ResourceAccess&) = delete;
398
399 /**
400 * Insert a resource into the cache.
401 */
insertResource(GrGpuResource * resource)402 void insertResource(GrGpuResource* resource) { fCache->insertResource(resource); }
403
404 /**
405 * Removes a resource from the cache.
406 */
removeResource(GrGpuResource * resource)407 void removeResource(GrGpuResource* resource) { fCache->removeResource(resource); }
408
409 /**
410 * Adds a ref to a resource with proper tracking if the resource has 0 refs prior to
411 * adding the ref.
412 */
refResource(GrGpuResource * resource)413 void refResource(GrGpuResource* resource) { fCache->refResource(resource); }
414
415 /**
416 * Notifications that should be sent to the cache when the ref/io cnt status of resources
417 * changes.
418 */
419 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 /**
426 * 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.
428 */
notifyARefCntReachedZero(GrGpuResource * resource,GrGpuResource::LastRemovedRef removedRef)429 void notifyARefCntReachedZero(GrGpuResource* resource,
430 GrGpuResource::LastRemovedRef removedRef) {
431 fCache->notifyARefCntReachedZero(resource, removedRef);
432 }
433
434 /**
435 * Called by GrGpuResources to change their unique keys.
436 */
changeUniqueKey(GrGpuResource * resource,const GrUniqueKey & newKey)437 void changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) {
438 fCache->changeUniqueKey(resource, newKey);
439 }
440
441 /**
442 * Called by a GrGpuResource to remove its unique key.
443 */
removeUniqueKey(GrGpuResource * resource)444 void removeUniqueKey(GrGpuResource* resource) { fCache->removeUniqueKey(resource); }
445
446 /**
447 * Called by a GrGpuResource when it removes its scratch key.
448 */
willRemoveScratchKey(const GrGpuResource * resource)449 void willRemoveScratchKey(const GrGpuResource* resource) {
450 fCache->willRemoveScratchKey(resource);
451 }
452
453 /**
454 * Called by GrGpuResources when they change from budgeted to unbudgeted or vice versa.
455 */
didChangeBudgetStatus(GrGpuResource * resource)456 void didChangeBudgetStatus(GrGpuResource* resource) { fCache->didChangeBudgetStatus(resource); }
457
458 // No taking addresses of this type.
459 const ResourceAccess* operator&() const;
460 ResourceAccess* operator&();
461
462 GrResourceCache* fCache;
463
464 friend class GrGpuResource; // To access all the proxy inline methods.
465 friend class GrResourceCache; // To create this type.
466 };
467
resourceAccess()468 inline GrResourceCache::ResourceAccess GrResourceCache::resourceAccess() {
469 return ResourceAccess(this);
470 }
471
472 #endif
473