blob: b6e5876fa5759381a222273d4c185c3668e0cbea [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
halcanaryff214762015-01-22 20:34:3239namespace {
thestige3f5f5d42015-07-16 19:46:2440
Wei Liab754772018-08-22 22:41:1741bool WriteAssetToBuffer(const SkStreamAsset* asset, void* buffer, size_t size) {
halcanaryff214762015-01-22 20:34:3242 // Calling duplicate() keeps original asset state unchanged.
dchengc3df9ba2016-04-07 23:09:3243 std::unique_ptr<SkStreamAsset> assetCopy(asset->duplicate());
halcanaryff214762015-01-22 20:34:3244 size_t length = assetCopy->getLength();
Lei Zhang662e4222017-12-14 20:18:3445 return length <= size && length == assetCopy->read(buffer, length);
halcanaryff214762015-01-22 20:34:3246}
47
thestige3f5f5d42015-07-16 19:46:2448} // namespace
49
[email protected]8f879292011-04-08 00:21:2050namespace printing {
51
halcanary8dd6f0242016-06-13 19:40:2352struct Page {
Lei Zhang8780d73e2018-08-10 18:45:5553 Page(const SkSize& s, sk_sp<cc::PaintRecord> c)
54 : size(s), content(std::move(c)) {}
55 Page(Page&& that) : size(that.size), content(std::move(that.content)) {}
halcanary8dd6f0242016-06-13 19:40:2356 Page(const Page&) = default;
57 Page& operator=(const Page&) = default;
58 Page& operator=(Page&& that) {
Lei Zhang8780d73e2018-08-10 18:45:5559 size = that.size;
60 content = std::move(that.content);
halcanary8dd6f0242016-06-13 19:40:2361 return *this;
62 }
Lei Zhang8780d73e2018-08-10 18:45:5563 SkSize size;
64 sk_sp<cc::PaintRecord> content;
halcanary8dd6f0242016-06-13 19:40:2365};
66
Wei Liab754772018-08-22 22:41:1767struct MetafileSkiaData {
Lei Zhang8780d73e2018-08-10 18:45:5568 cc::PaintRecorder recorder; // Current recording
halcanaryff214762015-01-22 20:34:3269
Lei Zhang8780d73e2018-08-10 18:45:5570 std::vector<Page> pages;
Wei Liab754772018-08-22 22:41:1771 std::unique_ptr<SkStreamAsset> data_stream;
Lei Zhang8780d73e2018-08-10 18:45:5572 ContentToProxyIdMap subframe_content_info;
73 std::map<uint32_t, sk_sp<SkPicture>> subframe_pics;
74 int document_cookie = 0;
halcanaryff214762015-01-22 20:34:3275
halcanary223f70a2016-06-09 00:07:4276 // The scale factor is used because Blink occasionally calls
enne7b64edf32017-02-16 20:10:0277 // PaintCanvas::getTotalMatrix() even though the total matrix is not as
halcanary223f70a2016-06-09 00:07:4278 // meaningful for a vector canvas as for a raster canvas.
Lei Zhang8780d73e2018-08-10 18:45:5579 float scale_factor;
80 SkSize size;
81 SkiaDocumentType type;
thestig99fc2132017-04-27 03:37:5482
83#if defined(OS_MACOSX)
Lei Zhang8780d73e2018-08-10 18:45:5584 PdfMetafileCg pdf_cg;
thestig99fc2132017-04-27 03:37:5485#endif
[email protected]8f879292011-04-08 00:21:2086};
87
Wei Liab754772018-08-22 22:41:1788MetafileSkia::MetafileSkia() : data_(std::make_unique<MetafileSkiaData>()) {
Lei Zhang8780d73e2018-08-10 18:45:5589 data_->type = SkiaDocumentType::PDF;
Wei Lie13ea642018-02-17 07:55:1990}
91
Wei Liab754772018-08-22 22:41:1792MetafileSkia::MetafileSkia(SkiaDocumentType type, int document_cookie)
93 : data_(std::make_unique<MetafileSkiaData>()) {
Lei Zhang8780d73e2018-08-10 18:45:5594 data_->type = type;
95 data_->document_cookie = document_cookie;
Lei Zhang662e4222017-12-14 20:18:3496}
97
Wei Liab754772018-08-22 22:41:1798MetafileSkia::~MetafileSkia() = default;
[email protected]8f879292011-04-08 00:21:2099
Wei Liab754772018-08-22 22:41:17100bool MetafileSkia::Init() {
[email protected]8f879292011-04-08 00:21:20101 return true;
102}
halcanaryff214762015-01-22 20:34:32103
104// TODO(halcanary): Create a Metafile class that only stores data.
105// Metafile::InitFromData is orthogonal to what the rest of
Wei Liab754772018-08-22 22:41:17106// MetafileSkia does.
107bool MetafileSkia::InitFromData(const void* src_buffer,
108 size_t src_buffer_size) {
109 data_->data_stream = std::make_unique<SkMemoryStream>(
thestig6b4461f2016-10-28 19:34:30110 src_buffer, src_buffer_size, true /* copy_data? */);
halcanaryff214762015-01-22 20:34:32111 return true;
[email protected]8f879292011-04-08 00:21:20112}
113
Wei Liab754772018-08-22 22:41:17114void MetafileSkia::StartPage(const gfx::Size& page_size,
115 const gfx::Rect& content_area,
116 const float& scale_factor) {
halcanary223f70a2016-06-09 00:07:42117 DCHECK_GT(page_size.width(), 0);
118 DCHECK_GT(page_size.height(), 0);
119 DCHECK_GT(scale_factor, 0.0f);
Lei Zhang8780d73e2018-08-10 18:45:55120 if (data_->recorder.getRecordingCanvas())
thestig5ff7db22016-02-13 04:42:45121 FinishPage();
Lei Zhang8780d73e2018-08-10 18:45:55122 DCHECK(!data_->recorder.getRecordingCanvas());
halcanary223f70a2016-06-09 00:07:42123
124 float inverse_scale = 1.0 / scale_factor;
Lei Zhang8780d73e2018-08-10 18:45:55125 cc::PaintCanvas* canvas = data_->recorder.beginRecording(
halcanary223f70a2016-06-09 00:07:42126 inverse_scale * page_size.width(), inverse_scale * page_size.height());
Lei Zhang8780d73e2018-08-10 18:45:55127 // Recording canvas is owned by the |data_->recorder|. No ref() necessary.
halcanary223f70a2016-06-09 00:07:42128 if (content_area != gfx::Rect(page_size)) {
129 canvas->scale(inverse_scale, inverse_scale);
130 SkRect sk_content_area = gfx::RectToSkRect(content_area);
131 canvas->clipRect(sk_content_area);
132 canvas->translate(sk_content_area.x(), sk_content_area.y());
133 canvas->scale(scale_factor, scale_factor);
134 }
135
Lei Zhang8780d73e2018-08-10 18:45:55136 data_->size = gfx::SizeFToSkSize(gfx::SizeF(page_size));
137 data_->scale_factor = scale_factor;
halcanary5baa8fb42015-04-03 21:39:30138 // We scale the recording canvas's size so that
139 // canvas->getTotalMatrix() returns a value that ignores the scale
halcanary223f70a2016-06-09 00:07:42140 // factor. We store the scale factor and re-apply it later.
141 // http://crbug.com/469656
[email protected]8f879292011-04-08 00:21:20142}
143
Wei Liab754772018-08-22 22:41:17144cc::PaintCanvas* MetafileSkia::GetVectorCanvasForNewPage(
halcanary5be808e2014-11-10 22:20:05145 const gfx::Size& page_size,
146 const gfx::Rect& content_area,
147 const float& scale_factor) {
halcanary223f70a2016-06-09 00:07:42148 StartPage(page_size, content_area, scale_factor);
Lei Zhang8780d73e2018-08-10 18:45:55149 return data_->recorder.getRecordingCanvas();
[email protected]8f879292011-04-08 00:21:20150}
151
Wei Liab754772018-08-22 22:41:17152bool MetafileSkia::FinishPage() {
Lei Zhang8780d73e2018-08-10 18:45:55153 if (!data_->recorder.getRecordingCanvas())
halcanaryff214762015-01-22 20:34:32154 return false;
halcanary223f70a2016-06-09 00:07:42155
Lei Zhang8780d73e2018-08-10 18:45:55156 sk_sp<cc::PaintRecord> pic = data_->recorder.finishRecordingAsPicture();
157 if (data_->scale_factor != 1.0f) {
158 cc::PaintCanvas* canvas = data_->recorder.beginRecording(
159 data_->size.width(), data_->size.height());
160 canvas->scale(data_->scale_factor, data_->scale_factor);
halcanary223f70a2016-06-09 00:07:42161 canvas->drawPicture(pic);
Lei Zhang8780d73e2018-08-10 18:45:55162 pic = data_->recorder.finishRecordingAsPicture();
halcanary223f70a2016-06-09 00:07:42163 }
Lei Zhang8780d73e2018-08-10 18:45:55164 data_->pages.emplace_back(data_->size, std::move(pic));
[email protected]8f879292011-04-08 00:21:20165 return true;
166}
167
Wei Liab754772018-08-22 22:41:17168bool MetafileSkia::FinishDocument() {
halcanaryff214762015-01-22 20:34:32169 // If we've already set the data in InitFromData, leave it be.
Wei Liab754772018-08-22 22:41:17170 if (data_->data_stream)
halcanaryff214762015-01-22 20:34:32171 return false;
[email protected]8f879292011-04-08 00:21:20172
Lei Zhang8780d73e2018-08-10 18:45:55173 if (data_->recorder.getRecordingCanvas())
thestig5ff7db22016-02-13 04:42:45174 FinishPage();
[email protected]67e16b392011-05-30 20:58:09175
halcanary223f70a2016-06-09 00:07:42176 SkDynamicMemoryWStream stream;
halcanaryffa005b2016-06-15 17:13:16177 sk_sp<SkDocument> doc;
Wei Lie13ea642018-02-17 07:55:19178 cc::PlaybackParams::CustomDataRasterCallback custom_callback;
Lei Zhang8780d73e2018-08-10 18:45:55179 switch (data_->type) {
Wei Lid0e05022017-08-22 03:56:52180 case SkiaDocumentType::PDF:
weilifabbf7572017-05-22 19:05:16181 doc = MakePdfDocument(printing::GetAgent(), &stream);
halcanaryffa005b2016-06-15 17:13:16182 break;
Wei Lid0e05022017-08-22 03:56:52183 case SkiaDocumentType::MSKP:
Lei Zhang8780d73e2018-08-10 18:45:55184 SkSerialProcs procs = SerializationProcs(&data_->subframe_content_info);
Wei Lie13ea642018-02-17 07:55:19185 doc = SkMakeMultiPictureDocument(&stream, &procs);
186 // It is safe to use base::Unretained(this) because the callback
187 // is only used by |canvas| in the following loop which has shorter
188 // lifetime than |this|.
Wei Liab754772018-08-22 22:41:17189 custom_callback = base::BindRepeating(
190 &MetafileSkia::CustomDataToSkPictureCallback, base::Unretained(this));
halcanaryffa005b2016-06-15 17:13:16191 break;
192 }
halcanaryfb232282016-04-28 18:34:24193
Lei Zhang8780d73e2018-08-10 18:45:55194 for (const Page& page : data_->pages) {
enne98c9f8052017-03-15 19:38:22195 cc::SkiaPaintCanvas canvas(
Lei Zhang8780d73e2018-08-10 18:45:55196 doc->beginPage(page.size.width(), page.size.height()));
197 canvas.drawPicture(page.content, custom_callback);
halcanary223f70a2016-06-09 00:07:42198 doc->endPage();
halcanary0aeeb3332016-03-18 14:32:39199 }
reede35e0532016-09-22 17:30:29200 doc->close();
[email protected]67e16b392011-05-30 20:58:09201
Wei Liab754772018-08-22 22:41:17202 data_->data_stream = stream.detachAsStream();
halcanaryff214762015-01-22 20:34:32203 return true;
[email protected]8f879292011-04-08 00:21:20204}
205
Wei Liab754772018-08-22 22:41:17206void MetafileSkia::FinishFrameContent() {
Wei Li5bb659742018-02-14 03:07:58207 // Sanity check to make sure we print the entire frame as a single page
208 // content.
Lei Zhang8780d73e2018-08-10 18:45:55209 DCHECK_EQ(data_->pages.size(), 1u);
Wei Li5bb659742018-02-14 03:07:58210 // Also make sure it is in skia multi-picture document format.
Lei Zhang8780d73e2018-08-10 18:45:55211 DCHECK_EQ(data_->type, SkiaDocumentType::MSKP);
Wei Liab754772018-08-22 22:41:17212 DCHECK(!data_->data_stream);
Wei Li5bb659742018-02-14 03:07:58213
Wei Li6b0cfc92018-08-08 19:52:25214 cc::PlaybackParams::CustomDataRasterCallback custom_callback =
Wei Liab754772018-08-22 22:41:17215 base::BindRepeating(&MetafileSkia::CustomDataToSkPictureCallback,
Wei Li6b0cfc92018-08-08 19:52:25216 base::Unretained(this));
Lei Zhang8780d73e2018-08-10 18:45:55217 sk_sp<SkPicture> pic = ToSkPicture(data_->pages[0].content,
218 SkRect::MakeSize(data_->pages[0].size),
Wei Li6b0cfc92018-08-08 19:52:25219 nullptr, custom_callback);
Lei Zhang8780d73e2018-08-10 18:45:55220 SkSerialProcs procs = SerializationProcs(&data_->subframe_content_info);
Wei Li6b0cfc92018-08-08 19:52:25221 SkDynamicMemoryWStream stream;
Wei Li5bb659742018-02-14 03:07:58222 pic->serialize(&stream, &procs);
Wei Liab754772018-08-22 22:41:17223 data_->data_stream = stream.detachAsStream();
Wei Li5bb659742018-02-14 03:07:58224}
225
Wei Liab754772018-08-22 22:41:17226uint32_t MetafileSkia::GetDataSize() const {
227 if (!data_->data_stream)
halcanaryff214762015-01-22 20:34:32228 return 0;
Wei Liab754772018-08-22 22:41:17229 return base::checked_cast<uint32_t>(data_->data_stream->getLength());
[email protected]8f879292011-04-08 00:21:20230}
231
Wei Liab754772018-08-22 22:41:17232bool MetafileSkia::GetData(void* dst_buffer, uint32_t dst_buffer_size) const {
233 if (!data_->data_stream)
[email protected]8f879292011-04-08 00:21:20234 return false;
Wei Liab754772018-08-22 22:41:17235 return WriteAssetToBuffer(data_->data_stream.get(), dst_buffer,
halcanaryff214762015-01-22 20:34:32236 base::checked_cast<size_t>(dst_buffer_size));
[email protected]8f879292011-04-08 00:21:20237}
238
Wei Liab754772018-08-22 22:41:17239gfx::Rect MetafileSkia::GetPageBounds(unsigned int page_number) const {
Lei Zhang8780d73e2018-08-10 18:45:55240 if (page_number < data_->pages.size()) {
241 SkSize size = data_->pages[page_number].size;
halcanary8dd6f0242016-06-13 19:40:23242 return gfx::Rect(gfx::ToRoundedInt(size.width()),
243 gfx::ToRoundedInt(size.height()));
halcanaryff214762015-01-22 20:34:32244 }
[email protected]8f879292011-04-08 00:21:20245 return gfx::Rect();
246}
247
Wei Liab754772018-08-22 22:41:17248unsigned int MetafileSkia::GetPageCount() const {
Lei Zhang8780d73e2018-08-10 18:45:55249 return base::checked_cast<unsigned int>(data_->pages.size());
[email protected]8f879292011-04-08 00:21:20250}
251
Wei Liab754772018-08-22 22:41:17252printing::NativeDrawingContext MetafileSkia::context() const {
[email protected]8f879292011-04-08 00:21:20253 NOTREACHED();
thestig192677ec2016-06-09 07:43:17254 return nullptr;
[email protected]8f879292011-04-08 00:21:20255}
256
257#if defined(OS_WIN)
Wei Liab754772018-08-22 22:41:17258bool MetafileSkia::Playback(printing::NativeDrawingContext hdc,
259 const RECT* rect) const {
thestig3109df12017-04-26 21:57:25260 NOTREACHED();
261 return false;
262}
263
Wei Liab754772018-08-22 22:41:17264bool MetafileSkia::SafePlayback(printing::NativeDrawingContext hdc) const {
[email protected]8f879292011-04-08 00:21:20265 NOTREACHED();
266 return false;
267}
thestig99fc2132017-04-27 03:37:54268
269#elif defined(OS_MACOSX)
270/* TODO(caryclark): The set up of PluginInstance::PrintPDFOutput may result in
271 rasterized output. Even if that flow uses PdfMetafileCg::RenderPage,
272 the drawing of the PDF into the canvas may result in a rasterized output.
273 PDFMetafileSkia::RenderPage should be not implemented as shown and instead
274 should do something like the following CL in PluginInstance::PrintPDFOutput:
275http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_instance.cc
276*/
Wei Liab754772018-08-22 22:41:17277bool MetafileSkia::RenderPage(unsigned int page_number,
278 CGContextRef context,
279 const CGRect rect,
280 const MacRenderPageParams& params) const {
thestig99fc2132017-04-27 03:37:54281 DCHECK_GT(GetDataSize(), 0U);
Lei Zhang8780d73e2018-08-10 18:45:55282 if (data_->pdf_cg.GetDataSize() == 0) {
thestig99fc2132017-04-27 03:37:54283 if (GetDataSize() == 0)
284 return false;
Wei Liab754772018-08-22 22:41:17285 size_t length = data_->data_stream->getLength();
thestig99fc2132017-04-27 03:37:54286 std::vector<uint8_t> buffer(length);
Wei Liab754772018-08-22 22:41:17287 (void)WriteAssetToBuffer(data_->data_stream.get(), &buffer[0], length);
Lei Zhang8780d73e2018-08-10 18:45:55288 data_->pdf_cg.InitFromData(&buffer[0], length);
thestig99fc2132017-04-27 03:37:54289 }
Lei Zhang8780d73e2018-08-10 18:45:55290 return data_->pdf_cg.RenderPage(page_number, context, rect, params);
thestig99fc2132017-04-27 03:37:54291}
[email protected]b8d85bc2011-06-22 13:34:57292#endif
[email protected]8f879292011-04-08 00:21:20293
Wei Liab754772018-08-22 22:41:17294bool MetafileSkia::SaveTo(base::File* file) const {
halcanary5be808e2014-11-10 22:20:05295 if (GetDataSize() == 0U)
296 return false;
halcanaryff214762015-01-22 20:34:32297
298 // Calling duplicate() keeps original asset state unchanged.
Wei Liab754772018-08-22 22:41:17299 std::unique_ptr<SkStreamAsset> asset(data_->data_stream->duplicate());
halcanaryff214762015-01-22 20:34:32300
Lei Zhang8780d73e2018-08-10 18:45:55301 static constexpr size_t kMaximumBufferSize = 1024 * 1024;
thestig192677ec2016-06-09 07:43:17302 std::vector<char> buffer(std::min(kMaximumBufferSize, asset->getLength()));
halcanaryff214762015-01-22 20:34:32303 do {
304 size_t read_size = asset->read(&buffer[0], buffer.size());
305 if (read_size == 0)
306 break;
307 DCHECK_GE(buffer.size(), read_size);
308 if (!file->WriteAtCurrentPos(&buffer[0],
309 base::checked_cast<int>(read_size))) {
310 return false;
311 }
312 } while (!asset->isAtEnd());
313
314 return true;
halcanary5be808e2014-11-10 22:20:05315}
316
Wei Liab754772018-08-22 22:41:17317std::unique_ptr<MetafileSkia> MetafileSkia::GetMetafileForCurrentPage(
halcanaryffa005b2016-06-15 17:13:16318 SkiaDocumentType type) {
halcanaryff214762015-01-22 20:34:32319 // If we only ever need the metafile for the last page, should we
enne7b64edf32017-02-16 20:10:02320 // only keep a handle on one PaintRecord?
Wei Liab754772018-08-22 22:41:17321 auto metafile = std::make_unique<MetafileSkia>(type, data_->document_cookie);
Lei Zhang8780d73e2018-08-10 18:45:55322 if (data_->pages.size() == 0)
dchenge48600452015-12-28 02:24:50323 return metafile;
[email protected]597516372011-07-01 05:10:44324
Lei Zhang8780d73e2018-08-10 18:45:55325 if (data_->recorder.getRecordingCanvas()) // page outstanding
dchenge48600452015-12-28 02:24:50326 return metafile;
[email protected]597516372011-07-01 05:10:44327
Lei Zhang8780d73e2018-08-10 18:45:55328 metafile->data_->pages.push_back(data_->pages.back());
329 metafile->data_->subframe_content_info = data_->subframe_content_info;
330 metafile->data_->subframe_pics = data_->subframe_pics;
halcanaryff214762015-01-22 20:34:32331
332 if (!metafile->FinishDocument()) // Generate PDF.
vitalybuka5d1290582014-09-12 09:19:59333 metafile.reset();
halcanaryff214762015-01-22 20:34:32334
dchenge48600452015-12-28 02:24:50335 return metafile;
[email protected]597516372011-07-01 05:10:44336}
[email protected]8f879292011-04-08 00:21:20337
Wei Liab754772018-08-22 22:41:17338uint32_t MetafileSkia::CreateContentForRemoteFrame(const gfx::Rect& rect,
339 int render_proxy_id) {
Wei Lie13ea642018-02-17 07:55:19340 // Create a place holder picture.
341 sk_sp<SkPicture> pic = SkPicture::MakePlaceholder(
342 SkRect::MakeXYWH(rect.x(), rect.y(), rect.width(), rect.height()));
343
344 // Store the map between content id and the proxy id.
345 uint32_t content_id = pic->uniqueID();
Lei Zhang8780d73e2018-08-10 18:45:55346 DCHECK(!base::ContainsKey(data_->subframe_content_info, content_id));
347 data_->subframe_content_info[content_id] = render_proxy_id;
Wei Lie13ea642018-02-17 07:55:19348
349 // Store the picture content.
Lei Zhang8780d73e2018-08-10 18:45:55350 data_->subframe_pics[content_id] = pic;
Wei Lie13ea642018-02-17 07:55:19351 return content_id;
352}
353
Wei Liab754772018-08-22 22:41:17354int MetafileSkia::GetDocumentCookie() const {
Lei Zhang8780d73e2018-08-10 18:45:55355 return data_->document_cookie;
Wei Lie13ea642018-02-17 07:55:19356}
357
Wei Liab754772018-08-22 22:41:17358const ContentToProxyIdMap& MetafileSkia::GetSubframeContentInfo() const {
Lei Zhang8780d73e2018-08-10 18:45:55359 return data_->subframe_content_info;
Wei Lie13ea642018-02-17 07:55:19360}
361
Wei Liab754772018-08-22 22:41:17362void MetafileSkia::AppendPage(const SkSize& page_size,
363 sk_sp<cc::PaintRecord> record) {
Lei Zhang8780d73e2018-08-10 18:45:55364 data_->pages.emplace_back(page_size, std::move(record));
Wei Li1d345bf2018-08-10 02:52:37365}
366
Wei Liab754772018-08-22 22:41:17367void MetafileSkia::AppendSubframeInfo(uint32_t content_id,
368 int proxy_id,
369 sk_sp<SkPicture> pic_holder) {
Lei Zhang8780d73e2018-08-10 18:45:55370 data_->subframe_content_info[content_id] = proxy_id;
371 data_->subframe_pics[content_id] = pic_holder;
Wei Li1d345bf2018-08-10 02:52:37372}
373
Wei Liab754772018-08-22 22:41:17374SkStreamAsset* MetafileSkia::GetPdfData() const {
375 return data_->data_stream.get();
Wei Li1d345bf2018-08-10 02:52:37376}
377
Wei Liab754772018-08-22 22:41:17378void MetafileSkia::CustomDataToSkPictureCallback(SkCanvas* canvas,
379 uint32_t content_id) {
Wei Lie13ea642018-02-17 07:55:19380 // Check whether this is the one we need to handle.
Lei Zhang8780d73e2018-08-10 18:45:55381 if (!base::ContainsKey(data_->subframe_content_info, content_id))
Wei Lie13ea642018-02-17 07:55:19382 return;
383
Lei Zhang8780d73e2018-08-10 18:45:55384 auto it = data_->subframe_pics.find(content_id);
385 DCHECK(it != data_->subframe_pics.end());
Wei Lie13ea642018-02-17 07:55:19386
387 // Found the picture, draw it on canvas.
388 sk_sp<SkPicture> pic = it->second;
389 SkRect rect = pic->cullRect();
390 SkMatrix matrix = SkMatrix::MakeTrans(rect.x(), rect.y());
391 canvas->drawPicture(it->second, &matrix, nullptr);
392}
393
[email protected]8f879292011-04-08 00:21:20394} // namespace printing