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 
17 using PipelineBuilderCallback =
18  std::function<PipelineRef(ContentContextOptions)>;
19 
22 
24  switch (type) {
26  return 0.0f;
28  return 1.0f;
30  return 2.0f;
32  return 3.0f;
34  return 4.0f;
35  }
36 }
37 
38 Scalar ToShaderStrokeJoin(Join join) {
39  switch (join) {
40  case Join::kMiter:
41  return 0.0f;
42  case Join::kBevel:
43  return 1.0f;
44  case Join::kRound:
45  return 2.0f;
46  }
47 }
48 
49 } // namespace
50 
51 std::unique_ptr<UberSDFContents> UberSDFContents::Make(
52  const UberSDFParameters& params,
53  std::unique_ptr<Geometry> geometry) {
54  return std::unique_ptr<UberSDFContents>(
55  new UberSDFContents(params, std::move(geometry)));
56 }
57 
58 UberSDFContents::UberSDFContents(const UberSDFParameters& params,
59  std::unique_ptr<Geometry> geometry)
60  : params_(params), geometry_(std::move(geometry)) {}
61 
63 
65  const Entity& entity,
66  RenderPass& pass) const {
67  auto& data_host_buffer = renderer.GetTransientsDataBuffer();
68 
69  VS::FrameInfo frame_info;
70  FS::FragInfo frag_info;
71  frag_info.type = ToShaderType(params_.type);
72  frag_info.color =
73  params_.color.WithAlpha(params_.color.alpha * GetOpacityFactor());
74  frag_info.center = params_.center;
75  frag_info.size = params_.size;
76  frag_info.stroked = params_.stroke ? 1.0f : 0.0f;
77  frag_info.stroke_width = params_.stroke ? params_.stroke->width : 0.0f;
78  frag_info.stroke_join =
79  params_.stroke ? ToShaderStrokeJoin(params_.stroke->join) : 0.0f;
80  frag_info.aa_pixels = UberSDFParameters::kAntialiasPixels;
81  frag_info.superellipse_degree = params_.superellipse_degree;
82  frag_info.superellipse_semi_axis = params_.superellipse_semi_axis;
83  frag_info.angle_span = params_.angle_span;
84  frag_info.octant_offset_c = params_.octant_offset_c;
85  frag_info.circle_center_top = params_.circle_center_top;
86  frag_info.circle_center_right = params_.circle_center_right;
87  frag_info.superellipse_scale = params_.superellipse_scale;
88  frag_info.radii = params_.radii;
89 
90  auto geometry_result =
91  GetGeometry()->GetPositionBuffer(renderer, entity, pass);
92 
93  PipelineBuilderCallback pipeline_callback =
94  [&renderer](ContentContextOptions options) {
95  return renderer.GetUberSDFPipeline(options);
96  };
97 
98  return ColorSourceContents::DrawGeometry<VS>(
99  this, GetGeometry(), renderer, entity, pass, pipeline_callback,
100  frame_info,
101  /*bind_fragment_callback=*/
102  [&frag_info, &data_host_buffer](RenderPass& pass) {
103  FS::BindFragInfo(pass, data_host_buffer.EmplaceUniform(frag_info));
104  pass.SetCommandLabel("UberSDF");
105  return true;
106  },
107  /*force_stencil=*/false,
108  /*create_geom_callback=*/
109  [geometry_result = std::move(geometry_result)](
110  const ContentContext& renderer, const Entity& entity,
111  RenderPass& pass,
112  const Geometry* geometry) { return geometry_result; });
113 }
114 
115 std::optional<Rect> UberSDFContents::GetCoverage(const Entity& entity) const {
116  return GetGeometry()->GetCoverage(entity.GetTransform());
117 }
118 
120  return geometry_.get();
121 }
122 
124  return params_.color;
125 }
126 
128  const ColorFilterProc& color_filter_proc) {
129  params_.color = color_filter_proc(params_.color);
130  return true;
131 }
132 
134  const Entity& entity,
135  ISize target_size) const {
136  if (params_.type != UberSDFParameters::Type::kRect) {
137  return std::nullopt;
138  }
139  const Geometry* geometry = GetGeometry();
140  if (geometry == nullptr) {
141  return std::nullopt;
142  }
143  IRect target_rect = IRect::MakeSize(target_size);
144  return geometry->CoversArea(entity.GetTransform(), target_rect)
145  ? GetColor()
146  : std::optional<Color>();
147 }
148 
149 } // namespace impeller
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
virtual bool CoversArea(const Matrix &transform, const IRect &rect) const
Determines if this geometry, transformed by the given transform, will completely cover all of the pix...
Definition: geometry.cc:136
virtual GeometryResult GetPositionBuffer(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const =0
virtual std::optional< Rect > GetCoverage(const Matrix &transform) const =0
The coverage rectangle of this geometry, transformed by the transform argument.
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
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 > Make(const UberSDFParameters &params, std::unique_ptr< Geometry > geometry)
std::optional< Color > AsBackgroundColor(const Entity &entity, ISize target_size) const override
Returns a color if this Contents will flood the given target_size with a color. This output color is ...
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
raw_ptr< Pipeline< PipelineDescriptor > > PipelineRef
A raw ptr to a pipeline object.
Definition: pipeline.h:89
LinePipeline::FragmentShader FS
LinePipeline::VertexShader VS
constexpr ArchiveShaderType ToShaderType(fb::Stage stage)
Definition: comparable.h:93
Scalar alpha
Definition: color.h:143
constexpr Color WithAlpha(Scalar new_alpha) const
Definition: color.h:283
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:150
Parameters for rendering shapes using the UberSDF shader.
Color color
The color used for filling or stroking the shape.
float octant_offset_c
The geometric offset 'c' used to connect the two octants of each quadrant.
Point superellipse_degree
The degree (n) of the superellipse curve for the top and right octants.
Point angle_span
The angular span of the circular cap for the top and right octants.
Type type
The type of shape to render.
Point center
The center point of the shape in local coordinates.
Type
The type of primitive shape.
static constexpr Scalar kAntialiasPixels
std::optional< StrokeParameters > stroke
The stroke parameters. If std::nullopt, the shape is filled.