| Flutter Impeller |

srgb_to_linear_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/srgb_to_linear_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 SrgbToLinearFilterContents::SrgbToLinearFilterContents() = default;
17
18 SrgbToLinearFilterContents::~SrgbToLinearFilterContents() = default;
19
20 std::optional<Entity> SrgbToLinearFilterContents::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 = SrgbToLinearFilterPipeline::VertexShader;
32 using FS = SrgbToLinearFilterPipeline::FragmentShader;
33
34 auto input_snapshot =
35 inputs[0]->GetSnapshot("SrgbToLinear", 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 pass.SetCommandLabel("sRGB to Linear Filter");
48
49 auto options = OptionsFromPassAndEntity(pass, entity);
50 options.primitive_type = PrimitiveType::kTriangleStrip;
51 pass.SetPipeline(renderer.GetSrgbToLinearFilterPipeline(options));
52
53 auto size = input_snapshot->texture->GetSize();
54
55 std::array<VS::PerVertexData, 4> vertices = {
56 VS::PerVertexData{Point(0, 0)},
57 VS::PerVertexData{Point(1, 0)},
58 VS::PerVertexData{Point(0, 1)},
59 VS::PerVertexData{Point(1, 1)},
60 };
61
62 auto& data_host_buffer = renderer.GetTransientsDataBuffer();
63 pass.SetVertexBuffer(CreateVertexBuffer(vertices, data_host_buffer));
64
65 VS::FrameInfo frame_info;
66 frame_info.mvp = Entity::GetShaderTransform(
67 entity.GetShaderClipDepth(), pass,
68 entity.GetTransform() * input_snapshot->transform *
69 Matrix::MakeScale(Vector2(size)));
70 frame_info.texture_sampler_y_coord_scale =
71 input_snapshot->texture->GetYCoordScale();
72
73 FS::FragInfo frag_info;
74 frag_info.input_alpha =
75 absorb_opacity == ColorFilterContents::AbsorbOpacity::kYes
76 ? input_snapshot->opacity
77 : 1.0f;
78
79 raw_ptr<const Sampler> sampler =
80 renderer.GetContext()->GetSamplerLibrary()->GetSampler({});
81 FS::BindInputTexture(pass, input_snapshot->texture, sampler);
82 FS::BindFragInfo(pass, data_host_buffer.EmplaceUniform(frag_info));
83 VS::BindFrameInfo(pass, data_host_buffer.EmplaceUniform(frame_info));
84
85 return pass.Draw().ok();
86 };
87
88 CoverageProc coverage_proc =
89 [coverage](const Entity& entity) -> std::optional<Rect> {
90 return coverage.TransformBounds(entity.GetTransform());
91 };
92
93 std::shared_ptr<Contents> contents =
94 AnonymousContents::Make(render_proc, coverage_proc);
95
96 Entity sub_entity;
97 sub_entity.SetContents(std::move(contents));
98 sub_entity.SetBlendMode(entity.GetBlendMode());
99 return sub_entity;
100 }
101
102 } // namespace impeller
anonymous_contents.h
impeller::AnonymousContents::Make
static std::shared_ptr< Contents > Make(RenderProc render_proc, CoverageProc coverage_proc)
Definition: anonymous_contents.cc:11
impeller::ColorFilterContents::AbsorbOpacity::kYes
@ kYes
impeller::ColorFilterContents::GetAbsorbOpacity
AbsorbOpacity GetAbsorbOpacity() const
Definition: color_filter_contents.cc:89
impeller::Contents::CoverageProc
std::function< std::optional< Rect >(const Entity &entity)> CoverageProc
Definition: contents.h:40
impeller::Contents::RenderProc
std::function< bool(const ContentContext &renderer, const Entity &entity, RenderPass &pass)> RenderProc
Definition: contents.h:39
impeller::Entity::GetShaderTransform
Matrix GetShaderTransform(const RenderPass &pass) const
Definition: entity.cc:50
impeller::FilterInput::Vector
std::vector< FilterInput::Ref > Vector
Definition: filter_input.h:33
impeller::RenderPipelineHandle::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:163
impeller::RenderPipelineHandle::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:164
impeller::SrgbToLinearFilterContents::~SrgbToLinearFilterContents
~SrgbToLinearFilterContents() override
impeller::SrgbToLinearFilterContents::SrgbToLinearFilterContents
SrgbToLinearFilterContents()
content_context.h
contents.h
impeller
Definition: allocation.cc:12
impeller::PrimitiveType::kTriangleStrip
@ kTriangleStrip
impeller::Vector2
Point Vector2
Definition: point.h:429
impeller::Rect
TRect< Scalar > Rect
Definition: rect.h:788
impeller::Point
TPoint< Scalar > Point
Definition: point.h:425
impeller::FS
LinePipeline::FragmentShader FS
Definition: line_contents.cc:14
impeller::CreateVertexBuffer
VertexBuffer CreateVertexBuffer(std::array< VertexType, size > input, HostBuffer &data_host_buffer)
Create an index-less vertex buffer from a fixed size array.
Definition: vertex_buffer_builder.h:24
impeller::VS
LinePipeline::VertexShader VS
Definition: line_contents.cc:13
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:34
point.h
render_pass.h
srgb_to_linear_filter_contents.h
impeller::Matrix::MakeScale
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104
vertex_buffer_builder.h