From a2bc059e706816dac2d7f785e02d1dfac137ce2e Mon Sep 17 00:00:00 2001 From: ColdPaleLight Date: Thu, 17 Feb 2022 14:41:25 +0800 Subject: [PATCH 1/2] Make sure the secondary callback is called after the vsync callback --- shell/common/vsync_waiter.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/shell/common/vsync_waiter.cc b/shell/common/vsync_waiter.cc index 1755755182e87..1a545368f1535 100644 --- a/shell/common/vsync_waiter.cc +++ b/shell/common/vsync_waiter.cc @@ -154,8 +154,7 @@ void VsyncWaiter::FireCallback(fml::TimePoint frame_start_time, } for (auto& secondary_callback : secondary_callbacks) { - task_runners_.GetUITaskRunner()->PostTaskForTime( - std::move(secondary_callback), frame_start_time); + task_runners_.GetUITaskRunner()->PostTask(std::move(secondary_callback)); } } From 59d849c723f2a0cce35c8bb434c75f2d713dc7ce Mon Sep 17 00:00:00 2001 From: ColdPaleLight Date: Sun, 20 Feb 2022 18:03:17 +0800 Subject: [PATCH 2/2] Add the unit test --- shell/common/fixtures/shell_test.dart | 2 +- shell/common/shell_unittests.cc | 46 ++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/shell/common/fixtures/shell_test.dart b/shell/common/fixtures/shell_test.dart index 337c771a00aea..85c406291c942 100644 --- a/shell/common/fixtures/shell_test.dart +++ b/shell/common/fixtures/shell_test.dart @@ -264,7 +264,7 @@ void canReceiveArgumentsWhenEngineSpawn(List args) { } @pragma('vm:entry-point') -void canScheduleFrameFromPlatform() { +void onBeginFrameWithNotifyNativeMain() { PlatformDispatcher.instance.onBeginFrame = (Duration beginTime) { nativeOnBeginFrame(beginTime.inMicroseconds); }; diff --git a/shell/common/shell_unittests.cc b/shell/common/shell_unittests.cc index 34c459678a447..3b759b3635c29 100644 --- a/shell/common/shell_unittests.cc +++ b/shell/common/shell_unittests.cc @@ -1847,7 +1847,7 @@ TEST_F(ShellTest, CanScheduleFrameFromPlatform) { ASSERT_TRUE(shell->IsSetup()); auto configuration = RunConfiguration::InferFromSettings(settings); - configuration.SetEntrypoint("canScheduleFrameFromPlatform"); + configuration.SetEntrypoint("onBeginFrameWithNotifyNativeMain"); RunEngine(shell.get(), std::move(configuration)); // Wait for the application to attach the listener. @@ -1860,6 +1860,50 @@ TEST_F(ShellTest, CanScheduleFrameFromPlatform) { DestroyShell(std::move(shell), std::move(task_runners)); } +TEST_F(ShellTest, SecondaryVsyncCallbackShouldBeCalledAfterVsyncCallback) { + bool is_on_begin_frame_called = false; + bool is_secondary_callback_called = false; + Settings settings = CreateSettingsForFixture(); + TaskRunners task_runners = GetTaskRunnersForFixture(); + fml::AutoResetWaitableEvent latch; + AddNativeCallback( + "NotifyNative", + CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) { latch.Signal(); })); + fml::CountDownLatch count_down_latch(2); + AddNativeCallback("NativeOnBeginFrame", + CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) { + EXPECT_FALSE(is_on_begin_frame_called); + EXPECT_FALSE(is_secondary_callback_called); + is_on_begin_frame_called = true; + count_down_latch.CountDown(); + })); + std::unique_ptr shell = + CreateShell(std::move(settings), std::move(task_runners)); + ASSERT_TRUE(shell->IsSetup()); + + auto configuration = RunConfiguration::InferFromSettings(settings); + configuration.SetEntrypoint("onBeginFrameWithNotifyNativeMain"); + RunEngine(shell.get(), std::move(configuration)); + + // Wait for the application to attach the listener. + latch.Wait(); + + fml::TaskRunner::RunNowOrPostTask( + shell->GetTaskRunners().GetUITaskRunner(), [&]() { + shell->GetEngine()->ScheduleSecondaryVsyncCallback(0, [&]() { + EXPECT_TRUE(is_on_begin_frame_called); + EXPECT_FALSE(is_secondary_callback_called); + is_secondary_callback_called = true; + count_down_latch.CountDown(); + }); + shell->GetEngine()->ScheduleFrame(); + }); + count_down_latch.Wait(); + EXPECT_TRUE(is_on_begin_frame_called); + EXPECT_TRUE(is_secondary_callback_called); + DestroyShell(std::move(shell), std::move(task_runners)); +} + static void LogSkData(sk_sp data, const char* title) { FML_LOG(ERROR) << "---------- " << title; std::ostringstream ostr;