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 #include "src/gpu/GrResourceCache.h"
9 #include <atomic>
10 #include "include/gpu/GrContext.h"
11 #include "include/gpu/GrTexture.h"
12 #include "include/private/GrSingleOwner.h"
13 #include "include/private/SkTo.h"
14 #include "include/utils/SkRandom.h"
15 #include "src/core/SkExchange.h"
16 #include "src/core/SkMessageBus.h"
17 #include "src/core/SkOpts.h"
18 #include "src/core/SkScopeExit.h"
19 #include "src/core/SkTSort.h"
20 #include "src/gpu/GrCaps.h"
21 #include "src/gpu/GrContextPriv.h"
22 #include "src/gpu/GrGpuResourceCacheAccess.h"
23 #include "src/gpu/GrProxyProvider.h"
24 #include "src/gpu/GrTextureProxyCacheAccess.h"
25 #include "src/gpu/GrTracing.h"
26 #include "src/gpu/SkGr.h"
27 
28 DECLARE_SKMESSAGEBUS_MESSAGE(GrUniqueKeyInvalidatedMessage);
29 
30 DECLARE_SKMESSAGEBUS_MESSAGE(GrTextureFreedMessage);
31 
32 #define ASSERT_SINGLE_OWNER \
33     SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);)
34 
35 //////////////////////////////////////////////////////////////////////////////
36 
GenerateResourceType()37 GrScratchKey::ResourceType GrScratchKey::GenerateResourceType() {
38     static std::atomic<int32_t> nextType{INHERITED::kInvalidDomain + 1};
39 
40     int32_t type = nextType++;
41     if (type > SkTo<int32_t>(UINT16_MAX)) {
42         SK_ABORT("Too many Resource Types");
43     }
44 
45     return static_cast<ResourceType>(type);
46 }
47 
GenerateDomain()48 GrUniqueKey::Domain GrUniqueKey::GenerateDomain() {
49     static std::atomic<int32_t> nextDomain{INHERITED::kInvalidDomain + 1};
50 
51     int32_t domain = nextDomain++;
52     if (domain > SkTo<int32_t>(UINT16_MAX)) {
53         SK_ABORT("Too many GrUniqueKey Domains");
54     }
55 
56     return static_cast<Domain>(domain);
57 }
58 
GrResourceKeyHash(const uint32_t * data,size_t size)59 uint32_t GrResourceKeyHash(const uint32_t* data, size_t size) {
60     return SkOpts::hash(data, size);
61 }
62 
63 //////////////////////////////////////////////////////////////////////////////
64 
65 class GrResourceCache::AutoValidate : ::SkNoncopyable {
66 public:
AutoValidate(GrResourceCache * cache)67     AutoValidate(GrResourceCache* cache) : fCache(cache) { cache->validate(); }
~AutoValidate()68     ~AutoValidate() { fCache->validate(); }
69 private:
70     GrResourceCache* fCache;
71 };
72 
73 //////////////////////////////////////////////////////////////////////////////
74 
75 inline GrResourceCache::TextureAwaitingUnref::TextureAwaitingUnref() = default;
76 
TextureAwaitingUnref(GrTexture * texture)77 inline GrResourceCache::TextureAwaitingUnref::TextureAwaitingUnref(GrTexture* texture)
78         : fTexture(texture), fNumUnrefs(1) {}
79 
TextureAwaitingUnref(TextureAwaitingUnref && that)80 inline GrResourceCache::TextureAwaitingUnref::TextureAwaitingUnref(TextureAwaitingUnref&& that) {
81     fTexture = skstd::exchange(that.fTexture, nullptr);
82     fNumUnrefs = skstd::exchange(that.fNumUnrefs, 0);
83 }
84 
operator =(TextureAwaitingUnref && that)85 inline GrResourceCache::TextureAwaitingUnref& GrResourceCache::TextureAwaitingUnref::operator=(
86         TextureAwaitingUnref&& that) {
87     fTexture = skstd::exchange(that.fTexture, nullptr);
88     fNumUnrefs = skstd::exchange(that.fNumUnrefs, 0);
89     return *this;
90 }
91 
~TextureAwaitingUnref()92 inline GrResourceCache::TextureAwaitingUnref::~TextureAwaitingUnref() {
93     if (fTexture) {
94         for (int i = 0; i < fNumUnrefs; ++i) {
95             fTexture->unref();
96         }
97     }
98 }
99 
addRef()100 inline void GrResourceCache::TextureAwaitingUnref::TextureAwaitingUnref::addRef() { ++fNumUnrefs; }
101 
unref()102 inline void GrResourceCache::TextureAwaitingUnref::unref() {
103     SkASSERT(fNumUnrefs > 0);
104     fTexture->unref();
105     --fNumUnrefs;
106 }
107 
finished()108 inline bool GrResourceCache::TextureAwaitingUnref::finished() { return !fNumUnrefs; }
109 
110 //////////////////////////////////////////////////////////////////////////////
111 
GrResourceCache(const GrCaps * caps,GrSingleOwner * singleOwner,uint32_t contextUniqueID)112 GrResourceCache::GrResourceCache(const GrCaps* caps, GrSingleOwner* singleOwner,
113                                  uint32_t contextUniqueID)
114         : fInvalidUniqueKeyInbox(contextUniqueID)
115         , fFreedTextureInbox(contextUniqueID)
116         , fContextUniqueID(contextUniqueID)
117         , fSingleOwner(singleOwner)
118         , fPreferVRAMUseOverFlushes(caps->preferVRAMUseOverFlushes()) {
119     SkASSERT(contextUniqueID != SK_InvalidUniqueID);
120 }
121 
~GrResourceCache()122 GrResourceCache::~GrResourceCache() {
123     this->releaseAll();
124 }
125 
setLimit(size_t bytes)126 void GrResourceCache::setLimit(size_t bytes) {
127     fMaxBytes = bytes;
128     this->purgeAsNeeded();
129 }
130 
insertResource(GrGpuResource * resource)131 void GrResourceCache::insertResource(GrGpuResource* resource) {
132     ASSERT_SINGLE_OWNER
133     SkASSERT(resource);
134     SkASSERT(!this->isInCache(resource));
135     SkASSERT(!resource->wasDestroyed());
136     SkASSERT(!resource->resourcePriv().isPurgeable());
137 
138     // We must set the timestamp before adding to the array in case the timestamp wraps and we wind
139     // up iterating over all the resources that already have timestamps.
140     resource->cacheAccess().setTimestamp(this->getNextTimestamp());
141 
142     this->addToNonpurgeableArray(resource);
143 
144     size_t size = resource->gpuMemorySize();
145     SkDEBUGCODE(++fCount;)
146     fBytes += size;
147 #if GR_CACHE_STATS
148     fHighWaterCount = SkTMax(this->getResourceCount(), fHighWaterCount);
149     fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes);
150 #endif
151     if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
152         ++fBudgetedCount;
153         fBudgetedBytes += size;
154         TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
155                        fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
156 #if GR_CACHE_STATS
157         fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount);
158         fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes);
159 #endif
160     }
161     if (resource->resourcePriv().getScratchKey().isValid() &&
162         !resource->getUniqueKey().isValid()) {
163         SkASSERT(!resource->resourcePriv().refsWrappedObjects());
164         fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
165     }
166 
167     this->purgeAsNeeded();
168 }
169 
removeResource(GrGpuResource * resource)170 void GrResourceCache::removeResource(GrGpuResource* resource) {
171     ASSERT_SINGLE_OWNER
172     this->validate();
173     SkASSERT(this->isInCache(resource));
174 
175     size_t size = resource->gpuMemorySize();
176     if (resource->resourcePriv().isPurgeable()) {
177         fPurgeableQueue.remove(resource);
178         fPurgeableBytes -= size;
179     } else {
180         this->removeFromNonpurgeableArray(resource);
181     }
182 
183     SkDEBUGCODE(--fCount;)
184     fBytes -= size;
185     if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
186         --fBudgetedCount;
187         fBudgetedBytes -= size;
188         TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
189                        fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
190     }
191 
192     if (resource->resourcePriv().getScratchKey().isValid() &&
193         !resource->getUniqueKey().isValid()) {
194         fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
195     }
196     if (resource->getUniqueKey().isValid()) {
197         fUniqueHash.remove(resource->getUniqueKey());
198     }
199     this->validate();
200 }
201 
abandonAll()202 void GrResourceCache::abandonAll() {
203     AutoValidate av(this);
204 
205     // We need to make sure to free any resources that were waiting on a free message but never
206     // received one.
207     fTexturesAwaitingUnref.reset();
208 
209     while (fNonpurgeableResources.count()) {
210         GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
211         SkASSERT(!back->wasDestroyed());
212         back->cacheAccess().abandon();
213     }
214 
215     while (fPurgeableQueue.count()) {
216         GrGpuResource* top = fPurgeableQueue.peek();
217         SkASSERT(!top->wasDestroyed());
218         top->cacheAccess().abandon();
219     }
220 
221     SkASSERT(!fScratchMap.count());
222     SkASSERT(!fUniqueHash.count());
223     SkASSERT(!fCount);
224     SkASSERT(!this->getResourceCount());
225     SkASSERT(!fBytes);
226     SkASSERT(!fBudgetedCount);
227     SkASSERT(!fBudgetedBytes);
228     SkASSERT(!fPurgeableBytes);
229     SkASSERT(!fTexturesAwaitingUnref.count());
230 }
231 
releaseAll()232 void GrResourceCache::releaseAll() {
233     AutoValidate av(this);
234 
235     this->processFreedGpuResources();
236 
237     // We need to make sure to free any resources that were waiting on a free message but never
238     // received one.
239     fTexturesAwaitingUnref.reset();
240 
241     SkASSERT(fProxyProvider); // better have called setProxyProvider
242     // We must remove the uniqueKeys from the proxies here. While they possess a uniqueKey
243     // they also have a raw pointer back to this class (which is presumably going away)!
244     fProxyProvider->removeAllUniqueKeys();
245 
246     while (fNonpurgeableResources.count()) {
247         GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
248         SkASSERT(!back->wasDestroyed());
249         back->cacheAccess().release();
250     }
251 
252     while (fPurgeableQueue.count()) {
253         GrGpuResource* top = fPurgeableQueue.peek();
254         SkASSERT(!top->wasDestroyed());
255         top->cacheAccess().release();
256     }
257 
258     SkASSERT(!fScratchMap.count());
259     SkASSERT(!fUniqueHash.count());
260     SkASSERT(!fCount);
261     SkASSERT(!this->getResourceCount());
262     SkASSERT(!fBytes);
263     SkASSERT(!fBudgetedCount);
264     SkASSERT(!fBudgetedBytes);
265     SkASSERT(!fPurgeableBytes);
266     SkASSERT(!fTexturesAwaitingUnref.count());
267 }
268 
refResource(GrGpuResource * resource)269 void GrResourceCache::refResource(GrGpuResource* resource) {
270     SkASSERT(resource);
271     SkASSERT(resource->getContext()->priv().getResourceCache() == this);
272     if (resource->cacheAccess().hasRef()) {
273         resource->ref();
274     } else {
275         this->refAndMakeResourceMRU(resource);
276     }
277     this->validate();
278 }
279 
280 class GrResourceCache::AvailableForScratchUse {
281 public:
AvailableForScratchUse()282     AvailableForScratchUse() { }
283 
operator ()(const GrGpuResource * resource) const284     bool operator()(const GrGpuResource* resource) const {
285         SkASSERT(!resource->getUniqueKey().isValid() &&
286                  resource->resourcePriv().getScratchKey().isValid());
287 
288         // isScratch() also tests that the resource is budgeted.
289         if (resource->internalHasRef() || !resource->cacheAccess().isScratch()) {
290             return false;
291         }
292         return true;
293     }
294 };
295 
findAndRefScratchResource(const GrScratchKey & scratchKey)296 GrGpuResource* GrResourceCache::findAndRefScratchResource(const GrScratchKey& scratchKey) {
297     SkASSERT(scratchKey.isValid());
298 
299     GrGpuResource* resource = fScratchMap.find(scratchKey, AvailableForScratchUse());
300     if (resource) {
301         this->refAndMakeResourceMRU(resource);
302         this->validate();
303     }
304     return resource;
305 }
306 
willRemoveScratchKey(const GrGpuResource * resource)307 void GrResourceCache::willRemoveScratchKey(const GrGpuResource* resource) {
308     ASSERT_SINGLE_OWNER
309     SkASSERT(resource->resourcePriv().getScratchKey().isValid());
310     if (!resource->getUniqueKey().isValid()) {
311         fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
312     }
313 }
314 
removeUniqueKey(GrGpuResource * resource)315 void GrResourceCache::removeUniqueKey(GrGpuResource* resource) {
316     ASSERT_SINGLE_OWNER
317     // Someone has a ref to this resource in order to have removed the key. When the ref count
318     // reaches zero we will get a ref cnt notification and figure out what to do with it.
319     if (resource->getUniqueKey().isValid()) {
320         SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
321         fUniqueHash.remove(resource->getUniqueKey());
322     }
323     resource->cacheAccess().removeUniqueKey();
324     if (resource->resourcePriv().getScratchKey().isValid()) {
325         fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
326     }
327 
328     // Removing a unique key from a kUnbudgetedCacheable resource would make the resource
329     // require purging. However, the resource must be ref'ed to get here and therefore can't
330     // be purgeable. We'll purge it when the refs reach zero.
331     SkASSERT(!resource->resourcePriv().isPurgeable());
332     this->validate();
333 }
334 
changeUniqueKey(GrGpuResource * resource,const GrUniqueKey & newKey)335 void GrResourceCache::changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) {
336     ASSERT_SINGLE_OWNER
337     SkASSERT(resource);
338     SkASSERT(this->isInCache(resource));
339 
340     // If another resource has the new key, remove its key then install the key on this resource.
341     if (newKey.isValid()) {
342         if (GrGpuResource* old = fUniqueHash.find(newKey)) {
343             // If the old resource using the key is purgeable and is unreachable, then remove it.
344             if (!old->resourcePriv().getScratchKey().isValid() &&
345                 old->resourcePriv().isPurgeable()) {
346                 old->cacheAccess().release();
347             } else {
348                 // removeUniqueKey expects an external owner of the resource.
349                 this->removeUniqueKey(sk_ref_sp(old).get());
350             }
351         }
352         SkASSERT(nullptr == fUniqueHash.find(newKey));
353 
354         // Remove the entry for this resource if it already has a unique key.
355         if (resource->getUniqueKey().isValid()) {
356             SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
357             fUniqueHash.remove(resource->getUniqueKey());
358             SkASSERT(nullptr == fUniqueHash.find(resource->getUniqueKey()));
359         } else {
360             // 'resource' didn't have a valid unique key before so it is switching sides. Remove it
361             // from the ScratchMap
362             if (resource->resourcePriv().getScratchKey().isValid()) {
363                 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
364             }
365         }
366 
367         resource->cacheAccess().setUniqueKey(newKey);
368         fUniqueHash.add(resource);
369     } else {
370         this->removeUniqueKey(resource);
371     }
372 
373     this->validate();
374 }
375 
refAndMakeResourceMRU(GrGpuResource * resource)376 void GrResourceCache::refAndMakeResourceMRU(GrGpuResource* resource) {
377     ASSERT_SINGLE_OWNER
378     SkASSERT(resource);
379     SkASSERT(this->isInCache(resource));
380 
381     if (resource->resourcePriv().isPurgeable()) {
382         // It's about to become unpurgeable.
383         fPurgeableBytes -= resource->gpuMemorySize();
384         fPurgeableQueue.remove(resource);
385         this->addToNonpurgeableArray(resource);
386     } else if (!resource->cacheAccess().hasRef() &&
387                resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
388         SkASSERT(fNumBudgetedResourcesFlushWillMakePurgeable > 0);
389         fNumBudgetedResourcesFlushWillMakePurgeable--;
390     }
391     resource->cacheAccess().ref();
392 
393     resource->cacheAccess().setTimestamp(this->getNextTimestamp());
394     this->validate();
395 }
396 
notifyRefCntReachedZero(GrGpuResource * resource)397 void GrResourceCache::notifyRefCntReachedZero(GrGpuResource* resource) {
398     ASSERT_SINGLE_OWNER
399     SkASSERT(resource);
400     SkASSERT(!resource->wasDestroyed());
401     SkASSERT(this->isInCache(resource));
402     // This resource should always be in the nonpurgeable array when this function is called. It
403     // will be moved to the queue if it is newly purgeable.
404     SkASSERT(fNonpurgeableResources[*resource->cacheAccess().accessCacheIndex()] == resource);
405 
406 #ifdef SK_DEBUG
407     // When the timestamp overflows validate() is called. validate() checks that resources in
408     // the nonpurgeable array are indeed not purgeable. However, the movement from the array to
409     // the purgeable queue happens just below in this function. So we mark it as an exception.
410     if (resource->resourcePriv().isPurgeable()) {
411         fNewlyPurgeableResourceForValidation = resource;
412     }
413 #endif
414     resource->cacheAccess().setTimestamp(this->getNextTimestamp());
415     SkDEBUGCODE(fNewlyPurgeableResourceForValidation = nullptr);
416 
417     if (!resource->resourcePriv().isPurgeable() &&
418         resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
419         ++fNumBudgetedResourcesFlushWillMakePurgeable;
420     }
421 
422     if (!resource->resourcePriv().isPurgeable()) {
423         this->validate();
424         return;
425     }
426 
427     this->removeFromNonpurgeableArray(resource);
428     fPurgeableQueue.insert(resource);
429     resource->cacheAccess().setTimeWhenResourceBecomePurgeable();
430     fPurgeableBytes += resource->gpuMemorySize();
431 
432     bool hasUniqueKey = resource->getUniqueKey().isValid();
433 
434     GrBudgetedType budgetedType = resource->resourcePriv().budgetedType();
435 
436     if (budgetedType == GrBudgetedType::kBudgeted) {
437         // Purge the resource immediately if we're over budget
438         // Also purge if the resource has neither a valid scratch key nor a unique key.
439         bool hasKey = resource->resourcePriv().getScratchKey().isValid() || hasUniqueKey;
440         if (!this->overBudget() && hasKey) {
441             return;
442         }
443     } else {
444         // We keep unbudgeted resources with a unique key in the purgeable queue of the cache so
445         // they can be reused again by the image connected to the unique key.
446         if (hasUniqueKey && budgetedType == GrBudgetedType::kUnbudgetedCacheable) {
447             return;
448         }
449         // Check whether this resource could still be used as a scratch resource.
450         if (!resource->resourcePriv().refsWrappedObjects() &&
451             resource->resourcePriv().getScratchKey().isValid()) {
452             // We won't purge an existing resource to make room for this one.
453             if (this->wouldFit(resource->gpuMemorySize())) {
454                 resource->resourcePriv().makeBudgeted();
455                 return;
456             }
457         }
458     }
459 
460     SkDEBUGCODE(int beforeCount = this->getResourceCount();)
461     resource->cacheAccess().release();
462     // We should at least free this resource, perhaps dependent resources as well.
463     SkASSERT(this->getResourceCount() < beforeCount);
464     this->validate();
465 }
466 
didChangeBudgetStatus(GrGpuResource * resource)467 void GrResourceCache::didChangeBudgetStatus(GrGpuResource* resource) {
468     ASSERT_SINGLE_OWNER
469     SkASSERT(resource);
470     SkASSERT(this->isInCache(resource));
471 
472     size_t size = resource->gpuMemorySize();
473     // Changing from BudgetedType::kUnbudgetedCacheable to another budgeted type could make
474     // resource become purgeable. However, we should never allow that transition. Wrapped
475     // resources are the only resources that can be in that state and they aren't allowed to
476     // transition from one budgeted state to another.
477     SkDEBUGCODE(bool wasPurgeable = resource->resourcePriv().isPurgeable());
478     if (resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
479         ++fBudgetedCount;
480         fBudgetedBytes += size;
481 #if GR_CACHE_STATS
482         fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes);
483         fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount);
484 #endif
485         if (!resource->resourcePriv().isPurgeable() && !resource->cacheAccess().hasRef()) {
486             ++fNumBudgetedResourcesFlushWillMakePurgeable;
487         }
488         this->purgeAsNeeded();
489     } else {
490         SkASSERT(resource->resourcePriv().budgetedType() != GrBudgetedType::kUnbudgetedCacheable);
491         --fBudgetedCount;
492         fBudgetedBytes -= size;
493         if (!resource->resourcePriv().isPurgeable() && !resource->cacheAccess().hasRef()) {
494             --fNumBudgetedResourcesFlushWillMakePurgeable;
495         }
496     }
497     SkASSERT(wasPurgeable == resource->resourcePriv().isPurgeable());
498     TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
499                    fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
500 
501     this->validate();
502 }
503 
purgeAsNeeded()504 void GrResourceCache::purgeAsNeeded() {
505     SkTArray<GrUniqueKeyInvalidatedMessage> invalidKeyMsgs;
506     fInvalidUniqueKeyInbox.poll(&invalidKeyMsgs);
507     if (invalidKeyMsgs.count()) {
508         SkASSERT(fProxyProvider);
509 
510         for (int i = 0; i < invalidKeyMsgs.count(); ++i) {
511             fProxyProvider->processInvalidUniqueKey(invalidKeyMsgs[i].key(), nullptr,
512                                                     GrProxyProvider::InvalidateGPUResource::kYes);
513             SkASSERT(!this->findAndRefUniqueResource(invalidKeyMsgs[i].key()));
514         }
515     }
516 
517     this->processFreedGpuResources();
518 
519     bool stillOverbudget = this->overBudget();
520     while (stillOverbudget && fPurgeableQueue.count()) {
521         GrGpuResource* resource = fPurgeableQueue.peek();
522         SkASSERT(resource->resourcePriv().isPurgeable());
523         resource->cacheAccess().release();
524         stillOverbudget = this->overBudget();
525     }
526 
527     this->validate();
528 }
529 
purgeUnlockedResources(bool scratchResourcesOnly)530 void GrResourceCache::purgeUnlockedResources(bool scratchResourcesOnly) {
531     if (!scratchResourcesOnly) {
532         // We could disable maintaining the heap property here, but it would add a lot of
533         // complexity. Moreover, this is rarely called.
534         while (fPurgeableQueue.count()) {
535             GrGpuResource* resource = fPurgeableQueue.peek();
536             SkASSERT(resource->resourcePriv().isPurgeable());
537             resource->cacheAccess().release();
538         }
539     } else {
540         // Sort the queue
541         fPurgeableQueue.sort();
542 
543         // Make a list of the scratch resources to delete
544         SkTDArray<GrGpuResource*> scratchResources;
545         for (int i = 0; i < fPurgeableQueue.count(); i++) {
546             GrGpuResource* resource = fPurgeableQueue.at(i);
547             SkASSERT(resource->resourcePriv().isPurgeable());
548             if (!resource->getUniqueKey().isValid()) {
549                 *scratchResources.append() = resource;
550             }
551         }
552 
553         // Delete the scratch resources. This must be done as a separate pass
554         // to avoid messing up the sorted order of the queue
555         for (int i = 0; i < scratchResources.count(); i++) {
556             scratchResources.getAt(i)->cacheAccess().release();
557         }
558     }
559 
560     this->validate();
561 }
562 
purgeResourcesNotUsedSince(GrStdSteadyClock::time_point purgeTime)563 void GrResourceCache::purgeResourcesNotUsedSince(GrStdSteadyClock::time_point purgeTime) {
564     while (fPurgeableQueue.count()) {
565         const GrStdSteadyClock::time_point resourceTime =
566                 fPurgeableQueue.peek()->cacheAccess().timeWhenResourceBecamePurgeable();
567         if (resourceTime >= purgeTime) {
568             // Resources were given both LRU timestamps and tagged with a frame number when
569             // they first became purgeable. The LRU timestamp won't change again until the
570             // resource is made non-purgeable again. So, at this point all the remaining
571             // resources in the timestamp-sorted queue will have a frame number >= to this
572             // one.
573             break;
574         }
575         GrGpuResource* resource = fPurgeableQueue.peek();
576         SkASSERT(resource->resourcePriv().isPurgeable());
577         resource->cacheAccess().release();
578     }
579 }
580 
purgeUnlockedResources(size_t bytesToPurge,bool preferScratchResources)581 void GrResourceCache::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
582 
583     const size_t tmpByteBudget = SkTMax((size_t)0, fBytes - bytesToPurge);
584     bool stillOverbudget = tmpByteBudget < fBytes;
585 
586     if (preferScratchResources && bytesToPurge < fPurgeableBytes) {
587         // Sort the queue
588         fPurgeableQueue.sort();
589 
590         // Make a list of the scratch resources to delete
591         SkTDArray<GrGpuResource*> scratchResources;
592         size_t scratchByteCount = 0;
593         for (int i = 0; i < fPurgeableQueue.count() && stillOverbudget; i++) {
594             GrGpuResource* resource = fPurgeableQueue.at(i);
595             SkASSERT(resource->resourcePriv().isPurgeable());
596             if (!resource->getUniqueKey().isValid()) {
597                 *scratchResources.append() = resource;
598                 scratchByteCount += resource->gpuMemorySize();
599                 stillOverbudget = tmpByteBudget < fBytes - scratchByteCount;
600             }
601         }
602 
603         // Delete the scratch resources. This must be done as a separate pass
604         // to avoid messing up the sorted order of the queue
605         for (int i = 0; i < scratchResources.count(); i++) {
606             scratchResources.getAt(i)->cacheAccess().release();
607         }
608         stillOverbudget = tmpByteBudget < fBytes;
609 
610         this->validate();
611     }
612 
613     // Purge any remaining resources in LRU order
614     if (stillOverbudget) {
615         const size_t cachedByteCount = fMaxBytes;
616         fMaxBytes = tmpByteBudget;
617         this->purgeAsNeeded();
618         fMaxBytes = cachedByteCount;
619     }
620 }
requestsFlush() const621 bool GrResourceCache::requestsFlush() const {
622     return this->overBudget() && !fPurgeableQueue.count() &&
623            fNumBudgetedResourcesFlushWillMakePurgeable > 0;
624 }
625 
626 
insertDelayedTextureUnref(GrTexture * texture)627 void GrResourceCache::insertDelayedTextureUnref(GrTexture* texture) {
628     texture->ref();
629     uint32_t id = texture->uniqueID().asUInt();
630     if (auto* data = fTexturesAwaitingUnref.find(id)) {
631         data->addRef();
632     } else {
633         fTexturesAwaitingUnref.set(id, {texture});
634     }
635 }
636 
processFreedGpuResources()637 void GrResourceCache::processFreedGpuResources() {
638     if (!fTexturesAwaitingUnref.count()) {
639       return;
640     }
641 
642     SkTArray<GrTextureFreedMessage> msgs;
643     fFreedTextureInbox.poll(&msgs);
644     for (int i = 0; i < msgs.count(); ++i) {
645         SkASSERT(msgs[i].fOwningUniqueID == fContextUniqueID);
646         uint32_t id = msgs[i].fTexture->uniqueID().asUInt();
647         TextureAwaitingUnref* info = fTexturesAwaitingUnref.find(id);
648         // If we called release or abandon on the GrContext we will have already released our ref on
649         // the GrGpuResource. If then the message arrives before the actual GrContext gets destroyed
650         // we will try to process the message when we destroy the GrContext. This protects us from
651         // trying to unref the resource twice.
652         if (info) {
653             info->unref();
654             if (info->finished()) {
655                 fTexturesAwaitingUnref.remove(id);
656             }
657         }
658     }
659 }
660 
addToNonpurgeableArray(GrGpuResource * resource)661 void GrResourceCache::addToNonpurgeableArray(GrGpuResource* resource) {
662     int index = fNonpurgeableResources.count();
663     *fNonpurgeableResources.append() = resource;
664     *resource->cacheAccess().accessCacheIndex() = index;
665 }
666 
removeFromNonpurgeableArray(GrGpuResource * resource)667 void GrResourceCache::removeFromNonpurgeableArray(GrGpuResource* resource) {
668     int* index = resource->cacheAccess().accessCacheIndex();
669     // Fill the whole we will create in the array with the tail object, adjust its index, and
670     // then pop the array
671     GrGpuResource* tail = *(fNonpurgeableResources.end() - 1);
672     SkASSERT(fNonpurgeableResources[*index] == resource);
673     fNonpurgeableResources[*index] = tail;
674     *tail->cacheAccess().accessCacheIndex() = *index;
675     fNonpurgeableResources.pop();
676     SkDEBUGCODE(*index = -1);
677 }
678 
getNextTimestamp()679 uint32_t GrResourceCache::getNextTimestamp() {
680     // If we wrap then all the existing resources will appear older than any resources that get
681     // a timestamp after the wrap.
682     if (0 == fTimestamp) {
683         int count = this->getResourceCount();
684         if (count) {
685             // Reset all the timestamps. We sort the resources by timestamp and then assign
686             // sequential timestamps beginning with 0. This is O(n*lg(n)) but it should be extremely
687             // rare.
688             SkTDArray<GrGpuResource*> sortedPurgeableResources;
689             sortedPurgeableResources.setReserve(fPurgeableQueue.count());
690 
691             while (fPurgeableQueue.count()) {
692                 *sortedPurgeableResources.append() = fPurgeableQueue.peek();
693                 fPurgeableQueue.pop();
694             }
695 
696             SkTQSort(fNonpurgeableResources.begin(), fNonpurgeableResources.end() - 1,
697                      CompareTimestamp);
698 
699             // Pick resources out of the purgeable and non-purgeable arrays based on lowest
700             // timestamp and assign new timestamps.
701             int currP = 0;
702             int currNP = 0;
703             while (currP < sortedPurgeableResources.count() &&
704                    currNP < fNonpurgeableResources.count()) {
705                 uint32_t tsP = sortedPurgeableResources[currP]->cacheAccess().timestamp();
706                 uint32_t tsNP = fNonpurgeableResources[currNP]->cacheAccess().timestamp();
707                 SkASSERT(tsP != tsNP);
708                 if (tsP < tsNP) {
709                     sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
710                 } else {
711                     // Correct the index in the nonpurgeable array stored on the resource post-sort.
712                     *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
713                     fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
714                 }
715             }
716 
717             // The above loop ended when we hit the end of one array. Finish the other one.
718             while (currP < sortedPurgeableResources.count()) {
719                 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
720             }
721             while (currNP < fNonpurgeableResources.count()) {
722                 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
723                 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
724             }
725 
726             // Rebuild the queue.
727             for (int i = 0; i < sortedPurgeableResources.count(); ++i) {
728                 fPurgeableQueue.insert(sortedPurgeableResources[i]);
729             }
730 
731             this->validate();
732             SkASSERT(count == this->getResourceCount());
733 
734             // count should be the next timestamp we return.
735             SkASSERT(fTimestamp == SkToU32(count));
736         }
737     }
738     return fTimestamp++;
739 }
740 
dumpMemoryStatistics(SkTraceMemoryDump * traceMemoryDump) const741 void GrResourceCache::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
742     for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
743         fNonpurgeableResources[i]->dumpMemoryStatistics(traceMemoryDump);
744     }
745     for (int i = 0; i < fPurgeableQueue.count(); ++i) {
746         fPurgeableQueue.at(i)->dumpMemoryStatistics(traceMemoryDump);
747     }
748 }
749 
750 #if GR_CACHE_STATS
getStats(Stats * stats) const751 void GrResourceCache::getStats(Stats* stats) const {
752     stats->reset();
753 
754     stats->fTotal = this->getResourceCount();
755     stats->fNumNonPurgeable = fNonpurgeableResources.count();
756     stats->fNumPurgeable = fPurgeableQueue.count();
757 
758     for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
759         stats->update(fNonpurgeableResources[i]);
760     }
761     for (int i = 0; i < fPurgeableQueue.count(); ++i) {
762         stats->update(fPurgeableQueue.at(i));
763     }
764 }
765 
766 #if GR_TEST_UTILS
dumpStats(SkString * out) const767 void GrResourceCache::dumpStats(SkString* out) const {
768     this->validate();
769 
770     Stats stats;
771 
772     this->getStats(&stats);
773 
774     float byteUtilization = (100.f * fBudgetedBytes) / fMaxBytes;
775 
776     out->appendf("Budget: %d bytes\n", (int)fMaxBytes);
777     out->appendf("\t\tEntry Count: current %d"
778                  " (%d budgeted, %d wrapped, %d locked, %d scratch), high %d\n",
779                  stats.fTotal, fBudgetedCount, stats.fWrapped, stats.fNumNonPurgeable,
780                  stats.fScratch, fHighWaterCount);
781     out->appendf("\t\tEntry Bytes: current %d (budgeted %d, %.2g%% full, %d unbudgeted) high %d\n",
782                  SkToInt(fBytes), SkToInt(fBudgetedBytes), byteUtilization,
783                  SkToInt(stats.fUnbudgetedSize), SkToInt(fHighWaterBytes));
784 }
785 
dumpStatsKeyValuePairs(SkTArray<SkString> * keys,SkTArray<double> * values) const786 void GrResourceCache::dumpStatsKeyValuePairs(SkTArray<SkString>* keys,
787                                              SkTArray<double>* values) const {
788     this->validate();
789 
790     Stats stats;
791     this->getStats(&stats);
792 
793     keys->push_back(SkString("gpu_cache_purgable_entries")); values->push_back(stats.fNumPurgeable);
794 }
795 #endif
796 
797 #endif
798 
799 #ifdef SK_DEBUG
validate() const800 void GrResourceCache::validate() const {
801     // Reduce the frequency of validations for large resource counts.
802     static SkRandom gRandom;
803     int mask = (SkNextPow2(fCount + 1) >> 5) - 1;
804     if (~mask && (gRandom.nextU() & mask)) {
805         return;
806     }
807 
808     struct Stats {
809         size_t fBytes;
810         int fBudgetedCount;
811         size_t fBudgetedBytes;
812         int fLocked;
813         int fScratch;
814         int fCouldBeScratch;
815         int fContent;
816         const ScratchMap* fScratchMap;
817         const UniqueHash* fUniqueHash;
818 
819         Stats(const GrResourceCache* cache) {
820             memset(this, 0, sizeof(*this));
821             fScratchMap = &cache->fScratchMap;
822             fUniqueHash = &cache->fUniqueHash;
823         }
824 
825         void update(GrGpuResource* resource) {
826             fBytes += resource->gpuMemorySize();
827 
828             if (!resource->resourcePriv().isPurgeable()) {
829                 ++fLocked;
830             }
831 
832             const GrScratchKey& scratchKey = resource->resourcePriv().getScratchKey();
833             const GrUniqueKey& uniqueKey = resource->getUniqueKey();
834 
835             if (resource->cacheAccess().isScratch()) {
836                 SkASSERT(!uniqueKey.isValid());
837                 ++fScratch;
838                 SkASSERT(fScratchMap->countForKey(scratchKey));
839                 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
840             } else if (scratchKey.isValid()) {
841                 SkASSERT(GrBudgetedType::kBudgeted != resource->resourcePriv().budgetedType() ||
842                          uniqueKey.isValid());
843                 if (!uniqueKey.isValid()) {
844                     ++fCouldBeScratch;
845                     SkASSERT(fScratchMap->countForKey(scratchKey));
846                 }
847                 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
848             }
849             if (uniqueKey.isValid()) {
850                 ++fContent;
851                 SkASSERT(fUniqueHash->find(uniqueKey) == resource);
852                 SkASSERT(GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType() ||
853                          resource->resourcePriv().refsWrappedObjects());
854 
855                 if (scratchKey.isValid()) {
856                     SkASSERT(!fScratchMap->has(resource, scratchKey));
857                 }
858             }
859 
860             if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
861                 ++fBudgetedCount;
862                 fBudgetedBytes += resource->gpuMemorySize();
863             }
864         }
865     };
866 
867     {
868         ScratchMap::ConstIter iter(&fScratchMap);
869 
870         int count = 0;
871         for ( ; !iter.done(); ++iter) {
872             const GrGpuResource* resource = *iter;
873             SkASSERT(resource->resourcePriv().getScratchKey().isValid());
874             SkASSERT(!resource->getUniqueKey().isValid());
875             count++;
876         }
877         SkASSERT(count == fScratchMap.count()); // ensure the iterator is working correctly
878     }
879 
880     Stats stats(this);
881     size_t purgeableBytes = 0;
882     int numBudgetedResourcesFlushWillMakePurgeable = 0;
883 
884     for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
885         SkASSERT(!fNonpurgeableResources[i]->resourcePriv().isPurgeable() ||
886                  fNewlyPurgeableResourceForValidation == fNonpurgeableResources[i]);
887         SkASSERT(*fNonpurgeableResources[i]->cacheAccess().accessCacheIndex() == i);
888         SkASSERT(!fNonpurgeableResources[i]->wasDestroyed());
889         if (fNonpurgeableResources[i]->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted &&
890             !fNonpurgeableResources[i]->cacheAccess().hasRef() &&
891             fNewlyPurgeableResourceForValidation != fNonpurgeableResources[i]) {
892             ++numBudgetedResourcesFlushWillMakePurgeable;
893         }
894         stats.update(fNonpurgeableResources[i]);
895     }
896     for (int i = 0; i < fPurgeableQueue.count(); ++i) {
897         SkASSERT(fPurgeableQueue.at(i)->resourcePriv().isPurgeable());
898         SkASSERT(*fPurgeableQueue.at(i)->cacheAccess().accessCacheIndex() == i);
899         SkASSERT(!fPurgeableQueue.at(i)->wasDestroyed());
900         stats.update(fPurgeableQueue.at(i));
901         purgeableBytes += fPurgeableQueue.at(i)->gpuMemorySize();
902     }
903 
904     SkASSERT(fCount == this->getResourceCount());
905     SkASSERT(fBudgetedCount <= fCount);
906     SkASSERT(fBudgetedBytes <= fBytes);
907     SkASSERT(stats.fBytes == fBytes);
908     SkASSERT(fNumBudgetedResourcesFlushWillMakePurgeable ==
909              numBudgetedResourcesFlushWillMakePurgeable);
910     SkASSERT(stats.fBudgetedBytes == fBudgetedBytes);
911     SkASSERT(stats.fBudgetedCount == fBudgetedCount);
912     SkASSERT(purgeableBytes == fPurgeableBytes);
913 #if GR_CACHE_STATS
914     SkASSERT(fBudgetedHighWaterCount <= fHighWaterCount);
915     SkASSERT(fBudgetedHighWaterBytes <= fHighWaterBytes);
916     SkASSERT(fBytes <= fHighWaterBytes);
917     SkASSERT(fCount <= fHighWaterCount);
918     SkASSERT(fBudgetedBytes <= fBudgetedHighWaterBytes);
919     SkASSERT(fBudgetedCount <= fBudgetedHighWaterCount);
920 #endif
921     SkASSERT(stats.fContent == fUniqueHash.count());
922     SkASSERT(stats.fScratch + stats.fCouldBeScratch == fScratchMap.count());
923 
924     // This assertion is not currently valid because we can be in recursive notifyCntReachedZero()
925     // calls. This will be fixed when subresource registration is explicit.
926     // bool overBudget = budgetedBytes > fMaxBytes || budgetedCount > fMaxCount;
927     // SkASSERT(!overBudget || locked == count || fPurging);
928 }
929 
isInCache(const GrGpuResource * resource) const930 bool GrResourceCache::isInCache(const GrGpuResource* resource) const {
931     int index = *resource->cacheAccess().accessCacheIndex();
932     if (index < 0) {
933         return false;
934     }
935     if (index < fPurgeableQueue.count() && fPurgeableQueue.at(index) == resource) {
936         return true;
937     }
938     if (index < fNonpurgeableResources.count() && fNonpurgeableResources[index] == resource) {
939         return true;
940     }
941     SkDEBUGFAIL("Resource index should be -1 or the resource should be in the cache.");
942     return false;
943 }
944 
945 #endif
946