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