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