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