blob: f70d141c72aeb6e461a2d5510cfc4421b16dc71e [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
bsalomon@google.com50398bf2011-07-26 20:45:30 +00009#include "GrResourceCache.h"
bsalomon6d3fe022014-07-25 08:35:45 -070010#include "GrGpuResource.h"
bsalomonbcf0a522014-10-08 08:40:09 -070011#include "GrTexturePriv.h"
12
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +000013DECLARE_SKMESSAGEBUS_MESSAGE(GrResourceInvalidatedMessage);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000014
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000015///////////////////////////////////////////////////////////////////////////////
16
bsalomon6d3fe022014-07-25 08:35:45 -070017void GrGpuResource::didChangeGpuMemorySize() const {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000018 if (this->isInCache()) {
19 fCacheEntry->didChangeResourceSize();
20 }
21}
22
23///////////////////////////////////////////////////////////////////////////////
24
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000025GrResourceKey::ResourceType GrResourceKey::GenerateResourceType() {
26 static int32_t gNextType = 0;
27
28 int32_t type = sk_atomic_inc(&gNextType);
29 if (type >= (1 << 8 * sizeof(ResourceType))) {
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +000030 SkFAIL("Too many Resource Types");
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000031 }
32
33 return static_cast<ResourceType>(type);
34}
35
36///////////////////////////////////////////////////////////////////////////////
37
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000038GrResourceCacheEntry::GrResourceCacheEntry(GrResourceCache* resourceCache,
39 const GrResourceKey& key,
bsalomon6d3fe022014-07-25 08:35:45 -070040 GrGpuResource* resource)
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000041 : fResourceCache(resourceCache),
42 fKey(key),
43 fResource(resource),
44 fCachedSize(resource->gpuMemorySize()),
45 fIsExclusive(false) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000046 // we assume ownership of the resource, and will unref it when we die
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000047 SkASSERT(resource);
skia.committer@gmail.com6c778162012-09-06 02:01:13 +000048 resource->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +000049}
50
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000051GrResourceCacheEntry::~GrResourceCacheEntry() {
robertphillips@google.com521eaf82012-08-22 11:03:19 +000052 fResource->setCacheEntry(NULL);
bsalomon@google.com50398bf2011-07-26 20:45:30 +000053 fResource->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +000054}
55
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000056#ifdef SK_DEBUG
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000057void GrResourceCacheEntry::validate() const {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000058 SkASSERT(fResourceCache);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000059 SkASSERT(fResource);
60 SkASSERT(fResource->getCacheEntry() == this);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000061 SkASSERT(fResource->gpuMemorySize() == fCachedSize);
bsalomon@google.com50398bf2011-07-26 20:45:30 +000062 fResource->validate();
reed@google.comac10a2d2010-12-22 21:39:39 +000063}
64#endif
65
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000066void GrResourceCacheEntry::didChangeResourceSize() {
67 size_t oldSize = fCachedSize;
68 fCachedSize = fResource->gpuMemorySize();
69 if (fCachedSize > oldSize) {
70 fResourceCache->didIncreaseResourceSize(this, fCachedSize - oldSize);
71 } else if (fCachedSize < oldSize) {
72 fResourceCache->didDecreaseResourceSize(this, oldSize - fCachedSize);
73 }
74}
75
reed@google.comac10a2d2010-12-22 21:39:39 +000076///////////////////////////////////////////////////////////////////////////////
77
bsalomonbcf0a522014-10-08 08:40:09 -070078GrResourceCache::GrResourceCache(const GrDrawTargetCaps* caps, int maxCount, size_t maxBytes)
79 : fMaxCount(maxCount)
80 , fMaxBytes(maxBytes)
81 , fCaps(SkRef(caps)) {
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;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +000085#endif
86
87 fEntryCount = 0;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +000088 fEntryBytes = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +000089
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +000090 fPurging = false;
91
92 fOverbudgetCB = NULL;
93 fOverbudgetData = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000094}
95
bsalomon@google.com50398bf2011-07-26 20:45:30 +000096GrResourceCache::~GrResourceCache() {
97 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +000098
bsalomon@google.coma2921122012-08-28 12:34:17 +000099 EntryList::Iter iter;
100
101 // Unlike the removeAll, here we really remove everything, including locked resources.
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000102 while (GrResourceCacheEntry* entry = fList.head()) {
bsalomon@google.coma2921122012-08-28 12:34:17 +0000103 GrAutoResourceCacheValidate atcv(this);
104
105 // remove from our cache
106 fCache.remove(entry->fKey, entry);
107
108 // remove from our llist
robertphillips@google.com209a1142012-10-31 12:25:21 +0000109 this->internalDetach(entry);
bsalomon@google.coma2921122012-08-28 12:34:17 +0000110
111 delete entry;
112 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000113}
114
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000115void GrResourceCache::getLimits(int* maxResources, size_t* maxResourceBytes) const{
bsalomon49f085d2014-09-05 13:34:00 -0700116 if (maxResources) {
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000117 *maxResources = fMaxCount;
118 }
bsalomon49f085d2014-09-05 13:34:00 -0700119 if (maxResourceBytes) {
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000120 *maxResourceBytes = fMaxBytes;
121 }
122}
reed@google.com01804b42011-01-18 21:50:41 +0000123
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000124void GrResourceCache::setLimits(int maxResources, size_t maxResourceBytes) {
125 bool smaller = (maxResources < fMaxCount) || (maxResourceBytes < fMaxBytes);
126
127 fMaxCount = maxResources;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000128 fMaxBytes = maxResourceBytes;
reed@google.com01804b42011-01-18 21:50:41 +0000129
130 if (smaller) {
131 this->purgeAsNeeded();
132 }
133}
134
bsalomonbcf0a522014-10-08 08:40:09 -0700135void GrResourceCache::internalDetach(GrResourceCacheEntry* entry) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000136 fList.remove(entry);
bsalomonbcf0a522014-10-08 08:40:09 -0700137 fEntryCount -= 1;
138 fEntryBytes -= entry->fCachedSize;
Brian Salomon9323b8b2014-10-07 15:07:38 -0400139}
140
bsalomonbcf0a522014-10-08 08:40:09 -0700141void GrResourceCache::attachToHead(GrResourceCacheEntry* entry) {
Brian Salomon9323b8b2014-10-07 15:07:38 -0400142 fList.addToHead(entry);
143
bsalomonbcf0a522014-10-08 08:40:09 -0700144 fEntryCount += 1;
145 fEntryBytes += entry->fCachedSize;
Brian Salomon9323b8b2014-10-07 15:07:38 -0400146
147#if GR_CACHE_STATS
bsalomonbcf0a522014-10-08 08:40:09 -0700148 if (fHighWaterEntryCount < fEntryCount) {
149 fHighWaterEntryCount = fEntryCount;
Brian Salomon9323b8b2014-10-07 15:07:38 -0400150 }
bsalomonbcf0a522014-10-08 08:40:09 -0700151 if (fHighWaterEntryBytes < fEntryBytes) {
152 fHighWaterEntryBytes = fEntryBytes;
153 }
154#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000155}
156
robertphillips@google.com209a1142012-10-31 12:25:21 +0000157// This functor just searches for an entry with only a single ref (from
158// the texture cache itself). Presumably in this situation no one else
159// is relying on the texture.
160class GrTFindUnreffedFunctor {
161public:
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000162 bool operator()(const GrResourceCacheEntry* entry) const {
bsalomonbcf0a522014-10-08 08:40:09 -0700163 return entry->resource()->isPurgable();
robertphillips@google.com209a1142012-10-31 12:25:21 +0000164 }
165};
166
bsalomonbcf0a522014-10-08 08:40:09 -0700167
168void GrResourceCache::makeResourceMRU(GrGpuResource* resource) {
169 GrResourceCacheEntry* entry = resource->getCacheEntry();
170 if (entry) {
171 this->internalDetach(entry);
172 this->attachToHead(entry);
173 }
174}
175
176void GrResourceCache::notifyPurgable(const GrGpuResource* resource) {
177 // Remove scratch textures from the cache the moment they become purgeable if
178 // scratch texture reuse is turned off.
179 SkASSERT(resource->getCacheEntry());
180 if (resource->getCacheEntry()->key().getResourceType() == GrTexturePriv::ResourceType() &&
bsalomon1e2530b2014-10-09 09:57:18 -0700181 resource->getCacheEntry()->key().isScratch() &&
bsalomonbcf0a522014-10-08 08:40:09 -0700182 !fCaps->reuseScratchTextures() &&
183 !(static_cast<const GrTexture*>(resource)->desc().fFlags &
184 kRenderTarget_GrTextureFlagBit)) {
185 this->deleteResource(resource->getCacheEntry());
186 }
187}
188
189GrGpuResource* GrResourceCache::find(const GrResourceKey& key) {
bsalomon1e2530b2014-10-09 09:57:18 -0700190 // GrResourceCache2 is responsible for scratch resources.
191 SkASSERT(!key.isScratch());
192
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000193 GrAutoResourceCacheValidate atcv(this);
194
bsalomon1e2530b2014-10-09 09:57:18 -0700195 GrResourceCacheEntry* entry = fCache.find(key);
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000196 if (NULL == entry) {
197 return NULL;
198 }
199
bsalomonbcf0a522014-10-08 08:40:09 -0700200 // Make this resource MRU
201 this->internalDetach(entry);
202 this->attachToHead(entry);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000203
204 return entry->fResource;
reed@google.comac10a2d2010-12-22 21:39:39 +0000205}
206
bsalomonbcf0a522014-10-08 08:40:09 -0700207void GrResourceCache::addResource(const GrResourceKey& key, GrGpuResource* resource) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000208 SkASSERT(NULL == resource->getCacheEntry());
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000209 // we don't expect to create new resources during a purge. In theory
210 // this could cause purgeAsNeeded() into an infinite loop (e.g.
211 // each resource destroyed creates and locks 2 resources and
212 // unlocks 1 thereby causing a new purge).
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000213 SkASSERT(!fPurging);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000214 GrAutoResourceCacheValidate atcv(this);
reed@google.comac10a2d2010-12-22 21:39:39 +0000215
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000216 GrResourceCacheEntry* entry = SkNEW_ARGS(GrResourceCacheEntry, (this, key, resource));
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000217 resource->setCacheEntry(entry);
218
robertphillips@google.com209a1142012-10-31 12:25:21 +0000219 this->attachToHead(entry);
reed@google.comac10a2d2010-12-22 21:39:39 +0000220 fCache.insert(key, entry);
Brian Salomon9323b8b2014-10-07 15:07:38 -0400221
bsalomonbcf0a522014-10-08 08:40:09 -0700222 this->purgeAsNeeded();
reed@google.comac10a2d2010-12-22 21:39:39 +0000223}
224
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000225void GrResourceCache::didIncreaseResourceSize(const GrResourceCacheEntry* entry, size_t amountInc) {
226 fEntryBytes += amountInc;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000227 this->purgeAsNeeded();
228}
229
230void GrResourceCache::didDecreaseResourceSize(const GrResourceCacheEntry* entry, size_t amountDec) {
231 fEntryBytes -= amountDec;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000232#ifdef SK_DEBUG
233 this->validate();
234#endif
235}
236
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000237/**
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000238 * Destroying a resource may potentially trigger the unlock of additional
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000239 * resources which in turn will trigger a nested purge. We block the nested
240 * purge using the fPurging variable. However, the initial purge will keep
241 * looping until either all resources in the cache are unlocked or we've met
242 * the budget. There is an assertion in createAndLock to check against a
243 * resource's destructor inserting new resources into the cache. If these
244 * new resources were unlocked before purgeAsNeeded completed it could
245 * potentially make purgeAsNeeded loop infinitely.
robertphillips@google.com41d25322013-07-18 17:12:57 +0000246 *
247 * extraCount and extraBytes are added to the current resource totals to account
248 * for incoming resources (e.g., GrContext is about to add 10MB split between
249 * 10 textures).
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000250 */
robertphillips@google.com41d25322013-07-18 17:12:57 +0000251void GrResourceCache::purgeAsNeeded(int extraCount, size_t extraBytes) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000252 if (fPurging) {
253 return;
reed@google.comac10a2d2010-12-22 21:39:39 +0000254 }
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000255
256 fPurging = true;
257
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000258 this->purgeInvalidated();
259
robertphillips@google.com41d25322013-07-18 17:12:57 +0000260 this->internalPurge(extraCount, extraBytes);
skia.committer@gmail.coma7991982013-07-19 07:00:57 +0000261 if (((fEntryCount+extraCount) > fMaxCount ||
robertphillips@google.com41d25322013-07-18 17:12:57 +0000262 (fEntryBytes+extraBytes) > fMaxBytes) &&
bsalomon49f085d2014-09-05 13:34:00 -0700263 fOverbudgetCB) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000264 // Despite the purge we're still over budget. See if Ganesh can
265 // release some resources and purge again.
266 if ((*fOverbudgetCB)(fOverbudgetData)) {
robertphillips@google.com41d25322013-07-18 17:12:57 +0000267 this->internalPurge(extraCount, extraBytes);
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000268 }
269 }
270
271 fPurging = false;
272}
273
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000274void GrResourceCache::purgeInvalidated() {
275 SkTDArray<GrResourceInvalidatedMessage> invalidated;
276 fInvalidationInbox.poll(&invalidated);
277
278 for (int i = 0; i < invalidated.count(); i++) {
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000279 while (GrResourceCacheEntry* entry = fCache.find(invalidated[i].key, GrTFindUnreffedFunctor())) {
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000280 this->deleteResource(entry);
281 }
282 }
283}
284
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000285void GrResourceCache::deleteResource(GrResourceCacheEntry* entry) {
bsalomonbcf0a522014-10-08 08:40:09 -0700286 SkASSERT(entry->fResource->isPurgable());
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000287
288 // remove from our cache
289 fCache.remove(entry->key(), entry);
290
291 // remove from our llist
292 this->internalDetach(entry);
293 delete entry;
294}
295
robertphillips@google.com41d25322013-07-18 17:12:57 +0000296void GrResourceCache::internalPurge(int extraCount, size_t extraBytes) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000297 SkASSERT(fPurging);
298
299 bool withinBudget = false;
300 bool changed = false;
301
302 // The purging process is repeated several times since one pass
303 // may free up other resources
304 do {
305 EntryList::Iter iter;
306
307 changed = false;
308
309 // Note: the following code relies on the fact that the
310 // doubly linked list doesn't invalidate its data/pointers
311 // outside of the specific area where a deletion occurs (e.g.,
312 // in internalDetach)
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000313 GrResourceCacheEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart);
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000314
bsalomon49f085d2014-09-05 13:34:00 -0700315 while (entry) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000316 GrAutoResourceCacheValidate atcv(this);
317
skia.committer@gmail.coma7991982013-07-19 07:00:57 +0000318 if ((fEntryCount+extraCount) <= fMaxCount &&
robertphillips@google.com41d25322013-07-18 17:12:57 +0000319 (fEntryBytes+extraBytes) <= fMaxBytes) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000320 withinBudget = true;
321 break;
322 }
323
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000324 GrResourceCacheEntry* prev = iter.prev();
bsalomonbcf0a522014-10-08 08:40:09 -0700325 if (entry->fResource->isPurgable()) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000326 changed = true;
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000327 this->deleteResource(entry);
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000328 }
329 entry = prev;
330 }
331 } while (!withinBudget && changed);
reed@google.comac10a2d2010-12-22 21:39:39 +0000332}
333
bsalomon@google.coma2921122012-08-28 12:34:17 +0000334void GrResourceCache::purgeAllUnlocked() {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000335 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +0000336
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000337 // we can have one GrCacheable holding a lock on another
bsalomon@google.come9a98942011-08-22 17:06:16 +0000338 // so we don't want to just do a simple loop kicking each
339 // entry out. Instead change the budget and purge.
reed@google.comac10a2d2010-12-22 21:39:39 +0000340
robertphillips@google.comadacc702013-10-14 21:53:24 +0000341 size_t savedMaxBytes = fMaxBytes;
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000342 int savedMaxCount = fMaxCount;
343 fMaxBytes = (size_t) -1;
344 fMaxCount = 0;
bsalomon@google.come9a98942011-08-22 17:06:16 +0000345 this->purgeAsNeeded();
346
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000347#ifdef SK_DEBUG
twiz@google.com0ec107f2012-02-21 19:15:53 +0000348 if (!fCache.count()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000349 SkASSERT(fList.isEmpty());
twiz@google.com0ec107f2012-02-21 19:15:53 +0000350 }
351#endif
bsalomon@google.come9a98942011-08-22 17:06:16 +0000352
353 fMaxBytes = savedMaxBytes;
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000354 fMaxCount = savedMaxCount;
reed@google.comac10a2d2010-12-22 21:39:39 +0000355}
356
357///////////////////////////////////////////////////////////////////////////////
358
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000359#ifdef SK_DEBUG
bsalomon@google.coma2921122012-08-28 12:34:17 +0000360size_t GrResourceCache::countBytes(const EntryList& list) {
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000361 size_t bytes = 0;
362
bsalomon@google.coma2921122012-08-28 12:34:17 +0000363 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000364
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000365 const GrResourceCacheEntry* entry = iter.init(const_cast<EntryList&>(list),
366 EntryList::Iter::kTail_IterStart);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000367
bsalomon49f085d2014-09-05 13:34:00 -0700368 for ( ; entry; entry = iter.prev()) {
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000369 bytes += entry->resource()->gpuMemorySize();
reed@google.comac10a2d2010-12-22 21:39:39 +0000370 }
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000371 return bytes;
reed@google.comac10a2d2010-12-22 21:39:39 +0000372}
373
reed@google.comb89a6432011-02-07 13:20:30 +0000374static bool both_zero_or_nonzero(int count, size_t bytes) {
375 return (count == 0 && bytes == 0) || (count > 0 && bytes > 0);
376}
reed@google.comb89a6432011-02-07 13:20:30 +0000377
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000378void GrResourceCache::validate() const {
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000379 fList.validate();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000380 SkASSERT(both_zero_or_nonzero(fEntryCount, fEntryBytes));
bsalomonbcf0a522014-10-08 08:40:09 -0700381 SkASSERT(fEntryCount == fCache.count());
reed@google.com01804b42011-01-18 21:50:41 +0000382
bsalomon@google.coma2921122012-08-28 12:34:17 +0000383 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000384
Brian Salomon9323b8b2014-10-07 15:07:38 -0400385 // check that the shareable entries are okay
bsalomonbcf0a522014-10-08 08:40:09 -0700386 const GrResourceCacheEntry* entry = iter.init(const_cast<EntryList&>(fList),
387 EntryList::Iter::kHead_IterStart);
Brian Salomon9323b8b2014-10-07 15:07:38 -0400388
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000389 int count = 0;
bsalomon49f085d2014-09-05 13:34:00 -0700390 for ( ; entry; entry = iter.next()) {
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000391 entry->validate();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000392 SkASSERT(fCache.find(entry->key()));
reed@google.comac10a2d2010-12-22 21:39:39 +0000393 count += 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000394 }
bsalomonbcf0a522014-10-08 08:40:09 -0700395 SkASSERT(count == fEntryCount);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000396
bsalomonbcf0a522014-10-08 08:40:09 -0700397 size_t bytes = this->countBytes(fList);
398 SkASSERT(bytes == fEntryBytes);
399 SkASSERT(fList.countEntries() == fEntryCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000400}
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000401#endif // SK_DEBUG
robertphillips@google.com59552022012-08-31 13:07:37 +0000402
403#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000404
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000405void GrResourceCache::printStats() {
406 int locked = 0;
bsalomon24234fe2014-10-24 09:34:41 -0700407 int scratch = 0;
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000408
409 EntryList::Iter iter;
410
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000411 GrResourceCacheEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000412
bsalomon49f085d2014-09-05 13:34:00 -0700413 for ( ; entry; entry = iter.prev()) {
bsalomon24234fe2014-10-24 09:34:41 -0700414 if (!entry->fResource->isPurgable()) {
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000415 ++locked;
416 }
bsalomon24234fe2014-10-24 09:34:41 -0700417 if (entry->fResource->isScratch()) {
418 ++scratch;
419 }
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000420 }
421
bsalomon24234fe2014-10-24 09:34:41 -0700422 float countUtilization = (100.f * fEntryCount) / fMaxCount;
423 float byteUtilization = (100.f * fEntryBytes) / fMaxBytes;
424
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000425 SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes);
bsalomon24234fe2014-10-24 09:34:41 -0700426 SkDebugf("\t\tEntry Count: current %d (%d locked, %d scratch %.2g%% full), high %d\n",
427 fEntryCount, locked, scratch, countUtilization, fHighWaterEntryCount);
428 SkDebugf("\t\tEntry Bytes: current %d (%.2g%% full) high %d\n",
429 fEntryBytes, byteUtilization, fHighWaterEntryBytes);
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000430}
431
reed@google.comac10a2d2010-12-22 21:39:39 +0000432#endif
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000433
434///////////////////////////////////////////////////////////////////////////////