blob: ba5ea4ee3dca84445a67fc6374e01b066798b8a9 [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:
bsalomon@google.com8f7e1da2012-06-21 19:48:32 +0000197 GrResourceCache(size_t maxResourceBytes);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000198 ~GrResourceCache();
199
200 /**
bsalomon@google.com8f7e1da2012-06-21 19:48:32 +0000201 * Returns the cache budget in bytes.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000202 */
bsalomon@google.com8f7e1da2012-06-21 19:48:32 +0000203 size_t getBudget() const { return fMaxBytes; }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000204
205 /**
bsalomon@google.com8f7e1da2012-06-21 19:48:32 +0000206 * Specify the resource cache budget in bytes of GPU memory. If the current
207 * cache exceeds the budget it will be purged to be within the budget.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000208 *
bsalomon@google.com8f7e1da2012-06-21 19:48:32 +0000209 * @param maxResourceBytes The maximum number of bytes of GPU memory that
210 * can be held in the cache.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000211 */
bsalomon@google.com8f7e1da2012-06-21 19:48:32 +0000212 void setBudget(size_t maxResourceBytes);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000213
214 /**
twiz@google.com05e70242012-01-27 19:12:00 +0000215 * Returns the number of bytes consumed by cached resources.
216 */
217 size_t getCachedResourceBytes() const { return fEntryBytes; }
218
219 /**
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000220 * Controls whether locks should be nestable or not.
221 */
222 enum LockType {
223 kNested_LockType,
224 kSingle_LockType,
225 };
226
227 /**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000228 * Search for an entry with the same Key. If found, "lock" it and return it.
229 * If not found, return null.
230 */
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000231 GrResourceEntry* findAndLock(const GrResourceKey&, LockType style);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000232
233 /**
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000234 * Create a new cache entry, based on the provided key and resource, and
235 * return it.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000236 *
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000237 * Ownership of the resource is transferred to the resource cache,
238 * which will unref() it when it is purged or deleted.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000239 */
240 GrResourceEntry* createAndLock(const GrResourceKey&, GrResource*);
241
242 /**
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000243 * Create a new cache entry, based on the provided key and resource.
244 *
245 * Ownership of the resource is transferred to the resource cache,
246 * which will unref() it when it is purged or deleted.
247 *
248 * Currently this entry point is only intended for textures "detached"
249 * from an AutoScratchTexture object.
250 */
251 void attach(const GrResourceKey& key, GrResource* resource);
252
253 /**
bsalomon@google.comfb309512011-11-30 14:13:48 +0000254 * Determines if the cache contains an entry matching a key. If a matching
255 * entry exists but was detached then it will not be found.
256 */
257 bool hasKey(const GrResourceKey& key) const;
258
259 /**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000260 * Detach removes an entry from the cache. This prevents the entry from
261 * being found by a subsequent findAndLock() until it is reattached. The
262 * entry still counts against the cache's budget and should be reattached
263 * when exclusive access is no longer needed.
264 */
265 void detach(GrResourceEntry*);
266
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000267 void freeEntry(GrResourceEntry* entry);
268
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000269 /**
270 * Reattaches a resource to the cache and unlocks it. Allows it to be found
271 * by a subsequent findAndLock or be purged (provided its lock count is
272 * now 0.)
273 */
274 void reattachAndUnlock(GrResourceEntry*);
275
276 /**
277 * When done with an entry, call unlock(entry) on it, which returns it to
278 * a purgable state.
279 */
280 void unlock(GrResourceEntry*);
281
282 void removeAll();
283
284#if GR_DEBUG
285 void validate() const;
286#else
287 void validate() const {}
288#endif
289
290private:
291 void internalDetach(GrResourceEntry*, bool);
292 void attachToHead(GrResourceEntry*, bool);
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000293 void purgeAsNeeded();
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000294
295 class Key;
296 GrTHashTable<GrResourceEntry, Key, 8> fCache;
297
298 // manage the dlink list
299 GrResourceEntry* fHead;
300 GrResourceEntry* fTail;
301
302 // our budget, used in purgeAsNeeded()
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000303 size_t fMaxBytes;
304
305 // our current stats, related to our budget
306 int fEntryCount;
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000307 int fUnlockedEntryCount;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000308 size_t fEntryBytes;
309 int fClientDetachedCount;
310 size_t fClientDetachedBytes;
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000311
312 // prevents recursive purging
313 bool fPurging;
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000314
315 GrResourceEntry* create(const GrResourceKey& key,
316 GrResource* resource,
317 bool lock);
318
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000319};
320
321///////////////////////////////////////////////////////////////////////////////
322
323#if GR_DEBUG
324 class GrAutoResourceCacheValidate {
325 public:
326 GrAutoResourceCacheValidate(GrResourceCache* cache) : fCache(cache) {
327 cache->validate();
328 }
329 ~GrAutoResourceCacheValidate() {
330 fCache->validate();
331 }
332 private:
333 GrResourceCache* fCache;
334 };
335#else
336 class GrAutoResourceCacheValidate {
337 public:
338 GrAutoResourceCacheValidate(GrResourceCache*) {}
339 };
340#endif
341
342#endif