dual_buffer_frame_consumer.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2016 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 REMOTING_CLIENT_DUAL_BUFFER_FRAME_CONSUMER_H_
  5. #define REMOTING_CLIENT_DUAL_BUFFER_FRAME_CONSUMER_H_
  6. #include "base/callback.h"
  7. #include "base/memory/ref_counted.h"
  8. #include "base/memory/weak_ptr.h"
  9. #include "base/task/single_thread_task_runner.h"
  10. #include "base/threading/thread_checker.h"
  11. #include "remoting/protocol/frame_consumer.h"
  12. #include "third_party/webrtc/modules/desktop_capture/desktop_region.h"
  13. #include "third_party/webrtc/modules/desktop_capture/shared_desktop_frame.h"
  14. namespace remoting {
  15. // This class continuously uses two BasicDesktopFrame as buffer for decoding
  16. // updated regions until the resolution is changed.
  17. // This class should be used and destroyed on the same thread. If |task_runner|
  18. // is null |callback| will be run directly upon the stack of DrawFrame,
  19. // otherwise a task will be posted to feed the callback on the thread of
  20. // |task_runner|.
  21. // Only areas bound by updated_region() on the buffer are considered valid to
  22. // |callback|. Please use RequestFullDesktopFrame() if you want to get a full
  23. // desktop frame.
  24. class DualBufferFrameConsumer : public protocol::FrameConsumer {
  25. public:
  26. // RenderCallback(decoded_frame, done)
  27. // |done| should be run after it is rendered. Can be called on any thread.
  28. using RenderCallback =
  29. base::RepeatingCallback<void(std::unique_ptr<webrtc::DesktopFrame>,
  30. base::OnceClosure)>;
  31. DualBufferFrameConsumer(
  32. RenderCallback callback,
  33. scoped_refptr<base::SingleThreadTaskRunner> task_runner,
  34. PixelFormat format);
  35. DualBufferFrameConsumer(const DualBufferFrameConsumer&) = delete;
  36. DualBufferFrameConsumer& operator=(const DualBufferFrameConsumer&) = delete;
  37. ~DualBufferFrameConsumer() override;
  38. // Feeds the callback on the right thread with a BasicDesktopFrame that merges
  39. // updates from buffer_[0] and buffer_[1]. Do nothing if no updates have
  40. // received yet.
  41. void RequestFullDesktopFrame();
  42. // FrameConsumer interface.
  43. std::unique_ptr<webrtc::DesktopFrame> AllocateFrame(
  44. const webrtc::DesktopSize& size) override;
  45. void DrawFrame(std::unique_ptr<webrtc::DesktopFrame> frame,
  46. base::OnceClosure done) override;
  47. PixelFormat GetPixelFormat() override;
  48. base::WeakPtr<DualBufferFrameConsumer> GetWeakPtr();
  49. private:
  50. void RunRenderCallback(std::unique_ptr<webrtc::DesktopFrame> frame,
  51. base::OnceClosure done);
  52. std::unique_ptr<webrtc::SharedDesktopFrame> buffers_[2];
  53. // Represents dirty regions that are currently in buffers_[1]. Will be used
  54. // when calling RequestFullDesktopFrame() to construct the full desktop frame.
  55. webrtc::DesktopRegion buffer_1_mask_;
  56. int current_buffer_ = 0;
  57. RenderCallback callback_;
  58. scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  59. PixelFormat pixel_format_;
  60. base::ThreadChecker thread_checker_;
  61. base::WeakPtr<DualBufferFrameConsumer> weak_ptr_;
  62. base::WeakPtrFactory<DualBufferFrameConsumer> weak_factory_{this};
  63. };
  64. } // namespace remoting
  65. #endif // REMOTING_CLIENT_DUAL_BUFFER_FRAME_CONSUMER_H_