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 "ui/accessibility/ax_tree_update.h"
26 class UnguessableToken;
31 struct MetafileSkiaData;
33 // This class uses Skia graphics library to generate a PDF or MSKP document.
34 class COMPONENT_EXPORT(PRINTING_METAFILE) MetafileSkia : public Metafile {
36 // Default constructor, for mojom::SkiaDocumentType::kPDF type only.
37 // TODO(weili): we should split up this use case into a different class, see
38 // comments before InitFromData()'s implementation.
40 MetafileSkia(mojom::SkiaDocumentType type, int document_cookie);
41 MetafileSkia(const MetafileSkia&) = delete;
42 MetafileSkia& operator=(const MetafileSkia&) = delete;
43 ~MetafileSkia() override;
47 bool InitFromData(base::span<const uint8_t> data) override;
49 void StartPage(const gfx::Size& page_size,
50 const gfx::Rect& content_area,
52 mojom::PageOrientation page_orientation) override;
53 bool FinishPage() override;
54 bool FinishDocument() override;
56 uint32_t GetDataSize() const override;
57 bool GetData(void* dst_buffer, uint32_t dst_buffer_size) const override;
58 bool ShouldCopySharedMemoryRegionData() const override;
59 mojom::MetafileDataType GetDataType() const override;
61 gfx::Rect GetPageBounds(unsigned int page_number) const override;
62 unsigned int GetPageCount() const override;
64 printing::NativeDrawingContext context() const override;
67 bool Playback(printing::NativeDrawingContext hdc,
68 const RECT* rect) const override;
69 bool SafePlayback(printing::NativeDrawingContext hdc) const override;
70 #elif BUILDFLAG(IS_MAC)
71 bool RenderPage(unsigned int page_number,
72 printing::NativeDrawingContext context,
75 bool fit_to_page) const override;
78 #if BUILDFLAG(IS_ANDROID)
79 bool SaveToFileDescriptor(int fd) const override;
81 bool SaveTo(base::File* file) const override;
82 #endif // BUILDFLAG(IS_ANDROID)
84 // 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();
89 // Return a new metafile containing just the current page in draft mode.
90 std::unique_ptr<MetafileSkia> GetMetafileForCurrentPage(
91 mojom::SkiaDocumentType type);
93 // This method calls StartPage and then returns an appropriate
94 // PlatformCanvas implementation bound to the context created by
95 // StartPage or NULL on error. The PaintCanvas pointer that
96 // is returned is owned by this MetafileSkia object and does not
97 // need to be ref()ed or unref()ed. The canvas will remain valid
98 // until FinishPage() or FinishDocument() is called.
99 cc::PaintCanvas* GetVectorCanvasForNewPage(
100 const gfx::Size& page_size,
101 const gfx::Rect& content_area,
103 mojom::PageOrientation page_orientation);
105 // 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.
109 uint32_t CreateContentForRemoteFrame(
110 const gfx::Rect& rect,
111 const base::UnguessableToken& render_proxy_token);
113 int GetDocumentCookie() const;
114 const ContentToProxyTokenMap& GetSubframeContentInfo() const;
116 void UtilizeTypefaceContext(ContentProxySet* typeface_content_info);
118 const ui::AXTreeUpdate& accessibility_tree() const {
119 return accessibility_tree_;
121 ui::AXTreeUpdate& accessibility_tree() { return accessibility_tree_; }
124 FRIEND_TEST_ALL_PREFIXES(MetafileSkiaTest, TestFrameContent);
125 FRIEND_TEST_ALL_PREFIXES(MetafileSkiaTest, TestMultiPictureDocumentTypefaces);
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,
130 const base::UnguessableToken& proxy_token,
131 sk_sp<SkPicture> subframe_pic_holder);
132 SkStreamAsset* GetPdfData() const;
134 // 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);
138 std::unique_ptr<MetafileSkiaData> data_;
140 ui::AXTreeUpdate accessibility_tree_;
143 } // namespace printing
145 #endif // PRINTING_METAFILE_SKIA_H_