blob: 00e62ea923159539a373dc7b7be9a2b39eadf0ee [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
14#include "GrTypes.h"
15#include "GrTHashCache.h"
robertphillips@google.com2ea0a232012-08-23 11:13:48 +000016#include "SkTDLinkedList.h"
bsalomon@google.com50398bf2011-07-26 20:45:30 +000017
18class GrResource;
19
20// return true if a<b, or false if b<a
21//
22#define RET_IF_LT_OR_GT(a, b) \
23 do { \
24 if ((a) < (b)) { \
25 return true; \
26 } \
27 if ((b) < (a)) { \
28 return false; \
29 } \
30 } while (0)
31
32/**
33 * Helper class for GrResourceCache, the Key is used to identify src data for
34 * a resource. It is identified by 2 32bit data fields which can hold any
35 * data (uninterpreted by the cache) and a width/height.
36 */
37class GrResourceKey {
38public:
39 enum {
40 kHashBits = 7,
41 kHashCount = 1 << kHashBits,
42 kHashMask = kHashCount - 1
43 };
44
45 GrResourceKey(uint32_t p0, uint32_t p1, uint32_t p2, uint32_t p3) {
46 fP[0] = p0;
47 fP[1] = p1;
48 fP[2] = p2;
49 fP[3] = p3;
50 this->computeHashIndex();
51 }
52
53 GrResourceKey(uint32_t v[4]) {
54 memcpy(fP, v, 4 * sizeof(uint32_t));
55 this->computeHashIndex();
56 }
57
58 GrResourceKey(const GrResourceKey& src) {
59 memcpy(fP, src.fP, 4 * sizeof(uint32_t));
60#if GR_DEBUG
61 this->computeHashIndex();
62 GrAssert(fHashIndex == src.fHashIndex);
63#endif
64 fHashIndex = src.fHashIndex;
65 }
66
67 //!< returns hash value [0..kHashMask] for the key
68 int hashIndex() const { return fHashIndex; }
69
70 friend bool operator==(const GrResourceKey& a, const GrResourceKey& b) {
71 GR_DEBUGASSERT(-1 != a.fHashIndex && -1 != b.fHashIndex);
72 return 0 == memcmp(a.fP, b.fP, 4 * sizeof(uint32_t));
73 }
74
75 friend bool operator!=(const GrResourceKey& a, const GrResourceKey& b) {
76 GR_DEBUGASSERT(-1 != a.fHashIndex && -1 != b.fHashIndex);
77 return !(a == b);
78 }
79
80 friend bool operator<(const GrResourceKey& a, const GrResourceKey& b) {
81 RET_IF_LT_OR_GT(a.fP[0], b.fP[0]);
82 RET_IF_LT_OR_GT(a.fP[1], b.fP[1]);
83 RET_IF_LT_OR_GT(a.fP[2], b.fP[2]);
84 return a.fP[3] < b.fP[3];
85 }
86
87 uint32_t getValue32(int i) const {
88 GrAssert(i >=0 && i < 4);
89 return fP[i];
90 }
91private:
92
93 static uint32_t rol(uint32_t x) {
94 return (x >> 24) | (x << 8);
95 }
96 static uint32_t ror(uint32_t x) {
97 return (x >> 8) | (x << 24);
98 }
99 static uint32_t rohalf(uint32_t x) {
100 return (x >> 16) | (x << 16);
101 }
102
103 void computeHashIndex() {
104 uint32_t hash = fP[0] ^ rol(fP[1]) ^ ror(fP[2]) ^ rohalf(fP[3]);
105 // this way to mix and reduce hash to its index may have to change
106 // depending on how many bits we allocate to the index
107 hash ^= hash >> 16;
108 hash ^= hash >> 8;
109 fHashIndex = hash & kHashMask;
110 }
111
112 uint32_t fP[4];
113
114 // this is computed from the fP... fields
115 int fHashIndex;
116
117 friend class GrContext;
118};
119
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000120
121class GrCacheKey {
122public:
123 GrCacheKey(const GrTextureDesc& desc, const GrResourceKey& key)
124 : fDesc(desc)
125 , fKey(key) {
126 }
127
128 void set(const GrTextureDesc& desc, const GrResourceKey& key) {
129 fDesc = desc;
130 fKey = key;
131 }
132
133 const GrTextureDesc& desc() const { return fDesc; }
134 const GrResourceKey& key() const { return fKey; }
135
136protected:
137 GrTextureDesc fDesc;
138 GrResourceKey fKey;
139};
140
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000141///////////////////////////////////////////////////////////////////////////////
142
143class GrResourceEntry {
144public:
145 GrResource* resource() const { return fResource; }
146 const GrResourceKey& key() const { return fKey; }
147
148#if GR_DEBUG
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000149 void validate() const;
150#else
151 void validate() const {}
152#endif
153
154private:
155 GrResourceEntry(const GrResourceKey& key, GrResource* resource);
156 ~GrResourceEntry();
157
158 bool isLocked() const { return fLockCount != 0; }
159 void lock() { ++fLockCount; }
160 void unlock() {
161 GrAssert(fLockCount > 0);
162 --fLockCount;
163 }
164
165 GrResourceKey fKey;
166 GrResource* fResource;
167
168 // track if we're in use, used when we need to purge
169 // we only purge unlocked entries
170 int fLockCount;
171
172 // we're a dlinklist
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000173 SK_DEFINE_DLINKEDLIST_INTERFACE(GrResourceEntry);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000174
175 friend class GrResourceCache;
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000176 friend class GrDLinkedList;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000177};
178
179///////////////////////////////////////////////////////////////////////////////
180
181#include "GrTHashCache.h"
182
183/**
184 * Cache of GrResource objects.
185 *
186 * These have a corresponding GrResourceKey, built from 128bits identifying the
187 * resource.
188 *
189 * The cache stores the entries in a double-linked list, which is its LRU.
190 * When an entry is "locked" (i.e. given to the caller), it is moved to the
191 * head of the list. If/when we must purge some of the entries, we walk the
192 * list backwards from the tail, since those are the least recently used.
193 *
194 * For fast searches, we maintain a sorted array (based on the GrResourceKey)
195 * which we can bsearch. When a new entry is added, it is inserted into this
196 * array.
197 *
198 * For even faster searches, a hash is computed from the Key. If there is
199 * a collision between two keys with the same hash, we fall back on the
200 * bsearch, and update the hash to reflect the most recent Key requested.
201 */
202class GrResourceCache {
203public:
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000204 GrResourceCache(int maxCount, size_t maxBytes);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000205 ~GrResourceCache();
206
207 /**
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000208 * Return the current resource cache limits.
209 *
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000210 * @param maxResource If non-null, returns maximum number of resources
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000211 * that can be held in the cache.
212 * @param maxBytes If non-null, returns maximum number of bytes of
213 * gpu memory that can be held in the cache.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000214 */
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000215 void getLimits(int* maxResources, size_t* maxBytes) const;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000216
217 /**
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000218 * Specify the resource cache limits. If the current cache exceeds either
219 * of these, it will be purged (LRU) to keep the cache within these limits.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000220 *
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000221 * @param maxResources The maximum number of resources that can be held in
222 * the cache.
223 * @param maxBytes The maximum number of bytes of resource memory that
224 * can be held in the cache.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000225 */
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000226 void setLimits(int maxResource, size_t maxResourceBytes);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000227
228 /**
twiz@google.com05e70242012-01-27 19:12:00 +0000229 * Returns the number of bytes consumed by cached resources.
230 */
231 size_t getCachedResourceBytes() const { return fEntryBytes; }
232
233 /**
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000234 * Controls whether locks should be nestable or not.
235 */
236 enum LockType {
237 kNested_LockType,
238 kSingle_LockType,
239 };
240
241 /**
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000242 * Search for an entry with the same Key. If found, return it.
243 * If not found, return null.
244 */
245 GrResource* find(const GrResourceKey& key);
246
247 /**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000248 * Search for an entry with the same Key. If found, "lock" it and return it.
249 * If not found, return null.
250 */
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000251 GrResource* findAndLock(const GrResourceKey&, LockType style);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000252
253 /**
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000254 * Create a new cache entry, based on the provided key and resource, and
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000255 * return it.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000256 *
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000257 * Ownership of the resource is transferred to the resource cache,
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000258 * which will unref() it when it is purged or deleted.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000259 */
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000260 void createAndLock(const GrResourceKey&, GrResource*);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000261
262 /**
bsalomon@google.comfb309512011-11-30 14:13:48 +0000263 * Determines if the cache contains an entry matching a key. If a matching
264 * entry exists but was detached then it will not be found.
265 */
266 bool hasKey(const GrResourceKey& key) const;
267
268 /**
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000269 * Hide 'entry' so that future searches will not find it. Such
270 * hidden entries will not be purged. The entry still counts against
271 * the cache's budget and should be made non-exclusive when exclusive access
272 * is no longer needed.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000273 */
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000274 void makeExclusive(GrResourceEntry* entry);
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000275
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000276 /**
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000277 * Restore 'entry' so that it can be found by future searches. 'entry'
278 * will also be purgeable (provided its lock count is now 0.)
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000279 */
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000280 void makeNonExclusive(GrResourceEntry* entry);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000281
282 /**
bsalomon@google.coma2921122012-08-28 12:34:17 +0000283 * When done with an entry, call unlock(entry) on it, which returns it to
284 * a purgable state.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000285 */
286 void unlock(GrResourceEntry*);
287
bsalomon@google.coma2921122012-08-28 12:34:17 +0000288 /**
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000289 * Make a resource un-purgeable.
290 */
291 void lock(GrResourceEntry* entry);
292
293 /**
bsalomon@google.coma2921122012-08-28 12:34:17 +0000294 * Removes every resource in the cache that isn't locked.
295 */
296 void purgeAllUnlocked();
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000297
298#if GR_DEBUG
299 void validate() const;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000300 void printStats() const;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000301#else
302 void validate() const {}
303#endif
304
305private:
306 void internalDetach(GrResourceEntry*, bool);
307 void attachToHead(GrResourceEntry*, bool);
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000308 void purgeAsNeeded();
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000309
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000310 void removeInvalidResource(GrResourceEntry* entry);
311
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000312 class Key;
313 GrTHashTable<GrResourceEntry, Key, 8> fCache;
314
315 // manage the dlink list
bsalomon@google.coma2921122012-08-28 12:34:17 +0000316 typedef SkTDLinkedList<GrResourceEntry> EntryList;
317 EntryList fList;
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000318
319#if GR_DEBUG
320 // These objects cannot be returned by a search
bsalomon@google.coma2921122012-08-28 12:34:17 +0000321 EntryList fExclusiveList;
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000322#endif
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000323
324 // our budget, used in purgeAsNeeded()
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000325 int fMaxCount;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000326 size_t fMaxBytes;
327
328 // our current stats, related to our budget
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000329#if GR_DEBUG
330 int fHighWaterEntryCount;
331 int fHighWaterUnlockedEntryCount;
332 size_t fHighWaterEntryBytes;
333 int fHighWaterClientDetachedCount;
334 size_t fHighWaterClientDetachedBytes;
335#endif
336
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000337 int fEntryCount;
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000338 int fUnlockedEntryCount;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000339 size_t fEntryBytes;
340 int fClientDetachedCount;
341 size_t fClientDetachedBytes;
robertphillips@google.com386319e2012-06-27 14:59:18 +0000342
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000343 // prevents recursive purging
344 bool fPurging;
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000345
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000346 void create(const GrResourceKey& key, GrResource* resource);
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000347
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000348#if GR_DEBUG
robertphillips@google.coma5c43a52012-08-23 11:24:02 +0000349 static size_t countBytes(const SkTDLinkedList<GrResourceEntry>& list);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000350#endif
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000351};
352
353///////////////////////////////////////////////////////////////////////////////
354
355#if GR_DEBUG
356 class GrAutoResourceCacheValidate {
357 public:
358 GrAutoResourceCacheValidate(GrResourceCache* cache) : fCache(cache) {
359 cache->validate();
360 }
361 ~GrAutoResourceCacheValidate() {
362 fCache->validate();
363 }
364 private:
365 GrResourceCache* fCache;
366 };
367#else
368 class GrAutoResourceCacheValidate {
369 public:
370 GrAutoResourceCacheValidate(GrResourceCache*) {}
371 };
372#endif
373
374#endif