blob: bfa528d30591ba5d1fc534a2f7937a6737c1f89b [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
robertphillips@google.com59552022012-08-31 13:07:37 +000014#include "GrConfig.h"
bsalomon@google.com50398bf2011-07-26 20:45:30 +000015#include "GrTypes.h"
16#include "GrTHashCache.h"
robertphillips@google.com2ea0a232012-08-23 11:13:48 +000017#include "SkTDLinkedList.h"
bsalomon@google.com50398bf2011-07-26 20:45:30 +000018
19class GrResource;
20
21// return true if a<b, or false if b<a
22//
23#define RET_IF_LT_OR_GT(a, b) \
24 do { \
25 if ((a) < (b)) { \
26 return true; \
27 } \
28 if ((b) < (a)) { \
29 return false; \
30 } \
31 } while (0)
32
33/**
34 * Helper class for GrResourceCache, the Key is used to identify src data for
35 * a resource. It is identified by 2 32bit data fields which can hold any
36 * data (uninterpreted by the cache) and a width/height.
37 */
38class GrResourceKey {
39public:
40 enum {
41 kHashBits = 7,
42 kHashCount = 1 << kHashBits,
43 kHashMask = kHashCount - 1
44 };
45
46 GrResourceKey(uint32_t p0, uint32_t p1, uint32_t p2, uint32_t p3) {
47 fP[0] = p0;
48 fP[1] = p1;
49 fP[2] = p2;
50 fP[3] = p3;
51 this->computeHashIndex();
52 }
53
54 GrResourceKey(uint32_t v[4]) {
55 memcpy(fP, v, 4 * sizeof(uint32_t));
56 this->computeHashIndex();
57 }
58
59 GrResourceKey(const GrResourceKey& src) {
60 memcpy(fP, src.fP, 4 * sizeof(uint32_t));
61#if GR_DEBUG
62 this->computeHashIndex();
63 GrAssert(fHashIndex == src.fHashIndex);
64#endif
65 fHashIndex = src.fHashIndex;
66 }
67
68 //!< returns hash value [0..kHashMask] for the key
69 int hashIndex() const { return fHashIndex; }
70
71 friend bool operator==(const GrResourceKey& a, const GrResourceKey& b) {
72 GR_DEBUGASSERT(-1 != a.fHashIndex && -1 != b.fHashIndex);
73 return 0 == memcmp(a.fP, b.fP, 4 * sizeof(uint32_t));
74 }
75
76 friend bool operator!=(const GrResourceKey& a, const GrResourceKey& b) {
77 GR_DEBUGASSERT(-1 != a.fHashIndex && -1 != b.fHashIndex);
78 return !(a == b);
79 }
80
81 friend bool operator<(const GrResourceKey& a, const GrResourceKey& b) {
82 RET_IF_LT_OR_GT(a.fP[0], b.fP[0]);
83 RET_IF_LT_OR_GT(a.fP[1], b.fP[1]);
84 RET_IF_LT_OR_GT(a.fP[2], b.fP[2]);
85 return a.fP[3] < b.fP[3];
86 }
87
88 uint32_t getValue32(int i) const {
89 GrAssert(i >=0 && i < 4);
90 return fP[i];
91 }
92private:
93
94 static uint32_t rol(uint32_t x) {
95 return (x >> 24) | (x << 8);
96 }
97 static uint32_t ror(uint32_t x) {
98 return (x >> 8) | (x << 24);
99 }
100 static uint32_t rohalf(uint32_t x) {
101 return (x >> 16) | (x << 16);
102 }
103
104 void computeHashIndex() {
105 uint32_t hash = fP[0] ^ rol(fP[1]) ^ ror(fP[2]) ^ rohalf(fP[3]);
106 // this way to mix and reduce hash to its index may have to change
107 // depending on how many bits we allocate to the index
108 hash ^= hash >> 16;
109 hash ^= hash >> 8;
110 fHashIndex = hash & kHashMask;
111 }
112
113 uint32_t fP[4];
114
115 // this is computed from the fP... fields
116 int fHashIndex;
117
118 friend class GrContext;
119};
120
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000121
122class GrCacheKey {
123public:
124 GrCacheKey(const GrTextureDesc& desc, const GrResourceKey& key)
125 : fDesc(desc)
126 , fKey(key) {
127 }
128
129 void set(const GrTextureDesc& desc, const GrResourceKey& key) {
130 fDesc = desc;
131 fKey = key;
132 }
133
134 const GrTextureDesc& desc() const { return fDesc; }
135 const GrResourceKey& key() const { return fKey; }
136
137protected:
138 GrTextureDesc fDesc;
139 GrResourceKey fKey;
140};
141
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000142///////////////////////////////////////////////////////////////////////////////
143
144class GrResourceEntry {
145public:
146 GrResource* resource() const { return fResource; }
147 const GrResourceKey& key() const { return fKey; }
148
149#if GR_DEBUG
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000150 void validate() const;
151#else
152 void validate() const {}
153#endif
154
155private:
156 GrResourceEntry(const GrResourceKey& key, GrResource* resource);
157 ~GrResourceEntry();
158
159 bool isLocked() const { return fLockCount != 0; }
160 void lock() { ++fLockCount; }
161 void unlock() {
162 GrAssert(fLockCount > 0);
163 --fLockCount;
164 }
165
166 GrResourceKey fKey;
167 GrResource* fResource;
168
169 // track if we're in use, used when we need to purge
170 // we only purge unlocked entries
171 int fLockCount;
172
173 // we're a dlinklist
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000174 SK_DEFINE_DLINKEDLIST_INTERFACE(GrResourceEntry);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000175
176 friend class GrResourceCache;
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000177 friend class GrDLinkedList;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000178};
179
180///////////////////////////////////////////////////////////////////////////////
181
182#include "GrTHashCache.h"
183
184/**
185 * Cache of GrResource objects.
186 *
187 * These have a corresponding GrResourceKey, built from 128bits identifying the
188 * resource.
189 *
190 * The cache stores the entries in a double-linked list, which is its LRU.
191 * When an entry is "locked" (i.e. given to the caller), it is moved to the
192 * head of the list. If/when we must purge some of the entries, we walk the
193 * list backwards from the tail, since those are the least recently used.
194 *
195 * For fast searches, we maintain a sorted array (based on the GrResourceKey)
196 * which we can bsearch. When a new entry is added, it is inserted into this
197 * array.
198 *
199 * For even faster searches, a hash is computed from the Key. If there is
200 * a collision between two keys with the same hash, we fall back on the
201 * bsearch, and update the hash to reflect the most recent Key requested.
202 */
203class GrResourceCache {
204public:
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000205 GrResourceCache(int maxCount, size_t maxBytes);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000206 ~GrResourceCache();
207
208 /**
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000209 * Return the current resource cache limits.
210 *
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000211 * @param maxResource If non-null, returns maximum number of resources
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000212 * that can be held in the cache.
213 * @param maxBytes If non-null, returns maximum number of bytes of
214 * gpu memory that can be held in the cache.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000215 */
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000216 void getLimits(int* maxResources, size_t* maxBytes) const;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000217
218 /**
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000219 * Specify the resource cache limits. If the current cache exceeds either
220 * of these, it will be purged (LRU) to keep the cache within these limits.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000221 *
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000222 * @param maxResources The maximum number of resources that can be held in
223 * the cache.
224 * @param maxBytes The maximum number of bytes of resource memory that
225 * can be held in the cache.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000226 */
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000227 void setLimits(int maxResource, size_t maxResourceBytes);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000228
229 /**
twiz@google.com05e70242012-01-27 19:12:00 +0000230 * Returns the number of bytes consumed by cached resources.
231 */
232 size_t getCachedResourceBytes() const { return fEntryBytes; }
233
234 /**
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000235 * Controls whether locks should be nestable or not.
236 */
237 enum LockType {
238 kNested_LockType,
239 kSingle_LockType,
240 };
241
242 /**
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000243 * Search for an entry with the same Key. If found, return it.
244 * If not found, return null.
245 */
246 GrResource* find(const GrResourceKey& key);
247
248 /**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000249 * Search for an entry with the same Key. If found, "lock" it and return it.
250 * If not found, return null.
251 */
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000252 GrResource* findAndLock(const GrResourceKey&, LockType style);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000253
254 /**
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000255 * Create a new cache entry, based on the provided key and resource, and
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000256 * return it.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000257 *
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000258 * Ownership of the resource is transferred to the resource cache,
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000259 * which will unref() it when it is purged or deleted.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000260 */
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000261 void createAndLock(const GrResourceKey&, GrResource*);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000262
263 /**
bsalomon@google.comfb309512011-11-30 14:13:48 +0000264 * Determines if the cache contains an entry matching a key. If a matching
265 * entry exists but was detached then it will not be found.
266 */
267 bool hasKey(const GrResourceKey& key) const;
268
269 /**
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000270 * Hide 'entry' so that future searches will not find it. Such
271 * hidden entries will not be purged. The entry still counts against
272 * the cache's budget and should be made non-exclusive when exclusive access
273 * is no longer needed.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000274 */
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000275 void makeExclusive(GrResourceEntry* entry);
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000276
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000277 /**
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000278 * Restore 'entry' so that it can be found by future searches. 'entry'
279 * will also be purgeable (provided its lock count is now 0.)
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000280 */
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000281 void makeNonExclusive(GrResourceEntry* entry);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000282
283 /**
bsalomon@google.coma2921122012-08-28 12:34:17 +0000284 * When done with an entry, call unlock(entry) on it, which returns it to
285 * a purgable state.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000286 */
287 void unlock(GrResourceEntry*);
288
bsalomon@google.coma2921122012-08-28 12:34:17 +0000289 /**
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000290 * Make a resource un-purgeable.
291 */
292 void lock(GrResourceEntry* entry);
293
294 /**
bsalomon@google.coma2921122012-08-28 12:34:17 +0000295 * Removes every resource in the cache that isn't locked.
296 */
297 void purgeAllUnlocked();
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000298
299#if GR_DEBUG
300 void validate() const;
301#else
302 void validate() const {}
303#endif
304
robertphillips@google.com59552022012-08-31 13:07:37 +0000305#if GR_CACHE_STATS
306 void printStats() const;
307#endif
308
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000309private:
310 void internalDetach(GrResourceEntry*, bool);
311 void attachToHead(GrResourceEntry*, bool);
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000312 void purgeAsNeeded();
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000313
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000314 void removeInvalidResource(GrResourceEntry* entry);
315
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000316 class Key;
317 GrTHashTable<GrResourceEntry, Key, 8> fCache;
318
319 // manage the dlink list
bsalomon@google.coma2921122012-08-28 12:34:17 +0000320 typedef SkTDLinkedList<GrResourceEntry> EntryList;
321 EntryList fList;
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000322
323#if GR_DEBUG
324 // These objects cannot be returned by a search
bsalomon@google.coma2921122012-08-28 12:34:17 +0000325 EntryList fExclusiveList;
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000326#endif
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000327
328 // our budget, used in purgeAsNeeded()
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000329 int fMaxCount;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000330 size_t fMaxBytes;
331
332 // our current stats, related to our budget
robertphillips@google.com59552022012-08-31 13:07:37 +0000333#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000334 int fHighWaterEntryCount;
335 int fHighWaterUnlockedEntryCount;
336 size_t fHighWaterEntryBytes;
337 int fHighWaterClientDetachedCount;
338 size_t fHighWaterClientDetachedBytes;
339#endif
340
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000341 int fEntryCount;
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000342 int fUnlockedEntryCount;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000343 size_t fEntryBytes;
344 int fClientDetachedCount;
345 size_t fClientDetachedBytes;
robertphillips@google.com386319e2012-06-27 14:59:18 +0000346
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000347 // prevents recursive purging
348 bool fPurging;
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000349
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000350 void create(const GrResourceKey& key, GrResource* resource);
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000351
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000352#if GR_DEBUG
robertphillips@google.coma5c43a52012-08-23 11:24:02 +0000353 static size_t countBytes(const SkTDLinkedList<GrResourceEntry>& list);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000354#endif
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000355};
356
357///////////////////////////////////////////////////////////////////////////////
358
359#if GR_DEBUG
360 class GrAutoResourceCacheValidate {
361 public:
362 GrAutoResourceCacheValidate(GrResourceCache* cache) : fCache(cache) {
363 cache->validate();
364 }
365 ~GrAutoResourceCacheValidate() {
366 fCache->validate();
367 }
368 private:
369 GrResourceCache* fCache;
370 };
371#else
372 class GrAutoResourceCacheValidate {
373 public:
374 GrAutoResourceCacheValidate(GrResourceCache*) {}
375 };
376#endif
377
378#endif