blob: c207b44a38c0ea4ebca2cafffd8595142e4a04c4 [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"
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"
bsalomon71cb0c22014-11-14 12:10:14 -080020
bsalomon8718aaf2015-02-19 07:24:21 -080021DECLARE_SKMESSAGEBUS_MESSAGE(GrUniqueKeyInvalidatedMessage);
bsalomon71cb0c22014-11-14 12:10:14 -080022
Brian Osman13dddce2017-05-09 13:19:50 -040023DECLARE_SKMESSAGEBUS_MESSAGE(GrGpuResourceFreedMessage);
24
bsalomon71cb0c22014-11-14 12:10:14 -080025//////////////////////////////////////////////////////////////////////////////
26
bsalomon7775c852014-12-30 12:50:52 -080027GrScratchKey::ResourceType GrScratchKey::GenerateResourceType() {
bsalomon24db3b12015-01-23 04:24:04 -080028 static int32_t gType = INHERITED::kInvalidDomain + 1;
bsalomonfe369ee2014-11-10 11:59:06 -080029
bsalomon7775c852014-12-30 12:50:52 -080030 int32_t type = sk_atomic_inc(&gType);
robertphillips9790a7b2015-01-05 12:29:15 -080031 if (type > SK_MaxU16) {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040032 SK_ABORT("Too many Resource Types");
bsalomon71cb0c22014-11-14 12:10:14 -080033 }
34
35 return static_cast<ResourceType>(type);
36}
37
bsalomon8718aaf2015-02-19 07:24:21 -080038GrUniqueKey::Domain GrUniqueKey::GenerateDomain() {
bsalomon24db3b12015-01-23 04:24:04 -080039 static int32_t gDomain = INHERITED::kInvalidDomain + 1;
bsalomon7775c852014-12-30 12:50:52 -080040
bsalomon24db3b12015-01-23 04:24:04 -080041 int32_t domain = sk_atomic_inc(&gDomain);
kkinnunen016dffb2015-01-23 06:43:05 -080042 if (domain > SK_MaxU16) {
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040043 SK_ABORT("Too many GrUniqueKey Domains");
bsalomon7775c852014-12-30 12:50:52 -080044 }
bsalomon24db3b12015-01-23 04:24:04 -080045
46 return static_cast<Domain>(domain);
47}
bsalomon3f324322015-04-08 11:01:54 -070048
bsalomon24db3b12015-01-23 04:24:04 -080049uint32_t GrResourceKeyHash(const uint32_t* data, size_t size) {
mtklein4e976072016-08-08 09:06:27 -070050 return SkOpts::hash(data, size);
bsalomon7775c852014-12-30 12:50:52 -080051}
52
bsalomonfe369ee2014-11-10 11:59:06 -080053//////////////////////////////////////////////////////////////////////////////
54
bsalomon0ea80f42015-02-11 10:49:59 -080055class GrResourceCache::AutoValidate : ::SkNoncopyable {
bsalomon71cb0c22014-11-14 12:10:14 -080056public:
bsalomon0ea80f42015-02-11 10:49:59 -080057 AutoValidate(GrResourceCache* cache) : fCache(cache) { cache->validate(); }
bsalomon71cb0c22014-11-14 12:10:14 -080058 ~AutoValidate() { fCache->validate(); }
59private:
bsalomon0ea80f42015-02-11 10:49:59 -080060 GrResourceCache* fCache;
bsalomon71cb0c22014-11-14 12:10:14 -080061};
62
63 //////////////////////////////////////////////////////////////////////////////
robertphillipsee843b22016-10-04 05:30:20 -070064
bsalomon71cb0c22014-11-14 12:10:14 -080065
Brian Osman13dddce2017-05-09 13:19:50 -040066GrResourceCache::GrResourceCache(const GrCaps* caps, uint32_t contextUniqueID)
bsalomon9f2d1572015-02-17 11:47:40 -080067 : fTimestamp(0)
68 , fMaxCount(kDefaultMaxCount)
bsalomon71cb0c22014-11-14 12:10:14 -080069 , fMaxBytes(kDefaultMaxSize)
bsalomon3f324322015-04-08 11:01:54 -070070 , fMaxUnusedFlushes(kDefaultMaxUnusedFlushes)
bsalomon71cb0c22014-11-14 12:10:14 -080071#if GR_CACHE_STATS
72 , fHighWaterCount(0)
73 , fHighWaterBytes(0)
bsalomondace19e2014-11-17 07:34:06 -080074 , fBudgetedHighWaterCount(0)
75 , fBudgetedHighWaterBytes(0)
bsalomon71cb0c22014-11-14 12:10:14 -080076#endif
bsalomon71cb0c22014-11-14 12:10:14 -080077 , fBytes(0)
bsalomondace19e2014-11-17 07:34:06 -080078 , fBudgetedCount(0)
79 , fBudgetedBytes(0)
Derek Sollenbergeree479142017-05-24 11:41:33 -040080 , fPurgeableBytes(0)
robertphillipsee843b22016-10-04 05:30:20 -070081 , fRequestFlush(false)
bsalomone2e87f32016-09-22 12:42:11 -070082 , fExternalFlushCnt(0)
Brian Osman13dddce2017-05-09 13:19:50 -040083 , fContextUniqueID(contextUniqueID)
robertphillips63926682015-08-20 09:39:02 -070084 , fPreferVRAMUseOverFlushes(caps->preferVRAMUseOverFlushes()) {
bsalomonf320e042015-02-17 15:09:34 -080085 SkDEBUGCODE(fCount = 0;)
halcanary96fcdcc2015-08-27 07:41:13 -070086 SkDEBUGCODE(fNewlyPurgeableResourceForValidation = nullptr;)
bsalomon71cb0c22014-11-14 12:10:14 -080087}
88
bsalomon0ea80f42015-02-11 10:49:59 -080089GrResourceCache::~GrResourceCache() {
bsalomonc8dc1f72014-08-21 13:02:13 -070090 this->releaseAll();
91}
92
bsalomon3f324322015-04-08 11:01:54 -070093void GrResourceCache::setLimits(int count, size_t bytes, int maxUnusedFlushes) {
bsalomon71cb0c22014-11-14 12:10:14 -080094 fMaxCount = count;
95 fMaxBytes = bytes;
bsalomon3f324322015-04-08 11:01:54 -070096 fMaxUnusedFlushes = maxUnusedFlushes;
bsalomon71cb0c22014-11-14 12:10:14 -080097 this->purgeAsNeeded();
98}
99
bsalomon0ea80f42015-02-11 10:49:59 -0800100void GrResourceCache::insertResource(GrGpuResource* resource) {
bsalomon49f085d2014-09-05 13:34:00 -0700101 SkASSERT(resource);
bsalomon16961262014-08-26 14:01:07 -0700102 SkASSERT(!this->isInCache(resource));
bsalomonf320e042015-02-17 15:09:34 -0800103 SkASSERT(!resource->wasDestroyed());
104 SkASSERT(!resource->isPurgeable());
bsalomonddf30e62015-02-19 11:38:44 -0800105
106 // We must set the timestamp before adding to the array in case the timestamp wraps and we wind
107 // up iterating over all the resources that already have timestamps.
108 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
109
bsalomonf320e042015-02-17 15:09:34 -0800110 this->addToNonpurgeableArray(resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800111
bsalomondace19e2014-11-17 07:34:06 -0800112 size_t size = resource->gpuMemorySize();
bsalomonf320e042015-02-17 15:09:34 -0800113 SkDEBUGCODE(++fCount;)
bsalomon84c8e622014-11-17 09:33:27 -0800114 fBytes += size;
bsalomon82b1d622014-11-14 13:59:57 -0800115#if GR_CACHE_STATS
bsalomonf320e042015-02-17 15:09:34 -0800116 fHighWaterCount = SkTMax(this->getResourceCount(), fHighWaterCount);
bsalomon82b1d622014-11-14 13:59:57 -0800117 fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes);
118#endif
bsalomon5ec26ae2016-02-25 08:33:02 -0800119 if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) {
bsalomondace19e2014-11-17 07:34:06 -0800120 ++fBudgetedCount;
121 fBudgetedBytes += size;
Brian Osman39c08ac2017-07-26 09:36:09 -0400122 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800123 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomondace19e2014-11-17 07:34:06 -0800124#if GR_CACHE_STATS
125 fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount);
126 fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes);
127#endif
128 }
robertphillipsc4ed6842016-05-24 14:17:12 -0700129 if (resource->resourcePriv().getScratchKey().isValid() &&
130 !resource->getUniqueKey().isValid()) {
kkinnunen2e6055b2016-04-22 01:48:29 -0700131 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
bsalomon3582d3e2015-02-13 14:20:05 -0800132 fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
bsalomon744998e2014-08-28 09:54:34 -0700133 }
bsalomon9f2d1572015-02-17 11:47:40 -0800134
bsalomon71cb0c22014-11-14 12:10:14 -0800135 this->purgeAsNeeded();
bsalomonc8dc1f72014-08-21 13:02:13 -0700136}
137
bsalomon0ea80f42015-02-11 10:49:59 -0800138void GrResourceCache::removeResource(GrGpuResource* resource) {
bsalomon9f2d1572015-02-17 11:47:40 -0800139 this->validate();
bsalomon16961262014-08-26 14:01:07 -0700140 SkASSERT(this->isInCache(resource));
bsalomondace19e2014-11-17 07:34:06 -0800141
Derek Sollenbergeree479142017-05-24 11:41:33 -0400142 size_t size = resource->gpuMemorySize();
bsalomon9f2d1572015-02-17 11:47:40 -0800143 if (resource->isPurgeable()) {
144 fPurgeableQueue.remove(resource);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400145 fPurgeableBytes -= size;
bsalomonf320e042015-02-17 15:09:34 -0800146 } else {
147 this->removeFromNonpurgeableArray(resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800148 }
149
bsalomonf320e042015-02-17 15:09:34 -0800150 SkDEBUGCODE(--fCount;)
bsalomondace19e2014-11-17 07:34:06 -0800151 fBytes -= size;
bsalomon5ec26ae2016-02-25 08:33:02 -0800152 if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) {
bsalomondace19e2014-11-17 07:34:06 -0800153 --fBudgetedCount;
154 fBudgetedBytes -= size;
Brian Osman39c08ac2017-07-26 09:36:09 -0400155 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800156 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomondace19e2014-11-17 07:34:06 -0800157 }
158
robertphillipsc4ed6842016-05-24 14:17:12 -0700159 if (resource->resourcePriv().getScratchKey().isValid() &&
160 !resource->getUniqueKey().isValid()) {
bsalomon3582d3e2015-02-13 14:20:05 -0800161 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
bsalomon744998e2014-08-28 09:54:34 -0700162 }
bsalomon8718aaf2015-02-19 07:24:21 -0800163 if (resource->getUniqueKey().isValid()) {
164 fUniqueHash.remove(resource->getUniqueKey());
bsalomon8b79d232014-11-10 10:19:06 -0800165 }
bsalomonb436ed62014-11-17 12:15:56 -0800166 this->validate();
bsalomonc8dc1f72014-08-21 13:02:13 -0700167}
168
bsalomon0ea80f42015-02-11 10:49:59 -0800169void GrResourceCache::abandonAll() {
bsalomon71cb0c22014-11-14 12:10:14 -0800170 AutoValidate av(this);
171
bsalomonf320e042015-02-17 15:09:34 -0800172 while (fNonpurgeableResources.count()) {
173 GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
174 SkASSERT(!back->wasDestroyed());
175 back->cacheAccess().abandon();
bsalomonc8dc1f72014-08-21 13:02:13 -0700176 }
bsalomonf320e042015-02-17 15:09:34 -0800177
178 while (fPurgeableQueue.count()) {
179 GrGpuResource* top = fPurgeableQueue.peek();
180 SkASSERT(!top->wasDestroyed());
181 top->cacheAccess().abandon();
182 }
183
bsalomon744998e2014-08-28 09:54:34 -0700184 SkASSERT(!fScratchMap.count());
bsalomon8718aaf2015-02-19 07:24:21 -0800185 SkASSERT(!fUniqueHash.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700186 SkASSERT(!fCount);
bsalomonf320e042015-02-17 15:09:34 -0800187 SkASSERT(!this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800188 SkASSERT(!fBytes);
189 SkASSERT(!fBudgetedCount);
190 SkASSERT(!fBudgetedBytes);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400191 SkASSERT(!fPurgeableBytes);
bsalomonc8dc1f72014-08-21 13:02:13 -0700192}
193
bsalomon0ea80f42015-02-11 10:49:59 -0800194void GrResourceCache::releaseAll() {
bsalomon71cb0c22014-11-14 12:10:14 -0800195 AutoValidate av(this);
196
Brian Osman13dddce2017-05-09 13:19:50 -0400197 this->processFreedGpuResources();
198
bsalomonf320e042015-02-17 15:09:34 -0800199 while(fNonpurgeableResources.count()) {
200 GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
201 SkASSERT(!back->wasDestroyed());
202 back->cacheAccess().release();
bsalomonc8dc1f72014-08-21 13:02:13 -0700203 }
bsalomonf320e042015-02-17 15:09:34 -0800204
205 while (fPurgeableQueue.count()) {
206 GrGpuResource* top = fPurgeableQueue.peek();
207 SkASSERT(!top->wasDestroyed());
208 top->cacheAccess().release();
209 }
210
bsalomon744998e2014-08-28 09:54:34 -0700211 SkASSERT(!fScratchMap.count());
bsalomon8718aaf2015-02-19 07:24:21 -0800212 SkASSERT(!fUniqueHash.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700213 SkASSERT(!fCount);
bsalomonf320e042015-02-17 15:09:34 -0800214 SkASSERT(!this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800215 SkASSERT(!fBytes);
216 SkASSERT(!fBudgetedCount);
217 SkASSERT(!fBudgetedBytes);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400218 SkASSERT(!fPurgeableBytes);
bsalomonc8dc1f72014-08-21 13:02:13 -0700219}
bsalomonbcf0a522014-10-08 08:40:09 -0700220
bsalomon0ea80f42015-02-11 10:49:59 -0800221class GrResourceCache::AvailableForScratchUse {
bsalomonbcf0a522014-10-08 08:40:09 -0700222public:
bsalomon000f8292014-10-15 19:04:14 -0700223 AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendingIO) { }
bsalomonbcf0a522014-10-08 08:40:09 -0700224
225 bool operator()(const GrGpuResource* resource) const {
robertphillipsc4ed6842016-05-24 14:17:12 -0700226 SkASSERT(!resource->getUniqueKey().isValid() &&
227 resource->resourcePriv().getScratchKey().isValid());
bsalomon12299ab2014-11-14 13:33:09 -0800228 if (resource->internalHasRef() || !resource->cacheAccess().isScratch()) {
bsalomon000f8292014-10-15 19:04:14 -0700229 return false;
bsalomonbcf0a522014-10-08 08:40:09 -0700230 }
bsalomon000f8292014-10-15 19:04:14 -0700231 return !fRejectPendingIO || !resource->internalHasPendingIO();
bsalomonbcf0a522014-10-08 08:40:09 -0700232 }
bsalomon1e2530b2014-10-09 09:57:18 -0700233
bsalomonbcf0a522014-10-08 08:40:09 -0700234private:
bsalomon000f8292014-10-15 19:04:14 -0700235 bool fRejectPendingIO;
bsalomonbcf0a522014-10-08 08:40:09 -0700236};
237
bsalomon0ea80f42015-02-11 10:49:59 -0800238GrGpuResource* GrResourceCache::findAndRefScratchResource(const GrScratchKey& scratchKey,
robertphillips6e83ac72015-08-13 05:19:14 -0700239 size_t resourceSize,
bsalomon9f2d1572015-02-17 11:47:40 -0800240 uint32_t flags) {
bsalomon7775c852014-12-30 12:50:52 -0800241 SkASSERT(scratchKey.isValid());
robertphillipsee843b22016-10-04 05:30:20 -0700242
bsalomon71cb0c22014-11-14 12:10:14 -0800243 GrGpuResource* resource;
bsalomon000f8292014-10-15 19:04:14 -0700244 if (flags & (kPreferNoPendingIO_ScratchFlag | kRequireNoPendingIO_ScratchFlag)) {
bsalomon71cb0c22014-11-14 12:10:14 -0800245 resource = fScratchMap.find(scratchKey, AvailableForScratchUse(true));
bsalomon000f8292014-10-15 19:04:14 -0700246 if (resource) {
bsalomon9f2d1572015-02-17 11:47:40 -0800247 this->refAndMakeResourceMRU(resource);
bsalomonb436ed62014-11-17 12:15:56 -0800248 this->validate();
249 return resource;
bsalomon000f8292014-10-15 19:04:14 -0700250 } else if (flags & kRequireNoPendingIO_ScratchFlag) {
halcanary96fcdcc2015-08-27 07:41:13 -0700251 return nullptr;
bsalomon000f8292014-10-15 19:04:14 -0700252 }
robertphillips63926682015-08-20 09:39:02 -0700253 // We would prefer to consume more available VRAM rather than flushing
254 // immediately, but on ANGLE this can lead to starving of the GPU.
255 if (fPreferVRAMUseOverFlushes && this->wouldFit(resourceSize)) {
robertphillips6e83ac72015-08-13 05:19:14 -0700256 // kPrefer is specified, we didn't find a resource without pending io,
robertphillips63926682015-08-20 09:39:02 -0700257 // but there is still space in our budget for the resource so force
258 // the caller to allocate a new resource.
halcanary96fcdcc2015-08-27 07:41:13 -0700259 return nullptr;
robertphillips6e83ac72015-08-13 05:19:14 -0700260 }
bsalomon000f8292014-10-15 19:04:14 -0700261 }
bsalomon71cb0c22014-11-14 12:10:14 -0800262 resource = fScratchMap.find(scratchKey, AvailableForScratchUse(false));
263 if (resource) {
bsalomon9f2d1572015-02-17 11:47:40 -0800264 this->refAndMakeResourceMRU(resource);
bsalomonb436ed62014-11-17 12:15:56 -0800265 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800266 }
267 return resource;
bsalomonbcf0a522014-10-08 08:40:09 -0700268}
bsalomon8b79d232014-11-10 10:19:06 -0800269
bsalomon0ea80f42015-02-11 10:49:59 -0800270void GrResourceCache::willRemoveScratchKey(const GrGpuResource* resource) {
bsalomon3582d3e2015-02-13 14:20:05 -0800271 SkASSERT(resource->resourcePriv().getScratchKey().isValid());
robertphillipsc4ed6842016-05-24 14:17:12 -0700272 if (!resource->getUniqueKey().isValid()) {
273 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
274 }
bsalomon10e23ca2014-11-25 05:52:06 -0800275}
276
bsalomonf99e9612015-02-19 08:24:16 -0800277void GrResourceCache::removeUniqueKey(GrGpuResource* resource) {
bsalomon3f324322015-04-08 11:01:54 -0700278 // Someone has a ref to this resource in order to have removed the key. When the ref count
279 // reaches zero we will get a ref cnt notification and figure out what to do with it.
bsalomonf99e9612015-02-19 08:24:16 -0800280 if (resource->getUniqueKey().isValid()) {
281 SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
282 fUniqueHash.remove(resource->getUniqueKey());
283 }
284 resource->cacheAccess().removeUniqueKey();
robertphillipsc4ed6842016-05-24 14:17:12 -0700285
286 if (resource->resourcePriv().getScratchKey().isValid()) {
287 fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
288 }
289
bsalomonf99e9612015-02-19 08:24:16 -0800290 this->validate();
bsalomon23e619c2015-02-06 11:54:28 -0800291}
292
bsalomonf99e9612015-02-19 08:24:16 -0800293void GrResourceCache::changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) {
bsalomon8b79d232014-11-10 10:19:06 -0800294 SkASSERT(resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800295 SkASSERT(this->isInCache(resource));
bsalomon8b79d232014-11-10 10:19:06 -0800296
bsalomonf99e9612015-02-19 08:24:16 -0800297 // If another resource has the new key, remove its key then install the key on this resource.
298 if (newKey.isValid()) {
Greg Daniel0d537802017-09-08 11:44:14 -0400299 if (GrGpuResource* old = fUniqueHash.find(newKey)) {
300 // If the old resource using the key is purgeable and is unreachable, then remove it.
301 if (!old->resourcePriv().getScratchKey().isValid() && old->isPurgeable()) {
302 old->cacheAccess().release();
303 } else {
304 this->removeUniqueKey(old);
305 }
306 }
307 SkASSERT(nullptr == fUniqueHash.find(newKey));
308
robertphillipsc4ed6842016-05-24 14:17:12 -0700309 // Remove the entry for this resource if it already has a unique key.
310 if (resource->getUniqueKey().isValid()) {
311 SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
312 fUniqueHash.remove(resource->getUniqueKey());
313 SkASSERT(nullptr == fUniqueHash.find(resource->getUniqueKey()));
314 } else {
315 // 'resource' didn't have a valid unique key before so it is switching sides. Remove it
316 // from the ScratchMap
317 if (resource->resourcePriv().getScratchKey().isValid()) {
318 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
319 }
320 }
321
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;
Brian Osman39c08ac2017-07-26 09:36:09 -0400422 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800423 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 }
Brian Osman39c08ac2017-07-26 09:36:09 -0400451 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800452 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
Derek Sollenberger5480a182017-05-25 16:43:59 -0400539void GrResourceCache::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
540
541 const size_t tmpByteBudget = SkTMax((size_t)0, fBytes - bytesToPurge);
542 bool stillOverbudget = tmpByteBudget < fBytes;
543
544 if (preferScratchResources && bytesToPurge < fPurgeableBytes) {
545 // Sort the queue
546 fPurgeableQueue.sort();
547
548 // Make a list of the scratch resources to delete
549 SkTDArray<GrGpuResource*> scratchResources;
550 size_t scratchByteCount = 0;
551 for (int i = 0; i < fPurgeableQueue.count() && stillOverbudget; i++) {
552 GrGpuResource* resource = fPurgeableQueue.at(i);
553 SkASSERT(resource->isPurgeable());
554 if (!resource->getUniqueKey().isValid()) {
555 *scratchResources.append() = resource;
556 scratchByteCount += resource->gpuMemorySize();
557 stillOverbudget = tmpByteBudget < fBytes - scratchByteCount;
558 }
559 }
560
561 // Delete the scratch resources. This must be done as a separate pass
562 // to avoid messing up the sorted order of the queue
563 for (int i = 0; i < scratchResources.count(); i++) {
564 scratchResources.getAt(i)->cacheAccess().release();
565 }
566 stillOverbudget = tmpByteBudget < fBytes;
567
568 this->validate();
569 }
570
571 // Purge any remaining resources in LRU order
572 if (stillOverbudget) {
573 const size_t cachedByteCount = fMaxBytes;
574 fMaxBytes = tmpByteBudget;
575 this->purgeAsNeeded();
576 fMaxBytes = cachedByteCount;
577 }
578}
579
bsalomon8718aaf2015-02-19 07:24:21 -0800580void GrResourceCache::processInvalidUniqueKeys(
581 const SkTArray<GrUniqueKeyInvalidatedMessage>& msgs) {
bsalomon23e619c2015-02-06 11:54:28 -0800582 for (int i = 0; i < msgs.count(); ++i) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400583 this->processInvalidProxyUniqueKey(msgs[i].key());
584
bsalomon8718aaf2015-02-19 07:24:21 -0800585 GrGpuResource* resource = this->findAndRefUniqueResource(msgs[i].key());
bsalomon23e619c2015-02-06 11:54:28 -0800586 if (resource) {
bsalomon8718aaf2015-02-19 07:24:21 -0800587 resource->resourcePriv().removeUniqueKey();
bsalomon3f324322015-04-08 11:01:54 -0700588 resource->unref(); // If this resource is now purgeable, the cache will be notified.
bsalomon23e619c2015-02-06 11:54:28 -0800589 }
590 }
591}
592
Brian Osman13dddce2017-05-09 13:19:50 -0400593void GrResourceCache::insertCrossContextGpuResource(GrGpuResource* resource) {
594 resource->ref();
595}
596
597void GrResourceCache::processFreedGpuResources() {
598 SkTArray<GrGpuResourceFreedMessage> msgs;
599 fFreedGpuResourceInbox.poll(&msgs);
600 for (int i = 0; i < msgs.count(); ++i) {
601 if (msgs[i].fOwningUniqueID == fContextUniqueID) {
602 msgs[i].fResource->unref();
603 }
604 }
605}
606
bsalomonf320e042015-02-17 15:09:34 -0800607void GrResourceCache::addToNonpurgeableArray(GrGpuResource* resource) {
608 int index = fNonpurgeableResources.count();
609 *fNonpurgeableResources.append() = resource;
610 *resource->cacheAccess().accessCacheIndex() = index;
611}
612
613void GrResourceCache::removeFromNonpurgeableArray(GrGpuResource* resource) {
614 int* index = resource->cacheAccess().accessCacheIndex();
615 // Fill the whole we will create in the array with the tail object, adjust its index, and
616 // then pop the array
617 GrGpuResource* tail = *(fNonpurgeableResources.end() - 1);
618 SkASSERT(fNonpurgeableResources[*index] == resource);
619 fNonpurgeableResources[*index] = tail;
620 *tail->cacheAccess().accessCacheIndex() = *index;
621 fNonpurgeableResources.pop();
622 SkDEBUGCODE(*index = -1);
623}
624
bsalomonddf30e62015-02-19 11:38:44 -0800625uint32_t GrResourceCache::getNextTimestamp() {
626 // If we wrap then all the existing resources will appear older than any resources that get
627 // a timestamp after the wrap.
628 if (0 == fTimestamp) {
629 int count = this->getResourceCount();
630 if (count) {
631 // Reset all the timestamps. We sort the resources by timestamp and then assign
632 // sequential timestamps beginning with 0. This is O(n*lg(n)) but it should be extremely
633 // rare.
634 SkTDArray<GrGpuResource*> sortedPurgeableResources;
635 sortedPurgeableResources.setReserve(fPurgeableQueue.count());
636
637 while (fPurgeableQueue.count()) {
638 *sortedPurgeableResources.append() = fPurgeableQueue.peek();
639 fPurgeableQueue.pop();
640 }
robertphillipsee843b22016-10-04 05:30:20 -0700641
bsalomone2e87f32016-09-22 12:42:11 -0700642 SkTQSort(fNonpurgeableResources.begin(), fNonpurgeableResources.end() - 1,
643 CompareTimestamp);
bsalomonddf30e62015-02-19 11:38:44 -0800644
645 // Pick resources out of the purgeable and non-purgeable arrays based on lowest
646 // timestamp and assign new timestamps.
647 int currP = 0;
648 int currNP = 0;
649 while (currP < sortedPurgeableResources.count() &&
mtklein56da0252015-11-16 11:16:23 -0800650 currNP < fNonpurgeableResources.count()) {
bsalomonddf30e62015-02-19 11:38:44 -0800651 uint32_t tsP = sortedPurgeableResources[currP]->cacheAccess().timestamp();
652 uint32_t tsNP = fNonpurgeableResources[currNP]->cacheAccess().timestamp();
653 SkASSERT(tsP != tsNP);
654 if (tsP < tsNP) {
655 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
656 } else {
657 // Correct the index in the nonpurgeable array stored on the resource post-sort.
658 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
659 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
660 }
661 }
662
663 // The above loop ended when we hit the end of one array. Finish the other one.
664 while (currP < sortedPurgeableResources.count()) {
665 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
666 }
667 while (currNP < fNonpurgeableResources.count()) {
668 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
669 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
670 }
671
672 // Rebuild the queue.
673 for (int i = 0; i < sortedPurgeableResources.count(); ++i) {
674 fPurgeableQueue.insert(sortedPurgeableResources[i]);
675 }
676
677 this->validate();
678 SkASSERT(count == this->getResourceCount());
679
680 // count should be the next timestamp we return.
681 SkASSERT(fTimestamp == SkToU32(count));
mtklein56da0252015-11-16 11:16:23 -0800682 }
bsalomonddf30e62015-02-19 11:38:44 -0800683 }
684 return fTimestamp++;
685}
686
bsalomonb77a9072016-09-07 10:02:04 -0700687void GrResourceCache::notifyFlushOccurred(FlushType type) {
688 switch (type) {
bsalomonb77a9072016-09-07 10:02:04 -0700689 case FlushType::kCacheRequested:
robertphillipsee843b22016-10-04 05:30:20 -0700690 SkASSERT(fRequestFlush);
691 fRequestFlush = false;
bsalomonb77a9072016-09-07 10:02:04 -0700692 break;
robertphillipsee843b22016-10-04 05:30:20 -0700693 case FlushType::kExternal:
bsalomone2e87f32016-09-22 12:42:11 -0700694 ++fExternalFlushCnt;
695 if (0 == fExternalFlushCnt) {
696 // When this wraps just reset all the purgeable resources' last used flush state.
697 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
698 fPurgeableQueue.at(i)->cacheAccess().setFlushCntWhenResourceBecamePurgeable(0);
699 }
bsalomonb77a9072016-09-07 10:02:04 -0700700 }
701 break;
bsalomon3f324322015-04-08 11:01:54 -0700702 }
robertphillipsee843b22016-10-04 05:30:20 -0700703 this->purgeAsNeeded();
bsalomon3f324322015-04-08 11:01:54 -0700704}
705
ericrk0a5fa482015-09-15 14:16:10 -0700706void GrResourceCache::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
707 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
708 fNonpurgeableResources[i]->dumpMemoryStatistics(traceMemoryDump);
709 }
710 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
711 fPurgeableQueue.at(i)->dumpMemoryStatistics(traceMemoryDump);
712 }
713}
714
bsalomon71cb0c22014-11-14 12:10:14 -0800715#ifdef SK_DEBUG
bsalomon0ea80f42015-02-11 10:49:59 -0800716void GrResourceCache::validate() const {
bsalomonc2f35b72015-01-23 07:19:22 -0800717 // Reduce the frequency of validations for large resource counts.
718 static SkRandom gRandom;
719 int mask = (SkNextPow2(fCount + 1) >> 5) - 1;
720 if (~mask && (gRandom.nextU() & mask)) {
721 return;
722 }
723
bsalomonf320e042015-02-17 15:09:34 -0800724 struct Stats {
725 size_t fBytes;
726 int fBudgetedCount;
727 size_t fBudgetedBytes;
728 int fLocked;
729 int fScratch;
730 int fCouldBeScratch;
731 int fContent;
732 const ScratchMap* fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800733 const UniqueHash* fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800734
bsalomonf320e042015-02-17 15:09:34 -0800735 Stats(const GrResourceCache* cache) {
736 memset(this, 0, sizeof(*this));
737 fScratchMap = &cache->fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800738 fUniqueHash = &cache->fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800739 }
740
bsalomonf320e042015-02-17 15:09:34 -0800741 void update(GrGpuResource* resource) {
742 fBytes += resource->gpuMemorySize();
bsalomondace19e2014-11-17 07:34:06 -0800743
bsalomonf320e042015-02-17 15:09:34 -0800744 if (!resource->isPurgeable()) {
745 ++fLocked;
746 }
bsalomon9f2d1572015-02-17 11:47:40 -0800747
robertphillipsc4ed6842016-05-24 14:17:12 -0700748 const GrScratchKey& scratchKey = resource->resourcePriv().getScratchKey();
749 const GrUniqueKey& uniqueKey = resource->getUniqueKey();
750
bsalomonf320e042015-02-17 15:09:34 -0800751 if (resource->cacheAccess().isScratch()) {
robertphillipsc4ed6842016-05-24 14:17:12 -0700752 SkASSERT(!uniqueKey.isValid());
bsalomonf320e042015-02-17 15:09:34 -0800753 ++fScratch;
robertphillipsc4ed6842016-05-24 14:17:12 -0700754 SkASSERT(fScratchMap->countForKey(scratchKey));
kkinnunen2e6055b2016-04-22 01:48:29 -0700755 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
robertphillipsc4ed6842016-05-24 14:17:12 -0700756 } else if (scratchKey.isValid()) {
bsalomon5ec26ae2016-02-25 08:33:02 -0800757 SkASSERT(SkBudgeted::kNo == resource->resourcePriv().isBudgeted() ||
robertphillipsc4ed6842016-05-24 14:17:12 -0700758 uniqueKey.isValid());
759 if (!uniqueKey.isValid()) {
mtklein4e976072016-08-08 09:06:27 -0700760 ++fCouldBeScratch;
robertphillipsc4ed6842016-05-24 14:17:12 -0700761 SkASSERT(fScratchMap->countForKey(scratchKey));
762 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700763 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
bsalomonf320e042015-02-17 15:09:34 -0800764 }
bsalomon8718aaf2015-02-19 07:24:21 -0800765 if (uniqueKey.isValid()) {
bsalomonf320e042015-02-17 15:09:34 -0800766 ++fContent;
bsalomon8718aaf2015-02-19 07:24:21 -0800767 SkASSERT(fUniqueHash->find(uniqueKey) == resource);
Brian Osman0562eb92017-05-08 11:16:39 -0400768 SkASSERT(SkBudgeted::kYes == resource->resourcePriv().isBudgeted() ||
769 resource->resourcePriv().refsWrappedObjects());
robertphillipsc4ed6842016-05-24 14:17:12 -0700770
771 if (scratchKey.isValid()) {
772 SkASSERT(!fScratchMap->has(resource, scratchKey));
773 }
bsalomonf320e042015-02-17 15:09:34 -0800774 }
775
bsalomon5ec26ae2016-02-25 08:33:02 -0800776 if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) {
bsalomonf320e042015-02-17 15:09:34 -0800777 ++fBudgetedCount;
778 fBudgetedBytes += resource->gpuMemorySize();
779 }
bsalomon9f2d1572015-02-17 11:47:40 -0800780 }
bsalomonf320e042015-02-17 15:09:34 -0800781 };
782
robertphillipsc4ed6842016-05-24 14:17:12 -0700783 {
784 ScratchMap::ConstIter iter(&fScratchMap);
785
786 int count = 0;
787 for ( ; !iter.done(); ++iter) {
788 const GrGpuResource* resource = *iter;
789 SkASSERT(resource->resourcePriv().getScratchKey().isValid());
790 SkASSERT(!resource->getUniqueKey().isValid());
791 count++;
792 }
793 SkASSERT(count == fScratchMap.count()); // ensure the iterator is working correctly
794 }
795
bsalomonf320e042015-02-17 15:09:34 -0800796 Stats stats(this);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400797 size_t purgeableBytes = 0;
bsalomonf320e042015-02-17 15:09:34 -0800798
799 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
bsalomon3f324322015-04-08 11:01:54 -0700800 SkASSERT(!fNonpurgeableResources[i]->isPurgeable() ||
801 fNewlyPurgeableResourceForValidation == fNonpurgeableResources[i]);
bsalomonf320e042015-02-17 15:09:34 -0800802 SkASSERT(*fNonpurgeableResources[i]->cacheAccess().accessCacheIndex() == i);
803 SkASSERT(!fNonpurgeableResources[i]->wasDestroyed());
804 stats.update(fNonpurgeableResources[i]);
bsalomon71cb0c22014-11-14 12:10:14 -0800805 }
bsalomon9f2d1572015-02-17 11:47:40 -0800806 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
807 SkASSERT(fPurgeableQueue.at(i)->isPurgeable());
bsalomonf320e042015-02-17 15:09:34 -0800808 SkASSERT(*fPurgeableQueue.at(i)->cacheAccess().accessCacheIndex() == i);
809 SkASSERT(!fPurgeableQueue.at(i)->wasDestroyed());
810 stats.update(fPurgeableQueue.at(i));
Derek Sollenbergeree479142017-05-24 11:41:33 -0400811 purgeableBytes += fPurgeableQueue.at(i)->gpuMemorySize();
bsalomon9f2d1572015-02-17 11:47:40 -0800812 }
813
bsalomonf320e042015-02-17 15:09:34 -0800814 SkASSERT(fCount == this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800815 SkASSERT(fBudgetedCount <= fCount);
bsalomonf320e042015-02-17 15:09:34 -0800816 SkASSERT(fBudgetedBytes <= fBytes);
817 SkASSERT(stats.fBytes == fBytes);
818 SkASSERT(stats.fBudgetedBytes == fBudgetedBytes);
819 SkASSERT(stats.fBudgetedCount == fBudgetedCount);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400820 SkASSERT(purgeableBytes == fPurgeableBytes);
bsalomon71cb0c22014-11-14 12:10:14 -0800821#if GR_CACHE_STATS
bsalomondace19e2014-11-17 07:34:06 -0800822 SkASSERT(fBudgetedHighWaterCount <= fHighWaterCount);
823 SkASSERT(fBudgetedHighWaterBytes <= fHighWaterBytes);
bsalomonf320e042015-02-17 15:09:34 -0800824 SkASSERT(fBytes <= fHighWaterBytes);
825 SkASSERT(fCount <= fHighWaterCount);
826 SkASSERT(fBudgetedBytes <= fBudgetedHighWaterBytes);
827 SkASSERT(fBudgetedCount <= fBudgetedHighWaterCount);
bsalomon71cb0c22014-11-14 12:10:14 -0800828#endif
bsalomon8718aaf2015-02-19 07:24:21 -0800829 SkASSERT(stats.fContent == fUniqueHash.count());
bsalomonf320e042015-02-17 15:09:34 -0800830 SkASSERT(stats.fScratch + stats.fCouldBeScratch == fScratchMap.count());
bsalomon71cb0c22014-11-14 12:10:14 -0800831
bsalomon3f324322015-04-08 11:01:54 -0700832 // This assertion is not currently valid because we can be in recursive notifyCntReachedZero()
bsalomon12299ab2014-11-14 13:33:09 -0800833 // calls. This will be fixed when subresource registration is explicit.
bsalomondace19e2014-11-17 07:34:06 -0800834 // bool overBudget = budgetedBytes > fMaxBytes || budgetedCount > fMaxCount;
bsalomon12299ab2014-11-14 13:33:09 -0800835 // SkASSERT(!overBudget || locked == count || fPurging);
bsalomon71cb0c22014-11-14 12:10:14 -0800836}
bsalomonf320e042015-02-17 15:09:34 -0800837
838bool GrResourceCache::isInCache(const GrGpuResource* resource) const {
839 int index = *resource->cacheAccess().accessCacheIndex();
840 if (index < 0) {
841 return false;
842 }
843 if (index < fPurgeableQueue.count() && fPurgeableQueue.at(index) == resource) {
844 return true;
845 }
846 if (index < fNonpurgeableResources.count() && fNonpurgeableResources[index] == resource) {
847 return true;
848 }
849 SkDEBUGFAIL("Resource index should be -1 or the resource should be in the cache.");
850 return false;
851}
852
bsalomon71cb0c22014-11-14 12:10:14 -0800853#endif
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400854
855void GrResourceCache::assignUniqueKeyToProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
856 SkASSERT(key.isValid());
857 SkASSERT(proxy);
858
859 // If there is already a GrResource with this key then the caller has violated the normal
860 // usage pattern of uniquely keyed resources (e.g., they have created one w/o first seeing
861 // if it already existed in the cache).
862 SkASSERT(!this->findAndRefUniqueResource(key));
863
864 // Uncached resources can never have a unique key, unless they're wrapped resources. Wrapped
865 // resources are a special case: the unique keys give us a weak ref so that we can reuse the
866 // same resource (rather than re-wrapping). When a wrapped resource is no longer referenced,
867 // it will always be released - it is never converted to a scratch resource.
868 if (SkBudgeted::kNo == proxy->isBudgeted() &&
869 (!proxy->priv().isInstantiated() ||
870 !proxy->priv().peekSurface()->resourcePriv().refsWrappedObjects())) {
871 return;
872 }
873
874 SkASSERT(!fUniquelyKeyedProxies.find(key)); // multiple proxies can't get the same key
875
876 proxy->cacheAccess().setUniqueKey(this, key);
877 SkASSERT(proxy->getUniqueKey() == key);
878 fUniquelyKeyedProxies.add(proxy);
879}
880
881sk_sp<GrTextureProxy> GrResourceCache::findProxyByUniqueKey(const GrUniqueKey& key,
882 GrSurfaceOrigin origin) {
883
884 sk_sp<GrTextureProxy> result = sk_ref_sp(fUniquelyKeyedProxies.find(key));
885 if (result) {
886 SkASSERT(result->origin() == origin);
887 return result;
888 }
889
890 GrGpuResource* resource = findAndRefUniqueResource(key);
891 if (!resource) {
892 return nullptr;
893 }
894
895 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
896 SkASSERT(texture);
897
898 result = GrSurfaceProxy::MakeWrapped(std::move(texture), origin);
899 SkASSERT(result->getUniqueKey() == key);
900 fUniquelyKeyedProxies.add(result.get());
901 return result;
902}
903
904void GrResourceCache::processInvalidProxyUniqueKey(const GrUniqueKey& key) {
905 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
906 // will not be in 'fUniquelyKeyedProxies'.
907 GrTextureProxy* proxy = fUniquelyKeyedProxies.find(key);
908 if (proxy) {
909 fUniquelyKeyedProxies.remove(key);
910 proxy->cacheAccess().clearUniqueKey();
911 }
912}
913