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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
|
/*
* Copyright 2013 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkChecksum.h"
#include "SkMessageBus.h"
#include "SkMipMap.h"
#include "SkPixelRef.h"
#include "SkResourceCache.h"
#include <stddef.h>
DECLARE_SKMESSAGEBUS_MESSAGE(SkResourceCache::PurgeSharedIDMessage)
// This can be defined by the caller's build system
//#define SK_USE_DISCARDABLE_SCALEDIMAGECACHE
#ifndef SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT
# define SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT 1024
#endif
#ifndef SK_DEFAULT_IMAGE_CACHE_LIMIT
#define SK_DEFAULT_IMAGE_CACHE_LIMIT (2 * 1024 * 1024)
#endif
void SkResourceCache::Key::init(void* nameSpace, uint64_t sharedID, size_t length) {
SkASSERT(SkAlign4(length) == length);
// fCount32 and fHash are not hashed
static const int kUnhashedLocal32s = 2; // fCache32 + fHash
static const int kSharedIDLocal32s = 2; // fSharedID_lo + fSharedID_hi
static const int kHashedLocal32s = kSharedIDLocal32s + (sizeof(fNamespace) >> 2);
static const int kLocal32s = kUnhashedLocal32s + kHashedLocal32s;
SK_COMPILE_ASSERT(sizeof(Key) == (kLocal32s << 2), unaccounted_key_locals);
SK_COMPILE_ASSERT(sizeof(Key) == offsetof(Key, fNamespace) + sizeof(fNamespace),
namespace_field_must_be_last);
fCount32 = SkToS32(kLocal32s + (length >> 2));
fSharedID_lo = (uint32_t)sharedID;
fSharedID_hi = (uint32_t)(sharedID >> 32);
fNamespace = nameSpace;
// skip unhashed fields when computing the murmur
fHash = SkChecksum::Murmur3(this->as32() + kUnhashedLocal32s,
(fCount32 - kUnhashedLocal32s) << 2);
}
#include "SkTDynamicHash.h"
class SkResourceCache::Hash :
public SkTDynamicHash<SkResourceCache::Rec, SkResourceCache::Key> {};
///////////////////////////////////////////////////////////////////////////////
void SkResourceCache::init() {
fHead = NULL;
fTail = NULL;
fHash = new Hash;
fTotalBytesUsed = 0;
fCount = 0;
fSingleAllocationByteLimit = 0;
fAllocator = NULL;
// One of these should be explicit set by the caller after we return.
fTotalByteLimit = 0;
fDiscardableFactory = NULL;
}
#include "SkDiscardableMemory.h"
class SkOneShotDiscardablePixelRef : public SkPixelRef {
public:
SK_DECLARE_INST_COUNT(SkOneShotDiscardablePixelRef)
// Ownership of the discardablememory is transfered to the pixelref
SkOneShotDiscardablePixelRef(const SkImageInfo&, SkDiscardableMemory*, size_t rowBytes);
~SkOneShotDiscardablePixelRef();
protected:
bool onNewLockPixels(LockRec*) SK_OVERRIDE;
void onUnlockPixels() SK_OVERRIDE;
size_t getAllocatedSizeInBytes() const SK_OVERRIDE;
private:
SkDiscardableMemory* fDM;
size_t fRB;
bool fFirstTime;
typedef SkPixelRef INHERITED;
};
SkOneShotDiscardablePixelRef::SkOneShotDiscardablePixelRef(const SkImageInfo& info,
SkDiscardableMemory* dm,
size_t rowBytes)
: INHERITED(info)
, fDM(dm)
, fRB(rowBytes)
{
SkASSERT(dm->data());
fFirstTime = true;
}
SkOneShotDiscardablePixelRef::~SkOneShotDiscardablePixelRef() {
SkDELETE(fDM);
}
bool SkOneShotDiscardablePixelRef::onNewLockPixels(LockRec* rec) {
if (fFirstTime) {
// we're already locked
SkASSERT(fDM->data());
fFirstTime = false;
goto SUCCESS;
}
// A previous call to onUnlock may have deleted our DM, so check for that
if (NULL == fDM) {
return false;
}
if (!fDM->lock()) {
// since it failed, we delete it now, to free-up the resource
delete fDM;
fDM = NULL;
return false;
}
SUCCESS:
rec->fPixels = fDM->data();
rec->fColorTable = NULL;
rec->fRowBytes = fRB;
return true;
}
void SkOneShotDiscardablePixelRef::onUnlockPixels() {
SkASSERT(!fFirstTime);
fDM->unlock();
}
size_t SkOneShotDiscardablePixelRef::getAllocatedSizeInBytes() const {
return this->info().getSafeSize(fRB);
}
class SkResourceCacheDiscardableAllocator : public SkBitmap::Allocator {
public:
SkResourceCacheDiscardableAllocator(SkResourceCache::DiscardableFactory factory) {
SkASSERT(factory);
fFactory = factory;
}
bool allocPixelRef(SkBitmap*, SkColorTable*) SK_OVERRIDE;
private:
SkResourceCache::DiscardableFactory fFactory;
};
bool SkResourceCacheDiscardableAllocator::allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) {
size_t size = bitmap->getSize();
uint64_t size64 = bitmap->computeSize64();
if (0 == size || size64 > (uint64_t)size) {
return false;
}
SkDiscardableMemory* dm = fFactory(size);
if (NULL == dm) {
return false;
}
// can we relax this?
if (kN32_SkColorType != bitmap->colorType()) {
return false;
}
SkImageInfo info = bitmap->info();
bitmap->setPixelRef(SkNEW_ARGS(SkOneShotDiscardablePixelRef,
(info, dm, bitmap->rowBytes())))->unref();
bitmap->lockPixels();
return bitmap->readyToDraw();
}
SkResourceCache::SkResourceCache(DiscardableFactory factory) {
this->init();
fDiscardableFactory = factory;
fAllocator = SkNEW_ARGS(SkResourceCacheDiscardableAllocator, (factory));
}
SkResourceCache::SkResourceCache(size_t byteLimit) {
this->init();
fTotalByteLimit = byteLimit;
}
SkResourceCache::~SkResourceCache() {
SkSafeUnref(fAllocator);
Rec* rec = fHead;
while (rec) {
Rec* next = rec->fNext;
SkDELETE(rec);
rec = next;
}
delete fHash;
}
////////////////////////////////////////////////////////////////////////////////
bool SkResourceCache::find(const Key& key, FindVisitor visitor, void* context) {
this->checkMessages();
Rec* rec = fHash->find(key);
if (rec) {
if (visitor(*rec, context)) {
this->moveToHead(rec); // for our LRU
return true;
} else {
this->remove(rec); // stale
return false;
}
}
return false;
}
static void make_size_str(size_t size, SkString* str) {
const char suffix[] = { 'b', 'k', 'm', 'g', 't', 0 };
int i = 0;
while (suffix[i] && (size > 1024)) {
i += 1;
size >>= 10;
}
str->printf("%zu%c", size, suffix[i]);
}
static bool gDumpCacheTransactions;
void SkResourceCache::add(Rec* rec) {
this->checkMessages();
SkASSERT(rec);
// See if we already have this key (racy inserts, etc.)
Rec* existing = fHash->find(rec->getKey());
if (existing) {
SkDELETE(rec);
return;
}
this->addToHead(rec);
fHash->add(rec);
if (gDumpCacheTransactions) {
SkString bytesStr, totalStr;
make_size_str(rec->bytesUsed(), &bytesStr);
make_size_str(fTotalBytesUsed, &totalStr);
SkDebugf("RC: add %5s %12p key %08x -- total %5s, count %d\n",
bytesStr.c_str(), rec, rec->getHash(), totalStr.c_str(), fCount);
}
// since the new rec may push us over-budget, we perform a purge check now
this->purgeAsNeeded();
}
void SkResourceCache::remove(Rec* rec) {
size_t used = rec->bytesUsed();
SkASSERT(used <= fTotalBytesUsed);
this->detach(rec);
fHash->remove(rec->getKey());
fTotalBytesUsed -= used;
fCount -= 1;
if (gDumpCacheTransactions) {
SkString bytesStr, totalStr;
make_size_str(used, &bytesStr);
make_size_str(fTotalBytesUsed, &totalStr);
SkDebugf("RC: remove %5s %12p key %08x -- total %5s, count %d\n",
bytesStr.c_str(), rec, rec->getHash(), totalStr.c_str(), fCount);
}
SkDELETE(rec);
}
void SkResourceCache::purgeAsNeeded(bool forcePurge) {
size_t byteLimit;
int countLimit;
if (fDiscardableFactory) {
countLimit = SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT;
byteLimit = SK_MaxU32; // no limit based on bytes
} else {
countLimit = SK_MaxS32; // no limit based on count
byteLimit = fTotalByteLimit;
}
Rec* rec = fTail;
while (rec) {
if (!forcePurge && fTotalBytesUsed < byteLimit && fCount < countLimit) {
break;
}
Rec* prev = rec->fPrev;
this->remove(rec);
rec = prev;
}
}
void SkResourceCache::purgeSharedID(uint64_t sharedID) {
if (0 == sharedID) {
return;
}
// go backwards, just like purgeAsNeeded, just to make the code similar.
// could iterate either direction and still be correct.
Rec* rec = fTail;
while (rec) {
Rec* prev = rec->fPrev;
if (rec->getKey().getSharedID() == sharedID) {
// SkDebugf("purgeSharedID id=%llx rec=%p\n", sharedID, rec);
this->remove(rec);
}
rec = prev;
}
}
size_t SkResourceCache::setTotalByteLimit(size_t newLimit) {
size_t prevLimit = fTotalByteLimit;
fTotalByteLimit = newLimit;
if (newLimit < prevLimit) {
this->purgeAsNeeded();
}
return prevLimit;
}
SkCachedData* SkResourceCache::newCachedData(size_t bytes) {
this->checkMessages();
if (fDiscardableFactory) {
SkDiscardableMemory* dm = fDiscardableFactory(bytes);
return dm ? SkNEW_ARGS(SkCachedData, (bytes, dm)) : NULL;
} else {
return SkNEW_ARGS(SkCachedData, (sk_malloc_throw(bytes), bytes));
}
}
///////////////////////////////////////////////////////////////////////////////
void SkResourceCache::detach(Rec* rec) {
Rec* prev = rec->fPrev;
Rec* next = rec->fNext;
if (!prev) {
SkASSERT(fHead == rec);
fHead = next;
} else {
prev->fNext = next;
}
if (!next) {
fTail = prev;
} else {
next->fPrev = prev;
}
rec->fNext = rec->fPrev = NULL;
}
void SkResourceCache::moveToHead(Rec* rec) {
if (fHead == rec) {
return;
}
SkASSERT(fHead);
SkASSERT(fTail);
this->validate();
this->detach(rec);
fHead->fPrev = rec;
rec->fNext = fHead;
fHead = rec;
this->validate();
}
void SkResourceCache::addToHead(Rec* rec) {
this->validate();
rec->fPrev = NULL;
rec->fNext = fHead;
if (fHead) {
fHead->fPrev = rec;
}
fHead = rec;
if (!fTail) {
fTail = rec;
}
fTotalBytesUsed += rec->bytesUsed();
fCount += 1;
this->validate();
}
///////////////////////////////////////////////////////////////////////////////
#ifdef SK_DEBUG
void SkResourceCache::validate() const {
if (NULL == fHead) {
SkASSERT(NULL == fTail);
SkASSERT(0 == fTotalBytesUsed);
return;
}
if (fHead == fTail) {
SkASSERT(NULL == fHead->fPrev);
SkASSERT(NULL == fHead->fNext);
SkASSERT(fHead->bytesUsed() == fTotalBytesUsed);
return;
}
SkASSERT(NULL == fHead->fPrev);
SkASSERT(fHead->fNext);
SkASSERT(NULL == fTail->fNext);
SkASSERT(fTail->fPrev);
size_t used = 0;
int count = 0;
const Rec* rec = fHead;
while (rec) {
count += 1;
used += rec->bytesUsed();
SkASSERT(used <= fTotalBytesUsed);
rec = rec->fNext;
}
SkASSERT(fCount == count);
rec = fTail;
while (rec) {
SkASSERT(count > 0);
count -= 1;
SkASSERT(used >= rec->bytesUsed());
used -= rec->bytesUsed();
rec = rec->fPrev;
}
SkASSERT(0 == count);
SkASSERT(0 == used);
}
#endif
void SkResourceCache::dump() const {
this->validate();
SkDebugf("SkResourceCache: count=%d bytes=%d %s\n",
fCount, fTotalBytesUsed, fDiscardableFactory ? "discardable" : "malloc");
}
size_t SkResourceCache::setSingleAllocationByteLimit(size_t newLimit) {
size_t oldLimit = fSingleAllocationByteLimit;
fSingleAllocationByteLimit = newLimit;
return oldLimit;
}
size_t SkResourceCache::getSingleAllocationByteLimit() const {
return fSingleAllocationByteLimit;
}
size_t SkResourceCache::getEffectiveSingleAllocationByteLimit() const {
// fSingleAllocationByteLimit == 0 means the caller is asking for our default
size_t limit = fSingleAllocationByteLimit;
// if we're not discardable (i.e. we are fixed-budget) then cap the single-limit
// to our budget.
if (NULL == fDiscardableFactory) {
if (0 == limit) {
limit = fTotalByteLimit;
} else {
limit = SkTMin(limit, fTotalByteLimit);
}
}
return limit;
}
void SkResourceCache::checkMessages() {
SkTArray<PurgeSharedIDMessage> msgs;
fPurgeSharedIDInbox.poll(&msgs);
for (int i = 0; i < msgs.count(); ++i) {
this->purgeSharedID(msgs[i].fSharedID);
}
}
///////////////////////////////////////////////////////////////////////////////
#include "SkThread.h"
SK_DECLARE_STATIC_MUTEX(gMutex);
static SkResourceCache* gResourceCache = NULL;
static void cleanup_gResourceCache() {
// We'll clean this up in our own tests, but disable for clients.
// Chrome seems to have funky multi-process things going on in unit tests that
// makes this unsafe to delete when the main process atexit()s.
// SkLazyPtr does the same sort of thing.
#if SK_DEVELOPER
SkDELETE(gResourceCache);
#endif
}
/** Must hold gMutex when calling. */
static SkResourceCache* get_cache() {
// gMutex is always held when this is called, so we don't need to be fancy in here.
gMutex.assertHeld();
if (NULL == gResourceCache) {
#ifdef SK_USE_DISCARDABLE_SCALEDIMAGECACHE
gResourceCache = SkNEW_ARGS(SkResourceCache, (SkDiscardableMemory::Create));
#else
gResourceCache = SkNEW_ARGS(SkResourceCache, (SK_DEFAULT_IMAGE_CACHE_LIMIT));
#endif
atexit(cleanup_gResourceCache);
}
return gResourceCache;
}
size_t SkResourceCache::GetTotalBytesUsed() {
SkAutoMutexAcquire am(gMutex);
return get_cache()->getTotalBytesUsed();
}
size_t SkResourceCache::GetTotalByteLimit() {
SkAutoMutexAcquire am(gMutex);
return get_cache()->getTotalByteLimit();
}
size_t SkResourceCache::SetTotalByteLimit(size_t newLimit) {
SkAutoMutexAcquire am(gMutex);
return get_cache()->setTotalByteLimit(newLimit);
}
SkResourceCache::DiscardableFactory SkResourceCache::GetDiscardableFactory() {
SkAutoMutexAcquire am(gMutex);
return get_cache()->discardableFactory();
}
SkBitmap::Allocator* SkResourceCache::GetAllocator() {
SkAutoMutexAcquire am(gMutex);
return get_cache()->allocator();
}
SkCachedData* SkResourceCache::NewCachedData(size_t bytes) {
SkAutoMutexAcquire am(gMutex);
return get_cache()->newCachedData(bytes);
}
void SkResourceCache::Dump() {
SkAutoMutexAcquire am(gMutex);
get_cache()->dump();
}
size_t SkResourceCache::SetSingleAllocationByteLimit(size_t size) {
SkAutoMutexAcquire am(gMutex);
return get_cache()->setSingleAllocationByteLimit(size);
}
size_t SkResourceCache::GetSingleAllocationByteLimit() {
SkAutoMutexAcquire am(gMutex);
return get_cache()->getSingleAllocationByteLimit();
}
size_t SkResourceCache::GetEffectiveSingleAllocationByteLimit() {
SkAutoMutexAcquire am(gMutex);
return get_cache()->getEffectiveSingleAllocationByteLimit();
}
void SkResourceCache::PurgeAll() {
SkAutoMutexAcquire am(gMutex);
return get_cache()->purgeAll();
}
bool SkResourceCache::Find(const Key& key, FindVisitor visitor, void* context) {
SkAutoMutexAcquire am(gMutex);
return get_cache()->find(key, visitor, context);
}
void SkResourceCache::Add(Rec* rec) {
SkAutoMutexAcquire am(gMutex);
get_cache()->add(rec);
}
void SkResourceCache::PostPurgeSharedID(uint64_t sharedID) {
if (sharedID) {
SkMessageBus<PurgeSharedIDMessage>::Post(PurgeSharedIDMessage(sharedID));
}
}
///////////////////////////////////////////////////////////////////////////////
#include "SkGraphics.h"
size_t SkGraphics::GetResourceCacheTotalBytesUsed() {
return SkResourceCache::GetTotalBytesUsed();
}
size_t SkGraphics::GetResourceCacheTotalByteLimit() {
return SkResourceCache::GetTotalByteLimit();
}
size_t SkGraphics::SetResourceCacheTotalByteLimit(size_t newLimit) {
return SkResourceCache::SetTotalByteLimit(newLimit);
}
size_t SkGraphics::GetResourceCacheSingleAllocationByteLimit() {
return SkResourceCache::GetSingleAllocationByteLimit();
}
size_t SkGraphics::SetResourceCacheSingleAllocationByteLimit(size_t newLimit) {
return SkResourceCache::SetSingleAllocationByteLimit(newLimit);
}
void SkGraphics::PurgeResourceCache() {
return SkResourceCache::PurgeAll();
}
|