vaapi_mjpeg_decode_accelerator.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2015 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_VAAPI_VAAPI_MJPEG_DECODE_ACCELERATOR_H_
  5. #define MEDIA_GPU_VAAPI_VAAPI_MJPEG_DECODE_ACCELERATOR_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include "base/containers/span.h"
  9. #include "base/memory/scoped_refptr.h"
  10. #include "base/memory/shared_memory_mapping.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/threading/thread.h"
  13. #include "components/chromeos_camera/mjpeg_decode_accelerator.h"
  14. #include "media/gpu/chromeos/image_processor_backend.h"
  15. #include "media/gpu/media_gpu_export.h"
  16. #include "media/gpu/vaapi/vaapi_jpeg_decoder.h"
  17. namespace base {
  18. class SingleThreadTaskRunner;
  19. }
  20. namespace media {
  21. class BitstreamBuffer;
  22. class ScopedVAImage;
  23. class VaapiWrapper;
  24. class VideoFrame;
  25. // Class to provide MJPEG decode acceleration for Intel systems with hardware
  26. // support for it, and on which libva is available.
  27. // Decoding tasks are performed on a separate |decoder_thread_|.
  28. //
  29. // Threading/life-cycle: this object is created & destroyed on the GPU
  30. // ChildThread. A few methods on it are called on the decoder thread which is
  31. // stopped during |this->Destroy()|, so any tasks posted to the decoder thread
  32. // can assume |*this| is still alive. See |weak_this_| below for more details.
  33. class MEDIA_GPU_EXPORT VaapiMjpegDecodeAccelerator
  34. : public chromeos_camera::MjpegDecodeAccelerator {
  35. public:
  36. VaapiMjpegDecodeAccelerator(
  37. const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner);
  38. VaapiMjpegDecodeAccelerator(const VaapiMjpegDecodeAccelerator&) = delete;
  39. VaapiMjpegDecodeAccelerator& operator=(const VaapiMjpegDecodeAccelerator&) =
  40. delete;
  41. ~VaapiMjpegDecodeAccelerator() override;
  42. // chromeos_camera::MjpegDecodeAccelerator implementation.
  43. void InitializeAsync(
  44. chromeos_camera::MjpegDecodeAccelerator::Client* client,
  45. chromeos_camera::MjpegDecodeAccelerator::InitCB init_cb) override;
  46. void Decode(BitstreamBuffer bitstream_buffer,
  47. scoped_refptr<VideoFrame> video_frame) override;
  48. void Decode(int32_t task_id,
  49. base::ScopedFD src_dmabuf_fd,
  50. size_t src_size,
  51. off_t src_offset,
  52. scoped_refptr<VideoFrame> dst_frame) override;
  53. bool IsSupported() override;
  54. private:
  55. // Notifies the client that an error has occurred and decoding cannot
  56. // continue. The client is notified on the |task_runner_|, i.e., the thread in
  57. // which |*this| was created.
  58. void NotifyError(int32_t task_id, Error error);
  59. // Notifies the client that a decode is ready. The client is notified on the
  60. // |task_runner_|, i.e., the thread in which |*this| was created.
  61. void VideoFrameReady(int32_t task_id);
  62. // Processes one decode request.
  63. void DecodeFromShmTask(int32_t task_id,
  64. base::WritableSharedMemoryMapping mapping,
  65. scoped_refptr<VideoFrame> dst_frame);
  66. void DecodeFromDmaBufTask(int32_t task_id,
  67. base::ScopedFD src_dmabuf_fd,
  68. size_t src_size,
  69. off_t src_offset,
  70. scoped_refptr<VideoFrame> dst_frame);
  71. // Decodes the JPEG in |src_image| into |dst_frame| and notifies the client
  72. // when finished or when an error occurs.
  73. void DecodeImpl(int32_t task_id,
  74. base::span<const uint8_t> src_image,
  75. scoped_refptr<VideoFrame> dst_frame);
  76. // Creates |image_processor_| for converting |src_frame| into |dst_frame|.
  77. void CreateImageProcessor(const VideoFrame* src_frame,
  78. const VideoFrame* dst_frame);
  79. // Puts contents of |surface| within |crop_rect| into given |video_frame|
  80. // using VA-API Video Processing Pipeline (VPP), and passes the |task_id| of
  81. // the resulting picture to client for output.
  82. bool OutputPictureVppOnTaskRunner(int32_t task_id,
  83. const ScopedVASurface* surface,
  84. scoped_refptr<VideoFrame> video_frame,
  85. const gfx::Rect& crop_rect);
  86. // Puts contents of |image| within |crop_rect| into the given |video_frame|
  87. // using libyuv, and passes the |task_id| of the resulting picture to client
  88. // for output.
  89. bool OutputPictureLibYuvOnTaskRunner(int32_t task_id,
  90. std::unique_ptr<ScopedVAImage> image,
  91. scoped_refptr<VideoFrame> video_frame,
  92. const gfx::Rect& crop_rect);
  93. void OnImageProcessorError();
  94. void InitializeOnDecoderTaskRunner(InitCB init_cb);
  95. void InitializeOnTaskRunner(
  96. chromeos_camera::MjpegDecodeAccelerator::Client* client,
  97. InitCB init_cb);
  98. void CleanUpOnDecoderThread();
  99. // ChildThread's task runner.
  100. const scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  101. // GPU IO task runner.
  102. const scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
  103. // The client of this class.
  104. chromeos_camera::MjpegDecodeAccelerator::Client* client_;
  105. std::unique_ptr<media::VaapiJpegDecoder> decoder_;
  106. // VaapiWrapper for VPP context. This is used to convert decoded data into
  107. // client buffer.
  108. scoped_refptr<VaapiWrapper> vpp_vaapi_wrapper_;
  109. // Image processor to convert the decoded frame into client buffer when VA-API
  110. // is not capable.
  111. std::unique_ptr<ImageProcessorBackend> image_processor_;
  112. base::Thread decoder_thread_;
  113. // Use this to post tasks to |decoder_thread_| instead of
  114. // |decoder_thread_.task_runner()| because the latter will be NULL once
  115. // |decoder_thread_.Stop()| returns.
  116. scoped_refptr<base::SingleThreadTaskRunner> decoder_task_runner_;
  117. // WeakPtr factory for use in posting tasks from |decoder_task_runner_| back
  118. // to |task_runner_|. Since |decoder_thread_| is a fully owned member of
  119. // this class, tasks posted to it may use base::Unretained(this), and tasks
  120. // posted from the |decoder_task_runner_| to |task_runner_| should use a
  121. // WeakPtr (obtained via weak_this_factory_.GetWeakPtr()).
  122. base::WeakPtrFactory<VaapiMjpegDecodeAccelerator> weak_this_factory_;
  123. };
  124. } // namespace media
  125. #endif // MEDIA_GPU_VAAPI_VAAPI_MJPEG_DECODE_ACCELERATOR_H_