From cff370118a0abf1ba3ee88d1600b1321396c6f09 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Thu, 7 Mar 2019 16:28:22 -0800 Subject: [PATCH] Delay the vsync callback till the frame start time specified by embedder. (flutter/engine#8072) The current assumption is that the embedder will wait till the vsync event and then fire the callback. However, some embedders have that information upfront. Since the time point has already been specified by the embedder, there is no reason to burden the embedder with having to setup a wait either. --- .../src/flutter/shell/common/vsync_waiter.cc | 5 ++-- .../shell/platform/embedder/embedder.h | 26 +++++++++++++------ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/engine/src/flutter/shell/common/vsync_waiter.cc b/engine/src/flutter/shell/common/vsync_waiter.cc index 9443fa8a55..a117c7fff5 100644 --- a/engine/src/flutter/shell/common/vsync_waiter.cc +++ b/engine/src/flutter/shell/common/vsync_waiter.cc @@ -35,7 +35,7 @@ void VsyncWaiter::FireCallback(fml::TimePoint frame_start_time, return; } - task_runners_.GetUITaskRunner()->PostTask( + task_runners_.GetUITaskRunner()->PostTaskForTime( [callback, frame_start_time, frame_target_time]() { #if defined(OS_FUCHSIA) // In general, traces on Fuchsia are recorded across the whole system. @@ -49,7 +49,8 @@ void VsyncWaiter::FireCallback(fml::TimePoint frame_start_time, TRACE_EVENT0("flutter", "VSYNC"); #endif callback(frame_start_time, frame_target_time); - }); + }, + frame_start_time); } float VsyncWaiter::GetDisplayRefreshRate() const { diff --git a/engine/src/flutter/shell/platform/embedder/embedder.h b/engine/src/flutter/shell/platform/embedder/embedder.h index 103da739a2..bbb1a9161b 100644 --- a/engine/src/flutter/shell/platform/embedder/embedder.h +++ b/engine/src/flutter/shell/platform/embedder/embedder.h @@ -549,13 +549,15 @@ typedef struct { // Flutter application (such as compiled shader programs used by Skia). // This is optional. The string must be NULL terminated. const char* persistent_cache_path; - // A callback that gets invoked by the engine when it attempts to wait for - // a platform vsync event. The engine will give the platform a baton that - // needs to be returned back to the engine via |FlutterEngineOnVsync|. All - // vsync operations must occur on the thread that made the call to - // |FlutterEngineRun|. All batons must be retured to the engine before - // initializing a |FlutterEngineShutdown|. Not doing the same will result in a - // memory leak. + // A callback that gets invoked by the engine when it attempts to wait for a + // platform vsync event. The engine will give the platform a baton that needs + // to be returned back to the engine via |FlutterEngineOnVsync|. All batons + // must be retured to the engine before initializing a + // |FlutterEngineShutdown|. Not doing the same will result in a memory leak. + // While the call to |FlutterEngineOnVsync| must occur on the thread that made + // the call to |FlutterEngineRun|, the engine will make this callback on an + // internal engine-managed thread. If the components accessed on the embedder + // are not thread safe, the appropriate re-threading must be done. VsyncCallback vsync_callback; } FlutterProjectArgs; @@ -645,7 +647,15 @@ FlutterEngineResult FlutterEngineDispatchSemanticsAction( size_t data_length); // Notify the engine that a vsync event occurred. A baton passed to the -// platform via the vsync callback must be returned. +// platform via the vsync callback must be returned. This call must be made on +// the thread on which the call to |FlutterEngineRun| was made. +// +// |frame_start_time_nanos| is the point at which the vsync event occurred. +// |frame_target_time_nanos| is the point at which the embedder anticipates the +// next vsync to occur. This is a hint the engine uses to schedule Dart VM +// garbage collection in periods in which the various threads are most likely to +// be idle. For example, for a 60Hz display, embedders should add 16.6 * 1e6 to +// the frame time field. FLUTTER_EXPORT FlutterEngineResult FlutterEngineOnVsync(FlutterEngine engine, intptr_t baton,