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