SkPDF: SkPDFStream takes only SkStreamAsset
authorhalcanary <halcanary@google.com>
Wed, 29 Jun 2016 13:31:32 +0000 (06:31 -0700)
committerCommit bot <commit-bot@chromium.org>
Wed, 29 Jun 2016 13:31:32 +0000 (06:31 -0700)
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

src/pdf/SkPDFFont.cpp
src/pdf/SkPDFFormXObject.cpp
src/pdf/SkPDFFormXObject.h
src/pdf/SkPDFShader.cpp
src/pdf/SkPDFStream.cpp
src/pdf/SkPDFStream.h

index b213594d4a37c3f8fed7f3c5667d056eeb020186..bdde6d62af1c722717eaad3a304c4c5a76ef141d 100644 (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;
index 99617e9c01ba7256bedabcd1b83e8fd27ded90b2..25c37807e048b00fa7208e99c32bd3c434e18f68 100644 (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);
 
index f6d708ebe94f98e39136f7889dd8b4f05d60a99b..f739a36d200d38184ae4dee4b9ab2386387e0656 100644 (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();
index 9a2e4a91030b95ab17eea92acb9071bce5e8e4d9..942fe65d2651b9705e39ddc90b221c7d0ab1b96c 100644 (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());
 
index d15f48a2ee94590e30588aaf8f55a6510a1e2fc9..50735efa6cb112d614e2bbce79d763cdedb53c56 100644 (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());
index 11a0c9769e11440bd514dfac8ef93049f17bc90e..f487dd296ea5dcf60fc1393c5aeda6d32d8b212e 100644 (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;
 };