blob: b82cdb2d4b3314817ea1a3737eaa9e494c27e826 [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.com07fc0d12012-06-22 15:15:59 +000037GrResourceCache::GrResourceCache(int maxCount, size_t maxBytes) :
38 fMaxCount(maxCount),
39 fMaxBytes(maxBytes) {
reed@google.comac10a2d2010-12-22 21:39:39 +000040 fEntryCount = 0;
bsalomon@google.coma5a1da82011-08-05 14:02:41 +000041 fUnlockedEntryCount = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +000042 fEntryBytes = 0;
43 fClientDetachedCount = 0;
44 fClientDetachedBytes = 0;
45
46 fHead = fTail = NULL;
bsalomon@google.coma5a1da82011-08-05 14:02:41 +000047 fPurging = false;
reed@google.comac10a2d2010-12-22 21:39:39 +000048}
49
bsalomon@google.com50398bf2011-07-26 20:45:30 +000050GrResourceCache::~GrResourceCache() {
51 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +000052
bsalomon@google.com8fe72472011-03-30 21:26:44 +000053 this->removeAll();
reed@google.comac10a2d2010-12-22 21:39:39 +000054}
55
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000056void GrResourceCache::getLimits(int* maxResources, size_t* maxResourceBytes) const{
57 if (maxResources) {
58 *maxResources = fMaxCount;
59 }
60 if (maxResourceBytes) {
61 *maxResourceBytes = fMaxBytes;
62 }
63}
reed@google.com01804b42011-01-18 21:50:41 +000064
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000065void GrResourceCache::setLimits(int maxResources, size_t maxResourceBytes) {
66 bool smaller = (maxResources < fMaxCount) || (maxResourceBytes < fMaxBytes);
67
68 fMaxCount = maxResources;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000069 fMaxBytes = maxResourceBytes;
reed@google.com01804b42011-01-18 21:50:41 +000070
71 if (smaller) {
72 this->purgeAsNeeded();
73 }
74}
75
bsalomon@google.com50398bf2011-07-26 20:45:30 +000076void GrResourceCache::internalDetach(GrResourceEntry* entry,
reed@google.comac10a2d2010-12-22 21:39:39 +000077 bool clientDetach) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000078 GrResourceEntry* prev = entry->fPrev;
79 GrResourceEntry* next = entry->fNext;
reed@google.comac10a2d2010-12-22 21:39:39 +000080
81 if (prev) {
82 prev->fNext = next;
83 } else {
84 fHead = next;
85 }
86 if (next) {
87 next->fPrev = prev;
88 } else {
89 fTail = prev;
90 }
bsalomon@google.coma5a1da82011-08-05 14:02:41 +000091 if (!entry->isLocked()) {
92 --fUnlockedEntryCount;
93 }
reed@google.comac10a2d2010-12-22 21:39:39 +000094
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000095 entry->fPrev = NULL;
96 entry->fNext = NULL;
97
reed@google.comac10a2d2010-12-22 21:39:39 +000098 // update our stats
99 if (clientDetach) {
100 fClientDetachedCount += 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000101 fClientDetachedBytes += entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000102 } else {
103 fEntryCount -= 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000104 fEntryBytes -= entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000105 }
106}
107
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000108void GrResourceCache::attachToHead(GrResourceEntry* entry,
reed@google.comac10a2d2010-12-22 21:39:39 +0000109 bool clientReattach) {
110 entry->fPrev = NULL;
111 entry->fNext = fHead;
112 if (fHead) {
113 fHead->fPrev = entry;
114 }
115 fHead = entry;
116 if (NULL == fTail) {
117 fTail = entry;
118 }
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000119 if (!entry->isLocked()) {
120 ++fUnlockedEntryCount;
121 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000122
123 // update our stats
124 if (clientReattach) {
125 fClientDetachedCount -= 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000126 fClientDetachedBytes -= entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000127 } else {
128 fEntryCount += 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000129 fEntryBytes += entry->resource()->sizeInBytes();
reed@google.comac10a2d2010-12-22 21:39:39 +0000130 }
131}
132
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000133class GrResourceCache::Key {
134 typedef GrResourceEntry T;
reed@google.comac10a2d2010-12-22 21:39:39 +0000135
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000136 const GrResourceKey& fKey;
reed@google.comac10a2d2010-12-22 21:39:39 +0000137public:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000138 Key(const GrResourceKey& key) : fKey(key) {}
reed@google.comac10a2d2010-12-22 21:39:39 +0000139
140 uint32_t getHash() const { return fKey.hashIndex(); }
reed@google.com01804b42011-01-18 21:50:41 +0000141
reed@google.comac10a2d2010-12-22 21:39:39 +0000142 static bool LT(const T& entry, const Key& key) {
143 return entry.key() < key.fKey;
144 }
145 static bool EQ(const T& entry, const Key& key) {
146 return entry.key() == key.fKey;
147 }
148#if GR_DEBUG
149 static uint32_t GetHash(const T& entry) {
150 return entry.key().hashIndex();
151 }
152 static bool LT(const T& a, const T& b) {
153 return a.key() < b.key();
154 }
155 static bool EQ(const T& a, const T& b) {
156 return a.key() == b.key();
157 }
158#endif
159};
160
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000161GrResourceEntry* GrResourceCache::findAndLock(const GrResourceKey& key,
162 LockType type) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000163 GrAutoResourceCacheValidate atcv(this);
reed@google.comac10a2d2010-12-22 21:39:39 +0000164
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000165 GrResourceEntry* entry = fCache.find(key);
reed@google.comac10a2d2010-12-22 21:39:39 +0000166 if (entry) {
167 this->internalDetach(entry, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000168 // mark the entry as "busy" so it doesn't get purged
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000169 // do this between detach and attach for locked count tracking
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000170 if (kNested_LockType == type || !entry->isLocked()) {
171 entry->lock();
172 }
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000173 this->attachToHead(entry, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000174 }
175 return entry;
176}
177
bsalomon@google.comfb309512011-11-30 14:13:48 +0000178bool GrResourceCache::hasKey(const GrResourceKey& key) const {
179 return NULL != fCache.find(key);
180}
181
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000182GrResourceEntry* GrResourceCache::create(const GrResourceKey& key,
183 GrResource* resource,
robertphillips@google.com386319e2012-06-27 14:59:18 +0000184 bool lock,
185 bool clientReattach) {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000186 // we don't expect to create new resources during a purge. In theory
187 // this could cause purgeAsNeeded() into an infinite loop (e.g.
188 // each resource destroyed creates and locks 2 resources and
189 // unlocks 1 thereby causing a new purge).
190 GrAssert(!fPurging);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000191 GrAutoResourceCacheValidate atcv(this);
reed@google.comac10a2d2010-12-22 21:39:39 +0000192
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000193 GrResourceEntry* entry = SkNEW_ARGS(GrResourceEntry, (key, resource));
reed@google.comac10a2d2010-12-22 21:39:39 +0000194
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000195 if (lock) {
196 // mark the entry as "busy" so it doesn't get purged
197 // do this before attach for locked count tracking
198 entry->lock();
199 }
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000200
robertphillips@google.com386319e2012-06-27 14:59:18 +0000201 this->attachToHead(entry, clientReattach);
reed@google.comac10a2d2010-12-22 21:39:39 +0000202 fCache.insert(key, entry);
203
204#if GR_DUMP_TEXTURE_UPLOAD
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000205 GrPrintf("--- add resource to cache %p, count=%d bytes= %d %d\n",
206 entry, fEntryCount, resource->sizeInBytes(), fEntryBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000207#endif
208
reed@google.comac10a2d2010-12-22 21:39:39 +0000209 this->purgeAsNeeded();
210 return entry;
211}
212
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000213GrResourceEntry* GrResourceCache::createAndLock(const GrResourceKey& key,
214 GrResource* resource) {
robertphillips@google.com386319e2012-06-27 14:59:18 +0000215 return this->create(key, resource, true, false);
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000216}
217
218void GrResourceCache::attach(const GrResourceKey& key,
219 GrResource* resource) {
robertphillips@google.com386319e2012-06-27 14:59:18 +0000220 this->create(key, resource, false, true);
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000221}
222
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000223void GrResourceCache::detach(GrResourceEntry* entry) {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000224 GrAutoResourceCacheValidate atcv(this);
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000225 this->internalDetach(entry, true);
reed@google.comac10a2d2010-12-22 21:39:39 +0000226 fCache.remove(entry->fKey, entry);
227}
228
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000229void GrResourceCache::freeEntry(GrResourceEntry* entry) {
230 GrAssert(NULL == entry->fNext && NULL == entry->fPrev);
231 delete entry;
232}
233
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000234void GrResourceCache::reattachAndUnlock(GrResourceEntry* entry) {
bsalomon@google.com60879752011-09-15 20:43:53 +0000235 GrAutoResourceCacheValidate atcv(this);
236 if (entry->resource()->isValid()) {
237 attachToHead(entry, true);
238 fCache.insert(entry->key(), entry);
239 } else {
240 // If the resource went invalid while it was detached then purge it
241 // This can happen when a 3D context was lost,
242 // the client called GrContext::contextDestroyed() to notify Gr,
243 // and then later an SkGpuDevice's destructor releases its backing
244 // texture (which was invalidated at contextDestroyed time).
245 fClientDetachedCount -= 1;
246 fEntryCount -= 1;
247 size_t size = entry->resource()->sizeInBytes();
248 fClientDetachedBytes -= size;
249 fEntryBytes -= size;
250 }
251 this->unlock(entry);
reed@google.comac10a2d2010-12-22 21:39:39 +0000252}
253
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000254void GrResourceCache::unlock(GrResourceEntry* entry) {
255 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +0000256
reed@google.comac10a2d2010-12-22 21:39:39 +0000257 GrAssert(entry);
258 GrAssert(entry->isLocked());
259 GrAssert(fCache.find(entry->key()));
260
261 entry->unlock();
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000262 if (!entry->isLocked()) {
263 ++fUnlockedEntryCount;
264 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000265 this->purgeAsNeeded();
266}
267
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000268/**
269 * Destroying a resource may potentially trigger the unlock of additional
270 * resources which in turn will trigger a nested purge. We block the nested
271 * purge using the fPurging variable. However, the initial purge will keep
272 * looping until either all resources in the cache are unlocked or we've met
273 * the budget. There is an assertion in createAndLock to check against a
274 * resource's destructor inserting new resources into the cache. If these
275 * new resources were unlocked before purgeAsNeeded completed it could
276 * potentially make purgeAsNeeded loop infinitely.
277 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000278void GrResourceCache::purgeAsNeeded() {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000279 if (!fPurging) {
280 fPurging = true;
281 bool withinBudget = false;
282 do {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000283 GrResourceEntry* entry = fTail;
284 while (entry && fUnlockedEntryCount) {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000285 GrAutoResourceCacheValidate atcv(this);
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000286 if (fEntryCount <= fMaxCount && fEntryBytes <= fMaxBytes) {
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000287 withinBudget = true;
288 break;
289 }
reed@google.com01804b42011-01-18 21:50:41 +0000290
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000291 GrResourceEntry* prev = entry->fPrev;
292 if (!entry->isLocked()) {
293 // remove from our cache
294 fCache.remove(entry->fKey, entry);
reed@google.comac10a2d2010-12-22 21:39:39 +0000295
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000296 // remove from our llist
297 this->internalDetach(entry, false);
reed@google.com01804b42011-01-18 21:50:41 +0000298
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000299 #if GR_DUMP_TEXTURE_UPLOAD
300 GrPrintf("--- ~resource from cache %p [%d %d]\n",
301 entry->resource(),
302 entry->resource()->width(),
303 entry->resource()->height());
304 #endif
305 delete entry;
306 }
307 entry = prev;
308 }
309 } while (!withinBudget && fUnlockedEntryCount);
310 fPurging = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000311 }
312}
313
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000314void GrResourceCache::removeAll() {
bsalomon@google.come9a98942011-08-22 17:06:16 +0000315 GrAutoResourceCacheValidate atcv(this);
reed@google.com01804b42011-01-18 21:50:41 +0000316
bsalomon@google.come9a98942011-08-22 17:06:16 +0000317 // we can have one GrResource holding a lock on another
318 // so we don't want to just do a simple loop kicking each
319 // entry out. Instead change the budget and purge.
reed@google.comac10a2d2010-12-22 21:39:39 +0000320
bsalomon@google.come9a98942011-08-22 17:06:16 +0000321 int savedMaxBytes = fMaxBytes;
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000322 int savedMaxCount = fMaxCount;
323 fMaxBytes = (size_t) -1;
324 fMaxCount = 0;
bsalomon@google.come9a98942011-08-22 17:06:16 +0000325 this->purgeAsNeeded();
326
twiz@google.com0ec107f2012-02-21 19:15:53 +0000327#if GR_DEBUG
bsalomon@google.come9a98942011-08-22 17:06:16 +0000328 GrAssert(!fUnlockedEntryCount);
twiz@google.com0ec107f2012-02-21 19:15:53 +0000329 if (!fCache.count()) {
330 // Items may have been detached from the cache (such as the backing
331 // texture for an SkGpuDevice). The above purge would not have removed
332 // them.
333 GrAssert(fEntryCount == fClientDetachedCount);
334 GrAssert(fEntryBytes == fClientDetachedBytes);
335 GrAssert(NULL == fHead);
336 GrAssert(NULL == fTail);
337 }
338#endif
bsalomon@google.come9a98942011-08-22 17:06:16 +0000339
340 fMaxBytes = savedMaxBytes;
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000341 fMaxCount = savedMaxCount;
reed@google.comac10a2d2010-12-22 21:39:39 +0000342}
343
344///////////////////////////////////////////////////////////////////////////////
345
346#if GR_DEBUG
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000347static int countMatches(const GrResourceEntry* head, const GrResourceEntry* target) {
348 const GrResourceEntry* entry = head;
reed@google.comac10a2d2010-12-22 21:39:39 +0000349 int count = 0;
350 while (entry) {
351 if (target == entry) {
352 count += 1;
353 }
354 entry = entry->next();
355 }
356 return count;
357}
358
reed@google.comb89a6432011-02-07 13:20:30 +0000359#if GR_DEBUG
360static bool both_zero_or_nonzero(int count, size_t bytes) {
361 return (count == 0 && bytes == 0) || (count > 0 && bytes > 0);
362}
363#endif
364
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000365void GrResourceCache::validate() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000366 GrAssert(!fHead == !fTail);
reed@google.comb89a6432011-02-07 13:20:30 +0000367 GrAssert(both_zero_or_nonzero(fEntryCount, fEntryBytes));
368 GrAssert(both_zero_or_nonzero(fClientDetachedCount, fClientDetachedBytes));
reed@google.comac10a2d2010-12-22 21:39:39 +0000369 GrAssert(fClientDetachedBytes <= fEntryBytes);
370 GrAssert(fClientDetachedCount <= fEntryCount);
371 GrAssert((fEntryCount - fClientDetachedCount) == fCache.count());
reed@google.com01804b42011-01-18 21:50:41 +0000372
reed@google.comac10a2d2010-12-22 21:39:39 +0000373 fCache.validate();
374
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000375 GrResourceEntry* entry = fHead;
reed@google.comac10a2d2010-12-22 21:39:39 +0000376 int count = 0;
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000377 int unlockCount = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000378 size_t bytes = 0;
379 while (entry) {
380 entry->validate();
381 GrAssert(fCache.find(entry->key()));
382 count += 1;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000383 bytes += entry->resource()->sizeInBytes();
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000384 if (!entry->isLocked()) {
385 unlockCount += 1;
386 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000387 entry = entry->fNext;
388 }
389 GrAssert(count == fEntryCount - fClientDetachedCount);
390 GrAssert(bytes == fEntryBytes - fClientDetachedBytes);
bsalomon@google.coma5a1da82011-08-05 14:02:41 +0000391 GrAssert(unlockCount == fUnlockedEntryCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000392
393 count = 0;
394 for (entry = fTail; entry; entry = entry->fPrev) {
395 count += 1;
396 }
397 GrAssert(count == fEntryCount - fClientDetachedCount);
398
399 for (int i = 0; i < count; i++) {
400 int matches = countMatches(fHead, fCache.getArray()[i]);
401 GrAssert(1 == matches);
402 }
403}
404#endif