| 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
8 #include "impeller/core/formats.h"
9 #include "impeller/core/vertex_buffer.h"
10 #include "impeller/entity/contents/content_context.h"
11 #include "impeller/entity/entity.h"
12 #include "impeller/renderer/render_pass.h"
13 #include "impeller/renderer/vertex_buffer_builder.h"
14
15 namespace impeller {
16
17 class Tessellator;
18
19 /// @brief The minimum stroke size can be less than one physical pixel because
20 /// of MSAA, but no less that half a physical pixel otherwise we might
21 /// not hit one of the sample positions.
22 static constexpr Scalar kMinStrokeSizeMSAA = 0.5f;
23
24 static constexpr Scalar kMinStrokeSize = 1.0f;
25
26 struct GeometryResult {
27 enum class Mode {
28 /// The geometry has no overlapping triangles.
29 kNormal,
30 /// The geometry may have overlapping triangles. The geometry should be
31 /// stenciled with the NonZero fill rule.
32 kNonZero,
33 /// The geometry may have overlapping triangles. The geometry should be
34 /// stenciled with the EvenOdd fill rule.
35 kEvenOdd,
36 /// The geometry may have overlapping triangles, but they should not
37 /// overdraw or cancel each other out. This is a special case for stroke
38 /// geometry.
39 kPreventOverdraw,
40 };
41
42 PrimitiveType type = PrimitiveType::kTriangleStrip;
43 VertexBuffer vertex_buffer;
44 Matrix transform;
45 Mode mode = Mode::kNormal;
46 };
47
48 static const GeometryResult kEmptyResult = {
49 .vertex_buffer =
50 {
51 .index_type = IndexType::kNone,
52 },
53 };
54
55 class Geometry {
56 public:
57 virtual ~Geometry() {}
58
59 static std::unique_ptr<Geometry> MakeFillPath(
60 const Path& path,
61 std::optional<Rect> inner_rect = std::nullopt);
62
63 static std::unique_ptr<Geometry> MakeStrokePath(
64 const Path& path,
65 Scalar stroke_width = 0.0,
66 Scalar miter_limit = 4.0,
67 Cap stroke_cap = Cap::kButt,
68 Join stroke_join = Join::kMiter);
69
70 static std::unique_ptr<Geometry> MakeCover();
71
72 static std::unique_ptr<Geometry> MakeRect(const Rect& rect);
73
74 static std::unique_ptr<Geometry> MakeOval(const Rect& rect);
75
76 static std::unique_ptr<Geometry> MakeLine(const Point& p0,
77 const Point& p1,
78 Scalar width,
79 Cap cap);
80
81 static std::unique_ptr<Geometry> MakeCircle(const Point& center,
82 Scalar radius);
83
84 static std::unique_ptr<Geometry> MakeStrokedCircle(const Point& center,
85 Scalar radius,
86 Scalar stroke_width);
87
88 static std::unique_ptr<Geometry> MakeRoundRect(const Rect& rect,
89 const Size& radii);
90
91 static std::unique_ptr<Geometry> MakePointField(std::vector<Point> points,
92 Scalar radius,
93 bool round);
94
95 virtual GeometryResult GetPositionBuffer(const ContentContext& renderer,
96 const Entity& entity,
97 RenderPass& pass) const = 0;
98
99 virtual GeometryResult::Mode GetResultMode() const;
100
101 virtual std::optional<Rect> GetCoverage(const Matrix& transform) const = 0;
102
103 /// @brief Compute an alpha value to simulate lower coverage of fractional
104 /// pixel strokes.
105 static Scalar ComputeStrokeAlphaCoverage(const Matrix& entity,
106 Scalar stroke_width);
107
108 /// @brief Determines if this geometry, transformed by the given
109 /// `transform`, will completely cover all surface area of the given
110 /// `rect`.
111 ///
112 /// This is a conservative estimate useful for certain
113 /// optimizations.
114 ///
115 /// @returns `true` if the transformed geometry is guaranteed to cover the
116 /// given `rect`. May return `false` in many undetected cases where
117 /// the transformed geometry does in fact cover the `rect`.
118 virtual bool CoversArea(const Matrix& transform, const Rect& rect) const;
119
120 virtual bool IsAxisAlignedRect() const;
121
122 virtual bool CanApplyMaskFilter() const;
123
124 virtual Scalar ComputeAlphaCoverage(const Matrix& transform) const {
125 return 1.0;
126 }
127
128 protected:
129 static GeometryResult ComputePositionGeometry(
130 const ContentContext& renderer,
131 const Tessellator::VertexGenerator& generator,
132 const Entity& entity,
133 RenderPass& pass);
134 };
135
136 } // namespace impeller
137
138 #endif // FLUTTER_IMPELLER_ENTITY_GEOMETRY_GEOMETRY_H_
impeller::GeometryResult::Mode::kNormal
@ kNormal
The geometry has no overlapping triangles.
impeller::VertexBuffer::index_type
IndexType index_type
Definition: vertex_buffer.h:29
impeller::Geometry::ComputeAlphaCoverage
virtual Scalar ComputeAlphaCoverage(const Matrix &transform) const
Definition: geometry.h:124
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::Geometry::MakeCircle
static std::unique_ptr< Geometry > MakeCircle(const Point &center, Scalar radius)
Definition: geometry.cc:104
vertex_buffer.h
impeller::kMinStrokeSizeMSAA
static constexpr Scalar kMinStrokeSizeMSAA
The minimum stroke size can be less than one physical pixel because of MSAA, but no less that half a ...
Definition: geometry.h:22
entity.h
impeller::Geometry::GetCoverage
virtual std::optional< Rect > GetCoverage(const Matrix &transform) const =0
impeller::VertexBuffer
Definition: vertex_buffer.h:13
impeller::Geometry::~Geometry
virtual ~Geometry()
Definition: geometry.h:57
impeller::Geometry::CanApplyMaskFilter
virtual bool CanApplyMaskFilter() const
Definition: geometry.cc:128
formats.h
impeller::Geometry::MakeOval
static std::unique_ptr< Geometry > MakeOval(const Rect &rect)
Definition: geometry.cc:93
impeller::Geometry::IsAxisAlignedRect
virtual bool IsAxisAlignedRect() const
Definition: geometry.cc:124
impeller::Geometry::GetResultMode
virtual GeometryResult::Mode GetResultMode() const
Definition: geometry.cc:56
impeller::Geometry::CoversArea
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:120
impeller::Cap::kButt
@ kButt
impeller::GeometryResult::Mode::kEvenOdd
@ kEvenOdd
stroke_width
const Scalar stroke_width
Definition: stroke_path_geometry.cc:297
impeller::kMinStrokeSize
static constexpr Scalar kMinStrokeSize
Definition: geometry.h:24
impeller::GeometryResult::Mode
Mode
Definition: geometry.h:27
impeller::Join::kMiter
@ kMiter
impeller::Geometry::ComputePositionGeometry
static GeometryResult ComputePositionGeometry(const ContentContext &renderer, const Tessellator::VertexGenerator &generator, const Entity &entity, RenderPass &pass)
Definition: geometry.cc:24
impeller::Entity
Definition: entity.h:20
impeller::TSize< Scalar >
impeller::Tessellator::VertexGenerator
An object which produces a list of vertices as |Point|s that tessellate a previously provided shape a...
Definition: tessellator.h:91
impeller::PrimitiveType::kTriangleStrip
@ kTriangleStrip
impeller::PrimitiveType
PrimitiveType
Decides how backend draws pixels based on input vertices.
Definition: formats.h:352
render_pass.h
impeller::GeometryResult::type
PrimitiveType type
Definition: geometry.h:42
impeller::Geometry::GetPositionBuffer
virtual GeometryResult GetPositionBuffer(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const =0
impeller::Path
Paths are lightweight objects that describe a collection of linear, quadratic, or cubic segments....
Definition: path.h:52
impeller::Geometry::MakeStrokePath
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:72
transform
Matrix transform
Definition: gaussian_blur_filter_contents.cc:213
impeller::Geometry::MakeStrokedCircle
static std::unique_ptr< Geometry > MakeStrokedCircle(const Point &center, Scalar radius, Scalar stroke_width)
Definition: geometry.cc:109
impeller::GeometryResult
Definition: geometry.h:26
impeller::IndexType::kNone
@ kNone
Does not use the index buffer.
impeller::Geometry::MakeRoundRect
static std::unique_ptr< Geometry > MakeRoundRect(const Rect &rect, const Size &radii)
Definition: geometry.cc:115
impeller::Geometry::MakeCover
static std::unique_ptr< Geometry > MakeCover()
Definition: geometry.cc:85
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:33
impeller::Geometry::MakeRect
static std::unique_ptr< Geometry > MakeRect(const Rect &rect)
Definition: geometry.cc:89
impeller::GeometryResult::transform
Matrix transform
Definition: geometry.h:44
content_context.h
impeller::Geometry
Definition: geometry.h:55
impeller::Geometry::MakePointField
static std::unique_ptr< Geometry > MakePointField(std::vector< Point > points, Scalar radius, bool round)
Definition: geometry.cc:66
impeller::Join
Join
Definition: path.h:24
impeller::kEmptyResult
static const GeometryResult kEmptyResult
Definition: geometry.h:48
impeller::Geometry::MakeLine
static std::unique_ptr< Geometry > MakeLine(const Point &p0, const Point &p1, Scalar width, Cap cap)
Definition: geometry.cc:97
impeller::TPoint
Definition: point.h:27
impeller::Geometry::MakeFillPath
static std::unique_ptr< Geometry > MakeFillPath(const Path &path, std::optional< Rect > inner_rect=std::nullopt)
Definition: geometry.cc:60
impeller::Geometry::ComputeStrokeAlphaCoverage
static Scalar ComputeStrokeAlphaCoverage(const Matrix &entity, Scalar stroke_width)
Compute an alpha value to simulate lower coverage of fractional pixel strokes.
Definition: geometry.cc:133
impeller::GeometryResult::Mode::kPreventOverdraw
@ kPreventOverdraw
impeller::GeometryResult::vertex_buffer
VertexBuffer vertex_buffer
Definition: geometry.h:43
impeller
Definition: allocation.cc:12
impeller::ContentContext
Definition: content_context.h:366
impeller::TRect
Definition: rect.h:122
impeller::GeometryResult::mode
Mode mode
Definition: geometry.h:45
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::Cap
Cap
Definition: path.h:18
impeller::GeometryResult::Mode::kNonZero
@ kNonZero
vertex_buffer_builder.h