blob: 777470b00da4bf5f631411ccd28966328d54de95 [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.com1f47f4f2012-08-16 14:49:16 +0000187GrResource* GrResourceCache::findAndLock(const GrResourceKey& key,
188 LockType type) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000189 GrAutoResourceCacheValidate atcv(this);
reed@google.comac10a2d2010-12-22 21:39:39 +0000190
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000191 GrResourceEntry* entry = fCache.find(key);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000192 if (NULL == entry) {
193 return NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000194 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000195
196 this->internalDetach(entry, false);
197 // mark the entry as "busy" so it doesn't get purged
198 // do this between detach and attach for locked count tracking
199 if (kNested_LockType == type || !entry->isLocked()) {
200 entry->lock();
201 }
202 this->attachToHead(entry, false);
203
204 return entry->fResource;
reed@google.comac10a2d2010-12-22 21:39:39 +0000205}
206
bsalomon@google.comfb309512011-11-30 14:13:48 +0000207bool GrResourceCache::hasKey(const GrResourceKey& key) const {
208 return NULL != fCache.find(key);
209}
210
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000211GrResourceEntry* GrResourceCache::create(const GrResourceKey& key,
212 GrResource* resource,
robertphillips@google.com386319e2012-06-27 14:59:18 +0000213 bool lock,
214 bool clientReattach) {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000215 // we don't expect to create new resources during a purge. In theory
216 // this could cause purgeAsNeeded() into an infinite loop (e.g.
217 // each resource destroyed creates and locks 2 resources and
218 // unlocks 1 thereby causing a new purge).
219 GrAssert(!fPurging);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000220 GrAutoResourceCacheValidate atcv(this);
reed@google.comac10a2d2010-12-22 21:39:39 +0000221
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000222 GrResourceEntry* entry = SkNEW_ARGS(GrResourceEntry, (key, resource));
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000223 resource->setCacheEntry(entry);
224
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000225 if (lock) {
226 // mark the entry as "busy" so it doesn't get purged
227 // do this before attach for locked count tracking
228 entry->lock();
229 }
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000230
robertphillips@google.com386319e2012-06-27 14:59:18 +0000231 this->attachToHead(entry, clientReattach);
reed@google.comac10a2d2010-12-22 21:39:39 +0000232 fCache.insert(key, entry);
233
234#if GR_DUMP_TEXTURE_UPLOAD
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000235 GrPrintf("--- add resource to cache %p, count=%d bytes= %d %d\n",
236 entry, fEntryCount, resource->sizeInBytes(), fEntryBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000237#endif
238
reed@google.comac10a2d2010-12-22 21:39:39 +0000239 this->purgeAsNeeded();
240 return entry;
241}
242
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000243void GrResourceCache::createAndLock(const GrResourceKey& key,
244 GrResource* resource) {
245 GrAssert(NULL == resource->getCacheEntry());
246 this->create(key, resource, true, false);
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000247}
248
249void GrResourceCache::attach(const GrResourceKey& key,
250 GrResource* resource) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000251 GrAssert(NULL == resource->getCacheEntry());
robertphillips@google.com386319e2012-06-27 14:59:18 +0000252 this->create(key, resource, false, true);
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000253}
254
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000255void GrResourceCache::makeExclusive(GrResourceEntry* entry) {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000256 GrAutoResourceCacheValidate atcv(this);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000257
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000258 this->internalDetach(entry, true);
reed@google.comac10a2d2010-12-22 21:39:39 +0000259 fCache.remove(entry->fKey, entry);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000260
261#if GR_DEBUG
262 fExclusiveList.addToHead(entry);
263#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000264}
265
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000266void GrResourceCache::removeInvalidResource(GrResourceEntry* entry) {
267 // If the resource went invalid while it was detached then purge it
268 // This can happen when a 3D context was lost,
269 // the client called GrContext::contextDestroyed() to notify Gr,
270 // and then later an SkGpuDevice's destructor releases its backing
271 // texture (which was invalidated at contextDestroyed time).
272 fClientDetachedCount -= 1;
273 fEntryCount -= 1;
274 size_t size = entry->resource()->sizeInBytes();
275 fClientDetachedBytes -= size;
276 fEntryBytes -= size;
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000277}
278
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000279void GrResourceCache::makeNonExclusive(GrResourceEntry* entry) {
bsalomon@google.com60879752011-09-15 20:43:53 +0000280 GrAutoResourceCacheValidate atcv(this);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000281
282#if GR_DEBUG
283 fExclusiveList.remove(entry);
284#endif
285
bsalomon@google.com60879752011-09-15 20:43:53 +0000286 if (entry->resource()->isValid()) {
287 attachToHead(entry, true);
288 fCache.insert(entry->key(), entry);
289 } else {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000290 this->removeInvalidResource(entry);
bsalomon@google.com60879752011-09-15 20:43:53 +0000291 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000292}
293
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000294void GrResourceCache::unlock(GrResourceEntry* entry) {
295 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +0000296
reed@google.comac10a2d2010-12-22 21:39:39 +0000297 GrAssert(entry);
298 GrAssert(entry->isLocked());
299 GrAssert(fCache.find(entry->key()));
300
301 entry->unlock();
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000302 if (!entry->isLocked()) {
303 ++fUnlockedEntryCount;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000304#if GR_DEBUG
305 if (fHighWaterUnlockedEntryCount < fUnlockedEntryCount) {
306 fHighWaterUnlockedEntryCount = fUnlockedEntryCount;
307 }
308#endif
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000309 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000310 this->purgeAsNeeded();
311}
312
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000313/**
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000314 * Destroying a resource may potentially trigger the unlock of additional
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000315 * resources which in turn will trigger a nested purge. We block the nested
316 * purge using the fPurging variable. However, the initial purge will keep
317 * looping until either all resources in the cache are unlocked or we've met
318 * the budget. There is an assertion in createAndLock to check against a
319 * resource's destructor inserting new resources into the cache. If these
320 * new resources were unlocked before purgeAsNeeded completed it could
321 * potentially make purgeAsNeeded loop infinitely.
322 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000323void GrResourceCache::purgeAsNeeded() {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000324 if (!fPurging) {
325 fPurging = true;
326 bool withinBudget = false;
327 do {
bsalomon@google.coma2921122012-08-28 12:34:17 +0000328 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000329
330 // Note: the following code relies on the fact that the
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000331 // doubly linked list doesn't invalidate its data/pointers
332 // outside of the specific area where a deletion occurs (e.g.,
333 // in internalDetach)
bsalomon@google.coma2921122012-08-28 12:34:17 +0000334 GrResourceEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000335
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000336 while (entry && fUnlockedEntryCount) {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000337 GrAutoResourceCacheValidate atcv(this);
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000338 if (fEntryCount <= fMaxCount && fEntryBytes <= fMaxBytes) {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000339 withinBudget = true;
340 break;
341 }
reed@google.com01804b42011-01-18 21:50:41 +0000342
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000343 GrResourceEntry* prev = iter.prev();
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000344 if (!entry->isLocked()) {
345 // remove from our cache
346 fCache.remove(entry->fKey, entry);
reed@google.comac10a2d2010-12-22 21:39:39 +0000347
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000348 // remove from our llist
349 this->internalDetach(entry, false);
reed@google.com01804b42011-01-18 21:50:41 +0000350
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000351 #if GR_DUMP_TEXTURE_UPLOAD
352 GrPrintf("--- ~resource from cache %p [%d %d]\n",
353 entry->resource(),
354 entry->resource()->width(),
355 entry->resource()->height());
356 #endif
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000357
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000358 delete entry;
359 }
360 entry = prev;
361 }
362 } while (!withinBudget && fUnlockedEntryCount);
363 fPurging = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000364 }
365}
366
bsalomon@google.coma2921122012-08-28 12:34:17 +0000367void GrResourceCache::purgeAllUnlocked() {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000368 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +0000369
bsalomon@google.come9a98942011-08-22 17:06:16 +0000370 // we can have one GrResource holding a lock on another
371 // so we don't want to just do a simple loop kicking each
372 // entry out. Instead change the budget and purge.
reed@google.comac10a2d2010-12-22 21:39:39 +0000373
bsalomon@google.come9a98942011-08-22 17:06:16 +0000374 int savedMaxBytes = fMaxBytes;
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000375 int savedMaxCount = fMaxCount;
376 fMaxBytes = (size_t) -1;
377 fMaxCount = 0;
bsalomon@google.come9a98942011-08-22 17:06:16 +0000378 this->purgeAsNeeded();
379
twiz@google.com0ec107f2012-02-21 19:15:53 +0000380#if GR_DEBUG
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000381 GrAssert(fExclusiveList.countEntries() == fClientDetachedCount);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000382 GrAssert(countBytes(fExclusiveList) == fClientDetachedBytes);
bsalomon@google.come9a98942011-08-22 17:06:16 +0000383 GrAssert(!fUnlockedEntryCount);
twiz@google.com0ec107f2012-02-21 19:15:53 +0000384 if (!fCache.count()) {
385 // Items may have been detached from the cache (such as the backing
386 // texture for an SkGpuDevice). The above purge would not have removed
387 // them.
388 GrAssert(fEntryCount == fClientDetachedCount);
389 GrAssert(fEntryBytes == fClientDetachedBytes);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000390 GrAssert(fList.isEmpty());
twiz@google.com0ec107f2012-02-21 19:15:53 +0000391 }
392#endif
bsalomon@google.come9a98942011-08-22 17:06:16 +0000393
394 fMaxBytes = savedMaxBytes;
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000395 fMaxCount = savedMaxCount;
reed@google.comac10a2d2010-12-22 21:39:39 +0000396}
397
398///////////////////////////////////////////////////////////////////////////////
399
400#if GR_DEBUG
bsalomon@google.coma2921122012-08-28 12:34:17 +0000401size_t GrResourceCache::countBytes(const EntryList& list) {
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000402 size_t bytes = 0;
403
bsalomon@google.coma2921122012-08-28 12:34:17 +0000404 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000405
bsalomon@google.coma2921122012-08-28 12:34:17 +0000406 const GrResourceEntry* entry = iter.init(const_cast<EntryList&>(list),
407 EntryList::Iter::kTail_IterStart);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000408
409 for ( ; NULL != entry; entry = iter.prev()) {
410 bytes += entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000411 }
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000412 return bytes;
reed@google.comac10a2d2010-12-22 21:39:39 +0000413}
414
reed@google.comb89a6432011-02-07 13:20:30 +0000415static bool both_zero_or_nonzero(int count, size_t bytes) {
416 return (count == 0 && bytes == 0) || (count > 0 && bytes > 0);
417}
reed@google.comb89a6432011-02-07 13:20:30 +0000418
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000419void GrResourceCache::validate() const {
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000420 fList.validate();
421 fExclusiveList.validate();
reed@google.comb89a6432011-02-07 13:20:30 +0000422 GrAssert(both_zero_or_nonzero(fEntryCount, fEntryBytes));
423 GrAssert(both_zero_or_nonzero(fClientDetachedCount, fClientDetachedBytes));
reed@google.comac10a2d2010-12-22 21:39:39 +0000424 GrAssert(fClientDetachedBytes <= fEntryBytes);
425 GrAssert(fClientDetachedCount <= fEntryCount);
426 GrAssert((fEntryCount - fClientDetachedCount) == fCache.count());
reed@google.com01804b42011-01-18 21:50:41 +0000427
reed@google.comac10a2d2010-12-22 21:39:39 +0000428 fCache.validate();
429
reed@google.comac10a2d2010-12-22 21:39:39 +0000430 int count = 0;
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000431 int unlockCount = 0;
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000432
bsalomon@google.coma2921122012-08-28 12:34:17 +0000433 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000434
bsalomon@google.coma2921122012-08-28 12:34:17 +0000435 const GrResourceEntry* entry = iter.init(const_cast<EntryList&>(fList),
436 EntryList::Iter::kHead_IterStart);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000437
438 for ( ; NULL != entry; entry = iter.next()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000439 entry->validate();
440 GrAssert(fCache.find(entry->key()));
441 count += 1;
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000442 if (!entry->isLocked()) {
443 unlockCount += 1;
444 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000445 }
446 GrAssert(count == fEntryCount - fClientDetachedCount);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000447
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000448 size_t bytes = countBytes(fList);
reed@google.comac10a2d2010-12-22 21:39:39 +0000449 GrAssert(bytes == fEntryBytes - fClientDetachedBytes);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000450
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000451 bytes = countBytes(fExclusiveList);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000452 GrAssert(bytes == fClientDetachedBytes);
453
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000454 GrAssert(unlockCount == fUnlockedEntryCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000455
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000456 GrAssert(fList.countEntries() == fEntryCount - fClientDetachedCount);
457
458 GrAssert(fExclusiveList.countEntries() == fClientDetachedCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000459}
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000460
461void GrResourceCache::printStats() const {
462 SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes);
463 SkDebugf("\t\tEntry Count: current %d high %d\n",
464 fEntryCount, fHighWaterEntryCount);
465 SkDebugf("\t\tUnlocked Entry Count: current %d high %d\n",
466 fUnlockedEntryCount, fHighWaterUnlockedEntryCount);
467 SkDebugf("\t\tEntry Bytes: current %d high %d\n",
468 fEntryBytes, fHighWaterEntryBytes);
469 SkDebugf("\t\tDetached Entry Count: current %d high %d\n",
470 fClientDetachedCount, fHighWaterClientDetachedCount);
471 SkDebugf("\t\tDetached Bytes: current %d high %d\n",
472 fClientDetachedBytes, fHighWaterClientDetachedBytes);
473}
474
reed@google.comac10a2d2010-12-22 21:39:39 +0000475#endif
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000476
477///////////////////////////////////////////////////////////////////////////////