Flutter Impeller
uber_sdf_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 
12 
13 namespace impeller {
14 
15 namespace {
16 using PipelineBuilderCallback =
17  std::function<PipelineRef(ContentContextOptions)>;
18 
21 
22 } // namespace
23 
24 std::unique_ptr<UberSDFContents> UberSDFContents::MakeRect(
25  Color color,
26  Scalar stroke_width,
27  Join stroke_join,
28  bool stroked,
29  const FillRectGeometry* geometry) {
30  Rect bounding_box = geometry->GetRect();
31  Scalar aa_padding = 1.0f;
32  return std::make_unique<UberSDFContents>(Type::kRect, bounding_box, color,
33  stroke_width, stroke_join, stroked,
34  geometry, aa_padding);
35 }
36 
37 std::unique_ptr<UberSDFContents> UberSDFContents::MakeCircle(
38  Color color,
39  bool stroked,
40  const CircleGeometry* geometry) {
41  Point center = geometry->GetCenter();
42  Scalar radius = geometry->GetRadius();
43  Rect bounding_box = Rect::MakeXYWH(center.x - radius, center.y - radius,
44  radius * 2, radius * 2);
45  Scalar aa_padding = geometry->GetAntialiasPadding();
46  return std::unique_ptr<UberSDFContents>(new UberSDFContents(
47  Type::kCircle, bounding_box, color, geometry->GetStrokeWidth(),
48  Join::kMiter, stroked, geometry, aa_padding));
49 }
50 
52  Rect bounding_box,
53  Color color,
54  Scalar stroke_width,
55  Join stroke_join,
56  bool stroked,
57  const Geometry* geometry,
58  Scalar aa_padding)
59  : type_(type),
60  bounding_box_(bounding_box),
61  color_(color),
62  stroke_width_(stroke_width),
63  stroke_join_(stroke_join),
64  stroked_(stroked),
65  geometry_(geometry),
66  aa_padding_(aa_padding) {}
67 
69 
71  const Entity& entity,
72  RenderPass& pass) const {
73  auto& data_host_buffer = renderer.GetTransientsDataBuffer();
74 
75  VS::FrameInfo frame_info;
76  FS::FragInfo frag_info;
77  frag_info.color = color_.WithAlpha(color_.alpha * GetOpacityFactor());
78  frag_info.center = bounding_box_.GetCenter();
79  frag_info.size =
80  Point(bounding_box_.GetWidth() / 2.0f, bounding_box_.GetHeight() / 2.0f);
81  frag_info.stroke_width = stroke_width_;
82  switch (stroke_join_) {
83  case Join::kMiter:
84  frag_info.stroke_join = 0.0f;
85  break;
86  case Join::kBevel:
87  frag_info.stroke_join = 1.0f;
88  break;
89  case Join::kRound:
90  frag_info.stroke_join = 2.0f;
91  break;
92  }
93  frag_info.aa_pixels = aa_padding_;
94  frag_info.stroked = stroked_ ? 1.0f : 0.0f;
95  frag_info.type = type_ == Type::kCircle ? 0.0f : 1.0f;
96 
97  auto geometry_result =
98  GetGeometry()->GetPositionBuffer(renderer, entity, pass);
99 
100  PipelineBuilderCallback pipeline_callback =
101  [&renderer](ContentContextOptions options) {
102  return renderer.GetUberSDFPipeline(options);
103  };
104 
105  return ColorSourceContents::DrawGeometry<VS>(
106  this, GetGeometry(), renderer, entity, pass, pipeline_callback,
107  frame_info,
108  /*bind_fragment_callback=*/
109  [&frag_info, &data_host_buffer](RenderPass& pass) {
110  FS::BindFragInfo(pass, data_host_buffer.EmplaceUniform(frag_info));
111  pass.SetCommandLabel("UberSDF");
112  return true;
113  },
114  /*force_stencil=*/false,
115  /*create_geom_callback=*/
116  [geometry_result = std::move(geometry_result)](
117  const ContentContext& renderer, const Entity& entity,
118  RenderPass& pass,
119  const Geometry* geometry) { return geometry_result; });
120 }
121 
122 std::optional<Rect> UberSDFContents::GetCoverage(const Entity& entity) const {
123  return GetGeometry()->GetCoverage(entity.GetTransform());
124 }
125 
127  return geometry_;
128 }
129 
131  return color_;
132 }
133 
135  const ColorFilterProc& color_filter_proc) {
136  color_ = color_filter_proc(color_);
137  return true;
138 }
139 
140 } // namespace impeller
Scalar GetAntialiasPadding() const
Scalar GetStrokeWidth() const
Scalar GetOpacityFactor() const
Get the opacity factor for this color source.
std::function< PipelineRef(ContentContextOptions)> PipelineBuilderCallback
PipelineRef GetUberSDFPipeline(ContentContextOptions opts) const
HostBuffer & GetTransientsDataBuffer() const
Retrieve the current host buffer for transient storage of other non-index data.
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:35
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:46
const Rect & GetRect() const
virtual GeometryResult GetPositionBuffer(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const =0
virtual std::optional< Rect > GetCoverage(const Matrix &transform) const =0
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:30
virtual void SetCommandLabel(std::string_view label)
The debugging label to use for the command.
Definition: render_pass.cc:97
FragmentShader_ FragmentShader
Definition: pipeline.h:169
static std::unique_ptr< UberSDFContents > MakeRect(Color color, Scalar stroke_width, Join stroke_join, bool stroked, const FillRectGeometry *geometry)
UberSDFContents(Type type, Rect rect, Color color, Scalar stroke_width, Join stroke_join, bool stroked, const Geometry *geometry, Scalar aa_padding)
std::optional< Rect > GetCoverage(const Entity &entity) const override
Get the area of the render pass that will be affected when this contents is rendered.
bool ApplyColorFilter(const ColorFilterProc &color_filter_proc) override
If possible, applies a color filter to this contents inputs on the CPU.
static std::unique_ptr< UberSDFContents > MakeCircle(Color color, bool stroked, const CircleGeometry *geometry)
const Geometry * GetGeometry() const override
Get the geometry that this contents will use to render.
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Join
An enum that describes ways to join two segments of a path.
float Scalar
Definition: scalar.h:19
TPoint< Scalar > Point
Definition: point.h:426
raw_ptr< Pipeline< PipelineDescriptor > > PipelineRef
A raw ptr to a pipeline object.
Definition: pipeline.h:89
LinePipeline::FragmentShader FS
LinePipeline::VertexShader VS
Scalar alpha
Definition: color.h:143
constexpr Color WithAlpha(Scalar new_alpha) const
Definition: color.h:278
constexpr Type GetHeight() const
Returns the height of the rectangle, equivalent to |GetSize().height|.
Definition: rect.h:347
constexpr static TRect MakeXYWH(Type x, Type y, Type width, Type height)
Definition: rect.h:136
constexpr Type GetWidth() const
Returns the width of the rectangle, equivalent to |GetSize().width|.
Definition: rect.h:341
constexpr Point GetCenter() const
Get the center point as a |Point|.
Definition: rect.h:382