From a3553b7f4181aae932172d8c73f05a2410c1f6db Mon Sep 17 00:00:00 2001 From: Kaushik Iska Date: Wed, 29 Sep 2021 13:48:02 -0400 Subject: [PATCH] vsync_waiter_fallback should schedule firecallback for future (flutter/engine#28817) --- .../src/flutter/shell/common/vsync_waiter.cc | 9 ++++++--- engine/src/flutter/shell/common/vsync_waiter.h | 2 ++ .../shell/common/vsync_waiter_fallback.cc | 18 +++++++++++++++--- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/engine/src/flutter/shell/common/vsync_waiter.cc b/engine/src/flutter/shell/common/vsync_waiter.cc index bfbd64c133..c2806d5b7d 100644 --- a/engine/src/flutter/shell/common/vsync_waiter.cc +++ b/engine/src/flutter/shell/common/vsync_waiter.cc @@ -7,8 +7,10 @@ #include "flow/frame_timings.h" #include "flutter/fml/task_runner.h" #include "flutter/fml/trace_event.h" +#include "fml/logging.h" #include "fml/message_loop_task_queues.h" #include "fml/task_queue_id.h" +#include "fml/time/time_point.h" namespace flutter { @@ -95,6 +97,8 @@ void VsyncWaiter::ScheduleSecondaryCallback(uintptr_t id, void VsyncWaiter::FireCallback(fml::TimePoint frame_start_time, fml::TimePoint frame_target_time, bool pause_secondary_tasks) { + FML_DCHECK(fml::TimePoint::Now() >= frame_start_time); + Callback callback; std::vector secondary_callbacks; @@ -137,7 +141,7 @@ void VsyncWaiter::FireCallback(fml::TimePoint frame_start_time, ui_task_queue_id = task_runners_.GetUITaskRunner()->GetTaskQueueId(); } - task_runners_.GetUITaskRunner()->PostTaskForTime( + task_runners_.GetUITaskRunner()->PostTask( [ui_task_queue_id, callback, flow_identifier, frame_start_time, frame_target_time, pause_secondary_tasks]() { FML_TRACE_EVENT("flutter", kVsyncTraceName, "StartTime", @@ -151,8 +155,7 @@ void VsyncWaiter::FireCallback(fml::TimePoint frame_start_time, if (pause_secondary_tasks) { ResumeDartMicroTasks(ui_task_queue_id); } - }, - frame_start_time); + }); } for (auto& secondary_callback : secondary_callbacks) { diff --git a/engine/src/flutter/shell/common/vsync_waiter.h b/engine/src/flutter/shell/common/vsync_waiter.h index 998789ef5c..b381f432a9 100644 --- a/engine/src/flutter/shell/common/vsync_waiter.h +++ b/engine/src/flutter/shell/common/vsync_waiter.h @@ -65,6 +65,8 @@ class VsyncWaiter : public std::enable_shared_from_this { // as AwaitVSync(). virtual void AwaitVSyncForSecondaryCallback() { AwaitVSync(); } + // Schedules the callback on the UI task runner. Needs to be invoked as close + // to the `frame_start_time` as possible. void FireCallback(fml::TimePoint frame_start_time, fml::TimePoint frame_target_time, bool pause_secondary_tasks = true); diff --git a/engine/src/flutter/shell/common/vsync_waiter_fallback.cc b/engine/src/flutter/shell/common/vsync_waiter_fallback.cc index 8bf51c5ce2..120e9a7855 100644 --- a/engine/src/flutter/shell/common/vsync_waiter_fallback.cc +++ b/engine/src/flutter/shell/common/vsync_waiter_fallback.cc @@ -4,7 +4,10 @@ #include "flutter/shell/common/vsync_waiter_fallback.h" +#include + #include "flutter/fml/logging.h" +#include "flutter/fml/message_loop.h" #include "flutter/fml/trace_event.h" namespace flutter { @@ -35,11 +38,20 @@ void VsyncWaiterFallback::AwaitVSync() { constexpr fml::TimeDelta kSingleFrameInterval = fml::TimeDelta::FromSecondsF(1.0 / 60.0); - - auto next = + auto frame_start_time = SnapToNextTick(fml::TimePoint::Now(), phase_, kSingleFrameInterval); + auto frame_target_time = frame_start_time + kSingleFrameInterval; + std::weak_ptr weak_this = + std::static_pointer_cast(shared_from_this()); - FireCallback(next, next + kSingleFrameInterval, !for_testing_); + task_runners_.GetUITaskRunner()->PostTaskForTime( + [frame_start_time, frame_target_time, weak_this]() { + if (auto vsync_waiter = weak_this.lock()) { + vsync_waiter->FireCallback(frame_start_time, frame_target_time, + !vsync_waiter->for_testing_); + } + }, + frame_start_time); } } // namespace flutter