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