SkPDF: SkPDFStream takes a unique_ptr
BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2188623004
Review-Url: https://codereview.chromium.org/2188623004
diff --git a/src/pdf/SkPDFDocument.cpp b/src/pdf/SkPDFDocument.cpp
index 221f240..2b7eda1 100644
--- a/src/pdf/SkPDFDocument.cpp
+++ b/src/pdf/SkPDFDocument.cpp
@@ -278,8 +278,7 @@
if (annotations->size() > 0) {
page->insertObject("Annots", std::move(annotations));
}
- auto contentData = fPageDevice->content();
- auto contentObject = sk_make_sp<SkPDFStream>(contentData.get());
+ auto contentObject = sk_make_sp<SkPDFStream>(fPageDevice->content());
this->serialize(contentObject);
page->insertObjRef("Contents", std::move(contentObject));
fPageDevice->appendDestinations(fDests.get(), page.get());
diff --git a/src/pdf/SkPDFFont.cpp b/src/pdf/SkPDFFont.cpp
index f088ed5..2f7137f 100644
--- a/src/pdf/SkPDFFont.cpp
+++ b/src/pdf/SkPDFFont.cpp
@@ -640,7 +640,7 @@
fontData->rewind();
// Fail over: just embed the whole font.
- *fontStream = new SkPDFStream(fontData.get());
+ *fontStream = new SkPDFStream(std::move(fontData));
return fontSize;
}
#endif
@@ -1356,11 +1356,9 @@
SkPDFUtils::PaintPath(paint.getStyle(), path->getFillType(),
&content);
}
- std::unique_ptr<SkMemoryStream> glyphStream(new SkMemoryStream());
- glyphStream->setData(content.copyToData())->unref();
-
charProcs->insertObjRef(
- characterName, sk_make_sp<SkPDFStream>(glyphStream.get()));
+ characterName, sk_make_sp<SkPDFStream>(
+ std::unique_ptr<SkStreamAsset>(content.detachAsStream())));
}
encoding->insertObject("Differences", std::move(encDiffs));
diff --git a/src/pdf/SkPDFFormXObject.cpp b/src/pdf/SkPDFFormXObject.cpp
index 25c3780..ba49df8 100644
--- a/src/pdf/SkPDFFormXObject.cpp
+++ b/src/pdf/SkPDFFormXObject.cpp
@@ -20,8 +20,7 @@
// resources).
auto resourceDict = device->makeResourceDict();
- auto content = device->content();
- this->setData(content.get());
+ this->setData(device->content());
sk_sp<SkPDFArray> bboxArray(device->copyMediaBox());
this->init(nullptr, resourceDict.get(), bboxArray.get());
@@ -43,10 +42,10 @@
/**
* Creates a FormXObject from a content stream and associated resources.
*/
-SkPDFFormXObject::SkPDFFormXObject(SkStreamAsset* content, SkRect bbox,
+SkPDFFormXObject::SkPDFFormXObject(std::unique_ptr<SkStreamAsset> content,
+ SkRect bbox,
SkPDFDict* resourceDict) {
- setData(content);
-
+ this->setData(std::move(content));
sk_sp<SkPDFArray> bboxArray(SkPDFUtils::RectToArray(bbox));
this->init("DeviceRGB", resourceDict, bboxArray.get());
}
diff --git a/src/pdf/SkPDFFormXObject.h b/src/pdf/SkPDFFormXObject.h
index f739a36..a483a60 100644
--- a/src/pdf/SkPDFFormXObject.h
+++ b/src/pdf/SkPDFFormXObject.h
@@ -37,7 +37,7 @@
* Create a PDF form XObject from a raw content stream and associated
* resources.
*/
- explicit SkPDFFormXObject(SkStreamAsset* content,
+ explicit SkPDFFormXObject(std::unique_ptr<SkStreamAsset> content,
SkRect bbox,
SkPDFDict* resourceDict);
virtual ~SkPDFFormXObject();
diff --git a/src/pdf/SkPDFShader.cpp b/src/pdf/SkPDFShader.cpp
index 37df296..13b4479 100644
--- a/src/pdf/SkPDFShader.cpp
+++ b/src/pdf/SkPDFShader.cpp
@@ -704,7 +704,7 @@
get_gradient_resource_dict(luminosityShader.get(), nullptr);
sk_sp<SkPDFFormXObject> alphaMask(
- new SkPDFFormXObject(alphaStream.get(), bbox, resources.get()));
+ new SkPDFFormXObject(std::move(alphaStream), bbox, resources.get()));
return SkPDFGraphicState::GetSMaskGraphicState(
alphaMask.get(), false,
@@ -739,7 +739,7 @@
std::unique_ptr<SkStreamAsset> colorStream(
create_pattern_fill_content(0, bbox));
- alphaFunctionShader->setData(colorStream.get());
+ alphaFunctionShader->setData(std::move(colorStream));
populate_tiling_pattern_dict(alphaFunctionShader, bbox, resourceDict.get(),
SkMatrix::I());
@@ -801,7 +801,7 @@
std::unique_ptr<SkStreamAsset> psCode,
SkPDFArray* domain,
sk_sp<SkPDFObject> range) {
- auto result = sk_make_sp<SkPDFStream>(psCode.get());
+ auto result = sk_make_sp<SkPDFStream>(std::move(psCode));
result->insertInt("FunctionType", 4);
result->insertObject("Domain", sk_ref_sp(domain));
result->insertObject("Range", std::move(range));
@@ -1207,10 +1207,8 @@
}
// Put the canvas into the pattern stream (fContent).
- auto content = patternDevice->content();
-
SkPDFImageShader* imageShader = new SkPDFImageShader(autoState->release());
- imageShader->setData(content.get());
+ imageShader->setData(patternDevice->content());
auto resourceDict = patternDevice->makeResourceDict();
populate_tiling_pattern_dict(imageShader, patternBBox,
diff --git a/src/pdf/SkPDFStream.cpp b/src/pdf/SkPDFStream.cpp
index a9b5864..a661456 100644
--- a/src/pdf/SkPDFStream.cpp
+++ b/src/pdf/SkPDFStream.cpp
@@ -32,13 +32,13 @@
stream->writeText("\nendstream");
}
-void SkPDFStream::setData(SkStreamAsset* stream) {
+void SkPDFStream::setData(std::unique_ptr<SkStreamAsset> stream) {
SkASSERT(!fCompressedData); // Only call this function once.
SkASSERT(stream);
// Code assumes that the stream starts at the beginning.
#ifdef SK_PDF_LESS_COMPRESSION
- fCompressedData.reset(stream->duplicate());
+ fCompressedData = std::move(stream);
SkASSERT(fCompressedData && fCompressedData->hasLength());
this->insertInt("Length", fCompressedData->getLength());
#else
@@ -46,13 +46,14 @@
SkASSERT(stream->hasLength());
SkDynamicMemoryWStream compressedData;
SkDeflateWStream deflateWStream(&compressedData);
- SkStreamCopy(&deflateWStream, stream);
+ SkStreamCopy(&deflateWStream, stream.get());
deflateWStream.finalize();
size_t compressedLength = compressedData.bytesWritten();
size_t originalLength = stream->getLength();
if (originalLength <= compressedLength + strlen("/Filter_/FlateDecode_")) {
- fCompressedData.reset(stream->duplicate());
+ SkAssertResult(stream->rewind());
+ fCompressedData = std::move(stream);
this->insertInt("Length", originalLength);
return;
}
diff --git a/src/pdf/SkPDFStream.h b/src/pdf/SkPDFStream.h
index f487dd2..0cc2d42 100644
--- a/src/pdf/SkPDFStream.h
+++ b/src/pdf/SkPDFStream.h
@@ -28,9 +28,11 @@
/** Create a PDF stream. A Length entry is automatically added to the
* stream dictionary.
- * @param stream The data part of the stream. Will not take ownership.
+ * @param stream The data part of the stream.
*/
- explicit SkPDFStream(SkStreamAsset* stream) { this->setData(stream); }
+ explicit SkPDFStream(std::unique_ptr<SkStreamAsset> stream) {
+ this->setData(std::move(stream));
+ }
virtual ~SkPDFStream();
@@ -47,10 +49,9 @@
SkPDFStream() {}
/** Only call this function once. */
- void setData(SkStreamAsset* stream);
+ void setData(std::unique_ptr<SkStreamAsset> stream);
void setData(SkData* data) {
- SkMemoryStream memoryStream(data);
- this->setData(&memoryStream);
+ this->setData(std::unique_ptr<SkStreamAsset>(new SkMemoryStream(data)));
}
private:
diff --git a/tests/PDFPrimitivesTest.cpp b/tests/PDFPrimitivesTest.cpp
index cef1150..0664ef4 100644
--- a/tests/PDFPrimitivesTest.cpp
+++ b/tests/PDFPrimitivesTest.cpp
@@ -83,9 +83,9 @@
static void TestPDFStream(skiatest::Reporter* reporter) {
char streamBytes[] = "Test\nFoo\tBar";
- SkAutoTDelete<SkMemoryStream> streamData(new SkMemoryStream(
+ std::unique_ptr<SkStreamAsset> streamData(new SkMemoryStream(
streamBytes, strlen(streamBytes), true));
- sk_sp<SkPDFStream> stream(new SkPDFStream(streamData.get()));
+ auto stream = sk_make_sp<SkPDFStream>(std::move(streamData));
assert_emit_eq(reporter,
*stream,
"<</Length 12>> stream\nTest\nFoo\tBar\nendstream");