Flutter Engine
The Flutter Engine
point_field_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
6
10
11namespace impeller {
12
14 Scalar radius,
15 bool round)
16 : points_(std::move(points)), radius_(radius), round_(round) {}
17
18GeometryResult PointFieldGeometry::GetPositionBuffer(
20 const Entity& entity,
21 RenderPass& pass) const {
22 if (radius_ < 0.0) {
23 return {};
24 }
25 Matrix transform = entity.GetTransform();
26 Scalar determinant = transform.GetDeterminant();
27 if (determinant == 0) {
28 return {};
29 }
30
31 Scalar min_size = 1.0f / sqrt(std::abs(determinant));
32 Scalar radius = std::max(radius_, min_size);
33
34 HostBuffer& host_buffer = renderer.GetTransientsBuffer();
35 VertexBufferBuilder<SolidFillVertexShader::PerVertexData> vtx_builder;
36
37 if (round_) {
38 // Get triangulation relative to {0, 0} so we can translate it to each
39 // point in turn.
40 auto generator =
41 renderer.GetTessellator()->FilledCircle(transform, {}, radius);
42 FML_DCHECK(generator.GetTriangleType() == PrimitiveType::kTriangleStrip);
43 std::vector<Point> circle_vertices;
44 circle_vertices.reserve(generator.GetVertexCount());
45 generator.GenerateVertices([&circle_vertices](const Point& p) { //
46 circle_vertices.push_back(p);
47 });
48 FML_DCHECK(circle_vertices.size() == generator.GetVertexCount());
49
50 vtx_builder.Reserve((circle_vertices.size() + 2) * points_.size() - 2);
51 for (auto& center : points_) {
52 if (vtx_builder.HasVertices()) {
53 vtx_builder.AppendVertex(vtx_builder.Last());
54 vtx_builder.AppendVertex({center + circle_vertices[0]});
55 }
56
57 for (auto& vertex : circle_vertices) {
58 vtx_builder.AppendVertex({center + vertex});
59 }
60 }
61 } else {
62 vtx_builder.Reserve(6 * points_.size() - 2);
63 for (auto& point : points_) {
64 auto first = Point(point.x - radius, point.y - radius);
65
66 if (vtx_builder.HasVertices()) {
67 vtx_builder.AppendVertex(vtx_builder.Last());
68 vtx_builder.AppendVertex({first});
69 }
70
71 // Z pattern from UL -> UR -> LL -> LR
72 vtx_builder.AppendVertex({first});
73 vtx_builder.AppendVertex({{point.x + radius, point.y - radius}});
74 vtx_builder.AppendVertex({{point.x - radius, point.y + radius}});
75 vtx_builder.AppendVertex({{point.x + radius, point.y + radius}});
76 }
77 }
78
79 return GeometryResult{
81 .vertex_buffer = vtx_builder.CreateVertexBuffer(host_buffer),
82 .transform = entity.GetShaderTransform(pass),
83 };
84}
85
86// |Geometry|
87std::optional<Rect> PointFieldGeometry::GetCoverage(
88 const Matrix& transform) const {
89 if (points_.size() > 0) {
90 // Doesn't use MakePointBounds as this isn't resilient to points that
91 // all lie along the same axis.
92 auto first = points_.begin();
93 auto last = points_.end();
94 auto left = first->x;
95 auto top = first->y;
96 auto right = first->x;
97 auto bottom = first->y;
98 for (auto it = first + 1; it < last; ++it) {
99 left = std::min(left, it->x);
100 top = std::min(top, it->y);
101 right = std::max(right, it->x);
102 bottom = std::max(bottom, it->y);
103 }
104 auto coverage = Rect::MakeLTRB(left - radius_, top - radius_,
105 right + radius_, bottom + radius_);
106 return coverage.TransformBounds(transform);
107 }
108 return std::nullopt;
109}
110
111} // namespace impeller
static void round(SkPoint *p)
static const int points[]
Matrix GetShaderTransform(const RenderPass &pass) const
Get the vertex shader transform used for drawing this Entity.
Definition: entity.cc:50
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:46
PointFieldGeometry(std::vector< Point > points, Scalar radius, bool round)
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:33
#define FML_DCHECK(condition)
Definition: logging.h:103
static float max(float r, float g, float b)
Definition: hsl.cpp:49
static float min(float r, float g, float b)
Definition: hsl.cpp:48
float Scalar
Definition: scalar.h:18
TPoint< Scalar > Point
Definition: point.h:322
SK_API sk_sp< PrecompileColorFilter > Matrix()
SIN Vec< N, float > abs(const Vec< N, float > &x)
Definition: SkVx.h:707
SIN Vec< N, float > sqrt(const Vec< N, float > &x)
Definition: SkVx.h:706
Definition: ref_ptr.h:256
static SkColor4f transform(SkColor4f c, SkColorSpace *src, SkColorSpace *dst)
Definition: p3.cpp:47
static constexpr TRect MakeLTRB(Type left, Type top, Type right, Type bottom)
Definition: rect.h:129