blob: f6e4d70402ddc15eb11973fa42d91a62c4281b5a [file] [log] [blame]
[email protected]b5cf844c2012-06-18 21:49:201// 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#ifndef PRINTING_METAFILE_SKIA_H_
6#define PRINTING_METAFILE_SKIA_H_
[email protected]8f879292011-04-08 00:21:207
avi126e93c2015-12-21 21:48:168#include <stdint.h>
9
dchengc3df9ba2016-04-07 23:09:3210#include <memory>
11
Wei Li1d345bf2018-08-10 02:52:3712#include "base/gtest_prod_util.h"
[email protected]8f879292011-04-08 00:21:2013#include "build/build_config.h"
enne7b64edf32017-02-16 20:10:0214#include "cc/paint/paint_canvas.h"
Wei Liab754772018-08-22 22:41:1715#include "printing/common/metafile_utils.h"
[email protected]7d7489902011-04-11 21:54:0616#include "printing/metafile.h"
Julie Jeongeun Kime1abed92020-06-11 11:01:4817#include "printing/mojom/print.mojom-forward.h"
vitalybukaa73fc372015-02-06 18:40:3718#include "skia/ext/platform_canvas.h"
Dominic Mazzoni0a39059c2020-01-08 19:30:4519#include "ui/accessibility/ax_tree_update.h"
[email protected]8f879292011-04-08 00:21:2020
Xiaohan Wange3d00dd62022-01-08 02:33:2821#if BUILDFLAG(IS_WIN)
[email protected]8f879292011-04-08 00:21:2022#include <windows.h>
23#endif
24
Gyuyoung Kim5f7f4c72020-07-14 13:09:0325namespace base {
26class UnguessableToken;
27} // namespace base
28
[email protected]8f879292011-04-08 00:21:2029namespace printing {
30
Wei Liab754772018-08-22 22:41:1731struct MetafileSkiaData;
[email protected]8f879292011-04-08 00:21:2032
Lei Zhang662e4222017-12-14 20:18:3433// This class uses Skia graphics library to generate a PDF or MSKP document.
Lei Zhang465804b2021-09-21 17:04:3134class COMPONENT_EXPORT(PRINTING_METAFILE) MetafileSkia : public Metafile {
[email protected]8f879292011-04-08 00:21:2035 public:
Julie Jeongeun Kime1abed92020-06-11 11:01:4836 // Default constructor, for mojom::SkiaDocumentType::kPDF type only.
Wei Lie13ea642018-02-17 07:55:1937 // TODO(weili): we should split up this use case into a different class, see
38 // comments before InitFromData()'s implementation.
Wei Liab754772018-08-22 22:41:1739 MetafileSkia();
Julie Jeongeun Kime1abed92020-06-11 11:01:4840 MetafileSkia(mojom::SkiaDocumentType type, int document_cookie);
Lei Zhangdd3e9b02020-08-19 23:04:4241 MetafileSkia(const MetafileSkia&) = delete;
42 MetafileSkia& operator=(const MetafileSkia&) = delete;
Wei Liab754772018-08-22 22:41:1743 ~MetafileSkia() override;
[email protected]8f879292011-04-08 00:21:2044
[email protected]7d7489902011-04-11 21:54:0645 // Metafile methods.
dchengd5a0f182014-10-22 00:04:3446 bool Init() override;
Lei Zhangb4ef0d5d2020-03-27 23:18:2947 bool InitFromData(base::span<const uint8_t> data) override;
[email protected]8f879292011-04-08 00:21:2048
thestig192677ec2016-06-09 07:43:1749 void StartPage(const gfx::Size& page_size,
dchengd5a0f182014-10-22 00:04:3450 const gfx::Rect& content_area,
Morten Stenshorneeebf2db2020-05-25 09:00:0551 float scale_factor,
52 mojom::PageOrientation page_orientation) override;
dchengd5a0f182014-10-22 00:04:3453 bool FinishPage() override;
54 bool FinishDocument() override;
[email protected]8f879292011-04-08 00:21:2055
thestig707a24b22015-09-14 18:16:3356 uint32_t GetDataSize() const override;
57 bool GetData(void* dst_buffer, uint32_t dst_buffer_size) const override;
Alan Screene35d2632021-12-02 18:44:4358 bool ShouldCopySharedMemoryRegionData() const override;
Alan Screen313f5082021-09-15 21:16:5359 mojom::MetafileDataType GetDataType() const override;
[email protected]8f879292011-04-08 00:21:2060
dchengd5a0f182014-10-22 00:04:3461 gfx::Rect GetPageBounds(unsigned int page_number) const override;
62 unsigned int GetPageCount() const override;
[email protected]8f879292011-04-08 00:21:2063
Nico Weber8e559562017-10-03 01:25:2664 printing::NativeDrawingContext context() const override;
[email protected]8f879292011-04-08 00:21:2065
Xiaohan Wange3d00dd62022-01-08 02:33:2866#if BUILDFLAG(IS_WIN)
Nico Weber8e559562017-10-03 01:25:2667 bool Playback(printing::NativeDrawingContext hdc,
thestig3109df12017-04-26 21:57:2568 const RECT* rect) const override;
Nico Weber8e559562017-10-03 01:25:2669 bool SafePlayback(printing::NativeDrawingContext hdc) const override;
Xiaohan Wange3d00dd62022-01-08 02:33:2870#elif BUILDFLAG(IS_MAC)
thestig99fc2132017-04-27 03:37:5471 bool RenderPage(unsigned int page_number,
Nico Weber8e559562017-10-03 01:25:2672 printing::NativeDrawingContext context,
Lei Zhangf69ea3b2020-03-24 17:53:4873 const CGRect& rect,
Daniel Hosseinian345ecfa2020-04-08 03:45:1674 bool autorotate,
75 bool fit_to_page) const override;
[email protected]b8d85bc2011-06-22 13:34:5776#endif
[email protected]8f879292011-04-08 00:21:2077
Xiaohan Wange3d00dd62022-01-08 02:33:2878#if BUILDFLAG(IS_ANDROID)
Shimi Zhang5ccdf202020-02-13 01:13:1979 bool SaveToFileDescriptor(int fd) const override;
80#else
halcanary5be808e2014-11-10 22:20:0581 bool SaveTo(base::File* file) const override;
Xiaohan Wange3d00dd62022-01-08 02:33:2882#endif // BUILDFLAG(IS_ANDROID)
halcanary5be808e2014-11-10 22:20:0583
Wei Li5bb659742018-02-14 03:07:5884 // Unlike FinishPage() or FinishDocument(), this is for out-of-process
85 // subframe printing. It will just serialize the content into SkPicture
86 // format and store it as final data.
87 void FinishFrameContent();
88
[email protected]597516372011-07-01 05:10:4489 // Return a new metafile containing just the current page in draft mode.
Wei Liab754772018-08-22 22:41:1790 std::unique_ptr<MetafileSkia> GetMetafileForCurrentPage(
Julie Jeongeun Kime1abed92020-06-11 11:01:4891 mojom::SkiaDocumentType type);
[email protected]597516372011-07-01 05:10:4492
vitalybuka9de71ea2014-11-05 09:22:3493 // This method calls StartPage and then returns an appropriate
vitalybukaa73fc372015-02-06 18:40:3794 // PlatformCanvas implementation bound to the context created by
enne7b64edf32017-02-16 20:10:0295 // StartPage or NULL on error. The PaintCanvas pointer that
Wei Liab754772018-08-22 22:41:1796 // is returned is owned by this MetafileSkia object and does not
halcanary5be808e2014-11-10 22:20:0597 // need to be ref()ed or unref()ed. The canvas will remain valid
98 // until FinishPage() or FinishDocument() is called.
Morten Stenshorneeebf2db2020-05-25 09:00:0599 cc::PaintCanvas* GetVectorCanvasForNewPage(
100 const gfx::Size& page_size,
101 const gfx::Rect& content_area,
102 float scale_factor,
103 mojom::PageOrientation page_orientation);
halcanary5be808e2014-11-10 22:20:05104
Wei Lie13ea642018-02-17 07:55:19105 // This is used for painting content of out-of-process subframes.
106 // For such a subframe, since the content is in another process, we create a
107 // place holder picture now, and replace it with actual content by pdf
108 // compositor service later.
Gyuyoung Kim5f7f4c72020-07-14 13:09:03109 uint32_t CreateContentForRemoteFrame(
110 const gfx::Rect& rect,
111 const base::UnguessableToken& render_proxy_token);
Wei Lie13ea642018-02-17 07:55:19112
113 int GetDocumentCookie() const;
Gyuyoung Kim5f7f4c72020-07-14 13:09:03114 const ContentToProxyTokenMap& GetSubframeContentInfo() const;
Wei Lie13ea642018-02-17 07:55:19115
Alan Screen06d20162020-06-26 17:42:12116 void UtilizeTypefaceContext(ContentProxySet* typeface_content_info);
117
Dominic Mazzoni0a39059c2020-01-08 19:30:45118 const ui::AXTreeUpdate& accessibility_tree() const {
119 return accessibility_tree_;
120 }
121 ui::AXTreeUpdate& accessibility_tree() { return accessibility_tree_; }
122
[email protected]8f879292011-04-08 00:21:20123 private:
Wei Liab754772018-08-22 22:41:17124 FRIEND_TEST_ALL_PREFIXES(MetafileSkiaTest, TestFrameContent);
Alan Screen06d20162020-06-26 17:42:12125 FRIEND_TEST_ALL_PREFIXES(MetafileSkiaTest, TestMultiPictureDocumentTypefaces);
Wei Li1d345bf2018-08-10 02:52:37126
127 // The following three functions are used for tests only.
128 void AppendPage(const SkSize& page_size, sk_sp<cc::PaintRecord> record);
129 void AppendSubframeInfo(uint32_t content_id,
Gyuyoung Kim5f7f4c72020-07-14 13:09:03130 const base::UnguessableToken& proxy_token,
Wei Li1d345bf2018-08-10 02:52:37131 sk_sp<SkPicture> subframe_pic_holder);
132 SkStreamAsset* GetPdfData() const;
133
Wei Lie13ea642018-02-17 07:55:19134 // Callback function used during page content drawing to replace a custom
135 // data holder with corresponding place holder SkPicture.
136 void CustomDataToSkPictureCallback(SkCanvas* canvas, uint32_t content_id);
137
Wei Liab754772018-08-22 22:41:17138 std::unique_ptr<MetafileSkiaData> data_;
[email protected]8f879292011-04-08 00:21:20139
Dominic Mazzoni0a39059c2020-01-08 19:30:45140 ui::AXTreeUpdate accessibility_tree_;
[email protected]8f879292011-04-08 00:21:20141};
142
143} // namespace printing
144
Wei Liab754772018-08-22 22:41:17145#endif // PRINTING_METAFILE_SKIA_H_