blob: 4b262d4b4d68533ee60061118e7649507cc13472 [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);
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +000020 resource->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +000021}
22
bsalomon@google.com50398bf2011-07-26 20:45:30 +000023GrResourceEntry::~GrResourceEntry() {
robertphillips@google.com521eaf82012-08-22 11:03:19 +000024 fResource->setCacheEntry(NULL);
bsalomon@google.com50398bf2011-07-26 20:45:30 +000025 fResource->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +000026}
27
28#if GR_DEBUG
bsalomon@google.com50398bf2011-07-26 20:45:30 +000029void GrResourceEntry::validate() const {
reed@google.comac10a2d2010-12-22 21:39:39 +000030 GrAssert(fLockCount >= 0);
bsalomon@google.com50398bf2011-07-26 20:45:30 +000031 GrAssert(fResource);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000032 GrAssert(fResource->getCacheEntry() == this);
bsalomon@google.com50398bf2011-07-26 20:45:30 +000033 fResource->validate();
reed@google.comac10a2d2010-12-22 21:39:39 +000034}
35#endif
36
37///////////////////////////////////////////////////////////////////////////////
38
bsalomon@google.coma2921122012-08-28 12:34:17 +000039class GrResourceCache::Key {
40 typedef GrResourceEntry T;
41
42 const GrResourceKey& fKey;
43public:
44 Key(const GrResourceKey& key) : fKey(key) {}
45
46 uint32_t getHash() const { return fKey.hashIndex(); }
47
48 static bool LT(const T& entry, const Key& key) {
49 return entry.key() < key.fKey;
50 }
51 static bool EQ(const T& entry, const Key& key) {
52 return entry.key() == key.fKey;
53 }
54#if GR_DEBUG
55 static uint32_t GetHash(const T& entry) {
56 return entry.key().hashIndex();
57 }
58 static bool LT(const T& a, const T& b) {
59 return a.key() < b.key();
60 }
61 static bool EQ(const T& a, const T& b) {
62 return a.key() == b.key();
63 }
64#endif
65};
66
67///////////////////////////////////////////////////////////////////////////////
68
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000069GrResourceCache::GrResourceCache(int maxCount, size_t maxBytes) :
70 fMaxCount(maxCount),
71 fMaxBytes(maxBytes) {
robertphillips@google.com59552022012-08-31 13:07:37 +000072#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +000073 fHighWaterEntryCount = 0;
74 fHighWaterUnlockedEntryCount = 0;
75 fHighWaterEntryBytes = 0;
76 fHighWaterClientDetachedCount = 0;
77 fHighWaterClientDetachedBytes = 0;
78#endif
79
80 fEntryCount = 0;
81 fUnlockedEntryCount = 0;
82 fEntryBytes = 0;
83 fClientDetachedCount = 0;
84 fClientDetachedBytes = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +000085
bsalomon@google.coma5a1da82011-08-05 14:02:41 +000086 fPurging = false;
reed@google.comac10a2d2010-12-22 21:39:39 +000087}
88
bsalomon@google.com50398bf2011-07-26 20:45:30 +000089GrResourceCache::~GrResourceCache() {
90 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +000091
bsalomon@google.coma2921122012-08-28 12:34:17 +000092 EntryList::Iter iter;
93
94 // Unlike the removeAll, here we really remove everything, including locked resources.
95 while (GrResourceEntry* entry = fList.head()) {
96 GrAutoResourceCacheValidate atcv(this);
97
98 // remove from our cache
99 fCache.remove(entry->fKey, entry);
100
101 // remove from our llist
102 this->internalDetach(entry, false);
103
104 delete entry;
105 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000106}
107
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000108void GrResourceCache::getLimits(int* maxResources, size_t* maxResourceBytes) const{
109 if (maxResources) {
110 *maxResources = fMaxCount;
111 }
112 if (maxResourceBytes) {
113 *maxResourceBytes = fMaxBytes;
114 }
115}
reed@google.com01804b42011-01-18 21:50:41 +0000116
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000117void GrResourceCache::setLimits(int maxResources, size_t maxResourceBytes) {
118 bool smaller = (maxResources < fMaxCount) || (maxResourceBytes < fMaxBytes);
119
120 fMaxCount = maxResources;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000121 fMaxBytes = maxResourceBytes;
reed@google.com01804b42011-01-18 21:50:41 +0000122
123 if (smaller) {
124 this->purgeAsNeeded();
125 }
126}
127
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000128void GrResourceCache::internalDetach(GrResourceEntry* entry,
reed@google.comac10a2d2010-12-22 21:39:39 +0000129 bool clientDetach) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000130 fList.remove(entry);
reed@google.comac10a2d2010-12-22 21:39:39 +0000131
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000132 if (!entry->isLocked()) {
133 --fUnlockedEntryCount;
134 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000135
136 // update our stats
137 if (clientDetach) {
138 fClientDetachedCount += 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000139 fClientDetachedBytes += entry->resource()->sizeInBytes();
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000140
robertphillips@google.com59552022012-08-31 13:07:37 +0000141#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000142 if (fHighWaterClientDetachedCount < fClientDetachedCount) {
143 fHighWaterClientDetachedCount = fClientDetachedCount;
144 }
145 if (fHighWaterClientDetachedBytes < fClientDetachedBytes) {
146 fHighWaterClientDetachedBytes = fClientDetachedBytes;
147 }
148#endif
149
reed@google.comac10a2d2010-12-22 21:39:39 +0000150 } else {
151 fEntryCount -= 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000152 fEntryBytes -= entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000153 }
154}
155
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000156void GrResourceCache::attachToHead(GrResourceEntry* entry,
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000157 bool clientReattach) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000158 fList.addToHead(entry);
159
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000160 if (!entry->isLocked()) {
161 ++fUnlockedEntryCount;
robertphillips@google.com59552022012-08-31 13:07:37 +0000162#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000163 if (fHighWaterUnlockedEntryCount < fUnlockedEntryCount) {
164 fHighWaterUnlockedEntryCount = fUnlockedEntryCount;
165 }
166#endif
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000167 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000168
169 // update our stats
170 if (clientReattach) {
171 fClientDetachedCount -= 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000172 fClientDetachedBytes -= entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000173 } else {
174 fEntryCount += 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000175 fEntryBytes += entry->resource()->sizeInBytes();
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000176
robertphillips@google.com59552022012-08-31 13:07:37 +0000177#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000178 if (fHighWaterEntryCount < fEntryCount) {
179 fHighWaterEntryCount = fEntryCount;
180 }
181 if (fHighWaterEntryBytes < fEntryBytes) {
182 fHighWaterEntryBytes = fEntryBytes;
183 }
184#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000185 }
186}
187
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000188GrResource* GrResourceCache::find(const GrResourceKey& key) {
189 GrAutoResourceCacheValidate atcv(this);
190
191 GrResourceEntry* entry = fCache.find(key);
192 if (NULL == entry) {
193 return NULL;
194 }
195
196 return entry->fResource;
197}
198
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000199GrResource* GrResourceCache::findAndLock(const GrResourceKey& key) {
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);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000208 this->attachToHead(entry, false);
209
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000210 this->lock(entry);
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000211
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000212 return entry->fResource;
reed@google.comac10a2d2010-12-22 21:39:39 +0000213}
214
bsalomon@google.comfb309512011-11-30 14:13:48 +0000215bool GrResourceCache::hasKey(const GrResourceKey& key) const {
216 return NULL != fCache.find(key);
217}
218
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000219void GrResourceCache::create(const GrResourceKey& key, GrResource* resource) {
220 GrAssert(NULL == resource->getCacheEntry());
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000221 // we don't expect to create new resources during a purge. In theory
222 // this could cause purgeAsNeeded() into an infinite loop (e.g.
223 // each resource destroyed creates and locks 2 resources and
224 // unlocks 1 thereby causing a new purge).
225 GrAssert(!fPurging);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000226 GrAutoResourceCacheValidate atcv(this);
reed@google.comac10a2d2010-12-22 21:39:39 +0000227
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000228 GrResourceEntry* entry = SkNEW_ARGS(GrResourceEntry, (key, resource));
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000229 resource->setCacheEntry(entry);
230
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000231 this->attachToHead(entry, false);
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
reed@google.comac10a2d2010-12-22 21:39:39 +0000238}
239
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000240void GrResourceCache::createAndLock(const GrResourceKey& key,
241 GrResource* resource) {
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000242 this->create(key, resource);
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000243
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000244 GrAssert(NULL != resource->getCacheEntry());
245 this->lock(resource->getCacheEntry());
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000246}
247
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000248void GrResourceCache::makeExclusive(GrResourceEntry* entry) {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000249 GrAutoResourceCacheValidate atcv(this);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000250
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000251 this->internalDetach(entry, true);
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000252 fCache.remove(entry->key(), entry);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000253
254#if GR_DEBUG
255 fExclusiveList.addToHead(entry);
256#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000257}
258
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000259void GrResourceCache::removeInvalidResource(GrResourceEntry* entry) {
260 // If the resource went invalid while it was detached then purge it
261 // This can happen when a 3D context was lost,
262 // the client called GrContext::contextDestroyed() to notify Gr,
263 // and then later an SkGpuDevice's destructor releases its backing
264 // texture (which was invalidated at contextDestroyed time).
265 fClientDetachedCount -= 1;
266 fEntryCount -= 1;
267 size_t size = entry->resource()->sizeInBytes();
268 fClientDetachedBytes -= size;
269 fEntryBytes -= size;
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000270}
271
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000272void GrResourceCache::makeNonExclusive(GrResourceEntry* entry) {
bsalomon@google.com60879752011-09-15 20:43:53 +0000273 GrAutoResourceCacheValidate atcv(this);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000274
275#if GR_DEBUG
276 fExclusiveList.remove(entry);
277#endif
278
bsalomon@google.com60879752011-09-15 20:43:53 +0000279 if (entry->resource()->isValid()) {
280 attachToHead(entry, true);
281 fCache.insert(entry->key(), entry);
282 } else {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000283 this->removeInvalidResource(entry);
bsalomon@google.com60879752011-09-15 20:43:53 +0000284 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000285}
286
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000287void GrResourceCache::lock(GrResourceEntry* entry) {
288 GrAutoResourceCacheValidate atcv(this);
289
290 GrAssert(entry);
291 GrAssert(fCache.find(entry->key()));
292
293 if (!entry->isLocked()) {
294 --fUnlockedEntryCount;
295 }
296
297 entry->lock();
298}
299
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000300void GrResourceCache::unlock(GrResourceEntry* entry) {
301 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +0000302
reed@google.comac10a2d2010-12-22 21:39:39 +0000303 GrAssert(entry);
304 GrAssert(entry->isLocked());
305 GrAssert(fCache.find(entry->key()));
306
307 entry->unlock();
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000308 if (!entry->isLocked()) {
309 ++fUnlockedEntryCount;
robertphillips@google.com59552022012-08-31 13:07:37 +0000310#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000311 if (fHighWaterUnlockedEntryCount < fUnlockedEntryCount) {
312 fHighWaterUnlockedEntryCount = fUnlockedEntryCount;
313 }
314#endif
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000315 }
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000316
reed@google.comac10a2d2010-12-22 21:39:39 +0000317 this->purgeAsNeeded();
318}
319
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000320/**
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000321 * Destroying a resource may potentially trigger the unlock of additional
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000322 * resources which in turn will trigger a nested purge. We block the nested
323 * purge using the fPurging variable. However, the initial purge will keep
324 * looping until either all resources in the cache are unlocked or we've met
325 * the budget. There is an assertion in createAndLock to check against a
326 * resource's destructor inserting new resources into the cache. If these
327 * new resources were unlocked before purgeAsNeeded completed it could
328 * potentially make purgeAsNeeded loop infinitely.
329 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000330void GrResourceCache::purgeAsNeeded() {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000331 if (!fPurging) {
332 fPurging = true;
333 bool withinBudget = false;
robertphillips@google.com6fc95182012-09-04 13:36:31 +0000334 int priorUnlockedEntryCount = 0;
335
336 // The purging process is repeated several times since one pass
337 // may free up other resources
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000338 do {
bsalomon@google.coma2921122012-08-28 12:34:17 +0000339 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000340
robertphillips@google.com6fc95182012-09-04 13:36:31 +0000341 priorUnlockedEntryCount = fUnlockedEntryCount;
342
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000343 // Note: the following code relies on the fact that the
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000344 // doubly linked list doesn't invalidate its data/pointers
345 // outside of the specific area where a deletion occurs (e.g.,
346 // in internalDetach)
bsalomon@google.coma2921122012-08-28 12:34:17 +0000347 GrResourceEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000348
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000349 while (entry && fUnlockedEntryCount) {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000350 GrAutoResourceCacheValidate atcv(this);
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000351 if (fEntryCount <= fMaxCount && fEntryBytes <= fMaxBytes) {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000352 withinBudget = true;
353 break;
354 }
reed@google.com01804b42011-01-18 21:50:41 +0000355
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000356 GrResourceEntry* prev = iter.prev();
robertphillips@google.com6fc95182012-09-04 13:36:31 +0000357 if (!entry->isLocked() && entry->fResource->getRefCnt() == 1) {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000358 // remove from our cache
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000359 fCache.remove(entry->key(), entry);
reed@google.comac10a2d2010-12-22 21:39:39 +0000360
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000361 // remove from our llist
362 this->internalDetach(entry, false);
reed@google.com01804b42011-01-18 21:50:41 +0000363
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000364 #if GR_DUMP_TEXTURE_UPLOAD
365 GrPrintf("--- ~resource from cache %p [%d %d]\n",
366 entry->resource(),
367 entry->resource()->width(),
368 entry->resource()->height());
369 #endif
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000370
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000371 delete entry;
372 }
373 entry = prev;
374 }
robertphillips@google.com6fc95182012-09-04 13:36:31 +0000375 } while (!withinBudget && (fUnlockedEntryCount != priorUnlockedEntryCount));
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000376 fPurging = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000377 }
378}
379
bsalomon@google.coma2921122012-08-28 12:34:17 +0000380void GrResourceCache::purgeAllUnlocked() {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000381 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +0000382
bsalomon@google.come9a98942011-08-22 17:06:16 +0000383 // we can have one GrResource holding a lock on another
384 // so we don't want to just do a simple loop kicking each
385 // entry out. Instead change the budget and purge.
reed@google.comac10a2d2010-12-22 21:39:39 +0000386
bsalomon@google.come9a98942011-08-22 17:06:16 +0000387 int savedMaxBytes = fMaxBytes;
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000388 int savedMaxCount = fMaxCount;
389 fMaxBytes = (size_t) -1;
390 fMaxCount = 0;
bsalomon@google.come9a98942011-08-22 17:06:16 +0000391 this->purgeAsNeeded();
392
twiz@google.com0ec107f2012-02-21 19:15:53 +0000393#if GR_DEBUG
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000394 GrAssert(fExclusiveList.countEntries() == fClientDetachedCount);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000395 GrAssert(countBytes(fExclusiveList) == fClientDetachedBytes);
bsalomon@google.come9a98942011-08-22 17:06:16 +0000396 GrAssert(!fUnlockedEntryCount);
twiz@google.com0ec107f2012-02-21 19:15:53 +0000397 if (!fCache.count()) {
398 // Items may have been detached from the cache (such as the backing
399 // texture for an SkGpuDevice). The above purge would not have removed
400 // them.
401 GrAssert(fEntryCount == fClientDetachedCount);
402 GrAssert(fEntryBytes == fClientDetachedBytes);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000403 GrAssert(fList.isEmpty());
twiz@google.com0ec107f2012-02-21 19:15:53 +0000404 }
405#endif
bsalomon@google.come9a98942011-08-22 17:06:16 +0000406
407 fMaxBytes = savedMaxBytes;
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000408 fMaxCount = savedMaxCount;
reed@google.comac10a2d2010-12-22 21:39:39 +0000409}
410
411///////////////////////////////////////////////////////////////////////////////
412
413#if GR_DEBUG
bsalomon@google.coma2921122012-08-28 12:34:17 +0000414size_t GrResourceCache::countBytes(const EntryList& list) {
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000415 size_t bytes = 0;
416
bsalomon@google.coma2921122012-08-28 12:34:17 +0000417 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000418
bsalomon@google.coma2921122012-08-28 12:34:17 +0000419 const GrResourceEntry* entry = iter.init(const_cast<EntryList&>(list),
420 EntryList::Iter::kTail_IterStart);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000421
422 for ( ; NULL != entry; entry = iter.prev()) {
423 bytes += entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000424 }
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000425 return bytes;
reed@google.comac10a2d2010-12-22 21:39:39 +0000426}
427
reed@google.comb89a6432011-02-07 13:20:30 +0000428static bool both_zero_or_nonzero(int count, size_t bytes) {
429 return (count == 0 && bytes == 0) || (count > 0 && bytes > 0);
430}
reed@google.comb89a6432011-02-07 13:20:30 +0000431
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000432void GrResourceCache::validate() const {
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000433 fList.validate();
434 fExclusiveList.validate();
reed@google.comb89a6432011-02-07 13:20:30 +0000435 GrAssert(both_zero_or_nonzero(fEntryCount, fEntryBytes));
436 GrAssert(both_zero_or_nonzero(fClientDetachedCount, fClientDetachedBytes));
reed@google.comac10a2d2010-12-22 21:39:39 +0000437 GrAssert(fClientDetachedBytes <= fEntryBytes);
438 GrAssert(fClientDetachedCount <= fEntryCount);
439 GrAssert((fEntryCount - fClientDetachedCount) == fCache.count());
reed@google.com01804b42011-01-18 21:50:41 +0000440
reed@google.comac10a2d2010-12-22 21:39:39 +0000441 fCache.validate();
442
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000443
bsalomon@google.coma2921122012-08-28 12:34:17 +0000444 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000445
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000446 const GrResourceEntry* entry = iter.init(const_cast<EntryList&>(fExclusiveList),
bsalomon@google.coma2921122012-08-28 12:34:17 +0000447 EntryList::Iter::kHead_IterStart);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000448
449 for ( ; NULL != entry; entry = iter.next()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000450 entry->validate();
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000451 GrAssert(entry->isLocked());
452 }
453
454 entry = iter.init(const_cast<EntryList&>(fList), EntryList::Iter::kHead_IterStart);
455
456 int count = 0;
457 int unlockCount = 0;
458 for ( ; NULL != entry; entry = iter.next()) {
459 entry->validate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000460 GrAssert(fCache.find(entry->key()));
461 count += 1;
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000462 if (!entry->isLocked()) {
463 unlockCount += 1;
464 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000465 }
466 GrAssert(count == fEntryCount - fClientDetachedCount);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000467
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000468 size_t bytes = countBytes(fList);
reed@google.comac10a2d2010-12-22 21:39:39 +0000469 GrAssert(bytes == fEntryBytes - fClientDetachedBytes);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000470
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000471 bytes = countBytes(fExclusiveList);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000472 GrAssert(bytes == fClientDetachedBytes);
473
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000474 GrAssert(unlockCount == fUnlockedEntryCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000475
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000476 GrAssert(fList.countEntries() == fEntryCount - fClientDetachedCount);
477
478 GrAssert(fExclusiveList.countEntries() == fClientDetachedCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000479}
robertphillips@google.com59552022012-08-31 13:07:37 +0000480#endif // GR_DEBUG
481
482#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000483
484void GrResourceCache::printStats() const {
485 SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes);
486 SkDebugf("\t\tEntry Count: current %d high %d\n",
487 fEntryCount, fHighWaterEntryCount);
488 SkDebugf("\t\tUnlocked Entry Count: current %d high %d\n",
489 fUnlockedEntryCount, fHighWaterUnlockedEntryCount);
490 SkDebugf("\t\tEntry Bytes: current %d high %d\n",
491 fEntryBytes, fHighWaterEntryBytes);
492 SkDebugf("\t\tDetached Entry Count: current %d high %d\n",
493 fClientDetachedCount, fHighWaterClientDetachedCount);
494 SkDebugf("\t\tDetached Bytes: current %d high %d\n",
495 fClientDetachedBytes, fHighWaterClientDetachedBytes);
496}
497
reed@google.comac10a2d2010-12-22 21:39:39 +0000498#endif
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000499
500///////////////////////////////////////////////////////////////////////////////