blob: e1207a204bca120da6268c35314ef1bed39a33f7 [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"
bsalomon@google.com42619d82012-12-03 14:54:59 +000017#include "SkTInternalLList.h"
bsalomon@google.com50398bf2011-07-26 20:45:30 +000018
19class GrResource;
20
bsalomon@google.com0b6ad222012-12-20 14:23:26 +000021// 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 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +000038class GrResourceKey {
39public:
40 enum {
41 kHashBits = 7,
42 kHashCount = 1 << kHashBits,
43 kHashMask = kHashCount - 1
44 };
45
bsalomon@google.com0b6ad222012-12-20 14:23:26 +000046 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();
bsalomon@google.com50398bf2011-07-26 20:45:30 +000052 }
53
bsalomon@google.com0b6ad222012-12-20 14:23:26 +000054 GrResourceKey(uint32_t v[4]) {
55 memcpy(fP, v, 4 * sizeof(uint32_t));
56 this->computeHashIndex();
57 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +000058
59 GrResourceKey(const GrResourceKey& src) {
bsalomon@google.com0b6ad222012-12-20 14:23:26 +000060 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;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000066 }
67
68 //!< returns hash value [0..kHashMask] for the key
bsalomon@google.com0b6ad222012-12-20 14:23:26 +000069 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));
bsalomon@google.com50398bf2011-07-26 20:45:30 +000074 }
75
bsalomon@google.com0b6ad222012-12-20 14:23:26 +000076 friend bool operator!=(const GrResourceKey& a, const GrResourceKey& b) {
77 GR_DEBUGASSERT(-1 != a.fHashIndex && -1 != b.fHashIndex);
78 return !(a == b);
bsalomon@google.com50398bf2011-07-26 20:45:30 +000079 }
80
bsalomon@google.com0b6ad222012-12-20 14:23:26 +000081 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];
bsalomon@google.com50398bf2011-07-26 20:45:30 +000086 }
87
bsalomon@google.com0b6ad222012-12-20 14:23:26 +000088 uint32_t getValue32(int i) const {
89 GrAssert(i >=0 && i < 4);
90 return fP[i];
bsalomon@google.com50398bf2011-07-26 20:45:30 +000091 }
92private:
93
bsalomon@google.com0b6ad222012-12-20 14:23:26 +000094 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);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000102 }
103
bsalomon@google.com0b6ad222012-12-20 14:23:26 +0000104 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 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000112
bsalomon@google.com0b6ad222012-12-20 14:23:26 +0000113 uint32_t fP[4];
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000114
bsalomon@google.com0b6ad222012-12-20 14:23:26 +0000115 // this is computed from the fP... fields
116 int fHashIndex;
117
118 friend class GrContext;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000119};
120
bsalomon@google.com0b6ad222012-12-20 14:23:26 +0000121
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000122///////////////////////////////////////////////////////////////////////////////
123
124class GrResourceEntry {
125public:
126 GrResource* resource() const { return fResource; }
127 const GrResourceKey& key() const { return fKey; }
128
129#if GR_DEBUG
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000130 void validate() const;
131#else
132 void validate() const {}
133#endif
134
135private:
136 GrResourceEntry(const GrResourceKey& key, GrResource* resource);
137 ~GrResourceEntry();
138
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000139 GrResourceKey fKey;
140 GrResource* fResource;
141
bsalomon@google.com42619d82012-12-03 14:54:59 +0000142 // we're a linked list
143 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrResourceEntry);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000144
145 friend class GrResourceCache;
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000146 friend class GrDLinkedList;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000147};
148
149///////////////////////////////////////////////////////////////////////////////
150
151#include "GrTHashCache.h"
152
153/**
154 * Cache of GrResource objects.
155 *
156 * These have a corresponding GrResourceKey, built from 128bits identifying the
157 * resource.
158 *
159 * The cache stores the entries in a double-linked list, which is its LRU.
160 * When an entry is "locked" (i.e. given to the caller), it is moved to the
161 * head of the list. If/when we must purge some of the entries, we walk the
162 * list backwards from the tail, since those are the least recently used.
163 *
164 * For fast searches, we maintain a sorted array (based on the GrResourceKey)
165 * which we can bsearch. When a new entry is added, it is inserted into this
166 * array.
167 *
168 * For even faster searches, a hash is computed from the Key. If there is
169 * a collision between two keys with the same hash, we fall back on the
170 * bsearch, and update the hash to reflect the most recent Key requested.
171 */
172class GrResourceCache {
173public:
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000174 GrResourceCache(int maxCount, size_t maxBytes);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000175 ~GrResourceCache();
176
177 /**
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000178 * Return the current resource cache limits.
179 *
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000180 * @param maxResource If non-null, returns maximum number of resources
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000181 * that can be held in the cache.
182 * @param maxBytes If non-null, returns maximum number of bytes of
183 * gpu memory that can be held in the cache.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000184 */
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000185 void getLimits(int* maxResources, size_t* maxBytes) const;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000186
187 /**
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000188 * Specify the resource cache limits. If the current cache exceeds either
189 * of these, it will be purged (LRU) to keep the cache within these limits.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000190 *
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000191 * @param maxResources The maximum number of resources that can be held in
192 * the cache.
193 * @param maxBytes The maximum number of bytes of resource memory that
194 * can be held in the cache.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000195 */
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000196 void setLimits(int maxResource, size_t maxResourceBytes);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000197
198 /**
twiz@google.com05e70242012-01-27 19:12:00 +0000199 * Returns the number of bytes consumed by cached resources.
200 */
201 size_t getCachedResourceBytes() const { return fEntryBytes; }
202
robertphillips@google.com209a1142012-10-31 12:25:21 +0000203 // For a found or added resource to be completely exclusive to the caller
204 // both the kNoOtherOwners and kHide flags need to be specified
205 enum OwnershipFlags {
206 kNoOtherOwners_OwnershipFlag = 0x1, // found/added resource has no other owners
207 kHide_OwnershipFlag = 0x2 // found/added resource is hidden from future 'find's
208 };
209
twiz@google.com05e70242012-01-27 19:12:00 +0000210 /**
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000211 * Search for an entry with the same Key. If found, return it.
212 * If not found, return null.
skia.committer@gmail.comf3dc1992012-11-01 02:01:27 +0000213 * If ownershipFlags includes kNoOtherOwners and a resource is returned
robertphillips@google.com209a1142012-10-31 12:25:21 +0000214 * then that resource has no other refs to it.
215 * If ownershipFlags includes kHide and a resource is returned then that
216 * resource will not be returned from future 'find' calls until it is
217 * 'freed' (and recycled) or makeNonExclusive is called.
218 * For a resource to be completely exclusive to a caller both kNoOtherOwners
219 * and kHide must be specified.
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000220 */
skia.committer@gmail.comf3dc1992012-11-01 02:01:27 +0000221 GrResource* find(const GrResourceKey& key,
robertphillips@google.com209a1142012-10-31 12:25:21 +0000222 uint32_t ownershipFlags = 0);
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000223
224 /**
skia.committer@gmail.comf3dc1992012-11-01 02:01:27 +0000225 * Add the new resource to the cache (by creating a new cache entry based
226 * on the provided key and resource).
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000227 *
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000228 * Ownership of the resource is transferred to the resource cache,
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000229 * which will unref() it when it is purged or deleted.
robertphillips@google.com209a1142012-10-31 12:25:21 +0000230 *
231 * If ownershipFlags includes kHide, subsequent calls to 'find' will not
skia.committer@gmail.comf3dc1992012-11-01 02:01:27 +0000232 * return 'resource' until it is 'freed' (and recycled) or makeNonExclusive
robertphillips@google.com209a1142012-10-31 12:25:21 +0000233 * is called.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000234 */
skia.committer@gmail.comf3dc1992012-11-01 02:01:27 +0000235 void addResource(const GrResourceKey& key,
robertphillips@google.com209a1142012-10-31 12:25:21 +0000236 GrResource* resource,
237 uint32_t ownershipFlags = 0);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000238
239 /**
bsalomon@google.comfb309512011-11-30 14:13:48 +0000240 * Determines if the cache contains an entry matching a key. If a matching
241 * entry exists but was detached then it will not be found.
242 */
243 bool hasKey(const GrResourceKey& key) const;
244
245 /**
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000246 * Hide 'entry' so that future searches will not find it. Such
247 * hidden entries will not be purged. The entry still counts against
248 * the cache's budget and should be made non-exclusive when exclusive access
249 * is no longer needed.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000250 */
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000251 void makeExclusive(GrResourceEntry* entry);
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000252
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000253 /**
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000254 * Restore 'entry' so that it can be found by future searches. 'entry'
255 * will also be purgeable (provided its lock count is now 0.)
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000256 */
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000257 void makeNonExclusive(GrResourceEntry* entry);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000258
259 /**
bsalomon@google.coma2921122012-08-28 12:34:17 +0000260 * Removes every resource in the cache that isn't locked.
261 */
262 void purgeAllUnlocked();
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000263
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000264 /**
265 * Allow cache to purge unused resources to obey resource limitations
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000266 * Note: this entry point will be hidden (again) once totally ref-driven
267 * cache maintenance is implemented
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000268 */
269 void purgeAsNeeded();
270
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000271#if GR_DEBUG
272 void validate() const;
273#else
274 void validate() const {}
275#endif
276
robertphillips@google.com59552022012-08-31 13:07:37 +0000277#if GR_CACHE_STATS
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000278 void printStats();
robertphillips@google.com59552022012-08-31 13:07:37 +0000279#endif
280
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000281private:
robertphillips@google.com209a1142012-10-31 12:25:21 +0000282 enum BudgetBehaviors {
283 kAccountFor_BudgetBehavior,
284 kIgnore_BudgetBehavior
285 };
286
287 void internalDetach(GrResourceEntry*, BudgetBehaviors behavior = kAccountFor_BudgetBehavior);
288 void attachToHead(GrResourceEntry*, BudgetBehaviors behavior = kAccountFor_BudgetBehavior);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000289
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000290 void removeInvalidResource(GrResourceEntry* entry);
291
bsalomon@google.com0b6ad222012-12-20 14:23:26 +0000292 class Key;
293 GrTHashTable<GrResourceEntry, Key, 8> fCache;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000294
bsalomon@google.com42619d82012-12-03 14:54:59 +0000295 // We're an internal doubly linked list
296 typedef SkTInternalLList<GrResourceEntry> EntryList;
bsalomon@google.coma2921122012-08-28 12:34:17 +0000297 EntryList fList;
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000298
299#if GR_DEBUG
300 // These objects cannot be returned by a search
bsalomon@google.coma2921122012-08-28 12:34:17 +0000301 EntryList fExclusiveList;
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000302#endif
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000303
304 // our budget, used in purgeAsNeeded()
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000305 int fMaxCount;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000306 size_t fMaxBytes;
307
308 // our current stats, related to our budget
robertphillips@google.com59552022012-08-31 13:07:37 +0000309#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000310 int fHighWaterEntryCount;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000311 size_t fHighWaterEntryBytes;
312 int fHighWaterClientDetachedCount;
313 size_t fHighWaterClientDetachedBytes;
314#endif
315
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000316 int fEntryCount;
317 size_t fEntryBytes;
318 int fClientDetachedCount;
319 size_t fClientDetachedBytes;
robertphillips@google.com386319e2012-06-27 14:59:18 +0000320
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000321 // prevents recursive purging
322 bool fPurging;
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000323
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000324#if GR_DEBUG
bsalomon@google.com42619d82012-12-03 14:54:59 +0000325 static size_t countBytes(const SkTInternalLList<GrResourceEntry>& list);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000326#endif
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