| 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 FillPathGeometry::FillPathGeometry(const Path& path,
16 std::optional<Rect> inner_rect)
17 : path_(path), inner_rect_(inner_rect) {}
18
19 FillPathGeometry::~FillPathGeometry() {}
20
21 GeometryResult FillPathGeometry::GetPositionBuffer(
22 const ContentContext& renderer,
23 const Entity& entity,
24 RenderPass& pass) const {
25 auto& host_buffer = renderer.GetTransientsBuffer();
26
27 const auto& bounding_box = path_.GetBoundingBox();
28 if (bounding_box.has_value() && 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 path_, host_buffer, entity.GetTransform().GetMaxBasisLengthXY(),
48 /*supports_primitive_restart=*/supports_primitive_restart,
49 /*supports_triangle_fan=*/supports_triangle_fan);
50
51 return GeometryResult{
52 .type = supports_triangle_fan ? PrimitiveType::kTriangleFan
53 : PrimitiveType::kTriangleStrip,
54 .vertex_buffer = std::move(vertex_buffer),
55 .transform = entity.GetShaderTransform(pass),
56 .mode = GetResultMode(),
57 };
58 }
59
60 GeometryResult::Mode FillPathGeometry::GetResultMode() const {
61 const auto& bounding_box = path_.GetBoundingBox();
62 if (path_.IsConvex() ||
63 (bounding_box.has_value() && bounding_box->IsEmpty())) {
64 return GeometryResult::Mode::kNormal;
65 }
66
67 switch (path_.GetFillType()) {
68 case FillType::kNonZero:
69 return GeometryResult::Mode::kNonZero;
70 case FillType::kOdd:
71 return GeometryResult::Mode::kEvenOdd;
72 }
73
74 FML_UNREACHABLE();
75 }
76
77 std::optional<Rect> FillPathGeometry::GetCoverage(
78 const Matrix& transform) const {
79 return path_.GetTransformedBoundingBox(transform);
80 }
81
82 bool FillPathGeometry::CoversArea(const Matrix& transform,
83 const Rect& rect) const {
84 if (!inner_rect_.has_value()) {
85 return false;
86 }
87 if (!transform.IsTranslationScaleOnly()) {
88 return false;
89 }
90 Rect coverage = inner_rect_->TransformBounds(transform);
91 return coverage.Contains(rect);
92 }
93
94 } // 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:296
impeller::ContentContext::GetDeviceCapabilities
const Capabilities & GetDeviceCapabilities() const
Definition: content_context.cc:917
impeller::ContentContext::GetTessellator
Tessellator & GetTessellator() const
Definition: content_context.cc:909
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::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:44
impeller::FillPathGeometry::~FillPathGeometry
~FillPathGeometry() override
Definition: fill_path_geometry.cc:19
impeller::FillPathGeometry::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:82
impeller::FillPathGeometry::FillPathGeometry
FillPathGeometry(const Path &path, std::optional< Rect > inner_rect=std::nullopt)
Definition: fill_path_geometry.cc:15
impeller::Path
Paths are lightweight objects that describe a collection of linear, quadratic, or cubic segments....
Definition: path.h:54
impeller::Path::GetFillType
FillType GetFillType() const
Definition: path.cc:110
impeller::Path::GetTransformedBoundingBox
std::optional< Rect > GetTransformedBoundingBox(const Matrix &transform) const
Definition: path.cc:470
impeller::Path::IsConvex
bool IsConvex() const
Definition: path.cc:114
impeller::Path::GetBoundingBox
std::optional< Rect > GetBoundingBox() const
Definition: path.cc:466
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 Path &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:40
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::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
constexpr Scalar GetMaxBasisLengthXY() const
Definition: matrix.h:323
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::VertexBuffer
Definition: vertex_buffer.h:15
impeller::VertexBuffer::vertex_buffer
BufferView vertex_buffer
Definition: vertex_buffer.h:16
vertex_buffer.h