blob: 7b5870e250099cbf9dc6d66880c969a4ca168075 [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;
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000160 friend class GrDLinkedList;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000161};
162
163///////////////////////////////////////////////////////////////////////////////
164
165#include "GrTHashCache.h"
166
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000167class GrDLinkedList {
168public:
169 GrDLinkedList()
170 : fHead(NULL)
171 , fTail(NULL) {
172 }
173
174 void remove(GrResourceEntry* entry);
175 void addToHead(GrResourceEntry* entry);
176
177#ifdef GR_DEBUG
178 bool isInList(const GrResourceEntry* entry) const;
179 bool isEmpty() const { return NULL == fHead && NULL == fTail; }
180 int countEntries() const;
181 size_t countBytes() const;
182#endif
183
184 GrResourceEntry* fHead;
185 GrResourceEntry* fTail;
186};
187
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000188/**
189 * Cache of GrResource objects.
190 *
191 * These have a corresponding GrResourceKey, built from 128bits identifying the
192 * resource.
193 *
194 * The cache stores the entries in a double-linked list, which is its LRU.
195 * When an entry is "locked" (i.e. given to the caller), it is moved to the
196 * head of the list. If/when we must purge some of the entries, we walk the
197 * list backwards from the tail, since those are the least recently used.
198 *
199 * For fast searches, we maintain a sorted array (based on the GrResourceKey)
200 * which we can bsearch. When a new entry is added, it is inserted into this
201 * array.
202 *
203 * For even faster searches, a hash is computed from the Key. If there is
204 * a collision between two keys with the same hash, we fall back on the
205 * bsearch, and update the hash to reflect the most recent Key requested.
206 */
207class GrResourceCache {
208public:
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000209 GrResourceCache(int maxCount, size_t maxBytes);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000210 ~GrResourceCache();
211
212 /**
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000213 * Return the current resource cache limits.
214 *
215 * @param maxResource If non-null, returns maximum number of resources
216 * that can be held in the cache.
217 * @param maxBytes If non-null, returns maximum number of bytes of
218 * gpu memory that can be held in the cache.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000219 */
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000220 void getLimits(int* maxResources, size_t* maxBytes) const;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000221
222 /**
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000223 * Specify the resource cache limits. If the current cache exceeds either
224 * of these, it will be purged (LRU) to keep the cache within these limits.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000225 *
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000226 * @param maxResources The maximum number of resources that can be held in
227 * the cache.
228 * @param maxBytes The maximum number of bytes of resource memory that
229 * can be held in the cache.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000230 */
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000231 void setLimits(int maxResource, size_t maxResourceBytes);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000232
233 /**
twiz@google.com05e70242012-01-27 19:12:00 +0000234 * Returns the number of bytes consumed by cached resources.
235 */
236 size_t getCachedResourceBytes() const { return fEntryBytes; }
237
238 /**
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000239 * Controls whether locks should be nestable or not.
240 */
241 enum LockType {
242 kNested_LockType,
243 kSingle_LockType,
244 };
245
246 /**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000247 * Search for an entry with the same Key. If found, "lock" it and return it.
248 * If not found, return null.
249 */
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000250 GrResource* findAndLock(const GrResourceKey&, LockType style);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000251
252 /**
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000253 * Create a new cache entry, based on the provided key and resource, and
254 * return it.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000255 *
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000256 * Ownership of the resource is transferred to the resource cache,
257 * which will unref() it when it is purged or deleted.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000258 */
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000259 void createAndLock(const GrResourceKey&, GrResource*);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000260
261 /**
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000262 * Create a new cache entry, based on the provided key and resource.
263 *
264 * Ownership of the resource is transferred to the resource cache,
265 * which will unref() it when it is purged or deleted.
266 *
267 * Currently this entry point is only intended for textures "detached"
268 * from an AutoScratchTexture object.
269 */
270 void attach(const GrResourceKey& key, GrResource* resource);
271
272 /**
bsalomon@google.comfb309512011-11-30 14:13:48 +0000273 * Determines if the cache contains an entry matching a key. If a matching
274 * entry exists but was detached then it will not be found.
275 */
276 bool hasKey(const GrResourceKey& key) const;
277
278 /**
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000279 * Hide 'entry' so that future searches will not find it. Such
280 * hidden entries will not be purged. The entry still counts against
281 * the cache's budget and should be made non-exclusive when exclusive access
282 * is no longer needed.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000283 */
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000284 void makeExclusive(GrResourceEntry* entry);
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000285
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000286 /**
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000287 * Restore 'entry' so that it can be found by future searches. 'entry'
288 * will also be purgeable (provided its lock count is now 0.)
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000289 */
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000290 void makeNonExclusive(GrResourceEntry* entry);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000291
292 /**
293 * When done with an entry, call unlock(entry) on it, which returns it to
294 * a purgable state.
295 */
296 void unlock(GrResourceEntry*);
297
298 void removeAll();
299
300#if GR_DEBUG
301 void validate() const;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000302 void printStats() const;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000303#else
304 void validate() const {}
305#endif
306
307private:
308 void internalDetach(GrResourceEntry*, bool);
309 void attachToHead(GrResourceEntry*, bool);
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000310 void purgeAsNeeded();
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000311
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000312 void removeInvalidResource(GrResourceEntry* entry);
313
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000314 class Key;
315 GrTHashTable<GrResourceEntry, Key, 8> fCache;
316
317 // manage the dlink list
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000318 GrDLinkedList fList;
319
320#if GR_DEBUG
321 // These objects cannot be returned by a search
322 GrDLinkedList fExclusiveList;
323#endif
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000324
325 // our budget, used in purgeAsNeeded()
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000326 int fMaxCount;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000327 size_t fMaxBytes;
328
329 // our current stats, related to our budget
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000330#if GR_DEBUG
331 int fHighWaterEntryCount;
332 int fHighWaterUnlockedEntryCount;
333 size_t fHighWaterEntryBytes;
334 int fHighWaterClientDetachedCount;
335 size_t fHighWaterClientDetachedBytes;
336#endif
337
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000338 int fEntryCount;
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000339 int fUnlockedEntryCount;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000340 size_t fEntryBytes;
341 int fClientDetachedCount;
342 size_t fClientDetachedBytes;
robertphillips@google.com386319e2012-06-27 14:59:18 +0000343
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000344 // prevents recursive purging
345 bool fPurging;
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000346
347 GrResourceEntry* create(const GrResourceKey& key,
348 GrResource* resource,
robertphillips@google.com386319e2012-06-27 14:59:18 +0000349 bool lock,
350 bool clientReattach);
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000351
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000352};
353
354///////////////////////////////////////////////////////////////////////////////
355
356#if GR_DEBUG
357 class GrAutoResourceCacheValidate {
358 public:
359 GrAutoResourceCacheValidate(GrResourceCache* cache) : fCache(cache) {
360 cache->validate();
361 }
362 ~GrAutoResourceCacheValidate() {
363 fCache->validate();
364 }
365 private:
366 GrResourceCache* fCache;
367 };
368#else
369 class GrAutoResourceCacheValidate {
370 public:
371 GrAutoResourceCacheValidate(GrResourceCache*) {}
372 };
373#endif
374
375#endif