blob: b83e8ec348ca6967876c257d42d15e7cb87ef99b [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
robertphillips@google.coma1e57952012-06-04 20:05:28 +000044 enum TypeBits {
45 // resource types
46 kTexture_TypeBit = 0x01,
47 kStencilBuffer_TypeBit = 0x02,
48
49 // Derived classes may add additional bits
50 kDummy_TypeBit,
51 kLastPublic_TypeBit = kDummy_TypeBit-1,
52 };
53
bsalomon@google.com50398bf2011-07-26 20:45:30 +000054 GrResourceKey(uint32_t p0, uint32_t p1, uint32_t p2, uint32_t p3) {
55 fP[0] = p0;
56 fP[1] = p1;
57 fP[2] = p2;
58 fP[3] = p3;
59 this->computeHashIndex();
60 }
61
62 GrResourceKey(uint32_t v[4]) {
63 memcpy(fP, v, 4 * sizeof(uint32_t));
64 this->computeHashIndex();
65 }
66
67 GrResourceKey(const GrResourceKey& src) {
68 memcpy(fP, src.fP, 4 * sizeof(uint32_t));
69#if GR_DEBUG
70 this->computeHashIndex();
71 GrAssert(fHashIndex == src.fHashIndex);
72#endif
73 fHashIndex = src.fHashIndex;
74 }
75
76 //!< returns hash value [0..kHashMask] for the key
77 int hashIndex() const { return fHashIndex; }
78
79 friend bool operator==(const GrResourceKey& a, const GrResourceKey& b) {
80 GR_DEBUGASSERT(-1 != a.fHashIndex && -1 != b.fHashIndex);
81 return 0 == memcmp(a.fP, b.fP, 4 * sizeof(uint32_t));
82 }
83
84 friend bool operator!=(const GrResourceKey& a, const GrResourceKey& b) {
85 GR_DEBUGASSERT(-1 != a.fHashIndex && -1 != b.fHashIndex);
86 return !(a == b);
87 }
88
89 friend bool operator<(const GrResourceKey& a, const GrResourceKey& b) {
90 RET_IF_LT_OR_GT(a.fP[0], b.fP[0]);
91 RET_IF_LT_OR_GT(a.fP[1], b.fP[1]);
92 RET_IF_LT_OR_GT(a.fP[2], b.fP[2]);
93 return a.fP[3] < b.fP[3];
94 }
95
96 uint32_t getValue32(int i) const {
97 GrAssert(i >=0 && i < 4);
98 return fP[i];
99 }
100private:
101
102 static uint32_t rol(uint32_t x) {
103 return (x >> 24) | (x << 8);
104 }
105 static uint32_t ror(uint32_t x) {
106 return (x >> 8) | (x << 24);
107 }
108 static uint32_t rohalf(uint32_t x) {
109 return (x >> 16) | (x << 16);
110 }
111
112 void computeHashIndex() {
113 uint32_t hash = fP[0] ^ rol(fP[1]) ^ ror(fP[2]) ^ rohalf(fP[3]);
114 // this way to mix and reduce hash to its index may have to change
115 // depending on how many bits we allocate to the index
116 hash ^= hash >> 16;
117 hash ^= hash >> 8;
118 fHashIndex = hash & kHashMask;
119 }
120
121 uint32_t fP[4];
122
123 // this is computed from the fP... fields
124 int fHashIndex;
125
126 friend class GrContext;
127};
128
129///////////////////////////////////////////////////////////////////////////////
130
131class GrResourceEntry {
132public:
133 GrResource* resource() const { return fResource; }
134 const GrResourceKey& key() const { return fKey; }
135
136#if GR_DEBUG
137 GrResourceEntry* next() const { return fNext; }
138 GrResourceEntry* prev() const { return fPrev; }
139#endif
140
141#if GR_DEBUG
142 void validate() const;
143#else
144 void validate() const {}
145#endif
146
147private:
148 GrResourceEntry(const GrResourceKey& key, GrResource* resource);
149 ~GrResourceEntry();
150
151 bool isLocked() const { return fLockCount != 0; }
152 void lock() { ++fLockCount; }
153 void unlock() {
154 GrAssert(fLockCount > 0);
155 --fLockCount;
156 }
157
158 GrResourceKey fKey;
159 GrResource* fResource;
160
161 // track if we're in use, used when we need to purge
162 // we only purge unlocked entries
163 int fLockCount;
164
165 // we're a dlinklist
166 GrResourceEntry* fPrev;
167 GrResourceEntry* fNext;
168
169 friend class GrResourceCache;
170};
171
172///////////////////////////////////////////////////////////////////////////////
173
174#include "GrTHashCache.h"
175
176/**
177 * Cache of GrResource objects.
178 *
179 * These have a corresponding GrResourceKey, built from 128bits identifying the
180 * resource.
181 *
182 * The cache stores the entries in a double-linked list, which is its LRU.
183 * When an entry is "locked" (i.e. given to the caller), it is moved to the
184 * head of the list. If/when we must purge some of the entries, we walk the
185 * list backwards from the tail, since those are the least recently used.
186 *
187 * For fast searches, we maintain a sorted array (based on the GrResourceKey)
188 * which we can bsearch. When a new entry is added, it is inserted into this
189 * array.
190 *
191 * For even faster searches, a hash is computed from the Key. If there is
192 * a collision between two keys with the same hash, we fall back on the
193 * bsearch, and update the hash to reflect the most recent Key requested.
194 */
195class GrResourceCache {
196public:
197 GrResourceCache(int maxCount, size_t maxBytes);
198 ~GrResourceCache();
199
200 /**
201 * Return the current resource cache limits.
202 *
203 * @param maxResource If non-null, returns maximum number of resources
204 * that can be held in the cache.
205 * @param maxBytes If non-null, returns maximum number of bytes of
206 * gpu memory that can be held in the cache.
207 */
208 void getLimits(int* maxResources, size_t* maxBytes) const;
209
210 /**
211 * Specify the resource cache limits. If the current cache exceeds either
212 * of these, it will be purged (LRU) to keep the cache within these limits.
213 *
214 * @param maxResources The maximum number of resources that can be held in
215 * the cache.
216 * @param maxBytes The maximum number of bytes of resource memory that
217 * can be held in the cache.
218 */
219 void setLimits(int maxResource, size_t maxResourceBytes);
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
226 /**
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000227 * Controls whether locks should be nestable or not.
228 */
229 enum LockType {
230 kNested_LockType,
231 kSingle_LockType,
232 };
233
234 /**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000235 * Search for an entry with the same Key. If found, "lock" it and return it.
236 * If not found, return null.
237 */
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000238 GrResourceEntry* findAndLock(const GrResourceKey&, LockType style);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000239
240 /**
241 * Create a new entry, based on the specified key and resource, and return
242 * its "locked" entry.
243 *
244 * Ownership of the resource is transferred to the Entry, which will unref()
245 * it when we are purged or deleted.
246 */
247 GrResourceEntry* createAndLock(const GrResourceKey&, GrResource*);
248
249 /**
bsalomon@google.comfb309512011-11-30 14:13:48 +0000250 * Determines if the cache contains an entry matching a key. If a matching
251 * entry exists but was detached then it will not be found.
252 */
253 bool hasKey(const GrResourceKey& key) const;
254
255 /**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000256 * Detach removes an entry from the cache. This prevents the entry from
257 * being found by a subsequent findAndLock() until it is reattached. The
258 * entry still counts against the cache's budget and should be reattached
259 * when exclusive access is no longer needed.
260 */
261 void detach(GrResourceEntry*);
262
263 /**
264 * Reattaches a resource to the cache and unlocks it. Allows it to be found
265 * by a subsequent findAndLock or be purged (provided its lock count is
266 * now 0.)
267 */
268 void reattachAndUnlock(GrResourceEntry*);
269
270 /**
271 * When done with an entry, call unlock(entry) on it, which returns it to
272 * a purgable state.
273 */
274 void unlock(GrResourceEntry*);
275
276 void removeAll();
277
278#if GR_DEBUG
279 void validate() const;
280#else
281 void validate() const {}
282#endif
283
284private:
285 void internalDetach(GrResourceEntry*, bool);
286 void attachToHead(GrResourceEntry*, bool);
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000287 void purgeAsNeeded();
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000288
289 class Key;
290 GrTHashTable<GrResourceEntry, Key, 8> fCache;
291
292 // manage the dlink list
293 GrResourceEntry* fHead;
294 GrResourceEntry* fTail;
295
296 // our budget, used in purgeAsNeeded()
297 int fMaxCount;
298 size_t fMaxBytes;
299
300 // our current stats, related to our budget
301 int fEntryCount;
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000302 int fUnlockedEntryCount;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000303 size_t fEntryBytes;
304 int fClientDetachedCount;
305 size_t fClientDetachedBytes;
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000306
307 // prevents recursive purging
308 bool fPurging;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000309};
310
311///////////////////////////////////////////////////////////////////////////////
312
313#if GR_DEBUG
314 class GrAutoResourceCacheValidate {
315 public:
316 GrAutoResourceCacheValidate(GrResourceCache* cache) : fCache(cache) {
317 cache->validate();
318 }
319 ~GrAutoResourceCacheValidate() {
320 fCache->validate();
321 }
322 private:
323 GrResourceCache* fCache;
324 };
325#else
326 class GrAutoResourceCacheValidate {
327 public:
328 GrAutoResourceCacheValidate(GrResourceCache*) {}
329 };
330#endif
331
332#endif