[email protected] | 0a4392a | 2012-03-23 17:50:19 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "printing/pdf_metafile_skia.h" |
| 6 | |
[email protected] | 14c1c23 | 2013-06-11 17:52:44 | [diff] [blame] | 7 | #include "base/containers/hash_tables.h" |
thestig | 22dfc401 | 2014-09-05 08:29:44 | [diff] [blame] | 8 | #include "base/files/file_util.h" |
[email protected] | 67e16b39 | 2011-05-30 20:58:09 | [diff] [blame] | 9 | #include "base/metrics/histogram.h" |
[email protected] | cb15406 | 2014-01-17 03:32:40 | [diff] [blame] | 10 | #include "base/numerics/safe_conversions.h" |
[email protected] | 2025d00 | 2012-11-14 20:54:35 | [diff] [blame] | 11 | #include "base/posix/eintr_wrapper.h" |
[email protected] | 584abe7 | 2013-05-18 09:40:26 | [diff] [blame] | 12 | #include "skia/ext/refptr.h" |
[email protected] | e195fbf5 | 2011-06-27 16:51:20 | [diff] [blame] | 13 | #include "third_party/skia/include/core/SkData.h" |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 14 | #include "third_party/skia/include/core/SkDocument.h" |
| 15 | #include "third_party/skia/include/core/SkPictureRecorder.h" |
| 16 | #include "third_party/skia/include/core/SkRect.h" |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 17 | #include "third_party/skia/include/core/SkRefCnt.h" |
[email protected] | a34f012 | 2011-04-12 17:36:39 | [diff] [blame] | 18 | #include "third_party/skia/include/core/SkScalar.h" |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 19 | #include "third_party/skia/include/core/SkSize.h" |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 20 | #include "third_party/skia/include/core/SkStream.h" |
tfarina | 655f81d | 2014-12-23 02:38:50 | [diff] [blame] | 21 | #include "ui/gfx/geometry/point.h" |
tfarina | 3b0452d | 2014-12-31 15:20:09 | [diff] [blame] | 22 | #include "ui/gfx/geometry/rect.h" |
tfarina | ebe974f0 | 2015-01-03 04:25:32 | [diff] [blame] | 23 | #include "ui/gfx/geometry/size.h" |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 24 | #include "ui/gfx/geometry/size_conversions.h" |
| 25 | #include "ui/gfx/skia_util.h" |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 26 | |
[email protected] | b8d85bc | 2011-06-22 13:34:57 | [diff] [blame] | 27 | #if defined(OS_MACOSX) |
| 28 | #include "printing/pdf_metafile_cg_mac.h" |
| 29 | #endif |
| 30 | |
[email protected] | 0daaebfe | 2014-03-15 00:09:05 | [diff] [blame] | 31 | #if defined(OS_POSIX) |
| 32 | #include "base/file_descriptor_posix.h" |
| 33 | #endif |
| 34 | |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 35 | namespace { |
thestig | e3f5f5d4 | 2015-07-16 19:46:24 | [diff] [blame^] | 36 | |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 37 | // This struct represents all the data we need to draw and redraw this |
| 38 | // page into a SkDocument. |
| 39 | struct Page { |
halcanary | 5baa8fb4 | 2015-04-03 21:39:30 | [diff] [blame] | 40 | Page(const SkSize& page_size, const SkRect& content_area, float scale) |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 41 | : page_size_(page_size), |
| 42 | content_area_(content_area), |
halcanary | 5baa8fb4 | 2015-04-03 21:39:30 | [diff] [blame] | 43 | scale_factor_(scale), |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 44 | content_(/*NULL*/) {} |
| 45 | SkSize page_size_; |
| 46 | SkRect content_area_; |
halcanary | 5baa8fb4 | 2015-04-03 21:39:30 | [diff] [blame] | 47 | float scale_factor_; |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 48 | skia::RefPtr<SkPicture> content_; |
| 49 | }; |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 50 | |
thestig | e3f5f5d4 | 2015-07-16 19:46:24 | [diff] [blame^] | 51 | bool WriteAssetToBuffer(const SkStreamAsset* asset, |
| 52 | void* buffer, |
| 53 | size_t size) { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 54 | // Calling duplicate() keeps original asset state unchanged. |
| 55 | scoped_ptr<SkStreamAsset> assetCopy(asset->duplicate()); |
| 56 | size_t length = assetCopy->getLength(); |
| 57 | if (length > size) |
| 58 | return false; |
| 59 | return (length == assetCopy->read(buffer, length)); |
| 60 | } |
| 61 | |
thestig | e3f5f5d4 | 2015-07-16 19:46:24 | [diff] [blame^] | 62 | } // namespace |
| 63 | |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 64 | namespace printing { |
| 65 | |
| 66 | struct PdfMetafileSkiaData { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 67 | SkPictureRecorder recorder_; // Current recording |
| 68 | |
| 69 | std::vector<Page> pages_; |
| 70 | scoped_ptr<SkStreamAsset> pdf_data_; |
| 71 | |
[email protected] | b8d85bc | 2011-06-22 13:34:57 | [diff] [blame] | 72 | #if defined(OS_MACOSX) |
| 73 | PdfMetafileCg pdf_cg_; |
| 74 | #endif |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | PdfMetafileSkia::~PdfMetafileSkia() {} |
| 78 | |
| 79 | bool PdfMetafileSkia::Init() { |
| 80 | return true; |
| 81 | } |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 82 | |
| 83 | // TODO(halcanary): Create a Metafile class that only stores data. |
| 84 | // Metafile::InitFromData is orthogonal to what the rest of |
| 85 | // PdfMetafileSkia does. |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 86 | bool PdfMetafileSkia::InitFromData(const void* src_buffer, |
| 87 | uint32 src_buffer_size) { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 88 | data_->pdf_data_.reset(new SkMemoryStream(src_buffer, src_buffer_size, true)); |
| 89 | return true; |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 90 | } |
| 91 | |
halcanary | 5be808e | 2014-11-10 22:20:05 | [diff] [blame] | 92 | bool PdfMetafileSkia::StartPage(const gfx::Size& page_size, |
| 93 | const gfx::Rect& content_area, |
| 94 | const float& scale_factor) { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 95 | if (data_->recorder_.getRecordingCanvas()) |
| 96 | this->FinishPage(); |
| 97 | DCHECK(!data_->recorder_.getRecordingCanvas()); |
| 98 | SkSize sk_page_size = gfx::SizeFToSkSize(gfx::SizeF(page_size)); |
halcanary | 5baa8fb4 | 2015-04-03 21:39:30 | [diff] [blame] | 99 | data_->pages_.push_back( |
| 100 | Page(sk_page_size, gfx::RectToSkRect(content_area), scale_factor)); |
| 101 | DCHECK_GT(scale_factor, 0.0f); |
| 102 | // We scale the recording canvas's size so that |
| 103 | // canvas->getTotalMatrix() returns a value that ignores the scale |
| 104 | // factor. We store the scale factor and re-apply it to the PDF |
| 105 | // Canvas later. http://crbug.com/469656 |
| 106 | // Recording canvas is owned by the data_->recorder_. No ref() necessary. |
| 107 | return !!data_->recorder_.beginRecording(sk_page_size.width() / scale_factor, |
| 108 | sk_page_size.height() / scale_factor, |
| 109 | NULL, 0); |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 110 | } |
| 111 | |
vitalybuka | a73fc37 | 2015-02-06 18:40:37 | [diff] [blame] | 112 | skia::PlatformCanvas* PdfMetafileSkia::GetVectorCanvasForNewPage( |
halcanary | 5be808e | 2014-11-10 22:20:05 | [diff] [blame] | 113 | const gfx::Size& page_size, |
| 114 | const gfx::Rect& content_area, |
| 115 | const float& scale_factor) { |
| 116 | if (!StartPage(page_size, content_area, scale_factor)) |
| 117 | return nullptr; |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 118 | return data_->recorder_.getRecordingCanvas(); |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | bool PdfMetafileSkia::FinishPage() { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 122 | if (!data_->recorder_.getRecordingCanvas()) |
| 123 | return false; |
| 124 | DCHECK(!(data_->pages_.back().content_)); |
| 125 | data_->pages_.back().content_ = |
thestig | 3523bf8 | 2015-04-08 18:49:00 | [diff] [blame] | 126 | skia::AdoptRef(data_->recorder_.endRecordingAsPicture()); |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 127 | return true; |
| 128 | } |
| 129 | |
| 130 | bool PdfMetafileSkia::FinishDocument() { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 131 | // If we've already set the data in InitFromData, leave it be. |
| 132 | if (data_->pdf_data_) |
| 133 | return false; |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 134 | |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 135 | if (data_->recorder_.getRecordingCanvas()) |
| 136 | this->FinishPage(); |
[email protected] | 67e16b39 | 2011-05-30 20:58:09 | [diff] [blame] | 137 | |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 138 | SkDynamicMemoryWStream pdf_stream; |
| 139 | skia::RefPtr<SkDocument> pdf_doc = |
| 140 | skia::AdoptRef(SkDocument::CreatePDF(&pdf_stream)); |
| 141 | for (const auto& page : data_->pages_) { |
| 142 | SkCanvas* canvas = pdf_doc->beginPage( |
| 143 | page.page_size_.width(), page.page_size_.height(), &page.content_area_); |
halcanary | 5baa8fb4 | 2015-04-03 21:39:30 | [diff] [blame] | 144 | // No need to save/restore, since this canvas is not reused after endPage() |
| 145 | canvas->scale(page.scale_factor_, page.scale_factor_); |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 146 | canvas->drawPicture(page.content_.get()); |
| 147 | pdf_doc->endPage(); |
[email protected] | 67e16b39 | 2011-05-30 20:58:09 | [diff] [blame] | 148 | } |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 149 | if (!pdf_doc->close()) |
| 150 | return false; |
[email protected] | 67e16b39 | 2011-05-30 20:58:09 | [diff] [blame] | 151 | |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 152 | data_->pdf_data_.reset(pdf_stream.detachAsStream()); |
| 153 | return true; |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | uint32 PdfMetafileSkia::GetDataSize() const { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 157 | if (!data_->pdf_data_) |
| 158 | return 0; |
| 159 | return base::checked_cast<uint32>(data_->pdf_data_->getLength()); |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | bool PdfMetafileSkia::GetData(void* dst_buffer, |
| 163 | uint32 dst_buffer_size) const { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 164 | if (!data_->pdf_data_) |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 165 | return false; |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 166 | return WriteAssetToBuffer(data_->pdf_data_.get(), dst_buffer, |
| 167 | base::checked_cast<size_t>(dst_buffer_size)); |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 168 | } |
| 169 | |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 170 | gfx::Rect PdfMetafileSkia::GetPageBounds(unsigned int page_number) const { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 171 | if (page_number < data_->pages_.size()) { |
| 172 | return gfx::Rect(gfx::ToFlooredSize( |
| 173 | gfx::SkSizeToSizeF(data_->pages_[page_number].page_size_))); |
| 174 | } |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 175 | return gfx::Rect(); |
| 176 | } |
| 177 | |
| 178 | unsigned int PdfMetafileSkia::GetPageCount() const { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 179 | return base::checked_cast<unsigned int>(data_->pages_.size()); |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | gfx::NativeDrawingContext PdfMetafileSkia::context() const { |
| 183 | NOTREACHED(); |
| 184 | return NULL; |
| 185 | } |
| 186 | |
| 187 | #if defined(OS_WIN) |
| 188 | bool PdfMetafileSkia::Playback(gfx::NativeDrawingContext hdc, |
| 189 | const RECT* rect) const { |
| 190 | NOTREACHED(); |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | bool PdfMetafileSkia::SafePlayback(gfx::NativeDrawingContext hdc) const { |
| 195 | NOTREACHED(); |
| 196 | return false; |
| 197 | } |
| 198 | |
[email protected] | b8d85bc | 2011-06-22 13:34:57 | [diff] [blame] | 199 | #elif defined(OS_MACOSX) |
| 200 | /* TODO(caryclark): The set up of PluginInstance::PrintPDFOutput may result in |
| 201 | rasterized output. Even if that flow uses PdfMetafileCg::RenderPage, |
| 202 | the drawing of the PDF into the canvas may result in a rasterized output. |
| 203 | PDFMetafileSkia::RenderPage should be not implemented as shown and instead |
| 204 | should do something like the following CL in PluginInstance::PrintPDFOutput: |
| 205 | http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_instance.cc |
| 206 | */ |
| 207 | bool PdfMetafileSkia::RenderPage(unsigned int page_number, |
| 208 | CGContextRef context, |
| 209 | const CGRect rect, |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 210 | const MacRenderPageParams& params) const { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 211 | DCHECK_GT(GetDataSize(), 0U); |
[email protected] | e195fbf5 | 2011-06-27 16:51:20 | [diff] [blame] | 212 | if (data_->pdf_cg_.GetDataSize() == 0) { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 213 | if (GetDataSize() == 0) |
| 214 | return false; |
| 215 | size_t length = data_->pdf_data_->getLength(); |
| 216 | std::vector<uint8_t> buffer(length); |
| 217 | (void)WriteAssetToBuffer(data_->pdf_data_.get(), &buffer[0], length); |
| 218 | data_->pdf_cg_.InitFromData(&buffer[0], base::checked_cast<uint32>(length)); |
[email protected] | e195fbf5 | 2011-06-27 16:51:20 | [diff] [blame] | 219 | } |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 220 | return data_->pdf_cg_.RenderPage(page_number, context, rect, params); |
[email protected] | b8d85bc | 2011-06-22 13:34:57 | [diff] [blame] | 221 | } |
| 222 | #endif |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 223 | |
halcanary | 5be808e | 2014-11-10 22:20:05 | [diff] [blame] | 224 | bool PdfMetafileSkia::SaveTo(base::File* file) const { |
| 225 | if (GetDataSize() == 0U) |
| 226 | return false; |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 227 | |
| 228 | // Calling duplicate() keeps original asset state unchanged. |
| 229 | scoped_ptr<SkStreamAsset> asset(data_->pdf_data_->duplicate()); |
| 230 | |
| 231 | const size_t maximum_buffer_size = 1024 * 1024; |
| 232 | std::vector<char> buffer(std::min(maximum_buffer_size, asset->getLength())); |
| 233 | do { |
| 234 | size_t read_size = asset->read(&buffer[0], buffer.size()); |
| 235 | if (read_size == 0) |
| 236 | break; |
| 237 | DCHECK_GE(buffer.size(), read_size); |
| 238 | if (!file->WriteAtCurrentPos(&buffer[0], |
| 239 | base::checked_cast<int>(read_size))) { |
| 240 | return false; |
| 241 | } |
| 242 | } while (!asset->isAtEnd()); |
| 243 | |
| 244 | return true; |
halcanary | 5be808e | 2014-11-10 22:20:05 | [diff] [blame] | 245 | } |
| 246 | |
[email protected] | b25f003 | 2013-08-19 22:26:25 | [diff] [blame] | 247 | #if defined(OS_CHROMEOS) || defined(OS_ANDROID) |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 248 | bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 249 | DCHECK_GT(GetDataSize(), 0U); |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 250 | |
| 251 | if (fd.fd < 0) { |
| 252 | DLOG(ERROR) << "Invalid file descriptor!"; |
| 253 | return false; |
| 254 | } |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame] | 255 | base::File file(fd.fd); |
halcanary | 5be808e | 2014-11-10 22:20:05 | [diff] [blame] | 256 | bool result = SaveTo(&file); |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame] | 257 | DLOG_IF(ERROR, !result) << "Failed to save file with fd " << fd.fd; |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 258 | |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame] | 259 | if (!fd.auto_close) |
| 260 | file.TakePlatformFile(); |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 261 | return result; |
| 262 | } |
| 263 | #endif |
| 264 | |
halcanary | 5be808e | 2014-11-10 22:20:05 | [diff] [blame] | 265 | PdfMetafileSkia::PdfMetafileSkia() : data_(new PdfMetafileSkiaData) { |
[email protected] | 19b9d3b | 2011-07-23 02:08:57 | [diff] [blame] | 266 | } |
[email protected] | 59751637 | 2011-07-01 05:10:44 | [diff] [blame] | 267 | |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame] | 268 | scoped_ptr<PdfMetafileSkia> PdfMetafileSkia::GetMetafileForCurrentPage() { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 269 | // If we only ever need the metafile for the last page, should we |
| 270 | // only keep a handle on one SkPicture? |
| 271 | scoped_ptr<PdfMetafileSkia> metafile(new PdfMetafileSkia); |
| 272 | |
| 273 | if (data_->pages_.size() == 0) |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame] | 274 | return metafile.Pass(); |
[email protected] | 59751637 | 2011-07-01 05:10:44 | [diff] [blame] | 275 | |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 276 | if (data_->recorder_.getRecordingCanvas()) // page outstanding |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame] | 277 | return metafile.Pass(); |
[email protected] | 59751637 | 2011-07-01 05:10:44 | [diff] [blame] | 278 | |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 279 | const Page& page = data_->pages_.back(); |
[email protected] | 59751637 | 2011-07-01 05:10:44 | [diff] [blame] | 280 | |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 281 | metafile->data_->pages_.push_back(page); |
| 282 | |
| 283 | if (!metafile->FinishDocument()) // Generate PDF. |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame] | 284 | metafile.reset(); |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 285 | |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame] | 286 | return metafile.Pass(); |
[email protected] | 59751637 | 2011-07-01 05:10:44 | [diff] [blame] | 287 | } |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 288 | |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 289 | } // namespace printing |