metafile_skia.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef PRINTING_METAFILE_SKIA_H_
  5. #define PRINTING_METAFILE_SKIA_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include "base/gtest_prod_util.h"
  9. #include "build/build_config.h"
  10. #include "cc/paint/paint_canvas.h"
  11. #include "printing/common/metafile_utils.h"
  12. #include "printing/metafile.h"
  13. #include "printing/mojom/print.mojom-forward.h"
  14. #include "skia/ext/platform_canvas.h"
  15. #include "ui/accessibility/ax_tree_update.h"
  16. #if BUILDFLAG(IS_WIN)
  17. #include <windows.h>
  18. #endif
  19. namespace base {
  20. class UnguessableToken;
  21. } // namespace base
  22. namespace printing {
  23. struct MetafileSkiaData;
  24. // This class uses Skia graphics library to generate a PDF or MSKP document.
  25. class COMPONENT_EXPORT(PRINTING_METAFILE) MetafileSkia : public Metafile {
  26. public:
  27. // Default constructor, for mojom::SkiaDocumentType::kPDF type only.
  28. // TODO(weili): we should split up this use case into a different class, see
  29. // comments before InitFromData()'s implementation.
  30. MetafileSkia();
  31. MetafileSkia(mojom::SkiaDocumentType type, int document_cookie);
  32. MetafileSkia(const MetafileSkia&) = delete;
  33. MetafileSkia& operator=(const MetafileSkia&) = delete;
  34. ~MetafileSkia() override;
  35. // Metafile methods.
  36. bool Init() override;
  37. bool InitFromData(base::span<const uint8_t> data) override;
  38. void StartPage(const gfx::Size& page_size,
  39. const gfx::Rect& content_area,
  40. float scale_factor,
  41. mojom::PageOrientation page_orientation) override;
  42. bool FinishPage() override;
  43. bool FinishDocument() override;
  44. uint32_t GetDataSize() const override;
  45. bool GetData(void* dst_buffer, uint32_t dst_buffer_size) const override;
  46. bool ShouldCopySharedMemoryRegionData() const override;
  47. mojom::MetafileDataType GetDataType() const override;
  48. gfx::Rect GetPageBounds(unsigned int page_number) const override;
  49. unsigned int GetPageCount() const override;
  50. printing::NativeDrawingContext context() const override;
  51. #if BUILDFLAG(IS_WIN)
  52. bool Playback(printing::NativeDrawingContext hdc,
  53. const RECT* rect) const override;
  54. bool SafePlayback(printing::NativeDrawingContext hdc) const override;
  55. #elif BUILDFLAG(IS_MAC)
  56. bool RenderPage(unsigned int page_number,
  57. printing::NativeDrawingContext context,
  58. const CGRect& rect,
  59. bool autorotate,
  60. bool fit_to_page) const override;
  61. #endif
  62. #if BUILDFLAG(IS_ANDROID)
  63. bool SaveToFileDescriptor(int fd) const override;
  64. #else
  65. bool SaveTo(base::File* file) const override;
  66. #endif // BUILDFLAG(IS_ANDROID)
  67. // Unlike FinishPage() or FinishDocument(), this is for out-of-process
  68. // subframe printing. It will just serialize the content into SkPicture
  69. // format and store it as final data.
  70. void FinishFrameContent();
  71. // Return a new metafile containing just the current page in draft mode.
  72. std::unique_ptr<MetafileSkia> GetMetafileForCurrentPage(
  73. mojom::SkiaDocumentType type);
  74. // This method calls StartPage and then returns an appropriate
  75. // PlatformCanvas implementation bound to the context created by
  76. // StartPage or NULL on error. The PaintCanvas pointer that
  77. // is returned is owned by this MetafileSkia object and does not
  78. // need to be ref()ed or unref()ed. The canvas will remain valid
  79. // until FinishPage() or FinishDocument() is called.
  80. cc::PaintCanvas* GetVectorCanvasForNewPage(
  81. const gfx::Size& page_size,
  82. const gfx::Rect& content_area,
  83. float scale_factor,
  84. mojom::PageOrientation page_orientation);
  85. // This is used for painting content of out-of-process subframes.
  86. // For such a subframe, since the content is in another process, we create a
  87. // place holder picture now, and replace it with actual content by pdf
  88. // compositor service later.
  89. uint32_t CreateContentForRemoteFrame(
  90. const gfx::Rect& rect,
  91. const base::UnguessableToken& render_proxy_token);
  92. int GetDocumentCookie() const;
  93. const ContentToProxyTokenMap& GetSubframeContentInfo() const;
  94. void UtilizeTypefaceContext(ContentProxySet* typeface_content_info);
  95. const ui::AXTreeUpdate& accessibility_tree() const {
  96. return accessibility_tree_;
  97. }
  98. ui::AXTreeUpdate& accessibility_tree() { return accessibility_tree_; }
  99. private:
  100. FRIEND_TEST_ALL_PREFIXES(MetafileSkiaTest, TestFrameContent);
  101. FRIEND_TEST_ALL_PREFIXES(MetafileSkiaTest, TestMultiPictureDocumentTypefaces);
  102. // The following three functions are used for tests only.
  103. void AppendPage(const SkSize& page_size, sk_sp<cc::PaintRecord> record);
  104. void AppendSubframeInfo(uint32_t content_id,
  105. const base::UnguessableToken& proxy_token,
  106. sk_sp<SkPicture> subframe_pic_holder);
  107. SkStreamAsset* GetPdfData() const;
  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. std::unique_ptr<MetafileSkiaData> data_;
  112. ui::AXTreeUpdate accessibility_tree_;
  113. };
  114. } // namespace printing
  115. #endif // PRINTING_METAFILE_SKIA_H_