| Flutter Impeller |

shadow_path_geometry.h
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 #ifndef FLUTTER_IMPELLER_ENTITY_GEOMETRY_SHADOW_PATH_GEOMETRY_H_
6 #define FLUTTER_IMPELLER_ENTITY_GEOMETRY_SHADOW_PATH_GEOMETRY_H_
7
8 #include "flutter/impeller/entity/geometry/geometry.h"
9 #include "flutter/impeller/geometry/path_source.h"
10 #include "flutter/impeller/tessellator/tessellator.h"
11
12 namespace impeller {
13
14 /// A class to hold a vertex mesh for rendering shadows. The vertices are
15 /// each associated with a gaussian coefficent that represents where that
16 /// vertex lives in the shadow from a value of 1.0 (at the edge of or fully
17 /// in the darkest part of the umbra) to 0.0 at the edge of or fully outside
18 /// the penumbra).
19 ///
20 /// The vertices are also associated with a vector of indices that assemble
21 /// them into a mesh that covers the full umbra and penumbra of the shape.
22 ///
23 /// The mesh is usually intended to be rendered at device (pixel) resolution.
24 class ShadowVertices {
25 public:
26 static const std::shared_ptr<ShadowVertices> kEmpty;
27
28 static std::shared_ptr<ShadowVertices> Make(std::vector<Point> vertices,
29 std::vector<uint16_t> indices,
30 std::vector<Scalar> gaussians) {
31 return std::make_shared<ShadowVertices>(
32 std::move(vertices), std::move(indices), std::move(gaussians));
33 }
34
35 constexpr ShadowVertices() {}
36
37 constexpr ShadowVertices(std::vector<Point> vertices,
38 std::vector<uint16_t> indices,
39 std::vector<Scalar> gaussians)
40 : vertices_(std::move(vertices)),
41 indices_(std::move(indices)),
42 gaussians_(std::move(gaussians)) {}
43
44 /// The count of the unique (duplicates minimized) vertices in the mesh.
45 /// This number is also the count of gaussian coefficients in the mesh
46 /// since the two are assigned 1:1.
47 size_t GetVertexCount() const { return vertices_.size(); }
48
49 /// The count of the indices that define the mesh.
50 size_t GetIndexCount() const { return indices_.size(); }
51
52 const std::vector<Point>& GetVertices() const { return vertices_; }
53 const std::vector<uint16_t>& GetIndices() const { return indices_; }
54 const std::vector<Scalar>& GetGaussians() const { return gaussians_; }
55
56 /// True if and only if there was no shadow for the shape and therefore
57 /// no mesh to generate.
58 bool IsEmpty() const { return vertices_.empty(); }
59
60 std::optional<Rect> GetBounds() const;
61
62 GeometryResult GetPositionBuffer(const ContentContext& renderer,
63 const Entity& entity,
64 RenderPass& pass) const;
65
66 private:
67 const std::vector<Point> vertices_;
68 const std::vector<uint16_t> indices_;
69 const std::vector<Scalar> gaussians_;
70 };
71
72 /// A class to compute and return the |ShadowVertices| for a path source
73 /// viewed under a given transform. The |occluder_height| is measured in
74 /// device pixels. The geometry of the |PathSource| is transformed by the
75 /// indicated matrix to produce a device space set of vertices, and the
76 /// shadow mesh is inset and outset by the indicated |occluder_height|
77 /// without any adjustment for the matrix. The results are un-transformed
78 /// and returned back iin the |ShadowVertices| in the original coordinate
79 /// system.
80 class ShadowPathGeometry {
81 public:
82 ShadowPathGeometry(Tessellator& tessellator,
83 const Matrix& matrix,
84 const PathSource& source,
85 Scalar occluder_height);
86
87 bool CanRender() const;
88
89 /// Returns true if this shadow has no effect, is not visible.
90 bool IsEmpty() const;
91
92 /// Returns a reference to the generated vertices, or null if the algorithm
93 /// failed to produce a mesh.
94 const std::shared_ptr<ShadowVertices>& GetShadowVertices() const;
95
96 /// Takes (returns the only copy of via std::move) the shadow vertices
97 /// or null if the algorithm failed to produce a mesh.
98 const std::shared_ptr<ShadowVertices> TakeShadowVertices();
99
100 /// Constructs a shadow mesh for the given |PathSource| at the given
101 /// |matrix| and with the indicated device-space |occluder_height|.
102 /// The tessellator is used to get a cached set of |Trigs| for the
103 /// radii associated with the mesh around various corners in the path.
104 static std::shared_ptr<ShadowVertices> MakeAmbientShadowVertices(
105 Tessellator& tessellator,
106 const PathSource& source,
107 Scalar occluder_height,
108 const Matrix& matrix);
109
110 private:
111 std::shared_ptr<ShadowVertices> shadow_vertices_;
112 };
113
114 } // namespace impeller
115
116 #endif // FLUTTER_IMPELLER_ENTITY_GEOMETRY_SHADOW_PATH_GEOMETRY_H_
impeller::ContentContext
Definition: content_context.h:122
impeller::Entity
Definition: entity.h:26
impeller::PathSource
Definition: path_source.h:55
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:30
impeller::ShadowPathGeometry
Definition: shadow_path_geometry.h:80
impeller::ShadowPathGeometry::GetShadowVertices
const std::shared_ptr< ShadowVertices > & GetShadowVertices() const
Definition: shadow_path_geometry.cc:1406
impeller::ShadowPathGeometry::ShadowPathGeometry
ShadowPathGeometry(Tessellator &tessellator, const Matrix &matrix, const PathSource &source, Scalar occluder_height)
Definition: shadow_path_geometry.cc:1389
impeller::ShadowPathGeometry::CanRender
bool CanRender() const
Definition: shadow_path_geometry.cc:1398
impeller::ShadowPathGeometry::IsEmpty
bool IsEmpty() const
Returns true if this shadow has no effect, is not visible.
Definition: shadow_path_geometry.cc:1402
impeller::ShadowPathGeometry::TakeShadowVertices
const std::shared_ptr< ShadowVertices > TakeShadowVertices()
Definition: shadow_path_geometry.cc:1411
impeller::ShadowPathGeometry::MakeAmbientShadowVertices
static std::shared_ptr< ShadowVertices > MakeAmbientShadowVertices(Tessellator &tessellator, const PathSource &source, Scalar occluder_height, const Matrix &matrix)
Definition: shadow_path_geometry.cc:1454
impeller::ShadowVertices
Definition: shadow_path_geometry.h:24
impeller::ShadowVertices::GetVertexCount
size_t GetVertexCount() const
Definition: shadow_path_geometry.h:47
impeller::ShadowVertices::GetIndices
const std::vector< uint16_t > & GetIndices() const
Definition: shadow_path_geometry.h:53
impeller::ShadowVertices::Make
static std::shared_ptr< ShadowVertices > Make(std::vector< Point > vertices, std::vector< uint16_t > indices, std::vector< Scalar > gaussians)
Definition: shadow_path_geometry.h:28
impeller::ShadowVertices::GetBounds
std::optional< Rect > GetBounds() const
Definition: shadow_path_geometry.cc:1385
impeller::ShadowVertices::kEmpty
static const std::shared_ptr< ShadowVertices > kEmpty
Definition: shadow_path_geometry.h:26
impeller::ShadowVertices::GetGaussians
const std::vector< Scalar > & GetGaussians() const
Definition: shadow_path_geometry.h:54
impeller::ShadowVertices::GetVertices
const std::vector< Point > & GetVertices() const
Definition: shadow_path_geometry.h:52
impeller::ShadowVertices::ShadowVertices
constexpr ShadowVertices(std::vector< Point > vertices, std::vector< uint16_t > indices, std::vector< Scalar > gaussians)
Definition: shadow_path_geometry.h:37
impeller::ShadowVertices::GetPositionBuffer
GeometryResult GetPositionBuffer(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const
Definition: shadow_path_geometry.cc:1415
impeller::ShadowVertices::GetIndexCount
size_t GetIndexCount() const
The count of the indices that define the mesh.
Definition: shadow_path_geometry.h:50
impeller::ShadowVertices::ShadowVertices
constexpr ShadowVertices()
Definition: shadow_path_geometry.h:35
impeller::ShadowVertices::IsEmpty
bool IsEmpty() const
Definition: shadow_path_geometry.h:58
impeller::Tessellator
A utility that generates triangles of the specified fill type given a polyline. This happens on the C...
Definition: tessellator.h:37
geometry.h
impeller
Definition: allocation.cc:12
impeller::Scalar
float Scalar
Definition: scalar.h:19
std
Definition: comparable.h:95
path_source.h
impeller::GeometryResult
Definition: geometry.h:21
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
tessellator.h