OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2010 Google Inc. | 3 * Copyright 2010 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #include "SkData.h" | 10 #include "SkData.h" |
11 #include "SkFlate.h" | 11 #include "SkFlate.h" |
12 #include "SkPDFCatalog.h" | 12 #include "SkPDFCatalog.h" |
13 #include "SkPDFStream.h" | 13 #include "SkPDFStream.h" |
14 #include "SkStream.h" | 14 #include "SkStream.h" |
| 15 #include "SkStreamPriv.h" |
15 | 16 |
16 static bool skip_compression(SkPDFCatalog* catalog) { | 17 static bool skip_compression(SkPDFCatalog* catalog) { |
17 return SkToBool(catalog->getDocumentFlags() & | 18 return SkToBool(catalog->getDocumentFlags() & |
18 SkPDFDocument::kFavorSpeedOverSize_Flags); | 19 SkPDFDocument::kFavorSpeedOverSize_Flags); |
19 } | 20 } |
20 | 21 |
21 SkPDFStream::SkPDFStream(SkStream* stream) : fState(kUnused_State) { | 22 SkPDFStream::SkPDFStream(SkStream* stream) : fState(kUnused_State) { |
22 setData(stream); | 23 this->setData(stream); |
23 } | 24 } |
24 | 25 |
25 SkPDFStream::SkPDFStream(SkData* data) : fState(kUnused_State) { | 26 SkPDFStream::SkPDFStream(SkData* data) : fState(kUnused_State) { |
26 setData(data); | 27 this->setData(data); |
27 } | 28 } |
28 | 29 |
29 SkPDFStream::SkPDFStream(const SkPDFStream& pdfStream) | 30 SkPDFStream::SkPDFStream(const SkPDFStream& pdfStream) |
30 : SkPDFDict(), | 31 : SkPDFDict(), |
31 fState(kUnused_State) { | 32 fState(kUnused_State) { |
32 setData(pdfStream.fData.get()); | 33 this->setData(pdfStream.fData.get()); |
33 bool removeLength = true; | 34 bool removeLength = true; |
34 // Don't uncompress an already compressed stream, but we could. | 35 // Don't uncompress an already compressed stream, but we could. |
35 if (pdfStream.fState == kCompressed_State) { | 36 if (pdfStream.fState == kCompressed_State) { |
36 fState = kCompressed_State; | 37 fState = kCompressed_State; |
37 removeLength = false; | 38 removeLength = false; |
38 } | 39 } |
39 this->mergeFrom(pdfStream); | 40 this->mergeFrom(pdfStream); |
40 if (removeLength) { | 41 if (removeLength) { |
41 this->remove("Length"); | 42 this->remove("Length"); |
42 } | 43 } |
43 } | 44 } |
44 | 45 |
45 SkPDFStream::~SkPDFStream() {} | 46 SkPDFStream::~SkPDFStream() {} |
46 | 47 |
47 void SkPDFStream::emitObject(SkWStream* stream, SkPDFCatalog* catalog, | 48 void SkPDFStream::emitObject(SkWStream* stream, SkPDFCatalog* catalog, |
48 bool indirect) { | 49 bool indirect) { |
49 if (indirect) { | 50 if (indirect) { |
50 return emitIndirectObject(stream, catalog); | 51 return emitIndirectObject(stream, catalog); |
51 } | 52 } |
| 53 SkAutoMutexAcquire lock(fMutex); // multiple threads could be calling emit |
52 if (!this->populate(catalog)) { | 54 if (!this->populate(catalog)) { |
53 return fSubstitute->emitObject(stream, catalog, indirect); | 55 return fSubstitute->emitObject(stream, catalog, indirect); |
54 } | 56 } |
55 | 57 |
56 this->INHERITED::emitObject(stream, catalog, false); | 58 this->INHERITED::emitObject(stream, catalog, false); |
57 stream->writeText(" stream\n"); | 59 stream->writeText(" stream\n"); |
58 stream->writeStream(fData.get(), fData->getLength()); | 60 if (fData.get()) { |
59 fData->rewind(); | 61 stream->write(fData->data(), fData->size()); |
| 62 } |
60 stream->writeText("\nendstream"); | 63 stream->writeText("\nendstream"); |
61 } | 64 } |
62 | 65 |
63 size_t SkPDFStream::getOutputSize(SkPDFCatalog* catalog, bool indirect) { | 66 size_t SkPDFStream::getOutputSize(SkPDFCatalog* catalog, bool indirect) { |
64 if (indirect) { | 67 if (indirect) { |
65 return getIndirectOutputSize(catalog); | 68 return getIndirectOutputSize(catalog); |
66 } | 69 } |
| 70 SkAutoMutexAcquire lock(fMutex); // multiple threads could be calling emit |
67 if (!this->populate(catalog)) { | 71 if (!this->populate(catalog)) { |
68 return fSubstitute->getOutputSize(catalog, indirect); | 72 return fSubstitute->getOutputSize(catalog, indirect); |
69 } | 73 } |
70 | 74 |
71 return this->INHERITED::getOutputSize(catalog, false) + | 75 return this->INHERITED::getOutputSize(catalog, false) + |
72 strlen(" stream\n\nendstream") + fData->getLength(); | 76 strlen(" stream\n\nendstream") + this->dataSize(); |
73 } | 77 } |
74 | 78 |
75 SkPDFStream::SkPDFStream() : fState(kUnused_State) {} | 79 SkPDFStream::SkPDFStream() : fState(kUnused_State) {} |
76 | 80 |
77 void SkPDFStream::setData(SkData* data) { | 81 void SkPDFStream::setData(SkData* data) { |
78 SkMemoryStream* stream = new SkMemoryStream; | 82 fData.reset(SkSafeRef(data)); |
79 stream->setData(data); | |
80 fData.reset(stream); // Transfer ownership. | |
81 } | 83 } |
82 | 84 |
83 void SkPDFStream::setData(SkStream* stream) { | 85 void SkPDFStream::setData(SkStream* stream) { |
84 // Code assumes that the stream starts at the beginning and is rewindable. | 86 // Code assumes that the stream starts at the beginning and is rewindable. |
85 if (stream) { | 87 if (stream) { |
86 SkASSERT(stream->getPosition() == 0); | 88 SkASSERT(stream->getPosition() == 0); |
87 SkASSERT(stream->rewind()); | 89 fData.reset(SkCopyStreamToData(stream)); |
| 90 SkAssertResult(stream->rewind()); |
| 91 } else { |
| 92 fData.reset(NULL); |
88 } | 93 } |
89 fData.reset(stream); | 94 } |
90 SkSafeRef(stream); | 95 |
| 96 size_t SkPDFStream::dataSize() const { |
| 97 return fData.get() ? fData->size() : 0; |
91 } | 98 } |
92 | 99 |
93 bool SkPDFStream::populate(SkPDFCatalog* catalog) { | 100 bool SkPDFStream::populate(SkPDFCatalog* catalog) { |
94 if (fState == kUnused_State) { | 101 if (fState == kUnused_State) { |
95 if (!skip_compression(catalog) && SkFlate::HaveFlate()) { | 102 if (!skip_compression(catalog) && SkFlate::HaveFlate()) { |
96 SkDynamicMemoryWStream compressedData; | 103 SkDynamicMemoryWStream compressedData; |
97 | 104 |
98 SkAssertResult(SkFlate::Deflate(fData.get(), &compressedData)); | 105 SkAssertResult(SkFlate::Deflate(fData.get(), &compressedData)); |
99 if (compressedData.getOffset() < fData->getLength()) { | 106 if (compressedData.getOffset() < this->dataSize()) { |
100 SkMemoryStream* stream = new SkMemoryStream; | 107 fData.reset(compressedData.copyToData()); |
101 stream->setData(compressedData.copyToData())->unref(); | |
102 fData.reset(stream); // Transfer ownership. | |
103 insertName("Filter", "FlateDecode"); | 108 insertName("Filter", "FlateDecode"); |
104 } | 109 } |
105 fState = kCompressed_State; | 110 fState = kCompressed_State; |
106 } else { | 111 } else { |
107 fState = kNoCompression_State; | 112 fState = kNoCompression_State; |
108 } | 113 } |
109 insertInt("Length", fData->getLength()); | 114 insertInt("Length", this->dataSize()); |
110 } else if (fState == kNoCompression_State && !skip_compression(catalog) && | 115 } else if (fState == kNoCompression_State && !skip_compression(catalog) && |
111 SkFlate::HaveFlate()) { | 116 SkFlate::HaveFlate()) { |
112 if (!fSubstitute.get()) { | 117 if (!fSubstitute.get()) { |
113 fSubstitute.reset(new SkPDFStream(*this)); | 118 fSubstitute.reset(new SkPDFStream(*this)); |
114 catalog->setSubstitute(this, fSubstitute.get()); | 119 catalog->setSubstitute(this, fSubstitute.get()); |
115 } | 120 } |
116 return false; | 121 return false; |
117 } | 122 } |
118 return true; | 123 return true; |
119 } | 124 } |
OLD | NEW |