blob: 3094721ceee158aa135b3f2cac70af2a0341e24c [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) {
bsalomon@google.com60879752011-09-15 20:43:53 +0000209 GrAutoResourceCacheValidate atcv(this);
210 if (entry->resource()->isValid()) {
211 attachToHead(entry, true);
212 fCache.insert(entry->key(), entry);
213 } else {
214 // If the resource went invalid while it was detached then purge it
215 // This can happen when a 3D context was lost,
216 // the client called GrContext::contextDestroyed() to notify Gr,
217 // and then later an SkGpuDevice's destructor releases its backing
218 // texture (which was invalidated at contextDestroyed time).
219 fClientDetachedCount -= 1;
220 fEntryCount -= 1;
221 size_t size = entry->resource()->sizeInBytes();
222 fClientDetachedBytes -= size;
223 fEntryBytes -= size;
224 }
225 this->unlock(entry);
reed@google.comac10a2d2010-12-22 21:39:39 +0000226}
227
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000228void GrResourceCache::unlock(GrResourceEntry* entry) {
229 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +0000230
reed@google.comac10a2d2010-12-22 21:39:39 +0000231 GrAssert(entry);
232 GrAssert(entry->isLocked());
233 GrAssert(fCache.find(entry->key()));
234
235 entry->unlock();
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000236 if (!entry->isLocked()) {
237 ++fUnlockedEntryCount;
238 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000239 this->purgeAsNeeded();
240}
241
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000242/**
243 * Destroying a resource may potentially trigger the unlock of additional
244 * resources which in turn will trigger a nested purge. We block the nested
245 * purge using the fPurging variable. However, the initial purge will keep
246 * looping until either all resources in the cache are unlocked or we've met
247 * the budget. There is an assertion in createAndLock to check against a
248 * resource's destructor inserting new resources into the cache. If these
249 * new resources were unlocked before purgeAsNeeded completed it could
250 * potentially make purgeAsNeeded loop infinitely.
251 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000252void GrResourceCache::purgeAsNeeded() {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000253 if (!fPurging) {
254 fPurging = true;
255 bool withinBudget = false;
256 do {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000257 GrResourceEntry* entry = fTail;
258 while (entry && fUnlockedEntryCount) {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000259 GrAutoResourceCacheValidate atcv(this);
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000260 if (fEntryCount <= fMaxCount && fEntryBytes <= fMaxBytes) {
261 withinBudget = true;
262 break;
263 }
reed@google.com01804b42011-01-18 21:50:41 +0000264
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000265 GrResourceEntry* prev = entry->fPrev;
266 if (!entry->isLocked()) {
267 // remove from our cache
268 fCache.remove(entry->fKey, entry);
reed@google.comac10a2d2010-12-22 21:39:39 +0000269
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000270 // remove from our llist
271 this->internalDetach(entry, false);
reed@google.com01804b42011-01-18 21:50:41 +0000272
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000273 #if GR_DUMP_TEXTURE_UPLOAD
274 GrPrintf("--- ~resource from cache %p [%d %d]\n",
275 entry->resource(),
276 entry->resource()->width(),
277 entry->resource()->height());
278 #endif
279 delete entry;
280 }
281 entry = prev;
282 }
283 } while (!withinBudget && fUnlockedEntryCount);
284 fPurging = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000285 }
286}
287
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000288void GrResourceCache::removeAll() {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000289 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +0000290
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000291 GrResourceEntry* entry = fHead;
reed@google.comac10a2d2010-12-22 21:39:39 +0000292
bsalomon@google.come9a98942011-08-22 17:06:16 +0000293 // we can have one GrResource holding a lock on another
294 // so we don't want to just do a simple loop kicking each
295 // entry out. Instead change the budget and purge.
reed@google.comac10a2d2010-12-22 21:39:39 +0000296
bsalomon@google.come9a98942011-08-22 17:06:16 +0000297 int savedMaxBytes = fMaxBytes;
298 int savedMaxCount = fMaxCount;
299 fMaxBytes = -1;
300 fMaxCount = 0;
301 this->purgeAsNeeded();
302
303 GrAssert(!fCache.count());
304 GrAssert(!fUnlockedEntryCount);
bsalomon@google.com60879752011-09-15 20:43:53 +0000305 // Items may have been detached from the cache (such as the backing texture
306 // for an SkGpuDevice). The above purge would not have removed them.
307 GrAssert(fEntryCount == fClientDetachedCount);
308 GrAssert(fEntryBytes == fClientDetachedBytes);
bsalomon@google.come9a98942011-08-22 17:06:16 +0000309 GrAssert(NULL == fHead);
310 GrAssert(NULL == fTail);
311
312 fMaxBytes = savedMaxBytes;
313 fMaxCount = savedMaxCount;
reed@google.comac10a2d2010-12-22 21:39:39 +0000314}
315
316///////////////////////////////////////////////////////////////////////////////
317
318#if GR_DEBUG
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000319static int countMatches(const GrResourceEntry* head, const GrResourceEntry* target) {
320 const GrResourceEntry* entry = head;
reed@google.comac10a2d2010-12-22 21:39:39 +0000321 int count = 0;
322 while (entry) {
323 if (target == entry) {
324 count += 1;
325 }
326 entry = entry->next();
327 }
328 return count;
329}
330
reed@google.comb89a6432011-02-07 13:20:30 +0000331#if GR_DEBUG
332static bool both_zero_or_nonzero(int count, size_t bytes) {
333 return (count == 0 && bytes == 0) || (count > 0 && bytes > 0);
334}
335#endif
336
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000337void GrResourceCache::validate() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000338 GrAssert(!fHead == !fTail);
reed@google.comb89a6432011-02-07 13:20:30 +0000339 GrAssert(both_zero_or_nonzero(fEntryCount, fEntryBytes));
340 GrAssert(both_zero_or_nonzero(fClientDetachedCount, fClientDetachedBytes));
reed@google.comac10a2d2010-12-22 21:39:39 +0000341 GrAssert(fClientDetachedBytes <= fEntryBytes);
342 GrAssert(fClientDetachedCount <= fEntryCount);
343 GrAssert((fEntryCount - fClientDetachedCount) == fCache.count());
reed@google.com01804b42011-01-18 21:50:41 +0000344
reed@google.comac10a2d2010-12-22 21:39:39 +0000345 fCache.validate();
346
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000347 GrResourceEntry* entry = fHead;
reed@google.comac10a2d2010-12-22 21:39:39 +0000348 int count = 0;
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000349 int unlockCount = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000350 size_t bytes = 0;
351 while (entry) {
352 entry->validate();
353 GrAssert(fCache.find(entry->key()));
354 count += 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000355 bytes += entry->resource()->sizeInBytes();
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000356 if (!entry->isLocked()) {
357 unlockCount += 1;
358 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000359 entry = entry->fNext;
360 }
361 GrAssert(count == fEntryCount - fClientDetachedCount);
362 GrAssert(bytes == fEntryBytes - fClientDetachedBytes);
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000363 GrAssert(unlockCount == fUnlockedEntryCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000364
365 count = 0;
366 for (entry = fTail; entry; entry = entry->fPrev) {
367 count += 1;
368 }
369 GrAssert(count == fEntryCount - fClientDetachedCount);
370
371 for (int i = 0; i < count; i++) {
372 int matches = countMatches(fHead, fCache.getArray()[i]);
373 GrAssert(1 == matches);
374 }
375}
376#endif