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