| Flutter Impeller |

round_rect_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 "flutter/impeller/entity/geometry/round_rect_geometry.h"
6
7 namespace impeller {
8
9 RoundRectGeometry::RoundRectGeometry(const Rect& bounds, const Size& radii)
10 : bounds_(bounds), radii_(radii) {}
11
12 RoundRectGeometry::~RoundRectGeometry() = default;
13
14 GeometryResult RoundRectGeometry::GetPositionBuffer(
15 const ContentContext& renderer,
16 const Entity& entity,
17 RenderPass& pass) const {
18 return ComputePositionGeometry(renderer,
19 renderer.GetTessellator().FilledRoundRect(
20 entity.GetTransform(), bounds_, radii_),
21 entity, pass);
22 }
23
24 std::optional<Rect> RoundRectGeometry::GetCoverage(
25 const Matrix& transform) const {
26 return bounds_.TransformBounds(transform);
27 }
28
29 bool RoundRectGeometry::CoversArea(const Matrix& transform,
30 const Rect& rect) const {
31 if (!transform.IsTranslationScaleOnly()) {
32 return false;
33 }
34 bool flat_on_tb = bounds_.GetWidth() > radii_.width * 2;
35 bool flat_on_lr = bounds_.GetHeight() > radii_.height * 2;
36 if (!flat_on_tb && !flat_on_lr) {
37 return false;
38 }
39 // We either transform the bounds and delta-transform the radii,
40 // or we compute the vertical and horizontal bounds and then
41 // transform each. Either way there are 2 transform operations.
42 // We could also get a weaker answer by computing just the
43 // "inner rect" and only doing a coverage analysis on that,
44 // but this process will produce more culling results.
45 if (flat_on_tb) {
46 Rect vertical_bounds = bounds_.Expand(Size{-radii_.width, 0});
47 Rect coverage = vertical_bounds.TransformBounds(transform);
48 if (coverage.Contains(rect)) {
49 return true;
50 }
51 }
52 if (flat_on_lr) {
53 Rect horizontal_bounds = bounds_.Expand(Size{0, -radii_.height});
54 Rect coverage = horizontal_bounds.TransformBounds(transform);
55 if (coverage.Contains(rect)) {
56 return true;
57 }
58 }
59 return false;
60 }
61
62 bool RoundRectGeometry::IsAxisAlignedRect() const {
63 return false;
64 }
65
66 FillRoundRectGeometry::FillRoundRectGeometry(const RoundRect& round_rect)
67 : FillPathSourceGeometry(std::nullopt), round_rect_source_(round_rect) {}
68
69 const PathSource& FillRoundRectGeometry::GetSource() const {
70 return round_rect_source_;
71 }
72
73 bool FillRoundRectGeometry::CoversArea(const Matrix& transform,
74 const Rect& rect) const {
75 // Similar procedure to |RoundRectGeometry| except that we have different
76 // radii at every corner.
77 if (!transform.IsTranslationScaleOnly()) {
78 return false;
79 }
80 const RoundRect& round_rect = round_rect_source_.GetRoundRect();
81
82 const Rect& bounds = round_rect.GetBounds();
83 const RoundingRadii& radii = round_rect.GetRadii();
84
85 Scalar left_margin = std::max(radii.top_left.width, radii.bottom_left.width);
86 Scalar right_margin =
87 std::max(radii.top_right.width, radii.bottom_right.width);
88 Scalar top_margin = std::max(radii.top_left.height, radii.top_right.height);
89 Scalar bottom_margin =
90 std::max(radii.bottom_left.height, radii.bottom_right.height);
91
92 bool flat_on_tb = bounds.GetWidth() > left_margin + right_margin;
93 bool flat_on_lr = bounds.GetHeight() > top_margin + bottom_margin;
94 if (!flat_on_tb && !flat_on_lr) {
95 return false;
96 }
97 // We either transform the bounds and delta-transform the radii,
98 // or we compute the vertical and horizontal bounds and then
99 // transform each. Either way there are 2 transform operations.
100 // We could also get a weaker answer by computing just the
101 // "inner rect" and only doing a coverage analysis on that,
102 // but this process will produce more culling results.
103 if (flat_on_tb) {
104 Rect vertical_bounds = bounds.Expand(-left_margin, 0, -right_margin, 0);
105 Rect coverage = vertical_bounds.TransformBounds(transform);
106 if (coverage.Contains(rect)) {
107 return true;
108 }
109 }
110 if (flat_on_lr) {
111 Rect horizontal_bounds = bounds.Expand(0, -top_margin, 0, -bottom_margin);
112 Rect coverage = horizontal_bounds.TransformBounds(transform);
113 if (coverage.Contains(rect)) {
114 return true;
115 }
116 }
117 return false;
118 }
119
120 StrokeRoundRectGeometry::StrokeRoundRectGeometry(
121 const RoundRect& round_rect,
122 const StrokeParameters& parameters)
123 : StrokePathSourceGeometry(parameters), round_rect_source_(round_rect) {}
124
125 const PathSource& StrokeRoundRectGeometry::GetSource() const {
126 return round_rect_source_;
127 }
128
129 } // namespace impeller
impeller::ContentContext
Definition: content_context.h:138
impeller::ContentContext::GetTessellator
Tessellator & GetTessellator() const
Definition: content_context.cc:946
impeller::Entity
Definition: entity.h:26
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:44
impeller::FillPathSourceGeometry
An abstract Geometry base class that produces fillable vertices for the interior of any |PathSource| ...
Definition: fill_path_geometry.h:19
impeller::FillRoundRectGeometry::GetSource
const PathSource & GetSource() const override
Definition: round_rect_geometry.cc:69
impeller::FillRoundRectGeometry::FillRoundRectGeometry
FillRoundRectGeometry(const RoundRect &round_rect)
Definition: round_rect_geometry.cc:66
impeller::FillRoundRectGeometry::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: round_rect_geometry.cc:73
impeller::Geometry::ComputePositionGeometry
static GeometryResult ComputePositionGeometry(const ContentContext &renderer, const Tessellator::VertexGenerator &generator, const Entity &entity, RenderPass &pass)
Definition: geometry.cc: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::RoundRectGeometry::~RoundRectGeometry
~RoundRectGeometry() override
impeller::RoundRectGeometry::RoundRectGeometry
RoundRectGeometry(const Rect &bounds, const Size &radii)
Definition: round_rect_geometry.cc:9
impeller::RoundRectGeometry::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: round_rect_geometry.cc:29
impeller::RoundRectGeometry::IsAxisAlignedRect
bool IsAxisAlignedRect() const override
Definition: round_rect_geometry.cc:62
impeller::RoundRectPathSource::GetRoundRect
const RoundRect & GetRoundRect() const
Definition: round_rect.h:151
impeller::StrokePathSourceGeometry
An abstract Geometry base class that produces fillable vertices representing the stroked outline from...
Definition: stroke_path_geometry.h:94
impeller::StrokeRoundRectGeometry::StrokeRoundRectGeometry
StrokeRoundRectGeometry(const RoundRect &rect, const StrokeParameters &parameters)
Definition: round_rect_geometry.cc:120
impeller::StrokeRoundRectGeometry::GetSource
const PathSource & GetSource() const override
Definition: round_rect_geometry.cc:125
impeller::Tessellator::FilledRoundRect
EllipticalVertexGenerator FilledRoundRect(const Matrix &view_transform, const Rect &bounds, const Size &radii)
Create a |VertexGenerator| that can produce vertices for a filled round rect within the given bounds ...
Definition: tessellator.cc:628
transform
Matrix transform
Definition: gaussian_blur_filter_contents.cc:214
impeller
Definition: allocation.cc:12
impeller::Scalar
float Scalar
Definition: scalar.h:19
std
Definition: comparable.h:95
round_rect_geometry.h
impeller::GeometryResult
Definition: geometry.h:21
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::RoundRect
Definition: round_rect.h:16
impeller::RoundRect::GetRadii
constexpr const RoundingRadii & GetRadii() const
Definition: round_rect.h:55
impeller::RoundRect::GetBounds
constexpr const Rect & GetBounds() const
Definition: round_rect.h:53
impeller::RoundingRadii
Definition: rounding_radii.h:14
impeller::RoundingRadii::top_right
Size top_right
Definition: rounding_radii.h:16
impeller::RoundingRadii::bottom_left
Size bottom_left
Definition: rounding_radii.h:17
impeller::RoundingRadii::bottom_right
Size bottom_right
Definition: rounding_radii.h:18
impeller::RoundingRadii::top_left
Size top_left
Definition: rounding_radii.h:15
impeller::StrokeParameters
A structure to store all of the parameters related to stroking a path or basic geometry object.
Definition: stroke_parameters.h:28
impeller::TRect< Scalar >
impeller::TRect::Expand
constexpr TRect< T > Expand(T left, T top, T right, T bottom) const
Returns a rectangle with expanded edges. Negative expansion results in shrinking.
Definition: rect.h:618
impeller::TRect::TransformBounds
constexpr TRect TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
Definition: rect.h:472
impeller::TRect::GetHeight
constexpr Type GetHeight() const
Returns the height of the rectangle, equivalent to |GetSize().height|.
Definition: rect.h:347
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:231
impeller::TRect::GetWidth
constexpr Type GetWidth() const
Returns the width of the rectangle, equivalent to |GetSize().width|.
Definition: rect.h:341
impeller::TSize< Scalar >
impeller::TSize::height
Type height
Definition: size.h:29
impeller::TSize::width
Type width
Definition: size.h:28