1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef PRINTING_METAFILE_SKIA_H_
6 #define PRINTING_METAFILE_SKIA_H_
12 #include "base/gtest_prod_util.h"
13 #include "build/build_config.h"
14 #include "cc/paint/paint_canvas.h"
15 #include "printing/common/metafile_utils.h"
16 #include "printing/metafile.h"
17 #include "printing/mojom/print.mojom-forward.h"
18 #include "skia/ext/platform_canvas.h"
19 #include "third_party/skia/include/core/SkRefCnt.h"
20 #include "ui/accessibility/ax_tree_update.h"
31 class UnguessableToken;
36 struct MetafileSkiaData;
38 // This class uses Skia graphics library to generate a PDF or MSKP document.
39 class COMPONENT_EXPORT(PRINTING_METAFILE) MetafileSkia : public Metafile {
41 // Default constructor, for mojom::SkiaDocumentType::kPDF type only.
42 // TODO(weili): we should split up this use case into a different class, see
43 // comments before InitFromData()'s implementation.
45 MetafileSkia(mojom::SkiaDocumentType type, int document_cookie);
46 MetafileSkia(const MetafileSkia&) = delete;
47 MetafileSkia& operator=(const MetafileSkia&) = delete;
48 ~MetafileSkia() override;
52 bool InitFromData(base::span<const uint8_t> data) override;
54 void StartPage(const gfx::Size& page_size,
55 const gfx::Rect& content_area,
57 mojom::PageOrientation page_orientation) override;
58 bool FinishPage() override;
59 bool FinishDocument() override;
61 uint32_t GetDataSize() const override;
62 bool GetData(void* dst_buffer, uint32_t dst_buffer_size) const override;
63 bool ShouldCopySharedMemoryRegionData() const override;
64 mojom::MetafileDataType GetDataType() const override;
66 gfx::Rect GetPageBounds(unsigned int page_number) const override;
67 unsigned int GetPageCount() const override;
69 printing::NativeDrawingContext context() const override;
72 bool Playback(printing::NativeDrawingContext hdc,
73 const RECT* rect) const override;
74 bool SafePlayback(printing::NativeDrawingContext hdc) const override;
75 #elif BUILDFLAG(IS_APPLE)
76 bool RenderPage(unsigned int page_number,
77 printing::NativeDrawingContext context,
80 bool fit_to_page) const override;
83 #if BUILDFLAG(IS_ANDROID)
84 bool SaveToFileDescriptor(int fd) const override;
86 bool SaveTo(base::File* file) const override;
87 #endif // BUILDFLAG(IS_ANDROID)
89 // Unlike FinishPage() or FinishDocument(), this is for out-of-process
90 // subframe printing. It will just serialize the content into SkPicture
91 // format and store it as final data.
92 void FinishFrameContent();
94 // Return a new metafile containing just the current page in draft mode.
95 std::unique_ptr<MetafileSkia> GetMetafileForCurrentPage(
96 mojom::SkiaDocumentType type);
98 // This method calls StartPage and then returns an appropriate
99 // PlatformCanvas implementation bound to the context created by
100 // StartPage or NULL on error. The PaintCanvas pointer that
101 // is returned is owned by this MetafileSkia object and does not
102 // need to be ref()ed or unref()ed. The canvas will remain valid
103 // until FinishPage() or FinishDocument() is called.
104 cc::PaintCanvas* GetVectorCanvasForNewPage(
105 const gfx::Size& page_size,
106 const gfx::Rect& content_area,
108 mojom::PageOrientation page_orientation);
110 // This is used for painting content of out-of-process subframes.
111 // For such a subframe, since the content is in another process, we create a
112 // place holder picture now, and replace it with actual content by pdf
113 // compositor service later.
114 uint32_t CreateContentForRemoteFrame(
115 const gfx::Rect& rect,
116 const base::UnguessableToken& render_proxy_token);
118 int GetDocumentCookie() const;
119 const ContentToProxyTokenMap& GetSubframeContentInfo() const;
121 void UtilizeTypefaceContext(ContentProxySet* typeface_content_info);
123 const ui::AXTreeUpdate& accessibility_tree() const {
124 return accessibility_tree_;
126 ui::AXTreeUpdate& accessibility_tree() { return accessibility_tree_; }
129 FRIEND_TEST_ALL_PREFIXES(MetafileSkiaTest, FrameContent);
130 FRIEND_TEST_ALL_PREFIXES(MetafileSkiaTest, GetPageBounds);
131 FRIEND_TEST_ALL_PREFIXES(MetafileSkiaTest, MultiPictureDocumentTypefaces);
133 void AppendPage(const SkSize& page_size, cc::PaintRecord record);
134 void AppendSubframeInfo(uint32_t content_id,
135 const base::UnguessableToken& proxy_token,
136 sk_sp<SkPicture> subframe_pic_holder);
138 // This is used for tests only.
139 SkStreamAsset* GetPdfData() const;
141 // Callback function used during page content drawing to replace a custom
142 // data holder with corresponding place holder SkPicture.
143 void CustomDataToSkPictureCallback(SkCanvas* canvas, uint32_t content_id);
145 std::unique_ptr<MetafileSkiaData> data_;
147 ui::AXTreeUpdate accessibility_tree_;
150 } // namespace printing
152 #endif // PRINTING_METAFILE_SKIA_H_