SkPDF: SkPDFStream takes only SkStreamAsset

This is possible since https://crrev.com/869763002. Later, I'll clean
up the logic and class constructors.  For now, I assert everywhere I
expect a SkStreamAsset to be well-behaved (duplcate, hasLength).
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2098393002

Review-Url: https://codereview.chromium.org/2098393002
This commit is contained in:
halcanary 2016-06-29 06:31:32 -07:00 committed by Commit bot
parent b8d6af169b
commit 29ed2ae2da
6 changed files with 18 additions and 13 deletions

View File

@ -591,7 +591,7 @@ static size_t get_subset_font_stream(const char* fontName,
const SkTDArray<uint32_t>& subset,
SkPDFStream** fontStream) {
int ttcIndex;
std::unique_ptr<SkStream> fontData(typeface->openStream(&ttcIndex));
std::unique_ptr<SkStreamAsset> fontData(typeface->openStream(&ttcIndex));
SkASSERT(fontData);
if (!fontData) {
return 0;

View File

@ -43,7 +43,7 @@ SkPDFFormXObject::SkPDFFormXObject(SkPDFDevice* device) {
/**
* Creates a FormXObject from a content stream and associated resources.
*/
SkPDFFormXObject::SkPDFFormXObject(SkStream* content, SkRect bbox,
SkPDFFormXObject::SkPDFFormXObject(SkStreamAsset* content, SkRect bbox,
SkPDFDict* resourceDict) {
setData(content);

View File

@ -37,7 +37,7 @@ public:
* Create a PDF form XObject from a raw content stream and associated
* resources.
*/
explicit SkPDFFormXObject(SkStream* content,
explicit SkPDFFormXObject(SkStreamAsset* content,
SkRect bbox,
SkPDFDict* resourceDict);
virtual ~SkPDFFormXObject();

View File

@ -672,7 +672,7 @@ static void populate_tiling_pattern_dict(SkPDFDict* pattern,
* @param gsIndex A graphics state resource index to apply, or <0 if no
* graphics state to apply.
*/
static SkStream* create_pattern_fill_content(int gsIndex, SkRect& bounds) {
static SkStreamAsset* create_pattern_fill_content(int gsIndex, SkRect& bounds) {
SkDynamicMemoryWStream content;
if (gsIndex >= 0) {
SkPDFUtils::ApplyGraphicState(gsIndex, &content);
@ -699,7 +699,7 @@ static sk_sp<SkPDFObject> create_smask_graphic_state(
sk_sp<SkPDFObject> luminosityShader(
get_pdf_shader_by_state(doc, dpi, &alphaToLuminosityState));
std::unique_ptr<SkStream> alphaStream(create_pattern_fill_content(-1, bbox));
std::unique_ptr<SkStreamAsset> alphaStream(create_pattern_fill_content(-1, bbox));
auto resources =
get_gradient_resource_dict(luminosityShader.get(), nullptr);
@ -738,7 +738,7 @@ SkPDFAlphaFunctionShader* SkPDFAlphaFunctionShader::Create(
auto resourceDict =
get_gradient_resource_dict(colorShader.get(), alphaGs.get());
std::unique_ptr<SkStream> colorStream(
std::unique_ptr<SkStreamAsset> colorStream(
create_pattern_fill_content(0, bbox));
alphaFunctionShader->setData(colorStream.get());

View File

@ -24,7 +24,7 @@ void SkPDFStream::emitObject(SkWStream* stream,
SkASSERT(fCompressedData);
this->INHERITED::emitObject(stream, objNumMap, substitutes);
// duplicate (a cheap operation) preserves const on fCompressedData.
std::unique_ptr<SkStreamRewindable> dup(fCompressedData->duplicate());
std::unique_ptr<SkStreamAsset> dup(fCompressedData->duplicate());
SkASSERT(dup);
SkASSERT(dup->hasLength());
stream->writeText(" stream\n");
@ -33,13 +33,14 @@ void SkPDFStream::emitObject(SkWStream* stream,
}
void SkPDFStream::setData(SkStream* stream) {
void SkPDFStream::setData(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
std::unique_ptr<SkStreamRewindable> duplicate(stream->duplicate());
std::unique_ptr<SkStreamAsset> duplicate(stream->duplicate());
SkASSERT(duplicate && duplicate->hasLength());
if (duplicate && duplicate->hasLength()) {
this->insertInt("Length", duplicate->getLength());
fCompressedData.reset(duplicate.release());
@ -53,8 +54,12 @@ void SkPDFStream::setData(SkStream* stream) {
deflateWStream.finalize();
size_t length = compressedData.bytesWritten();
SkASSERT(stream->hasLength());
if (stream->hasLength()) {
std::unique_ptr<SkStreamRewindable> dup(stream->duplicate());
std::unique_ptr<SkStreamAsset> dup(stream->duplicate());
SkASSERT(stream->hasLength());
SkASSERT(stream->getLength() == dup->getLength());
SkASSERT(dup && dup->hasLength());
if (dup && dup->hasLength() &&
dup->getLength() <= length + strlen("/Filter_/FlateDecode_")) {
this->insertInt("Length", dup->getLength());

View File

@ -30,7 +30,7 @@ public:
* stream dictionary.
* @param stream The data part of the stream. Will not take ownership.
*/
explicit SkPDFStream(SkStream* stream) { this->setData(stream); }
explicit SkPDFStream(SkStreamAsset* stream) { this->setData(stream); }
virtual ~SkPDFStream();
@ -47,14 +47,14 @@ protected:
SkPDFStream() {}
/** Only call this function once. */
void setData(SkStream* stream);
void setData(SkStreamAsset* stream);
void setData(SkData* data) {
SkMemoryStream memoryStream(data);
this->setData(&memoryStream);
}
private:
std::unique_ptr<SkStreamRewindable> fCompressedData;
std::unique_ptr<SkStreamAsset> fCompressedData;
typedef SkPDFDict INHERITED;
};