Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(330)

Side by Side Diff: src/pdf/SkPDFStream.cpp

Issue 340783013: Switch SkPDFStream's internal storage from SkStream to SkData (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: AnotherPatchSet Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« src/pdf/SkPDFStream.h ('K') | « src/pdf/SkPDFStream.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "SkStreamHelpers.h" // CopyStreamToData
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 SkPDFDict::Iter dict(pdfStream); 40 SkPDFDict::Iter dict(pdfStream);
40 SkPDFName* key; 41 SkPDFName* key;
41 SkPDFObject* value; 42 SkPDFObject* value;
42 SkPDFName lengthName("Length"); 43 SkPDFName lengthName("Length");
(...skipping 11 matching lines...) Expand all
54 bool indirect) { 55 bool indirect) {
55 if (indirect) { 56 if (indirect) {
56 return emitIndirectObject(stream, catalog); 57 return emitIndirectObject(stream, catalog);
57 } 58 }
58 if (!this->populate(catalog)) { 59 if (!this->populate(catalog)) {
59 return fSubstitute->emitObject(stream, catalog, indirect); 60 return fSubstitute->emitObject(stream, catalog, indirect);
60 } 61 }
61 62
62 this->INHERITED::emitObject(stream, catalog, false); 63 this->INHERITED::emitObject(stream, catalog, false);
63 stream->writeText(" stream\n"); 64 stream->writeText(" stream\n");
64 stream->writeStream(fData.get(), fData->getLength()); 65 if (fData.get()) {
65 fData->rewind(); 66 stream->write(fData->data(), fData->size());
67 }
66 stream->writeText("\nendstream"); 68 stream->writeText("\nendstream");
67 } 69 }
68 70
69 size_t SkPDFStream::getOutputSize(SkPDFCatalog* catalog, bool indirect) { 71 size_t SkPDFStream::getOutputSize(SkPDFCatalog* catalog, bool indirect) {
70 if (indirect) { 72 if (indirect) {
71 return getIndirectOutputSize(catalog); 73 return getIndirectOutputSize(catalog);
72 } 74 }
73 if (!this->populate(catalog)) { 75 if (!this->populate(catalog)) {
74 return fSubstitute->getOutputSize(catalog, indirect); 76 return fSubstitute->getOutputSize(catalog, indirect);
75 } 77 }
76 78
77 return this->INHERITED::getOutputSize(catalog, false) + 79 return this->INHERITED::getOutputSize(catalog, false) +
78 strlen(" stream\n\nendstream") + fData->getLength(); 80 strlen(" stream\n\nendstream") + this->dataSize();
79 } 81 }
80 82
81 SkPDFStream::SkPDFStream() : fState(kUnused_State) {} 83 SkPDFStream::SkPDFStream() : fState(kUnused_State) {}
82 84
83 void SkPDFStream::setData(SkData* data) { 85 void SkPDFStream::setData(SkData* data) {
84 SkMemoryStream* stream = new SkMemoryStream; 86 fData.reset(SkSafeRef(data));
85 stream->setData(data);
86 fData.reset(stream); // Transfer ownership.
87 } 87 }
88 88
89 void SkPDFStream::setData(SkStream* stream) { 89 void SkPDFStream::setData(SkStream* stream) {
90 // Code assumes that the stream starts at the beginning and is rewindable. 90 // Code assumes that the stream starts at the beginning and is rewindable.
91 if (stream) { 91 if (stream) {
92 SkASSERT(stream->getPosition() == 0); 92 SkASSERT(stream->getPosition() == 0);
93 fData.reset(CopyStreamToData(stream));
93 SkASSERT(stream->rewind()); 94 SkASSERT(stream->rewind());
95 } else {
96 fData.reset(NULL);
94 } 97 }
95 fData.reset(stream); 98 }
96 SkSafeRef(stream); 99
100 size_t SkPDFStream::dataSize() {
101 return fData.get() ? fData->size() : 0;
97 } 102 }
98 103
99 bool SkPDFStream::populate(SkPDFCatalog* catalog) { 104 bool SkPDFStream::populate(SkPDFCatalog* catalog) {
105 SkAutoMutexAcquire lock(fMutex); // multiple threads could be calling emit
mtklein 2014/06/26 13:21:59 Why not lock in emitObject then? I'd recommend yo
hal.canary 2014/06/26 18:42:02 Done.
100 if (fState == kUnused_State) { 106 if (fState == kUnused_State) {
101 if (!skip_compression(catalog) && SkFlate::HaveFlate()) { 107 if (!skip_compression(catalog) && SkFlate::HaveFlate()) {
102 SkDynamicMemoryWStream compressedData; 108 SkDynamicMemoryWStream compressedData;
103 109
104 SkAssertResult(SkFlate::Deflate(fData.get(), &compressedData)); 110 SkAssertResult(SkFlate::Deflate(fData.get(), &compressedData));
105 if (compressedData.getOffset() < fData->getLength()) { 111 if (compressedData.getOffset() < this->dataSize()) {
106 SkMemoryStream* stream = new SkMemoryStream; 112 fData.reset(compressedData.copyToData());
107 stream->setData(compressedData.copyToData())->unref();
108 fData.reset(stream); // Transfer ownership.
109 insertName("Filter", "FlateDecode"); 113 insertName("Filter", "FlateDecode");
110 } 114 }
111 fState = kCompressed_State; 115 fState = kCompressed_State;
112 } else { 116 } else {
113 fState = kNoCompression_State; 117 fState = kNoCompression_State;
114 } 118 }
115 insertInt("Length", fData->getLength()); 119 insertInt("Length", this->dataSize());
116 } else if (fState == kNoCompression_State && !skip_compression(catalog) && 120 } else if (fState == kNoCompression_State && !skip_compression(catalog) &&
117 SkFlate::HaveFlate()) { 121 SkFlate::HaveFlate()) {
118 if (!fSubstitute.get()) { 122 if (!fSubstitute.get()) {
119 fSubstitute.reset(new SkPDFStream(*this)); 123 fSubstitute.reset(new SkPDFStream(*this));
120 catalog->setSubstitute(this, fSubstitute.get()); 124 catalog->setSubstitute(this, fSubstitute.get());
121 } 125 }
122 return false; 126 return false;
123 } 127 }
124 return true; 128 return true;
125 } 129 }
OLDNEW
« src/pdf/SkPDFStream.h ('K') | « src/pdf/SkPDFStream.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698