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