| Flutter Impeller |

fill_path_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/fill_path_geometry.h"
6
7 #include "fml/logging.h"
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/geometry/geometry.h"
12
13 namespace impeller {
14
15 FillPathSourceGeometry::FillPathSourceGeometry(std::optional<Rect> inner_rect)
16 : inner_rect_(inner_rect) {}
17
18 FillPathSourceGeometry::~FillPathSourceGeometry() {}
19
20 GeometryResult FillPathSourceGeometry::GetPositionBuffer(
21 const ContentContext& renderer,
22 const Entity& entity,
23 RenderPass& pass) const {
24 auto& data_host_buffer = renderer.GetTransientsDataBuffer();
25 auto& indexes_host_buffer = renderer.GetTransientsIndexesBuffer();
26
27 const auto& bounding_box = GetSource().GetBounds();
28 if (bounding_box.IsEmpty()) {
29 return GeometryResult{
30 .type = PrimitiveType::kTriangle,
31 .vertex_buffer =
32 VertexBuffer{
33 .vertex_buffer = {},
34 .vertex_count = 0,
35 .index_type = IndexType::k16bit,
36 },
37 .transform = pass.GetOrthographicTransform() * entity.GetTransform(),
38 };
39 }
40
41 bool supports_primitive_restart =
42 renderer.GetDeviceCapabilities().SupportsPrimitiveRestart();
43 bool supports_triangle_fan =
44 renderer.GetDeviceCapabilities().SupportsTriangleFan() &&
45 supports_primitive_restart;
46 VertexBuffer vertex_buffer = renderer.GetTessellator().TessellateConvex(
47 GetSource(), data_host_buffer, indexes_host_buffer,
48 entity.GetTransform().GetMaxBasisLengthXY(),
49 /*supports_primitive_restart=*/supports_primitive_restart,
50 /*supports_triangle_fan=*/supports_triangle_fan);
51
52 return GeometryResult{
53 .type = supports_triangle_fan ? PrimitiveType::kTriangleFan
54 : PrimitiveType::kTriangleStrip,
55 .vertex_buffer = std::move(vertex_buffer),
56 .transform = entity.GetShaderTransform(pass),
57 .mode = GetResultMode(),
58 };
59 }
60
61 GeometryResult::Mode FillPathSourceGeometry::GetResultMode() const {
62 const PathSource& source = GetSource();
63 const auto& bounding_box = source.GetBounds();
64 if (source.IsConvex() || bounding_box.IsEmpty()) {
65 return GeometryResult::Mode::kNormal;
66 }
67
68 switch (source.GetFillType()) {
69 case FillType::kNonZero:
70 return GeometryResult::Mode::kNonZero;
71 case FillType::kOdd:
72 return GeometryResult::Mode::kEvenOdd;
73 }
74
75 FML_UNREACHABLE();
76 }
77
78 std::optional<Rect> FillPathSourceGeometry::GetCoverage(
79 const Matrix& transform) const {
80 return GetSource().GetBounds().TransformAndClipBounds(transform);
81 }
82
83 bool FillPathSourceGeometry::CoversArea(const Matrix& transform,
84 const Rect& rect) const {
85 if (!inner_rect_.has_value()) {
86 return false;
87 }
88 if (!transform.IsTranslationScaleOnly()) {
89 return false;
90 }
91 Rect coverage = inner_rect_->TransformBounds(transform);
92 return coverage.Contains(rect);
93 }
94
95 FillPathFromSourceGeometry::FillPathFromSourceGeometry(const PathSource& source)
96 : FillPathSourceGeometry(std::nullopt), source_(source) {}
97
98 const PathSource& FillPathFromSourceGeometry::GetSource() const {
99 return source_;
100 }
101
102 FillPathGeometry::FillPathGeometry(const flutter::DlPath& path,
103 std::optional<Rect> inner_rect)
104 : FillPathSourceGeometry(inner_rect), path_(path) {}
105
106 const PathSource& FillPathGeometry::GetSource() const {
107 return path_;
108 }
109
110 FillDiffRoundRectGeometry::FillDiffRoundRectGeometry(const RoundRect& outer,
111 const RoundRect& inner)
112 : FillPathSourceGeometry(std::nullopt), source_(outer, inner) {}
113
114 const PathSource& FillDiffRoundRectGeometry::GetSource() const {
115 return source_;
116 }
117
118 } // namespace impeller
impeller::Capabilities::SupportsTriangleFan
virtual bool SupportsTriangleFan() const =0
Whether the primitive type TriangleFan is supported by the backend.
impeller::Capabilities::SupportsPrimitiveRestart
virtual bool SupportsPrimitiveRestart() const =0
Whether primitive restart is supported.
impeller::ContentContext
Definition: content_context.h:122
impeller::ContentContext::GetDeviceCapabilities
const Capabilities & GetDeviceCapabilities() const
Definition: content_context.cc:973
impeller::ContentContext::GetTransientsIndexesBuffer
HostBuffer & GetTransientsIndexesBuffer() const
Retrieve the current host buffer for transient storage of indexes used for indexed draws.
Definition: content_context.h:290
impeller::ContentContext::GetTransientsDataBuffer
HostBuffer & GetTransientsDataBuffer() const
Retrieve the current host buffer for transient storage of other non-index data.
Definition: content_context.h:299
impeller::ContentContext::GetTessellator
Tessellator & GetTessellator() const
Definition: content_context.cc:965
impeller::Entity
Definition: entity.h:26
impeller::Entity::GetShaderTransform
Matrix GetShaderTransform(const RenderPass &pass) const
Definition: entity.cc:50
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:46
impeller::FillDiffRoundRectGeometry::GetSource
const PathSource & GetSource() const override
Definition: fill_path_geometry.cc:114
impeller::FillDiffRoundRectGeometry::FillDiffRoundRectGeometry
FillDiffRoundRectGeometry(const RoundRect &outer, const RoundRect &inner)
Definition: fill_path_geometry.cc:110
impeller::FillPathFromSourceGeometry::FillPathFromSourceGeometry
FillPathFromSourceGeometry(const PathSource &source)
Definition: fill_path_geometry.cc:95
impeller::FillPathFromSourceGeometry::GetSource
const PathSource & GetSource() const override
Definition: fill_path_geometry.cc:98
impeller::FillPathGeometry::FillPathGeometry
FillPathGeometry(const flutter::DlPath &path, std::optional< Rect > inner_rect=std::nullopt)
Definition: fill_path_geometry.cc:102
impeller::FillPathGeometry::GetSource
const PathSource & GetSource() const override
Definition: fill_path_geometry.cc:106
impeller::FillPathSourceGeometry
An abstract Geometry base class that produces fillable vertices for the interior of any |PathSource| ...
Definition: fill_path_geometry.h:19
impeller::FillPathSourceGeometry::FillPathSourceGeometry
FillPathSourceGeometry(std::optional< Rect > inner_rect)
Definition: fill_path_geometry.cc:15
impeller::FillPathSourceGeometry::CoversArea
bool CoversArea(const Matrix &transform, const Rect &rect) const override
Determines if this geometry, transformed by the given transform, will completely cover all surface ar...
Definition: fill_path_geometry.cc:83
impeller::FillPathSourceGeometry::GetSource
virtual const PathSource & GetSource() const =0
impeller::FillPathSourceGeometry::~FillPathSourceGeometry
~FillPathSourceGeometry() override
Definition: fill_path_geometry.cc:18
impeller::PathSource
Definition: path_source.h:55
impeller::PathSource::GetBounds
virtual Rect GetBounds() const =0
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:30
impeller::RenderPass::GetOrthographicTransform
const Matrix & GetOrthographicTransform() const
Definition: render_pass.cc:51
impeller::Tessellator::TessellateConvex
VertexBuffer TessellateConvex(const PathSource &path, HostBuffer &data_host_buffer, HostBuffer &indexes_host_buffer, Scalar tolerance, bool supports_primitive_restart=false, bool supports_triangle_fan=false)
Given a convex path, create a triangle fan structure.
Definition: tessellator.cc:429
content_context.h
formats.h
fill_path_geometry.h
transform
Matrix transform
Definition: gaussian_blur_filter_contents.cc:312
geometry.h
impeller
Definition: allocation.cc:12
impeller::PrimitiveType::kTriangleFan
@ kTriangleFan
impeller::PrimitiveType::kTriangle
@ kTriangle
impeller::PrimitiveType::kTriangleStrip
@ kTriangleStrip
impeller::IndexType::k16bit
@ k16bit
impeller::FillType::kOdd
@ kOdd
impeller::FillType::kNonZero
@ kNonZero
impeller::DlPath
flutter::DlPath DlPath
Definition: dl_dispatcher.h:29
std
Definition: comparable.h:95
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::GeometryResult::Mode::kEvenOdd
@ kEvenOdd
impeller::GeometryResult::Mode::kNonZero
@ kNonZero
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::Matrix::GetMaxBasisLengthXY
Scalar GetMaxBasisLengthXY() const
Return the maximum scale applied specifically to either the X axis or Y axis unit vectors (the bases)...
Definition: matrix.h:328
impeller::RoundRect
Definition: round_rect.h:16
impeller::TRect< Scalar >
impeller::TRect::Contains
constexpr bool Contains(const TPoint< Type > &p) const
Returns true iff the provided point |p| is inside the half-open interior of this rectangle.
Definition: rect.h:231
impeller::TRect::TransformAndClipBounds
constexpr TRect TransformAndClipBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle, clipped against the near clippin...
Definition: rect.h:438
impeller::VertexBuffer
Definition: vertex_buffer.h:15
impeller::VertexBuffer::vertex_buffer
BufferView vertex_buffer
Definition: vertex_buffer.h:16
vertex_buffer.h