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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
/*
* Copyright 2014 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "GrResourceCache2.h"
#include "GrGpuResource.h"
#include "SkGr.h"
#include "SkMessageBus.h"
DECLARE_SKMESSAGEBUS_MESSAGE(GrResourceInvalidatedMessage);
//////////////////////////////////////////////////////////////////////////////
GrResourceKey& GrResourceKey::NullScratchKey() {
static const GrCacheID::Key kBogusKey = { { {0} } };
static GrCacheID kBogusID(ScratchDomain(), kBogusKey);
static GrResourceKey kNullScratchKey(kBogusID, NoneResourceType(), 0);
return kNullScratchKey;
}
GrResourceKey::ResourceType GrResourceKey::NoneResourceType() {
static const ResourceType gNoneResourceType = GenerateResourceType();
return gNoneResourceType;
}
GrCacheID::Domain GrResourceKey::ScratchDomain() {
static const GrCacheID::Domain gDomain = GrCacheID::GenerateDomain();
return gDomain;
}
GrResourceKey::ResourceType GrResourceKey::GenerateResourceType() {
static int32_t gNextType = 0;
int32_t type = sk_atomic_inc(&gNextType);
if (type >= (1 << 8 * sizeof(ResourceType))) {
SkFAIL("Too many Resource Types");
}
return static_cast<ResourceType>(type);
}
//////////////////////////////////////////////////////////////////////////////
class GrResourceCache2::AutoValidate : ::SkNoncopyable {
public:
AutoValidate(GrResourceCache2* cache) : fCache(cache) { cache->validate(); }
~AutoValidate() { fCache->validate(); }
private:
GrResourceCache2* fCache;
};
//////////////////////////////////////////////////////////////////////////////
static const int kDefaultMaxCount = 2 * (1 << 10);
static const size_t kDefaultMaxSize = 96 * (1 << 20);
GrResourceCache2::GrResourceCache2()
: fMaxCount(kDefaultMaxCount)
, fMaxBytes(kDefaultMaxSize)
#if GR_CACHE_STATS
, fHighWaterCount(0)
, fHighWaterBytes(0)
#endif
, fCount(0)
, fBytes(0)
, fPurging(false)
, fNewlyPurgableResourceWhilePurging(false)
, fOverBudgetCB(NULL)
, fOverBudgetData(NULL) {
}
GrResourceCache2::~GrResourceCache2() {
this->releaseAll();
}
void GrResourceCache2::setLimits(int count, size_t bytes) {
fMaxCount = count;
fMaxBytes = bytes;
this->purgeAsNeeded();
}
void GrResourceCache2::insertResource(GrGpuResource* resource) {
AutoValidate av(this);
SkASSERT(resource);
SkASSERT(!resource->wasDestroyed());
SkASSERT(!this->isInCache(resource));
SkASSERT(!fPurging);
fResources.addToHead(resource);
resource->ref();
++fCount;
SkDEBUGCODE(fHighWaterCount = SkTMax(fCount, fHighWaterCount));
fBytes += resource->gpuMemorySize();
SkDEBUGCODE(fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes));
if (!resource->cacheAccess().getScratchKey().isNullScratch()) {
// TODO(bsalomon): Make this assertion possible.
// SkASSERT(!resource->isWrapped());
fScratchMap.insert(resource->cacheAccess().getScratchKey(), resource);
}
this->purgeAsNeeded();
}
void GrResourceCache2::removeResource(GrGpuResource* resource) {
AutoValidate av(this);
--fCount;
fBytes -= resource->gpuMemorySize();
SkASSERT(this->isInCache(resource));
fResources.remove(resource);
if (!resource->cacheAccess().getScratchKey().isNullScratch()) {
fScratchMap.remove(resource->cacheAccess().getScratchKey(), resource);
}
if (const GrResourceKey* contentKey = resource->cacheAccess().getContentKey()) {
fContentHash.remove(*contentKey);
}
}
void GrResourceCache2::abandonAll() {
AutoValidate av(this);
SkASSERT(!fPurging);
while (GrGpuResource* head = fResources.head()) {
SkASSERT(!head->wasDestroyed());
head->abandon();
head->unref();
// abandon should have already removed this from the list.
SkASSERT(head != fResources.head());
}
SkASSERT(!fScratchMap.count());
SkASSERT(!fContentHash.count());
SkASSERT(!fCount);
}
void GrResourceCache2::releaseAll() {
AutoValidate av(this);
SkASSERT(!fPurging);
while (GrGpuResource* head = fResources.head()) {
SkASSERT(!head->wasDestroyed());
head->release();
head->unref();
// release should have already removed this from the list.
SkASSERT(head != fResources.head());
}
SkASSERT(!fScratchMap.count());
SkASSERT(!fCount);
}
class GrResourceCache2::AvailableForScratchUse {
public:
AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendingIO) { }
bool operator()(const GrGpuResource* resource) const {
if (!resource->reffedOnlyByCache() || !resource->cacheAccess().isScratch()) {
return false;
}
return !fRejectPendingIO || !resource->internalHasPendingIO();
}
private:
bool fRejectPendingIO;
};
GrGpuResource* GrResourceCache2::findAndRefScratchResource(const GrResourceKey& scratchKey,
uint32_t flags) {
AutoValidate av(this);
SkASSERT(!fPurging);
SkASSERT(scratchKey.isScratch());
GrGpuResource* resource;
if (flags & (kPreferNoPendingIO_ScratchFlag | kRequireNoPendingIO_ScratchFlag)) {
resource = fScratchMap.find(scratchKey, AvailableForScratchUse(true));
if (resource) {
this->makeResourceMRU(resource);
return SkRef(resource);
} else if (flags & kRequireNoPendingIO_ScratchFlag) {
return NULL;
}
// TODO: fail here when kPrefer is specified, we didn't find a resource without pending io,
// but there is still space in our budget for the resource.
}
resource = fScratchMap.find(scratchKey, AvailableForScratchUse(false));
if (resource) {
resource->ref();
this->makeResourceMRU(resource);
}
return resource;
}
bool GrResourceCache2::didSetContentKey(GrGpuResource* resource) {
SkASSERT(!fPurging);
SkASSERT(resource);
SkASSERT(this->isInCache(resource));
SkASSERT(resource->cacheAccess().getContentKey());
SkASSERT(!resource->cacheAccess().getContentKey()->isScratch());
GrGpuResource* res = fContentHash.find(*resource->cacheAccess().getContentKey());
if (NULL != res) {
return false;
}
fContentHash.add(resource);
this->validate();
return true;
}
void GrResourceCache2::makeResourceMRU(GrGpuResource* resource) {
AutoValidate av(this);
SkASSERT(!fPurging);
SkASSERT(resource);
SkASSERT(this->isInCache(resource));
fResources.remove(resource);
fResources.addToHead(resource);
}
void GrResourceCache2::notifyPurgable(const GrGpuResource* resource) {
SkASSERT(resource);
SkASSERT(this->isInCache(resource));
SkASSERT(resource->isPurgable());
// We can't purge if in the middle of purging because purge is iterating. Instead record
// that additional resources became purgable.
if (fPurging) {
fNewlyPurgableResourceWhilePurging = true;
return;
}
// Purge the resource if we're over budget
bool overBudget = fCount > fMaxCount || fBytes > fMaxBytes;
// We should not be over budget here unless all resources are unpuragble.
#ifdef SK_DEBUG
if (overBudget) {
ResourceList::Iter iter;
GrGpuResource* r = iter.init(fResources, ResourceList::Iter::kHead_IterStart);
for ( ; r; r = iter.next()) {
SkASSERT(r == resource || !r->isPurgable());
}
}
#endif
// Also purge if the resource has neither a valid scratch key nor a content key.
bool noKey = !resource->cacheAccess().isScratch() &&
(NULL == resource->cacheAccess().getContentKey());
if (overBudget || noKey) {
SkDEBUGCODE(int beforeCount = fCount;)
resource->unref();
// We should at least have freed resource. It may have in turn freed other resources.
SkASSERT(fCount < beforeCount);
}
this->validate();
}
void GrResourceCache2::didChangeGpuMemorySize(const GrGpuResource* resource, size_t oldSize) {
// SkASSERT(!fPurging); GrPathRange increases size during flush. :(
SkASSERT(resource);
SkASSERT(this->isInCache(resource));
fBytes += resource->gpuMemorySize() - oldSize;
SkDEBUGCODE(fHighWaterBytes = SkTMax(fBytes, fHighWaterBytes));
this->purgeAsNeeded();
this->validate();
}
void GrResourceCache2::internalPurgeAsNeeded() {
SkASSERT(!fPurging);
SkASSERT(!fNewlyPurgableResourceWhilePurging);
SkASSERT(fCount > fMaxCount || fBytes > fMaxBytes);
fPurging = true;
AutoValidate av(this); // Put this after setting fPurging so we're allowed to be over budget.
bool overBudget = true;
do {
fNewlyPurgableResourceWhilePurging = false;
ResourceList::Iter resourceIter;
GrGpuResource* resource = resourceIter.init(fResources,
ResourceList::Iter::kTail_IterStart);
while (resource) {
GrGpuResource* prev = resourceIter.prev();
if (resource->isPurgable()) {
resource->unref();
}
resource = prev;
if (fCount <= fMaxCount && fBytes <= fMaxBytes) {
overBudget = false;
resource = NULL;
}
}
if (!fNewlyPurgableResourceWhilePurging && overBudget && fOverBudgetCB) {
// Despite the purge we're still over budget. Call our over budget callback.
(*fOverBudgetCB)(fOverBudgetData);
}
} while (overBudget && fNewlyPurgableResourceWhilePurging);
fNewlyPurgableResourceWhilePurging = false;
fPurging = false;
}
void GrResourceCache2::purgeAllUnlocked() {
SkASSERT(!fPurging);
SkASSERT(!fNewlyPurgableResourceWhilePurging);
fPurging = true;
AutoValidate av(this); // Put this after setting fPurging so we're allowed to be over budget.
do {
fNewlyPurgableResourceWhilePurging = false;
ResourceList::Iter resourceIter;
GrGpuResource* resource =
resourceIter.init(fResources, ResourceList::Iter::kTail_IterStart);
while (resource) {
GrGpuResource* prev = resourceIter.prev();
if (resource->isPurgable()) {
resource->unref();
}
resource = prev;
}
if (!fNewlyPurgableResourceWhilePurging && fCount && fOverBudgetCB) {
(*fOverBudgetCB)(fOverBudgetData);
}
} while (fNewlyPurgableResourceWhilePurging);
fPurging = false;
}
#ifdef SK_DEBUG
void GrResourceCache2::validate() const {
size_t bytes = 0;
int count = 0;
int locked = 0;
int scratch = 0;
int couldBeScratch = 0;
int content = 0;
ResourceList::Iter iter;
GrGpuResource* resource = iter.init(fResources, ResourceList::Iter::kHead_IterStart);
for ( ; resource; resource = iter.next()) {
bytes += resource->gpuMemorySize();
++count;
if (!resource->isPurgable()) {
++locked;
}
if (resource->cacheAccess().isScratch()) {
SkASSERT(NULL == resource->cacheAccess().getContentKey());
++scratch;
SkASSERT(fScratchMap.countForKey(resource->cacheAccess().getScratchKey()));
} else if (!resource->cacheAccess().getScratchKey().isNullScratch()) {
SkASSERT(NULL != resource->cacheAccess().getContentKey());
++couldBeScratch;
SkASSERT(fScratchMap.countForKey(resource->cacheAccess().getScratchKey()));
}
if (const GrResourceKey* contentKey = resource->cacheAccess().getContentKey()) {
++content;
SkASSERT(fContentHash.find(*contentKey) == resource);
}
}
SkASSERT(bytes == fBytes);
SkASSERT(count == fCount);
#if GR_CACHE_STATS
SkASSERT(bytes <= fHighWaterBytes);
SkASSERT(count <= fHighWaterCount);
#endif
SkASSERT(content == fContentHash.count());
SkASSERT(scratch + couldBeScratch == fScratchMap.count());
bool overBudget = bytes > fMaxBytes || count > fMaxCount;
SkASSERT(!overBudget || locked == count || fPurging);
}
#endif
#if GR_CACHE_STATS
void GrResourceCache2::printStats() const {
this->validate();
int locked = 0;
int scratch = 0;
ResourceList::Iter iter;
GrGpuResource* resource = iter.init(fResources, ResourceList::Iter::kHead_IterStart);
for ( ; resource; resource = iter.next()) {
if (!resource->isPurgable()) {
++locked;
}
if (resource->cacheAccess().isScratch()) {
++scratch;
}
}
float countUtilization = (100.f * fCount) / fMaxCount;
float byteUtilization = (100.f * fBytes) / fMaxBytes;
SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes);
SkDebugf("\t\tEntry Count: current %d (%d locked, %d scratch %.2g%% full), high %d\n",
fCount, locked, scratch, countUtilization, fHighWaterCount);
SkDebugf("\t\tEntry Bytes: current %d (%.2g%% full) high %d\n",
fBytes, byteUtilization, fHighWaterBytes);
}
#endif
|