Flutter Engine
The Flutter Engine
line_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
7namespace impeller {
8
10 : p0_(p0), p1_(p1), width_(width), cap_(cap) {
11 FML_DCHECK(width >= 0);
12}
13
15 Scalar width) {
16 auto determinant = transform.GetDeterminant();
17 if (determinant == 0) {
18 return 0.0f;
19 }
20
21 Scalar min_size = 1.0f / sqrt(std::abs(determinant));
22 return std::max(width, min_size) * 0.5f;
23}
24
25Vector2 LineGeometry::ComputeAlongVector(const Matrix& transform,
26 bool allow_zero_length) const {
27 Scalar stroke_half_width = ComputePixelHalfWidth(transform, width_);
28 if (stroke_half_width < kEhCloseEnough) {
29 return {};
30 }
31
32 auto along = p1_ - p0_;
33 Scalar length = along.GetLength();
34 if (length < kEhCloseEnough) {
35 if (!allow_zero_length) {
36 // We won't enclose any pixels unless the endpoints are extended
37 return {};
38 }
39 return {stroke_half_width, 0};
40 } else {
41 return along * stroke_half_width / length;
42 }
43}
44
45bool LineGeometry::ComputeCorners(Point corners[4],
46 const Matrix& transform,
47 bool extend_endpoints) const {
48 auto along = ComputeAlongVector(transform, extend_endpoints);
49 if (along.IsZero()) {
50 return false;
51 }
52
53 auto across = Vector2(along.y, -along.x);
54 corners[0] = p0_ - across;
55 corners[1] = p1_ - across;
56 corners[2] = p0_ + across;
57 corners[3] = p1_ + across;
58 if (extend_endpoints) {
59 corners[0] -= along;
60 corners[1] += along;
61 corners[2] -= along;
62 corners[3] += along;
63 }
64 return true;
65}
66
67GeometryResult LineGeometry::GetPositionBuffer(const ContentContext& renderer,
68 const Entity& entity,
69 RenderPass& pass) const {
70 using VT = SolidFillVertexShader::PerVertexData;
71
72 auto& transform = entity.GetTransform();
73 auto radius = ComputePixelHalfWidth(transform, width_);
74
75 if (cap_ == Cap::kRound) {
76 std::shared_ptr<Tessellator> tessellator = renderer.GetTessellator();
77 auto generator = tessellator->RoundCapLine(transform, p0_, p1_, radius);
78 return ComputePositionGeometry(renderer, generator, entity, pass);
79 }
80
81 Point corners[4];
82 if (!ComputeCorners(corners, transform, cap_ == Cap::kSquare)) {
83 return kEmptyResult;
84 }
85
86 auto& host_buffer = renderer.GetTransientsBuffer();
87
88 size_t count = 4;
89 BufferView vertex_buffer = host_buffer.Emplace(
90 count * sizeof(VT), alignof(VT), [&corners](uint8_t* buffer) {
91 auto vertices = reinterpret_cast<VT*>(buffer);
92 for (auto& corner : corners) {
93 *vertices++ = {
94 .position = corner,
95 };
96 }
97 });
98
99 return GeometryResult{
101 .vertex_buffer =
102 {
103 .vertex_buffer = vertex_buffer,
104 .vertex_count = count,
105 .index_type = IndexType::kNone,
106 },
107 .transform = entity.GetShaderTransform(pass),
108 };
109}
110
111std::optional<Rect> LineGeometry::GetCoverage(const Matrix& transform) const {
112 Point corners[4];
113 if (!ComputeCorners(corners, transform, cap_ != Cap::kButt)) {
114 return {};
115 }
116
117 for (int i = 0; i < 4; i++) {
118 corners[i] = transform * corners[i];
119 }
120 return Rect::MakePointBounds(std::begin(corners), std::end(corners));
121}
122
124 if (!transform.IsTranslationScaleOnly() || !IsAxisAlignedRect()) {
125 return false;
126 }
127 auto coverage = GetCoverage(transform);
128 return coverage.has_value() ? coverage->Contains(rect) : false;
129}
130
132 return cap_ != Cap::kRound && (p0_.x == p1_.x || p0_.y == p1_.y);
133}
134
135} // namespace impeller
int count
Definition: FontMgrTest.cpp:50
static GeometryResult ComputePositionGeometry(const ContentContext &renderer, const Tessellator::VertexGenerator &generator, const Entity &entity, RenderPass &pass)
Definition: geometry.cc:24
LineGeometry(Point p0, Point p1, Scalar width, Cap cap)
Definition: line_geometry.cc:9
static Scalar ComputePixelHalfWidth(const Matrix &transform, Scalar width)
bool IsAxisAlignedRect() const override
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...
static const char * begin(const StringSlice &s)
Definition: editor.cpp:252
#define FML_DCHECK(condition)
Definition: logging.h:103
static float max(float r, float g, float b)
Definition: hsl.cpp:49
size_t length
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
@ kNone
Does not use the index buffer.
Point Vector2
Definition: point.h:326
float Scalar
Definition: scalar.h:18
constexpr float kEhCloseEnough
Definition: constants.h:56
TPoint< Scalar > Point
Definition: point.h:322
Cap
Definition: path.h:18
static const GeometryResult kEmptyResult
Definition: geometry.h:41
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
static SkColor4f transform(SkColor4f c, SkColorSpace *src, SkColorSpace *dst)
Definition: p3.cpp:47
int32_t width
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
static constexpr std::optional< TRect > MakePointBounds(const U &value)
Definition: rect.h:151