| Flutter Impeller |

linear_to_srgb_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/linear_to_srgb_filter_contents.h"
6
7 #include "impeller/entity/contents/anonymous_contents.h"
8 #include "impeller/entity/contents/content_context.h"
9 #include "impeller/entity/contents/contents.h"
10 #include "impeller/geometry/point.h"
11 #include "impeller/renderer/render_pass.h"
12 #include "impeller/renderer/vertex_buffer_builder.h"
13
14 namespace impeller {
15
16 LinearToSrgbFilterContents::LinearToSrgbFilterContents() = default;
17
18 LinearToSrgbFilterContents::~LinearToSrgbFilterContents() = default;
19
20 std::optional<Entity> LinearToSrgbFilterContents::RenderFilter(
21 const FilterInput::Vector& inputs,
22 const ContentContext& renderer,
23 const Entity& entity,
24 const Matrix& effect_transform,
25 const Rect& coverage,
26 const std::optional<Rect>& coverage_hint) const {
27 if (inputs.empty()) {
28 return std::nullopt;
29 }
30
31 using VS = LinearToSrgbFilterPipeline::VertexShader;
32 using FS = LinearToSrgbFilterPipeline::FragmentShader;
33
34 auto input_snapshot =
35 inputs[0]->GetSnapshot("LinearToSrgb", renderer, entity);
36 if (!input_snapshot.has_value()) {
37 return std::nullopt;
38 }
39
40 //----------------------------------------------------------------------------
41 /// Create AnonymousContents for rendering.
42 ///
43 RenderProc render_proc = [input_snapshot,
44 absorb_opacity = GetAbsorbOpacity()](
45 const ContentContext& renderer,
46 const Entity& entity, RenderPass& pass) -> bool {
47 Command cmd;
48 DEBUG_COMMAND_INFO(cmd, "Linear to sRGB Filter");
49 cmd.stencil_reference = entity.GetClipDepth();
50
51 auto options = OptionsFromPassAndEntity(pass, entity);
52 options.primitive_type = PrimitiveType::kTriangleStrip;
53 cmd.pipeline = renderer.GetLinearToSrgbFilterPipeline(options);
54
55 auto size = input_snapshot->texture->GetSize();
56
57 VertexBufferBuilder<VS::PerVertexData> vtx_builder;
58 vtx_builder.AddVertices({
59 {Point(0, 0)},
60 {Point(1, 0)},
61 {Point(0, 1)},
62 {Point(1, 1)},
63 });
64
65 auto& host_buffer = pass.GetTransientsBuffer();
66 cmd.BindVertices(vtx_builder.CreateVertexBuffer(host_buffer));
67
68 VS::FrameInfo frame_info;
69 frame_info.mvp = pass.GetOrthographicTransform() * entity.GetTransform() *
70 input_snapshot->transform *
71 Matrix::MakeScale(Vector2(size));
72 frame_info.texture_sampler_y_coord_scale =
73 input_snapshot->texture->GetYCoordScale();
74
75 FS::FragInfo frag_info;
76 frag_info.input_alpha =
77 absorb_opacity == ColorFilterContents::AbsorbOpacity::kYes
78 ? input_snapshot->opacity
79 : 1.0f;
80
81 auto sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler({});
82 FS::BindInputTexture(cmd, input_snapshot->texture, sampler);
83 FS::BindFragInfo(cmd, host_buffer.EmplaceUniform(frag_info));
84 VS::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info));
85
86 return pass.AddCommand(std::move(cmd));
87 };
88
89 CoverageProc coverage_proc =
90 [coverage](const Entity& entity) -> std::optional<Rect> {
91 return coverage.TransformBounds(entity.GetTransform());
92 };
93
94 auto contents = AnonymousContents::Make(render_proc, coverage_proc);
95
96 Entity sub_entity;
97 sub_entity.SetContents(std::move(contents));
98 sub_entity.SetClipDepth(entity.GetClipDepth());
99 sub_entity.SetBlendMode(entity.GetBlendMode());
100 return sub_entity;
101 }
102
103 } // namespace impeller
DEBUG_COMMAND_INFO
#define DEBUG_COMMAND_INFO(obj, arg)
Definition: command.h:28
contents.h
point.h
linear_to_srgb_filter_contents.h
impeller::Vector2
Point Vector2
Definition: point.h:312
impeller::RenderPipelineT::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:93
impeller::LinearToSrgbFilterContents::LinearToSrgbFilterContents
LinearToSrgbFilterContents()
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::ColorFilterContents::GetAbsorbOpacity
AbsorbOpacity GetAbsorbOpacity() const
Definition: color_filter_contents.cc:89
impeller::Rect
TRect< Scalar > Rect
Definition: rect.h:488
anonymous_contents.h
content_context.h
impeller::Contents::RenderProc
std::function< bool(const ContentContext &renderer, const Entity &entity, RenderPass &pass)> RenderProc
Definition: contents.h:49
impeller::Contents::CoverageProc
std::function< std::optional< Rect >(const Entity &entity)> CoverageProc
Definition: contents.h:50
impeller::ColorFilterContents::AbsorbOpacity::kYes
@ kYes
impeller::FilterInput::Vector
std::vector< FilterInput::Ref > Vector
Definition: filter_input.h:33
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
impeller::AnonymousContents::Make
static std::shared_ptr< Contents > Make(RenderProc render_proc, CoverageProc coverage_proc)
Definition: anonymous_contents.cc:11
vertex_buffer_builder.h
impeller::LinearToSrgbFilterContents::~LinearToSrgbFilterContents
~LinearToSrgbFilterContents() override