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