blob: ca69ce44ce6731c8ef319546d35a35d84fb5ca93 [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.com5f9f2f52012-08-22 10:57:05 +000071#if GR_DEBUG
72 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
140#if GR_DEBUG
141 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,
reed@google.comac10a2d2010-12-22 21:39:39 +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.com5f9f2f52012-08-22 10:57:05 +0000161#if GR_DEBUG
162 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
176#if GR_DEBUG
177 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.com1f47f4f2012-08-16 14:49:16 +0000198GrResource* GrResourceCache::findAndLock(const GrResourceKey& key,
199 LockType type) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000200 GrAutoResourceCacheValidate atcv(this);
reed@google.comac10a2d2010-12-22 21:39:39 +0000201
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000202 GrResourceEntry* entry = fCache.find(key);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000203 if (NULL == entry) {
204 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000205 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000206
207 this->internalDetach(entry, false);
208 // mark the entry as "busy" so it doesn't get purged
209 // do this between detach and attach for locked count tracking
210 if (kNested_LockType == type || !entry->isLocked()) {
211 entry->lock();
212 }
213 this->attachToHead(entry, false);
214
215 return entry->fResource;
reed@google.comac10a2d2010-12-22 21:39:39 +0000216}
217
bsalomon@google.comfb309512011-11-30 14:13:48 +0000218bool GrResourceCache::hasKey(const GrResourceKey& key) const {
219 return NULL != fCache.find(key);
220}
221
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000222GrResourceEntry* GrResourceCache::create(const GrResourceKey& key,
223 GrResource* resource,
robertphillips@google.com386319e2012-06-27 14:59:18 +0000224 bool lock,
225 bool clientReattach) {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000226 // we don't expect to create new resources during a purge. In theory
227 // this could cause purgeAsNeeded() into an infinite loop (e.g.
228 // each resource destroyed creates and locks 2 resources and
229 // unlocks 1 thereby causing a new purge).
230 GrAssert(!fPurging);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000231 GrAutoResourceCacheValidate atcv(this);
reed@google.comac10a2d2010-12-22 21:39:39 +0000232
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000233 GrResourceEntry* entry = SkNEW_ARGS(GrResourceEntry, (key, resource));
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000234 resource->setCacheEntry(entry);
235
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000236 if (lock) {
237 // mark the entry as "busy" so it doesn't get purged
238 // do this before attach for locked count tracking
239 entry->lock();
240 }
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000241
robertphillips@google.com386319e2012-06-27 14:59:18 +0000242 this->attachToHead(entry, clientReattach);
reed@google.comac10a2d2010-12-22 21:39:39 +0000243 fCache.insert(key, entry);
244
245#if GR_DUMP_TEXTURE_UPLOAD
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000246 GrPrintf("--- add resource to cache %p, count=%d bytes= %d %d\n",
247 entry, fEntryCount, resource->sizeInBytes(), fEntryBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000248#endif
249
reed@google.comac10a2d2010-12-22 21:39:39 +0000250 this->purgeAsNeeded();
251 return entry;
252}
253
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000254void GrResourceCache::createAndLock(const GrResourceKey& key,
255 GrResource* resource) {
256 GrAssert(NULL == resource->getCacheEntry());
257 this->create(key, resource, true, false);
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000258}
259
260void GrResourceCache::attach(const GrResourceKey& key,
261 GrResource* resource) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000262 GrAssert(NULL == resource->getCacheEntry());
robertphillips@google.com386319e2012-06-27 14:59:18 +0000263 this->create(key, resource, false, true);
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000264}
265
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000266void GrResourceCache::makeExclusive(GrResourceEntry* entry) {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000267 GrAutoResourceCacheValidate atcv(this);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000268
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000269 this->internalDetach(entry, true);
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000270 fCache.remove(entry->key(), entry);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000271
272#if GR_DEBUG
273 fExclusiveList.addToHead(entry);
274#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000275}
276
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000277void GrResourceCache::removeInvalidResource(GrResourceEntry* entry) {
278 // If the resource went invalid while it was detached then purge it
279 // This can happen when a 3D context was lost,
280 // the client called GrContext::contextDestroyed() to notify Gr,
281 // and then later an SkGpuDevice's destructor releases its backing
282 // texture (which was invalidated at contextDestroyed time).
283 fClientDetachedCount -= 1;
284 fEntryCount -= 1;
285 size_t size = entry->resource()->sizeInBytes();
286 fClientDetachedBytes -= size;
287 fEntryBytes -= size;
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000288}
289
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000290void GrResourceCache::makeNonExclusive(GrResourceEntry* entry) {
bsalomon@google.com60879752011-09-15 20:43:53 +0000291 GrAutoResourceCacheValidate atcv(this);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000292
293#if GR_DEBUG
294 fExclusiveList.remove(entry);
295#endif
296
bsalomon@google.com60879752011-09-15 20:43:53 +0000297 if (entry->resource()->isValid()) {
298 attachToHead(entry, true);
299 fCache.insert(entry->key(), entry);
300 } else {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000301 this->removeInvalidResource(entry);
bsalomon@google.com60879752011-09-15 20:43:53 +0000302 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000303}
304
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000305void GrResourceCache::lock(GrResourceEntry* entry) {
306 GrAutoResourceCacheValidate atcv(this);
307
308 GrAssert(entry);
309 GrAssert(fCache.find(entry->key()));
310
311 if (!entry->isLocked()) {
312 --fUnlockedEntryCount;
313 }
314
315 entry->lock();
316}
317
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000318void GrResourceCache::unlock(GrResourceEntry* entry) {
319 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +0000320
reed@google.comac10a2d2010-12-22 21:39:39 +0000321 GrAssert(entry);
322 GrAssert(entry->isLocked());
323 GrAssert(fCache.find(entry->key()));
324
325 entry->unlock();
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000326 if (!entry->isLocked()) {
327 ++fUnlockedEntryCount;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000328#if GR_DEBUG
329 if (fHighWaterUnlockedEntryCount < fUnlockedEntryCount) {
330 fHighWaterUnlockedEntryCount = fUnlockedEntryCount;
331 }
332#endif
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000333 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000334 this->purgeAsNeeded();
335}
336
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000337/**
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000338 * Destroying a resource may potentially trigger the unlock of additional
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000339 * resources which in turn will trigger a nested purge. We block the nested
340 * purge using the fPurging variable. However, the initial purge will keep
341 * looping until either all resources in the cache are unlocked or we've met
342 * the budget. There is an assertion in createAndLock to check against a
343 * resource's destructor inserting new resources into the cache. If these
344 * new resources were unlocked before purgeAsNeeded completed it could
345 * potentially make purgeAsNeeded loop infinitely.
346 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000347void GrResourceCache::purgeAsNeeded() {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000348 if (!fPurging) {
349 fPurging = true;
350 bool withinBudget = false;
351 do {
bsalomon@google.coma2921122012-08-28 12:34:17 +0000352 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000353
354 // Note: the following code relies on the fact that the
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000355 // doubly linked list doesn't invalidate its data/pointers
356 // outside of the specific area where a deletion occurs (e.g.,
357 // in internalDetach)
bsalomon@google.coma2921122012-08-28 12:34:17 +0000358 GrResourceEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000359
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000360 while (entry && fUnlockedEntryCount) {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000361 GrAutoResourceCacheValidate atcv(this);
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000362 if (fEntryCount <= fMaxCount && fEntryBytes <= fMaxBytes) {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000363 withinBudget = true;
364 break;
365 }
reed@google.com01804b42011-01-18 21:50:41 +0000366
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000367 GrResourceEntry* prev = iter.prev();
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000368 if (!entry->isLocked()) {
369 // remove from our cache
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000370 fCache.remove(entry->key(), entry);
reed@google.comac10a2d2010-12-22 21:39:39 +0000371
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000372 // remove from our llist
373 this->internalDetach(entry, false);
reed@google.com01804b42011-01-18 21:50:41 +0000374
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000375 #if GR_DUMP_TEXTURE_UPLOAD
376 GrPrintf("--- ~resource from cache %p [%d %d]\n",
377 entry->resource(),
378 entry->resource()->width(),
379 entry->resource()->height());
380 #endif
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000381
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000382 delete entry;
383 }
384 entry = prev;
385 }
386 } while (!withinBudget && fUnlockedEntryCount);
387 fPurging = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000388 }
389}
390
bsalomon@google.coma2921122012-08-28 12:34:17 +0000391void GrResourceCache::purgeAllUnlocked() {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000392 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +0000393
bsalomon@google.come9a98942011-08-22 17:06:16 +0000394 // we can have one GrResource holding a lock on another
395 // so we don't want to just do a simple loop kicking each
396 // entry out. Instead change the budget and purge.
reed@google.comac10a2d2010-12-22 21:39:39 +0000397
bsalomon@google.come9a98942011-08-22 17:06:16 +0000398 int savedMaxBytes = fMaxBytes;
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000399 int savedMaxCount = fMaxCount;
400 fMaxBytes = (size_t) -1;
401 fMaxCount = 0;
bsalomon@google.come9a98942011-08-22 17:06:16 +0000402 this->purgeAsNeeded();
403
twiz@google.com0ec107f2012-02-21 19:15:53 +0000404#if GR_DEBUG
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000405 GrAssert(fExclusiveList.countEntries() == fClientDetachedCount);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000406 GrAssert(countBytes(fExclusiveList) == fClientDetachedBytes);
bsalomon@google.come9a98942011-08-22 17:06:16 +0000407 GrAssert(!fUnlockedEntryCount);
twiz@google.com0ec107f2012-02-21 19:15:53 +0000408 if (!fCache.count()) {
409 // Items may have been detached from the cache (such as the backing
410 // texture for an SkGpuDevice). The above purge would not have removed
411 // them.
412 GrAssert(fEntryCount == fClientDetachedCount);
413 GrAssert(fEntryBytes == fClientDetachedBytes);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000414 GrAssert(fList.isEmpty());
twiz@google.com0ec107f2012-02-21 19:15:53 +0000415 }
416#endif
bsalomon@google.come9a98942011-08-22 17:06:16 +0000417
418 fMaxBytes = savedMaxBytes;
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000419 fMaxCount = savedMaxCount;
reed@google.comac10a2d2010-12-22 21:39:39 +0000420}
421
422///////////////////////////////////////////////////////////////////////////////
423
424#if GR_DEBUG
bsalomon@google.coma2921122012-08-28 12:34:17 +0000425size_t GrResourceCache::countBytes(const EntryList& list) {
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000426 size_t bytes = 0;
427
bsalomon@google.coma2921122012-08-28 12:34:17 +0000428 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000429
bsalomon@google.coma2921122012-08-28 12:34:17 +0000430 const GrResourceEntry* entry = iter.init(const_cast<EntryList&>(list),
431 EntryList::Iter::kTail_IterStart);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000432
433 for ( ; NULL != entry; entry = iter.prev()) {
434 bytes += entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000435 }
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000436 return bytes;
reed@google.comac10a2d2010-12-22 21:39:39 +0000437}
438
reed@google.comb89a6432011-02-07 13:20:30 +0000439static bool both_zero_or_nonzero(int count, size_t bytes) {
440 return (count == 0 && bytes == 0) || (count > 0 && bytes > 0);
441}
reed@google.comb89a6432011-02-07 13:20:30 +0000442
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000443void GrResourceCache::validate() const {
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000444 fList.validate();
445 fExclusiveList.validate();
reed@google.comb89a6432011-02-07 13:20:30 +0000446 GrAssert(both_zero_or_nonzero(fEntryCount, fEntryBytes));
447 GrAssert(both_zero_or_nonzero(fClientDetachedCount, fClientDetachedBytes));
reed@google.comac10a2d2010-12-22 21:39:39 +0000448 GrAssert(fClientDetachedBytes <= fEntryBytes);
449 GrAssert(fClientDetachedCount <= fEntryCount);
450 GrAssert((fEntryCount - fClientDetachedCount) == fCache.count());
reed@google.com01804b42011-01-18 21:50:41 +0000451
reed@google.comac10a2d2010-12-22 21:39:39 +0000452 fCache.validate();
453
reed@google.comac10a2d2010-12-22 21:39:39 +0000454 int count = 0;
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000455 int unlockCount = 0;
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000456
bsalomon@google.coma2921122012-08-28 12:34:17 +0000457 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000458
bsalomon@google.coma2921122012-08-28 12:34:17 +0000459 const GrResourceEntry* entry = iter.init(const_cast<EntryList&>(fList),
460 EntryList::Iter::kHead_IterStart);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000461
462 for ( ; NULL != entry; entry = iter.next()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000463 entry->validate();
464 GrAssert(fCache.find(entry->key()));
465 count += 1;
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000466 if (!entry->isLocked()) {
467 unlockCount += 1;
468 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000469 }
470 GrAssert(count == fEntryCount - fClientDetachedCount);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000471
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000472 size_t bytes = countBytes(fList);
reed@google.comac10a2d2010-12-22 21:39:39 +0000473 GrAssert(bytes == fEntryBytes - fClientDetachedBytes);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000474
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000475 bytes = countBytes(fExclusiveList);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000476 GrAssert(bytes == fClientDetachedBytes);
477
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000478 GrAssert(unlockCount == fUnlockedEntryCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000479
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000480 GrAssert(fList.countEntries() == fEntryCount - fClientDetachedCount);
481
482 GrAssert(fExclusiveList.countEntries() == fClientDetachedCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000483}
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000484
485void GrResourceCache::printStats() const {
486 SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes);
487 SkDebugf("\t\tEntry Count: current %d high %d\n",
488 fEntryCount, fHighWaterEntryCount);
489 SkDebugf("\t\tUnlocked Entry Count: current %d high %d\n",
490 fUnlockedEntryCount, fHighWaterUnlockedEntryCount);
491 SkDebugf("\t\tEntry Bytes: current %d high %d\n",
492 fEntryBytes, fHighWaterEntryBytes);
493 SkDebugf("\t\tDetached Entry Count: current %d high %d\n",
494 fClientDetachedCount, fHighWaterClientDetachedCount);
495 SkDebugf("\t\tDetached Bytes: current %d high %d\n",
496 fClientDetachedBytes, fHighWaterClientDetachedBytes);
497}
498
reed@google.comac10a2d2010-12-22 21:39:39 +0000499#endif
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000500
501///////////////////////////////////////////////////////////////////////////////