[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Wei Li | ab75477 | 2018-08-22 22:41:17 | [diff] [blame] | 5 | #ifndef PRINTING_METAFILE_SKIA_H_ |
| 6 | #define PRINTING_METAFILE_SKIA_H_ |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 7 | |
avi | 126e93c | 2015-12-21 21:48:16 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
dcheng | c3df9ba | 2016-04-07 23:09:32 | [diff] [blame] | 10 | #include <memory> |
| 11 | |
Wei Li | 1d345bf | 2018-08-10 02:52:37 | [diff] [blame] | 12 | #include "base/gtest_prod_util.h" |
avi | 126e93c | 2015-12-21 21:48:16 | [diff] [blame] | 13 | #include "base/macros.h" |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 14 | #include "build/build_config.h" |
enne | 7b64edf3 | 2017-02-16 20:10:02 | [diff] [blame] | 15 | #include "cc/paint/paint_canvas.h" |
Wei Li | ab75477 | 2018-08-22 22:41:17 | [diff] [blame] | 16 | #include "printing/common/metafile_utils.h" |
[email protected] | 7d748990 | 2011-04-11 21:54:06 | [diff] [blame] | 17 | #include "printing/metafile.h" |
vitalybuka | a73fc37 | 2015-02-06 18:40:37 | [diff] [blame] | 18 | #include "skia/ext/platform_canvas.h" |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 19 | |
| 20 | #if defined(OS_WIN) |
| 21 | #include <windows.h> |
| 22 | #endif |
| 23 | |
| 24 | namespace printing { |
| 25 | |
Wei Li | ab75477 | 2018-08-22 22:41:17 | [diff] [blame] | 26 | struct MetafileSkiaData; |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 27 | |
Lei Zhang | 662e422 | 2017-12-14 20:18:34 | [diff] [blame] | 28 | // This class uses Skia graphics library to generate a PDF or MSKP document. |
Wei Li | ab75477 | 2018-08-22 22:41:17 | [diff] [blame] | 29 | class PRINTING_EXPORT MetafileSkia : public Metafile { |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 30 | public: |
Wei Li | e13ea64 | 2018-02-17 07:55:19 | [diff] [blame] | 31 | // Default constructor, for SkiaDocumentType::PDF type only. |
| 32 | // TODO(weili): we should split up this use case into a different class, see |
| 33 | // comments before InitFromData()'s implementation. |
Wei Li | ab75477 | 2018-08-22 22:41:17 | [diff] [blame] | 34 | MetafileSkia(); |
| 35 | MetafileSkia(SkiaDocumentType type, int document_cookie); |
| 36 | ~MetafileSkia() override; |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 37 | |
[email protected] | 7d748990 | 2011-04-11 21:54:06 | [diff] [blame] | 38 | // Metafile methods. |
dcheng | d5a0f18 | 2014-10-22 00:04:34 | [diff] [blame] | 39 | bool Init() override; |
thestig | 6b4461f | 2016-10-28 19:34:30 | [diff] [blame] | 40 | bool InitFromData(const void* src_buffer, size_t src_buffer_size) override; |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 41 | |
thestig | 192677ec | 2016-06-09 07:43:17 | [diff] [blame] | 42 | void StartPage(const gfx::Size& page_size, |
dcheng | d5a0f18 | 2014-10-22 00:04:34 | [diff] [blame] | 43 | const gfx::Rect& content_area, |
| 44 | const float& scale_factor) override; |
| 45 | bool FinishPage() override; |
| 46 | bool FinishDocument() override; |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 47 | |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 48 | uint32_t GetDataSize() const override; |
| 49 | bool GetData(void* dst_buffer, uint32_t dst_buffer_size) const override; |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 50 | |
dcheng | d5a0f18 | 2014-10-22 00:04:34 | [diff] [blame] | 51 | gfx::Rect GetPageBounds(unsigned int page_number) const override; |
| 52 | unsigned int GetPageCount() const override; |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 53 | |
Nico Weber | 8e55956 | 2017-10-03 01:25:26 | [diff] [blame] | 54 | printing::NativeDrawingContext context() const override; |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 55 | |
| 56 | #if defined(OS_WIN) |
Nico Weber | 8e55956 | 2017-10-03 01:25:26 | [diff] [blame] | 57 | bool Playback(printing::NativeDrawingContext hdc, |
thestig | 3109df1 | 2017-04-26 21:57:25 | [diff] [blame] | 58 | const RECT* rect) const override; |
Nico Weber | 8e55956 | 2017-10-03 01:25:26 | [diff] [blame] | 59 | bool SafePlayback(printing::NativeDrawingContext hdc) const override; |
thestig | 99fc213 | 2017-04-27 03:37:54 | [diff] [blame] | 60 | #elif defined(OS_MACOSX) |
| 61 | bool RenderPage(unsigned int page_number, |
Nico Weber | 8e55956 | 2017-10-03 01:25:26 | [diff] [blame] | 62 | printing::NativeDrawingContext context, |
thestig | 99fc213 | 2017-04-27 03:37:54 | [diff] [blame] | 63 | const CGRect rect, |
| 64 | const MacRenderPageParams& params) const override; |
[email protected] | b8d85bc | 2011-06-22 13:34:57 | [diff] [blame] | 65 | #endif |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 66 | |
halcanary | 5be808e | 2014-11-10 22:20:05 | [diff] [blame] | 67 | bool SaveTo(base::File* file) const override; |
| 68 | |
Wei Li | 5bb65974 | 2018-02-14 03:07:58 | [diff] [blame] | 69 | // Unlike FinishPage() or FinishDocument(), this is for out-of-process |
| 70 | // subframe printing. It will just serialize the content into SkPicture |
| 71 | // format and store it as final data. |
| 72 | void FinishFrameContent(); |
| 73 | |
[email protected] | 59751637 | 2011-07-01 05:10:44 | [diff] [blame] | 74 | // Return a new metafile containing just the current page in draft mode. |
Wei Li | ab75477 | 2018-08-22 22:41:17 | [diff] [blame] | 75 | std::unique_ptr<MetafileSkia> GetMetafileForCurrentPage( |
halcanary | ffa005b | 2016-06-15 17:13:16 | [diff] [blame] | 76 | SkiaDocumentType type); |
[email protected] | 59751637 | 2011-07-01 05:10:44 | [diff] [blame] | 77 | |
vitalybuka | 9de71ea | 2014-11-05 09:22:34 | [diff] [blame] | 78 | // This method calls StartPage and then returns an appropriate |
vitalybuka | a73fc37 | 2015-02-06 18:40:37 | [diff] [blame] | 79 | // PlatformCanvas implementation bound to the context created by |
enne | 7b64edf3 | 2017-02-16 20:10:02 | [diff] [blame] | 80 | // StartPage or NULL on error. The PaintCanvas pointer that |
Wei Li | ab75477 | 2018-08-22 22:41:17 | [diff] [blame] | 81 | // is returned is owned by this MetafileSkia object and does not |
halcanary | 5be808e | 2014-11-10 22:20:05 | [diff] [blame] | 82 | // need to be ref()ed or unref()ed. The canvas will remain valid |
| 83 | // until FinishPage() or FinishDocument() is called. |
enne | 7b64edf3 | 2017-02-16 20:10:02 | [diff] [blame] | 84 | cc::PaintCanvas* GetVectorCanvasForNewPage(const gfx::Size& page_size, |
| 85 | const gfx::Rect& content_area, |
| 86 | const float& scale_factor); |
halcanary | 5be808e | 2014-11-10 22:20:05 | [diff] [blame] | 87 | |
Wei Li | e13ea64 | 2018-02-17 07:55:19 | [diff] [blame] | 88 | // This is used for painting content of out-of-process subframes. |
| 89 | // For such a subframe, since the content is in another process, we create a |
| 90 | // place holder picture now, and replace it with actual content by pdf |
| 91 | // compositor service later. |
| 92 | uint32_t CreateContentForRemoteFrame(const gfx::Rect& rect, |
| 93 | int render_proxy_id); |
| 94 | |
| 95 | int GetDocumentCookie() const; |
| 96 | const ContentToProxyIdMap& GetSubframeContentInfo() const; |
| 97 | |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 98 | private: |
Wei Li | ab75477 | 2018-08-22 22:41:17 | [diff] [blame] | 99 | FRIEND_TEST_ALL_PREFIXES(MetafileSkiaTest, TestFrameContent); |
Wei Li | 1d345bf | 2018-08-10 02:52:37 | [diff] [blame] | 100 | |
| 101 | // The following three functions are used for tests only. |
| 102 | void AppendPage(const SkSize& page_size, sk_sp<cc::PaintRecord> record); |
| 103 | void AppendSubframeInfo(uint32_t content_id, |
| 104 | int proxy_id, |
| 105 | sk_sp<SkPicture> subframe_pic_holder); |
| 106 | SkStreamAsset* GetPdfData() const; |
| 107 | |
Wei Li | e13ea64 | 2018-02-17 07:55:19 | [diff] [blame] | 108 | // Callback function used during page content drawing to replace a custom |
| 109 | // data holder with corresponding place holder SkPicture. |
| 110 | void CustomDataToSkPictureCallback(SkCanvas* canvas, uint32_t content_id); |
| 111 | |
Wei Li | ab75477 | 2018-08-22 22:41:17 | [diff] [blame] | 112 | std::unique_ptr<MetafileSkiaData> data_; |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 113 | |
Wei Li | ab75477 | 2018-08-22 22:41:17 | [diff] [blame] | 114 | DISALLOW_COPY_AND_ASSIGN(MetafileSkia); |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | } // namespace printing |
| 118 | |
Wei Li | ab75477 | 2018-08-22 22:41:17 | [diff] [blame] | 119 | #endif // PRINTING_METAFILE_SKIA_H_ |