aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Mike Reed <reed@google.com>2017-07-26 11:35:53 -0400
committerGravatar Skia Commit-Bot <skia-commit-bot@chromium.org>2017-07-26 15:59:49 +0000
commit847068ce833f2409ae2f5e39d14616899b24e1a5 (patch)
treec4a94a3568ced9c3f8cdaf3a18a54a6031fa0a59
parent50fa3ff1381028e8a155522a4598678911bce53d (diff)
add Make factory to SkMemoryStream (simplify call-sites)
Bug: skia:6888 Change-Id: Ia4e432673ed089a91229697c8dde0489f220000d Reviewed-on: https://skia-review.googlesource.com/26884 Reviewed-by: Mike Reed <reed@google.com> Commit-Queue: Mike Reed <reed@google.com>
-rw-r--r--include/core/SkStream.h14
-rw-r--r--src/codec/SkAndroidCodec.cpp2
-rw-r--r--src/codec/SkCodec.cpp3
-rw-r--r--src/codec/SkIcoCodec.cpp2
-rw-r--r--src/codec/SkRawCodec.cpp6
-rw-r--r--src/core/SkFontDescriptor.cpp2
-rw-r--r--src/core/SkStream.cpp12
-rw-r--r--tests/CodecTest.cpp4
-rw-r--r--tests/GifTest.cpp7
-rw-r--r--tools/Resources.cpp4
10 files changed, 40 insertions, 16 deletions
diff --git a/include/core/SkStream.h b/include/core/SkStream.h
index 01fd82a530..1862117bb6 100644
--- a/include/core/SkStream.h
+++ b/include/core/SkStream.h
@@ -263,6 +263,11 @@ public:
~SkFILEStream() override;
+ static std::unique_ptr<SkFILEStream> Make(const char path[]) {
+ SkFILEStream* stream = new SkFILEStream(path);
+ return stream->isValid() ? std::unique_ptr<SkFILEStream>(stream) : nullptr;
+ }
+
/** Returns true if the current path could be opened. */
bool isValid() const { return fFILE != nullptr; }
@@ -308,6 +313,15 @@ public:
/** Creates the stream to read from the specified data */
SkMemoryStream(sk_sp<SkData>);
+ /** Returns a stream with a copy of the input data. */
+ static std::unique_ptr<SkMemoryStream> MakeCopy(const void* data, size_t length);
+
+ /** Returns a stream with a bare pointer reference to the input data. */
+ static std::unique_ptr<SkMemoryStream> MakeDirect(const void* data, size_t length);
+
+ /** Returns a stream with a shared reference to the input data. */
+ static std::unique_ptr<SkMemoryStream> Make(sk_sp<SkData> data);
+
/** Resets the stream to the specified data and length,
just like the constructor.
if copyData is true, the stream makes a private copy of the data
diff --git a/src/codec/SkAndroidCodec.cpp b/src/codec/SkAndroidCodec.cpp
index 3a23e06c8d..3a57352113 100644
--- a/src/codec/SkAndroidCodec.cpp
+++ b/src/codec/SkAndroidCodec.cpp
@@ -103,7 +103,7 @@ std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromData(sk_sp<SkData> data,
return nullptr;
}
- return MakeFromStream(skstd::make_unique<SkMemoryStream>(std::move(data)), chunkReader);
+ return MakeFromStream(SkMemoryStream::Make(std::move(data)), chunkReader);
}
SkColorType SkAndroidCodec::computeOutputColorType(SkColorType requestedColorType) {
diff --git a/src/codec/SkCodec.cpp b/src/codec/SkCodec.cpp
index d0958e836b..09343e2dff 100644
--- a/src/codec/SkCodec.cpp
+++ b/src/codec/SkCodec.cpp
@@ -123,8 +123,7 @@ std::unique_ptr<SkCodec> SkCodec::MakeFromData(sk_sp<SkData> data, SkPngChunkRea
if (!data) {
return nullptr;
}
- return MakeFromStream(std::unique_ptr<SkStream>(new SkMemoryStream(std::move(data))),
- nullptr, reader);
+ return MakeFromStream(SkMemoryStream::Make(std::move(data)), nullptr, reader);
}
SkCodec::SkCodec(int width, int height, const SkEncodedInfo& info,
diff --git a/src/codec/SkIcoCodec.cpp b/src/codec/SkIcoCodec.cpp
index fce14a2f35..24549faea4 100644
--- a/src/codec/SkIcoCodec.cpp
+++ b/src/codec/SkIcoCodec.cpp
@@ -144,7 +144,7 @@ std::unique_ptr<SkCodec> SkIcoCodec::MakeFromStream(std::unique_ptr<SkStream> st
}
sk_sp<SkData> data(SkData::MakeFromMalloc(buffer.release(), size));
- std::unique_ptr<SkMemoryStream> embeddedStream(new SkMemoryStream(data));
+ auto embeddedStream = SkMemoryStream::Make(data);
bytesRead += size;
// Check if the embedded codec is bmp or png and create the codec
diff --git a/src/codec/SkRawCodec.cpp b/src/codec/SkRawCodec.cpp
index dbcbd28499..cf41d17e9b 100644
--- a/src/codec/SkRawCodec.cpp
+++ b/src/codec/SkRawCodec.cpp
@@ -288,7 +288,7 @@ public:
}
}
}
- return skstd::make_unique<SkMemoryStream>(data);
+ return SkMemoryStream::Make(data);
}
private:
@@ -381,7 +381,7 @@ public:
sk_sp<SkData> data(SkData::MakeWithCopy(
static_cast<const uint8_t*>(fStream->getMemoryBase()) + offset, bytesToRead));
fStream.reset();
- return skstd::make_unique<SkMemoryStream>(data);
+ return SkMemoryStream::Make(data);
} else {
sk_sp<SkData> data(SkData::MakeUninitialized(bytesToRead));
if (!fStream->seek(offset)) {
@@ -391,7 +391,7 @@ public:
if (bytesRead < bytesToRead) {
data = SkData::MakeSubset(data.get(), 0, bytesRead);
}
- return skstd::make_unique<SkMemoryStream>(data);
+ return SkMemoryStream::Make(data);
}
}
private:
diff --git a/src/core/SkFontDescriptor.cpp b/src/core/SkFontDescriptor.cpp
index 73ea2058ce..519e8f2fe7 100644
--- a/src/core/SkFontDescriptor.cpp
+++ b/src/core/SkFontDescriptor.cpp
@@ -108,7 +108,7 @@ bool SkFontDescriptor::Deserialize(SkStream* stream, SkFontDescriptor* result) {
sk_sp<SkData> data(SkData::MakeUninitialized(length));
if (stream->read(data->writable_data(), length) == length) {
result->fFontData = skstd::make_unique<SkFontData>(
- skstd::make_unique<SkMemoryStream>(data), index, axis, axisCount);
+ SkMemoryStream::Make(std::move(data)), index, axis, axisCount);
} else {
SkDEBUGFAIL("Could not read font data");
return false;
diff --git a/src/core/SkStream.cpp b/src/core/SkStream.cpp
index 7bb2079f7f..139a72f7ca 100644
--- a/src/core/SkStream.cpp
+++ b/src/core/SkStream.cpp
@@ -274,6 +274,18 @@ SkMemoryStream::SkMemoryStream(sk_sp<SkData> data) : fData(std::move(data)) {
fOffset = 0;
}
+std::unique_ptr<SkMemoryStream> SkMemoryStream::MakeCopy(const void* data, size_t length) {
+ return skstd::make_unique<SkMemoryStream>(data, length, true);
+}
+
+std::unique_ptr<SkMemoryStream> SkMemoryStream::MakeDirect(const void* data, size_t length) {
+ return skstd::make_unique<SkMemoryStream>(data, length, false);
+}
+
+std::unique_ptr<SkMemoryStream> SkMemoryStream::Make(sk_sp<SkData> data) {
+ return skstd::make_unique<SkMemoryStream>(std::move(data));
+}
+
void SkMemoryStream::setMemoryOwned(const void* src, size_t size) {
fData = SkData::MakeFromMalloc(src, size);
fOffset = 0;
diff --git a/tests/CodecTest.cpp b/tests/CodecTest.cpp
index 698af46de7..4e68cd1ac6 100644
--- a/tests/CodecTest.cpp
+++ b/tests/CodecTest.cpp
@@ -1389,8 +1389,8 @@ DEF_TEST(Codec_InvalidImages, r) {
static void test_invalid_header(skiatest::Reporter* r, const char* path) {
SkString resourcePath = GetResourcePath(path);
- std::unique_ptr<SkFILEStream> stream(new SkFILEStream(resourcePath.c_str()));
- if (!stream->isValid()) {
+ auto stream = SkFILEStream::Make(resourcePath.c_str());
+ if (!stream) {
return;
}
diff --git a/tests/GifTest.cpp b/tests/GifTest.cpp
index daa5cf56d0..29150baeb1 100644
--- a/tests/GifTest.cpp
+++ b/tests/GifTest.cpp
@@ -225,10 +225,9 @@ DEF_TEST(Gif, reporter) {
// Regression test for decoding a gif image with sampleSize of 4, which was
// previously crashing.
DEF_TEST(Gif_Sampled, r) {
- std::unique_ptr<SkFILEStream> stream(
- new SkFILEStream(GetResourcePath("test640x479.gif").c_str()));
- REPORTER_ASSERT(r, stream->isValid());
- if (!stream->isValid()) {
+ auto stream = SkFILEStream::Make(GetResourcePath("test640x479.gif").c_str());
+ REPORTER_ASSERT(r, stream);
+ if (!stream) {
return;
}
diff --git a/tools/Resources.cpp b/tools/Resources.cpp
index 27ccef44f6..948949bf04 100644
--- a/tools/Resources.cpp
+++ b/tools/Resources.cpp
@@ -46,8 +46,8 @@ sk_sp<SkImage> GetResourceAsImage(const char* resource) {
std::unique_ptr<SkStreamAsset> GetResourceAsStream(const char* resource) {
SkString resourcePath = GetResourcePath(resource);
- std::unique_ptr<SkFILEStream> stream(new SkFILEStream(resourcePath.c_str()));
- if (!stream->isValid()) {
+ auto stream = SkFILEStream::Make(resourcePath.c_str());
+ if (!stream) {
SkDebugf("Resource %s not found.\n", resource);
return nullptr;
}