blob: 9462a7384d73d45d3a5a857060ab7755d7082903 [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
21//////////////////////////////////////////////////////////////////////////////
22
bsalomon7775c852014-12-30 12:50:52 -080023GrScratchKey::ResourceType GrScratchKey::GenerateResourceType() {
bsalomon24db3b12015-01-23 04:24:04 -080024 static int32_t gType = INHERITED::kInvalidDomain + 1;
bsalomonfe369ee2014-11-10 11:59:06 -080025
bsalomon7775c852014-12-30 12:50:52 -080026 int32_t type = sk_atomic_inc(&gType);
robertphillips9790a7b2015-01-05 12:29:15 -080027 if (type > SK_MaxU16) {
bsalomon71cb0c22014-11-14 12:10:14 -080028 SkFAIL("Too many Resource Types");
29 }
30
31 return static_cast<ResourceType>(type);
32}
33
bsalomon8718aaf2015-02-19 07:24:21 -080034GrUniqueKey::Domain GrUniqueKey::GenerateDomain() {
bsalomon24db3b12015-01-23 04:24:04 -080035 static int32_t gDomain = INHERITED::kInvalidDomain + 1;
bsalomon7775c852014-12-30 12:50:52 -080036
bsalomon24db3b12015-01-23 04:24:04 -080037 int32_t domain = sk_atomic_inc(&gDomain);
kkinnunen016dffb2015-01-23 06:43:05 -080038 if (domain > SK_MaxU16) {
bsalomon8718aaf2015-02-19 07:24:21 -080039 SkFAIL("Too many GrUniqueKey Domains");
bsalomon7775c852014-12-30 12:50:52 -080040 }
bsalomon24db3b12015-01-23 04:24:04 -080041
42 return static_cast<Domain>(domain);
43}
bsalomon3f324322015-04-08 11:01:54 -070044
bsalomon24db3b12015-01-23 04:24:04 -080045uint32_t GrResourceKeyHash(const uint32_t* data, size_t size) {
mtklein4e976072016-08-08 09:06:27 -070046 return SkOpts::hash(data, size);
bsalomon7775c852014-12-30 12:50:52 -080047}
48
bsalomonfe369ee2014-11-10 11:59:06 -080049//////////////////////////////////////////////////////////////////////////////
50
bsalomon0ea80f42015-02-11 10:49:59 -080051class GrResourceCache::AutoValidate : ::SkNoncopyable {
bsalomon71cb0c22014-11-14 12:10:14 -080052public:
bsalomon0ea80f42015-02-11 10:49:59 -080053 AutoValidate(GrResourceCache* cache) : fCache(cache) { cache->validate(); }
bsalomon71cb0c22014-11-14 12:10:14 -080054 ~AutoValidate() { fCache->validate(); }
55private:
bsalomon0ea80f42015-02-11 10:49:59 -080056 GrResourceCache* fCache;
bsalomon71cb0c22014-11-14 12:10:14 -080057};
58
59 //////////////////////////////////////////////////////////////////////////////
60
bsalomon71cb0c22014-11-14 12:10:14 -080061
robertphillips63926682015-08-20 09:39:02 -070062GrResourceCache::GrResourceCache(const GrCaps* caps)
bsalomon9f2d1572015-02-17 11:47:40 -080063 : fTimestamp(0)
64 , fMaxCount(kDefaultMaxCount)
bsalomon71cb0c22014-11-14 12:10:14 -080065 , fMaxBytes(kDefaultMaxSize)
bsalomon3f324322015-04-08 11:01:54 -070066 , fMaxUnusedFlushes(kDefaultMaxUnusedFlushes)
bsalomon71cb0c22014-11-14 12:10:14 -080067#if GR_CACHE_STATS
68 , fHighWaterCount(0)
69 , fHighWaterBytes(0)
bsalomondace19e2014-11-17 07:34:06 -080070 , fBudgetedHighWaterCount(0)
71 , fBudgetedHighWaterBytes(0)
bsalomon71cb0c22014-11-14 12:10:14 -080072#endif
bsalomon71cb0c22014-11-14 12:10:14 -080073 , fBytes(0)
bsalomondace19e2014-11-17 07:34:06 -080074 , fBudgetedCount(0)
75 , fBudgetedBytes(0)
bsalomonb77a9072016-09-07 10:02:04 -070076 , fRequestFlush(false)
bsalomone2e87f32016-09-22 12:42:11 -070077 , fExternalFlushCnt(0)
robertphillips63926682015-08-20 09:39:02 -070078 , fPreferVRAMUseOverFlushes(caps->preferVRAMUseOverFlushes()) {
bsalomonf320e042015-02-17 15:09:34 -080079 SkDEBUGCODE(fCount = 0;)
halcanary96fcdcc2015-08-27 07:41:13 -070080 SkDEBUGCODE(fNewlyPurgeableResourceForValidation = nullptr;)
bsalomon71cb0c22014-11-14 12:10:14 -080081}
82
bsalomon0ea80f42015-02-11 10:49:59 -080083GrResourceCache::~GrResourceCache() {
bsalomonc8dc1f72014-08-21 13:02:13 -070084 this->releaseAll();
85}
86
bsalomon3f324322015-04-08 11:01:54 -070087void GrResourceCache::setLimits(int count, size_t bytes, int maxUnusedFlushes) {
bsalomon71cb0c22014-11-14 12:10:14 -080088 fMaxCount = count;
89 fMaxBytes = bytes;
bsalomon3f324322015-04-08 11:01:54 -070090 fMaxUnusedFlushes = maxUnusedFlushes;
bsalomon71cb0c22014-11-14 12:10:14 -080091 this->purgeAsNeeded();
92}
93
bsalomon0ea80f42015-02-11 10:49:59 -080094void GrResourceCache::insertResource(GrGpuResource* resource) {
bsalomon49f085d2014-09-05 13:34:00 -070095 SkASSERT(resource);
bsalomon16961262014-08-26 14:01:07 -070096 SkASSERT(!this->isInCache(resource));
bsalomonf320e042015-02-17 15:09:34 -080097 SkASSERT(!resource->wasDestroyed());
98 SkASSERT(!resource->isPurgeable());
bsalomonddf30e62015-02-19 11:38:44 -080099
100 // We must set the timestamp before adding to the array in case the timestamp wraps and we wind
101 // up iterating over all the resources that already have timestamps.
102 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
103
bsalomonf320e042015-02-17 15:09:34 -0800104 this->addToNonpurgeableArray(resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800105
bsalomondace19e2014-11-17 07:34:06 -0800106 size_t size = resource->gpuMemorySize();
bsalomonf320e042015-02-17 15:09:34 -0800107 SkDEBUGCODE(++fCount;)
bsalomon84c8e622014-11-17 09:33:27 -0800108 fBytes += size;
bsalomon82b1d622014-11-14 13:59:57 -0800109#if GR_CACHE_STATS
bsalomonf320e042015-02-17 15:09:34 -0800110 fHighWaterCount = SkTMax(this->getResourceCount(), fHighWaterCount);
bsalomon82b1d622014-11-14 13:59:57 -0800111 fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes);
112#endif
bsalomon5ec26ae2016-02-25 08:33:02 -0800113 if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) {
bsalomondace19e2014-11-17 07:34:06 -0800114 ++fBudgetedCount;
115 fBudgetedBytes += size;
hendrikw876c3132015-03-04 10:33:49 -0800116 TRACE_COUNTER2(TRACE_DISABLED_BY_DEFAULT("skia.gpu.cache"), "skia budget", "used",
117 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomondace19e2014-11-17 07:34:06 -0800118#if GR_CACHE_STATS
119 fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount);
120 fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes);
121#endif
122 }
robertphillipsc4ed6842016-05-24 14:17:12 -0700123 if (resource->resourcePriv().getScratchKey().isValid() &&
124 !resource->getUniqueKey().isValid()) {
kkinnunen2e6055b2016-04-22 01:48:29 -0700125 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
bsalomon3582d3e2015-02-13 14:20:05 -0800126 fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
bsalomon744998e2014-08-28 09:54:34 -0700127 }
bsalomon9f2d1572015-02-17 11:47:40 -0800128
bsalomon71cb0c22014-11-14 12:10:14 -0800129 this->purgeAsNeeded();
bsalomonc8dc1f72014-08-21 13:02:13 -0700130}
131
bsalomon0ea80f42015-02-11 10:49:59 -0800132void GrResourceCache::removeResource(GrGpuResource* resource) {
bsalomon9f2d1572015-02-17 11:47:40 -0800133 this->validate();
bsalomon16961262014-08-26 14:01:07 -0700134 SkASSERT(this->isInCache(resource));
bsalomondace19e2014-11-17 07:34:06 -0800135
bsalomon9f2d1572015-02-17 11:47:40 -0800136 if (resource->isPurgeable()) {
137 fPurgeableQueue.remove(resource);
bsalomonf320e042015-02-17 15:09:34 -0800138 } else {
139 this->removeFromNonpurgeableArray(resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800140 }
141
bsalomondace19e2014-11-17 07:34:06 -0800142 size_t size = resource->gpuMemorySize();
bsalomonf320e042015-02-17 15:09:34 -0800143 SkDEBUGCODE(--fCount;)
bsalomondace19e2014-11-17 07:34:06 -0800144 fBytes -= size;
bsalomon5ec26ae2016-02-25 08:33:02 -0800145 if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) {
bsalomondace19e2014-11-17 07:34:06 -0800146 --fBudgetedCount;
147 fBudgetedBytes -= size;
hendrikw876c3132015-03-04 10:33:49 -0800148 TRACE_COUNTER2(TRACE_DISABLED_BY_DEFAULT("skia.gpu.cache"), "skia budget", "used",
149 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomondace19e2014-11-17 07:34:06 -0800150 }
151
robertphillipsc4ed6842016-05-24 14:17:12 -0700152 if (resource->resourcePriv().getScratchKey().isValid() &&
153 !resource->getUniqueKey().isValid()) {
bsalomon3582d3e2015-02-13 14:20:05 -0800154 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
bsalomon744998e2014-08-28 09:54:34 -0700155 }
bsalomon8718aaf2015-02-19 07:24:21 -0800156 if (resource->getUniqueKey().isValid()) {
157 fUniqueHash.remove(resource->getUniqueKey());
bsalomon8b79d232014-11-10 10:19:06 -0800158 }
bsalomonb436ed62014-11-17 12:15:56 -0800159 this->validate();
bsalomonc8dc1f72014-08-21 13:02:13 -0700160}
161
bsalomon0ea80f42015-02-11 10:49:59 -0800162void GrResourceCache::abandonAll() {
bsalomon71cb0c22014-11-14 12:10:14 -0800163 AutoValidate av(this);
164
bsalomonf320e042015-02-17 15:09:34 -0800165 while (fNonpurgeableResources.count()) {
166 GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
167 SkASSERT(!back->wasDestroyed());
168 back->cacheAccess().abandon();
bsalomonc8dc1f72014-08-21 13:02:13 -0700169 }
bsalomonf320e042015-02-17 15:09:34 -0800170
171 while (fPurgeableQueue.count()) {
172 GrGpuResource* top = fPurgeableQueue.peek();
173 SkASSERT(!top->wasDestroyed());
174 top->cacheAccess().abandon();
175 }
176
bsalomon744998e2014-08-28 09:54:34 -0700177 SkASSERT(!fScratchMap.count());
bsalomon8718aaf2015-02-19 07:24:21 -0800178 SkASSERT(!fUniqueHash.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700179 SkASSERT(!fCount);
bsalomonf320e042015-02-17 15:09:34 -0800180 SkASSERT(!this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800181 SkASSERT(!fBytes);
182 SkASSERT(!fBudgetedCount);
183 SkASSERT(!fBudgetedBytes);
bsalomonc8dc1f72014-08-21 13:02:13 -0700184}
185
bsalomon0ea80f42015-02-11 10:49:59 -0800186void GrResourceCache::releaseAll() {
bsalomon71cb0c22014-11-14 12:10:14 -0800187 AutoValidate av(this);
188
bsalomonf320e042015-02-17 15:09:34 -0800189 while(fNonpurgeableResources.count()) {
190 GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
191 SkASSERT(!back->wasDestroyed());
192 back->cacheAccess().release();
bsalomonc8dc1f72014-08-21 13:02:13 -0700193 }
bsalomonf320e042015-02-17 15:09:34 -0800194
195 while (fPurgeableQueue.count()) {
196 GrGpuResource* top = fPurgeableQueue.peek();
197 SkASSERT(!top->wasDestroyed());
198 top->cacheAccess().release();
199 }
200
bsalomon744998e2014-08-28 09:54:34 -0700201 SkASSERT(!fScratchMap.count());
bsalomon8718aaf2015-02-19 07:24:21 -0800202 SkASSERT(!fUniqueHash.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700203 SkASSERT(!fCount);
bsalomonf320e042015-02-17 15:09:34 -0800204 SkASSERT(!this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800205 SkASSERT(!fBytes);
206 SkASSERT(!fBudgetedCount);
207 SkASSERT(!fBudgetedBytes);
bsalomonc8dc1f72014-08-21 13:02:13 -0700208}
bsalomonbcf0a522014-10-08 08:40:09 -0700209
bsalomon0ea80f42015-02-11 10:49:59 -0800210class GrResourceCache::AvailableForScratchUse {
bsalomonbcf0a522014-10-08 08:40:09 -0700211public:
bsalomon000f8292014-10-15 19:04:14 -0700212 AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendingIO) { }
bsalomonbcf0a522014-10-08 08:40:09 -0700213
214 bool operator()(const GrGpuResource* resource) const {
robertphillipsc4ed6842016-05-24 14:17:12 -0700215 SkASSERT(!resource->getUniqueKey().isValid() &&
216 resource->resourcePriv().getScratchKey().isValid());
bsalomon12299ab2014-11-14 13:33:09 -0800217 if (resource->internalHasRef() || !resource->cacheAccess().isScratch()) {
bsalomon000f8292014-10-15 19:04:14 -0700218 return false;
bsalomonbcf0a522014-10-08 08:40:09 -0700219 }
bsalomon000f8292014-10-15 19:04:14 -0700220 return !fRejectPendingIO || !resource->internalHasPendingIO();
bsalomonbcf0a522014-10-08 08:40:09 -0700221 }
bsalomon1e2530b2014-10-09 09:57:18 -0700222
bsalomonbcf0a522014-10-08 08:40:09 -0700223private:
bsalomon000f8292014-10-15 19:04:14 -0700224 bool fRejectPendingIO;
bsalomonbcf0a522014-10-08 08:40:09 -0700225};
226
bsalomon0ea80f42015-02-11 10:49:59 -0800227GrGpuResource* GrResourceCache::findAndRefScratchResource(const GrScratchKey& scratchKey,
robertphillips6e83ac72015-08-13 05:19:14 -0700228 size_t resourceSize,
bsalomon9f2d1572015-02-17 11:47:40 -0800229 uint32_t flags) {
bsalomon7775c852014-12-30 12:50:52 -0800230 SkASSERT(scratchKey.isValid());
bsalomon000f8292014-10-15 19:04:14 -0700231
bsalomon71cb0c22014-11-14 12:10:14 -0800232 GrGpuResource* resource;
bsalomon000f8292014-10-15 19:04:14 -0700233 if (flags & (kPreferNoPendingIO_ScratchFlag | kRequireNoPendingIO_ScratchFlag)) {
bsalomon71cb0c22014-11-14 12:10:14 -0800234 resource = fScratchMap.find(scratchKey, AvailableForScratchUse(true));
bsalomon000f8292014-10-15 19:04:14 -0700235 if (resource) {
bsalomon9f2d1572015-02-17 11:47:40 -0800236 this->refAndMakeResourceMRU(resource);
bsalomonb436ed62014-11-17 12:15:56 -0800237 this->validate();
238 return resource;
bsalomon000f8292014-10-15 19:04:14 -0700239 } else if (flags & kRequireNoPendingIO_ScratchFlag) {
halcanary96fcdcc2015-08-27 07:41:13 -0700240 return nullptr;
bsalomon000f8292014-10-15 19:04:14 -0700241 }
robertphillips63926682015-08-20 09:39:02 -0700242 // We would prefer to consume more available VRAM rather than flushing
243 // immediately, but on ANGLE this can lead to starving of the GPU.
244 if (fPreferVRAMUseOverFlushes && this->wouldFit(resourceSize)) {
robertphillips6e83ac72015-08-13 05:19:14 -0700245 // kPrefer is specified, we didn't find a resource without pending io,
robertphillips63926682015-08-20 09:39:02 -0700246 // but there is still space in our budget for the resource so force
247 // the caller to allocate a new resource.
halcanary96fcdcc2015-08-27 07:41:13 -0700248 return nullptr;
robertphillips6e83ac72015-08-13 05:19:14 -0700249 }
bsalomon000f8292014-10-15 19:04:14 -0700250 }
bsalomon71cb0c22014-11-14 12:10:14 -0800251 resource = fScratchMap.find(scratchKey, AvailableForScratchUse(false));
252 if (resource) {
bsalomon9f2d1572015-02-17 11:47:40 -0800253 this->refAndMakeResourceMRU(resource);
bsalomonb436ed62014-11-17 12:15:56 -0800254 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800255 }
256 return resource;
bsalomonbcf0a522014-10-08 08:40:09 -0700257}
bsalomon8b79d232014-11-10 10:19:06 -0800258
bsalomon0ea80f42015-02-11 10:49:59 -0800259void GrResourceCache::willRemoveScratchKey(const GrGpuResource* resource) {
bsalomon3582d3e2015-02-13 14:20:05 -0800260 SkASSERT(resource->resourcePriv().getScratchKey().isValid());
robertphillipsc4ed6842016-05-24 14:17:12 -0700261 if (!resource->getUniqueKey().isValid()) {
262 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
263 }
bsalomon10e23ca2014-11-25 05:52:06 -0800264}
265
bsalomonf99e9612015-02-19 08:24:16 -0800266void GrResourceCache::removeUniqueKey(GrGpuResource* resource) {
bsalomon3f324322015-04-08 11:01:54 -0700267 // Someone has a ref to this resource in order to have removed the key. When the ref count
268 // reaches zero we will get a ref cnt notification and figure out what to do with it.
bsalomonf99e9612015-02-19 08:24:16 -0800269 if (resource->getUniqueKey().isValid()) {
270 SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
271 fUniqueHash.remove(resource->getUniqueKey());
272 }
273 resource->cacheAccess().removeUniqueKey();
robertphillipsc4ed6842016-05-24 14:17:12 -0700274
275 if (resource->resourcePriv().getScratchKey().isValid()) {
276 fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
277 }
278
bsalomonf99e9612015-02-19 08:24:16 -0800279 this->validate();
bsalomon23e619c2015-02-06 11:54:28 -0800280}
281
bsalomonf99e9612015-02-19 08:24:16 -0800282void GrResourceCache::changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) {
bsalomon8b79d232014-11-10 10:19:06 -0800283 SkASSERT(resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800284 SkASSERT(this->isInCache(resource));
bsalomon8b79d232014-11-10 10:19:06 -0800285
bsalomonf99e9612015-02-19 08:24:16 -0800286 // If another resource has the new key, remove its key then install the key on this resource.
287 if (newKey.isValid()) {
robertphillipsc4ed6842016-05-24 14:17:12 -0700288 // Remove the entry for this resource if it already has a unique key.
289 if (resource->getUniqueKey().isValid()) {
290 SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
291 fUniqueHash.remove(resource->getUniqueKey());
292 SkASSERT(nullptr == fUniqueHash.find(resource->getUniqueKey()));
293 } else {
294 // 'resource' didn't have a valid unique key before so it is switching sides. Remove it
295 // from the ScratchMap
296 if (resource->resourcePriv().getScratchKey().isValid()) {
297 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
298 }
299 }
300
bsalomonf99e9612015-02-19 08:24:16 -0800301 if (GrGpuResource* old = fUniqueHash.find(newKey)) {
302 // If the old resource using the key is purgeable and is unreachable, then remove it.
303 if (!old->resourcePriv().getScratchKey().isValid() && old->isPurgeable()) {
304 // release may call validate() which will assert that resource is in fUniqueHash
305 // if it has a valid key. So in debug reset the key here before we assign it.
306 SkDEBUGCODE(resource->cacheAccess().removeUniqueKey();)
307 old->cacheAccess().release();
308 } else {
robertphillipsc4ed6842016-05-24 14:17:12 -0700309 this->removeUniqueKey(old);
bsalomonf99e9612015-02-19 08:24:16 -0800310 }
311 }
halcanary96fcdcc2015-08-27 07:41:13 -0700312 SkASSERT(nullptr == fUniqueHash.find(newKey));
bsalomonf99e9612015-02-19 08:24:16 -0800313 resource->cacheAccess().setUniqueKey(newKey);
314 fUniqueHash.add(resource);
315 } else {
robertphillipsc4ed6842016-05-24 14:17:12 -0700316 this->removeUniqueKey(resource);
bsalomonf99e9612015-02-19 08:24:16 -0800317 }
318
bsalomon71cb0c22014-11-14 12:10:14 -0800319 this->validate();
bsalomon8b79d232014-11-10 10:19:06 -0800320}
bsalomon71cb0c22014-11-14 12:10:14 -0800321
bsalomon9f2d1572015-02-17 11:47:40 -0800322void GrResourceCache::refAndMakeResourceMRU(GrGpuResource* resource) {
bsalomon71cb0c22014-11-14 12:10:14 -0800323 SkASSERT(resource);
324 SkASSERT(this->isInCache(resource));
bsalomonddf30e62015-02-19 11:38:44 -0800325
bsalomon9f2d1572015-02-17 11:47:40 -0800326 if (resource->isPurgeable()) {
327 // It's about to become unpurgeable.
328 fPurgeableQueue.remove(resource);
bsalomonf320e042015-02-17 15:09:34 -0800329 this->addToNonpurgeableArray(resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800330 }
331 resource->ref();
bsalomonddf30e62015-02-19 11:38:44 -0800332
333 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
bsalomonf320e042015-02-17 15:09:34 -0800334 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800335}
336
bsalomon3f324322015-04-08 11:01:54 -0700337void GrResourceCache::notifyCntReachedZero(GrGpuResource* resource, uint32_t flags) {
bsalomon71cb0c22014-11-14 12:10:14 -0800338 SkASSERT(resource);
bsalomon3f324322015-04-08 11:01:54 -0700339 SkASSERT(!resource->wasDestroyed());
340 SkASSERT(flags);
bsalomon71cb0c22014-11-14 12:10:14 -0800341 SkASSERT(this->isInCache(resource));
bsalomon3f324322015-04-08 11:01:54 -0700342 // This resource should always be in the nonpurgeable array when this function is called. It
343 // will be moved to the queue if it is newly purgeable.
344 SkASSERT(fNonpurgeableResources[*resource->cacheAccess().accessCacheIndex()] == resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800345
bsalomon3f324322015-04-08 11:01:54 -0700346 if (SkToBool(ResourceAccess::kRefCntReachedZero_RefNotificationFlag & flags)) {
347#ifdef SK_DEBUG
348 // When the timestamp overflows validate() is called. validate() checks that resources in
349 // the nonpurgeable array are indeed not purgeable. However, the movement from the array to
350 // the purgeable queue happens just below in this function. So we mark it as an exception.
351 if (resource->isPurgeable()) {
352 fNewlyPurgeableResourceForValidation = resource;
353 }
354#endif
355 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
halcanary96fcdcc2015-08-27 07:41:13 -0700356 SkDEBUGCODE(fNewlyPurgeableResourceForValidation = nullptr);
bsalomon3f324322015-04-08 11:01:54 -0700357 }
358
359 if (!SkToBool(ResourceAccess::kAllCntsReachedZero_RefNotificationFlag & flags)) {
360 SkASSERT(!resource->isPurgeable());
361 return;
362 }
363
364 SkASSERT(resource->isPurgeable());
bsalomonf320e042015-02-17 15:09:34 -0800365 this->removeFromNonpurgeableArray(resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800366 fPurgeableQueue.insert(resource);
bsalomone2e87f32016-09-22 12:42:11 -0700367 resource->cacheAccess().setFlushCntWhenResourceBecamePurgeable(fExternalFlushCnt);
bsalomon71cb0c22014-11-14 12:10:14 -0800368
bsalomon5ec26ae2016-02-25 08:33:02 -0800369 if (SkBudgeted::kNo == resource->resourcePriv().isBudgeted()) {
bsalomonc2f35b72015-01-23 07:19:22 -0800370 // Check whether this resource could still be used as a scratch resource.
kkinnunen2e6055b2016-04-22 01:48:29 -0700371 if (!resource->resourcePriv().refsWrappedObjects() &&
bsalomon9f2d1572015-02-17 11:47:40 -0800372 resource->resourcePriv().getScratchKey().isValid()) {
bsalomonc2f35b72015-01-23 07:19:22 -0800373 // We won't purge an existing resource to make room for this one.
bsalomonf320e042015-02-17 15:09:34 -0800374 if (fBudgetedCount < fMaxCount &&
375 fBudgetedBytes + resource->gpuMemorySize() <= fMaxBytes) {
bsalomon3582d3e2015-02-13 14:20:05 -0800376 resource->resourcePriv().makeBudgeted();
bsalomon9f2d1572015-02-17 11:47:40 -0800377 return;
bsalomonc2f35b72015-01-23 07:19:22 -0800378 }
bsalomonc2f35b72015-01-23 07:19:22 -0800379 }
380 } else {
bsalomon9f2d1572015-02-17 11:47:40 -0800381 // Purge the resource immediately if we're over budget
bsalomon8718aaf2015-02-19 07:24:21 -0800382 // Also purge if the resource has neither a valid scratch key nor a unique key.
bsalomon3582d3e2015-02-13 14:20:05 -0800383 bool noKey = !resource->resourcePriv().getScratchKey().isValid() &&
bsalomon8718aaf2015-02-19 07:24:21 -0800384 !resource->getUniqueKey().isValid();
bsalomonf320e042015-02-17 15:09:34 -0800385 if (!this->overBudget() && !noKey) {
bsalomon9f2d1572015-02-17 11:47:40 -0800386 return;
bsalomonc2f35b72015-01-23 07:19:22 -0800387 }
388 }
bsalomondace19e2014-11-17 07:34:06 -0800389
bsalomonf320e042015-02-17 15:09:34 -0800390 SkDEBUGCODE(int beforeCount = this->getResourceCount();)
bsalomon9f2d1572015-02-17 11:47:40 -0800391 resource->cacheAccess().release();
392 // We should at least free this resource, perhaps dependent resources as well.
bsalomonf320e042015-02-17 15:09:34 -0800393 SkASSERT(this->getResourceCount() < beforeCount);
bsalomon71cb0c22014-11-14 12:10:14 -0800394 this->validate();
395}
396
bsalomon0ea80f42015-02-11 10:49:59 -0800397void GrResourceCache::didChangeGpuMemorySize(const GrGpuResource* resource, size_t oldSize) {
bsalomon71cb0c22014-11-14 12:10:14 -0800398 // SkASSERT(!fPurging); GrPathRange increases size during flush. :(
399 SkASSERT(resource);
400 SkASSERT(this->isInCache(resource));
401
bsalomondace19e2014-11-17 07:34:06 -0800402 ptrdiff_t delta = resource->gpuMemorySize() - oldSize;
403
404 fBytes += delta;
bsalomon82b1d622014-11-14 13:59:57 -0800405#if GR_CACHE_STATS
406 fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes);
407#endif
bsalomon5ec26ae2016-02-25 08:33:02 -0800408 if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) {
bsalomondace19e2014-11-17 07:34:06 -0800409 fBudgetedBytes += delta;
hendrikw876c3132015-03-04 10:33:49 -0800410 TRACE_COUNTER2(TRACE_DISABLED_BY_DEFAULT("skia.gpu.cache"), "skia budget", "used",
411 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomondace19e2014-11-17 07:34:06 -0800412#if GR_CACHE_STATS
413 fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes);
414#endif
415 }
bsalomon71cb0c22014-11-14 12:10:14 -0800416
417 this->purgeAsNeeded();
418 this->validate();
419}
420
bsalomon0ea80f42015-02-11 10:49:59 -0800421void GrResourceCache::didChangeBudgetStatus(GrGpuResource* resource) {
bsalomon84c8e622014-11-17 09:33:27 -0800422 SkASSERT(resource);
423 SkASSERT(this->isInCache(resource));
424
425 size_t size = resource->gpuMemorySize();
426
bsalomon5ec26ae2016-02-25 08:33:02 -0800427 if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) {
bsalomon84c8e622014-11-17 09:33:27 -0800428 ++fBudgetedCount;
429 fBudgetedBytes += size;
bsalomonafe30052015-01-16 07:32:33 -0800430#if GR_CACHE_STATS
431 fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes);
432 fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount);
433#endif
bsalomon84c8e622014-11-17 09:33:27 -0800434 this->purgeAsNeeded();
435 } else {
436 --fBudgetedCount;
437 fBudgetedBytes -= size;
438 }
hendrikw876c3132015-03-04 10:33:49 -0800439 TRACE_COUNTER2(TRACE_DISABLED_BY_DEFAULT("skia.gpu.cache"), "skia budget", "used",
440 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomon84c8e622014-11-17 09:33:27 -0800441
442 this->validate();
443}
444
bsalomon3f324322015-04-08 11:01:54 -0700445void GrResourceCache::purgeAsNeeded() {
446 SkTArray<GrUniqueKeyInvalidatedMessage> invalidKeyMsgs;
447 fInvalidUniqueKeyInbox.poll(&invalidKeyMsgs);
448 if (invalidKeyMsgs.count()) {
449 this->processInvalidUniqueKeys(invalidKeyMsgs);
450 }
bsalomon71cb0c22014-11-14 12:10:14 -0800451
bsalomone2e87f32016-09-22 12:42:11 -0700452 if (fMaxUnusedFlushes > 0) {
453 // We want to know how many complete flushes have occurred without the resource being used.
454 // If the resource was tagged when fExternalFlushCnt was N then this means it became
455 // purgeable during activity that became the N+1th flush. So when the flush count is N+2
456 // it has sat in the purgeable queue for one entire flush.
457 uint32_t oldestAllowedFlushCnt = fExternalFlushCnt - fMaxUnusedFlushes - 1;
458 // check for underflow
459 if (oldestAllowedFlushCnt < fExternalFlushCnt) {
460 while (fPurgeableQueue.count()) {
461 uint32_t flushWhenResourceBecamePurgeable =
462 fPurgeableQueue.peek()->cacheAccess().flushCntWhenResourceBecamePurgeable();
463 if (oldestAllowedFlushCnt < flushWhenResourceBecamePurgeable) {
464 // Resources were given both LRU timestamps and tagged with a flush cnt when
465 // they first became purgeable. The LRU timestamp won't change again until the
466 // resource is made non-purgeable again. So, at this point all the remaining
467 // resources in the timestamp-sorted queue will have a flush count >= to this
468 // one.
469 break;
470 }
471 GrGpuResource* resource = fPurgeableQueue.peek();
472 SkASSERT(resource->isPurgeable());
473 resource->cacheAccess().release();
bsalomon3f324322015-04-08 11:01:54 -0700474 }
bsalomon3f324322015-04-08 11:01:54 -0700475 }
476 }
477
478 bool stillOverbudget = this->overBudget();
479 while (stillOverbudget && fPurgeableQueue.count()) {
bsalomon9f2d1572015-02-17 11:47:40 -0800480 GrGpuResource* resource = fPurgeableQueue.peek();
481 SkASSERT(resource->isPurgeable());
482 resource->cacheAccess().release();
bsalomon3f324322015-04-08 11:01:54 -0700483 stillOverbudget = this->overBudget();
bsalomon9f2d1572015-02-17 11:47:40 -0800484 }
bsalomon71cb0c22014-11-14 12:10:14 -0800485
bsalomonb436ed62014-11-17 12:15:56 -0800486 this->validate();
bsalomon9f2d1572015-02-17 11:47:40 -0800487
488 if (stillOverbudget) {
bsalomonb77a9072016-09-07 10:02:04 -0700489 // Set this so that GrDrawingManager will issue a flush to free up resources with pending
490 // IO that we were unable to purge in this pass.
491 fRequestFlush = true;
bsalomon9f2d1572015-02-17 11:47:40 -0800492 }
bsalomon71cb0c22014-11-14 12:10:14 -0800493}
494
bsalomon0ea80f42015-02-11 10:49:59 -0800495void GrResourceCache::purgeAllUnlocked() {
bsalomon9f2d1572015-02-17 11:47:40 -0800496 // We could disable maintaining the heap property here, but it would add a lot of complexity.
497 // Moreover, this is rarely called.
498 while (fPurgeableQueue.count()) {
499 GrGpuResource* resource = fPurgeableQueue.peek();
500 SkASSERT(resource->isPurgeable());
501 resource->cacheAccess().release();
502 }
bsalomon71cb0c22014-11-14 12:10:14 -0800503
bsalomonb436ed62014-11-17 12:15:56 -0800504 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800505}
506
bsalomon8718aaf2015-02-19 07:24:21 -0800507void GrResourceCache::processInvalidUniqueKeys(
508 const SkTArray<GrUniqueKeyInvalidatedMessage>& msgs) {
bsalomon23e619c2015-02-06 11:54:28 -0800509 for (int i = 0; i < msgs.count(); ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -0800510 GrGpuResource* resource = this->findAndRefUniqueResource(msgs[i].key());
bsalomon23e619c2015-02-06 11:54:28 -0800511 if (resource) {
bsalomon8718aaf2015-02-19 07:24:21 -0800512 resource->resourcePriv().removeUniqueKey();
bsalomon3f324322015-04-08 11:01:54 -0700513 resource->unref(); // If this resource is now purgeable, the cache will be notified.
bsalomon23e619c2015-02-06 11:54:28 -0800514 }
515 }
516}
517
bsalomonf320e042015-02-17 15:09:34 -0800518void GrResourceCache::addToNonpurgeableArray(GrGpuResource* resource) {
519 int index = fNonpurgeableResources.count();
520 *fNonpurgeableResources.append() = resource;
521 *resource->cacheAccess().accessCacheIndex() = index;
522}
523
524void GrResourceCache::removeFromNonpurgeableArray(GrGpuResource* resource) {
525 int* index = resource->cacheAccess().accessCacheIndex();
526 // Fill the whole we will create in the array with the tail object, adjust its index, and
527 // then pop the array
528 GrGpuResource* tail = *(fNonpurgeableResources.end() - 1);
529 SkASSERT(fNonpurgeableResources[*index] == resource);
530 fNonpurgeableResources[*index] = tail;
531 *tail->cacheAccess().accessCacheIndex() = *index;
532 fNonpurgeableResources.pop();
533 SkDEBUGCODE(*index = -1);
534}
535
bsalomonddf30e62015-02-19 11:38:44 -0800536uint32_t GrResourceCache::getNextTimestamp() {
537 // If we wrap then all the existing resources will appear older than any resources that get
538 // a timestamp after the wrap.
539 if (0 == fTimestamp) {
540 int count = this->getResourceCount();
541 if (count) {
542 // Reset all the timestamps. We sort the resources by timestamp and then assign
543 // sequential timestamps beginning with 0. This is O(n*lg(n)) but it should be extremely
544 // rare.
545 SkTDArray<GrGpuResource*> sortedPurgeableResources;
546 sortedPurgeableResources.setReserve(fPurgeableQueue.count());
547
548 while (fPurgeableQueue.count()) {
549 *sortedPurgeableResources.append() = fPurgeableQueue.peek();
550 fPurgeableQueue.pop();
551 }
552
bsalomone2e87f32016-09-22 12:42:11 -0700553 SkTQSort(fNonpurgeableResources.begin(), fNonpurgeableResources.end() - 1,
554 CompareTimestamp);
bsalomonddf30e62015-02-19 11:38:44 -0800555
556 // Pick resources out of the purgeable and non-purgeable arrays based on lowest
557 // timestamp and assign new timestamps.
558 int currP = 0;
559 int currNP = 0;
560 while (currP < sortedPurgeableResources.count() &&
mtklein56da0252015-11-16 11:16:23 -0800561 currNP < fNonpurgeableResources.count()) {
bsalomonddf30e62015-02-19 11:38:44 -0800562 uint32_t tsP = sortedPurgeableResources[currP]->cacheAccess().timestamp();
563 uint32_t tsNP = fNonpurgeableResources[currNP]->cacheAccess().timestamp();
564 SkASSERT(tsP != tsNP);
565 if (tsP < tsNP) {
566 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
567 } else {
568 // Correct the index in the nonpurgeable array stored on the resource post-sort.
569 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
570 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
571 }
572 }
573
574 // The above loop ended when we hit the end of one array. Finish the other one.
575 while (currP < sortedPurgeableResources.count()) {
576 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
577 }
578 while (currNP < fNonpurgeableResources.count()) {
579 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
580 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
581 }
582
583 // Rebuild the queue.
584 for (int i = 0; i < sortedPurgeableResources.count(); ++i) {
585 fPurgeableQueue.insert(sortedPurgeableResources[i]);
586 }
587
588 this->validate();
589 SkASSERT(count == this->getResourceCount());
590
591 // count should be the next timestamp we return.
592 SkASSERT(fTimestamp == SkToU32(count));
mtklein56da0252015-11-16 11:16:23 -0800593 }
bsalomonddf30e62015-02-19 11:38:44 -0800594 }
595 return fTimestamp++;
596}
597
bsalomonb77a9072016-09-07 10:02:04 -0700598void GrResourceCache::notifyFlushOccurred(FlushType type) {
599 switch (type) {
600 case FlushType::kImmediateMode:
601 break;
602 case FlushType::kCacheRequested:
603 SkASSERT(fRequestFlush);
604 fRequestFlush = false;
605 break;
606 case FlushType::kExternal:
bsalomone2e87f32016-09-22 12:42:11 -0700607 ++fExternalFlushCnt;
608 if (0 == fExternalFlushCnt) {
609 // When this wraps just reset all the purgeable resources' last used flush state.
610 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
611 fPurgeableQueue.at(i)->cacheAccess().setFlushCntWhenResourceBecamePurgeable(0);
612 }
bsalomonb77a9072016-09-07 10:02:04 -0700613 }
614 break;
bsalomon3f324322015-04-08 11:01:54 -0700615 }
bsalomonb77a9072016-09-07 10:02:04 -0700616 this->purgeAsNeeded();
bsalomon3f324322015-04-08 11:01:54 -0700617}
618
ericrk0a5fa482015-09-15 14:16:10 -0700619void GrResourceCache::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
620 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
621 fNonpurgeableResources[i]->dumpMemoryStatistics(traceMemoryDump);
622 }
623 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
624 fPurgeableQueue.at(i)->dumpMemoryStatistics(traceMemoryDump);
625 }
626}
627
bsalomon71cb0c22014-11-14 12:10:14 -0800628#ifdef SK_DEBUG
bsalomon0ea80f42015-02-11 10:49:59 -0800629void GrResourceCache::validate() const {
bsalomonc2f35b72015-01-23 07:19:22 -0800630 // Reduce the frequency of validations for large resource counts.
631 static SkRandom gRandom;
632 int mask = (SkNextPow2(fCount + 1) >> 5) - 1;
633 if (~mask && (gRandom.nextU() & mask)) {
634 return;
635 }
636
bsalomonf320e042015-02-17 15:09:34 -0800637 struct Stats {
638 size_t fBytes;
639 int fBudgetedCount;
640 size_t fBudgetedBytes;
641 int fLocked;
642 int fScratch;
643 int fCouldBeScratch;
644 int fContent;
645 const ScratchMap* fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800646 const UniqueHash* fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800647
bsalomonf320e042015-02-17 15:09:34 -0800648 Stats(const GrResourceCache* cache) {
649 memset(this, 0, sizeof(*this));
650 fScratchMap = &cache->fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800651 fUniqueHash = &cache->fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800652 }
653
bsalomonf320e042015-02-17 15:09:34 -0800654 void update(GrGpuResource* resource) {
655 fBytes += resource->gpuMemorySize();
bsalomondace19e2014-11-17 07:34:06 -0800656
bsalomonf320e042015-02-17 15:09:34 -0800657 if (!resource->isPurgeable()) {
658 ++fLocked;
659 }
bsalomon9f2d1572015-02-17 11:47:40 -0800660
robertphillipsc4ed6842016-05-24 14:17:12 -0700661 const GrScratchKey& scratchKey = resource->resourcePriv().getScratchKey();
662 const GrUniqueKey& uniqueKey = resource->getUniqueKey();
663
bsalomonf320e042015-02-17 15:09:34 -0800664 if (resource->cacheAccess().isScratch()) {
robertphillipsc4ed6842016-05-24 14:17:12 -0700665 SkASSERT(!uniqueKey.isValid());
bsalomonf320e042015-02-17 15:09:34 -0800666 ++fScratch;
robertphillipsc4ed6842016-05-24 14:17:12 -0700667 SkASSERT(fScratchMap->countForKey(scratchKey));
kkinnunen2e6055b2016-04-22 01:48:29 -0700668 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
robertphillipsc4ed6842016-05-24 14:17:12 -0700669 } else if (scratchKey.isValid()) {
bsalomon5ec26ae2016-02-25 08:33:02 -0800670 SkASSERT(SkBudgeted::kNo == resource->resourcePriv().isBudgeted() ||
robertphillipsc4ed6842016-05-24 14:17:12 -0700671 uniqueKey.isValid());
672 if (!uniqueKey.isValid()) {
mtklein4e976072016-08-08 09:06:27 -0700673 ++fCouldBeScratch;
robertphillipsc4ed6842016-05-24 14:17:12 -0700674 SkASSERT(fScratchMap->countForKey(scratchKey));
675 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700676 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
bsalomonf320e042015-02-17 15:09:34 -0800677 }
bsalomon8718aaf2015-02-19 07:24:21 -0800678 if (uniqueKey.isValid()) {
bsalomonf320e042015-02-17 15:09:34 -0800679 ++fContent;
bsalomon8718aaf2015-02-19 07:24:21 -0800680 SkASSERT(fUniqueHash->find(uniqueKey) == resource);
kkinnunen2e6055b2016-04-22 01:48:29 -0700681 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
bsalomon5ec26ae2016-02-25 08:33:02 -0800682 SkASSERT(SkBudgeted::kYes == resource->resourcePriv().isBudgeted());
robertphillipsc4ed6842016-05-24 14:17:12 -0700683
684 if (scratchKey.isValid()) {
685 SkASSERT(!fScratchMap->has(resource, scratchKey));
686 }
bsalomonf320e042015-02-17 15:09:34 -0800687 }
688
bsalomon5ec26ae2016-02-25 08:33:02 -0800689 if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) {
bsalomonf320e042015-02-17 15:09:34 -0800690 ++fBudgetedCount;
691 fBudgetedBytes += resource->gpuMemorySize();
692 }
bsalomon9f2d1572015-02-17 11:47:40 -0800693 }
bsalomonf320e042015-02-17 15:09:34 -0800694 };
695
robertphillipsc4ed6842016-05-24 14:17:12 -0700696 {
697 ScratchMap::ConstIter iter(&fScratchMap);
698
699 int count = 0;
700 for ( ; !iter.done(); ++iter) {
701 const GrGpuResource* resource = *iter;
702 SkASSERT(resource->resourcePriv().getScratchKey().isValid());
703 SkASSERT(!resource->getUniqueKey().isValid());
704 count++;
705 }
706 SkASSERT(count == fScratchMap.count()); // ensure the iterator is working correctly
707 }
708
bsalomonf320e042015-02-17 15:09:34 -0800709 Stats stats(this);
710
711 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
bsalomon3f324322015-04-08 11:01:54 -0700712 SkASSERT(!fNonpurgeableResources[i]->isPurgeable() ||
713 fNewlyPurgeableResourceForValidation == fNonpurgeableResources[i]);
bsalomonf320e042015-02-17 15:09:34 -0800714 SkASSERT(*fNonpurgeableResources[i]->cacheAccess().accessCacheIndex() == i);
715 SkASSERT(!fNonpurgeableResources[i]->wasDestroyed());
716 stats.update(fNonpurgeableResources[i]);
bsalomon71cb0c22014-11-14 12:10:14 -0800717 }
bsalomon9f2d1572015-02-17 11:47:40 -0800718 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
719 SkASSERT(fPurgeableQueue.at(i)->isPurgeable());
bsalomonf320e042015-02-17 15:09:34 -0800720 SkASSERT(*fPurgeableQueue.at(i)->cacheAccess().accessCacheIndex() == i);
721 SkASSERT(!fPurgeableQueue.at(i)->wasDestroyed());
722 stats.update(fPurgeableQueue.at(i));
bsalomon9f2d1572015-02-17 11:47:40 -0800723 }
724
bsalomonf320e042015-02-17 15:09:34 -0800725 SkASSERT(fCount == this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800726 SkASSERT(fBudgetedCount <= fCount);
bsalomonf320e042015-02-17 15:09:34 -0800727 SkASSERT(fBudgetedBytes <= fBytes);
728 SkASSERT(stats.fBytes == fBytes);
729 SkASSERT(stats.fBudgetedBytes == fBudgetedBytes);
730 SkASSERT(stats.fBudgetedCount == fBudgetedCount);
bsalomon71cb0c22014-11-14 12:10:14 -0800731#if GR_CACHE_STATS
bsalomondace19e2014-11-17 07:34:06 -0800732 SkASSERT(fBudgetedHighWaterCount <= fHighWaterCount);
733 SkASSERT(fBudgetedHighWaterBytes <= fHighWaterBytes);
bsalomonf320e042015-02-17 15:09:34 -0800734 SkASSERT(fBytes <= fHighWaterBytes);
735 SkASSERT(fCount <= fHighWaterCount);
736 SkASSERT(fBudgetedBytes <= fBudgetedHighWaterBytes);
737 SkASSERT(fBudgetedCount <= fBudgetedHighWaterCount);
bsalomon71cb0c22014-11-14 12:10:14 -0800738#endif
bsalomon8718aaf2015-02-19 07:24:21 -0800739 SkASSERT(stats.fContent == fUniqueHash.count());
bsalomonf320e042015-02-17 15:09:34 -0800740 SkASSERT(stats.fScratch + stats.fCouldBeScratch == fScratchMap.count());
bsalomon71cb0c22014-11-14 12:10:14 -0800741
bsalomon3f324322015-04-08 11:01:54 -0700742 // This assertion is not currently valid because we can be in recursive notifyCntReachedZero()
bsalomon12299ab2014-11-14 13:33:09 -0800743 // calls. This will be fixed when subresource registration is explicit.
bsalomondace19e2014-11-17 07:34:06 -0800744 // bool overBudget = budgetedBytes > fMaxBytes || budgetedCount > fMaxCount;
bsalomon12299ab2014-11-14 13:33:09 -0800745 // SkASSERT(!overBudget || locked == count || fPurging);
bsalomon71cb0c22014-11-14 12:10:14 -0800746}
bsalomonf320e042015-02-17 15:09:34 -0800747
748bool GrResourceCache::isInCache(const GrGpuResource* resource) const {
749 int index = *resource->cacheAccess().accessCacheIndex();
750 if (index < 0) {
751 return false;
752 }
753 if (index < fPurgeableQueue.count() && fPurgeableQueue.at(index) == resource) {
754 return true;
755 }
756 if (index < fNonpurgeableResources.count() && fNonpurgeableResources[index] == resource) {
757 return true;
758 }
759 SkDEBUGFAIL("Resource index should be -1 or the resource should be in the cache.");
760 return false;
761}
762
bsalomon71cb0c22014-11-14 12:10:14 -0800763#endif