blob: a73d11703db0ddd1686916fd8634b340e12661cf [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"
bsalomon453cf402014-11-11 14:15:57 -080011#include "GrGpuResourceCacheAccess.h"
bsalomonbcf0a522014-10-08 08:40:09 -070012#include "GrTexturePriv.h"
13
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
bsalomon6d3fe022014-07-25 08:35:45 -070018void GrGpuResource::didChangeGpuMemorySize() const {
bsalomon69ed47f2014-11-12 11:13:39 -080019 fGpuMemorySize = kInvalidGpuMemorySize;
bsalomon453cf402014-11-11 14:15:57 -080020 if (this->cacheAccess().isInCache()) {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000021 fCacheEntry->didChangeResourceSize();
22 }
23}
24
25///////////////////////////////////////////////////////////////////////////////
26
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000027GrResourceKey::ResourceType GrResourceKey::GenerateResourceType() {
28 static int32_t gNextType = 0;
29
30 int32_t type = sk_atomic_inc(&gNextType);
31 if (type >= (1 << 8 * sizeof(ResourceType))) {
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +000032 SkFAIL("Too many Resource Types");
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000033 }
34
35 return static_cast<ResourceType>(type);
36}
37
38///////////////////////////////////////////////////////////////////////////////
39
bsalomon6d4488c2014-11-11 07:27:16 -080040GrResourceCacheEntry::GrResourceCacheEntry(GrResourceCache* resourceCache, GrGpuResource* resource)
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000041 : fResourceCache(resourceCache),
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000042 fResource(resource),
bsalomon6d4488c2014-11-11 07:27:16 -080043 fCachedSize(resource->gpuMemorySize()) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000044 // we assume ownership of the resource, and will unref it when we die
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000045 SkASSERT(resource);
skia.committer@gmail.com6c778162012-09-06 02:01:13 +000046 resource->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +000047}
48
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000049GrResourceCacheEntry::~GrResourceCacheEntry() {
bsalomon8b79d232014-11-10 10:19:06 -080050 // We're relying on having the cache entry to remove this from GrResourceCache2's content hash.
51 // fResource->setCacheEntry(NULL);
bsalomon@google.com50398bf2011-07-26 20:45:30 +000052 fResource->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +000053}
54
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000055#ifdef SK_DEBUG
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000056void GrResourceCacheEntry::validate() const {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000057 SkASSERT(fResourceCache);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000058 SkASSERT(fResource);
bsalomon453cf402014-11-11 14:15:57 -080059 SkASSERT(fResource->cacheAccess().getCacheEntry() == this);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000060 SkASSERT(fResource->gpuMemorySize() == fCachedSize);
bsalomon@google.com50398bf2011-07-26 20:45:30 +000061 fResource->validate();
reed@google.comac10a2d2010-12-22 21:39:39 +000062}
63#endif
64
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000065void GrResourceCacheEntry::didChangeResourceSize() {
66 size_t oldSize = fCachedSize;
67 fCachedSize = fResource->gpuMemorySize();
68 if (fCachedSize > oldSize) {
69 fResourceCache->didIncreaseResourceSize(this, fCachedSize - oldSize);
70 } else if (fCachedSize < oldSize) {
71 fResourceCache->didDecreaseResourceSize(this, oldSize - fCachedSize);
72 }
73}
74
reed@google.comac10a2d2010-12-22 21:39:39 +000075///////////////////////////////////////////////////////////////////////////////
76
bsalomonbcf0a522014-10-08 08:40:09 -070077GrResourceCache::GrResourceCache(const GrDrawTargetCaps* caps, int maxCount, size_t maxBytes)
78 : fMaxCount(maxCount)
79 , fMaxBytes(maxBytes)
80 , fCaps(SkRef(caps)) {
robertphillips@google.com59552022012-08-31 13:07:37 +000081#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +000082 fHighWaterEntryCount = 0;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +000083 fHighWaterEntryBytes = 0;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +000084#endif
85
86 fEntryCount = 0;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +000087 fEntryBytes = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +000088
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +000089 fPurging = false;
90
91 fOverbudgetCB = NULL;
92 fOverbudgetData = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000093}
94
bsalomon@google.com50398bf2011-07-26 20:45:30 +000095GrResourceCache::~GrResourceCache() {
96 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +000097
bsalomon@google.coma2921122012-08-28 12:34:17 +000098 EntryList::Iter iter;
99
100 // Unlike the removeAll, here we really remove everything, including locked resources.
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000101 while (GrResourceCacheEntry* entry = fList.head()) {
bsalomon@google.coma2921122012-08-28 12:34:17 +0000102 GrAutoResourceCacheValidate atcv(this);
103
bsalomon@google.coma2921122012-08-28 12:34:17 +0000104 // remove from our llist
robertphillips@google.com209a1142012-10-31 12:25:21 +0000105 this->internalDetach(entry);
bsalomon@google.coma2921122012-08-28 12:34:17 +0000106
107 delete entry;
108 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000109}
110
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000111void GrResourceCache::getLimits(int* maxResources, size_t* maxResourceBytes) const{
bsalomon49f085d2014-09-05 13:34:00 -0700112 if (maxResources) {
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000113 *maxResources = fMaxCount;
114 }
bsalomon49f085d2014-09-05 13:34:00 -0700115 if (maxResourceBytes) {
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000116 *maxResourceBytes = fMaxBytes;
117 }
118}
reed@google.com01804b42011-01-18 21:50:41 +0000119
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000120void GrResourceCache::setLimits(int maxResources, size_t maxResourceBytes) {
121 bool smaller = (maxResources < fMaxCount) || (maxResourceBytes < fMaxBytes);
122
123 fMaxCount = maxResources;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000124 fMaxBytes = maxResourceBytes;
reed@google.com01804b42011-01-18 21:50:41 +0000125
126 if (smaller) {
127 this->purgeAsNeeded();
128 }
129}
130
bsalomonbcf0a522014-10-08 08:40:09 -0700131void GrResourceCache::internalDetach(GrResourceCacheEntry* entry) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000132 fList.remove(entry);
bsalomonbcf0a522014-10-08 08:40:09 -0700133 fEntryCount -= 1;
134 fEntryBytes -= entry->fCachedSize;
Brian Salomon9323b8b2014-10-07 15:07:38 -0400135}
136
bsalomonbcf0a522014-10-08 08:40:09 -0700137void GrResourceCache::attachToHead(GrResourceCacheEntry* entry) {
Brian Salomon9323b8b2014-10-07 15:07:38 -0400138 fList.addToHead(entry);
139
bsalomonbcf0a522014-10-08 08:40:09 -0700140 fEntryCount += 1;
141 fEntryBytes += entry->fCachedSize;
Brian Salomon9323b8b2014-10-07 15:07:38 -0400142
143#if GR_CACHE_STATS
bsalomonbcf0a522014-10-08 08:40:09 -0700144 if (fHighWaterEntryCount < fEntryCount) {
145 fHighWaterEntryCount = fEntryCount;
Brian Salomon9323b8b2014-10-07 15:07:38 -0400146 }
bsalomonbcf0a522014-10-08 08:40:09 -0700147 if (fHighWaterEntryBytes < fEntryBytes) {
148 fHighWaterEntryBytes = fEntryBytes;
149 }
150#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000151}
152
bsalomonbcf0a522014-10-08 08:40:09 -0700153
154void GrResourceCache::makeResourceMRU(GrGpuResource* resource) {
bsalomon453cf402014-11-11 14:15:57 -0800155 GrResourceCacheEntry* entry = resource->cacheAccess().getCacheEntry();
bsalomonbcf0a522014-10-08 08:40:09 -0700156 if (entry) {
157 this->internalDetach(entry);
158 this->attachToHead(entry);
159 }
160}
161
162void GrResourceCache::notifyPurgable(const GrGpuResource* resource) {
163 // Remove scratch textures from the cache the moment they become purgeable if
164 // scratch texture reuse is turned off.
bsalomon453cf402014-11-11 14:15:57 -0800165 SkASSERT(resource->cacheAccess().getCacheEntry());
166 if (resource->cacheAccess().isScratch()) {
167 const GrResourceKey& key = resource->cacheAccess().getScratchKey();
bsalomon6d4488c2014-11-11 07:27:16 -0800168 if (key.getResourceType() == GrTexturePriv::ResourceType() &&
169 !fCaps->reuseScratchTextures() &&
170 !(static_cast<const GrSurface*>(resource)->desc().fFlags & kRenderTarget_GrSurfaceFlag)) {
bsalomon453cf402014-11-11 14:15:57 -0800171 this->deleteResource(resource->cacheAccess().getCacheEntry());
bsalomon6d4488c2014-11-11 07:27:16 -0800172 }
bsalomonbcf0a522014-10-08 08:40:09 -0700173 }
174}
175
bsalomon8b79d232014-11-10 10:19:06 -0800176bool GrResourceCache::addResource(const GrResourceKey& key, GrGpuResource* resource) {
bsalomon453cf402014-11-11 14:15:57 -0800177 if (NULL != resource->cacheAccess().getCacheEntry()) {
bsalomon8b79d232014-11-10 10:19:06 -0800178 return false;
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000179 }
bsalomon6d4488c2014-11-11 07:27:16 -0800180
181 if (key.isScratch()) {
bsalomon453cf402014-11-11 14:15:57 -0800182 SkASSERT(resource->cacheAccess().isScratch());
183 SkASSERT(key == resource->cacheAccess().getScratchKey());
bsalomon6d4488c2014-11-11 07:27:16 -0800184 } else {
bsalomon453cf402014-11-11 14:15:57 -0800185 if (!resource->cacheAccess().setContentKey(key)) {
bsalomon6d4488c2014-11-11 07:27:16 -0800186 return false;
187 }
188 }
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000189
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000190 // we don't expect to create new resources during a purge. In theory
191 // this could cause purgeAsNeeded() into an infinite loop (e.g.
192 // each resource destroyed creates and locks 2 resources and
193 // unlocks 1 thereby causing a new purge).
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000194 SkASSERT(!fPurging);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000195 GrAutoResourceCacheValidate atcv(this);
reed@google.comac10a2d2010-12-22 21:39:39 +0000196
bsalomon6d4488c2014-11-11 07:27:16 -0800197 GrResourceCacheEntry* entry = SkNEW_ARGS(GrResourceCacheEntry, (this, resource));
bsalomon453cf402014-11-11 14:15:57 -0800198 resource->cacheAccess().setCacheEntry(entry);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000199
robertphillips@google.com209a1142012-10-31 12:25:21 +0000200 this->attachToHead(entry);
bsalomonbcf0a522014-10-08 08:40:09 -0700201 this->purgeAsNeeded();
bsalomon8b79d232014-11-10 10:19:06 -0800202 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000203}
204
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000205void GrResourceCache::didIncreaseResourceSize(const GrResourceCacheEntry* entry, size_t amountInc) {
206 fEntryBytes += amountInc;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000207 this->purgeAsNeeded();
208}
209
210void GrResourceCache::didDecreaseResourceSize(const GrResourceCacheEntry* entry, size_t amountDec) {
211 fEntryBytes -= amountDec;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000212#ifdef SK_DEBUG
213 this->validate();
214#endif
215}
216
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000217/**
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000218 * Destroying a resource may potentially trigger the unlock of additional
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000219 * resources which in turn will trigger a nested purge. We block the nested
220 * purge using the fPurging variable. However, the initial purge will keep
221 * looping until either all resources in the cache are unlocked or we've met
222 * the budget. There is an assertion in createAndLock to check against a
223 * resource's destructor inserting new resources into the cache. If these
224 * new resources were unlocked before purgeAsNeeded completed it could
225 * potentially make purgeAsNeeded loop infinitely.
robertphillips@google.com41d25322013-07-18 17:12:57 +0000226 *
227 * extraCount and extraBytes are added to the current resource totals to account
228 * for incoming resources (e.g., GrContext is about to add 10MB split between
229 * 10 textures).
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000230 */
robertphillips@google.com41d25322013-07-18 17:12:57 +0000231void GrResourceCache::purgeAsNeeded(int extraCount, size_t extraBytes) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000232 if (fPurging) {
233 return;
reed@google.comac10a2d2010-12-22 21:39:39 +0000234 }
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000235
236 fPurging = true;
237
robertphillips@google.com41d25322013-07-18 17:12:57 +0000238 this->internalPurge(extraCount, extraBytes);
skia.committer@gmail.coma7991982013-07-19 07:00:57 +0000239 if (((fEntryCount+extraCount) > fMaxCount ||
robertphillips@google.com41d25322013-07-18 17:12:57 +0000240 (fEntryBytes+extraBytes) > fMaxBytes) &&
bsalomon49f085d2014-09-05 13:34:00 -0700241 fOverbudgetCB) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000242 // Despite the purge we're still over budget. See if Ganesh can
243 // release some resources and purge again.
244 if ((*fOverbudgetCB)(fOverbudgetData)) {
robertphillips@google.com41d25322013-07-18 17:12:57 +0000245 this->internalPurge(extraCount, extraBytes);
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000246 }
247 }
248
249 fPurging = false;
250}
251
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000252void GrResourceCache::purgeInvalidated() {
bsalomon6d4488c2014-11-11 07:27:16 -0800253 // TODO: Implement this in GrResourceCache2.
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000254}
255
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000256void GrResourceCache::deleteResource(GrResourceCacheEntry* entry) {
bsalomonbcf0a522014-10-08 08:40:09 -0700257 SkASSERT(entry->fResource->isPurgable());
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000258 // remove from our llist
259 this->internalDetach(entry);
260 delete entry;
261}
262
robertphillips@google.com41d25322013-07-18 17:12:57 +0000263void GrResourceCache::internalPurge(int extraCount, size_t extraBytes) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000264 SkASSERT(fPurging);
265
266 bool withinBudget = false;
267 bool changed = false;
268
269 // The purging process is repeated several times since one pass
270 // may free up other resources
271 do {
272 EntryList::Iter iter;
273
274 changed = false;
275
276 // Note: the following code relies on the fact that the
277 // doubly linked list doesn't invalidate its data/pointers
278 // outside of the specific area where a deletion occurs (e.g.,
279 // in internalDetach)
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000280 GrResourceCacheEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart);
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000281
bsalomon49f085d2014-09-05 13:34:00 -0700282 while (entry) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000283 GrAutoResourceCacheValidate atcv(this);
284
skia.committer@gmail.coma7991982013-07-19 07:00:57 +0000285 if ((fEntryCount+extraCount) <= fMaxCount &&
robertphillips@google.com41d25322013-07-18 17:12:57 +0000286 (fEntryBytes+extraBytes) <= fMaxBytes) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000287 withinBudget = true;
288 break;
289 }
290
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000291 GrResourceCacheEntry* prev = iter.prev();
bsalomonbcf0a522014-10-08 08:40:09 -0700292 if (entry->fResource->isPurgable()) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000293 changed = true;
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000294 this->deleteResource(entry);
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000295 }
296 entry = prev;
297 }
298 } while (!withinBudget && changed);
reed@google.comac10a2d2010-12-22 21:39:39 +0000299}
300
bsalomon@google.coma2921122012-08-28 12:34:17 +0000301void GrResourceCache::purgeAllUnlocked() {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000302 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +0000303
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000304 // we can have one GrCacheable holding a lock on another
bsalomon@google.come9a98942011-08-22 17:06:16 +0000305 // so we don't want to just do a simple loop kicking each
306 // entry out. Instead change the budget and purge.
reed@google.comac10a2d2010-12-22 21:39:39 +0000307
robertphillips@google.comadacc702013-10-14 21:53:24 +0000308 size_t savedMaxBytes = fMaxBytes;
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000309 int savedMaxCount = fMaxCount;
310 fMaxBytes = (size_t) -1;
311 fMaxCount = 0;
bsalomon@google.come9a98942011-08-22 17:06:16 +0000312 this->purgeAsNeeded();
313
bsalomon@google.come9a98942011-08-22 17:06:16 +0000314 fMaxBytes = savedMaxBytes;
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000315 fMaxCount = savedMaxCount;
reed@google.comac10a2d2010-12-22 21:39:39 +0000316}
317
318///////////////////////////////////////////////////////////////////////////////
319
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000320#ifdef SK_DEBUG
bsalomon@google.coma2921122012-08-28 12:34:17 +0000321size_t GrResourceCache::countBytes(const EntryList& list) {
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000322 size_t bytes = 0;
323
bsalomon@google.coma2921122012-08-28 12:34:17 +0000324 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000325
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000326 const GrResourceCacheEntry* entry = iter.init(const_cast<EntryList&>(list),
327 EntryList::Iter::kTail_IterStart);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000328
bsalomon49f085d2014-09-05 13:34:00 -0700329 for ( ; entry; entry = iter.prev()) {
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000330 bytes += entry->resource()->gpuMemorySize();
reed@google.comac10a2d2010-12-22 21:39:39 +0000331 }
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000332 return bytes;
reed@google.comac10a2d2010-12-22 21:39:39 +0000333}
334
reed@google.comb89a6432011-02-07 13:20:30 +0000335static bool both_zero_or_nonzero(int count, size_t bytes) {
336 return (count == 0 && bytes == 0) || (count > 0 && bytes > 0);
337}
reed@google.comb89a6432011-02-07 13:20:30 +0000338
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000339void GrResourceCache::validate() const {
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000340 fList.validate();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000341 SkASSERT(both_zero_or_nonzero(fEntryCount, fEntryBytes));
reed@google.com01804b42011-01-18 21:50:41 +0000342
bsalomon@google.coma2921122012-08-28 12:34:17 +0000343 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000344
Brian Salomon9323b8b2014-10-07 15:07:38 -0400345 // check that the shareable entries are okay
bsalomonbcf0a522014-10-08 08:40:09 -0700346 const GrResourceCacheEntry* entry = iter.init(const_cast<EntryList&>(fList),
347 EntryList::Iter::kHead_IterStart);
Brian Salomon9323b8b2014-10-07 15:07:38 -0400348
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000349 int count = 0;
bsalomon49f085d2014-09-05 13:34:00 -0700350 for ( ; entry; entry = iter.next()) {
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000351 entry->validate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000352 count += 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000353 }
bsalomonbcf0a522014-10-08 08:40:09 -0700354 SkASSERT(count == fEntryCount);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000355
bsalomonbcf0a522014-10-08 08:40:09 -0700356 size_t bytes = this->countBytes(fList);
357 SkASSERT(bytes == fEntryBytes);
358 SkASSERT(fList.countEntries() == fEntryCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000359}
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000360#endif // SK_DEBUG
robertphillips@google.com59552022012-08-31 13:07:37 +0000361
362#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000363
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000364void GrResourceCache::printStats() {
365 int locked = 0;
bsalomon24234fe2014-10-24 09:34:41 -0700366 int scratch = 0;
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000367
368 EntryList::Iter iter;
369
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000370 GrResourceCacheEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000371
bsalomon49f085d2014-09-05 13:34:00 -0700372 for ( ; entry; entry = iter.prev()) {
bsalomon24234fe2014-10-24 09:34:41 -0700373 if (!entry->fResource->isPurgable()) {
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000374 ++locked;
375 }
bsalomon453cf402014-11-11 14:15:57 -0800376 if (entry->fResource->cacheAccess().isScratch()) {
bsalomon24234fe2014-10-24 09:34:41 -0700377 ++scratch;
378 }
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000379 }
380
bsalomon24234fe2014-10-24 09:34:41 -0700381 float countUtilization = (100.f * fEntryCount) / fMaxCount;
382 float byteUtilization = (100.f * fEntryBytes) / fMaxBytes;
383
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000384 SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes);
bsalomon24234fe2014-10-24 09:34:41 -0700385 SkDebugf("\t\tEntry Count: current %d (%d locked, %d scratch %.2g%% full), high %d\n",
386 fEntryCount, locked, scratch, countUtilization, fHighWaterEntryCount);
387 SkDebugf("\t\tEntry Bytes: current %d (%.2g%% full) high %d\n",
388 fEntryBytes, byteUtilization, fHighWaterEntryBytes);
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000389}
390
reed@google.comac10a2d2010-12-22 21:39:39 +0000391#endif
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000392
393///////////////////////////////////////////////////////////////////////////////