| 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 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 | 383 |
| 384 bool SkMemoryStream::isAtEnd() const { | 384 bool SkMemoryStream::isAtEnd() const { |
| 385 return fOffset == fData->size(); | 385 return fOffset == fData->size(); |
| 386 } | 386 } |
| 387 | 387 |
| 388 bool SkMemoryStream::rewind() { | 388 bool SkMemoryStream::rewind() { |
| 389 fOffset = 0; | 389 fOffset = 0; |
| 390 return true; | 390 return true; |
| 391 } | 391 } |
| 392 | 392 |
| 393 SkMemoryStream* SkMemoryStream::duplicate() const { | 393 SkMemoryStream* SkMemoryStream::duplicate() const { return new SkMemoryStream(fD
ata); } |
| 394 return SkNEW_ARGS(SkMemoryStream, (fData)); | |
| 395 } | |
| 396 | 394 |
| 397 size_t SkMemoryStream::getPosition() const { | 395 size_t SkMemoryStream::getPosition() const { |
| 398 return fOffset; | 396 return fOffset; |
| 399 } | 397 } |
| 400 | 398 |
| 401 bool SkMemoryStream::seek(size_t position) { | 399 bool SkMemoryStream::seek(size_t position) { |
| 402 fOffset = position > fData->size() | 400 fOffset = position > fData->size() |
| 403 ? fData->size() | 401 ? fData->size() |
| 404 : position; | 402 : position; |
| 405 return true; | 403 return true; |
| (...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 680 block = next; | 678 block = next; |
| 681 } | 679 } |
| 682 } | 680 } |
| 683 | 681 |
| 684 SkDynamicMemoryWStream::Block* const fHead; | 682 SkDynamicMemoryWStream::Block* const fHead; |
| 685 }; | 683 }; |
| 686 | 684 |
| 687 class SkBlockMemoryStream : public SkStreamAsset { | 685 class SkBlockMemoryStream : public SkStreamAsset { |
| 688 public: | 686 public: |
| 689 SkBlockMemoryStream(SkDynamicMemoryWStream::Block* head, size_t size) | 687 SkBlockMemoryStream(SkDynamicMemoryWStream::Block* head, size_t size) |
| 690 : fBlockMemory(SkNEW_ARGS(SkBlockMemoryRefCnt, (head))), fCurrent(head) | 688 : fBlockMemory(new SkBlockMemoryRefCnt(head)) |
| 691 , fSize(size) , fOffset(0), fCurrentOffset(0) { } | 689 , fCurrent(head) |
| 690 , fSize(size) |
| 691 , fOffset(0) |
| 692 , fCurrentOffset(0) {} |
| 692 | 693 |
| 693 SkBlockMemoryStream(SkBlockMemoryRefCnt* headRef, size_t size) | 694 SkBlockMemoryStream(SkBlockMemoryRefCnt* headRef, size_t size) |
| 694 : fBlockMemory(SkRef(headRef)), fCurrent(fBlockMemory->fHead) | 695 : fBlockMemory(SkRef(headRef)), fCurrent(fBlockMemory->fHead) |
| 695 , fSize(size) , fOffset(0), fCurrentOffset(0) { } | 696 , fSize(size) , fOffset(0), fCurrentOffset(0) { } |
| 696 | 697 |
| 697 size_t read(void* buffer, size_t rawCount) override { | 698 size_t read(void* buffer, size_t rawCount) override { |
| 698 size_t count = rawCount; | 699 size_t count = rawCount; |
| 699 if (fOffset + count > fSize) { | 700 if (fOffset + count > fSize) { |
| 700 count = fSize - fOffset; | 701 count = fSize - fOffset; |
| 701 } | 702 } |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 746 } | 747 } |
| 747 | 748 |
| 748 bool rewind() override { | 749 bool rewind() override { |
| 749 fCurrent = fBlockMemory->fHead; | 750 fCurrent = fBlockMemory->fHead; |
| 750 fOffset = 0; | 751 fOffset = 0; |
| 751 fCurrentOffset = 0; | 752 fCurrentOffset = 0; |
| 752 return true; | 753 return true; |
| 753 } | 754 } |
| 754 | 755 |
| 755 SkBlockMemoryStream* duplicate() const override { | 756 SkBlockMemoryStream* duplicate() const override { |
| 756 return SkNEW_ARGS(SkBlockMemoryStream, (fBlockMemory.get(), fSize)); | 757 return new SkBlockMemoryStream(fBlockMemory.get(), fSize); |
| 757 } | 758 } |
| 758 | 759 |
| 759 size_t getPosition() const override { | 760 size_t getPosition() const override { |
| 760 return fOffset; | 761 return fOffset; |
| 761 } | 762 } |
| 762 | 763 |
| 763 bool seek(size_t position) override { | 764 bool seek(size_t position) override { |
| 764 // If possible, skip forward. | 765 // If possible, skip forward. |
| 765 if (position >= fOffset) { | 766 if (position >= fOffset) { |
| 766 size_t skipAmount = position - fOffset; | 767 size_t skipAmount = position - fOffset; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 803 private: | 804 private: |
| 804 SkAutoTUnref<SkBlockMemoryRefCnt> const fBlockMemory; | 805 SkAutoTUnref<SkBlockMemoryRefCnt> const fBlockMemory; |
| 805 SkDynamicMemoryWStream::Block const * fCurrent; | 806 SkDynamicMemoryWStream::Block const * fCurrent; |
| 806 size_t const fSize; | 807 size_t const fSize; |
| 807 size_t fOffset; | 808 size_t fOffset; |
| 808 size_t fCurrentOffset; | 809 size_t fCurrentOffset; |
| 809 }; | 810 }; |
| 810 | 811 |
| 811 SkStreamAsset* SkDynamicMemoryWStream::detachAsStream() { | 812 SkStreamAsset* SkDynamicMemoryWStream::detachAsStream() { |
| 812 if (fCopy) { | 813 if (fCopy) { |
| 813 SkMemoryStream* stream = SkNEW_ARGS(SkMemoryStream, (fCopy)); | 814 SkMemoryStream* stream = new SkMemoryStream(fCopy); |
| 814 this->reset(); | 815 this->reset(); |
| 815 return stream; | 816 return stream; |
| 816 } | 817 } |
| 817 SkBlockMemoryStream* stream = SkNEW_ARGS(SkBlockMemoryStream, (fHead, fBytes
Written)); | 818 SkBlockMemoryStream* stream = new SkBlockMemoryStream(fHead, fBytesWritten); |
| 818 fHead = 0; | 819 fHead = 0; |
| 819 this->reset(); | 820 this->reset(); |
| 820 return stream; | 821 return stream; |
| 821 } | 822 } |
| 822 | 823 |
| 823 /////////////////////////////////////////////////////////////////////////////// | 824 /////////////////////////////////////////////////////////////////////////////// |
| 824 | 825 |
| 825 void SkDebugWStream::newline() | 826 void SkDebugWStream::newline() |
| 826 { | 827 { |
| 827 #if defined(SK_DEBUG) || defined(SK_DEVELOPER) | 828 #if defined(SK_DEBUG) || defined(SK_DEVELOPER) |
| (...skipping 26 matching lines...) Expand all Loading... |
| 854 } | 855 } |
| 855 | 856 |
| 856 SkData* data = SkData::NewFromFILE(file); | 857 SkData* data = SkData::NewFromFILE(file); |
| 857 sk_fclose(file); | 858 sk_fclose(file); |
| 858 return data; | 859 return data; |
| 859 } | 860 } |
| 860 | 861 |
| 861 SkStreamAsset* SkStream::NewFromFile(const char path[]) { | 862 SkStreamAsset* SkStream::NewFromFile(const char path[]) { |
| 862 SkAutoTUnref<SkData> data(mmap_filename(path)); | 863 SkAutoTUnref<SkData> data(mmap_filename(path)); |
| 863 if (data.get()) { | 864 if (data.get()) { |
| 864 return SkNEW_ARGS(SkMemoryStream, (data.get())); | 865 return new SkMemoryStream(data.get()); |
| 865 } | 866 } |
| 866 | 867 |
| 867 // If we get here, then our attempt at using mmap failed, so try normal | 868 // If we get here, then our attempt at using mmap failed, so try normal |
| 868 // file access. | 869 // file access. |
| 869 SkFILEStream* stream = SkNEW_ARGS(SkFILEStream, (path)); | 870 SkFILEStream* stream = new SkFILEStream(path); |
| 870 if (!stream->isValid()) { | 871 if (!stream->isValid()) { |
| 871 SkDELETE(stream); | 872 delete stream; |
| 872 stream = NULL; | 873 stream = NULL; |
| 873 } | 874 } |
| 874 return stream; | 875 return stream; |
| 875 } | 876 } |
| 876 | 877 |
| 877 // Declared in SkStreamPriv.h: | 878 // Declared in SkStreamPriv.h: |
| 878 size_t SkCopyStreamToStorage(SkAutoMalloc* storage, SkStream* stream) { | 879 size_t SkCopyStreamToStorage(SkAutoMalloc* storage, SkStream* stream) { |
| 879 SkASSERT(storage != NULL); | 880 SkASSERT(storage != NULL); |
| 880 SkASSERT(stream != NULL); | 881 SkASSERT(stream != NULL); |
| 881 | 882 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 931 if (dupStream) { | 932 if (dupStream) { |
| 932 return dupStream.detach(); | 933 return dupStream.detach(); |
| 933 } | 934 } |
| 934 stream->rewind(); | 935 stream->rewind(); |
| 935 if (stream->hasLength()) { | 936 if (stream->hasLength()) { |
| 936 size_t length = stream->getLength(); | 937 size_t length = stream->getLength(); |
| 937 if (stream->hasPosition()) { // If stream has length, but can't rewind. | 938 if (stream->hasPosition()) { // If stream has length, but can't rewind. |
| 938 length -= stream->getPosition(); | 939 length -= stream->getPosition(); |
| 939 } | 940 } |
| 940 SkAutoTUnref<SkData> data(SkData::NewFromStream(stream, length)); | 941 SkAutoTUnref<SkData> data(SkData::NewFromStream(stream, length)); |
| 941 return SkNEW_ARGS(SkMemoryStream, (data.get())); | 942 return new SkMemoryStream(data.get()); |
| 942 } | 943 } |
| 943 SkDynamicMemoryWStream tempStream; | 944 SkDynamicMemoryWStream tempStream; |
| 944 const size_t bufferSize = 4096; | 945 const size_t bufferSize = 4096; |
| 945 char buffer[bufferSize]; | 946 char buffer[bufferSize]; |
| 946 do { | 947 do { |
| 947 size_t bytesRead = stream->read(buffer, bufferSize); | 948 size_t bytesRead = stream->read(buffer, bufferSize); |
| 948 tempStream.write(buffer, bytesRead); | 949 tempStream.write(buffer, bytesRead); |
| 949 } while (!stream->isAtEnd()); | 950 } while (!stream->isAtEnd()); |
| 950 return tempStream.detachAsStream(); // returns a SkBlockMemoryStream, | 951 return tempStream.detachAsStream(); // returns a SkBlockMemoryStream, |
| 951 // cheaper than copying to SkData | 952 // cheaper than copying to SkData |
| (...skipping 13 matching lines...) Expand all Loading... |
| 965 while (true) { | 966 while (true) { |
| 966 count = input->read(scratch, sizeof(scratch)); | 967 count = input->read(scratch, sizeof(scratch)); |
| 967 if (0 == count) { | 968 if (0 == count) { |
| 968 return true; | 969 return true; |
| 969 } | 970 } |
| 970 if (!out->write(scratch, count)) { | 971 if (!out->write(scratch, count)) { |
| 971 return false; | 972 return false; |
| 972 } | 973 } |
| 973 } | 974 } |
| 974 } | 975 } |
| OLD | NEW |