epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 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. |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 11 | #ifndef GrResourceCache_DEFINED |
| 12 | #define GrResourceCache_DEFINED |
| 13 | |
robertphillips@google.com | 5955202 | 2012-08-31 13:07:37 +0000 | [diff] [blame] | 14 | #include "GrConfig.h" |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 15 | #include "GrTypes.h" |
robertphillips | 7a037f4 | 2014-07-21 12:40:57 -0700 | [diff] [blame] | 16 | #include "SkTMultiMap.h" |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 17 | #include "GrBinHashKey.h" |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 18 | #include "SkMessageBus.h" |
bsalomon@google.com | 42619d8 | 2012-12-03 14:54:59 +0000 | [diff] [blame] | 19 | #include "SkTInternalLList.h" |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 20 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 21 | class GrGpuResource; |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 22 | class GrResourceCache; |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 23 | class GrResourceCacheEntry; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 24 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 25 | class GrResourceKey { |
| 26 | public: |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 27 | static GrCacheID::Domain ScratchDomain() { |
| 28 | static const GrCacheID::Domain gDomain = GrCacheID::GenerateDomain(); |
| 29 | return gDomain; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 30 | } |
| 31 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 32 | /** Uniquely identifies the GrGpuResource subclass in the key to avoid collisions |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 33 | across resource types. */ |
| 34 | typedef uint8_t ResourceType; |
| 35 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 36 | /** Flags set by the GrGpuResource subclass. */ |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 37 | typedef uint8_t ResourceFlags; |
| 38 | |
| 39 | /** Generate a unique ResourceType */ |
| 40 | static ResourceType GenerateResourceType(); |
| 41 | |
| 42 | /** Creates a key for resource */ |
| 43 | GrResourceKey(const GrCacheID& id, ResourceType type, ResourceFlags flags) { |
| 44 | this->init(id.getDomain(), id.getKey(), type, flags); |
| 45 | }; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 46 | |
| 47 | GrResourceKey(const GrResourceKey& src) { |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 48 | fKey = src.fKey; |
| 49 | } |
| 50 | |
| 51 | GrResourceKey() { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 52 | fKey.reset(); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void reset(const GrCacheID& id, ResourceType type, ResourceFlags flags) { |
| 56 | this->init(id.getDomain(), id.getKey(), type, flags); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 57 | } |
| 58 | |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 59 | uint32_t getHash() const { |
| 60 | return fKey.getHash(); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 61 | } |
| 62 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 63 | bool isScratch() const { |
| 64 | return ScratchDomain() == |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 65 | *reinterpret_cast<const GrCacheID::Domain*>(fKey.getData() + |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 66 | kCacheIDDomainOffset); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 67 | } |
| 68 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 69 | ResourceType getResourceType() const { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 70 | return *reinterpret_cast<const ResourceType*>(fKey.getData() + |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 71 | kResourceTypeOffset); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 72 | } |
| 73 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 74 | ResourceFlags getResourceFlags() const { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 75 | return *reinterpret_cast<const ResourceFlags*>(fKey.getData() + |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 76 | kResourceFlagsOffset); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 77 | } |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 78 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 79 | bool operator==(const GrResourceKey& other) const { return fKey == other.fKey; } |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 80 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 81 | private: |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 82 | enum { |
| 83 | kCacheIDKeyOffset = 0, |
| 84 | kCacheIDDomainOffset = kCacheIDKeyOffset + sizeof(GrCacheID::Key), |
| 85 | kResourceTypeOffset = kCacheIDDomainOffset + sizeof(GrCacheID::Domain), |
| 86 | kResourceFlagsOffset = kResourceTypeOffset + sizeof(ResourceType), |
| 87 | kPadOffset = kResourceFlagsOffset + sizeof(ResourceFlags), |
| 88 | kKeySize = SkAlign4(kPadOffset), |
| 89 | kPadSize = kKeySize - kPadOffset |
| 90 | }; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 91 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 92 | void init(const GrCacheID::Domain domain, |
| 93 | const GrCacheID::Key& key, |
| 94 | ResourceType type, |
| 95 | ResourceFlags flags) { |
| 96 | union { |
| 97 | uint8_t fKey8[kKeySize]; |
| 98 | uint32_t fKey32[kKeySize / 4]; |
| 99 | } keyData; |
skia.committer@gmail.com | 2859eb7 | 2012-12-21 02:01:28 +0000 | [diff] [blame] | 100 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 101 | uint8_t* k = keyData.fKey8; |
| 102 | memcpy(k + kCacheIDKeyOffset, key.fData8, sizeof(GrCacheID::Key)); |
| 103 | memcpy(k + kCacheIDDomainOffset, &domain, sizeof(GrCacheID::Domain)); |
| 104 | memcpy(k + kResourceTypeOffset, &type, sizeof(ResourceType)); |
| 105 | memcpy(k + kResourceFlagsOffset, &flags, sizeof(ResourceFlags)); |
| 106 | memset(k + kPadOffset, 0, kPadSize); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 107 | fKey.setKeyData(keyData.fKey32); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 108 | } |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 109 | GrBinHashKey<kKeySize> fKey; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 110 | }; |
| 111 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 112 | // The cache listens for these messages to purge junk resources proactively. |
| 113 | struct GrResourceInvalidatedMessage { |
| 114 | GrResourceKey key; |
| 115 | }; |
| 116 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 117 | /////////////////////////////////////////////////////////////////////////////// |
| 118 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 119 | class GrResourceCacheEntry { |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 120 | public: |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 121 | GrGpuResource* resource() const { return fResource; } |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 122 | const GrResourceKey& key() const { return fKey; } |
| 123 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 124 | static const GrResourceKey& GetKey(const GrResourceCacheEntry& e) { return e.key(); } |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 125 | static uint32_t Hash(const GrResourceKey& key) { return key.getHash(); } |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 126 | #ifdef SK_DEBUG |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 127 | void validate() const; |
| 128 | #else |
| 129 | void validate() const {} |
| 130 | #endif |
| 131 | |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 132 | /** |
| 133 | * Update the cached size for this entry and inform the resource cache that |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 134 | * it has changed. Usually invoked from GrGpuResource::didChangeGpuMemorySize, |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 135 | * not directly from here. |
| 136 | */ |
| 137 | void didChangeResourceSize(); |
| 138 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 139 | private: |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 140 | GrResourceCacheEntry(GrResourceCache* resourceCache, |
| 141 | const GrResourceKey& key, |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 142 | GrGpuResource* resource); |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 143 | ~GrResourceCacheEntry(); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 144 | |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 145 | GrResourceCache* fResourceCache; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 146 | GrResourceKey fKey; |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 147 | GrGpuResource* fResource; |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 148 | size_t fCachedSize; |
| 149 | bool fIsExclusive; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 150 | |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 151 | // Linked list for the LRU ordering. |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 152 | SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrResourceCacheEntry); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 153 | |
| 154 | friend class GrResourceCache; |
bsalomon | 02e36f2 | 2014-08-22 12:01:46 -0700 | [diff] [blame^] | 155 | friend class GrContext; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | /////////////////////////////////////////////////////////////////////////////// |
| 159 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 160 | /** |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 161 | * Cache of GrGpuResource objects. |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 162 | * |
| 163 | * These have a corresponding GrResourceKey, built from 128bits identifying the |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 164 | * resource. Multiple resources can map to same GrResourceKey. |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 165 | * |
| 166 | * The cache stores the entries in a double-linked list, which is its LRU. |
| 167 | * When an entry is "locked" (i.e. given to the caller), it is moved to the |
| 168 | * head of the list. If/when we must purge some of the entries, we walk the |
| 169 | * list backwards from the tail, since those are the least recently used. |
| 170 | * |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 171 | * For fast searches, we maintain a hash map based on the GrResourceKey. |
bsalomon@google.com | 76202b8 | 2013-05-10 19:08:22 +0000 | [diff] [blame] | 172 | * |
| 173 | * It is a goal to make the GrResourceCache the central repository and bookkeeper |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 174 | * of all resources. It should replace the linked list of GrGpuResources that |
bsalomon@google.com | 76202b8 | 2013-05-10 19:08:22 +0000 | [diff] [blame] | 175 | * GrGpu uses to call abandon/release. |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 176 | */ |
| 177 | class GrResourceCache { |
| 178 | public: |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 179 | GrResourceCache(int maxCount, size_t maxBytes); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 180 | ~GrResourceCache(); |
| 181 | |
| 182 | /** |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 183 | * Return the current resource cache limits. |
| 184 | * |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 185 | * @param maxResource If non-null, returns maximum number of resources |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 186 | * that can be held in the cache. |
| 187 | * @param maxBytes If non-null, returns maximum number of bytes of |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 188 | * gpu memory that can be held in the cache. |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 189 | */ |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 190 | void getLimits(int* maxResources, size_t* maxBytes) const; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 191 | |
| 192 | /** |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 193 | * Specify the resource cache limits. If the current cache exceeds either |
| 194 | * of these, it will be purged (LRU) to keep the cache within these limits. |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 195 | * |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 196 | * @param maxResources The maximum number of resources that can be held in |
| 197 | * the cache. |
| 198 | * @param maxBytes The maximum number of bytes of resource memory that |
| 199 | * can be held in the cache. |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 200 | */ |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 201 | void setLimits(int maxResources, size_t maxResourceBytes); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 202 | |
| 203 | /** |
commit-bot@chromium.org | cae27fe | 2013-07-10 10:14:35 +0000 | [diff] [blame] | 204 | * The callback function used by the cache when it is still over budget |
skia.committer@gmail.com | de2e4e8 | 2013-07-11 07:01:01 +0000 | [diff] [blame] | 205 | * after a purge. The passed in 'data' is the same 'data' handed to |
commit-bot@chromium.org | cae27fe | 2013-07-10 10:14:35 +0000 | [diff] [blame] | 206 | * setOverbudgetCallback. The callback returns true if some resources |
| 207 | * have been freed. |
| 208 | */ |
| 209 | typedef bool (*PFOverbudgetCB)(void* data); |
| 210 | |
| 211 | /** |
| 212 | * Set the callback the cache should use when it is still over budget |
skia.committer@gmail.com | de2e4e8 | 2013-07-11 07:01:01 +0000 | [diff] [blame] | 213 | * after a purge. The 'data' provided here will be passed back to the |
skia.committer@gmail.com | 1f3c738 | 2013-07-20 07:00:58 +0000 | [diff] [blame] | 214 | * callback. Note that the cache will attempt to purge any resources newly |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 215 | * freed by the callback. |
commit-bot@chromium.org | cae27fe | 2013-07-10 10:14:35 +0000 | [diff] [blame] | 216 | */ |
| 217 | void setOverbudgetCallback(PFOverbudgetCB overbudgetCB, void* data) { |
| 218 | fOverbudgetCB = overbudgetCB; |
| 219 | fOverbudgetData = data; |
| 220 | } |
| 221 | |
| 222 | /** |
twiz@google.com | 05e7024 | 2012-01-27 19:12:00 +0000 | [diff] [blame] | 223 | * Returns the number of bytes consumed by cached resources. |
| 224 | */ |
| 225 | size_t getCachedResourceBytes() const { return fEntryBytes; } |
| 226 | |
commit-bot@chromium.org | d8a57af | 2014-03-19 21:19:16 +0000 | [diff] [blame] | 227 | /** |
| 228 | * Returns the number of cached resources. |
| 229 | */ |
| 230 | int getCachedResourceCount() const { return fEntryCount; } |
| 231 | |
robertphillips@google.com | 209a114 | 2012-10-31 12:25:21 +0000 | [diff] [blame] | 232 | // For a found or added resource to be completely exclusive to the caller |
| 233 | // both the kNoOtherOwners and kHide flags need to be specified |
| 234 | enum OwnershipFlags { |
| 235 | kNoOtherOwners_OwnershipFlag = 0x1, // found/added resource has no other owners |
| 236 | kHide_OwnershipFlag = 0x2 // found/added resource is hidden from future 'find's |
| 237 | }; |
| 238 | |
twiz@google.com | 05e7024 | 2012-01-27 19:12:00 +0000 | [diff] [blame] | 239 | /** |
robertphillips@google.com | a9b0623 | 2012-08-30 11:06:31 +0000 | [diff] [blame] | 240 | * Search for an entry with the same Key. If found, return it. |
| 241 | * If not found, return null. |
skia.committer@gmail.com | f3dc199 | 2012-11-01 02:01:27 +0000 | [diff] [blame] | 242 | * If ownershipFlags includes kNoOtherOwners and a resource is returned |
robertphillips@google.com | 209a114 | 2012-10-31 12:25:21 +0000 | [diff] [blame] | 243 | * then that resource has no other refs to it. |
| 244 | * If ownershipFlags includes kHide and a resource is returned then that |
| 245 | * resource will not be returned from future 'find' calls until it is |
| 246 | * 'freed' (and recycled) or makeNonExclusive is called. |
| 247 | * For a resource to be completely exclusive to a caller both kNoOtherOwners |
| 248 | * and kHide must be specified. |
robertphillips@google.com | a9b0623 | 2012-08-30 11:06:31 +0000 | [diff] [blame] | 249 | */ |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 250 | GrGpuResource* find(const GrResourceKey& key, |
| 251 | uint32_t ownershipFlags = 0); |
robertphillips@google.com | a9b0623 | 2012-08-30 11:06:31 +0000 | [diff] [blame] | 252 | |
| 253 | /** |
skia.committer@gmail.com | f3dc199 | 2012-11-01 02:01:27 +0000 | [diff] [blame] | 254 | * Add the new resource to the cache (by creating a new cache entry based |
| 255 | * on the provided key and resource). |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 256 | * |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 257 | * Ownership of the resource is transferred to the resource cache, |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 258 | * which will unref() it when it is purged or deleted. |
robertphillips@google.com | 209a114 | 2012-10-31 12:25:21 +0000 | [diff] [blame] | 259 | * |
| 260 | * If ownershipFlags includes kHide, subsequent calls to 'find' will not |
skia.committer@gmail.com | f3dc199 | 2012-11-01 02:01:27 +0000 | [diff] [blame] | 261 | * return 'resource' until it is 'freed' (and recycled) or makeNonExclusive |
robertphillips@google.com | 209a114 | 2012-10-31 12:25:21 +0000 | [diff] [blame] | 262 | * is called. |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 263 | */ |
skia.committer@gmail.com | f3dc199 | 2012-11-01 02:01:27 +0000 | [diff] [blame] | 264 | void addResource(const GrResourceKey& key, |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 265 | GrGpuResource* resource, |
robertphillips@google.com | 209a114 | 2012-10-31 12:25:21 +0000 | [diff] [blame] | 266 | uint32_t ownershipFlags = 0); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 267 | |
| 268 | /** |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 269 | * Determines if the cache contains an entry matching a key. If a matching |
| 270 | * entry exists but was detached then it will not be found. |
| 271 | */ |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 272 | bool hasKey(const GrResourceKey& key) const { return NULL != fCache.find(key); } |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 273 | |
| 274 | /** |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 275 | * Hide 'entry' so that future searches will not find it. Such |
| 276 | * hidden entries will not be purged. The entry still counts against |
| 277 | * the cache's budget and should be made non-exclusive when exclusive access |
| 278 | * is no longer needed. |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 279 | */ |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 280 | void makeExclusive(GrResourceCacheEntry* entry); |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 281 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 282 | /** |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 283 | * Restore 'entry' so that it can be found by future searches. 'entry' |
| 284 | * will also be purgeable (provided its lock count is now 0.) |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 285 | */ |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 286 | void makeNonExclusive(GrResourceCacheEntry* entry); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 287 | |
| 288 | /** |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 289 | * Notify the cache that the size of a resource has changed. |
| 290 | */ |
| 291 | void didIncreaseResourceSize(const GrResourceCacheEntry*, size_t amountInc); |
| 292 | void didDecreaseResourceSize(const GrResourceCacheEntry*, size_t amountDec); |
| 293 | |
| 294 | /** |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 295 | * Remove a resource from the cache and delete it! |
| 296 | */ |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 297 | void deleteResource(GrResourceCacheEntry* entry); |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 298 | |
| 299 | /** |
bsalomon@google.com | a292112 | 2012-08-28 12:34:17 +0000 | [diff] [blame] | 300 | * Removes every resource in the cache that isn't locked. |
| 301 | */ |
| 302 | void purgeAllUnlocked(); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 303 | |
robertphillips@google.com | 50a035d | 2012-09-07 19:44:33 +0000 | [diff] [blame] | 304 | /** |
| 305 | * Allow cache to purge unused resources to obey resource limitations |
robertphillips@google.com | 9fbcad0 | 2012-09-09 14:44:15 +0000 | [diff] [blame] | 306 | * Note: this entry point will be hidden (again) once totally ref-driven |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 307 | * cache maintenance is implemented. Note that the overbudget callback |
| 308 | * will be called if the initial purge doesn't get the cache under |
| 309 | * its budget. |
robertphillips@google.com | 41d2532 | 2013-07-18 17:12:57 +0000 | [diff] [blame] | 310 | * |
| 311 | * extraCount and extraBytes are added to the current resource allocation |
| 312 | * to make sure enough room is available for future additions (e.g, |
| 313 | * 10MB across 10 textures is about to be added). |
robertphillips@google.com | 50a035d | 2012-09-07 19:44:33 +0000 | [diff] [blame] | 314 | */ |
robertphillips@google.com | 41d2532 | 2013-07-18 17:12:57 +0000 | [diff] [blame] | 315 | void purgeAsNeeded(int extraCount = 0, size_t extraBytes = 0); |
robertphillips@google.com | 50a035d | 2012-09-07 19:44:33 +0000 | [diff] [blame] | 316 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 317 | #ifdef SK_DEBUG |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 318 | void validate() const; |
| 319 | #else |
| 320 | void validate() const {} |
| 321 | #endif |
| 322 | |
robertphillips@google.com | 5955202 | 2012-08-31 13:07:37 +0000 | [diff] [blame] | 323 | #if GR_CACHE_STATS |
robertphillips@google.com | 9fbcad0 | 2012-09-09 14:44:15 +0000 | [diff] [blame] | 324 | void printStats(); |
robertphillips@google.com | 5955202 | 2012-08-31 13:07:37 +0000 | [diff] [blame] | 325 | #endif |
| 326 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 327 | private: |
robertphillips@google.com | 209a114 | 2012-10-31 12:25:21 +0000 | [diff] [blame] | 328 | enum BudgetBehaviors { |
| 329 | kAccountFor_BudgetBehavior, |
| 330 | kIgnore_BudgetBehavior |
| 331 | }; |
| 332 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 333 | void internalDetach(GrResourceCacheEntry*, BudgetBehaviors behavior = kAccountFor_BudgetBehavior); |
| 334 | void attachToHead(GrResourceCacheEntry*, BudgetBehaviors behavior = kAccountFor_BudgetBehavior); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 335 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 336 | void removeInvalidResource(GrResourceCacheEntry* entry); |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 337 | |
robertphillips | 7a037f4 | 2014-07-21 12:40:57 -0700 | [diff] [blame] | 338 | SkTMultiMap<GrResourceCacheEntry, GrResourceKey> fCache; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 339 | |
bsalomon@google.com | 42619d8 | 2012-12-03 14:54:59 +0000 | [diff] [blame] | 340 | // We're an internal doubly linked list |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 341 | typedef SkTInternalLList<GrResourceCacheEntry> EntryList; |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 342 | EntryList fList; |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 343 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 344 | #ifdef SK_DEBUG |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 345 | // These objects cannot be returned by a search |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 346 | EntryList fExclusiveList; |
robertphillips@google.com | 521eaf8 | 2012-08-22 11:03:19 +0000 | [diff] [blame] | 347 | #endif |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 348 | |
| 349 | // our budget, used in purgeAsNeeded() |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 350 | int fMaxCount; |
| 351 | size_t fMaxBytes; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 352 | |
| 353 | // our current stats, related to our budget |
robertphillips@google.com | 5955202 | 2012-08-31 13:07:37 +0000 | [diff] [blame] | 354 | #if GR_CACHE_STATS |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 355 | int fHighWaterEntryCount; |
| 356 | size_t fHighWaterEntryBytes; |
| 357 | int fHighWaterClientDetachedCount; |
| 358 | size_t fHighWaterClientDetachedBytes; |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame] | 359 | #endif |
| 360 | |
robertphillips@google.com | e4eaea2 | 2013-07-19 16:51:46 +0000 | [diff] [blame] | 361 | int fEntryCount; |
| 362 | size_t fEntryBytes; |
| 363 | int fClientDetachedCount; |
| 364 | size_t fClientDetachedBytes; |
robertphillips@google.com | 386319e | 2012-06-27 14:59:18 +0000 | [diff] [blame] | 365 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 366 | // prevents recursive purging |
commit-bot@chromium.org | cae27fe | 2013-07-10 10:14:35 +0000 | [diff] [blame] | 367 | bool fPurging; |
| 368 | |
| 369 | PFOverbudgetCB fOverbudgetCB; |
| 370 | void* fOverbudgetData; |
| 371 | |
robertphillips@google.com | 41d2532 | 2013-07-18 17:12:57 +0000 | [diff] [blame] | 372 | void internalPurge(int extraCount, size_t extraBytes); |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 373 | |
commit-bot@chromium.org | 50a3043 | 2013-10-24 17:44:27 +0000 | [diff] [blame] | 374 | // Listen for messages that a resource has been invalidated and purge cached junk proactively. |
| 375 | SkMessageBus<GrResourceInvalidatedMessage>::Inbox fInvalidationInbox; |
| 376 | void purgeInvalidated(); |
| 377 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 378 | #ifdef SK_DEBUG |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 379 | static size_t countBytes(const SkTInternalLList<GrResourceCacheEntry>& list); |
robertphillips@google.com | 2ea0a23 | 2012-08-23 11:13:48 +0000 | [diff] [blame] | 380 | #endif |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 381 | }; |
| 382 | |
| 383 | /////////////////////////////////////////////////////////////////////////////// |
| 384 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 385 | #ifdef SK_DEBUG |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 386 | class GrAutoResourceCacheValidate { |
| 387 | public: |
| 388 | GrAutoResourceCacheValidate(GrResourceCache* cache) : fCache(cache) { |
| 389 | cache->validate(); |
| 390 | } |
| 391 | ~GrAutoResourceCacheValidate() { |
| 392 | fCache->validate(); |
| 393 | } |
| 394 | private: |
| 395 | GrResourceCache* fCache; |
| 396 | }; |
| 397 | #else |
| 398 | class GrAutoResourceCacheValidate { |
| 399 | public: |
| 400 | GrAutoResourceCacheValidate(GrResourceCache*) {} |
| 401 | }; |
| 402 | #endif |
| 403 | |
| 404 | #endif |