blob: 2f5dfaf157d59bf1e4ee0e6b333a304866a717da [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 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.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
bsalomon@google.com50398bf2011-07-26 20:45:30 +000011#include "GrResourceCache.h"
12#include "GrResource.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000013
bsalomon@google.com50398bf2011-07-26 20:45:30 +000014GrResourceEntry::GrResourceEntry(const GrResourceKey& key, GrResource* resource)
15 : fKey(key), fResource(resource) {
reed@google.comac10a2d2010-12-22 21:39:39 +000016 fLockCount = 0;
17 fPrev = fNext = NULL;
18
bsalomon@google.com50398bf2011-07-26 20:45:30 +000019 // we assume ownership of the resource, and will unref it when we die
20 GrAssert(resource);
reed@google.comac10a2d2010-12-22 21:39:39 +000021}
22
bsalomon@google.com50398bf2011-07-26 20:45:30 +000023GrResourceEntry::~GrResourceEntry() {
24 fResource->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +000025}
26
27#if GR_DEBUG
bsalomon@google.com50398bf2011-07-26 20:45:30 +000028void GrResourceEntry::validate() const {
reed@google.comac10a2d2010-12-22 21:39:39 +000029 GrAssert(fLockCount >= 0);
bsalomon@google.com50398bf2011-07-26 20:45:30 +000030 GrAssert(fResource);
31 fResource->validate();
reed@google.comac10a2d2010-12-22 21:39:39 +000032}
33#endif
34
35///////////////////////////////////////////////////////////////////////////////
36
bsalomon@google.com50398bf2011-07-26 20:45:30 +000037GrResourceCache::GrResourceCache(int maxCount, size_t maxBytes) :
reed@google.comac10a2d2010-12-22 21:39:39 +000038 fMaxCount(maxCount),
39 fMaxBytes(maxBytes) {
40 fEntryCount = 0;
bsalomon@google.coma5a1da82011-08-05 14:02:41 +000041 fUnlockedEntryCount = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +000042 fEntryBytes = 0;
43 fClientDetachedCount = 0;
44 fClientDetachedBytes = 0;
45
46 fHead = fTail = NULL;
bsalomon@google.coma5a1da82011-08-05 14:02:41 +000047 fPurging = false;
reed@google.comac10a2d2010-12-22 21:39:39 +000048}
49
bsalomon@google.com50398bf2011-07-26 20:45:30 +000050GrResourceCache::~GrResourceCache() {
51 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +000052
bsalomon@google.com8fe72472011-03-30 21:26:44 +000053 this->removeAll();
reed@google.comac10a2d2010-12-22 21:39:39 +000054}
55
bsalomon@google.com50398bf2011-07-26 20:45:30 +000056void GrResourceCache::getLimits(int* maxResources, size_t* maxResourceBytes) const{
57 if (maxResources) {
58 *maxResources = fMaxCount;
reed@google.com01804b42011-01-18 21:50:41 +000059 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +000060 if (maxResourceBytes) {
61 *maxResourceBytes = fMaxBytes;
reed@google.com01804b42011-01-18 21:50:41 +000062 }
63}
64
bsalomon@google.com50398bf2011-07-26 20:45:30 +000065void GrResourceCache::setLimits(int maxResources, size_t maxResourceBytes) {
66 bool smaller = (maxResources < fMaxCount) || (maxResourceBytes < fMaxBytes);
reed@google.com01804b42011-01-18 21:50:41 +000067
bsalomon@google.com50398bf2011-07-26 20:45:30 +000068 fMaxCount = maxResources;
69 fMaxBytes = maxResourceBytes;
reed@google.com01804b42011-01-18 21:50:41 +000070
71 if (smaller) {
72 this->purgeAsNeeded();
73 }
74}
75
bsalomon@google.com50398bf2011-07-26 20:45:30 +000076void GrResourceCache::internalDetach(GrResourceEntry* entry,
reed@google.comac10a2d2010-12-22 21:39:39 +000077 bool clientDetach) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000078 GrResourceEntry* prev = entry->fPrev;
79 GrResourceEntry* next = entry->fNext;
reed@google.comac10a2d2010-12-22 21:39:39 +000080
81 if (prev) {
82 prev->fNext = next;
83 } else {
84 fHead = next;
85 }
86 if (next) {
87 next->fPrev = prev;
88 } else {
89 fTail = prev;
90 }
bsalomon@google.coma5a1da82011-08-05 14:02:41 +000091 if (!entry->isLocked()) {
92 --fUnlockedEntryCount;
93 }
reed@google.comac10a2d2010-12-22 21:39:39 +000094
95 // update our stats
96 if (clientDetach) {
97 fClientDetachedCount += 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000098 fClientDetachedBytes += entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +000099 } else {
100 fEntryCount -= 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000101 fEntryBytes -= entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000102 }
103}
104
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000105void GrResourceCache::attachToHead(GrResourceEntry* entry,
reed@google.comac10a2d2010-12-22 21:39:39 +0000106 bool clientReattach) {
107 entry->fPrev = NULL;
108 entry->fNext = fHead;
109 if (fHead) {
110 fHead->fPrev = entry;
111 }
112 fHead = entry;
113 if (NULL == fTail) {
114 fTail = entry;
115 }
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000116 if (!entry->isLocked()) {
117 ++fUnlockedEntryCount;
118 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000119
120 // update our stats
121 if (clientReattach) {
122 fClientDetachedCount -= 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000123 fClientDetachedBytes -= entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000124 } else {
125 fEntryCount += 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000126 fEntryBytes += entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000127 }
128}
129
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000130class GrResourceCache::Key {
131 typedef GrResourceEntry T;
reed@google.comac10a2d2010-12-22 21:39:39 +0000132
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000133 const GrResourceKey& fKey;
reed@google.comac10a2d2010-12-22 21:39:39 +0000134public:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000135 Key(const GrResourceKey& key) : fKey(key) {}
reed@google.comac10a2d2010-12-22 21:39:39 +0000136
137 uint32_t getHash() const { return fKey.hashIndex(); }
reed@google.com01804b42011-01-18 21:50:41 +0000138
reed@google.comac10a2d2010-12-22 21:39:39 +0000139 static bool LT(const T& entry, const Key& key) {
140 return entry.key() < key.fKey;
141 }
142 static bool EQ(const T& entry, const Key& key) {
143 return entry.key() == key.fKey;
144 }
145#if GR_DEBUG
146 static uint32_t GetHash(const T& entry) {
147 return entry.key().hashIndex();
148 }
149 static bool LT(const T& a, const T& b) {
150 return a.key() < b.key();
151 }
152 static bool EQ(const T& a, const T& b) {
153 return a.key() == b.key();
154 }
155#endif
156};
157
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000158GrResourceEntry* GrResourceCache::findAndLock(const GrResourceKey& key) {
159 GrAutoResourceCacheValidate atcv(this);
reed@google.comac10a2d2010-12-22 21:39:39 +0000160
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000161 GrResourceEntry* entry = fCache.find(key);
reed@google.comac10a2d2010-12-22 21:39:39 +0000162 if (entry) {
163 this->internalDetach(entry, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000164 // mark the entry as "busy" so it doesn't get purged
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000165 // do this between detach and attach for locked count tracking
reed@google.comac10a2d2010-12-22 21:39:39 +0000166 entry->lock();
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000167 this->attachToHead(entry, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000168 }
169 return entry;
170}
171
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000172GrResourceEntry* GrResourceCache::createAndLock(const GrResourceKey& key,
173 GrResource* resource) {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000174 // we don't expect to create new resources during a purge. In theory
175 // this could cause purgeAsNeeded() into an infinite loop (e.g.
176 // each resource destroyed creates and locks 2 resources and
177 // unlocks 1 thereby causing a new purge).
178 GrAssert(!fPurging);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000179 GrAutoResourceCacheValidate atcv(this);
reed@google.comac10a2d2010-12-22 21:39:39 +0000180
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000181 GrResourceEntry* entry = new GrResourceEntry(key, resource);
reed@google.comac10a2d2010-12-22 21:39:39 +0000182
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000183 // mark the entry as "busy" so it doesn't get purged
184 // do this before attach for locked count tracking
185 entry->lock();
186
reed@google.comac10a2d2010-12-22 21:39:39 +0000187 this->attachToHead(entry, false);
188 fCache.insert(key, entry);
189
190#if GR_DUMP_TEXTURE_UPLOAD
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000191 GrPrintf("--- add resource to cache %p, count=%d bytes= %d %d\n",
192 entry, fEntryCount, resource->sizeInBytes(), fEntryBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000193#endif
194
reed@google.comac10a2d2010-12-22 21:39:39 +0000195 this->purgeAsNeeded();
196 return entry;
197}
198
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000199void GrResourceCache::detach(GrResourceEntry* entry) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000200 internalDetach(entry, true);
201 fCache.remove(entry->fKey, entry);
202}
203
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000204void GrResourceCache::reattachAndUnlock(GrResourceEntry* entry) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000205 attachToHead(entry, true);
206 fCache.insert(entry->key(), entry);
207 unlock(entry);
208}
209
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000210void GrResourceCache::unlock(GrResourceEntry* entry) {
211 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +0000212
reed@google.comac10a2d2010-12-22 21:39:39 +0000213 GrAssert(entry);
214 GrAssert(entry->isLocked());
215 GrAssert(fCache.find(entry->key()));
216
217 entry->unlock();
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000218 if (!entry->isLocked()) {
219 ++fUnlockedEntryCount;
220 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000221 this->purgeAsNeeded();
222}
223
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000224/**
225 * Destroying a resource may potentially trigger the unlock of additional
226 * resources which in turn will trigger a nested purge. We block the nested
227 * purge using the fPurging variable. However, the initial purge will keep
228 * looping until either all resources in the cache are unlocked or we've met
229 * the budget. There is an assertion in createAndLock to check against a
230 * resource's destructor inserting new resources into the cache. If these
231 * new resources were unlocked before purgeAsNeeded completed it could
232 * potentially make purgeAsNeeded loop infinitely.
233 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000234void GrResourceCache::purgeAsNeeded() {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000235 if (!fPurging) {
236 fPurging = true;
237 bool withinBudget = false;
238 do {
239 GrAutoResourceCacheValidate atcv(this);
240 GrResourceEntry* entry = fTail;
241 while (entry && fUnlockedEntryCount) {
242 if (fEntryCount <= fMaxCount && fEntryBytes <= fMaxBytes) {
243 withinBudget = true;
244 break;
245 }
reed@google.com01804b42011-01-18 21:50:41 +0000246
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000247 GrResourceEntry* prev = entry->fPrev;
248 if (!entry->isLocked()) {
249 // remove from our cache
250 fCache.remove(entry->fKey, entry);
reed@google.comac10a2d2010-12-22 21:39:39 +0000251
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000252 // remove from our llist
253 this->internalDetach(entry, false);
reed@google.com01804b42011-01-18 21:50:41 +0000254
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000255 #if GR_DUMP_TEXTURE_UPLOAD
256 GrPrintf("--- ~resource from cache %p [%d %d]\n",
257 entry->resource(),
258 entry->resource()->width(),
259 entry->resource()->height());
260 #endif
261 delete entry;
262 }
263 entry = prev;
264 }
265 } while (!withinBudget && fUnlockedEntryCount);
266 fPurging = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000267 }
268}
269
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000270void GrResourceCache::removeAll() {
reed@google.comac10a2d2010-12-22 21:39:39 +0000271 GrAssert(!fClientDetachedCount);
272 GrAssert(!fClientDetachedBytes);
reed@google.com01804b42011-01-18 21:50:41 +0000273
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000274 GrResourceEntry* entry = fHead;
reed@google.comac10a2d2010-12-22 21:39:39 +0000275 while (entry) {
276 GrAssert(!entry->isLocked());
277
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000278 GrResourceEntry* next = entry->fNext;
reed@google.comac10a2d2010-12-22 21:39:39 +0000279 delete entry;
280 entry = next;
281 }
282
283 fCache.removeAll();
284 fHead = fTail = NULL;
285 fEntryCount = 0;
286 fEntryBytes = 0;
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000287 fUnlockedEntryCount = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000288}
289
290///////////////////////////////////////////////////////////////////////////////
291
292#if GR_DEBUG
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000293static int countMatches(const GrResourceEntry* head, const GrResourceEntry* target) {
294 const GrResourceEntry* entry = head;
reed@google.comac10a2d2010-12-22 21:39:39 +0000295 int count = 0;
296 while (entry) {
297 if (target == entry) {
298 count += 1;
299 }
300 entry = entry->next();
301 }
302 return count;
303}
304
reed@google.comb89a6432011-02-07 13:20:30 +0000305#if GR_DEBUG
306static bool both_zero_or_nonzero(int count, size_t bytes) {
307 return (count == 0 && bytes == 0) || (count > 0 && bytes > 0);
308}
309#endif
310
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000311void GrResourceCache::validate() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000312 GrAssert(!fHead == !fTail);
reed@google.comb89a6432011-02-07 13:20:30 +0000313 GrAssert(both_zero_or_nonzero(fEntryCount, fEntryBytes));
314 GrAssert(both_zero_or_nonzero(fClientDetachedCount, fClientDetachedBytes));
reed@google.comac10a2d2010-12-22 21:39:39 +0000315 GrAssert(fClientDetachedBytes <= fEntryBytes);
316 GrAssert(fClientDetachedCount <= fEntryCount);
317 GrAssert((fEntryCount - fClientDetachedCount) == fCache.count());
reed@google.com01804b42011-01-18 21:50:41 +0000318
reed@google.comac10a2d2010-12-22 21:39:39 +0000319 fCache.validate();
320
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000321 GrResourceEntry* entry = fHead;
reed@google.comac10a2d2010-12-22 21:39:39 +0000322 int count = 0;
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000323 int unlockCount = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000324 size_t bytes = 0;
325 while (entry) {
326 entry->validate();
327 GrAssert(fCache.find(entry->key()));
328 count += 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000329 bytes += entry->resource()->sizeInBytes();
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000330 if (!entry->isLocked()) {
331 unlockCount += 1;
332 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000333 entry = entry->fNext;
334 }
335 GrAssert(count == fEntryCount - fClientDetachedCount);
336 GrAssert(bytes == fEntryBytes - fClientDetachedBytes);
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000337 GrAssert(unlockCount == fUnlockedEntryCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000338
339 count = 0;
340 for (entry = fTail; entry; entry = entry->fPrev) {
341 count += 1;
342 }
343 GrAssert(count == fEntryCount - fClientDetachedCount);
344
345 for (int i = 0; i < count; i++) {
346 int matches = countMatches(fHead, fCache.getArray()[i]);
347 GrAssert(1 == matches);
348 }
349}
350#endif
351
352