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