blob: 96b11aaafe5b047cf4cc46e83e3f6594157e21ad [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
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +000065 fPurging = false;
66
67 fOverbudgetCB = NULL;
68 fOverbudgetData = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000069}
70
bsalomon@google.com50398bf2011-07-26 20:45:30 +000071GrResourceCache::~GrResourceCache() {
72 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +000073
bsalomon@google.coma2921122012-08-28 12:34:17 +000074 EntryList::Iter iter;
75
76 // Unlike the removeAll, here we really remove everything, including locked resources.
77 while (GrResourceEntry* entry = fList.head()) {
78 GrAutoResourceCacheValidate atcv(this);
79
80 // remove from our cache
81 fCache.remove(entry->fKey, entry);
82
83 // remove from our llist
robertphillips@google.com209a1142012-10-31 12:25:21 +000084 this->internalDetach(entry);
bsalomon@google.coma2921122012-08-28 12:34:17 +000085
86 delete entry;
87 }
reed@google.comac10a2d2010-12-22 21:39:39 +000088}
89
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000090void GrResourceCache::getLimits(int* maxResources, size_t* maxResourceBytes) const{
robertphillips@google.come4eaea22013-07-19 16:51:46 +000091 if (NULL != maxResources) {
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000092 *maxResources = fMaxCount;
93 }
robertphillips@google.come4eaea22013-07-19 16:51:46 +000094 if (NULL != maxResourceBytes) {
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000095 *maxResourceBytes = fMaxBytes;
96 }
97}
reed@google.com01804b42011-01-18 21:50:41 +000098
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000099void GrResourceCache::setLimits(int maxResources, size_t maxResourceBytes) {
100 bool smaller = (maxResources < fMaxCount) || (maxResourceBytes < fMaxBytes);
101
102 fMaxCount = maxResources;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000103 fMaxBytes = maxResourceBytes;
reed@google.com01804b42011-01-18 21:50:41 +0000104
105 if (smaller) {
106 this->purgeAsNeeded();
107 }
108}
109
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000110void GrResourceCache::internalDetach(GrResourceEntry* entry,
robertphillips@google.com209a1142012-10-31 12:25:21 +0000111 BudgetBehaviors behavior) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000112 fList.remove(entry);
reed@google.comac10a2d2010-12-22 21:39:39 +0000113
reed@google.comac10a2d2010-12-22 21:39:39 +0000114 // update our stats
robertphillips@google.com209a1142012-10-31 12:25:21 +0000115 if (kIgnore_BudgetBehavior == behavior) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000116 fClientDetachedCount += 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000117 fClientDetachedBytes += entry->resource()->sizeInBytes();
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000118
robertphillips@google.com59552022012-08-31 13:07:37 +0000119#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000120 if (fHighWaterClientDetachedCount < fClientDetachedCount) {
121 fHighWaterClientDetachedCount = fClientDetachedCount;
122 }
123 if (fHighWaterClientDetachedBytes < fClientDetachedBytes) {
124 fHighWaterClientDetachedBytes = fClientDetachedBytes;
125 }
126#endif
127
reed@google.comac10a2d2010-12-22 21:39:39 +0000128 } else {
robertphillips@google.com209a1142012-10-31 12:25:21 +0000129 GrAssert(kAccountFor_BudgetBehavior == behavior);
130
reed@google.comac10a2d2010-12-22 21:39:39 +0000131 fEntryCount -= 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000132 fEntryBytes -= entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000133 }
134}
135
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000136void GrResourceCache::attachToHead(GrResourceEntry* entry,
robertphillips@google.com209a1142012-10-31 12:25:21 +0000137 BudgetBehaviors behavior) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000138 fList.addToHead(entry);
139
reed@google.comac10a2d2010-12-22 21:39:39 +0000140 // update our stats
robertphillips@google.com209a1142012-10-31 12:25:21 +0000141 if (kIgnore_BudgetBehavior == behavior) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000142 fClientDetachedCount -= 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000143 fClientDetachedBytes -= entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000144 } else {
robertphillips@google.com209a1142012-10-31 12:25:21 +0000145 GrAssert(kAccountFor_BudgetBehavior == behavior);
146
reed@google.comac10a2d2010-12-22 21:39:39 +0000147 fEntryCount += 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000148 fEntryBytes += entry->resource()->sizeInBytes();
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000149
robertphillips@google.com59552022012-08-31 13:07:37 +0000150#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000151 if (fHighWaterEntryCount < fEntryCount) {
152 fHighWaterEntryCount = fEntryCount;
153 }
154 if (fHighWaterEntryBytes < fEntryBytes) {
155 fHighWaterEntryBytes = fEntryBytes;
156 }
157#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000158 }
159}
160
robertphillips@google.com209a1142012-10-31 12:25:21 +0000161// This functor just searches for an entry with only a single ref (from
162// the texture cache itself). Presumably in this situation no one else
163// is relying on the texture.
164class GrTFindUnreffedFunctor {
165public:
skia.committer@gmail.comf3dc1992012-11-01 02:01:27 +0000166 bool operator()(const GrResourceEntry* entry) const {
bungeman@google.comf64c6842013-07-19 23:18:52 +0000167 return entry->resource()->unique();
robertphillips@google.com209a1142012-10-31 12:25:21 +0000168 }
169};
170
171GrResource* GrResourceCache::find(const GrResourceKey& key, uint32_t ownershipFlags) {
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000172 GrAutoResourceCacheValidate atcv(this);
173
robertphillips@google.com209a1142012-10-31 12:25:21 +0000174 GrResourceEntry* entry = NULL;
175
176 if (ownershipFlags & kNoOtherOwners_OwnershipFlag) {
177 GrTFindUnreffedFunctor functor;
178
179 entry = fCache.find<GrTFindUnreffedFunctor>(key, functor);
180 } else {
181 entry = fCache.find(key);
182 }
183
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000184 if (NULL == entry) {
185 return NULL;
186 }
187
robertphillips@google.com209a1142012-10-31 12:25:21 +0000188 if (ownershipFlags & kHide_OwnershipFlag) {
189 this->makeExclusive(entry);
190 } else {
191 // Make this resource MRU
192 this->internalDetach(entry);
193 this->attachToHead(entry);
194 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000195
196 return entry->fResource;
reed@google.comac10a2d2010-12-22 21:39:39 +0000197}
198
skia.committer@gmail.comf3dc1992012-11-01 02:01:27 +0000199void GrResourceCache::addResource(const GrResourceKey& key,
robertphillips@google.com209a1142012-10-31 12:25:21 +0000200 GrResource* resource,
201 uint32_t ownershipFlags) {
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000202 GrAssert(NULL == resource->getCacheEntry());
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000203 // we don't expect to create new resources during a purge. In theory
204 // this could cause purgeAsNeeded() into an infinite loop (e.g.
205 // each resource destroyed creates and locks 2 resources and
206 // unlocks 1 thereby causing a new purge).
207 GrAssert(!fPurging);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000208 GrAutoResourceCacheValidate atcv(this);
reed@google.comac10a2d2010-12-22 21:39:39 +0000209
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000210 GrResourceEntry* entry = SkNEW_ARGS(GrResourceEntry, (key, resource));
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000211 resource->setCacheEntry(entry);
212
robertphillips@google.com209a1142012-10-31 12:25:21 +0000213 this->attachToHead(entry);
reed@google.comac10a2d2010-12-22 21:39:39 +0000214 fCache.insert(key, entry);
215
robertphillips@google.com209a1142012-10-31 12:25:21 +0000216 if (ownershipFlags & kHide_OwnershipFlag) {
217 this->makeExclusive(entry);
218 }
219
reed@google.comac10a2d2010-12-22 21:39:39 +0000220}
221
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000222void GrResourceCache::makeExclusive(GrResourceEntry* entry) {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000223 GrAutoResourceCacheValidate atcv(this);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000224
robertphillips@google.com209a1142012-10-31 12:25:21 +0000225 // When scratch textures are detached (to hide them from future finds) they
226 // still count against the resource budget
227 this->internalDetach(entry, kIgnore_BudgetBehavior);
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000228 fCache.remove(entry->key(), entry);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000229
230#if GR_DEBUG
231 fExclusiveList.addToHead(entry);
232#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000233}
234
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000235void GrResourceCache::removeInvalidResource(GrResourceEntry* entry) {
236 // If the resource went invalid while it was detached then purge it
237 // This can happen when a 3D context was lost,
238 // the client called GrContext::contextDestroyed() to notify Gr,
239 // and then later an SkGpuDevice's destructor releases its backing
240 // texture (which was invalidated at contextDestroyed time).
241 fClientDetachedCount -= 1;
242 fEntryCount -= 1;
243 size_t size = entry->resource()->sizeInBytes();
244 fClientDetachedBytes -= size;
245 fEntryBytes -= size;
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000246}
247
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000248void GrResourceCache::makeNonExclusive(GrResourceEntry* entry) {
bsalomon@google.com60879752011-09-15 20:43:53 +0000249 GrAutoResourceCacheValidate atcv(this);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000250
251#if GR_DEBUG
252 fExclusiveList.remove(entry);
253#endif
254
bsalomon@google.com60879752011-09-15 20:43:53 +0000255 if (entry->resource()->isValid()) {
skia.committer@gmail.comf3dc1992012-11-01 02:01:27 +0000256 // Since scratch textures still count against the cache budget even
257 // when they have been removed from the cache, re-adding them doesn't
robertphillips@google.com209a1142012-10-31 12:25:21 +0000258 // alter the budget information.
259 attachToHead(entry, kIgnore_BudgetBehavior);
bsalomon@google.com60879752011-09-15 20:43:53 +0000260 fCache.insert(entry->key(), entry);
261 } else {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000262 this->removeInvalidResource(entry);
bsalomon@google.com60879752011-09-15 20:43:53 +0000263 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000264}
265
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000266/**
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000267 * Destroying a resource may potentially trigger the unlock of additional
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000268 * resources which in turn will trigger a nested purge. We block the nested
269 * purge using the fPurging variable. However, the initial purge will keep
270 * looping until either all resources in the cache are unlocked or we've met
271 * the budget. There is an assertion in createAndLock to check against a
272 * resource's destructor inserting new resources into the cache. If these
273 * new resources were unlocked before purgeAsNeeded completed it could
274 * potentially make purgeAsNeeded loop infinitely.
robertphillips@google.com41d25322013-07-18 17:12:57 +0000275 *
276 * extraCount and extraBytes are added to the current resource totals to account
277 * for incoming resources (e.g., GrContext is about to add 10MB split between
278 * 10 textures).
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000279 */
robertphillips@google.com41d25322013-07-18 17:12:57 +0000280void GrResourceCache::purgeAsNeeded(int extraCount, size_t extraBytes) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000281 if (fPurging) {
282 return;
reed@google.comac10a2d2010-12-22 21:39:39 +0000283 }
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000284
285 fPurging = true;
286
robertphillips@google.com41d25322013-07-18 17:12:57 +0000287 this->internalPurge(extraCount, extraBytes);
skia.committer@gmail.coma7991982013-07-19 07:00:57 +0000288 if (((fEntryCount+extraCount) > fMaxCount ||
robertphillips@google.com41d25322013-07-18 17:12:57 +0000289 (fEntryBytes+extraBytes) > fMaxBytes) &&
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000290 NULL != fOverbudgetCB) {
291 // Despite the purge we're still over budget. See if Ganesh can
292 // release some resources and purge again.
293 if ((*fOverbudgetCB)(fOverbudgetData)) {
robertphillips@google.com41d25322013-07-18 17:12:57 +0000294 this->internalPurge(extraCount, extraBytes);
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000295 }
296 }
297
298 fPurging = false;
299}
300
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000301void GrResourceCache::deleteResource(GrResourceEntry* entry) {
302 GrAssert(1 == entry->fResource->getRefCnt());
303
304 // remove from our cache
305 fCache.remove(entry->key(), entry);
306
307 // remove from our llist
308 this->internalDetach(entry);
309 delete entry;
310}
311
robertphillips@google.com41d25322013-07-18 17:12:57 +0000312void GrResourceCache::internalPurge(int extraCount, size_t extraBytes) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000313 SkASSERT(fPurging);
314
315 bool withinBudget = false;
316 bool changed = false;
317
318 // The purging process is repeated several times since one pass
319 // may free up other resources
320 do {
321 EntryList::Iter iter;
322
323 changed = false;
324
325 // Note: the following code relies on the fact that the
326 // doubly linked list doesn't invalidate its data/pointers
327 // outside of the specific area where a deletion occurs (e.g.,
328 // in internalDetach)
329 GrResourceEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart);
330
331 while (NULL != entry) {
332 GrAutoResourceCacheValidate atcv(this);
333
skia.committer@gmail.coma7991982013-07-19 07:00:57 +0000334 if ((fEntryCount+extraCount) <= fMaxCount &&
robertphillips@google.com41d25322013-07-18 17:12:57 +0000335 (fEntryBytes+extraBytes) <= fMaxBytes) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000336 withinBudget = true;
337 break;
338 }
339
340 GrResourceEntry* prev = iter.prev();
bungeman@google.comf64c6842013-07-19 23:18:52 +0000341 if (entry->fResource->unique()) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000342 changed = true;
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000343 this->deleteResource(entry);
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000344 }
345 entry = prev;
346 }
347 } while (!withinBudget && changed);
reed@google.comac10a2d2010-12-22 21:39:39 +0000348}
349
bsalomon@google.coma2921122012-08-28 12:34:17 +0000350void GrResourceCache::purgeAllUnlocked() {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000351 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +0000352
bsalomon@google.come9a98942011-08-22 17:06:16 +0000353 // we can have one GrResource holding a lock on another
354 // so we don't want to just do a simple loop kicking each
355 // entry out. Instead change the budget and purge.
reed@google.comac10a2d2010-12-22 21:39:39 +0000356
bsalomon@google.come9a98942011-08-22 17:06:16 +0000357 int savedMaxBytes = fMaxBytes;
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000358 int savedMaxCount = fMaxCount;
359 fMaxBytes = (size_t) -1;
360 fMaxCount = 0;
bsalomon@google.come9a98942011-08-22 17:06:16 +0000361 this->purgeAsNeeded();
362
twiz@google.com0ec107f2012-02-21 19:15:53 +0000363#if GR_DEBUG
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000364 GrAssert(fExclusiveList.countEntries() == fClientDetachedCount);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000365 GrAssert(countBytes(fExclusiveList) == fClientDetachedBytes);
twiz@google.com0ec107f2012-02-21 19:15:53 +0000366 if (!fCache.count()) {
367 // Items may have been detached from the cache (such as the backing
368 // texture for an SkGpuDevice). The above purge would not have removed
369 // them.
370 GrAssert(fEntryCount == fClientDetachedCount);
371 GrAssert(fEntryBytes == fClientDetachedBytes);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000372 GrAssert(fList.isEmpty());
twiz@google.com0ec107f2012-02-21 19:15:53 +0000373 }
374#endif
bsalomon@google.come9a98942011-08-22 17:06:16 +0000375
376 fMaxBytes = savedMaxBytes;
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000377 fMaxCount = savedMaxCount;
reed@google.comac10a2d2010-12-22 21:39:39 +0000378}
379
380///////////////////////////////////////////////////////////////////////////////
381
382#if GR_DEBUG
bsalomon@google.coma2921122012-08-28 12:34:17 +0000383size_t GrResourceCache::countBytes(const EntryList& list) {
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000384 size_t bytes = 0;
385
bsalomon@google.coma2921122012-08-28 12:34:17 +0000386 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000387
bsalomon@google.coma2921122012-08-28 12:34:17 +0000388 const GrResourceEntry* entry = iter.init(const_cast<EntryList&>(list),
389 EntryList::Iter::kTail_IterStart);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000390
391 for ( ; NULL != entry; entry = iter.prev()) {
392 bytes += entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000393 }
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000394 return bytes;
reed@google.comac10a2d2010-12-22 21:39:39 +0000395}
396
reed@google.comb89a6432011-02-07 13:20:30 +0000397static bool both_zero_or_nonzero(int count, size_t bytes) {
398 return (count == 0 && bytes == 0) || (count > 0 && bytes > 0);
399}
reed@google.comb89a6432011-02-07 13:20:30 +0000400
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000401void GrResourceCache::validate() const {
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000402 fList.validate();
403 fExclusiveList.validate();
reed@google.comb89a6432011-02-07 13:20:30 +0000404 GrAssert(both_zero_or_nonzero(fEntryCount, fEntryBytes));
405 GrAssert(both_zero_or_nonzero(fClientDetachedCount, fClientDetachedBytes));
reed@google.comac10a2d2010-12-22 21:39:39 +0000406 GrAssert(fClientDetachedBytes <= fEntryBytes);
407 GrAssert(fClientDetachedCount <= fEntryCount);
408 GrAssert((fEntryCount - fClientDetachedCount) == fCache.count());
reed@google.com01804b42011-01-18 21:50:41 +0000409
reed@google.comac10a2d2010-12-22 21:39:39 +0000410 fCache.validate();
411
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000412
bsalomon@google.coma2921122012-08-28 12:34:17 +0000413 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000414
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000415 // check that the exclusively held entries are okay
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000416 const GrResourceEntry* entry = iter.init(const_cast<EntryList&>(fExclusiveList),
bsalomon@google.coma2921122012-08-28 12:34:17 +0000417 EntryList::Iter::kHead_IterStart);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000418
419 for ( ; NULL != entry; entry = iter.next()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000420 entry->validate();
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000421 }
422
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000423 // check that the shareable entries are okay
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000424 entry = iter.init(const_cast<EntryList&>(fList), EntryList::Iter::kHead_IterStart);
425
426 int count = 0;
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000427 for ( ; NULL != entry; entry = iter.next()) {
428 entry->validate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000429 GrAssert(fCache.find(entry->key()));
430 count += 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000431 }
432 GrAssert(count == fEntryCount - fClientDetachedCount);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000433
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000434 size_t bytes = countBytes(fList);
reed@google.comac10a2d2010-12-22 21:39:39 +0000435 GrAssert(bytes == fEntryBytes - fClientDetachedBytes);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000436
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000437 bytes = countBytes(fExclusiveList);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000438 GrAssert(bytes == fClientDetachedBytes);
439
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000440 GrAssert(fList.countEntries() == fEntryCount - fClientDetachedCount);
441
442 GrAssert(fExclusiveList.countEntries() == fClientDetachedCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000443}
robertphillips@google.com59552022012-08-31 13:07:37 +0000444#endif // GR_DEBUG
445
446#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000447
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000448void GrResourceCache::printStats() {
449 int locked = 0;
450
451 EntryList::Iter iter;
452
453 GrResourceEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart);
454
455 for ( ; NULL != entry; entry = iter.prev()) {
456 if (entry->fResource->getRefCnt() > 1) {
457 ++locked;
458 }
459 }
460
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000461 SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000462 SkDebugf("\t\tEntry Count: current %d (%d locked) high %d\n",
463 fEntryCount, locked, fHighWaterEntryCount);
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000464 SkDebugf("\t\tEntry Bytes: current %d high %d\n",
465 fEntryBytes, fHighWaterEntryBytes);
466 SkDebugf("\t\tDetached Entry Count: current %d high %d\n",
467 fClientDetachedCount, fHighWaterClientDetachedCount);
468 SkDebugf("\t\tDetached Bytes: current %d high %d\n",
469 fClientDetachedBytes, fHighWaterClientDetachedBytes);
470}
471
reed@google.comac10a2d2010-12-22 21:39:39 +0000472#endif
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000473
474///////////////////////////////////////////////////////////////////////////////