blob: 4e69444b612875393f63586a5fe9cb886485810b [file] [log] [blame]
bsalomonc8dc1f72014-08-21 13:02:13 -07001/*
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
bsalomon0ea80f42015-02-11 10:49:59 -08008#include "GrResourceCache.h"
robertphillips28a838e2016-06-23 14:07:00 -07009
10#include "GrCaps.h"
bsalomon3582d3e2015-02-13 14:20:05 -080011#include "GrGpuResourceCacheAccess.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050012#include "GrProxyProvider.h"
Robert Phillipsae7d3f32017-09-21 08:26:08 -040013#include "GrTexture.h"
14#include "GrTextureProxyCacheAccess.h"
hendrikw876c3132015-03-04 10:33:49 -080015#include "GrTracing.h"
bsalomon71cb0c22014-11-14 12:10:14 -080016#include "SkGr.h"
17#include "SkMessageBus.h"
mtklein4e976072016-08-08 09:06:27 -070018#include "SkOpts.h"
bsalomonddf30e62015-02-19 11:38:44 -080019#include "SkTSort.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040020#include "SkTo.h"
bsalomon71cb0c22014-11-14 12:10:14 -080021
bsalomon8718aaf2015-02-19 07:24:21 -080022DECLARE_SKMESSAGEBUS_MESSAGE(GrUniqueKeyInvalidatedMessage);
bsalomon71cb0c22014-11-14 12:10:14 -080023
Brian Osman13dddce2017-05-09 13:19:50 -040024DECLARE_SKMESSAGEBUS_MESSAGE(GrGpuResourceFreedMessage);
25
bsalomon71cb0c22014-11-14 12:10:14 -080026//////////////////////////////////////////////////////////////////////////////
27
bsalomon7775c852014-12-30 12:50:52 -080028GrScratchKey::ResourceType GrScratchKey::GenerateResourceType() {
bsalomon24db3b12015-01-23 04:24:04 -080029 static int32_t gType = INHERITED::kInvalidDomain + 1;
bsalomonfe369ee2014-11-10 11:59:06 -080030
bsalomon7775c852014-12-30 12:50:52 -080031 int32_t type = sk_atomic_inc(&gType);
Hal Canarye1bc7de2018-06-13 15:01:39 -040032 if (type > UINT16_MAX) {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040033 SK_ABORT("Too many Resource Types");
bsalomon71cb0c22014-11-14 12:10:14 -080034 }
35
36 return static_cast<ResourceType>(type);
37}
38
bsalomon8718aaf2015-02-19 07:24:21 -080039GrUniqueKey::Domain GrUniqueKey::GenerateDomain() {
bsalomon24db3b12015-01-23 04:24:04 -080040 static int32_t gDomain = INHERITED::kInvalidDomain + 1;
bsalomon7775c852014-12-30 12:50:52 -080041
bsalomon24db3b12015-01-23 04:24:04 -080042 int32_t domain = sk_atomic_inc(&gDomain);
Hal Canarye1bc7de2018-06-13 15:01:39 -040043 if (domain > UINT16_MAX) {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040044 SK_ABORT("Too many GrUniqueKey Domains");
bsalomon7775c852014-12-30 12:50:52 -080045 }
bsalomon24db3b12015-01-23 04:24:04 -080046
47 return static_cast<Domain>(domain);
48}
bsalomon3f324322015-04-08 11:01:54 -070049
bsalomon24db3b12015-01-23 04:24:04 -080050uint32_t GrResourceKeyHash(const uint32_t* data, size_t size) {
mtklein4e976072016-08-08 09:06:27 -070051 return SkOpts::hash(data, size);
bsalomon7775c852014-12-30 12:50:52 -080052}
53
bsalomonfe369ee2014-11-10 11:59:06 -080054//////////////////////////////////////////////////////////////////////////////
55
bsalomon0ea80f42015-02-11 10:49:59 -080056class GrResourceCache::AutoValidate : ::SkNoncopyable {
bsalomon71cb0c22014-11-14 12:10:14 -080057public:
bsalomon0ea80f42015-02-11 10:49:59 -080058 AutoValidate(GrResourceCache* cache) : fCache(cache) { cache->validate(); }
bsalomon71cb0c22014-11-14 12:10:14 -080059 ~AutoValidate() { fCache->validate(); }
60private:
bsalomon0ea80f42015-02-11 10:49:59 -080061 GrResourceCache* fCache;
bsalomon71cb0c22014-11-14 12:10:14 -080062};
63
64 //////////////////////////////////////////////////////////////////////////////
robertphillipsee843b22016-10-04 05:30:20 -070065
bsalomon71cb0c22014-11-14 12:10:14 -080066
Brian Osman13dddce2017-05-09 13:19:50 -040067GrResourceCache::GrResourceCache(const GrCaps* caps, uint32_t contextUniqueID)
Robert Phillips1afd4cd2018-01-08 13:40:32 -050068 : fProxyProvider(nullptr)
69 , fTimestamp(0)
bsalomon9f2d1572015-02-17 11:47:40 -080070 , fMaxCount(kDefaultMaxCount)
bsalomon71cb0c22014-11-14 12:10:14 -080071 , fMaxBytes(kDefaultMaxSize)
bsalomon3f324322015-04-08 11:01:54 -070072 , fMaxUnusedFlushes(kDefaultMaxUnusedFlushes)
bsalomon71cb0c22014-11-14 12:10:14 -080073#if GR_CACHE_STATS
74 , fHighWaterCount(0)
75 , fHighWaterBytes(0)
bsalomondace19e2014-11-17 07:34:06 -080076 , fBudgetedHighWaterCount(0)
77 , fBudgetedHighWaterBytes(0)
bsalomon71cb0c22014-11-14 12:10:14 -080078#endif
bsalomon71cb0c22014-11-14 12:10:14 -080079 , fBytes(0)
bsalomondace19e2014-11-17 07:34:06 -080080 , fBudgetedCount(0)
81 , fBudgetedBytes(0)
Derek Sollenbergeree479142017-05-24 11:41:33 -040082 , fPurgeableBytes(0)
robertphillipsee843b22016-10-04 05:30:20 -070083 , fRequestFlush(false)
bsalomone2e87f32016-09-22 12:42:11 -070084 , fExternalFlushCnt(0)
Brian Osman13dddce2017-05-09 13:19:50 -040085 , fContextUniqueID(contextUniqueID)
robertphillips63926682015-08-20 09:39:02 -070086 , fPreferVRAMUseOverFlushes(caps->preferVRAMUseOverFlushes()) {
bsalomonf320e042015-02-17 15:09:34 -080087 SkDEBUGCODE(fCount = 0;)
halcanary96fcdcc2015-08-27 07:41:13 -070088 SkDEBUGCODE(fNewlyPurgeableResourceForValidation = nullptr;)
bsalomon71cb0c22014-11-14 12:10:14 -080089}
90
bsalomon0ea80f42015-02-11 10:49:59 -080091GrResourceCache::~GrResourceCache() {
bsalomonc8dc1f72014-08-21 13:02:13 -070092 this->releaseAll();
93}
94
bsalomon3f324322015-04-08 11:01:54 -070095void GrResourceCache::setLimits(int count, size_t bytes, int maxUnusedFlushes) {
bsalomon71cb0c22014-11-14 12:10:14 -080096 fMaxCount = count;
97 fMaxBytes = bytes;
bsalomon3f324322015-04-08 11:01:54 -070098 fMaxUnusedFlushes = maxUnusedFlushes;
bsalomon71cb0c22014-11-14 12:10:14 -080099 this->purgeAsNeeded();
100}
101
bsalomon0ea80f42015-02-11 10:49:59 -0800102void GrResourceCache::insertResource(GrGpuResource* resource) {
bsalomon49f085d2014-09-05 13:34:00 -0700103 SkASSERT(resource);
bsalomon16961262014-08-26 14:01:07 -0700104 SkASSERT(!this->isInCache(resource));
bsalomonf320e042015-02-17 15:09:34 -0800105 SkASSERT(!resource->wasDestroyed());
106 SkASSERT(!resource->isPurgeable());
bsalomonddf30e62015-02-19 11:38:44 -0800107
108 // We must set the timestamp before adding to the array in case the timestamp wraps and we wind
109 // up iterating over all the resources that already have timestamps.
110 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
111
bsalomonf320e042015-02-17 15:09:34 -0800112 this->addToNonpurgeableArray(resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800113
bsalomondace19e2014-11-17 07:34:06 -0800114 size_t size = resource->gpuMemorySize();
bsalomonf320e042015-02-17 15:09:34 -0800115 SkDEBUGCODE(++fCount;)
bsalomon84c8e622014-11-17 09:33:27 -0800116 fBytes += size;
bsalomon82b1d622014-11-14 13:59:57 -0800117#if GR_CACHE_STATS
bsalomonf320e042015-02-17 15:09:34 -0800118 fHighWaterCount = SkTMax(this->getResourceCount(), fHighWaterCount);
bsalomon82b1d622014-11-14 13:59:57 -0800119 fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes);
120#endif
bsalomon5ec26ae2016-02-25 08:33:02 -0800121 if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) {
bsalomondace19e2014-11-17 07:34:06 -0800122 ++fBudgetedCount;
123 fBudgetedBytes += size;
Brian Osman39c08ac2017-07-26 09:36:09 -0400124 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800125 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomondace19e2014-11-17 07:34:06 -0800126#if GR_CACHE_STATS
127 fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount);
128 fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes);
129#endif
130 }
robertphillipsc4ed6842016-05-24 14:17:12 -0700131 if (resource->resourcePriv().getScratchKey().isValid() &&
132 !resource->getUniqueKey().isValid()) {
kkinnunen2e6055b2016-04-22 01:48:29 -0700133 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
bsalomon3582d3e2015-02-13 14:20:05 -0800134 fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
bsalomon744998e2014-08-28 09:54:34 -0700135 }
bsalomon9f2d1572015-02-17 11:47:40 -0800136
bsalomon71cb0c22014-11-14 12:10:14 -0800137 this->purgeAsNeeded();
bsalomonc8dc1f72014-08-21 13:02:13 -0700138}
139
bsalomon0ea80f42015-02-11 10:49:59 -0800140void GrResourceCache::removeResource(GrGpuResource* resource) {
bsalomon9f2d1572015-02-17 11:47:40 -0800141 this->validate();
bsalomon16961262014-08-26 14:01:07 -0700142 SkASSERT(this->isInCache(resource));
bsalomondace19e2014-11-17 07:34:06 -0800143
Derek Sollenbergeree479142017-05-24 11:41:33 -0400144 size_t size = resource->gpuMemorySize();
bsalomon9f2d1572015-02-17 11:47:40 -0800145 if (resource->isPurgeable()) {
146 fPurgeableQueue.remove(resource);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400147 fPurgeableBytes -= size;
bsalomonf320e042015-02-17 15:09:34 -0800148 } else {
149 this->removeFromNonpurgeableArray(resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800150 }
151
bsalomonf320e042015-02-17 15:09:34 -0800152 SkDEBUGCODE(--fCount;)
bsalomondace19e2014-11-17 07:34:06 -0800153 fBytes -= size;
bsalomon5ec26ae2016-02-25 08:33:02 -0800154 if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) {
bsalomondace19e2014-11-17 07:34:06 -0800155 --fBudgetedCount;
156 fBudgetedBytes -= size;
Brian Osman39c08ac2017-07-26 09:36:09 -0400157 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800158 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomondace19e2014-11-17 07:34:06 -0800159 }
160
robertphillipsc4ed6842016-05-24 14:17:12 -0700161 if (resource->resourcePriv().getScratchKey().isValid() &&
162 !resource->getUniqueKey().isValid()) {
bsalomon3582d3e2015-02-13 14:20:05 -0800163 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
bsalomon744998e2014-08-28 09:54:34 -0700164 }
bsalomon8718aaf2015-02-19 07:24:21 -0800165 if (resource->getUniqueKey().isValid()) {
166 fUniqueHash.remove(resource->getUniqueKey());
bsalomon8b79d232014-11-10 10:19:06 -0800167 }
bsalomonb436ed62014-11-17 12:15:56 -0800168 this->validate();
bsalomonc8dc1f72014-08-21 13:02:13 -0700169}
170
bsalomon0ea80f42015-02-11 10:49:59 -0800171void GrResourceCache::abandonAll() {
bsalomon71cb0c22014-11-14 12:10:14 -0800172 AutoValidate av(this);
173
bsalomonf320e042015-02-17 15:09:34 -0800174 while (fNonpurgeableResources.count()) {
175 GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
176 SkASSERT(!back->wasDestroyed());
177 back->cacheAccess().abandon();
bsalomonc8dc1f72014-08-21 13:02:13 -0700178 }
bsalomonf320e042015-02-17 15:09:34 -0800179
180 while (fPurgeableQueue.count()) {
181 GrGpuResource* top = fPurgeableQueue.peek();
182 SkASSERT(!top->wasDestroyed());
183 top->cacheAccess().abandon();
184 }
185
bsalomon744998e2014-08-28 09:54:34 -0700186 SkASSERT(!fScratchMap.count());
bsalomon8718aaf2015-02-19 07:24:21 -0800187 SkASSERT(!fUniqueHash.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700188 SkASSERT(!fCount);
bsalomonf320e042015-02-17 15:09:34 -0800189 SkASSERT(!this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800190 SkASSERT(!fBytes);
191 SkASSERT(!fBudgetedCount);
192 SkASSERT(!fBudgetedBytes);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400193 SkASSERT(!fPurgeableBytes);
bsalomonc8dc1f72014-08-21 13:02:13 -0700194}
195
bsalomon0ea80f42015-02-11 10:49:59 -0800196void GrResourceCache::releaseAll() {
bsalomon71cb0c22014-11-14 12:10:14 -0800197 AutoValidate av(this);
198
Brian Osman13dddce2017-05-09 13:19:50 -0400199 this->processFreedGpuResources();
200
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500201 SkASSERT(fProxyProvider); // better have called setProxyProvider
Robert Phillips3ec95732017-09-29 15:10:39 -0400202 // We must remove the uniqueKeys from the proxies here. While they possess a uniqueKey
203 // they also have a raw pointer back to this class (which is presumably going away)!
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500204 fProxyProvider->removeAllUniqueKeys();
Robert Phillips45a6f142017-09-29 09:49:41 -0400205
bsalomonf320e042015-02-17 15:09:34 -0800206 while(fNonpurgeableResources.count()) {
207 GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
208 SkASSERT(!back->wasDestroyed());
209 back->cacheAccess().release();
bsalomonc8dc1f72014-08-21 13:02:13 -0700210 }
bsalomonf320e042015-02-17 15:09:34 -0800211
212 while (fPurgeableQueue.count()) {
213 GrGpuResource* top = fPurgeableQueue.peek();
214 SkASSERT(!top->wasDestroyed());
215 top->cacheAccess().release();
216 }
217
bsalomon744998e2014-08-28 09:54:34 -0700218 SkASSERT(!fScratchMap.count());
bsalomon8718aaf2015-02-19 07:24:21 -0800219 SkASSERT(!fUniqueHash.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700220 SkASSERT(!fCount);
bsalomonf320e042015-02-17 15:09:34 -0800221 SkASSERT(!this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800222 SkASSERT(!fBytes);
223 SkASSERT(!fBudgetedCount);
224 SkASSERT(!fBudgetedBytes);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400225 SkASSERT(!fPurgeableBytes);
bsalomonc8dc1f72014-08-21 13:02:13 -0700226}
bsalomonbcf0a522014-10-08 08:40:09 -0700227
bsalomon0ea80f42015-02-11 10:49:59 -0800228class GrResourceCache::AvailableForScratchUse {
bsalomonbcf0a522014-10-08 08:40:09 -0700229public:
bsalomon000f8292014-10-15 19:04:14 -0700230 AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendingIO) { }
bsalomonbcf0a522014-10-08 08:40:09 -0700231
232 bool operator()(const GrGpuResource* resource) const {
robertphillipsc4ed6842016-05-24 14:17:12 -0700233 SkASSERT(!resource->getUniqueKey().isValid() &&
234 resource->resourcePriv().getScratchKey().isValid());
bsalomon12299ab2014-11-14 13:33:09 -0800235 if (resource->internalHasRef() || !resource->cacheAccess().isScratch()) {
bsalomon000f8292014-10-15 19:04:14 -0700236 return false;
bsalomonbcf0a522014-10-08 08:40:09 -0700237 }
bsalomon000f8292014-10-15 19:04:14 -0700238 return !fRejectPendingIO || !resource->internalHasPendingIO();
bsalomonbcf0a522014-10-08 08:40:09 -0700239 }
bsalomon1e2530b2014-10-09 09:57:18 -0700240
bsalomonbcf0a522014-10-08 08:40:09 -0700241private:
bsalomon000f8292014-10-15 19:04:14 -0700242 bool fRejectPendingIO;
bsalomonbcf0a522014-10-08 08:40:09 -0700243};
244
bsalomon0ea80f42015-02-11 10:49:59 -0800245GrGpuResource* GrResourceCache::findAndRefScratchResource(const GrScratchKey& scratchKey,
robertphillips6e83ac72015-08-13 05:19:14 -0700246 size_t resourceSize,
bsalomon9f2d1572015-02-17 11:47:40 -0800247 uint32_t flags) {
bsalomon7775c852014-12-30 12:50:52 -0800248 SkASSERT(scratchKey.isValid());
robertphillipsee843b22016-10-04 05:30:20 -0700249
bsalomon71cb0c22014-11-14 12:10:14 -0800250 GrGpuResource* resource;
bsalomon000f8292014-10-15 19:04:14 -0700251 if (flags & (kPreferNoPendingIO_ScratchFlag | kRequireNoPendingIO_ScratchFlag)) {
bsalomon71cb0c22014-11-14 12:10:14 -0800252 resource = fScratchMap.find(scratchKey, AvailableForScratchUse(true));
bsalomon000f8292014-10-15 19:04:14 -0700253 if (resource) {
bsalomon9f2d1572015-02-17 11:47:40 -0800254 this->refAndMakeResourceMRU(resource);
bsalomonb436ed62014-11-17 12:15:56 -0800255 this->validate();
256 return resource;
bsalomon000f8292014-10-15 19:04:14 -0700257 } else if (flags & kRequireNoPendingIO_ScratchFlag) {
halcanary96fcdcc2015-08-27 07:41:13 -0700258 return nullptr;
bsalomon000f8292014-10-15 19:04:14 -0700259 }
robertphillips63926682015-08-20 09:39:02 -0700260 // We would prefer to consume more available VRAM rather than flushing
261 // immediately, but on ANGLE this can lead to starving of the GPU.
262 if (fPreferVRAMUseOverFlushes && this->wouldFit(resourceSize)) {
robertphillips6e83ac72015-08-13 05:19:14 -0700263 // kPrefer is specified, we didn't find a resource without pending io,
robertphillips63926682015-08-20 09:39:02 -0700264 // but there is still space in our budget for the resource so force
265 // the caller to allocate a new resource.
halcanary96fcdcc2015-08-27 07:41:13 -0700266 return nullptr;
robertphillips6e83ac72015-08-13 05:19:14 -0700267 }
bsalomon000f8292014-10-15 19:04:14 -0700268 }
bsalomon71cb0c22014-11-14 12:10:14 -0800269 resource = fScratchMap.find(scratchKey, AvailableForScratchUse(false));
270 if (resource) {
bsalomon9f2d1572015-02-17 11:47:40 -0800271 this->refAndMakeResourceMRU(resource);
bsalomonb436ed62014-11-17 12:15:56 -0800272 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800273 }
274 return resource;
bsalomonbcf0a522014-10-08 08:40:09 -0700275}
bsalomon8b79d232014-11-10 10:19:06 -0800276
bsalomon0ea80f42015-02-11 10:49:59 -0800277void GrResourceCache::willRemoveScratchKey(const GrGpuResource* resource) {
bsalomon3582d3e2015-02-13 14:20:05 -0800278 SkASSERT(resource->resourcePriv().getScratchKey().isValid());
robertphillipsc4ed6842016-05-24 14:17:12 -0700279 if (!resource->getUniqueKey().isValid()) {
280 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
281 }
bsalomon10e23ca2014-11-25 05:52:06 -0800282}
283
bsalomonf99e9612015-02-19 08:24:16 -0800284void GrResourceCache::removeUniqueKey(GrGpuResource* resource) {
bsalomon3f324322015-04-08 11:01:54 -0700285 // Someone has a ref to this resource in order to have removed the key. When the ref count
286 // reaches zero we will get a ref cnt notification and figure out what to do with it.
bsalomonf99e9612015-02-19 08:24:16 -0800287 if (resource->getUniqueKey().isValid()) {
288 SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
289 fUniqueHash.remove(resource->getUniqueKey());
290 }
291 resource->cacheAccess().removeUniqueKey();
robertphillipsc4ed6842016-05-24 14:17:12 -0700292
293 if (resource->resourcePriv().getScratchKey().isValid()) {
294 fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
295 }
296
bsalomonf99e9612015-02-19 08:24:16 -0800297 this->validate();
bsalomon23e619c2015-02-06 11:54:28 -0800298}
299
bsalomonf99e9612015-02-19 08:24:16 -0800300void GrResourceCache::changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) {
bsalomon8b79d232014-11-10 10:19:06 -0800301 SkASSERT(resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800302 SkASSERT(this->isInCache(resource));
bsalomon8b79d232014-11-10 10:19:06 -0800303
bsalomonf99e9612015-02-19 08:24:16 -0800304 // If another resource has the new key, remove its key then install the key on this resource.
305 if (newKey.isValid()) {
Greg Daniel0d537802017-09-08 11:44:14 -0400306 if (GrGpuResource* old = fUniqueHash.find(newKey)) {
307 // If the old resource using the key is purgeable and is unreachable, then remove it.
308 if (!old->resourcePriv().getScratchKey().isValid() && old->isPurgeable()) {
309 old->cacheAccess().release();
310 } else {
311 this->removeUniqueKey(old);
312 }
313 }
314 SkASSERT(nullptr == fUniqueHash.find(newKey));
315
robertphillipsc4ed6842016-05-24 14:17:12 -0700316 // Remove the entry for this resource if it already has a unique key.
317 if (resource->getUniqueKey().isValid()) {
318 SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
319 fUniqueHash.remove(resource->getUniqueKey());
320 SkASSERT(nullptr == fUniqueHash.find(resource->getUniqueKey()));
321 } else {
322 // 'resource' didn't have a valid unique key before so it is switching sides. Remove it
323 // from the ScratchMap
324 if (resource->resourcePriv().getScratchKey().isValid()) {
325 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
326 }
327 }
328
bsalomonf99e9612015-02-19 08:24:16 -0800329 resource->cacheAccess().setUniqueKey(newKey);
330 fUniqueHash.add(resource);
331 } else {
robertphillipsc4ed6842016-05-24 14:17:12 -0700332 this->removeUniqueKey(resource);
bsalomonf99e9612015-02-19 08:24:16 -0800333 }
334
bsalomon71cb0c22014-11-14 12:10:14 -0800335 this->validate();
bsalomon8b79d232014-11-10 10:19:06 -0800336}
bsalomon71cb0c22014-11-14 12:10:14 -0800337
bsalomon9f2d1572015-02-17 11:47:40 -0800338void GrResourceCache::refAndMakeResourceMRU(GrGpuResource* resource) {
bsalomon71cb0c22014-11-14 12:10:14 -0800339 SkASSERT(resource);
340 SkASSERT(this->isInCache(resource));
bsalomonddf30e62015-02-19 11:38:44 -0800341
bsalomon9f2d1572015-02-17 11:47:40 -0800342 if (resource->isPurgeable()) {
343 // It's about to become unpurgeable.
Derek Sollenbergeree479142017-05-24 11:41:33 -0400344 fPurgeableBytes -= resource->gpuMemorySize();
bsalomon9f2d1572015-02-17 11:47:40 -0800345 fPurgeableQueue.remove(resource);
bsalomonf320e042015-02-17 15:09:34 -0800346 this->addToNonpurgeableArray(resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800347 }
348 resource->ref();
bsalomonddf30e62015-02-19 11:38:44 -0800349
350 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
bsalomonf320e042015-02-17 15:09:34 -0800351 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800352}
353
bsalomon3f324322015-04-08 11:01:54 -0700354void GrResourceCache::notifyCntReachedZero(GrGpuResource* resource, uint32_t flags) {
bsalomon71cb0c22014-11-14 12:10:14 -0800355 SkASSERT(resource);
bsalomon3f324322015-04-08 11:01:54 -0700356 SkASSERT(!resource->wasDestroyed());
357 SkASSERT(flags);
bsalomon71cb0c22014-11-14 12:10:14 -0800358 SkASSERT(this->isInCache(resource));
bsalomon3f324322015-04-08 11:01:54 -0700359 // This resource should always be in the nonpurgeable array when this function is called. It
360 // will be moved to the queue if it is newly purgeable.
361 SkASSERT(fNonpurgeableResources[*resource->cacheAccess().accessCacheIndex()] == resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800362
bsalomon3f324322015-04-08 11:01:54 -0700363 if (SkToBool(ResourceAccess::kRefCntReachedZero_RefNotificationFlag & flags)) {
364#ifdef SK_DEBUG
365 // When the timestamp overflows validate() is called. validate() checks that resources in
366 // the nonpurgeable array are indeed not purgeable. However, the movement from the array to
367 // the purgeable queue happens just below in this function. So we mark it as an exception.
368 if (resource->isPurgeable()) {
369 fNewlyPurgeableResourceForValidation = resource;
370 }
371#endif
372 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
halcanary96fcdcc2015-08-27 07:41:13 -0700373 SkDEBUGCODE(fNewlyPurgeableResourceForValidation = nullptr);
bsalomon3f324322015-04-08 11:01:54 -0700374 }
375
376 if (!SkToBool(ResourceAccess::kAllCntsReachedZero_RefNotificationFlag & flags)) {
377 SkASSERT(!resource->isPurgeable());
378 return;
379 }
380
381 SkASSERT(resource->isPurgeable());
bsalomonf320e042015-02-17 15:09:34 -0800382 this->removeFromNonpurgeableArray(resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800383 fPurgeableQueue.insert(resource);
bsalomone2e87f32016-09-22 12:42:11 -0700384 resource->cacheAccess().setFlushCntWhenResourceBecamePurgeable(fExternalFlushCnt);
Brian Salomon5e150852017-03-22 14:53:13 -0400385 resource->cacheAccess().setTimeWhenResourceBecomePurgeable();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400386 fPurgeableBytes += resource->gpuMemorySize();
bsalomon71cb0c22014-11-14 12:10:14 -0800387
bsalomon5ec26ae2016-02-25 08:33:02 -0800388 if (SkBudgeted::kNo == resource->resourcePriv().isBudgeted()) {
bsalomonc2f35b72015-01-23 07:19:22 -0800389 // Check whether this resource could still be used as a scratch resource.
kkinnunen2e6055b2016-04-22 01:48:29 -0700390 if (!resource->resourcePriv().refsWrappedObjects() &&
bsalomon9f2d1572015-02-17 11:47:40 -0800391 resource->resourcePriv().getScratchKey().isValid()) {
bsalomonc2f35b72015-01-23 07:19:22 -0800392 // We won't purge an existing resource to make room for this one.
bsalomonf320e042015-02-17 15:09:34 -0800393 if (fBudgetedCount < fMaxCount &&
394 fBudgetedBytes + resource->gpuMemorySize() <= fMaxBytes) {
bsalomon3582d3e2015-02-13 14:20:05 -0800395 resource->resourcePriv().makeBudgeted();
bsalomon9f2d1572015-02-17 11:47:40 -0800396 return;
bsalomonc2f35b72015-01-23 07:19:22 -0800397 }
bsalomonc2f35b72015-01-23 07:19:22 -0800398 }
399 } else {
bsalomon9f2d1572015-02-17 11:47:40 -0800400 // Purge the resource immediately if we're over budget
bsalomon8718aaf2015-02-19 07:24:21 -0800401 // Also purge if the resource has neither a valid scratch key nor a unique key.
robertphillipsee843b22016-10-04 05:30:20 -0700402 bool noKey = !resource->resourcePriv().getScratchKey().isValid() &&
403 !resource->getUniqueKey().isValid();
404 if (!this->overBudget() && !noKey) {
bsalomon9f2d1572015-02-17 11:47:40 -0800405 return;
bsalomonc2f35b72015-01-23 07:19:22 -0800406 }
407 }
bsalomondace19e2014-11-17 07:34:06 -0800408
bsalomonf320e042015-02-17 15:09:34 -0800409 SkDEBUGCODE(int beforeCount = this->getResourceCount();)
bsalomon9f2d1572015-02-17 11:47:40 -0800410 resource->cacheAccess().release();
411 // We should at least free this resource, perhaps dependent resources as well.
bsalomonf320e042015-02-17 15:09:34 -0800412 SkASSERT(this->getResourceCount() < beforeCount);
bsalomon71cb0c22014-11-14 12:10:14 -0800413 this->validate();
414}
415
bsalomon0ea80f42015-02-11 10:49:59 -0800416void GrResourceCache::didChangeBudgetStatus(GrGpuResource* resource) {
bsalomon84c8e622014-11-17 09:33:27 -0800417 SkASSERT(resource);
418 SkASSERT(this->isInCache(resource));
419
420 size_t size = resource->gpuMemorySize();
421
bsalomon5ec26ae2016-02-25 08:33:02 -0800422 if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) {
bsalomon84c8e622014-11-17 09:33:27 -0800423 ++fBudgetedCount;
424 fBudgetedBytes += size;
bsalomonafe30052015-01-16 07:32:33 -0800425#if GR_CACHE_STATS
426 fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes);
427 fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount);
428#endif
bsalomon84c8e622014-11-17 09:33:27 -0800429 this->purgeAsNeeded();
430 } else {
431 --fBudgetedCount;
432 fBudgetedBytes -= size;
433 }
Brian Osman39c08ac2017-07-26 09:36:09 -0400434 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800435 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomon84c8e622014-11-17 09:33:27 -0800436
437 this->validate();
438}
439
robertphillipsee843b22016-10-04 05:30:20 -0700440void GrResourceCache::purgeAsNeeded() {
bsalomon3f324322015-04-08 11:01:54 -0700441 SkTArray<GrUniqueKeyInvalidatedMessage> invalidKeyMsgs;
442 fInvalidUniqueKeyInbox.poll(&invalidKeyMsgs);
443 if (invalidKeyMsgs.count()) {
444 this->processInvalidUniqueKeys(invalidKeyMsgs);
445 }
bsalomon71cb0c22014-11-14 12:10:14 -0800446
Brian Osman13dddce2017-05-09 13:19:50 -0400447 this->processFreedGpuResources();
448
bsalomone2e87f32016-09-22 12:42:11 -0700449 if (fMaxUnusedFlushes > 0) {
450 // We want to know how many complete flushes have occurred without the resource being used.
451 // If the resource was tagged when fExternalFlushCnt was N then this means it became
452 // purgeable during activity that became the N+1th flush. So when the flush count is N+2
453 // it has sat in the purgeable queue for one entire flush.
454 uint32_t oldestAllowedFlushCnt = fExternalFlushCnt - fMaxUnusedFlushes - 1;
455 // check for underflow
456 if (oldestAllowedFlushCnt < fExternalFlushCnt) {
457 while (fPurgeableQueue.count()) {
458 uint32_t flushWhenResourceBecamePurgeable =
459 fPurgeableQueue.peek()->cacheAccess().flushCntWhenResourceBecamePurgeable();
460 if (oldestAllowedFlushCnt < flushWhenResourceBecamePurgeable) {
461 // Resources were given both LRU timestamps and tagged with a flush cnt when
462 // they first became purgeable. The LRU timestamp won't change again until the
463 // resource is made non-purgeable again. So, at this point all the remaining
464 // resources in the timestamp-sorted queue will have a flush count >= to this
465 // one.
466 break;
467 }
468 GrGpuResource* resource = fPurgeableQueue.peek();
469 SkASSERT(resource->isPurgeable());
470 resource->cacheAccess().release();
bsalomon3f324322015-04-08 11:01:54 -0700471 }
bsalomon3f324322015-04-08 11:01:54 -0700472 }
473 }
474
475 bool stillOverbudget = this->overBudget();
476 while (stillOverbudget && fPurgeableQueue.count()) {
robertphillipsee843b22016-10-04 05:30:20 -0700477 GrGpuResource* resource = fPurgeableQueue.peek();
bsalomon9f2d1572015-02-17 11:47:40 -0800478 SkASSERT(resource->isPurgeable());
479 resource->cacheAccess().release();
bsalomon3f324322015-04-08 11:01:54 -0700480 stillOverbudget = this->overBudget();
bsalomon9f2d1572015-02-17 11:47:40 -0800481 }
bsalomon71cb0c22014-11-14 12:10:14 -0800482
bsalomonb436ed62014-11-17 12:15:56 -0800483 this->validate();
robertphillipsee843b22016-10-04 05:30:20 -0700484
485 if (stillOverbudget) {
486 // Set this so that GrDrawingManager will issue a flush to free up resources with pending
487 // IO that we were unable to purge in this pass.
488 fRequestFlush = true;
489 }
bsalomon71cb0c22014-11-14 12:10:14 -0800490}
491
Robert Phillips6eba0632018-03-28 12:25:42 -0400492void GrResourceCache::purgeUnlockedResources(bool scratchResourcesOnly) {
493 if (!scratchResourcesOnly) {
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 SkASSERT(resource->isPurgeable());
499 resource->cacheAccess().release();
500 }
501 } else {
502 // Sort the queue
503 fPurgeableQueue.sort();
504
505 // Make a list of the scratch resources to delete
506 SkTDArray<GrGpuResource*> scratchResources;
507 for (int i = 0; i < fPurgeableQueue.count(); i++) {
508 GrGpuResource* resource = fPurgeableQueue.at(i);
509 SkASSERT(resource->isPurgeable());
510 if (!resource->getUniqueKey().isValid()) {
511 *scratchResources.append() = resource;
512 }
513 }
514
515 // Delete the scratch resources. This must be done as a separate pass
516 // to avoid messing up the sorted order of the queue
517 for (int i = 0; i < scratchResources.count(); i++) {
518 scratchResources.getAt(i)->cacheAccess().release();
519 }
bsalomon9f2d1572015-02-17 11:47:40 -0800520 }
bsalomon71cb0c22014-11-14 12:10:14 -0800521
bsalomonb436ed62014-11-17 12:15:56 -0800522 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800523}
524
Brian Salomon5e150852017-03-22 14:53:13 -0400525void GrResourceCache::purgeResourcesNotUsedSince(GrStdSteadyClock::time_point purgeTime) {
526 while (fPurgeableQueue.count()) {
527 const GrStdSteadyClock::time_point resourceTime =
528 fPurgeableQueue.peek()->cacheAccess().timeWhenResourceBecamePurgeable();
529 if (resourceTime >= purgeTime) {
530 // Resources were given both LRU timestamps and tagged with a frame number when
531 // they first became purgeable. The LRU timestamp won't change again until the
532 // resource is made non-purgeable again. So, at this point all the remaining
533 // resources in the timestamp-sorted queue will have a frame number >= to this
534 // one.
535 break;
536 }
537 GrGpuResource* resource = fPurgeableQueue.peek();
538 SkASSERT(resource->isPurgeable());
539 resource->cacheAccess().release();
540 }
541}
542
Derek Sollenberger5480a182017-05-25 16:43:59 -0400543void GrResourceCache::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
544
545 const size_t tmpByteBudget = SkTMax((size_t)0, fBytes - bytesToPurge);
546 bool stillOverbudget = tmpByteBudget < fBytes;
547
548 if (preferScratchResources && bytesToPurge < fPurgeableBytes) {
549 // Sort the queue
550 fPurgeableQueue.sort();
551
552 // Make a list of the scratch resources to delete
553 SkTDArray<GrGpuResource*> scratchResources;
554 size_t scratchByteCount = 0;
555 for (int i = 0; i < fPurgeableQueue.count() && stillOverbudget; i++) {
556 GrGpuResource* resource = fPurgeableQueue.at(i);
557 SkASSERT(resource->isPurgeable());
558 if (!resource->getUniqueKey().isValid()) {
559 *scratchResources.append() = resource;
560 scratchByteCount += resource->gpuMemorySize();
561 stillOverbudget = tmpByteBudget < fBytes - scratchByteCount;
562 }
563 }
564
565 // Delete the scratch resources. This must be done as a separate pass
566 // to avoid messing up the sorted order of the queue
567 for (int i = 0; i < scratchResources.count(); i++) {
568 scratchResources.getAt(i)->cacheAccess().release();
569 }
570 stillOverbudget = tmpByteBudget < fBytes;
571
572 this->validate();
573 }
574
575 // Purge any remaining resources in LRU order
576 if (stillOverbudget) {
577 const size_t cachedByteCount = fMaxBytes;
578 fMaxBytes = tmpByteBudget;
579 this->purgeAsNeeded();
580 fMaxBytes = cachedByteCount;
581 }
582}
583
bsalomon8718aaf2015-02-19 07:24:21 -0800584void GrResourceCache::processInvalidUniqueKeys(
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500585 const SkTArray<GrUniqueKeyInvalidatedMessage>& msgs) {
586 SkASSERT(fProxyProvider); // better have called setProxyProvider
587
bsalomon23e619c2015-02-06 11:54:28 -0800588 for (int i = 0; i < msgs.count(); ++i) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500589 fProxyProvider->processInvalidProxyUniqueKey(msgs[i].key());
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400590
bsalomon8718aaf2015-02-19 07:24:21 -0800591 GrGpuResource* resource = this->findAndRefUniqueResource(msgs[i].key());
bsalomon23e619c2015-02-06 11:54:28 -0800592 if (resource) {
bsalomon8718aaf2015-02-19 07:24:21 -0800593 resource->resourcePriv().removeUniqueKey();
bsalomon3f324322015-04-08 11:01:54 -0700594 resource->unref(); // If this resource is now purgeable, the cache will be notified.
bsalomon23e619c2015-02-06 11:54:28 -0800595 }
596 }
597}
598
Brian Osman13dddce2017-05-09 13:19:50 -0400599void GrResourceCache::insertCrossContextGpuResource(GrGpuResource* resource) {
600 resource->ref();
601}
602
603void GrResourceCache::processFreedGpuResources() {
604 SkTArray<GrGpuResourceFreedMessage> msgs;
605 fFreedGpuResourceInbox.poll(&msgs);
606 for (int i = 0; i < msgs.count(); ++i) {
607 if (msgs[i].fOwningUniqueID == fContextUniqueID) {
608 msgs[i].fResource->unref();
609 }
610 }
611}
612
bsalomonf320e042015-02-17 15:09:34 -0800613void GrResourceCache::addToNonpurgeableArray(GrGpuResource* resource) {
614 int index = fNonpurgeableResources.count();
615 *fNonpurgeableResources.append() = resource;
616 *resource->cacheAccess().accessCacheIndex() = index;
617}
618
619void GrResourceCache::removeFromNonpurgeableArray(GrGpuResource* resource) {
620 int* index = resource->cacheAccess().accessCacheIndex();
621 // Fill the whole we will create in the array with the tail object, adjust its index, and
622 // then pop the array
623 GrGpuResource* tail = *(fNonpurgeableResources.end() - 1);
624 SkASSERT(fNonpurgeableResources[*index] == resource);
625 fNonpurgeableResources[*index] = tail;
626 *tail->cacheAccess().accessCacheIndex() = *index;
627 fNonpurgeableResources.pop();
628 SkDEBUGCODE(*index = -1);
629}
630
bsalomonddf30e62015-02-19 11:38:44 -0800631uint32_t GrResourceCache::getNextTimestamp() {
632 // If we wrap then all the existing resources will appear older than any resources that get
633 // a timestamp after the wrap.
634 if (0 == fTimestamp) {
635 int count = this->getResourceCount();
636 if (count) {
637 // Reset all the timestamps. We sort the resources by timestamp and then assign
638 // sequential timestamps beginning with 0. This is O(n*lg(n)) but it should be extremely
639 // rare.
640 SkTDArray<GrGpuResource*> sortedPurgeableResources;
641 sortedPurgeableResources.setReserve(fPurgeableQueue.count());
642
643 while (fPurgeableQueue.count()) {
644 *sortedPurgeableResources.append() = fPurgeableQueue.peek();
645 fPurgeableQueue.pop();
646 }
robertphillipsee843b22016-10-04 05:30:20 -0700647
bsalomone2e87f32016-09-22 12:42:11 -0700648 SkTQSort(fNonpurgeableResources.begin(), fNonpurgeableResources.end() - 1,
649 CompareTimestamp);
bsalomonddf30e62015-02-19 11:38:44 -0800650
651 // Pick resources out of the purgeable and non-purgeable arrays based on lowest
652 // timestamp and assign new timestamps.
653 int currP = 0;
654 int currNP = 0;
655 while (currP < sortedPurgeableResources.count() &&
mtklein56da0252015-11-16 11:16:23 -0800656 currNP < fNonpurgeableResources.count()) {
bsalomonddf30e62015-02-19 11:38:44 -0800657 uint32_t tsP = sortedPurgeableResources[currP]->cacheAccess().timestamp();
658 uint32_t tsNP = fNonpurgeableResources[currNP]->cacheAccess().timestamp();
659 SkASSERT(tsP != tsNP);
660 if (tsP < tsNP) {
661 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
662 } else {
663 // Correct the index in the nonpurgeable array stored on the resource post-sort.
664 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
665 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
666 }
667 }
668
669 // The above loop ended when we hit the end of one array. Finish the other one.
670 while (currP < sortedPurgeableResources.count()) {
671 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
672 }
673 while (currNP < fNonpurgeableResources.count()) {
674 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
675 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
676 }
677
678 // Rebuild the queue.
679 for (int i = 0; i < sortedPurgeableResources.count(); ++i) {
680 fPurgeableQueue.insert(sortedPurgeableResources[i]);
681 }
682
683 this->validate();
684 SkASSERT(count == this->getResourceCount());
685
686 // count should be the next timestamp we return.
687 SkASSERT(fTimestamp == SkToU32(count));
mtklein56da0252015-11-16 11:16:23 -0800688 }
bsalomonddf30e62015-02-19 11:38:44 -0800689 }
690 return fTimestamp++;
691}
692
bsalomonb77a9072016-09-07 10:02:04 -0700693void GrResourceCache::notifyFlushOccurred(FlushType type) {
694 switch (type) {
bsalomonb77a9072016-09-07 10:02:04 -0700695 case FlushType::kCacheRequested:
robertphillipsee843b22016-10-04 05:30:20 -0700696 SkASSERT(fRequestFlush);
697 fRequestFlush = false;
bsalomonb77a9072016-09-07 10:02:04 -0700698 break;
robertphillipsee843b22016-10-04 05:30:20 -0700699 case FlushType::kExternal:
bsalomone2e87f32016-09-22 12:42:11 -0700700 ++fExternalFlushCnt;
701 if (0 == fExternalFlushCnt) {
702 // When this wraps just reset all the purgeable resources' last used flush state.
703 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
704 fPurgeableQueue.at(i)->cacheAccess().setFlushCntWhenResourceBecamePurgeable(0);
705 }
bsalomonb77a9072016-09-07 10:02:04 -0700706 }
707 break;
bsalomon3f324322015-04-08 11:01:54 -0700708 }
robertphillipsee843b22016-10-04 05:30:20 -0700709 this->purgeAsNeeded();
bsalomon3f324322015-04-08 11:01:54 -0700710}
711
ericrk0a5fa482015-09-15 14:16:10 -0700712void GrResourceCache::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
713 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
714 fNonpurgeableResources[i]->dumpMemoryStatistics(traceMemoryDump);
715 }
716 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
717 fPurgeableQueue.at(i)->dumpMemoryStatistics(traceMemoryDump);
718 }
719}
720
bsalomon71cb0c22014-11-14 12:10:14 -0800721#ifdef SK_DEBUG
bsalomon0ea80f42015-02-11 10:49:59 -0800722void GrResourceCache::validate() const {
bsalomonc2f35b72015-01-23 07:19:22 -0800723 // Reduce the frequency of validations for large resource counts.
724 static SkRandom gRandom;
725 int mask = (SkNextPow2(fCount + 1) >> 5) - 1;
726 if (~mask && (gRandom.nextU() & mask)) {
727 return;
728 }
729
bsalomonf320e042015-02-17 15:09:34 -0800730 struct Stats {
731 size_t fBytes;
732 int fBudgetedCount;
733 size_t fBudgetedBytes;
734 int fLocked;
735 int fScratch;
736 int fCouldBeScratch;
737 int fContent;
738 const ScratchMap* fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800739 const UniqueHash* fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800740
bsalomonf320e042015-02-17 15:09:34 -0800741 Stats(const GrResourceCache* cache) {
742 memset(this, 0, sizeof(*this));
743 fScratchMap = &cache->fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800744 fUniqueHash = &cache->fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800745 }
746
bsalomonf320e042015-02-17 15:09:34 -0800747 void update(GrGpuResource* resource) {
748 fBytes += resource->gpuMemorySize();
bsalomondace19e2014-11-17 07:34:06 -0800749
bsalomonf320e042015-02-17 15:09:34 -0800750 if (!resource->isPurgeable()) {
751 ++fLocked;
752 }
bsalomon9f2d1572015-02-17 11:47:40 -0800753
robertphillipsc4ed6842016-05-24 14:17:12 -0700754 const GrScratchKey& scratchKey = resource->resourcePriv().getScratchKey();
755 const GrUniqueKey& uniqueKey = resource->getUniqueKey();
756
bsalomonf320e042015-02-17 15:09:34 -0800757 if (resource->cacheAccess().isScratch()) {
robertphillipsc4ed6842016-05-24 14:17:12 -0700758 SkASSERT(!uniqueKey.isValid());
bsalomonf320e042015-02-17 15:09:34 -0800759 ++fScratch;
robertphillipsc4ed6842016-05-24 14:17:12 -0700760 SkASSERT(fScratchMap->countForKey(scratchKey));
kkinnunen2e6055b2016-04-22 01:48:29 -0700761 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
robertphillipsc4ed6842016-05-24 14:17:12 -0700762 } else if (scratchKey.isValid()) {
bsalomon5ec26ae2016-02-25 08:33:02 -0800763 SkASSERT(SkBudgeted::kNo == resource->resourcePriv().isBudgeted() ||
robertphillipsc4ed6842016-05-24 14:17:12 -0700764 uniqueKey.isValid());
765 if (!uniqueKey.isValid()) {
mtklein4e976072016-08-08 09:06:27 -0700766 ++fCouldBeScratch;
robertphillipsc4ed6842016-05-24 14:17:12 -0700767 SkASSERT(fScratchMap->countForKey(scratchKey));
768 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700769 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
bsalomonf320e042015-02-17 15:09:34 -0800770 }
bsalomon8718aaf2015-02-19 07:24:21 -0800771 if (uniqueKey.isValid()) {
bsalomonf320e042015-02-17 15:09:34 -0800772 ++fContent;
bsalomon8718aaf2015-02-19 07:24:21 -0800773 SkASSERT(fUniqueHash->find(uniqueKey) == resource);
Brian Osman0562eb92017-05-08 11:16:39 -0400774 SkASSERT(SkBudgeted::kYes == resource->resourcePriv().isBudgeted() ||
775 resource->resourcePriv().refsWrappedObjects());
robertphillipsc4ed6842016-05-24 14:17:12 -0700776
777 if (scratchKey.isValid()) {
778 SkASSERT(!fScratchMap->has(resource, scratchKey));
779 }
bsalomonf320e042015-02-17 15:09:34 -0800780 }
781
bsalomon5ec26ae2016-02-25 08:33:02 -0800782 if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) {
bsalomonf320e042015-02-17 15:09:34 -0800783 ++fBudgetedCount;
784 fBudgetedBytes += resource->gpuMemorySize();
785 }
bsalomon9f2d1572015-02-17 11:47:40 -0800786 }
bsalomonf320e042015-02-17 15:09:34 -0800787 };
788
robertphillipsc4ed6842016-05-24 14:17:12 -0700789 {
790 ScratchMap::ConstIter iter(&fScratchMap);
791
792 int count = 0;
793 for ( ; !iter.done(); ++iter) {
794 const GrGpuResource* resource = *iter;
795 SkASSERT(resource->resourcePriv().getScratchKey().isValid());
796 SkASSERT(!resource->getUniqueKey().isValid());
797 count++;
798 }
799 SkASSERT(count == fScratchMap.count()); // ensure the iterator is working correctly
800 }
801
bsalomonf320e042015-02-17 15:09:34 -0800802 Stats stats(this);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400803 size_t purgeableBytes = 0;
bsalomonf320e042015-02-17 15:09:34 -0800804
805 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
bsalomon3f324322015-04-08 11:01:54 -0700806 SkASSERT(!fNonpurgeableResources[i]->isPurgeable() ||
807 fNewlyPurgeableResourceForValidation == fNonpurgeableResources[i]);
bsalomonf320e042015-02-17 15:09:34 -0800808 SkASSERT(*fNonpurgeableResources[i]->cacheAccess().accessCacheIndex() == i);
809 SkASSERT(!fNonpurgeableResources[i]->wasDestroyed());
810 stats.update(fNonpurgeableResources[i]);
bsalomon71cb0c22014-11-14 12:10:14 -0800811 }
bsalomon9f2d1572015-02-17 11:47:40 -0800812 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
813 SkASSERT(fPurgeableQueue.at(i)->isPurgeable());
bsalomonf320e042015-02-17 15:09:34 -0800814 SkASSERT(*fPurgeableQueue.at(i)->cacheAccess().accessCacheIndex() == i);
815 SkASSERT(!fPurgeableQueue.at(i)->wasDestroyed());
816 stats.update(fPurgeableQueue.at(i));
Derek Sollenbergeree479142017-05-24 11:41:33 -0400817 purgeableBytes += fPurgeableQueue.at(i)->gpuMemorySize();
bsalomon9f2d1572015-02-17 11:47:40 -0800818 }
819
bsalomonf320e042015-02-17 15:09:34 -0800820 SkASSERT(fCount == this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800821 SkASSERT(fBudgetedCount <= fCount);
bsalomonf320e042015-02-17 15:09:34 -0800822 SkASSERT(fBudgetedBytes <= fBytes);
823 SkASSERT(stats.fBytes == fBytes);
824 SkASSERT(stats.fBudgetedBytes == fBudgetedBytes);
825 SkASSERT(stats.fBudgetedCount == fBudgetedCount);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400826 SkASSERT(purgeableBytes == fPurgeableBytes);
bsalomon71cb0c22014-11-14 12:10:14 -0800827#if GR_CACHE_STATS
bsalomondace19e2014-11-17 07:34:06 -0800828 SkASSERT(fBudgetedHighWaterCount <= fHighWaterCount);
829 SkASSERT(fBudgetedHighWaterBytes <= fHighWaterBytes);
bsalomonf320e042015-02-17 15:09:34 -0800830 SkASSERT(fBytes <= fHighWaterBytes);
831 SkASSERT(fCount <= fHighWaterCount);
832 SkASSERT(fBudgetedBytes <= fBudgetedHighWaterBytes);
833 SkASSERT(fBudgetedCount <= fBudgetedHighWaterCount);
bsalomon71cb0c22014-11-14 12:10:14 -0800834#endif
bsalomon8718aaf2015-02-19 07:24:21 -0800835 SkASSERT(stats.fContent == fUniqueHash.count());
bsalomonf320e042015-02-17 15:09:34 -0800836 SkASSERT(stats.fScratch + stats.fCouldBeScratch == fScratchMap.count());
bsalomon71cb0c22014-11-14 12:10:14 -0800837
bsalomon3f324322015-04-08 11:01:54 -0700838 // This assertion is not currently valid because we can be in recursive notifyCntReachedZero()
bsalomon12299ab2014-11-14 13:33:09 -0800839 // calls. This will be fixed when subresource registration is explicit.
bsalomondace19e2014-11-17 07:34:06 -0800840 // bool overBudget = budgetedBytes > fMaxBytes || budgetedCount > fMaxCount;
bsalomon12299ab2014-11-14 13:33:09 -0800841 // SkASSERT(!overBudget || locked == count || fPurging);
bsalomon71cb0c22014-11-14 12:10:14 -0800842}
bsalomonf320e042015-02-17 15:09:34 -0800843
844bool GrResourceCache::isInCache(const GrGpuResource* resource) const {
845 int index = *resource->cacheAccess().accessCacheIndex();
846 if (index < 0) {
847 return false;
848 }
849 if (index < fPurgeableQueue.count() && fPurgeableQueue.at(index) == resource) {
850 return true;
851 }
852 if (index < fNonpurgeableResources.count() && fNonpurgeableResources[index] == resource) {
853 return true;
854 }
855 SkDEBUGFAIL("Resource index should be -1 or the resource should be in the cache.");
856 return false;
857}
858
bsalomon71cb0c22014-11-14 12:10:14 -0800859#endif