blob: 525c67e34fb37b7b39f258d24045dc2a406958cf [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.com07fc0d12012-06-22 15:15:59 +0000197 GrResourceCache(int maxCount, size_t maxBytes);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000198 ~GrResourceCache();
199
200 /**
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000201 * 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.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000207 */
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000208 void getLimits(int* maxResources, size_t* maxBytes) const;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000209
210 /**
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000211 * 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.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000213 *
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000214 * @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.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000218 */
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000219 void setLimits(int maxResource, size_t maxResourceBytes);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000220
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 /**
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000241 * Create a new cache entry, based on the provided key and resource, and
242 * return it.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000243 *
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000244 * Ownership of the resource is transferred to the resource cache,
245 * which will unref() it when it is purged or deleted.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000246 */
247 GrResourceEntry* createAndLock(const GrResourceKey&, GrResource*);
248
249 /**
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000250 * Create a new cache entry, based on the provided key and resource.
251 *
252 * Ownership of the resource is transferred to the resource cache,
253 * which will unref() it when it is purged or deleted.
254 *
255 * Currently this entry point is only intended for textures "detached"
256 * from an AutoScratchTexture object.
257 */
258 void attach(const GrResourceKey& key, GrResource* resource);
259
260 /**
bsalomon@google.comfb309512011-11-30 14:13:48 +0000261 * Determines if the cache contains an entry matching a key. If a matching
262 * entry exists but was detached then it will not be found.
263 */
264 bool hasKey(const GrResourceKey& key) const;
265
266 /**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000267 * Detach removes an entry from the cache. This prevents the entry from
268 * being found by a subsequent findAndLock() until it is reattached. The
269 * entry still counts against the cache's budget and should be reattached
270 * when exclusive access is no longer needed.
271 */
272 void detach(GrResourceEntry*);
273
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000274 void freeEntry(GrResourceEntry* entry);
275
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000276 /**
277 * Reattaches a resource to the cache and unlocks it. Allows it to be found
278 * by a subsequent findAndLock or be purged (provided its lock count is
279 * now 0.)
280 */
281 void reattachAndUnlock(GrResourceEntry*);
282
283 /**
284 * When done with an entry, call unlock(entry) on it, which returns it to
285 * a purgable state.
286 */
287 void unlock(GrResourceEntry*);
288
289 void removeAll();
290
291#if GR_DEBUG
292 void validate() const;
293#else
294 void validate() const {}
295#endif
296
297private:
298 void internalDetach(GrResourceEntry*, bool);
299 void attachToHead(GrResourceEntry*, bool);
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000300 void purgeAsNeeded();
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000301
302 class Key;
303 GrTHashTable<GrResourceEntry, Key, 8> fCache;
304
305 // manage the dlink list
306 GrResourceEntry* fHead;
307 GrResourceEntry* fTail;
308
309 // our budget, used in purgeAsNeeded()
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000310 int fMaxCount;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000311 size_t fMaxBytes;
312
313 // our current stats, related to our budget
314 int fEntryCount;
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000315 int fUnlockedEntryCount;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000316 size_t fEntryBytes;
317 int fClientDetachedCount;
318 size_t fClientDetachedBytes;
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000319
320 // prevents recursive purging
321 bool fPurging;
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000322
323 GrResourceEntry* create(const GrResourceKey& key,
324 GrResource* resource,
325 bool lock);
326
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000327};
328
329///////////////////////////////////////////////////////////////////////////////
330
331#if GR_DEBUG
332 class GrAutoResourceCacheValidate {
333 public:
334 GrAutoResourceCacheValidate(GrResourceCache* cache) : fCache(cache) {
335 cache->validate();
336 }
337 ~GrAutoResourceCacheValidate() {
338 fCache->validate();
339 }
340 private:
341 GrResourceCache* fCache;
342 };
343#else
344 class GrAutoResourceCacheValidate {
345 public:
346 GrAutoResourceCacheValidate(GrResourceCache*) {}
347 };
348#endif
349
350#endif