video_frame_yuv_converter.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2020 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_RENDERERS_VIDEO_FRAME_YUV_CONVERTER_H_
  5. #define MEDIA_RENDERERS_VIDEO_FRAME_YUV_CONVERTER_H_
  6. #include <array>
  7. #include "gpu/GLES2/gl2extchromium.h"
  8. #include "gpu/command_buffer/common/mailbox_holder.h"
  9. #include "media/base/media_export.h"
  10. #include "ui/gfx/color_space.h"
  11. #include "ui/gfx/geometry/size.h"
  12. namespace viz {
  13. class RasterContextProvider;
  14. } // namespace viz
  15. namespace media {
  16. class VideoFrame;
  17. class VideoFrameYUVMailboxesHolder;
  18. // Converts YUV video frames to RGB format and stores the results in the
  19. // provided mailbox. The caller of functions in this class maintains ownership
  20. // of the destination mailbox. VideoFrames that wrap external textures can be
  21. // I420 or NV12 format. Automatically handles upload of CPU memory backed
  22. // VideoFrames in I420 format. Converting CPU backed VideoFrames requires
  23. // creation of shared images to upload the frame to the GPU where the conversion
  24. // takes place.
  25. // IMPORTANT: Callers of this function can cache this class and call
  26. // ConvertYUVVideoFrame() to prevent repeated creation/deletion of shared
  27. // images.
  28. class MEDIA_EXPORT VideoFrameYUVConverter {
  29. public:
  30. // These parameters are only supported by ConvertYUVVideoFrame et al when the
  31. // specified RasterContextProvider also has a GrContext (equivalently, when
  32. // OOP-R is disabled). Isolate them in their own structure, so they can
  33. // eventually be removed once OOP-R is universal.
  34. struct GrParams {
  35. unsigned int internal_format = GL_RGBA;
  36. unsigned int type = GL_UNSIGNED_BYTE;
  37. bool flip_y = false;
  38. bool use_visible_rect = false;
  39. };
  40. VideoFrameYUVConverter();
  41. ~VideoFrameYUVConverter();
  42. static bool IsVideoFrameFormatSupported(const VideoFrame& video_frame);
  43. static bool ConvertYUVVideoFrameNoCaching(
  44. const VideoFrame* video_frame,
  45. viz::RasterContextProvider* raster_context_provider,
  46. const gpu::MailboxHolder& dest_mailbox_holder,
  47. absl::optional<GrParams> gr_params = absl::nullopt);
  48. bool ConvertYUVVideoFrame(const VideoFrame* video_frame,
  49. viz::RasterContextProvider* raster_context_provider,
  50. const gpu::MailboxHolder& dest_mailbox_holder,
  51. absl::optional<GrParams> gr_params = absl::nullopt);
  52. void ReleaseCachedData();
  53. private:
  54. bool ConvertFromVideoFrameYUVWithGrContext(
  55. const VideoFrame* video_frame,
  56. viz::RasterContextProvider* raster_context_provider,
  57. const gpu::MailboxHolder& dest_mailbox_holder,
  58. const GrParams& gr_params);
  59. std::unique_ptr<VideoFrameYUVMailboxesHolder> holder_;
  60. };
  61. } // namespace media
  62. #endif // MEDIA_RENDERERS_VIDEO_FRAME_YUV_CONVERTER_H_