From a0e4467f4dc1653a50b17c53e4fcdeb2d93145c7 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Wed, 11 Jan 2017 14:58:06 -0800 Subject: [PATCH] iOS: Ensure that task observers for VSync callbacks are not bypassed in VSyncWaiterIOS. (flutter/engine#3334) --- .../ios/framework/Source/vsync_waiter_ios.mm | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/vsync_waiter_ios.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/vsync_waiter_ios.mm index 5cb01970d5..fb9bda0d33 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/vsync_waiter_ios.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/vsync_waiter_ios.mm @@ -52,11 +52,21 @@ - (void)onDisplayLink:(CADisplayLink*)link { _traceCounter = !_traceCounter; TRACE_COUNTER1("flutter", "OnDisplayLink", _traceCounter); - ftl::TimePoint frame_time = ftl::TimePoint::Now(); _displayLink.paused = YES; - auto callback = std::move(_pendingCallback); - _pendingCallback = shell::VsyncWaiter::Callback(); - callback(frame_time); + + // Note: Even though we know we are on the UI thread already (since the + // display link was scheduled on the UI thread in the contructor), we use + // the PostTask mechanism because the callback may have side-effects that need + // to be addressed via a task observer. Invoking the callback by itself + // bypasses such task observers. + // + // We are not using the PostTask for thread switching, but to make task + // observers work. + blink::Threads::UI()->PostTask([callback = _pendingCallback]() { + callback(ftl::TimePoint::Now()); + }); + + _pendingCallback = nullptr; } - (void)dealloc { @@ -77,7 +87,7 @@ VsyncWaiterIOS::~VsyncWaiterIOS() { } void VsyncWaiterIOS::AsyncWaitForVsync(Callback callback) { - [client_ await:std::move(callback)]; + [client_ await:callback]; } } // namespace shell