OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #include "SkStream.h" | 10 #include "SkStream.h" |
(...skipping 643 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
654 return fCopy; | 654 return fCopy; |
655 } | 655 } |
656 | 656 |
657 void SkDynamicMemoryWStream::invalidateCopy() { | 657 void SkDynamicMemoryWStream::invalidateCopy() { |
658 if (fCopy) { | 658 if (fCopy) { |
659 fCopy->unref(); | 659 fCopy->unref(); |
660 fCopy = NULL; | 660 fCopy = NULL; |
661 } | 661 } |
662 } | 662 } |
663 | 663 |
| 664 class SkBlockMemoryRefCnt : public SkRefCnt { |
| 665 public: |
| 666 SkBlockMemoryRefCnt(SkDynamicMemoryWStream::Block* head) : fHead(head) { } |
| 667 |
| 668 virtual ~SkBlockMemoryRefCnt() { |
| 669 SkDynamicMemoryWStream::Block* block = fHead; |
| 670 while (block != NULL) { |
| 671 SkDynamicMemoryWStream::Block* next = block->fNext; |
| 672 sk_free(block); |
| 673 block = next; |
| 674 } |
| 675 } |
| 676 |
| 677 SkDynamicMemoryWStream::Block* const fHead; |
| 678 }; |
| 679 |
| 680 class SkBlockMemoryStream : public SkStreamAsset { |
| 681 public: |
| 682 SkBlockMemoryStream(SkDynamicMemoryWStream::Block* head, size_t size) |
| 683 : fBlockMemory(new SkBlockMemoryRefCnt(head)), fCurrent(head), fSize(siz
e) |
| 684 , fOffset(0), fCurrentOffset(0) { } |
| 685 |
| 686 SkBlockMemoryStream(SkBlockMemoryRefCnt* headRef, size_t size) |
| 687 : fBlockMemory(SkRef(headRef)), fCurrent(fBlockMemory->fHead), fSize(siz
e) |
| 688 , fOffset(0), fCurrentOffset(0) { } |
| 689 |
| 690 virtual size_t read(void* buffer, size_t rawCount) SK_OVERRIDE { |
| 691 size_t count = rawCount; |
| 692 if (fOffset + count > fSize) { |
| 693 count = fSize - fOffset; |
| 694 } |
| 695 size_t bytesLeftToRead = count; |
| 696 while (fCurrent != NULL) { |
| 697 size_t bytesLeftInCurrent = fCurrent->written() - fCurrentOffset; |
| 698 size_t bytesFromCurrent = bytesLeftToRead <= bytesLeftInCurrent |
| 699 ? bytesLeftToRead : bytesLeftInCurrent; |
| 700 if (buffer) { |
| 701 memcpy(buffer, fCurrent->start() + fCurrentOffset, bytesFromCurr
ent); |
| 702 } |
| 703 if (bytesLeftToRead <= bytesFromCurrent) { |
| 704 fCurrentOffset += bytesFromCurrent; |
| 705 fOffset += count; |
| 706 return count; |
| 707 } |
| 708 bytesLeftToRead -= bytesFromCurrent; |
| 709 buffer = (void*) ((char* ) buffer + bytesFromCurrent); |
| 710 fCurrent = fCurrent->fNext; |
| 711 fCurrentOffset = 0; |
| 712 } |
| 713 SkASSERT(false); |
| 714 return 0; |
| 715 } |
| 716 |
| 717 virtual bool isAtEnd() const SK_OVERRIDE { |
| 718 return fOffset == fSize; |
| 719 } |
| 720 |
| 721 virtual bool rewind() SK_OVERRIDE { |
| 722 fCurrent = fBlockMemory->fHead; |
| 723 fCurrentOffset = 0; |
| 724 fOffset = 0; |
| 725 return true; |
| 726 } |
| 727 virtual SkBlockMemoryStream* duplicate() const SK_OVERRIDE { |
| 728 return new SkBlockMemoryStream(fBlockMemory.get(), fSize); |
| 729 } |
| 730 |
| 731 virtual size_t getPosition() const SK_OVERRIDE { |
| 732 return fOffset; |
| 733 } |
| 734 virtual bool seek(size_t position) SK_OVERRIDE { |
| 735 if (position >= fOffset) { |
| 736 size_t skipAmount = position - fOffset; |
| 737 return this->skip(skipAmount) == skipAmount; |
| 738 } |
| 739 return this->rewind() && this->skip(position) == position; |
| 740 } |
| 741 virtual bool move(long offset) SK_OVERRIDE { |
| 742 return seek(fOffset + offset); |
| 743 } |
| 744 virtual SkBlockMemoryStream* fork() const SK_OVERRIDE { |
| 745 SkAutoTUnref<SkBlockMemoryStream> that(this->duplicate()); |
| 746 that->fOffset = this->fOffset; |
| 747 that->fCurrent = this->fCurrent; |
| 748 that->fCurrentOffset = this->fCurrentOffset; |
| 749 return that.detach(); |
| 750 } |
| 751 |
| 752 virtual size_t getLength() const SK_OVERRIDE { |
| 753 return fSize; |
| 754 } |
| 755 |
| 756 virtual const void* getMemoryBase() SK_OVERRIDE { |
| 757 if (fBlockMemory->fHead->fNext == NULL) { |
| 758 return fBlockMemory->fHead->start(); |
| 759 } |
| 760 return false; |
| 761 } |
| 762 |
| 763 private: |
| 764 SkAutoTUnref<SkBlockMemoryRefCnt> fBlockMemory; |
| 765 SkDynamicMemoryWStream::Block const * fCurrent; |
| 766 size_t const fSize; |
| 767 size_t fOffset; |
| 768 size_t fCurrentOffset; |
| 769 }; |
| 770 |
| 771 SkStreamAsset* SkDynamicMemoryWStream::detatchAsStream() { |
| 772 if (fCopy) { |
| 773 SkMemoryStream* stream = new SkMemoryStream(fCopy); |
| 774 this->reset(); |
| 775 return stream; |
| 776 } |
| 777 SkBlockMemoryStream* stream = new SkBlockMemoryStream(fHead, fBytesWritten); |
| 778 fHead = 0; |
| 779 this->reset(); |
| 780 return stream; |
| 781 } |
| 782 |
664 /////////////////////////////////////////////////////////////////////////////// | 783 /////////////////////////////////////////////////////////////////////////////// |
665 | 784 |
666 void SkDebugWStream::newline() | 785 void SkDebugWStream::newline() |
667 { | 786 { |
668 #if defined(SK_DEBUG) || defined(SK_DEVELOPER) | 787 #if defined(SK_DEBUG) || defined(SK_DEVELOPER) |
669 SkDebugf("\n"); | 788 SkDebugf("\n"); |
670 #endif | 789 #endif |
671 } | 790 } |
672 | 791 |
673 bool SkDebugWStream::write(const void* buffer, size_t size) | 792 bool SkDebugWStream::write(const void* buffer, size_t size) |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
705 | 824 |
706 // If we get here, then our attempt at using mmap failed, so try normal | 825 // If we get here, then our attempt at using mmap failed, so try normal |
707 // file access. | 826 // file access. |
708 SkFILEStream* stream = SkNEW_ARGS(SkFILEStream, (path)); | 827 SkFILEStream* stream = SkNEW_ARGS(SkFILEStream, (path)); |
709 if (!stream->isValid()) { | 828 if (!stream->isValid()) { |
710 stream->unref(); | 829 stream->unref(); |
711 stream = NULL; | 830 stream = NULL; |
712 } | 831 } |
713 return stream; | 832 return stream; |
714 } | 833 } |
OLD | NEW |