| 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 656 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 667 class SkBlockMemoryStream : public SkStreamAsset { | 667 class SkBlockMemoryStream : public SkStreamAsset { |
| 668 public: | 668 public: |
| 669 SkBlockMemoryStream(SkDynamicMemoryWStream::Block* head, size_t size) | 669 SkBlockMemoryStream(SkDynamicMemoryWStream::Block* head, size_t size) |
| 670 : fBlockMemory(SkNEW_ARGS(SkBlockMemoryRefCnt, (head))), fCurrent(head) | 670 : fBlockMemory(SkNEW_ARGS(SkBlockMemoryRefCnt, (head))), fCurrent(head) |
| 671 , fSize(size) , fOffset(0), fCurrentOffset(0) { } | 671 , fSize(size) , fOffset(0), fCurrentOffset(0) { } |
| 672 | 672 |
| 673 SkBlockMemoryStream(SkBlockMemoryRefCnt* headRef, size_t size) | 673 SkBlockMemoryStream(SkBlockMemoryRefCnt* headRef, size_t size) |
| 674 : fBlockMemory(SkRef(headRef)), fCurrent(fBlockMemory->fHead) | 674 : fBlockMemory(SkRef(headRef)), fCurrent(fBlockMemory->fHead) |
| 675 , fSize(size) , fOffset(0), fCurrentOffset(0) { } | 675 , fSize(size) , fOffset(0), fCurrentOffset(0) { } |
| 676 | 676 |
| 677 virtual size_t read(void* buffer, size_t rawCount) SK_OVERRIDE { | 677 size_t read(void* buffer, size_t rawCount) SK_OVERRIDE { |
| 678 size_t count = rawCount; | 678 size_t count = rawCount; |
| 679 if (fOffset + count > fSize) { | 679 if (fOffset + count > fSize) { |
| 680 count = fSize - fOffset; | 680 count = fSize - fOffset; |
| 681 } | 681 } |
| 682 size_t bytesLeftToRead = count; | 682 size_t bytesLeftToRead = count; |
| 683 while (fCurrent != NULL) { | 683 while (fCurrent != NULL) { |
| 684 size_t bytesLeftInCurrent = fCurrent->written() - fCurrentOffset; | 684 size_t bytesLeftInCurrent = fCurrent->written() - fCurrentOffset; |
| 685 size_t bytesFromCurrent = SkTMin(bytesLeftToRead, bytesLeftInCurrent
); | 685 size_t bytesFromCurrent = SkTMin(bytesLeftToRead, bytesLeftInCurrent
); |
| 686 if (buffer) { | 686 if (buffer) { |
| 687 memcpy(buffer, fCurrent->start() + fCurrentOffset, bytesFromCurr
ent); | 687 memcpy(buffer, fCurrent->start() + fCurrentOffset, bytesFromCurr
ent); |
| 688 buffer = SkTAddOffset<void>(buffer, bytesFromCurrent); | 688 buffer = SkTAddOffset<void>(buffer, bytesFromCurrent); |
| 689 } | 689 } |
| 690 if (bytesLeftToRead <= bytesFromCurrent) { | 690 if (bytesLeftToRead <= bytesFromCurrent) { |
| 691 fCurrentOffset += bytesFromCurrent; | 691 fCurrentOffset += bytesFromCurrent; |
| 692 fOffset += count; | 692 fOffset += count; |
| 693 return count; | 693 return count; |
| 694 } | 694 } |
| 695 bytesLeftToRead -= bytesFromCurrent; | 695 bytesLeftToRead -= bytesFromCurrent; |
| 696 fCurrent = fCurrent->fNext; | 696 fCurrent = fCurrent->fNext; |
| 697 fCurrentOffset = 0; | 697 fCurrentOffset = 0; |
| 698 } | 698 } |
| 699 SkASSERT(false); | 699 SkASSERT(false); |
| 700 return 0; | 700 return 0; |
| 701 } | 701 } |
| 702 | 702 |
| 703 virtual bool isAtEnd() const SK_OVERRIDE { | 703 bool isAtEnd() const SK_OVERRIDE { |
| 704 return fOffset == fSize; | 704 return fOffset == fSize; |
| 705 } | 705 } |
| 706 | 706 |
| 707 virtual bool rewind() SK_OVERRIDE { | 707 bool rewind() SK_OVERRIDE { |
| 708 fCurrent = fBlockMemory->fHead; | 708 fCurrent = fBlockMemory->fHead; |
| 709 fOffset = 0; | 709 fOffset = 0; |
| 710 fCurrentOffset = 0; | 710 fCurrentOffset = 0; |
| 711 return true; | 711 return true; |
| 712 } | 712 } |
| 713 | 713 |
| 714 virtual SkBlockMemoryStream* duplicate() const SK_OVERRIDE { | 714 SkBlockMemoryStream* duplicate() const SK_OVERRIDE { |
| 715 return SkNEW_ARGS(SkBlockMemoryStream, (fBlockMemory.get(), fSize)); | 715 return SkNEW_ARGS(SkBlockMemoryStream, (fBlockMemory.get(), fSize)); |
| 716 } | 716 } |
| 717 | 717 |
| 718 virtual size_t getPosition() const SK_OVERRIDE { | 718 size_t getPosition() const SK_OVERRIDE { |
| 719 return fOffset; | 719 return fOffset; |
| 720 } | 720 } |
| 721 | 721 |
| 722 virtual bool seek(size_t position) SK_OVERRIDE { | 722 bool seek(size_t position) SK_OVERRIDE { |
| 723 // If possible, skip forward. | 723 // If possible, skip forward. |
| 724 if (position >= fOffset) { | 724 if (position >= fOffset) { |
| 725 size_t skipAmount = position - fOffset; | 725 size_t skipAmount = position - fOffset; |
| 726 return this->skip(skipAmount) == skipAmount; | 726 return this->skip(skipAmount) == skipAmount; |
| 727 } | 727 } |
| 728 // If possible, move backward within the current block. | 728 // If possible, move backward within the current block. |
| 729 size_t moveBackAmount = fOffset - position; | 729 size_t moveBackAmount = fOffset - position; |
| 730 if (moveBackAmount <= fCurrentOffset) { | 730 if (moveBackAmount <= fCurrentOffset) { |
| 731 fCurrentOffset -= moveBackAmount; | 731 fCurrentOffset -= moveBackAmount; |
| 732 fOffset -= moveBackAmount; | 732 fOffset -= moveBackAmount; |
| 733 return true; | 733 return true; |
| 734 } | 734 } |
| 735 // Otherwise rewind and move forward. | 735 // Otherwise rewind and move forward. |
| 736 return this->rewind() && this->skip(position) == position; | 736 return this->rewind() && this->skip(position) == position; |
| 737 } | 737 } |
| 738 | 738 |
| 739 virtual bool move(long offset) SK_OVERRIDE { | 739 bool move(long offset) SK_OVERRIDE { |
| 740 return seek(fOffset + offset); | 740 return seek(fOffset + offset); |
| 741 } | 741 } |
| 742 | 742 |
| 743 virtual SkBlockMemoryStream* fork() const SK_OVERRIDE { | 743 SkBlockMemoryStream* fork() const SK_OVERRIDE { |
| 744 SkAutoTUnref<SkBlockMemoryStream> that(this->duplicate()); | 744 SkAutoTUnref<SkBlockMemoryStream> that(this->duplicate()); |
| 745 that->fCurrent = this->fCurrent; | 745 that->fCurrent = this->fCurrent; |
| 746 that->fOffset = this->fOffset; | 746 that->fOffset = this->fOffset; |
| 747 that->fCurrentOffset = this->fCurrentOffset; | 747 that->fCurrentOffset = this->fCurrentOffset; |
| 748 return that.detach(); | 748 return that.detach(); |
| 749 } | 749 } |
| 750 | 750 |
| 751 virtual size_t getLength() const SK_OVERRIDE { | 751 size_t getLength() const SK_OVERRIDE { |
| 752 return fSize; | 752 return fSize; |
| 753 } | 753 } |
| 754 | 754 |
| 755 virtual const void* getMemoryBase() SK_OVERRIDE { | 755 const void* getMemoryBase() SK_OVERRIDE { |
| 756 if (NULL == fBlockMemory->fHead->fNext) { | 756 if (NULL == fBlockMemory->fHead->fNext) { |
| 757 return fBlockMemory->fHead->start(); | 757 return fBlockMemory->fHead->start(); |
| 758 } | 758 } |
| 759 return NULL; | 759 return NULL; |
| 760 } | 760 } |
| 761 | 761 |
| 762 private: | 762 private: |
| 763 SkAutoTUnref<SkBlockMemoryRefCnt> const fBlockMemory; | 763 SkAutoTUnref<SkBlockMemoryRefCnt> const fBlockMemory; |
| 764 SkDynamicMemoryWStream::Block const * fCurrent; | 764 SkDynamicMemoryWStream::Block const * fCurrent; |
| 765 size_t const fSize; | 765 size_t const fSize; |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 902 SkDynamicMemoryWStream tempStream; | 902 SkDynamicMemoryWStream tempStream; |
| 903 const size_t bufferSize = 4096; | 903 const size_t bufferSize = 4096; |
| 904 char buffer[bufferSize]; | 904 char buffer[bufferSize]; |
| 905 do { | 905 do { |
| 906 size_t bytesRead = stream->read(buffer, bufferSize); | 906 size_t bytesRead = stream->read(buffer, bufferSize); |
| 907 tempStream.write(buffer, bytesRead); | 907 tempStream.write(buffer, bytesRead); |
| 908 } while (!stream->isAtEnd()); | 908 } while (!stream->isAtEnd()); |
| 909 return tempStream.detachAsStream(); // returns a SkBlockMemoryStream, | 909 return tempStream.detachAsStream(); // returns a SkBlockMemoryStream, |
| 910 // cheaper than copying to SkData | 910 // cheaper than copying to SkData |
| 911 } | 911 } |
| OLD | NEW |