video_frame_yuv_mailboxes_holder.h 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2021 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_MAILBOXES_HOLDER_H_
  5. #define MEDIA_RENDERERS_VIDEO_FRAME_YUV_MAILBOXES_HOLDER_H_
  6. #include "media/base/media_export.h"
  7. #include "media/base/video_frame.h"
  8. #include "third_party/skia/include/core/SkYUVAInfo.h"
  9. #include "third_party/skia/include/gpu/GrYUVABackendTextures.h"
  10. #include "third_party/skia/include/gpu/gl/GrGLTypes.h"
  11. namespace viz {
  12. class RasterContextProvider;
  13. } // namespace viz
  14. namespace media {
  15. class MEDIA_EXPORT VideoFrameYUVMailboxesHolder {
  16. public:
  17. VideoFrameYUVMailboxesHolder();
  18. ~VideoFrameYUVMailboxesHolder();
  19. void ReleaseCachedData();
  20. void ReleaseTextures();
  21. // Extracts shared image information if |video_frame| is texture backed or
  22. // creates new shared images and uploads YUV data to GPU if |video_frame| is
  23. // mappable. This function can be called repeatedly to re-use shared images in
  24. // the case of CPU backed VideoFrames. The planes are returned in |mailboxes|.
  25. void VideoFrameToMailboxes(
  26. const VideoFrame* video_frame,
  27. viz::RasterContextProvider* raster_context_provider,
  28. gpu::Mailbox mailboxes[SkYUVAInfo::kMaxPlanes]);
  29. // Returns a YUV SkImage for the specified video frame.
  30. sk_sp<SkImage> VideoFrameToSkImage(
  31. const VideoFrame* video_frame,
  32. viz::RasterContextProvider* raster_context_provider);
  33. // Creates SkSurfaces for each plane for the specified video frame. Returns
  34. // true only if surfaces for all planes were created.
  35. bool VideoFrameToPlaneSkSurfaces(
  36. const VideoFrame* video_frame,
  37. viz::RasterContextProvider* raster_context_provider,
  38. sk_sp<SkSurface> surfaces[SkYUVAInfo::kMaxPlanes]);
  39. const SkYUVAInfo& yuva_info() const { return yuva_info_; }
  40. // Utility to convert a media pixel format to SkYUVAInfo.
  41. static std::tuple<SkYUVAInfo::PlaneConfig, SkYUVAInfo::Subsampling>
  42. VideoPixelFormatToSkiaValues(VideoPixelFormat video_format);
  43. // Utility to populate a SkYUVAInfo from a video frame.
  44. static SkYUVAInfo VideoFrameGetSkYUVAInfo(const VideoFrame* video_frame);
  45. private:
  46. static constexpr size_t kMaxPlanes =
  47. static_cast<size_t>(SkYUVAInfo::kMaxPlanes);
  48. // Like VideoFrameToMailboxes but imports the textures from the mailboxes and
  49. // returns the planes as a set of YUVA GrBackendTextures. If |for_surface| is
  50. // true, then select color types and pixel formats that are renderable as
  51. // SkSurfaces.
  52. GrYUVABackendTextures VideoFrameToSkiaTextures(
  53. const VideoFrame* video_frame,
  54. viz::RasterContextProvider* raster_context_provider,
  55. bool for_surface);
  56. void ImportTextures(bool for_surface);
  57. scoped_refptr<viz::RasterContextProvider> provider_;
  58. bool imported_textures_ = false;
  59. bool created_shared_images_ = false;
  60. gfx::Size cached_video_size_;
  61. gfx::ColorSpace cached_video_color_space_;
  62. // The properties of the most recently received video frame.
  63. size_t num_planes_ = 0;
  64. SkYUVAInfo yuva_info_;
  65. SkISize plane_sizes_[SkYUVAInfo::kMaxPlanes];
  66. // Populated by VideoFrameToMailboxes.
  67. std::array<gpu::MailboxHolder, kMaxPlanes> holders_;
  68. // Populated by ImportTextures.
  69. struct YUVPlaneTextureInfo {
  70. GrGLTextureInfo texture = {0, 0};
  71. bool is_shared_image = false;
  72. };
  73. std::array<YUVPlaneTextureInfo, kMaxPlanes> textures_;
  74. };
  75. } // namespace media
  76. #endif // MEDIA_RENDERERS_VIDEO_FRAME_YUV_MAILBOXES_HOLDER_H_