image_processor_with_pool.h 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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_IMAGE_PROCESSOR_WITH_POOL_H_
  5. #define MEDIA_GPU_CHROMEOS_IMAGE_PROCESSOR_WITH_POOL_H_
  6. #include <memory>
  7. #include "base/containers/queue.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/sequence_checker.h"
  11. #include "base/task/sequenced_task_runner.h"
  12. #include "media/base/video_frame.h"
  13. #include "media/gpu/chromeos/dmabuf_video_frame_pool.h"
  14. #include "media/gpu/chromeos/image_processor.h"
  15. #include "media/gpu/media_gpu_export.h"
  16. namespace media {
  17. // A simple client implementation of ImageProcessor.
  18. // By giving DmabufVideoFramePool, which provides VideoFrame for output, the
  19. // caller can process input frames without managing VideoFrame for output.
  20. class ImageProcessorWithPool {
  21. public:
  22. using FrameReadyCB = ImageProcessor::FrameReadyCB;
  23. // Initializes |frame_pool| and creates an ImageProcessorWithPool instance.
  24. // |num_frames| is the number of frames requested from |frame_pool|.
  25. // Returns a valid ImageProcessorWithPool instance if successful
  26. // otherwise returns any given error from the set of CroStatus::Codes.
  27. static CroStatus::Or<std::unique_ptr<ImageProcessorWithPool>> Create(
  28. std::unique_ptr<ImageProcessor> image_processor,
  29. DmabufVideoFramePool* const frame_pool,
  30. size_t num_frames,
  31. bool use_protected,
  32. const scoped_refptr<base::SequencedTaskRunner> task_runner);
  33. ~ImageProcessorWithPool();
  34. // Processes |frame| by |image_processor_|. The processed output frame will be
  35. // returned by |ready_cb|, which is called on |task_runner_|.
  36. void Process(scoped_refptr<VideoFrame> frame, FrameReadyCB ready_cb);
  37. // Returns true if there are pending frames.
  38. bool HasPendingFrames() const;
  39. // Abandons all pending process. After called, no previous frame will be
  40. // returned by |ready_cb|.
  41. void Reset();
  42. // Returns true if the image processor supports buffers allocated
  43. // incoherently. The MTK MDP3 image processor has coherency issues, but the
  44. // Libyuv image processor benefits greatly from incoherent allocations.
  45. bool SupportsIncoherentBufs() const {
  46. return image_processor_ && image_processor_->SupportsIncoherentBufs();
  47. }
  48. private:
  49. friend class VideoDecoderPipelineTest;
  50. ImageProcessorWithPool(
  51. std::unique_ptr<ImageProcessor> image_processor,
  52. DmabufVideoFramePool* const frame_pool,
  53. const scoped_refptr<base::SequencedTaskRunner> task_runner);
  54. // Tries to pass pending frames to |image_processor_|.
  55. void PumpProcessFrames();
  56. // Called when |image_processor_| finishes processing frames.
  57. void OnFrameProcessed(FrameReadyCB ready_cb, scoped_refptr<VideoFrame> frame);
  58. // The image processor for processing frames.
  59. std::unique_ptr<ImageProcessor> image_processor_;
  60. // The frame pool to allocate output frames of the image processor.
  61. // The caller should guarantee the pool alive during the lifetime of this
  62. // ImageProcessorWithPool instance.
  63. const raw_ptr<DmabufVideoFramePool> frame_pool_;
  64. // The pending input frames that wait for passing to |image_processor_|.
  65. base::queue<std::pair<scoped_refptr<VideoFrame>, FrameReadyCB>>
  66. pending_frames_;
  67. // Number of frames that are processed in |image_processor_|.
  68. size_t num_frames_in_ip_ = 0;
  69. // The main task runner and its checker. All methods should be called on
  70. // |task_runner_|.
  71. const scoped_refptr<base::SequencedTaskRunner> task_runner_;
  72. SEQUENCE_CHECKER(sequence_checker_);
  73. // WeakPtr of this instance and its factory, bound to |task_runner_|.
  74. base::WeakPtr<ImageProcessorWithPool> weak_this_;
  75. base::WeakPtrFactory<ImageProcessorWithPool> weak_this_factory_{this};
  76. };
  77. } // namespace media
  78. #endif // MEDIA_GPU_CHROMEOS_IMAGE_PROCESSOR_WITH_POOL_H_