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