blob: 2ad5d6f4b210d05bb90b2acd70caef68486d440d [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.com07fc0d12012-06-22 15:15:59 +000037GrResourceCache::GrResourceCache(int maxCount, size_t maxBytes) :
38 fMaxCount(maxCount),
39 fMaxBytes(maxBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +000040 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.com07fc0d12012-06-22 15:15:59 +000056void GrResourceCache::getLimits(int* maxResources, size_t* maxResourceBytes) const{
57 if (maxResources) {
58 *maxResources = fMaxCount;
59 }
60 if (maxResourceBytes) {
61 *maxResourceBytes = fMaxBytes;
62 }
63}
reed@google.com01804b42011-01-18 21:50:41 +000064
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000065void GrResourceCache::setLimits(int maxResources, size_t maxResourceBytes) {
66 bool smaller = (maxResources < fMaxCount) || (maxResourceBytes < fMaxBytes);
67
68 fMaxCount = maxResources;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000069 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
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000095 entry->fPrev = NULL;
96 entry->fNext = NULL;
97
reed@google.comac10a2d2010-12-22 21:39:39 +000098 // update our stats
99 if (clientDetach) {
100 fClientDetachedCount += 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000101 fClientDetachedBytes += entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000102 } else {
103 fEntryCount -= 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000104 fEntryBytes -= entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000105 }
106}
107
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000108void GrResourceCache::attachToHead(GrResourceEntry* entry,
reed@google.comac10a2d2010-12-22 21:39:39 +0000109 bool clientReattach) {
110 entry->fPrev = NULL;
111 entry->fNext = fHead;
112 if (fHead) {
113 fHead->fPrev = entry;
114 }
115 fHead = entry;
116 if (NULL == fTail) {
117 fTail = entry;
118 }
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000119 if (!entry->isLocked()) {
120 ++fUnlockedEntryCount;
121 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000122
123 // update our stats
124 if (clientReattach) {
125 fClientDetachedCount -= 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000126 fClientDetachedBytes -= entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000127 } else {
128 fEntryCount += 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000129 fEntryBytes += entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000130 }
131}
132
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000133class GrResourceCache::Key {
134 typedef GrResourceEntry T;
reed@google.comac10a2d2010-12-22 21:39:39 +0000135
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000136 const GrResourceKey& fKey;
reed@google.comac10a2d2010-12-22 21:39:39 +0000137public:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000138 Key(const GrResourceKey& key) : fKey(key) {}
reed@google.comac10a2d2010-12-22 21:39:39 +0000139
140 uint32_t getHash() const { return fKey.hashIndex(); }
reed@google.com01804b42011-01-18 21:50:41 +0000141
reed@google.comac10a2d2010-12-22 21:39:39 +0000142 static bool LT(const T& entry, const Key& key) {
143 return entry.key() < key.fKey;
144 }
145 static bool EQ(const T& entry, const Key& key) {
146 return entry.key() == key.fKey;
147 }
148#if GR_DEBUG
149 static uint32_t GetHash(const T& entry) {
150 return entry.key().hashIndex();
151 }
152 static bool LT(const T& a, const T& b) {
153 return a.key() < b.key();
154 }
155 static bool EQ(const T& a, const T& b) {
156 return a.key() == b.key();
157 }
158#endif
159};
160
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000161GrResourceEntry* GrResourceCache::findAndLock(const GrResourceKey& key,
162 LockType type) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000163 GrAutoResourceCacheValidate atcv(this);
reed@google.comac10a2d2010-12-22 21:39:39 +0000164
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000165 GrResourceEntry* entry = fCache.find(key);
reed@google.comac10a2d2010-12-22 21:39:39 +0000166 if (entry) {
167 this->internalDetach(entry, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000168 // mark the entry as "busy" so it doesn't get purged
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000169 // do this between detach and attach for locked count tracking
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000170 if (kNested_LockType == type || !entry->isLocked()) {
171 entry->lock();
172 }
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000173 this->attachToHead(entry, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000174 }
175 return entry;
176}
177
bsalomon@google.comfb309512011-11-30 14:13:48 +0000178bool GrResourceCache::hasKey(const GrResourceKey& key) const {
179 return NULL != fCache.find(key);
180}
181
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000182GrResourceEntry* GrResourceCache::create(const GrResourceKey& key,
183 GrResource* resource,
184 bool lock) {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000185 // we don't expect to create new resources during a purge. In theory
186 // this could cause purgeAsNeeded() into an infinite loop (e.g.
187 // each resource destroyed creates and locks 2 resources and
188 // unlocks 1 thereby causing a new purge).
189 GrAssert(!fPurging);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000190 GrAutoResourceCacheValidate atcv(this);
reed@google.comac10a2d2010-12-22 21:39:39 +0000191
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000192 GrResourceEntry* entry = new GrResourceEntry(key, resource);
reed@google.comac10a2d2010-12-22 21:39:39 +0000193
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000194 if (lock) {
195 // mark the entry as "busy" so it doesn't get purged
196 // do this before attach for locked count tracking
197 entry->lock();
198 }
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000199
reed@google.comac10a2d2010-12-22 21:39:39 +0000200 this->attachToHead(entry, false);
201 fCache.insert(key, entry);
202
203#if GR_DUMP_TEXTURE_UPLOAD
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000204 GrPrintf("--- add resource to cache %p, count=%d bytes= %d %d\n",
205 entry, fEntryCount, resource->sizeInBytes(), fEntryBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000206#endif
207
reed@google.comac10a2d2010-12-22 21:39:39 +0000208 this->purgeAsNeeded();
209 return entry;
210}
211
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000212GrResourceEntry* GrResourceCache::createAndLock(const GrResourceKey& key,
213 GrResource* resource) {
214 return this->create(key, resource, true);
215}
216
217void GrResourceCache::attach(const GrResourceKey& key,
218 GrResource* resource) {
219 this->create(key, resource, false);
220}
221
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000222void GrResourceCache::detach(GrResourceEntry* entry) {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000223 GrAutoResourceCacheValidate atcv(this);
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000224 this->internalDetach(entry, true);
reed@google.comac10a2d2010-12-22 21:39:39 +0000225 fCache.remove(entry->fKey, entry);
226}
227
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000228void GrResourceCache::freeEntry(GrResourceEntry* entry) {
229 GrAssert(NULL == entry->fNext && NULL == entry->fPrev);
230 delete entry;
231}
232
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000233void GrResourceCache::reattachAndUnlock(GrResourceEntry* entry) {
bsalomon@google.com60879752011-09-15 20:43:53 +0000234 GrAutoResourceCacheValidate atcv(this);
235 if (entry->resource()->isValid()) {
236 attachToHead(entry, true);
237 fCache.insert(entry->key(), entry);
238 } else {
239 // If the resource went invalid while it was detached then purge it
240 // This can happen when a 3D context was lost,
241 // the client called GrContext::contextDestroyed() to notify Gr,
242 // and then later an SkGpuDevice's destructor releases its backing
243 // texture (which was invalidated at contextDestroyed time).
244 fClientDetachedCount -= 1;
245 fEntryCount -= 1;
246 size_t size = entry->resource()->sizeInBytes();
247 fClientDetachedBytes -= size;
248 fEntryBytes -= size;
249 }
250 this->unlock(entry);
reed@google.comac10a2d2010-12-22 21:39:39 +0000251}
252
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000253void GrResourceCache::unlock(GrResourceEntry* entry) {
254 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +0000255
reed@google.comac10a2d2010-12-22 21:39:39 +0000256 GrAssert(entry);
257 GrAssert(entry->isLocked());
258 GrAssert(fCache.find(entry->key()));
259
260 entry->unlock();
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000261 if (!entry->isLocked()) {
262 ++fUnlockedEntryCount;
263 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000264 this->purgeAsNeeded();
265}
266
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000267/**
268 * Destroying a resource may potentially trigger the unlock of additional
269 * resources which in turn will trigger a nested purge. We block the nested
270 * purge using the fPurging variable. However, the initial purge will keep
271 * looping until either all resources in the cache are unlocked or we've met
272 * the budget. There is an assertion in createAndLock to check against a
273 * resource's destructor inserting new resources into the cache. If these
274 * new resources were unlocked before purgeAsNeeded completed it could
275 * potentially make purgeAsNeeded loop infinitely.
276 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000277void GrResourceCache::purgeAsNeeded() {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000278 if (!fPurging) {
279 fPurging = true;
280 bool withinBudget = false;
281 do {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000282 GrResourceEntry* entry = fTail;
283 while (entry && fUnlockedEntryCount) {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000284 GrAutoResourceCacheValidate atcv(this);
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000285 if (fEntryCount <= fMaxCount && fEntryBytes <= fMaxBytes) {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000286 withinBudget = true;
287 break;
288 }
reed@google.com01804b42011-01-18 21:50:41 +0000289
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000290 GrResourceEntry* prev = entry->fPrev;
291 if (!entry->isLocked()) {
292 // remove from our cache
293 fCache.remove(entry->fKey, entry);
reed@google.comac10a2d2010-12-22 21:39:39 +0000294
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000295 // remove from our llist
296 this->internalDetach(entry, false);
reed@google.com01804b42011-01-18 21:50:41 +0000297
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000298 #if GR_DUMP_TEXTURE_UPLOAD
299 GrPrintf("--- ~resource from cache %p [%d %d]\n",
300 entry->resource(),
301 entry->resource()->width(),
302 entry->resource()->height());
303 #endif
304 delete entry;
305 }
306 entry = prev;
307 }
308 } while (!withinBudget && fUnlockedEntryCount);
309 fPurging = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000310 }
311}
312
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000313void GrResourceCache::removeAll() {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000314 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +0000315
bsalomon@google.come9a98942011-08-22 17:06:16 +0000316 // we can have one GrResource holding a lock on another
317 // so we don't want to just do a simple loop kicking each
318 // entry out. Instead change the budget and purge.
reed@google.comac10a2d2010-12-22 21:39:39 +0000319
bsalomon@google.come9a98942011-08-22 17:06:16 +0000320 int savedMaxBytes = fMaxBytes;
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000321 int savedMaxCount = fMaxCount;
322 fMaxBytes = (size_t) -1;
323 fMaxCount = 0;
bsalomon@google.come9a98942011-08-22 17:06:16 +0000324 this->purgeAsNeeded();
325
twiz@google.com0ec107f2012-02-21 19:15:53 +0000326#if GR_DEBUG
bsalomon@google.come9a98942011-08-22 17:06:16 +0000327 GrAssert(!fUnlockedEntryCount);
twiz@google.com0ec107f2012-02-21 19:15:53 +0000328 if (!fCache.count()) {
329 // Items may have been detached from the cache (such as the backing
330 // texture for an SkGpuDevice). The above purge would not have removed
331 // them.
332 GrAssert(fEntryCount == fClientDetachedCount);
333 GrAssert(fEntryBytes == fClientDetachedBytes);
334 GrAssert(NULL == fHead);
335 GrAssert(NULL == fTail);
336 }
337#endif
bsalomon@google.come9a98942011-08-22 17:06:16 +0000338
339 fMaxBytes = savedMaxBytes;
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000340 fMaxCount = savedMaxCount;
reed@google.comac10a2d2010-12-22 21:39:39 +0000341}
342
343///////////////////////////////////////////////////////////////////////////////
344
345#if GR_DEBUG
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000346static int countMatches(const GrResourceEntry* head, const GrResourceEntry* target) {
347 const GrResourceEntry* entry = head;
reed@google.comac10a2d2010-12-22 21:39:39 +0000348 int count = 0;
349 while (entry) {
350 if (target == entry) {
351 count += 1;
352 }
353 entry = entry->next();
354 }
355 return count;
356}
357
reed@google.comb89a6432011-02-07 13:20:30 +0000358#if GR_DEBUG
359static bool both_zero_or_nonzero(int count, size_t bytes) {
360 return (count == 0 && bytes == 0) || (count > 0 && bytes > 0);
361}
362#endif
363
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000364void GrResourceCache::validate() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000365 GrAssert(!fHead == !fTail);
reed@google.comb89a6432011-02-07 13:20:30 +0000366 GrAssert(both_zero_or_nonzero(fEntryCount, fEntryBytes));
367 GrAssert(both_zero_or_nonzero(fClientDetachedCount, fClientDetachedBytes));
reed@google.comac10a2d2010-12-22 21:39:39 +0000368 GrAssert(fClientDetachedBytes <= fEntryBytes);
369 GrAssert(fClientDetachedCount <= fEntryCount);
370 GrAssert((fEntryCount - fClientDetachedCount) == fCache.count());
reed@google.com01804b42011-01-18 21:50:41 +0000371
reed@google.comac10a2d2010-12-22 21:39:39 +0000372 fCache.validate();
373
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000374 GrResourceEntry* entry = fHead;
reed@google.comac10a2d2010-12-22 21:39:39 +0000375 int count = 0;
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000376 int unlockCount = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000377 size_t bytes = 0;
378 while (entry) {
379 entry->validate();
380 GrAssert(fCache.find(entry->key()));
381 count += 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000382 bytes += entry->resource()->sizeInBytes();
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000383 if (!entry->isLocked()) {
384 unlockCount += 1;
385 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000386 entry = entry->fNext;
387 }
388 GrAssert(count == fEntryCount - fClientDetachedCount);
389 GrAssert(bytes == fEntryBytes - fClientDetachedBytes);
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000390 GrAssert(unlockCount == fUnlockedEntryCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000391
392 count = 0;
393 for (entry = fTail; entry; entry = entry->fPrev) {
394 count += 1;
395 }
396 GrAssert(count == fEntryCount - fClientDetachedCount);
397
398 for (int i = 0; i < count; i++) {
399 int matches = countMatches(fHead, fCache.getArray()[i]);
400 GrAssert(1 == matches);
401 }
402}
403#endif