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 |
| 25 | * resource instances with the same properties (e.g. multipass rendering that ping pongs |
| 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 | /** |
| 50 | * Returns the number of cached resources. |
| 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 | /** |
| 55 | * Returns the number of bytes consumed by cached resources. |
| 56 | */ |
| 57 | size_t getResourceBytes() const { return fBytes; } |
| 58 | |
| 59 | /** |
| 60 | * Returns the cached resources count budget. |
| 61 | */ |
| 62 | int getMaxResourceCount() const { return fMaxCount; } |
| 63 | |
| 64 | /** |
| 65 | * Returns the number of bytes consumed by cached resources. |
| 66 | */ |
| 67 | size_t getMaxResourceBytes() const { return fMaxBytes; } |
| 68 | |
| 69 | /** |
| 70 | * Abandons the backend API resources owned by all GrGpuResource objects and removes them from |
| 71 | * the cache. |
| 72 | */ |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 73 | void abandonAll(); |
| 74 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 75 | /** |
| 76 | * Releases the backend API resources owned by all GrGpuResource objects and removes them from |
| 77 | * the cache. |
| 78 | */ |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 79 | void releaseAll(); |
| 80 | |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 81 | enum { |
| 82 | /** Preferentially returns scratch resources with no pending IO. */ |
| 83 | kPreferNoPendingIO_ScratchFlag = 0x1, |
| 84 | /** Will not return any resources that match but have pending IO. */ |
| 85 | kRequireNoPendingIO_ScratchFlag = 0x2, |
| 86 | }; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * Find a resource that matches a scratch key. |
| 90 | */ |
bsalomon | 000f829 | 2014-10-15 19:04:14 -0700 | [diff] [blame] | 91 | GrGpuResource* findAndRefScratchResource(const GrResourceKey& scratchKey, uint32_t flags = 0); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 92 | |
| 93 | #ifdef SK_DEBUG |
| 94 | // This is not particularly fast and only used for validation, so debug only. |
| 95 | int countScratchEntriesForKey(const GrResourceKey& scratchKey) const { |
| 96 | SkASSERT(scratchKey.isScratch()); |
| 97 | return fScratchMap.countForKey(scratchKey); |
| 98 | } |
| 99 | #endif |
| 100 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 101 | /** |
| 102 | * Find a resource that matches a content key. |
| 103 | */ |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 104 | GrGpuResource* findAndRefContentResource(const GrResourceKey& contentKey) { |
| 105 | SkASSERT(!contentKey.isScratch()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 106 | GrGpuResource* resource = fContentHash.find(contentKey); |
| 107 | if (resource) { |
| 108 | resource->ref(); |
| 109 | this->makeResourceMRU(resource); |
| 110 | } |
| 111 | return resource; |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 112 | } |
| 113 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 114 | /** |
| 115 | * Query whether a content key exists in the cache. |
| 116 | */ |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 117 | bool hasContentKey(const GrResourceKey& contentKey) const { |
| 118 | SkASSERT(!contentKey.isScratch()); |
| 119 | return SkToBool(fContentHash.find(contentKey)); |
| 120 | } |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 121 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 122 | /** Purges all resources that don't have external owners. */ |
| 123 | void purgeAllUnlocked(); |
| 124 | |
| 125 | /** |
| 126 | * The callback function used by the cache when it is still over budget after a purge. The |
| 127 | * passed in 'data' is the same 'data' handed to setOverbudgetCallback. |
| 128 | */ |
| 129 | typedef void (*PFOverBudgetCB)(void* data); |
| 130 | |
| 131 | /** |
| 132 | * Set the callback the cache should use when it is still over budget after a purge. The 'data' |
| 133 | * provided here will be passed back to the callback. Note that the cache will attempt to purge |
| 134 | * any resources newly freed by the callback. |
| 135 | */ |
| 136 | void setOverBudgetCallback(PFOverBudgetCB overBudgetCB, void* data) { |
| 137 | fOverBudgetCB = overBudgetCB; |
| 138 | fOverBudgetData = data; |
| 139 | } |
| 140 | |
| 141 | #if GR_GPU_STATS |
| 142 | void printStats() const; |
| 143 | #endif |
| 144 | |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 145 | private: |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 146 | /////////////////////////////////////////////////////////////////////////// |
| 147 | /// @name Methods accessible via ResourceAccess |
| 148 | //// |
| 149 | void insertResource(GrGpuResource*); |
| 150 | void removeResource(GrGpuResource*); |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame^] | 151 | void notifyPurgable(GrGpuResource*); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 152 | void didChangeGpuMemorySize(const GrGpuResource*, size_t oldSize); |
| 153 | bool didSetContentKey(GrGpuResource*); |
| 154 | void makeResourceMRU(GrGpuResource*); |
| 155 | /// @} |
| 156 | |
| 157 | void purgeAsNeeded() { |
| 158 | if (fPurging || (fCount <= fMaxCount && fBytes < fMaxBytes)) { |
| 159 | return; |
| 160 | } |
| 161 | this->internalPurgeAsNeeded(); |
| 162 | } |
| 163 | |
| 164 | void internalPurgeAsNeeded(); |
| 165 | |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 166 | #ifdef SK_DEBUG |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 167 | bool isInCache(const GrGpuResource* r) const { return fResources.isInList(r); } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 168 | void validate() const; |
| 169 | #else |
| 170 | void validate() const {} |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 171 | #endif |
| 172 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 173 | class AutoValidate; |
| 174 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 175 | class AvailableForScratchUse; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 176 | |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 177 | struct ScratchMapTraits { |
| 178 | static const GrResourceKey& GetKey(const GrGpuResource& r) { |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 179 | return r.cacheAccess().getScratchKey(); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static uint32_t Hash(const GrResourceKey& key) { return key.getHash(); } |
| 183 | }; |
| 184 | typedef SkTMultiMap<GrGpuResource, GrResourceKey, ScratchMapTraits> ScratchMap; |
| 185 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 186 | struct ContentHashTraits { |
| 187 | static const GrResourceKey& GetKey(const GrGpuResource& r) { |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 188 | return *r.cacheAccess().getContentKey(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static uint32_t Hash(const GrResourceKey& key) { return key.getHash(); } |
| 192 | }; |
| 193 | typedef SkTDynamicHash<GrGpuResource, GrResourceKey, ContentHashTraits> ContentHash; |
| 194 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 195 | typedef SkTInternalLList<GrGpuResource> ResourceList; |
| 196 | |
| 197 | ResourceList fResources; |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 198 | // This map holds all resources that can be used as scratch resources. |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 199 | ScratchMap fScratchMap; |
| 200 | // This holds all resources that have content keys. |
| 201 | ContentHash fContentHash; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 202 | |
| 203 | // our budget, used in purgeAsNeeded() |
| 204 | int fMaxCount; |
| 205 | size_t fMaxBytes; |
| 206 | |
| 207 | #if GR_CACHE_STATS |
| 208 | int fHighWaterCount; |
| 209 | size_t fHighWaterBytes; |
| 210 | #endif |
| 211 | |
| 212 | // our current stats, related to our budget |
| 213 | int fCount; |
| 214 | size_t fBytes; |
| 215 | |
| 216 | // prevents recursive purging |
| 217 | bool fPurging; |
| 218 | bool fNewlyPurgableResourceWhilePurging; |
| 219 | |
| 220 | PFOverBudgetCB fOverBudgetCB; |
| 221 | void* fOverBudgetData; |
| 222 | |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 223 | }; |
| 224 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 225 | class GrResourceCache2::ResourceAccess { |
| 226 | private: |
| 227 | ResourceAccess(GrResourceCache2* cache) : fCache(cache) { } |
| 228 | ResourceAccess(const ResourceAccess& that) : fCache(that.fCache) { } |
| 229 | ResourceAccess& operator=(const ResourceAccess&); // unimpl |
| 230 | |
| 231 | /** |
| 232 | * Insert a resource into the cache. |
| 233 | */ |
| 234 | void insertResource(GrGpuResource* resource) { fCache->insertResource(resource); } |
| 235 | |
| 236 | /** |
| 237 | * Removes a resource from the cache. |
| 238 | */ |
| 239 | void removeResource(GrGpuResource* resource) { fCache->removeResource(resource); } |
| 240 | |
| 241 | /** |
| 242 | * Called by GrGpuResources when they detects that they are newly purgable. |
| 243 | */ |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame^] | 244 | void notifyPurgable(GrGpuResource* resource) { fCache->notifyPurgable(resource); } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 245 | |
| 246 | /** |
| 247 | * Called by GrGpuResources when their sizes change. |
| 248 | */ |
| 249 | void didChangeGpuMemorySize(const GrGpuResource* resource, size_t oldSize) { |
| 250 | fCache->didChangeGpuMemorySize(resource, oldSize); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Called by GrGpuResources when their content keys change. |
| 255 | * |
| 256 | * This currently returns a bool and fails when an existing resource has a key that collides |
| 257 | * with the new content key. In the future it will null out the content key for the existing |
| 258 | * resource. The failure is a temporary measure taken because duties are split between two |
| 259 | * cache objects currently. |
| 260 | */ |
| 261 | bool didSetContentKey(GrGpuResource* resource) { return fCache->didSetContentKey(resource); } |
| 262 | |
| 263 | // No taking addresses of this type. |
| 264 | const ResourceAccess* operator&() const; |
| 265 | ResourceAccess* operator&(); |
| 266 | |
| 267 | GrResourceCache2* fCache; |
| 268 | |
| 269 | friend class GrGpuResource; // To access all the proxy inline methods. |
| 270 | friend class GrResourceCache2; // To create this type. |
| 271 | }; |
| 272 | |
| 273 | inline GrResourceCache2::ResourceAccess GrResourceCache2::resourceAccess() { |
| 274 | return ResourceAccess(this); |
| 275 | } |
| 276 | |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 277 | #endif |