Skip to content

Commit 31ed3b7

Browse files
authored
Scale aa lines (#165917)
fixes flutter/flutter#165905 Notice that the horizontal lines both have about 1 pixel of blurring for both scales. <img width="687" alt="Screenshot 2025-03-25 at 1 09 16 PM" src="https://github.com/user-attachments/assets/b65dc1bf-6bf3-44d0-9605-95b51efc932c" /> ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [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. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [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 7cb926a commit 31ed3b7

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

engine/src/flutter/impeller/display_list/aiks_dl_path_unittests.cc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,41 @@ TEST_P(AiksTest, DrawLinesRenderCorrectly) {
382382
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
383383
}
384384

385+
// The goal of this test is to show that scaling the lines doesn't also scale
386+
// the antialiasing. The amount of blurring should be the same for both
387+
// horizontal lines.
388+
TEST_P(AiksTest, ScaleExperimentAntialiasLines) {
389+
Scalar scale = 5.0;
390+
Scalar line_width = 10.f;
391+
auto callback = [&]() -> sk_sp<DisplayList> {
392+
if (AiksTest::ImGuiBegin("Controls", nullptr,
393+
ImGuiWindowFlags_AlwaysAutoResize)) {
394+
ImGui::SliderFloat("Scale", &scale, 0.001, 5);
395+
ImGui::SliderFloat("Width", &line_width, 1, 20);
396+
397+
ImGui::End();
398+
}
399+
DisplayListBuilder builder;
400+
builder.Scale(GetContentScale().x, GetContentScale().y);
401+
402+
DlPaint paint;
403+
paint.setColor(DlColor::kGreenYellow());
404+
paint.setStrokeWidth(line_width);
405+
406+
builder.DrawLine(DlPoint(100, 100), DlPoint(350, 100), paint);
407+
builder.DrawLine(DlPoint(100, 100), DlPoint(350, 150), paint);
408+
409+
builder.Translate(100, 300);
410+
builder.Scale(scale, scale);
411+
builder.Translate(-100, -300);
412+
builder.DrawLine(DlPoint(100, 300), DlPoint(350, 300), paint);
413+
builder.DrawLine(DlPoint(100, 300), DlPoint(350, 450), paint);
414+
415+
return builder.Build();
416+
};
417+
ASSERT_TRUE(OpenPlaygroundHere(callback));
418+
}
419+
385420
TEST_P(AiksTest, SimpleExperimentAntialiasLines) {
386421
DisplayListBuilder builder;
387422
builder.Scale(GetContentScale().x, GetContentScale().y);

engine/src/flutter/impeller/entity/contents/line_contents.cc

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ using CreateGeometryCallback =
2424
const Geometry* geometry)>;
2525

2626
const int32_t kCurveResolution = 32;
27-
const Scalar kSampleRadius = 0.5f;
27+
const float kSampleRadius = 1.f;
2828

2929
struct LineInfo {
3030
Vector3 e0;
@@ -62,6 +62,8 @@ uint8_t DoubleToUint8(double x) {
6262
/// See also: CreateGradientTexture
6363
std::shared_ptr<Texture> CreateCurveTexture(
6464
Scalar width,
65+
Scalar radius,
66+
Scalar scale,
6567
const std::shared_ptr<impeller::Context>& context) {
6668
//
6769
impeller::TextureDescriptor texture_descriptor;
@@ -73,8 +75,8 @@ std::shared_ptr<Texture> CreateCurveTexture(
7375
curve_data.reserve(kCurveResolution);
7476
for (int i = 0; i < kCurveResolution; ++i) {
7577
double norm = (static_cast<double>(i) + 1.0) / 32.0;
76-
double loc = norm * (kSampleRadius + width / 2.0);
77-
double den = kSampleRadius * 2.0 + 1.0;
78+
double loc = scale * norm * (radius + width / 2.0);
79+
double den = radius * 2.0 + 1.0;
7880
curve_data.push_back(DoubleToUint8(loc / den));
7981
}
8082

@@ -96,13 +98,14 @@ GeometryResult CreateGeometry(const ContentContext& renderer,
9698
corners, transform,
9799
/*extend_endpoints=*/line_geometry->GetCap() != Cap::kButt,
98100
line_geometry->GetP0(), line_geometry->GetP1(),
99-
line_geometry->GetWidth() + kSampleRadius)) {
101+
line_geometry->GetWidth() + kSampleRadius * 2.0)) {
100102
return kEmptyResult;
101103
}
102104

103105
auto& host_buffer = renderer.GetTransientsBuffer();
104106

105107
size_t count = 4;
108+
Scalar scale = entity.GetTransform().GetMaxBasisLengthXY();
106109
LineInfo line_info =
107110
CalculateLineInfo(line_geometry->GetP0(), line_geometry->GetP1(),
108111
line_geometry->GetWidth(), kSampleRadius);
@@ -121,8 +124,8 @@ GeometryResult CreateGeometry(const ContentContext& renderer,
121124
}
122125
});
123126

124-
std::shared_ptr<Texture> curve_texture =
125-
CreateCurveTexture(line_geometry->GetWidth(), renderer.GetContext());
127+
std::shared_ptr<Texture> curve_texture = CreateCurveTexture(
128+
line_geometry->GetWidth(), kSampleRadius, scale, renderer.GetContext());
126129

127130
SamplerDescriptor sampler_desc;
128131
sampler_desc.min_filter = MinMagFilter::kLinear;

engine/src/flutter/testing/impeller_golden_tests_output.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,7 @@ impeller_Play_AiksTest_SaveLayerDrawsBehindSubsequentEntities_Vulkan.png
886886
impeller_Play_AiksTest_SaveLayerFiltersScaleWithTransform_Metal.png
887887
impeller_Play_AiksTest_SaveLayerFiltersScaleWithTransform_OpenGLES.png
888888
impeller_Play_AiksTest_SaveLayerFiltersScaleWithTransform_Vulkan.png
889+
impeller_Play_AiksTest_ScaleExperimentAntialiasLines_Metal.png
889890
impeller_Play_AiksTest_ScaledK_Metal.png
890891
impeller_Play_AiksTest_ScaledK_OpenGLES.png
891892
impeller_Play_AiksTest_ScaledK_Vulkan.png

0 commit comments

Comments
 (0)