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  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& host_buffer = renderer.GetTransientsBuffer();
73  pass.SetVertexBuffer(
74  CreateVertexBuffer(vertices, renderer.GetTransientsBuffer()));
75 
76  VS::FrameInfo frame_info;
77  frame_info.mvp = Entity::GetShaderTransform(
78  entity.GetShaderClipDepth(), pass,
79  entity.GetTransform() * input_snapshot->transform *
80  Matrix::MakeScale(Vector2(size)));
81  frame_info.texture_sampler_y_coord_scale =
82  input_snapshot->texture->GetYCoordScale();
83 
84  FS::FragInfo frag_info;
85  const float* matrix = color_matrix.array;
86  frag_info.color_v = Vector4(matrix[4], matrix[9], matrix[14], matrix[19]);
87  frag_info.color_m = Matrix(matrix[0], matrix[5], matrix[10], matrix[15], //
88  matrix[1], matrix[6], matrix[11], matrix[16], //
89  matrix[2], matrix[7], matrix[12], matrix[17], //
90  matrix[3], matrix[8], matrix[13], matrix[18] //
91  );
92  frag_info.input_alpha =
94  ? input_snapshot->opacity
95  : 1.0f;
96  frag_info.output_alpha = 1;
97 
98  raw_ptr<const Sampler> sampler =
99  renderer.GetContext()->GetSamplerLibrary()->GetSampler({});
100  FS::BindInputTexture(pass, input_snapshot->texture, sampler);
101  FS::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
102  VS::BindFrameInfo(pass, host_buffer.EmplaceUniform(frame_info));
103 
104  return pass.Draw().ok();
105  };
106 
107  CoverageProc coverage_proc =
108  [coverage](const Entity& entity) -> std::optional<Rect> {
109  return coverage.TransformBounds(entity.GetTransform());
110  };
111 
112  auto contents = AnonymousContents::Make(render_proc, coverage_proc);
113 
114  Entity sub_entity;
115  sub_entity.SetContents(std::move(contents));
116  sub_entity.SetBlendMode(entity.GetBlendMode());
117  return sub_entity;
118 }
119 
120 } // namespace impeller
static std::shared_ptr< Contents > Make(RenderProc render_proc, CoverageProc coverage_proc)
AbsorbOpacity GetAbsorbOpacity() const
std::function< std::optional< Rect >(const Entity &entity)> CoverageProc
Definition: contents.h:40
std::function< bool(const ContentContext &renderer, const Entity &entity, RenderPass &pass)> RenderProc
Definition: contents.h:39
Matrix GetShaderTransform(const RenderPass &pass) const
Definition: entity.cc:48
std::vector< FilterInput::Ref > Vector
Definition: filter_input.h:33
FragmentShader_ FragmentShader
Definition: pipeline.h:164
Point Vector2
Definition: point.h:331
TPoint< Scalar > Point
Definition: point.h:327
LinePipeline::FragmentShader FS
VertexBuffer CreateVertexBuffer(std::array< VertexType, size > input, HostBuffer &host_buffer)
Create an index-less vertex buffer from a fixed size array.
LinePipeline::VertexShader VS
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:34
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104
constexpr TRect TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
Definition: rect.h:476