Flutter Impeller
geometry.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 <memory>
8 #include <optional>
9 
19 #include "impeller/geometry/rect.h"
20 
21 namespace impeller {
22 
24  const ContentContext& renderer,
25  const Tessellator::VertexGenerator& generator,
26  const Entity& entity,
27  RenderPass& pass) {
28  using VT = SolidFillVertexShader::PerVertexData;
29 
30  size_t count = generator.GetVertexCount();
31 
32  return GeometryResult{
33  .type = generator.GetTriangleType(),
34  .vertex_buffer =
35  {
36  .vertex_buffer = renderer.GetTransientsBuffer().Emplace(
37  count * sizeof(VT), alignof(VT),
38  [&generator](uint8_t* buffer) {
39  auto vertices = reinterpret_cast<VT*>(buffer);
40  generator.GenerateVertices([&vertices](const Point& p) {
41  *vertices++ = {
42  .position = p,
43  };
44  });
45  FML_DCHECK(vertices == reinterpret_cast<VT*>(buffer) +
46  generator.GetVertexCount());
47  }),
48  .vertex_count = count,
49  .index_type = IndexType::kNone,
50  },
51  .transform = entity.GetShaderTransform(pass),
52  };
53 }
54 
57 }
58 
59 std::unique_ptr<Geometry> Geometry::MakeFillPath(
60  const Path& path,
61  std::optional<Rect> inner_rect) {
62  return std::make_unique<FillPathGeometry>(path, inner_rect);
63 }
64 
65 std::unique_ptr<Geometry> Geometry::MakeStrokePath(const Path& path,
67  Scalar miter_limit,
68  Cap stroke_cap,
69  Join stroke_join) {
70  // Skia behaves like this.
71  if (miter_limit < 0) {
72  miter_limit = 4.0;
73  }
74  return std::make_unique<StrokePathGeometry>(path, stroke_width, miter_limit,
75  stroke_cap, stroke_join);
76 }
77 
78 std::unique_ptr<Geometry> Geometry::MakeCover() {
79  return std::make_unique<CoverGeometry>();
80 }
81 
82 std::unique_ptr<Geometry> Geometry::MakeRect(const Rect& rect) {
83  return std::make_unique<RectGeometry>(rect);
84 }
85 
86 std::unique_ptr<Geometry> Geometry::MakeOval(const Rect& rect) {
87  return std::make_unique<EllipseGeometry>(rect);
88 }
89 
90 std::unique_ptr<Geometry> Geometry::MakeLine(const Point& p0,
91  const Point& p1,
92  Scalar width,
93  Cap cap) {
94  return std::make_unique<LineGeometry>(p0, p1, width, cap);
95 }
96 
97 std::unique_ptr<Geometry> Geometry::MakeCircle(const Point& center,
98  Scalar radius) {
99  return std::make_unique<CircleGeometry>(center, radius);
100 }
101 
102 std::unique_ptr<Geometry> Geometry::MakeStrokedCircle(const Point& center,
103  Scalar radius,
105  return std::make_unique<CircleGeometry>(center, radius, stroke_width);
106 }
107 
108 std::unique_ptr<Geometry> Geometry::MakeRoundRect(const Rect& rect,
109  const Size& radii) {
110  return std::make_unique<RoundRectGeometry>(rect, radii);
111 }
112 
113 bool Geometry::CoversArea(const Matrix& transform, const Rect& rect) const {
114  return false;
115 }
116 
118  return false;
119 }
120 
122  return true;
123 }
124 
125 // static
128  Scalar scaled_stroke_width = transform.GetMaxBasisLengthXY() * stroke_width;
129  if (scaled_stroke_width == 0.0 || scaled_stroke_width >= kMinStrokeSize) {
130  return 1.0;
131  }
132  // This scalling is eyeballed from Skia.
133  return std::clamp(scaled_stroke_width * 2.0f, 0.f, 1.f);
134 }
135 
136 } // namespace impeller
HostBuffer & GetTransientsBuffer() const
Retrieve the currnent host buffer for transient storage.
Matrix GetShaderTransform(const RenderPass &pass) const
Get the vertex shader transform used for drawing this Entity.
Definition: entity.cc:48
static std::unique_ptr< Geometry > MakeStrokePath(const Path &path, Scalar stroke_width=0.0, Scalar miter_limit=4.0, Cap stroke_cap=Cap::kButt, Join stroke_join=Join::kMiter)
Definition: geometry.cc:65
static std::unique_ptr< Geometry > MakeFillPath(const Path &path, std::optional< Rect > inner_rect=std::nullopt)
Definition: geometry.cc:59
static std::unique_ptr< Geometry > MakeLine(const Point &p0, const Point &p1, Scalar width, Cap cap)
Definition: geometry.cc:90
static Scalar ComputeStrokeAlphaCoverage(const Matrix &entity, Scalar stroke_width)
Compute an alpha value to simulate lower coverage of fractional pixel strokes.
Definition: geometry.cc:126
virtual GeometryResult::Mode GetResultMode() const
Definition: geometry.cc:55
static std::unique_ptr< Geometry > MakeRect(const Rect &rect)
Definition: geometry.cc:82
static std::unique_ptr< Geometry > MakeCircle(const Point &center, Scalar radius)
Definition: geometry.cc:97
virtual bool CanApplyMaskFilter() const
Definition: geometry.cc:121
static std::unique_ptr< Geometry > MakeRoundRect(const Rect &rect, const Size &radii)
Definition: geometry.cc:108
static GeometryResult ComputePositionGeometry(const ContentContext &renderer, const Tessellator::VertexGenerator &generator, const Entity &entity, RenderPass &pass)
Definition: geometry.cc:23
virtual bool CoversArea(const Matrix &transform, const Rect &rect) const
Determines if this geometry, transformed by the given transform, will completely cover all surface ar...
Definition: geometry.cc:113
static std::unique_ptr< Geometry > MakeOval(const Rect &rect)
Definition: geometry.cc:86
static std::unique_ptr< Geometry > MakeStrokedCircle(const Point &center, Scalar radius, Scalar stroke_width)
Definition: geometry.cc:102
static std::unique_ptr< Geometry > MakeCover()
Definition: geometry.cc:78
virtual bool IsAxisAlignedRect() const
Definition: geometry.cc:117
BufferView Emplace(const BufferType &buffer, size_t alignment=0)
Emplace non-uniform data (like contiguous vertices) onto the host buffer.
Definition: host_buffer.h:93
Paths are lightweight objects that describe a collection of linear, quadratic, or cubic segments....
Definition: path.h:53
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:30
An object which produces a list of vertices as |Point|s that tessellate a previously provided shape a...
Definition: tessellator.h:94
virtual size_t GetVertexCount() const =0
Returns the number of vertices that the generator plans to produce, if known.
virtual PrimitiveType GetTriangleType() const =0
Returns the |PrimitiveType| that describes the relationship among the list of vertices produced by th...
virtual void GenerateVertices(const TessellatedVertexProc &proc) const =0
Generate the vertices and deliver them in the necessary order (as required by the PrimitiveType) to t...
@ kNone
Does not use the index buffer.
Join
Definition: path.h:25
float Scalar
Definition: scalar.h:18
Cap
Definition: path.h:19
static constexpr Scalar kMinStrokeSize
Definition: geometry.h:19
const Scalar stroke_width
PrimitiveType type
Definition: geometry.h:37
@ kNormal
The geometry has no overlapping triangles.
A 4x4 matrix using column-major storage.
Definition: matrix.h:37