blob: 8c939714d3847f59bf08915ff68d56ec709921c1 [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"
12#include "GrResource.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000013
bsalomon@google.com50398bf2011-07-26 20:45:30 +000014GrResourceEntry::GrResourceEntry(const GrResourceKey& key, GrResource* resource)
15 : fKey(key), fResource(resource) {
reed@google.comac10a2d2010-12-22 21:39:39 +000016 fLockCount = 0;
17 fPrev = fNext = NULL;
18
bsalomon@google.com50398bf2011-07-26 20:45:30 +000019 // we assume ownership of the resource, and will unref it when we die
20 GrAssert(resource);
reed@google.comac10a2d2010-12-22 21:39:39 +000021}
22
bsalomon@google.com50398bf2011-07-26 20:45:30 +000023GrResourceEntry::~GrResourceEntry() {
24 fResource->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +000025}
26
27#if GR_DEBUG
bsalomon@google.com50398bf2011-07-26 20:45:30 +000028void GrResourceEntry::validate() const {
reed@google.comac10a2d2010-12-22 21:39:39 +000029 GrAssert(fLockCount >= 0);
bsalomon@google.com50398bf2011-07-26 20:45:30 +000030 GrAssert(fResource);
31 fResource->validate();
reed@google.comac10a2d2010-12-22 21:39:39 +000032}
33#endif
34
35///////////////////////////////////////////////////////////////////////////////
36
bsalomon@google.com8f7e1da2012-06-21 19:48:32 +000037GrResourceCache::GrResourceCache(size_t maxBytes) {
38 fMaxBytes = maxBytes;
reed@google.comac10a2d2010-12-22 21:39:39 +000039 fEntryCount = 0;
bsalomon@google.coma5a1da82011-08-05 14:02:41 +000040 fUnlockedEntryCount = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +000041 fEntryBytes = 0;
42 fClientDetachedCount = 0;
43 fClientDetachedBytes = 0;
44
45 fHead = fTail = NULL;
bsalomon@google.coma5a1da82011-08-05 14:02:41 +000046 fPurging = false;
reed@google.comac10a2d2010-12-22 21:39:39 +000047}
48
bsalomon@google.com50398bf2011-07-26 20:45:30 +000049GrResourceCache::~GrResourceCache() {
50 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +000051
bsalomon@google.com8fe72472011-03-30 21:26:44 +000052 this->removeAll();
reed@google.comac10a2d2010-12-22 21:39:39 +000053}
54
bsalomon@google.com8f7e1da2012-06-21 19:48:32 +000055void GrResourceCache::setBudget(size_t maxResourceBytes) {
56 bool smaller = maxResourceBytes < fMaxBytes;
reed@google.com01804b42011-01-18 21:50:41 +000057
bsalomon@google.com50398bf2011-07-26 20:45:30 +000058 fMaxBytes = maxResourceBytes;
reed@google.com01804b42011-01-18 21:50:41 +000059
60 if (smaller) {
61 this->purgeAsNeeded();
62 }
63}
64
bsalomon@google.com50398bf2011-07-26 20:45:30 +000065void GrResourceCache::internalDetach(GrResourceEntry* entry,
reed@google.comac10a2d2010-12-22 21:39:39 +000066 bool clientDetach) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000067 GrResourceEntry* prev = entry->fPrev;
68 GrResourceEntry* next = entry->fNext;
reed@google.comac10a2d2010-12-22 21:39:39 +000069
70 if (prev) {
71 prev->fNext = next;
72 } else {
73 fHead = next;
74 }
75 if (next) {
76 next->fPrev = prev;
77 } else {
78 fTail = prev;
79 }
bsalomon@google.coma5a1da82011-08-05 14:02:41 +000080 if (!entry->isLocked()) {
81 --fUnlockedEntryCount;
82 }
reed@google.comac10a2d2010-12-22 21:39:39 +000083
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000084 entry->fPrev = NULL;
85 entry->fNext = NULL;
86
reed@google.comac10a2d2010-12-22 21:39:39 +000087 // update our stats
88 if (clientDetach) {
89 fClientDetachedCount += 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000090 fClientDetachedBytes += entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +000091 } else {
92 fEntryCount -= 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000093 fEntryBytes -= entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +000094 }
95}
96
bsalomon@google.com50398bf2011-07-26 20:45:30 +000097void GrResourceCache::attachToHead(GrResourceEntry* entry,
reed@google.comac10a2d2010-12-22 21:39:39 +000098 bool clientReattach) {
99 entry->fPrev = NULL;
100 entry->fNext = fHead;
101 if (fHead) {
102 fHead->fPrev = entry;
103 }
104 fHead = entry;
105 if (NULL == fTail) {
106 fTail = entry;
107 }
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000108 if (!entry->isLocked()) {
109 ++fUnlockedEntryCount;
110 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000111
112 // update our stats
113 if (clientReattach) {
114 fClientDetachedCount -= 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000115 fClientDetachedBytes -= entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000116 } else {
117 fEntryCount += 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000118 fEntryBytes += entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000119 }
120}
121
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000122class GrResourceCache::Key {
123 typedef GrResourceEntry T;
reed@google.comac10a2d2010-12-22 21:39:39 +0000124
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000125 const GrResourceKey& fKey;
reed@google.comac10a2d2010-12-22 21:39:39 +0000126public:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000127 Key(const GrResourceKey& key) : fKey(key) {}
reed@google.comac10a2d2010-12-22 21:39:39 +0000128
129 uint32_t getHash() const { return fKey.hashIndex(); }
reed@google.com01804b42011-01-18 21:50:41 +0000130
reed@google.comac10a2d2010-12-22 21:39:39 +0000131 static bool LT(const T& entry, const Key& key) {
132 return entry.key() < key.fKey;
133 }
134 static bool EQ(const T& entry, const Key& key) {
135 return entry.key() == key.fKey;
136 }
137#if GR_DEBUG
138 static uint32_t GetHash(const T& entry) {
139 return entry.key().hashIndex();
140 }
141 static bool LT(const T& a, const T& b) {
142 return a.key() < b.key();
143 }
144 static bool EQ(const T& a, const T& b) {
145 return a.key() == b.key();
146 }
147#endif
148};
149
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000150GrResourceEntry* GrResourceCache::findAndLock(const GrResourceKey& key,
151 LockType type) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000152 GrAutoResourceCacheValidate atcv(this);
reed@google.comac10a2d2010-12-22 21:39:39 +0000153
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000154 GrResourceEntry* entry = fCache.find(key);
reed@google.comac10a2d2010-12-22 21:39:39 +0000155 if (entry) {
156 this->internalDetach(entry, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000157 // mark the entry as "busy" so it doesn't get purged
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000158 // do this between detach and attach for locked count tracking
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000159 if (kNested_LockType == type || !entry->isLocked()) {
160 entry->lock();
161 }
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000162 this->attachToHead(entry, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000163 }
164 return entry;
165}
166
bsalomon@google.comfb309512011-11-30 14:13:48 +0000167bool GrResourceCache::hasKey(const GrResourceKey& key) const {
168 return NULL != fCache.find(key);
169}
170
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000171GrResourceEntry* GrResourceCache::create(const GrResourceKey& key,
172 GrResource* resource,
173 bool lock) {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000174 // we don't expect to create new resources during a purge. In theory
175 // this could cause purgeAsNeeded() into an infinite loop (e.g.
176 // each resource destroyed creates and locks 2 resources and
177 // unlocks 1 thereby causing a new purge).
178 GrAssert(!fPurging);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000179 GrAutoResourceCacheValidate atcv(this);
reed@google.comac10a2d2010-12-22 21:39:39 +0000180
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000181 GrResourceEntry* entry = new GrResourceEntry(key, resource);
reed@google.comac10a2d2010-12-22 21:39:39 +0000182
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000183 if (lock) {
184 // mark the entry as "busy" so it doesn't get purged
185 // do this before attach for locked count tracking
186 entry->lock();
187 }
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000188
reed@google.comac10a2d2010-12-22 21:39:39 +0000189 this->attachToHead(entry, false);
190 fCache.insert(key, entry);
191
192#if GR_DUMP_TEXTURE_UPLOAD
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000193 GrPrintf("--- add resource to cache %p, count=%d bytes= %d %d\n",
194 entry, fEntryCount, resource->sizeInBytes(), fEntryBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000195#endif
196
reed@google.comac10a2d2010-12-22 21:39:39 +0000197 this->purgeAsNeeded();
198 return entry;
199}
200
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000201GrResourceEntry* GrResourceCache::createAndLock(const GrResourceKey& key,
202 GrResource* resource) {
203 return this->create(key, resource, true);
204}
205
206void GrResourceCache::attach(const GrResourceKey& key,
207 GrResource* resource) {
208 this->create(key, resource, false);
209}
210
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000211void GrResourceCache::detach(GrResourceEntry* entry) {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000212 GrAutoResourceCacheValidate atcv(this);
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000213 this->internalDetach(entry, true);
reed@google.comac10a2d2010-12-22 21:39:39 +0000214 fCache.remove(entry->fKey, entry);
215}
216
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000217void GrResourceCache::freeEntry(GrResourceEntry* entry) {
218 GrAssert(NULL == entry->fNext && NULL == entry->fPrev);
219 delete entry;
220}
221
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000222void GrResourceCache::reattachAndUnlock(GrResourceEntry* entry) {
bsalomon@google.com60879752011-09-15 20:43:53 +0000223 GrAutoResourceCacheValidate atcv(this);
224 if (entry->resource()->isValid()) {
225 attachToHead(entry, true);
226 fCache.insert(entry->key(), entry);
227 } else {
228 // If the resource went invalid while it was detached then purge it
229 // This can happen when a 3D context was lost,
230 // the client called GrContext::contextDestroyed() to notify Gr,
231 // and then later an SkGpuDevice's destructor releases its backing
232 // texture (which was invalidated at contextDestroyed time).
233 fClientDetachedCount -= 1;
234 fEntryCount -= 1;
235 size_t size = entry->resource()->sizeInBytes();
bsalomon@google.com8f7e1da2012-06-21 19:48:32 +0000236 GrAssert(size > 0);
bsalomon@google.com60879752011-09-15 20:43:53 +0000237 fClientDetachedBytes -= size;
238 fEntryBytes -= size;
239 }
240 this->unlock(entry);
reed@google.comac10a2d2010-12-22 21:39:39 +0000241}
242
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000243void GrResourceCache::unlock(GrResourceEntry* entry) {
244 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +0000245
reed@google.comac10a2d2010-12-22 21:39:39 +0000246 GrAssert(entry);
247 GrAssert(entry->isLocked());
248 GrAssert(fCache.find(entry->key()));
249
250 entry->unlock();
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000251 if (!entry->isLocked()) {
252 ++fUnlockedEntryCount;
253 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000254 this->purgeAsNeeded();
255}
256
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000257/**
258 * Destroying a resource may potentially trigger the unlock of additional
259 * resources which in turn will trigger a nested purge. We block the nested
260 * purge using the fPurging variable. However, the initial purge will keep
261 * looping until either all resources in the cache are unlocked or we've met
262 * the budget. There is an assertion in createAndLock to check against a
263 * resource's destructor inserting new resources into the cache. If these
264 * new resources were unlocked before purgeAsNeeded completed it could
265 * potentially make purgeAsNeeded loop infinitely.
266 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000267void GrResourceCache::purgeAsNeeded() {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000268 if (!fPurging) {
269 fPurging = true;
270 bool withinBudget = false;
271 do {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000272 GrResourceEntry* entry = fTail;
273 while (entry && fUnlockedEntryCount) {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000274 GrAutoResourceCacheValidate atcv(this);
bsalomon@google.com8f7e1da2012-06-21 19:48:32 +0000275 if (fEntryBytes <= fMaxBytes) {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000276 withinBudget = true;
277 break;
278 }
reed@google.com01804b42011-01-18 21:50:41 +0000279
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000280 GrResourceEntry* prev = entry->fPrev;
281 if (!entry->isLocked()) {
282 // remove from our cache
283 fCache.remove(entry->fKey, entry);
reed@google.comac10a2d2010-12-22 21:39:39 +0000284
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000285 // remove from our llist
286 this->internalDetach(entry, false);
reed@google.com01804b42011-01-18 21:50:41 +0000287
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000288 #if GR_DUMP_TEXTURE_UPLOAD
289 GrPrintf("--- ~resource from cache %p [%d %d]\n",
290 entry->resource(),
291 entry->resource()->width(),
292 entry->resource()->height());
293 #endif
294 delete entry;
295 }
296 entry = prev;
297 }
298 } while (!withinBudget && fUnlockedEntryCount);
299 fPurging = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000300 }
301}
302
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000303void GrResourceCache::removeAll() {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000304 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +0000305
bsalomon@google.come9a98942011-08-22 17:06:16 +0000306 // we can have one GrResource holding a lock on another
307 // so we don't want to just do a simple loop kicking each
308 // entry out. Instead change the budget and purge.
reed@google.comac10a2d2010-12-22 21:39:39 +0000309
bsalomon@google.come9a98942011-08-22 17:06:16 +0000310 int savedMaxBytes = fMaxBytes;
bsalomon@google.com8f7e1da2012-06-21 19:48:32 +0000311 fMaxBytes = 0;
bsalomon@google.come9a98942011-08-22 17:06:16 +0000312 this->purgeAsNeeded();
313
twiz@google.com0ec107f2012-02-21 19:15:53 +0000314#if GR_DEBUG
bsalomon@google.come9a98942011-08-22 17:06:16 +0000315 GrAssert(!fUnlockedEntryCount);
twiz@google.com0ec107f2012-02-21 19:15:53 +0000316 if (!fCache.count()) {
317 // Items may have been detached from the cache (such as the backing
318 // texture for an SkGpuDevice). The above purge would not have removed
319 // them.
320 GrAssert(fEntryCount == fClientDetachedCount);
321 GrAssert(fEntryBytes == fClientDetachedBytes);
322 GrAssert(NULL == fHead);
323 GrAssert(NULL == fTail);
324 }
325#endif
bsalomon@google.come9a98942011-08-22 17:06:16 +0000326
327 fMaxBytes = savedMaxBytes;
reed@google.comac10a2d2010-12-22 21:39:39 +0000328}
329
330///////////////////////////////////////////////////////////////////////////////
331
332#if GR_DEBUG
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000333static int countMatches(const GrResourceEntry* head, const GrResourceEntry* target) {
334 const GrResourceEntry* entry = head;
reed@google.comac10a2d2010-12-22 21:39:39 +0000335 int count = 0;
336 while (entry) {
337 if (target == entry) {
338 count += 1;
339 }
340 entry = entry->next();
341 }
342 return count;
343}
344
reed@google.comb89a6432011-02-07 13:20:30 +0000345#if GR_DEBUG
346static bool both_zero_or_nonzero(int count, size_t bytes) {
347 return (count == 0 && bytes == 0) || (count > 0 && bytes > 0);
348}
349#endif
350
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000351void GrResourceCache::validate() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000352 GrAssert(!fHead == !fTail);
reed@google.comb89a6432011-02-07 13:20:30 +0000353 GrAssert(both_zero_or_nonzero(fEntryCount, fEntryBytes));
354 GrAssert(both_zero_or_nonzero(fClientDetachedCount, fClientDetachedBytes));
reed@google.comac10a2d2010-12-22 21:39:39 +0000355 GrAssert(fClientDetachedBytes <= fEntryBytes);
356 GrAssert(fClientDetachedCount <= fEntryCount);
357 GrAssert((fEntryCount - fClientDetachedCount) == fCache.count());
reed@google.com01804b42011-01-18 21:50:41 +0000358
reed@google.comac10a2d2010-12-22 21:39:39 +0000359 fCache.validate();
360
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000361 GrResourceEntry* entry = fHead;
reed@google.comac10a2d2010-12-22 21:39:39 +0000362 int count = 0;
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000363 int unlockCount = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000364 size_t bytes = 0;
365 while (entry) {
366 entry->validate();
367 GrAssert(fCache.find(entry->key()));
368 count += 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000369 bytes += entry->resource()->sizeInBytes();
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000370 if (!entry->isLocked()) {
371 unlockCount += 1;
372 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000373 entry = entry->fNext;
374 }
375 GrAssert(count == fEntryCount - fClientDetachedCount);
376 GrAssert(bytes == fEntryBytes - fClientDetachedBytes);
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000377 GrAssert(unlockCount == fUnlockedEntryCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000378
379 count = 0;
380 for (entry = fTail; entry; entry = entry->fPrev) {
381 count += 1;
382 }
383 GrAssert(count == fEntryCount - fClientDetachedCount);
384
385 for (int i = 0; i < count; i++) {
386 int matches = countMatches(fHead, fCache.getArray()[i]);
387 GrAssert(1 == matches);
388 }
389}
390#endif