blob: 529c3a5d1db8979d1d16d151f30d18aa9d2d76ce [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"
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000012#include "GrCacheable.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
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000016///////////////////////////////////////////////////////////////////////////////
17
18void GrCacheable::didChangeGpuMemorySize() const {
19 if (this->isInCache()) {
20 fCacheEntry->didChangeResourceSize();
21 }
22}
23
24///////////////////////////////////////////////////////////////////////////////
25
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000026GrResourceKey::ResourceType GrResourceKey::GenerateResourceType() {
27 static int32_t gNextType = 0;
28
29 int32_t type = sk_atomic_inc(&gNextType);
30 if (type >= (1 << 8 * sizeof(ResourceType))) {
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +000031 SkFAIL("Too many Resource Types");
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000032 }
33
34 return static_cast<ResourceType>(type);
35}
36
37///////////////////////////////////////////////////////////////////////////////
38
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000039GrResourceCacheEntry::GrResourceCacheEntry(GrResourceCache* resourceCache,
40 const GrResourceKey& key,
41 GrCacheable* resource)
42 : fResourceCache(resourceCache),
43 fKey(key),
44 fResource(resource),
45 fCachedSize(resource->gpuMemorySize()),
46 fIsExclusive(false) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000047 // we assume ownership of the resource, and will unref it when we die
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000048 SkASSERT(resource);
skia.committer@gmail.com6c778162012-09-06 02:01:13 +000049 resource->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +000050}
51
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000052GrResourceCacheEntry::~GrResourceCacheEntry() {
robertphillips@google.com521eaf82012-08-22 11:03:19 +000053 fResource->setCacheEntry(NULL);
bsalomon@google.com50398bf2011-07-26 20:45:30 +000054 fResource->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +000055}
56
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000057#ifdef SK_DEBUG
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000058void GrResourceCacheEntry::validate() const {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000059 SkASSERT(fResourceCache);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000060 SkASSERT(fResource);
61 SkASSERT(fResource->getCacheEntry() == this);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000062 SkASSERT(fResource->gpuMemorySize() == fCachedSize);
bsalomon@google.com50398bf2011-07-26 20:45:30 +000063 fResource->validate();
reed@google.comac10a2d2010-12-22 21:39:39 +000064}
65#endif
66
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000067void GrResourceCacheEntry::didChangeResourceSize() {
68 size_t oldSize = fCachedSize;
69 fCachedSize = fResource->gpuMemorySize();
70 if (fCachedSize > oldSize) {
71 fResourceCache->didIncreaseResourceSize(this, fCachedSize - oldSize);
72 } else if (fCachedSize < oldSize) {
73 fResourceCache->didDecreaseResourceSize(this, oldSize - fCachedSize);
74 }
75}
76
reed@google.comac10a2d2010-12-22 21:39:39 +000077///////////////////////////////////////////////////////////////////////////////
78
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000079GrResourceCache::GrResourceCache(int maxCount, size_t maxBytes) :
80 fMaxCount(maxCount),
81 fMaxBytes(maxBytes) {
robertphillips@google.com59552022012-08-31 13:07:37 +000082#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +000083 fHighWaterEntryCount = 0;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +000084 fHighWaterEntryBytes = 0;
85 fHighWaterClientDetachedCount = 0;
86 fHighWaterClientDetachedBytes = 0;
87#endif
88
89 fEntryCount = 0;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +000090 fEntryBytes = 0;
91 fClientDetachedCount = 0;
92 fClientDetachedBytes = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +000093
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +000094 fPurging = false;
95
96 fOverbudgetCB = NULL;
97 fOverbudgetData = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000098}
99
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000100GrResourceCache::~GrResourceCache() {
101 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +0000102
bsalomon@google.coma2921122012-08-28 12:34:17 +0000103 EntryList::Iter iter;
104
105 // Unlike the removeAll, here we really remove everything, including locked resources.
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000106 while (GrResourceCacheEntry* entry = fList.head()) {
bsalomon@google.coma2921122012-08-28 12:34:17 +0000107 GrAutoResourceCacheValidate atcv(this);
108
109 // remove from our cache
110 fCache.remove(entry->fKey, entry);
111
112 // remove from our llist
robertphillips@google.com209a1142012-10-31 12:25:21 +0000113 this->internalDetach(entry);
bsalomon@google.coma2921122012-08-28 12:34:17 +0000114
115 delete entry;
116 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000117}
118
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000119void GrResourceCache::getLimits(int* maxResources, size_t* maxResourceBytes) const{
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000120 if (NULL != maxResources) {
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000121 *maxResources = fMaxCount;
122 }
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000123 if (NULL != maxResourceBytes) {
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000124 *maxResourceBytes = fMaxBytes;
125 }
126}
reed@google.com01804b42011-01-18 21:50:41 +0000127
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000128void GrResourceCache::setLimits(int maxResources, size_t maxResourceBytes) {
129 bool smaller = (maxResources < fMaxCount) || (maxResourceBytes < fMaxBytes);
130
131 fMaxCount = maxResources;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000132 fMaxBytes = maxResourceBytes;
reed@google.com01804b42011-01-18 21:50:41 +0000133
134 if (smaller) {
135 this->purgeAsNeeded();
136 }
137}
138
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000139void GrResourceCache::internalDetach(GrResourceCacheEntry* entry,
robertphillips@google.com209a1142012-10-31 12:25:21 +0000140 BudgetBehaviors behavior) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000141 fList.remove(entry);
reed@google.comac10a2d2010-12-22 21:39:39 +0000142
reed@google.comac10a2d2010-12-22 21:39:39 +0000143 // update our stats
robertphillips@google.com209a1142012-10-31 12:25:21 +0000144 if (kIgnore_BudgetBehavior == behavior) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000145 fClientDetachedCount += 1;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000146 fClientDetachedBytes += entry->fCachedSize;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000147
robertphillips@google.com59552022012-08-31 13:07:37 +0000148#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000149 if (fHighWaterClientDetachedCount < fClientDetachedCount) {
150 fHighWaterClientDetachedCount = fClientDetachedCount;
151 }
152 if (fHighWaterClientDetachedBytes < fClientDetachedBytes) {
153 fHighWaterClientDetachedBytes = fClientDetachedBytes;
154 }
155#endif
156
reed@google.comac10a2d2010-12-22 21:39:39 +0000157 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000158 SkASSERT(kAccountFor_BudgetBehavior == behavior);
robertphillips@google.com209a1142012-10-31 12:25:21 +0000159
reed@google.comac10a2d2010-12-22 21:39:39 +0000160 fEntryCount -= 1;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000161 fEntryBytes -= entry->fCachedSize;
reed@google.comac10a2d2010-12-22 21:39:39 +0000162 }
163}
164
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000165void GrResourceCache::attachToHead(GrResourceCacheEntry* entry,
robertphillips@google.com209a1142012-10-31 12:25:21 +0000166 BudgetBehaviors behavior) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000167 fList.addToHead(entry);
168
reed@google.comac10a2d2010-12-22 21:39:39 +0000169 // update our stats
robertphillips@google.com209a1142012-10-31 12:25:21 +0000170 if (kIgnore_BudgetBehavior == behavior) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000171 fClientDetachedCount -= 1;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000172 fClientDetachedBytes -= entry->fCachedSize;
reed@google.comac10a2d2010-12-22 21:39:39 +0000173 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000174 SkASSERT(kAccountFor_BudgetBehavior == behavior);
robertphillips@google.com209a1142012-10-31 12:25:21 +0000175
reed@google.comac10a2d2010-12-22 21:39:39 +0000176 fEntryCount += 1;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000177 fEntryBytes += entry->fCachedSize;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000178
robertphillips@google.com59552022012-08-31 13:07:37 +0000179#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000180 if (fHighWaterEntryCount < fEntryCount) {
181 fHighWaterEntryCount = fEntryCount;
182 }
183 if (fHighWaterEntryBytes < fEntryBytes) {
184 fHighWaterEntryBytes = fEntryBytes;
185 }
186#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000187 }
188}
189
robertphillips@google.com209a1142012-10-31 12:25:21 +0000190// This functor just searches for an entry with only a single ref (from
191// the texture cache itself). Presumably in this situation no one else
192// is relying on the texture.
193class GrTFindUnreffedFunctor {
194public:
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000195 bool operator()(const GrResourceCacheEntry* entry) const {
bungeman@google.comf64c6842013-07-19 23:18:52 +0000196 return entry->resource()->unique();
robertphillips@google.com209a1142012-10-31 12:25:21 +0000197 }
198};
199
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000200GrCacheable* GrResourceCache::find(const GrResourceKey& key, uint32_t ownershipFlags) {
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000201 GrAutoResourceCacheValidate atcv(this);
202
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000203 GrResourceCacheEntry* entry = NULL;
robertphillips@google.com209a1142012-10-31 12:25:21 +0000204
205 if (ownershipFlags & kNoOtherOwners_OwnershipFlag) {
206 GrTFindUnreffedFunctor functor;
207
208 entry = fCache.find<GrTFindUnreffedFunctor>(key, functor);
209 } else {
210 entry = fCache.find(key);
211 }
212
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000213 if (NULL == entry) {
214 return NULL;
215 }
216
robertphillips@google.com209a1142012-10-31 12:25:21 +0000217 if (ownershipFlags & kHide_OwnershipFlag) {
218 this->makeExclusive(entry);
219 } else {
220 // Make this resource MRU
221 this->internalDetach(entry);
222 this->attachToHead(entry);
223 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000224
225 return entry->fResource;
reed@google.comac10a2d2010-12-22 21:39:39 +0000226}
227
skia.committer@gmail.comf3dc1992012-11-01 02:01:27 +0000228void GrResourceCache::addResource(const GrResourceKey& key,
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000229 GrCacheable* resource,
robertphillips@google.com209a1142012-10-31 12:25:21 +0000230 uint32_t ownershipFlags) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000231 SkASSERT(NULL == resource->getCacheEntry());
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000232 // we don't expect to create new resources during a purge. In theory
233 // this could cause purgeAsNeeded() into an infinite loop (e.g.
234 // each resource destroyed creates and locks 2 resources and
235 // unlocks 1 thereby causing a new purge).
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000236 SkASSERT(!fPurging);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000237 GrAutoResourceCacheValidate atcv(this);
reed@google.comac10a2d2010-12-22 21:39:39 +0000238
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000239 GrResourceCacheEntry* entry = SkNEW_ARGS(GrResourceCacheEntry, (this, key, resource));
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000240 resource->setCacheEntry(entry);
241
robertphillips@google.com209a1142012-10-31 12:25:21 +0000242 this->attachToHead(entry);
reed@google.comac10a2d2010-12-22 21:39:39 +0000243 fCache.insert(key, entry);
244
robertphillips@google.com209a1142012-10-31 12:25:21 +0000245 if (ownershipFlags & kHide_OwnershipFlag) {
246 this->makeExclusive(entry);
247 }
248
reed@google.comac10a2d2010-12-22 21:39:39 +0000249}
250
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000251void GrResourceCache::makeExclusive(GrResourceCacheEntry* entry) {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000252 GrAutoResourceCacheValidate atcv(this);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000253
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000254 SkASSERT(!entry->fIsExclusive);
255 entry->fIsExclusive = true;
256
robertphillips@google.com209a1142012-10-31 12:25:21 +0000257 // When scratch textures are detached (to hide them from future finds) they
258 // still count against the resource budget
259 this->internalDetach(entry, kIgnore_BudgetBehavior);
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000260 fCache.remove(entry->key(), entry);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000261
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000262#ifdef SK_DEBUG
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000263 fExclusiveList.addToHead(entry);
264#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000265}
266
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000267void GrResourceCache::removeInvalidResource(GrResourceCacheEntry* entry) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000268 // If the resource went invalid while it was detached then purge it
269 // This can happen when a 3D context was lost,
270 // the client called GrContext::contextDestroyed() to notify Gr,
271 // and then later an SkGpuDevice's destructor releases its backing
272 // texture (which was invalidated at contextDestroyed time).
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000273 // TODO: Safely delete the GrResourceCacheEntry as well.
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000274 fClientDetachedCount -= 1;
275 fEntryCount -= 1;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000276 fClientDetachedBytes -= entry->fCachedSize;
277 fEntryBytes -= entry->fCachedSize;
278 entry->fCachedSize = 0;
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000279}
280
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000281void GrResourceCache::makeNonExclusive(GrResourceCacheEntry* entry) {
bsalomon@google.com60879752011-09-15 20:43:53 +0000282 GrAutoResourceCacheValidate atcv(this);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000283
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000284#ifdef SK_DEBUG
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000285 fExclusiveList.remove(entry);
286#endif
287
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000288 if (entry->resource()->isValidOnGpu()) {
skia.committer@gmail.comf3dc1992012-11-01 02:01:27 +0000289 // Since scratch textures still count against the cache budget even
290 // when they have been removed from the cache, re-adding them doesn't
robertphillips@google.com209a1142012-10-31 12:25:21 +0000291 // alter the budget information.
292 attachToHead(entry, kIgnore_BudgetBehavior);
bsalomon@google.com60879752011-09-15 20:43:53 +0000293 fCache.insert(entry->key(), entry);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000294
295 SkASSERT(entry->fIsExclusive);
296 entry->fIsExclusive = false;
bsalomon@google.com60879752011-09-15 20:43:53 +0000297 } else {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000298 this->removeInvalidResource(entry);
bsalomon@google.com60879752011-09-15 20:43:53 +0000299 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000300}
301
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000302void GrResourceCache::didIncreaseResourceSize(const GrResourceCacheEntry* entry, size_t amountInc) {
303 fEntryBytes += amountInc;
304 if (entry->fIsExclusive) {
305 fClientDetachedBytes += amountInc;
306 }
307 this->purgeAsNeeded();
308}
309
310void GrResourceCache::didDecreaseResourceSize(const GrResourceCacheEntry* entry, size_t amountDec) {
311 fEntryBytes -= amountDec;
312 if (entry->fIsExclusive) {
313 fClientDetachedBytes -= amountDec;
314 }
315#ifdef SK_DEBUG
316 this->validate();
317#endif
318}
319
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000320/**
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000321 * Destroying a resource may potentially trigger the unlock of additional
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000322 * resources which in turn will trigger a nested purge. We block the nested
323 * purge using the fPurging variable. However, the initial purge will keep
324 * looping until either all resources in the cache are unlocked or we've met
325 * the budget. There is an assertion in createAndLock to check against a
326 * resource's destructor inserting new resources into the cache. If these
327 * new resources were unlocked before purgeAsNeeded completed it could
328 * potentially make purgeAsNeeded loop infinitely.
robertphillips@google.com41d25322013-07-18 17:12:57 +0000329 *
330 * extraCount and extraBytes are added to the current resource totals to account
331 * for incoming resources (e.g., GrContext is about to add 10MB split between
332 * 10 textures).
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000333 */
robertphillips@google.com41d25322013-07-18 17:12:57 +0000334void GrResourceCache::purgeAsNeeded(int extraCount, size_t extraBytes) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000335 if (fPurging) {
336 return;
reed@google.comac10a2d2010-12-22 21:39:39 +0000337 }
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000338
339 fPurging = true;
340
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000341 this->purgeInvalidated();
342
robertphillips@google.com41d25322013-07-18 17:12:57 +0000343 this->internalPurge(extraCount, extraBytes);
skia.committer@gmail.coma7991982013-07-19 07:00:57 +0000344 if (((fEntryCount+extraCount) > fMaxCount ||
robertphillips@google.com41d25322013-07-18 17:12:57 +0000345 (fEntryBytes+extraBytes) > fMaxBytes) &&
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000346 NULL != fOverbudgetCB) {
347 // Despite the purge we're still over budget. See if Ganesh can
348 // release some resources and purge again.
349 if ((*fOverbudgetCB)(fOverbudgetData)) {
robertphillips@google.com41d25322013-07-18 17:12:57 +0000350 this->internalPurge(extraCount, extraBytes);
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000351 }
352 }
353
354 fPurging = false;
355}
356
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000357void GrResourceCache::purgeInvalidated() {
358 SkTDArray<GrResourceInvalidatedMessage> invalidated;
359 fInvalidationInbox.poll(&invalidated);
360
361 for (int i = 0; i < invalidated.count(); i++) {
362 // We're somewhat missing an opportunity here. We could use the
363 // default find functor that gives us back resources whether we own
364 // them exclusively or not, and when they're not exclusively owned mark
365 // them for purging later when they do become exclusively owned.
366 //
367 // This is complicated and confusing. May try this in the future. For
368 // now, these resources are just LRU'd as if we never got the message.
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000369 while (GrResourceCacheEntry* entry = fCache.find(invalidated[i].key, GrTFindUnreffedFunctor())) {
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000370 this->deleteResource(entry);
371 }
372 }
373}
374
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000375void GrResourceCache::deleteResource(GrResourceCacheEntry* entry) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000376 SkASSERT(1 == entry->fResource->getRefCnt());
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000377
378 // remove from our cache
379 fCache.remove(entry->key(), entry);
380
381 // remove from our llist
382 this->internalDetach(entry);
383 delete entry;
384}
385
robertphillips@google.com41d25322013-07-18 17:12:57 +0000386void GrResourceCache::internalPurge(int extraCount, size_t extraBytes) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000387 SkASSERT(fPurging);
388
389 bool withinBudget = false;
390 bool changed = false;
391
392 // The purging process is repeated several times since one pass
393 // may free up other resources
394 do {
395 EntryList::Iter iter;
396
397 changed = false;
398
399 // Note: the following code relies on the fact that the
400 // doubly linked list doesn't invalidate its data/pointers
401 // outside of the specific area where a deletion occurs (e.g.,
402 // in internalDetach)
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000403 GrResourceCacheEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart);
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000404
405 while (NULL != entry) {
406 GrAutoResourceCacheValidate atcv(this);
407
skia.committer@gmail.coma7991982013-07-19 07:00:57 +0000408 if ((fEntryCount+extraCount) <= fMaxCount &&
robertphillips@google.com41d25322013-07-18 17:12:57 +0000409 (fEntryBytes+extraBytes) <= fMaxBytes) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000410 withinBudget = true;
411 break;
412 }
413
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000414 GrResourceCacheEntry* prev = iter.prev();
bungeman@google.comf64c6842013-07-19 23:18:52 +0000415 if (entry->fResource->unique()) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000416 changed = true;
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000417 this->deleteResource(entry);
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000418 }
419 entry = prev;
420 }
421 } while (!withinBudget && changed);
reed@google.comac10a2d2010-12-22 21:39:39 +0000422}
423
bsalomon@google.coma2921122012-08-28 12:34:17 +0000424void GrResourceCache::purgeAllUnlocked() {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000425 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +0000426
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000427 // we can have one GrCacheable holding a lock on another
bsalomon@google.come9a98942011-08-22 17:06:16 +0000428 // so we don't want to just do a simple loop kicking each
429 // entry out. Instead change the budget and purge.
reed@google.comac10a2d2010-12-22 21:39:39 +0000430
robertphillips@google.comadacc702013-10-14 21:53:24 +0000431 size_t savedMaxBytes = fMaxBytes;
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000432 int savedMaxCount = fMaxCount;
433 fMaxBytes = (size_t) -1;
434 fMaxCount = 0;
bsalomon@google.come9a98942011-08-22 17:06:16 +0000435 this->purgeAsNeeded();
436
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000437#ifdef SK_DEBUG
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000438 SkASSERT(fExclusiveList.countEntries() == fClientDetachedCount);
439 SkASSERT(countBytes(fExclusiveList) == fClientDetachedBytes);
twiz@google.com0ec107f2012-02-21 19:15:53 +0000440 if (!fCache.count()) {
441 // Items may have been detached from the cache (such as the backing
442 // texture for an SkGpuDevice). The above purge would not have removed
443 // them.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000444 SkASSERT(fEntryCount == fClientDetachedCount);
445 SkASSERT(fEntryBytes == fClientDetachedBytes);
446 SkASSERT(fList.isEmpty());
twiz@google.com0ec107f2012-02-21 19:15:53 +0000447 }
448#endif
bsalomon@google.come9a98942011-08-22 17:06:16 +0000449
450 fMaxBytes = savedMaxBytes;
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000451 fMaxCount = savedMaxCount;
reed@google.comac10a2d2010-12-22 21:39:39 +0000452}
453
454///////////////////////////////////////////////////////////////////////////////
455
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000456#ifdef SK_DEBUG
bsalomon@google.coma2921122012-08-28 12:34:17 +0000457size_t GrResourceCache::countBytes(const EntryList& list) {
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000458 size_t bytes = 0;
459
bsalomon@google.coma2921122012-08-28 12:34:17 +0000460 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000461
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000462 const GrResourceCacheEntry* entry = iter.init(const_cast<EntryList&>(list),
463 EntryList::Iter::kTail_IterStart);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000464
465 for ( ; NULL != entry; entry = iter.prev()) {
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000466 bytes += entry->resource()->gpuMemorySize();
reed@google.comac10a2d2010-12-22 21:39:39 +0000467 }
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000468 return bytes;
reed@google.comac10a2d2010-12-22 21:39:39 +0000469}
470
reed@google.comb89a6432011-02-07 13:20:30 +0000471static bool both_zero_or_nonzero(int count, size_t bytes) {
472 return (count == 0 && bytes == 0) || (count > 0 && bytes > 0);
473}
reed@google.comb89a6432011-02-07 13:20:30 +0000474
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000475void GrResourceCache::validate() const {
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000476 fList.validate();
477 fExclusiveList.validate();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000478 SkASSERT(both_zero_or_nonzero(fEntryCount, fEntryBytes));
479 SkASSERT(both_zero_or_nonzero(fClientDetachedCount, fClientDetachedBytes));
480 SkASSERT(fClientDetachedBytes <= fEntryBytes);
481 SkASSERT(fClientDetachedCount <= fEntryCount);
482 SkASSERT((fEntryCount - fClientDetachedCount) == fCache.count());
reed@google.com01804b42011-01-18 21:50:41 +0000483
bsalomon@google.coma2921122012-08-28 12:34:17 +0000484 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000485
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000486 // check that the exclusively held entries are okay
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000487 const GrResourceCacheEntry* entry = iter.init(const_cast<EntryList&>(fExclusiveList),
488 EntryList::Iter::kHead_IterStart);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000489
490 for ( ; NULL != entry; entry = iter.next()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000491 entry->validate();
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000492 }
493
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000494 // check that the shareable entries are okay
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000495 entry = iter.init(const_cast<EntryList&>(fList), EntryList::Iter::kHead_IterStart);
496
497 int count = 0;
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000498 for ( ; NULL != entry; entry = iter.next()) {
499 entry->validate();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000500 SkASSERT(fCache.find(entry->key()));
reed@google.comac10a2d2010-12-22 21:39:39 +0000501 count += 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000502 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000503 SkASSERT(count == fEntryCount - fClientDetachedCount);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000504
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000505 size_t bytes = countBytes(fList);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000506 SkASSERT(bytes == fEntryBytes - fClientDetachedBytes);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000507
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000508 bytes = countBytes(fExclusiveList);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000509 SkASSERT(bytes == fClientDetachedBytes);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000510
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000511 SkASSERT(fList.countEntries() == fEntryCount - fClientDetachedCount);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000512
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000513 SkASSERT(fExclusiveList.countEntries() == fClientDetachedCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000514}
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000515#endif // SK_DEBUG
robertphillips@google.com59552022012-08-31 13:07:37 +0000516
517#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000518
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000519void GrResourceCache::printStats() {
520 int locked = 0;
521
522 EntryList::Iter iter;
523
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000524 GrResourceCacheEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000525
526 for ( ; NULL != entry; entry = iter.prev()) {
527 if (entry->fResource->getRefCnt() > 1) {
528 ++locked;
529 }
530 }
531
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000532 SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000533 SkDebugf("\t\tEntry Count: current %d (%d locked) high %d\n",
534 fEntryCount, locked, fHighWaterEntryCount);
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000535 SkDebugf("\t\tEntry Bytes: current %d high %d\n",
536 fEntryBytes, fHighWaterEntryBytes);
537 SkDebugf("\t\tDetached Entry Count: current %d high %d\n",
538 fClientDetachedCount, fHighWaterClientDetachedCount);
539 SkDebugf("\t\tDetached Bytes: current %d high %d\n",
540 fClientDetachedBytes, fHighWaterClientDetachedBytes);
541}
542
reed@google.comac10a2d2010-12-22 21:39:39 +0000543#endif
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000544
545///////////////////////////////////////////////////////////////////////////////