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 
6 
7 #include <optional>
8 
17 
18 namespace impeller {
19 
21 
23 
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 {
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  Command cmd;
59  DEBUG_COMMAND_INFO(cmd, "Color Matrix Filter");
60  cmd.stencil_reference = entity.GetClipDepth();
61 
62  auto options = OptionsFromPassAndEntity(pass, entity);
63  options.primitive_type = PrimitiveType::kTriangleStrip;
64  cmd.pipeline = renderer.GetColorMatrixColorFilterPipeline(options);
65 
66  auto size = input_snapshot->texture->GetSize();
67 
68  VertexBufferBuilder<VS::PerVertexData> vtx_builder;
69  vtx_builder.AddVertices({
70  {Point(0, 0)},
71  {Point(1, 0)},
72  {Point(0, 1)},
73  {Point(1, 1)},
74  });
75  auto& host_buffer = pass.GetTransientsBuffer();
76  cmd.BindVertices(vtx_builder.CreateVertexBuffer(host_buffer));
77 
78  VS::FrameInfo frame_info;
79  frame_info.mvp = pass.GetOrthographicTransform() * entity.GetTransform() *
80  input_snapshot->transform *
82  frame_info.texture_sampler_y_coord_scale =
83  input_snapshot->texture->GetYCoordScale();
84 
85  FS::FragInfo frag_info;
86  const float* matrix = color_matrix.array;
87  frag_info.color_v = Vector4(matrix[4], matrix[9], matrix[14], matrix[19]);
88  // clang-format off
89  frag_info.color_m = Matrix(
90  matrix[0], matrix[5], matrix[10], matrix[15],
91  matrix[1], matrix[6], matrix[11], matrix[16],
92  matrix[2], matrix[7], matrix[12], matrix[17],
93  matrix[3], matrix[8], matrix[13], matrix[18]
94  );
95  // clang-format on
96  frag_info.input_alpha =
98  ? input_snapshot->opacity
99  : 1.0f;
100  auto sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler({});
101  FS::BindInputTexture(cmd, input_snapshot->texture, sampler);
102  FS::BindFragInfo(cmd, host_buffer.EmplaceUniform(frag_info));
103 
104  VS::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info));
105 
106  return pass.AddCommand(std::move(cmd));
107  };
108 
109  CoverageProc coverage_proc =
110  [coverage](const Entity& entity) -> std::optional<Rect> {
111  return coverage.TransformBounds(entity.GetTransform());
112  };
113 
114  auto contents = AnonymousContents::Make(render_proc, coverage_proc);
115 
116  Entity sub_entity;
117  sub_entity.SetContents(std::move(contents));
118  sub_entity.SetClipDepth(entity.GetClipDepth());
119  sub_entity.SetBlendMode(entity.GetBlendMode());
120  return sub_entity;
121 }
122 
123 } // namespace impeller
DEBUG_COMMAND_INFO
#define DEBUG_COMMAND_INFO(obj, arg)
Definition: command.h:28
contents.h
color_matrix_filter_contents.h
point.h
impeller::TRect::TransformBounds
constexpr TRect TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
Definition: rect.h:264
impeller::Vector2
Point Vector2
Definition: point.h:312
impeller::RenderPipelineT::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:93
impeller::ColorMatrixFilterContents::~ColorMatrixFilterContents
~ColorMatrixFilterContents() override
impeller::ColorMatrixFilterContents::SetMatrix
void SetMatrix(const ColorMatrix &matrix)
Definition: color_matrix_filter_contents.cc:24
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::ColorMatrixFilterContents::ColorMatrixFilterContents
ColorMatrixFilterContents()
impeller::ColorFilterContents::GetAbsorbOpacity
AbsorbOpacity GetAbsorbOpacity() const
Definition: color_filter_contents.cc:89
color_filter_contents.h
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
vector.h
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::ColorMatrix
Definition: color.h:117
impeller
Definition: aiks_context.cc:10
impeller::Matrix::MakeScale
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104
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