blob: fd39bbf29c0b0f4661ffc3129e228e84be3ecec8 [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
Robert Phillips45a6f142017-09-29 09:49:41 -0400199 UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies);
200 for (UniquelyKeyedProxyHash::Iter iter(&fUniquelyKeyedProxies) ; !iter.done(); ++iter) {
201 GrTextureProxy& tmp = *iter;
202
203 this->processInvalidProxyUniqueKey(tmp.getUniqueKey(), &tmp, false);
204 }
205
bsalomonf320e042015-02-17 15:09:34 -0800206 while(fNonpurgeableResources.count()) {
207 GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
208 SkASSERT(!back->wasDestroyed());
209 back->cacheAccess().release();
bsalomonc8dc1f72014-08-21 13:02:13 -0700210 }
bsalomonf320e042015-02-17 15:09:34 -0800211
212 while (fPurgeableQueue.count()) {
213 GrGpuResource* top = fPurgeableQueue.peek();
214 SkASSERT(!top->wasDestroyed());
215 top->cacheAccess().release();
216 }
217
bsalomon744998e2014-08-28 09:54:34 -0700218 SkASSERT(!fScratchMap.count());
bsalomon8718aaf2015-02-19 07:24:21 -0800219 SkASSERT(!fUniqueHash.count());
bsalomonc8dc1f72014-08-21 13:02:13 -0700220 SkASSERT(!fCount);
bsalomonf320e042015-02-17 15:09:34 -0800221 SkASSERT(!this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800222 SkASSERT(!fBytes);
223 SkASSERT(!fBudgetedCount);
224 SkASSERT(!fBudgetedBytes);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400225 SkASSERT(!fPurgeableBytes);
bsalomonc8dc1f72014-08-21 13:02:13 -0700226}
bsalomonbcf0a522014-10-08 08:40:09 -0700227
bsalomon0ea80f42015-02-11 10:49:59 -0800228class GrResourceCache::AvailableForScratchUse {
bsalomonbcf0a522014-10-08 08:40:09 -0700229public:
bsalomon000f8292014-10-15 19:04:14 -0700230 AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendingIO) { }
bsalomonbcf0a522014-10-08 08:40:09 -0700231
232 bool operator()(const GrGpuResource* resource) const {
robertphillipsc4ed6842016-05-24 14:17:12 -0700233 SkASSERT(!resource->getUniqueKey().isValid() &&
234 resource->resourcePriv().getScratchKey().isValid());
bsalomon12299ab2014-11-14 13:33:09 -0800235 if (resource->internalHasRef() || !resource->cacheAccess().isScratch()) {
bsalomon000f8292014-10-15 19:04:14 -0700236 return false;
bsalomonbcf0a522014-10-08 08:40:09 -0700237 }
bsalomon000f8292014-10-15 19:04:14 -0700238 return !fRejectPendingIO || !resource->internalHasPendingIO();
bsalomonbcf0a522014-10-08 08:40:09 -0700239 }
bsalomon1e2530b2014-10-09 09:57:18 -0700240
bsalomonbcf0a522014-10-08 08:40:09 -0700241private:
bsalomon000f8292014-10-15 19:04:14 -0700242 bool fRejectPendingIO;
bsalomonbcf0a522014-10-08 08:40:09 -0700243};
244
bsalomon0ea80f42015-02-11 10:49:59 -0800245GrGpuResource* GrResourceCache::findAndRefScratchResource(const GrScratchKey& scratchKey,
robertphillips6e83ac72015-08-13 05:19:14 -0700246 size_t resourceSize,
bsalomon9f2d1572015-02-17 11:47:40 -0800247 uint32_t flags) {
bsalomon7775c852014-12-30 12:50:52 -0800248 SkASSERT(scratchKey.isValid());
robertphillipsee843b22016-10-04 05:30:20 -0700249
bsalomon71cb0c22014-11-14 12:10:14 -0800250 GrGpuResource* resource;
bsalomon000f8292014-10-15 19:04:14 -0700251 if (flags & (kPreferNoPendingIO_ScratchFlag | kRequireNoPendingIO_ScratchFlag)) {
bsalomon71cb0c22014-11-14 12:10:14 -0800252 resource = fScratchMap.find(scratchKey, AvailableForScratchUse(true));
bsalomon000f8292014-10-15 19:04:14 -0700253 if (resource) {
bsalomon9f2d1572015-02-17 11:47:40 -0800254 this->refAndMakeResourceMRU(resource);
bsalomonb436ed62014-11-17 12:15:56 -0800255 this->validate();
256 return resource;
bsalomon000f8292014-10-15 19:04:14 -0700257 } else if (flags & kRequireNoPendingIO_ScratchFlag) {
halcanary96fcdcc2015-08-27 07:41:13 -0700258 return nullptr;
bsalomon000f8292014-10-15 19:04:14 -0700259 }
robertphillips63926682015-08-20 09:39:02 -0700260 // We would prefer to consume more available VRAM rather than flushing
261 // immediately, but on ANGLE this can lead to starving of the GPU.
262 if (fPreferVRAMUseOverFlushes && this->wouldFit(resourceSize)) {
robertphillips6e83ac72015-08-13 05:19:14 -0700263 // kPrefer is specified, we didn't find a resource without pending io,
robertphillips63926682015-08-20 09:39:02 -0700264 // but there is still space in our budget for the resource so force
265 // the caller to allocate a new resource.
halcanary96fcdcc2015-08-27 07:41:13 -0700266 return nullptr;
robertphillips6e83ac72015-08-13 05:19:14 -0700267 }
bsalomon000f8292014-10-15 19:04:14 -0700268 }
bsalomon71cb0c22014-11-14 12:10:14 -0800269 resource = fScratchMap.find(scratchKey, AvailableForScratchUse(false));
270 if (resource) {
bsalomon9f2d1572015-02-17 11:47:40 -0800271 this->refAndMakeResourceMRU(resource);
bsalomonb436ed62014-11-17 12:15:56 -0800272 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800273 }
274 return resource;
bsalomonbcf0a522014-10-08 08:40:09 -0700275}
bsalomon8b79d232014-11-10 10:19:06 -0800276
bsalomon0ea80f42015-02-11 10:49:59 -0800277void GrResourceCache::willRemoveScratchKey(const GrGpuResource* resource) {
bsalomon3582d3e2015-02-13 14:20:05 -0800278 SkASSERT(resource->resourcePriv().getScratchKey().isValid());
robertphillipsc4ed6842016-05-24 14:17:12 -0700279 if (!resource->getUniqueKey().isValid()) {
280 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
281 }
bsalomon10e23ca2014-11-25 05:52:06 -0800282}
283
bsalomonf99e9612015-02-19 08:24:16 -0800284void GrResourceCache::removeUniqueKey(GrGpuResource* resource) {
bsalomon3f324322015-04-08 11:01:54 -0700285 // Someone has a ref to this resource in order to have removed the key. When the ref count
286 // reaches zero we will get a ref cnt notification and figure out what to do with it.
bsalomonf99e9612015-02-19 08:24:16 -0800287 if (resource->getUniqueKey().isValid()) {
288 SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
289 fUniqueHash.remove(resource->getUniqueKey());
290 }
291 resource->cacheAccess().removeUniqueKey();
robertphillipsc4ed6842016-05-24 14:17:12 -0700292
293 if (resource->resourcePriv().getScratchKey().isValid()) {
294 fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
295 }
296
bsalomonf99e9612015-02-19 08:24:16 -0800297 this->validate();
bsalomon23e619c2015-02-06 11:54:28 -0800298}
299
bsalomonf99e9612015-02-19 08:24:16 -0800300void GrResourceCache::changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) {
bsalomon8b79d232014-11-10 10:19:06 -0800301 SkASSERT(resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800302 SkASSERT(this->isInCache(resource));
bsalomon8b79d232014-11-10 10:19:06 -0800303
bsalomonf99e9612015-02-19 08:24:16 -0800304 // If another resource has the new key, remove its key then install the key on this resource.
305 if (newKey.isValid()) {
Greg Daniel0d537802017-09-08 11:44:14 -0400306 if (GrGpuResource* old = fUniqueHash.find(newKey)) {
307 // If the old resource using the key is purgeable and is unreachable, then remove it.
308 if (!old->resourcePriv().getScratchKey().isValid() && old->isPurgeable()) {
309 old->cacheAccess().release();
310 } else {
311 this->removeUniqueKey(old);
312 }
313 }
314 SkASSERT(nullptr == fUniqueHash.find(newKey));
315
robertphillipsc4ed6842016-05-24 14:17:12 -0700316 // Remove the entry for this resource if it already has a unique key.
317 if (resource->getUniqueKey().isValid()) {
318 SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
319 fUniqueHash.remove(resource->getUniqueKey());
320 SkASSERT(nullptr == fUniqueHash.find(resource->getUniqueKey()));
321 } else {
322 // 'resource' didn't have a valid unique key before so it is switching sides. Remove it
323 // from the ScratchMap
324 if (resource->resourcePriv().getScratchKey().isValid()) {
325 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
326 }
327 }
328
bsalomonf99e9612015-02-19 08:24:16 -0800329 resource->cacheAccess().setUniqueKey(newKey);
330 fUniqueHash.add(resource);
331 } else {
robertphillipsc4ed6842016-05-24 14:17:12 -0700332 this->removeUniqueKey(resource);
bsalomonf99e9612015-02-19 08:24:16 -0800333 }
334
bsalomon71cb0c22014-11-14 12:10:14 -0800335 this->validate();
bsalomon8b79d232014-11-10 10:19:06 -0800336}
bsalomon71cb0c22014-11-14 12:10:14 -0800337
bsalomon9f2d1572015-02-17 11:47:40 -0800338void GrResourceCache::refAndMakeResourceMRU(GrGpuResource* resource) {
bsalomon71cb0c22014-11-14 12:10:14 -0800339 SkASSERT(resource);
340 SkASSERT(this->isInCache(resource));
bsalomonddf30e62015-02-19 11:38:44 -0800341
bsalomon9f2d1572015-02-17 11:47:40 -0800342 if (resource->isPurgeable()) {
343 // It's about to become unpurgeable.
Derek Sollenbergeree479142017-05-24 11:41:33 -0400344 fPurgeableBytes -= resource->gpuMemorySize();
bsalomon9f2d1572015-02-17 11:47:40 -0800345 fPurgeableQueue.remove(resource);
bsalomonf320e042015-02-17 15:09:34 -0800346 this->addToNonpurgeableArray(resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800347 }
348 resource->ref();
bsalomonddf30e62015-02-19 11:38:44 -0800349
350 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
bsalomonf320e042015-02-17 15:09:34 -0800351 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800352}
353
bsalomon3f324322015-04-08 11:01:54 -0700354void GrResourceCache::notifyCntReachedZero(GrGpuResource* resource, uint32_t flags) {
bsalomon71cb0c22014-11-14 12:10:14 -0800355 SkASSERT(resource);
bsalomon3f324322015-04-08 11:01:54 -0700356 SkASSERT(!resource->wasDestroyed());
357 SkASSERT(flags);
bsalomon71cb0c22014-11-14 12:10:14 -0800358 SkASSERT(this->isInCache(resource));
bsalomon3f324322015-04-08 11:01:54 -0700359 // This resource should always be in the nonpurgeable array when this function is called. It
360 // will be moved to the queue if it is newly purgeable.
361 SkASSERT(fNonpurgeableResources[*resource->cacheAccess().accessCacheIndex()] == resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800362
bsalomon3f324322015-04-08 11:01:54 -0700363 if (SkToBool(ResourceAccess::kRefCntReachedZero_RefNotificationFlag & flags)) {
364#ifdef SK_DEBUG
365 // When the timestamp overflows validate() is called. validate() checks that resources in
366 // the nonpurgeable array are indeed not purgeable. However, the movement from the array to
367 // the purgeable queue happens just below in this function. So we mark it as an exception.
368 if (resource->isPurgeable()) {
369 fNewlyPurgeableResourceForValidation = resource;
370 }
371#endif
372 resource->cacheAccess().setTimestamp(this->getNextTimestamp());
halcanary96fcdcc2015-08-27 07:41:13 -0700373 SkDEBUGCODE(fNewlyPurgeableResourceForValidation = nullptr);
bsalomon3f324322015-04-08 11:01:54 -0700374 }
375
376 if (!SkToBool(ResourceAccess::kAllCntsReachedZero_RefNotificationFlag & flags)) {
377 SkASSERT(!resource->isPurgeable());
378 return;
379 }
380
381 SkASSERT(resource->isPurgeable());
bsalomonf320e042015-02-17 15:09:34 -0800382 this->removeFromNonpurgeableArray(resource);
bsalomon9f2d1572015-02-17 11:47:40 -0800383 fPurgeableQueue.insert(resource);
bsalomone2e87f32016-09-22 12:42:11 -0700384 resource->cacheAccess().setFlushCntWhenResourceBecamePurgeable(fExternalFlushCnt);
Brian Salomon5e150852017-03-22 14:53:13 -0400385 resource->cacheAccess().setTimeWhenResourceBecomePurgeable();
Derek Sollenbergeree479142017-05-24 11:41:33 -0400386 fPurgeableBytes += resource->gpuMemorySize();
bsalomon71cb0c22014-11-14 12:10:14 -0800387
bsalomon5ec26ae2016-02-25 08:33:02 -0800388 if (SkBudgeted::kNo == resource->resourcePriv().isBudgeted()) {
bsalomonc2f35b72015-01-23 07:19:22 -0800389 // Check whether this resource could still be used as a scratch resource.
kkinnunen2e6055b2016-04-22 01:48:29 -0700390 if (!resource->resourcePriv().refsWrappedObjects() &&
bsalomon9f2d1572015-02-17 11:47:40 -0800391 resource->resourcePriv().getScratchKey().isValid()) {
bsalomonc2f35b72015-01-23 07:19:22 -0800392 // We won't purge an existing resource to make room for this one.
bsalomonf320e042015-02-17 15:09:34 -0800393 if (fBudgetedCount < fMaxCount &&
394 fBudgetedBytes + resource->gpuMemorySize() <= fMaxBytes) {
bsalomon3582d3e2015-02-13 14:20:05 -0800395 resource->resourcePriv().makeBudgeted();
bsalomon9f2d1572015-02-17 11:47:40 -0800396 return;
bsalomonc2f35b72015-01-23 07:19:22 -0800397 }
bsalomonc2f35b72015-01-23 07:19:22 -0800398 }
399 } else {
bsalomon9f2d1572015-02-17 11:47:40 -0800400 // Purge the resource immediately if we're over budget
bsalomon8718aaf2015-02-19 07:24:21 -0800401 // Also purge if the resource has neither a valid scratch key nor a unique key.
robertphillipsee843b22016-10-04 05:30:20 -0700402 bool noKey = !resource->resourcePriv().getScratchKey().isValid() &&
403 !resource->getUniqueKey().isValid();
404 if (!this->overBudget() && !noKey) {
bsalomon9f2d1572015-02-17 11:47:40 -0800405 return;
bsalomonc2f35b72015-01-23 07:19:22 -0800406 }
407 }
bsalomondace19e2014-11-17 07:34:06 -0800408
bsalomonf320e042015-02-17 15:09:34 -0800409 SkDEBUGCODE(int beforeCount = this->getResourceCount();)
bsalomon9f2d1572015-02-17 11:47:40 -0800410 resource->cacheAccess().release();
411 // We should at least free this resource, perhaps dependent resources as well.
bsalomonf320e042015-02-17 15:09:34 -0800412 SkASSERT(this->getResourceCount() < beforeCount);
bsalomon71cb0c22014-11-14 12:10:14 -0800413 this->validate();
414}
415
bsalomon0ea80f42015-02-11 10:49:59 -0800416void GrResourceCache::didChangeGpuMemorySize(const GrGpuResource* resource, size_t oldSize) {
bsalomon71cb0c22014-11-14 12:10:14 -0800417 // SkASSERT(!fPurging); GrPathRange increases size during flush. :(
418 SkASSERT(resource);
419 SkASSERT(this->isInCache(resource));
420
bsalomondace19e2014-11-17 07:34:06 -0800421 ptrdiff_t delta = resource->gpuMemorySize() - oldSize;
422
423 fBytes += delta;
bsalomon82b1d622014-11-14 13:59:57 -0800424#if GR_CACHE_STATS
425 fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes);
426#endif
bsalomon5ec26ae2016-02-25 08:33:02 -0800427 if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) {
bsalomondace19e2014-11-17 07:34:06 -0800428 fBudgetedBytes += delta;
Brian Osman39c08ac2017-07-26 09:36:09 -0400429 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800430 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomondace19e2014-11-17 07:34:06 -0800431#if GR_CACHE_STATS
432 fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes);
433#endif
434 }
bsalomon71cb0c22014-11-14 12:10:14 -0800435
436 this->purgeAsNeeded();
437 this->validate();
438}
439
bsalomon0ea80f42015-02-11 10:49:59 -0800440void GrResourceCache::didChangeBudgetStatus(GrGpuResource* resource) {
bsalomon84c8e622014-11-17 09:33:27 -0800441 SkASSERT(resource);
442 SkASSERT(this->isInCache(resource));
443
444 size_t size = resource->gpuMemorySize();
445
bsalomon5ec26ae2016-02-25 08:33:02 -0800446 if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) {
bsalomon84c8e622014-11-17 09:33:27 -0800447 ++fBudgetedCount;
448 fBudgetedBytes += size;
bsalomonafe30052015-01-16 07:32:33 -0800449#if GR_CACHE_STATS
450 fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes);
451 fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount);
452#endif
bsalomon84c8e622014-11-17 09:33:27 -0800453 this->purgeAsNeeded();
454 } else {
455 --fBudgetedCount;
456 fBudgetedBytes -= size;
457 }
Brian Osman39c08ac2017-07-26 09:36:09 -0400458 TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
hendrikw876c3132015-03-04 10:33:49 -0800459 fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
bsalomon84c8e622014-11-17 09:33:27 -0800460
461 this->validate();
462}
463
robertphillipsee843b22016-10-04 05:30:20 -0700464void GrResourceCache::purgeAsNeeded() {
bsalomon3f324322015-04-08 11:01:54 -0700465 SkTArray<GrUniqueKeyInvalidatedMessage> invalidKeyMsgs;
466 fInvalidUniqueKeyInbox.poll(&invalidKeyMsgs);
467 if (invalidKeyMsgs.count()) {
468 this->processInvalidUniqueKeys(invalidKeyMsgs);
469 }
bsalomon71cb0c22014-11-14 12:10:14 -0800470
Brian Osman13dddce2017-05-09 13:19:50 -0400471 this->processFreedGpuResources();
472
bsalomone2e87f32016-09-22 12:42:11 -0700473 if (fMaxUnusedFlushes > 0) {
474 // We want to know how many complete flushes have occurred without the resource being used.
475 // If the resource was tagged when fExternalFlushCnt was N then this means it became
476 // purgeable during activity that became the N+1th flush. So when the flush count is N+2
477 // it has sat in the purgeable queue for one entire flush.
478 uint32_t oldestAllowedFlushCnt = fExternalFlushCnt - fMaxUnusedFlushes - 1;
479 // check for underflow
480 if (oldestAllowedFlushCnt < fExternalFlushCnt) {
481 while (fPurgeableQueue.count()) {
482 uint32_t flushWhenResourceBecamePurgeable =
483 fPurgeableQueue.peek()->cacheAccess().flushCntWhenResourceBecamePurgeable();
484 if (oldestAllowedFlushCnt < flushWhenResourceBecamePurgeable) {
485 // Resources were given both LRU timestamps and tagged with a flush cnt when
486 // they first became purgeable. The LRU timestamp won't change again until the
487 // resource is made non-purgeable again. So, at this point all the remaining
488 // resources in the timestamp-sorted queue will have a flush count >= to this
489 // one.
490 break;
491 }
492 GrGpuResource* resource = fPurgeableQueue.peek();
493 SkASSERT(resource->isPurgeable());
494 resource->cacheAccess().release();
bsalomon3f324322015-04-08 11:01:54 -0700495 }
bsalomon3f324322015-04-08 11:01:54 -0700496 }
497 }
498
499 bool stillOverbudget = this->overBudget();
500 while (stillOverbudget && fPurgeableQueue.count()) {
robertphillipsee843b22016-10-04 05:30:20 -0700501 GrGpuResource* resource = fPurgeableQueue.peek();
bsalomon9f2d1572015-02-17 11:47:40 -0800502 SkASSERT(resource->isPurgeable());
503 resource->cacheAccess().release();
bsalomon3f324322015-04-08 11:01:54 -0700504 stillOverbudget = this->overBudget();
bsalomon9f2d1572015-02-17 11:47:40 -0800505 }
bsalomon71cb0c22014-11-14 12:10:14 -0800506
bsalomonb436ed62014-11-17 12:15:56 -0800507 this->validate();
robertphillipsee843b22016-10-04 05:30:20 -0700508
509 if (stillOverbudget) {
510 // Set this so that GrDrawingManager will issue a flush to free up resources with pending
511 // IO that we were unable to purge in this pass.
512 fRequestFlush = true;
513 }
bsalomon71cb0c22014-11-14 12:10:14 -0800514}
515
bsalomon0ea80f42015-02-11 10:49:59 -0800516void GrResourceCache::purgeAllUnlocked() {
bsalomon9f2d1572015-02-17 11:47:40 -0800517 // We could disable maintaining the heap property here, but it would add a lot of complexity.
518 // Moreover, this is rarely called.
519 while (fPurgeableQueue.count()) {
520 GrGpuResource* resource = fPurgeableQueue.peek();
521 SkASSERT(resource->isPurgeable());
522 resource->cacheAccess().release();
523 }
bsalomon71cb0c22014-11-14 12:10:14 -0800524
bsalomonb436ed62014-11-17 12:15:56 -0800525 this->validate();
bsalomon71cb0c22014-11-14 12:10:14 -0800526}
527
Brian Salomon5e150852017-03-22 14:53:13 -0400528void GrResourceCache::purgeResourcesNotUsedSince(GrStdSteadyClock::time_point purgeTime) {
529 while (fPurgeableQueue.count()) {
530 const GrStdSteadyClock::time_point resourceTime =
531 fPurgeableQueue.peek()->cacheAccess().timeWhenResourceBecamePurgeable();
532 if (resourceTime >= purgeTime) {
533 // Resources were given both LRU timestamps and tagged with a frame number when
534 // they first became purgeable. The LRU timestamp won't change again until the
535 // resource is made non-purgeable again. So, at this point all the remaining
536 // resources in the timestamp-sorted queue will have a frame number >= to this
537 // one.
538 break;
539 }
540 GrGpuResource* resource = fPurgeableQueue.peek();
541 SkASSERT(resource->isPurgeable());
542 resource->cacheAccess().release();
543 }
544}
545
Derek Sollenberger5480a182017-05-25 16:43:59 -0400546void GrResourceCache::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
547
548 const size_t tmpByteBudget = SkTMax((size_t)0, fBytes - bytesToPurge);
549 bool stillOverbudget = tmpByteBudget < fBytes;
550
551 if (preferScratchResources && bytesToPurge < fPurgeableBytes) {
552 // Sort the queue
553 fPurgeableQueue.sort();
554
555 // Make a list of the scratch resources to delete
556 SkTDArray<GrGpuResource*> scratchResources;
557 size_t scratchByteCount = 0;
558 for (int i = 0; i < fPurgeableQueue.count() && stillOverbudget; i++) {
559 GrGpuResource* resource = fPurgeableQueue.at(i);
560 SkASSERT(resource->isPurgeable());
561 if (!resource->getUniqueKey().isValid()) {
562 *scratchResources.append() = resource;
563 scratchByteCount += resource->gpuMemorySize();
564 stillOverbudget = tmpByteBudget < fBytes - scratchByteCount;
565 }
566 }
567
568 // Delete the scratch resources. This must be done as a separate pass
569 // to avoid messing up the sorted order of the queue
570 for (int i = 0; i < scratchResources.count(); i++) {
571 scratchResources.getAt(i)->cacheAccess().release();
572 }
573 stillOverbudget = tmpByteBudget < fBytes;
574
575 this->validate();
576 }
577
578 // Purge any remaining resources in LRU order
579 if (stillOverbudget) {
580 const size_t cachedByteCount = fMaxBytes;
581 fMaxBytes = tmpByteBudget;
582 this->purgeAsNeeded();
583 fMaxBytes = cachedByteCount;
584 }
585}
586
bsalomon8718aaf2015-02-19 07:24:21 -0800587void GrResourceCache::processInvalidUniqueKeys(
588 const SkTArray<GrUniqueKeyInvalidatedMessage>& msgs) {
bsalomon23e619c2015-02-06 11:54:28 -0800589 for (int i = 0; i < msgs.count(); ++i) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400590 this->processInvalidProxyUniqueKey(msgs[i].key());
591
bsalomon8718aaf2015-02-19 07:24:21 -0800592 GrGpuResource* resource = this->findAndRefUniqueResource(msgs[i].key());
bsalomon23e619c2015-02-06 11:54:28 -0800593 if (resource) {
bsalomon8718aaf2015-02-19 07:24:21 -0800594 resource->resourcePriv().removeUniqueKey();
bsalomon3f324322015-04-08 11:01:54 -0700595 resource->unref(); // If this resource is now purgeable, the cache will be notified.
bsalomon23e619c2015-02-06 11:54:28 -0800596 }
597 }
598}
599
Brian Osman13dddce2017-05-09 13:19:50 -0400600void GrResourceCache::insertCrossContextGpuResource(GrGpuResource* resource) {
601 resource->ref();
602}
603
604void GrResourceCache::processFreedGpuResources() {
605 SkTArray<GrGpuResourceFreedMessage> msgs;
606 fFreedGpuResourceInbox.poll(&msgs);
607 for (int i = 0; i < msgs.count(); ++i) {
608 if (msgs[i].fOwningUniqueID == fContextUniqueID) {
609 msgs[i].fResource->unref();
610 }
611 }
612}
613
bsalomonf320e042015-02-17 15:09:34 -0800614void GrResourceCache::addToNonpurgeableArray(GrGpuResource* resource) {
615 int index = fNonpurgeableResources.count();
616 *fNonpurgeableResources.append() = resource;
617 *resource->cacheAccess().accessCacheIndex() = index;
618}
619
620void GrResourceCache::removeFromNonpurgeableArray(GrGpuResource* resource) {
621 int* index = resource->cacheAccess().accessCacheIndex();
622 // Fill the whole we will create in the array with the tail object, adjust its index, and
623 // then pop the array
624 GrGpuResource* tail = *(fNonpurgeableResources.end() - 1);
625 SkASSERT(fNonpurgeableResources[*index] == resource);
626 fNonpurgeableResources[*index] = tail;
627 *tail->cacheAccess().accessCacheIndex() = *index;
628 fNonpurgeableResources.pop();
629 SkDEBUGCODE(*index = -1);
630}
631
bsalomonddf30e62015-02-19 11:38:44 -0800632uint32_t GrResourceCache::getNextTimestamp() {
633 // If we wrap then all the existing resources will appear older than any resources that get
634 // a timestamp after the wrap.
635 if (0 == fTimestamp) {
636 int count = this->getResourceCount();
637 if (count) {
638 // Reset all the timestamps. We sort the resources by timestamp and then assign
639 // sequential timestamps beginning with 0. This is O(n*lg(n)) but it should be extremely
640 // rare.
641 SkTDArray<GrGpuResource*> sortedPurgeableResources;
642 sortedPurgeableResources.setReserve(fPurgeableQueue.count());
643
644 while (fPurgeableQueue.count()) {
645 *sortedPurgeableResources.append() = fPurgeableQueue.peek();
646 fPurgeableQueue.pop();
647 }
robertphillipsee843b22016-10-04 05:30:20 -0700648
bsalomone2e87f32016-09-22 12:42:11 -0700649 SkTQSort(fNonpurgeableResources.begin(), fNonpurgeableResources.end() - 1,
650 CompareTimestamp);
bsalomonddf30e62015-02-19 11:38:44 -0800651
652 // Pick resources out of the purgeable and non-purgeable arrays based on lowest
653 // timestamp and assign new timestamps.
654 int currP = 0;
655 int currNP = 0;
656 while (currP < sortedPurgeableResources.count() &&
mtklein56da0252015-11-16 11:16:23 -0800657 currNP < fNonpurgeableResources.count()) {
bsalomonddf30e62015-02-19 11:38:44 -0800658 uint32_t tsP = sortedPurgeableResources[currP]->cacheAccess().timestamp();
659 uint32_t tsNP = fNonpurgeableResources[currNP]->cacheAccess().timestamp();
660 SkASSERT(tsP != tsNP);
661 if (tsP < tsNP) {
662 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
663 } else {
664 // Correct the index in the nonpurgeable array stored on the resource post-sort.
665 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
666 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
667 }
668 }
669
670 // The above loop ended when we hit the end of one array. Finish the other one.
671 while (currP < sortedPurgeableResources.count()) {
672 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
673 }
674 while (currNP < fNonpurgeableResources.count()) {
675 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
676 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
677 }
678
679 // Rebuild the queue.
680 for (int i = 0; i < sortedPurgeableResources.count(); ++i) {
681 fPurgeableQueue.insert(sortedPurgeableResources[i]);
682 }
683
684 this->validate();
685 SkASSERT(count == this->getResourceCount());
686
687 // count should be the next timestamp we return.
688 SkASSERT(fTimestamp == SkToU32(count));
mtklein56da0252015-11-16 11:16:23 -0800689 }
bsalomonddf30e62015-02-19 11:38:44 -0800690 }
691 return fTimestamp++;
692}
693
bsalomonb77a9072016-09-07 10:02:04 -0700694void GrResourceCache::notifyFlushOccurred(FlushType type) {
695 switch (type) {
bsalomonb77a9072016-09-07 10:02:04 -0700696 case FlushType::kCacheRequested:
robertphillipsee843b22016-10-04 05:30:20 -0700697 SkASSERT(fRequestFlush);
698 fRequestFlush = false;
bsalomonb77a9072016-09-07 10:02:04 -0700699 break;
robertphillipsee843b22016-10-04 05:30:20 -0700700 case FlushType::kExternal:
bsalomone2e87f32016-09-22 12:42:11 -0700701 ++fExternalFlushCnt;
702 if (0 == fExternalFlushCnt) {
703 // When this wraps just reset all the purgeable resources' last used flush state.
704 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
705 fPurgeableQueue.at(i)->cacheAccess().setFlushCntWhenResourceBecamePurgeable(0);
706 }
bsalomonb77a9072016-09-07 10:02:04 -0700707 }
708 break;
bsalomon3f324322015-04-08 11:01:54 -0700709 }
robertphillipsee843b22016-10-04 05:30:20 -0700710 this->purgeAsNeeded();
bsalomon3f324322015-04-08 11:01:54 -0700711}
712
ericrk0a5fa482015-09-15 14:16:10 -0700713void GrResourceCache::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
714 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
715 fNonpurgeableResources[i]->dumpMemoryStatistics(traceMemoryDump);
716 }
717 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
718 fPurgeableQueue.at(i)->dumpMemoryStatistics(traceMemoryDump);
719 }
720}
721
bsalomon71cb0c22014-11-14 12:10:14 -0800722#ifdef SK_DEBUG
bsalomon0ea80f42015-02-11 10:49:59 -0800723void GrResourceCache::validate() const {
bsalomonc2f35b72015-01-23 07:19:22 -0800724 // Reduce the frequency of validations for large resource counts.
725 static SkRandom gRandom;
726 int mask = (SkNextPow2(fCount + 1) >> 5) - 1;
727 if (~mask && (gRandom.nextU() & mask)) {
728 return;
729 }
730
bsalomonf320e042015-02-17 15:09:34 -0800731 struct Stats {
732 size_t fBytes;
733 int fBudgetedCount;
734 size_t fBudgetedBytes;
735 int fLocked;
736 int fScratch;
737 int fCouldBeScratch;
738 int fContent;
739 const ScratchMap* fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800740 const UniqueHash* fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800741
bsalomonf320e042015-02-17 15:09:34 -0800742 Stats(const GrResourceCache* cache) {
743 memset(this, 0, sizeof(*this));
744 fScratchMap = &cache->fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800745 fUniqueHash = &cache->fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800746 }
747
bsalomonf320e042015-02-17 15:09:34 -0800748 void update(GrGpuResource* resource) {
749 fBytes += resource->gpuMemorySize();
bsalomondace19e2014-11-17 07:34:06 -0800750
bsalomonf320e042015-02-17 15:09:34 -0800751 if (!resource->isPurgeable()) {
752 ++fLocked;
753 }
bsalomon9f2d1572015-02-17 11:47:40 -0800754
robertphillipsc4ed6842016-05-24 14:17:12 -0700755 const GrScratchKey& scratchKey = resource->resourcePriv().getScratchKey();
756 const GrUniqueKey& uniqueKey = resource->getUniqueKey();
757
bsalomonf320e042015-02-17 15:09:34 -0800758 if (resource->cacheAccess().isScratch()) {
robertphillipsc4ed6842016-05-24 14:17:12 -0700759 SkASSERT(!uniqueKey.isValid());
bsalomonf320e042015-02-17 15:09:34 -0800760 ++fScratch;
robertphillipsc4ed6842016-05-24 14:17:12 -0700761 SkASSERT(fScratchMap->countForKey(scratchKey));
kkinnunen2e6055b2016-04-22 01:48:29 -0700762 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
robertphillipsc4ed6842016-05-24 14:17:12 -0700763 } else if (scratchKey.isValid()) {
bsalomon5ec26ae2016-02-25 08:33:02 -0800764 SkASSERT(SkBudgeted::kNo == resource->resourcePriv().isBudgeted() ||
robertphillipsc4ed6842016-05-24 14:17:12 -0700765 uniqueKey.isValid());
766 if (!uniqueKey.isValid()) {
mtklein4e976072016-08-08 09:06:27 -0700767 ++fCouldBeScratch;
robertphillipsc4ed6842016-05-24 14:17:12 -0700768 SkASSERT(fScratchMap->countForKey(scratchKey));
769 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700770 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
bsalomonf320e042015-02-17 15:09:34 -0800771 }
bsalomon8718aaf2015-02-19 07:24:21 -0800772 if (uniqueKey.isValid()) {
bsalomonf320e042015-02-17 15:09:34 -0800773 ++fContent;
bsalomon8718aaf2015-02-19 07:24:21 -0800774 SkASSERT(fUniqueHash->find(uniqueKey) == resource);
Brian Osman0562eb92017-05-08 11:16:39 -0400775 SkASSERT(SkBudgeted::kYes == resource->resourcePriv().isBudgeted() ||
776 resource->resourcePriv().refsWrappedObjects());
robertphillipsc4ed6842016-05-24 14:17:12 -0700777
778 if (scratchKey.isValid()) {
779 SkASSERT(!fScratchMap->has(resource, scratchKey));
780 }
bsalomonf320e042015-02-17 15:09:34 -0800781 }
782
bsalomon5ec26ae2016-02-25 08:33:02 -0800783 if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) {
bsalomonf320e042015-02-17 15:09:34 -0800784 ++fBudgetedCount;
785 fBudgetedBytes += resource->gpuMemorySize();
786 }
bsalomon9f2d1572015-02-17 11:47:40 -0800787 }
bsalomonf320e042015-02-17 15:09:34 -0800788 };
789
robertphillipsc4ed6842016-05-24 14:17:12 -0700790 {
791 ScratchMap::ConstIter iter(&fScratchMap);
792
793 int count = 0;
794 for ( ; !iter.done(); ++iter) {
795 const GrGpuResource* resource = *iter;
796 SkASSERT(resource->resourcePriv().getScratchKey().isValid());
797 SkASSERT(!resource->getUniqueKey().isValid());
798 count++;
799 }
800 SkASSERT(count == fScratchMap.count()); // ensure the iterator is working correctly
801 }
802
bsalomonf320e042015-02-17 15:09:34 -0800803 Stats stats(this);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400804 size_t purgeableBytes = 0;
bsalomonf320e042015-02-17 15:09:34 -0800805
806 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
bsalomon3f324322015-04-08 11:01:54 -0700807 SkASSERT(!fNonpurgeableResources[i]->isPurgeable() ||
808 fNewlyPurgeableResourceForValidation == fNonpurgeableResources[i]);
bsalomonf320e042015-02-17 15:09:34 -0800809 SkASSERT(*fNonpurgeableResources[i]->cacheAccess().accessCacheIndex() == i);
810 SkASSERT(!fNonpurgeableResources[i]->wasDestroyed());
811 stats.update(fNonpurgeableResources[i]);
bsalomon71cb0c22014-11-14 12:10:14 -0800812 }
bsalomon9f2d1572015-02-17 11:47:40 -0800813 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
814 SkASSERT(fPurgeableQueue.at(i)->isPurgeable());
bsalomonf320e042015-02-17 15:09:34 -0800815 SkASSERT(*fPurgeableQueue.at(i)->cacheAccess().accessCacheIndex() == i);
816 SkASSERT(!fPurgeableQueue.at(i)->wasDestroyed());
817 stats.update(fPurgeableQueue.at(i));
Derek Sollenbergeree479142017-05-24 11:41:33 -0400818 purgeableBytes += fPurgeableQueue.at(i)->gpuMemorySize();
bsalomon9f2d1572015-02-17 11:47:40 -0800819 }
820
bsalomonf320e042015-02-17 15:09:34 -0800821 SkASSERT(fCount == this->getResourceCount());
bsalomondace19e2014-11-17 07:34:06 -0800822 SkASSERT(fBudgetedCount <= fCount);
bsalomonf320e042015-02-17 15:09:34 -0800823 SkASSERT(fBudgetedBytes <= fBytes);
824 SkASSERT(stats.fBytes == fBytes);
825 SkASSERT(stats.fBudgetedBytes == fBudgetedBytes);
826 SkASSERT(stats.fBudgetedCount == fBudgetedCount);
Derek Sollenbergeree479142017-05-24 11:41:33 -0400827 SkASSERT(purgeableBytes == fPurgeableBytes);
bsalomon71cb0c22014-11-14 12:10:14 -0800828#if GR_CACHE_STATS
bsalomondace19e2014-11-17 07:34:06 -0800829 SkASSERT(fBudgetedHighWaterCount <= fHighWaterCount);
830 SkASSERT(fBudgetedHighWaterBytes <= fHighWaterBytes);
bsalomonf320e042015-02-17 15:09:34 -0800831 SkASSERT(fBytes <= fHighWaterBytes);
832 SkASSERT(fCount <= fHighWaterCount);
833 SkASSERT(fBudgetedBytes <= fBudgetedHighWaterBytes);
834 SkASSERT(fBudgetedCount <= fBudgetedHighWaterCount);
bsalomon71cb0c22014-11-14 12:10:14 -0800835#endif
bsalomon8718aaf2015-02-19 07:24:21 -0800836 SkASSERT(stats.fContent == fUniqueHash.count());
bsalomonf320e042015-02-17 15:09:34 -0800837 SkASSERT(stats.fScratch + stats.fCouldBeScratch == fScratchMap.count());
bsalomon71cb0c22014-11-14 12:10:14 -0800838
bsalomon3f324322015-04-08 11:01:54 -0700839 // This assertion is not currently valid because we can be in recursive notifyCntReachedZero()
bsalomon12299ab2014-11-14 13:33:09 -0800840 // calls. This will be fixed when subresource registration is explicit.
bsalomondace19e2014-11-17 07:34:06 -0800841 // bool overBudget = budgetedBytes > fMaxBytes || budgetedCount > fMaxCount;
bsalomon12299ab2014-11-14 13:33:09 -0800842 // SkASSERT(!overBudget || locked == count || fPurging);
bsalomon71cb0c22014-11-14 12:10:14 -0800843}
bsalomonf320e042015-02-17 15:09:34 -0800844
845bool GrResourceCache::isInCache(const GrGpuResource* resource) const {
846 int index = *resource->cacheAccess().accessCacheIndex();
847 if (index < 0) {
848 return false;
849 }
850 if (index < fPurgeableQueue.count() && fPurgeableQueue.at(index) == resource) {
851 return true;
852 }
853 if (index < fNonpurgeableResources.count() && fNonpurgeableResources[index] == resource) {
854 return true;
855 }
856 SkDEBUGFAIL("Resource index should be -1 or the resource should be in the cache.");
857 return false;
858}
859
bsalomon71cb0c22014-11-14 12:10:14 -0800860#endif
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400861
Greg Danielcd871402017-09-26 12:49:26 -0400862void GrResourceCache::adoptUniqueKeyFromSurface(GrTextureProxy* proxy, const GrSurface* surf) {
863 SkASSERT(surf->getUniqueKey().isValid());
864 proxy->cacheAccess().setUniqueKey(this, surf->getUniqueKey());
865 SkASSERT(proxy->getUniqueKey() == surf->getUniqueKey());
866 // multiple proxies can't get the same key
867 SkASSERT(!fUniquelyKeyedProxies.find(surf->getUniqueKey()));
868 fUniquelyKeyedProxies.add(proxy);
869}
870
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400871void GrResourceCache::assignUniqueKeyToProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
872 SkASSERT(key.isValid());
873 SkASSERT(proxy);
874
875 // If there is already a GrResource with this key then the caller has violated the normal
876 // usage pattern of uniquely keyed resources (e.g., they have created one w/o first seeing
877 // if it already existed in the cache).
878 SkASSERT(!this->findAndRefUniqueResource(key));
879
880 // Uncached resources can never have a unique key, unless they're wrapped resources. Wrapped
881 // resources are a special case: the unique keys give us a weak ref so that we can reuse the
882 // same resource (rather than re-wrapping). When a wrapped resource is no longer referenced,
883 // it will always be released - it is never converted to a scratch resource.
884 if (SkBudgeted::kNo == proxy->isBudgeted() &&
885 (!proxy->priv().isInstantiated() ||
886 !proxy->priv().peekSurface()->resourcePriv().refsWrappedObjects())) {
887 return;
888 }
889
890 SkASSERT(!fUniquelyKeyedProxies.find(key)); // multiple proxies can't get the same key
891
892 proxy->cacheAccess().setUniqueKey(this, key);
893 SkASSERT(proxy->getUniqueKey() == key);
894 fUniquelyKeyedProxies.add(proxy);
895}
896
897sk_sp<GrTextureProxy> GrResourceCache::findProxyByUniqueKey(const GrUniqueKey& key,
898 GrSurfaceOrigin origin) {
899
900 sk_sp<GrTextureProxy> result = sk_ref_sp(fUniquelyKeyedProxies.find(key));
901 if (result) {
902 SkASSERT(result->origin() == origin);
Greg Danielcd871402017-09-26 12:49:26 -0400903 }
904 return result;
905}
906
907sk_sp<GrTextureProxy> GrResourceCache::findOrCreateProxyByUniqueKey(const GrUniqueKey& key,
908 GrSurfaceOrigin origin) {
909 sk_sp<GrTextureProxy> result = this->findProxyByUniqueKey(key, origin);
910 if (result) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400911 return result;
912 }
913
914 GrGpuResource* resource = findAndRefUniqueResource(key);
915 if (!resource) {
916 return nullptr;
917 }
918
919 sk_sp<GrTexture> texture(static_cast<GrSurface*>(resource)->asTexture());
920 SkASSERT(texture);
921
922 result = GrSurfaceProxy::MakeWrapped(std::move(texture), origin);
923 SkASSERT(result->getUniqueKey() == key);
Greg Danielcd871402017-09-26 12:49:26 -0400924 // MakeWrapped should've added this for us
925 SkASSERT(fUniquelyKeyedProxies.find(key));
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400926 return result;
927}
928
929void GrResourceCache::processInvalidProxyUniqueKey(const GrUniqueKey& key) {
930 // Note: this method is called for the whole variety of GrGpuResources so often 'key'
931 // will not be in 'fUniquelyKeyedProxies'.
932 GrTextureProxy* proxy = fUniquelyKeyedProxies.find(key);
933 if (proxy) {
Greg Danielcd871402017-09-26 12:49:26 -0400934 this->processInvalidProxyUniqueKey(key, proxy, false);
935 }
936}
937
938void GrResourceCache::processInvalidProxyUniqueKey(const GrUniqueKey& key, GrTextureProxy* proxy,
939 bool invalidateSurface) {
940 SkASSERT(proxy);
941 SkASSERT(proxy->getUniqueKey().isValid());
942 SkASSERT(proxy->getUniqueKey() == key);
943
944 fUniquelyKeyedProxies.remove(key);
945 proxy->cacheAccess().clearUniqueKey();
946
947 if (invalidateSurface && proxy->priv().isInstantiated()) {
948 GrSurface* surface = proxy->priv().peekSurface();
949 if (surface) {
950 surface->resourcePriv().removeUniqueKey();
951 }
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400952 }
953}
954