blob: 1a81fe61d125e55d76fd1f560b8fdc1e72986a2a [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
bsalomon@google.com50398bf2011-07-26 20:45:30 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.com50398bf2011-07-26 20:45:30 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
bsalomon@google.com50398bf2011-07-26 20:45:30 +000011#ifndef GrResourceCache_DEFINED
12#define GrResourceCache_DEFINED
13
robertphillips@google.com59552022012-08-31 13:07:37 +000014#include "GrConfig.h"
bsalomon@google.com50398bf2011-07-26 20:45:30 +000015#include "GrTypes.h"
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +000016#include "GrTMultiMap.h"
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000017#include "GrBinHashKey.h"
commit-bot@chromium.org50a30432013-10-24 17:44:27 +000018#include "SkMessageBus.h"
bsalomon@google.com42619d82012-12-03 14:54:59 +000019#include "SkTInternalLList.h"
bsalomon@google.com50398bf2011-07-26 20:45:30 +000020
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000021class GrCacheable;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000022class GrResourceCache;
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000023class GrResourceCacheEntry;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000024
bsalomon@google.com50398bf2011-07-26 20:45:30 +000025class GrResourceKey {
26public:
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000027 static GrCacheID::Domain ScratchDomain() {
28 static const GrCacheID::Domain gDomain = GrCacheID::GenerateDomain();
29 return gDomain;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000030 }
31
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000032 /** Uniquely identifies the GrCacheable subclass in the key to avoid collisions
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000033 across resource types. */
34 typedef uint8_t ResourceType;
35
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000036 /** Flags set by the GrCacheable subclass. */
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000037 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.com50398bf2011-07-26 20:45:30 +000046
47 GrResourceKey(const GrResourceKey& src) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000048 fKey = src.fKey;
49 }
50
51 GrResourceKey() {
rmistry@google.comd6bab022013-12-02 13:50:38 +000052 fKey.reset();
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000053 }
54
55 void reset(const GrCacheID& id, ResourceType type, ResourceFlags flags) {
56 this->init(id.getDomain(), id.getKey(), type, flags);
bsalomon@google.com50398bf2011-07-26 20:45:30 +000057 }
58
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +000059 uint32_t getHash() const {
60 return fKey.getHash();
bsalomon@google.com50398bf2011-07-26 20:45:30 +000061 }
62
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000063 bool isScratch() const {
64 return ScratchDomain() ==
rmistry@google.comd6bab022013-12-02 13:50:38 +000065 *reinterpret_cast<const GrCacheID::Domain*>(fKey.getData() +
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000066 kCacheIDDomainOffset);
bsalomon@google.com50398bf2011-07-26 20:45:30 +000067 }
68
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000069 ResourceType getResourceType() const {
rmistry@google.comd6bab022013-12-02 13:50:38 +000070 return *reinterpret_cast<const ResourceType*>(fKey.getData() +
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000071 kResourceTypeOffset);
bsalomon@google.com50398bf2011-07-26 20:45:30 +000072 }
73
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000074 ResourceFlags getResourceFlags() const {
rmistry@google.comd6bab022013-12-02 13:50:38 +000075 return *reinterpret_cast<const ResourceFlags*>(fKey.getData() +
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000076 kResourceFlagsOffset);
bsalomon@google.com50398bf2011-07-26 20:45:30 +000077 }
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000078
rmistry@google.comd6bab022013-12-02 13:50:38 +000079 bool operator==(const GrResourceKey& other) const { return fKey == other.fKey; }
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000080
bsalomon@google.com50398bf2011-07-26 20:45:30 +000081private:
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000082 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.com50398bf2011-07-26 20:45:30 +000091
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000092 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.com2859eb72012-12-21 02:01:28 +0000100
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000101 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.comd6bab022013-12-02 13:50:38 +0000107 fKey.setKeyData(keyData.fKey32);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000108 }
rmistry@google.comd6bab022013-12-02 13:50:38 +0000109 GrBinHashKey<kKeySize> fKey;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000110};
111
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000112// The cache listens for these messages to purge junk resources proactively.
113struct GrResourceInvalidatedMessage {
114 GrResourceKey key;
115};
116
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000117///////////////////////////////////////////////////////////////////////////////
118
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000119class GrResourceCacheEntry {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000120public:
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000121 GrCacheable* resource() const { return fResource; }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000122 const GrResourceKey& key() const { return fKey; }
123
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000124 static const GrResourceKey& GetKey(const GrResourceCacheEntry& e) { return e.key(); }
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000125 static uint32_t Hash(const GrResourceKey& key) { return key.getHash(); }
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000126#ifdef SK_DEBUG
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000127 void validate() const;
128#else
129 void validate() const {}
130#endif
131
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000132 /**
133 * Update the cached size for this entry and inform the resource cache that
134 * it has changed. Usually invoked from GrCacheable::didChangeGpuMemorySize,
135 * not directly from here.
136 */
137 void didChangeResourceSize();
138
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000139private:
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000140 GrResourceCacheEntry(GrResourceCache* resourceCache,
141 const GrResourceKey& key,
142 GrCacheable* resource);
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000143 ~GrResourceCacheEntry();
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000144
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000145 GrResourceCache* fResourceCache;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000146 GrResourceKey fKey;
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000147 GrCacheable* fResource;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000148 size_t fCachedSize;
149 bool fIsExclusive;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000150
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000151 // Linked list for the LRU ordering.
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000152 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrResourceCacheEntry);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000153
154 friend class GrResourceCache;
155};
156
157///////////////////////////////////////////////////////////////////////////////
158
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000159/**
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000160 * Cache of GrCacheable objects.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000161 *
162 * These have a corresponding GrResourceKey, built from 128bits identifying the
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000163 * resource. Multiple resources can map to same GrResourceKey.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000164 *
165 * The cache stores the entries in a double-linked list, which is its LRU.
166 * When an entry is "locked" (i.e. given to the caller), it is moved to the
167 * head of the list. If/when we must purge some of the entries, we walk the
168 * list backwards from the tail, since those are the least recently used.
169 *
commit-bot@chromium.orgbd58feb2014-01-17 17:56:21 +0000170 * For fast searches, we maintain a hash map based on the GrResourceKey.
bsalomon@google.com76202b82013-05-10 19:08:22 +0000171 *
172 * It is a goal to make the GrResourceCache the central repository and bookkeeper
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000173 * of all resources. It should replace the linked list of GrGpuObjects that
bsalomon@google.com76202b82013-05-10 19:08:22 +0000174 * GrGpu uses to call abandon/release.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000175 */
176class GrResourceCache {
177public:
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000178 GrResourceCache(int maxCount, size_t maxBytes);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000179 ~GrResourceCache();
180
181 /**
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000182 * Return the current resource cache limits.
183 *
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000184 * @param maxResource If non-null, returns maximum number of resources
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000185 * that can be held in the cache.
186 * @param maxBytes If non-null, returns maximum number of bytes of
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000187 * gpu memory that can be held in the cache.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000188 */
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000189 void getLimits(int* maxResources, size_t* maxBytes) const;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000190
191 /**
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000192 * Specify the resource cache limits. If the current cache exceeds either
193 * of these, it will be purged (LRU) to keep the cache within these limits.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000194 *
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000195 * @param maxResources The maximum number of resources that can be held in
196 * the cache.
197 * @param maxBytes The maximum number of bytes of resource memory that
198 * can be held in the cache.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000199 */
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000200 void setLimits(int maxResources, size_t maxResourceBytes);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000201
202 /**
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000203 * The callback function used by the cache when it is still over budget
skia.committer@gmail.comde2e4e82013-07-11 07:01:01 +0000204 * after a purge. The passed in 'data' is the same 'data' handed to
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000205 * setOverbudgetCallback. The callback returns true if some resources
206 * have been freed.
207 */
208 typedef bool (*PFOverbudgetCB)(void* data);
209
210 /**
211 * Set the callback the cache should use when it is still over budget
skia.committer@gmail.comde2e4e82013-07-11 07:01:01 +0000212 * after a purge. The 'data' provided here will be passed back to the
skia.committer@gmail.com1f3c7382013-07-20 07:00:58 +0000213 * callback. Note that the cache will attempt to purge any resources newly
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000214 * freed by the callback.
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000215 */
216 void setOverbudgetCallback(PFOverbudgetCB overbudgetCB, void* data) {
217 fOverbudgetCB = overbudgetCB;
218 fOverbudgetData = data;
219 }
220
221 /**
twiz@google.com05e70242012-01-27 19:12:00 +0000222 * Returns the number of bytes consumed by cached resources.
223 */
224 size_t getCachedResourceBytes() const { return fEntryBytes; }
225
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000226 /**
227 * Returns the number of cached resources.
228 */
229 int getCachedResourceCount() const { return fEntryCount; }
230
robertphillips@google.com209a1142012-10-31 12:25:21 +0000231 // For a found or added resource to be completely exclusive to the caller
232 // both the kNoOtherOwners and kHide flags need to be specified
233 enum OwnershipFlags {
234 kNoOtherOwners_OwnershipFlag = 0x1, // found/added resource has no other owners
235 kHide_OwnershipFlag = 0x2 // found/added resource is hidden from future 'find's
236 };
237
twiz@google.com05e70242012-01-27 19:12:00 +0000238 /**
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000239 * Search for an entry with the same Key. If found, return it.
240 * If not found, return null.
skia.committer@gmail.comf3dc1992012-11-01 02:01:27 +0000241 * If ownershipFlags includes kNoOtherOwners and a resource is returned
robertphillips@google.com209a1142012-10-31 12:25:21 +0000242 * then that resource has no other refs to it.
243 * If ownershipFlags includes kHide and a resource is returned then that
244 * resource will not be returned from future 'find' calls until it is
245 * 'freed' (and recycled) or makeNonExclusive is called.
246 * For a resource to be completely exclusive to a caller both kNoOtherOwners
247 * and kHide must be specified.
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000248 */
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000249 GrCacheable* find(const GrResourceKey& key,
250 uint32_t ownershipFlags = 0);
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000251
252 /**
skia.committer@gmail.comf3dc1992012-11-01 02:01:27 +0000253 * Add the new resource to the cache (by creating a new cache entry based
254 * on the provided key and resource).
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000255 *
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000256 * Ownership of the resource is transferred to the resource cache,
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000257 * which will unref() it when it is purged or deleted.
robertphillips@google.com209a1142012-10-31 12:25:21 +0000258 *
259 * If ownershipFlags includes kHide, subsequent calls to 'find' will not
skia.committer@gmail.comf3dc1992012-11-01 02:01:27 +0000260 * return 'resource' until it is 'freed' (and recycled) or makeNonExclusive
robertphillips@google.com209a1142012-10-31 12:25:21 +0000261 * is called.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000262 */
skia.committer@gmail.comf3dc1992012-11-01 02:01:27 +0000263 void addResource(const GrResourceKey& key,
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000264 GrCacheable* resource,
robertphillips@google.com209a1142012-10-31 12:25:21 +0000265 uint32_t ownershipFlags = 0);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000266
267 /**
bsalomon@google.comfb309512011-11-30 14:13:48 +0000268 * Determines if the cache contains an entry matching a key. If a matching
269 * entry exists but was detached then it will not be found.
270 */
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000271 bool hasKey(const GrResourceKey& key) const { return NULL != fCache.find(key); }
bsalomon@google.comfb309512011-11-30 14:13:48 +0000272
273 /**
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000274 * Hide 'entry' so that future searches will not find it. Such
275 * hidden entries will not be purged. The entry still counts against
276 * the cache's budget and should be made non-exclusive when exclusive access
277 * is no longer needed.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000278 */
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000279 void makeExclusive(GrResourceCacheEntry* entry);
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000280
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000281 /**
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000282 * Restore 'entry' so that it can be found by future searches. 'entry'
283 * will also be purgeable (provided its lock count is now 0.)
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000284 */
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000285 void makeNonExclusive(GrResourceCacheEntry* entry);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000286
287 /**
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000288 * Notify the cache that the size of a resource has changed.
289 */
290 void didIncreaseResourceSize(const GrResourceCacheEntry*, size_t amountInc);
291 void didDecreaseResourceSize(const GrResourceCacheEntry*, size_t amountDec);
292
293 /**
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000294 * Remove a resource from the cache and delete it!
295 */
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000296 void deleteResource(GrResourceCacheEntry* entry);
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000297
298 /**
bsalomon@google.coma2921122012-08-28 12:34:17 +0000299 * Removes every resource in the cache that isn't locked.
300 */
301 void purgeAllUnlocked();
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000302
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000303 /**
304 * Allow cache to purge unused resources to obey resource limitations
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000305 * Note: this entry point will be hidden (again) once totally ref-driven
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000306 * cache maintenance is implemented. Note that the overbudget callback
307 * will be called if the initial purge doesn't get the cache under
308 * its budget.
robertphillips@google.com41d25322013-07-18 17:12:57 +0000309 *
310 * extraCount and extraBytes are added to the current resource allocation
311 * to make sure enough room is available for future additions (e.g,
312 * 10MB across 10 textures is about to be added).
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000313 */
robertphillips@google.com41d25322013-07-18 17:12:57 +0000314 void purgeAsNeeded(int extraCount = 0, size_t extraBytes = 0);
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000315
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000316#ifdef SK_DEBUG
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000317 void validate() const;
318#else
319 void validate() const {}
320#endif
321
robertphillips@google.com59552022012-08-31 13:07:37 +0000322#if GR_CACHE_STATS
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000323 void printStats();
robertphillips@google.com59552022012-08-31 13:07:37 +0000324#endif
325
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000326private:
robertphillips@google.com209a1142012-10-31 12:25:21 +0000327 enum BudgetBehaviors {
328 kAccountFor_BudgetBehavior,
329 kIgnore_BudgetBehavior
330 };
331
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000332 void internalDetach(GrResourceCacheEntry*, BudgetBehaviors behavior = kAccountFor_BudgetBehavior);
333 void attachToHead(GrResourceCacheEntry*, BudgetBehaviors behavior = kAccountFor_BudgetBehavior);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000334
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000335 void removeInvalidResource(GrResourceCacheEntry* entry);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000336
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000337 GrTMultiMap<GrResourceCacheEntry, GrResourceKey> fCache;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000338
bsalomon@google.com42619d82012-12-03 14:54:59 +0000339 // We're an internal doubly linked list
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000340 typedef SkTInternalLList<GrResourceCacheEntry> EntryList;
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000341 EntryList fList;
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000342
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000343#ifdef SK_DEBUG
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000344 // These objects cannot be returned by a search
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000345 EntryList fExclusiveList;
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000346#endif
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000347
348 // our budget, used in purgeAsNeeded()
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000349 int fMaxCount;
350 size_t fMaxBytes;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000351
352 // our current stats, related to our budget
robertphillips@google.com59552022012-08-31 13:07:37 +0000353#if GR_CACHE_STATS
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000354 int fHighWaterEntryCount;
355 size_t fHighWaterEntryBytes;
356 int fHighWaterClientDetachedCount;
357 size_t fHighWaterClientDetachedBytes;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000358#endif
359
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000360 int fEntryCount;
361 size_t fEntryBytes;
362 int fClientDetachedCount;
363 size_t fClientDetachedBytes;
robertphillips@google.com386319e2012-06-27 14:59:18 +0000364
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000365 // prevents recursive purging
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000366 bool fPurging;
367
368 PFOverbudgetCB fOverbudgetCB;
369 void* fOverbudgetData;
370
robertphillips@google.com41d25322013-07-18 17:12:57 +0000371 void internalPurge(int extraCount, size_t extraBytes);
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000372
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000373 // Listen for messages that a resource has been invalidated and purge cached junk proactively.
374 SkMessageBus<GrResourceInvalidatedMessage>::Inbox fInvalidationInbox;
375 void purgeInvalidated();
376
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000377#ifdef SK_DEBUG
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000378 static size_t countBytes(const SkTInternalLList<GrResourceCacheEntry>& list);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000379#endif
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000380};
381
382///////////////////////////////////////////////////////////////////////////////
383
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000384#ifdef SK_DEBUG
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000385 class GrAutoResourceCacheValidate {
386 public:
387 GrAutoResourceCacheValidate(GrResourceCache* cache) : fCache(cache) {
388 cache->validate();
389 }
390 ~GrAutoResourceCacheValidate() {
391 fCache->validate();
392 }
393 private:
394 GrResourceCache* fCache;
395 };
396#else
397 class GrAutoResourceCacheValidate {
398 public:
399 GrAutoResourceCacheValidate(GrResourceCache*) {}
400 };
401#endif
402
403#endif