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 
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.
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 
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.
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_
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:30
const std::shared_ptr< ShadowVertices > & GetShadowVertices() const
ShadowPathGeometry(Tessellator &tessellator, const Matrix &matrix, const PathSource &source, Scalar occluder_height)
bool IsEmpty() const
Returns true if this shadow has no effect, is not visible.
const std::shared_ptr< ShadowVertices > TakeShadowVertices()
static std::shared_ptr< ShadowVertices > MakeAmbientShadowVertices(Tessellator &tessellator, const PathSource &source, Scalar occluder_height, const Matrix &matrix)
const std::vector< uint16_t > & GetIndices() const
static std::shared_ptr< ShadowVertices > Make(std::vector< Point > vertices, std::vector< uint16_t > indices, std::vector< Scalar > gaussians)
std::optional< Rect > GetBounds() const
static const std::shared_ptr< ShadowVertices > kEmpty
const std::vector< Scalar > & GetGaussians() const
const std::vector< Point > & GetVertices() const
constexpr ShadowVertices(std::vector< Point > vertices, std::vector< uint16_t > indices, std::vector< Scalar > gaussians)
GeometryResult GetPositionBuffer(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const
size_t GetIndexCount() const
The count of the indices that define the mesh.
A utility that generates triangles of the specified fill type given a polyline. This happens on the C...
Definition: tessellator.h:37
float Scalar
Definition: scalar.h:19
Definition: comparable.h:95
A 4x4 matrix using column-major storage.
Definition: matrix.h:37