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"
14 
15 namespace impeller {
16 
17 class Tessellator;
18 
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 Path& path,
56  std::optional<Rect> inner_rect = std::nullopt);
57 
58  static std::unique_ptr<Geometry> MakeStrokePath(
59  const Path& path,
60  Scalar stroke_width = 0.0,
61  Scalar miter_limit = 4.0,
62  Cap stroke_cap = Cap::kButt,
63  Join stroke_join = Join::kMiter);
64 
65  static std::unique_ptr<Geometry> MakeCover();
66 
67  static std::unique_ptr<Geometry> MakeRect(const Rect& rect);
68 
69  static std::unique_ptr<Geometry> MakeOval(const Rect& rect);
70 
71  static std::unique_ptr<Geometry> MakeLine(const Point& p0,
72  const Point& p1,
73  Scalar width,
74  Cap cap);
75 
76  static std::unique_ptr<Geometry> MakeCircle(const Point& center,
77  Scalar radius);
78 
79  static std::unique_ptr<Geometry> MakeStrokedCircle(const Point& center,
80  Scalar radius,
82 
83  static std::unique_ptr<Geometry> MakeRoundRect(const Rect& rect,
84  const Size& radii);
85 
86  static std::unique_ptr<Geometry> MakeRoundSuperellipse(const Rect& rect,
87  Scalar corner_radius);
88 
90  const Entity& entity,
91  RenderPass& pass) const = 0;
92 
93  virtual GeometryResult::Mode GetResultMode() const;
94 
95  virtual std::optional<Rect> GetCoverage(const Matrix& transform) const = 0;
96 
97  /// @brief Compute an alpha value to simulate lower coverage of fractional
98  /// pixel strokes.
99  static Scalar ComputeStrokeAlphaCoverage(const Matrix& entity,
101 
102  /// @brief Determines if this geometry, transformed by the given
103  /// `transform`, will completely cover all surface area of the given
104  /// `rect`.
105  ///
106  /// This is a conservative estimate useful for certain
107  /// optimizations.
108  ///
109  /// @returns `true` if the transformed geometry is guaranteed to cover the
110  /// given `rect`. May return `false` in many undetected cases where
111  /// the transformed geometry does in fact cover the `rect`.
112  virtual bool CoversArea(const Matrix& transform, const Rect& rect) const;
113 
114  virtual bool IsAxisAlignedRect() const;
115 
116  virtual bool CanApplyMaskFilter() const;
117 
118  virtual Scalar ComputeAlphaCoverage(const Matrix& transform) const {
119  return 1.0;
120  }
121 
123  const ContentContext& renderer,
124  const Tessellator::VertexGenerator& generator,
125  const Entity& entity,
126  RenderPass& pass);
127 };
128 
129 } // namespace impeller
130 
131 #endif // FLUTTER_IMPELLER_ENTITY_GEOMETRY_GEOMETRY_H_
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:67
virtual Scalar ComputeAlphaCoverage(const Matrix &transform) const
Definition: geometry.h:118
static std::unique_ptr< Geometry > MakeFillPath(const Path &path, std::optional< Rect > inner_rect=std::nullopt)
Definition: geometry.cc:61
static std::unique_ptr< Geometry > MakeLine(const Point &p0, const Point &p1, Scalar width, Cap cap)
Definition: geometry.cc:92
static Scalar ComputeStrokeAlphaCoverage(const Matrix &entity, Scalar stroke_width)
Compute an alpha value to simulate lower coverage of fractional pixel strokes.
Definition: geometry.cc:134
virtual GeometryResult::Mode GetResultMode() const
Definition: geometry.cc:57
static std::unique_ptr< Geometry > MakeRect(const Rect &rect)
Definition: geometry.cc:84
static std::unique_ptr< Geometry > MakeCircle(const Point &center, Scalar radius)
Definition: geometry.cc:99
virtual GeometryResult GetPositionBuffer(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const =0
virtual bool CanApplyMaskFilter() const
Definition: geometry.cc:129
static std::unique_ptr< Geometry > MakeRoundRect(const Rect &rect, const Size &radii)
Definition: geometry.cc:110
static GeometryResult ComputePositionGeometry(const ContentContext &renderer, const Tessellator::VertexGenerator &generator, const Entity &entity, RenderPass &pass)
Definition: geometry.cc:25
virtual std::optional< Rect > GetCoverage(const Matrix &transform) const =0
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:121
static std::unique_ptr< Geometry > MakeOval(const Rect &rect)
Definition: geometry.cc:88
static std::unique_ptr< Geometry > MakeStrokedCircle(const Point &center, Scalar radius, Scalar stroke_width)
Definition: geometry.cc:104
static std::unique_ptr< Geometry > MakeRoundSuperellipse(const Rect &rect, Scalar corner_radius)
Definition: geometry.cc:115
virtual ~Geometry()
Definition: geometry.h:52
static std::unique_ptr< Geometry > MakeCover()
Definition: geometry.cc:80
virtual bool IsAxisAlignedRect() const
Definition: geometry.cc:125
Paths are lightweight objects that describe a collection of linear, quadratic, or cubic segments....
Definition: path.h:54
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
@ kNone
Does not use the index buffer.
Join
Definition: path.h:26
PrimitiveType
Decides how backend draws pixels based on input vertices.
Definition: formats.h:352
float Scalar
Definition: scalar.h:18
Cap
Definition: path.h:20
static const GeometryResult kEmptyResult
Definition: geometry.h:43
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.
VertexBuffer vertex_buffer
Definition: geometry.h:38
A 4x4 matrix using column-major storage.
Definition: matrix.h:37