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