Index: src/pdf/SkPDFStream.cpp |
diff --git a/src/pdf/SkPDFStream.cpp b/src/pdf/SkPDFStream.cpp |
index 815ef481ee2135abab110171975687f6d8a6d28a..8715d9376b7f2f87907c7a65de880fd6fd9a3ebf 100644 |
--- a/src/pdf/SkPDFStream.cpp |
+++ b/src/pdf/SkPDFStream.cpp |
@@ -12,6 +12,7 @@ |
#include "SkPDFCatalog.h" |
#include "SkPDFStream.h" |
#include "SkStream.h" |
+#include "SkStreamPriv.h" |
static bool skip_compression(SkPDFCatalog* catalog) { |
return SkToBool(catalog->getDocumentFlags() & |
@@ -19,17 +20,17 @@ static bool skip_compression(SkPDFCatalog* catalog) { |
} |
SkPDFStream::SkPDFStream(SkStream* stream) : fState(kUnused_State) { |
- setData(stream); |
+ this->setData(stream); |
} |
SkPDFStream::SkPDFStream(SkData* data) : fState(kUnused_State) { |
- setData(data); |
+ this->setData(data); |
} |
SkPDFStream::SkPDFStream(const SkPDFStream& pdfStream) |
: SkPDFDict(), |
fState(kUnused_State) { |
- setData(pdfStream.fData.get()); |
+ this->setData(pdfStream.fData.get()); |
bool removeLength = true; |
// Don't uncompress an already compressed stream, but we could. |
if (pdfStream.fState == kCompressed_State) { |
@@ -49,14 +50,16 @@ void SkPDFStream::emitObject(SkWStream* stream, SkPDFCatalog* catalog, |
if (indirect) { |
return emitIndirectObject(stream, catalog); |
} |
+ SkAutoMutexAcquire lock(fMutex); // multiple threads could be calling emit |
if (!this->populate(catalog)) { |
return fSubstitute->emitObject(stream, catalog, indirect); |
} |
this->INHERITED::emitObject(stream, catalog, false); |
stream->writeText(" stream\n"); |
- stream->writeStream(fData.get(), fData->getLength()); |
- fData->rewind(); |
+ if (fData.get()) { |
+ stream->write(fData->data(), fData->size()); |
+ } |
stream->writeText("\nendstream"); |
} |
@@ -64,30 +67,34 @@ size_t SkPDFStream::getOutputSize(SkPDFCatalog* catalog, bool indirect) { |
if (indirect) { |
return getIndirectOutputSize(catalog); |
} |
+ SkAutoMutexAcquire lock(fMutex); // multiple threads could be calling emit |
if (!this->populate(catalog)) { |
return fSubstitute->getOutputSize(catalog, indirect); |
} |
return this->INHERITED::getOutputSize(catalog, false) + |
- strlen(" stream\n\nendstream") + fData->getLength(); |
+ strlen(" stream\n\nendstream") + this->dataSize(); |
} |
SkPDFStream::SkPDFStream() : fState(kUnused_State) {} |
void SkPDFStream::setData(SkData* data) { |
- SkMemoryStream* stream = new SkMemoryStream; |
- stream->setData(data); |
- fData.reset(stream); // Transfer ownership. |
+ fData.reset(SkSafeRef(data)); |
} |
void SkPDFStream::setData(SkStream* stream) { |
// Code assumes that the stream starts at the beginning and is rewindable. |
if (stream) { |
SkASSERT(stream->getPosition() == 0); |
- SkASSERT(stream->rewind()); |
+ fData.reset(SkCopyStreamToData(stream)); |
+ SkAssertResult(stream->rewind()); |
+ } else { |
+ fData.reset(NULL); |
} |
- fData.reset(stream); |
- SkSafeRef(stream); |
+} |
+ |
+size_t SkPDFStream::dataSize() const { |
+ return fData.get() ? fData->size() : 0; |
} |
bool SkPDFStream::populate(SkPDFCatalog* catalog) { |
@@ -96,17 +103,15 @@ bool SkPDFStream::populate(SkPDFCatalog* catalog) { |
SkDynamicMemoryWStream compressedData; |
SkAssertResult(SkFlate::Deflate(fData.get(), &compressedData)); |
- if (compressedData.getOffset() < fData->getLength()) { |
- SkMemoryStream* stream = new SkMemoryStream; |
- stream->setData(compressedData.copyToData())->unref(); |
- fData.reset(stream); // Transfer ownership. |
+ if (compressedData.getOffset() < this->dataSize()) { |
+ fData.reset(compressedData.copyToData()); |
insertName("Filter", "FlateDecode"); |
} |
fState = kCompressed_State; |
} else { |
fState = kNoCompression_State; |
} |
- insertInt("Length", fData->getLength()); |
+ insertInt("Length", this->dataSize()); |
} else if (fState == kNoCompression_State && !skip_compression(catalog) && |
SkFlate::HaveFlate()) { |
if (!fSubstitute.get()) { |