| Flutter Impeller |

color_matrix_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/color_matrix_filter_contents.h"
6
7 #include <optional>
8
9 #include "impeller/entity/contents/anonymous_contents.h"
10 #include "impeller/entity/contents/content_context.h"
11 #include "impeller/entity/contents/contents.h"
12 #include "impeller/entity/contents/filters/color_filter_contents.h"
13 #include "impeller/geometry/point.h"
14 #include "impeller/geometry/vector.h"
15 #include "impeller/renderer/render_pass.h"
16 #include "impeller/renderer/vertex_buffer_builder.h"
17
18 namespace impeller {
19
20 ColorMatrixFilterContents::ColorMatrixFilterContents() = default;
21
22 ColorMatrixFilterContents::~ColorMatrixFilterContents() = default;
23
24 void ColorMatrixFilterContents::SetMatrix(const ColorMatrix& matrix) {
25 matrix_ = matrix;
26 }
27
28 std::optional<Entity> ColorMatrixFilterContents::RenderFilter(
29 const FilterInput::Vector& inputs,
30 const ContentContext& renderer,
31 const Entity& entity,
32 const Matrix& effect_transform,
33 const Rect& coverage,
34 const std::optional<Rect>& coverage_hint) const {
35 using VS = ColorMatrixColorFilterPipeline::VertexShader;
36 using FS = ColorMatrixColorFilterPipeline::FragmentShader;
37
38 //----------------------------------------------------------------------------
39 /// Handle inputs.
40 ///
41
42 if (inputs.empty()) {
43 return std::nullopt;
44 }
45
46 auto input_snapshot = inputs[0]->GetSnapshot("ColorMatrix", renderer, entity);
47 if (!input_snapshot.has_value()) {
48 return std::nullopt;
49 }
50
51 //----------------------------------------------------------------------------
52 /// Create AnonymousContents for rendering.
53 ///
54 RenderProc render_proc = [input_snapshot, color_matrix = matrix_,
55 absorb_opacity = GetAbsorbOpacity()](
56 const ContentContext& renderer,
57 const Entity& entity, RenderPass& pass) -> bool {
58 pass.SetCommandLabel("Color Matrix Filter");
59
60 auto options = OptionsFromPassAndEntity(pass, entity);
61 options.primitive_type = PrimitiveType::kTriangleStrip;
62 pass.SetPipeline(renderer.GetColorMatrixColorFilterPipeline(options));
63
64 auto size = input_snapshot->texture->GetSize();
65
66 std::array<VS::PerVertexData, 4> vertices = {
67 VS::PerVertexData{Point(0, 0), Point(0, 0)},
68 VS::PerVertexData{Point(1, 0), Point(1, 0)},
69 VS::PerVertexData{Point(0, 1), Point(0, 1)},
70 VS::PerVertexData{Point(1, 1), Point(1, 1)},
71 };
72 auto& data_host_buffer = renderer.GetTransientsDataBuffer();
73 pass.SetVertexBuffer(CreateVertexBuffer(vertices, data_host_buffer));
74
75 VS::FrameInfo frame_info;
76 frame_info.mvp = Entity::GetShaderTransform(
77 entity.GetShaderClipDepth(), pass,
78 entity.GetTransform() * input_snapshot->transform *
79 Matrix::MakeScale(Vector2(size)));
80 frame_info.texture_sampler_y_coord_scale =
81 input_snapshot->texture->GetYCoordScale();
82
83 FS::FragInfo frag_info;
84 const float* matrix = color_matrix.array;
85 frag_info.color_v = Vector4(matrix[4], matrix[9], matrix[14], matrix[19]);
86 frag_info.color_m = Matrix(matrix[0], matrix[5], matrix[10], matrix[15], //
87 matrix[1], matrix[6], matrix[11], matrix[16], //
88 matrix[2], matrix[7], matrix[12], matrix[17], //
89 matrix[3], matrix[8], matrix[13], matrix[18] //
90 );
91 frag_info.input_alpha =
92 absorb_opacity == ColorFilterContents::AbsorbOpacity::kYes
93 ? input_snapshot->opacity
94 : 1.0f;
95 frag_info.output_alpha = 1;
96
97 raw_ptr<const Sampler> sampler =
98 renderer.GetContext()->GetSamplerLibrary()->GetSampler({});
99 FS::BindInputTexture(pass, input_snapshot->texture, sampler);
100 FS::BindFragInfo(pass, data_host_buffer.EmplaceUniform(frag_info));
101 VS::BindFrameInfo(pass, data_host_buffer.EmplaceUniform(frame_info));
102
103 return pass.Draw().ok();
104 };
105
106 CoverageProc coverage_proc =
107 [coverage](const Entity& entity) -> std::optional<Rect> {
108 return coverage.TransformBounds(entity.GetTransform());
109 };
110
111 auto contents = AnonymousContents::Make(render_proc, coverage_proc);
112
113 Entity sub_entity;
114 sub_entity.SetContents(std::move(contents));
115 sub_entity.SetBlendMode(entity.GetBlendMode());
116 return sub_entity;
117 }
118
119 } // 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::ColorMatrixFilterContents::~ColorMatrixFilterContents
~ColorMatrixFilterContents() override
impeller::ColorMatrixFilterContents::SetMatrix
void SetMatrix(const ColorMatrix &matrix)
Definition: color_matrix_filter_contents.cc:24
impeller::ColorMatrixFilterContents::ColorMatrixFilterContents
ColorMatrixFilterContents()
impeller::ContentContext
Definition: content_context.h:138
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
Definition: entity.h:26
impeller::Entity::GetShaderTransform
Matrix GetShaderTransform(const RenderPass &pass) const
Definition: entity.cc:48
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
color_filter_contents.h
color_matrix_filter_contents.h
content_context.h
contents.h
impeller
Definition: allocation.cc:12
impeller::PrimitiveType::kTriangleStrip
@ kTriangleStrip
impeller::Vector2
Point Vector2
Definition: point.h:331
impeller::Point
TPoint< Scalar > Point
Definition: point.h:327
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
impeller::ColorMatrix
Definition: color.h:117
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::Matrix::MakeScale
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104
impeller::TRect< Scalar >
impeller::TRect::TransformBounds
constexpr TRect TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
Definition: rect.h:476
vector.h
vertex_buffer_builder.h