Flutter Impeller
geometry.h
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 
5 #ifndef FLUTTER_IMPELLER_ENTITY_GEOMETRY_GEOMETRY_H_
6 #define FLUTTER_IMPELLER_ENTITY_GEOMETRY_GEOMETRY_H_
7 
11 #include "impeller/entity/entity.h"
15 
16 namespace impeller {
17 
18 [[maybe_unused]]
19 static constexpr Scalar kMinStrokeSize = 1.0f;
20 
22  enum class Mode {
23  /// The geometry has no overlapping triangles.
24  kNormal,
25  /// The geometry may have overlapping triangles. The geometry should be
26  /// stenciled with the NonZero fill rule.
27  kNonZero,
28  /// The geometry may have overlapping triangles. The geometry should be
29  /// stenciled with the EvenOdd fill rule.
30  kEvenOdd,
31  /// The geometry may have overlapping triangles, but they should not
32  /// overdraw or cancel each other out. This is a special case for stroke
33  /// geometry.
35  };
36 
41 };
42 
43 static const GeometryResult kEmptyResult = {
44  .vertex_buffer =
45  {
47  },
48 };
49 
50 class Geometry {
51  public:
52  virtual ~Geometry() {}
53 
54  static std::unique_ptr<Geometry> MakeFillPath(
55  const flutter::DlPath& path,
56  std::optional<Rect> inner_rect = std::nullopt);
57 
58  static std::unique_ptr<Geometry> MakeStrokePath(
59  const flutter::DlPath& path,
60  const StrokeParameters& stroke = {});
61 
62  static std::unique_ptr<Geometry> MakeCover();
63 
64  static std::unique_ptr<Geometry> MakeRect(const Rect& rect);
65 
66  static std::unique_ptr<Geometry> MakeOval(const Rect& rect);
67 
68  static std::unique_ptr<Geometry> MakeLine(const Point& p0,
69  const Point& p1,
70  const StrokeParameters& stroke);
71 
72  static std::unique_ptr<Geometry> MakeCircle(const Point& center,
73  Scalar radius);
74 
75  static std::unique_ptr<Geometry> MakeStrokedCircle(const Point& center,
76  Scalar radius,
77  Scalar stroke_width);
78 
79  static std::unique_ptr<Geometry> MakeFilledArc(const Rect& oval_bounds,
80  Degrees start,
81  Degrees sweep,
82  bool include_center);
83 
84  static std::unique_ptr<Geometry> MakeStrokedArc(
85  const Rect& oval_bounds,
86  Degrees start,
87  Degrees sweep,
88  const StrokeParameters& stroke);
89 
90  static std::unique_ptr<Geometry> MakeRoundRect(const Rect& rect,
91  const Size& radii);
92 
93  static std::unique_ptr<Geometry> MakeRoundSuperellipse(const Rect& rect,
94  Scalar corner_radius);
95 
97  const Entity& entity,
98  RenderPass& pass) const = 0;
99 
100  virtual GeometryResult::Mode GetResultMode() const;
101 
102  /// @brief The coverage rectangle of this geometry, transformed by the
103  /// `transform` argument.
104  virtual std::optional<Rect> GetCoverage(const Matrix& transform) const = 0;
105 
106  /// @brief Compute an alpha value to simulate lower coverage of fractional
107  /// pixel strokes.
108  static Scalar ComputeStrokeAlphaCoverage(const Matrix& entity,
109  Scalar stroke_width);
110 
111  /// @brief Determines if this geometry, transformed by the given
112  /// `transform`, will completely cover all of the pixels
113  /// within the given integer `rect`.
114  ///
115  /// The integer `rect`, by definition, will contain all of
116  /// the area covered by any pixel within its boundary.
117  ///
118  /// The return value can be a conservative estimate which
119  /// will still be useful for certain optimizations. It may
120  /// return `false` for obscure cases that might actually contain
121  /// all of the pixels if it is too computationally costly
122  /// to prove containment, but it should never return 'true'
123  /// unless the implementation can prove that all pixels are
124  /// fully covered (rendered) by the geometry.
125  ///
126  /// @returns `true` if the transformed geometry is guaranteed to cover
127  /// the given `rect`. May return `false` in some cases where
128  /// the transformed geometry does in fact cover the `rect`.
129  virtual bool CoversArea(const Matrix& transform, const IRect& rect) const;
130 
131  virtual bool IsAxisAlignedRect() const;
132 
133  virtual bool CanApplyMaskFilter() const;
134 
135  virtual Scalar ComputeAlphaCoverage(const Matrix& transform) const {
136  return 1.0;
137  }
138 
140  const ContentContext& renderer,
141  const Tessellator::VertexGenerator& generator,
142  const Entity& entity,
143  RenderPass& pass);
144 };
145 
146 } // namespace impeller
147 
148 #endif // FLUTTER_IMPELLER_ENTITY_GEOMETRY_GEOMETRY_H_
virtual Scalar ComputeAlphaCoverage(const Matrix &transform) const
Definition: geometry.h:135
static std::unique_ptr< Geometry > MakeFillPath(const flutter::DlPath &path, std::optional< Rect > inner_rect=std::nullopt)
Definition: geometry.cc:62
static Scalar ComputeStrokeAlphaCoverage(const Matrix &entity, Scalar stroke_width)
Compute an alpha value to simulate lower coverage of fractional pixel strokes.
Definition: geometry.cc:149
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::Mode GetResultMode() const
Definition: geometry.cc:58
static std::unique_ptr< Geometry > MakeRect(const Rect &rect)
Definition: geometry.cc:83
static std::unique_ptr< Geometry > MakeFilledArc(const Rect &oval_bounds, Degrees start, Degrees sweep, bool include_center)
Definition: geometry.cc:108
static std::unique_ptr< Geometry > MakeStrokePath(const flutter::DlPath &path, const StrokeParameters &stroke={})
Definition: geometry.cc:68
static std::unique_ptr< Geometry > MakeCircle(const Point &center, Scalar radius)
Definition: geometry.cc:97
virtual GeometryResult GetPositionBuffer(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const =0
virtual bool CanApplyMaskFilter() const
Definition: geometry.cc:144
static std::unique_ptr< Geometry > MakeRoundRect(const Rect &rect, const Size &radii)
Definition: geometry.cc:125
static GeometryResult ComputePositionGeometry(const ContentContext &renderer, const Tessellator::VertexGenerator &generator, const Entity &entity, RenderPass &pass)
Definition: geometry.cc:26
virtual std::optional< Rect > GetCoverage(const Matrix &transform) const =0
The coverage rectangle of this geometry, transformed by the transform argument.
static std::unique_ptr< Geometry > MakeLine(const Point &p0, const Point &p1, const StrokeParameters &stroke)
Definition: geometry.cc:91
static std::unique_ptr< Geometry > MakeOval(const Rect &rect)
Definition: geometry.cc:87
static std::unique_ptr< Geometry > MakeStrokedCircle(const Point &center, Scalar radius, Scalar stroke_width)
Definition: geometry.cc:102
static std::unique_ptr< Geometry > MakeRoundSuperellipse(const Rect &rect, Scalar corner_radius)
Definition: geometry.cc:130
virtual ~Geometry()
Definition: geometry.h:52
static std::unique_ptr< Geometry > MakeCover()
Definition: geometry.cc:79
static std::unique_ptr< Geometry > MakeStrokedArc(const Rect &oval_bounds, Degrees start, Degrees sweep, const StrokeParameters &stroke)
Definition: geometry.cc:116
virtual bool IsAxisAlignedRect() const
Definition: geometry.cc:140
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:110
PrimitiveType
Decides how backend draws pixels based on input vertices.
Definition: formats.h:529
float Scalar
Definition: scalar.h:19
@ kNone
Does not use the index buffer.
TRect< Scalar > Rect
Definition: rect.h:857
TPoint< Scalar > Point
Definition: point.h:426
static const GeometryResult kEmptyResult
Definition: geometry.h:43
flutter::DlPath DlPath
Definition: dl_dispatcher.h:34
TSize< Scalar > Size
Definition: size.h:159
static constexpr Scalar kMinStrokeSize
Definition: geometry.h:19
PrimitiveType type
Definition: geometry.h:37
@ kNormal
The geometry has no overlapping triangles.
VertexBuffer vertex_buffer
Definition: geometry.h:38
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
A structure to store all of the parameters related to stroking a path or basic geometry object.
const size_t start