blob: ba8b9629eb4fd81ef4d32df02814a61cf30ac429 [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
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +000014DECLARE_SKMESSAGEBUS_MESSAGE(GrResourceInvalidatedMessage);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000015
16GrResourceKey::ResourceType GrResourceKey::GenerateResourceType() {
17 static int32_t gNextType = 0;
18
19 int32_t type = sk_atomic_inc(&gNextType);
20 if (type >= (1 << 8 * sizeof(ResourceType))) {
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +000021 SkFAIL("Too many Resource Types");
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000022 }
23
24 return static_cast<ResourceType>(type);
25}
26
27///////////////////////////////////////////////////////////////////////////////
28
bsalomon@google.com50398bf2011-07-26 20:45:30 +000029GrResourceEntry::GrResourceEntry(const GrResourceKey& key, GrResource* resource)
30 : fKey(key), fResource(resource) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000031 // we assume ownership of the resource, and will unref it when we die
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000032 SkASSERT(resource);
skia.committer@gmail.com6c778162012-09-06 02:01:13 +000033 resource->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +000034}
35
bsalomon@google.com50398bf2011-07-26 20:45:30 +000036GrResourceEntry::~GrResourceEntry() {
robertphillips@google.com521eaf82012-08-22 11:03:19 +000037 fResource->setCacheEntry(NULL);
bsalomon@google.com50398bf2011-07-26 20:45:30 +000038 fResource->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +000039}
40
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000041#ifdef SK_DEBUG
bsalomon@google.com50398bf2011-07-26 20:45:30 +000042void GrResourceEntry::validate() const {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000043 SkASSERT(fResource);
44 SkASSERT(fResource->getCacheEntry() == this);
bsalomon@google.com50398bf2011-07-26 20:45:30 +000045 fResource->validate();
reed@google.comac10a2d2010-12-22 21:39:39 +000046}
47#endif
48
49///////////////////////////////////////////////////////////////////////////////
50
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000051GrResourceCache::GrResourceCache(int maxCount, size_t maxBytes) :
52 fMaxCount(maxCount),
53 fMaxBytes(maxBytes) {
robertphillips@google.com59552022012-08-31 13:07:37 +000054#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +000055 fHighWaterEntryCount = 0;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +000056 fHighWaterEntryBytes = 0;
57 fHighWaterClientDetachedCount = 0;
58 fHighWaterClientDetachedBytes = 0;
59#endif
60
61 fEntryCount = 0;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +000062 fEntryBytes = 0;
63 fClientDetachedCount = 0;
64 fClientDetachedBytes = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +000065
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +000066 fPurging = false;
67
68 fOverbudgetCB = NULL;
69 fOverbudgetData = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000070}
71
bsalomon@google.com50398bf2011-07-26 20:45:30 +000072GrResourceCache::~GrResourceCache() {
73 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +000074
bsalomon@google.coma2921122012-08-28 12:34:17 +000075 EntryList::Iter iter;
76
77 // Unlike the removeAll, here we really remove everything, including locked resources.
78 while (GrResourceEntry* entry = fList.head()) {
79 GrAutoResourceCacheValidate atcv(this);
80
81 // remove from our cache
82 fCache.remove(entry->fKey, entry);
83
84 // remove from our llist
robertphillips@google.com209a1142012-10-31 12:25:21 +000085 this->internalDetach(entry);
bsalomon@google.coma2921122012-08-28 12:34:17 +000086
87 delete entry;
88 }
reed@google.comac10a2d2010-12-22 21:39:39 +000089}
90
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000091void GrResourceCache::getLimits(int* maxResources, size_t* maxResourceBytes) const{
robertphillips@google.come4eaea22013-07-19 16:51:46 +000092 if (NULL != maxResources) {
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000093 *maxResources = fMaxCount;
94 }
robertphillips@google.come4eaea22013-07-19 16:51:46 +000095 if (NULL != maxResourceBytes) {
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000096 *maxResourceBytes = fMaxBytes;
97 }
98}
reed@google.com01804b42011-01-18 21:50:41 +000099
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000100void GrResourceCache::setLimits(int maxResources, size_t maxResourceBytes) {
101 bool smaller = (maxResources < fMaxCount) || (maxResourceBytes < fMaxBytes);
102
103 fMaxCount = maxResources;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000104 fMaxBytes = maxResourceBytes;
reed@google.com01804b42011-01-18 21:50:41 +0000105
106 if (smaller) {
107 this->purgeAsNeeded();
108 }
109}
110
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000111void GrResourceCache::internalDetach(GrResourceEntry* entry,
robertphillips@google.com209a1142012-10-31 12:25:21 +0000112 BudgetBehaviors behavior) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000113 fList.remove(entry);
reed@google.comac10a2d2010-12-22 21:39:39 +0000114
reed@google.comac10a2d2010-12-22 21:39:39 +0000115 // update our stats
robertphillips@google.com209a1142012-10-31 12:25:21 +0000116 if (kIgnore_BudgetBehavior == behavior) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000117 fClientDetachedCount += 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000118 fClientDetachedBytes += entry->resource()->sizeInBytes();
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000119
robertphillips@google.com59552022012-08-31 13:07:37 +0000120#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000121 if (fHighWaterClientDetachedCount < fClientDetachedCount) {
122 fHighWaterClientDetachedCount = fClientDetachedCount;
123 }
124 if (fHighWaterClientDetachedBytes < fClientDetachedBytes) {
125 fHighWaterClientDetachedBytes = fClientDetachedBytes;
126 }
127#endif
128
reed@google.comac10a2d2010-12-22 21:39:39 +0000129 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000130 SkASSERT(kAccountFor_BudgetBehavior == behavior);
robertphillips@google.com209a1142012-10-31 12:25:21 +0000131
reed@google.comac10a2d2010-12-22 21:39:39 +0000132 fEntryCount -= 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000133 fEntryBytes -= entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000134 }
135}
136
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000137void GrResourceCache::attachToHead(GrResourceEntry* entry,
robertphillips@google.com209a1142012-10-31 12:25:21 +0000138 BudgetBehaviors behavior) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000139 fList.addToHead(entry);
140
reed@google.comac10a2d2010-12-22 21:39:39 +0000141 // update our stats
robertphillips@google.com209a1142012-10-31 12:25:21 +0000142 if (kIgnore_BudgetBehavior == behavior) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000143 fClientDetachedCount -= 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000144 fClientDetachedBytes -= entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000145 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000146 SkASSERT(kAccountFor_BudgetBehavior == behavior);
robertphillips@google.com209a1142012-10-31 12:25:21 +0000147
reed@google.comac10a2d2010-12-22 21:39:39 +0000148 fEntryCount += 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000149 fEntryBytes += entry->resource()->sizeInBytes();
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000150
robertphillips@google.com59552022012-08-31 13:07:37 +0000151#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000152 if (fHighWaterEntryCount < fEntryCount) {
153 fHighWaterEntryCount = fEntryCount;
154 }
155 if (fHighWaterEntryBytes < fEntryBytes) {
156 fHighWaterEntryBytes = fEntryBytes;
157 }
158#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000159 }
160}
161
robertphillips@google.com209a1142012-10-31 12:25:21 +0000162// This functor just searches for an entry with only a single ref (from
163// the texture cache itself). Presumably in this situation no one else
164// is relying on the texture.
165class GrTFindUnreffedFunctor {
166public:
skia.committer@gmail.comf3dc1992012-11-01 02:01:27 +0000167 bool operator()(const GrResourceEntry* entry) const {
bungeman@google.comf64c6842013-07-19 23:18:52 +0000168 return entry->resource()->unique();
robertphillips@google.com209a1142012-10-31 12:25:21 +0000169 }
170};
171
172GrResource* GrResourceCache::find(const GrResourceKey& key, uint32_t ownershipFlags) {
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000173 GrAutoResourceCacheValidate atcv(this);
174
robertphillips@google.com209a1142012-10-31 12:25:21 +0000175 GrResourceEntry* entry = NULL;
176
177 if (ownershipFlags & kNoOtherOwners_OwnershipFlag) {
178 GrTFindUnreffedFunctor functor;
179
180 entry = fCache.find<GrTFindUnreffedFunctor>(key, functor);
181 } else {
182 entry = fCache.find(key);
183 }
184
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000185 if (NULL == entry) {
186 return NULL;
187 }
188
robertphillips@google.com209a1142012-10-31 12:25:21 +0000189 if (ownershipFlags & kHide_OwnershipFlag) {
190 this->makeExclusive(entry);
191 } else {
192 // Make this resource MRU
193 this->internalDetach(entry);
194 this->attachToHead(entry);
195 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000196
197 return entry->fResource;
reed@google.comac10a2d2010-12-22 21:39:39 +0000198}
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) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000203 SkASSERT(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).
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000208 SkASSERT(!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
robertphillips@google.com209a1142012-10-31 12:25:21 +0000217 if (ownershipFlags & kHide_OwnershipFlag) {
218 this->makeExclusive(entry);
219 }
220
reed@google.comac10a2d2010-12-22 21:39:39 +0000221}
222
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000223void GrResourceCache::makeExclusive(GrResourceEntry* entry) {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000224 GrAutoResourceCacheValidate atcv(this);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000225
robertphillips@google.com209a1142012-10-31 12:25:21 +0000226 // When scratch textures are detached (to hide them from future finds) they
227 // still count against the resource budget
228 this->internalDetach(entry, kIgnore_BudgetBehavior);
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000229 fCache.remove(entry->key(), entry);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000230
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000231#ifdef SK_DEBUG
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000232 fExclusiveList.addToHead(entry);
233#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000234}
235
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000236void GrResourceCache::removeInvalidResource(GrResourceEntry* entry) {
237 // If the resource went invalid while it was detached then purge it
238 // This can happen when a 3D context was lost,
239 // the client called GrContext::contextDestroyed() to notify Gr,
240 // and then later an SkGpuDevice's destructor releases its backing
241 // texture (which was invalidated at contextDestroyed time).
242 fClientDetachedCount -= 1;
243 fEntryCount -= 1;
244 size_t size = entry->resource()->sizeInBytes();
245 fClientDetachedBytes -= size;
246 fEntryBytes -= size;
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000247}
248
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000249void GrResourceCache::makeNonExclusive(GrResourceEntry* entry) {
bsalomon@google.com60879752011-09-15 20:43:53 +0000250 GrAutoResourceCacheValidate atcv(this);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000251
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000252#ifdef SK_DEBUG
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000253 fExclusiveList.remove(entry);
254#endif
255
bsalomon@google.com60879752011-09-15 20:43:53 +0000256 if (entry->resource()->isValid()) {
skia.committer@gmail.comf3dc1992012-11-01 02:01:27 +0000257 // Since scratch textures still count against the cache budget even
258 // when they have been removed from the cache, re-adding them doesn't
robertphillips@google.com209a1142012-10-31 12:25:21 +0000259 // alter the budget information.
260 attachToHead(entry, kIgnore_BudgetBehavior);
bsalomon@google.com60879752011-09-15 20:43:53 +0000261 fCache.insert(entry->key(), entry);
262 } else {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000263 this->removeInvalidResource(entry);
bsalomon@google.com60879752011-09-15 20:43:53 +0000264 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000265}
266
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000267/**
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000268 * Destroying a resource may potentially trigger the unlock of additional
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000269 * resources which in turn will trigger a nested purge. We block the nested
270 * purge using the fPurging variable. However, the initial purge will keep
271 * looping until either all resources in the cache are unlocked or we've met
272 * the budget. There is an assertion in createAndLock to check against a
273 * resource's destructor inserting new resources into the cache. If these
274 * new resources were unlocked before purgeAsNeeded completed it could
275 * potentially make purgeAsNeeded loop infinitely.
robertphillips@google.com41d25322013-07-18 17:12:57 +0000276 *
277 * extraCount and extraBytes are added to the current resource totals to account
278 * for incoming resources (e.g., GrContext is about to add 10MB split between
279 * 10 textures).
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000280 */
robertphillips@google.com41d25322013-07-18 17:12:57 +0000281void GrResourceCache::purgeAsNeeded(int extraCount, size_t extraBytes) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000282 if (fPurging) {
283 return;
reed@google.comac10a2d2010-12-22 21:39:39 +0000284 }
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000285
286 fPurging = true;
287
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000288 this->purgeInvalidated();
289
robertphillips@google.com41d25322013-07-18 17:12:57 +0000290 this->internalPurge(extraCount, extraBytes);
skia.committer@gmail.coma7991982013-07-19 07:00:57 +0000291 if (((fEntryCount+extraCount) > fMaxCount ||
robertphillips@google.com41d25322013-07-18 17:12:57 +0000292 (fEntryBytes+extraBytes) > fMaxBytes) &&
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000293 NULL != fOverbudgetCB) {
294 // Despite the purge we're still over budget. See if Ganesh can
295 // release some resources and purge again.
296 if ((*fOverbudgetCB)(fOverbudgetData)) {
robertphillips@google.com41d25322013-07-18 17:12:57 +0000297 this->internalPurge(extraCount, extraBytes);
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000298 }
299 }
300
301 fPurging = false;
302}
303
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000304void GrResourceCache::purgeInvalidated() {
305 SkTDArray<GrResourceInvalidatedMessage> invalidated;
306 fInvalidationInbox.poll(&invalidated);
307
308 for (int i = 0; i < invalidated.count(); i++) {
309 // We're somewhat missing an opportunity here. We could use the
310 // default find functor that gives us back resources whether we own
311 // them exclusively or not, and when they're not exclusively owned mark
312 // them for purging later when they do become exclusively owned.
313 //
314 // This is complicated and confusing. May try this in the future. For
315 // now, these resources are just LRU'd as if we never got the message.
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +0000316 while (GrResourceEntry* entry = fCache.find(invalidated[i].key, GrTFindUnreffedFunctor())) {
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000317 this->deleteResource(entry);
318 }
319 }
320}
321
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000322void GrResourceCache::deleteResource(GrResourceEntry* entry) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000323 SkASSERT(1 == entry->fResource->getRefCnt());
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000324
325 // remove from our cache
326 fCache.remove(entry->key(), entry);
327
328 // remove from our llist
329 this->internalDetach(entry);
330 delete entry;
331}
332
robertphillips@google.com41d25322013-07-18 17:12:57 +0000333void GrResourceCache::internalPurge(int extraCount, size_t extraBytes) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000334 SkASSERT(fPurging);
335
336 bool withinBudget = false;
337 bool changed = false;
338
339 // The purging process is repeated several times since one pass
340 // may free up other resources
341 do {
342 EntryList::Iter iter;
343
344 changed = false;
345
346 // Note: the following code relies on the fact that the
347 // doubly linked list doesn't invalidate its data/pointers
348 // outside of the specific area where a deletion occurs (e.g.,
349 // in internalDetach)
350 GrResourceEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart);
351
352 while (NULL != entry) {
353 GrAutoResourceCacheValidate atcv(this);
354
skia.committer@gmail.coma7991982013-07-19 07:00:57 +0000355 if ((fEntryCount+extraCount) <= fMaxCount &&
robertphillips@google.com41d25322013-07-18 17:12:57 +0000356 (fEntryBytes+extraBytes) <= fMaxBytes) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000357 withinBudget = true;
358 break;
359 }
360
361 GrResourceEntry* prev = iter.prev();
bungeman@google.comf64c6842013-07-19 23:18:52 +0000362 if (entry->fResource->unique()) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000363 changed = true;
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000364 this->deleteResource(entry);
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000365 }
366 entry = prev;
367 }
368 } while (!withinBudget && changed);
reed@google.comac10a2d2010-12-22 21:39:39 +0000369}
370
bsalomon@google.coma2921122012-08-28 12:34:17 +0000371void GrResourceCache::purgeAllUnlocked() {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000372 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +0000373
bsalomon@google.come9a98942011-08-22 17:06:16 +0000374 // we can have one GrResource holding a lock on another
375 // so we don't want to just do a simple loop kicking each
376 // entry out. Instead change the budget and purge.
reed@google.comac10a2d2010-12-22 21:39:39 +0000377
robertphillips@google.comadacc702013-10-14 21:53:24 +0000378 size_t savedMaxBytes = fMaxBytes;
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000379 int savedMaxCount = fMaxCount;
380 fMaxBytes = (size_t) -1;
381 fMaxCount = 0;
bsalomon@google.come9a98942011-08-22 17:06:16 +0000382 this->purgeAsNeeded();
383
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000384#ifdef SK_DEBUG
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000385 SkASSERT(fExclusiveList.countEntries() == fClientDetachedCount);
386 SkASSERT(countBytes(fExclusiveList) == fClientDetachedBytes);
twiz@google.com0ec107f2012-02-21 19:15:53 +0000387 if (!fCache.count()) {
388 // Items may have been detached from the cache (such as the backing
389 // texture for an SkGpuDevice). The above purge would not have removed
390 // them.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000391 SkASSERT(fEntryCount == fClientDetachedCount);
392 SkASSERT(fEntryBytes == fClientDetachedBytes);
393 SkASSERT(fList.isEmpty());
twiz@google.com0ec107f2012-02-21 19:15:53 +0000394 }
395#endif
bsalomon@google.come9a98942011-08-22 17:06:16 +0000396
397 fMaxBytes = savedMaxBytes;
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000398 fMaxCount = savedMaxCount;
reed@google.comac10a2d2010-12-22 21:39:39 +0000399}
400
401///////////////////////////////////////////////////////////////////////////////
402
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000403#ifdef SK_DEBUG
bsalomon@google.coma2921122012-08-28 12:34:17 +0000404size_t GrResourceCache::countBytes(const EntryList& list) {
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000405 size_t bytes = 0;
406
bsalomon@google.coma2921122012-08-28 12:34:17 +0000407 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000408
bsalomon@google.coma2921122012-08-28 12:34:17 +0000409 const GrResourceEntry* entry = iter.init(const_cast<EntryList&>(list),
410 EntryList::Iter::kTail_IterStart);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000411
412 for ( ; NULL != entry; entry = iter.prev()) {
413 bytes += entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000414 }
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000415 return bytes;
reed@google.comac10a2d2010-12-22 21:39:39 +0000416}
417
reed@google.comb89a6432011-02-07 13:20:30 +0000418static bool both_zero_or_nonzero(int count, size_t bytes) {
419 return (count == 0 && bytes == 0) || (count > 0 && bytes > 0);
420}
reed@google.comb89a6432011-02-07 13:20:30 +0000421
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000422void GrResourceCache::validate() const {
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000423 fList.validate();
424 fExclusiveList.validate();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000425 SkASSERT(both_zero_or_nonzero(fEntryCount, fEntryBytes));
426 SkASSERT(both_zero_or_nonzero(fClientDetachedCount, fClientDetachedBytes));
427 SkASSERT(fClientDetachedBytes <= fEntryBytes);
428 SkASSERT(fClientDetachedCount <= fEntryCount);
429 SkASSERT((fEntryCount - fClientDetachedCount) == fCache.count());
reed@google.com01804b42011-01-18 21:50:41 +0000430
bsalomon@google.coma2921122012-08-28 12:34:17 +0000431 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000432
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000433 // check that the exclusively held entries are okay
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000434 const GrResourceEntry* entry = iter.init(const_cast<EntryList&>(fExclusiveList),
bsalomon@google.coma2921122012-08-28 12:34:17 +0000435 EntryList::Iter::kHead_IterStart);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000436
437 for ( ; NULL != entry; entry = iter.next()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000438 entry->validate();
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000439 }
440
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000441 // check that the shareable entries are okay
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000442 entry = iter.init(const_cast<EntryList&>(fList), EntryList::Iter::kHead_IterStart);
443
444 int count = 0;
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000445 for ( ; NULL != entry; entry = iter.next()) {
446 entry->validate();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000447 SkASSERT(fCache.find(entry->key()));
reed@google.comac10a2d2010-12-22 21:39:39 +0000448 count += 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000449 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000450 SkASSERT(count == fEntryCount - fClientDetachedCount);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000451
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000452 size_t bytes = countBytes(fList);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000453 SkASSERT(bytes == fEntryBytes - fClientDetachedBytes);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000454
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000455 bytes = countBytes(fExclusiveList);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000456 SkASSERT(bytes == fClientDetachedBytes);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000457
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000458 SkASSERT(fList.countEntries() == fEntryCount - fClientDetachedCount);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000459
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000460 SkASSERT(fExclusiveList.countEntries() == fClientDetachedCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000461}
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000462#endif // SK_DEBUG
robertphillips@google.com59552022012-08-31 13:07:37 +0000463
464#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000465
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000466void GrResourceCache::printStats() {
467 int locked = 0;
468
469 EntryList::Iter iter;
470
471 GrResourceEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart);
472
473 for ( ; NULL != entry; entry = iter.prev()) {
474 if (entry->fResource->getRefCnt() > 1) {
475 ++locked;
476 }
477 }
478
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000479 SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000480 SkDebugf("\t\tEntry Count: current %d (%d locked) high %d\n",
481 fEntryCount, locked, fHighWaterEntryCount);
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000482 SkDebugf("\t\tEntry Bytes: current %d high %d\n",
483 fEntryBytes, fHighWaterEntryBytes);
484 SkDebugf("\t\tDetached Entry Count: current %d high %d\n",
485 fClientDetachedCount, fHighWaterClientDetachedCount);
486 SkDebugf("\t\tDetached Bytes: current %d high %d\n",
487 fClientDetachedBytes, fHighWaterClientDetachedBytes);
488}
489
reed@google.comac10a2d2010-12-22 21:39:39 +0000490#endif
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000491
492///////////////////////////////////////////////////////////////////////////////