| 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
5 #include "impeller/entity/geometry/geometry.h"
6
7 #include <memory>
8 #include <optional>
9
10 #include "impeller/entity/contents/content_context.h"
11 #include "impeller/entity/contents/pipelines.h"
12 #include "impeller/entity/geometry/arc_geometry.h"
13 #include "impeller/entity/geometry/circle_geometry.h"
14 #include "impeller/entity/geometry/cover_geometry.h"
15 #include "impeller/entity/geometry/ellipse_geometry.h"
16 #include "impeller/entity/geometry/fill_path_geometry.h"
17 #include "impeller/entity/geometry/line_geometry.h"
18 #include "impeller/entity/geometry/rect_geometry.h"
19 #include "impeller/entity/geometry/round_rect_geometry.h"
20 #include "impeller/entity/geometry/round_superellipse_geometry.h"
21 #include "impeller/entity/geometry/stroke_path_geometry.h"
22 #include "impeller/geometry/rect.h"
23
24 namespace impeller {
25
26 GeometryResult Geometry::ComputePositionGeometry(
27 const ContentContext& renderer,
28 const Tessellator::VertexGenerator& generator,
29 const Entity& entity,
30 RenderPass& pass) {
31 using VT = SolidFillVertexShader::PerVertexData;
32
33 size_t count = generator.GetVertexCount();
34
35 return GeometryResult{
36 .type = generator.GetTriangleType(),
37 .vertex_buffer =
38 {
39 .vertex_buffer = renderer.GetTransientsBuffer().Emplace(
40 count * sizeof(VT), alignof(VT),
41 [&generator](uint8_t* buffer) {
42 auto vertices = reinterpret_cast<VT*>(buffer);
43 generator.GenerateVertices([&vertices](const Point& p) {
44 *vertices++ = {
45 .position = p,
46 };
47 });
48 FML_DCHECK(vertices == reinterpret_cast<VT*>(buffer) +
49 generator.GetVertexCount());
50 }),
51 .vertex_count = count,
52 .index_type = IndexType::kNone,
53 },
54 .transform = entity.GetShaderTransform(pass),
55 };
56 }
57
58 GeometryResult::Mode Geometry::GetResultMode() const {
59 return GeometryResult::Mode::kNormal;
60 }
61
62 std::unique_ptr<Geometry> Geometry::MakeFillPath(
63 const flutter::DlPath& path,
64 std::optional<Rect> inner_rect) {
65 return std::make_unique<FillPathGeometry>(path, inner_rect);
66 }
67
68 std::unique_ptr<Geometry> Geometry::MakeStrokePath(
69 const flutter::DlPath& path,
70 const StrokeParameters& stroke) {
71 // Skia behaves like this.
72 StrokeParameters parameters = stroke;
73 if (parameters.miter_limit < 0) {
74 parameters.miter_limit = 4.0;
75 }
76 return std::make_unique<StrokePathGeometry>(path, parameters);
77 }
78
79 std::unique_ptr<Geometry> Geometry::MakeCover() {
80 return std::make_unique<CoverGeometry>();
81 }
82
83 std::unique_ptr<Geometry> Geometry::MakeRect(const Rect& rect) {
84 return std::make_unique<FillRectGeometry>(rect);
85 }
86
87 std::unique_ptr<Geometry> Geometry::MakeOval(const Rect& rect) {
88 return std::make_unique<EllipseGeometry>(rect);
89 }
90
91 std::unique_ptr<Geometry> Geometry::MakeLine(const Point& p0,
92 const Point& p1,
93 const StrokeParameters& stroke) {
94 return std::make_unique<LineGeometry>(p0, p1, stroke);
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,
104 Scalar stroke_width) {
105 return std::make_unique<CircleGeometry>(center, radius, stroke_width);
106 }
107
108 std::unique_ptr<Geometry> Geometry::MakeFilledArc(const Rect& oval_bounds,
109 Degrees start,
110 Degrees sweep,
111 bool include_center) {
112 return std::make_unique<ArcGeometry>(
113 Arc(oval_bounds, start, sweep, include_center));
114 }
115
116 std::unique_ptr<Geometry> Geometry::MakeStrokedArc(
117 const Rect& oval_bounds,
118 Degrees start,
119 Degrees sweep,
120 const StrokeParameters& stroke) {
121 return std::make_unique<ArcGeometry>(Arc(oval_bounds, start, sweep, false),
122 stroke);
123 }
124
125 std::unique_ptr<Geometry> Geometry::MakeRoundRect(const Rect& rect,
126 const Size& radii) {
127 return std::make_unique<RoundRectGeometry>(rect, radii);
128 }
129
130 std::unique_ptr<Geometry> Geometry::MakeRoundSuperellipse(
131 const Rect& rect,
132 Scalar corner_radius) {
133 return std::make_unique<RoundSuperellipseGeometry>(rect, corner_radius);
134 }
135
136 bool Geometry::CoversArea(const Matrix& transform, const Rect& rect) const {
137 return false;
138 }
139
140 bool Geometry::IsAxisAlignedRect() const {
141 return false;
142 }
143
144 bool Geometry::CanApplyMaskFilter() const {
145 return true;
146 }
147
148 // static
149 Scalar Geometry::ComputeStrokeAlphaCoverage(const Matrix& transform,
150 Scalar stroke_width) {
151 Scalar scaled_stroke_width = transform.GetMaxBasisLengthXY() * stroke_width;
152 if (scaled_stroke_width == 0.0 || scaled_stroke_width >= kMinStrokeSize) {
153 return 1.0;
154 }
155 // This scalling is eyeballed from Skia.
156 return std::clamp(scaled_stroke_width * 2.0f, 0.f, 1.f);
157 }
158
159 } // namespace impeller
arc_geometry.h
circle_geometry.h
impeller::ContentContext
Definition: content_context.h:138
impeller::ContentContext::GetTransientsBuffer
HostBuffer & GetTransientsBuffer() const
Retrieve the currnent host buffer for transient storage.
Definition: content_context.h:297
impeller::Entity
Definition: entity.h:26
impeller::Entity::GetShaderTransform
Matrix GetShaderTransform(const RenderPass &pass) const
Definition: entity.cc:48
impeller::Geometry::MakeFillPath
static std::unique_ptr< Geometry > MakeFillPath(const flutter::DlPath &path, std::optional< Rect > inner_rect=std::nullopt)
Definition: geometry.cc:62
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:149
impeller::Geometry::GetResultMode
virtual GeometryResult::Mode GetResultMode() const
Definition: geometry.cc:58
impeller::Geometry::MakeRect
static std::unique_ptr< Geometry > MakeRect(const Rect &rect)
Definition: geometry.cc:83
impeller::Geometry::MakeFilledArc
static std::unique_ptr< Geometry > MakeFilledArc(const Rect &oval_bounds, Degrees start, Degrees sweep, bool include_center)
Definition: geometry.cc:108
impeller::Geometry::MakeStrokePath
static std::unique_ptr< Geometry > MakeStrokePath(const flutter::DlPath &path, const StrokeParameters &stroke={})
Definition: geometry.cc:68
impeller::Geometry::MakeCircle
static std::unique_ptr< Geometry > MakeCircle(const Point &center, Scalar radius)
Definition: geometry.cc:97
impeller::Geometry::CanApplyMaskFilter
virtual bool CanApplyMaskFilter() const
Definition: geometry.cc:144
impeller::Geometry::MakeRoundRect
static std::unique_ptr< Geometry > MakeRoundRect(const Rect &rect, const Size &radii)
Definition: geometry.cc:125
impeller::Geometry::ComputePositionGeometry
static GeometryResult ComputePositionGeometry(const ContentContext &renderer, const Tessellator::VertexGenerator &generator, const Entity &entity, RenderPass &pass)
Definition: geometry.cc:26
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:136
impeller::Geometry::MakeLine
static std::unique_ptr< Geometry > MakeLine(const Point &p0, const Point &p1, const StrokeParameters &stroke)
Definition: geometry.cc:91
impeller::Geometry::MakeOval
static std::unique_ptr< Geometry > MakeOval(const Rect &rect)
Definition: geometry.cc:87
impeller::Geometry::MakeStrokedCircle
static std::unique_ptr< Geometry > MakeStrokedCircle(const Point &center, Scalar radius, Scalar stroke_width)
Definition: geometry.cc:102
impeller::Geometry::MakeRoundSuperellipse
static std::unique_ptr< Geometry > MakeRoundSuperellipse(const Rect &rect, Scalar corner_radius)
Definition: geometry.cc:130
impeller::Geometry::MakeCover
static std::unique_ptr< Geometry > MakeCover()
Definition: geometry.cc:79
impeller::Geometry::MakeStrokedArc
static std::unique_ptr< Geometry > MakeStrokedArc(const Rect &oval_bounds, Degrees start, Degrees sweep, const StrokeParameters &stroke)
Definition: geometry.cc:116
impeller::Geometry::IsAxisAlignedRect
virtual bool IsAxisAlignedRect() const
Definition: geometry.cc:140
impeller::HostBuffer::Emplace
BufferView Emplace(const BufferType &buffer, size_t alignment=0)
Emplace non-uniform data (like contiguous vertices) onto the host buffer.
Definition: host_buffer.h:92
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:30
impeller::Tessellator::VertexGenerator
An object which produces a list of vertices as |Point|s that tessellate a previously provided shape a...
Definition: tessellator.h:110
impeller::Tessellator::VertexGenerator::GetVertexCount
virtual size_t GetVertexCount() const =0
Returns the number of vertices that the generator plans to produce, if known.
impeller::Tessellator::VertexGenerator::GetTriangleType
virtual PrimitiveType GetTriangleType() const =0
Returns the |PrimitiveType| that describes the relationship among the list of vertices produced by th...
impeller::Tessellator::VertexGenerator::GenerateVertices
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...
content_context.h
cover_geometry.h
ellipse_geometry.h
fill_path_geometry.h
transform
Matrix transform
Definition: gaussian_blur_filter_contents.cc:214
geometry.h
line_geometry.h
impeller
Definition: allocation.cc:12
impeller::Scalar
float Scalar
Definition: scalar.h:19
impeller::IndexType::kNone
@ kNone
Does not use the index buffer.
impeller::DlPath
flutter::DlPath DlPath
Definition: dl_dispatcher.h:29
impeller::kMinStrokeSize
static constexpr Scalar kMinStrokeSize
Definition: geometry.h:19
pipelines.h
rect.h
rect_geometry.h
round_rect_geometry.h
round_superellipse_geometry.h
stroke_path_geometry.h
impeller::Arc
Definition: arc.h:13
impeller::Degrees
Definition: scalar.h:76
impeller::GeometryResult
Definition: geometry.h:21
impeller::GeometryResult::type
PrimitiveType type
Definition: geometry.h:37
impeller::GeometryResult::Mode
Mode
Definition: geometry.h:22
impeller::GeometryResult::Mode::kNormal
@ kNormal
The geometry has no overlapping triangles.
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::StrokeParameters
A structure to store all of the parameters related to stroking a path or basic geometry object.
Definition: stroke_parameters.h:28
impeller::StrokeParameters::miter_limit
Scalar miter_limit
Definition: stroke_parameters.h:32
impeller::TPoint< Scalar >
impeller::TRect< Scalar >
impeller::TSize< Scalar >
start
const size_t start
Definition: tessellator_libtess.cc:62