blob: d30994d41269c1b56d33ef0c02673709795ba455 [file] [log] [blame]
[email protected]0a4392a2012-03-23 17:50:191// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]8f879292011-04-08 00:21:202// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Wei Liab754772018-08-22 22:41:175#include "printing/metafile_skia.h"
[email protected]8f879292011-04-08 00:21:206
thestig6b4461f2016-10-28 19:34:307#include <algorithm>
Lei Zhang8780d73e2018-08-10 18:45:558#include <map>
thestig6b4461f2016-10-28 19:34:309#include <utility>
10#include <vector>
11
Wei Lie13ea642018-02-17 07:55:1912#include "base/bind.h"
13#include "base/bind_helpers.h"
halcanary223f70a2016-06-09 00:07:4214#include "base/files/file.h"
Wei Lie13ea642018-02-17 07:55:1915#include "base/stl_util.h"
halcanary67ce00b82015-09-27 21:59:5316#include "base/time/time.h"
enne7b64edf32017-02-16 20:10:0217#include "cc/paint/paint_record.h"
18#include "cc/paint/paint_recorder.h"
enne59df29de2017-05-02 03:23:3719#include "cc/paint/skia_paint_canvas.h"
halcanary73b63fd2015-11-06 00:02:1420#include "printing/print_settings.h"
Wei Lie13ea642018-02-17 07:55:1921#include "third_party/skia/include/core/SkCanvas.h"
22#include "third_party/skia/include/core/SkPicture.h"
Wei Li5bb659742018-02-14 03:07:5823#include "third_party/skia/include/core/SkSerialProcs.h"
[email protected]8f879292011-04-08 00:21:2024#include "third_party/skia/include/core/SkStream.h"
halcanaryffa005b2016-06-15 17:13:1625// 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"
halcanary223f70a2016-06-09 00:07:4228#include "ui/gfx/geometry/safe_integer_conversions.h"
halcanaryff214762015-01-22 20:34:3229#include "ui/gfx/skia_util.h"
[email protected]8f879292011-04-08 00:21:2030
thestig99fc2132017-04-27 03:37:5431#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 Zhang5ccdf202020-02-13 01:13:1939#if defined(OS_ANDROID)
40#include "base/files/file_util.h"
41#endif // defined(OS_ANDROID)
42
halcanaryff214762015-01-22 20:34:3243namespace {
thestige3f5f5d42015-07-16 19:46:2444
Wei Liab754772018-08-22 22:41:1745bool WriteAssetToBuffer(const SkStreamAsset* asset, void* buffer, size_t size) {
halcanaryff214762015-01-22 20:34:3246 // Calling duplicate() keeps original asset state unchanged.
dchengc3df9ba2016-04-07 23:09:3247 std::unique_ptr<SkStreamAsset> assetCopy(asset->duplicate());
halcanaryff214762015-01-22 20:34:3248 size_t length = assetCopy->getLength();
Lei Zhang662e4222017-12-14 20:18:3449 return length <= size && length == assetCopy->read(buffer, length);
halcanaryff214762015-01-22 20:34:3250}
51
thestige3f5f5d42015-07-16 19:46:2452} // namespace
53
[email protected]8f879292011-04-08 00:21:2054namespace printing {
55
halcanary8dd6f0242016-06-13 19:40:2356struct Page {
Lei Zhang8780d73e2018-08-10 18:45:5557 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)) {}
halcanary8dd6f0242016-06-13 19:40:2360 Page(const Page&) = default;
61 Page& operator=(const Page&) = default;
62 Page& operator=(Page&& that) {
Lei Zhang8780d73e2018-08-10 18:45:5563 size = that.size;
64 content = std::move(that.content);
halcanary8dd6f0242016-06-13 19:40:2365 return *this;
66 }
Lei Zhang8780d73e2018-08-10 18:45:5567 SkSize size;
68 sk_sp<cc::PaintRecord> content;
halcanary8dd6f0242016-06-13 19:40:2369};
70
Wei Liab754772018-08-22 22:41:1771struct MetafileSkiaData {
Lei Zhang8780d73e2018-08-10 18:45:5572 cc::PaintRecorder recorder; // Current recording
halcanaryff214762015-01-22 20:34:3273
Lei Zhang8780d73e2018-08-10 18:45:5574 std::vector<Page> pages;
Wei Liab754772018-08-22 22:41:1775 std::unique_ptr<SkStreamAsset> data_stream;
Lei Zhang8780d73e2018-08-10 18:45:5576 ContentToProxyIdMap subframe_content_info;
77 std::map<uint32_t, sk_sp<SkPicture>> subframe_pics;
78 int document_cookie = 0;
halcanaryff214762015-01-22 20:34:3279
halcanary223f70a2016-06-09 00:07:4280 // The scale factor is used because Blink occasionally calls
enne7b64edf32017-02-16 20:10:0281 // PaintCanvas::getTotalMatrix() even though the total matrix is not as
halcanary223f70a2016-06-09 00:07:4282 // meaningful for a vector canvas as for a raster canvas.
Lei Zhang8780d73e2018-08-10 18:45:5583 float scale_factor;
84 SkSize size;
85 SkiaDocumentType type;
thestig99fc2132017-04-27 03:37:5486
87#if defined(OS_MACOSX)
Lei Zhang8780d73e2018-08-10 18:45:5588 PdfMetafileCg pdf_cg;
thestig99fc2132017-04-27 03:37:5489#endif
[email protected]8f879292011-04-08 00:21:2090};
91
Wei Liab754772018-08-22 22:41:1792MetafileSkia::MetafileSkia() : data_(std::make_unique<MetafileSkiaData>()) {
Lei Zhang8780d73e2018-08-10 18:45:5593 data_->type = SkiaDocumentType::PDF;
Wei Lie13ea642018-02-17 07:55:1994}
95
Wei Liab754772018-08-22 22:41:1796MetafileSkia::MetafileSkia(SkiaDocumentType type, int document_cookie)
97 : data_(std::make_unique<MetafileSkiaData>()) {
Lei Zhang8780d73e2018-08-10 18:45:5598 data_->type = type;
99 data_->document_cookie = document_cookie;
Lei Zhang662e4222017-12-14 20:18:34100}
101
Wei Liab754772018-08-22 22:41:17102MetafileSkia::~MetafileSkia() = default;
[email protected]8f879292011-04-08 00:21:20103
Wei Liab754772018-08-22 22:41:17104bool MetafileSkia::Init() {
[email protected]8f879292011-04-08 00:21:20105 return true;
106}
halcanaryff214762015-01-22 20:34:32107
108// TODO(halcanary): Create a Metafile class that only stores data.
109// Metafile::InitFromData is orthogonal to what the rest of
Wei Liab754772018-08-22 22:41:17110// MetafileSkia does.
111bool MetafileSkia::InitFromData(const void* src_buffer,
112 size_t src_buffer_size) {
113 data_->data_stream = std::make_unique<SkMemoryStream>(
thestig6b4461f2016-10-28 19:34:30114 src_buffer, src_buffer_size, true /* copy_data? */);
halcanaryff214762015-01-22 20:34:32115 return true;
[email protected]8f879292011-04-08 00:21:20116}
117
Wei Liab754772018-08-22 22:41:17118void MetafileSkia::StartPage(const gfx::Size& page_size,
119 const gfx::Rect& content_area,
Lei Zhang5c6ed5b2020-03-24 17:42:53120 float scale_factor) {
halcanary223f70a2016-06-09 00:07:42121 DCHECK_GT(page_size.width(), 0);
122 DCHECK_GT(page_size.height(), 0);
123 DCHECK_GT(scale_factor, 0.0f);
Lei Zhang8780d73e2018-08-10 18:45:55124 if (data_->recorder.getRecordingCanvas())
thestig5ff7db22016-02-13 04:42:45125 FinishPage();
Lei Zhang8780d73e2018-08-10 18:45:55126 DCHECK(!data_->recorder.getRecordingCanvas());
halcanary223f70a2016-06-09 00:07:42127
128 float inverse_scale = 1.0 / scale_factor;
Lei Zhang8780d73e2018-08-10 18:45:55129 cc::PaintCanvas* canvas = data_->recorder.beginRecording(
halcanary223f70a2016-06-09 00:07:42130 inverse_scale * page_size.width(), inverse_scale * page_size.height());
Lei Zhang8780d73e2018-08-10 18:45:55131 // Recording canvas is owned by the |data_->recorder|. No ref() necessary.
halcanary223f70a2016-06-09 00:07:42132 if (content_area != gfx::Rect(page_size)) {
133 canvas->scale(inverse_scale, inverse_scale);
134 SkRect sk_content_area = gfx::RectToSkRect(content_area);
135 canvas->clipRect(sk_content_area);
136 canvas->translate(sk_content_area.x(), sk_content_area.y());
137 canvas->scale(scale_factor, scale_factor);
138 }
139
Lei Zhang8780d73e2018-08-10 18:45:55140 data_->size = gfx::SizeFToSkSize(gfx::SizeF(page_size));
141 data_->scale_factor = scale_factor;
halcanary5baa8fb42015-04-03 21:39:30142 // We scale the recording canvas's size so that
143 // canvas->getTotalMatrix() returns a value that ignores the scale
halcanary223f70a2016-06-09 00:07:42144 // factor. We store the scale factor and re-apply it later.
145 // http://crbug.com/469656
[email protected]8f879292011-04-08 00:21:20146}
147
Wei Liab754772018-08-22 22:41:17148cc::PaintCanvas* MetafileSkia::GetVectorCanvasForNewPage(
halcanary5be808e2014-11-10 22:20:05149 const gfx::Size& page_size,
150 const gfx::Rect& content_area,
Lei Zhang5c6ed5b2020-03-24 17:42:53151 float scale_factor) {
halcanary223f70a2016-06-09 00:07:42152 StartPage(page_size, content_area, scale_factor);
Lei Zhang8780d73e2018-08-10 18:45:55153 return data_->recorder.getRecordingCanvas();
[email protected]8f879292011-04-08 00:21:20154}
155
Wei Liab754772018-08-22 22:41:17156bool MetafileSkia::FinishPage() {
Lei Zhang8780d73e2018-08-10 18:45:55157 if (!data_->recorder.getRecordingCanvas())
halcanaryff214762015-01-22 20:34:32158 return false;
halcanary223f70a2016-06-09 00:07:42159
Lei Zhang8780d73e2018-08-10 18:45:55160 sk_sp<cc::PaintRecord> pic = data_->recorder.finishRecordingAsPicture();
161 if (data_->scale_factor != 1.0f) {
162 cc::PaintCanvas* canvas = data_->recorder.beginRecording(
163 data_->size.width(), data_->size.height());
164 canvas->scale(data_->scale_factor, data_->scale_factor);
halcanary223f70a2016-06-09 00:07:42165 canvas->drawPicture(pic);
Lei Zhang8780d73e2018-08-10 18:45:55166 pic = data_->recorder.finishRecordingAsPicture();
halcanary223f70a2016-06-09 00:07:42167 }
Lei Zhang8780d73e2018-08-10 18:45:55168 data_->pages.emplace_back(data_->size, std::move(pic));
[email protected]8f879292011-04-08 00:21:20169 return true;
170}
171
Wei Liab754772018-08-22 22:41:17172bool MetafileSkia::FinishDocument() {
halcanaryff214762015-01-22 20:34:32173 // If we've already set the data in InitFromData, leave it be.
Wei Liab754772018-08-22 22:41:17174 if (data_->data_stream)
halcanaryff214762015-01-22 20:34:32175 return false;
[email protected]8f879292011-04-08 00:21:20176
Lei Zhang8780d73e2018-08-10 18:45:55177 if (data_->recorder.getRecordingCanvas())
thestig5ff7db22016-02-13 04:42:45178 FinishPage();
[email protected]67e16b392011-05-30 20:58:09179
halcanary223f70a2016-06-09 00:07:42180 SkDynamicMemoryWStream stream;
halcanaryffa005b2016-06-15 17:13:16181 sk_sp<SkDocument> doc;
Wei Lie13ea642018-02-17 07:55:19182 cc::PlaybackParams::CustomDataRasterCallback custom_callback;
Lei Zhang8780d73e2018-08-10 18:45:55183 switch (data_->type) {
Wei Lid0e05022017-08-22 03:56:52184 case SkiaDocumentType::PDF:
Dominic Mazzoni0a39059c2020-01-08 19:30:45185 doc = MakePdfDocument(printing::GetAgent(), accessibility_tree_, &stream);
halcanaryffa005b2016-06-15 17:13:16186 break;
Wei Lid0e05022017-08-22 03:56:52187 case SkiaDocumentType::MSKP:
Lei Zhang8780d73e2018-08-10 18:45:55188 SkSerialProcs procs = SerializationProcs(&data_->subframe_content_info);
Wei Lie13ea642018-02-17 07:55:19189 doc = SkMakeMultiPictureDocument(&stream, &procs);
190 // It is safe to use base::Unretained(this) because the callback
191 // is only used by |canvas| in the following loop which has shorter
192 // lifetime than |this|.
Wei Liab754772018-08-22 22:41:17193 custom_callback = base::BindRepeating(
194 &MetafileSkia::CustomDataToSkPictureCallback, base::Unretained(this));
halcanaryffa005b2016-06-15 17:13:16195 break;
196 }
halcanaryfb232282016-04-28 18:34:24197
Lei Zhang8780d73e2018-08-10 18:45:55198 for (const Page& page : data_->pages) {
enne98c9f8052017-03-15 19:38:22199 cc::SkiaPaintCanvas canvas(
Lei Zhang8780d73e2018-08-10 18:45:55200 doc->beginPage(page.size.width(), page.size.height()));
201 canvas.drawPicture(page.content, custom_callback);
halcanary223f70a2016-06-09 00:07:42202 doc->endPage();
halcanary0aeeb3332016-03-18 14:32:39203 }
reede35e0532016-09-22 17:30:29204 doc->close();
[email protected]67e16b392011-05-30 20:58:09205
Wei Liab754772018-08-22 22:41:17206 data_->data_stream = stream.detachAsStream();
halcanaryff214762015-01-22 20:34:32207 return true;
[email protected]8f879292011-04-08 00:21:20208}
209
Wei Liab754772018-08-22 22:41:17210void MetafileSkia::FinishFrameContent() {
Wei Li5bb659742018-02-14 03:07:58211 // Sanity check to make sure we print the entire frame as a single page
212 // content.
Lei Zhang8780d73e2018-08-10 18:45:55213 DCHECK_EQ(data_->pages.size(), 1u);
Wei Li5bb659742018-02-14 03:07:58214 // Also make sure it is in skia multi-picture document format.
Lei Zhang8780d73e2018-08-10 18:45:55215 DCHECK_EQ(data_->type, SkiaDocumentType::MSKP);
Wei Liab754772018-08-22 22:41:17216 DCHECK(!data_->data_stream);
Wei Li5bb659742018-02-14 03:07:58217
Wei Li6b0cfc92018-08-08 19:52:25218 cc::PlaybackParams::CustomDataRasterCallback custom_callback =
Wei Liab754772018-08-22 22:41:17219 base::BindRepeating(&MetafileSkia::CustomDataToSkPictureCallback,
Wei Li6b0cfc92018-08-08 19:52:25220 base::Unretained(this));
Lei Zhang8780d73e2018-08-10 18:45:55221 sk_sp<SkPicture> pic = ToSkPicture(data_->pages[0].content,
222 SkRect::MakeSize(data_->pages[0].size),
Wei Li6b0cfc92018-08-08 19:52:25223 nullptr, custom_callback);
Lei Zhang8780d73e2018-08-10 18:45:55224 SkSerialProcs procs = SerializationProcs(&data_->subframe_content_info);
Wei Li6b0cfc92018-08-08 19:52:25225 SkDynamicMemoryWStream stream;
Wei Li5bb659742018-02-14 03:07:58226 pic->serialize(&stream, &procs);
Wei Liab754772018-08-22 22:41:17227 data_->data_stream = stream.detachAsStream();
Wei Li5bb659742018-02-14 03:07:58228}
229
Wei Liab754772018-08-22 22:41:17230uint32_t MetafileSkia::GetDataSize() const {
231 if (!data_->data_stream)
halcanaryff214762015-01-22 20:34:32232 return 0;
Wei Liab754772018-08-22 22:41:17233 return base::checked_cast<uint32_t>(data_->data_stream->getLength());
[email protected]8f879292011-04-08 00:21:20234}
235
Wei Liab754772018-08-22 22:41:17236bool MetafileSkia::GetData(void* dst_buffer, uint32_t dst_buffer_size) const {
237 if (!data_->data_stream)
[email protected]8f879292011-04-08 00:21:20238 return false;
Wei Liab754772018-08-22 22:41:17239 return WriteAssetToBuffer(data_->data_stream.get(), dst_buffer,
halcanaryff214762015-01-22 20:34:32240 base::checked_cast<size_t>(dst_buffer_size));
[email protected]8f879292011-04-08 00:21:20241}
242
Wei Liab754772018-08-22 22:41:17243gfx::Rect MetafileSkia::GetPageBounds(unsigned int page_number) const {
Lei Zhang8780d73e2018-08-10 18:45:55244 if (page_number < data_->pages.size()) {
245 SkSize size = data_->pages[page_number].size;
halcanary8dd6f0242016-06-13 19:40:23246 return gfx::Rect(gfx::ToRoundedInt(size.width()),
247 gfx::ToRoundedInt(size.height()));
halcanaryff214762015-01-22 20:34:32248 }
[email protected]8f879292011-04-08 00:21:20249 return gfx::Rect();
250}
251
Wei Liab754772018-08-22 22:41:17252unsigned int MetafileSkia::GetPageCount() const {
Lei Zhang8780d73e2018-08-10 18:45:55253 return base::checked_cast<unsigned int>(data_->pages.size());
[email protected]8f879292011-04-08 00:21:20254}
255
Wei Liab754772018-08-22 22:41:17256printing::NativeDrawingContext MetafileSkia::context() const {
[email protected]8f879292011-04-08 00:21:20257 NOTREACHED();
thestig192677ec2016-06-09 07:43:17258 return nullptr;
[email protected]8f879292011-04-08 00:21:20259}
260
261#if defined(OS_WIN)
Wei Liab754772018-08-22 22:41:17262bool MetafileSkia::Playback(printing::NativeDrawingContext hdc,
263 const RECT* rect) const {
thestig3109df12017-04-26 21:57:25264 NOTREACHED();
265 return false;
266}
267
Wei Liab754772018-08-22 22:41:17268bool MetafileSkia::SafePlayback(printing::NativeDrawingContext hdc) const {
[email protected]8f879292011-04-08 00:21:20269 NOTREACHED();
270 return false;
271}
thestig99fc2132017-04-27 03:37:54272
273#elif defined(OS_MACOSX)
274/* TODO(caryclark): The set up of PluginInstance::PrintPDFOutput may result in
275 rasterized output. Even if that flow uses PdfMetafileCg::RenderPage,
276 the drawing of the PDF into the canvas may result in a rasterized output.
277 PDFMetafileSkia::RenderPage should be not implemented as shown and instead
278 should do something like the following CL in PluginInstance::PrintPDFOutput:
279http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_instance.cc
280*/
Wei Liab754772018-08-22 22:41:17281bool MetafileSkia::RenderPage(unsigned int page_number,
282 CGContextRef context,
Lei Zhangf69ea3b2020-03-24 17:53:48283 const CGRect& rect,
Wei Liab754772018-08-22 22:41:17284 const MacRenderPageParams& params) const {
thestig99fc2132017-04-27 03:37:54285 DCHECK_GT(GetDataSize(), 0U);
Lei Zhang8780d73e2018-08-10 18:45:55286 if (data_->pdf_cg.GetDataSize() == 0) {
thestig99fc2132017-04-27 03:37:54287 if (GetDataSize() == 0)
288 return false;
Wei Liab754772018-08-22 22:41:17289 size_t length = data_->data_stream->getLength();
thestig99fc2132017-04-27 03:37:54290 std::vector<uint8_t> buffer(length);
Wei Liab754772018-08-22 22:41:17291 (void)WriteAssetToBuffer(data_->data_stream.get(), &buffer[0], length);
Lei Zhang8780d73e2018-08-10 18:45:55292 data_->pdf_cg.InitFromData(&buffer[0], length);
thestig99fc2132017-04-27 03:37:54293 }
Lei Zhang8780d73e2018-08-10 18:45:55294 return data_->pdf_cg.RenderPage(page_number, context, rect, params);
thestig99fc2132017-04-27 03:37:54295}
[email protected]b8d85bc2011-06-22 13:34:57296#endif
[email protected]8f879292011-04-08 00:21:20297
Shimi Zhang5ccdf202020-02-13 01:13:19298#if defined(OS_ANDROID)
299bool MetafileSkia::SaveToFileDescriptor(int fd) const {
300 if (GetDataSize() == 0u)
301 return false;
302
303 std::unique_ptr<SkStreamAsset> asset(data_->data_stream->duplicate());
304
305 static constexpr size_t kMaximumBufferSize = 1024 * 1024;
306 std::vector<uint8_t> buffer(std::min(kMaximumBufferSize, asset->getLength()));
307 do {
308 size_t read_size = asset->read(&buffer[0], buffer.size());
309 if (read_size == 0u)
310 break;
311 DCHECK_GE(buffer.size(), read_size);
312 if (!base::WriteFileDescriptor(
313 fd, reinterpret_cast<const char*>(buffer.data()), read_size)) {
314 return false;
315 }
316 } while (!asset->isAtEnd());
317
318 return true;
319}
320#else
Wei Liab754772018-08-22 22:41:17321bool MetafileSkia::SaveTo(base::File* file) const {
halcanary5be808e2014-11-10 22:20:05322 if (GetDataSize() == 0U)
323 return false;
halcanaryff214762015-01-22 20:34:32324
325 // Calling duplicate() keeps original asset state unchanged.
Wei Liab754772018-08-22 22:41:17326 std::unique_ptr<SkStreamAsset> asset(data_->data_stream->duplicate());
halcanaryff214762015-01-22 20:34:32327
Lei Zhang8780d73e2018-08-10 18:45:55328 static constexpr size_t kMaximumBufferSize = 1024 * 1024;
Lei Zhang12893bb62019-09-25 18:31:04329 std::vector<uint8_t> buffer(std::min(kMaximumBufferSize, asset->getLength()));
halcanaryff214762015-01-22 20:34:32330 do {
331 size_t read_size = asset->read(&buffer[0], buffer.size());
332 if (read_size == 0)
333 break;
334 DCHECK_GE(buffer.size(), read_size);
Lei Zhang12893bb62019-09-25 18:31:04335 if (!file->WriteAtCurrentPosAndCheck(
336 base::make_span(&buffer[0], read_size))) {
halcanaryff214762015-01-22 20:34:32337 return false;
338 }
339 } while (!asset->isAtEnd());
340
341 return true;
halcanary5be808e2014-11-10 22:20:05342}
Shimi Zhang5ccdf202020-02-13 01:13:19343#endif // defined(OS_ANDROID)
halcanary5be808e2014-11-10 22:20:05344
Wei Liab754772018-08-22 22:41:17345std::unique_ptr<MetafileSkia> MetafileSkia::GetMetafileForCurrentPage(
halcanaryffa005b2016-06-15 17:13:16346 SkiaDocumentType type) {
halcanaryff214762015-01-22 20:34:32347 // If we only ever need the metafile for the last page, should we
enne7b64edf32017-02-16 20:10:02348 // only keep a handle on one PaintRecord?
Wei Liab754772018-08-22 22:41:17349 auto metafile = std::make_unique<MetafileSkia>(type, data_->document_cookie);
Lei Zhang8780d73e2018-08-10 18:45:55350 if (data_->pages.size() == 0)
dchenge48600452015-12-28 02:24:50351 return metafile;
[email protected]597516372011-07-01 05:10:44352
Lei Zhang8780d73e2018-08-10 18:45:55353 if (data_->recorder.getRecordingCanvas()) // page outstanding
dchenge48600452015-12-28 02:24:50354 return metafile;
[email protected]597516372011-07-01 05:10:44355
Lei Zhang8780d73e2018-08-10 18:45:55356 metafile->data_->pages.push_back(data_->pages.back());
357 metafile->data_->subframe_content_info = data_->subframe_content_info;
358 metafile->data_->subframe_pics = data_->subframe_pics;
halcanaryff214762015-01-22 20:34:32359
360 if (!metafile->FinishDocument()) // Generate PDF.
vitalybuka5d1290582014-09-12 09:19:59361 metafile.reset();
halcanaryff214762015-01-22 20:34:32362
dchenge48600452015-12-28 02:24:50363 return metafile;
[email protected]597516372011-07-01 05:10:44364}
[email protected]8f879292011-04-08 00:21:20365
Wei Liab754772018-08-22 22:41:17366uint32_t MetafileSkia::CreateContentForRemoteFrame(const gfx::Rect& rect,
367 int render_proxy_id) {
Wei Lie13ea642018-02-17 07:55:19368 // Create a place holder picture.
369 sk_sp<SkPicture> pic = SkPicture::MakePlaceholder(
370 SkRect::MakeXYWH(rect.x(), rect.y(), rect.width(), rect.height()));
371
372 // Store the map between content id and the proxy id.
373 uint32_t content_id = pic->uniqueID();
Jan Wilken Dörrie3ced3ccc2019-06-06 21:41:58374 DCHECK(!base::Contains(data_->subframe_content_info, content_id));
Lei Zhang8780d73e2018-08-10 18:45:55375 data_->subframe_content_info[content_id] = render_proxy_id;
Wei Lie13ea642018-02-17 07:55:19376
377 // Store the picture content.
Lei Zhang8780d73e2018-08-10 18:45:55378 data_->subframe_pics[content_id] = pic;
Wei Lie13ea642018-02-17 07:55:19379 return content_id;
380}
381
Wei Liab754772018-08-22 22:41:17382int MetafileSkia::GetDocumentCookie() const {
Lei Zhang8780d73e2018-08-10 18:45:55383 return data_->document_cookie;
Wei Lie13ea642018-02-17 07:55:19384}
385
Wei Liab754772018-08-22 22:41:17386const ContentToProxyIdMap& MetafileSkia::GetSubframeContentInfo() const {
Lei Zhang8780d73e2018-08-10 18:45:55387 return data_->subframe_content_info;
Wei Lie13ea642018-02-17 07:55:19388}
389
Wei Liab754772018-08-22 22:41:17390void MetafileSkia::AppendPage(const SkSize& page_size,
391 sk_sp<cc::PaintRecord> record) {
Lei Zhang8780d73e2018-08-10 18:45:55392 data_->pages.emplace_back(page_size, std::move(record));
Wei Li1d345bf2018-08-10 02:52:37393}
394
Wei Liab754772018-08-22 22:41:17395void MetafileSkia::AppendSubframeInfo(uint32_t content_id,
396 int proxy_id,
397 sk_sp<SkPicture> pic_holder) {
Lei Zhang8780d73e2018-08-10 18:45:55398 data_->subframe_content_info[content_id] = proxy_id;
399 data_->subframe_pics[content_id] = pic_holder;
Wei Li1d345bf2018-08-10 02:52:37400}
401
Wei Liab754772018-08-22 22:41:17402SkStreamAsset* MetafileSkia::GetPdfData() const {
403 return data_->data_stream.get();
Wei Li1d345bf2018-08-10 02:52:37404}
405
Wei Liab754772018-08-22 22:41:17406void MetafileSkia::CustomDataToSkPictureCallback(SkCanvas* canvas,
407 uint32_t content_id) {
Wei Lie13ea642018-02-17 07:55:19408 // Check whether this is the one we need to handle.
Jan Wilken Dörrie3ced3ccc2019-06-06 21:41:58409 if (!base::Contains(data_->subframe_content_info, content_id))
Wei Lie13ea642018-02-17 07:55:19410 return;
411
Lei Zhang8780d73e2018-08-10 18:45:55412 auto it = data_->subframe_pics.find(content_id);
413 DCHECK(it != data_->subframe_pics.end());
Wei Lie13ea642018-02-17 07:55:19414
415 // Found the picture, draw it on canvas.
416 sk_sp<SkPicture> pic = it->second;
417 SkRect rect = pic->cullRect();
418 SkMatrix matrix = SkMatrix::MakeTrans(rect.x(), rect.y());
419 canvas->drawPicture(it->second, &matrix, nullptr);
420}
421
[email protected]8f879292011-04-08 00:21:20422} // namespace printing