From 9e1d0e0f8c58f07e7efa261cdf87850a3efb4893 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Thu, 7 Mar 2019 13:16:27 -0800 Subject: [PATCH 1/2] Delay the vsync callback till the frame start time specified by embedder. 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. --- shell/common/vsync_waiter.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/shell/common/vsync_waiter.cc b/shell/common/vsync_waiter.cc index 9443fa8a55791..a117c7fff5142 100644 --- a/shell/common/vsync_waiter.cc +++ b/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 { From 150acc8a3baf78da0b115dbfe02a3ce20a36604c Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Thu, 7 Mar 2019 13:58:55 -0800 Subject: [PATCH 2/2] Clarify documentation. --- shell/platform/embedder/embedder.h | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/shell/platform/embedder/embedder.h b/shell/platform/embedder/embedder.h index 103da739a2147..bbb1a9161bc43 100644 --- a/shell/platform/embedder/embedder.h +++ b/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,