bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2014 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 9 | #ifndef GrResourceCache_DEFINED |
| 10 | #define GrResourceCache_DEFINED |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 11 | |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 12 | #include "GrGpuResource.h" |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 13 | #include "GrGpuResourceCacheAccess.h" |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 14 | #include "GrGpuResourcePriv.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 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 23 | class SkString; |
| 24 | |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 25 | /** |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 26 | * Manages the lifetime of all GrGpuResource instances. |
| 27 | * |
| 28 | * Resources may have optionally have two types of keys: |
| 29 | * 1) A scratch key. This is for resources whose allocations are cached but not their contents. |
| 30 | * 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] | 31 | * resource instances with the same properties (e.g. multipass rendering that ping-pongs |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 32 | * 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] | 33 | * should never change. Resources need not have a scratch key. |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 34 | * 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] | 35 | * resource may have a given unique key. The unique key can be set, cleared, or changed |
| 36 | * anytime after resource creation. |
| 37 | * |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 38 | * 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] | 39 | * 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] | 40 | * is dropped. |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 41 | */ |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 42 | class GrResourceCache { |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 43 | public: |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 44 | GrResourceCache(); |
| 45 | ~GrResourceCache(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 46 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 47 | /** Used to access functionality needed by GrGpuResource for lifetime management. */ |
| 48 | class ResourceAccess; |
| 49 | ResourceAccess resourceAccess(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 50 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 51 | /** |
| 52 | * Sets the cache limits in terms of number of resources and max gpu memory byte size. |
| 53 | */ |
| 54 | void setLimits(int count, size_t bytes); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 55 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 56 | /** |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 57 | * Returns the number of resources. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 58 | */ |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 59 | int getResourceCount() const { |
| 60 | return fPurgeableQueue.count() + fNonpurgeableResources.count(); |
| 61 | } |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 62 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 63 | /** |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 64 | * Returns the number of resources that count against the budget. |
| 65 | */ |
| 66 | int getBudgetedResourceCount() const { return fBudgetedCount; } |
| 67 | |
| 68 | /** |
| 69 | * Returns the number of bytes consumed by resources. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 70 | */ |
| 71 | size_t getResourceBytes() const { return fBytes; } |
| 72 | |
| 73 | /** |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 74 | * Returns the number of bytes consumed by budgeted resources. |
| 75 | */ |
| 76 | size_t getBudgetedResourceBytes() const { return fBudgetedBytes; } |
| 77 | |
| 78 | /** |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 79 | * Returns the cached resources count budget. |
| 80 | */ |
| 81 | int getMaxResourceCount() const { return fMaxCount; } |
| 82 | |
| 83 | /** |
| 84 | * Returns the number of bytes consumed by cached resources. |
| 85 | */ |
| 86 | size_t getMaxResourceBytes() const { return fMaxBytes; } |
| 87 | |
| 88 | /** |
| 89 | * Abandons the backend API resources owned by all GrGpuResource objects and removes them from |
| 90 | * the cache. |
| 91 | */ |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 92 | void abandonAll(); |
| 93 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 94 | /** |
| 95 | * Releases the backend API resources owned by all GrGpuResource objects and removes them from |
| 96 | * the cache. |
| 97 | */ |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 98 | void releaseAll(); |
| 99 | |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 100 | enum { |
| 101 | /** Preferentially returns scratch resources with no pending IO. */ |
| 102 | kPreferNoPendingIO_ScratchFlag = 0x1, |
| 103 | /** Will not return any resources that match but have pending IO. */ |
| 104 | kRequireNoPendingIO_ScratchFlag = 0x2, |
| 105 | }; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 106 | |
| 107 | /** |
| 108 | * Find a resource that matches a scratch key. |
| 109 | */ |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 110 | GrGpuResource* findAndRefScratchResource(const GrScratchKey& scratchKey, uint32_t flags = 0); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 111 | |
| 112 | #ifdef SK_DEBUG |
| 113 | // This is not particularly fast and only used for validation, so debug only. |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 114 | int countScratchEntriesForKey(const GrScratchKey& scratchKey) const { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 115 | return fScratchMap.countForKey(scratchKey); |
| 116 | } |
| 117 | #endif |
| 118 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 119 | /** |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 120 | * Find a resource that matches a unique key. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 121 | */ |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 122 | GrGpuResource* findAndRefUniqueResource(const GrUniqueKey& key) { |
| 123 | GrGpuResource* resource = fUniqueHash.find(key); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 124 | if (resource) { |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 125 | this->refAndMakeResourceMRU(resource); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 126 | } |
| 127 | return resource; |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 128 | } |
| 129 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 130 | /** |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 131 | * Query whether a unique key exists in the cache. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 132 | */ |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 133 | bool hasUniqueKey(const GrUniqueKey& key) const { |
| 134 | return SkToBool(fUniqueHash.find(key)); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 135 | } |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 136 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 137 | /** Purges resources to become under budget and processes resources with invalidated unique |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 138 | keys. */ |
| 139 | void purgeAsNeeded() { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 140 | SkTArray<GrUniqueKeyInvalidatedMessage> invalidKeyMsgs; |
| 141 | fInvalidUniqueKeyInbox.poll(&invalidKeyMsgs); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 142 | if (invalidKeyMsgs.count()) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 143 | this->processInvalidUniqueKeys(invalidKeyMsgs); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 144 | } |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 145 | if (fBudgetedCount <= fMaxCount && fBudgetedBytes <= fMaxBytes) { |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 146 | return; |
| 147 | } |
| 148 | this->internalPurgeAsNeeded(); |
| 149 | } |
| 150 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 151 | /** Purges all resources that don't have external owners. */ |
| 152 | void purgeAllUnlocked(); |
| 153 | |
| 154 | /** |
| 155 | * The callback function used by the cache when it is still over budget after a purge. The |
| 156 | * passed in 'data' is the same 'data' handed to setOverbudgetCallback. |
| 157 | */ |
| 158 | typedef void (*PFOverBudgetCB)(void* data); |
| 159 | |
| 160 | /** |
| 161 | * Set the callback the cache should use when it is still over budget after a purge. The 'data' |
| 162 | * provided here will be passed back to the callback. Note that the cache will attempt to purge |
| 163 | * any resources newly freed by the callback. |
| 164 | */ |
| 165 | void setOverBudgetCallback(PFOverBudgetCB overBudgetCB, void* data) { |
| 166 | fOverBudgetCB = overBudgetCB; |
| 167 | fOverBudgetData = data; |
| 168 | } |
| 169 | |
| 170 | #if GR_GPU_STATS |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 171 | void dumpStats(SkString*) const; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 172 | #endif |
| 173 | |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 174 | // This function is for unit testing and is only defined in test tools. |
| 175 | void changeTimestamp(uint32_t newTimestamp); |
| 176 | |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 177 | private: |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 178 | /////////////////////////////////////////////////////////////////////////// |
| 179 | /// @name Methods accessible via ResourceAccess |
| 180 | //// |
| 181 | void insertResource(GrGpuResource*); |
| 182 | void removeResource(GrGpuResource*); |
bsalomon | 63c992f | 2015-01-23 12:47:59 -0800 | [diff] [blame] | 183 | void notifyPurgeable(GrGpuResource*); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 184 | void didChangeGpuMemorySize(const GrGpuResource*, size_t oldSize); |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 185 | void changeUniqueKey(GrGpuResource*, const GrUniqueKey&); |
| 186 | void removeUniqueKey(GrGpuResource*); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 187 | void willRemoveScratchKey(const GrGpuResource*); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 188 | void didChangeBudgetStatus(GrGpuResource*); |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 189 | void refAndMakeResourceMRU(GrGpuResource*); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 190 | /// @} |
| 191 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 192 | void internalPurgeAsNeeded(); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 193 | void processInvalidUniqueKeys(const SkTArray<GrUniqueKeyInvalidatedMessage>&); |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 194 | void addToNonpurgeableArray(GrGpuResource*); |
| 195 | void removeFromNonpurgeableArray(GrGpuResource*); |
| 196 | bool overBudget() const { return fBudgetedBytes > fMaxBytes || fBudgetedCount > fMaxCount; } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 197 | |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 198 | uint32_t getNextTimestamp(); |
| 199 | |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 200 | #ifdef SK_DEBUG |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 201 | bool isInCache(const GrGpuResource* r) const; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 202 | void validate() const; |
| 203 | #else |
| 204 | void validate() const {} |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 205 | #endif |
| 206 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 207 | class AutoValidate; |
| 208 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 209 | class AvailableForScratchUse; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 210 | |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 211 | struct ScratchMapTraits { |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 212 | static const GrScratchKey& GetKey(const GrGpuResource& r) { |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 213 | return r.resourcePriv().getScratchKey(); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 214 | } |
| 215 | |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 216 | static uint32_t Hash(const GrScratchKey& key) { return key.hash(); } |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 217 | }; |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 218 | typedef SkTMultiMap<GrGpuResource, GrScratchKey, ScratchMapTraits> ScratchMap; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 219 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 220 | struct UniqueHashTraits { |
| 221 | static const GrUniqueKey& GetKey(const GrGpuResource& r) { return r.getUniqueKey(); } |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 222 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 223 | static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); } |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 224 | }; |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 225 | typedef SkTDynamicHash<GrGpuResource, GrUniqueKey, UniqueHashTraits> UniqueHash; |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 226 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 227 | static bool CompareTimestamp(GrGpuResource* const& a, GrGpuResource* const& b) { |
| 228 | return a->cacheAccess().timestamp() < b->cacheAccess().timestamp(); |
| 229 | } |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 230 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 231 | static int* AccessResourceIndex(GrGpuResource* const& res) { |
| 232 | return res->cacheAccess().accessCacheIndex(); |
| 233 | } |
| 234 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 235 | typedef SkMessageBus<GrUniqueKeyInvalidatedMessage>::Inbox InvalidUniqueKeyInbox; |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 236 | typedef SkTDPQueue<GrGpuResource*, CompareTimestamp, AccessResourceIndex> PurgeableQueue; |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 237 | typedef SkTDArray<GrGpuResource*> ResourceArray; |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 238 | |
| 239 | // Whenever a resource is added to the cache or the result of a cache lookup, fTimestamp is |
| 240 | // assigned as the resource's timestamp and then incremented. fPurgeableQueue orders the |
| 241 | // purgeable resources by this value, and thus is used to purge resources in LRU order. |
| 242 | uint32_t fTimestamp; |
| 243 | PurgeableQueue fPurgeableQueue; |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 244 | ResourceArray fNonpurgeableResources; |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 245 | |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 246 | // This map holds all resources that can be used as scratch resources. |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 247 | ScratchMap fScratchMap; |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 248 | // This holds all resources that have unique keys. |
| 249 | UniqueHash fUniqueHash; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 250 | |
| 251 | // our budget, used in purgeAsNeeded() |
| 252 | int fMaxCount; |
| 253 | size_t fMaxBytes; |
| 254 | |
| 255 | #if GR_CACHE_STATS |
| 256 | int fHighWaterCount; |
| 257 | size_t fHighWaterBytes; |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 258 | int fBudgetedHighWaterCount; |
| 259 | size_t fBudgetedHighWaterBytes; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 260 | #endif |
| 261 | |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 262 | // our current stats for all resources |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 263 | SkDEBUGCODE(int fCount;) |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 264 | size_t fBytes; |
| 265 | |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 266 | // our current stats for resources that count against the budget |
| 267 | int fBudgetedCount; |
| 268 | size_t fBudgetedBytes; |
| 269 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 270 | PFOverBudgetCB fOverBudgetCB; |
| 271 | void* fOverBudgetData; |
| 272 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 273 | InvalidUniqueKeyInbox fInvalidUniqueKeyInbox; |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 274 | }; |
| 275 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 276 | class GrResourceCache::ResourceAccess { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 277 | private: |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 278 | ResourceAccess(GrResourceCache* cache) : fCache(cache) { } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 279 | ResourceAccess(const ResourceAccess& that) : fCache(that.fCache) { } |
| 280 | ResourceAccess& operator=(const ResourceAccess&); // unimpl |
| 281 | |
| 282 | /** |
| 283 | * Insert a resource into the cache. |
| 284 | */ |
| 285 | void insertResource(GrGpuResource* resource) { fCache->insertResource(resource); } |
| 286 | |
| 287 | /** |
| 288 | * Removes a resource from the cache. |
| 289 | */ |
| 290 | void removeResource(GrGpuResource* resource) { fCache->removeResource(resource); } |
| 291 | |
| 292 | /** |
bsalomon | 63c992f | 2015-01-23 12:47:59 -0800 | [diff] [blame] | 293 | * Called by GrGpuResources when they detects that they are newly purgeable. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 294 | */ |
bsalomon | 63c992f | 2015-01-23 12:47:59 -0800 | [diff] [blame] | 295 | void notifyPurgeable(GrGpuResource* resource) { fCache->notifyPurgeable(resource); } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 296 | |
| 297 | /** |
| 298 | * Called by GrGpuResources when their sizes change. |
| 299 | */ |
| 300 | void didChangeGpuMemorySize(const GrGpuResource* resource, size_t oldSize) { |
| 301 | fCache->didChangeGpuMemorySize(resource, oldSize); |
| 302 | } |
| 303 | |
| 304 | /** |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 305 | * Called by GrGpuResources to change their unique keys. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 306 | */ |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 307 | void changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) { |
| 308 | fCache->changeUniqueKey(resource, newKey); |
| 309 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 310 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 311 | /** |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 312 | * Called by a GrGpuResource to remove its unique key. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 313 | */ |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 314 | void removeUniqueKey(GrGpuResource* resource) { fCache->removeUniqueKey(resource); } |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 315 | |
| 316 | /** |
| 317 | * Called by a GrGpuResource when it removes its scratch key. |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 318 | */ |
| 319 | void willRemoveScratchKey(const GrGpuResource* resource) { |
| 320 | fCache->willRemoveScratchKey(resource); |
| 321 | } |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 322 | |
| 323 | /** |
| 324 | * Called by GrGpuResources when they change from budgeted to unbudgeted or vice versa. |
| 325 | */ |
| 326 | void didChangeBudgetStatus(GrGpuResource* resource) { fCache->didChangeBudgetStatus(resource); } |
| 327 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 328 | // No taking addresses of this type. |
| 329 | const ResourceAccess* operator&() const; |
| 330 | ResourceAccess* operator&(); |
| 331 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 332 | GrResourceCache* fCache; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 333 | |
| 334 | friend class GrGpuResource; // To access all the proxy inline methods. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 335 | friend class GrResourceCache; // To create this type. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 336 | }; |
| 337 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 338 | inline GrResourceCache::ResourceAccess GrResourceCache::resourceAccess() { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 339 | return ResourceAccess(this); |
| 340 | } |
| 341 | |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 342 | #endif |