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