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 
6 
13 
14 namespace impeller {
15 
17 
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 
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  pass.SetCommandLabel("Linear to sRGB Filter");
48 
49  auto options = OptionsFromPassAndEntity(pass, entity);
50  options.primitive_type = PrimitiveType::kTriangleStrip;
51  pass.SetPipeline(renderer.GetLinearToSrgbFilterPipeline(options));
52 
53  auto size = input_snapshot->texture->GetSize();
54  std::array<VS::PerVertexData, 4> vertices = {
55  VS::PerVertexData{Point(0, 0)},
56  VS::PerVertexData{Point(1, 0)},
57  VS::PerVertexData{Point(0, 1)},
58  VS::PerVertexData{Point(1, 1)},
59  };
60 
61  auto& host_buffer = renderer.GetTransientsBuffer();
62  pass.SetVertexBuffer(CreateVertexBuffer(vertices, host_buffer));
63 
64  VS::FrameInfo frame_info;
65  frame_info.mvp = Entity::GetShaderTransform(
66  entity.GetShaderClipDepth(), pass,
67  entity.GetTransform() * input_snapshot->transform *
68  Matrix::MakeScale(Vector2(size)));
69  frame_info.texture_sampler_y_coord_scale =
70  input_snapshot->texture->GetYCoordScale();
71 
72  FS::FragInfo frag_info;
73  frag_info.input_alpha =
75  ? input_snapshot->opacity
76  : 1.0f;
77 
78  FS::BindInputTexture(
79  pass, input_snapshot->texture,
80  renderer.GetContext()->GetSamplerLibrary()->GetSampler({}));
81  FS::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
82  VS::BindFrameInfo(pass, host_buffer.EmplaceUniform(frame_info));
83 
84  return pass.Draw().ok();
85  };
86 
87  CoverageProc coverage_proc =
88  [coverage](const Entity& entity) -> std::optional<Rect> {
89  return coverage.TransformBounds(entity.GetTransform());
90  };
91 
92  auto contents = AnonymousContents::Make(render_proc, coverage_proc);
93 
94  Entity sub_entity;
95  sub_entity.SetContents(std::move(contents));
96  sub_entity.SetBlendMode(entity.GetBlendMode());
97  return sub_entity;
98 }
99 
100 } // 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
TRect< Scalar > Rect
Definition: rect.h:792
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
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104