Upload upstream chromium 120.0.6099.5
[platform/framework/web/chromium-efl.git] / media / renderers / video_frame_yuv_converter.h
1 // Copyright 2020 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef MEDIA_RENDERERS_VIDEO_FRAME_YUV_CONVERTER_H_
6 #define MEDIA_RENDERERS_VIDEO_FRAME_YUV_CONVERTER_H_
7
8 #include <array>
9
10 #include "gpu/GLES2/gl2extchromium.h"
11 #include "gpu/command_buffer/common/mailbox_holder.h"
12 #include "media/base/media_export.h"
13 #include "ui/gfx/color_space.h"
14 #include "ui/gfx/geometry/size.h"
15
16 namespace viz {
17 class RasterContextProvider;
18 }  // namespace viz
19
20 namespace media {
21
22 class VideoFrame;
23 class VideoFrameYUVMailboxesHolder;
24
25 // Converts YUV video frames to RGB format and stores the results in the
26 // provided mailbox. The caller of functions in this class maintains ownership
27 // of the destination mailbox. VideoFrames that wrap external textures can be
28 // I420 or NV12 format. Automatically handles upload of CPU memory backed
29 // VideoFrames in I420 format. Converting CPU backed VideoFrames requires
30 // creation of shared images to upload the frame to the GPU where the conversion
31 // takes place. This will not perform any color space conversion besides the
32 // YUV to RGB conversion (it will ignore the color space of the SharedImage
33 // backing the destination mailbox).
34 // IMPORTANT: Callers of this function can cache this class and call
35 // ConvertYUVVideoFrame() to prevent repeated creation/deletion of shared
36 // images.
37 class MEDIA_EXPORT VideoFrameYUVConverter {
38  public:
39   // These parameters are only supported by ConvertYUVVideoFrame et al when the
40   // specified RasterContextProvider also has a GrContext (equivalently, when
41   // OOP-R is disabled). Isolate them in their own structure, so they can
42   // eventually be removed once OOP-R is universal.
43   struct GrParams {
44     unsigned int internal_format = GL_RGBA;
45     unsigned int type = GL_UNSIGNED_BYTE;
46     bool flip_y = false;
47     bool use_visible_rect = false;
48   };
49
50   VideoFrameYUVConverter();
51   ~VideoFrameYUVConverter();
52   static bool IsVideoFrameFormatSupported(const VideoFrame& video_frame);
53
54   static bool ConvertYUVVideoFrameNoCaching(
55       const VideoFrame* video_frame,
56       viz::RasterContextProvider* raster_context_provider,
57       const gpu::MailboxHolder& dest_mailbox_holder,
58       absl::optional<GrParams> gr_params = absl::nullopt);
59   bool ConvertYUVVideoFrame(const VideoFrame* video_frame,
60                             viz::RasterContextProvider* raster_context_provider,
61                             const gpu::MailboxHolder& dest_mailbox_holder,
62                             absl::optional<GrParams> gr_params = absl::nullopt);
63   void ReleaseCachedData();
64
65  private:
66   bool ConvertFromVideoFrameYUVWithGrContext(
67       const VideoFrame* video_frame,
68       viz::RasterContextProvider* raster_context_provider,
69       const gpu::MailboxHolder& dest_mailbox_holder,
70       const GrParams& gr_params);
71
72   std::unique_ptr<VideoFrameYUVMailboxesHolder> holder_;
73 };
74 }  // namespace media
75
76 #endif  // MEDIA_RENDERERS_VIDEO_FRAME_YUV_CONVERTER_H_