diff --git a/engine/src/flutter/impeller/display_list/aiks_dl_text_unittests.cc b/engine/src/flutter/impeller/display_list/aiks_dl_text_unittests.cc index 555c9ea276..0d7cbaec7d 100644 --- a/engine/src/flutter/impeller/display_list/aiks_dl_text_unittests.cc +++ b/engine/src/flutter/impeller/display_list/aiks_dl_text_unittests.cc @@ -180,6 +180,35 @@ TEST_P(AiksTest, ScaledK) { ASSERT_TRUE(OpenPlaygroundHere(builder.Build())); } +TEST_P(AiksTest, CanRenderTextFrameWithScalingOverflow) { + Scalar scale = 60.0; + Scalar offsetx = -500.0; + Scalar offsety = 700.0; + auto callback = [&]() -> sk_sp { + if (AiksTest::ImGuiBegin("Controls", nullptr, + ImGuiWindowFlags_AlwaysAutoResize)) { + ImGui::SliderFloat("scale", &scale, 1.f, 300.f); + ImGui::SliderFloat("offsetx", &offsetx, -600.f, 100.f); + ImGui::SliderFloat("offsety", &offsety, 600.f, 2048.f); + ImGui::End(); + } + DisplayListBuilder builder; + builder.Scale(GetContentScale().x, GetContentScale().y); + DlPaint paint; + paint.setColor(DlColor::ARGB(1, 0.1, 0.1, 0.1)); + builder.DrawPaint(paint); + builder.Scale(scale, scale); + + RenderTextInCanvasSkia( + GetContext(), builder, "test", "Roboto-Regular.ttf", + TextRenderOptions{ + .position = DlPoint(offsetx / scale, offsety / scale), + }); + return builder.Build(); + }; + ASSERT_TRUE(OpenPlaygroundHere(callback)); +} + TEST_P(AiksTest, CanRenderTextFrameWithFractionScaling) { Scalar fine_scale = 0.f; bool is_subpixel = false; diff --git a/engine/src/flutter/impeller/entity/contents/text_contents.cc b/engine/src/flutter/impeller/entity/contents/text_contents.cc index df38d351c5..a2a43aaca4 100644 --- a/engine/src/flutter/impeller/entity/contents/text_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/text_contents.cc @@ -77,6 +77,22 @@ void TextContents::SetTextProperties(Color color, } } +namespace { +Scalar AttractToOne(Scalar x) { + // Epsilon was decided by looking at the floating point inaccuracies in + // the ScaledK test. + const Scalar epsilon = 0.005f; + if (std::abs(x - 1.f) < epsilon) { + return 1.f; + } + if (std::abs(x + 1.f) < epsilon) { + return -1.f; + } + return x; +} + +} // namespace + void TextContents::ComputeVertexData( VS::PerVertexData* vtx_contents, const std::shared_ptr& frame, @@ -165,8 +181,9 @@ void TextContents::ComputeVertexData( atlas_glyph_bounds = maybe_atlas_glyph_bounds.value().atlas_bounds; } - Rect scaled_bounds = - glyph_bounds.Scale(static_cast(rounded_scale.Invert())); + Scalar inverted_rounded_scale = + static_cast(rounded_scale.Invert()); + Rect scaled_bounds = glyph_bounds.Scale(inverted_rounded_scale); // For each glyph, we compute two rectangles. One for the vertex // positions and one for the texture coordinates (UVs). The atlas // glyph bounds are used to compute UVs in cases where the @@ -176,8 +193,14 @@ void TextContents::ComputeVertexData( Point uv_size = SizeToPoint(atlas_glyph_bounds.GetSize()) / atlas_size; Matrix unscaled_basis = - Matrix::MakeScale({basis_transform.m[0] > 0 ? 1.f : -1.f, - basis_transform.m[5] > 0 ? 1.f : -1.f, 1.f}); + basis_transform * Matrix::MakeScale({inverted_rounded_scale, + inverted_rounded_scale, 1}); + + // In typical scales < 48x these values should be -1 or 1. We round to + // those to avoid inaccuracies. + unscaled_basis.m[0] = AttractToOne(unscaled_basis.m[0]); + unscaled_basis.m[5] = AttractToOne(unscaled_basis.m[5]); + Point unrounded_glyph_position = // This is for RTL text. unscaled_basis * glyph_bounds.GetLeftTop() + diff --git a/engine/src/flutter/testing/impeller_golden_tests_output.txt b/engine/src/flutter/testing/impeller_golden_tests_output.txt index f3b05af8ed..4fd15e6f6f 100644 --- a/engine/src/flutter/testing/impeller_golden_tests_output.txt +++ b/engine/src/flutter/testing/impeller_golden_tests_output.txt @@ -490,6 +490,9 @@ impeller_Play_AiksTest_CanRenderTextFrameWithHalfScaling_Vulkan.png impeller_Play_AiksTest_CanRenderTextFrameWithInvertedTransform_Metal.png impeller_Play_AiksTest_CanRenderTextFrameWithInvertedTransform_OpenGLES.png impeller_Play_AiksTest_CanRenderTextFrameWithInvertedTransform_Vulkan.png +impeller_Play_AiksTest_CanRenderTextFrameWithScalingOverflow_Metal.png +impeller_Play_AiksTest_CanRenderTextFrameWithScalingOverflow_OpenGLES.png +impeller_Play_AiksTest_CanRenderTextFrameWithScalingOverflow_Vulkan.png impeller_Play_AiksTest_CanRenderTextFrame_Metal.png impeller_Play_AiksTest_CanRenderTextFrame_OpenGLES.png impeller_Play_AiksTest_CanRenderTextFrame_Vulkan.png