[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 | |
thestig | 6b4461f | 2016-10-28 19:34:30 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | #include <string> |
| 9 | #include <utility> |
| 10 | #include <vector> |
| 11 | |
Wei Li | e13ea64 | 2018-02-17 07:55:19 | [diff] [blame] | 12 | #include "base/bind.h" |
| 13 | #include "base/bind_helpers.h" |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 14 | #include "base/files/file.h" |
Wei Li | e13ea64 | 2018-02-17 07:55:19 | [diff] [blame] | 15 | #include "base/stl_util.h" |
halcanary | 67ce00b8 | 2015-09-27 21:59:53 | [diff] [blame] | 16 | #include "base/time/time.h" |
enne | 7b64edf3 | 2017-02-16 20:10:02 | [diff] [blame] | 17 | #include "cc/paint/paint_record.h" |
| 18 | #include "cc/paint/paint_recorder.h" |
enne | 59df29de | 2017-05-02 03:23:37 | [diff] [blame] | 19 | #include "cc/paint/skia_paint_canvas.h" |
halcanary | 73b63fd | 2015-11-06 00:02:14 | [diff] [blame] | 20 | #include "printing/print_settings.h" |
Wei Li | e13ea64 | 2018-02-17 07:55:19 | [diff] [blame] | 21 | #include "third_party/skia/include/core/SkCanvas.h" |
| 22 | #include "third_party/skia/include/core/SkPicture.h" |
Wei Li | 5bb65974 | 2018-02-14 03:07:58 | [diff] [blame] | 23 | #include "third_party/skia/include/core/SkSerialProcs.h" |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 24 | #include "third_party/skia/include/core/SkStream.h" |
halcanary | ffa005b | 2016-06-15 17:13:16 | [diff] [blame] | 25 | // Note that headers in third_party/skia/src are fragile. This is |
| 26 | // an experimental, fragile, and diagnostic-only document type. |
| 27 | #include "third_party/skia/src/utils/SkMultiPictureDocument.h" |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 28 | #include "ui/gfx/geometry/safe_integer_conversions.h" |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 29 | #include "ui/gfx/skia_util.h" |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 30 | |
thestig | 99fc213 | 2017-04-27 03:37:54 | [diff] [blame] | 31 | #if defined(OS_MACOSX) |
| 32 | #include "printing/pdf_metafile_cg_mac.h" |
| 33 | #endif |
| 34 | |
| 35 | #if defined(OS_POSIX) |
| 36 | #include "base/file_descriptor_posix.h" |
| 37 | #endif |
| 38 | |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 39 | namespace { |
thestig | e3f5f5d4 | 2015-07-16 19:46:24 | [diff] [blame] | 40 | |
thestig | e3f5f5d4 | 2015-07-16 19:46:24 | [diff] [blame] | 41 | bool WriteAssetToBuffer(const SkStreamAsset* asset, |
| 42 | void* buffer, |
| 43 | size_t size) { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 44 | // Calling duplicate() keeps original asset state unchanged. |
dcheng | c3df9ba | 2016-04-07 23:09:32 | [diff] [blame] | 45 | std::unique_ptr<SkStreamAsset> assetCopy(asset->duplicate()); |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 46 | size_t length = assetCopy->getLength(); |
Lei Zhang | 662e422 | 2017-12-14 20:18:34 | [diff] [blame] | 47 | return length <= size && length == assetCopy->read(buffer, length); |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 48 | } |
| 49 | |
thestig | e3f5f5d4 | 2015-07-16 19:46:24 | [diff] [blame] | 50 | } // namespace |
| 51 | |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 52 | namespace printing { |
| 53 | |
Lei Zhang | 662e422 | 2017-12-14 20:18:34 | [diff] [blame] | 54 | // TODO(thestig): struct members should not have trailing underscore. |
halcanary | 8dd6f024 | 2016-06-13 19:40:23 | [diff] [blame] | 55 | struct Page { |
enne | 7b64edf3 | 2017-02-16 20:10:02 | [diff] [blame] | 56 | Page(SkSize s, sk_sp<cc::PaintRecord> c) : size_(s), content_(std::move(c)) {} |
halcanary | 8dd6f024 | 2016-06-13 19:40:23 | [diff] [blame] | 57 | Page(Page&& that) : size_(that.size_), content_(std::move(that.content_)) {} |
| 58 | Page(const Page&) = default; |
| 59 | Page& operator=(const Page&) = default; |
| 60 | Page& operator=(Page&& that) { |
| 61 | size_ = that.size_; |
| 62 | content_ = std::move(that.content_); |
| 63 | return *this; |
| 64 | } |
| 65 | SkSize size_; |
enne | 7b64edf3 | 2017-02-16 20:10:02 | [diff] [blame] | 66 | sk_sp<cc::PaintRecord> content_; |
halcanary | 8dd6f024 | 2016-06-13 19:40:23 | [diff] [blame] | 67 | }; |
| 68 | |
Wei Li | 5bb65974 | 2018-02-14 03:07:58 | [diff] [blame] | 69 | // TODO(weili): Remove pdf from struct name and field names since it is used for |
Wei Li | e13ea64 | 2018-02-17 07:55:19 | [diff] [blame] | 70 | // other formats as well. Also change member variable names to |
| 71 | // conform with our style guide. |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 72 | struct PdfMetafileSkiaData { |
enne | 7b64edf3 | 2017-02-16 20:10:02 | [diff] [blame] | 73 | cc::PaintRecorder recorder_; // Current recording |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 74 | |
halcanary | 8dd6f024 | 2016-06-13 19:40:23 | [diff] [blame] | 75 | std::vector<Page> pages_; |
dcheng | c3df9ba | 2016-04-07 23:09:32 | [diff] [blame] | 76 | std::unique_ptr<SkStreamAsset> pdf_data_; |
Wei Li | e13ea64 | 2018-02-17 07:55:19 | [diff] [blame] | 77 | ContentToProxyIdMap subframe_content_info_; |
| 78 | std::map<uint32_t, sk_sp<SkPicture>> subframe_pics_; |
| 79 | int document_cookie_ = 0; |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 80 | |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 81 | // The scale factor is used because Blink occasionally calls |
enne | 7b64edf3 | 2017-02-16 20:10:02 | [diff] [blame] | 82 | // PaintCanvas::getTotalMatrix() even though the total matrix is not as |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 83 | // meaningful for a vector canvas as for a raster canvas. |
| 84 | float scale_factor_; |
halcanary | 8dd6f024 | 2016-06-13 19:40:23 | [diff] [blame] | 85 | SkSize size_; |
halcanary | ffa005b | 2016-06-15 17:13:16 | [diff] [blame] | 86 | SkiaDocumentType type_; |
thestig | 99fc213 | 2017-04-27 03:37:54 | [diff] [blame] | 87 | |
| 88 | #if defined(OS_MACOSX) |
| 89 | PdfMetafileCg pdf_cg_; |
| 90 | #endif |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 91 | }; |
| 92 | |
Wei Li | e13ea64 | 2018-02-17 07:55:19 | [diff] [blame] | 93 | PdfMetafileSkia::PdfMetafileSkia() |
| 94 | : data_(std::make_unique<PdfMetafileSkiaData>()) { |
| 95 | data_->type_ = SkiaDocumentType::PDF; |
| 96 | } |
| 97 | |
| 98 | PdfMetafileSkia::PdfMetafileSkia(SkiaDocumentType type, int document_cookie) |
Lei Zhang | 662e422 | 2017-12-14 20:18:34 | [diff] [blame] | 99 | : data_(std::make_unique<PdfMetafileSkiaData>()) { |
| 100 | data_->type_ = type; |
Wei Li | e13ea64 | 2018-02-17 07:55:19 | [diff] [blame] | 101 | data_->document_cookie_ = document_cookie; |
Lei Zhang | 662e422 | 2017-12-14 20:18:34 | [diff] [blame] | 102 | } |
| 103 | |
Chris Watkins | c360b97 | 2017-12-01 05:50:23 | [diff] [blame] | 104 | PdfMetafileSkia::~PdfMetafileSkia() = default; |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 105 | |
| 106 | bool PdfMetafileSkia::Init() { |
| 107 | return true; |
| 108 | } |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 109 | |
| 110 | // TODO(halcanary): Create a Metafile class that only stores data. |
| 111 | // Metafile::InitFromData is orthogonal to what the rest of |
| 112 | // PdfMetafileSkia does. |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 113 | bool PdfMetafileSkia::InitFromData(const void* src_buffer, |
thestig | 6b4461f | 2016-10-28 19:34:30 | [diff] [blame] | 114 | size_t src_buffer_size) { |
Lei Zhang | 662e422 | 2017-12-14 20:18:34 | [diff] [blame] | 115 | data_->pdf_data_ = std::make_unique<SkMemoryStream>( |
thestig | 6b4461f | 2016-10-28 19:34:30 | [diff] [blame] | 116 | src_buffer, src_buffer_size, true /* copy_data? */); |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 117 | return true; |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 118 | } |
| 119 | |
thestig | 192677ec | 2016-06-09 07:43:17 | [diff] [blame] | 120 | void PdfMetafileSkia::StartPage(const gfx::Size& page_size, |
halcanary | 5be808e | 2014-11-10 22:20:05 | [diff] [blame] | 121 | const gfx::Rect& content_area, |
| 122 | const float& scale_factor) { |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 123 | DCHECK_GT(page_size.width(), 0); |
| 124 | DCHECK_GT(page_size.height(), 0); |
| 125 | DCHECK_GT(scale_factor, 0.0f); |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 126 | if (data_->recorder_.getRecordingCanvas()) |
thestig | 5ff7db2 | 2016-02-13 04:42:45 | [diff] [blame] | 127 | FinishPage(); |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 128 | DCHECK(!data_->recorder_.getRecordingCanvas()); |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 129 | |
| 130 | float inverse_scale = 1.0 / scale_factor; |
enne | 7b64edf3 | 2017-02-16 20:10:02 | [diff] [blame] | 131 | cc::PaintCanvas* canvas = data_->recorder_.beginRecording( |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 132 | inverse_scale * page_size.width(), inverse_scale * page_size.height()); |
halcanary | ffa005b | 2016-06-15 17:13:16 | [diff] [blame] | 133 | // Recording canvas is owned by the data_->recorder_. No ref() necessary. |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 134 | if (content_area != gfx::Rect(page_size)) { |
| 135 | canvas->scale(inverse_scale, inverse_scale); |
| 136 | SkRect sk_content_area = gfx::RectToSkRect(content_area); |
| 137 | canvas->clipRect(sk_content_area); |
| 138 | canvas->translate(sk_content_area.x(), sk_content_area.y()); |
| 139 | canvas->scale(scale_factor, scale_factor); |
| 140 | } |
| 141 | |
halcanary | 8dd6f024 | 2016-06-13 19:40:23 | [diff] [blame] | 142 | data_->size_ = gfx::SizeFToSkSize(gfx::SizeF(page_size)); |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 143 | data_->scale_factor_ = scale_factor; |
halcanary | 5baa8fb4 | 2015-04-03 21:39:30 | [diff] [blame] | 144 | // We scale the recording canvas's size so that |
| 145 | // canvas->getTotalMatrix() returns a value that ignores the scale |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 146 | // factor. We store the scale factor and re-apply it later. |
| 147 | // http://crbug.com/469656 |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 148 | } |
| 149 | |
enne | 7b64edf3 | 2017-02-16 20:10:02 | [diff] [blame] | 150 | cc::PaintCanvas* PdfMetafileSkia::GetVectorCanvasForNewPage( |
halcanary | 5be808e | 2014-11-10 22:20:05 | [diff] [blame] | 151 | const gfx::Size& page_size, |
| 152 | const gfx::Rect& content_area, |
| 153 | const float& scale_factor) { |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 154 | StartPage(page_size, content_area, scale_factor); |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 155 | return data_->recorder_.getRecordingCanvas(); |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | bool PdfMetafileSkia::FinishPage() { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 159 | if (!data_->recorder_.getRecordingCanvas()) |
| 160 | return false; |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 161 | |
enne | 7b64edf3 | 2017-02-16 20:10:02 | [diff] [blame] | 162 | sk_sp<cc::PaintRecord> pic = data_->recorder_.finishRecordingAsPicture(); |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 163 | if (data_->scale_factor_ != 1.0f) { |
enne | 7b64edf3 | 2017-02-16 20:10:02 | [diff] [blame] | 164 | cc::PaintCanvas* canvas = data_->recorder_.beginRecording( |
| 165 | data_->size_.width(), data_->size_.height()); |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 166 | canvas->scale(data_->scale_factor_, data_->scale_factor_); |
| 167 | canvas->drawPicture(pic); |
| 168 | pic = data_->recorder_.finishRecordingAsPicture(); |
| 169 | } |
halcanary | 8dd6f024 | 2016-06-13 19:40:23 | [diff] [blame] | 170 | data_->pages_.emplace_back(data_->size_, std::move(pic)); |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 171 | return true; |
| 172 | } |
| 173 | |
| 174 | bool PdfMetafileSkia::FinishDocument() { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 175 | // If we've already set the data in InitFromData, leave it be. |
| 176 | if (data_->pdf_data_) |
| 177 | return false; |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 178 | |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 179 | if (data_->recorder_.getRecordingCanvas()) |
thestig | 5ff7db2 | 2016-02-13 04:42:45 | [diff] [blame] | 180 | FinishPage(); |
[email protected] | 67e16b39 | 2011-05-30 20:58:09 | [diff] [blame] | 181 | |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 182 | SkDynamicMemoryWStream stream; |
halcanary | ffa005b | 2016-06-15 17:13:16 | [diff] [blame] | 183 | sk_sp<SkDocument> doc; |
Wei Li | e13ea64 | 2018-02-17 07:55:19 | [diff] [blame] | 184 | cc::PlaybackParams::CustomDataRasterCallback custom_callback; |
halcanary | ffa005b | 2016-06-15 17:13:16 | [diff] [blame] | 185 | switch (data_->type_) { |
Wei Li | d0e0502 | 2017-08-22 03:56:52 | [diff] [blame] | 186 | case SkiaDocumentType::PDF: |
weili | fabbf757 | 2017-05-22 19:05:16 | [diff] [blame] | 187 | doc = MakePdfDocument(printing::GetAgent(), &stream); |
halcanary | ffa005b | 2016-06-15 17:13:16 | [diff] [blame] | 188 | break; |
Wei Li | d0e0502 | 2017-08-22 03:56:52 | [diff] [blame] | 189 | case SkiaDocumentType::MSKP: |
Wei Li | e13ea64 | 2018-02-17 07:55:19 | [diff] [blame] | 190 | SkSerialProcs procs = SerializationProcs(&data_->subframe_content_info_); |
| 191 | doc = SkMakeMultiPictureDocument(&stream, &procs); |
| 192 | // It is safe to use base::Unretained(this) because the callback |
| 193 | // is only used by |canvas| in the following loop which has shorter |
| 194 | // lifetime than |this|. |
| 195 | custom_callback = |
| 196 | base::BindRepeating(&PdfMetafileSkia::CustomDataToSkPictureCallback, |
| 197 | base::Unretained(this)); |
halcanary | ffa005b | 2016-06-15 17:13:16 | [diff] [blame] | 198 | break; |
| 199 | } |
halcanary | fb23228 | 2016-04-28 18:34:24 | [diff] [blame] | 200 | |
halcanary | 8dd6f024 | 2016-06-13 19:40:23 | [diff] [blame] | 201 | for (const Page& page : data_->pages_) { |
enne | 98c9f805 | 2017-03-15 19:38:22 | [diff] [blame] | 202 | cc::SkiaPaintCanvas canvas( |
enne | 7b64edf3 | 2017-02-16 20:10:02 | [diff] [blame] | 203 | doc->beginPage(page.size_.width(), page.size_.height())); |
Wei Li | e13ea64 | 2018-02-17 07:55:19 | [diff] [blame] | 204 | canvas.drawPicture(page.content_, custom_callback); |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 205 | doc->endPage(); |
halcanary | 0aeeb333 | 2016-03-18 14:32:39 | [diff] [blame] | 206 | } |
reed | e35e053 | 2016-09-22 17:30:29 | [diff] [blame] | 207 | doc->close(); |
[email protected] | 67e16b39 | 2011-05-30 20:58:09 | [diff] [blame] | 208 | |
halcanary | de655aa | 2017-04-03 16:16:13 | [diff] [blame] | 209 | data_->pdf_data_ = stream.detachAsStream(); |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 210 | return true; |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 211 | } |
| 212 | |
Wei Li | 5bb65974 | 2018-02-14 03:07:58 | [diff] [blame] | 213 | void PdfMetafileSkia::FinishFrameContent() { |
| 214 | // Sanity check to make sure we print the entire frame as a single page |
| 215 | // content. |
| 216 | DCHECK_EQ(data_->pages_.size(), 1u); |
| 217 | // Also make sure it is in skia multi-picture document format. |
| 218 | DCHECK_EQ(data_->type_, SkiaDocumentType::MSKP); |
| 219 | DCHECK(!data_->pdf_data_); |
| 220 | |
Wei Li | 6b0cfc9 | 2018-08-08 19:52:25 | [diff] [blame] | 221 | cc::PlaybackParams::CustomDataRasterCallback custom_callback = |
| 222 | base::BindRepeating(&PdfMetafileSkia::CustomDataToSkPictureCallback, |
| 223 | base::Unretained(this)); |
Wei Li | 5bb65974 | 2018-02-14 03:07:58 | [diff] [blame] | 224 | sk_sp<SkPicture> pic = ToSkPicture(data_->pages_[0].content_, |
Wei Li | 6b0cfc9 | 2018-08-08 19:52:25 | [diff] [blame] | 225 | SkRect::MakeSize(data_->pages_[0].size_), |
| 226 | nullptr, custom_callback); |
Wei Li | e13ea64 | 2018-02-17 07:55:19 | [diff] [blame] | 227 | SkSerialProcs procs = SerializationProcs(&data_->subframe_content_info_); |
Wei Li | 6b0cfc9 | 2018-08-08 19:52:25 | [diff] [blame] | 228 | SkDynamicMemoryWStream stream; |
Wei Li | 5bb65974 | 2018-02-14 03:07:58 | [diff] [blame] | 229 | pic->serialize(&stream, &procs); |
| 230 | data_->pdf_data_ = stream.detachAsStream(); |
| 231 | } |
| 232 | |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 233 | uint32_t PdfMetafileSkia::GetDataSize() const { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 234 | if (!data_->pdf_data_) |
| 235 | return 0; |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 236 | return base::checked_cast<uint32_t>(data_->pdf_data_->getLength()); |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | bool PdfMetafileSkia::GetData(void* dst_buffer, |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 240 | uint32_t dst_buffer_size) const { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 241 | if (!data_->pdf_data_) |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 242 | return false; |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 243 | return WriteAssetToBuffer(data_->pdf_data_.get(), dst_buffer, |
| 244 | base::checked_cast<size_t>(dst_buffer_size)); |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 245 | } |
| 246 | |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 247 | gfx::Rect PdfMetafileSkia::GetPageBounds(unsigned int page_number) const { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 248 | if (page_number < data_->pages_.size()) { |
halcanary | 8dd6f024 | 2016-06-13 19:40:23 | [diff] [blame] | 249 | SkSize size = data_->pages_[page_number].size_; |
| 250 | return gfx::Rect(gfx::ToRoundedInt(size.width()), |
| 251 | gfx::ToRoundedInt(size.height())); |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 252 | } |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 253 | return gfx::Rect(); |
| 254 | } |
| 255 | |
| 256 | unsigned int PdfMetafileSkia::GetPageCount() const { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 257 | return base::checked_cast<unsigned int>(data_->pages_.size()); |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 258 | } |
| 259 | |
Nico Weber | 8e55956 | 2017-10-03 01:25:26 | [diff] [blame] | 260 | printing::NativeDrawingContext PdfMetafileSkia::context() const { |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 261 | NOTREACHED(); |
thestig | 192677ec | 2016-06-09 07:43:17 | [diff] [blame] | 262 | return nullptr; |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 263 | } |
| 264 | |
thestig | 192677ec | 2016-06-09 07:43:17 | [diff] [blame] | 265 | |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 266 | #if defined(OS_WIN) |
Nico Weber | 8e55956 | 2017-10-03 01:25:26 | [diff] [blame] | 267 | bool PdfMetafileSkia::Playback(printing::NativeDrawingContext hdc, |
thestig | 3109df1 | 2017-04-26 21:57:25 | [diff] [blame] | 268 | const RECT* rect) const { |
| 269 | NOTREACHED(); |
| 270 | return false; |
| 271 | } |
| 272 | |
Nico Weber | 8e55956 | 2017-10-03 01:25:26 | [diff] [blame] | 273 | bool PdfMetafileSkia::SafePlayback(printing::NativeDrawingContext hdc) const { |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 274 | NOTREACHED(); |
| 275 | return false; |
| 276 | } |
thestig | 99fc213 | 2017-04-27 03:37:54 | [diff] [blame] | 277 | |
| 278 | #elif defined(OS_MACOSX) |
| 279 | /* TODO(caryclark): The set up of PluginInstance::PrintPDFOutput may result in |
| 280 | rasterized output. Even if that flow uses PdfMetafileCg::RenderPage, |
| 281 | the drawing of the PDF into the canvas may result in a rasterized output. |
| 282 | PDFMetafileSkia::RenderPage should be not implemented as shown and instead |
| 283 | should do something like the following CL in PluginInstance::PrintPDFOutput: |
| 284 | http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_instance.cc |
| 285 | */ |
| 286 | bool PdfMetafileSkia::RenderPage(unsigned int page_number, |
| 287 | CGContextRef context, |
| 288 | const CGRect rect, |
| 289 | const MacRenderPageParams& params) const { |
| 290 | DCHECK_GT(GetDataSize(), 0U); |
| 291 | if (data_->pdf_cg_.GetDataSize() == 0) { |
| 292 | if (GetDataSize() == 0) |
| 293 | return false; |
| 294 | size_t length = data_->pdf_data_->getLength(); |
| 295 | std::vector<uint8_t> buffer(length); |
| 296 | (void)WriteAssetToBuffer(data_->pdf_data_.get(), &buffer[0], length); |
| 297 | data_->pdf_cg_.InitFromData(&buffer[0], length); |
| 298 | } |
| 299 | return data_->pdf_cg_.RenderPage(page_number, context, rect, params); |
| 300 | } |
[email protected] | b8d85bc | 2011-06-22 13:34:57 | [diff] [blame] | 301 | #endif |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 302 | |
halcanary | 5be808e | 2014-11-10 22:20:05 | [diff] [blame] | 303 | bool PdfMetafileSkia::SaveTo(base::File* file) const { |
| 304 | if (GetDataSize() == 0U) |
| 305 | return false; |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 306 | |
| 307 | // Calling duplicate() keeps original asset state unchanged. |
dcheng | c3df9ba | 2016-04-07 23:09:32 | [diff] [blame] | 308 | std::unique_ptr<SkStreamAsset> asset(data_->pdf_data_->duplicate()); |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 309 | |
thestig | 192677ec | 2016-06-09 07:43:17 | [diff] [blame] | 310 | const size_t kMaximumBufferSize = 1024 * 1024; |
| 311 | std::vector<char> buffer(std::min(kMaximumBufferSize, asset->getLength())); |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 312 | do { |
| 313 | size_t read_size = asset->read(&buffer[0], buffer.size()); |
| 314 | if (read_size == 0) |
| 315 | break; |
| 316 | DCHECK_GE(buffer.size(), read_size); |
| 317 | if (!file->WriteAtCurrentPos(&buffer[0], |
| 318 | base::checked_cast<int>(read_size))) { |
| 319 | return false; |
| 320 | } |
| 321 | } while (!asset->isAtEnd()); |
| 322 | |
| 323 | return true; |
halcanary | 5be808e | 2014-11-10 22:20:05 | [diff] [blame] | 324 | } |
| 325 | |
halcanary | ffa005b | 2016-06-15 17:13:16 | [diff] [blame] | 326 | std::unique_ptr<PdfMetafileSkia> PdfMetafileSkia::GetMetafileForCurrentPage( |
| 327 | SkiaDocumentType type) { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 328 | // If we only ever need the metafile for the last page, should we |
enne | 7b64edf3 | 2017-02-16 20:10:02 | [diff] [blame] | 329 | // only keep a handle on one PaintRecord? |
Wei Li | e13ea64 | 2018-02-17 07:55:19 | [diff] [blame] | 330 | auto metafile = |
| 331 | std::make_unique<PdfMetafileSkia>(type, data_->document_cookie_); |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 332 | if (data_->pages_.size() == 0) |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 333 | return metafile; |
[email protected] | 59751637 | 2011-07-01 05:10:44 | [diff] [blame] | 334 | |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 335 | if (data_->recorder_.getRecordingCanvas()) // page outstanding |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 336 | return metafile; |
[email protected] | 59751637 | 2011-07-01 05:10:44 | [diff] [blame] | 337 | |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 338 | metafile->data_->pages_.push_back(data_->pages_.back()); |
Wei Li | e13ea64 | 2018-02-17 07:55:19 | [diff] [blame] | 339 | metafile->data_->subframe_content_info_ = data_->subframe_content_info_; |
| 340 | metafile->data_->subframe_pics_ = data_->subframe_pics_; |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 341 | |
| 342 | if (!metafile->FinishDocument()) // Generate PDF. |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame] | 343 | metafile.reset(); |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 344 | |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 345 | return metafile; |
[email protected] | 59751637 | 2011-07-01 05:10:44 | [diff] [blame] | 346 | } |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 347 | |
Wei Li | e13ea64 | 2018-02-17 07:55:19 | [diff] [blame] | 348 | uint32_t PdfMetafileSkia::CreateContentForRemoteFrame(const gfx::Rect& rect, |
| 349 | int render_proxy_id) { |
| 350 | // Create a place holder picture. |
| 351 | sk_sp<SkPicture> pic = SkPicture::MakePlaceholder( |
| 352 | SkRect::MakeXYWH(rect.x(), rect.y(), rect.width(), rect.height())); |
| 353 | |
| 354 | // Store the map between content id and the proxy id. |
| 355 | uint32_t content_id = pic->uniqueID(); |
| 356 | DCHECK(!base::ContainsKey(data_->subframe_content_info_, content_id)); |
| 357 | data_->subframe_content_info_[content_id] = render_proxy_id; |
| 358 | |
| 359 | // Store the picture content. |
| 360 | data_->subframe_pics_[content_id] = pic; |
| 361 | return content_id; |
| 362 | } |
| 363 | |
| 364 | int PdfMetafileSkia::GetDocumentCookie() const { |
| 365 | return data_->document_cookie_; |
| 366 | } |
| 367 | |
| 368 | const ContentToProxyIdMap& PdfMetafileSkia::GetSubframeContentInfo() const { |
| 369 | return data_->subframe_content_info_; |
| 370 | } |
| 371 | |
Wei Li | 1d345bf | 2018-08-10 02:52:37 | [diff] [blame^] | 372 | void PdfMetafileSkia::AppendPage(const SkSize& page_size, |
| 373 | sk_sp<cc::PaintRecord> record) { |
| 374 | data_->pages_.emplace_back(page_size, std::move(record)); |
| 375 | } |
| 376 | |
| 377 | void PdfMetafileSkia::AppendSubframeInfo(uint32_t content_id, |
| 378 | int proxy_id, |
| 379 | sk_sp<SkPicture> pic_holder) { |
| 380 | data_->subframe_content_info_[content_id] = proxy_id; |
| 381 | data_->subframe_pics_[content_id] = pic_holder; |
| 382 | } |
| 383 | |
| 384 | SkStreamAsset* PdfMetafileSkia::GetPdfData() const { |
| 385 | return data_->pdf_data_.get(); |
| 386 | } |
| 387 | |
Wei Li | e13ea64 | 2018-02-17 07:55:19 | [diff] [blame] | 388 | void PdfMetafileSkia::CustomDataToSkPictureCallback(SkCanvas* canvas, |
| 389 | uint32_t content_id) { |
| 390 | // Check whether this is the one we need to handle. |
| 391 | if (!base::ContainsKey(data_->subframe_content_info_, content_id)) |
| 392 | return; |
| 393 | |
| 394 | auto it = data_->subframe_pics_.find(content_id); |
| 395 | DCHECK(it != data_->subframe_pics_.end()); |
| 396 | |
| 397 | // Found the picture, draw it on canvas. |
| 398 | sk_sp<SkPicture> pic = it->second; |
| 399 | SkRect rect = pic->cullRect(); |
| 400 | SkMatrix matrix = SkMatrix::MakeTrans(rect.x(), rect.y()); |
| 401 | canvas->drawPicture(it->second, &matrix, nullptr); |
| 402 | } |
| 403 | |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 404 | } // namespace printing |