Skip to content

Commit de177df

Browse files
authored
Fix Rect::ExpandToMinTransformedSize to return the input rectangle when no expansion is needed, and remove 1-pixel roundrect to rect simplification (flutter#189808)
Fixes flutter#189807 See the issue for more context. Adds a unit test that fails without this change, and passes with this change. Verified the demo app in the issue renders properly with this change. From the issue: ## Root cause and fix [Rect::ExpandToMinTransformedSize](https://github.com/flutter/flutter/blob/1c8b3088238220102df66761685f3a4718b5aa15/engine/src/flutter/impeller/geometry/rect.h#L705) always constructs a new expanded rectangle from the input Rect from its center point and half-extents. If the input rectangle is already large enough and does not need expansion, this should return an identical rectangle. The returned rectangle is equality-checked against the input in [Canvas::DrawRoundRect](https://github.com/flutter/flutter/blob/1c8b3088238220102df66761685f3a4718b5aa15/engine/src/flutter/impeller/display_list/canvas.cc#L1129) to determine whether expansion happened. That code assumes that if the output does not equal the input, pixel-expansion happened, which means that the rounded rect had sub-pixel dimensions and can be safely drawn as a non-rounded rectangle. The issue is that for certain cases, particularly with non-integer sizes, ExpandToMinTransformedSize returns a different rectangle even if no expansion happened. This is due to small floating point imprecisions when constructing the new rectangle using the input rectangle's center and half-extents. To fix this, ExpandToMinTransformedSize should not construct a new rectangle unless expansion is actually needed. ## Removal of 1-pixel roundrect to rect simplification This PR also removes the 1-pixel roundrect to rect simplification in `Canvas::DrawRoundRect`. As described in flutter#189807 (comment), this optimization resulted in slightly different colors at the tips of thin roundrects. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent cb34620 commit de177df

3 files changed

Lines changed: 26 additions & 20 deletions

File tree

‎engine/src/flutter/impeller/display_list/canvas.cc‎

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,36 +1111,27 @@ void Canvas::DrawRoundRect(const RoundRect& round_rect, const Paint& paint) {
11111111

11121112
if (renderer_.GetContext()->GetFlags().use_sdfs &&
11131113
IsCompatibleWithSDFRendering(paint) && radii.AreAllCornersCircular()) {
1114+
Color effective_color = paint.color;
1115+
Rect bounds = round_rect.GetBounds();
1116+
11141117
// Expand rrect bounds to 1 pixel minimum dimensions if applicable.
11151118
if (paint.style == Paint::Style::kFill &&
11161119
!GetCurrentTransform().HasPerspective2D()) {
1117-
Rect rrect_bounds = round_rect.GetBounds();
1118-
auto [expanded, alpha_scaled_color] =
1119-
ExpandRectToPixelMinimum(rrect_bounds, paint.color,
1120-
GetCurrentTransform(), /*scale_alpha=*/true);
1120+
auto [expanded, alpha_scaled_color] = ExpandRectToPixelMinimum(
1121+
bounds, paint.color, GetCurrentTransform(), /*scale_alpha=*/true);
11211122

11221123
if (expanded.IsEmpty()) {
11231124
// RRect is invisible due to transform scaling or alpha scaling.
11241125
return;
11251126
}
11261127

1127-
// Pixel-minimum expansion is applicable if the expanded bounds is
1128-
// different from the original bounds.
1129-
if (expanded != rrect_bounds) {
1130-
// At a 1-pixel size, the rounded corners can be ignored. Draw a regular
1131-
// rect matching the expanded bounds.
1132-
auto params = UberSDFParameters::MakeRect(
1133-
/*color=*/alpha_scaled_color,
1134-
/*rect=*/expanded,
1135-
/*stroke=*/std::nullopt);
1136-
AddRenderSDFEntityToCurrentPass(paint, params);
1137-
return;
1138-
}
1128+
bounds = expanded;
1129+
effective_color = alpha_scaled_color;
11391130
}
11401131

11411132
auto params = UberSDFParameters::MakeRoundedRect(
1142-
/*color=*/paint.color,
1143-
/*rect=*/round_rect.GetBounds(),
1133+
/*color=*/effective_color,
1134+
/*rect=*/bounds,
11441135
/*radii=*/radii,
11451136
/*stroke=*/paint.style == Paint::Style::kStroke
11461137
? std::make_optional(paint.stroke)

‎engine/src/flutter/impeller/geometry/rect.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,13 @@ struct TRect {
719719
return std::nullopt;
720720
}
721721
Size current_size = GetSize();
722+
723+
if (current_size.width >= min_local_size.width &&
724+
current_size.height >= min_local_size.height) {
725+
// No expansion needed.
726+
return *this;
727+
}
728+
722729
Size expanded_size = current_size.Max(min_local_size);
723730
return MakeEllipseBounds(GetCenter(), Point(expanded_size.width * 0.5f,
724731
expanded_size.height * 0.5f));

‎engine/src/flutter/impeller/geometry/rect_unittests.cc‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,13 +1444,21 @@ TEST(RectTest, RectExpandToMinTransformedSizeNullForScaledToZero) {
14441444
std::nullopt);
14451445
}
14461446

1447-
TEST(RectTest,
1448-
RectExpandToMinTransformedSizeRectReturnsUnmodifiedWhenLargeEnough) {
1447+
TEST(RectTest, RectExpandToMinTransformedSizeReturnsUnmodifiedWhenLargeEnough) {
14491448
Rect rect = Rect::MakeXYWH(0, 0, 10, 20);
14501449
auto transform = Matrix::MakeScale(Vector3(0.5f, 0.5f));
14511450
EXPECT_EQ(rect.ExpandToMinTransformedSize({1.0f, 1.0f}, transform), rect);
14521451
}
14531452

1453+
TEST(
1454+
RectTest,
1455+
RectExpandToMinTransformedSizeReturnsUnmodifiedWhenLargeEnoughWithFractionalSize) {
1456+
// Regression test for https://github.com/flutter/flutter/issues/189807.
1457+
Rect rect = Rect::MakeXYWH(100.0f, 50.0f, 64.2f, 40.0f);
1458+
auto transform = Matrix();
1459+
EXPECT_EQ(rect.ExpandToMinTransformedSize({1.0f, 1.0f}, transform), rect);
1460+
}
1461+
14541462
TEST(RectTest, RectExpandToMinTransformedSizeRectWithIdentityTransform) {
14551463
Size size = Size(2.0f, 2.0f);
14561464
Rect rect = Rect::MakeEllipseBounds(Point(), size * 0.5f);

0 commit comments

Comments
 (0)