blob: 53a62c05c0cb7b2e6b9ad25e18be36bec59cddce [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
8
bsalomon0ea80f42015-02-11 10:49:59 -08009#include "GrResourceCache.h"
robertphillips28a838e2016-06-23 14:07:00 -070010
11#include "GrCaps.h"
bsalomon3582d3e2015-02-13 14:20:05 -080012#include "GrGpuResourceCacheAccess.h"
hendrikw876c3132015-03-04 10:33:49 -080013#include "GrTracing.h"
bsalomon71cb0c22014-11-14 12:10:14 -080014#include "SkGr.h"
15#include "SkMessageBus.h"
mtklein4e976072016-08-08 09:06:27 -070016#include "SkOpts.h"
bsalomonddf30e62015-02-19 11:38:44 -080017#include "SkTSort.h"
bsalomon71cb0c22014-11-14 12:10:14 -080018
bsalomon8718aaf2015-02-19 07:24:21 -080019DECLARE_SKMESSAGEBUS_MESSAGE(GrUniqueKeyInvalidatedMessage);
bsalomon71cb0c22014-11-14 12:10:14 -080020
Brian Osman13dddce2017-05-09 13:19:50 -040021DECLARE_SKMESSAGEBUS_MESSAGE(GrGpuResourceFreedMessage);
22
bsalomon71cb0c22014-11-14 12:10:14 -080023//////////////////////////////////////////////////////////////////////////////
24
bsalomon7775c852014-12-30 12:50:52 -080025GrScratchKey::ResourceType GrScratchKey::GenerateResourceType() {
bsalomon24db3b12015-01-23 04:24:04 -080026 static int32_t gType = INHERITED::kInvalidDomain + 1;
bsalomonfe369ee2014-11-10 11:59:06 -080027
bsalomon7775c852014-12-30 12:50:52 -080028 int32_t type = sk_atomic_inc(&gType);
robertphillips9790a7b2015-01-05 12:29:15 -080029 if (type > SK_MaxU16) {
bsalomon71cb0c22014-11-14 12:10:14 -080030 SkFAIL("Too many Resource Types");
31 }
32
33 return static_cast<ResourceType>(type);
34}
35
bsalomon8718aaf2015-02-19 07:24:21 -080036GrUniqueKey::Domain GrUniqueKey::GenerateDomain() {
bsalomon24db3b12015-01-23 04:24:04 -080037 static int32_t gDomain = INHERITED::kInvalidDomain + 1;
bsalomon7775c852014-12-30 12:50:52 -080038
bsalomon24db3b12015-01-23 04:24:04 -080039 int32_t domain = sk_atomic_inc(&gDomain);
kkinnunen016dffb2015-01-23 06:43:05 -080040 if (domain > SK_MaxU16) {
bsalomon8718aaf2015-02-19 07:24:21 -080041 SkFAIL("Too many GrUniqueKey Domains");
bsalomon7775c852014-12-30 12:50:52 -080042 }
bsalomon24db3b12015-01-23 04:24:04 -080043
44 return static_cast<Domain>(domain);
45}
bsalomon3f324322015-04-08 11:01:54 -070046
bsalomon24db3b12015-01-23 04:24:04 -080047uint32_t GrResourceKeyHash(const uint32_t* data, size_t size) {
mtklein4e976072016-08-08 09:06:27 -070048 return SkOpts::hash(data, size);
bsalomon7775c852014-12-30 12:50:52 -080049}
50
bsalomonfe369ee2014-11-10 11:59:06 -080051//////////////////////////////////////////////////////////////////////////////
52
bsalomon0ea80f42015-02-11 10:49:59 -080053class GrResourceCache::AutoValidate : ::SkNoncopyable {
bsalomon71cb0c22014-11-14 12:10:14 -080054public:
bsalomon0ea80f42015-02-11 10:49:59 -080055 AutoValidate(GrResourceCache* cache) : fCache(cache) { cache->validate(); }
bsalomon71cb0c22014-11-14 12:10:14 -080056 ~AutoValidate() { fCache->validate(); }
57private:
bsalomon0ea80f42015-02-11 10:49:59 -080058 GrResourceCache* fCache;
bsalomon71cb0c22014-11-14 12:10:14 -080059};
60
61 //////////////////////////////////////////////////////////////////////////////
robertphillipsee843b22016-10-04 05:30:20 -070062
bsalomon71cb0c22014-11-14 12:10:14 -080063
Brian Osman13dddce2017-05-09 13:19:50 -040064GrResourceCache::GrResourceCache(const GrCaps* caps, uint32_t contextUniqueID)
bsalomon9f2d1572015-02-17 11:47:40 -080065 : fTimestamp(0)
66 , fMaxCount(kDefaultMaxCount)
bsalomon71cb0c22014-11-14 12:10:14 -080067 , fMaxBytes(kDefaultMaxSize)
bsalomon3f324322015-04-08 11:01:54 -070068 , fMaxUnusedFlushes(kDefaultMaxUnusedFlushes)
bsalomon71cb0c22014-11-14 12:10:14 -080069#if GR_CACHE_STATS
70 , fHighWaterCount(0)
71 , fHighWaterBytes(0)
bsalomondace19e2014-11-17 07:34:06 -080072 , fBudgetedHighWaterCount(0)
73 , fBudgetedHighWaterBytes(0)
bsalomon71cb0c22014-11-14 12:10:14 -080074#endif
bsalomon71cb0c22014-11-14 12:10:14 -080075 , fBytes(0)
bsalomondace19e2014-11-17 07:34:06 -080076 , fBudgetedCount(0)
77 , fBudgetedBytes(0)
Derek Sollenbergeree479142017-05-24 11:41:33 -040078 , fPurgeableBytes(0)
robertphillipsee843b22016-10-04 05:30:20 -070079 , fRequestFlush(false)
bsalomone2e87f32016-09-22 12:42:11 -070080 , fExternalFlushCnt(0)
Brian Osman13dddce2017-05-09 13:19:50 -040081 , fContextUniqueID(contextUniqueID)
robertphillips63926682015-08-20 09:39:02 -070082 , fPreferVRAMUseOverFlushes(caps->preferVRAMUseOverFlushes()) {
bsalomonf320e042015-02-17 15:09:34 -080083 SkDEBUGCODE(fCount = 0;)
halcanary96fcdcc2015-08-27 07:41:13 -070084 SkDEBUGCODE(fNewlyPurgeableResourceForValidation = nullptr;)
bsalomon71cb0c22014-11-14 12:10:14 -080085}
86
bsalomon0ea80f42015-02-11 10:49:59 -080087GrResourceCache::~GrResourceCache() {
bsalomonc8dc1f72014-08-21 13:02:13 -070088 this->releaseAll();
89}
90
bsalomon3f324322015-04-08 11:01:54 -070091void GrResourceCache::setLimits(int count, size_t bytes, int maxUnusedFlushes) {
bsalomon71cb0c22014-11-14 12:10:14 -080092 fMaxCount = count;
93 fMaxBytes = bytes;
bsalomon3f324322015-04-08 11:01:54 -070094 fMaxUnusedFlushes = maxUnusedFlushes;
bsalomon71cb0c22014-11-14 12:10:14 -080095 this->purgeAsNeeded();
96}
97
bsalomon0ea80f42015-02-11 10:49:59 -080098void GrResourceCache::insertResource(GrGpuResource* resource) {
bsalomon49f085d2014-09-05 13:34:00 -070099 SkASSERT(resource);
bsalomon16961262014-08-26 14:01:07 -0700100 SkASSERT(!this->isInCache(resource));
bsalomonf320e042015-02-17 15:09:34 -0800101 SkASSERT(!resource->wasDestroyed());
102 SkASSERT(!resource->isPurgeable());
bsalomonddf30e62015-02-19 11:38:44 -0800103
104 // We must set the timestamp before adding to the array in case the timestamp wraps and we wind
105 // up iterating over all the resources that already have timestamps.
106 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
107
bsalomonf320e042015-02-17 15:09:34 -0800108 this->addToNonpurgeableArray(resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800109
bsalomondace19e2014-11-17 07:34:06 -0800110 size_t size = resource->gpuMemorySize();
bsalomonf320e042015-02-17 15:09:34 -0800111 SkDEBUGCODE(++fCount;)
bsalomon84c8e622014-11-17 09:33:27 -0800112 fBytes += size;
bsalomon82b1d622014-11-14 13:59:57 -0800113#if GR_CACHE_STATS
bsalomonf320e042015-02-17 15:09:34 -0800114 fHighWaterCount = SkTMax(this->getResourceCount(), fHighWaterCount);
bsalomon82b1d622014-11-14 13:59:57 -0800115 fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes);
116#endif
bsalomon5ec26ae2016-02-25 08:33:02 -0800117 if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) {
bsalomondace19e2014-11-17 07:34:06 -0800118 ++fBudgetedCount;
119 fBudgetedBytes += size;
hendrikw876c3132015-03-04 10:33:49 -0800120 TRACE_COUNTER2(TRACE_DISABLED_BY_DEFAULT("skia.gpu.cache"), "skia budget", "used",
121 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomondace19e2014-11-17 07:34:06 -0800122#if GR_CACHE_STATS
123 fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount);
124 fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes);
125#endif
126 }
robertphillipsc4ed6842016-05-24 14:17:12 -0700127 if (resource->resourcePriv().getScratchKey().isValid() &&
128 !resource->getUniqueKey().isValid()) {
kkinnunen2e6055b2016-04-22 01:48:29 -0700129 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
bsalomon3582d3e2015-02-13 14:20:05 -0800130 fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
bsalomon744998e2014-08-28 09:54:34 -0700131 }
bsalomon9f2d1572015-02-17 11:47:40 -0800132
bsalomon71cb0c22014-11-14 12:10:14 -0800133 this->purgeAsNeeded();
bsalomonc8dc1f72014-08-21 13:02:13 -0700134}
135
bsalomon0ea80f42015-02-11 10:49:59 -0800136void GrResourceCache::removeResource(GrGpuResource* resource) {
bsalomon9f2d1572015-02-17 11:47:40 -0800137 this->validate();
bsalomon16961262014-08-26 14:01:07 -0700138 SkASSERT(this->isInCache(resource));
bsalomondace19e2014-11-17 07:34:06 -0800139
Derek Sollenbergeree479142017-05-24 11:41:33 -0400140 size_t size = resource->gpuMemorySize();
bsalomon9f2d1572015-02-17 11:47:40 -0800141 if (resource->isPurgeable()) {
142 fPurgeableQueue.remove(resource);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400143 fPurgeableBytes -= size;
bsalomonf320e042015-02-17 15:09:34 -0800144 } else {
145 this->removeFromNonpurgeableArray(resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800146 }
147
bsalomonf320e042015-02-17 15:09:34 -0800148 SkDEBUGCODE(--fCount;)
bsalomondace19e2014-11-17 07:34:06 -0800149 fBytes -= size;
bsalomon5ec26ae2016-02-25 08:33:02 -0800150 if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) {
bsalomondace19e2014-11-17 07:34:06 -0800151 --fBudgetedCount;
152 fBudgetedBytes -= size;
hendrikw876c3132015-03-04 10:33:49 -0800153 TRACE_COUNTER2(TRACE_DISABLED_BY_DEFAULT("skia.gpu.cache"), "skia budget", "used",
154 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomondace19e2014-11-17 07:34:06 -0800155 }
156
robertphillipsc4ed6842016-05-24 14:17:12 -0700157 if (resource->resourcePriv().getScratchKey().isValid() &&
158 !resource->getUniqueKey().isValid()) {
bsalomon3582d3e2015-02-13 14:20:05 -0800159 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
bsalomon744998e2014-08-28 09:54:34 -0700160 }
bsalomon8718aaf2015-02-19 07:24:21 -0800161 if (resource->getUniqueKey().isValid()) {
162 fUniqueHash.remove(resource->getUniqueKey());
bsalomon8b79d232014-11-10 10:19:06 -0800163 }
bsalomonb436ed62014-11-17 12:15:56 -0800164 this->validate();
bsalomonc8dc1f72014-08-21 13:02:13 -0700165}
166
bsalomon0ea80f42015-02-11 10:49:59 -0800167void GrResourceCache::abandonAll() {
bsalomon71cb0c22014-11-14 12:10:14 -0800168 AutoValidate av(this);
169
bsalomonf320e042015-02-17 15:09:34 -0800170 while (fNonpurgeableResources.count()) {
171 GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
172 SkASSERT(!back->wasDestroyed());
173 back->cacheAccess().abandon();
bsalomonc8dc1f72014-08-21 13:02:13 -0700174 }
bsalomonf320e042015-02-17 15:09:34 -0800175
176 while (fPurgeableQueue.count()) {
177 GrGpuResource* top = fPurgeableQueue.peek();
178 SkASSERT(!top->wasDestroyed());
179 top->cacheAccess().abandon();
180 }
181
bsalomon744998e2014-08-28 09:54:34 -0700182 SkASSERT(!fScratchMap.count());
bsalomon8718aaf2015-02-19 07:24:21 -0800183 SkASSERT(!fUniqueHash.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700184 SkASSERT(!fCount);
bsalomonf320e042015-02-17 15:09:34 -0800185 SkASSERT(!this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800186 SkASSERT(!fBytes);
187 SkASSERT(!fBudgetedCount);
188 SkASSERT(!fBudgetedBytes);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400189 SkASSERT(!fPurgeableBytes);
bsalomonc8dc1f72014-08-21 13:02:13 -0700190}
191
bsalomon0ea80f42015-02-11 10:49:59 -0800192void GrResourceCache::releaseAll() {
bsalomon71cb0c22014-11-14 12:10:14 -0800193 AutoValidate av(this);
194
Brian Osman13dddce2017-05-09 13:19:50 -0400195 this->processFreedGpuResources();
196
bsalomonf320e042015-02-17 15:09:34 -0800197 while(fNonpurgeableResources.count()) {
198 GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
199 SkASSERT(!back->wasDestroyed());
200 back->cacheAccess().release();
bsalomonc8dc1f72014-08-21 13:02:13 -0700201 }
bsalomonf320e042015-02-17 15:09:34 -0800202
203 while (fPurgeableQueue.count()) {
204 GrGpuResource* top = fPurgeableQueue.peek();
205 SkASSERT(!top->wasDestroyed());
206 top->cacheAccess().release();
207 }
208
bsalomon744998e2014-08-28 09:54:34 -0700209 SkASSERT(!fScratchMap.count());
bsalomon8718aaf2015-02-19 07:24:21 -0800210 SkASSERT(!fUniqueHash.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700211 SkASSERT(!fCount);
bsalomonf320e042015-02-17 15:09:34 -0800212 SkASSERT(!this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800213 SkASSERT(!fBytes);
214 SkASSERT(!fBudgetedCount);
215 SkASSERT(!fBudgetedBytes);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400216 SkASSERT(!fPurgeableBytes);
bsalomonc8dc1f72014-08-21 13:02:13 -0700217}
bsalomonbcf0a522014-10-08 08:40:09 -0700218
bsalomon0ea80f42015-02-11 10:49:59 -0800219class GrResourceCache::AvailableForScratchUse {
bsalomonbcf0a522014-10-08 08:40:09 -0700220public:
bsalomon000f8292014-10-15 19:04:14 -0700221 AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendingIO) { }
bsalomonbcf0a522014-10-08 08:40:09 -0700222
223 bool operator()(const GrGpuResource* resource) const {
robertphillipsc4ed6842016-05-24 14:17:12 -0700224 SkASSERT(!resource->getUniqueKey().isValid() &&
225 resource->resourcePriv().getScratchKey().isValid());
bsalomon12299ab2014-11-14 13:33:09 -0800226 if (resource->internalHasRef() || !resource->cacheAccess().isScratch()) {
bsalomon000f8292014-10-15 19:04:14 -0700227 return false;
bsalomonbcf0a522014-10-08 08:40:09 -0700228 }
bsalomon000f8292014-10-15 19:04:14 -0700229 return !fRejectPendingIO || !resource->internalHasPendingIO();
bsalomonbcf0a522014-10-08 08:40:09 -0700230 }
bsalomon1e2530b2014-10-09 09:57:18 -0700231
bsalomonbcf0a522014-10-08 08:40:09 -0700232private:
bsalomon000f8292014-10-15 19:04:14 -0700233 bool fRejectPendingIO;
bsalomonbcf0a522014-10-08 08:40:09 -0700234};
235
bsalomon0ea80f42015-02-11 10:49:59 -0800236GrGpuResource* GrResourceCache::findAndRefScratchResource(const GrScratchKey& scratchKey,
robertphillips6e83ac72015-08-13 05:19:14 -0700237 size_t resourceSize,
bsalomon9f2d1572015-02-17 11:47:40 -0800238 uint32_t flags) {
bsalomon7775c852014-12-30 12:50:52 -0800239 SkASSERT(scratchKey.isValid());
robertphillipsee843b22016-10-04 05:30:20 -0700240
bsalomon71cb0c22014-11-14 12:10:14 -0800241 GrGpuResource* resource;
bsalomon000f8292014-10-15 19:04:14 -0700242 if (flags & (kPreferNoPendingIO_ScratchFlag | kRequireNoPendingIO_ScratchFlag)) {
bsalomon71cb0c22014-11-14 12:10:14 -0800243 resource = fScratchMap.find(scratchKey, AvailableForScratchUse(true));
bsalomon000f8292014-10-15 19:04:14 -0700244 if (resource) {
bsalomon9f2d1572015-02-17 11:47:40 -0800245 this->refAndMakeResourceMRU(resource);
bsalomonb436ed62014-11-17 12:15:56 -0800246 this->validate();
247 return resource;
bsalomon000f8292014-10-15 19:04:14 -0700248 } else if (flags & kRequireNoPendingIO_ScratchFlag) {
halcanary96fcdcc2015-08-27 07:41:13 -0700249 return nullptr;
bsalomon000f8292014-10-15 19:04:14 -0700250 }
robertphillips63926682015-08-20 09:39:02 -0700251 // We would prefer to consume more available VRAM rather than flushing
252 // immediately, but on ANGLE this can lead to starving of the GPU.
253 if (fPreferVRAMUseOverFlushes && this->wouldFit(resourceSize)) {
robertphillips6e83ac72015-08-13 05:19:14 -0700254 // kPrefer is specified, we didn't find a resource without pending io,
robertphillips63926682015-08-20 09:39:02 -0700255 // but there is still space in our budget for the resource so force
256 // the caller to allocate a new resource.
halcanary96fcdcc2015-08-27 07:41:13 -0700257 return nullptr;
robertphillips6e83ac72015-08-13 05:19:14 -0700258 }
bsalomon000f8292014-10-15 19:04:14 -0700259 }
bsalomon71cb0c22014-11-14 12:10:14 -0800260 resource = fScratchMap.find(scratchKey, AvailableForScratchUse(false));
261 if (resource) {
bsalomon9f2d1572015-02-17 11:47:40 -0800262 this->refAndMakeResourceMRU(resource);
bsalomonb436ed62014-11-17 12:15:56 -0800263 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800264 }
265 return resource;
bsalomonbcf0a522014-10-08 08:40:09 -0700266}
bsalomon8b79d232014-11-10 10:19:06 -0800267
bsalomon0ea80f42015-02-11 10:49:59 -0800268void GrResourceCache::willRemoveScratchKey(const GrGpuResource* resource) {
bsalomon3582d3e2015-02-13 14:20:05 -0800269 SkASSERT(resource->resourcePriv().getScratchKey().isValid());
robertphillipsc4ed6842016-05-24 14:17:12 -0700270 if (!resource->getUniqueKey().isValid()) {
271 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
272 }
bsalomon10e23ca2014-11-25 05:52:06 -0800273}
274
bsalomonf99e9612015-02-19 08:24:16 -0800275void GrResourceCache::removeUniqueKey(GrGpuResource* resource) {
bsalomon3f324322015-04-08 11:01:54 -0700276 // Someone has a ref to this resource in order to have removed the key. When the ref count
277 // reaches zero we will get a ref cnt notification and figure out what to do with it.
bsalomonf99e9612015-02-19 08:24:16 -0800278 if (resource->getUniqueKey().isValid()) {
279 SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
280 fUniqueHash.remove(resource->getUniqueKey());
281 }
282 resource->cacheAccess().removeUniqueKey();
robertphillipsc4ed6842016-05-24 14:17:12 -0700283
284 if (resource->resourcePriv().getScratchKey().isValid()) {
285 fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
286 }
287
bsalomonf99e9612015-02-19 08:24:16 -0800288 this->validate();
bsalomon23e619c2015-02-06 11:54:28 -0800289}
290
bsalomonf99e9612015-02-19 08:24:16 -0800291void GrResourceCache::changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) {
bsalomon8b79d232014-11-10 10:19:06 -0800292 SkASSERT(resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800293 SkASSERT(this->isInCache(resource));
bsalomon8b79d232014-11-10 10:19:06 -0800294
bsalomonf99e9612015-02-19 08:24:16 -0800295 // If another resource has the new key, remove its key then install the key on this resource.
296 if (newKey.isValid()) {
robertphillipsc4ed6842016-05-24 14:17:12 -0700297 // Remove the entry for this resource if it already has a unique key.
298 if (resource->getUniqueKey().isValid()) {
299 SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
300 fUniqueHash.remove(resource->getUniqueKey());
301 SkASSERT(nullptr == fUniqueHash.find(resource->getUniqueKey()));
302 } else {
303 // 'resource' didn't have a valid unique key before so it is switching sides. Remove it
304 // from the ScratchMap
305 if (resource->resourcePriv().getScratchKey().isValid()) {
306 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
307 }
308 }
309
bsalomonf99e9612015-02-19 08:24:16 -0800310 if (GrGpuResource* old = fUniqueHash.find(newKey)) {
311 // If the old resource using the key is purgeable and is unreachable, then remove it.
312 if (!old->resourcePriv().getScratchKey().isValid() && old->isPurgeable()) {
313 // release may call validate() which will assert that resource is in fUniqueHash
314 // if it has a valid key. So in debug reset the key here before we assign it.
315 SkDEBUGCODE(resource->cacheAccess().removeUniqueKey();)
316 old->cacheAccess().release();
317 } else {
robertphillipsc4ed6842016-05-24 14:17:12 -0700318 this->removeUniqueKey(old);
bsalomonf99e9612015-02-19 08:24:16 -0800319 }
320 }
halcanary96fcdcc2015-08-27 07:41:13 -0700321 SkASSERT(nullptr == fUniqueHash.find(newKey));
bsalomonf99e9612015-02-19 08:24:16 -0800322 resource->cacheAccess().setUniqueKey(newKey);
323 fUniqueHash.add(resource);
324 } else {
robertphillipsc4ed6842016-05-24 14:17:12 -0700325 this->removeUniqueKey(resource);
bsalomonf99e9612015-02-19 08:24:16 -0800326 }
327
bsalomon71cb0c22014-11-14 12:10:14 -0800328 this->validate();
bsalomon8b79d232014-11-10 10:19:06 -0800329}
bsalomon71cb0c22014-11-14 12:10:14 -0800330
bsalomon9f2d1572015-02-17 11:47:40 -0800331void GrResourceCache::refAndMakeResourceMRU(GrGpuResource* resource) {
bsalomon71cb0c22014-11-14 12:10:14 -0800332 SkASSERT(resource);
333 SkASSERT(this->isInCache(resource));
bsalomonddf30e62015-02-19 11:38:44 -0800334
bsalomon9f2d1572015-02-17 11:47:40 -0800335 if (resource->isPurgeable()) {
336 // It's about to become unpurgeable.
Derek Sollenbergeree479142017-05-24 11:41:33 -0400337 fPurgeableBytes -= resource->gpuMemorySize();
bsalomon9f2d1572015-02-17 11:47:40 -0800338 fPurgeableQueue.remove(resource);
bsalomonf320e042015-02-17 15:09:34 -0800339 this->addToNonpurgeableArray(resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800340 }
341 resource->ref();
bsalomonddf30e62015-02-19 11:38:44 -0800342
343 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
bsalomonf320e042015-02-17 15:09:34 -0800344 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800345}
346
bsalomon3f324322015-04-08 11:01:54 -0700347void GrResourceCache::notifyCntReachedZero(GrGpuResource* resource, uint32_t flags) {
bsalomon71cb0c22014-11-14 12:10:14 -0800348 SkASSERT(resource);
bsalomon3f324322015-04-08 11:01:54 -0700349 SkASSERT(!resource->wasDestroyed());
350 SkASSERT(flags);
bsalomon71cb0c22014-11-14 12:10:14 -0800351 SkASSERT(this->isInCache(resource));
bsalomon3f324322015-04-08 11:01:54 -0700352 // This resource should always be in the nonpurgeable array when this function is called. It
353 // will be moved to the queue if it is newly purgeable.
354 SkASSERT(fNonpurgeableResources[*resource->cacheAccess().accessCacheIndex()] == resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800355
bsalomon3f324322015-04-08 11:01:54 -0700356 if (SkToBool(ResourceAccess::kRefCntReachedZero_RefNotificationFlag & flags)) {
357#ifdef SK_DEBUG
358 // When the timestamp overflows validate() is called. validate() checks that resources in
359 // the nonpurgeable array are indeed not purgeable. However, the movement from the array to
360 // the purgeable queue happens just below in this function. So we mark it as an exception.
361 if (resource->isPurgeable()) {
362 fNewlyPurgeableResourceForValidation = resource;
363 }
364#endif
365 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
halcanary96fcdcc2015-08-27 07:41:13 -0700366 SkDEBUGCODE(fNewlyPurgeableResourceForValidation = nullptr);
bsalomon3f324322015-04-08 11:01:54 -0700367 }
368
369 if (!SkToBool(ResourceAccess::kAllCntsReachedZero_RefNotificationFlag & flags)) {
370 SkASSERT(!resource->isPurgeable());
371 return;
372 }
373
374 SkASSERT(resource->isPurgeable());
bsalomonf320e042015-02-17 15:09:34 -0800375 this->removeFromNonpurgeableArray(resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800376 fPurgeableQueue.insert(resource);
bsalomone2e87f32016-09-22 12:42:11 -0700377 resource->cacheAccess().setFlushCntWhenResourceBecamePurgeable(fExternalFlushCnt);
Brian Salomon5e150852017-03-22 14:53:13 -0400378 resource->cacheAccess().setTimeWhenResourceBecomePurgeable();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400379 fPurgeableBytes += resource->gpuMemorySize();
bsalomon71cb0c22014-11-14 12:10:14 -0800380
bsalomon5ec26ae2016-02-25 08:33:02 -0800381 if (SkBudgeted::kNo == resource->resourcePriv().isBudgeted()) {
bsalomonc2f35b72015-01-23 07:19:22 -0800382 // Check whether this resource could still be used as a scratch resource.
kkinnunen2e6055b2016-04-22 01:48:29 -0700383 if (!resource->resourcePriv().refsWrappedObjects() &&
bsalomon9f2d1572015-02-17 11:47:40 -0800384 resource->resourcePriv().getScratchKey().isValid()) {
bsalomonc2f35b72015-01-23 07:19:22 -0800385 // We won't purge an existing resource to make room for this one.
bsalomonf320e042015-02-17 15:09:34 -0800386 if (fBudgetedCount < fMaxCount &&
387 fBudgetedBytes + resource->gpuMemorySize() <= fMaxBytes) {
bsalomon3582d3e2015-02-13 14:20:05 -0800388 resource->resourcePriv().makeBudgeted();
bsalomon9f2d1572015-02-17 11:47:40 -0800389 return;
bsalomonc2f35b72015-01-23 07:19:22 -0800390 }
bsalomonc2f35b72015-01-23 07:19:22 -0800391 }
392 } else {
bsalomon9f2d1572015-02-17 11:47:40 -0800393 // Purge the resource immediately if we're over budget
bsalomon8718aaf2015-02-19 07:24:21 -0800394 // Also purge if the resource has neither a valid scratch key nor a unique key.
robertphillipsee843b22016-10-04 05:30:20 -0700395 bool noKey = !resource->resourcePriv().getScratchKey().isValid() &&
396 !resource->getUniqueKey().isValid();
397 if (!this->overBudget() && !noKey) {
bsalomon9f2d1572015-02-17 11:47:40 -0800398 return;
bsalomonc2f35b72015-01-23 07:19:22 -0800399 }
400 }
bsalomondace19e2014-11-17 07:34:06 -0800401
bsalomonf320e042015-02-17 15:09:34 -0800402 SkDEBUGCODE(int beforeCount = this->getResourceCount();)
bsalomon9f2d1572015-02-17 11:47:40 -0800403 resource->cacheAccess().release();
404 // We should at least free this resource, perhaps dependent resources as well.
bsalomonf320e042015-02-17 15:09:34 -0800405 SkASSERT(this->getResourceCount() < beforeCount);
bsalomon71cb0c22014-11-14 12:10:14 -0800406 this->validate();
407}
408
bsalomon0ea80f42015-02-11 10:49:59 -0800409void GrResourceCache::didChangeGpuMemorySize(const GrGpuResource* resource, size_t oldSize) {
bsalomon71cb0c22014-11-14 12:10:14 -0800410 // SkASSERT(!fPurging); GrPathRange increases size during flush. :(
411 SkASSERT(resource);
412 SkASSERT(this->isInCache(resource));
413
bsalomondace19e2014-11-17 07:34:06 -0800414 ptrdiff_t delta = resource->gpuMemorySize() - oldSize;
415
416 fBytes += delta;
bsalomon82b1d622014-11-14 13:59:57 -0800417#if GR_CACHE_STATS
418 fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes);
419#endif
bsalomon5ec26ae2016-02-25 08:33:02 -0800420 if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) {
bsalomondace19e2014-11-17 07:34:06 -0800421 fBudgetedBytes += delta;
hendrikw876c3132015-03-04 10:33:49 -0800422 TRACE_COUNTER2(TRACE_DISABLED_BY_DEFAULT("skia.gpu.cache"), "skia budget", "used",
423 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomondace19e2014-11-17 07:34:06 -0800424#if GR_CACHE_STATS
425 fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes);
426#endif
427 }
bsalomon71cb0c22014-11-14 12:10:14 -0800428
429 this->purgeAsNeeded();
430 this->validate();
431}
432
bsalomon0ea80f42015-02-11 10:49:59 -0800433void GrResourceCache::didChangeBudgetStatus(GrGpuResource* resource) {
bsalomon84c8e622014-11-17 09:33:27 -0800434 SkASSERT(resource);
435 SkASSERT(this->isInCache(resource));
436
437 size_t size = resource->gpuMemorySize();
438
bsalomon5ec26ae2016-02-25 08:33:02 -0800439 if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) {
bsalomon84c8e622014-11-17 09:33:27 -0800440 ++fBudgetedCount;
441 fBudgetedBytes += size;
bsalomonafe30052015-01-16 07:32:33 -0800442#if GR_CACHE_STATS
443 fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes);
444 fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount);
445#endif
bsalomon84c8e622014-11-17 09:33:27 -0800446 this->purgeAsNeeded();
447 } else {
448 --fBudgetedCount;
449 fBudgetedBytes -= size;
450 }
hendrikw876c3132015-03-04 10:33:49 -0800451 TRACE_COUNTER2(TRACE_DISABLED_BY_DEFAULT("skia.gpu.cache"), "skia budget", "used",
452 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomon84c8e622014-11-17 09:33:27 -0800453
454 this->validate();
455}
456
robertphillipsee843b22016-10-04 05:30:20 -0700457void GrResourceCache::purgeAsNeeded() {
bsalomon3f324322015-04-08 11:01:54 -0700458 SkTArray<GrUniqueKeyInvalidatedMessage> invalidKeyMsgs;
459 fInvalidUniqueKeyInbox.poll(&invalidKeyMsgs);
460 if (invalidKeyMsgs.count()) {
461 this->processInvalidUniqueKeys(invalidKeyMsgs);
462 }
bsalomon71cb0c22014-11-14 12:10:14 -0800463
Brian Osman13dddce2017-05-09 13:19:50 -0400464 this->processFreedGpuResources();
465
bsalomone2e87f32016-09-22 12:42:11 -0700466 if (fMaxUnusedFlushes > 0) {
467 // We want to know how many complete flushes have occurred without the resource being used.
468 // If the resource was tagged when fExternalFlushCnt was N then this means it became
469 // purgeable during activity that became the N+1th flush. So when the flush count is N+2
470 // it has sat in the purgeable queue for one entire flush.
471 uint32_t oldestAllowedFlushCnt = fExternalFlushCnt - fMaxUnusedFlushes - 1;
472 // check for underflow
473 if (oldestAllowedFlushCnt < fExternalFlushCnt) {
474 while (fPurgeableQueue.count()) {
475 uint32_t flushWhenResourceBecamePurgeable =
476 fPurgeableQueue.peek()->cacheAccess().flushCntWhenResourceBecamePurgeable();
477 if (oldestAllowedFlushCnt < flushWhenResourceBecamePurgeable) {
478 // Resources were given both LRU timestamps and tagged with a flush cnt when
479 // they first became purgeable. The LRU timestamp won't change again until the
480 // resource is made non-purgeable again. So, at this point all the remaining
481 // resources in the timestamp-sorted queue will have a flush count >= to this
482 // one.
483 break;
484 }
485 GrGpuResource* resource = fPurgeableQueue.peek();
486 SkASSERT(resource->isPurgeable());
487 resource->cacheAccess().release();
bsalomon3f324322015-04-08 11:01:54 -0700488 }
bsalomon3f324322015-04-08 11:01:54 -0700489 }
490 }
491
492 bool stillOverbudget = this->overBudget();
493 while (stillOverbudget && fPurgeableQueue.count()) {
robertphillipsee843b22016-10-04 05:30:20 -0700494 GrGpuResource* resource = fPurgeableQueue.peek();
bsalomon9f2d1572015-02-17 11:47:40 -0800495 SkASSERT(resource->isPurgeable());
496 resource->cacheAccess().release();
bsalomon3f324322015-04-08 11:01:54 -0700497 stillOverbudget = this->overBudget();
bsalomon9f2d1572015-02-17 11:47:40 -0800498 }
bsalomon71cb0c22014-11-14 12:10:14 -0800499
bsalomonb436ed62014-11-17 12:15:56 -0800500 this->validate();
robertphillipsee843b22016-10-04 05:30:20 -0700501
502 if (stillOverbudget) {
503 // Set this so that GrDrawingManager will issue a flush to free up resources with pending
504 // IO that we were unable to purge in this pass.
505 fRequestFlush = true;
506 }
bsalomon71cb0c22014-11-14 12:10:14 -0800507}
508
bsalomon0ea80f42015-02-11 10:49:59 -0800509void GrResourceCache::purgeAllUnlocked() {
bsalomon9f2d1572015-02-17 11:47:40 -0800510 // We could disable maintaining the heap property here, but it would add a lot of complexity.
511 // Moreover, this is rarely called.
512 while (fPurgeableQueue.count()) {
513 GrGpuResource* resource = fPurgeableQueue.peek();
514 SkASSERT(resource->isPurgeable());
515 resource->cacheAccess().release();
516 }
bsalomon71cb0c22014-11-14 12:10:14 -0800517
bsalomonb436ed62014-11-17 12:15:56 -0800518 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800519}
520
Brian Salomon5e150852017-03-22 14:53:13 -0400521void GrResourceCache::purgeResourcesNotUsedSince(GrStdSteadyClock::time_point purgeTime) {
522 while (fPurgeableQueue.count()) {
523 const GrStdSteadyClock::time_point resourceTime =
524 fPurgeableQueue.peek()->cacheAccess().timeWhenResourceBecamePurgeable();
525 if (resourceTime >= purgeTime) {
526 // Resources were given both LRU timestamps and tagged with a frame number when
527 // they first became purgeable. The LRU timestamp won't change again until the
528 // resource is made non-purgeable again. So, at this point all the remaining
529 // resources in the timestamp-sorted queue will have a frame number >= to this
530 // one.
531 break;
532 }
533 GrGpuResource* resource = fPurgeableQueue.peek();
534 SkASSERT(resource->isPurgeable());
535 resource->cacheAccess().release();
536 }
537}
538
bsalomon8718aaf2015-02-19 07:24:21 -0800539void GrResourceCache::processInvalidUniqueKeys(
540 const SkTArray<GrUniqueKeyInvalidatedMessage>& msgs) {
bsalomon23e619c2015-02-06 11:54:28 -0800541 for (int i = 0; i < msgs.count(); ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -0800542 GrGpuResource* resource = this->findAndRefUniqueResource(msgs[i].key());
bsalomon23e619c2015-02-06 11:54:28 -0800543 if (resource) {
bsalomon8718aaf2015-02-19 07:24:21 -0800544 resource->resourcePriv().removeUniqueKey();
bsalomon3f324322015-04-08 11:01:54 -0700545 resource->unref(); // If this resource is now purgeable, the cache will be notified.
bsalomon23e619c2015-02-06 11:54:28 -0800546 }
547 }
548}
549
Brian Osman13dddce2017-05-09 13:19:50 -0400550void GrResourceCache::insertCrossContextGpuResource(GrGpuResource* resource) {
551 resource->ref();
552}
553
554void GrResourceCache::processFreedGpuResources() {
555 SkTArray<GrGpuResourceFreedMessage> msgs;
556 fFreedGpuResourceInbox.poll(&msgs);
557 for (int i = 0; i < msgs.count(); ++i) {
558 if (msgs[i].fOwningUniqueID == fContextUniqueID) {
559 msgs[i].fResource->unref();
560 }
561 }
562}
563
bsalomonf320e042015-02-17 15:09:34 -0800564void GrResourceCache::addToNonpurgeableArray(GrGpuResource* resource) {
565 int index = fNonpurgeableResources.count();
566 *fNonpurgeableResources.append() = resource;
567 *resource->cacheAccess().accessCacheIndex() = index;
568}
569
570void GrResourceCache::removeFromNonpurgeableArray(GrGpuResource* resource) {
571 int* index = resource->cacheAccess().accessCacheIndex();
572 // Fill the whole we will create in the array with the tail object, adjust its index, and
573 // then pop the array
574 GrGpuResource* tail = *(fNonpurgeableResources.end() - 1);
575 SkASSERT(fNonpurgeableResources[*index] == resource);
576 fNonpurgeableResources[*index] = tail;
577 *tail->cacheAccess().accessCacheIndex() = *index;
578 fNonpurgeableResources.pop();
579 SkDEBUGCODE(*index = -1);
580}
581
bsalomonddf30e62015-02-19 11:38:44 -0800582uint32_t GrResourceCache::getNextTimestamp() {
583 // If we wrap then all the existing resources will appear older than any resources that get
584 // a timestamp after the wrap.
585 if (0 == fTimestamp) {
586 int count = this->getResourceCount();
587 if (count) {
588 // Reset all the timestamps. We sort the resources by timestamp and then assign
589 // sequential timestamps beginning with 0. This is O(n*lg(n)) but it should be extremely
590 // rare.
591 SkTDArray<GrGpuResource*> sortedPurgeableResources;
592 sortedPurgeableResources.setReserve(fPurgeableQueue.count());
593
594 while (fPurgeableQueue.count()) {
595 *sortedPurgeableResources.append() = fPurgeableQueue.peek();
596 fPurgeableQueue.pop();
597 }
robertphillipsee843b22016-10-04 05:30:20 -0700598
bsalomone2e87f32016-09-22 12:42:11 -0700599 SkTQSort(fNonpurgeableResources.begin(), fNonpurgeableResources.end() - 1,
600 CompareTimestamp);
bsalomonddf30e62015-02-19 11:38:44 -0800601
602 // Pick resources out of the purgeable and non-purgeable arrays based on lowest
603 // timestamp and assign new timestamps.
604 int currP = 0;
605 int currNP = 0;
606 while (currP < sortedPurgeableResources.count() &&
mtklein56da0252015-11-16 11:16:23 -0800607 currNP < fNonpurgeableResources.count()) {
bsalomonddf30e62015-02-19 11:38:44 -0800608 uint32_t tsP = sortedPurgeableResources[currP]->cacheAccess().timestamp();
609 uint32_t tsNP = fNonpurgeableResources[currNP]->cacheAccess().timestamp();
610 SkASSERT(tsP != tsNP);
611 if (tsP < tsNP) {
612 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
613 } else {
614 // Correct the index in the nonpurgeable array stored on the resource post-sort.
615 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
616 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
617 }
618 }
619
620 // The above loop ended when we hit the end of one array. Finish the other one.
621 while (currP < sortedPurgeableResources.count()) {
622 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
623 }
624 while (currNP < fNonpurgeableResources.count()) {
625 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
626 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
627 }
628
629 // Rebuild the queue.
630 for (int i = 0; i < sortedPurgeableResources.count(); ++i) {
631 fPurgeableQueue.insert(sortedPurgeableResources[i]);
632 }
633
634 this->validate();
635 SkASSERT(count == this->getResourceCount());
636
637 // count should be the next timestamp we return.
638 SkASSERT(fTimestamp == SkToU32(count));
mtklein56da0252015-11-16 11:16:23 -0800639 }
bsalomonddf30e62015-02-19 11:38:44 -0800640 }
641 return fTimestamp++;
642}
643
bsalomonb77a9072016-09-07 10:02:04 -0700644void GrResourceCache::notifyFlushOccurred(FlushType type) {
645 switch (type) {
646 case FlushType::kImmediateMode:
647 break;
648 case FlushType::kCacheRequested:
robertphillipsee843b22016-10-04 05:30:20 -0700649 SkASSERT(fRequestFlush);
650 fRequestFlush = false;
bsalomonb77a9072016-09-07 10:02:04 -0700651 break;
robertphillipsee843b22016-10-04 05:30:20 -0700652 case FlushType::kExternal:
bsalomone2e87f32016-09-22 12:42:11 -0700653 ++fExternalFlushCnt;
654 if (0 == fExternalFlushCnt) {
655 // When this wraps just reset all the purgeable resources' last used flush state.
656 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
657 fPurgeableQueue.at(i)->cacheAccess().setFlushCntWhenResourceBecamePurgeable(0);
658 }
bsalomonb77a9072016-09-07 10:02:04 -0700659 }
660 break;
bsalomon3f324322015-04-08 11:01:54 -0700661 }
robertphillipsee843b22016-10-04 05:30:20 -0700662 this->purgeAsNeeded();
bsalomon3f324322015-04-08 11:01:54 -0700663}
664
ericrk0a5fa482015-09-15 14:16:10 -0700665void GrResourceCache::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
666 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
667 fNonpurgeableResources[i]->dumpMemoryStatistics(traceMemoryDump);
668 }
669 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
670 fPurgeableQueue.at(i)->dumpMemoryStatistics(traceMemoryDump);
671 }
672}
673
bsalomon71cb0c22014-11-14 12:10:14 -0800674#ifdef SK_DEBUG
bsalomon0ea80f42015-02-11 10:49:59 -0800675void GrResourceCache::validate() const {
bsalomonc2f35b72015-01-23 07:19:22 -0800676 // Reduce the frequency of validations for large resource counts.
677 static SkRandom gRandom;
678 int mask = (SkNextPow2(fCount + 1) >> 5) - 1;
679 if (~mask && (gRandom.nextU() & mask)) {
680 return;
681 }
682
bsalomonf320e042015-02-17 15:09:34 -0800683 struct Stats {
684 size_t fBytes;
685 int fBudgetedCount;
686 size_t fBudgetedBytes;
687 int fLocked;
688 int fScratch;
689 int fCouldBeScratch;
690 int fContent;
691 const ScratchMap* fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800692 const UniqueHash* fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800693
bsalomonf320e042015-02-17 15:09:34 -0800694 Stats(const GrResourceCache* cache) {
695 memset(this, 0, sizeof(*this));
696 fScratchMap = &cache->fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800697 fUniqueHash = &cache->fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800698 }
699
bsalomonf320e042015-02-17 15:09:34 -0800700 void update(GrGpuResource* resource) {
701 fBytes += resource->gpuMemorySize();
bsalomondace19e2014-11-17 07:34:06 -0800702
bsalomonf320e042015-02-17 15:09:34 -0800703 if (!resource->isPurgeable()) {
704 ++fLocked;
705 }
bsalomon9f2d1572015-02-17 11:47:40 -0800706
robertphillipsc4ed6842016-05-24 14:17:12 -0700707 const GrScratchKey& scratchKey = resource->resourcePriv().getScratchKey();
708 const GrUniqueKey& uniqueKey = resource->getUniqueKey();
709
bsalomonf320e042015-02-17 15:09:34 -0800710 if (resource->cacheAccess().isScratch()) {
robertphillipsc4ed6842016-05-24 14:17:12 -0700711 SkASSERT(!uniqueKey.isValid());
bsalomonf320e042015-02-17 15:09:34 -0800712 ++fScratch;
robertphillipsc4ed6842016-05-24 14:17:12 -0700713 SkASSERT(fScratchMap->countForKey(scratchKey));
kkinnunen2e6055b2016-04-22 01:48:29 -0700714 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
robertphillipsc4ed6842016-05-24 14:17:12 -0700715 } else if (scratchKey.isValid()) {
bsalomon5ec26ae2016-02-25 08:33:02 -0800716 SkASSERT(SkBudgeted::kNo == resource->resourcePriv().isBudgeted() ||
robertphillipsc4ed6842016-05-24 14:17:12 -0700717 uniqueKey.isValid());
718 if (!uniqueKey.isValid()) {
mtklein4e976072016-08-08 09:06:27 -0700719 ++fCouldBeScratch;
robertphillipsc4ed6842016-05-24 14:17:12 -0700720 SkASSERT(fScratchMap->countForKey(scratchKey));
721 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700722 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
bsalomonf320e042015-02-17 15:09:34 -0800723 }
bsalomon8718aaf2015-02-19 07:24:21 -0800724 if (uniqueKey.isValid()) {
bsalomonf320e042015-02-17 15:09:34 -0800725 ++fContent;
bsalomon8718aaf2015-02-19 07:24:21 -0800726 SkASSERT(fUniqueHash->find(uniqueKey) == resource);
Brian Osman0562eb92017-05-08 11:16:39 -0400727 SkASSERT(SkBudgeted::kYes == resource->resourcePriv().isBudgeted() ||
728 resource->resourcePriv().refsWrappedObjects());
robertphillipsc4ed6842016-05-24 14:17:12 -0700729
730 if (scratchKey.isValid()) {
731 SkASSERT(!fScratchMap->has(resource, scratchKey));
732 }
bsalomonf320e042015-02-17 15:09:34 -0800733 }
734
bsalomon5ec26ae2016-02-25 08:33:02 -0800735 if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) {
bsalomonf320e042015-02-17 15:09:34 -0800736 ++fBudgetedCount;
737 fBudgetedBytes += resource->gpuMemorySize();
738 }
bsalomon9f2d1572015-02-17 11:47:40 -0800739 }
bsalomonf320e042015-02-17 15:09:34 -0800740 };
741
robertphillipsc4ed6842016-05-24 14:17:12 -0700742 {
743 ScratchMap::ConstIter iter(&fScratchMap);
744
745 int count = 0;
746 for ( ; !iter.done(); ++iter) {
747 const GrGpuResource* resource = *iter;
748 SkASSERT(resource->resourcePriv().getScratchKey().isValid());
749 SkASSERT(!resource->getUniqueKey().isValid());
750 count++;
751 }
752 SkASSERT(count == fScratchMap.count()); // ensure the iterator is working correctly
753 }
754
bsalomonf320e042015-02-17 15:09:34 -0800755 Stats stats(this);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400756 size_t purgeableBytes = 0;
bsalomonf320e042015-02-17 15:09:34 -0800757
758 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
bsalomon3f324322015-04-08 11:01:54 -0700759 SkASSERT(!fNonpurgeableResources[i]->isPurgeable() ||
760 fNewlyPurgeableResourceForValidation == fNonpurgeableResources[i]);
bsalomonf320e042015-02-17 15:09:34 -0800761 SkASSERT(*fNonpurgeableResources[i]->cacheAccess().accessCacheIndex() == i);
762 SkASSERT(!fNonpurgeableResources[i]->wasDestroyed());
763 stats.update(fNonpurgeableResources[i]);
bsalomon71cb0c22014-11-14 12:10:14 -0800764 }
bsalomon9f2d1572015-02-17 11:47:40 -0800765 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
766 SkASSERT(fPurgeableQueue.at(i)->isPurgeable());
bsalomonf320e042015-02-17 15:09:34 -0800767 SkASSERT(*fPurgeableQueue.at(i)->cacheAccess().accessCacheIndex() == i);
768 SkASSERT(!fPurgeableQueue.at(i)->wasDestroyed());
769 stats.update(fPurgeableQueue.at(i));
Derek Sollenbergeree479142017-05-24 11:41:33 -0400770 purgeableBytes += fPurgeableQueue.at(i)->gpuMemorySize();
bsalomon9f2d1572015-02-17 11:47:40 -0800771 }
772
bsalomonf320e042015-02-17 15:09:34 -0800773 SkASSERT(fCount == this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800774 SkASSERT(fBudgetedCount <= fCount);
bsalomonf320e042015-02-17 15:09:34 -0800775 SkASSERT(fBudgetedBytes <= fBytes);
776 SkASSERT(stats.fBytes == fBytes);
777 SkASSERT(stats.fBudgetedBytes == fBudgetedBytes);
778 SkASSERT(stats.fBudgetedCount == fBudgetedCount);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400779 SkASSERT(purgeableBytes == fPurgeableBytes);
bsalomon71cb0c22014-11-14 12:10:14 -0800780#if GR_CACHE_STATS
bsalomondace19e2014-11-17 07:34:06 -0800781 SkASSERT(fBudgetedHighWaterCount <= fHighWaterCount);
782 SkASSERT(fBudgetedHighWaterBytes <= fHighWaterBytes);
bsalomonf320e042015-02-17 15:09:34 -0800783 SkASSERT(fBytes <= fHighWaterBytes);
784 SkASSERT(fCount <= fHighWaterCount);
785 SkASSERT(fBudgetedBytes <= fBudgetedHighWaterBytes);
786 SkASSERT(fBudgetedCount <= fBudgetedHighWaterCount);
bsalomon71cb0c22014-11-14 12:10:14 -0800787#endif
bsalomon8718aaf2015-02-19 07:24:21 -0800788 SkASSERT(stats.fContent == fUniqueHash.count());
bsalomonf320e042015-02-17 15:09:34 -0800789 SkASSERT(stats.fScratch + stats.fCouldBeScratch == fScratchMap.count());
bsalomon71cb0c22014-11-14 12:10:14 -0800790
bsalomon3f324322015-04-08 11:01:54 -0700791 // This assertion is not currently valid because we can be in recursive notifyCntReachedZero()
bsalomon12299ab2014-11-14 13:33:09 -0800792 // calls. This will be fixed when subresource registration is explicit.
bsalomondace19e2014-11-17 07:34:06 -0800793 // bool overBudget = budgetedBytes > fMaxBytes || budgetedCount > fMaxCount;
bsalomon12299ab2014-11-14 13:33:09 -0800794 // SkASSERT(!overBudget || locked == count || fPurging);
bsalomon71cb0c22014-11-14 12:10:14 -0800795}
bsalomonf320e042015-02-17 15:09:34 -0800796
797bool GrResourceCache::isInCache(const GrGpuResource* resource) const {
798 int index = *resource->cacheAccess().accessCacheIndex();
799 if (index < 0) {
800 return false;
801 }
802 if (index < fPurgeableQueue.count() && fPurgeableQueue.at(index) == resource) {
803 return true;
804 }
805 if (index < fNonpurgeableResources.count() && fNonpurgeableResources[index] == resource) {
806 return true;
807 }
808 SkDEBUGFAIL("Resource index should be -1 or the resource should be in the cache.");
809 return false;
810}
811
bsalomon71cb0c22014-11-14 12:10:14 -0800812#endif