dc_layer_tree.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Copyright 2019 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 UI_GL_DC_LAYER_TREE_H_
  5. #define UI_GL_DC_LAYER_TREE_H_
  6. #include <windows.h>
  7. #include <d3d11.h>
  8. #include <dcomp.h>
  9. #include <wrl/client.h>
  10. #include <memory>
  11. #include "base/containers/flat_map.h"
  12. #include "mojo/public/cpp/bindings/pending_receiver.h"
  13. #include "ui/gfx/color_space_win.h"
  14. #include "ui/gfx/geometry/size.h"
  15. #include "ui/gl/dc_renderer_layer_params.h"
  16. #include "ui/gl/delegated_ink_point_renderer_gpu.h"
  17. #include "ui/gl/hdr_metadata_helper_win.h"
  18. namespace gfx {
  19. namespace mojom {
  20. class DelegatedInkPointRenderer;
  21. } // namespace mojom
  22. class DelegatedInkMetadata;
  23. } // namespace gfx
  24. namespace gl {
  25. class DirectCompositionChildSurfaceWin;
  26. class SwapChainPresenter;
  27. enum class VideoProcessorType { kSDR, kHDR };
  28. // Cache video processor and its size.
  29. struct VideoProcessorWrapper {
  30. VideoProcessorWrapper();
  31. ~VideoProcessorWrapper();
  32. VideoProcessorWrapper(VideoProcessorWrapper&& other);
  33. VideoProcessorWrapper& operator=(VideoProcessorWrapper&& other);
  34. VideoProcessorWrapper(const VideoProcessorWrapper&) = delete;
  35. VideoProcessorWrapper& operator=(VideoProcessorWrapper& other) = delete;
  36. // Input and output size of video processor .
  37. gfx::Size video_input_size;
  38. gfx::Size video_output_size;
  39. // The video processor is cached so SwapChains don't have to recreate it
  40. // whenever they're created.
  41. Microsoft::WRL::ComPtr<ID3D11VideoDevice> video_device;
  42. Microsoft::WRL::ComPtr<ID3D11VideoContext> video_context;
  43. Microsoft::WRL::ComPtr<ID3D11VideoProcessor> video_processor;
  44. Microsoft::WRL::ComPtr<ID3D11VideoProcessorEnumerator>
  45. video_processor_enumerator;
  46. };
  47. // DCLayerTree manages a tree of direct composition visuals, and associated
  48. // swap chains for given overlay layers. It maintains a list of pending layers
  49. // submitted using ScheduleDCLayer() that are presented and committed in
  50. // CommitAndClearPendingOverlays().
  51. class DCLayerTree {
  52. public:
  53. using VideoProcessorMap =
  54. base::flat_map<VideoProcessorType, VideoProcessorWrapper>;
  55. using DelegatedInkRenderer =
  56. DelegatedInkPointRendererGpu<IDCompositionInkTrailDevice,
  57. IDCompositionDelegatedInkTrail,
  58. DCompositionInkTrailPoint>;
  59. DCLayerTree(bool disable_nv12_dynamic_textures,
  60. bool disable_vp_scaling,
  61. bool disable_vp_super_resolution,
  62. bool no_downscaled_overlay_promotion);
  63. DCLayerTree(const DCLayerTree&) = delete;
  64. DCLayerTree& operator=(const DCLayerTree&) = delete;
  65. ~DCLayerTree();
  66. // Returns true on success.
  67. bool Initialize(HWND window);
  68. // Present pending overlay layers, and perform a direct composition commit if
  69. // necessary. Returns true if presentation and commit succeeded.
  70. bool CommitAndClearPendingOverlays(
  71. DirectCompositionChildSurfaceWin* root_surface);
  72. // Schedule an overlay layer for the next CommitAndClearPendingOverlays call.
  73. bool ScheduleDCLayer(std::unique_ptr<ui::DCRendererLayerParams> params);
  74. // Called by SwapChainPresenter to initialize video processor that can handle
  75. // at least given input and output size. The video processor is shared across
  76. // layers so the same one can be reused if it's large enough. Returns true on
  77. // success.
  78. VideoProcessorWrapper* InitializeVideoProcessor(const gfx::Size& input_size,
  79. const gfx::Size& output_size,
  80. bool is_hdr_output);
  81. void SetNeedsRebuildVisualTree() { needs_rebuild_visual_tree_ = true; }
  82. bool disable_nv12_dynamic_textures() const {
  83. return disable_nv12_dynamic_textures_;
  84. }
  85. bool disable_vp_scaling() const { return disable_vp_scaling_; }
  86. bool disable_vp_super_resolution() const {
  87. return disable_vp_super_resolution_;
  88. }
  89. bool no_downscaled_overlay_promotion() const {
  90. return no_downscaled_overlay_promotion_;
  91. }
  92. VideoProcessorWrapper& GetOrCreateVideoProcessor(bool is_hdr);
  93. Microsoft::WRL::ComPtr<IDXGISwapChain1> GetLayerSwapChainForTesting(
  94. size_t index) const;
  95. void GetSwapChainVisualInfoForTesting(size_t index,
  96. gfx::Transform* transform,
  97. gfx::Point* offset,
  98. gfx::Rect* clip_rect) const;
  99. void SetFrameRate(float frame_rate);
  100. const std::unique_ptr<HDRMetadataHelperWin>& GetHDRMetadataHelper() {
  101. return hdr_metadata_helper_;
  102. }
  103. HWND window() const { return window_; }
  104. bool SupportsDelegatedInk();
  105. void SetDelegatedInkTrailStartPoint(
  106. std::unique_ptr<gfx::DelegatedInkMetadata>);
  107. void InitDelegatedInkPointRendererReceiver(
  108. mojo::PendingReceiver<gfx::mojom::DelegatedInkPointRenderer>
  109. pending_receiver);
  110. DelegatedInkRenderer* GetInkRendererForTesting() const {
  111. return ink_renderer_.get();
  112. }
  113. private:
  114. // This will add an ink visual to the visual tree to enable delegated ink
  115. // trails. This will initially always be called directly before an OS
  116. // delegated ink API is used. After that, it can also be added anytime the
  117. // visual tree is rebuilt.
  118. void AddDelegatedInkVisualToTree();
  119. // The ink renderer must be initialized before an OS API is used in order to
  120. // set up the delegated ink visual and delegated ink trail object.
  121. bool InitializeInkRenderer();
  122. const bool disable_nv12_dynamic_textures_;
  123. const bool disable_vp_scaling_;
  124. const bool disable_vp_super_resolution_;
  125. const bool no_downscaled_overlay_promotion_;
  126. HWND window_;
  127. Microsoft::WRL::ComPtr<ID3D11Device> d3d11_device_;
  128. Microsoft::WRL::ComPtr<IDCompositionDevice2> dcomp_device_;
  129. Microsoft::WRL::ComPtr<IDCompositionTarget> dcomp_target_;
  130. // Store video processor for SDR/HDR mode separately, which could avoid
  131. // problem in (http://crbug.com/1121061).
  132. VideoProcessorMap video_processor_map_;
  133. // Current video processor input and output colorspace.
  134. gfx::ColorSpace video_input_color_space_;
  135. gfx::ColorSpace video_output_color_space_;
  136. // Set to true if a direct composition visual tree needs rebuild.
  137. bool needs_rebuild_visual_tree_ = false;
  138. // Set if root surface is using a swap chain currently.
  139. Microsoft::WRL::ComPtr<IDXGISwapChain1> root_swap_chain_;
  140. // Set if root surface is using a direct composition surface currently.
  141. Microsoft::WRL::ComPtr<IDCompositionSurface> root_dcomp_surface_;
  142. uint64_t root_dcomp_surface_serial_;
  143. // Direct composition visual for root surface.
  144. Microsoft::WRL::ComPtr<IDCompositionVisual2> root_surface_visual_;
  145. // Root direct composition visual for window dcomp target.
  146. Microsoft::WRL::ComPtr<IDCompositionVisual2> dcomp_root_visual_;
  147. // List of pending overlay layers from ScheduleDCLayer().
  148. std::vector<std::unique_ptr<ui::DCRendererLayerParams>> pending_overlays_;
  149. // List of swap chain presenters for previous frame.
  150. std::vector<std::unique_ptr<SwapChainPresenter>> video_swap_chains_;
  151. // Number of frames per second.
  152. float frame_rate_ = 0.f;
  153. // dealing with hdr metadata
  154. std::unique_ptr<HDRMetadataHelperWin> hdr_metadata_helper_;
  155. // Renderer for drawing delegated ink trails using OS APIs. This is created
  156. // when the DCLayerTree is created, but can only be queried to check if the
  157. // platform supports delegated ink trails. It must be initialized via the
  158. // Initialize() method in order to be used for drawing delegated ink trails.
  159. std::unique_ptr<DelegatedInkRenderer> ink_renderer_;
  160. };
  161. } // namespace gl
  162. #endif // UI_GL_DC_LAYER_TREE_H_