| 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& host_buffer = renderer.GetTransientsBuffer();
25
26 const auto& bounding_box = GetSource().GetBounds();
27 if (bounding_box.IsEmpty()) {
28 return GeometryResult{
29 .type = PrimitiveType::kTriangle,
30 .vertex_buffer =
31 VertexBuffer{
32 .vertex_buffer = {},
33 .vertex_count = 0,
34 .index_type = IndexType::k16bit,
35 },
36 .transform = pass.GetOrthographicTransform() * entity.GetTransform(),
37 };
38 }
39
40 bool supports_primitive_restart =
41 renderer.GetDeviceCapabilities().SupportsPrimitiveRestart();
42 bool supports_triangle_fan =
43 renderer.GetDeviceCapabilities().SupportsTriangleFan() &&
44 supports_primitive_restart;
45 VertexBuffer vertex_buffer = renderer.GetTessellator().TessellateConvex(
46 GetSource(), host_buffer, entity.GetTransform().GetMaxBasisLengthXY(),
47 /*supports_primitive_restart=*/supports_primitive_restart,
48 /*supports_triangle_fan=*/supports_triangle_fan);
49
50 return GeometryResult{
51 .type = supports_triangle_fan ? PrimitiveType::kTriangleFan
52 : PrimitiveType::kTriangleStrip,
53 .vertex_buffer = std::move(vertex_buffer),
54 .transform = entity.GetShaderTransform(pass),
55 .mode = GetResultMode(),
56 };
57 }
58
59 GeometryResult::Mode FillPathSourceGeometry::GetResultMode() const {
60 const PathSource& source = GetSource();
61 const auto& bounding_box = source.GetBounds();
62 if (source.IsConvex() || bounding_box.IsEmpty()) {
63 return GeometryResult::Mode::kNormal;
64 }
65
66 switch (source.GetFillType()) {
67 case FillType::kNonZero:
68 return GeometryResult::Mode::kNonZero;
69 case FillType::kOdd:
70 return GeometryResult::Mode::kEvenOdd;
71 }
72
73 FML_UNREACHABLE();
74 }
75
76 std::optional<Rect> FillPathSourceGeometry::GetCoverage(
77 const Matrix& transform) const {
78 return GetSource().GetBounds().TransformAndClipBounds(transform);
79 }
80
81 bool FillPathSourceGeometry::CoversArea(const Matrix& transform,
82 const Rect& rect) const {
83 if (!inner_rect_.has_value()) {
84 return false;
85 }
86 if (!transform.IsTranslationScaleOnly()) {
87 return false;
88 }
89 Rect coverage = inner_rect_->TransformBounds(transform);
90 return coverage.Contains(rect);
91 }
92
93 FillPathGeometry::FillPathGeometry(const flutter::DlPath& path,
94 std::optional<Rect> inner_rect)
95 : FillPathSourceGeometry(inner_rect), path_(path) {}
96
97 const PathSource& FillPathGeometry::GetSource() const {
98 return path_;
99 }
100
101 FillDiffRoundRectGeometry::FillDiffRoundRectGeometry(const RoundRect& outer,
102 const RoundRect& inner)
103 : FillPathSourceGeometry(std::nullopt), source_(outer, inner) {}
104
105 const PathSource& FillDiffRoundRectGeometry::GetSource() const {
106 return source_;
107 }
108
109 } // 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:138
impeller::ContentContext::GetTransientsBuffer
HostBuffer & GetTransientsBuffer() const
Retrieve the currnent host buffer for transient storage.
Definition: content_context.h:297
impeller::ContentContext::GetDeviceCapabilities
const Capabilities & GetDeviceCapabilities() const
Definition: content_context.cc:943
impeller::ContentContext::GetTessellator
Tessellator & GetTessellator() const
Definition: content_context.cc:935
impeller::Entity
Definition: entity.h:26
impeller::Entity::GetShaderTransform
Matrix GetShaderTransform(const RenderPass &pass) const
Definition: entity.cc:48
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:44
impeller::FillDiffRoundRectGeometry::GetSource
const PathSource & GetSource() const override
Definition: fill_path_geometry.cc:105
impeller::FillDiffRoundRectGeometry::FillDiffRoundRectGeometry
FillDiffRoundRectGeometry(const RoundRect &outer, const RoundRect &inner)
Definition: fill_path_geometry.cc:101
impeller::FillPathGeometry::FillPathGeometry
FillPathGeometry(const flutter::DlPath &path, std::optional< Rect > inner_rect=std::nullopt)
Definition: fill_path_geometry.cc:93
impeller::FillPathGeometry::GetSource
const PathSource & GetSource() const override
Definition: fill_path_geometry.cc:97
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:81
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 &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:314
content_context.h
formats.h
fill_path_geometry.h
transform
Matrix transform
Definition: gaussian_blur_filter_contents.cc:214
geometry.h
impeller
Definition: allocation.cc:12
impeller::IndexType::k16bit
@ k16bit
impeller::PrimitiveType::kTriangleFan
@ kTriangleFan
impeller::PrimitiveType::kTriangle
@ kTriangle
impeller::PrimitiveType::kTriangleStrip
@ kTriangleStrip
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
Definition: matrix.h:323
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:235
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:442
impeller::VertexBuffer
Definition: vertex_buffer.h:15
impeller::VertexBuffer::vertex_buffer
BufferView vertex_buffer
Definition: vertex_buffer.h:16
vertex_buffer.h