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