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