blob: 7083cf374b5c99ae2a7785a42dff38096bd66c83 [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 {
bsalomon453cf402014-11-11 14:15:57 -080019 if (this->cacheAccess().isInCache()) {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000020 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
bsalomon6d4488c2014-11-11 07:27:16 -080039GrResourceCacheEntry::GrResourceCacheEntry(GrResourceCache* resourceCache, GrGpuResource* resource)
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000040 : fResourceCache(resourceCache),
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000041 fResource(resource),
bsalomon6d4488c2014-11-11 07:27:16 -080042 fCachedSize(resource->gpuMemorySize()) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000043 // we assume ownership of the resource, and will unref it when we die
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000044 SkASSERT(resource);
skia.committer@gmail.com6c778162012-09-06 02:01:13 +000045 resource->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +000046}
47
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000048GrResourceCacheEntry::~GrResourceCacheEntry() {
bsalomon8b79d232014-11-10 10:19:06 -080049 // We're relying on having the cache entry to remove this from GrResourceCache2's content hash.
50 // fResource->setCacheEntry(NULL);
bsalomon@google.com50398bf2011-07-26 20:45:30 +000051 fResource->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +000052}
53
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000054#ifdef SK_DEBUG
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000055void GrResourceCacheEntry::validate() const {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000056 SkASSERT(fResourceCache);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000057 SkASSERT(fResource);
bsalomon453cf402014-11-11 14:15:57 -080058 SkASSERT(fResource->cacheAccess().getCacheEntry() == this);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000059 SkASSERT(fResource->gpuMemorySize() == fCachedSize);
bsalomon@google.com50398bf2011-07-26 20:45:30 +000060 fResource->validate();
reed@google.comac10a2d2010-12-22 21:39:39 +000061}
62#endif
63
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000064void GrResourceCacheEntry::didChangeResourceSize() {
65 size_t oldSize = fCachedSize;
66 fCachedSize = fResource->gpuMemorySize();
67 if (fCachedSize > oldSize) {
68 fResourceCache->didIncreaseResourceSize(this, fCachedSize - oldSize);
69 } else if (fCachedSize < oldSize) {
70 fResourceCache->didDecreaseResourceSize(this, oldSize - fCachedSize);
71 }
72}
73
reed@google.comac10a2d2010-12-22 21:39:39 +000074///////////////////////////////////////////////////////////////////////////////
75
bsalomonbcf0a522014-10-08 08:40:09 -070076GrResourceCache::GrResourceCache(const GrDrawTargetCaps* caps, int maxCount, size_t maxBytes)
77 : fMaxCount(maxCount)
78 , fMaxBytes(maxBytes)
79 , fCaps(SkRef(caps)) {
robertphillips@google.com59552022012-08-31 13:07:37 +000080#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +000081 fHighWaterEntryCount = 0;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +000082 fHighWaterEntryBytes = 0;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +000083#endif
84
85 fEntryCount = 0;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +000086 fEntryBytes = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +000087
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +000088 fPurging = false;
89
90 fOverbudgetCB = NULL;
91 fOverbudgetData = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000092}
93
bsalomon@google.com50398bf2011-07-26 20:45:30 +000094GrResourceCache::~GrResourceCache() {
95 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +000096
bsalomon@google.coma2921122012-08-28 12:34:17 +000097 EntryList::Iter iter;
98
99 // Unlike the removeAll, here we really remove everything, including locked resources.
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000100 while (GrResourceCacheEntry* entry = fList.head()) {
bsalomon@google.coma2921122012-08-28 12:34:17 +0000101 GrAutoResourceCacheValidate atcv(this);
102
bsalomon@google.coma2921122012-08-28 12:34:17 +0000103 // remove from our llist
robertphillips@google.com209a1142012-10-31 12:25:21 +0000104 this->internalDetach(entry);
bsalomon@google.coma2921122012-08-28 12:34:17 +0000105
106 delete entry;
107 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000108}
109
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000110void GrResourceCache::getLimits(int* maxResources, size_t* maxResourceBytes) const{
bsalomon49f085d2014-09-05 13:34:00 -0700111 if (maxResources) {
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000112 *maxResources = fMaxCount;
113 }
bsalomon49f085d2014-09-05 13:34:00 -0700114 if (maxResourceBytes) {
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000115 *maxResourceBytes = fMaxBytes;
116 }
117}
reed@google.com01804b42011-01-18 21:50:41 +0000118
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000119void GrResourceCache::setLimits(int maxResources, size_t maxResourceBytes) {
120 bool smaller = (maxResources < fMaxCount) || (maxResourceBytes < fMaxBytes);
121
122 fMaxCount = maxResources;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000123 fMaxBytes = maxResourceBytes;
reed@google.com01804b42011-01-18 21:50:41 +0000124
125 if (smaller) {
126 this->purgeAsNeeded();
127 }
128}
129
bsalomonbcf0a522014-10-08 08:40:09 -0700130void GrResourceCache::internalDetach(GrResourceCacheEntry* entry) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000131 fList.remove(entry);
bsalomonbcf0a522014-10-08 08:40:09 -0700132 fEntryCount -= 1;
133 fEntryBytes -= entry->fCachedSize;
Brian Salomon9323b8b2014-10-07 15:07:38 -0400134}
135
bsalomonbcf0a522014-10-08 08:40:09 -0700136void GrResourceCache::attachToHead(GrResourceCacheEntry* entry) {
Brian Salomon9323b8b2014-10-07 15:07:38 -0400137 fList.addToHead(entry);
138
bsalomonbcf0a522014-10-08 08:40:09 -0700139 fEntryCount += 1;
140 fEntryBytes += entry->fCachedSize;
Brian Salomon9323b8b2014-10-07 15:07:38 -0400141
142#if GR_CACHE_STATS
bsalomonbcf0a522014-10-08 08:40:09 -0700143 if (fHighWaterEntryCount < fEntryCount) {
144 fHighWaterEntryCount = fEntryCount;
Brian Salomon9323b8b2014-10-07 15:07:38 -0400145 }
bsalomonbcf0a522014-10-08 08:40:09 -0700146 if (fHighWaterEntryBytes < fEntryBytes) {
147 fHighWaterEntryBytes = fEntryBytes;
148 }
149#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000150}
151
bsalomonbcf0a522014-10-08 08:40:09 -0700152
153void GrResourceCache::makeResourceMRU(GrGpuResource* resource) {
bsalomon453cf402014-11-11 14:15:57 -0800154 GrResourceCacheEntry* entry = resource->cacheAccess().getCacheEntry();
bsalomonbcf0a522014-10-08 08:40:09 -0700155 if (entry) {
156 this->internalDetach(entry);
157 this->attachToHead(entry);
158 }
159}
160
161void GrResourceCache::notifyPurgable(const GrGpuResource* resource) {
162 // Remove scratch textures from the cache the moment they become purgeable if
163 // scratch texture reuse is turned off.
bsalomon453cf402014-11-11 14:15:57 -0800164 SkASSERT(resource->cacheAccess().getCacheEntry());
165 if (resource->cacheAccess().isScratch()) {
166 const GrResourceKey& key = resource->cacheAccess().getScratchKey();
bsalomon6d4488c2014-11-11 07:27:16 -0800167 if (key.getResourceType() == GrTexturePriv::ResourceType() &&
168 !fCaps->reuseScratchTextures() &&
169 !(static_cast<const GrSurface*>(resource)->desc().fFlags & kRenderTarget_GrSurfaceFlag)) {
bsalomon453cf402014-11-11 14:15:57 -0800170 this->deleteResource(resource->cacheAccess().getCacheEntry());
bsalomon6d4488c2014-11-11 07:27:16 -0800171 }
bsalomonbcf0a522014-10-08 08:40:09 -0700172 }
173}
174
bsalomon8b79d232014-11-10 10:19:06 -0800175bool GrResourceCache::addResource(const GrResourceKey& key, GrGpuResource* resource) {
bsalomon453cf402014-11-11 14:15:57 -0800176 if (NULL != resource->cacheAccess().getCacheEntry()) {
bsalomon8b79d232014-11-10 10:19:06 -0800177 return false;
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000178 }
bsalomon6d4488c2014-11-11 07:27:16 -0800179
180 if (key.isScratch()) {
bsalomon453cf402014-11-11 14:15:57 -0800181 SkASSERT(resource->cacheAccess().isScratch());
182 SkASSERT(key == resource->cacheAccess().getScratchKey());
bsalomon6d4488c2014-11-11 07:27:16 -0800183 } else {
bsalomon453cf402014-11-11 14:15:57 -0800184 if (!resource->cacheAccess().setContentKey(key)) {
bsalomon6d4488c2014-11-11 07:27:16 -0800185 return false;
186 }
187 }
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000188
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000189 // we don't expect to create new resources during a purge. In theory
190 // this could cause purgeAsNeeded() into an infinite loop (e.g.
191 // each resource destroyed creates and locks 2 resources and
192 // unlocks 1 thereby causing a new purge).
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000193 SkASSERT(!fPurging);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000194 GrAutoResourceCacheValidate atcv(this);
reed@google.comac10a2d2010-12-22 21:39:39 +0000195
bsalomon6d4488c2014-11-11 07:27:16 -0800196 GrResourceCacheEntry* entry = SkNEW_ARGS(GrResourceCacheEntry, (this, resource));
bsalomon453cf402014-11-11 14:15:57 -0800197 resource->cacheAccess().setCacheEntry(entry);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000198
robertphillips@google.com209a1142012-10-31 12:25:21 +0000199 this->attachToHead(entry);
bsalomonbcf0a522014-10-08 08:40:09 -0700200 this->purgeAsNeeded();
bsalomon8b79d232014-11-10 10:19:06 -0800201 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000202}
203
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000204void GrResourceCache::didIncreaseResourceSize(const GrResourceCacheEntry* entry, size_t amountInc) {
205 fEntryBytes += amountInc;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000206 this->purgeAsNeeded();
207}
208
209void GrResourceCache::didDecreaseResourceSize(const GrResourceCacheEntry* entry, size_t amountDec) {
210 fEntryBytes -= amountDec;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000211#ifdef SK_DEBUG
212 this->validate();
213#endif
214}
215
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000216/**
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000217 * Destroying a resource may potentially trigger the unlock of additional
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000218 * resources which in turn will trigger a nested purge. We block the nested
219 * purge using the fPurging variable. However, the initial purge will keep
220 * looping until either all resources in the cache are unlocked or we've met
221 * the budget. There is an assertion in createAndLock to check against a
222 * resource's destructor inserting new resources into the cache. If these
223 * new resources were unlocked before purgeAsNeeded completed it could
224 * potentially make purgeAsNeeded loop infinitely.
robertphillips@google.com41d25322013-07-18 17:12:57 +0000225 *
226 * extraCount and extraBytes are added to the current resource totals to account
227 * for incoming resources (e.g., GrContext is about to add 10MB split between
228 * 10 textures).
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000229 */
robertphillips@google.com41d25322013-07-18 17:12:57 +0000230void GrResourceCache::purgeAsNeeded(int extraCount, size_t extraBytes) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000231 if (fPurging) {
232 return;
reed@google.comac10a2d2010-12-22 21:39:39 +0000233 }
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000234
235 fPurging = true;
236
robertphillips@google.com41d25322013-07-18 17:12:57 +0000237 this->internalPurge(extraCount, extraBytes);
skia.committer@gmail.coma7991982013-07-19 07:00:57 +0000238 if (((fEntryCount+extraCount) > fMaxCount ||
robertphillips@google.com41d25322013-07-18 17:12:57 +0000239 (fEntryBytes+extraBytes) > fMaxBytes) &&
bsalomon49f085d2014-09-05 13:34:00 -0700240 fOverbudgetCB) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000241 // Despite the purge we're still over budget. See if Ganesh can
242 // release some resources and purge again.
243 if ((*fOverbudgetCB)(fOverbudgetData)) {
robertphillips@google.com41d25322013-07-18 17:12:57 +0000244 this->internalPurge(extraCount, extraBytes);
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000245 }
246 }
247
248 fPurging = false;
249}
250
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000251void GrResourceCache::purgeInvalidated() {
bsalomon6d4488c2014-11-11 07:27:16 -0800252 // TODO: Implement this in GrResourceCache2.
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000253}
254
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000255void GrResourceCache::deleteResource(GrResourceCacheEntry* entry) {
bsalomonbcf0a522014-10-08 08:40:09 -0700256 SkASSERT(entry->fResource->isPurgable());
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000257 // remove from our llist
258 this->internalDetach(entry);
259 delete entry;
260}
261
robertphillips@google.com41d25322013-07-18 17:12:57 +0000262void GrResourceCache::internalPurge(int extraCount, size_t extraBytes) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000263 SkASSERT(fPurging);
264
265 bool withinBudget = false;
266 bool changed = false;
267
268 // The purging process is repeated several times since one pass
269 // may free up other resources
270 do {
271 EntryList::Iter iter;
272
273 changed = false;
274
275 // Note: the following code relies on the fact that the
276 // doubly linked list doesn't invalidate its data/pointers
277 // outside of the specific area where a deletion occurs (e.g.,
278 // in internalDetach)
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000279 GrResourceCacheEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart);
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000280
bsalomon49f085d2014-09-05 13:34:00 -0700281 while (entry) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000282 GrAutoResourceCacheValidate atcv(this);
283
skia.committer@gmail.coma7991982013-07-19 07:00:57 +0000284 if ((fEntryCount+extraCount) <= fMaxCount &&
robertphillips@google.com41d25322013-07-18 17:12:57 +0000285 (fEntryBytes+extraBytes) <= fMaxBytes) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000286 withinBudget = true;
287 break;
288 }
289
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000290 GrResourceCacheEntry* prev = iter.prev();
bsalomonbcf0a522014-10-08 08:40:09 -0700291 if (entry->fResource->isPurgable()) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000292 changed = true;
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000293 this->deleteResource(entry);
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000294 }
295 entry = prev;
296 }
297 } while (!withinBudget && changed);
reed@google.comac10a2d2010-12-22 21:39:39 +0000298}
299
bsalomon@google.coma2921122012-08-28 12:34:17 +0000300void GrResourceCache::purgeAllUnlocked() {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000301 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +0000302
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000303 // we can have one GrCacheable holding a lock on another
bsalomon@google.come9a98942011-08-22 17:06:16 +0000304 // so we don't want to just do a simple loop kicking each
305 // entry out. Instead change the budget and purge.
reed@google.comac10a2d2010-12-22 21:39:39 +0000306
robertphillips@google.comadacc702013-10-14 21:53:24 +0000307 size_t savedMaxBytes = fMaxBytes;
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000308 int savedMaxCount = fMaxCount;
309 fMaxBytes = (size_t) -1;
310 fMaxCount = 0;
bsalomon@google.come9a98942011-08-22 17:06:16 +0000311 this->purgeAsNeeded();
312
bsalomon@google.come9a98942011-08-22 17:06:16 +0000313 fMaxBytes = savedMaxBytes;
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000314 fMaxCount = savedMaxCount;
reed@google.comac10a2d2010-12-22 21:39:39 +0000315}
316
317///////////////////////////////////////////////////////////////////////////////
318
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000319#ifdef SK_DEBUG
bsalomon@google.coma2921122012-08-28 12:34:17 +0000320size_t GrResourceCache::countBytes(const EntryList& list) {
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000321 size_t bytes = 0;
322
bsalomon@google.coma2921122012-08-28 12:34:17 +0000323 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000324
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000325 const GrResourceCacheEntry* entry = iter.init(const_cast<EntryList&>(list),
326 EntryList::Iter::kTail_IterStart);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000327
bsalomon49f085d2014-09-05 13:34:00 -0700328 for ( ; entry; entry = iter.prev()) {
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000329 bytes += entry->resource()->gpuMemorySize();
reed@google.comac10a2d2010-12-22 21:39:39 +0000330 }
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000331 return bytes;
reed@google.comac10a2d2010-12-22 21:39:39 +0000332}
333
reed@google.comb89a6432011-02-07 13:20:30 +0000334static bool both_zero_or_nonzero(int count, size_t bytes) {
335 return (count == 0 && bytes == 0) || (count > 0 && bytes > 0);
336}
reed@google.comb89a6432011-02-07 13:20:30 +0000337
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000338void GrResourceCache::validate() const {
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000339 fList.validate();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000340 SkASSERT(both_zero_or_nonzero(fEntryCount, fEntryBytes));
reed@google.com01804b42011-01-18 21:50:41 +0000341
bsalomon@google.coma2921122012-08-28 12:34:17 +0000342 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000343
Brian Salomon9323b8b2014-10-07 15:07:38 -0400344 // check that the shareable entries are okay
bsalomonbcf0a522014-10-08 08:40:09 -0700345 const GrResourceCacheEntry* entry = iter.init(const_cast<EntryList&>(fList),
346 EntryList::Iter::kHead_IterStart);
Brian Salomon9323b8b2014-10-07 15:07:38 -0400347
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000348 int count = 0;
bsalomon49f085d2014-09-05 13:34:00 -0700349 for ( ; entry; entry = iter.next()) {
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000350 entry->validate();
reed@google.comac10a2d2010-12-22 21:39:39 +0000351 count += 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000352 }
bsalomonbcf0a522014-10-08 08:40:09 -0700353 SkASSERT(count == fEntryCount);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000354
bsalomonbcf0a522014-10-08 08:40:09 -0700355 size_t bytes = this->countBytes(fList);
356 SkASSERT(bytes == fEntryBytes);
357 SkASSERT(fList.countEntries() == fEntryCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000358}
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000359#endif // SK_DEBUG
robertphillips@google.com59552022012-08-31 13:07:37 +0000360
361#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000362
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000363void GrResourceCache::printStats() {
364 int locked = 0;
bsalomon24234fe2014-10-24 09:34:41 -0700365 int scratch = 0;
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000366
367 EntryList::Iter iter;
368
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000369 GrResourceCacheEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000370
bsalomon49f085d2014-09-05 13:34:00 -0700371 for ( ; entry; entry = iter.prev()) {
bsalomon24234fe2014-10-24 09:34:41 -0700372 if (!entry->fResource->isPurgable()) {
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000373 ++locked;
374 }
bsalomon453cf402014-11-11 14:15:57 -0800375 if (entry->fResource->cacheAccess().isScratch()) {
bsalomon24234fe2014-10-24 09:34:41 -0700376 ++scratch;
377 }
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000378 }
379
bsalomon24234fe2014-10-24 09:34:41 -0700380 float countUtilization = (100.f * fEntryCount) / fMaxCount;
381 float byteUtilization = (100.f * fEntryBytes) / fMaxBytes;
382
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000383 SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes);
bsalomon24234fe2014-10-24 09:34:41 -0700384 SkDebugf("\t\tEntry Count: current %d (%d locked, %d scratch %.2g%% full), high %d\n",
385 fEntryCount, locked, scratch, countUtilization, fHighWaterEntryCount);
386 SkDebugf("\t\tEntry Bytes: current %d (%.2g%% full) high %d\n",
387 fEntryBytes, byteUtilization, fHighWaterEntryBytes);
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000388}
389
reed@google.comac10a2d2010-12-22 21:39:39 +0000390#endif
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000391
392///////////////////////////////////////////////////////////////////////////////