blob: 89fce55f700209aacb93e32799bb74a49710e090 [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) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000016 // we assume ownership of the resource, and will unref it when we die
17 GrAssert(resource);
skia.committer@gmail.com6c778162012-09-06 02:01:13 +000018 resource->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +000019}
20
bsalomon@google.com50398bf2011-07-26 20:45:30 +000021GrResourceEntry::~GrResourceEntry() {
robertphillips@google.com521eaf82012-08-22 11:03:19 +000022 fResource->setCacheEntry(NULL);
bsalomon@google.com50398bf2011-07-26 20:45:30 +000023 fResource->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +000024}
25
26#if GR_DEBUG
bsalomon@google.com50398bf2011-07-26 20:45:30 +000027void GrResourceEntry::validate() const {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000028 GrAssert(fResource);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000029 GrAssert(fResource->getCacheEntry() == this);
bsalomon@google.com50398bf2011-07-26 20:45:30 +000030 fResource->validate();
reed@google.comac10a2d2010-12-22 21:39:39 +000031}
32#endif
33
34///////////////////////////////////////////////////////////////////////////////
35
bsalomon@google.coma2921122012-08-28 12:34:17 +000036class GrResourceCache::Key {
37 typedef GrResourceEntry T;
38
39 const GrResourceKey& fKey;
40public:
41 Key(const GrResourceKey& key) : fKey(key) {}
42
43 uint32_t getHash() const { return fKey.hashIndex(); }
44
45 static bool LT(const T& entry, const Key& key) {
46 return entry.key() < key.fKey;
47 }
48 static bool EQ(const T& entry, const Key& key) {
49 return entry.key() == key.fKey;
50 }
51#if GR_DEBUG
52 static uint32_t GetHash(const T& entry) {
53 return entry.key().hashIndex();
54 }
55 static bool LT(const T& a, const T& b) {
56 return a.key() < b.key();
57 }
58 static bool EQ(const T& a, const T& b) {
59 return a.key() == b.key();
60 }
61#endif
62};
63
64///////////////////////////////////////////////////////////////////////////////
65
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000066GrResourceCache::GrResourceCache(int maxCount, size_t maxBytes) :
67 fMaxCount(maxCount),
68 fMaxBytes(maxBytes) {
robertphillips@google.com59552022012-08-31 13:07:37 +000069#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +000070 fHighWaterEntryCount = 0;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +000071 fHighWaterEntryBytes = 0;
72 fHighWaterClientDetachedCount = 0;
73 fHighWaterClientDetachedBytes = 0;
74#endif
75
76 fEntryCount = 0;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +000077 fEntryBytes = 0;
78 fClientDetachedCount = 0;
79 fClientDetachedBytes = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +000080
bsalomon@google.coma5a1da82011-08-05 14:02:41 +000081 fPurging = false;
reed@google.comac10a2d2010-12-22 21:39:39 +000082}
83
bsalomon@google.com50398bf2011-07-26 20:45:30 +000084GrResourceCache::~GrResourceCache() {
85 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +000086
bsalomon@google.coma2921122012-08-28 12:34:17 +000087 EntryList::Iter iter;
88
89 // Unlike the removeAll, here we really remove everything, including locked resources.
90 while (GrResourceEntry* entry = fList.head()) {
91 GrAutoResourceCacheValidate atcv(this);
92
93 // remove from our cache
94 fCache.remove(entry->fKey, entry);
95
96 // remove from our llist
97 this->internalDetach(entry, false);
98
99 delete entry;
100 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000101}
102
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000103void GrResourceCache::getLimits(int* maxResources, size_t* maxResourceBytes) const{
104 if (maxResources) {
105 *maxResources = fMaxCount;
106 }
107 if (maxResourceBytes) {
108 *maxResourceBytes = fMaxBytes;
109 }
110}
reed@google.com01804b42011-01-18 21:50:41 +0000111
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000112void GrResourceCache::setLimits(int maxResources, size_t maxResourceBytes) {
113 bool smaller = (maxResources < fMaxCount) || (maxResourceBytes < fMaxBytes);
114
115 fMaxCount = maxResources;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000116 fMaxBytes = maxResourceBytes;
reed@google.com01804b42011-01-18 21:50:41 +0000117
118 if (smaller) {
119 this->purgeAsNeeded();
120 }
121}
122
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000123void GrResourceCache::internalDetach(GrResourceEntry* entry,
reed@google.comac10a2d2010-12-22 21:39:39 +0000124 bool clientDetach) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000125 fList.remove(entry);
reed@google.comac10a2d2010-12-22 21:39:39 +0000126
reed@google.comac10a2d2010-12-22 21:39:39 +0000127 // update our stats
128 if (clientDetach) {
129 fClientDetachedCount += 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000130 fClientDetachedBytes += entry->resource()->sizeInBytes();
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000131
robertphillips@google.com59552022012-08-31 13:07:37 +0000132#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000133 if (fHighWaterClientDetachedCount < fClientDetachedCount) {
134 fHighWaterClientDetachedCount = fClientDetachedCount;
135 }
136 if (fHighWaterClientDetachedBytes < fClientDetachedBytes) {
137 fHighWaterClientDetachedBytes = fClientDetachedBytes;
138 }
139#endif
140
reed@google.comac10a2d2010-12-22 21:39:39 +0000141 } else {
142 fEntryCount -= 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000143 fEntryBytes -= entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000144 }
145}
146
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000147void GrResourceCache::attachToHead(GrResourceEntry* entry,
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000148 bool clientReattach) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000149 fList.addToHead(entry);
150
reed@google.comac10a2d2010-12-22 21:39:39 +0000151 // update our stats
152 if (clientReattach) {
153 fClientDetachedCount -= 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000154 fClientDetachedBytes -= entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000155 } else {
156 fEntryCount += 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000157 fEntryBytes += entry->resource()->sizeInBytes();
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000158
robertphillips@google.com59552022012-08-31 13:07:37 +0000159#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000160 if (fHighWaterEntryCount < fEntryCount) {
161 fHighWaterEntryCount = fEntryCount;
162 }
163 if (fHighWaterEntryBytes < fEntryBytes) {
164 fHighWaterEntryBytes = fEntryBytes;
165 }
166#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000167 }
168}
169
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000170GrResource* GrResourceCache::find(const GrResourceKey& key) {
171 GrAutoResourceCacheValidate atcv(this);
172
173 GrResourceEntry* entry = fCache.find(key);
174 if (NULL == entry) {
175 return NULL;
176 }
177
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000178 this->internalDetach(entry, false);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000179 this->attachToHead(entry, false);
180
181 return entry->fResource;
reed@google.comac10a2d2010-12-22 21:39:39 +0000182}
183
bsalomon@google.comfb309512011-11-30 14:13:48 +0000184bool GrResourceCache::hasKey(const GrResourceKey& key) const {
185 return NULL != fCache.find(key);
186}
187
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000188void GrResourceCache::create(const GrResourceKey& key, GrResource* resource) {
189 GrAssert(NULL == resource->getCacheEntry());
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000190 // we don't expect to create new resources during a purge. In theory
191 // this could cause purgeAsNeeded() into an infinite loop (e.g.
192 // each resource destroyed creates and locks 2 resources and
193 // unlocks 1 thereby causing a new purge).
194 GrAssert(!fPurging);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000195 GrAutoResourceCacheValidate atcv(this);
reed@google.comac10a2d2010-12-22 21:39:39 +0000196
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000197 GrResourceEntry* entry = SkNEW_ARGS(GrResourceEntry, (key, resource));
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000198 resource->setCacheEntry(entry);
199
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000200 this->attachToHead(entry, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000201 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
reed@google.comac10a2d2010-12-22 21:39:39 +0000207}
208
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000209void GrResourceCache::makeExclusive(GrResourceEntry* entry) {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000210 GrAutoResourceCacheValidate atcv(this);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000211
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000212 this->internalDetach(entry, true);
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000213 fCache.remove(entry->key(), entry);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000214
215#if GR_DEBUG
216 fExclusiveList.addToHead(entry);
217#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000218}
219
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000220void GrResourceCache::removeInvalidResource(GrResourceEntry* entry) {
221 // If the resource went invalid while it was detached then purge it
222 // This can happen when a 3D context was lost,
223 // the client called GrContext::contextDestroyed() to notify Gr,
224 // and then later an SkGpuDevice's destructor releases its backing
225 // texture (which was invalidated at contextDestroyed time).
226 fClientDetachedCount -= 1;
227 fEntryCount -= 1;
228 size_t size = entry->resource()->sizeInBytes();
229 fClientDetachedBytes -= size;
230 fEntryBytes -= size;
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000231}
232
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000233void GrResourceCache::makeNonExclusive(GrResourceEntry* entry) {
bsalomon@google.com60879752011-09-15 20:43:53 +0000234 GrAutoResourceCacheValidate atcv(this);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000235
236#if GR_DEBUG
237 fExclusiveList.remove(entry);
238#endif
239
bsalomon@google.com60879752011-09-15 20:43:53 +0000240 if (entry->resource()->isValid()) {
241 attachToHead(entry, true);
242 fCache.insert(entry->key(), entry);
243 } else {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000244 this->removeInvalidResource(entry);
bsalomon@google.com60879752011-09-15 20:43:53 +0000245 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000246}
247
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000248/**
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000249 * Destroying a resource may potentially trigger the unlock of additional
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000250 * resources which in turn will trigger a nested purge. We block the nested
251 * purge using the fPurging variable. However, the initial purge will keep
252 * looping until either all resources in the cache are unlocked or we've met
253 * the budget. There is an assertion in createAndLock to check against a
254 * resource's destructor inserting new resources into the cache. If these
255 * new resources were unlocked before purgeAsNeeded completed it could
256 * potentially make purgeAsNeeded loop infinitely.
257 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000258void GrResourceCache::purgeAsNeeded() {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000259 if (!fPurging) {
260 fPurging = true;
261 bool withinBudget = false;
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000262 bool changed = false;
robertphillips@google.com6fc95182012-09-04 13:36:31 +0000263
264 // The purging process is repeated several times since one pass
265 // may free up other resources
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000266 do {
bsalomon@google.coma2921122012-08-28 12:34:17 +0000267 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000268
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000269 changed = false;
robertphillips@google.com6fc95182012-09-04 13:36:31 +0000270
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000271 // Note: the following code relies on the fact that the
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000272 // doubly linked list doesn't invalidate its data/pointers
273 // outside of the specific area where a deletion occurs (e.g.,
274 // in internalDetach)
bsalomon@google.coma2921122012-08-28 12:34:17 +0000275 GrResourceEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000276
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000277 while (NULL != entry) {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000278 GrAutoResourceCacheValidate atcv(this);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000279
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000280 if (fEntryCount <= fMaxCount && fEntryBytes <= fMaxBytes) {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000281 withinBudget = true;
282 break;
283 }
reed@google.com01804b42011-01-18 21:50:41 +0000284
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000285 GrResourceEntry* prev = iter.prev();
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000286 if (1 == entry->fResource->getRefCnt()) {
287 changed = true;
288
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000289 // remove from our cache
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000290 fCache.remove(entry->key(), entry);
reed@google.comac10a2d2010-12-22 21:39:39 +0000291
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000292 // remove from our llist
293 this->internalDetach(entry, false);
reed@google.com01804b42011-01-18 21:50:41 +0000294
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000295 #if GR_DUMP_TEXTURE_UPLOAD
296 GrPrintf("--- ~resource from cache %p [%d %d]\n",
297 entry->resource(),
298 entry->resource()->width(),
299 entry->resource()->height());
300 #endif
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000301
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000302 delete entry;
303 }
304 entry = prev;
305 }
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000306 } while (!withinBudget && changed);
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000307 fPurging = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000308 }
309}
310
bsalomon@google.coma2921122012-08-28 12:34:17 +0000311void GrResourceCache::purgeAllUnlocked() {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000312 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +0000313
bsalomon@google.come9a98942011-08-22 17:06:16 +0000314 // we can have one GrResource holding a lock on another
315 // so we don't want to just do a simple loop kicking each
316 // entry out. Instead change the budget and purge.
reed@google.comac10a2d2010-12-22 21:39:39 +0000317
bsalomon@google.come9a98942011-08-22 17:06:16 +0000318 int savedMaxBytes = fMaxBytes;
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000319 int savedMaxCount = fMaxCount;
320 fMaxBytes = (size_t) -1;
321 fMaxCount = 0;
bsalomon@google.come9a98942011-08-22 17:06:16 +0000322 this->purgeAsNeeded();
323
twiz@google.com0ec107f2012-02-21 19:15:53 +0000324#if GR_DEBUG
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000325 GrAssert(fExclusiveList.countEntries() == fClientDetachedCount);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000326 GrAssert(countBytes(fExclusiveList) == fClientDetachedBytes);
twiz@google.com0ec107f2012-02-21 19:15:53 +0000327 if (!fCache.count()) {
328 // Items may have been detached from the cache (such as the backing
329 // texture for an SkGpuDevice). The above purge would not have removed
330 // them.
331 GrAssert(fEntryCount == fClientDetachedCount);
332 GrAssert(fEntryBytes == fClientDetachedBytes);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000333 GrAssert(fList.isEmpty());
twiz@google.com0ec107f2012-02-21 19:15:53 +0000334 }
335#endif
bsalomon@google.come9a98942011-08-22 17:06:16 +0000336
337 fMaxBytes = savedMaxBytes;
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000338 fMaxCount = savedMaxCount;
reed@google.comac10a2d2010-12-22 21:39:39 +0000339}
340
341///////////////////////////////////////////////////////////////////////////////
342
343#if GR_DEBUG
bsalomon@google.coma2921122012-08-28 12:34:17 +0000344size_t GrResourceCache::countBytes(const EntryList& list) {
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000345 size_t bytes = 0;
346
bsalomon@google.coma2921122012-08-28 12:34:17 +0000347 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000348
bsalomon@google.coma2921122012-08-28 12:34:17 +0000349 const GrResourceEntry* entry = iter.init(const_cast<EntryList&>(list),
350 EntryList::Iter::kTail_IterStart);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000351
352 for ( ; NULL != entry; entry = iter.prev()) {
353 bytes += entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000354 }
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000355 return bytes;
reed@google.comac10a2d2010-12-22 21:39:39 +0000356}
357
reed@google.comb89a6432011-02-07 13:20:30 +0000358static bool both_zero_or_nonzero(int count, size_t bytes) {
359 return (count == 0 && bytes == 0) || (count > 0 && bytes > 0);
360}
reed@google.comb89a6432011-02-07 13:20:30 +0000361
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000362void GrResourceCache::validate() const {
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000363 fList.validate();
364 fExclusiveList.validate();
reed@google.comb89a6432011-02-07 13:20:30 +0000365 GrAssert(both_zero_or_nonzero(fEntryCount, fEntryBytes));
366 GrAssert(both_zero_or_nonzero(fClientDetachedCount, fClientDetachedBytes));
reed@google.comac10a2d2010-12-22 21:39:39 +0000367 GrAssert(fClientDetachedBytes <= fEntryBytes);
368 GrAssert(fClientDetachedCount <= fEntryCount);
369 GrAssert((fEntryCount - fClientDetachedCount) == fCache.count());
reed@google.com01804b42011-01-18 21:50:41 +0000370
reed@google.comac10a2d2010-12-22 21:39:39 +0000371 fCache.validate();
372
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000373
bsalomon@google.coma2921122012-08-28 12:34:17 +0000374 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000375
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000376 // check that the exclusively held entries are okay
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000377 const GrResourceEntry* entry = iter.init(const_cast<EntryList&>(fExclusiveList),
bsalomon@google.coma2921122012-08-28 12:34:17 +0000378 EntryList::Iter::kHead_IterStart);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000379
380 for ( ; NULL != entry; entry = iter.next()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000381 entry->validate();
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000382 }
383
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000384 // check that the shareable entries are okay
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000385 entry = iter.init(const_cast<EntryList&>(fList), EntryList::Iter::kHead_IterStart);
386
387 int count = 0;
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000388 for ( ; NULL != entry; entry = iter.next()) {
389 entry->validate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000390 GrAssert(fCache.find(entry->key()));
391 count += 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000392 }
393 GrAssert(count == fEntryCount - fClientDetachedCount);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000394
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000395 size_t bytes = countBytes(fList);
reed@google.comac10a2d2010-12-22 21:39:39 +0000396 GrAssert(bytes == fEntryBytes - fClientDetachedBytes);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000397
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000398 bytes = countBytes(fExclusiveList);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000399 GrAssert(bytes == fClientDetachedBytes);
400
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000401 GrAssert(fList.countEntries() == fEntryCount - fClientDetachedCount);
402
403 GrAssert(fExclusiveList.countEntries() == fClientDetachedCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000404}
robertphillips@google.com59552022012-08-31 13:07:37 +0000405#endif // GR_DEBUG
406
407#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000408
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000409void GrResourceCache::printStats() {
410 int locked = 0;
411
412 EntryList::Iter iter;
413
414 GrResourceEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart);
415
416 for ( ; NULL != entry; entry = iter.prev()) {
417 if (entry->fResource->getRefCnt() > 1) {
418 ++locked;
419 }
420 }
421
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000422 SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000423 SkDebugf("\t\tEntry Count: current %d (%d locked) high %d\n",
424 fEntryCount, locked, fHighWaterEntryCount);
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000425 SkDebugf("\t\tEntry Bytes: current %d high %d\n",
426 fEntryBytes, fHighWaterEntryBytes);
427 SkDebugf("\t\tDetached Entry Count: current %d high %d\n",
428 fClientDetachedCount, fHighWaterClientDetachedCount);
429 SkDebugf("\t\tDetached Bytes: current %d high %d\n",
430 fClientDetachedBytes, fHighWaterClientDetachedBytes);
431}
432
reed@google.comac10a2d2010-12-22 21:39:39 +0000433#endif
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000434
435///////////////////////////////////////////////////////////////////////////////