dmabuf_video_frame_pool.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 MEDIA_GPU_CHROMEOS_DMABUF_VIDEO_FRAME_POOL_H_
  5. #define MEDIA_GPU_CHROMEOS_DMABUF_VIDEO_FRAME_POOL_H_
  6. #include "base/memory/scoped_refptr.h"
  7. #include "base/task/sequenced_task_runner.h"
  8. #include "base/time/time.h"
  9. #include "media/base/status.h"
  10. #include "media/base/video_frame.h"
  11. #include "media/gpu/chromeos/chromeos_status.h"
  12. #include "media/gpu/chromeos/fourcc.h"
  13. #include "media/gpu/chromeos/gpu_buffer_layout.h"
  14. #include "media/gpu/media_gpu_export.h"
  15. #include "third_party/abseil-cpp/absl/types/optional.h"
  16. #include "ui/gfx/geometry/rect.h"
  17. #include "ui/gfx/geometry/size.h"
  18. namespace media {
  19. // Forward declare for use in AsPlatformVideoFramePool.
  20. class PlatformVideoFramePool;
  21. // Interface for allocating and managing DMA-buf VideoFrame. The client should
  22. // set a task runner first, and guarantee both GetFrame() and the destructor are
  23. // executed on this task runner.
  24. // Note: other public methods might be called at different thread. The
  25. // implementation must be thread-safe.
  26. class MEDIA_GPU_EXPORT DmabufVideoFramePool {
  27. public:
  28. using DmabufId = const std::vector<base::ScopedFD>*;
  29. using CreateFrameCB =
  30. base::RepeatingCallback<CroStatus::Or<scoped_refptr<VideoFrame>>(
  31. VideoPixelFormat,
  32. const gfx::Size&,
  33. const gfx::Rect&,
  34. const gfx::Size&,
  35. bool,
  36. bool,
  37. base::TimeDelta)>;
  38. // Get the identifier of Dmabuf-backed |frame|. Calling this method with the
  39. // frames backed by the same Dmabuf should return the same result.
  40. static DmabufId GetDmabufId(const VideoFrame& frame);
  41. DmabufVideoFramePool();
  42. virtual ~DmabufVideoFramePool();
  43. // Setter method of |parent_task_runner_|. GetFrame() and destructor method
  44. // should be called at |parent_task_runner_|.
  45. // This method must be called only once before any GetFrame() is called.
  46. virtual void set_parent_task_runner(
  47. scoped_refptr<base::SequencedTaskRunner> parent_task_runner);
  48. // Allows downcasting to an implementation of DmabufVideoFramePool safely
  49. // since it has custom behavior that VaapiVideoDecoder needs to take
  50. // advantage of.
  51. virtual PlatformVideoFramePool* AsPlatformVideoFramePool();
  52. // Sets the parameters of allocating frames and the maximum number of frames
  53. // which can be allocated.
  54. // Returns a valid GpuBufferLayout if the initialization is successful,
  55. // otherwise returns any given error from the set of CroStatus::Codes.
  56. virtual CroStatus::Or<GpuBufferLayout> Initialize(
  57. const Fourcc& fourcc,
  58. const gfx::Size& coded_size,
  59. const gfx::Rect& visible_rect,
  60. const gfx::Size& natural_size,
  61. size_t max_num_frames,
  62. bool use_protected,
  63. bool use_linear_buffers = false) = 0;
  64. // Returns a frame from the pool with the layout that is returned by the
  65. // previous Initialize() method and zero timestamp. Returns nullptr if the
  66. // pool is exhausted.
  67. virtual scoped_refptr<VideoFrame> GetFrame() = 0;
  68. // Checks whether the pool is exhausted. This happens when the pool reached
  69. // its maximum size and all frames are in use. Calling GetFrame() when the
  70. // pool is exhausted will return a nullptr.
  71. virtual bool IsExhausted() = 0;
  72. // Set the callback for notifying when the pool is no longer exhausted. The
  73. // callback will be called on |parent_task_runner_|. Note: if there is a
  74. // pending callback when calling NotifyWhenFrameAvailable(), the old callback
  75. // would be dropped immediately.
  76. virtual void NotifyWhenFrameAvailable(base::OnceClosure cb) = 0;
  77. // Invoke to cause the pool to release all the frames it has allocated before
  78. // which will cause new ones to be allocated. This method must be called on
  79. // |parent_task_runner_| because it may invalidate weak ptrs.
  80. virtual void ReleaseAllFrames() = 0;
  81. // Returns true if and only if the pool is a mock pool used for testing.
  82. virtual bool IsFakeVideoFramePool();
  83. protected:
  84. scoped_refptr<base::SequencedTaskRunner> parent_task_runner_;
  85. };
  86. } // namespace media
  87. #endif // MEDIA_GPU_CHROMEOS_DMABUF_VIDEO_FRAME_POOL_H_