| 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/geometry/circle_geometry.h"
12 #include "impeller/entity/geometry/cover_geometry.h"
13 #include "impeller/entity/geometry/ellipse_geometry.h"
14 #include "impeller/entity/geometry/fill_path_geometry.h"
15 #include "impeller/entity/geometry/line_geometry.h"
16 #include "impeller/entity/geometry/point_field_geometry.h"
17 #include "impeller/entity/geometry/rect_geometry.h"
18 #include "impeller/entity/geometry/round_rect_geometry.h"
19 #include "impeller/entity/geometry/stroke_path_geometry.h"
20 #include "impeller/geometry/rect.h"
21
22 namespace impeller {
23
24 GeometryResult Geometry::ComputePositionGeometry(
25 const ContentContext& renderer,
26 const Tessellator::VertexGenerator& generator,
27 const Entity& entity,
28 RenderPass& pass) {
29 using VT = SolidFillVertexShader::PerVertexData;
30
31 size_t count = generator.GetVertexCount();
32
33 return GeometryResult{
34 .type = generator.GetTriangleType(),
35 .vertex_buffer =
36 {
37 .vertex_buffer = renderer.GetTransientsBuffer().Emplace(
38 count * sizeof(VT), alignof(VT),
39 [&generator](uint8_t* buffer) {
40 auto vertices = reinterpret_cast<VT*>(buffer);
41 generator.GenerateVertices([&vertices](const Point& p) {
42 *vertices++ = {
43 .position = p,
44 };
45 });
46 FML_DCHECK(vertices == reinterpret_cast<VT*>(buffer) +
47 generator.GetVertexCount());
48 }),
49 .vertex_count = count,
50 .index_type = IndexType::kNone,
51 },
52 .transform = entity.GetShaderTransform(pass),
53 };
54 }
55
56 GeometryResult::Mode Geometry::GetResultMode() const {
57 return GeometryResult::Mode::kNormal;
58 }
59
60 std::unique_ptr<Geometry> Geometry::MakeFillPath(
61 const Path& path,
62 std::optional<Rect> inner_rect) {
63 return std::make_unique<FillPathGeometry>(path, inner_rect);
64 }
65
66 std::unique_ptr<Geometry> Geometry::MakePointField(std::vector<Point> points,
67 Scalar radius,
68 bool round) {
69 return std::make_unique<PointFieldGeometry>(std::move(points), radius, round);
70 }
71
72 std::unique_ptr<Geometry> Geometry::MakeStrokePath(const Path& path,
73 Scalar stroke_width,
74 Scalar miter_limit,
75 Cap stroke_cap,
76 Join stroke_join) {
77 // Skia behaves like this.
78 if (miter_limit < 0) {
79 miter_limit = 4.0;
80 }
81 return std::make_unique<StrokePathGeometry>(path, stroke_width, miter_limit,
82 stroke_cap, stroke_join);
83 }
84
85 std::unique_ptr<Geometry> Geometry::MakeCover() {
86 return std::make_unique<CoverGeometry>();
87 }
88
89 std::unique_ptr<Geometry> Geometry::MakeRect(const Rect& rect) {
90 return std::make_unique<RectGeometry>(rect);
91 }
92
93 std::unique_ptr<Geometry> Geometry::MakeOval(const Rect& rect) {
94 return std::make_unique<EllipseGeometry>(rect);
95 }
96
97 std::unique_ptr<Geometry> Geometry::MakeLine(const Point& p0,
98 const Point& p1,
99 Scalar width,
100 Cap cap) {
101 return std::make_unique<LineGeometry>(p0, p1, width, cap);
102 }
103
104 std::unique_ptr<Geometry> Geometry::MakeCircle(const Point& center,
105 Scalar radius) {
106 return std::make_unique<CircleGeometry>(center, radius);
107 }
108
109 std::unique_ptr<Geometry> Geometry::MakeStrokedCircle(const Point& center,
110 Scalar radius,
111 Scalar stroke_width) {
112 return std::make_unique<CircleGeometry>(center, radius, stroke_width);
113 }
114
115 std::unique_ptr<Geometry> Geometry::MakeRoundRect(const Rect& rect,
116 const Size& radii) {
117 return std::make_unique<RoundRectGeometry>(rect, radii);
118 }
119
120 bool Geometry::CoversArea(const Matrix& transform, const Rect& rect) const {
121 return false;
122 }
123
124 bool Geometry::IsAxisAlignedRect() const {
125 return false;
126 }
127
128 bool Geometry::CanApplyMaskFilter() const {
129 return true;
130 }
131
132 // static
133 Scalar Geometry::ComputeStrokeAlphaCoverage(const Matrix& transform,
134 Scalar stroke_width) {
135 Scalar scaled_stroke_width = transform.GetMaxBasisLengthXY() * stroke_width;
136 // If the stroke width is 0 or greater than kMinStrokeSizeMSAA, don't apply
137 // any additional alpha. This is intended to match Skia behavior.
138 if (scaled_stroke_width == 0.0 || scaled_stroke_width >= kMinStrokeSizeMSAA) {
139 return 1.0;
140 }
141 // This scalling is eyeballed from Skia.
142 return std::clamp(scaled_stroke_width * 2.0f, 0.f, 1.f);
143 }
144
145 } // namespace impeller
impeller::GeometryResult::Mode::kNormal
@ kNormal
The geometry has no overlapping triangles.
impeller::Entity::GetShaderTransform
Matrix GetShaderTransform(const RenderPass &pass) const
Get the vertex shader transform used for drawing this Entity.
Definition: entity.cc:51
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:95
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
geometry.h
stroke_path_geometry.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
cover_geometry.h
point_field_geometry.h
impeller::Geometry::CanApplyMaskFilter
virtual bool CanApplyMaskFilter() const
Definition: geometry.cc:128
impeller::Geometry::MakeOval
static std::unique_ptr< Geometry > MakeOval(const Rect &rect)
Definition: geometry.cc:93
rect_geometry.h
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::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...
stroke_width
const Scalar stroke_width
Definition: stroke_path_geometry.cc:297
impeller::GeometryResult::Mode
Mode
Definition: geometry.h:27
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::GeometryResult::type
PrimitiveType type
Definition: geometry.h:42
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.
ellipse_geometry.h
impeller::Geometry::MakeRoundRect
static std::unique_ptr< Geometry > MakeRoundRect(const Rect &rect, const Size &radii)
Definition: geometry.cc:115
fill_path_geometry.h
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
content_context.h
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::Geometry::MakeLine
static std::unique_ptr< Geometry > MakeLine(const Point &p0, const Point &p1, Scalar width, Cap cap)
Definition: geometry.cc:97
rect.h
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::TPoint< Scalar >
impeller::Geometry::MakeFillPath
static std::unique_ptr< Geometry > MakeFillPath(const Path &path, std::optional< Rect > inner_rect=std::nullopt)
Definition: geometry.cc:60
impeller::Tessellator::VertexGenerator::GetVertexCount
virtual size_t GetVertexCount() const =0
Returns the number of vertices that the generator plans to produce, if known.
line_geometry.h
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
circle_geometry.h
round_rect_geometry.h
impeller
Definition: allocation.cc:12
impeller::ContentContext
Definition: content_context.h:366
impeller::TRect< Scalar >
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::ContentContext::GetTransientsBuffer
HostBuffer & GetTransientsBuffer() const
Retrieve the currnent host buffer for transient storage.
Definition: content_context.h:753
impeller::Cap
Cap
Definition: path.h:18