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