Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1136)

Side by Side Diff: src/core/SkScaledImageCache.cpp

Issue 462993003: Set maximum output size for scaled-image-cache images (Closed) Base URL: https://skia.googlesource.com/skia.git@m37_2062
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/core/SkScaledImageCache.h ('k') | tests/ImageCacheTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "SkPixelRef.h" 10 #include "SkPixelRef.h"
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 #endif 158 #endif
159 159
160 void SkScaledImageCache::init() { 160 void SkScaledImageCache::init() {
161 fHead = NULL; 161 fHead = NULL;
162 fTail = NULL; 162 fTail = NULL;
163 #ifdef USE_HASH 163 #ifdef USE_HASH
164 fHash = new Hash; 164 fHash = new Hash;
165 #else 165 #else
166 fHash = NULL; 166 fHash = NULL;
167 #endif 167 #endif
168 fBytesUsed = 0; 168 fTotalBytesUsed = 0;
169 fCount = 0; 169 fCount = 0;
170 fSingleAllocationByteLimit = 0;
170 fAllocator = NULL; 171 fAllocator = NULL;
171 172
172 // One of these should be explicit set by the caller after we return. 173 // One of these should be explicit set by the caller after we return.
173 fByteLimit = 0; 174 fTotalByteLimit = 0;
174 fDiscardableFactory = NULL; 175 fDiscardableFactory = NULL;
175 } 176 }
176 177
177 #include "SkDiscardableMemory.h" 178 #include "SkDiscardableMemory.h"
178 179
179 class SkOneShotDiscardablePixelRef : public SkPixelRef { 180 class SkOneShotDiscardablePixelRef : public SkPixelRef {
180 public: 181 public:
181 SK_DECLARE_INST_COUNT(SkOneShotDiscardablePixelRef) 182 SK_DECLARE_INST_COUNT(SkOneShotDiscardablePixelRef)
182 // Ownership of the discardablememory is transfered to the pixelref 183 // Ownership of the discardablememory is transfered to the pixelref
183 SkOneShotDiscardablePixelRef(const SkImageInfo&, SkDiscardableMemory*, size_ t rowBytes); 184 SkOneShotDiscardablePixelRef(const SkImageInfo&, SkDiscardableMemory*, size_ t rowBytes);
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 291
291 SkScaledImageCache::SkScaledImageCache(DiscardableFactory factory) { 292 SkScaledImageCache::SkScaledImageCache(DiscardableFactory factory) {
292 this->init(); 293 this->init();
293 fDiscardableFactory = factory; 294 fDiscardableFactory = factory;
294 295
295 fAllocator = SkNEW_ARGS(SkScaledImageCacheDiscardableAllocator, (factory)); 296 fAllocator = SkNEW_ARGS(SkScaledImageCacheDiscardableAllocator, (factory));
296 } 297 }
297 298
298 SkScaledImageCache::SkScaledImageCache(size_t byteLimit) { 299 SkScaledImageCache::SkScaledImageCache(size_t byteLimit) {
299 this->init(); 300 this->init();
300 fByteLimit = byteLimit; 301 fTotalByteLimit = byteLimit;
301 } 302 }
302 303
303 SkScaledImageCache::~SkScaledImageCache() { 304 SkScaledImageCache::~SkScaledImageCache() {
304 SkSafeUnref(fAllocator); 305 SkSafeUnref(fAllocator);
305 306
306 Rec* rec = fHead; 307 Rec* rec = fHead;
307 while (rec) { 308 while (rec) {
308 Rec* next = rec->fNext; 309 Rec* next = rec->fNext;
309 SkDELETE(rec); 310 SkDELETE(rec);
310 rec = next; 311 rec = next;
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
495 496
496 void SkScaledImageCache::purgeAsNeeded() { 497 void SkScaledImageCache::purgeAsNeeded() {
497 size_t byteLimit; 498 size_t byteLimit;
498 int countLimit; 499 int countLimit;
499 500
500 if (fDiscardableFactory) { 501 if (fDiscardableFactory) {
501 countLimit = SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT; 502 countLimit = SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT;
502 byteLimit = SK_MaxU32; // no limit based on bytes 503 byteLimit = SK_MaxU32; // no limit based on bytes
503 } else { 504 } else {
504 countLimit = SK_MaxS32; // no limit based on count 505 countLimit = SK_MaxS32; // no limit based on count
505 byteLimit = fByteLimit; 506 byteLimit = fTotalByteLimit;
506 } 507 }
507 508
508 size_t bytesUsed = fBytesUsed; 509 size_t bytesUsed = fTotalBytesUsed;
509 int countUsed = fCount; 510 int countUsed = fCount;
510 511
511 Rec* rec = fTail; 512 Rec* rec = fTail;
512 while (rec) { 513 while (rec) {
513 if (bytesUsed < byteLimit && countUsed < countLimit) { 514 if (bytesUsed < byteLimit && countUsed < countLimit) {
514 break; 515 break;
515 } 516 }
516 517
517 Rec* prev = rec->fPrev; 518 Rec* prev = rec->fPrev;
518 if (0 == rec->fLockCount) { 519 if (0 == rec->fLockCount) {
519 size_t used = rec->bytesUsed(); 520 size_t used = rec->bytesUsed();
520 SkASSERT(used <= bytesUsed); 521 SkASSERT(used <= bytesUsed);
521 this->detach(rec); 522 this->detach(rec);
522 #ifdef USE_HASH 523 #ifdef USE_HASH
523 fHash->remove(rec->fKey); 524 fHash->remove(rec->fKey);
524 #endif 525 #endif
525 526
526 SkDELETE(rec); 527 SkDELETE(rec);
527 528
528 bytesUsed -= used; 529 bytesUsed -= used;
529 countUsed -= 1; 530 countUsed -= 1;
530 } 531 }
531 rec = prev; 532 rec = prev;
532 } 533 }
533 534
534 fBytesUsed = bytesUsed; 535 fTotalBytesUsed = bytesUsed;
535 fCount = countUsed; 536 fCount = countUsed;
536 } 537 }
537 538
538 size_t SkScaledImageCache::setByteLimit(size_t newLimit) { 539 size_t SkScaledImageCache::setTotalByteLimit(size_t newLimit) {
539 size_t prevLimit = fByteLimit; 540 size_t prevLimit = fTotalByteLimit;
540 fByteLimit = newLimit; 541 fTotalByteLimit = newLimit;
541 if (newLimit < prevLimit) { 542 if (newLimit < prevLimit) {
542 this->purgeAsNeeded(); 543 this->purgeAsNeeded();
543 } 544 }
544 return prevLimit; 545 return prevLimit;
545 } 546 }
546 547
547 /////////////////////////////////////////////////////////////////////////////// 548 ///////////////////////////////////////////////////////////////////////////////
548 549
549 void SkScaledImageCache::detach(Rec* rec) { 550 void SkScaledImageCache::detach(Rec* rec) {
550 Rec* prev = rec->fPrev; 551 Rec* prev = rec->fPrev;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
590 591
591 rec->fPrev = NULL; 592 rec->fPrev = NULL;
592 rec->fNext = fHead; 593 rec->fNext = fHead;
593 if (fHead) { 594 if (fHead) {
594 fHead->fPrev = rec; 595 fHead->fPrev = rec;
595 } 596 }
596 fHead = rec; 597 fHead = rec;
597 if (!fTail) { 598 if (!fTail) {
598 fTail = rec; 599 fTail = rec;
599 } 600 }
600 fBytesUsed += rec->bytesUsed(); 601 fTotalBytesUsed += rec->bytesUsed();
601 fCount += 1; 602 fCount += 1;
602 603
603 this->validate(); 604 this->validate();
604 } 605 }
605 606
606 /////////////////////////////////////////////////////////////////////////////// 607 ///////////////////////////////////////////////////////////////////////////////
607 608
608 #ifdef SK_DEBUG 609 #ifdef SK_DEBUG
609 void SkScaledImageCache::validate() const { 610 void SkScaledImageCache::validate() const {
610 if (NULL == fHead) { 611 if (NULL == fHead) {
611 SkASSERT(NULL == fTail); 612 SkASSERT(NULL == fTail);
612 SkASSERT(0 == fBytesUsed); 613 SkASSERT(0 == fTotalBytesUsed);
613 return; 614 return;
614 } 615 }
615 616
616 if (fHead == fTail) { 617 if (fHead == fTail) {
617 SkASSERT(NULL == fHead->fPrev); 618 SkASSERT(NULL == fHead->fPrev);
618 SkASSERT(NULL == fHead->fNext); 619 SkASSERT(NULL == fHead->fNext);
619 SkASSERT(fHead->bytesUsed() == fBytesUsed); 620 SkASSERT(fHead->bytesUsed() == fTotalBytesUsed);
620 return; 621 return;
621 } 622 }
622 623
623 SkASSERT(NULL == fHead->fPrev); 624 SkASSERT(NULL == fHead->fPrev);
624 SkASSERT(NULL != fHead->fNext); 625 SkASSERT(NULL != fHead->fNext);
625 SkASSERT(NULL == fTail->fNext); 626 SkASSERT(NULL == fTail->fNext);
626 SkASSERT(NULL != fTail->fPrev); 627 SkASSERT(NULL != fTail->fPrev);
627 628
628 size_t used = 0; 629 size_t used = 0;
629 int count = 0; 630 int count = 0;
630 const Rec* rec = fHead; 631 const Rec* rec = fHead;
631 while (rec) { 632 while (rec) {
632 count += 1; 633 count += 1;
633 used += rec->bytesUsed(); 634 used += rec->bytesUsed();
634 SkASSERT(used <= fBytesUsed); 635 SkASSERT(used <= fTotalBytesUsed);
635 rec = rec->fNext; 636 rec = rec->fNext;
636 } 637 }
637 SkASSERT(fCount == count); 638 SkASSERT(fCount == count);
638 639
639 rec = fTail; 640 rec = fTail;
640 while (rec) { 641 while (rec) {
641 SkASSERT(count > 0); 642 SkASSERT(count > 0);
642 count -= 1; 643 count -= 1;
643 SkASSERT(used >= rec->bytesUsed()); 644 SkASSERT(used >= rec->bytesUsed());
644 used -= rec->bytesUsed(); 645 used -= rec->bytesUsed();
645 rec = rec->fPrev; 646 rec = rec->fPrev;
646 } 647 }
647 648
648 SkASSERT(0 == count); 649 SkASSERT(0 == count);
649 SkASSERT(0 == used); 650 SkASSERT(0 == used);
650 } 651 }
651 #endif 652 #endif
652 653
653 void SkScaledImageCache::dump() const { 654 void SkScaledImageCache::dump() const {
654 this->validate(); 655 this->validate();
655 656
656 const Rec* rec = fHead; 657 const Rec* rec = fHead;
657 int locked = 0; 658 int locked = 0;
658 while (rec) { 659 while (rec) {
659 locked += rec->fLockCount > 0; 660 locked += rec->fLockCount > 0;
660 rec = rec->fNext; 661 rec = rec->fNext;
661 } 662 }
662 663
663 SkDebugf("SkScaledImageCache: count=%d bytes=%d locked=%d %s\n", 664 SkDebugf("SkScaledImageCache: count=%d bytes=%d locked=%d %s\n",
664 fCount, fBytesUsed, locked, 665 fCount, fTotalBytesUsed, locked,
665 fDiscardableFactory ? "discardable" : "malloc"); 666 fDiscardableFactory ? "discardable" : "malloc");
666 } 667 }
667 668
669 size_t SkScaledImageCache::setSingleAllocationByteLimit(size_t newLimit) {
670 size_t oldLimit = fSingleAllocationByteLimit;
671 fSingleAllocationByteLimit = newLimit;
672 return oldLimit;
673 }
674
675 size_t SkScaledImageCache::getSingleAllocationByteLimit() const {
676 return fSingleAllocationByteLimit;
677 }
678
668 /////////////////////////////////////////////////////////////////////////////// 679 ///////////////////////////////////////////////////////////////////////////////
669 680
670 #include "SkThread.h" 681 #include "SkThread.h"
671 682
672 SK_DECLARE_STATIC_MUTEX(gMutex); 683 SK_DECLARE_STATIC_MUTEX(gMutex);
673 static SkScaledImageCache* gScaledImageCache = NULL; 684 static SkScaledImageCache* gScaledImageCache = NULL;
674 static void cleanup_gScaledImageCache() { 685 static void cleanup_gScaledImageCache() {
675 // We'll clean this up in our own tests, but disable for clients. 686 // We'll clean this up in our own tests, but disable for clients.
676 // Chrome seems to have funky multi-process things going on in unit tests th at 687 // Chrome seems to have funky multi-process things going on in unit tests th at
677 // makes this unsafe to delete when the main process atexit()s. 688 // makes this unsafe to delete when the main process atexit()s.
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
744 return get_cache()->addAndLockMip(orig, mip); 755 return get_cache()->addAndLockMip(orig, mip);
745 } 756 }
746 757
747 void SkScaledImageCache::Unlock(SkScaledImageCache::ID* id) { 758 void SkScaledImageCache::Unlock(SkScaledImageCache::ID* id) {
748 SkAutoMutexAcquire am(gMutex); 759 SkAutoMutexAcquire am(gMutex);
749 get_cache()->unlock(id); 760 get_cache()->unlock(id);
750 761
751 // get_cache()->dump(); 762 // get_cache()->dump();
752 } 763 }
753 764
754 size_t SkScaledImageCache::GetBytesUsed() { 765 size_t SkScaledImageCache::GetTotalBytesUsed() {
755 SkAutoMutexAcquire am(gMutex); 766 SkAutoMutexAcquire am(gMutex);
756 return get_cache()->getBytesUsed(); 767 return get_cache()->getTotalBytesUsed();
757 } 768 }
758 769
759 size_t SkScaledImageCache::GetByteLimit() { 770 size_t SkScaledImageCache::GetTotalByteLimit() {
760 SkAutoMutexAcquire am(gMutex); 771 SkAutoMutexAcquire am(gMutex);
761 return get_cache()->getByteLimit(); 772 return get_cache()->getTotalByteLimit();
762 } 773 }
763 774
764 size_t SkScaledImageCache::SetByteLimit(size_t newLimit) { 775 size_t SkScaledImageCache::SetTotalByteLimit(size_t newLimit) {
765 SkAutoMutexAcquire am(gMutex); 776 SkAutoMutexAcquire am(gMutex);
766 return get_cache()->setByteLimit(newLimit); 777 return get_cache()->setTotalByteLimit(newLimit);
767 } 778 }
768 779
769 SkBitmap::Allocator* SkScaledImageCache::GetAllocator() { 780 SkBitmap::Allocator* SkScaledImageCache::GetAllocator() {
770 SkAutoMutexAcquire am(gMutex); 781 SkAutoMutexAcquire am(gMutex);
771 return get_cache()->allocator(); 782 return get_cache()->allocator();
772 } 783 }
773 784
774 void SkScaledImageCache::Dump() { 785 void SkScaledImageCache::Dump() {
775 SkAutoMutexAcquire am(gMutex); 786 SkAutoMutexAcquire am(gMutex);
776 get_cache()->dump(); 787 get_cache()->dump();
777 } 788 }
778 789
790 size_t SkScaledImageCache::SetSingleAllocationByteLimit(size_t size) {
791 SkAutoMutexAcquire am(gMutex);
792 return get_cache()->setSingleAllocationByteLimit(size);
793 }
794
795 size_t SkScaledImageCache::GetSingleAllocationByteLimit() {
796 SkAutoMutexAcquire am(gMutex);
797 return get_cache()->getSingleAllocationByteLimit();
798 }
799
779 /////////////////////////////////////////////////////////////////////////////// 800 ///////////////////////////////////////////////////////////////////////////////
780 801
781 #include "SkGraphics.h" 802 #include "SkGraphics.h"
782 803
783 size_t SkGraphics::GetImageCacheBytesUsed() { 804 size_t SkGraphics::GetImageCacheTotalBytesUsed() {
784 return SkScaledImageCache::GetBytesUsed(); 805 return SkScaledImageCache::GetTotalBytesUsed();
785 } 806 }
786 807
787 size_t SkGraphics::GetImageCacheByteLimit() { 808 size_t SkGraphics::GetImageCacheTotalByteLimit() {
788 return SkScaledImageCache::GetByteLimit(); 809 return SkScaledImageCache::GetTotalByteLimit();
789 } 810 }
790 811
791 size_t SkGraphics::SetImageCacheByteLimit(size_t newLimit) { 812 size_t SkGraphics::SetImageCacheTotalByteLimit(size_t newLimit) {
792 return SkScaledImageCache::SetByteLimit(newLimit); 813 return SkScaledImageCache::SetTotalByteLimit(newLimit);
793 } 814 }
815
816 size_t SkGraphics::GetImageCacheSingleAllocationByteLimit() {
817 return SkScaledImageCache::GetSingleAllocationByteLimit();
818 }
819
820 size_t SkGraphics::SetImageCacheSingleAllocationByteLimit(size_t newLimit) {
821 return SkScaledImageCache::SetSingleAllocationByteLimit(newLimit);
822 }
823
OLDNEW
« no previous file with comments | « src/core/SkScaledImageCache.h ('k') | tests/ImageCacheTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698