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