camera_video_frame_renderer.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright 2022 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 ASH_CAPTURE_MODE_CAMERA_VIDEO_FRAME_RENDERER_H_
  5. #define ASH_CAPTURE_MODE_CAMERA_VIDEO_FRAME_RENDERER_H_
  6. #include <memory>
  7. #include "ash/capture_mode/camera_video_frame_handler.h"
  8. #include "base/memory/scoped_refptr.h"
  9. #include "cc/trees/layer_tree_frame_sink_client.h"
  10. #include "components/viz/client/client_resource_provider.h"
  11. #include "components/viz/common/frame_sinks/begin_frame_source.h"
  12. #include "components/viz/common/quads/compositor_frame.h"
  13. #include "ui/aura/window.h"
  14. #include "ui/gfx/geometry/size.h"
  15. namespace cc {
  16. class LayerTreeFrameSink;
  17. } // namespace cc
  18. namespace media {
  19. class VideoResourceUpdater;
  20. } // namespace media
  21. namespace viz {
  22. class ContextProvider;
  23. class RasterContextProvider;
  24. } // namespace viz
  25. namespace ash {
  26. // Renders the video frames received from the camera video device by creating
  27. // independent compositor frames containing the video frames and submitting them
  28. // on a layer tree frame sink created on the `host_window_`.
  29. class CameraVideoFrameRenderer : public CameraVideoFrameHandler::Delegate,
  30. public viz::BeginFrameObserverBase,
  31. public cc::LayerTreeFrameSinkClient {
  32. public:
  33. CameraVideoFrameRenderer(
  34. mojo::Remote<video_capture::mojom::VideoSource> camera_video_source,
  35. const media::VideoCaptureFormat& capture_format,
  36. bool should_flip_frames_horizontally);
  37. CameraVideoFrameRenderer(const CameraVideoFrameRenderer&) = delete;
  38. CameraVideoFrameRenderer& operator=(const CameraVideoFrameRenderer&) = delete;
  39. ~CameraVideoFrameRenderer() override;
  40. aura::Window* host_window() { return &host_window_; }
  41. bool should_flip_frames_horizontally() const {
  42. return should_flip_frames_horizontally_;
  43. }
  44. // Initializes this renderer by creating the `layer_tree_frame_sink_` and
  45. // starts receiving video frames from the camera device.
  46. void Initialize();
  47. // CameraVideoFrameHandler::Delegate:
  48. void OnCameraVideoFrame(scoped_refptr<media::VideoFrame> frame) override;
  49. // viz::BeginFrameObserverBase:
  50. void OnBeginFrameSourcePausedChanged(bool paused) override;
  51. bool OnBeginFrameDerivedImpl(const viz::BeginFrameArgs& args) override;
  52. // cc::LayerTreeFrameSinkClient:
  53. void SetBeginFrameSource(viz::BeginFrameSource* source) override;
  54. absl::optional<viz::HitTestRegionList> BuildHitTestData() override;
  55. void ReclaimResources(std::vector<viz::ReturnedResource> resources) override;
  56. void SetTreeActivationCallback(base::RepeatingClosure callback) override;
  57. void DidReceiveCompositorFrameAck() override;
  58. void DidPresentCompositorFrame(
  59. uint32_t frame_token,
  60. const viz::FrameTimingDetails& details) override;
  61. void DidLoseLayerTreeFrameSink() override;
  62. void OnDraw(const gfx::Transform& transform,
  63. const gfx::Rect& viewport,
  64. bool resourceless_software_draw,
  65. bool skip_draw) override;
  66. void SetMemoryPolicy(const cc::ManagedMemoryPolicy& policy) override;
  67. void SetExternalTilePriorityConstraints(
  68. const gfx::Rect& viewport_rect,
  69. const gfx::Transform& transform) override;
  70. private:
  71. friend class CaptureModeTestApi;
  72. // Creates and returns a compositor frame for the given `video_frame`.
  73. viz::CompositorFrame CreateCompositorFrame(
  74. const viz::BeginFrameAck& begin_frame_ack,
  75. scoped_refptr<media::VideoFrame> video_frame);
  76. // The window hosting the rendered video frames.
  77. aura::Window host_window_;
  78. // The handler that subscribes to the camera video device.
  79. CameraVideoFrameHandler video_frame_handler_;
  80. // Used to identify gpu or software resources obtained from a video frame in
  81. // order for these resources to be given to the Viz display compositor.
  82. viz::ClientResourceProvider client_resource_provider_;
  83. // Generates a frame token for the next compositor frame we create.
  84. viz::FrameTokenGenerator compositor_frame_token_generator_;
  85. // If not empty, the video frame that will be rendered next when
  86. // `OnBeginFrameDerivedImpl()` is called.
  87. scoped_refptr<media::VideoFrame> current_video_frame_;
  88. // The pixel size and the DSF of the most recent submitted compositor frame.
  89. // If either changes, we'll need to allocate a new local surface ID.
  90. gfx::Size last_compositor_frame_size_pixels_;
  91. float last_compositor_frame_dsf_ = 1.0f;
  92. scoped_refptr<viz::ContextProvider> context_provider_;
  93. scoped_refptr<viz::RasterContextProvider> raster_context_provider_;
  94. // The layer tree frame sink created from `host_window_`, which is used to
  95. // submit compositor frames for the camera video frames.
  96. std::unique_ptr<cc::LayerTreeFrameSink> layer_tree_frame_sink_;
  97. // Used to produce the camera video frame's content as resources consumable by
  98. // the Viz display compositor.
  99. std::unique_ptr<media::VideoResourceUpdater> video_resource_updater_;
  100. // The currently observed `BeginFrameSource` which will notify us with
  101. // `OnBeginFrameDerivedImpl()`.
  102. viz::BeginFrameSource* begin_frame_source_ = nullptr;
  103. // A callback used for tests to be called after `frame` has been rendered.
  104. base::OnceCallback<void(scoped_refptr<media::VideoFrame> frame)>
  105. on_video_frame_rendered_for_test_;
  106. // True if we submitted a compositor frame and are waiting for a call to
  107. // `DidReceiveCompositorFrameAck()`.
  108. bool pending_compositor_frame_ack_ = false;
  109. // Whether the camera video frames should be flipped horizontally around the Y
  110. // axis so that the camera behaves like a mirror. This can be false for world-
  111. // facing cameras.
  112. bool should_flip_frames_horizontally_ = true;
  113. };
  114. } // namespace ash
  115. #endif // ASH_CAPTURE_MODE_CAMERA_VIDEO_FRAME_RENDERER_H_