bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
Hal Canary | fdcfb8b | 2018-06-13 09:42:32 -0400 | [diff] [blame] | 8 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 9 | #include "GrResourceCache.h" |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 10 | |
| 11 | #include "GrCaps.h" |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 12 | #include "GrGpuResourceCacheAccess.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 13 | #include "GrProxyProvider.h" |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 14 | #include "GrTexture.h" |
| 15 | #include "GrTextureProxyCacheAccess.h" |
hendrikw | 876c313 | 2015-03-04 10:33:49 -0800 | [diff] [blame] | 16 | #include "GrTracing.h" |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 17 | #include "SkGr.h" |
| 18 | #include "SkMessageBus.h" |
mtklein | 4e97607 | 2016-08-08 09:06:27 -0700 | [diff] [blame] | 19 | #include "SkOpts.h" |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 20 | #include "SkTSort.h" |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 21 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 22 | DECLARE_SKMESSAGEBUS_MESSAGE(GrUniqueKeyInvalidatedMessage); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 23 | |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 24 | DECLARE_SKMESSAGEBUS_MESSAGE(GrGpuResourceFreedMessage); |
| 25 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 26 | ////////////////////////////////////////////////////////////////////////////// |
| 27 | |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 28 | GrScratchKey::ResourceType GrScratchKey::GenerateResourceType() { |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 29 | static int32_t gType = INHERITED::kInvalidDomain + 1; |
bsalomon | fe369ee | 2014-11-10 11:59:06 -0800 | [diff] [blame] | 30 | |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 31 | int32_t type = sk_atomic_inc(&gType); |
robertphillips | 9790a7b | 2015-01-05 12:29:15 -0800 | [diff] [blame] | 32 | if (type > SK_MaxU16) { |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 33 | SK_ABORT("Too many Resource Types"); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | return static_cast<ResourceType>(type); |
| 37 | } |
| 38 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 39 | GrUniqueKey::Domain GrUniqueKey::GenerateDomain() { |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 40 | static int32_t gDomain = INHERITED::kInvalidDomain + 1; |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 41 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 42 | int32_t domain = sk_atomic_inc(&gDomain); |
kkinnunen | 016dffb | 2015-01-23 06:43:05 -0800 | [diff] [blame] | 43 | if (domain > SK_MaxU16) { |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 44 | SK_ABORT("Too many GrUniqueKey Domains"); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 45 | } |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 46 | |
| 47 | return static_cast<Domain>(domain); |
| 48 | } |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 49 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 50 | uint32_t GrResourceKeyHash(const uint32_t* data, size_t size) { |
mtklein | 4e97607 | 2016-08-08 09:06:27 -0700 | [diff] [blame] | 51 | return SkOpts::hash(data, size); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 52 | } |
| 53 | |
bsalomon | fe369ee | 2014-11-10 11:59:06 -0800 | [diff] [blame] | 54 | ////////////////////////////////////////////////////////////////////////////// |
| 55 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 56 | class GrResourceCache::AutoValidate : ::SkNoncopyable { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 57 | public: |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 58 | AutoValidate(GrResourceCache* cache) : fCache(cache) { cache->validate(); } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 59 | ~AutoValidate() { fCache->validate(); } |
| 60 | private: |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 61 | GrResourceCache* fCache; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | ////////////////////////////////////////////////////////////////////////////// |
robertphillips | ee843b2 | 2016-10-04 05:30:20 -0700 | [diff] [blame] | 65 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 66 | |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 67 | GrResourceCache::GrResourceCache(const GrCaps* caps, uint32_t contextUniqueID) |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 68 | : fProxyProvider(nullptr) |
| 69 | , fTimestamp(0) |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 70 | , fMaxCount(kDefaultMaxCount) |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 71 | , fMaxBytes(kDefaultMaxSize) |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 72 | , fMaxUnusedFlushes(kDefaultMaxUnusedFlushes) |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 73 | #if GR_CACHE_STATS |
| 74 | , fHighWaterCount(0) |
| 75 | , fHighWaterBytes(0) |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 76 | , fBudgetedHighWaterCount(0) |
| 77 | , fBudgetedHighWaterBytes(0) |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 78 | #endif |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 79 | , fBytes(0) |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 80 | , fBudgetedCount(0) |
| 81 | , fBudgetedBytes(0) |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 82 | , fPurgeableBytes(0) |
robertphillips | ee843b2 | 2016-10-04 05:30:20 -0700 | [diff] [blame] | 83 | , fRequestFlush(false) |
bsalomon | e2e87f3 | 2016-09-22 12:42:11 -0700 | [diff] [blame] | 84 | , fExternalFlushCnt(0) |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 85 | , fContextUniqueID(contextUniqueID) |
robertphillips | 6392668 | 2015-08-20 09:39:02 -0700 | [diff] [blame] | 86 | , fPreferVRAMUseOverFlushes(caps->preferVRAMUseOverFlushes()) { |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 87 | SkDEBUGCODE(fCount = 0;) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 88 | SkDEBUGCODE(fNewlyPurgeableResourceForValidation = nullptr;) |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 89 | } |
| 90 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 91 | GrResourceCache::~GrResourceCache() { |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 92 | this->releaseAll(); |
| 93 | } |
| 94 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 95 | void GrResourceCache::setLimits(int count, size_t bytes, int maxUnusedFlushes) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 96 | fMaxCount = count; |
| 97 | fMaxBytes = bytes; |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 98 | fMaxUnusedFlushes = maxUnusedFlushes; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 99 | this->purgeAsNeeded(); |
| 100 | } |
| 101 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 102 | void GrResourceCache::insertResource(GrGpuResource* resource) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 103 | SkASSERT(resource); |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 104 | SkASSERT(!this->isInCache(resource)); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 105 | SkASSERT(!resource->wasDestroyed()); |
| 106 | SkASSERT(!resource->isPurgeable()); |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 107 | |
| 108 | // We must set the timestamp before adding to the array in case the timestamp wraps and we wind |
| 109 | // up iterating over all the resources that already have timestamps. |
| 110 | resource->cacheAccess().setTimestamp(this->getNextTimestamp()); |
| 111 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 112 | this->addToNonpurgeableArray(resource); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 113 | |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 114 | size_t size = resource->gpuMemorySize(); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 115 | SkDEBUGCODE(++fCount;) |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 116 | fBytes += size; |
bsalomon | 82b1d62 | 2014-11-14 13:59:57 -0800 | [diff] [blame] | 117 | #if GR_CACHE_STATS |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 118 | fHighWaterCount = SkTMax(this->getResourceCount(), fHighWaterCount); |
bsalomon | 82b1d62 | 2014-11-14 13:59:57 -0800 | [diff] [blame] | 119 | fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes); |
| 120 | #endif |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 121 | if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) { |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 122 | ++fBudgetedCount; |
| 123 | fBudgetedBytes += size; |
Brian Osman | 39c08ac | 2017-07-26 09:36:09 -0400 | [diff] [blame] | 124 | TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used", |
hendrikw | 876c313 | 2015-03-04 10:33:49 -0800 | [diff] [blame] | 125 | fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 126 | #if GR_CACHE_STATS |
| 127 | fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount); |
| 128 | fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes); |
| 129 | #endif |
| 130 | } |
robertphillips | c4ed684 | 2016-05-24 14:17:12 -0700 | [diff] [blame] | 131 | if (resource->resourcePriv().getScratchKey().isValid() && |
| 132 | !resource->getUniqueKey().isValid()) { |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 133 | SkASSERT(!resource->resourcePriv().refsWrappedObjects()); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 134 | fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 135 | } |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 136 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 137 | this->purgeAsNeeded(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 138 | } |
| 139 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 140 | void GrResourceCache::removeResource(GrGpuResource* resource) { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 141 | this->validate(); |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 142 | SkASSERT(this->isInCache(resource)); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 143 | |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 144 | size_t size = resource->gpuMemorySize(); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 145 | if (resource->isPurgeable()) { |
| 146 | fPurgeableQueue.remove(resource); |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 147 | fPurgeableBytes -= size; |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 148 | } else { |
| 149 | this->removeFromNonpurgeableArray(resource); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 150 | } |
| 151 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 152 | SkDEBUGCODE(--fCount;) |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 153 | fBytes -= size; |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 154 | if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) { |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 155 | --fBudgetedCount; |
| 156 | fBudgetedBytes -= size; |
Brian Osman | 39c08ac | 2017-07-26 09:36:09 -0400 | [diff] [blame] | 157 | TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used", |
hendrikw | 876c313 | 2015-03-04 10:33:49 -0800 | [diff] [blame] | 158 | fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 159 | } |
| 160 | |
robertphillips | c4ed684 | 2016-05-24 14:17:12 -0700 | [diff] [blame] | 161 | if (resource->resourcePriv().getScratchKey().isValid() && |
| 162 | !resource->getUniqueKey().isValid()) { |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 163 | fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 164 | } |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 165 | if (resource->getUniqueKey().isValid()) { |
| 166 | fUniqueHash.remove(resource->getUniqueKey()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 167 | } |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 168 | this->validate(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 169 | } |
| 170 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 171 | void GrResourceCache::abandonAll() { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 172 | AutoValidate av(this); |
| 173 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 174 | while (fNonpurgeableResources.count()) { |
| 175 | GrGpuResource* back = *(fNonpurgeableResources.end() - 1); |
| 176 | SkASSERT(!back->wasDestroyed()); |
| 177 | back->cacheAccess().abandon(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 178 | } |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 179 | |
| 180 | while (fPurgeableQueue.count()) { |
| 181 | GrGpuResource* top = fPurgeableQueue.peek(); |
| 182 | SkASSERT(!top->wasDestroyed()); |
| 183 | top->cacheAccess().abandon(); |
| 184 | } |
| 185 | |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 186 | SkASSERT(!fScratchMap.count()); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 187 | SkASSERT(!fUniqueHash.count()); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 188 | SkASSERT(!fCount); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 189 | SkASSERT(!this->getResourceCount()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 190 | SkASSERT(!fBytes); |
| 191 | SkASSERT(!fBudgetedCount); |
| 192 | SkASSERT(!fBudgetedBytes); |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 193 | SkASSERT(!fPurgeableBytes); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 194 | } |
| 195 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 196 | void GrResourceCache::releaseAll() { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 197 | AutoValidate av(this); |
| 198 | |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 199 | this->processFreedGpuResources(); |
| 200 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 201 | SkASSERT(fProxyProvider); // better have called setProxyProvider |
Robert Phillips | 3ec9573 | 2017-09-29 15:10:39 -0400 | [diff] [blame] | 202 | // We must remove the uniqueKeys from the proxies here. While they possess a uniqueKey |
| 203 | // they also have a raw pointer back to this class (which is presumably going away)! |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 204 | fProxyProvider->removeAllUniqueKeys(); |
Robert Phillips | 45a6f14 | 2017-09-29 09:49:41 -0400 | [diff] [blame] | 205 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 206 | while(fNonpurgeableResources.count()) { |
| 207 | GrGpuResource* back = *(fNonpurgeableResources.end() - 1); |
| 208 | SkASSERT(!back->wasDestroyed()); |
| 209 | back->cacheAccess().release(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 210 | } |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 211 | |
| 212 | while (fPurgeableQueue.count()) { |
| 213 | GrGpuResource* top = fPurgeableQueue.peek(); |
| 214 | SkASSERT(!top->wasDestroyed()); |
| 215 | top->cacheAccess().release(); |
| 216 | } |
| 217 | |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 218 | SkASSERT(!fScratchMap.count()); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 219 | SkASSERT(!fUniqueHash.count()); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 220 | SkASSERT(!fCount); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 221 | SkASSERT(!this->getResourceCount()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 222 | SkASSERT(!fBytes); |
| 223 | SkASSERT(!fBudgetedCount); |
| 224 | SkASSERT(!fBudgetedBytes); |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 225 | SkASSERT(!fPurgeableBytes); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 226 | } |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 227 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 228 | class GrResourceCache::AvailableForScratchUse { |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 229 | public: |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 230 | AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendingIO) { } |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 231 | |
| 232 | bool operator()(const GrGpuResource* resource) const { |
robertphillips | c4ed684 | 2016-05-24 14:17:12 -0700 | [diff] [blame] | 233 | SkASSERT(!resource->getUniqueKey().isValid() && |
| 234 | resource->resourcePriv().getScratchKey().isValid()); |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 235 | if (resource->internalHasRef() || !resource->cacheAccess().isScratch()) { |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 236 | return false; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 237 | } |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 238 | return !fRejectPendingIO || !resource->internalHasPendingIO(); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 239 | } |
bsalomon | 1e2530b | 2014-10-09 09:57:18 -0700 | [diff] [blame] | 240 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 241 | private: |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 242 | bool fRejectPendingIO; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 243 | }; |
| 244 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 245 | GrGpuResource* GrResourceCache::findAndRefScratchResource(const GrScratchKey& scratchKey, |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 246 | size_t resourceSize, |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 247 | uint32_t flags) { |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 248 | SkASSERT(scratchKey.isValid()); |
robertphillips | ee843b2 | 2016-10-04 05:30:20 -0700 | [diff] [blame] | 249 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 250 | GrGpuResource* resource; |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 251 | if (flags & (kPreferNoPendingIO_ScratchFlag | kRequireNoPendingIO_ScratchFlag)) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 252 | resource = fScratchMap.find(scratchKey, AvailableForScratchUse(true)); |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 253 | if (resource) { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 254 | this->refAndMakeResourceMRU(resource); |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 255 | this->validate(); |
| 256 | return resource; |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 257 | } else if (flags & kRequireNoPendingIO_ScratchFlag) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 258 | return nullptr; |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 259 | } |
robertphillips | 6392668 | 2015-08-20 09:39:02 -0700 | [diff] [blame] | 260 | // 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)) { |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 263 | // kPrefer is specified, we didn't find a resource without pending io, |
robertphillips | 6392668 | 2015-08-20 09:39:02 -0700 | [diff] [blame] | 264 | // but there is still space in our budget for the resource so force |
| 265 | // the caller to allocate a new resource. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 266 | return nullptr; |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 267 | } |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 268 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 269 | resource = fScratchMap.find(scratchKey, AvailableForScratchUse(false)); |
| 270 | if (resource) { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 271 | this->refAndMakeResourceMRU(resource); |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 272 | this->validate(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 273 | } |
| 274 | return resource; |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 275 | } |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 276 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 277 | void GrResourceCache::willRemoveScratchKey(const GrGpuResource* resource) { |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 278 | SkASSERT(resource->resourcePriv().getScratchKey().isValid()); |
robertphillips | c4ed684 | 2016-05-24 14:17:12 -0700 | [diff] [blame] | 279 | if (!resource->getUniqueKey().isValid()) { |
| 280 | fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource); |
| 281 | } |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 282 | } |
| 283 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 284 | void GrResourceCache::removeUniqueKey(GrGpuResource* resource) { |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 285 | // 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. |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 287 | if (resource->getUniqueKey().isValid()) { |
| 288 | SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey())); |
| 289 | fUniqueHash.remove(resource->getUniqueKey()); |
| 290 | } |
| 291 | resource->cacheAccess().removeUniqueKey(); |
robertphillips | c4ed684 | 2016-05-24 14:17:12 -0700 | [diff] [blame] | 292 | |
| 293 | if (resource->resourcePriv().getScratchKey().isValid()) { |
| 294 | fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource); |
| 295 | } |
| 296 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 297 | this->validate(); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 298 | } |
| 299 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 300 | void GrResourceCache::changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 301 | SkASSERT(resource); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 302 | SkASSERT(this->isInCache(resource)); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 303 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 304 | // If another resource has the new key, remove its key then install the key on this resource. |
| 305 | if (newKey.isValid()) { |
Greg Daniel | 0d53780 | 2017-09-08 11:44:14 -0400 | [diff] [blame] | 306 | 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 | |
robertphillips | c4ed684 | 2016-05-24 14:17:12 -0700 | [diff] [blame] | 316 | // 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 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 329 | resource->cacheAccess().setUniqueKey(newKey); |
| 330 | fUniqueHash.add(resource); |
| 331 | } else { |
robertphillips | c4ed684 | 2016-05-24 14:17:12 -0700 | [diff] [blame] | 332 | this->removeUniqueKey(resource); |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 333 | } |
| 334 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 335 | this->validate(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 336 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 337 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 338 | void GrResourceCache::refAndMakeResourceMRU(GrGpuResource* resource) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 339 | SkASSERT(resource); |
| 340 | SkASSERT(this->isInCache(resource)); |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 341 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 342 | if (resource->isPurgeable()) { |
| 343 | // It's about to become unpurgeable. |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 344 | fPurgeableBytes -= resource->gpuMemorySize(); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 345 | fPurgeableQueue.remove(resource); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 346 | this->addToNonpurgeableArray(resource); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 347 | } |
| 348 | resource->ref(); |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 349 | |
| 350 | resource->cacheAccess().setTimestamp(this->getNextTimestamp()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 351 | this->validate(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 352 | } |
| 353 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 354 | void GrResourceCache::notifyCntReachedZero(GrGpuResource* resource, uint32_t flags) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 355 | SkASSERT(resource); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 356 | SkASSERT(!resource->wasDestroyed()); |
| 357 | SkASSERT(flags); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 358 | SkASSERT(this->isInCache(resource)); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 359 | // 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); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 362 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 363 | 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()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 373 | SkDEBUGCODE(fNewlyPurgeableResourceForValidation = nullptr); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | if (!SkToBool(ResourceAccess::kAllCntsReachedZero_RefNotificationFlag & flags)) { |
| 377 | SkASSERT(!resource->isPurgeable()); |
| 378 | return; |
| 379 | } |
| 380 | |
| 381 | SkASSERT(resource->isPurgeable()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 382 | this->removeFromNonpurgeableArray(resource); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 383 | fPurgeableQueue.insert(resource); |
bsalomon | e2e87f3 | 2016-09-22 12:42:11 -0700 | [diff] [blame] | 384 | resource->cacheAccess().setFlushCntWhenResourceBecamePurgeable(fExternalFlushCnt); |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 385 | resource->cacheAccess().setTimeWhenResourceBecomePurgeable(); |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 386 | fPurgeableBytes += resource->gpuMemorySize(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 387 | |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 388 | if (SkBudgeted::kNo == resource->resourcePriv().isBudgeted()) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 389 | // Check whether this resource could still be used as a scratch resource. |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 390 | if (!resource->resourcePriv().refsWrappedObjects() && |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 391 | resource->resourcePriv().getScratchKey().isValid()) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 392 | // We won't purge an existing resource to make room for this one. |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 393 | if (fBudgetedCount < fMaxCount && |
| 394 | fBudgetedBytes + resource->gpuMemorySize() <= fMaxBytes) { |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 395 | resource->resourcePriv().makeBudgeted(); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 396 | return; |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 397 | } |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 398 | } |
| 399 | } else { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 400 | // Purge the resource immediately if we're over budget |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 401 | // Also purge if the resource has neither a valid scratch key nor a unique key. |
robertphillips | ee843b2 | 2016-10-04 05:30:20 -0700 | [diff] [blame] | 402 | bool noKey = !resource->resourcePriv().getScratchKey().isValid() && |
| 403 | !resource->getUniqueKey().isValid(); |
| 404 | if (!this->overBudget() && !noKey) { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 405 | return; |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 406 | } |
| 407 | } |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 408 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 409 | SkDEBUGCODE(int beforeCount = this->getResourceCount();) |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 410 | resource->cacheAccess().release(); |
| 411 | // We should at least free this resource, perhaps dependent resources as well. |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 412 | SkASSERT(this->getResourceCount() < beforeCount); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 413 | this->validate(); |
| 414 | } |
| 415 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 416 | void GrResourceCache::didChangeBudgetStatus(GrGpuResource* resource) { |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 417 | SkASSERT(resource); |
| 418 | SkASSERT(this->isInCache(resource)); |
| 419 | |
| 420 | size_t size = resource->gpuMemorySize(); |
| 421 | |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 422 | if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) { |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 423 | ++fBudgetedCount; |
| 424 | fBudgetedBytes += size; |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 425 | #if GR_CACHE_STATS |
| 426 | fBudgetedHighWaterBytes = SkTMax(fBudgetedBytes, fBudgetedHighWaterBytes); |
| 427 | fBudgetedHighWaterCount = SkTMax(fBudgetedCount, fBudgetedHighWaterCount); |
| 428 | #endif |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 429 | this->purgeAsNeeded(); |
| 430 | } else { |
| 431 | --fBudgetedCount; |
| 432 | fBudgetedBytes -= size; |
| 433 | } |
Brian Osman | 39c08ac | 2017-07-26 09:36:09 -0400 | [diff] [blame] | 434 | TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used", |
hendrikw | 876c313 | 2015-03-04 10:33:49 -0800 | [diff] [blame] | 435 | fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 436 | |
| 437 | this->validate(); |
| 438 | } |
| 439 | |
robertphillips | ee843b2 | 2016-10-04 05:30:20 -0700 | [diff] [blame] | 440 | void GrResourceCache::purgeAsNeeded() { |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 441 | SkTArray<GrUniqueKeyInvalidatedMessage> invalidKeyMsgs; |
| 442 | fInvalidUniqueKeyInbox.poll(&invalidKeyMsgs); |
| 443 | if (invalidKeyMsgs.count()) { |
| 444 | this->processInvalidUniqueKeys(invalidKeyMsgs); |
| 445 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 446 | |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 447 | this->processFreedGpuResources(); |
| 448 | |
bsalomon | e2e87f3 | 2016-09-22 12:42:11 -0700 | [diff] [blame] | 449 | if (fMaxUnusedFlushes > 0) { |
| 450 | // We want to know how many complete flushes have occurred without the resource being used. |
| 451 | // If the resource was tagged when fExternalFlushCnt was N then this means it became |
| 452 | // purgeable during activity that became the N+1th flush. So when the flush count is N+2 |
| 453 | // it has sat in the purgeable queue for one entire flush. |
| 454 | uint32_t oldestAllowedFlushCnt = fExternalFlushCnt - fMaxUnusedFlushes - 1; |
| 455 | // check for underflow |
| 456 | if (oldestAllowedFlushCnt < fExternalFlushCnt) { |
| 457 | while (fPurgeableQueue.count()) { |
| 458 | uint32_t flushWhenResourceBecamePurgeable = |
| 459 | fPurgeableQueue.peek()->cacheAccess().flushCntWhenResourceBecamePurgeable(); |
| 460 | if (oldestAllowedFlushCnt < flushWhenResourceBecamePurgeable) { |
| 461 | // Resources were given both LRU timestamps and tagged with a flush cnt when |
| 462 | // they first became purgeable. The LRU timestamp won't change again until the |
| 463 | // resource is made non-purgeable again. So, at this point all the remaining |
| 464 | // resources in the timestamp-sorted queue will have a flush count >= to this |
| 465 | // one. |
| 466 | break; |
| 467 | } |
| 468 | GrGpuResource* resource = fPurgeableQueue.peek(); |
| 469 | SkASSERT(resource->isPurgeable()); |
| 470 | resource->cacheAccess().release(); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 471 | } |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
| 475 | bool stillOverbudget = this->overBudget(); |
| 476 | while (stillOverbudget && fPurgeableQueue.count()) { |
robertphillips | ee843b2 | 2016-10-04 05:30:20 -0700 | [diff] [blame] | 477 | GrGpuResource* resource = fPurgeableQueue.peek(); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 478 | SkASSERT(resource->isPurgeable()); |
| 479 | resource->cacheAccess().release(); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 480 | stillOverbudget = this->overBudget(); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 481 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 482 | |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 483 | this->validate(); |
robertphillips | ee843b2 | 2016-10-04 05:30:20 -0700 | [diff] [blame] | 484 | |
| 485 | if (stillOverbudget) { |
| 486 | // Set this so that GrDrawingManager will issue a flush to free up resources with pending |
| 487 | // IO that we were unable to purge in this pass. |
| 488 | fRequestFlush = true; |
| 489 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 490 | } |
| 491 | |
Robert Phillips | 6eba063 | 2018-03-28 12:25:42 -0400 | [diff] [blame] | 492 | void GrResourceCache::purgeUnlockedResources(bool scratchResourcesOnly) { |
| 493 | if (!scratchResourcesOnly) { |
| 494 | // We could disable maintaining the heap property here, but it would add a lot of |
| 495 | // complexity. Moreover, this is rarely called. |
| 496 | while (fPurgeableQueue.count()) { |
| 497 | GrGpuResource* resource = fPurgeableQueue.peek(); |
| 498 | SkASSERT(resource->isPurgeable()); |
| 499 | resource->cacheAccess().release(); |
| 500 | } |
| 501 | } else { |
| 502 | // Sort the queue |
| 503 | fPurgeableQueue.sort(); |
| 504 | |
| 505 | // Make a list of the scratch resources to delete |
| 506 | SkTDArray<GrGpuResource*> scratchResources; |
| 507 | for (int i = 0; i < fPurgeableQueue.count(); i++) { |
| 508 | GrGpuResource* resource = fPurgeableQueue.at(i); |
| 509 | SkASSERT(resource->isPurgeable()); |
| 510 | if (!resource->getUniqueKey().isValid()) { |
| 511 | *scratchResources.append() = resource; |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | // Delete the scratch resources. This must be done as a separate pass |
| 516 | // to avoid messing up the sorted order of the queue |
| 517 | for (int i = 0; i < scratchResources.count(); i++) { |
| 518 | scratchResources.getAt(i)->cacheAccess().release(); |
| 519 | } |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 520 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 521 | |
bsalomon | b436ed6 | 2014-11-17 12:15:56 -0800 | [diff] [blame] | 522 | this->validate(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 523 | } |
| 524 | |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 525 | void GrResourceCache::purgeResourcesNotUsedSince(GrStdSteadyClock::time_point purgeTime) { |
| 526 | while (fPurgeableQueue.count()) { |
| 527 | const GrStdSteadyClock::time_point resourceTime = |
| 528 | fPurgeableQueue.peek()->cacheAccess().timeWhenResourceBecamePurgeable(); |
| 529 | if (resourceTime >= purgeTime) { |
| 530 | // Resources were given both LRU timestamps and tagged with a frame number when |
| 531 | // they first became purgeable. The LRU timestamp won't change again until the |
| 532 | // resource is made non-purgeable again. So, at this point all the remaining |
| 533 | // resources in the timestamp-sorted queue will have a frame number >= to this |
| 534 | // one. |
| 535 | break; |
| 536 | } |
| 537 | GrGpuResource* resource = fPurgeableQueue.peek(); |
| 538 | SkASSERT(resource->isPurgeable()); |
| 539 | resource->cacheAccess().release(); |
| 540 | } |
| 541 | } |
| 542 | |
Derek Sollenberger | 5480a18 | 2017-05-25 16:43:59 -0400 | [diff] [blame] | 543 | void GrResourceCache::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) { |
| 544 | |
| 545 | const size_t tmpByteBudget = SkTMax((size_t)0, fBytes - bytesToPurge); |
| 546 | bool stillOverbudget = tmpByteBudget < fBytes; |
| 547 | |
| 548 | if (preferScratchResources && bytesToPurge < fPurgeableBytes) { |
| 549 | // Sort the queue |
| 550 | fPurgeableQueue.sort(); |
| 551 | |
| 552 | // Make a list of the scratch resources to delete |
| 553 | SkTDArray<GrGpuResource*> scratchResources; |
| 554 | size_t scratchByteCount = 0; |
| 555 | for (int i = 0; i < fPurgeableQueue.count() && stillOverbudget; i++) { |
| 556 | GrGpuResource* resource = fPurgeableQueue.at(i); |
| 557 | SkASSERT(resource->isPurgeable()); |
| 558 | if (!resource->getUniqueKey().isValid()) { |
| 559 | *scratchResources.append() = resource; |
| 560 | scratchByteCount += resource->gpuMemorySize(); |
| 561 | stillOverbudget = tmpByteBudget < fBytes - scratchByteCount; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | // Delete the scratch resources. This must be done as a separate pass |
| 566 | // to avoid messing up the sorted order of the queue |
| 567 | for (int i = 0; i < scratchResources.count(); i++) { |
| 568 | scratchResources.getAt(i)->cacheAccess().release(); |
| 569 | } |
| 570 | stillOverbudget = tmpByteBudget < fBytes; |
| 571 | |
| 572 | this->validate(); |
| 573 | } |
| 574 | |
| 575 | // Purge any remaining resources in LRU order |
| 576 | if (stillOverbudget) { |
| 577 | const size_t cachedByteCount = fMaxBytes; |
| 578 | fMaxBytes = tmpByteBudget; |
| 579 | this->purgeAsNeeded(); |
| 580 | fMaxBytes = cachedByteCount; |
| 581 | } |
| 582 | } |
| 583 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 584 | void GrResourceCache::processInvalidUniqueKeys( |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 585 | const SkTArray<GrUniqueKeyInvalidatedMessage>& msgs) { |
| 586 | SkASSERT(fProxyProvider); // better have called setProxyProvider |
| 587 | |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 588 | for (int i = 0; i < msgs.count(); ++i) { |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 589 | fProxyProvider->processInvalidProxyUniqueKey(msgs[i].key()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 590 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 591 | GrGpuResource* resource = this->findAndRefUniqueResource(msgs[i].key()); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 592 | if (resource) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 593 | resource->resourcePriv().removeUniqueKey(); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 594 | resource->unref(); // If this resource is now purgeable, the cache will be notified. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | } |
| 598 | |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 599 | void GrResourceCache::insertCrossContextGpuResource(GrGpuResource* resource) { |
| 600 | resource->ref(); |
| 601 | } |
| 602 | |
| 603 | void GrResourceCache::processFreedGpuResources() { |
| 604 | SkTArray<GrGpuResourceFreedMessage> msgs; |
| 605 | fFreedGpuResourceInbox.poll(&msgs); |
| 606 | for (int i = 0; i < msgs.count(); ++i) { |
| 607 | if (msgs[i].fOwningUniqueID == fContextUniqueID) { |
| 608 | msgs[i].fResource->unref(); |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 613 | void GrResourceCache::addToNonpurgeableArray(GrGpuResource* resource) { |
| 614 | int index = fNonpurgeableResources.count(); |
| 615 | *fNonpurgeableResources.append() = resource; |
| 616 | *resource->cacheAccess().accessCacheIndex() = index; |
| 617 | } |
| 618 | |
| 619 | void GrResourceCache::removeFromNonpurgeableArray(GrGpuResource* resource) { |
| 620 | int* index = resource->cacheAccess().accessCacheIndex(); |
| 621 | // Fill the whole we will create in the array with the tail object, adjust its index, and |
| 622 | // then pop the array |
| 623 | GrGpuResource* tail = *(fNonpurgeableResources.end() - 1); |
| 624 | SkASSERT(fNonpurgeableResources[*index] == resource); |
| 625 | fNonpurgeableResources[*index] = tail; |
| 626 | *tail->cacheAccess().accessCacheIndex() = *index; |
| 627 | fNonpurgeableResources.pop(); |
| 628 | SkDEBUGCODE(*index = -1); |
| 629 | } |
| 630 | |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 631 | uint32_t GrResourceCache::getNextTimestamp() { |
| 632 | // If we wrap then all the existing resources will appear older than any resources that get |
| 633 | // a timestamp after the wrap. |
| 634 | if (0 == fTimestamp) { |
| 635 | int count = this->getResourceCount(); |
| 636 | if (count) { |
| 637 | // Reset all the timestamps. We sort the resources by timestamp and then assign |
| 638 | // sequential timestamps beginning with 0. This is O(n*lg(n)) but it should be extremely |
| 639 | // rare. |
| 640 | SkTDArray<GrGpuResource*> sortedPurgeableResources; |
| 641 | sortedPurgeableResources.setReserve(fPurgeableQueue.count()); |
| 642 | |
| 643 | while (fPurgeableQueue.count()) { |
| 644 | *sortedPurgeableResources.append() = fPurgeableQueue.peek(); |
| 645 | fPurgeableQueue.pop(); |
| 646 | } |
robertphillips | ee843b2 | 2016-10-04 05:30:20 -0700 | [diff] [blame] | 647 | |
bsalomon | e2e87f3 | 2016-09-22 12:42:11 -0700 | [diff] [blame] | 648 | SkTQSort(fNonpurgeableResources.begin(), fNonpurgeableResources.end() - 1, |
| 649 | CompareTimestamp); |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 650 | |
| 651 | // Pick resources out of the purgeable and non-purgeable arrays based on lowest |
| 652 | // timestamp and assign new timestamps. |
| 653 | int currP = 0; |
| 654 | int currNP = 0; |
| 655 | while (currP < sortedPurgeableResources.count() && |
mtklein | 56da025 | 2015-11-16 11:16:23 -0800 | [diff] [blame] | 656 | currNP < fNonpurgeableResources.count()) { |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 657 | uint32_t tsP = sortedPurgeableResources[currP]->cacheAccess().timestamp(); |
| 658 | uint32_t tsNP = fNonpurgeableResources[currNP]->cacheAccess().timestamp(); |
| 659 | SkASSERT(tsP != tsNP); |
| 660 | if (tsP < tsNP) { |
| 661 | sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++); |
| 662 | } else { |
| 663 | // Correct the index in the nonpurgeable array stored on the resource post-sort. |
| 664 | *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP; |
| 665 | fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++); |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | // The above loop ended when we hit the end of one array. Finish the other one. |
| 670 | while (currP < sortedPurgeableResources.count()) { |
| 671 | sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++); |
| 672 | } |
| 673 | while (currNP < fNonpurgeableResources.count()) { |
| 674 | *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP; |
| 675 | fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++); |
| 676 | } |
| 677 | |
| 678 | // Rebuild the queue. |
| 679 | for (int i = 0; i < sortedPurgeableResources.count(); ++i) { |
| 680 | fPurgeableQueue.insert(sortedPurgeableResources[i]); |
| 681 | } |
| 682 | |
| 683 | this->validate(); |
| 684 | SkASSERT(count == this->getResourceCount()); |
| 685 | |
| 686 | // count should be the next timestamp we return. |
| 687 | SkASSERT(fTimestamp == SkToU32(count)); |
mtklein | 56da025 | 2015-11-16 11:16:23 -0800 | [diff] [blame] | 688 | } |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 689 | } |
| 690 | return fTimestamp++; |
| 691 | } |
| 692 | |
bsalomon | b77a907 | 2016-09-07 10:02:04 -0700 | [diff] [blame] | 693 | void GrResourceCache::notifyFlushOccurred(FlushType type) { |
| 694 | switch (type) { |
bsalomon | b77a907 | 2016-09-07 10:02:04 -0700 | [diff] [blame] | 695 | case FlushType::kCacheRequested: |
robertphillips | ee843b2 | 2016-10-04 05:30:20 -0700 | [diff] [blame] | 696 | SkASSERT(fRequestFlush); |
| 697 | fRequestFlush = false; |
bsalomon | b77a907 | 2016-09-07 10:02:04 -0700 | [diff] [blame] | 698 | break; |
robertphillips | ee843b2 | 2016-10-04 05:30:20 -0700 | [diff] [blame] | 699 | case FlushType::kExternal: |
bsalomon | e2e87f3 | 2016-09-22 12:42:11 -0700 | [diff] [blame] | 700 | ++fExternalFlushCnt; |
| 701 | if (0 == fExternalFlushCnt) { |
| 702 | // When this wraps just reset all the purgeable resources' last used flush state. |
| 703 | for (int i = 0; i < fPurgeableQueue.count(); ++i) { |
| 704 | fPurgeableQueue.at(i)->cacheAccess().setFlushCntWhenResourceBecamePurgeable(0); |
| 705 | } |
bsalomon | b77a907 | 2016-09-07 10:02:04 -0700 | [diff] [blame] | 706 | } |
| 707 | break; |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 708 | } |
robertphillips | ee843b2 | 2016-10-04 05:30:20 -0700 | [diff] [blame] | 709 | this->purgeAsNeeded(); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 710 | } |
| 711 | |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 712 | void GrResourceCache::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const { |
| 713 | for (int i = 0; i < fNonpurgeableResources.count(); ++i) { |
| 714 | fNonpurgeableResources[i]->dumpMemoryStatistics(traceMemoryDump); |
| 715 | } |
| 716 | for (int i = 0; i < fPurgeableQueue.count(); ++i) { |
| 717 | fPurgeableQueue.at(i)->dumpMemoryStatistics(traceMemoryDump); |
| 718 | } |
| 719 | } |
| 720 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 721 | #ifdef SK_DEBUG |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 722 | void GrResourceCache::validate() const { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 723 | // Reduce the frequency of validations for large resource counts. |
| 724 | static SkRandom gRandom; |
| 725 | int mask = (SkNextPow2(fCount + 1) >> 5) - 1; |
| 726 | if (~mask && (gRandom.nextU() & mask)) { |
| 727 | return; |
| 728 | } |
| 729 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 730 | struct Stats { |
| 731 | size_t fBytes; |
| 732 | int fBudgetedCount; |
| 733 | size_t fBudgetedBytes; |
| 734 | int fLocked; |
| 735 | int fScratch; |
| 736 | int fCouldBeScratch; |
| 737 | int fContent; |
| 738 | const ScratchMap* fScratchMap; |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 739 | const UniqueHash* fUniqueHash; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 740 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 741 | Stats(const GrResourceCache* cache) { |
| 742 | memset(this, 0, sizeof(*this)); |
| 743 | fScratchMap = &cache->fScratchMap; |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 744 | fUniqueHash = &cache->fUniqueHash; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 745 | } |
| 746 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 747 | void update(GrGpuResource* resource) { |
| 748 | fBytes += resource->gpuMemorySize(); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 749 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 750 | if (!resource->isPurgeable()) { |
| 751 | ++fLocked; |
| 752 | } |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 753 | |
robertphillips | c4ed684 | 2016-05-24 14:17:12 -0700 | [diff] [blame] | 754 | const GrScratchKey& scratchKey = resource->resourcePriv().getScratchKey(); |
| 755 | const GrUniqueKey& uniqueKey = resource->getUniqueKey(); |
| 756 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 757 | if (resource->cacheAccess().isScratch()) { |
robertphillips | c4ed684 | 2016-05-24 14:17:12 -0700 | [diff] [blame] | 758 | SkASSERT(!uniqueKey.isValid()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 759 | ++fScratch; |
robertphillips | c4ed684 | 2016-05-24 14:17:12 -0700 | [diff] [blame] | 760 | SkASSERT(fScratchMap->countForKey(scratchKey)); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 761 | SkASSERT(!resource->resourcePriv().refsWrappedObjects()); |
robertphillips | c4ed684 | 2016-05-24 14:17:12 -0700 | [diff] [blame] | 762 | } else if (scratchKey.isValid()) { |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 763 | SkASSERT(SkBudgeted::kNo == resource->resourcePriv().isBudgeted() || |
robertphillips | c4ed684 | 2016-05-24 14:17:12 -0700 | [diff] [blame] | 764 | uniqueKey.isValid()); |
| 765 | if (!uniqueKey.isValid()) { |
mtklein | 4e97607 | 2016-08-08 09:06:27 -0700 | [diff] [blame] | 766 | ++fCouldBeScratch; |
robertphillips | c4ed684 | 2016-05-24 14:17:12 -0700 | [diff] [blame] | 767 | SkASSERT(fScratchMap->countForKey(scratchKey)); |
| 768 | } |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 769 | SkASSERT(!resource->resourcePriv().refsWrappedObjects()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 770 | } |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 771 | if (uniqueKey.isValid()) { |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 772 | ++fContent; |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 773 | SkASSERT(fUniqueHash->find(uniqueKey) == resource); |
Brian Osman | 0562eb9 | 2017-05-08 11:16:39 -0400 | [diff] [blame] | 774 | SkASSERT(SkBudgeted::kYes == resource->resourcePriv().isBudgeted() || |
| 775 | resource->resourcePriv().refsWrappedObjects()); |
robertphillips | c4ed684 | 2016-05-24 14:17:12 -0700 | [diff] [blame] | 776 | |
| 777 | if (scratchKey.isValid()) { |
| 778 | SkASSERT(!fScratchMap->has(resource, scratchKey)); |
| 779 | } |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 780 | } |
| 781 | |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 782 | if (SkBudgeted::kYes == resource->resourcePriv().isBudgeted()) { |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 783 | ++fBudgetedCount; |
| 784 | fBudgetedBytes += resource->gpuMemorySize(); |
| 785 | } |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 786 | } |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 787 | }; |
| 788 | |
robertphillips | c4ed684 | 2016-05-24 14:17:12 -0700 | [diff] [blame] | 789 | { |
| 790 | ScratchMap::ConstIter iter(&fScratchMap); |
| 791 | |
| 792 | int count = 0; |
| 793 | for ( ; !iter.done(); ++iter) { |
| 794 | const GrGpuResource* resource = *iter; |
| 795 | SkASSERT(resource->resourcePriv().getScratchKey().isValid()); |
| 796 | SkASSERT(!resource->getUniqueKey().isValid()); |
| 797 | count++; |
| 798 | } |
| 799 | SkASSERT(count == fScratchMap.count()); // ensure the iterator is working correctly |
| 800 | } |
| 801 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 802 | Stats stats(this); |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 803 | size_t purgeableBytes = 0; |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 804 | |
| 805 | for (int i = 0; i < fNonpurgeableResources.count(); ++i) { |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 806 | SkASSERT(!fNonpurgeableResources[i]->isPurgeable() || |
| 807 | fNewlyPurgeableResourceForValidation == fNonpurgeableResources[i]); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 808 | SkASSERT(*fNonpurgeableResources[i]->cacheAccess().accessCacheIndex() == i); |
| 809 | SkASSERT(!fNonpurgeableResources[i]->wasDestroyed()); |
| 810 | stats.update(fNonpurgeableResources[i]); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 811 | } |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 812 | for (int i = 0; i < fPurgeableQueue.count(); ++i) { |
| 813 | SkASSERT(fPurgeableQueue.at(i)->isPurgeable()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 814 | SkASSERT(*fPurgeableQueue.at(i)->cacheAccess().accessCacheIndex() == i); |
| 815 | SkASSERT(!fPurgeableQueue.at(i)->wasDestroyed()); |
| 816 | stats.update(fPurgeableQueue.at(i)); |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 817 | purgeableBytes += fPurgeableQueue.at(i)->gpuMemorySize(); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 818 | } |
| 819 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 820 | SkASSERT(fCount == this->getResourceCount()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 821 | SkASSERT(fBudgetedCount <= fCount); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 822 | SkASSERT(fBudgetedBytes <= fBytes); |
| 823 | SkASSERT(stats.fBytes == fBytes); |
| 824 | SkASSERT(stats.fBudgetedBytes == fBudgetedBytes); |
| 825 | SkASSERT(stats.fBudgetedCount == fBudgetedCount); |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 826 | SkASSERT(purgeableBytes == fPurgeableBytes); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 827 | #if GR_CACHE_STATS |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 828 | SkASSERT(fBudgetedHighWaterCount <= fHighWaterCount); |
| 829 | SkASSERT(fBudgetedHighWaterBytes <= fHighWaterBytes); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 830 | SkASSERT(fBytes <= fHighWaterBytes); |
| 831 | SkASSERT(fCount <= fHighWaterCount); |
| 832 | SkASSERT(fBudgetedBytes <= fBudgetedHighWaterBytes); |
| 833 | SkASSERT(fBudgetedCount <= fBudgetedHighWaterCount); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 834 | #endif |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 835 | SkASSERT(stats.fContent == fUniqueHash.count()); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 836 | SkASSERT(stats.fScratch + stats.fCouldBeScratch == fScratchMap.count()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 837 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 838 | // This assertion is not currently valid because we can be in recursive notifyCntReachedZero() |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 839 | // calls. This will be fixed when subresource registration is explicit. |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 840 | // bool overBudget = budgetedBytes > fMaxBytes || budgetedCount > fMaxCount; |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 841 | // SkASSERT(!overBudget || locked == count || fPurging); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 842 | } |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 843 | |
| 844 | bool GrResourceCache::isInCache(const GrGpuResource* resource) const { |
| 845 | int index = *resource->cacheAccess().accessCacheIndex(); |
| 846 | if (index < 0) { |
| 847 | return false; |
| 848 | } |
| 849 | if (index < fPurgeableQueue.count() && fPurgeableQueue.at(index) == resource) { |
| 850 | return true; |
| 851 | } |
| 852 | if (index < fNonpurgeableResources.count() && fNonpurgeableResources[index] == resource) { |
| 853 | return true; |
| 854 | } |
| 855 | SkDEBUGFAIL("Resource index should be -1 or the resource should be in the cache."); |
| 856 | return false; |
| 857 | } |
| 858 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 859 | #endif |