blob: 4561aeaaa8c2fa32fcfcc31ca1a596cb2d749f0f [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;
reed@google.comac10a2d2010-12-22 21:39:39 +000017
bsalomon@google.com50398bf2011-07-26 20:45:30 +000018 // we assume ownership of the resource, and will unref it when we die
19 GrAssert(resource);
reed@google.comac10a2d2010-12-22 21:39:39 +000020}
21
bsalomon@google.com50398bf2011-07-26 20:45:30 +000022GrResourceEntry::~GrResourceEntry() {
robertphillips@google.com521eaf82012-08-22 11:03:19 +000023 fResource->setCacheEntry(NULL);
bsalomon@google.com50398bf2011-07-26 20:45:30 +000024 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);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000031 GrAssert(fResource->getCacheEntry() == this);
bsalomon@google.com50398bf2011-07-26 20:45:30 +000032 fResource->validate();
reed@google.comac10a2d2010-12-22 21:39:39 +000033}
34#endif
35
36///////////////////////////////////////////////////////////////////////////////
37
bsalomon@google.coma2921122012-08-28 12:34:17 +000038class GrResourceCache::Key {
39 typedef GrResourceEntry T;
40
41 const GrResourceKey& fKey;
42public:
43 Key(const GrResourceKey& key) : fKey(key) {}
44
45 uint32_t getHash() const { return fKey.hashIndex(); }
46
47 static bool LT(const T& entry, const Key& key) {
48 return entry.key() < key.fKey;
49 }
50 static bool EQ(const T& entry, const Key& key) {
51 return entry.key() == key.fKey;
52 }
53#if GR_DEBUG
54 static uint32_t GetHash(const T& entry) {
55 return entry.key().hashIndex();
56 }
57 static bool LT(const T& a, const T& b) {
58 return a.key() < b.key();
59 }
60 static bool EQ(const T& a, const T& b) {
61 return a.key() == b.key();
62 }
63#endif
64};
65
66///////////////////////////////////////////////////////////////////////////////
67
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000068GrResourceCache::GrResourceCache(int maxCount, size_t maxBytes) :
69 fMaxCount(maxCount),
70 fMaxBytes(maxBytes) {
robertphillips@google.com59552022012-08-31 13:07:37 +000071#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +000072 fHighWaterEntryCount = 0;
73 fHighWaterUnlockedEntryCount = 0;
74 fHighWaterEntryBytes = 0;
75 fHighWaterClientDetachedCount = 0;
76 fHighWaterClientDetachedBytes = 0;
77#endif
78
79 fEntryCount = 0;
80 fUnlockedEntryCount = 0;
81 fEntryBytes = 0;
82 fClientDetachedCount = 0;
83 fClientDetachedBytes = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +000084
bsalomon@google.coma5a1da82011-08-05 14:02:41 +000085 fPurging = false;
reed@google.comac10a2d2010-12-22 21:39:39 +000086}
87
bsalomon@google.com50398bf2011-07-26 20:45:30 +000088GrResourceCache::~GrResourceCache() {
89 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +000090
bsalomon@google.coma2921122012-08-28 12:34:17 +000091 EntryList::Iter iter;
92
93 // Unlike the removeAll, here we really remove everything, including locked resources.
94 while (GrResourceEntry* entry = fList.head()) {
95 GrAutoResourceCacheValidate atcv(this);
96
97 // remove from our cache
98 fCache.remove(entry->fKey, entry);
99
100 // remove from our llist
101 this->internalDetach(entry, false);
102
103 delete entry;
104 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000105}
106
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000107void GrResourceCache::getLimits(int* maxResources, size_t* maxResourceBytes) const{
108 if (maxResources) {
109 *maxResources = fMaxCount;
110 }
111 if (maxResourceBytes) {
112 *maxResourceBytes = fMaxBytes;
113 }
114}
reed@google.com01804b42011-01-18 21:50:41 +0000115
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000116void GrResourceCache::setLimits(int maxResources, size_t maxResourceBytes) {
117 bool smaller = (maxResources < fMaxCount) || (maxResourceBytes < fMaxBytes);
118
119 fMaxCount = maxResources;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000120 fMaxBytes = maxResourceBytes;
reed@google.com01804b42011-01-18 21:50:41 +0000121
122 if (smaller) {
123 this->purgeAsNeeded();
124 }
125}
126
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000127void GrResourceCache::internalDetach(GrResourceEntry* entry,
reed@google.comac10a2d2010-12-22 21:39:39 +0000128 bool clientDetach) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000129 fList.remove(entry);
reed@google.comac10a2d2010-12-22 21:39:39 +0000130
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000131 if (!entry->isLocked()) {
132 --fUnlockedEntryCount;
133 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000134
135 // update our stats
136 if (clientDetach) {
137 fClientDetachedCount += 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000138 fClientDetachedBytes += entry->resource()->sizeInBytes();
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000139
robertphillips@google.com59552022012-08-31 13:07:37 +0000140#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000141 if (fHighWaterClientDetachedCount < fClientDetachedCount) {
142 fHighWaterClientDetachedCount = fClientDetachedCount;
143 }
144 if (fHighWaterClientDetachedBytes < fClientDetachedBytes) {
145 fHighWaterClientDetachedBytes = fClientDetachedBytes;
146 }
147#endif
148
reed@google.comac10a2d2010-12-22 21:39:39 +0000149 } else {
150 fEntryCount -= 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000151 fEntryBytes -= entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000152 }
153}
154
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000155void GrResourceCache::attachToHead(GrResourceEntry* entry,
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000156 bool clientReattach) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000157 fList.addToHead(entry);
158
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000159 if (!entry->isLocked()) {
160 ++fUnlockedEntryCount;
robertphillips@google.com59552022012-08-31 13:07:37 +0000161#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000162 if (fHighWaterUnlockedEntryCount < fUnlockedEntryCount) {
163 fHighWaterUnlockedEntryCount = fUnlockedEntryCount;
164 }
165#endif
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000166 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000167
168 // update our stats
169 if (clientReattach) {
170 fClientDetachedCount -= 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000171 fClientDetachedBytes -= entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000172 } else {
173 fEntryCount += 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000174 fEntryBytes += entry->resource()->sizeInBytes();
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000175
robertphillips@google.com59552022012-08-31 13:07:37 +0000176#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000177 if (fHighWaterEntryCount < fEntryCount) {
178 fHighWaterEntryCount = fEntryCount;
179 }
180 if (fHighWaterEntryBytes < fEntryBytes) {
181 fHighWaterEntryBytes = fEntryBytes;
182 }
183#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000184 }
185}
186
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000187GrResource* GrResourceCache::find(const GrResourceKey& key) {
188 GrAutoResourceCacheValidate atcv(this);
189
190 GrResourceEntry* entry = fCache.find(key);
191 if (NULL == entry) {
192 return NULL;
193 }
194
195 return entry->fResource;
196}
197
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000198GrResource* GrResourceCache::findAndLock(const GrResourceKey& key) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000199 GrAutoResourceCacheValidate atcv(this);
reed@google.comac10a2d2010-12-22 21:39:39 +0000200
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000201 GrResourceEntry* entry = fCache.find(key);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000202 if (NULL == entry) {
203 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000204 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000205
206 this->internalDetach(entry, false);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000207 this->attachToHead(entry, false);
208
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000209 this->lock(entry);
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000210
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000211 return entry->fResource;
reed@google.comac10a2d2010-12-22 21:39:39 +0000212}
213
bsalomon@google.comfb309512011-11-30 14:13:48 +0000214bool GrResourceCache::hasKey(const GrResourceKey& key) const {
215 return NULL != fCache.find(key);
216}
217
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000218void GrResourceCache::create(const GrResourceKey& key, GrResource* resource) {
219 GrAssert(NULL == resource->getCacheEntry());
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000220 // we don't expect to create new resources during a purge. In theory
221 // this could cause purgeAsNeeded() into an infinite loop (e.g.
222 // each resource destroyed creates and locks 2 resources and
223 // unlocks 1 thereby causing a new purge).
224 GrAssert(!fPurging);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000225 GrAutoResourceCacheValidate atcv(this);
reed@google.comac10a2d2010-12-22 21:39:39 +0000226
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000227 GrResourceEntry* entry = SkNEW_ARGS(GrResourceEntry, (key, resource));
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000228 resource->setCacheEntry(entry);
229
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000230 this->attachToHead(entry, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000231 fCache.insert(key, entry);
232
233#if GR_DUMP_TEXTURE_UPLOAD
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000234 GrPrintf("--- add resource to cache %p, count=%d bytes= %d %d\n",
235 entry, fEntryCount, resource->sizeInBytes(), fEntryBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000236#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000237}
238
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000239void GrResourceCache::createAndLock(const GrResourceKey& key,
240 GrResource* resource) {
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000241 this->create(key, resource);
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000242
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000243 GrAssert(NULL != resource->getCacheEntry());
244 this->lock(resource->getCacheEntry());
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000245}
246
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000247void GrResourceCache::makeExclusive(GrResourceEntry* entry) {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000248 GrAutoResourceCacheValidate atcv(this);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000249
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000250 this->internalDetach(entry, true);
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000251 fCache.remove(entry->key(), entry);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000252
253#if GR_DEBUG
254 fExclusiveList.addToHead(entry);
255#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000256}
257
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000258void GrResourceCache::removeInvalidResource(GrResourceEntry* entry) {
259 // If the resource went invalid while it was detached then purge it
260 // This can happen when a 3D context was lost,
261 // the client called GrContext::contextDestroyed() to notify Gr,
262 // and then later an SkGpuDevice's destructor releases its backing
263 // texture (which was invalidated at contextDestroyed time).
264 fClientDetachedCount -= 1;
265 fEntryCount -= 1;
266 size_t size = entry->resource()->sizeInBytes();
267 fClientDetachedBytes -= size;
268 fEntryBytes -= size;
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000269}
270
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000271void GrResourceCache::makeNonExclusive(GrResourceEntry* entry) {
bsalomon@google.com60879752011-09-15 20:43:53 +0000272 GrAutoResourceCacheValidate atcv(this);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000273
274#if GR_DEBUG
275 fExclusiveList.remove(entry);
276#endif
277
bsalomon@google.com60879752011-09-15 20:43:53 +0000278 if (entry->resource()->isValid()) {
279 attachToHead(entry, true);
280 fCache.insert(entry->key(), entry);
281 } else {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000282 this->removeInvalidResource(entry);
bsalomon@google.com60879752011-09-15 20:43:53 +0000283 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000284}
285
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000286void GrResourceCache::lock(GrResourceEntry* entry) {
287 GrAutoResourceCacheValidate atcv(this);
288
289 GrAssert(entry);
290 GrAssert(fCache.find(entry->key()));
291
292 if (!entry->isLocked()) {
293 --fUnlockedEntryCount;
294 }
295
296 entry->lock();
297}
298
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000299void GrResourceCache::unlock(GrResourceEntry* entry) {
300 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +0000301
reed@google.comac10a2d2010-12-22 21:39:39 +0000302 GrAssert(entry);
303 GrAssert(entry->isLocked());
304 GrAssert(fCache.find(entry->key()));
305
306 entry->unlock();
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000307 if (!entry->isLocked()) {
308 ++fUnlockedEntryCount;
robertphillips@google.com59552022012-08-31 13:07:37 +0000309#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000310 if (fHighWaterUnlockedEntryCount < fUnlockedEntryCount) {
311 fHighWaterUnlockedEntryCount = fUnlockedEntryCount;
312 }
313#endif
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000314 }
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000315
reed@google.comac10a2d2010-12-22 21:39:39 +0000316 this->purgeAsNeeded();
317}
318
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000319/**
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000320 * Destroying a resource may potentially trigger the unlock of additional
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000321 * resources which in turn will trigger a nested purge. We block the nested
322 * purge using the fPurging variable. However, the initial purge will keep
323 * looping until either all resources in the cache are unlocked or we've met
324 * the budget. There is an assertion in createAndLock to check against a
325 * resource's destructor inserting new resources into the cache. If these
326 * new resources were unlocked before purgeAsNeeded completed it could
327 * potentially make purgeAsNeeded loop infinitely.
328 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000329void GrResourceCache::purgeAsNeeded() {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000330 if (!fPurging) {
331 fPurging = true;
332 bool withinBudget = false;
robertphillips@google.com6fc95182012-09-04 13:36:31 +0000333 int priorUnlockedEntryCount = 0;
334
335 // The purging process is repeated several times since one pass
336 // may free up other resources
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000337 do {
bsalomon@google.coma2921122012-08-28 12:34:17 +0000338 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000339
robertphillips@google.com6fc95182012-09-04 13:36:31 +0000340 priorUnlockedEntryCount = fUnlockedEntryCount;
341
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000342 // Note: the following code relies on the fact that the
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000343 // doubly linked list doesn't invalidate its data/pointers
344 // outside of the specific area where a deletion occurs (e.g.,
345 // in internalDetach)
bsalomon@google.coma2921122012-08-28 12:34:17 +0000346 GrResourceEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000347
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000348 while (entry && fUnlockedEntryCount) {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000349 GrAutoResourceCacheValidate atcv(this);
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000350 if (fEntryCount <= fMaxCount && fEntryBytes <= fMaxBytes) {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000351 withinBudget = true;
352 break;
353 }
reed@google.com01804b42011-01-18 21:50:41 +0000354
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000355 GrResourceEntry* prev = iter.prev();
robertphillips@google.com6fc95182012-09-04 13:36:31 +0000356 if (!entry->isLocked() && entry->fResource->getRefCnt() == 1) {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000357 // remove from our cache
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000358 fCache.remove(entry->key(), entry);
reed@google.comac10a2d2010-12-22 21:39:39 +0000359
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000360 // remove from our llist
361 this->internalDetach(entry, false);
reed@google.com01804b42011-01-18 21:50:41 +0000362
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000363 #if GR_DUMP_TEXTURE_UPLOAD
364 GrPrintf("--- ~resource from cache %p [%d %d]\n",
365 entry->resource(),
366 entry->resource()->width(),
367 entry->resource()->height());
368 #endif
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000369
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000370 delete entry;
371 }
372 entry = prev;
373 }
robertphillips@google.com6fc95182012-09-04 13:36:31 +0000374 } while (!withinBudget && (fUnlockedEntryCount != priorUnlockedEntryCount));
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000375 fPurging = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000376 }
377}
378
bsalomon@google.coma2921122012-08-28 12:34:17 +0000379void GrResourceCache::purgeAllUnlocked() {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000380 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +0000381
bsalomon@google.come9a98942011-08-22 17:06:16 +0000382 // we can have one GrResource holding a lock on another
383 // so we don't want to just do a simple loop kicking each
384 // entry out. Instead change the budget and purge.
reed@google.comac10a2d2010-12-22 21:39:39 +0000385
bsalomon@google.come9a98942011-08-22 17:06:16 +0000386 int savedMaxBytes = fMaxBytes;
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000387 int savedMaxCount = fMaxCount;
388 fMaxBytes = (size_t) -1;
389 fMaxCount = 0;
bsalomon@google.come9a98942011-08-22 17:06:16 +0000390 this->purgeAsNeeded();
391
twiz@google.com0ec107f2012-02-21 19:15:53 +0000392#if GR_DEBUG
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000393 GrAssert(fExclusiveList.countEntries() == fClientDetachedCount);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000394 GrAssert(countBytes(fExclusiveList) == fClientDetachedBytes);
bsalomon@google.come9a98942011-08-22 17:06:16 +0000395 GrAssert(!fUnlockedEntryCount);
twiz@google.com0ec107f2012-02-21 19:15:53 +0000396 if (!fCache.count()) {
397 // Items may have been detached from the cache (such as the backing
398 // texture for an SkGpuDevice). The above purge would not have removed
399 // them.
400 GrAssert(fEntryCount == fClientDetachedCount);
401 GrAssert(fEntryBytes == fClientDetachedBytes);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000402 GrAssert(fList.isEmpty());
twiz@google.com0ec107f2012-02-21 19:15:53 +0000403 }
404#endif
bsalomon@google.come9a98942011-08-22 17:06:16 +0000405
406 fMaxBytes = savedMaxBytes;
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000407 fMaxCount = savedMaxCount;
reed@google.comac10a2d2010-12-22 21:39:39 +0000408}
409
410///////////////////////////////////////////////////////////////////////////////
411
412#if GR_DEBUG
bsalomon@google.coma2921122012-08-28 12:34:17 +0000413size_t GrResourceCache::countBytes(const EntryList& list) {
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000414 size_t bytes = 0;
415
bsalomon@google.coma2921122012-08-28 12:34:17 +0000416 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000417
bsalomon@google.coma2921122012-08-28 12:34:17 +0000418 const GrResourceEntry* entry = iter.init(const_cast<EntryList&>(list),
419 EntryList::Iter::kTail_IterStart);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000420
421 for ( ; NULL != entry; entry = iter.prev()) {
422 bytes += entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000423 }
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000424 return bytes;
reed@google.comac10a2d2010-12-22 21:39:39 +0000425}
426
reed@google.comb89a6432011-02-07 13:20:30 +0000427static bool both_zero_or_nonzero(int count, size_t bytes) {
428 return (count == 0 && bytes == 0) || (count > 0 && bytes > 0);
429}
reed@google.comb89a6432011-02-07 13:20:30 +0000430
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000431void GrResourceCache::validate() const {
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000432 fList.validate();
433 fExclusiveList.validate();
reed@google.comb89a6432011-02-07 13:20:30 +0000434 GrAssert(both_zero_or_nonzero(fEntryCount, fEntryBytes));
435 GrAssert(both_zero_or_nonzero(fClientDetachedCount, fClientDetachedBytes));
reed@google.comac10a2d2010-12-22 21:39:39 +0000436 GrAssert(fClientDetachedBytes <= fEntryBytes);
437 GrAssert(fClientDetachedCount <= fEntryCount);
438 GrAssert((fEntryCount - fClientDetachedCount) == fCache.count());
reed@google.com01804b42011-01-18 21:50:41 +0000439
reed@google.comac10a2d2010-12-22 21:39:39 +0000440 fCache.validate();
441
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000442
bsalomon@google.coma2921122012-08-28 12:34:17 +0000443 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000444
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000445 const GrResourceEntry* entry = iter.init(const_cast<EntryList&>(fExclusiveList),
bsalomon@google.coma2921122012-08-28 12:34:17 +0000446 EntryList::Iter::kHead_IterStart);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000447
448 for ( ; NULL != entry; entry = iter.next()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000449 entry->validate();
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000450 GrAssert(entry->isLocked());
451 }
452
453 entry = iter.init(const_cast<EntryList&>(fList), EntryList::Iter::kHead_IterStart);
454
455 int count = 0;
456 int unlockCount = 0;
457 for ( ; NULL != entry; entry = iter.next()) {
458 entry->validate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000459 GrAssert(fCache.find(entry->key()));
460 count += 1;
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000461 if (!entry->isLocked()) {
462 unlockCount += 1;
463 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000464 }
465 GrAssert(count == fEntryCount - fClientDetachedCount);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000466
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000467 size_t bytes = countBytes(fList);
reed@google.comac10a2d2010-12-22 21:39:39 +0000468 GrAssert(bytes == fEntryBytes - fClientDetachedBytes);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000469
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000470 bytes = countBytes(fExclusiveList);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000471 GrAssert(bytes == fClientDetachedBytes);
472
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000473 GrAssert(unlockCount == fUnlockedEntryCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000474
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000475 GrAssert(fList.countEntries() == fEntryCount - fClientDetachedCount);
476
477 GrAssert(fExclusiveList.countEntries() == fClientDetachedCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000478}
robertphillips@google.com59552022012-08-31 13:07:37 +0000479#endif // GR_DEBUG
480
481#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000482
483void GrResourceCache::printStats() const {
484 SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes);
485 SkDebugf("\t\tEntry Count: current %d high %d\n",
486 fEntryCount, fHighWaterEntryCount);
487 SkDebugf("\t\tUnlocked Entry Count: current %d high %d\n",
488 fUnlockedEntryCount, fHighWaterUnlockedEntryCount);
489 SkDebugf("\t\tEntry Bytes: current %d high %d\n",
490 fEntryBytes, fHighWaterEntryBytes);
491 SkDebugf("\t\tDetached Entry Count: current %d high %d\n",
492 fClientDetachedCount, fHighWaterClientDetachedCount);
493 SkDebugf("\t\tDetached Bytes: current %d high %d\n",
494 fClientDetachedBytes, fHighWaterClientDetachedBytes);
495}
496
reed@google.comac10a2d2010-12-22 21:39:39 +0000497#endif
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000498
499///////////////////////////////////////////////////////////////////////////////