blob: 2b95f261551ec96af2eec080db9b18b267e5197a [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.com558a75b2011-08-08 17:01:14 +0000158GrResourceEntry* GrResourceCache::findAndLock(const GrResourceKey& key,
159 LockType type) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000160 GrAutoResourceCacheValidate atcv(this);
reed@google.comac10a2d2010-12-22 21:39:39 +0000161
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000162 GrResourceEntry* entry = fCache.find(key);
reed@google.comac10a2d2010-12-22 21:39:39 +0000163 if (entry) {
164 this->internalDetach(entry, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000165 // mark the entry as "busy" so it doesn't get purged
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000166 // do this between detach and attach for locked count tracking
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000167 if (kNested_LockType == type || !entry->isLocked()) {
168 entry->lock();
169 }
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000170 this->attachToHead(entry, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000171 }
172 return entry;
173}
174
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000175GrResourceEntry* GrResourceCache::createAndLock(const GrResourceKey& key,
176 GrResource* resource) {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000177 // we don't expect to create new resources during a purge. In theory
178 // this could cause purgeAsNeeded() into an infinite loop (e.g.
179 // each resource destroyed creates and locks 2 resources and
180 // unlocks 1 thereby causing a new purge).
181 GrAssert(!fPurging);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000182 GrAutoResourceCacheValidate atcv(this);
reed@google.comac10a2d2010-12-22 21:39:39 +0000183
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000184 GrResourceEntry* entry = new GrResourceEntry(key, resource);
reed@google.comac10a2d2010-12-22 21:39:39 +0000185
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000186 // mark the entry as "busy" so it doesn't get purged
187 // do this before attach for locked count tracking
188 entry->lock();
189
reed@google.comac10a2d2010-12-22 21:39:39 +0000190 this->attachToHead(entry, false);
191 fCache.insert(key, entry);
192
193#if GR_DUMP_TEXTURE_UPLOAD
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000194 GrPrintf("--- add resource to cache %p, count=%d bytes= %d %d\n",
195 entry, fEntryCount, resource->sizeInBytes(), fEntryBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000196#endif
197
reed@google.comac10a2d2010-12-22 21:39:39 +0000198 this->purgeAsNeeded();
199 return entry;
200}
201
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000202void GrResourceCache::detach(GrResourceEntry* entry) {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000203 GrAutoResourceCacheValidate atcv(this);
reed@google.comac10a2d2010-12-22 21:39:39 +0000204 internalDetach(entry, true);
205 fCache.remove(entry->fKey, entry);
206}
207
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000208void GrResourceCache::reattachAndUnlock(GrResourceEntry* entry) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000209 attachToHead(entry, true);
210 fCache.insert(entry->key(), entry);
211 unlock(entry);
212}
213
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000214void GrResourceCache::unlock(GrResourceEntry* entry) {
215 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +0000216
reed@google.comac10a2d2010-12-22 21:39:39 +0000217 GrAssert(entry);
218 GrAssert(entry->isLocked());
219 GrAssert(fCache.find(entry->key()));
220
221 entry->unlock();
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000222 if (!entry->isLocked()) {
223 ++fUnlockedEntryCount;
224 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000225 this->purgeAsNeeded();
226}
227
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000228/**
229 * Destroying a resource may potentially trigger the unlock of additional
230 * resources which in turn will trigger a nested purge. We block the nested
231 * purge using the fPurging variable. However, the initial purge will keep
232 * looping until either all resources in the cache are unlocked or we've met
233 * the budget. There is an assertion in createAndLock to check against a
234 * resource's destructor inserting new resources into the cache. If these
235 * new resources were unlocked before purgeAsNeeded completed it could
236 * potentially make purgeAsNeeded loop infinitely.
237 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000238void GrResourceCache::purgeAsNeeded() {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000239 if (!fPurging) {
240 fPurging = true;
241 bool withinBudget = false;
242 do {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000243 GrResourceEntry* entry = fTail;
244 while (entry && fUnlockedEntryCount) {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000245 GrAutoResourceCacheValidate atcv(this);
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000246 if (fEntryCount <= fMaxCount && fEntryBytes <= fMaxBytes) {
247 withinBudget = true;
248 break;
249 }
reed@google.com01804b42011-01-18 21:50:41 +0000250
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000251 GrResourceEntry* prev = entry->fPrev;
252 if (!entry->isLocked()) {
253 // remove from our cache
254 fCache.remove(entry->fKey, entry);
reed@google.comac10a2d2010-12-22 21:39:39 +0000255
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000256 // remove from our llist
257 this->internalDetach(entry, false);
reed@google.com01804b42011-01-18 21:50:41 +0000258
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000259 #if GR_DUMP_TEXTURE_UPLOAD
260 GrPrintf("--- ~resource from cache %p [%d %d]\n",
261 entry->resource(),
262 entry->resource()->width(),
263 entry->resource()->height());
264 #endif
265 delete entry;
266 }
267 entry = prev;
268 }
269 } while (!withinBudget && fUnlockedEntryCount);
270 fPurging = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000271 }
272}
273
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000274void GrResourceCache::removeAll() {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000275 GrAutoResourceCacheValidate atcv(this);
reed@google.comac10a2d2010-12-22 21:39:39 +0000276 GrAssert(!fClientDetachedCount);
277 GrAssert(!fClientDetachedBytes);
reed@google.com01804b42011-01-18 21:50:41 +0000278
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000279 GrResourceEntry* entry = fHead;
reed@google.comac10a2d2010-12-22 21:39:39 +0000280
bsalomon@google.come9a98942011-08-22 17:06:16 +0000281 // we can have one GrResource holding a lock on another
282 // so we don't want to just do a simple loop kicking each
283 // entry out. Instead change the budget and purge.
reed@google.comac10a2d2010-12-22 21:39:39 +0000284
bsalomon@google.come9a98942011-08-22 17:06:16 +0000285 int savedMaxBytes = fMaxBytes;
286 int savedMaxCount = fMaxCount;
287 fMaxBytes = -1;
288 fMaxCount = 0;
289 this->purgeAsNeeded();
290
291 GrAssert(!fCache.count());
292 GrAssert(!fUnlockedEntryCount);
293 GrAssert(!fEntryCount);
294 GrAssert(!fEntryBytes);
295 GrAssert(NULL == fHead);
296 GrAssert(NULL == fTail);
297
298 fMaxBytes = savedMaxBytes;
299 fMaxCount = savedMaxCount;
reed@google.comac10a2d2010-12-22 21:39:39 +0000300}
301
302///////////////////////////////////////////////////////////////////////////////
303
304#if GR_DEBUG
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000305static int countMatches(const GrResourceEntry* head, const GrResourceEntry* target) {
306 const GrResourceEntry* entry = head;
reed@google.comac10a2d2010-12-22 21:39:39 +0000307 int count = 0;
308 while (entry) {
309 if (target == entry) {
310 count += 1;
311 }
312 entry = entry->next();
313 }
314 return count;
315}
316
reed@google.comb89a6432011-02-07 13:20:30 +0000317#if GR_DEBUG
318static bool both_zero_or_nonzero(int count, size_t bytes) {
319 return (count == 0 && bytes == 0) || (count > 0 && bytes > 0);
320}
321#endif
322
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000323void GrResourceCache::validate() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000324 GrAssert(!fHead == !fTail);
reed@google.comb89a6432011-02-07 13:20:30 +0000325 GrAssert(both_zero_or_nonzero(fEntryCount, fEntryBytes));
326 GrAssert(both_zero_or_nonzero(fClientDetachedCount, fClientDetachedBytes));
reed@google.comac10a2d2010-12-22 21:39:39 +0000327 GrAssert(fClientDetachedBytes <= fEntryBytes);
328 GrAssert(fClientDetachedCount <= fEntryCount);
329 GrAssert((fEntryCount - fClientDetachedCount) == fCache.count());
reed@google.com01804b42011-01-18 21:50:41 +0000330
reed@google.comac10a2d2010-12-22 21:39:39 +0000331 fCache.validate();
332
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000333 GrResourceEntry* entry = fHead;
reed@google.comac10a2d2010-12-22 21:39:39 +0000334 int count = 0;
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000335 int unlockCount = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000336 size_t bytes = 0;
337 while (entry) {
338 entry->validate();
339 GrAssert(fCache.find(entry->key()));
340 count += 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000341 bytes += entry->resource()->sizeInBytes();
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000342 if (!entry->isLocked()) {
343 unlockCount += 1;
344 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000345 entry = entry->fNext;
346 }
347 GrAssert(count == fEntryCount - fClientDetachedCount);
348 GrAssert(bytes == fEntryBytes - fClientDetachedBytes);
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000349 GrAssert(unlockCount == fUnlockedEntryCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000350
351 count = 0;
352 for (entry = fTail; entry; entry = entry->fPrev) {
353 count += 1;
354 }
355 GrAssert(count == fEntryCount - fClientDetachedCount);
356
357 for (int i = 0; i < count; i++) {
358 int matches = countMatches(fHead, fCache.getArray()[i]);
359 GrAssert(1 == matches);
360 }
361}
362#endif
363
364