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 | |
| 9 | #ifndef GrResourceCache2_DEFINED |
| 10 | #define GrResourceCache2_DEFINED |
| 11 | |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 12 | #include "GrGpuResource.h" |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 13 | #include "GrGpuResourceCacheAccess.h" |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 14 | #include "GrResourceKey.h" |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 15 | #include "SkRefCnt.h" |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 16 | #include "SkTInternalLList.h" |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 17 | #include "SkTMultiMap.h" |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 18 | |
| 19 | /** |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 20 | * Manages the lifetime of all GrGpuResource instances. |
| 21 | * |
| 22 | * Resources may have optionally have two types of keys: |
| 23 | * 1) A scratch key. This is for resources whose allocations are cached but not their contents. |
| 24 | * 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] | 25 | * resource instances with the same properties (e.g. multipass rendering that ping-pongs |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 26 | * between two temporary surfaces. The scratch key is set at resource creation time and |
| 27 | * should never change. Resources need not have a scratch key. |
| 28 | * 2) A content key. This key represents the contents of the resource rather than just its |
| 29 | * allocation properties. They may not collide. The content key can be set after resource |
| 30 | * creation. Currently it may only be set once and cannot be cleared. This restriction will |
| 31 | * be removed. |
| 32 | * If a resource has neither key type then it will be deleted as soon as the last reference to it |
| 33 | * is dropped. If a key has both keys the content key takes precedence. |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 34 | */ |
| 35 | class GrResourceCache2 { |
| 36 | public: |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 37 | GrResourceCache2(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 38 | ~GrResourceCache2(); |
| 39 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 40 | /** Used to access functionality needed by GrGpuResource for lifetime management. */ |
| 41 | class ResourceAccess; |
| 42 | ResourceAccess resourceAccess(); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 43 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 44 | /** |
| 45 | * Sets the cache limits in terms of number of resources and max gpu memory byte size. |
| 46 | */ |
| 47 | void setLimits(int count, size_t bytes); |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 48 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 49 | /** |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 50 | * Returns the number of resources. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 51 | */ |
| 52 | int getResourceCount() const { return fCount; } |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 53 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 54 | /** |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 55 | * Returns the number of resources that count against the budget. |
| 56 | */ |
| 57 | int getBudgetedResourceCount() const { return fBudgetedCount; } |
| 58 | |
| 59 | /** |
| 60 | * Returns the number of bytes consumed by resources. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 61 | */ |
| 62 | size_t getResourceBytes() const { return fBytes; } |
| 63 | |
| 64 | /** |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 65 | * Returns the number of bytes consumed by budgeted resources. |
| 66 | */ |
| 67 | size_t getBudgetedResourceBytes() const { return fBudgetedBytes; } |
| 68 | |
| 69 | /** |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 70 | * Returns the cached resources count budget. |
| 71 | */ |
| 72 | int getMaxResourceCount() const { return fMaxCount; } |
| 73 | |
| 74 | /** |
| 75 | * Returns the number of bytes consumed by cached resources. |
| 76 | */ |
| 77 | size_t getMaxResourceBytes() const { return fMaxBytes; } |
| 78 | |
| 79 | /** |
| 80 | * Abandons the backend API resources owned by all GrGpuResource objects and removes them from |
| 81 | * the cache. |
| 82 | */ |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 83 | void abandonAll(); |
| 84 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 85 | /** |
| 86 | * Releases the backend API resources owned by all GrGpuResource objects and removes them from |
| 87 | * the cache. |
| 88 | */ |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 89 | void releaseAll(); |
| 90 | |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 91 | enum { |
| 92 | /** Preferentially returns scratch resources with no pending IO. */ |
| 93 | kPreferNoPendingIO_ScratchFlag = 0x1, |
| 94 | /** Will not return any resources that match but have pending IO. */ |
| 95 | kRequireNoPendingIO_ScratchFlag = 0x2, |
| 96 | }; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 97 | |
| 98 | /** |
| 99 | * Find a resource that matches a scratch key. |
| 100 | */ |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame^] | 101 | GrGpuResource* findAndRefScratchResource(const GrScratchKey& scratchKey, uint32_t flags = 0); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 102 | |
| 103 | #ifdef SK_DEBUG |
| 104 | // This is not particularly fast and only used for validation, so debug only. |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame^] | 105 | int countScratchEntriesForKey(const GrScratchKey& scratchKey) const { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 106 | return fScratchMap.countForKey(scratchKey); |
| 107 | } |
| 108 | #endif |
| 109 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 110 | /** |
| 111 | * Find a resource that matches a content key. |
| 112 | */ |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 113 | GrGpuResource* findAndRefContentResource(const GrResourceKey& contentKey) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 114 | GrGpuResource* resource = fContentHash.find(contentKey); |
| 115 | if (resource) { |
| 116 | resource->ref(); |
| 117 | this->makeResourceMRU(resource); |
| 118 | } |
| 119 | return resource; |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 120 | } |
| 121 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 122 | /** |
| 123 | * Query whether a content key exists in the cache. |
| 124 | */ |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 125 | bool hasContentKey(const GrResourceKey& contentKey) const { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 126 | return SkToBool(fContentHash.find(contentKey)); |
| 127 | } |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 128 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 129 | /** Purges all resources that don't have external owners. */ |
| 130 | void purgeAllUnlocked(); |
| 131 | |
| 132 | /** |
| 133 | * The callback function used by the cache when it is still over budget after a purge. The |
| 134 | * passed in 'data' is the same 'data' handed to setOverbudgetCallback. |
| 135 | */ |
| 136 | typedef void (*PFOverBudgetCB)(void* data); |
| 137 | |
| 138 | /** |
| 139 | * Set the callback the cache should use when it is still over budget after a purge. The 'data' |
| 140 | * provided here will be passed back to the callback. Note that the cache will attempt to purge |
| 141 | * any resources newly freed by the callback. |
| 142 | */ |
| 143 | void setOverBudgetCallback(PFOverBudgetCB overBudgetCB, void* data) { |
| 144 | fOverBudgetCB = overBudgetCB; |
| 145 | fOverBudgetData = data; |
| 146 | } |
| 147 | |
| 148 | #if GR_GPU_STATS |
| 149 | void printStats() const; |
| 150 | #endif |
| 151 | |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 152 | private: |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 153 | /////////////////////////////////////////////////////////////////////////// |
| 154 | /// @name Methods accessible via ResourceAccess |
| 155 | //// |
| 156 | void insertResource(GrGpuResource*); |
| 157 | void removeResource(GrGpuResource*); |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 158 | void notifyPurgable(GrGpuResource*); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 159 | void didChangeGpuMemorySize(const GrGpuResource*, size_t oldSize); |
| 160 | bool didSetContentKey(GrGpuResource*); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 161 | void willRemoveScratchKey(const GrGpuResource*); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 162 | void didChangeBudgetStatus(GrGpuResource*); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 163 | void makeResourceMRU(GrGpuResource*); |
| 164 | /// @} |
| 165 | |
| 166 | void purgeAsNeeded() { |
bsalomon | 1dbd816 | 2014-11-26 06:06:02 -0800 | [diff] [blame] | 167 | if (fPurging || (fBudgetedCount <= fMaxCount && fBudgetedBytes <= fMaxBytes)) { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 168 | return; |
| 169 | } |
| 170 | this->internalPurgeAsNeeded(); |
| 171 | } |
| 172 | |
| 173 | void internalPurgeAsNeeded(); |
| 174 | |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 175 | #ifdef SK_DEBUG |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 176 | bool isInCache(const GrGpuResource* r) const { return fResources.isInList(r); } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 177 | void validate() const; |
| 178 | #else |
| 179 | void validate() const {} |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 180 | #endif |
| 181 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 182 | class AutoValidate; |
| 183 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 184 | class AvailableForScratchUse; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 185 | |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 186 | struct ScratchMapTraits { |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame^] | 187 | static const GrScratchKey& GetKey(const GrGpuResource& r) { |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 188 | return r.cacheAccess().getScratchKey(); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 189 | } |
| 190 | |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame^] | 191 | static uint32_t Hash(const GrScratchKey& key) { return key.hash(); } |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 192 | }; |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame^] | 193 | typedef SkTMultiMap<GrGpuResource, GrScratchKey, ScratchMapTraits> ScratchMap; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 194 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 195 | struct ContentHashTraits { |
| 196 | static const GrResourceKey& GetKey(const GrGpuResource& r) { |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 197 | return *r.cacheAccess().getContentKey(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | static uint32_t Hash(const GrResourceKey& key) { return key.getHash(); } |
| 201 | }; |
| 202 | typedef SkTDynamicHash<GrGpuResource, GrResourceKey, ContentHashTraits> ContentHash; |
| 203 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 204 | typedef SkTInternalLList<GrGpuResource> ResourceList; |
| 205 | |
| 206 | ResourceList fResources; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 207 | // This map holds all resources that can be used as scratch resources. |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 208 | ScratchMap fScratchMap; |
| 209 | // This holds all resources that have content keys. |
| 210 | ContentHash fContentHash; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 211 | |
| 212 | // our budget, used in purgeAsNeeded() |
| 213 | int fMaxCount; |
| 214 | size_t fMaxBytes; |
| 215 | |
| 216 | #if GR_CACHE_STATS |
| 217 | int fHighWaterCount; |
| 218 | size_t fHighWaterBytes; |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 219 | int fBudgetedHighWaterCount; |
| 220 | size_t fBudgetedHighWaterBytes; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 221 | #endif |
| 222 | |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 223 | // our current stats for all resources |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 224 | int fCount; |
| 225 | size_t fBytes; |
| 226 | |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 227 | // our current stats for resources that count against the budget |
| 228 | int fBudgetedCount; |
| 229 | size_t fBudgetedBytes; |
| 230 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 231 | // prevents recursive purging |
| 232 | bool fPurging; |
| 233 | bool fNewlyPurgableResourceWhilePurging; |
| 234 | |
| 235 | PFOverBudgetCB fOverBudgetCB; |
| 236 | void* fOverBudgetData; |
| 237 | |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 238 | }; |
| 239 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 240 | class GrResourceCache2::ResourceAccess { |
| 241 | private: |
| 242 | ResourceAccess(GrResourceCache2* cache) : fCache(cache) { } |
| 243 | ResourceAccess(const ResourceAccess& that) : fCache(that.fCache) { } |
| 244 | ResourceAccess& operator=(const ResourceAccess&); // unimpl |
| 245 | |
| 246 | /** |
| 247 | * Insert a resource into the cache. |
| 248 | */ |
| 249 | void insertResource(GrGpuResource* resource) { fCache->insertResource(resource); } |
| 250 | |
| 251 | /** |
| 252 | * Removes a resource from the cache. |
| 253 | */ |
| 254 | void removeResource(GrGpuResource* resource) { fCache->removeResource(resource); } |
| 255 | |
| 256 | /** |
| 257 | * Called by GrGpuResources when they detects that they are newly purgable. |
| 258 | */ |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 259 | void notifyPurgable(GrGpuResource* resource) { fCache->notifyPurgable(resource); } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 260 | |
| 261 | /** |
| 262 | * Called by GrGpuResources when their sizes change. |
| 263 | */ |
| 264 | void didChangeGpuMemorySize(const GrGpuResource* resource, size_t oldSize) { |
| 265 | fCache->didChangeGpuMemorySize(resource, oldSize); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Called by GrGpuResources when their content keys change. |
| 270 | * |
| 271 | * This currently returns a bool and fails when an existing resource has a key that collides |
| 272 | * with the new content key. In the future it will null out the content key for the existing |
| 273 | * resource. The failure is a temporary measure taken because duties are split between two |
| 274 | * cache objects currently. |
| 275 | */ |
| 276 | bool didSetContentKey(GrGpuResource* resource) { return fCache->didSetContentKey(resource); } |
| 277 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 278 | /** |
| 279 | * Called by GrGpuResources when the remove their scratch key. |
| 280 | */ |
| 281 | void willRemoveScratchKey(const GrGpuResource* resource) { |
| 282 | fCache->willRemoveScratchKey(resource); |
| 283 | } |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 284 | |
| 285 | /** |
| 286 | * Called by GrGpuResources when they change from budgeted to unbudgeted or vice versa. |
| 287 | */ |
| 288 | void didChangeBudgetStatus(GrGpuResource* resource) { fCache->didChangeBudgetStatus(resource); } |
| 289 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 290 | // No taking addresses of this type. |
| 291 | const ResourceAccess* operator&() const; |
| 292 | ResourceAccess* operator&(); |
| 293 | |
| 294 | GrResourceCache2* fCache; |
| 295 | |
| 296 | friend class GrGpuResource; // To access all the proxy inline methods. |
| 297 | friend class GrResourceCache2; // To create this type. |
| 298 | }; |
| 299 | |
| 300 | inline GrResourceCache2::ResourceAccess GrResourceCache2::resourceAccess() { |
| 301 | return ResourceAccess(this); |
| 302 | } |
| 303 | |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 304 | #endif |