OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkChecksum.h" | 8 #include "SkChecksum.h" |
9 #include "SkMessageBus.h" | 9 #include "SkMessageBus.h" |
10 #include "SkMipMap.h" | 10 #include "SkMipMap.h" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 | 52 |
53 #include "SkTDynamicHash.h" | 53 #include "SkTDynamicHash.h" |
54 | 54 |
55 class SkResourceCache::Hash : | 55 class SkResourceCache::Hash : |
56 public SkTDynamicHash<SkResourceCache::Rec, SkResourceCache::Key> {}; | 56 public SkTDynamicHash<SkResourceCache::Rec, SkResourceCache::Key> {}; |
57 | 57 |
58 | 58 |
59 /////////////////////////////////////////////////////////////////////////////// | 59 /////////////////////////////////////////////////////////////////////////////// |
60 | 60 |
61 void SkResourceCache::init() { | 61 void SkResourceCache::init() { |
62 fHead = NULL; | 62 fHead = nullptr; |
63 fTail = NULL; | 63 fTail = nullptr; |
64 fHash = new Hash; | 64 fHash = new Hash; |
65 fTotalBytesUsed = 0; | 65 fTotalBytesUsed = 0; |
66 fCount = 0; | 66 fCount = 0; |
67 fSingleAllocationByteLimit = 0; | 67 fSingleAllocationByteLimit = 0; |
68 fAllocator = NULL; | 68 fAllocator = nullptr; |
69 | 69 |
70 // One of these should be explicit set by the caller after we return. | 70 // One of these should be explicit set by the caller after we return. |
71 fTotalByteLimit = 0; | 71 fTotalByteLimit = 0; |
72 fDiscardableFactory = NULL; | 72 fDiscardableFactory = nullptr; |
73 } | 73 } |
74 | 74 |
75 #include "SkDiscardableMemory.h" | 75 #include "SkDiscardableMemory.h" |
76 | 76 |
77 class SkOneShotDiscardablePixelRef : public SkPixelRef { | 77 class SkOneShotDiscardablePixelRef : public SkPixelRef { |
78 public: | 78 public: |
79 | 79 |
80 // Ownership of the discardablememory is transfered to the pixelref | 80 // Ownership of the discardablememory is transfered to the pixelref |
81 SkOneShotDiscardablePixelRef(const SkImageInfo&, SkDiscardableMemory*, size_
t rowBytes); | 81 SkOneShotDiscardablePixelRef(const SkImageInfo&, SkDiscardableMemory*, size_
t rowBytes); |
82 ~SkOneShotDiscardablePixelRef(); | 82 ~SkOneShotDiscardablePixelRef(); |
(...skipping 26 matching lines...) Expand all Loading... |
109 | 109 |
110 bool SkOneShotDiscardablePixelRef::onNewLockPixels(LockRec* rec) { | 110 bool SkOneShotDiscardablePixelRef::onNewLockPixels(LockRec* rec) { |
111 if (fFirstTime) { | 111 if (fFirstTime) { |
112 // we're already locked | 112 // we're already locked |
113 SkASSERT(fDM->data()); | 113 SkASSERT(fDM->data()); |
114 fFirstTime = false; | 114 fFirstTime = false; |
115 goto SUCCESS; | 115 goto SUCCESS; |
116 } | 116 } |
117 | 117 |
118 // A previous call to onUnlock may have deleted our DM, so check for that | 118 // A previous call to onUnlock may have deleted our DM, so check for that |
119 if (NULL == fDM) { | 119 if (nullptr == fDM) { |
120 return false; | 120 return false; |
121 } | 121 } |
122 | 122 |
123 if (!fDM->lock()) { | 123 if (!fDM->lock()) { |
124 // since it failed, we delete it now, to free-up the resource | 124 // since it failed, we delete it now, to free-up the resource |
125 delete fDM; | 125 delete fDM; |
126 fDM = NULL; | 126 fDM = nullptr; |
127 return false; | 127 return false; |
128 } | 128 } |
129 | 129 |
130 SUCCESS: | 130 SUCCESS: |
131 rec->fPixels = fDM->data(); | 131 rec->fPixels = fDM->data(); |
132 rec->fColorTable = NULL; | 132 rec->fColorTable = nullptr; |
133 rec->fRowBytes = fRB; | 133 rec->fRowBytes = fRB; |
134 return true; | 134 return true; |
135 } | 135 } |
136 | 136 |
137 void SkOneShotDiscardablePixelRef::onUnlockPixels() { | 137 void SkOneShotDiscardablePixelRef::onUnlockPixels() { |
138 SkASSERT(!fFirstTime); | 138 SkASSERT(!fFirstTime); |
139 fDM->unlock(); | 139 fDM->unlock(); |
140 } | 140 } |
141 | 141 |
142 size_t SkOneShotDiscardablePixelRef::getAllocatedSizeInBytes() const { | 142 size_t SkOneShotDiscardablePixelRef::getAllocatedSizeInBytes() const { |
(...skipping 14 matching lines...) Expand all Loading... |
157 }; | 157 }; |
158 | 158 |
159 bool SkResourceCacheDiscardableAllocator::allocPixelRef(SkBitmap* bitmap, SkColo
rTable* ctable) { | 159 bool SkResourceCacheDiscardableAllocator::allocPixelRef(SkBitmap* bitmap, SkColo
rTable* ctable) { |
160 size_t size = bitmap->getSize(); | 160 size_t size = bitmap->getSize(); |
161 uint64_t size64 = bitmap->computeSize64(); | 161 uint64_t size64 = bitmap->computeSize64(); |
162 if (0 == size || size64 > (uint64_t)size) { | 162 if (0 == size || size64 > (uint64_t)size) { |
163 return false; | 163 return false; |
164 } | 164 } |
165 | 165 |
166 SkDiscardableMemory* dm = fFactory(size); | 166 SkDiscardableMemory* dm = fFactory(size); |
167 if (NULL == dm) { | 167 if (nullptr == dm) { |
168 return false; | 168 return false; |
169 } | 169 } |
170 | 170 |
171 // can we relax this? | 171 // can we relax this? |
172 if (kN32_SkColorType != bitmap->colorType()) { | 172 if (kN32_SkColorType != bitmap->colorType()) { |
173 return false; | 173 return false; |
174 } | 174 } |
175 | 175 |
176 SkImageInfo info = bitmap->info(); | 176 SkImageInfo info = bitmap->info(); |
177 bitmap->setPixelRef(new SkOneShotDiscardablePixelRef(info, dm, bitmap->rowBy
tes()))->unref(); | 177 bitmap->setPixelRef(new SkOneShotDiscardablePixelRef(info, dm, bitmap->rowBy
tes()))->unref(); |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 this->purgeAsNeeded(); | 364 this->purgeAsNeeded(); |
365 } | 365 } |
366 return prevLimit; | 366 return prevLimit; |
367 } | 367 } |
368 | 368 |
369 SkCachedData* SkResourceCache::newCachedData(size_t bytes) { | 369 SkCachedData* SkResourceCache::newCachedData(size_t bytes) { |
370 this->checkMessages(); | 370 this->checkMessages(); |
371 | 371 |
372 if (fDiscardableFactory) { | 372 if (fDiscardableFactory) { |
373 SkDiscardableMemory* dm = fDiscardableFactory(bytes); | 373 SkDiscardableMemory* dm = fDiscardableFactory(bytes); |
374 return dm ? new SkCachedData(bytes, dm) : NULL; | 374 return dm ? new SkCachedData(bytes, dm) : nullptr; |
375 } else { | 375 } else { |
376 return new SkCachedData(sk_malloc_throw(bytes), bytes); | 376 return new SkCachedData(sk_malloc_throw(bytes), bytes); |
377 } | 377 } |
378 } | 378 } |
379 | 379 |
380 /////////////////////////////////////////////////////////////////////////////// | 380 /////////////////////////////////////////////////////////////////////////////// |
381 | 381 |
382 void SkResourceCache::detach(Rec* rec) { | 382 void SkResourceCache::detach(Rec* rec) { |
383 Rec* prev = rec->fPrev; | 383 Rec* prev = rec->fPrev; |
384 Rec* next = rec->fNext; | 384 Rec* next = rec->fNext; |
385 | 385 |
386 if (!prev) { | 386 if (!prev) { |
387 SkASSERT(fHead == rec); | 387 SkASSERT(fHead == rec); |
388 fHead = next; | 388 fHead = next; |
389 } else { | 389 } else { |
390 prev->fNext = next; | 390 prev->fNext = next; |
391 } | 391 } |
392 | 392 |
393 if (!next) { | 393 if (!next) { |
394 fTail = prev; | 394 fTail = prev; |
395 } else { | 395 } else { |
396 next->fPrev = prev; | 396 next->fPrev = prev; |
397 } | 397 } |
398 | 398 |
399 rec->fNext = rec->fPrev = NULL; | 399 rec->fNext = rec->fPrev = nullptr; |
400 } | 400 } |
401 | 401 |
402 void SkResourceCache::moveToHead(Rec* rec) { | 402 void SkResourceCache::moveToHead(Rec* rec) { |
403 if (fHead == rec) { | 403 if (fHead == rec) { |
404 return; | 404 return; |
405 } | 405 } |
406 | 406 |
407 SkASSERT(fHead); | 407 SkASSERT(fHead); |
408 SkASSERT(fTail); | 408 SkASSERT(fTail); |
409 | 409 |
410 this->validate(); | 410 this->validate(); |
411 | 411 |
412 this->detach(rec); | 412 this->detach(rec); |
413 | 413 |
414 fHead->fPrev = rec; | 414 fHead->fPrev = rec; |
415 rec->fNext = fHead; | 415 rec->fNext = fHead; |
416 fHead = rec; | 416 fHead = rec; |
417 | 417 |
418 this->validate(); | 418 this->validate(); |
419 } | 419 } |
420 | 420 |
421 void SkResourceCache::addToHead(Rec* rec) { | 421 void SkResourceCache::addToHead(Rec* rec) { |
422 this->validate(); | 422 this->validate(); |
423 | 423 |
424 rec->fPrev = NULL; | 424 rec->fPrev = nullptr; |
425 rec->fNext = fHead; | 425 rec->fNext = fHead; |
426 if (fHead) { | 426 if (fHead) { |
427 fHead->fPrev = rec; | 427 fHead->fPrev = rec; |
428 } | 428 } |
429 fHead = rec; | 429 fHead = rec; |
430 if (!fTail) { | 430 if (!fTail) { |
431 fTail = rec; | 431 fTail = rec; |
432 } | 432 } |
433 fTotalBytesUsed += rec->bytesUsed(); | 433 fTotalBytesUsed += rec->bytesUsed(); |
434 fCount += 1; | 434 fCount += 1; |
435 | 435 |
436 this->validate(); | 436 this->validate(); |
437 } | 437 } |
438 | 438 |
439 /////////////////////////////////////////////////////////////////////////////// | 439 /////////////////////////////////////////////////////////////////////////////// |
440 | 440 |
441 #ifdef SK_DEBUG | 441 #ifdef SK_DEBUG |
442 void SkResourceCache::validate() const { | 442 void SkResourceCache::validate() const { |
443 if (NULL == fHead) { | 443 if (nullptr == fHead) { |
444 SkASSERT(NULL == fTail); | 444 SkASSERT(nullptr == fTail); |
445 SkASSERT(0 == fTotalBytesUsed); | 445 SkASSERT(0 == fTotalBytesUsed); |
446 return; | 446 return; |
447 } | 447 } |
448 | 448 |
449 if (fHead == fTail) { | 449 if (fHead == fTail) { |
450 SkASSERT(NULL == fHead->fPrev); | 450 SkASSERT(nullptr == fHead->fPrev); |
451 SkASSERT(NULL == fHead->fNext); | 451 SkASSERT(nullptr == fHead->fNext); |
452 SkASSERT(fHead->bytesUsed() == fTotalBytesUsed); | 452 SkASSERT(fHead->bytesUsed() == fTotalBytesUsed); |
453 return; | 453 return; |
454 } | 454 } |
455 | 455 |
456 SkASSERT(NULL == fHead->fPrev); | 456 SkASSERT(nullptr == fHead->fPrev); |
457 SkASSERT(fHead->fNext); | 457 SkASSERT(fHead->fNext); |
458 SkASSERT(NULL == fTail->fNext); | 458 SkASSERT(nullptr == fTail->fNext); |
459 SkASSERT(fTail->fPrev); | 459 SkASSERT(fTail->fPrev); |
460 | 460 |
461 size_t used = 0; | 461 size_t used = 0; |
462 int count = 0; | 462 int count = 0; |
463 const Rec* rec = fHead; | 463 const Rec* rec = fHead; |
464 while (rec) { | 464 while (rec) { |
465 count += 1; | 465 count += 1; |
466 used += rec->bytesUsed(); | 466 used += rec->bytesUsed(); |
467 SkASSERT(used <= fTotalBytesUsed); | 467 SkASSERT(used <= fTotalBytesUsed); |
468 rec = rec->fNext; | 468 rec = rec->fNext; |
(...skipping 30 matching lines...) Expand all Loading... |
499 size_t SkResourceCache::getSingleAllocationByteLimit() const { | 499 size_t SkResourceCache::getSingleAllocationByteLimit() const { |
500 return fSingleAllocationByteLimit; | 500 return fSingleAllocationByteLimit; |
501 } | 501 } |
502 | 502 |
503 size_t SkResourceCache::getEffectiveSingleAllocationByteLimit() const { | 503 size_t SkResourceCache::getEffectiveSingleAllocationByteLimit() const { |
504 // fSingleAllocationByteLimit == 0 means the caller is asking for our defaul
t | 504 // fSingleAllocationByteLimit == 0 means the caller is asking for our defaul
t |
505 size_t limit = fSingleAllocationByteLimit; | 505 size_t limit = fSingleAllocationByteLimit; |
506 | 506 |
507 // if we're not discardable (i.e. we are fixed-budget) then cap the single-l
imit | 507 // if we're not discardable (i.e. we are fixed-budget) then cap the single-l
imit |
508 // to our budget. | 508 // to our budget. |
509 if (NULL == fDiscardableFactory) { | 509 if (nullptr == fDiscardableFactory) { |
510 if (0 == limit) { | 510 if (0 == limit) { |
511 limit = fTotalByteLimit; | 511 limit = fTotalByteLimit; |
512 } else { | 512 } else { |
513 limit = SkTMin(limit, fTotalByteLimit); | 513 limit = SkTMin(limit, fTotalByteLimit); |
514 } | 514 } |
515 } | 515 } |
516 return limit; | 516 return limit; |
517 } | 517 } |
518 | 518 |
519 void SkResourceCache::checkMessages() { | 519 void SkResourceCache::checkMessages() { |
520 SkTArray<PurgeSharedIDMessage> msgs; | 520 SkTArray<PurgeSharedIDMessage> msgs; |
521 fPurgeSharedIDInbox.poll(&msgs); | 521 fPurgeSharedIDInbox.poll(&msgs); |
522 for (int i = 0; i < msgs.count(); ++i) { | 522 for (int i = 0; i < msgs.count(); ++i) { |
523 this->purgeSharedID(msgs[i].fSharedID); | 523 this->purgeSharedID(msgs[i].fSharedID); |
524 } | 524 } |
525 } | 525 } |
526 | 526 |
527 /////////////////////////////////////////////////////////////////////////////// | 527 /////////////////////////////////////////////////////////////////////////////// |
528 | 528 |
529 SK_DECLARE_STATIC_MUTEX(gMutex); | 529 SK_DECLARE_STATIC_MUTEX(gMutex); |
530 static SkResourceCache* gResourceCache = NULL; | 530 static SkResourceCache* gResourceCache = nullptr; |
531 static void cleanup_gResourceCache() { | 531 static void cleanup_gResourceCache() { |
532 // We'll clean this up in our own tests, but disable for clients. | 532 // We'll clean this up in our own tests, but disable for clients. |
533 // Chrome seems to have funky multi-process things going on in unit tests th
at | 533 // Chrome seems to have funky multi-process things going on in unit tests th
at |
534 // makes this unsafe to delete when the main process atexit()s. | 534 // makes this unsafe to delete when the main process atexit()s. |
535 // SkLazyPtr does the same sort of thing. | 535 // SkLazyPtr does the same sort of thing. |
536 #if SK_DEVELOPER | 536 #if SK_DEVELOPER |
537 delete gResourceCache; | 537 delete gResourceCache; |
538 #endif | 538 #endif |
539 } | 539 } |
540 | 540 |
541 /** Must hold gMutex when calling. */ | 541 /** Must hold gMutex when calling. */ |
542 static SkResourceCache* get_cache() { | 542 static SkResourceCache* get_cache() { |
543 // gMutex is always held when this is called, so we don't need to be fancy i
n here. | 543 // gMutex is always held when this is called, so we don't need to be fancy i
n here. |
544 gMutex.assertHeld(); | 544 gMutex.assertHeld(); |
545 if (NULL == gResourceCache) { | 545 if (nullptr == gResourceCache) { |
546 #ifdef SK_USE_DISCARDABLE_SCALEDIMAGECACHE | 546 #ifdef SK_USE_DISCARDABLE_SCALEDIMAGECACHE |
547 gResourceCache = new SkResourceCache(SkDiscardableMemory::Create); | 547 gResourceCache = new SkResourceCache(SkDiscardableMemory::Create); |
548 #else | 548 #else |
549 gResourceCache = new SkResourceCache(SK_DEFAULT_IMAGE_CACHE_LIMIT); | 549 gResourceCache = new SkResourceCache(SK_DEFAULT_IMAGE_CACHE_LIMIT); |
550 #endif | 550 #endif |
551 atexit(cleanup_gResourceCache); | 551 atexit(cleanup_gResourceCache); |
552 } | 552 } |
553 return gResourceCache; | 553 return gResourceCache; |
554 } | 554 } |
555 | 555 |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
662 ///////////// | 662 ///////////// |
663 | 663 |
664 static void dump_visitor(const SkResourceCache::Rec& rec, void*) { | 664 static void dump_visitor(const SkResourceCache::Rec& rec, void*) { |
665 SkDebugf("RC: %12s bytes %9lu discardable %p\n", | 665 SkDebugf("RC: %12s bytes %9lu discardable %p\n", |
666 rec.getCategory(), rec.bytesUsed(), rec.diagnostic_only_getDiscarda
ble()); | 666 rec.getCategory(), rec.bytesUsed(), rec.diagnostic_only_getDiscarda
ble()); |
667 } | 667 } |
668 | 668 |
669 void SkResourceCache::TestDumpMemoryStatistics() { | 669 void SkResourceCache::TestDumpMemoryStatistics() { |
670 VisitAll(dump_visitor, nullptr); | 670 VisitAll(dump_visitor, nullptr); |
671 } | 671 } |
OLD | NEW |