OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "printing/pdf_metafile_skia.h" | 5 #include "printing/pdf_metafile_skia.h" |
6 | 6 |
7 #include "base/containers/hash_tables.h" | 7 #include "base/containers/hash_tables.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/numerics/safe_conversions.h" | 10 #include "base/numerics/safe_conversions.h" |
11 #include "base/posix/eintr_wrapper.h" | 11 #include "base/posix/eintr_wrapper.h" |
12 #include "skia/ext/refptr.h" | 12 #include "skia/ext/refptr.h" |
13 #include "skia/ext/vector_canvas.h" | 13 #include "skia/ext/vector_canvas.h" |
14 #include "third_party/skia/include/core/SkData.h" | 14 #include "third_party/skia/include/core/SkData.h" |
15 #include "third_party/skia/include/core/SkDocument.h" | |
16 #include "third_party/skia/include/core/SkPictureRecorder.h" | |
17 #include "third_party/skia/include/core/SkRect.h" | |
15 #include "third_party/skia/include/core/SkRefCnt.h" | 18 #include "third_party/skia/include/core/SkRefCnt.h" |
16 #include "third_party/skia/include/core/SkScalar.h" | 19 #include "third_party/skia/include/core/SkScalar.h" |
20 #include "third_party/skia/include/core/SkSize.h" | |
17 #include "third_party/skia/include/core/SkStream.h" | 21 #include "third_party/skia/include/core/SkStream.h" |
18 #include "third_party/skia/include/core/SkTypeface.h" | 22 #include "ui/gfx/geometry/size_conversions.h" |
19 #include "third_party/skia/include/pdf/SkPDFDevice.h" | |
20 #include "third_party/skia/include/pdf/SkPDFDocument.h" | |
21 #include "ui/gfx/point.h" | 23 #include "ui/gfx/point.h" |
22 #include "ui/gfx/rect.h" | 24 #include "ui/gfx/rect.h" |
23 #include "ui/gfx/size.h" | 25 #include "ui/gfx/size.h" |
26 #include "ui/gfx/skia_util.h" | |
24 | 27 |
25 #if defined(OS_MACOSX) | 28 #if defined(OS_MACOSX) |
26 #include "printing/pdf_metafile_cg_mac.h" | 29 #include "printing/pdf_metafile_cg_mac.h" |
27 #endif | 30 #endif |
28 | 31 |
29 #if defined(OS_POSIX) | 32 #if defined(OS_POSIX) |
30 #include "base/file_descriptor_posix.h" | 33 #include "base/file_descriptor_posix.h" |
31 #endif | 34 #endif |
32 | 35 |
36 namespace { | |
37 // This struct represents all the data we need to draw and redraw this | |
38 // page into a SkDocument. | |
39 struct Page { | |
40 Page(const SkSize& page_size, const SkRect& content_area) | |
41 : page_size_(page_size), | |
42 content_area_(content_area), | |
43 content_(/*NULL*/) {} | |
44 SkSize page_size_; | |
45 SkRect content_area_; | |
46 skia::RefPtr<SkPicture> content_; | |
47 }; | |
48 } // namespace | |
49 | |
50 static bool WriteAssetToBuffer(const SkStreamAsset* asset, | |
51 void* buffer, | |
52 size_t size) { | |
53 skia::RefPtr<SkStreamAsset> assetCopy = skia::AdoptRef(asset->duplicate()); | |
54 // keeps original asset state unchanged. | |
55 size_t length = assetCopy->getLength(); | |
56 if (length > size) | |
57 return false; | |
58 return (length == assetCopy->read(buffer, length)); | |
59 } | |
60 | |
33 namespace printing { | 61 namespace printing { |
34 | 62 |
35 struct PdfMetafileSkiaData { | 63 struct PdfMetafileSkiaData { |
36 skia::RefPtr<SkPDFDevice> current_page_; | 64 SkPictureRecorder recorder_; // Current recording |
37 skia::RefPtr<SkCanvas> current_page_canvas_; | 65 |
38 SkPDFDocument pdf_doc_; | 66 std::vector<Page> pages_; |
39 SkDynamicMemoryWStream pdf_stream_; | 67 skia::RefPtr<SkStreamAsset> pdf_data_; |
68 | |
40 #if defined(OS_MACOSX) | 69 #if defined(OS_MACOSX) |
41 PdfMetafileCg pdf_cg_; | 70 PdfMetafileCg pdf_cg_; |
42 #endif | 71 #endif |
43 }; | 72 }; |
44 | 73 |
45 PdfMetafileSkia::~PdfMetafileSkia() {} | 74 PdfMetafileSkia::~PdfMetafileSkia() {} |
46 | 75 |
47 bool PdfMetafileSkia::Init() { | 76 bool PdfMetafileSkia::Init() { |
48 return true; | 77 return true; |
49 } | 78 } |
79 | |
80 // TODO(halcanary): Create a Metafile class that only stores data. | |
81 // Metafile::InitFromData is orthogonal to what the rest of | |
82 // PdfMetafileSkia does. | |
50 bool PdfMetafileSkia::InitFromData(const void* src_buffer, | 83 bool PdfMetafileSkia::InitFromData(const void* src_buffer, |
51 uint32 src_buffer_size) { | 84 uint32 src_buffer_size) { |
52 return data_->pdf_stream_.write(src_buffer, src_buffer_size); | 85 data_->pdf_data_ = |
86 skia::AdoptRef(new SkMemoryStream(src_buffer, src_buffer_size, true)); | |
87 return true; | |
53 } | 88 } |
54 | 89 |
55 bool PdfMetafileSkia::StartPage(const gfx::Size& page_size, | 90 bool PdfMetafileSkia::StartPage(const gfx::Size& page_size, |
56 const gfx::Rect& content_area, | 91 const gfx::Rect& content_area, |
57 const float& scale_factor) { | 92 const float& scale_factor) { |
58 DCHECK(!data_->current_page_canvas_); | 93 if (data_->recorder_.getRecordingCanvas()) |
94 this->FinishPage(); | |
95 DCHECK(!data_->recorder_.getRecordingCanvas()); | |
96 SkSize sk_page_size = gfx::SizeFToSkSize(gfx::SizeF(page_size)); | |
97 data_->pages_.push_back(Page(sk_page_size, gfx::RectToSkRect(content_area))); | |
59 | 98 |
60 // Adjust for the margins and apply the scale factor. | 99 SkCanvas* recordingCanvas = data_->recorder_.beginRecording( |
61 SkMatrix transform; | 100 sk_page_size.width(), sk_page_size.height(), NULL, 0); |
62 transform.setTranslate(SkIntToScalar(content_area.x()), | 101 // recordingCanvas is owned by the data_->recorder_. No ref() necessary. |
63 SkIntToScalar(content_area.y())); | 102 if (!recordingCanvas) |
64 transform.preScale(SkFloatToScalar(scale_factor), | 103 return false; |
65 SkFloatToScalar(scale_factor)); | 104 recordingCanvas->scale(scale_factor, scale_factor); |
66 | |
67 SkISize pdf_page_size = SkISize::Make(page_size.width(), page_size.height()); | |
68 SkISize pdf_content_size = | |
69 SkISize::Make(content_area.width(), content_area.height()); | |
70 | |
71 data_->current_page_ = skia::AdoptRef( | |
72 new SkPDFDevice(pdf_page_size, pdf_content_size, transform)); | |
73 data_->current_page_canvas_ = | |
74 skia::AdoptRef(new SkCanvas(data_->current_page_.get())); | |
75 return true; | 105 return true; |
76 } | 106 } |
77 | 107 |
78 skia::VectorCanvas* PdfMetafileSkia::GetVectorCanvasForNewPage( | 108 skia::VectorCanvas* PdfMetafileSkia::GetVectorCanvasForNewPage( |
79 const gfx::Size& page_size, | 109 const gfx::Size& page_size, |
80 const gfx::Rect& content_area, | 110 const gfx::Rect& content_area, |
81 const float& scale_factor) { | 111 const float& scale_factor) { |
82 if (!StartPage(page_size, content_area, scale_factor)) | 112 if (!StartPage(page_size, content_area, scale_factor)) |
83 return nullptr; | 113 return nullptr; |
84 return data_->current_page_canvas_.get(); | 114 return data_->recorder_.getRecordingCanvas(); |
85 } | 115 } |
86 | 116 |
87 bool PdfMetafileSkia::FinishPage() { | 117 bool PdfMetafileSkia::FinishPage() { |
88 DCHECK(data_->current_page_canvas_); | 118 if (!data_->recorder_.getRecordingCanvas()) |
89 DCHECK(data_->current_page_); | 119 return false; |
90 | 120 DCHECK(!(data_->pages_.back().content_)); |
91 data_->current_page_canvas_.clear(); // Unref SkCanvas. | 121 data_->pages_.back().content_ = |
92 data_->pdf_doc_.appendPage(data_->current_page_.get()); | 122 skia::AdoptRef(data_->recorder_.endRecording()); |
93 return true; | 123 return true; |
94 } | 124 } |
95 | 125 |
96 bool PdfMetafileSkia::FinishDocument() { | 126 bool PdfMetafileSkia::FinishDocument() { |
97 // Don't do anything if we've already set the data in InitFromData. | 127 // If we've already set the data in InitFromData, overwrite it. |
98 if (data_->pdf_stream_.getOffset()) | 128 if (data_->pdf_data_) |
99 return true; | 129 data_->pdf_data_.clear(); // Free up RAM first. |
100 | 130 |
101 if (data_->current_page_canvas_) | 131 if (data_->recorder_.getRecordingCanvas()) |
102 FinishPage(); | 132 this->FinishPage(); |
103 | 133 |
104 data_->current_page_.clear(); | 134 SkDynamicMemoryWStream pdf_stream; |
135 skia::RefPtr<SkDocument> pdf_doc = | |
136 skia::AdoptRef(SkDocument::CreatePDF(&pdf_stream)); | |
137 for (const auto& page : data_->pages_) { | |
138 SkCanvas* canvas = pdf_doc->beginPage( | |
139 page.page_size_.width(), page.page_size_.height(), &page.content_area_); | |
140 canvas->drawPicture(page.content_.get()); | |
141 pdf_doc->endPage(); | |
142 } | |
143 if (!pdf_doc->close()) | |
144 return false; | |
105 | 145 |
106 int font_counts[SkAdvancedTypefaceMetrics::kOther_Font + 2]; | 146 data_->pdf_data_ = skia::AdoptRef(pdf_stream.detachAsStream()); |
107 data_->pdf_doc_.getCountOfFontTypes(font_counts); | 147 return true; |
108 for (int type = 0; | |
109 type <= SkAdvancedTypefaceMetrics::kOther_Font + 1; | |
110 type++) { | |
111 for (int count = 0; count < font_counts[type]; count++) { | |
112 UMA_HISTOGRAM_ENUMERATION( | |
113 "PrintPreview.FontType", type, | |
114 SkAdvancedTypefaceMetrics::kOther_Font + 2); | |
115 } | |
116 } | |
117 | |
118 return data_->pdf_doc_.emitPDF(&data_->pdf_stream_); | |
119 } | 148 } |
120 | 149 |
121 uint32 PdfMetafileSkia::GetDataSize() const { | 150 uint32 PdfMetafileSkia::GetDataSize() const { |
122 return base::checked_cast<uint32>(data_->pdf_stream_.getOffset()); | 151 if (!data_->pdf_data_) |
152 return 0; | |
153 return base::checked_cast<uint32>(data_->pdf_data_->getLength()); | |
123 } | 154 } |
124 | 155 |
125 bool PdfMetafileSkia::GetData(void* dst_buffer, | 156 bool PdfMetafileSkia::GetData(void* dst_buffer, |
126 uint32 dst_buffer_size) const { | 157 uint32 dst_buffer_size) const { |
127 if (dst_buffer_size < GetDataSize()) | 158 if (!data_->pdf_data_) |
128 return false; | 159 return false; |
129 | 160 return WriteAssetToBuffer(data_->pdf_data_.get(), dst_buffer, |
130 SkAutoDataUnref data(data_->pdf_stream_.copyToData()); | 161 base::checked_cast<size_t>(dst_buffer_size)); |
131 memcpy(dst_buffer, data->bytes(), dst_buffer_size); | |
132 return true; | |
133 } | 162 } |
134 | 163 |
135 gfx::Rect PdfMetafileSkia::GetPageBounds(unsigned int page_number) const { | 164 gfx::Rect PdfMetafileSkia::GetPageBounds(unsigned int page_number) const { |
136 // TODO(vandebo) add a method to get the page size for a given page to | 165 if (page_number < data_->pages_.size()) |
danakj
2014/12/23 16:17:20
{}
| |
137 // SkPDFDocument. | 166 return gfx::Rect(gfx::ToFlooredSize( |
138 NOTIMPLEMENTED(); | 167 gfx::SkSizeToSizeF(data_->pages_[page_number].page_size_))); |
139 return gfx::Rect(); | 168 return gfx::Rect(); |
140 } | 169 } |
141 | 170 |
142 unsigned int PdfMetafileSkia::GetPageCount() const { | 171 unsigned int PdfMetafileSkia::GetPageCount() const { |
143 // TODO(vandebo) add a method to get the number of pages to SkPDFDocument. | 172 return base::checked_cast<unsigned int>(data_->pages_.size()); |
144 NOTIMPLEMENTED(); | |
145 return 0; | |
146 } | 173 } |
147 | 174 |
148 gfx::NativeDrawingContext PdfMetafileSkia::context() const { | 175 gfx::NativeDrawingContext PdfMetafileSkia::context() const { |
149 NOTREACHED(); | 176 NOTREACHED(); |
150 return NULL; | 177 return NULL; |
151 } | 178 } |
152 | 179 |
153 #if defined(OS_WIN) | 180 #if defined(OS_WIN) |
154 bool PdfMetafileSkia::Playback(gfx::NativeDrawingContext hdc, | 181 bool PdfMetafileSkia::Playback(gfx::NativeDrawingContext hdc, |
155 const RECT* rect) const { | 182 const RECT* rect) const { |
(...skipping 11 matching lines...) Expand all Loading... | |
167 rasterized output. Even if that flow uses PdfMetafileCg::RenderPage, | 194 rasterized output. Even if that flow uses PdfMetafileCg::RenderPage, |
168 the drawing of the PDF into the canvas may result in a rasterized output. | 195 the drawing of the PDF into the canvas may result in a rasterized output. |
169 PDFMetafileSkia::RenderPage should be not implemented as shown and instead | 196 PDFMetafileSkia::RenderPage should be not implemented as shown and instead |
170 should do something like the following CL in PluginInstance::PrintPDFOutput: | 197 should do something like the following CL in PluginInstance::PrintPDFOutput: |
171 http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_ instance.cc | 198 http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_ instance.cc |
172 */ | 199 */ |
173 bool PdfMetafileSkia::RenderPage(unsigned int page_number, | 200 bool PdfMetafileSkia::RenderPage(unsigned int page_number, |
174 CGContextRef context, | 201 CGContextRef context, |
175 const CGRect rect, | 202 const CGRect rect, |
176 const MacRenderPageParams& params) const { | 203 const MacRenderPageParams& params) const { |
177 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); | 204 DCHECK_GT(GetDataSize(), 0U); |
178 if (data_->pdf_cg_.GetDataSize() == 0) { | 205 if (data_->pdf_cg_.GetDataSize() == 0) { |
179 SkAutoDataUnref data(data_->pdf_stream_.copyToData()); | 206 if (GetDataSize() == 0) |
180 data_->pdf_cg_.InitFromData(data->bytes(), data->size()); | 207 return false; |
208 size_t length = data_->pdf_data_->getLength(); | |
209 std::vector<uint8_t> buffer(length); | |
210 (void)WriteAssetToBuffer(data_->pdf_data_.get(), &buffer[0], length); | |
211 data_->pdf_cg_.InitFromData(&buffer[0], base::checked_cast<uint32>(length)); | |
181 } | 212 } |
182 return data_->pdf_cg_.RenderPage(page_number, context, rect, params); | 213 return data_->pdf_cg_.RenderPage(page_number, context, rect, params); |
183 } | 214 } |
184 #endif | 215 #endif |
185 | 216 |
186 bool PdfMetafileSkia::SaveTo(base::File* file) const { | 217 bool PdfMetafileSkia::SaveTo(base::File* file) const { |
187 if (GetDataSize() == 0U) | 218 if (GetDataSize() == 0U) |
188 return false; | 219 return false; |
189 SkAutoDataUnref data(data_->pdf_stream_.copyToData()); | 220 DCHECK(data_->pdf_data_.get()); |
190 // TODO(halcanary): rewrite this function without extra data copy | 221 |
191 // using SkStreamAsset. | 222 skia::RefPtr<SkStreamAsset> asset = |
192 const char* ptr = reinterpret_cast<const char*>(data->data()); | 223 skia::AdoptRef(data_->pdf_data_->duplicate()); |
193 int size = base::checked_cast<int>(data->size()); | 224 // keeps original asset state unchanged. |
194 return file->WriteAtCurrentPos(ptr, size) == size; | 225 |
226 const size_t maximum_buffer_size = 1024 * 1024; | |
227 std::vector<char> buffer(std::min(maximum_buffer_size, asset->getLength())); | |
228 do { | |
229 size_t read_size = asset->read(&buffer[0], buffer.size()); | |
230 if (read_size == 0) | |
231 break; | |
232 DCHECK_GE(buffer.size(), read_size); | |
233 if (!file->WriteAtCurrentPos(&buffer[0], | |
234 base::checked_cast<int>(read_size))) { | |
235 return false; | |
236 } | |
237 } while (!asset->isAtEnd()); | |
238 | |
239 return true; | |
195 } | 240 } |
196 | 241 |
197 #if defined(OS_CHROMEOS) || defined(OS_ANDROID) | 242 #if defined(OS_CHROMEOS) || defined(OS_ANDROID) |
198 bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const { | 243 bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const { |
199 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); | 244 DCHECK_GT(GetDataSize(), 0U); |
200 | 245 |
201 if (fd.fd < 0) { | 246 if (fd.fd < 0) { |
202 DLOG(ERROR) << "Invalid file descriptor!"; | 247 DLOG(ERROR) << "Invalid file descriptor!"; |
203 return false; | 248 return false; |
204 } | 249 } |
205 base::File file(fd.fd); | 250 base::File file(fd.fd); |
206 bool result = SaveTo(&file); | 251 bool result = SaveTo(&file); |
207 DLOG_IF(ERROR, !result) << "Failed to save file with fd " << fd.fd; | 252 DLOG_IF(ERROR, !result) << "Failed to save file with fd " << fd.fd; |
208 | 253 |
209 if (!fd.auto_close) | 254 if (!fd.auto_close) |
210 file.TakePlatformFile(); | 255 file.TakePlatformFile(); |
211 return result; | 256 return result; |
212 } | 257 } |
213 #endif | 258 #endif |
214 | 259 |
215 PdfMetafileSkia::PdfMetafileSkia() : data_(new PdfMetafileSkiaData) { | 260 PdfMetafileSkia::PdfMetafileSkia() : data_(new PdfMetafileSkiaData) { |
216 } | 261 } |
217 | 262 |
218 scoped_ptr<PdfMetafileSkia> PdfMetafileSkia::GetMetafileForCurrentPage() { | 263 scoped_ptr<PdfMetafileSkia> PdfMetafileSkia::GetMetafileForCurrentPage() { |
219 scoped_ptr<PdfMetafileSkia> metafile; | 264 // If we only ever need the metafile for the last page, should we |
220 SkPDFDocument pdf_doc(SkPDFDocument::kDraftMode_Flags); | 265 // only keep a handle on one SkPicture? |
221 if (!pdf_doc.appendPage(data_->current_page_.get())) | 266 scoped_ptr<PdfMetafileSkia> metafile(new PdfMetafileSkia); |
267 | |
268 if (data_->pages_.size() == 0) | |
222 return metafile.Pass(); | 269 return metafile.Pass(); |
223 | 270 |
224 SkDynamicMemoryWStream pdf_stream; | 271 if (data_->recorder_.getRecordingCanvas()) // page outstanding |
225 if (!pdf_doc.emitPDF(&pdf_stream)) | |
226 return metafile.Pass(); | 272 return metafile.Pass(); |
227 | 273 |
228 SkAutoDataUnref data_copy(pdf_stream.copyToData()); | 274 const Page& page = data_->pages_.back(); |
229 if (data_copy->size() == 0) | |
230 return scoped_ptr<PdfMetafileSkia>(); | |
231 | 275 |
232 metafile.reset(new PdfMetafileSkia); | 276 metafile->data_->pages_.push_back(page); // Copy page data; |
233 if (!metafile->InitFromData(data_copy->bytes(), | 277 // Should increment refcnt on page->content_. |
234 base::checked_cast<uint32>(data_copy->size()))) { | 278 |
279 if (!metafile->FinishDocument()) // Generate PDF. | |
235 metafile.reset(); | 280 metafile.reset(); |
236 } | 281 |
237 return metafile.Pass(); | 282 return metafile.Pass(); |
238 } | 283 } |
239 | 284 |
240 } // namespace printing | 285 } // namespace printing |
OLD | NEW |