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 "SkScaledImageCache.h" | 8 #include "SkScaledImageCache.h" |
9 #include "SkMipMap.h" | 9 #include "SkMipMap.h" |
10 #include "SkOnce.h" | 10 #include "SkOnce.h" |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 SkDELETE(fDM); | 232 SkDELETE(fDM); |
233 } | 233 } |
234 | 234 |
235 void* SkOneShotDiscardablePixelRef::onLockPixels(SkColorTable** ctable) { | 235 void* SkOneShotDiscardablePixelRef::onLockPixels(SkColorTable** ctable) { |
236 if (fFirstTime) { | 236 if (fFirstTime) { |
237 // we're already locked | 237 // we're already locked |
238 fFirstTime = false; | 238 fFirstTime = false; |
239 return fDM->data(); | 239 return fDM->data(); |
240 } | 240 } |
241 | 241 |
242 SkASSERT(!fIsLocked); | 242 // A previous call to onUnlock may have deleted our DM, so check for that |
243 fIsLocked = fDM->lock(); | 243 if (NULL == fDM) { |
244 return fIsLocked ? fDM->data() : NULL; | 244 return NULL; |
| 245 } |
| 246 |
| 247 if (!fDM->lock()) { |
| 248 // since it failed, we delete it now, to free-up the resource |
| 249 delete fDM; |
| 250 fDM = NULL; |
| 251 return NULL; |
| 252 } |
| 253 return fDM->data(); |
245 } | 254 } |
246 | 255 |
247 void SkOneShotDiscardablePixelRef::onUnlockPixels() { | 256 void SkOneShotDiscardablePixelRef::onUnlockPixels() { |
248 SkASSERT(!fFirstTime); | 257 SkASSERT(!fFirstTime); |
249 if (fIsLocked) { | 258 if (fIsLocked) { |
250 fIsLocked = false; | 259 fIsLocked = false; |
251 fDM->unlock(); | 260 fDM->unlock(); |
252 } | 261 } |
253 } | 262 } |
254 | 263 |
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
606 fHead = rec; | 615 fHead = rec; |
607 if (!fTail) { | 616 if (!fTail) { |
608 fTail = rec; | 617 fTail = rec; |
609 } | 618 } |
610 fBytesUsed += rec->bytesUsed(); | 619 fBytesUsed += rec->bytesUsed(); |
611 fCount += 1; | 620 fCount += 1; |
612 | 621 |
613 this->validate(); | 622 this->validate(); |
614 } | 623 } |
615 | 624 |
| 625 /////////////////////////////////////////////////////////////////////////////// |
| 626 |
616 #ifdef SK_DEBUG | 627 #ifdef SK_DEBUG |
617 void SkScaledImageCache::validate() const { | 628 void SkScaledImageCache::validate() const { |
618 if (NULL == fHead) { | 629 if (NULL == fHead) { |
619 SkASSERT(NULL == fTail); | 630 SkASSERT(NULL == fTail); |
620 SkASSERT(0 == fBytesUsed); | 631 SkASSERT(0 == fBytesUsed); |
621 return; | 632 return; |
622 } | 633 } |
623 | 634 |
624 if (fHead == fTail) { | 635 if (fHead == fTail) { |
625 SkASSERT(NULL == fHead->fPrev); | 636 SkASSERT(NULL == fHead->fPrev); |
(...skipping 25 matching lines...) Expand all Loading... |
651 SkASSERT(used >= rec->bytesUsed()); | 662 SkASSERT(used >= rec->bytesUsed()); |
652 used -= rec->bytesUsed(); | 663 used -= rec->bytesUsed(); |
653 rec = rec->fPrev; | 664 rec = rec->fPrev; |
654 } | 665 } |
655 | 666 |
656 SkASSERT(0 == count); | 667 SkASSERT(0 == count); |
657 SkASSERT(0 == used); | 668 SkASSERT(0 == used); |
658 } | 669 } |
659 #endif | 670 #endif |
660 | 671 |
| 672 void SkScaledImageCache::dump() const { |
| 673 this->validate(); |
| 674 |
| 675 const Rec* rec = fHead; |
| 676 int locked = 0; |
| 677 while (rec) { |
| 678 locked += rec->fLockCount > 0; |
| 679 rec = rec->fNext; |
| 680 } |
| 681 |
| 682 SkDebugf("SkScaledImageCache: count=%d bytes=%d locked=%d %s\n", |
| 683 fCount, fBytesUsed, locked, |
| 684 fDiscardableFactory ? "discardable" : "malloc"); |
| 685 } |
| 686 |
661 /////////////////////////////////////////////////////////////////////////////// | 687 /////////////////////////////////////////////////////////////////////////////// |
662 | 688 |
663 #include "SkThread.h" | 689 #include "SkThread.h" |
664 | 690 |
665 SK_DECLARE_STATIC_MUTEX(gMutex); | 691 SK_DECLARE_STATIC_MUTEX(gMutex); |
666 | 692 |
667 static void create_cache(SkScaledImageCache** cache) { | 693 static void create_cache(SkScaledImageCache** cache) { |
668 #ifdef SK_USE_DISCARDABLE_SCALEDIMAGECACHE | 694 #ifdef SK_USE_DISCARDABLE_SCALEDIMAGECACHE |
669 *cache = SkNEW_ARGS(SkScaledImageCache, (SkDiscardableMemory::Create)); | 695 *cache = SkNEW_ARGS(SkScaledImageCache, (SkDiscardableMemory::Create)); |
670 #else | 696 #else |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
723 } | 749 } |
724 | 750 |
725 SkScaledImageCache::ID* SkScaledImageCache::AddAndLockMip(const SkBitmap& orig, | 751 SkScaledImageCache::ID* SkScaledImageCache::AddAndLockMip(const SkBitmap& orig, |
726 const SkMipMap* mip) { | 752 const SkMipMap* mip) { |
727 SkAutoMutexAcquire am(gMutex); | 753 SkAutoMutexAcquire am(gMutex); |
728 return get_cache()->addAndLockMip(orig, mip); | 754 return get_cache()->addAndLockMip(orig, mip); |
729 } | 755 } |
730 | 756 |
731 void SkScaledImageCache::Unlock(SkScaledImageCache::ID* id) { | 757 void SkScaledImageCache::Unlock(SkScaledImageCache::ID* id) { |
732 SkAutoMutexAcquire am(gMutex); | 758 SkAutoMutexAcquire am(gMutex); |
733 return get_cache()->unlock(id); | 759 get_cache()->unlock(id); |
| 760 |
| 761 // get_cache()->dump(); |
734 } | 762 } |
735 | 763 |
736 size_t SkScaledImageCache::GetBytesUsed() { | 764 size_t SkScaledImageCache::GetBytesUsed() { |
737 SkAutoMutexAcquire am(gMutex); | 765 SkAutoMutexAcquire am(gMutex); |
738 return get_cache()->getBytesUsed(); | 766 return get_cache()->getBytesUsed(); |
739 } | 767 } |
740 | 768 |
741 size_t SkScaledImageCache::GetByteLimit() { | 769 size_t SkScaledImageCache::GetByteLimit() { |
742 SkAutoMutexAcquire am(gMutex); | 770 SkAutoMutexAcquire am(gMutex); |
743 return get_cache()->getByteLimit(); | 771 return get_cache()->getByteLimit(); |
744 } | 772 } |
745 | 773 |
746 size_t SkScaledImageCache::SetByteLimit(size_t newLimit) { | 774 size_t SkScaledImageCache::SetByteLimit(size_t newLimit) { |
747 SkAutoMutexAcquire am(gMutex); | 775 SkAutoMutexAcquire am(gMutex); |
748 return get_cache()->setByteLimit(newLimit); | 776 return get_cache()->setByteLimit(newLimit); |
749 } | 777 } |
750 | 778 |
751 SkBitmap::Allocator* SkScaledImageCache::GetAllocator() { | 779 SkBitmap::Allocator* SkScaledImageCache::GetAllocator() { |
752 SkAutoMutexAcquire am(gMutex); | 780 SkAutoMutexAcquire am(gMutex); |
753 return get_cache()->allocator(); | 781 return get_cache()->allocator(); |
754 } | 782 } |
755 | 783 |
| 784 void SkScaledImageCache::Dump() { |
| 785 SkAutoMutexAcquire am(gMutex); |
| 786 get_cache()->dump(); |
| 787 } |
| 788 |
756 /////////////////////////////////////////////////////////////////////////////// | 789 /////////////////////////////////////////////////////////////////////////////// |
757 | 790 |
758 #include "SkGraphics.h" | 791 #include "SkGraphics.h" |
759 | 792 |
760 size_t SkGraphics::GetImageCacheBytesUsed() { | 793 size_t SkGraphics::GetImageCacheBytesUsed() { |
761 return SkScaledImageCache::GetBytesUsed(); | 794 return SkScaledImageCache::GetBytesUsed(); |
762 } | 795 } |
763 | 796 |
764 size_t SkGraphics::GetImageCacheByteLimit() { | 797 size_t SkGraphics::GetImageCacheByteLimit() { |
765 return SkScaledImageCache::GetByteLimit(); | 798 return SkScaledImageCache::GetByteLimit(); |
766 } | 799 } |
767 | 800 |
768 size_t SkGraphics::SetImageCacheByteLimit(size_t newLimit) { | 801 size_t SkGraphics::SetImageCacheByteLimit(size_t newLimit) { |
769 return SkScaledImageCache::SetByteLimit(newLimit); | 802 return SkScaledImageCache::SetByteLimit(newLimit); |
770 } | 803 } |
OLD | NEW |