blob: a1f1d794e5bcb7ea65fb78533bb159224349fb77 [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.com0b6ad222012-12-20 14:23:26 +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
robertphillips@google.com209a1142012-10-31 12:25:21 +000097 this->internalDetach(entry);
bsalomon@google.coma2921122012-08-28 12:34:17 +000098
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,
robertphillips@google.com209a1142012-10-31 12:25:21 +0000124 BudgetBehaviors behavior) {
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
robertphillips@google.com209a1142012-10-31 12:25:21 +0000128 if (kIgnore_BudgetBehavior == behavior) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000129 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 {
robertphillips@google.com209a1142012-10-31 12:25:21 +0000142 GrAssert(kAccountFor_BudgetBehavior == behavior);
143
reed@google.comac10a2d2010-12-22 21:39:39 +0000144 fEntryCount -= 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000145 fEntryBytes -= entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000146 }
147}
148
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000149void GrResourceCache::attachToHead(GrResourceEntry* entry,
robertphillips@google.com209a1142012-10-31 12:25:21 +0000150 BudgetBehaviors behavior) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000151 fList.addToHead(entry);
152
reed@google.comac10a2d2010-12-22 21:39:39 +0000153 // update our stats
robertphillips@google.com209a1142012-10-31 12:25:21 +0000154 if (kIgnore_BudgetBehavior == behavior) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000155 fClientDetachedCount -= 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000156 fClientDetachedBytes -= entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000157 } else {
robertphillips@google.com209a1142012-10-31 12:25:21 +0000158 GrAssert(kAccountFor_BudgetBehavior == behavior);
159
reed@google.comac10a2d2010-12-22 21:39:39 +0000160 fEntryCount += 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000161 fEntryBytes += entry->resource()->sizeInBytes();
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000162
robertphillips@google.com59552022012-08-31 13:07:37 +0000163#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000164 if (fHighWaterEntryCount < fEntryCount) {
165 fHighWaterEntryCount = fEntryCount;
166 }
167 if (fHighWaterEntryBytes < fEntryBytes) {
168 fHighWaterEntryBytes = fEntryBytes;
169 }
170#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000171 }
172}
173
robertphillips@google.com209a1142012-10-31 12:25:21 +0000174// This functor just searches for an entry with only a single ref (from
175// the texture cache itself). Presumably in this situation no one else
176// is relying on the texture.
177class GrTFindUnreffedFunctor {
178public:
skia.committer@gmail.comf3dc1992012-11-01 02:01:27 +0000179 bool operator()(const GrResourceEntry* entry) const {
robertphillips@google.com209a1142012-10-31 12:25:21 +0000180 return 1 == entry->resource()->getRefCnt();
181 }
182};
183
184GrResource* GrResourceCache::find(const GrResourceKey& key, uint32_t ownershipFlags) {
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000185 GrAutoResourceCacheValidate atcv(this);
186
robertphillips@google.com209a1142012-10-31 12:25:21 +0000187 GrResourceEntry* entry = NULL;
188
189 if (ownershipFlags & kNoOtherOwners_OwnershipFlag) {
190 GrTFindUnreffedFunctor functor;
191
192 entry = fCache.find<GrTFindUnreffedFunctor>(key, functor);
193 } else {
194 entry = fCache.find(key);
195 }
196
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000197 if (NULL == entry) {
198 return NULL;
199 }
200
robertphillips@google.com209a1142012-10-31 12:25:21 +0000201 if (ownershipFlags & kHide_OwnershipFlag) {
202 this->makeExclusive(entry);
203 } else {
204 // Make this resource MRU
205 this->internalDetach(entry);
206 this->attachToHead(entry);
207 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000208
209 return entry->fResource;
reed@google.comac10a2d2010-12-22 21:39:39 +0000210}
211
bsalomon@google.comfb309512011-11-30 14:13:48 +0000212bool GrResourceCache::hasKey(const GrResourceKey& key) const {
213 return NULL != fCache.find(key);
214}
215
skia.committer@gmail.comf3dc1992012-11-01 02:01:27 +0000216void GrResourceCache::addResource(const GrResourceKey& key,
robertphillips@google.com209a1142012-10-31 12:25:21 +0000217 GrResource* resource,
218 uint32_t ownershipFlags) {
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000219 GrAssert(NULL == resource->getCacheEntry());
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000220 // we don't expect to create new resources during a purge. In theory
221 // this could cause purgeAsNeeded() into an infinite loop (e.g.
222 // each resource destroyed creates and locks 2 resources and
223 // unlocks 1 thereby causing a new purge).
224 GrAssert(!fPurging);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000225 GrAutoResourceCacheValidate atcv(this);
reed@google.comac10a2d2010-12-22 21:39:39 +0000226
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000227 GrResourceEntry* entry = SkNEW_ARGS(GrResourceEntry, (key, resource));
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000228 resource->setCacheEntry(entry);
229
robertphillips@google.com209a1142012-10-31 12:25:21 +0000230 this->attachToHead(entry);
reed@google.comac10a2d2010-12-22 21:39:39 +0000231 fCache.insert(key, entry);
232
233#if GR_DUMP_TEXTURE_UPLOAD
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000234 GrPrintf("--- add resource to cache %p, count=%d bytes= %d %d\n",
235 entry, fEntryCount, resource->sizeInBytes(), fEntryBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000236#endif
robertphillips@google.com209a1142012-10-31 12:25:21 +0000237
238 if (ownershipFlags & kHide_OwnershipFlag) {
239 this->makeExclusive(entry);
240 }
241
reed@google.comac10a2d2010-12-22 21:39:39 +0000242}
243
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000244void GrResourceCache::makeExclusive(GrResourceEntry* entry) {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000245 GrAutoResourceCacheValidate atcv(this);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000246
robertphillips@google.com209a1142012-10-31 12:25:21 +0000247 // When scratch textures are detached (to hide them from future finds) they
248 // still count against the resource budget
249 this->internalDetach(entry, kIgnore_BudgetBehavior);
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000250 fCache.remove(entry->key(), entry);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000251
252#if GR_DEBUG
253 fExclusiveList.addToHead(entry);
254#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000255}
256
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000257void GrResourceCache::removeInvalidResource(GrResourceEntry* entry) {
258 // If the resource went invalid while it was detached then purge it
259 // This can happen when a 3D context was lost,
260 // the client called GrContext::contextDestroyed() to notify Gr,
261 // and then later an SkGpuDevice's destructor releases its backing
262 // texture (which was invalidated at contextDestroyed time).
263 fClientDetachedCount -= 1;
264 fEntryCount -= 1;
265 size_t size = entry->resource()->sizeInBytes();
266 fClientDetachedBytes -= size;
267 fEntryBytes -= size;
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000268}
269
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000270void GrResourceCache::makeNonExclusive(GrResourceEntry* entry) {
bsalomon@google.com60879752011-09-15 20:43:53 +0000271 GrAutoResourceCacheValidate atcv(this);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000272
273#if GR_DEBUG
274 fExclusiveList.remove(entry);
275#endif
276
bsalomon@google.com60879752011-09-15 20:43:53 +0000277 if (entry->resource()->isValid()) {
skia.committer@gmail.comf3dc1992012-11-01 02:01:27 +0000278 // Since scratch textures still count against the cache budget even
279 // when they have been removed from the cache, re-adding them doesn't
robertphillips@google.com209a1142012-10-31 12:25:21 +0000280 // alter the budget information.
281 attachToHead(entry, kIgnore_BudgetBehavior);
bsalomon@google.com60879752011-09-15 20:43:53 +0000282 fCache.insert(entry->key(), entry);
283 } else {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000284 this->removeInvalidResource(entry);
bsalomon@google.com60879752011-09-15 20:43:53 +0000285 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000286}
287
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000288/**
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000289 * Destroying a resource may potentially trigger the unlock of additional
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000290 * resources which in turn will trigger a nested purge. We block the nested
291 * purge using the fPurging variable. However, the initial purge will keep
292 * looping until either all resources in the cache are unlocked or we've met
293 * the budget. There is an assertion in createAndLock to check against a
294 * resource's destructor inserting new resources into the cache. If these
295 * new resources were unlocked before purgeAsNeeded completed it could
296 * potentially make purgeAsNeeded loop infinitely.
297 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000298void GrResourceCache::purgeAsNeeded() {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000299 if (!fPurging) {
300 fPurging = true;
301 bool withinBudget = false;
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000302 bool changed = false;
robertphillips@google.com6fc95182012-09-04 13:36:31 +0000303
304 // The purging process is repeated several times since one pass
305 // may free up other resources
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000306 do {
bsalomon@google.coma2921122012-08-28 12:34:17 +0000307 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000308
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000309 changed = false;
robertphillips@google.com6fc95182012-09-04 13:36:31 +0000310
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000311 // Note: the following code relies on the fact that the
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000312 // doubly linked list doesn't invalidate its data/pointers
313 // outside of the specific area where a deletion occurs (e.g.,
314 // in internalDetach)
bsalomon@google.coma2921122012-08-28 12:34:17 +0000315 GrResourceEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000316
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000317 while (NULL != entry) {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000318 GrAutoResourceCacheValidate atcv(this);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000319
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000320 if (fEntryCount <= fMaxCount && fEntryBytes <= fMaxBytes) {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000321 withinBudget = true;
322 break;
323 }
reed@google.com01804b42011-01-18 21:50:41 +0000324
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000325 GrResourceEntry* prev = iter.prev();
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000326 if (1 == entry->fResource->getRefCnt()) {
327 changed = true;
328
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000329 // remove from our cache
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000330 fCache.remove(entry->key(), entry);
reed@google.comac10a2d2010-12-22 21:39:39 +0000331
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000332 // remove from our llist
robertphillips@google.com209a1142012-10-31 12:25:21 +0000333 this->internalDetach(entry);
reed@google.com01804b42011-01-18 21:50:41 +0000334
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000335 #if GR_DUMP_TEXTURE_UPLOAD
336 GrPrintf("--- ~resource from cache %p [%d %d]\n",
337 entry->resource(),
338 entry->resource()->width(),
339 entry->resource()->height());
340 #endif
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000341
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000342 delete entry;
343 }
344 entry = prev;
345 }
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000346 } while (!withinBudget && changed);
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000347 fPurging = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000348 }
349}
350
bsalomon@google.coma2921122012-08-28 12:34:17 +0000351void GrResourceCache::purgeAllUnlocked() {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000352 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +0000353
bsalomon@google.come9a98942011-08-22 17:06:16 +0000354 // we can have one GrResource holding a lock on another
355 // so we don't want to just do a simple loop kicking each
356 // entry out. Instead change the budget and purge.
reed@google.comac10a2d2010-12-22 21:39:39 +0000357
bsalomon@google.come9a98942011-08-22 17:06:16 +0000358 int savedMaxBytes = fMaxBytes;
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000359 int savedMaxCount = fMaxCount;
360 fMaxBytes = (size_t) -1;
361 fMaxCount = 0;
bsalomon@google.come9a98942011-08-22 17:06:16 +0000362 this->purgeAsNeeded();
363
twiz@google.com0ec107f2012-02-21 19:15:53 +0000364#if GR_DEBUG
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000365 GrAssert(fExclusiveList.countEntries() == fClientDetachedCount);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000366 GrAssert(countBytes(fExclusiveList) == fClientDetachedBytes);
twiz@google.com0ec107f2012-02-21 19:15:53 +0000367 if (!fCache.count()) {
368 // Items may have been detached from the cache (such as the backing
369 // texture for an SkGpuDevice). The above purge would not have removed
370 // them.
371 GrAssert(fEntryCount == fClientDetachedCount);
372 GrAssert(fEntryBytes == fClientDetachedBytes);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000373 GrAssert(fList.isEmpty());
twiz@google.com0ec107f2012-02-21 19:15:53 +0000374 }
375#endif
bsalomon@google.come9a98942011-08-22 17:06:16 +0000376
377 fMaxBytes = savedMaxBytes;
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000378 fMaxCount = savedMaxCount;
reed@google.comac10a2d2010-12-22 21:39:39 +0000379}
380
381///////////////////////////////////////////////////////////////////////////////
382
383#if GR_DEBUG
bsalomon@google.coma2921122012-08-28 12:34:17 +0000384size_t GrResourceCache::countBytes(const EntryList& list) {
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000385 size_t bytes = 0;
386
bsalomon@google.coma2921122012-08-28 12:34:17 +0000387 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000388
bsalomon@google.coma2921122012-08-28 12:34:17 +0000389 const GrResourceEntry* entry = iter.init(const_cast<EntryList&>(list),
390 EntryList::Iter::kTail_IterStart);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000391
392 for ( ; NULL != entry; entry = iter.prev()) {
393 bytes += entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000394 }
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000395 return bytes;
reed@google.comac10a2d2010-12-22 21:39:39 +0000396}
397
reed@google.comb89a6432011-02-07 13:20:30 +0000398static bool both_zero_or_nonzero(int count, size_t bytes) {
399 return (count == 0 && bytes == 0) || (count > 0 && bytes > 0);
400}
reed@google.comb89a6432011-02-07 13:20:30 +0000401
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000402void GrResourceCache::validate() const {
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000403 fList.validate();
404 fExclusiveList.validate();
reed@google.comb89a6432011-02-07 13:20:30 +0000405 GrAssert(both_zero_or_nonzero(fEntryCount, fEntryBytes));
406 GrAssert(both_zero_or_nonzero(fClientDetachedCount, fClientDetachedBytes));
reed@google.comac10a2d2010-12-22 21:39:39 +0000407 GrAssert(fClientDetachedBytes <= fEntryBytes);
408 GrAssert(fClientDetachedCount <= fEntryCount);
409 GrAssert((fEntryCount - fClientDetachedCount) == fCache.count());
reed@google.com01804b42011-01-18 21:50:41 +0000410
reed@google.comac10a2d2010-12-22 21:39:39 +0000411 fCache.validate();
412
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000413
bsalomon@google.coma2921122012-08-28 12:34:17 +0000414 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000415
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000416 // check that the exclusively held entries are okay
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000417 const GrResourceEntry* entry = iter.init(const_cast<EntryList&>(fExclusiveList),
bsalomon@google.coma2921122012-08-28 12:34:17 +0000418 EntryList::Iter::kHead_IterStart);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000419
420 for ( ; NULL != entry; entry = iter.next()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000421 entry->validate();
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000422 }
423
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000424 // check that the shareable entries are okay
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000425 entry = iter.init(const_cast<EntryList&>(fList), EntryList::Iter::kHead_IterStart);
426
427 int count = 0;
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000428 for ( ; NULL != entry; entry = iter.next()) {
429 entry->validate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000430 GrAssert(fCache.find(entry->key()));
431 count += 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000432 }
433 GrAssert(count == fEntryCount - fClientDetachedCount);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000434
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000435 size_t bytes = countBytes(fList);
reed@google.comac10a2d2010-12-22 21:39:39 +0000436 GrAssert(bytes == fEntryBytes - fClientDetachedBytes);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000437
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000438 bytes = countBytes(fExclusiveList);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000439 GrAssert(bytes == fClientDetachedBytes);
440
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000441 GrAssert(fList.countEntries() == fEntryCount - fClientDetachedCount);
442
443 GrAssert(fExclusiveList.countEntries() == fClientDetachedCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000444}
robertphillips@google.com59552022012-08-31 13:07:37 +0000445#endif // GR_DEBUG
446
447#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000448
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000449void GrResourceCache::printStats() {
450 int locked = 0;
451
452 EntryList::Iter iter;
453
454 GrResourceEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart);
455
456 for ( ; NULL != entry; entry = iter.prev()) {
457 if (entry->fResource->getRefCnt() > 1) {
458 ++locked;
459 }
460 }
461
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000462 SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000463 SkDebugf("\t\tEntry Count: current %d (%d locked) high %d\n",
464 fEntryCount, locked, fHighWaterEntryCount);
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000465 SkDebugf("\t\tEntry Bytes: current %d high %d\n",
466 fEntryBytes, fHighWaterEntryBytes);
467 SkDebugf("\t\tDetached Entry Count: current %d high %d\n",
468 fClientDetachedCount, fHighWaterClientDetachedCount);
469 SkDebugf("\t\tDetached Bytes: current %d high %d\n",
470 fClientDetachedBytes, fHighWaterClientDetachedBytes);
471}
472
reed@google.comac10a2d2010-12-22 21:39:39 +0000473#endif
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000474
475///////////////////////////////////////////////////////////////////////////////