1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
/*
* Copyright 2010 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "GrResourceCache.h"
#include "GrResource.h"
GrResourceEntry::GrResourceEntry(const GrResourceKey& key, GrResource* resource)
: fKey(key), fResource(resource) {
fLockCount = 0;
fPrev = fNext = NULL;
// we assume ownership of the resource, and will unref it when we die
GrAssert(resource);
}
GrResourceEntry::~GrResourceEntry() {
fResource->unref();
}
#if GR_DEBUG
void GrResourceEntry::validate() const {
GrAssert(fLockCount >= 0);
GrAssert(fResource);
fResource->validate();
}
#endif
///////////////////////////////////////////////////////////////////////////////
GrResourceCache::GrResourceCache(int maxCount, size_t maxBytes) :
fMaxCount(maxCount),
fMaxBytes(maxBytes) {
fEntryCount = 0;
fUnlockedEntryCount = 0;
fEntryBytes = 0;
fClientDetachedCount = 0;
fClientDetachedBytes = 0;
fHead = fTail = NULL;
fPurging = false;
}
GrResourceCache::~GrResourceCache() {
GrAutoResourceCacheValidate atcv(this);
this->removeAll();
}
void GrResourceCache::getLimits(int* maxResources, size_t* maxResourceBytes) const{
if (maxResources) {
*maxResources = fMaxCount;
}
if (maxResourceBytes) {
*maxResourceBytes = fMaxBytes;
}
}
void GrResourceCache::setLimits(int maxResources, size_t maxResourceBytes) {
bool smaller = (maxResources < fMaxCount) || (maxResourceBytes < fMaxBytes);
fMaxCount = maxResources;
fMaxBytes = maxResourceBytes;
if (smaller) {
this->purgeAsNeeded();
}
}
void GrResourceCache::internalDetach(GrResourceEntry* entry,
bool clientDetach) {
GrResourceEntry* prev = entry->fPrev;
GrResourceEntry* next = entry->fNext;
if (prev) {
prev->fNext = next;
} else {
fHead = next;
}
if (next) {
next->fPrev = prev;
} else {
fTail = prev;
}
if (!entry->isLocked()) {
--fUnlockedEntryCount;
}
// update our stats
if (clientDetach) {
fClientDetachedCount += 1;
fClientDetachedBytes += entry->resource()->sizeInBytes();
} else {
fEntryCount -= 1;
fEntryBytes -= entry->resource()->sizeInBytes();
}
}
void GrResourceCache::attachToHead(GrResourceEntry* entry,
bool clientReattach) {
entry->fPrev = NULL;
entry->fNext = fHead;
if (fHead) {
fHead->fPrev = entry;
}
fHead = entry;
if (NULL == fTail) {
fTail = entry;
}
if (!entry->isLocked()) {
++fUnlockedEntryCount;
}
// update our stats
if (clientReattach) {
fClientDetachedCount -= 1;
fClientDetachedBytes -= entry->resource()->sizeInBytes();
} else {
fEntryCount += 1;
fEntryBytes += entry->resource()->sizeInBytes();
}
}
class GrResourceCache::Key {
typedef GrResourceEntry T;
const GrResourceKey& fKey;
public:
Key(const GrResourceKey& key) : fKey(key) {}
uint32_t getHash() const { return fKey.hashIndex(); }
static bool LT(const T& entry, const Key& key) {
return entry.key() < key.fKey;
}
static bool EQ(const T& entry, const Key& key) {
return entry.key() == key.fKey;
}
#if GR_DEBUG
static uint32_t GetHash(const T& entry) {
return entry.key().hashIndex();
}
static bool LT(const T& a, const T& b) {
return a.key() < b.key();
}
static bool EQ(const T& a, const T& b) {
return a.key() == b.key();
}
#endif
};
GrResourceEntry* GrResourceCache::findAndLock(const GrResourceKey& key,
LockType type) {
GrAutoResourceCacheValidate atcv(this);
GrResourceEntry* entry = fCache.find(key);
if (entry) {
this->internalDetach(entry, false);
// mark the entry as "busy" so it doesn't get purged
// do this between detach and attach for locked count tracking
if (kNested_LockType == type || !entry->isLocked()) {
entry->lock();
}
this->attachToHead(entry, false);
}
return entry;
}
bool GrResourceCache::hasKey(const GrResourceKey& key) const {
return NULL != fCache.find(key);
}
GrResourceEntry* GrResourceCache::createAndLock(const GrResourceKey& key,
GrResource* resource) {
// we don't expect to create new resources during a purge. In theory
// this could cause purgeAsNeeded() into an infinite loop (e.g.
// each resource destroyed creates and locks 2 resources and
// unlocks 1 thereby causing a new purge).
GrAssert(!fPurging);
GrAutoResourceCacheValidate atcv(this);
GrResourceEntry* entry = new GrResourceEntry(key, resource);
// mark the entry as "busy" so it doesn't get purged
// do this before attach for locked count tracking
entry->lock();
this->attachToHead(entry, false);
fCache.insert(key, entry);
#if GR_DUMP_TEXTURE_UPLOAD
GrPrintf("--- add resource to cache %p, count=%d bytes= %d %d\n",
entry, fEntryCount, resource->sizeInBytes(), fEntryBytes);
#endif
this->purgeAsNeeded();
return entry;
}
void GrResourceCache::detach(GrResourceEntry* entry) {
GrAutoResourceCacheValidate atcv(this);
internalDetach(entry, true);
fCache.remove(entry->fKey, entry);
}
void GrResourceCache::reattachAndUnlock(GrResourceEntry* entry) {
GrAutoResourceCacheValidate atcv(this);
if (entry->resource()->isValid()) {
attachToHead(entry, true);
fCache.insert(entry->key(), entry);
} else {
// If the resource went invalid while it was detached then purge it
// This can happen when a 3D context was lost,
// the client called GrContext::contextDestroyed() to notify Gr,
// and then later an SkGpuDevice's destructor releases its backing
// texture (which was invalidated at contextDestroyed time).
fClientDetachedCount -= 1;
fEntryCount -= 1;
size_t size = entry->resource()->sizeInBytes();
fClientDetachedBytes -= size;
fEntryBytes -= size;
}
this->unlock(entry);
}
void GrResourceCache::unlock(GrResourceEntry* entry) {
GrAutoResourceCacheValidate atcv(this);
GrAssert(entry);
GrAssert(entry->isLocked());
GrAssert(fCache.find(entry->key()));
entry->unlock();
if (!entry->isLocked()) {
++fUnlockedEntryCount;
}
this->purgeAsNeeded();
}
/**
* Destroying a resource may potentially trigger the unlock of additional
* resources which in turn will trigger a nested purge. We block the nested
* purge using the fPurging variable. However, the initial purge will keep
* looping until either all resources in the cache are unlocked or we've met
* the budget. There is an assertion in createAndLock to check against a
* resource's destructor inserting new resources into the cache. If these
* new resources were unlocked before purgeAsNeeded completed it could
* potentially make purgeAsNeeded loop infinitely.
*/
void GrResourceCache::purgeAsNeeded() {
if (!fPurging) {
fPurging = true;
bool withinBudget = false;
do {
GrResourceEntry* entry = fTail;
while (entry && fUnlockedEntryCount) {
GrAutoResourceCacheValidate atcv(this);
if (fEntryCount <= fMaxCount && fEntryBytes <= fMaxBytes) {
withinBudget = true;
break;
}
GrResourceEntry* prev = entry->fPrev;
if (!entry->isLocked()) {
// remove from our cache
fCache.remove(entry->fKey, entry);
// remove from our llist
this->internalDetach(entry, false);
#if GR_DUMP_TEXTURE_UPLOAD
GrPrintf("--- ~resource from cache %p [%d %d]\n",
entry->resource(),
entry->resource()->width(),
entry->resource()->height());
#endif
delete entry;
}
entry = prev;
}
} while (!withinBudget && fUnlockedEntryCount);
fPurging = false;
}
}
void GrResourceCache::removeAll() {
GrAutoResourceCacheValidate atcv(this);
// we can have one GrResource holding a lock on another
// so we don't want to just do a simple loop kicking each
// entry out. Instead change the budget and purge.
int savedMaxBytes = fMaxBytes;
int savedMaxCount = fMaxCount;
fMaxBytes = (size_t) -1;
fMaxCount = 0;
this->purgeAsNeeded();
#if GR_DEBUG
GrAssert(!fUnlockedEntryCount);
if (!fCache.count()) {
// Items may have been detached from the cache (such as the backing
// texture for an SkGpuDevice). The above purge would not have removed
// them.
GrAssert(fEntryCount == fClientDetachedCount);
GrAssert(fEntryBytes == fClientDetachedBytes);
GrAssert(NULL == fHead);
GrAssert(NULL == fTail);
}
#endif
fMaxBytes = savedMaxBytes;
fMaxCount = savedMaxCount;
}
///////////////////////////////////////////////////////////////////////////////
#if GR_DEBUG
static int countMatches(const GrResourceEntry* head, const GrResourceEntry* target) {
const GrResourceEntry* entry = head;
int count = 0;
while (entry) {
if (target == entry) {
count += 1;
}
entry = entry->next();
}
return count;
}
#if GR_DEBUG
static bool both_zero_or_nonzero(int count, size_t bytes) {
return (count == 0 && bytes == 0) || (count > 0 && bytes > 0);
}
#endif
void GrResourceCache::validate() const {
GrAssert(!fHead == !fTail);
GrAssert(both_zero_or_nonzero(fEntryCount, fEntryBytes));
GrAssert(both_zero_or_nonzero(fClientDetachedCount, fClientDetachedBytes));
GrAssert(fClientDetachedBytes <= fEntryBytes);
GrAssert(fClientDetachedCount <= fEntryCount);
GrAssert((fEntryCount - fClientDetachedCount) == fCache.count());
fCache.validate();
GrResourceEntry* entry = fHead;
int count = 0;
int unlockCount = 0;
size_t bytes = 0;
while (entry) {
entry->validate();
GrAssert(fCache.find(entry->key()));
count += 1;
bytes += entry->resource()->sizeInBytes();
if (!entry->isLocked()) {
unlockCount += 1;
}
entry = entry->fNext;
}
GrAssert(count == fEntryCount - fClientDetachedCount);
GrAssert(bytes == fEntryBytes - fClientDetachedBytes);
GrAssert(unlockCount == fUnlockedEntryCount);
count = 0;
for (entry = fTail; entry; entry = entry->fPrev) {
count += 1;
}
GrAssert(count == fEntryCount - fClientDetachedCount);
for (int i = 0; i < count; i++) {
int matches = countMatches(fHead, fCache.getArray()[i]);
GrAssert(1 == matches);
}
}
#endif
|