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
This commit is contained in:
halcanary 2016-07-27 11:12:23 -07:00 committed by Commit bot
parent cb571e1148
commit ac0e00dcec
8 changed files with 26 additions and 30 deletions

View File

@ -278,8 +278,7 @@ void SkPDFDocument::onEndPage() {
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());

View File

@ -640,7 +640,7 @@ static size_t get_subset_font_stream(const char* fontName,
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 @@ bool SkPDFType3Font::populate(uint16_t glyphID) {
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));

View File

@ -20,8 +20,7 @@ SkPDFFormXObject::SkPDFFormXObject(SkPDFDevice* device) {
// 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 @@ SkPDFFormXObject::SkPDFFormXObject(SkPDFDevice* device) {
/**
* 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());
}

View File

@ -37,7 +37,7 @@ public:
* 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();

View File

@ -704,7 +704,7 @@ static sk_sp<SkPDFObject> create_smask_graphic_state(
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 @@ SkPDFAlphaFunctionShader* SkPDFAlphaFunctionShader::Create(
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 @@ static sk_sp<SkPDFStream> make_ps_function(
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 @@ SkPDFImageShader* SkPDFImageShader::Create(
}
// 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,

View File

@ -32,13 +32,13 @@ void SkPDFStream::emitObject(SkWStream* stream,
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 @@ void SkPDFStream::setData(SkStreamAsset* stream) {
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;
}

View File

@ -28,9 +28,11 @@ public:
/** 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 @@ protected:
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:

View File

@ -83,9 +83,9 @@ static void assert_emit_eq(skiatest::Reporter* reporter,
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");