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
diff --git a/src/pdf/SkPDFFont.cpp b/src/pdf/SkPDFFont.cpp
index b213594..bdde6d6 100644
--- a/src/pdf/SkPDFFont.cpp
+++ b/src/pdf/SkPDFFont.cpp
@@ -591,7 +591,7 @@
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;
diff --git a/src/pdf/SkPDFFormXObject.cpp b/src/pdf/SkPDFFormXObject.cpp
index 99617e9..25c3780 100644
--- a/src/pdf/SkPDFFormXObject.cpp
+++ b/src/pdf/SkPDFFormXObject.cpp
@@ -43,7 +43,7 @@
/**
* 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);
diff --git a/src/pdf/SkPDFFormXObject.h b/src/pdf/SkPDFFormXObject.h
index f6d708e..f739a36 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(SkStream* content,
+ explicit SkPDFFormXObject(SkStreamAsset* content,
SkRect bbox,
SkPDFDict* resourceDict);
virtual ~SkPDFFormXObject();
diff --git a/src/pdf/SkPDFShader.cpp b/src/pdf/SkPDFShader.cpp
index 9a2e4a9..942fe65 100644
--- a/src/pdf/SkPDFShader.cpp
+++ b/src/pdf/SkPDFShader.cpp
@@ -672,7 +672,7 @@
* @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 @@
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 @@
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());
diff --git a/src/pdf/SkPDFStream.cpp b/src/pdf/SkPDFStream.cpp
index d15f48a..50735ef 100644
--- a/src/pdf/SkPDFStream.cpp
+++ b/src/pdf/SkPDFStream.cpp
@@ -24,7 +24,7 @@
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::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 @@
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());
diff --git a/src/pdf/SkPDFStream.h b/src/pdf/SkPDFStream.h
index 11a0c97..f487dd2 100644
--- a/src/pdf/SkPDFStream.h
+++ b/src/pdf/SkPDFStream.h
@@ -30,7 +30,7 @@
* 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 @@
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;
};