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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/core/SkReader32.h ('k') | src/core/SkStreamPriv.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/core/SkStream.cpp
diff --git a/src/core/SkStream.cpp b/src/core/SkStream.cpp
index 9529308e8689ff135aa77e24dddb7639ec97a812..b6d0a2bb33dcdaa5650ee5d74cb52747fe919796 100644
--- a/src/core/SkStream.cpp
+++ b/src/core/SkStream.cpp
@@ -245,8 +245,8 @@ SkStreamAsset* SkFILEStream::duplicate() const {
}
}
- fData.reset(SkData::NewFromFILE(fFILE));
- if (nullptr == fData.get()) {
+ fData = SkData::MakeFromFILE(fFILE);
+ if (nullptr == fData) {
return nullptr;
}
return new SkMemoryStream(fData);
@@ -283,21 +283,21 @@ const void* SkFILEStream::getMemoryBase() {
///////////////////////////////////////////////////////////////////////////////
-static SkData* newFromParams(const void* src, size_t size, bool copyData) {
+static sk_sp<SkData> newFromParams(const void* src, size_t size, bool copyData) {
if (copyData) {
- return SkData::NewWithCopy(src, size);
+ return SkData::MakeWithCopy(src, size);
} else {
- return SkData::NewWithoutCopy(src, size);
+ return SkData::MakeWithoutCopy(src, size);
}
}
SkMemoryStream::SkMemoryStream() {
- fData = SkData::NewEmpty();
+ fData = SkData::MakeEmpty();
fOffset = 0;
}
SkMemoryStream::SkMemoryStream(size_t size) {
- fData = SkData::NewUninitialized(size);
+ fData = SkData::MakeUninitialized(size);
fOffset = 0;
}
@@ -306,44 +306,41 @@ SkMemoryStream::SkMemoryStream(const void* src, size_t size, bool copyData) {
fOffset = 0;
}
+SkMemoryStream::SkMemoryStream(sk_sp<SkData> data) : fData(std::move(data)) {
+ if (nullptr == fData) {
+ fData = SkData::MakeEmpty();
+ }
+ fOffset = 0;
+}
+
SkMemoryStream::SkMemoryStream(SkData* data) {
if (nullptr == data) {
- fData = SkData::NewEmpty();
+ fData = SkData::MakeEmpty();
} else {
- fData = data;
- fData->ref();
+ fData = sk_ref_sp(data);
}
fOffset = 0;
}
-SkMemoryStream::~SkMemoryStream() {
- fData->unref();
-}
-
void SkMemoryStream::setMemoryOwned(const void* src, size_t size) {
- fData->unref();
- fData = SkData::NewFromMalloc(src, size);
+ fData = SkData::MakeFromMalloc(src, size);
fOffset = 0;
}
void SkMemoryStream::setMemory(const void* src, size_t size, bool copyData) {
- fData->unref();
fData = newFromParams(src, size, copyData);
fOffset = 0;
}
SkData* SkMemoryStream::copyToData() const {
- fData->ref();
- return fData;
+ return SkSafeRef(fData.get());
}
SkData* SkMemoryStream::setData(SkData* data) {
- fData->unref();
if (nullptr == data) {
- fData = SkData::NewEmpty();
+ fData = SkData::MakeEmpty();
} else {
- fData = data;
- fData->ref();
+ fData = sk_ref_sp(data);
}
fOffset = 0;
return data;
@@ -519,9 +516,8 @@ struct SkDynamicMemoryWStream::Block {
};
SkDynamicMemoryWStream::SkDynamicMemoryWStream()
- : fHead(nullptr), fTail(nullptr), fBytesWritten(0), fCopy(nullptr)
-{
-}
+ : fHead(nullptr), fTail(nullptr), fBytesWritten(0)
+{}
SkDynamicMemoryWStream::~SkDynamicMemoryWStream()
{
@@ -655,19 +651,16 @@ void SkDynamicMemoryWStream::padToAlign4()
SkData* SkDynamicMemoryWStream::copyToData() const {
if (nullptr == fCopy) {
- SkData* data = SkData::NewUninitialized(fBytesWritten);
+ auto data = SkData::MakeUninitialized(fBytesWritten);
// be sure to call copyTo() before we assign to fCopy
this->copyTo(data->writable_data());
- fCopy = data;
+ fCopy = std::move(data);
}
- return SkRef(fCopy);
+ return SkRef(fCopy.get());
}
void SkDynamicMemoryWStream::invalidateCopy() {
- if (fCopy) {
- fCopy->unref();
- fCopy = nullptr;
- }
+ fCopy = nullptr;
}
class SkBlockMemoryRefCnt : public SkRefCnt {
@@ -854,21 +847,21 @@ bool SkDebugWStream::write(const void* buffer, size_t size)
///////////////////////////////////////////////////////////////////////////////
-static SkData* mmap_filename(const char path[]) {
+static sk_sp<SkData> mmap_filename(const char path[]) {
FILE* file = sk_fopen(path, kRead_SkFILE_Flag);
if (nullptr == file) {
return nullptr;
}
- SkData* data = SkData::NewFromFILE(file);
+ auto data = SkData::MakeFromFILE(file);
sk_fclose(file);
return data;
}
SkStreamAsset* SkStream::NewFromFile(const char path[]) {
- SkAutoTUnref<SkData> data(mmap_filename(path));
- if (data.get()) {
- return new SkMemoryStream(data.get());
+ auto data(mmap_filename(path));
+ if (data) {
+ return new SkMemoryStream(std::move(data));
}
// If we get here, then our attempt at using mmap failed, so try normal
@@ -882,11 +875,11 @@ SkStreamAsset* SkStream::NewFromFile(const char path[]) {
}
// Declared in SkStreamPriv.h:
-SkData* SkCopyStreamToData(SkStream* stream) {
+sk_sp<SkData> SkCopyStreamToData(SkStream* stream) {
SkASSERT(stream != nullptr);
if (stream->hasLength()) {
- return SkData::NewFromStream(stream, stream->getLength());
+ return SkData::MakeFromStream(stream, stream->getLength());
}
SkDynamicMemoryWStream tempStream;
@@ -896,7 +889,7 @@ SkData* SkCopyStreamToData(SkStream* stream) {
size_t bytesRead = stream->read(buffer, bufferSize);
tempStream.write(buffer, bytesRead);
} while (!stream->isAtEnd());
- return tempStream.copyToData();
+ return sk_sp<SkData>(tempStream.copyToData());
}
bool SkStreamCopy(SkWStream* out, SkStream* input) {
« 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