blob: d708e0a19374ff5d988fe479a69d99f5b629699e [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"
16
17class GrResource;
18
19// return true if a<b, or false if b<a
20//
21#define RET_IF_LT_OR_GT(a, b) \
22 do { \
23 if ((a) < (b)) { \
24 return true; \
25 } \
26 if ((b) < (a)) { \
27 return false; \
28 } \
29 } while (0)
30
31/**
32 * Helper class for GrResourceCache, the Key is used to identify src data for
33 * a resource. It is identified by 2 32bit data fields which can hold any
34 * data (uninterpreted by the cache) and a width/height.
35 */
36class GrResourceKey {
37public:
38 enum {
39 kHashBits = 7,
40 kHashCount = 1 << kHashBits,
41 kHashMask = kHashCount - 1
42 };
43
44 GrResourceKey(uint32_t p0, uint32_t p1, uint32_t p2, uint32_t p3) {
45 fP[0] = p0;
46 fP[1] = p1;
47 fP[2] = p2;
48 fP[3] = p3;
49 this->computeHashIndex();
50 }
51
52 GrResourceKey(uint32_t v[4]) {
53 memcpy(fP, v, 4 * sizeof(uint32_t));
54 this->computeHashIndex();
55 }
56
57 GrResourceKey(const GrResourceKey& src) {
58 memcpy(fP, src.fP, 4 * sizeof(uint32_t));
59#if GR_DEBUG
60 this->computeHashIndex();
61 GrAssert(fHashIndex == src.fHashIndex);
62#endif
63 fHashIndex = src.fHashIndex;
64 }
65
66 //!< returns hash value [0..kHashMask] for the key
67 int hashIndex() const { return fHashIndex; }
68
69 friend bool operator==(const GrResourceKey& a, const GrResourceKey& b) {
70 GR_DEBUGASSERT(-1 != a.fHashIndex && -1 != b.fHashIndex);
71 return 0 == memcmp(a.fP, b.fP, 4 * sizeof(uint32_t));
72 }
73
74 friend bool operator!=(const GrResourceKey& a, const GrResourceKey& b) {
75 GR_DEBUGASSERT(-1 != a.fHashIndex && -1 != b.fHashIndex);
76 return !(a == b);
77 }
78
79 friend bool operator<(const GrResourceKey& a, const GrResourceKey& b) {
80 RET_IF_LT_OR_GT(a.fP[0], b.fP[0]);
81 RET_IF_LT_OR_GT(a.fP[1], b.fP[1]);
82 RET_IF_LT_OR_GT(a.fP[2], b.fP[2]);
83 return a.fP[3] < b.fP[3];
84 }
85
86 uint32_t getValue32(int i) const {
87 GrAssert(i >=0 && i < 4);
88 return fP[i];
89 }
90private:
91
92 static uint32_t rol(uint32_t x) {
93 return (x >> 24) | (x << 8);
94 }
95 static uint32_t ror(uint32_t x) {
96 return (x >> 8) | (x << 24);
97 }
98 static uint32_t rohalf(uint32_t x) {
99 return (x >> 16) | (x << 16);
100 }
101
102 void computeHashIndex() {
103 uint32_t hash = fP[0] ^ rol(fP[1]) ^ ror(fP[2]) ^ rohalf(fP[3]);
104 // this way to mix and reduce hash to its index may have to change
105 // depending on how many bits we allocate to the index
106 hash ^= hash >> 16;
107 hash ^= hash >> 8;
108 fHashIndex = hash & kHashMask;
109 }
110
111 uint32_t fP[4];
112
113 // this is computed from the fP... fields
114 int fHashIndex;
115
116 friend class GrContext;
117};
118
119///////////////////////////////////////////////////////////////////////////////
120
121class GrResourceEntry {
122public:
123 GrResource* resource() const { return fResource; }
124 const GrResourceKey& key() const { return fKey; }
125
126#if GR_DEBUG
127 GrResourceEntry* next() const { return fNext; }
128 GrResourceEntry* prev() const { return fPrev; }
129#endif
130
131#if GR_DEBUG
132 void validate() const;
133#else
134 void validate() const {}
135#endif
136
137private:
138 GrResourceEntry(const GrResourceKey& key, GrResource* resource);
139 ~GrResourceEntry();
140
141 bool isLocked() const { return fLockCount != 0; }
142 void lock() { ++fLockCount; }
143 void unlock() {
144 GrAssert(fLockCount > 0);
145 --fLockCount;
146 }
147
148 GrResourceKey fKey;
149 GrResource* fResource;
150
151 // track if we're in use, used when we need to purge
152 // we only purge unlocked entries
153 int fLockCount;
154
155 // we're a dlinklist
156 GrResourceEntry* fPrev;
157 GrResourceEntry* fNext;
158
159 friend class GrResourceCache;
160};
161
162///////////////////////////////////////////////////////////////////////////////
163
164#include "GrTHashCache.h"
165
166/**
167 * Cache of GrResource objects.
168 *
169 * These have a corresponding GrResourceKey, built from 128bits identifying the
170 * resource.
171 *
172 * The cache stores the entries in a double-linked list, which is its LRU.
173 * When an entry is "locked" (i.e. given to the caller), it is moved to the
174 * head of the list. If/when we must purge some of the entries, we walk the
175 * list backwards from the tail, since those are the least recently used.
176 *
177 * For fast searches, we maintain a sorted array (based on the GrResourceKey)
178 * which we can bsearch. When a new entry is added, it is inserted into this
179 * array.
180 *
181 * For even faster searches, a hash is computed from the Key. If there is
182 * a collision between two keys with the same hash, we fall back on the
183 * bsearch, and update the hash to reflect the most recent Key requested.
184 */
185class GrResourceCache {
186public:
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000187 GrResourceCache(int maxCount, size_t maxBytes);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000188 ~GrResourceCache();
189
190 /**
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000191 * Return the current resource cache limits.
192 *
193 * @param maxResource If non-null, returns maximum number of resources
194 * that can be held in the cache.
195 * @param maxBytes If non-null, returns maximum number of bytes of
196 * gpu memory that can be held in the cache.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000197 */
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000198 void getLimits(int* maxResources, size_t* maxBytes) const;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000199
200 /**
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000201 * Specify the resource cache limits. If the current cache exceeds either
202 * of these, it will be purged (LRU) to keep the cache within these limits.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000203 *
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000204 * @param maxResources The maximum number of resources that can be held in
205 * the cache.
206 * @param maxBytes The maximum number of bytes of resource memory that
207 * can be held in the cache.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000208 */
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000209 void setLimits(int maxResource, size_t maxResourceBytes);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000210
211 /**
twiz@google.com05e70242012-01-27 19:12:00 +0000212 * Returns the number of bytes consumed by cached resources.
213 */
214 size_t getCachedResourceBytes() const { return fEntryBytes; }
215
216 /**
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000217 * Controls whether locks should be nestable or not.
218 */
219 enum LockType {
220 kNested_LockType,
221 kSingle_LockType,
222 };
223
224 /**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000225 * Search for an entry with the same Key. If found, "lock" it and return it.
226 * If not found, return null.
227 */
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000228 GrResource* findAndLock(const GrResourceKey&, LockType style);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000229
230 /**
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000231 * Create a new cache entry, based on the provided key and resource, and
232 * return it.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000233 *
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000234 * Ownership of the resource is transferred to the resource cache,
235 * which will unref() it when it is purged or deleted.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000236 */
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000237 void createAndLock(const GrResourceKey&, GrResource*);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000238
239 /**
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000240 * Create a new cache entry, based on the provided key and resource.
241 *
242 * Ownership of the resource is transferred to the resource cache,
243 * which will unref() it when it is purged or deleted.
244 *
245 * Currently this entry point is only intended for textures "detached"
246 * from an AutoScratchTexture object.
247 */
248 void attach(const GrResourceKey& key, GrResource* resource);
249
250 /**
bsalomon@google.comfb309512011-11-30 14:13:48 +0000251 * Determines if the cache contains an entry matching a key. If a matching
252 * entry exists but was detached then it will not be found.
253 */
254 bool hasKey(const GrResourceKey& key) const;
255
256 /**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000257 * Detach removes an entry from the cache. This prevents the entry from
258 * being found by a subsequent findAndLock() until it is reattached. The
259 * entry still counts against the cache's budget and should be reattached
260 * when exclusive access is no longer needed.
261 */
262 void detach(GrResourceEntry*);
263
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000264 void freeEntry(GrResourceEntry* entry);
265
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000266 /**
267 * Reattaches a resource to the cache and unlocks it. Allows it to be found
268 * by a subsequent findAndLock or be purged (provided its lock count is
269 * now 0.)
270 */
271 void reattachAndUnlock(GrResourceEntry*);
272
273 /**
274 * When done with an entry, call unlock(entry) on it, which returns it to
275 * a purgable state.
276 */
277 void unlock(GrResourceEntry*);
278
279 void removeAll();
280
281#if GR_DEBUG
282 void validate() const;
283#else
284 void validate() const {}
285#endif
286
287private:
288 void internalDetach(GrResourceEntry*, bool);
289 void attachToHead(GrResourceEntry*, bool);
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000290 void purgeAsNeeded();
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000291
292 class Key;
293 GrTHashTable<GrResourceEntry, Key, 8> fCache;
294
295 // manage the dlink list
296 GrResourceEntry* fHead;
297 GrResourceEntry* fTail;
298
299 // our budget, used in purgeAsNeeded()
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000300 int fMaxCount;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000301 size_t fMaxBytes;
302
303 // our current stats, related to our budget
304 int fEntryCount;
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000305 int fUnlockedEntryCount;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000306 size_t fEntryBytes;
307 int fClientDetachedCount;
308 size_t fClientDetachedBytes;
robertphillips@google.com386319e2012-06-27 14:59:18 +0000309
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000310 // prevents recursive purging
311 bool fPurging;
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000312
313 GrResourceEntry* create(const GrResourceKey& key,
314 GrResource* resource,
robertphillips@google.com386319e2012-06-27 14:59:18 +0000315 bool lock,
316 bool clientReattach);
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000317
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000318};
319
320///////////////////////////////////////////////////////////////////////////////
321
322#if GR_DEBUG
323 class GrAutoResourceCacheValidate {
324 public:
325 GrAutoResourceCacheValidate(GrResourceCache* cache) : fCache(cache) {
326 cache->validate();
327 }
328 ~GrAutoResourceCacheValidate() {
329 fCache->validate();
330 }
331 private:
332 GrResourceCache* fCache;
333 };
334#else
335 class GrAutoResourceCacheValidate {
336 public:
337 GrAutoResourceCacheValidate(GrResourceCache*) {}
338 };
339#endif
340
341#endif