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 | b77a907 | 2016-09-07 10:02:04 -0700 | [diff] [blame] | 14 | #include "GrResourceCache.h" |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 15 | #include "GrResourceKey.h" |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 16 | #include "SkMessageBus.h" |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 17 | #include "SkRefCnt.h" |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 18 | #include "SkTArray.h" |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 19 | #include "SkTDPQueue.h" |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 20 | #include "SkTInternalLList.h" |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 21 | #include "SkTMultiMap.h" |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 22 | |
robertphillips | 6392668 | 2015-08-20 09:39:02 -0700 | [diff] [blame] | 23 | class GrCaps; |
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 | |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 32 | /** |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 33 | * Manages the lifetime of all GrGpuResource instances. |
| 34 | * |
| 35 | * Resources may have optionally have two types of keys: |
| 36 | * 1) A scratch key. This is for resources whose allocations are cached but not their contents. |
| 37 | * 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] | 38 | * resource instances with the same properties (e.g. multipass rendering that ping-pongs |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 39 | * 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] | 40 | * should never change. Resources need not have a scratch key. |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 41 | * 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] | 42 | * resource may have a given unique key. The unique key can be set, cleared, or changed |
| 43 | * anytime after resource creation. |
| 44 | * |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 45 | * 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] | 46 | * 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] | 47 | * is dropped. |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 48 | */ |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 49 | class GrResourceCache { |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 50 | public: |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 51 | GrResourceCache(const GrCaps* caps, uint32_t contextUniqueID); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 52 | ~GrResourceCache(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 53 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 54 | // Default maximum number of budgeted resources in the cache. |
| 55 | static const int kDefaultMaxCount = 2 * (1 << 12); |
| 56 | // Default maximum number of bytes of gpu memory of budgeted resources in the cache. |
| 57 | static const size_t kDefaultMaxSize = 96 * (1 << 20); |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 58 | // Default number of external flushes a budgeted resources can go unused in the cache before it |
| 59 | // is purged. Using a value <= 0 disables this feature. This will be removed once Chrome |
| 60 | // starts using time-based purging. |
bsalomon | e2e87f3 | 2016-09-22 12:42:11 -0700 | [diff] [blame] | 61 | static const int kDefaultMaxUnusedFlushes = |
| 62 | 1 * /* flushes per frame */ |
| 63 | 60 * /* fps */ |
| 64 | 30; /* seconds */ |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 65 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 66 | /** Used to access functionality needed by GrGpuResource for lifetime management. */ |
| 67 | class ResourceAccess; |
| 68 | ResourceAccess resourceAccess(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 69 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 70 | /** |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 71 | * 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] | 72 | * of external GrContext flushes that a resource can be unused before it is evicted. The latter |
| 73 | * value is a suggestion and there is no promise that a resource will be purged immediately |
| 74 | * after it hasn't been used in maxUnusedFlushes flushes. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 75 | */ |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 76 | void setLimits(int count, size_t bytes, int maxUnusedFlushes = kDefaultMaxUnusedFlushes); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 77 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 78 | /** |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 79 | * Returns the number of resources. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 80 | */ |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 81 | int getResourceCount() const { |
| 82 | return fPurgeableQueue.count() + fNonpurgeableResources.count(); |
| 83 | } |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [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 that count against the budget. |
| 87 | */ |
| 88 | int getBudgetedResourceCount() const { return fBudgetedCount; } |
| 89 | |
| 90 | /** |
| 91 | * Returns the number of bytes consumed by resources. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 92 | */ |
| 93 | size_t getResourceBytes() const { return fBytes; } |
| 94 | |
| 95 | /** |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 96 | * Returns the number of bytes held by unlocked reosources which are available for purging. |
| 97 | */ |
| 98 | size_t getPurgeableBytes() const { return fPurgeableBytes; } |
| 99 | |
| 100 | /** |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 101 | * Returns the number of bytes consumed by budgeted resources. |
| 102 | */ |
| 103 | size_t getBudgetedResourceBytes() const { return fBudgetedBytes; } |
| 104 | |
| 105 | /** |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 106 | * Returns the cached resources count budget. |
| 107 | */ |
| 108 | int getMaxResourceCount() const { return fMaxCount; } |
| 109 | |
| 110 | /** |
| 111 | * Returns the number of bytes consumed by cached resources. |
| 112 | */ |
| 113 | size_t getMaxResourceBytes() const { return fMaxBytes; } |
| 114 | |
| 115 | /** |
| 116 | * Abandons the backend API resources owned by all GrGpuResource objects and removes them from |
| 117 | * the cache. |
| 118 | */ |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 119 | void abandonAll(); |
| 120 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 121 | /** |
| 122 | * Releases the backend API resources owned by all GrGpuResource objects and removes them from |
| 123 | * the cache. |
| 124 | */ |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 125 | void releaseAll(); |
| 126 | |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 127 | enum { |
| 128 | /** Preferentially returns scratch resources with no pending IO. */ |
| 129 | kPreferNoPendingIO_ScratchFlag = 0x1, |
| 130 | /** Will not return any resources that match but have pending IO. */ |
| 131 | kRequireNoPendingIO_ScratchFlag = 0x2, |
| 132 | }; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 133 | |
| 134 | /** |
| 135 | * Find a resource that matches a scratch key. |
| 136 | */ |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 137 | GrGpuResource* findAndRefScratchResource(const GrScratchKey& scratchKey, |
| 138 | size_t resourceSize, |
| 139 | uint32_t flags); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 140 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 141 | #ifdef SK_DEBUG |
| 142 | // This is not particularly fast and only used for validation, so debug only. |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 143 | int countScratchEntriesForKey(const GrScratchKey& scratchKey) const { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 144 | return fScratchMap.countForKey(scratchKey); |
| 145 | } |
| 146 | #endif |
| 147 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 148 | /** |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 149 | * Find a resource that matches a unique key. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 150 | */ |
robertphillips | ee843b2 | 2016-10-04 05:30:20 -0700 | [diff] [blame] | 151 | GrGpuResource* findAndRefUniqueResource(const GrUniqueKey& key) { |
| 152 | GrGpuResource* resource = fUniqueHash.find(key); |
| 153 | if (resource) { |
| 154 | this->refAndMakeResourceMRU(resource); |
| 155 | } |
| 156 | return resource; |
| 157 | } |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 158 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 159 | /** |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 160 | * Query whether a unique key exists in the cache. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 161 | */ |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 162 | bool hasUniqueKey(const GrUniqueKey& key) const { |
| 163 | return SkToBool(fUniqueHash.find(key)); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 164 | } |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 165 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 166 | /** Purges resources to become under budget and processes resources with invalidated unique |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 167 | keys. */ |
robertphillips | ee843b2 | 2016-10-04 05:30:20 -0700 | [diff] [blame] | 168 | void purgeAsNeeded(); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 169 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 170 | /** Purges all resources that don't have external owners. */ |
| 171 | void purgeAllUnlocked(); |
| 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 | |
Derek Sollenberger | 5480a18 | 2017-05-25 16:43:59 -0400 | [diff] [blame] | 176 | /** |
| 177 | * Purge unlocked resources from the cache until the the provided byte count has been reached |
| 178 | * or we have purged all unlocked resources. The default policy is to purge in LRU order, but |
| 179 | * can be overridden to prefer purging scratch resources (in LRU order) prior to purging other |
| 180 | * resource types. |
| 181 | * |
| 182 | * @param maxBytesToPurge the desired number of bytes to be purged. |
| 183 | * @param preferScratchResources If true scratch resources will be purged prior to other |
| 184 | * resource types. |
| 185 | */ |
| 186 | void purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources); |
| 187 | |
bsalomon | b77a907 | 2016-09-07 10:02:04 -0700 | [diff] [blame] | 188 | /** Returns true if the cache would like a flush to occur in order to make more resources |
| 189 | purgeable. */ |
robertphillips | ee843b2 | 2016-10-04 05:30:20 -0700 | [diff] [blame] | 190 | bool requestsFlush() const { return fRequestFlush; } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 191 | |
bsalomon | b77a907 | 2016-09-07 10:02:04 -0700 | [diff] [blame] | 192 | enum FlushType { |
| 193 | kExternal, |
bsalomon | b77a907 | 2016-09-07 10:02:04 -0700 | [diff] [blame] | 194 | kCacheRequested, |
| 195 | }; |
| 196 | void notifyFlushOccurred(FlushType); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 197 | |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 198 | /** Maintain a ref to this resource until we receive a GrGpuResourceFreedMessage. */ |
| 199 | void insertCrossContextGpuResource(GrGpuResource* resource); |
| 200 | |
robertphillips | 60029a5 | 2015-11-09 13:51:06 -0800 | [diff] [blame] | 201 | #if GR_CACHE_STATS |
| 202 | struct Stats { |
| 203 | int fTotal; |
| 204 | int fNumPurgeable; |
| 205 | int fNumNonPurgeable; |
| 206 | |
| 207 | int fScratch; |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 208 | int fWrapped; |
robertphillips | 60029a5 | 2015-11-09 13:51:06 -0800 | [diff] [blame] | 209 | size_t fUnbudgetedSize; |
| 210 | |
| 211 | Stats() { this->reset(); } |
| 212 | |
| 213 | void reset() { |
| 214 | fTotal = 0; |
| 215 | fNumPurgeable = 0; |
| 216 | fNumNonPurgeable = 0; |
| 217 | fScratch = 0; |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 218 | fWrapped = 0; |
robertphillips | 60029a5 | 2015-11-09 13:51:06 -0800 | [diff] [blame] | 219 | fUnbudgetedSize = 0; |
| 220 | } |
| 221 | |
| 222 | void update(GrGpuResource* resource) { |
| 223 | if (resource->cacheAccess().isScratch()) { |
| 224 | ++fScratch; |
| 225 | } |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 226 | if (resource->resourcePriv().refsWrappedObjects()) { |
| 227 | ++fWrapped; |
robertphillips | 60029a5 | 2015-11-09 13:51:06 -0800 | [diff] [blame] | 228 | } |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 229 | if (SkBudgeted::kNo == resource->resourcePriv().isBudgeted()) { |
robertphillips | 60029a5 | 2015-11-09 13:51:06 -0800 | [diff] [blame] | 230 | fUnbudgetedSize += resource->gpuMemorySize(); |
| 231 | } |
| 232 | } |
| 233 | }; |
| 234 | |
| 235 | void getStats(Stats*) const; |
| 236 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 237 | void dumpStats(SkString*) const; |
joshualitt | dc5685a | 2015-12-02 14:08:25 -0800 | [diff] [blame] | 238 | |
| 239 | void dumpStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* value) const; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 240 | #endif |
| 241 | |
Brian Salomon | 1090da6 | 2017-01-06 12:04:19 -0500 | [diff] [blame] | 242 | #ifdef SK_DEBUG |
| 243 | int countUniqueKeysWithTag(const char* tag) const; |
| 244 | #endif |
| 245 | |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 246 | // This function is for unit testing and is only defined in test tools. |
| 247 | void changeTimestamp(uint32_t newTimestamp); |
| 248 | |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 249 | // Enumerates all cached resources and dumps their details to traceMemoryDump. |
| 250 | void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const; |
| 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 | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 259 | void didChangeGpuMemorySize(const GrGpuResource*, size_t oldSize); |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 260 | void changeUniqueKey(GrGpuResource*, const GrUniqueKey&); |
| 261 | void removeUniqueKey(GrGpuResource*); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 262 | void willRemoveScratchKey(const GrGpuResource*); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 263 | void didChangeBudgetStatus(GrGpuResource*); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 264 | void refAndMakeResourceMRU(GrGpuResource*); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 265 | /// @} |
| 266 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 267 | void processInvalidUniqueKeys(const SkTArray<GrUniqueKeyInvalidatedMessage>&); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 268 | void processFreedGpuResources(); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 269 | void addToNonpurgeableArray(GrGpuResource*); |
| 270 | void removeFromNonpurgeableArray(GrGpuResource*); |
| 271 | bool overBudget() const { return fBudgetedBytes > fMaxBytes || fBudgetedCount > fMaxCount; } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 272 | |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 273 | bool wouldFit(size_t bytes) { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 274 | return fBudgetedBytes+bytes <= fMaxBytes && fBudgetedCount+1 <= fMaxCount; |
robertphillips | 6e83ac7 | 2015-08-13 05:19:14 -0700 | [diff] [blame] | 275 | } |
| 276 | |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 277 | uint32_t getNextTimestamp(); |
| 278 | |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 279 | #ifdef SK_DEBUG |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 280 | bool isInCache(const GrGpuResource* r) const; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 281 | void validate() const; |
| 282 | #else |
| 283 | void validate() const {} |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 284 | #endif |
| 285 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 286 | class AutoValidate; |
| 287 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 288 | class AvailableForScratchUse; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 289 | |
robertphillips | ee843b2 | 2016-10-04 05:30:20 -0700 | [diff] [blame] | 290 | struct ScratchMapTraits { |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 291 | static const GrScratchKey& GetKey(const GrGpuResource& r) { |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 292 | return r.resourcePriv().getScratchKey(); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 293 | } |
robertphillips | ee843b2 | 2016-10-04 05:30:20 -0700 | [diff] [blame] | 294 | |
| 295 | static uint32_t Hash(const GrScratchKey& key) { return key.hash(); } |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 296 | }; |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 297 | typedef SkTMultiMap<GrGpuResource, GrScratchKey, ScratchMapTraits> ScratchMap; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 298 | |
robertphillips | ee843b2 | 2016-10-04 05:30:20 -0700 | [diff] [blame] | 299 | struct UniqueHashTraits { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 300 | static const GrUniqueKey& GetKey(const GrGpuResource& r) { return r.getUniqueKey(); } |
robertphillips | ee843b2 | 2016-10-04 05:30:20 -0700 | [diff] [blame] | 301 | |
| 302 | static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); } |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 303 | }; |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 304 | typedef SkTDynamicHash<GrGpuResource, GrUniqueKey, UniqueHashTraits> UniqueHash; |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 305 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 306 | static bool CompareTimestamp(GrGpuResource* const& a, GrGpuResource* const& b) { |
| 307 | return a->cacheAccess().timestamp() < b->cacheAccess().timestamp(); |
| 308 | } |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 309 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 310 | static int* AccessResourceIndex(GrGpuResource* const& res) { |
| 311 | return res->cacheAccess().accessCacheIndex(); |
| 312 | } |
| 313 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 314 | typedef SkMessageBus<GrUniqueKeyInvalidatedMessage>::Inbox InvalidUniqueKeyInbox; |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 315 | typedef SkMessageBus<GrGpuResourceFreedMessage>::Inbox FreedGpuResourceInbox; |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 316 | typedef SkTDPQueue<GrGpuResource*, CompareTimestamp, AccessResourceIndex> PurgeableQueue; |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 317 | typedef SkTDArray<GrGpuResource*> ResourceArray; |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 318 | |
| 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; |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 334 | int fMaxUnusedFlushes; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 335 | |
| 336 | #if GR_CACHE_STATS |
| 337 | int fHighWaterCount; |
| 338 | size_t fHighWaterBytes; |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 339 | int fBudgetedHighWaterCount; |
| 340 | size_t fBudgetedHighWaterBytes; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 341 | #endif |
| 342 | |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 343 | // our current stats for all resources |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 344 | SkDEBUGCODE(int fCount;) |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 345 | size_t fBytes; |
| 346 | |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 347 | // our current stats for resources that count against the budget |
| 348 | int fBudgetedCount; |
| 349 | size_t fBudgetedBytes; |
Derek Sollenberger | ee47914 | 2017-05-24 11:41:33 -0400 | [diff] [blame] | 350 | size_t fPurgeableBytes; |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 351 | |
robertphillips | ee843b2 | 2016-10-04 05:30:20 -0700 | [diff] [blame] | 352 | bool fRequestFlush; |
bsalomon | e2e87f3 | 2016-09-22 12:42:11 -0700 | [diff] [blame] | 353 | uint32_t fExternalFlushCnt; |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 354 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 355 | InvalidUniqueKeyInbox fInvalidUniqueKeyInbox; |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 356 | FreedGpuResourceInbox fFreedGpuResourceInbox; |
| 357 | |
| 358 | uint32_t fContextUniqueID; |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 359 | |
| 360 | // This resource is allowed to be in the nonpurgeable array for the sake of validate() because |
| 361 | // we're in the midst of converting it to purgeable status. |
| 362 | SkDEBUGCODE(GrGpuResource* fNewlyPurgeableResourceForValidation;) |
robertphillips | 6392668 | 2015-08-20 09:39:02 -0700 | [diff] [blame] | 363 | |
| 364 | bool fPreferVRAMUseOverFlushes; |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 365 | }; |
| 366 | |
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 | /** |
| 406 | * Called by GrGpuResources when their sizes change. |
| 407 | */ |
| 408 | void didChangeGpuMemorySize(const GrGpuResource* resource, size_t oldSize) { |
| 409 | fCache->didChangeGpuMemorySize(resource, oldSize); |
| 410 | } |
| 411 | |
| 412 | /** |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 413 | * Called by GrGpuResources to change their unique keys. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 414 | */ |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 415 | void changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) { |
| 416 | fCache->changeUniqueKey(resource, newKey); |
| 417 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 418 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 419 | /** |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 420 | * Called by a GrGpuResource to remove its unique key. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 421 | */ |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 422 | void removeUniqueKey(GrGpuResource* resource) { fCache->removeUniqueKey(resource); } |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 423 | |
| 424 | /** |
| 425 | * Called by a GrGpuResource when it removes its scratch key. |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 426 | */ |
| 427 | void willRemoveScratchKey(const GrGpuResource* resource) { |
| 428 | fCache->willRemoveScratchKey(resource); |
| 429 | } |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 430 | |
| 431 | /** |
| 432 | * Called by GrGpuResources when they change from budgeted to unbudgeted or vice versa. |
| 433 | */ |
| 434 | void didChangeBudgetStatus(GrGpuResource* resource) { fCache->didChangeBudgetStatus(resource); } |
| 435 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 436 | // No taking addresses of this type. |
| 437 | const ResourceAccess* operator&() const; |
| 438 | ResourceAccess* operator&(); |
| 439 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 440 | GrResourceCache* fCache; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 441 | |
| 442 | friend class GrGpuResource; // To access all the proxy inline methods. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 443 | friend class GrResourceCache; // To create this type. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 444 | }; |
| 445 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 446 | inline GrResourceCache::ResourceAccess GrResourceCache::resourceAccess() { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 447 | return ResourceAccess(this); |
| 448 | } |
| 449 | |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 450 | #endif |