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

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

Issue 1779263003: Make sp variants for SkData (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years, 9 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/SkReader32.h ('k') | src/core/SkStreamPriv.h » ('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 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 return new SkMemoryStream(fData); 238 return new SkMemoryStream(fData);
239 } 239 }
240 240
241 if (!fName.isEmpty()) { 241 if (!fName.isEmpty()) {
242 SkAutoTDelete<SkFILEStream> that(new SkFILEStream(fName.c_str())); 242 SkAutoTDelete<SkFILEStream> that(new SkFILEStream(fName.c_str()));
243 if (sk_fidentical(that->fFILE, this->fFILE)) { 243 if (sk_fidentical(that->fFILE, this->fFILE)) {
244 return that.detach(); 244 return that.detach();
245 } 245 }
246 } 246 }
247 247
248 fData.reset(SkData::NewFromFILE(fFILE)); 248 fData = SkData::MakeFromFILE(fFILE);
249 if (nullptr == fData.get()) { 249 if (nullptr == fData) {
250 return nullptr; 250 return nullptr;
251 } 251 }
252 return new SkMemoryStream(fData); 252 return new SkMemoryStream(fData);
253 } 253 }
254 254
255 size_t SkFILEStream::getPosition() const { 255 size_t SkFILEStream::getPosition() const {
256 return sk_ftell(fFILE); 256 return sk_ftell(fFILE);
257 } 257 }
258 258
259 bool SkFILEStream::seek(size_t position) { 259 bool SkFILEStream::seek(size_t position) {
(...skipping 16 matching lines...) Expand all
276 276
277 const void* SkFILEStream::getMemoryBase() { 277 const void* SkFILEStream::getMemoryBase() {
278 if (nullptr == fData.get()) { 278 if (nullptr == fData.get()) {
279 return nullptr; 279 return nullptr;
280 } 280 }
281 return fData->data(); 281 return fData->data();
282 } 282 }
283 283
284 /////////////////////////////////////////////////////////////////////////////// 284 ///////////////////////////////////////////////////////////////////////////////
285 285
286 static SkData* newFromParams(const void* src, size_t size, bool copyData) { 286 static sk_sp<SkData> newFromParams(const void* src, size_t size, bool copyData) {
287 if (copyData) { 287 if (copyData) {
288 return SkData::NewWithCopy(src, size); 288 return SkData::MakeWithCopy(src, size);
289 } else { 289 } else {
290 return SkData::NewWithoutCopy(src, size); 290 return SkData::MakeWithoutCopy(src, size);
291 } 291 }
292 } 292 }
293 293
294 SkMemoryStream::SkMemoryStream() { 294 SkMemoryStream::SkMemoryStream() {
295 fData = SkData::NewEmpty(); 295 fData = SkData::MakeEmpty();
296 fOffset = 0; 296 fOffset = 0;
297 } 297 }
298 298
299 SkMemoryStream::SkMemoryStream(size_t size) { 299 SkMemoryStream::SkMemoryStream(size_t size) {
300 fData = SkData::NewUninitialized(size); 300 fData = SkData::MakeUninitialized(size);
301 fOffset = 0; 301 fOffset = 0;
302 } 302 }
303 303
304 SkMemoryStream::SkMemoryStream(const void* src, size_t size, bool copyData) { 304 SkMemoryStream::SkMemoryStream(const void* src, size_t size, bool copyData) {
305 fData = newFromParams(src, size, copyData); 305 fData = newFromParams(src, size, copyData);
306 fOffset = 0; 306 fOffset = 0;
307 } 307 }
308 308
309 SkMemoryStream::SkMemoryStream(sk_sp<SkData> data) : fData(std::move(data)) {
310 if (nullptr == fData) {
311 fData = SkData::MakeEmpty();
312 }
313 fOffset = 0;
314 }
315
309 SkMemoryStream::SkMemoryStream(SkData* data) { 316 SkMemoryStream::SkMemoryStream(SkData* data) {
310 if (nullptr == data) { 317 if (nullptr == data) {
311 fData = SkData::NewEmpty(); 318 fData = SkData::MakeEmpty();
312 } else { 319 } else {
313 fData = data; 320 fData = sk_ref_sp(data);
314 fData->ref();
315 } 321 }
316 fOffset = 0; 322 fOffset = 0;
317 } 323 }
318 324
319 SkMemoryStream::~SkMemoryStream() {
320 fData->unref();
321 }
322
323 void SkMemoryStream::setMemoryOwned(const void* src, size_t size) { 325 void SkMemoryStream::setMemoryOwned(const void* src, size_t size) {
324 fData->unref(); 326 fData = SkData::MakeFromMalloc(src, size);
325 fData = SkData::NewFromMalloc(src, size);
326 fOffset = 0; 327 fOffset = 0;
327 } 328 }
328 329
329 void SkMemoryStream::setMemory(const void* src, size_t size, bool copyData) { 330 void SkMemoryStream::setMemory(const void* src, size_t size, bool copyData) {
330 fData->unref();
331 fData = newFromParams(src, size, copyData); 331 fData = newFromParams(src, size, copyData);
332 fOffset = 0; 332 fOffset = 0;
333 } 333 }
334 334
335 SkData* SkMemoryStream::copyToData() const { 335 SkData* SkMemoryStream::copyToData() const {
336 fData->ref(); 336 return SkSafeRef(fData.get());
337 return fData;
338 } 337 }
339 338
340 SkData* SkMemoryStream::setData(SkData* data) { 339 SkData* SkMemoryStream::setData(SkData* data) {
341 fData->unref();
342 if (nullptr == data) { 340 if (nullptr == data) {
343 fData = SkData::NewEmpty(); 341 fData = SkData::MakeEmpty();
344 } else { 342 } else {
345 fData = data; 343 fData = sk_ref_sp(data);
346 fData->ref();
347 } 344 }
348 fOffset = 0; 345 fOffset = 0;
349 return data; 346 return data;
350 } 347 }
351 348
352 void SkMemoryStream::skipToAlign4() { 349 void SkMemoryStream::skipToAlign4() {
353 // cast to remove unary-minus warning 350 // cast to remove unary-minus warning
354 fOffset += -(int)fOffset & 0x03; 351 fOffset += -(int)fOffset & 0x03;
355 } 352 }
356 353
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
512 const void* append(const void* data, size_t size) 509 const void* append(const void* data, size_t size)
513 { 510 {
514 SkASSERT((size_t)(fStop - fCurr) >= size); 511 SkASSERT((size_t)(fStop - fCurr) >= size);
515 memcpy(fCurr, data, size); 512 memcpy(fCurr, data, size);
516 fCurr += size; 513 fCurr += size;
517 return (const void*)((const char*)data + size); 514 return (const void*)((const char*)data + size);
518 } 515 }
519 }; 516 };
520 517
521 SkDynamicMemoryWStream::SkDynamicMemoryWStream() 518 SkDynamicMemoryWStream::SkDynamicMemoryWStream()
522 : fHead(nullptr), fTail(nullptr), fBytesWritten(0), fCopy(nullptr) 519 : fHead(nullptr), fTail(nullptr), fBytesWritten(0)
523 { 520 {}
524 }
525 521
526 SkDynamicMemoryWStream::~SkDynamicMemoryWStream() 522 SkDynamicMemoryWStream::~SkDynamicMemoryWStream()
527 { 523 {
528 reset(); 524 reset();
529 } 525 }
530 526
531 void SkDynamicMemoryWStream::reset() 527 void SkDynamicMemoryWStream::reset()
532 { 528 {
533 this->invalidateCopy(); 529 this->invalidateCopy();
534 530
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
648 // cast to remove unary-minus warning 644 // cast to remove unary-minus warning
649 int padBytes = -(int)fBytesWritten & 0x03; 645 int padBytes = -(int)fBytesWritten & 0x03;
650 if (padBytes == 0) 646 if (padBytes == 0)
651 return; 647 return;
652 int zero = 0; 648 int zero = 0;
653 write(&zero, padBytes); 649 write(&zero, padBytes);
654 } 650 }
655 651
656 SkData* SkDynamicMemoryWStream::copyToData() const { 652 SkData* SkDynamicMemoryWStream::copyToData() const {
657 if (nullptr == fCopy) { 653 if (nullptr == fCopy) {
658 SkData* data = SkData::NewUninitialized(fBytesWritten); 654 auto data = SkData::MakeUninitialized(fBytesWritten);
659 // be sure to call copyTo() before we assign to fCopy 655 // be sure to call copyTo() before we assign to fCopy
660 this->copyTo(data->writable_data()); 656 this->copyTo(data->writable_data());
661 fCopy = data; 657 fCopy = std::move(data);
662 } 658 }
663 return SkRef(fCopy); 659 return SkRef(fCopy.get());
664 } 660 }
665 661
666 void SkDynamicMemoryWStream::invalidateCopy() { 662 void SkDynamicMemoryWStream::invalidateCopy() {
667 if (fCopy) { 663 fCopy = nullptr;
668 fCopy->unref();
669 fCopy = nullptr;
670 }
671 } 664 }
672 665
673 class SkBlockMemoryRefCnt : public SkRefCnt { 666 class SkBlockMemoryRefCnt : public SkRefCnt {
674 public: 667 public:
675 explicit SkBlockMemoryRefCnt(SkDynamicMemoryWStream::Block* head) : fHead(he ad) { } 668 explicit SkBlockMemoryRefCnt(SkDynamicMemoryWStream::Block* head) : fHead(he ad) { }
676 669
677 virtual ~SkBlockMemoryRefCnt() { 670 virtual ~SkBlockMemoryRefCnt() {
678 SkDynamicMemoryWStream::Block* block = fHead; 671 SkDynamicMemoryWStream::Block* block = fHead;
679 while (block != nullptr) { 672 while (block != nullptr) {
680 SkDynamicMemoryWStream::Block* next = block->fNext; 673 SkDynamicMemoryWStream::Block* next = block->fNext;
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
847 delete[] s; 840 delete[] s;
848 fBytesWritten += size; 841 fBytesWritten += size;
849 #endif 842 #endif
850 return true; 843 return true;
851 } 844 }
852 845
853 /////////////////////////////////////////////////////////////////////////////// 846 ///////////////////////////////////////////////////////////////////////////////
854 /////////////////////////////////////////////////////////////////////////////// 847 ///////////////////////////////////////////////////////////////////////////////
855 848
856 849
857 static SkData* mmap_filename(const char path[]) { 850 static sk_sp<SkData> mmap_filename(const char path[]) {
858 FILE* file = sk_fopen(path, kRead_SkFILE_Flag); 851 FILE* file = sk_fopen(path, kRead_SkFILE_Flag);
859 if (nullptr == file) { 852 if (nullptr == file) {
860 return nullptr; 853 return nullptr;
861 } 854 }
862 855
863 SkData* data = SkData::NewFromFILE(file); 856 auto data = SkData::MakeFromFILE(file);
864 sk_fclose(file); 857 sk_fclose(file);
865 return data; 858 return data;
866 } 859 }
867 860
868 SkStreamAsset* SkStream::NewFromFile(const char path[]) { 861 SkStreamAsset* SkStream::NewFromFile(const char path[]) {
869 SkAutoTUnref<SkData> data(mmap_filename(path)); 862 auto data(mmap_filename(path));
870 if (data.get()) { 863 if (data) {
871 return new SkMemoryStream(data.get()); 864 return new SkMemoryStream(std::move(data));
872 } 865 }
873 866
874 // If we get here, then our attempt at using mmap failed, so try normal 867 // If we get here, then our attempt at using mmap failed, so try normal
875 // file access. 868 // file access.
876 SkFILEStream* stream = new SkFILEStream(path); 869 SkFILEStream* stream = new SkFILEStream(path);
877 if (!stream->isValid()) { 870 if (!stream->isValid()) {
878 delete stream; 871 delete stream;
879 stream = nullptr; 872 stream = nullptr;
880 } 873 }
881 return stream; 874 return stream;
882 } 875 }
883 876
884 // Declared in SkStreamPriv.h: 877 // Declared in SkStreamPriv.h:
885 SkData* SkCopyStreamToData(SkStream* stream) { 878 sk_sp<SkData> SkCopyStreamToData(SkStream* stream) {
886 SkASSERT(stream != nullptr); 879 SkASSERT(stream != nullptr);
887 880
888 if (stream->hasLength()) { 881 if (stream->hasLength()) {
889 return SkData::NewFromStream(stream, stream->getLength()); 882 return SkData::MakeFromStream(stream, stream->getLength());
890 } 883 }
891 884
892 SkDynamicMemoryWStream tempStream; 885 SkDynamicMemoryWStream tempStream;
893 const size_t bufferSize = 4096; 886 const size_t bufferSize = 4096;
894 char buffer[bufferSize]; 887 char buffer[bufferSize];
895 do { 888 do {
896 size_t bytesRead = stream->read(buffer, bufferSize); 889 size_t bytesRead = stream->read(buffer, bufferSize);
897 tempStream.write(buffer, bytesRead); 890 tempStream.write(buffer, bytesRead);
898 } while (!stream->isAtEnd()); 891 } while (!stream->isAtEnd());
899 return tempStream.copyToData(); 892 return sk_sp<SkData>(tempStream.copyToData());
900 } 893 }
901 894
902 bool SkStreamCopy(SkWStream* out, SkStream* input) { 895 bool SkStreamCopy(SkWStream* out, SkStream* input) {
903 const char* base = static_cast<const char*>(input->getMemoryBase()); 896 const char* base = static_cast<const char*>(input->getMemoryBase());
904 if (base && input->hasPosition() && input->hasLength()) { 897 if (base && input->hasPosition() && input->hasLength()) {
905 // Shortcut that avoids the while loop. 898 // Shortcut that avoids the while loop.
906 size_t position = input->getPosition(); 899 size_t position = input->getPosition();
907 size_t length = input->getLength(); 900 size_t length = input->getLength();
908 SkASSERT(length >= position); 901 SkASSERT(length >= position);
909 return out->write(&base[position], length - position); 902 return out->write(&base[position], length - position);
910 } 903 }
911 char scratch[4096]; 904 char scratch[4096];
912 size_t count; 905 size_t count;
913 while (true) { 906 while (true) {
914 count = input->read(scratch, sizeof(scratch)); 907 count = input->read(scratch, sizeof(scratch));
915 if (0 == count) { 908 if (0 == count) {
916 return true; 909 return true;
917 } 910 }
918 if (!out->write(scratch, count)) { 911 if (!out->write(scratch, count)) {
919 return false; 912 return false;
920 } 913 }
921 } 914 }
922 } 915 }
OLDNEW
« no previous file with comments | « src/core/SkReader32.h ('k') | src/core/SkStreamPriv.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698