blob: 45b999f3d0110bab643a2b9548a668c6ed532ee4 [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.com0797c2c2012-12-20 15:13:01 +000014
15GrResourceKey::ResourceType GrResourceKey::GenerateResourceType() {
16 static int32_t gNextType = 0;
17
18 int32_t type = sk_atomic_inc(&gNextType);
19 if (type >= (1 << 8 * sizeof(ResourceType))) {
20 GrCrash("Too many Resource Types");
21 }
22
23 return static_cast<ResourceType>(type);
24}
25
26///////////////////////////////////////////////////////////////////////////////
27
bsalomon@google.com50398bf2011-07-26 20:45:30 +000028GrResourceEntry::GrResourceEntry(const GrResourceKey& key, GrResource* resource)
29 : fKey(key), fResource(resource) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000030 // we assume ownership of the resource, and will unref it when we die
31 GrAssert(resource);
skia.committer@gmail.com6c778162012-09-06 02:01:13 +000032 resource->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +000033}
34
bsalomon@google.com50398bf2011-07-26 20:45:30 +000035GrResourceEntry::~GrResourceEntry() {
robertphillips@google.com521eaf82012-08-22 11:03:19 +000036 fResource->setCacheEntry(NULL);
bsalomon@google.com50398bf2011-07-26 20:45:30 +000037 fResource->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +000038}
39
40#if GR_DEBUG
bsalomon@google.com50398bf2011-07-26 20:45:30 +000041void GrResourceEntry::validate() const {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000042 GrAssert(fResource);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000043 GrAssert(fResource->getCacheEntry() == this);
bsalomon@google.com50398bf2011-07-26 20:45:30 +000044 fResource->validate();
reed@google.comac10a2d2010-12-22 21:39:39 +000045}
46#endif
47
48///////////////////////////////////////////////////////////////////////////////
49
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000050GrResourceCache::GrResourceCache(int maxCount, size_t maxBytes) :
51 fMaxCount(maxCount),
52 fMaxBytes(maxBytes) {
robertphillips@google.com59552022012-08-31 13:07:37 +000053#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +000054 fHighWaterEntryCount = 0;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +000055 fHighWaterEntryBytes = 0;
56 fHighWaterClientDetachedCount = 0;
57 fHighWaterClientDetachedBytes = 0;
58#endif
59
60 fEntryCount = 0;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +000061 fEntryBytes = 0;
62 fClientDetachedCount = 0;
63 fClientDetachedBytes = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +000064
bsalomon@google.coma5a1da82011-08-05 14:02:41 +000065 fPurging = false;
reed@google.comac10a2d2010-12-22 21:39:39 +000066}
67
bsalomon@google.com50398bf2011-07-26 20:45:30 +000068GrResourceCache::~GrResourceCache() {
69 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +000070
bsalomon@google.coma2921122012-08-28 12:34:17 +000071 EntryList::Iter iter;
72
73 // Unlike the removeAll, here we really remove everything, including locked resources.
74 while (GrResourceEntry* entry = fList.head()) {
75 GrAutoResourceCacheValidate atcv(this);
76
77 // remove from our cache
78 fCache.remove(entry->fKey, entry);
79
80 // remove from our llist
robertphillips@google.com209a1142012-10-31 12:25:21 +000081 this->internalDetach(entry);
bsalomon@google.coma2921122012-08-28 12:34:17 +000082
83 delete entry;
84 }
reed@google.comac10a2d2010-12-22 21:39:39 +000085}
86
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000087void GrResourceCache::getLimits(int* maxResources, size_t* maxResourceBytes) const{
88 if (maxResources) {
89 *maxResources = fMaxCount;
90 }
91 if (maxResourceBytes) {
92 *maxResourceBytes = fMaxBytes;
93 }
94}
reed@google.com01804b42011-01-18 21:50:41 +000095
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000096void GrResourceCache::setLimits(int maxResources, size_t maxResourceBytes) {
97 bool smaller = (maxResources < fMaxCount) || (maxResourceBytes < fMaxBytes);
98
99 fMaxCount = maxResources;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000100 fMaxBytes = maxResourceBytes;
reed@google.com01804b42011-01-18 21:50:41 +0000101
102 if (smaller) {
103 this->purgeAsNeeded();
104 }
105}
106
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000107void GrResourceCache::internalDetach(GrResourceEntry* entry,
robertphillips@google.com209a1142012-10-31 12:25:21 +0000108 BudgetBehaviors behavior) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000109 fList.remove(entry);
reed@google.comac10a2d2010-12-22 21:39:39 +0000110
reed@google.comac10a2d2010-12-22 21:39:39 +0000111 // update our stats
robertphillips@google.com209a1142012-10-31 12:25:21 +0000112 if (kIgnore_BudgetBehavior == behavior) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000113 fClientDetachedCount += 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000114 fClientDetachedBytes += entry->resource()->sizeInBytes();
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000115
robertphillips@google.com59552022012-08-31 13:07:37 +0000116#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000117 if (fHighWaterClientDetachedCount < fClientDetachedCount) {
118 fHighWaterClientDetachedCount = fClientDetachedCount;
119 }
120 if (fHighWaterClientDetachedBytes < fClientDetachedBytes) {
121 fHighWaterClientDetachedBytes = fClientDetachedBytes;
122 }
123#endif
124
reed@google.comac10a2d2010-12-22 21:39:39 +0000125 } else {
robertphillips@google.com209a1142012-10-31 12:25:21 +0000126 GrAssert(kAccountFor_BudgetBehavior == behavior);
127
reed@google.comac10a2d2010-12-22 21:39:39 +0000128 fEntryCount -= 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000129 fEntryBytes -= entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000130 }
131}
132
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000133void GrResourceCache::attachToHead(GrResourceEntry* entry,
robertphillips@google.com209a1142012-10-31 12:25:21 +0000134 BudgetBehaviors behavior) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000135 fList.addToHead(entry);
136
reed@google.comac10a2d2010-12-22 21:39:39 +0000137 // update our stats
robertphillips@google.com209a1142012-10-31 12:25:21 +0000138 if (kIgnore_BudgetBehavior == behavior) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000139 fClientDetachedCount -= 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000140 fClientDetachedBytes -= entry->resource()->sizeInBytes();
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();
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000146
robertphillips@google.com59552022012-08-31 13:07:37 +0000147#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000148 if (fHighWaterEntryCount < fEntryCount) {
149 fHighWaterEntryCount = fEntryCount;
150 }
151 if (fHighWaterEntryBytes < fEntryBytes) {
152 fHighWaterEntryBytes = fEntryBytes;
153 }
154#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000155 }
156}
157
robertphillips@google.com209a1142012-10-31 12:25:21 +0000158// This functor just searches for an entry with only a single ref (from
159// the texture cache itself). Presumably in this situation no one else
160// is relying on the texture.
161class GrTFindUnreffedFunctor {
162public:
skia.committer@gmail.comf3dc1992012-11-01 02:01:27 +0000163 bool operator()(const GrResourceEntry* entry) const {
robertphillips@google.com209a1142012-10-31 12:25:21 +0000164 return 1 == entry->resource()->getRefCnt();
165 }
166};
167
168GrResource* GrResourceCache::find(const GrResourceKey& key, uint32_t ownershipFlags) {
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000169 GrAutoResourceCacheValidate atcv(this);
170
robertphillips@google.com209a1142012-10-31 12:25:21 +0000171 GrResourceEntry* entry = NULL;
172
173 if (ownershipFlags & kNoOtherOwners_OwnershipFlag) {
174 GrTFindUnreffedFunctor functor;
175
176 entry = fCache.find<GrTFindUnreffedFunctor>(key, functor);
177 } else {
178 entry = fCache.find(key);
179 }
180
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000181 if (NULL == entry) {
182 return NULL;
183 }
184
robertphillips@google.com209a1142012-10-31 12:25:21 +0000185 if (ownershipFlags & kHide_OwnershipFlag) {
186 this->makeExclusive(entry);
187 } else {
188 // Make this resource MRU
189 this->internalDetach(entry);
190 this->attachToHead(entry);
191 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000192
193 return entry->fResource;
reed@google.comac10a2d2010-12-22 21:39:39 +0000194}
195
bsalomon@google.comfb309512011-11-30 14:13:48 +0000196bool GrResourceCache::hasKey(const GrResourceKey& key) const {
197 return NULL != fCache.find(key);
198}
199
skia.committer@gmail.comf3dc1992012-11-01 02:01:27 +0000200void GrResourceCache::addResource(const GrResourceKey& key,
robertphillips@google.com209a1142012-10-31 12:25:21 +0000201 GrResource* resource,
202 uint32_t ownershipFlags) {
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000203 GrAssert(NULL == resource->getCacheEntry());
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000204 // we don't expect to create new resources during a purge. In theory
205 // this could cause purgeAsNeeded() into an infinite loop (e.g.
206 // each resource destroyed creates and locks 2 resources and
207 // unlocks 1 thereby causing a new purge).
208 GrAssert(!fPurging);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000209 GrAutoResourceCacheValidate atcv(this);
reed@google.comac10a2d2010-12-22 21:39:39 +0000210
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000211 GrResourceEntry* entry = SkNEW_ARGS(GrResourceEntry, (key, resource));
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000212 resource->setCacheEntry(entry);
213
robertphillips@google.com209a1142012-10-31 12:25:21 +0000214 this->attachToHead(entry);
reed@google.comac10a2d2010-12-22 21:39:39 +0000215 fCache.insert(key, entry);
216
217#if GR_DUMP_TEXTURE_UPLOAD
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000218 GrPrintf("--- add resource to cache %p, count=%d bytes= %d %d\n",
219 entry, fEntryCount, resource->sizeInBytes(), fEntryBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000220#endif
robertphillips@google.com209a1142012-10-31 12:25:21 +0000221
222 if (ownershipFlags & kHide_OwnershipFlag) {
223 this->makeExclusive(entry);
224 }
225
reed@google.comac10a2d2010-12-22 21:39:39 +0000226}
227
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000228void GrResourceCache::makeExclusive(GrResourceEntry* entry) {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000229 GrAutoResourceCacheValidate atcv(this);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000230
robertphillips@google.com209a1142012-10-31 12:25:21 +0000231 // When scratch textures are detached (to hide them from future finds) they
232 // still count against the resource budget
233 this->internalDetach(entry, kIgnore_BudgetBehavior);
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000234 fCache.remove(entry->key(), entry);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000235
236#if GR_DEBUG
237 fExclusiveList.addToHead(entry);
238#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000239}
240
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000241void GrResourceCache::removeInvalidResource(GrResourceEntry* entry) {
242 // If the resource went invalid while it was detached then purge it
243 // This can happen when a 3D context was lost,
244 // the client called GrContext::contextDestroyed() to notify Gr,
245 // and then later an SkGpuDevice's destructor releases its backing
246 // texture (which was invalidated at contextDestroyed time).
247 fClientDetachedCount -= 1;
248 fEntryCount -= 1;
249 size_t size = entry->resource()->sizeInBytes();
250 fClientDetachedBytes -= size;
251 fEntryBytes -= size;
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000252}
253
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000254void GrResourceCache::makeNonExclusive(GrResourceEntry* entry) {
bsalomon@google.com60879752011-09-15 20:43:53 +0000255 GrAutoResourceCacheValidate atcv(this);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000256
257#if GR_DEBUG
258 fExclusiveList.remove(entry);
259#endif
260
bsalomon@google.com60879752011-09-15 20:43:53 +0000261 if (entry->resource()->isValid()) {
skia.committer@gmail.comf3dc1992012-11-01 02:01:27 +0000262 // Since scratch textures still count against the cache budget even
263 // when they have been removed from the cache, re-adding them doesn't
robertphillips@google.com209a1142012-10-31 12:25:21 +0000264 // alter the budget information.
265 attachToHead(entry, kIgnore_BudgetBehavior);
bsalomon@google.com60879752011-09-15 20:43:53 +0000266 fCache.insert(entry->key(), entry);
267 } else {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000268 this->removeInvalidResource(entry);
bsalomon@google.com60879752011-09-15 20:43:53 +0000269 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000270}
271
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000272/**
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000273 * Destroying a resource may potentially trigger the unlock of additional
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000274 * resources which in turn will trigger a nested purge. We block the nested
275 * purge using the fPurging variable. However, the initial purge will keep
276 * looping until either all resources in the cache are unlocked or we've met
277 * the budget. There is an assertion in createAndLock to check against a
278 * resource's destructor inserting new resources into the cache. If these
279 * new resources were unlocked before purgeAsNeeded completed it could
280 * potentially make purgeAsNeeded loop infinitely.
281 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000282void GrResourceCache::purgeAsNeeded() {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000283 if (!fPurging) {
284 fPurging = true;
285 bool withinBudget = false;
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000286 bool changed = false;
robertphillips@google.com6fc95182012-09-04 13:36:31 +0000287
288 // The purging process is repeated several times since one pass
289 // may free up other resources
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000290 do {
bsalomon@google.coma2921122012-08-28 12:34:17 +0000291 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000292
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000293 changed = false;
robertphillips@google.com6fc95182012-09-04 13:36:31 +0000294
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000295 // Note: the following code relies on the fact that the
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000296 // doubly linked list doesn't invalidate its data/pointers
297 // outside of the specific area where a deletion occurs (e.g.,
298 // in internalDetach)
bsalomon@google.coma2921122012-08-28 12:34:17 +0000299 GrResourceEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000300
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000301 while (NULL != entry) {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000302 GrAutoResourceCacheValidate atcv(this);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000303
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000304 if (fEntryCount <= fMaxCount && fEntryBytes <= fMaxBytes) {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000305 withinBudget = true;
306 break;
307 }
reed@google.com01804b42011-01-18 21:50:41 +0000308
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000309 GrResourceEntry* prev = iter.prev();
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000310 if (1 == entry->fResource->getRefCnt()) {
311 changed = true;
312
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000313 // remove from our cache
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000314 fCache.remove(entry->key(), entry);
reed@google.comac10a2d2010-12-22 21:39:39 +0000315
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000316 // remove from our llist
robertphillips@google.com209a1142012-10-31 12:25:21 +0000317 this->internalDetach(entry);
reed@google.com01804b42011-01-18 21:50:41 +0000318
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000319 #if GR_DUMP_TEXTURE_UPLOAD
320 GrPrintf("--- ~resource from cache %p [%d %d]\n",
321 entry->resource(),
322 entry->resource()->width(),
323 entry->resource()->height());
324 #endif
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000325
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000326 delete entry;
327 }
328 entry = prev;
329 }
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000330 } while (!withinBudget && changed);
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000331 fPurging = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000332 }
333}
334
bsalomon@google.coma2921122012-08-28 12:34:17 +0000335void GrResourceCache::purgeAllUnlocked() {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000336 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +0000337
bsalomon@google.come9a98942011-08-22 17:06:16 +0000338 // we can have one GrResource holding a lock on another
339 // so we don't want to just do a simple loop kicking each
340 // entry out. Instead change the budget and purge.
reed@google.comac10a2d2010-12-22 21:39:39 +0000341
bsalomon@google.come9a98942011-08-22 17:06:16 +0000342 int savedMaxBytes = fMaxBytes;
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000343 int savedMaxCount = fMaxCount;
344 fMaxBytes = (size_t) -1;
345 fMaxCount = 0;
bsalomon@google.come9a98942011-08-22 17:06:16 +0000346 this->purgeAsNeeded();
347
twiz@google.com0ec107f2012-02-21 19:15:53 +0000348#if GR_DEBUG
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000349 GrAssert(fExclusiveList.countEntries() == fClientDetachedCount);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000350 GrAssert(countBytes(fExclusiveList) == fClientDetachedBytes);
twiz@google.com0ec107f2012-02-21 19:15:53 +0000351 if (!fCache.count()) {
352 // Items may have been detached from the cache (such as the backing
353 // texture for an SkGpuDevice). The above purge would not have removed
354 // them.
355 GrAssert(fEntryCount == fClientDetachedCount);
356 GrAssert(fEntryBytes == fClientDetachedBytes);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000357 GrAssert(fList.isEmpty());
twiz@google.com0ec107f2012-02-21 19:15:53 +0000358 }
359#endif
bsalomon@google.come9a98942011-08-22 17:06:16 +0000360
361 fMaxBytes = savedMaxBytes;
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000362 fMaxCount = savedMaxCount;
reed@google.comac10a2d2010-12-22 21:39:39 +0000363}
364
365///////////////////////////////////////////////////////////////////////////////
366
367#if GR_DEBUG
bsalomon@google.coma2921122012-08-28 12:34:17 +0000368size_t GrResourceCache::countBytes(const EntryList& list) {
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000369 size_t bytes = 0;
370
bsalomon@google.coma2921122012-08-28 12:34:17 +0000371 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000372
bsalomon@google.coma2921122012-08-28 12:34:17 +0000373 const GrResourceEntry* entry = iter.init(const_cast<EntryList&>(list),
374 EntryList::Iter::kTail_IterStart);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000375
376 for ( ; NULL != entry; entry = iter.prev()) {
377 bytes += entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000378 }
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000379 return bytes;
reed@google.comac10a2d2010-12-22 21:39:39 +0000380}
381
reed@google.comb89a6432011-02-07 13:20:30 +0000382static bool both_zero_or_nonzero(int count, size_t bytes) {
383 return (count == 0 && bytes == 0) || (count > 0 && bytes > 0);
384}
reed@google.comb89a6432011-02-07 13:20:30 +0000385
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000386void GrResourceCache::validate() const {
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000387 fList.validate();
388 fExclusiveList.validate();
reed@google.comb89a6432011-02-07 13:20:30 +0000389 GrAssert(both_zero_or_nonzero(fEntryCount, fEntryBytes));
390 GrAssert(both_zero_or_nonzero(fClientDetachedCount, fClientDetachedBytes));
reed@google.comac10a2d2010-12-22 21:39:39 +0000391 GrAssert(fClientDetachedBytes <= fEntryBytes);
392 GrAssert(fClientDetachedCount <= fEntryCount);
393 GrAssert((fEntryCount - fClientDetachedCount) == fCache.count());
reed@google.com01804b42011-01-18 21:50:41 +0000394
reed@google.comac10a2d2010-12-22 21:39:39 +0000395 fCache.validate();
396
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000397
bsalomon@google.coma2921122012-08-28 12:34:17 +0000398 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000399
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000400 // check that the exclusively held entries are okay
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000401 const GrResourceEntry* entry = iter.init(const_cast<EntryList&>(fExclusiveList),
bsalomon@google.coma2921122012-08-28 12:34:17 +0000402 EntryList::Iter::kHead_IterStart);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000403
404 for ( ; NULL != entry; entry = iter.next()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000405 entry->validate();
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000406 }
407
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000408 // check that the shareable entries are okay
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000409 entry = iter.init(const_cast<EntryList&>(fList), EntryList::Iter::kHead_IterStart);
410
411 int count = 0;
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000412 for ( ; NULL != entry; entry = iter.next()) {
413 entry->validate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000414 GrAssert(fCache.find(entry->key()));
415 count += 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000416 }
417 GrAssert(count == fEntryCount - fClientDetachedCount);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000418
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000419 size_t bytes = countBytes(fList);
reed@google.comac10a2d2010-12-22 21:39:39 +0000420 GrAssert(bytes == fEntryBytes - fClientDetachedBytes);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000421
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000422 bytes = countBytes(fExclusiveList);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000423 GrAssert(bytes == fClientDetachedBytes);
424
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000425 GrAssert(fList.countEntries() == fEntryCount - fClientDetachedCount);
426
427 GrAssert(fExclusiveList.countEntries() == fClientDetachedCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000428}
robertphillips@google.com59552022012-08-31 13:07:37 +0000429#endif // GR_DEBUG
430
431#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000432
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000433void GrResourceCache::printStats() {
434 int locked = 0;
435
436 EntryList::Iter iter;
437
438 GrResourceEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart);
439
440 for ( ; NULL != entry; entry = iter.prev()) {
441 if (entry->fResource->getRefCnt() > 1) {
442 ++locked;
443 }
444 }
445
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000446 SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000447 SkDebugf("\t\tEntry Count: current %d (%d locked) high %d\n",
448 fEntryCount, locked, fHighWaterEntryCount);
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000449 SkDebugf("\t\tEntry Bytes: current %d high %d\n",
450 fEntryBytes, fHighWaterEntryBytes);
451 SkDebugf("\t\tDetached Entry Count: current %d high %d\n",
452 fClientDetachedCount, fHighWaterClientDetachedCount);
453 SkDebugf("\t\tDetached Bytes: current %d high %d\n",
454 fClientDetachedBytes, fHighWaterClientDetachedBytes);
455}
456
reed@google.comac10a2d2010-12-22 21:39:39 +0000457#endif
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000458
459///////////////////////////////////////////////////////////////////////////////