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 | #ifndef GrResourceCache_DEFINED |
| 9 | #define GrResourceCache_DEFINED |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkRefCnt.h" |
Robert Phillips | d074b62 | 2021-03-15 08:49:24 -0400 | [diff] [blame] | 12 | #include "include/gpu/GrDirectContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/private/GrResourceKey.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "include/private/SkTArray.h" |
| 15 | #include "include/private/SkTHash.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/SkTDPQueue.h" |
Ben Wagner | 729a23f | 2019-05-17 16:29:34 -0400 | [diff] [blame] | 18 | #include "src/core/SkTInternalLList.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "src/core/SkTMultiMap.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 20 | #include "src/gpu/GrGpuResource.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "src/gpu/GrGpuResourceCacheAccess.h" |
| 22 | #include "src/gpu/GrGpuResourcePriv.h" |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 23 | |
robertphillips | 6392668 | 2015-08-20 09:39:02 -0700 | [diff] [blame] | 24 | class GrCaps; |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 25 | class GrProxyProvider; |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 26 | class SkString; |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 27 | class SkTraceMemoryDump; |
Brian Salomon | 8f8995a | 2018-10-15 14:32:15 -0400 | [diff] [blame] | 28 | class GrSingleOwner; |
Greg Daniel | 7c90211 | 2020-03-06 13:07:10 -0500 | [diff] [blame] | 29 | class GrTexture; |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame] | 30 | class GrThreadSafeCache; |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 31 | |
Robert Phillips | ddc2148 | 2019-10-16 14:30:09 -0400 | [diff] [blame] | 32 | struct GrTextureFreedMessage { |
| 33 | GrTexture* fTexture; |
Robert Phillips | d074b62 | 2021-03-15 08:49:24 -0400 | [diff] [blame] | 34 | GrDirectContext::DirectContextID fIntendedRecipient; |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 35 | }; |
| 36 | |
Chris Dalton | 9a986cf | 2018-10-18 15:27:59 -0600 | [diff] [blame] | 37 | static inline bool SkShouldPostMessageToBus( |
Robert Phillips | d074b62 | 2021-03-15 08:49:24 -0400 | [diff] [blame] | 38 | const GrTextureFreedMessage& msg, GrDirectContext::DirectContextID potentialRecipient) { |
| 39 | return potentialRecipient == msg.fIntendedRecipient; |
Chris Dalton | 9a986cf | 2018-10-18 15:27:59 -0600 | [diff] [blame] | 40 | } |
| 41 | |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 42 | /** |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 43 | * Manages the lifetime of all GrGpuResource instances. |
| 44 | * |
| 45 | * Resources may have optionally have two types of keys: |
| 46 | * 1) A scratch key. This is for resources whose allocations are cached but not their contents. |
| 47 | * Multiple resources can share the same scratch key. This is so a caller can have two |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 48 | * resource instances with the same properties (e.g. multipass rendering that ping-pongs |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 49 | * between two temporary surfaces). The scratch key is set at resource creation time and |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 50 | * should never change. Resources need not have a scratch key. |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 51 | * 2) A unique key. This key's meaning is specific to the domain that created the key. Only one |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 52 | * resource may have a given unique key. The unique key can be set, cleared, or changed |
| 53 | * anytime after resource creation. |
| 54 | * |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 55 | * A unique key always takes precedence over a scratch key when a resource has both types of keys. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 56 | * If a resource has neither key type then it will be deleted as soon as the last reference to it |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 57 | * is dropped. |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 58 | */ |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 59 | class GrResourceCache { |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 60 | public: |
Robert Phillips | d074b62 | 2021-03-15 08:49:24 -0400 | [diff] [blame] | 61 | GrResourceCache(GrSingleOwner* owner, |
| 62 | GrDirectContext::DirectContextID owningContextID, |
| 63 | uint32_t familyID); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 64 | ~GrResourceCache(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 65 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 66 | // Default maximum number of bytes of gpu memory of budgeted resources in the cache. |
Chris Dalton | 062d2d9 | 2020-05-27 14:04:15 -0600 | [diff] [blame] | 67 | static const size_t kDefaultMaxSize = 256 * (1 << 20); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 68 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 69 | /** Used to access functionality needed by GrGpuResource for lifetime management. */ |
| 70 | class ResourceAccess; |
| 71 | ResourceAccess resourceAccess(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 72 | |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 73 | /** Unique ID of the owning GrContext. */ |
| 74 | uint32_t contextUniqueID() const { return fContextUniqueID; } |
| 75 | |
Robert Phillips | cf39f37 | 2019-09-03 10:29:20 -0400 | [diff] [blame] | 76 | /** Sets the max gpu memory byte size of the cache. */ |
| 77 | void setLimit(size_t bytes); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 78 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 79 | /** |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 80 | * Returns the number of resources. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 81 | */ |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 82 | int getResourceCount() const { |
| 83 | return fPurgeableQueue.count() + fNonpurgeableResources.count(); |
| 84 | } |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 85 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 86 | /** |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 87 | * Returns the number of resources that count against the budget. |
| 88 | */ |
| 89 | int getBudgetedResourceCount() const { return fBudgetedCount; } |
| 90 | |
| 91 | /** |
| 92 | * Returns the number of bytes consumed by resources. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 93 | */ |
| 94 | size_t getResourceBytes() const { return fBytes; } |
| 95 | |
| 96 | /** |
Robert Phillips | 12d06a3 | 2020-09-16 12:31:34 -0400 | [diff] [blame] | 97 | * Returns the number of bytes held by unlocked resources which are available for purging. |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 98 | */ |
| 99 | size_t getPurgeableBytes() const { return fPurgeableBytes; } |
| 100 | |
| 101 | /** |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 102 | * Returns the number of bytes consumed by budgeted resources. |
| 103 | */ |
| 104 | size_t getBudgetedResourceBytes() const { return fBudgetedBytes; } |
| 105 | |
| 106 | /** |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 107 | * Returns the number of bytes consumed by cached resources. |
| 108 | */ |
| 109 | size_t getMaxResourceBytes() const { return fMaxBytes; } |
| 110 | |
| 111 | /** |
| 112 | * Abandons the backend API resources owned by all GrGpuResource objects and removes them from |
| 113 | * the cache. |
| 114 | */ |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 115 | void abandonAll(); |
| 116 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 117 | /** |
| 118 | * Releases the backend API resources owned by all GrGpuResource objects and removes them from |
| 119 | * the cache. |
| 120 | */ |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 121 | void releaseAll(); |
| 122 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 123 | /** |
| 124 | * Find a resource that matches a scratch key. |
| 125 | */ |
Robert Phillips | aee18c9 | 2019-09-06 11:48:27 -0400 | [diff] [blame] | 126 | GrGpuResource* findAndRefScratchResource(const GrScratchKey& scratchKey); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 127 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 128 | #ifdef SK_DEBUG |
| 129 | // This is not particularly fast and only used for validation, so debug only. |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 130 | int countScratchEntriesForKey(const GrScratchKey& scratchKey) const { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 131 | return fScratchMap.countForKey(scratchKey); |
| 132 | } |
| 133 | #endif |
| 134 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 135 | /** |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 136 | * Find a resource that matches a unique key. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 137 | */ |
robertphillips | ee843b2 | 2016-10-04 05:30:20 -0700 | [diff] [blame] | 138 | GrGpuResource* findAndRefUniqueResource(const GrUniqueKey& key) { |
| 139 | GrGpuResource* resource = fUniqueHash.find(key); |
| 140 | if (resource) { |
| 141 | this->refAndMakeResourceMRU(resource); |
| 142 | } |
| 143 | return resource; |
| 144 | } |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 145 | |
Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 146 | /** |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 147 | * Query whether a unique key exists in the cache. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 148 | */ |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 149 | bool hasUniqueKey(const GrUniqueKey& key) const { |
| 150 | return SkToBool(fUniqueHash.find(key)); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 151 | } |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 152 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 153 | /** Purges resources to become under budget and processes resources with invalidated unique |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 154 | keys. */ |
robertphillips | ee843b2 | 2016-10-04 05:30:20 -0700 | [diff] [blame] | 155 | void purgeAsNeeded(); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 156 | |
Robert Phillips | 6eba063 | 2018-03-28 12:25:42 -0400 | [diff] [blame] | 157 | // Purge unlocked resources. If 'scratchResourcesOnly' is true the purgeable resources |
| 158 | // containing persistent data are spared. If it is false then all purgeable resources will |
| 159 | // be deleted. |
Michael Ludwig | 9d1cc05 | 2021-06-09 20:49:48 -0400 | [diff] [blame] | 160 | void purgeUnlockedResources(bool scratchResourcesOnly=false) { |
| 161 | this->purgeUnlockedResources(/*purgeTime=*/nullptr, scratchResourcesOnly); |
| 162 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 163 | |
Michael Ludwig | 9d1cc05 | 2021-06-09 20:49:48 -0400 | [diff] [blame] | 164 | // Purge unlocked resources not used since the passed point in time. If 'scratchResourcesOnly' |
| 165 | // is true the purgeable resources containing persistent data are spared. If it is false then |
| 166 | // all purgeable resources older than 'purgeTime' will be deleted. |
| 167 | void purgeResourcesNotUsedSince(GrStdSteadyClock::time_point purgeTime, |
| 168 | bool scratchResourcesOnly=false) { |
| 169 | this->purgeUnlockedResources(&purgeTime, scratchResourcesOnly); |
| 170 | } |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 171 | |
Adlai Holler | e1c8a38 | 2021-04-08 15:30:12 -0400 | [diff] [blame] | 172 | /** If it's possible to purge enough resources to get the provided amount of budget |
| 173 | headroom, do so and return true. If it's not possible, do nothing and return false. |
| 174 | */ |
| 175 | bool purgeToMakeHeadroom(size_t desiredHeadroomBytes); |
| 176 | |
Robert Phillips | cf39f37 | 2019-09-03 10:29:20 -0400 | [diff] [blame] | 177 | bool overBudget() const { return fBudgetedBytes > fMaxBytes; } |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 178 | |
Derek Sollenberger | 5480a18 | 2017-05-25 16:43:59 -0400 | [diff] [blame] | 179 | /** |
| 180 | * Purge unlocked resources from the cache until the the provided byte count has been reached |
| 181 | * or we have purged all unlocked resources. The default policy is to purge in LRU order, but |
| 182 | * can be overridden to prefer purging scratch resources (in LRU order) prior to purging other |
| 183 | * resource types. |
| 184 | * |
| 185 | * @param maxBytesToPurge the desired number of bytes to be purged. |
| 186 | * @param preferScratchResources If true scratch resources will be purged prior to other |
| 187 | * resource types. |
| 188 | */ |
| 189 | void purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources); |
| 190 | |
bsalomon | b77a907 | 2016-09-07 10:02:04 -0700 | [diff] [blame] | 191 | /** Returns true if the cache would like a flush to occur in order to make more resources |
| 192 | purgeable. */ |
Brian Salomon | 8cefa3e | 2019-04-04 11:39:55 -0400 | [diff] [blame] | 193 | bool requestsFlush() const; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 194 | |
Robert Phillips | ddc2148 | 2019-10-16 14:30:09 -0400 | [diff] [blame] | 195 | /** Maintain a ref to this texture until we receive a GrTextureFreedMessage. */ |
| 196 | void insertDelayedTextureUnref(GrTexture*); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 197 | |
robertphillips | 60029a5 | 2015-11-09 13:51:06 -0800 | [diff] [blame] | 198 | #if GR_CACHE_STATS |
| 199 | struct Stats { |
| 200 | int fTotal; |
| 201 | int fNumPurgeable; |
| 202 | int fNumNonPurgeable; |
| 203 | |
| 204 | int fScratch; |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 205 | int fWrapped; |
robertphillips | 60029a5 | 2015-11-09 13:51:06 -0800 | [diff] [blame] | 206 | size_t fUnbudgetedSize; |
| 207 | |
| 208 | Stats() { this->reset(); } |
| 209 | |
| 210 | void reset() { |
| 211 | fTotal = 0; |
| 212 | fNumPurgeable = 0; |
| 213 | fNumNonPurgeable = 0; |
| 214 | fScratch = 0; |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 215 | fWrapped = 0; |
robertphillips | 60029a5 | 2015-11-09 13:51:06 -0800 | [diff] [blame] | 216 | fUnbudgetedSize = 0; |
| 217 | } |
| 218 | |
| 219 | void update(GrGpuResource* resource) { |
| 220 | if (resource->cacheAccess().isScratch()) { |
| 221 | ++fScratch; |
| 222 | } |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 223 | if (resource->resourcePriv().refsWrappedObjects()) { |
| 224 | ++fWrapped; |
robertphillips | 60029a5 | 2015-11-09 13:51:06 -0800 | [diff] [blame] | 225 | } |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 226 | if (GrBudgetedType::kBudgeted != resource->resourcePriv().budgetedType()) { |
robertphillips | 60029a5 | 2015-11-09 13:51:06 -0800 | [diff] [blame] | 227 | fUnbudgetedSize += resource->gpuMemorySize(); |
| 228 | } |
| 229 | } |
| 230 | }; |
| 231 | |
| 232 | void getStats(Stats*) const; |
| 233 | |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 234 | #if GR_TEST_UTILS |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 235 | void dumpStats(SkString*) const; |
joshualitt | dc5685a | 2015-12-02 14:08:25 -0800 | [diff] [blame] | 236 | |
| 237 | void dumpStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* value) const; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 238 | #endif |
| 239 | |
Robert Phillips | 72354b0 | 2021-07-06 13:43:56 -0400 | [diff] [blame] | 240 | #endif // GR_CACHE_STATS |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 241 | |
Robert Phillips | 72354b0 | 2021-07-06 13:43:56 -0400 | [diff] [blame] | 242 | #if GR_TEST_UTILS |
Brian Salomon | 1090da6 | 2017-01-06 12:04:19 -0500 | [diff] [blame] | 243 | int countUniqueKeysWithTag(const char* tag) const; |
Brian Salomon | 1090da6 | 2017-01-06 12:04:19 -0500 | [diff] [blame] | 244 | |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 245 | // This function is for unit testing and is only defined in test tools. |
| 246 | void changeTimestamp(uint32_t newTimestamp); |
Robert Phillips | 72354b0 | 2021-07-06 13:43:56 -0400 | [diff] [blame] | 247 | #endif |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 248 | |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 249 | // Enumerates all cached resources and dumps their details to traceMemoryDump. |
| 250 | void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const; |
| 251 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 252 | void setProxyProvider(GrProxyProvider* proxyProvider) { fProxyProvider = proxyProvider; } |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame] | 253 | void setThreadSafeCache(GrThreadSafeCache* threadSafeCache) { |
| 254 | fThreadSafeCache = threadSafeCache; |
Robert Phillips | 12d06a3 | 2020-09-16 12:31:34 -0400 | [diff] [blame] | 255 | } |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 256 | |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 257 | private: |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 258 | /////////////////////////////////////////////////////////////////////////// |
| 259 | /// @name Methods accessible via ResourceAccess |
| 260 | //// |
| 261 | void insertResource(GrGpuResource*); |
| 262 | void removeResource(GrGpuResource*); |
Greg Daniel | da64261 | 2021-02-09 18:04:02 -0500 | [diff] [blame] | 263 | void notifyARefCntReachedZero(GrGpuResource*, GrGpuResource::LastRemovedRef); |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 264 | void changeUniqueKey(GrGpuResource*, const GrUniqueKey&); |
| 265 | void removeUniqueKey(GrGpuResource*); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 266 | void willRemoveScratchKey(const GrGpuResource*); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 267 | void didChangeBudgetStatus(GrGpuResource*); |
Brian Salomon | 2c791fc | 2019-04-02 11:52:03 -0400 | [diff] [blame] | 268 | void refResource(GrGpuResource* resource); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 269 | /// @} |
| 270 | |
Brian Salomon | 2c791fc | 2019-04-02 11:52:03 -0400 | [diff] [blame] | 271 | void refAndMakeResourceMRU(GrGpuResource*); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 272 | void processFreedGpuResources(); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 273 | void addToNonpurgeableArray(GrGpuResource*); |
| 274 | void removeFromNonpurgeableArray(GrGpuResource*); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 275 | |
Robert Phillips | cf39f37 | 2019-09-03 10:29:20 -0400 | [diff] [blame] | 276 | bool wouldFit(size_t bytes) const { return fBudgetedBytes+bytes <= fMaxBytes; } |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 277 | |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 278 | uint32_t getNextTimestamp(); |
| 279 | |
Michael Ludwig | 9d1cc05 | 2021-06-09 20:49:48 -0400 | [diff] [blame] | 280 | void purgeUnlockedResources(const GrStdSteadyClock::time_point* purgeTime, |
| 281 | bool scratchResourcesOnly); |
| 282 | |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 283 | #ifdef SK_DEBUG |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 284 | bool isInCache(const GrGpuResource* r) const; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 285 | void validate() const; |
| 286 | #else |
| 287 | void validate() const {} |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 288 | #endif |
| 289 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 290 | class AutoValidate; |
| 291 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 292 | class AvailableForScratchUse; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 293 | |
robertphillips | ee843b2 | 2016-10-04 05:30:20 -0700 | [diff] [blame] | 294 | struct ScratchMapTraits { |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 295 | static const GrScratchKey& GetKey(const GrGpuResource& r) { |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 296 | return r.resourcePriv().getScratchKey(); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 297 | } |
robertphillips | ee843b2 | 2016-10-04 05:30:20 -0700 | [diff] [blame] | 298 | |
| 299 | static uint32_t Hash(const GrScratchKey& key) { return key.hash(); } |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 300 | static void OnFree(GrGpuResource*) { } |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 301 | }; |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 302 | typedef SkTMultiMap<GrGpuResource, GrScratchKey, ScratchMapTraits> ScratchMap; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 303 | |
robertphillips | ee843b2 | 2016-10-04 05:30:20 -0700 | [diff] [blame] | 304 | struct UniqueHashTraits { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 305 | static const GrUniqueKey& GetKey(const GrGpuResource& r) { return r.getUniqueKey(); } |
robertphillips | ee843b2 | 2016-10-04 05:30:20 -0700 | [diff] [blame] | 306 | |
| 307 | static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); } |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 308 | }; |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 309 | typedef SkTDynamicHash<GrGpuResource, GrUniqueKey, UniqueHashTraits> UniqueHash; |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 310 | |
Robert Phillips | ddc2148 | 2019-10-16 14:30:09 -0400 | [diff] [blame] | 311 | class TextureAwaitingUnref { |
Brian Salomon | 876a017 | 2019-03-08 11:12:14 -0500 | [diff] [blame] | 312 | public: |
Robert Phillips | ddc2148 | 2019-10-16 14:30:09 -0400 | [diff] [blame] | 313 | TextureAwaitingUnref(); |
| 314 | TextureAwaitingUnref(GrTexture* texture); |
| 315 | TextureAwaitingUnref(const TextureAwaitingUnref&) = delete; |
| 316 | TextureAwaitingUnref& operator=(const TextureAwaitingUnref&) = delete; |
| 317 | TextureAwaitingUnref(TextureAwaitingUnref&&); |
| 318 | TextureAwaitingUnref& operator=(TextureAwaitingUnref&&); |
| 319 | ~TextureAwaitingUnref(); |
Brian Salomon | 876a017 | 2019-03-08 11:12:14 -0500 | [diff] [blame] | 320 | void addRef(); |
| 321 | void unref(); |
| 322 | bool finished(); |
| 323 | |
| 324 | private: |
Robert Phillips | ddc2148 | 2019-10-16 14:30:09 -0400 | [diff] [blame] | 325 | GrTexture* fTexture = nullptr; |
Brian Salomon | 876a017 | 2019-03-08 11:12:14 -0500 | [diff] [blame] | 326 | int fNumUnrefs = 0; |
| 327 | }; |
Robert Phillips | ddc2148 | 2019-10-16 14:30:09 -0400 | [diff] [blame] | 328 | using TexturesAwaitingUnref = SkTHashMap<uint32_t, TextureAwaitingUnref>; |
Brian Salomon | 876a017 | 2019-03-08 11:12:14 -0500 | [diff] [blame] | 329 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 330 | static bool CompareTimestamp(GrGpuResource* const& a, GrGpuResource* const& b) { |
| 331 | return a->cacheAccess().timestamp() < b->cacheAccess().timestamp(); |
| 332 | } |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 333 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 334 | static int* AccessResourceIndex(GrGpuResource* const& res) { |
| 335 | return res->cacheAccess().accessCacheIndex(); |
| 336 | } |
| 337 | |
Robert Phillips | d074b62 | 2021-03-15 08:49:24 -0400 | [diff] [blame] | 338 | using TextureFreedMessageBus = SkMessageBus<GrTextureFreedMessage, |
| 339 | GrDirectContext::DirectContextID>; |
| 340 | |
Robert Phillips | e7a959d | 2021-03-11 14:44:42 -0500 | [diff] [blame] | 341 | typedef SkMessageBus<GrUniqueKeyInvalidatedMessage, uint32_t>::Inbox InvalidUniqueKeyInbox; |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 342 | typedef SkTDPQueue<GrGpuResource*, CompareTimestamp, AccessResourceIndex> PurgeableQueue; |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 343 | typedef SkTDArray<GrGpuResource*> ResourceArray; |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 344 | |
Brian Salomon | 2c791fc | 2019-04-02 11:52:03 -0400 | [diff] [blame] | 345 | GrProxyProvider* fProxyProvider = nullptr; |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame] | 346 | GrThreadSafeCache* fThreadSafeCache = nullptr; |
Robert Phillips | 12d06a3 | 2020-09-16 12:31:34 -0400 | [diff] [blame] | 347 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 348 | // Whenever a resource is added to the cache or the result of a cache lookup, fTimestamp is |
| 349 | // assigned as the resource's timestamp and then incremented. fPurgeableQueue orders the |
| 350 | // purgeable resources by this value, and thus is used to purge resources in LRU order. |
Brian Salomon | 2c791fc | 2019-04-02 11:52:03 -0400 | [diff] [blame] | 351 | uint32_t fTimestamp = 0; |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 352 | PurgeableQueue fPurgeableQueue; |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 353 | ResourceArray fNonpurgeableResources; |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 354 | |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 355 | // This map holds all resources that can be used as scratch resources. |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 356 | ScratchMap fScratchMap; |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 357 | // This holds all resources that have unique keys. |
| 358 | UniqueHash fUniqueHash; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 359 | |
| 360 | // our budget, used in purgeAsNeeded() |
Brian Salomon | 2c791fc | 2019-04-02 11:52:03 -0400 | [diff] [blame] | 361 | size_t fMaxBytes = kDefaultMaxSize; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 362 | |
| 363 | #if GR_CACHE_STATS |
Brian Salomon | 2c791fc | 2019-04-02 11:52:03 -0400 | [diff] [blame] | 364 | int fHighWaterCount = 0; |
| 365 | size_t fHighWaterBytes = 0; |
| 366 | int fBudgetedHighWaterCount = 0; |
| 367 | size_t fBudgetedHighWaterBytes = 0; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 368 | #endif |
| 369 | |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 370 | // our current stats for all resources |
Brian Salomon | 2c791fc | 2019-04-02 11:52:03 -0400 | [diff] [blame] | 371 | SkDEBUGCODE(int fCount = 0;) |
| 372 | size_t fBytes = 0; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 373 | |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 374 | // our current stats for resources that count against the budget |
Brian Salomon | 2c791fc | 2019-04-02 11:52:03 -0400 | [diff] [blame] | 375 | int fBudgetedCount = 0; |
| 376 | size_t fBudgetedBytes = 0; |
| 377 | size_t fPurgeableBytes = 0; |
| 378 | int fNumBudgetedResourcesFlushWillMakePurgeable = 0; |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 379 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 380 | InvalidUniqueKeyInbox fInvalidUniqueKeyInbox; |
Robert Phillips | d074b62 | 2021-03-15 08:49:24 -0400 | [diff] [blame] | 381 | TextureFreedMessageBus::Inbox fFreedTextureInbox; |
Robert Phillips | ddc2148 | 2019-10-16 14:30:09 -0400 | [diff] [blame] | 382 | TexturesAwaitingUnref fTexturesAwaitingUnref; |
Greg Daniel | c27eb72 | 2018-08-10 09:48:08 -0400 | [diff] [blame] | 383 | |
Robert Phillips | d074b62 | 2021-03-15 08:49:24 -0400 | [diff] [blame] | 384 | GrDirectContext::DirectContextID fOwningContextID; |
Brian Salomon | 2c791fc | 2019-04-02 11:52:03 -0400 | [diff] [blame] | 385 | uint32_t fContextUniqueID = SK_InvalidUniqueID; |
| 386 | GrSingleOwner* fSingleOwner = nullptr; |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 387 | |
| 388 | // This resource is allowed to be in the nonpurgeable array for the sake of validate() because |
| 389 | // we're in the midst of converting it to purgeable status. |
Brian Salomon | 2c791fc | 2019-04-02 11:52:03 -0400 | [diff] [blame] | 390 | SkDEBUGCODE(GrGpuResource* fNewlyPurgeableResourceForValidation = nullptr;) |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 391 | }; |
| 392 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 393 | class GrResourceCache::ResourceAccess { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 394 | private: |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 395 | ResourceAccess(GrResourceCache* cache) : fCache(cache) { } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 396 | ResourceAccess(const ResourceAccess& that) : fCache(that.fCache) { } |
John Stiles | b35c3b8 | 2020-08-06 19:58:52 -0400 | [diff] [blame] | 397 | ResourceAccess& operator=(const ResourceAccess&) = delete; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 398 | |
| 399 | /** |
| 400 | * Insert a resource into the cache. |
| 401 | */ |
| 402 | void insertResource(GrGpuResource* resource) { fCache->insertResource(resource); } |
| 403 | |
| 404 | /** |
| 405 | * Removes a resource from the cache. |
| 406 | */ |
| 407 | void removeResource(GrGpuResource* resource) { fCache->removeResource(resource); } |
| 408 | |
| 409 | /** |
Brian Salomon | 2c791fc | 2019-04-02 11:52:03 -0400 | [diff] [blame] | 410 | * Adds a ref to a resource with proper tracking if the resource has 0 refs prior to |
| 411 | * adding the ref. |
| 412 | */ |
| 413 | void refResource(GrGpuResource* resource) { fCache->refResource(resource); } |
| 414 | |
| 415 | /** |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 416 | * Notifications that should be sent to the cache when the ref/io cnt status of resources |
| 417 | * changes. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 418 | */ |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 419 | enum RefNotificationFlags { |
| 420 | /** All types of refs on the resource have reached zero. */ |
| 421 | kAllCntsReachedZero_RefNotificationFlag = 0x1, |
| 422 | /** The normal (not pending IO type) ref cnt has reached zero. */ |
| 423 | kRefCntReachedZero_RefNotificationFlag = 0x2, |
| 424 | }; |
| 425 | /** |
Greg Daniel | da64261 | 2021-02-09 18:04:02 -0500 | [diff] [blame] | 426 | * Called by GrGpuResources when they detect one of their ref cnts have reached zero. This may |
| 427 | * either be the main ref or the command buffer usage ref. |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 428 | */ |
Greg Daniel | da64261 | 2021-02-09 18:04:02 -0500 | [diff] [blame] | 429 | void notifyARefCntReachedZero(GrGpuResource* resource, |
| 430 | GrGpuResource::LastRemovedRef removedRef) { |
| 431 | fCache->notifyARefCntReachedZero(resource, removedRef); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 432 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 433 | |
| 434 | /** |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 435 | * Called by GrGpuResources to change their unique keys. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 436 | */ |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 437 | void changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) { |
| 438 | fCache->changeUniqueKey(resource, newKey); |
| 439 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 440 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 441 | /** |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 442 | * Called by a GrGpuResource to remove its unique key. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 443 | */ |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 444 | void removeUniqueKey(GrGpuResource* resource) { fCache->removeUniqueKey(resource); } |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 445 | |
| 446 | /** |
| 447 | * Called by a GrGpuResource when it removes its scratch key. |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 448 | */ |
| 449 | void willRemoveScratchKey(const GrGpuResource* resource) { |
| 450 | fCache->willRemoveScratchKey(resource); |
| 451 | } |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 452 | |
| 453 | /** |
| 454 | * Called by GrGpuResources when they change from budgeted to unbudgeted or vice versa. |
| 455 | */ |
| 456 | void didChangeBudgetStatus(GrGpuResource* resource) { fCache->didChangeBudgetStatus(resource); } |
| 457 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 458 | // No taking addresses of this type. |
| 459 | const ResourceAccess* operator&() const; |
| 460 | ResourceAccess* operator&(); |
| 461 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 462 | GrResourceCache* fCache; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 463 | |
| 464 | friend class GrGpuResource; // To access all the proxy inline methods. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 465 | friend class GrResourceCache; // To create this type. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 466 | }; |
| 467 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 468 | inline GrResourceCache::ResourceAccess GrResourceCache::resourceAccess() { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 469 | return ResourceAccess(this); |
| 470 | } |
| 471 | |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 472 | #endif |