blob: c683b5bcfcc0e65cf53bf459909ce025cb9cbd68 [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"
bsalomon6d3fe022014-07-25 08:35:45 -070012#include "GrGpuResource.h"
bsalomonbcf0a522014-10-08 08:40:09 -070013#include "GrTexturePriv.h"
14
reed@google.comac10a2d2010-12-22 21:39:39 +000015
commit-bot@chromium.orgc6658042014-01-15 23:09:01 +000016DECLARE_SKMESSAGEBUS_MESSAGE(GrResourceInvalidatedMessage);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000017
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000018///////////////////////////////////////////////////////////////////////////////
19
bsalomon6d3fe022014-07-25 08:35:45 -070020void GrGpuResource::didChangeGpuMemorySize() const {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000021 if (this->isInCache()) {
22 fCacheEntry->didChangeResourceSize();
23 }
24}
25
26///////////////////////////////////////////////////////////////////////////////
27
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000028GrResourceKey::ResourceType GrResourceKey::GenerateResourceType() {
29 static int32_t gNextType = 0;
30
31 int32_t type = sk_atomic_inc(&gNextType);
32 if (type >= (1 << 8 * sizeof(ResourceType))) {
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +000033 SkFAIL("Too many Resource Types");
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000034 }
35
36 return static_cast<ResourceType>(type);
37}
38
39///////////////////////////////////////////////////////////////////////////////
40
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000041GrResourceCacheEntry::GrResourceCacheEntry(GrResourceCache* resourceCache,
42 const GrResourceKey& key,
bsalomon6d3fe022014-07-25 08:35:45 -070043 GrGpuResource* resource)
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000044 : fResourceCache(resourceCache),
45 fKey(key),
46 fResource(resource),
47 fCachedSize(resource->gpuMemorySize()),
48 fIsExclusive(false) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000049 // we assume ownership of the resource, and will unref it when we die
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000050 SkASSERT(resource);
skia.committer@gmail.com6c778162012-09-06 02:01:13 +000051 resource->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +000052}
53
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000054GrResourceCacheEntry::~GrResourceCacheEntry() {
robertphillips@google.com521eaf82012-08-22 11:03:19 +000055 fResource->setCacheEntry(NULL);
bsalomon@google.com50398bf2011-07-26 20:45:30 +000056 fResource->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +000057}
58
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000059#ifdef SK_DEBUG
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000060void GrResourceCacheEntry::validate() const {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000061 SkASSERT(fResourceCache);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000062 SkASSERT(fResource);
63 SkASSERT(fResource->getCacheEntry() == this);
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000064 SkASSERT(fResource->gpuMemorySize() == fCachedSize);
bsalomon@google.com50398bf2011-07-26 20:45:30 +000065 fResource->validate();
reed@google.comac10a2d2010-12-22 21:39:39 +000066}
67#endif
68
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000069void GrResourceCacheEntry::didChangeResourceSize() {
70 size_t oldSize = fCachedSize;
71 fCachedSize = fResource->gpuMemorySize();
72 if (fCachedSize > oldSize) {
73 fResourceCache->didIncreaseResourceSize(this, fCachedSize - oldSize);
74 } else if (fCachedSize < oldSize) {
75 fResourceCache->didDecreaseResourceSize(this, oldSize - fCachedSize);
76 }
77}
78
reed@google.comac10a2d2010-12-22 21:39:39 +000079///////////////////////////////////////////////////////////////////////////////
80
bsalomonbcf0a522014-10-08 08:40:09 -070081GrResourceCache::GrResourceCache(const GrDrawTargetCaps* caps, int maxCount, size_t maxBytes)
82 : fMaxCount(maxCount)
83 , fMaxBytes(maxBytes)
84 , fCaps(SkRef(caps)) {
robertphillips@google.com59552022012-08-31 13:07:37 +000085#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +000086 fHighWaterEntryCount = 0;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +000087 fHighWaterEntryBytes = 0;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +000088#endif
89
90 fEntryCount = 0;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +000091 fEntryBytes = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +000092
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +000093 fPurging = false;
94
95 fOverbudgetCB = NULL;
96 fOverbudgetData = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000097}
98
bsalomon@google.com50398bf2011-07-26 20:45:30 +000099GrResourceCache::~GrResourceCache() {
100 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +0000101
bsalomon@google.coma2921122012-08-28 12:34:17 +0000102 EntryList::Iter iter;
103
104 // Unlike the removeAll, here we really remove everything, including locked resources.
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000105 while (GrResourceCacheEntry* entry = fList.head()) {
bsalomon@google.coma2921122012-08-28 12:34:17 +0000106 GrAutoResourceCacheValidate atcv(this);
107
108 // remove from our cache
109 fCache.remove(entry->fKey, entry);
110
111 // remove from our llist
robertphillips@google.com209a1142012-10-31 12:25:21 +0000112 this->internalDetach(entry);
bsalomon@google.coma2921122012-08-28 12:34:17 +0000113
114 delete entry;
115 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000116}
117
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000118void GrResourceCache::getLimits(int* maxResources, size_t* maxResourceBytes) const{
bsalomon49f085d2014-09-05 13:34:00 -0700119 if (maxResources) {
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000120 *maxResources = fMaxCount;
121 }
bsalomon49f085d2014-09-05 13:34:00 -0700122 if (maxResourceBytes) {
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000123 *maxResourceBytes = fMaxBytes;
124 }
125}
reed@google.com01804b42011-01-18 21:50:41 +0000126
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000127void GrResourceCache::setLimits(int maxResources, size_t maxResourceBytes) {
128 bool smaller = (maxResources < fMaxCount) || (maxResourceBytes < fMaxBytes);
129
130 fMaxCount = maxResources;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000131 fMaxBytes = maxResourceBytes;
reed@google.com01804b42011-01-18 21:50:41 +0000132
133 if (smaller) {
134 this->purgeAsNeeded();
135 }
136}
137
bsalomonbcf0a522014-10-08 08:40:09 -0700138void GrResourceCache::internalDetach(GrResourceCacheEntry* entry) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000139 fList.remove(entry);
bsalomonbcf0a522014-10-08 08:40:09 -0700140 fEntryCount -= 1;
141 fEntryBytes -= entry->fCachedSize;
Brian Salomon9323b8b2014-10-07 15:07:38 -0400142}
143
bsalomonbcf0a522014-10-08 08:40:09 -0700144void GrResourceCache::attachToHead(GrResourceCacheEntry* entry) {
Brian Salomon9323b8b2014-10-07 15:07:38 -0400145 fList.addToHead(entry);
146
bsalomonbcf0a522014-10-08 08:40:09 -0700147 fEntryCount += 1;
148 fEntryBytes += entry->fCachedSize;
Brian Salomon9323b8b2014-10-07 15:07:38 -0400149
150#if GR_CACHE_STATS
bsalomonbcf0a522014-10-08 08:40:09 -0700151 if (fHighWaterEntryCount < fEntryCount) {
152 fHighWaterEntryCount = fEntryCount;
Brian Salomon9323b8b2014-10-07 15:07:38 -0400153 }
bsalomonbcf0a522014-10-08 08:40:09 -0700154 if (fHighWaterEntryBytes < fEntryBytes) {
155 fHighWaterEntryBytes = fEntryBytes;
156 }
157#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000158}
159
robertphillips@google.com209a1142012-10-31 12:25:21 +0000160// This functor just searches for an entry with only a single ref (from
161// the texture cache itself). Presumably in this situation no one else
162// is relying on the texture.
163class GrTFindUnreffedFunctor {
164public:
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000165 bool operator()(const GrResourceCacheEntry* entry) const {
bsalomonbcf0a522014-10-08 08:40:09 -0700166 return entry->resource()->isPurgable();
robertphillips@google.com209a1142012-10-31 12:25:21 +0000167 }
168};
169
bsalomonbcf0a522014-10-08 08:40:09 -0700170
171void GrResourceCache::makeResourceMRU(GrGpuResource* resource) {
172 GrResourceCacheEntry* entry = resource->getCacheEntry();
173 if (entry) {
174 this->internalDetach(entry);
175 this->attachToHead(entry);
176 }
177}
178
179void GrResourceCache::notifyPurgable(const GrGpuResource* resource) {
180 // Remove scratch textures from the cache the moment they become purgeable if
181 // scratch texture reuse is turned off.
182 SkASSERT(resource->getCacheEntry());
183 if (resource->getCacheEntry()->key().getResourceType() == GrTexturePriv::ResourceType() &&
184 resource->fIsScratch &&
185 !fCaps->reuseScratchTextures() &&
186 !(static_cast<const GrTexture*>(resource)->desc().fFlags &
187 kRenderTarget_GrTextureFlagBit)) {
188 this->deleteResource(resource->getCacheEntry());
189 }
190}
191
192GrGpuResource* GrResourceCache::find(const GrResourceKey& key) {
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000193 GrAutoResourceCacheValidate atcv(this);
194
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000195 GrResourceCacheEntry* entry = NULL;
robertphillips@google.com209a1142012-10-31 12:25:21 +0000196
bsalomonbcf0a522014-10-08 08:40:09 -0700197 entry = fCache.find(key);
robertphillips@google.com209a1142012-10-31 12:25:21 +0000198
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000199 if (NULL == entry) {
200 return NULL;
201 }
202
bsalomonbcf0a522014-10-08 08:40:09 -0700203 // Make this resource MRU
204 this->internalDetach(entry);
205 this->attachToHead(entry);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000206
bsalomonbcf0a522014-10-08 08:40:09 -0700207 // GrResourceCache2 is responsible for scratch resources.
208 SkASSERT(GrGpuResource::kNo_IsScratch == entry->resource()->fIsScratch);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000209 return entry->fResource;
reed@google.comac10a2d2010-12-22 21:39:39 +0000210}
211
bsalomonbcf0a522014-10-08 08:40:09 -0700212void GrResourceCache::addResource(const GrResourceKey& key, GrGpuResource* resource) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000213 SkASSERT(NULL == resource->getCacheEntry());
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000214 // we don't expect to create new resources during a purge. In theory
215 // this could cause purgeAsNeeded() into an infinite loop (e.g.
216 // each resource destroyed creates and locks 2 resources and
217 // unlocks 1 thereby causing a new purge).
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000218 SkASSERT(!fPurging);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000219 GrAutoResourceCacheValidate atcv(this);
reed@google.comac10a2d2010-12-22 21:39:39 +0000220
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000221 GrResourceCacheEntry* entry = SkNEW_ARGS(GrResourceCacheEntry, (this, key, resource));
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000222 resource->setCacheEntry(entry);
223
robertphillips@google.com209a1142012-10-31 12:25:21 +0000224 this->attachToHead(entry);
reed@google.comac10a2d2010-12-22 21:39:39 +0000225 fCache.insert(key, entry);
Brian Salomon9323b8b2014-10-07 15:07:38 -0400226
bsalomonbcf0a522014-10-08 08:40:09 -0700227 this->purgeAsNeeded();
reed@google.comac10a2d2010-12-22 21:39:39 +0000228}
229
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000230void GrResourceCache::didIncreaseResourceSize(const GrResourceCacheEntry* entry, size_t amountInc) {
231 fEntryBytes += amountInc;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000232 this->purgeAsNeeded();
233}
234
235void GrResourceCache::didDecreaseResourceSize(const GrResourceCacheEntry* entry, size_t amountDec) {
236 fEntryBytes -= amountDec;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +0000237#ifdef SK_DEBUG
238 this->validate();
239#endif
240}
241
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000242/**
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000243 * Destroying a resource may potentially trigger the unlock of additional
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000244 * resources which in turn will trigger a nested purge. We block the nested
245 * purge using the fPurging variable. However, the initial purge will keep
246 * looping until either all resources in the cache are unlocked or we've met
247 * the budget. There is an assertion in createAndLock to check against a
248 * resource's destructor inserting new resources into the cache. If these
249 * new resources were unlocked before purgeAsNeeded completed it could
250 * potentially make purgeAsNeeded loop infinitely.
robertphillips@google.com41d25322013-07-18 17:12:57 +0000251 *
252 * extraCount and extraBytes are added to the current resource totals to account
253 * for incoming resources (e.g., GrContext is about to add 10MB split between
254 * 10 textures).
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000255 */
robertphillips@google.com41d25322013-07-18 17:12:57 +0000256void GrResourceCache::purgeAsNeeded(int extraCount, size_t extraBytes) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000257 if (fPurging) {
258 return;
reed@google.comac10a2d2010-12-22 21:39:39 +0000259 }
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000260
261 fPurging = true;
262
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000263 this->purgeInvalidated();
264
robertphillips@google.com41d25322013-07-18 17:12:57 +0000265 this->internalPurge(extraCount, extraBytes);
skia.committer@gmail.coma7991982013-07-19 07:00:57 +0000266 if (((fEntryCount+extraCount) > fMaxCount ||
robertphillips@google.com41d25322013-07-18 17:12:57 +0000267 (fEntryBytes+extraBytes) > fMaxBytes) &&
bsalomon49f085d2014-09-05 13:34:00 -0700268 fOverbudgetCB) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000269 // Despite the purge we're still over budget. See if Ganesh can
270 // release some resources and purge again.
271 if ((*fOverbudgetCB)(fOverbudgetData)) {
robertphillips@google.com41d25322013-07-18 17:12:57 +0000272 this->internalPurge(extraCount, extraBytes);
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000273 }
274 }
275
276 fPurging = false;
277}
278
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000279void GrResourceCache::purgeInvalidated() {
280 SkTDArray<GrResourceInvalidatedMessage> invalidated;
281 fInvalidationInbox.poll(&invalidated);
282
283 for (int i = 0; i < invalidated.count(); i++) {
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000284 while (GrResourceCacheEntry* entry = fCache.find(invalidated[i].key, GrTFindUnreffedFunctor())) {
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000285 this->deleteResource(entry);
286 }
287 }
288}
289
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000290void GrResourceCache::deleteResource(GrResourceCacheEntry* entry) {
bsalomonbcf0a522014-10-08 08:40:09 -0700291 SkASSERT(entry->fResource->isPurgable());
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000292
293 // remove from our cache
294 fCache.remove(entry->key(), entry);
295
296 // remove from our llist
297 this->internalDetach(entry);
298 delete entry;
299}
300
robertphillips@google.com41d25322013-07-18 17:12:57 +0000301void GrResourceCache::internalPurge(int extraCount, size_t extraBytes) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000302 SkASSERT(fPurging);
303
304 bool withinBudget = false;
305 bool changed = false;
306
307 // The purging process is repeated several times since one pass
308 // may free up other resources
309 do {
310 EntryList::Iter iter;
311
312 changed = false;
313
314 // Note: the following code relies on the fact that the
315 // doubly linked list doesn't invalidate its data/pointers
316 // outside of the specific area where a deletion occurs (e.g.,
317 // in internalDetach)
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000318 GrResourceCacheEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart);
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000319
bsalomon49f085d2014-09-05 13:34:00 -0700320 while (entry) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000321 GrAutoResourceCacheValidate atcv(this);
322
skia.committer@gmail.coma7991982013-07-19 07:00:57 +0000323 if ((fEntryCount+extraCount) <= fMaxCount &&
robertphillips@google.com41d25322013-07-18 17:12:57 +0000324 (fEntryBytes+extraBytes) <= fMaxBytes) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000325 withinBudget = true;
326 break;
327 }
328
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000329 GrResourceCacheEntry* prev = iter.prev();
bsalomonbcf0a522014-10-08 08:40:09 -0700330 if (entry->fResource->isPurgable()) {
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000331 changed = true;
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000332 this->deleteResource(entry);
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000333 }
334 entry = prev;
335 }
336 } while (!withinBudget && changed);
reed@google.comac10a2d2010-12-22 21:39:39 +0000337}
338
bsalomon@google.coma2921122012-08-28 12:34:17 +0000339void GrResourceCache::purgeAllUnlocked() {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000340 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +0000341
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000342 // we can have one GrCacheable holding a lock on another
bsalomon@google.come9a98942011-08-22 17:06:16 +0000343 // so we don't want to just do a simple loop kicking each
344 // entry out. Instead change the budget and purge.
reed@google.comac10a2d2010-12-22 21:39:39 +0000345
robertphillips@google.comadacc702013-10-14 21:53:24 +0000346 size_t savedMaxBytes = fMaxBytes;
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000347 int savedMaxCount = fMaxCount;
348 fMaxBytes = (size_t) -1;
349 fMaxCount = 0;
bsalomon@google.come9a98942011-08-22 17:06:16 +0000350 this->purgeAsNeeded();
351
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000352#ifdef SK_DEBUG
twiz@google.com0ec107f2012-02-21 19:15:53 +0000353 if (!fCache.count()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000354 SkASSERT(fList.isEmpty());
twiz@google.com0ec107f2012-02-21 19:15:53 +0000355 }
356#endif
bsalomon@google.come9a98942011-08-22 17:06:16 +0000357
358 fMaxBytes = savedMaxBytes;
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000359 fMaxCount = savedMaxCount;
reed@google.comac10a2d2010-12-22 21:39:39 +0000360}
361
362///////////////////////////////////////////////////////////////////////////////
363
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000364#ifdef SK_DEBUG
bsalomon@google.coma2921122012-08-28 12:34:17 +0000365size_t GrResourceCache::countBytes(const EntryList& list) {
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000366 size_t bytes = 0;
367
bsalomon@google.coma2921122012-08-28 12:34:17 +0000368 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000369
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000370 const GrResourceCacheEntry* entry = iter.init(const_cast<EntryList&>(list),
371 EntryList::Iter::kTail_IterStart);
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000372
bsalomon49f085d2014-09-05 13:34:00 -0700373 for ( ; entry; entry = iter.prev()) {
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000374 bytes += entry->resource()->gpuMemorySize();
reed@google.comac10a2d2010-12-22 21:39:39 +0000375 }
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000376 return bytes;
reed@google.comac10a2d2010-12-22 21:39:39 +0000377}
378
reed@google.comb89a6432011-02-07 13:20:30 +0000379static bool both_zero_or_nonzero(int count, size_t bytes) {
380 return (count == 0 && bytes == 0) || (count > 0 && bytes > 0);
381}
reed@google.comb89a6432011-02-07 13:20:30 +0000382
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000383void GrResourceCache::validate() const {
robertphillips@google.com2ea0a232012-08-23 11:13:48 +0000384 fList.validate();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000385 SkASSERT(both_zero_or_nonzero(fEntryCount, fEntryBytes));
bsalomonbcf0a522014-10-08 08:40:09 -0700386 SkASSERT(fEntryCount == fCache.count());
reed@google.com01804b42011-01-18 21:50:41 +0000387
bsalomon@google.coma2921122012-08-28 12:34:17 +0000388 EntryList::Iter iter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000389
Brian Salomon9323b8b2014-10-07 15:07:38 -0400390 // check that the shareable entries are okay
bsalomonbcf0a522014-10-08 08:40:09 -0700391 const GrResourceCacheEntry* entry = iter.init(const_cast<EntryList&>(fList),
392 EntryList::Iter::kHead_IterStart);
Brian Salomon9323b8b2014-10-07 15:07:38 -0400393
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000394 int count = 0;
bsalomon49f085d2014-09-05 13:34:00 -0700395 for ( ; entry; entry = iter.next()) {
robertphillips@google.comd07cb0c2012-08-30 19:22:29 +0000396 entry->validate();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000397 SkASSERT(fCache.find(entry->key()));
reed@google.comac10a2d2010-12-22 21:39:39 +0000398 count += 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000399 }
bsalomonbcf0a522014-10-08 08:40:09 -0700400 SkASSERT(count == fEntryCount);
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000401
bsalomonbcf0a522014-10-08 08:40:09 -0700402 size_t bytes = this->countBytes(fList);
403 SkASSERT(bytes == fEntryBytes);
404 SkASSERT(fList.countEntries() == fEntryCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000405}
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000406#endif // SK_DEBUG
robertphillips@google.com59552022012-08-31 13:07:37 +0000407
408#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000409
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000410void GrResourceCache::printStats() {
411 int locked = 0;
412
413 EntryList::Iter iter;
414
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000415 GrResourceCacheEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000416
bsalomon49f085d2014-09-05 13:34:00 -0700417 for ( ; entry; entry = iter.prev()) {
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000418 if (entry->fResource->getRefCnt() > 1) {
419 ++locked;
420 }
421 }
422
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000423 SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000424 SkDebugf("\t\tEntry Count: current %d (%d locked) high %d\n",
425 fEntryCount, locked, fHighWaterEntryCount);
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000426 SkDebugf("\t\tEntry Bytes: current %d high %d\n",
427 fEntryBytes, fHighWaterEntryBytes);
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000428}
429
reed@google.comac10a2d2010-12-22 21:39:39 +0000430#endif
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000431
432///////////////////////////////////////////////////////////////////////////////