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