| Flutter Impeller |

yuv_to_rgb_filter_contents.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter 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
5 #include "impeller/entity/contents/filters/yuv_to_rgb_filter_contents.h"
6
7 #include "impeller/core/formats.h"
8 #include "impeller/entity/contents/anonymous_contents.h"
9 #include "impeller/entity/contents/content_context.h"
10 #include "impeller/geometry/matrix.h"
11 #include "impeller/renderer/render_pass.h"
12 #include "impeller/renderer/vertex_buffer_builder.h"
13
14 namespace impeller {
15
16 // clang-format off
17 constexpr Matrix kMatrixBT601LimitedRange = {
18 1.164, 1.164, 1.164, 0.0,
19 0.0, -0.392, 2.017, 0.0,
20 1.596, -0.813, 0.0, 0.0,
21 0.0, 0.0, 0.0, 1.0
22 };
23
24 constexpr Matrix kMatrixBT601FullRange = {
25 1.0, 1.0, 1.0, 0.0,
26 0.0, -0.344, 1.772, 0.0,
27 1.402, -0.714, 0.0, 0.0,
28 0.0, 0.0, 0.0, 1.0
29 };
30 // clang-format on
31
32 YUVToRGBFilterContents::YUVToRGBFilterContents() = default;
33
34 YUVToRGBFilterContents::~YUVToRGBFilterContents() = default;
35
36 void YUVToRGBFilterContents::SetYUVColorSpace(YUVColorSpace yuv_color_space) {
37 yuv_color_space_ = yuv_color_space;
38 }
39
40 std::optional<Entity> YUVToRGBFilterContents::RenderFilter(
41 const FilterInput::Vector& inputs,
42 const ContentContext& renderer,
43 const Entity& entity,
44 const Matrix& effect_transform,
45 const Rect& coverage,
46 const std::optional<Rect>& coverage_hint) const {
47 if (inputs.size() < 2) {
48 return std::nullopt;
49 }
50
51 using VS = YUVToRGBFilterPipeline::VertexShader;
52 using FS = YUVToRGBFilterPipeline::FragmentShader;
53
54 auto y_input_snapshot =
55 inputs[0]->GetSnapshot("YUVToRGB(Y)", renderer, entity);
56 auto uv_input_snapshot =
57 inputs[1]->GetSnapshot("YUVToRGB(UV)", renderer, entity);
58 if (!y_input_snapshot.has_value() || !uv_input_snapshot.has_value()) {
59 return std::nullopt;
60 }
61
62 if (y_input_snapshot->texture->GetTextureDescriptor().format !=
63 PixelFormat::kR8UNormInt ||
64 uv_input_snapshot->texture->GetTextureDescriptor().format !=
65 PixelFormat::kR8G8UNormInt) {
66 return std::nullopt;
67 }
68
69 //----------------------------------------------------------------------------
70 /// Create AnonymousContents for rendering.
71 ///
72 RenderProc render_proc = [y_input_snapshot, uv_input_snapshot,
73 yuv_color_space = yuv_color_space_](
74 const ContentContext& renderer,
75 const Entity& entity, RenderPass& pass) -> bool {
76 Command cmd;
77 DEBUG_COMMAND_INFO(cmd, "YUV to RGB Filter");
78 cmd.stencil_reference = entity.GetClipDepth();
79
80 auto options = OptionsFromPassAndEntity(pass, entity);
81 options.primitive_type = PrimitiveType::kTriangleStrip;
82 cmd.pipeline = renderer.GetYUVToRGBFilterPipeline(options);
83
84 auto size = y_input_snapshot->texture->GetSize();
85
86 VertexBufferBuilder<VS::PerVertexData> vtx_builder;
87 vtx_builder.AddVertices({
88 {Point(0, 0)},
89 {Point(1, 0)},
90 {Point(0, 1)},
91 {Point(1, 1)},
92 });
93
94 auto& host_buffer = pass.GetTransientsBuffer();
95 cmd.BindVertices(vtx_builder.CreateVertexBuffer(host_buffer));
96
97 VS::FrameInfo frame_info;
98 frame_info.mvp = pass.GetOrthographicTransform() * entity.GetTransform() *
99 y_input_snapshot->transform *
100 Matrix::MakeScale(Vector2(size));
101 frame_info.texture_sampler_y_coord_scale =
102 y_input_snapshot->texture->GetYCoordScale();
103
104 FS::FragInfo frag_info;
105 frag_info.yuv_color_space = static_cast<Scalar>(yuv_color_space);
106 switch (yuv_color_space) {
107 case YUVColorSpace::kBT601LimitedRange:
108 frag_info.matrix = kMatrixBT601LimitedRange;
109 break;
110 case YUVColorSpace::kBT601FullRange:
111 frag_info.matrix = kMatrixBT601FullRange;
112 break;
113 }
114
115 auto sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler({});
116 FS::BindYTexture(cmd, y_input_snapshot->texture, sampler);
117 FS::BindUvTexture(cmd, uv_input_snapshot->texture, sampler);
118
119 FS::BindFragInfo(cmd, host_buffer.EmplaceUniform(frag_info));
120 VS::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info));
121
122 return pass.AddCommand(std::move(cmd));
123 };
124
125 CoverageProc coverage_proc =
126 [coverage](const Entity& entity) -> std::optional<Rect> {
127 return coverage.TransformBounds(entity.GetTransform());
128 };
129
130 auto contents = AnonymousContents::Make(render_proc, coverage_proc);
131
132 Entity sub_entity;
133 sub_entity.SetContents(std::move(contents));
134 sub_entity.SetClipDepth(entity.GetClipDepth());
135 sub_entity.SetBlendMode(entity.GetBlendMode());
136 return sub_entity;
137 }
138
139 std::optional<Rect> YUVToRGBFilterContents::GetFilterSourceCoverage(
140 const Matrix& effect_transform,
141 const Rect& output_limit) const {
142 return output_limit;
143 }
144
145 } // namespace impeller
DEBUG_COMMAND_INFO
#define DEBUG_COMMAND_INFO(obj, arg)
Definition: command.h:28
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::PixelFormat::kR8UNormInt
@ kR8UNormInt
impeller::TRect::TransformBounds
constexpr TRect TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
Definition: rect.h:264
formats.h
impeller::Vector2
Point Vector2
Definition: point.h:312
impeller::RenderPipelineT::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:93
impeller::kMatrixBT601FullRange
constexpr Matrix kMatrixBT601FullRange
Definition: yuv_to_rgb_filter_contents.cc:24
matrix.h
impeller::Entity
Definition: entity.h:21
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:28
impeller::PrimitiveType::kTriangleStrip
@ kTriangleStrip
impeller::Point
TPoint< Scalar > Point
Definition: point.h:308
render_pass.h
impeller::YUVToRGBFilterContents::~YUVToRGBFilterContents
~YUVToRGBFilterContents() override
impeller::PixelFormat::kR8G8UNormInt
@ kR8G8UNormInt
impeller::YUVColorSpace::kBT601FullRange
@ kBT601FullRange
impeller::Rect
TRect< Scalar > Rect
Definition: rect.h:488
impeller::YUVToRGBFilterContents::YUVToRGBFilterContents
YUVToRGBFilterContents()
impeller::YUVColorSpace::kBT601LimitedRange
@ kBT601LimitedRange
anonymous_contents.h
impeller::kMatrixBT601LimitedRange
constexpr Matrix kMatrixBT601LimitedRange
Definition: yuv_to_rgb_filter_contents.cc:17
content_context.h
impeller::Contents::RenderProc
std::function< bool(const ContentContext &renderer, const Entity &entity, RenderPass &pass)> RenderProc
Definition: contents.h:49
impeller::YUVColorSpace
YUVColorSpace
Definition: color.h:55
impeller::Contents::CoverageProc
std::function< std::optional< Rect >(const Entity &entity)> CoverageProc
Definition: contents.h:50
impeller::FilterInput::Vector
std::vector< FilterInput::Ref > Vector
Definition: filter_input.h:33
impeller::YUVToRGBFilterContents::SetYUVColorSpace
void SetYUVColorSpace(YUVColorSpace yuv_color_space)
Definition: yuv_to_rgb_filter_contents.cc:36
impeller::RenderPipelineT::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:92
impeller
Definition: aiks_context.cc:10
impeller::Matrix::MakeScale
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104
yuv_to_rgb_filter_contents.h
impeller::ContentContext
Definition: content_context.h:332
impeller::TRect
Definition: rect.h:22
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::AnonymousContents::Make
static std::shared_ptr< Contents > Make(RenderProc render_proc, CoverageProc coverage_proc)
Definition: anonymous_contents.cc:11
vertex_buffer_builder.h