| 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/circle_geometry.h"
13 #include "impeller/entity/geometry/cover_geometry.h"
14 #include "impeller/entity/geometry/ellipse_geometry.h"
15 #include "impeller/entity/geometry/fill_path_geometry.h"
16 #include "impeller/entity/geometry/line_geometry.h"
17 #include "impeller/entity/geometry/rect_geometry.h"
18 #include "impeller/entity/geometry/round_rect_geometry.h"
19 #include "impeller/entity/geometry/round_superellipse_geometry.h"
20 #include "impeller/entity/geometry/stroke_path_geometry.h"
21 #include "impeller/geometry/rect.h"
22
23 namespace impeller {
24
25 GeometryResult Geometry::ComputePositionGeometry(
26 const ContentContext& renderer,
27 const Tessellator::VertexGenerator& generator,
28 const Entity& entity,
29 RenderPass& pass) {
30 using VT = SolidFillVertexShader::PerVertexData;
31
32 size_t count = generator.GetVertexCount();
33
34 return GeometryResult{
35 .type = generator.GetTriangleType(),
36 .vertex_buffer =
37 {
38 .vertex_buffer = renderer.GetTransientsBuffer().Emplace(
39 count * sizeof(VT), alignof(VT),
40 [&generator](uint8_t* buffer) {
41 auto vertices = reinterpret_cast<VT*>(buffer);
42 generator.GenerateVertices([&vertices](const Point& p) {
43 *vertices++ = {
44 .position = p,
45 };
46 });
47 FML_DCHECK(vertices == reinterpret_cast<VT*>(buffer) +
48 generator.GetVertexCount());
49 }),
50 .vertex_count = count,
51 .index_type = IndexType::kNone,
52 },
53 .transform = entity.GetShaderTransform(pass),
54 };
55 }
56
57 GeometryResult::Mode Geometry::GetResultMode() const {
58 return GeometryResult::Mode::kNormal;
59 }
60
61 std::unique_ptr<Geometry> Geometry::MakeFillPath(
62 const Path& path,
63 std::optional<Rect> inner_rect) {
64 return std::make_unique<FillPathGeometry>(path, inner_rect);
65 }
66
67 std::unique_ptr<Geometry> Geometry::MakeStrokePath(const Path& path,
68 Scalar stroke_width,
69 Scalar miter_limit,
70 Cap stroke_cap,
71 Join stroke_join) {
72 // Skia behaves like this.
73 if (miter_limit < 0) {
74 miter_limit = 4.0;
75 }
76 return std::make_unique<StrokePathGeometry>(path, stroke_width, miter_limit,
77 stroke_cap, stroke_join);
78 }
79
80 std::unique_ptr<Geometry> Geometry::MakeCover() {
81 return std::make_unique<CoverGeometry>();
82 }
83
84 std::unique_ptr<Geometry> Geometry::MakeRect(const Rect& rect) {
85 return std::make_unique<RectGeometry>(rect);
86 }
87
88 std::unique_ptr<Geometry> Geometry::MakeOval(const Rect& rect) {
89 return std::make_unique<EllipseGeometry>(rect);
90 }
91
92 std::unique_ptr<Geometry> Geometry::MakeLine(const Point& p0,
93 const Point& p1,
94 Scalar width,
95 Cap cap) {
96 return std::make_unique<LineGeometry>(p0, p1, width, cap);
97 }
98
99 std::unique_ptr<Geometry> Geometry::MakeCircle(const Point& center,
100 Scalar radius) {
101 return std::make_unique<CircleGeometry>(center, radius);
102 }
103
104 std::unique_ptr<Geometry> Geometry::MakeStrokedCircle(const Point& center,
105 Scalar radius,
106 Scalar stroke_width) {
107 return std::make_unique<CircleGeometry>(center, radius, stroke_width);
108 }
109
110 std::unique_ptr<Geometry> Geometry::MakeRoundRect(const Rect& rect,
111 const Size& radii) {
112 return std::make_unique<RoundRectGeometry>(rect, radii);
113 }
114
115 std::unique_ptr<Geometry> Geometry::MakeRoundSuperellipse(
116 const Rect& rect,
117 Scalar corner_radius) {
118 return std::make_unique<RoundSuperellipseGeometry>(rect, corner_radius);
119 }
120
121 bool Geometry::CoversArea(const Matrix& transform, const Rect& rect) const {
122 return false;
123 }
124
125 bool Geometry::IsAxisAlignedRect() const {
126 return false;
127 }
128
129 bool Geometry::CanApplyMaskFilter() const {
130 return true;
131 }
132
133 // static
134 Scalar Geometry::ComputeStrokeAlphaCoverage(const Matrix& transform,
135 Scalar stroke_width) {
136 Scalar scaled_stroke_width = transform.GetMaxBasisLengthXY() * stroke_width;
137 if (scaled_stroke_width == 0.0 || scaled_stroke_width >= kMinStrokeSize) {
138 return 1.0;
139 }
140 // This scalling is eyeballed from Skia.
141 return std::clamp(scaled_stroke_width * 2.0f, 0.f, 1.f);
142 }
143
144 } // namespace impeller
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:296
impeller::Entity
Definition: entity.h:20
impeller::Entity::GetShaderTransform
Matrix GetShaderTransform(const RenderPass &pass) const
Get the vertex shader transform used for drawing this Entity.
Definition: entity.cc:48
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:67
impeller::Geometry::MakeFillPath
static std::unique_ptr< Geometry > MakeFillPath(const Path &path, std::optional< Rect > inner_rect=std::nullopt)
Definition: geometry.cc:61
impeller::Geometry::MakeLine
static std::unique_ptr< Geometry > MakeLine(const Point &p0, const Point &p1, Scalar width, Cap cap)
Definition: geometry.cc:92
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:134
impeller::Geometry::GetResultMode
virtual GeometryResult::Mode GetResultMode() const
Definition: geometry.cc:57
impeller::Geometry::MakeRect
static std::unique_ptr< Geometry > MakeRect(const Rect &rect)
Definition: geometry.cc:84
impeller::Geometry::MakeCircle
static std::unique_ptr< Geometry > MakeCircle(const Point &center, Scalar radius)
Definition: geometry.cc:99
impeller::Geometry::CanApplyMaskFilter
virtual bool CanApplyMaskFilter() const
Definition: geometry.cc:129
impeller::Geometry::MakeRoundRect
static std::unique_ptr< Geometry > MakeRoundRect(const Rect &rect, const Size &radii)
Definition: geometry.cc:110
impeller::Geometry::ComputePositionGeometry
static GeometryResult ComputePositionGeometry(const ContentContext &renderer, const Tessellator::VertexGenerator &generator, const Entity &entity, RenderPass &pass)
Definition: geometry.cc:25
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:121
impeller::Geometry::MakeOval
static std::unique_ptr< Geometry > MakeOval(const Rect &rect)
Definition: geometry.cc:88
impeller::Geometry::MakeStrokedCircle
static std::unique_ptr< Geometry > MakeStrokedCircle(const Point &center, Scalar radius, Scalar stroke_width)
Definition: geometry.cc:104
impeller::Geometry::MakeRoundSuperellipse
static std::unique_ptr< Geometry > MakeRoundSuperellipse(const Rect &rect, Scalar corner_radius)
Definition: geometry.cc:115
impeller::Geometry::MakeCover
static std::unique_ptr< Geometry > MakeCover()
Definition: geometry.cc:80
impeller::Geometry::IsAxisAlignedRect
virtual bool IsAxisAlignedRect() const
Definition: geometry.cc:125
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:93
impeller::Path
Paths are lightweight objects that describe a collection of linear, quadratic, or cubic segments....
Definition: path.h:54
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:94
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::IndexType::kNone
@ kNone
Does not use the index buffer.
impeller::Join
Join
Definition: path.h:26
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::Cap
Cap
Definition: path.h:20
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_width
const Scalar stroke_width
Definition: stroke_path_geometry.cc:315
stroke_path_geometry.h
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::TPoint< Scalar >
impeller::TRect< Scalar >
impeller::TSize< Scalar >