diff --git a/engine/src/flutter/ci/licenses_golden/excluded_files b/engine/src/flutter/ci/licenses_golden/excluded_files index 1b9049d599..3d9a1de31f 100644 --- a/engine/src/flutter/ci/licenses_golden/excluded_files +++ b/engine/src/flutter/ci/licenses_golden/excluded_files @@ -152,6 +152,7 @@ ../../../flutter/impeller/display_list/dl_golden_unittests.h ../../../flutter/impeller/display_list/dl_unittests.cc ../../../flutter/impeller/display_list/skia_conversions_unittests.cc +../../../flutter/impeller/display_list/testing ../../../flutter/impeller/docs ../../../flutter/impeller/entity/clip_stack_unittests.cc ../../../flutter/impeller/entity/contents/filters/blend_filter_contents_unittests.cc diff --git a/engine/src/flutter/impeller/display_list/BUILD.gn b/engine/src/flutter/impeller/display_list/BUILD.gn index d52f73ea76..3c4f1aefc8 100644 --- a/engine/src/flutter/impeller/display_list/BUILD.gn +++ b/engine/src/flutter/impeller/display_list/BUILD.gn @@ -78,6 +78,10 @@ template("display_list_unittests_component") { "dl_playground.cc", "dl_playground.h", "dl_unittests.cc", + "testing/render_text_in_canvas.cc", + "testing/render_text_in_canvas.h", + "testing/rmse.cc", + "testing/rmse.h", ] additional_sources = [] if (defined(invoker.sources)) { 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 f51f56d08e..0d03982a35 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 @@ -152,17 +152,27 @@ TEST_P(AiksTest, CanRenderTextFrameWithHalfScaling) { } TEST_P(AiksTest, CanRenderTextFrameWithFractionScaling) { - DisplayListBuilder builder; + Scalar fine_scale = 0.f; + auto callback = [&]() -> sk_sp { + if (AiksTest::ImGuiBegin("Controls", nullptr, + ImGuiWindowFlags_AlwaysAutoResize)) { + ImGui::SliderFloat("Fine Scale", &fine_scale, -1, 1); + ImGui::End(); + } - DlPaint paint; - paint.setColor(DlColor::ARGB(1, 0.1, 0.1, 0.1)); - builder.DrawPaint(paint); - builder.Scale(2.625, 2.625); + DisplayListBuilder builder; + DlPaint paint; + paint.setColor(DlColor::ARGB(1, 0.1, 0.1, 0.1)); + builder.DrawPaint(paint); + Scalar scale = 2.625 + fine_scale; + builder.Scale(scale, scale); + RenderTextInCanvasSkia(GetContext(), builder, + "the quick brown fox jumped over the lazy dog!.?", + "Roboto-Regular.ttf"); + return builder.Build(); + }; - ASSERT_TRUE(RenderTextInCanvasSkia( - GetContext(), builder, "the quick brown fox jumped over the lazy dog!.?", - "Roboto-Regular.ttf")); - ASSERT_TRUE(OpenPlaygroundHere(builder.Build())); + ASSERT_TRUE(OpenPlaygroundHere(callback)); } TEST_P(AiksTest, TextFrameSubpixelAlignment) { diff --git a/engine/src/flutter/impeller/display_list/dl_golden_blur_unittests.cc b/engine/src/flutter/impeller/display_list/dl_golden_blur_unittests.cc index b070667adf..9b42a4d1bf 100644 --- a/engine/src/flutter/impeller/display_list/dl_golden_blur_unittests.cc +++ b/engine/src/flutter/impeller/display_list/dl_golden_blur_unittests.cc @@ -6,58 +6,18 @@ #include "flutter/display_list/dl_builder.h" #include "flutter/display_list/effects/dl_mask_filter.h" +#include "flutter/impeller/display_list/testing/render_text_in_canvas.h" +#include "flutter/impeller/display_list/testing/rmse.h" #include "flutter/impeller/geometry/round_rect.h" #include "flutter/impeller/golden_tests/screenshot.h" #include "flutter/testing/testing.h" #include "gtest/gtest.h" -#include "impeller/typographer/backends/skia/text_frame_skia.h" -#include "txt/platform.h" namespace flutter { namespace testing { using impeller::Font; -namespace { -struct TextRenderOptions { - bool stroke = false; - DlScalar font_size = 50; - DlColor color = DlColor::kYellow(); - std::shared_ptr mask_filter; -}; - -bool RenderTextInCanvasSkia(DlCanvas* canvas, - const std::string& text, - const std::string_view& font_fixture, - DlPoint position, - const TextRenderOptions& options = {}) { - auto c_font_fixture = std::string(font_fixture); - auto mapping = flutter::testing::OpenFixtureAsSkData(c_font_fixture.c_str()); - if (!mapping) { - return false; - } - sk_sp font_mgr = txt::GetDefaultFontManager(); - SkFont sk_font(font_mgr->makeFromData(mapping), options.font_size); - auto blob = SkTextBlob::MakeFromString(text.c_str(), sk_font); - if (!blob) { - return false; - } - - auto frame = impeller::MakeTextFrameFromTextBlobSkia(blob); - - DlPaint text_paint; - text_paint.setColor(options.color); - text_paint.setMaskFilter(options.mask_filter); - // text_paint.mask_blur_descriptor = options.mask_blur_descriptor; - // text_paint.stroke_width = 1; - // text_paint.style = - // options.stroke ? Paint::Style::kStroke : Paint::Style::kFill; - canvas->DrawTextFrame(frame, position.x, position.y, text_paint); - return true; -} - -} // namespace - TEST_P(DlGoldenTest, TextBlurMaskFilterRespectCTM) { impeller::Point content_scale = GetContentScale(); auto draw = [&](DlCanvas* canvas, @@ -112,41 +72,6 @@ TEST_P(DlGoldenTest, TextBlurMaskFilterDisrespectCTM) { ASSERT_TRUE(OpenPlaygroundHere(builder.Build())); } -namespace { -double CalculateDistance(const uint8_t* left, const uint8_t* right) { - double diff[4] = { - static_cast(left[0]) - right[0], // - static_cast(left[1]) - right[1], // - static_cast(left[2]) - right[2], // - static_cast(left[3]) - right[3] // - }; - return sqrt((diff[0] * diff[0]) + // - (diff[1] * diff[1]) + // - (diff[2] * diff[2]) + // - (diff[3] * diff[3])); -} - -double RMSE(const impeller::testing::Screenshot* left, - const impeller::testing::Screenshot* right) { - FML_CHECK(left); - FML_CHECK(right); - FML_CHECK(left->GetWidth() == right->GetWidth()); - FML_CHECK(left->GetHeight() == right->GetHeight()); - - int64_t samples = left->GetWidth() * left->GetHeight(); - double tally = 0; - - const uint8_t* left_ptr = left->GetBytes(); - const uint8_t* right_ptr = right->GetBytes(); - for (int64_t i = 0; i < samples; ++i, left_ptr += 4, right_ptr += 4) { - double distance = CalculateDistance(left_ptr, right_ptr); - tally += distance * distance; - } - - return sqrt(tally / static_cast(samples)); -} -} // namespace - // This is a test to make sure that we don't regress "shimmering" in the // gaussian blur. Shimmering is abrupt changes in signal when making tiny // changes to the blur parameters. diff --git a/engine/src/flutter/impeller/display_list/dl_golden_unittests.cc b/engine/src/flutter/impeller/display_list/dl_golden_unittests.cc index f3f3577eff..5236a01ce0 100644 --- a/engine/src/flutter/impeller/display_list/dl_golden_unittests.cc +++ b/engine/src/flutter/impeller/display_list/dl_golden_unittests.cc @@ -8,6 +8,8 @@ #include "display_list/dl_paint.h" #include "display_list/geometry/dl_geometry_types.h" #include "flutter/display_list/dl_builder.h" +#include "flutter/impeller/display_list/testing/render_text_in_canvas.h" +#include "flutter/impeller/display_list/testing/rmse.h" #include "flutter/impeller/geometry/path_builder.h" #include "flutter/testing/testing.h" #include "gtest/gtest.h" @@ -350,5 +352,52 @@ TEST_P(DlGoldenTest, SaveLayerAtFractionalValue) { ASSERT_TRUE(OpenPlaygroundHere(builder.Build())); } +namespace { +int32_t CalculateMaxY(const impeller::testing::Screenshot* img) { + const uint32_t* ptr = reinterpret_cast(img->GetBytes()); + int32_t max_y = 0; + for (uint32_t i = 0; i < img->GetHeight(); ++i) { + for (uint32_t j = 0; j < img->GetWidth(); ++j) { + uint32_t pixel = *ptr++; + if ((pixel & 0x00ffffff) != 0) { + max_y = std::max(max_y, static_cast(i)); + } + } + } + return max_y; +} +} // namespace + +TEST_P(DlGoldenTest, BaselineHE) { + SetWindowSize(impeller::ISize(1024, 200)); + impeller::Scalar font_size = 300; + auto callback = [&](const char* text, + impeller::Scalar scale) -> sk_sp { + DisplayListBuilder builder; + DlPaint paint; + paint.setColor(DlColor::ARGB(1, 0, 0, 0)); + builder.DrawPaint(paint); + builder.Scale(scale, scale); + RenderTextInCanvasSkia(&builder, text, "Roboto-Regular.ttf", + DlPoint::MakeXY(10, 300), + TextRenderOptions{ + .font_size = font_size, + }); + return builder.Build(); + }; + + std::unique_ptr right = + MakeScreenshot(callback("h", 0.444)); + if (!right) { + GTEST_SKIP() << "making screenshots not supported."; + } + std::unique_ptr left = + MakeScreenshot(callback("e", 0.444)); + + int32_t left_max_y = CalculateMaxY(left.get()); + int32_t right_max_y = CalculateMaxY(right.get()); + int32_t y_diff = std::abs(left_max_y - right_max_y); + EXPECT_TRUE(y_diff <= 2) << "y diff: " << y_diff; +} } // namespace testing } // namespace flutter diff --git a/engine/src/flutter/impeller/display_list/testing/render_text_in_canvas.cc b/engine/src/flutter/impeller/display_list/testing/render_text_in_canvas.cc new file mode 100644 index 0000000000..aca644d3f2 --- /dev/null +++ b/engine/src/flutter/impeller/display_list/testing/render_text_in_canvas.cc @@ -0,0 +1,43 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "impeller/display_list/testing/render_text_in_canvas.h" + +#include "flutter/testing/testing.h" +#include "txt/platform.h" + +namespace flutter { +namespace testing { + +bool RenderTextInCanvasSkia(DlCanvas* canvas, + const std::string& text, + const std::string_view& font_fixture, + DlPoint position, + const TextRenderOptions& options) { + auto c_font_fixture = std::string(font_fixture); + auto mapping = flutter::testing::OpenFixtureAsSkData(c_font_fixture.c_str()); + if (!mapping) { + return false; + } + sk_sp font_mgr = txt::GetDefaultFontManager(); + SkFont sk_font(font_mgr->makeFromData(mapping), options.font_size); + auto blob = SkTextBlob::MakeFromString(text.c_str(), sk_font); + if (!blob) { + return false; + } + + auto frame = impeller::MakeTextFrameFromTextBlobSkia(blob); + + DlPaint text_paint; + text_paint.setColor(options.color); + text_paint.setMaskFilter(options.mask_filter); + // text_paint.mask_blur_descriptor = options.mask_blur_descriptor; + // text_paint.stroke_width = 1; + // text_paint.style = + // options.stroke ? Paint::Style::kStroke : Paint::Style::kFill; + canvas->DrawTextFrame(frame, position.x, position.y, text_paint); + return true; +} +} // namespace testing +} // namespace flutter diff --git a/engine/src/flutter/impeller/display_list/testing/render_text_in_canvas.h b/engine/src/flutter/impeller/display_list/testing/render_text_in_canvas.h new file mode 100644 index 0000000000..cdbf3c2259 --- /dev/null +++ b/engine/src/flutter/impeller/display_list/testing/render_text_in_canvas.h @@ -0,0 +1,34 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_IMPELLER_DISPLAY_LIST_TESTING_RENDER_TEXT_IN_CANVAS_H_ +#define FLUTTER_IMPELLER_DISPLAY_LIST_TESTING_RENDER_TEXT_IN_CANVAS_H_ + +#include + +#include "flutter/display_list/dl_canvas.h" +#include "flutter/display_list/dl_color.h" +#include "flutter/display_list/effects/dl_mask_filter.h" +#include "impeller/typographer/backends/skia/text_frame_skia.h" + +namespace flutter { +namespace testing { + +struct TextRenderOptions { + bool stroke = false; + SkScalar font_size = 50; + DlColor color = DlColor::kYellow(); + std::shared_ptr mask_filter; +}; + +bool RenderTextInCanvasSkia(DlCanvas* canvas, + const std::string& text, + const std::string_view& font_fixture, + DlPoint position, + const TextRenderOptions& options = {}); + +} // namespace testing +} // namespace flutter + +#endif // FLUTTER_IMPELLER_DISPLAY_LIST_TESTING_RENDER_TEXT_IN_CANVAS_H_ diff --git a/engine/src/flutter/impeller/display_list/testing/rmse.cc b/engine/src/flutter/impeller/display_list/testing/rmse.cc new file mode 100644 index 0000000000..45e19c8037 --- /dev/null +++ b/engine/src/flutter/impeller/display_list/testing/rmse.cc @@ -0,0 +1,47 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "impeller/display_list/testing/rmse.h" + +#include "flutter/fml/logging.h" + +namespace flutter { +namespace testing { +namespace { +double CalculateDistance(const uint8_t* left, const uint8_t* right) { + double diff[4] = { + static_cast(left[0]) - right[0], // + static_cast(left[1]) - right[1], // + static_cast(left[2]) - right[2], // + static_cast(left[3]) - right[3] // + }; + return sqrt((diff[0] * diff[0]) + // + (diff[1] * diff[1]) + // + (diff[2] * diff[2]) + // + (diff[3] * diff[3])); +} +} // namespace + +double RMSE(const impeller::testing::Screenshot* left, + const impeller::testing::Screenshot* right) { + FML_CHECK(left); + FML_CHECK(right); + FML_CHECK(left->GetWidth() == right->GetWidth()); + FML_CHECK(left->GetHeight() == right->GetHeight()); + + int64_t samples = left->GetWidth() * left->GetHeight(); + double tally = 0; + + const uint8_t* left_ptr = left->GetBytes(); + const uint8_t* right_ptr = right->GetBytes(); + for (int64_t i = 0; i < samples; ++i, left_ptr += 4, right_ptr += 4) { + double distance = CalculateDistance(left_ptr, right_ptr); + tally += distance * distance; + } + + return sqrt(tally / static_cast(samples)); +} + +} // namespace testing +} // namespace flutter diff --git a/engine/src/flutter/impeller/display_list/testing/rmse.h b/engine/src/flutter/impeller/display_list/testing/rmse.h new file mode 100644 index 0000000000..aadb8302d8 --- /dev/null +++ b/engine/src/flutter/impeller/display_list/testing/rmse.h @@ -0,0 +1,16 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_IMPELLER_DISPLAY_LIST_TESTING_RMSE_H_ +#define FLUTTER_IMPELLER_DISPLAY_LIST_TESTING_RMSE_H_ + +#include "flutter/impeller/golden_tests/screenshot.h" + +namespace flutter { +namespace testing { +double RMSE(const impeller::testing::Screenshot* left, + const impeller::testing::Screenshot* right); +} +} // namespace flutter +#endif // FLUTTER_IMPELLER_DISPLAY_LIST_TESTING_RMSE_H_ diff --git a/engine/src/flutter/impeller/entity/contents/text_contents.cc b/engine/src/flutter/impeller/entity/contents/text_contents.cc index 1835c5b435..57678cfa2c 100644 --- a/engine/src/flutter/impeller/entity/contents/text_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/text_contents.cc @@ -172,8 +172,11 @@ void TextContents::ComputeVertexData( Point uv_size = (atlas_glyph_bounds.GetSize() + Point(1, 1)) / atlas_size; Point unrounded_glyph_position = - basis_transform * - (glyph_position.position + scaled_bounds.GetLeftTop()); + // This is for RTL text. + (basis_transform.m[0] < 0 ? Matrix::MakeScale({-1, 1, 1}) + : Matrix()) * + glyph_bounds.GetLeftTop() + + (basis_transform * glyph_position.position); Point screen_glyph_position = (screen_offset + unrounded_glyph_position + subpixel_adjustment)