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

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

Issue 560653004: SkData can allocate room for its contents in the same block (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: update dox, change fPtr to non const Created 6 years, 3 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/SkFlattenableSerialization.cpp ('k') | src/images/SkDecodingImageGenerator.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 /* 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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 } else { 62 } else {
63 return byte; 63 return byte;
64 } 64 }
65 } 65 }
66 66
67 SkData* SkStream::readData() { 67 SkData* SkStream::readData() {
68 size_t size = this->readU32(); 68 size_t size = this->readU32();
69 if (0 == size) { 69 if (0 == size) {
70 return SkData::NewEmpty(); 70 return SkData::NewEmpty();
71 } else { 71 } else {
72 void* buffer = sk_malloc_throw(size); 72 SkData* data = SkData::NewUninitialized(size);
73 this->read(buffer, size); 73 this->read(data->writable_data(), size);
74 return SkData::NewFromMalloc(buffer, size); 74 return data;
75 } 75 }
76 } 76 }
77 77
78 //////////////////////////////////////////////////////////////////////////////// ////// 78 //////////////////////////////////////////////////////////////////////////////// //////
79 79
80 SkWStream::~SkWStream() 80 SkWStream::~SkWStream()
81 { 81 {
82 } 82 }
83 83
84 void SkWStream::newline() 84 void SkWStream::newline()
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
301 } 301 }
302 return fData->data(); 302 return fData->data();
303 } 303 }
304 304
305 /////////////////////////////////////////////////////////////////////////////// 305 ///////////////////////////////////////////////////////////////////////////////
306 306
307 static SkData* newFromParams(const void* src, size_t size, bool copyData) { 307 static SkData* newFromParams(const void* src, size_t size, bool copyData) {
308 if (copyData) { 308 if (copyData) {
309 return SkData::NewWithCopy(src, size); 309 return SkData::NewWithCopy(src, size);
310 } else { 310 } else {
311 return SkData::NewWithProc(src, size, NULL, NULL); 311 return SkData::NewWithoutCopy(src, size);
312 } 312 }
313 } 313 }
314 314
315 SkMemoryStream::SkMemoryStream() { 315 SkMemoryStream::SkMemoryStream() {
316 fData = SkData::NewEmpty(); 316 fData = SkData::NewEmpty();
317 fOffset = 0; 317 fOffset = 0;
318 } 318 }
319 319
320 SkMemoryStream::SkMemoryStream(size_t size) { 320 SkMemoryStream::SkMemoryStream(size_t size) {
321 fData = SkData::NewFromMalloc(sk_malloc_throw(size), size); 321 fData = SkData::NewUninitialized(size);
322 fOffset = 0; 322 fOffset = 0;
323 } 323 }
324 324
325 SkMemoryStream::SkMemoryStream(const void* src, size_t size, bool copyData) { 325 SkMemoryStream::SkMemoryStream(const void* src, size_t size, bool copyData) {
326 fData = newFromParams(src, size, copyData); 326 fData = newFromParams(src, size, copyData);
327 fOffset = 0; 327 fOffset = 0;
328 } 328 }
329 329
330 SkMemoryStream::SkMemoryStream(SkData* data) { 330 SkMemoryStream::SkMemoryStream(SkData* data) {
331 if (NULL == data) { 331 if (NULL == data) {
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after
647 // cast to remove unary-minus warning 647 // cast to remove unary-minus warning
648 int padBytes = -(int)fBytesWritten & 0x03; 648 int padBytes = -(int)fBytesWritten & 0x03;
649 if (padBytes == 0) 649 if (padBytes == 0)
650 return; 650 return;
651 int zero = 0; 651 int zero = 0;
652 write(&zero, padBytes); 652 write(&zero, padBytes);
653 } 653 }
654 654
655 SkData* SkDynamicMemoryWStream::copyToData() const { 655 SkData* SkDynamicMemoryWStream::copyToData() const {
656 if (NULL == fCopy) { 656 if (NULL == fCopy) {
657 void* buffer = sk_malloc_throw(fBytesWritten); 657 SkData* data = SkData::NewUninitialized(fBytesWritten);
658 this->copyTo(buffer); 658 // be sure to call copyTo() before we assign to fCopy
659 fCopy = SkData::NewFromMalloc(buffer, fBytesWritten); 659 this->copyTo(data->writable_data());
660 fCopy = data;
660 } 661 }
661 fCopy->ref(); 662 return SkRef(fCopy);
662 return fCopy;
663 } 663 }
664 664
665 void SkDynamicMemoryWStream::invalidateCopy() { 665 void SkDynamicMemoryWStream::invalidateCopy() {
666 if (fCopy) { 666 if (fCopy) {
667 fCopy->unref(); 667 fCopy->unref();
668 fCopy = NULL; 668 fCopy = NULL;
669 } 669 }
670 } 670 }
671 671
672 class SkBlockMemoryRefCnt : public SkRefCnt { 672 class SkBlockMemoryRefCnt : public SkRefCnt {
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
884 tempStream.copyTo(dst); 884 tempStream.copyTo(dst);
885 return length; 885 return length;
886 } 886 }
887 887
888 // Declared in SkStreamPriv.h: 888 // Declared in SkStreamPriv.h:
889 SkData* SkCopyStreamToData(SkStream* stream) { 889 SkData* SkCopyStreamToData(SkStream* stream) {
890 SkASSERT(stream != NULL); 890 SkASSERT(stream != NULL);
891 891
892 if (stream->hasLength()) { 892 if (stream->hasLength()) {
893 const size_t length = stream->getLength(); 893 const size_t length = stream->getLength();
894 SkAutoMalloc dst(length); 894 SkData* data = SkData::NewUninitialized(length);
895 if (stream->read(dst.get(), length) != length) { 895 if (stream->read(data->writable_data(), length) != length) {
896 return NULL; 896 data->unref();
897 data = NULL;
897 } 898 }
898 return SkData::NewFromMalloc(dst.detach(), length); 899 return data;
899 } 900 }
900 901
901 SkDynamicMemoryWStream tempStream; 902 SkDynamicMemoryWStream tempStream;
902 const size_t bufferSize = 4096; 903 const size_t bufferSize = 4096;
903 char buffer[bufferSize]; 904 char buffer[bufferSize];
904 do { 905 do {
905 size_t bytesRead = stream->read(buffer, bufferSize); 906 size_t bytesRead = stream->read(buffer, bufferSize);
906 tempStream.write(buffer, bytesRead); 907 tempStream.write(buffer, bytesRead);
907 } while (!stream->isAtEnd()); 908 } while (!stream->isAtEnd());
908 return tempStream.copyToData(); 909 return tempStream.copyToData();
909 } 910 }
910 911
911 SkStreamRewindable* SkStreamRewindableFromSkStream(SkStream* stream) { 912 SkStreamRewindable* SkStreamRewindableFromSkStream(SkStream* stream) {
912 if (!stream) { 913 if (!stream) {
913 return NULL; 914 return NULL;
914 } 915 }
915 SkAutoTUnref<SkStreamRewindable> dupStream(stream->duplicate()); 916 SkAutoTUnref<SkStreamRewindable> dupStream(stream->duplicate());
916 if (dupStream) { 917 if (dupStream) {
917 return dupStream.detach(); 918 return dupStream.detach();
918 } 919 }
919 stream->rewind(); 920 stream->rewind();
920 if (stream->hasLength()) { 921 if (stream->hasLength()) {
921 size_t length = stream->getLength(); 922 size_t length = stream->getLength();
922 if (stream->hasPosition()) { // If stream has length, but can't rewind. 923 if (stream->hasPosition()) { // If stream has length, but can't rewind.
923 length -= stream->getPosition(); 924 length -= stream->getPosition();
924 } 925 }
925 SkAutoMalloc allocMemory(length); 926 SkAutoTUnref<SkData> data(SkData::NewUninitialized(length));
926 SkDEBUGCODE(size_t read =) stream->read(allocMemory.get(), length); 927 SkDEBUGCODE(size_t read =) stream->read(data->writable_data(), length);
927 SkASSERT(length == read); 928 SkASSERT(length == read);
928 SkAutoTUnref<SkData> data(
929 SkData::NewFromMalloc(allocMemory.detach(), length));
930 return SkNEW_ARGS(SkMemoryStream, (data.get())); 929 return SkNEW_ARGS(SkMemoryStream, (data.get()));
931 } 930 }
932 SkDynamicMemoryWStream tempStream; 931 SkDynamicMemoryWStream tempStream;
933 const size_t bufferSize = 4096; 932 const size_t bufferSize = 4096;
934 char buffer[bufferSize]; 933 char buffer[bufferSize];
935 do { 934 do {
936 size_t bytesRead = stream->read(buffer, bufferSize); 935 size_t bytesRead = stream->read(buffer, bufferSize);
937 tempStream.write(buffer, bytesRead); 936 tempStream.write(buffer, bytesRead);
938 } while (!stream->isAtEnd()); 937 } while (!stream->isAtEnd());
939 return tempStream.detachAsStream(); // returns a SkBlockMemoryStream, 938 return tempStream.detachAsStream(); // returns a SkBlockMemoryStream,
940 // cheaper than copying to SkData 939 // cheaper than copying to SkData
941 } 940 }
OLDNEW
« no previous file with comments | « src/core/SkFlattenableSerialization.cpp ('k') | src/images/SkDecodingImageGenerator.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698