From d6e340346d47ac9e9332676356cf05ffb1bb64fc Mon Sep 17 00:00:00 2001 From: Ryan Macnak Date: Fri, 1 Sep 2017 11:11:25 -0700 Subject: [PATCH] Initial implementation of idle notification. (flutter/engine#4012) - Assumes only the Dart VM is interested in idle notification. - Gives the VM the time remaining in each frame. - Gives the VM 100ms if there is no pending frame. Issue flutter/flutter#9594 --- .../src/flutter/runtime/runtime_controller.cc | 9 ++++++ .../src/flutter/runtime/runtime_controller.h | 1 + engine/src/flutter/shell/common/animator.cc | 30 ++++++++++++++++--- engine/src/flutter/shell/common/animator.h | 5 +++- engine/src/flutter/shell/common/engine.cc | 6 ++++ engine/src/flutter/shell/common/engine.h | 1 + .../src/flutter/shell/common/vsync_waiter.h | 3 +- .../shell/common/vsync_waiter_fallback.cc | 6 ++-- .../android/io/flutter/view/FlutterView.java | 4 +++ .../android/io/flutter/view/VsyncWaiter.java | 7 +++-- .../platform/android/vsync_waiter_android.cc | 15 ++++++---- .../platform/android/vsync_waiter_android.h | 2 +- .../darwin/desktop/vsync_waiter_mac.cc | 12 ++++++-- .../ios/framework/Source/vsync_waiter_ios.mm | 11 +++++-- 14 files changed, 90 insertions(+), 22 deletions(-) diff --git a/engine/src/flutter/runtime/runtime_controller.cc b/engine/src/flutter/runtime/runtime_controller.cc index f998d41a84..8d04e2036c 100644 --- a/engine/src/flutter/runtime/runtime_controller.cc +++ b/engine/src/flutter/runtime/runtime_controller.cc @@ -76,6 +76,15 @@ void RuntimeController::BeginFrame(ftl::TimePoint frame_time) { GetWindow()->BeginFrame(frame_time); } +void RuntimeController::NotifyIdle(int64_t deadline) { + UIDartState* dart_state = dart_controller_->dart_state(); + if (!dart_state) { + return; + } + DartState::Scope scope(dart_state); + Dart_NotifyIdle(deadline); +} + void RuntimeController::DispatchPlatformMessage( ftl::RefPtr message) { TRACE_EVENT0("flutter", "RuntimeController::DispatchPlatformMessage"); diff --git a/engine/src/flutter/runtime/runtime_controller.h b/engine/src/flutter/runtime/runtime_controller.h index 8c95954a67..21245c2140 100644 --- a/engine/src/flutter/runtime/runtime_controller.h +++ b/engine/src/flutter/runtime/runtime_controller.h @@ -38,6 +38,7 @@ class RuntimeController : public WindowClient, public IsolateClient { void SetSemanticsEnabled(bool enabled); void BeginFrame(ftl::TimePoint frame_time); + void NotifyIdle(int64_t deadline); void DispatchPlatformMessage(ftl::RefPtr message); void DispatchPointerDataPacket(const PointerDataPacket& packet); diff --git a/engine/src/flutter/shell/common/animator.cc b/engine/src/flutter/shell/common/animator.cc index d954bdee3b..3202af2f0f 100644 --- a/engine/src/flutter/shell/common/animator.cc +++ b/engine/src/flutter/shell/common/animator.cc @@ -7,6 +7,7 @@ #include "flutter/common/threads.h" #include "flutter/fml/trace_event.h" #include "lib/ftl/time/stopwatch.h" +#include "dart/runtime/include/dart_tools_api.h" namespace shell { @@ -16,10 +17,13 @@ Animator::Animator(ftl::WeakPtr rasterizer, : rasterizer_(rasterizer), waiter_(waiter), engine_(engine), + last_begin_frame_time_(), + dart_frame_deadline_(0), layer_tree_pipeline_(ftl::MakeRefCounted(2)), pending_frame_semaphore_(1), frame_number_(1), paused_(false), + frame_scheduled_(false), weak_factory_(this) {} Animator::~Animator() = default; @@ -37,9 +41,17 @@ void Animator::Start() { RequestFrame(); } -void Animator::BeginFrame(ftl::TimePoint frame_time) { +static int64_t FtlToDartOrEarlier(ftl::TimePoint time) { + int64_t dart_now = Dart_TimelineGetMicros(); + ftl::TimePoint ftl_now = ftl::TimePoint::Now(); + return (time - ftl_now).ToMicroseconds() + dart_now; +} + +void Animator::BeginFrame(ftl::TimePoint frame_start_time, + ftl::TimePoint frame_target_time) { TRACE_EVENT_ASYNC_END0("flutter", "Frame Request Pending", frame_number_++); + frame_scheduled_ = false; pending_frame_semaphore_.Signal(); if (!producer_continuation_) { @@ -62,8 +74,15 @@ void Animator::BeginFrame(ftl::TimePoint frame_time) { // to service potential frame. FTL_DCHECK(producer_continuation_); - last_begin_frame_time_ = frame_time; + last_begin_frame_time_ = frame_start_time; + dart_frame_deadline_ = FtlToDartOrEarlier(frame_target_time); engine_->BeginFrame(last_begin_frame_time_); + + if (!frame_scheduled_) { + // We don't have another frame pending, so we're waiting on user input + // or I/O. Allow the Dart VM 100 ms. + engine_->NotifyIdle(dart_frame_deadline_ + 100000); + } } void Animator::Render(std::unique_ptr layer_tree) { @@ -111,14 +130,17 @@ void Animator::RequestFrame() { frame_number); self->AwaitVSync(); }); + frame_scheduled_ = true; } void Animator::AwaitVSync() { waiter_->AsyncWaitForVsync([self = weak_factory_.GetWeakPtr()]( - ftl::TimePoint frame_time) { + ftl::TimePoint frame_start_time, ftl::TimePoint frame_target_time) { if (self) - self->BeginFrame(frame_time); + self->BeginFrame(frame_start_time, frame_target_time); }); + + engine_->NotifyIdle(dart_frame_deadline_); } } // namespace shell diff --git a/engine/src/flutter/shell/common/animator.h b/engine/src/flutter/shell/common/animator.h index 475c1156ff..1057ec9999 100644 --- a/engine/src/flutter/shell/common/animator.h +++ b/engine/src/flutter/shell/common/animator.h @@ -35,7 +35,8 @@ class Animator { private: using LayerTreePipeline = flutter::Pipeline; - void BeginFrame(ftl::TimePoint frame_time); + void BeginFrame(ftl::TimePoint frame_start_time, + ftl::TimePoint frame_target_time); void AwaitVSync(); @@ -44,11 +45,13 @@ class Animator { Engine* engine_; ftl::TimePoint last_begin_frame_time_; + int64_t dart_frame_deadline_; ftl::RefPtr layer_tree_pipeline_; flutter::Semaphore pending_frame_semaphore_; LayerTreePipeline::ProducerContinuation producer_continuation_; int64_t frame_number_; bool paused_; + bool frame_scheduled_; ftl::WeakPtrFactory weak_factory_; diff --git a/engine/src/flutter/shell/common/engine.cc b/engine/src/flutter/shell/common/engine.cc index 4d7631887b..78b006f7a7 100644 --- a/engine/src/flutter/shell/common/engine.cc +++ b/engine/src/flutter/shell/common/engine.cc @@ -243,6 +243,12 @@ void Engine::BeginFrame(ftl::TimePoint frame_time) { runtime_->BeginFrame(frame_time); } +void Engine::NotifyIdle(int64_t deadline) { + TRACE_EVENT0("flutter", "Engine::NotifyIdle"); + if (runtime_) + runtime_->NotifyIdle(deadline); +} + void Engine::RunFromSource(const std::string& main, const std::string& packages, const std::string& bundle_path) { diff --git a/engine/src/flutter/shell/common/engine.h b/engine/src/flutter/shell/common/engine.h index 3e3b8efdb8..0ec339390c 100644 --- a/engine/src/flutter/shell/common/engine.h +++ b/engine/src/flutter/shell/common/engine.h @@ -50,6 +50,7 @@ class Engine : public blink::RuntimeDelegate { const std::string& packages); void BeginFrame(ftl::TimePoint frame_time); + void NotifyIdle(int64_t deadline); void RunFromSource(const std::string& main, const std::string& packages, diff --git a/engine/src/flutter/shell/common/vsync_waiter.h b/engine/src/flutter/shell/common/vsync_waiter.h index c061e0e7d4..ccd59d3cf2 100644 --- a/engine/src/flutter/shell/common/vsync_waiter.h +++ b/engine/src/flutter/shell/common/vsync_waiter.h @@ -13,7 +13,8 @@ namespace shell { class VsyncWaiter { public: - using Callback = std::function; + using Callback = std::function; virtual void AsyncWaitForVsync(Callback callback) = 0; diff --git a/engine/src/flutter/shell/common/vsync_waiter_fallback.cc b/engine/src/flutter/shell/common/vsync_waiter_fallback.cc index 268ee8923d..3e9d9a737b 100644 --- a/engine/src/flutter/shell/common/vsync_waiter_fallback.cc +++ b/engine/src/flutter/shell/common/vsync_waiter_fallback.cc @@ -26,12 +26,12 @@ VsyncWaiterFallback::VsyncWaiterFallback() VsyncWaiterFallback::~VsyncWaiterFallback() = default; +constexpr ftl::TimeDelta interval = ftl::TimeDelta::FromSecondsF(1.0 / 60.0); + void VsyncWaiterFallback::AsyncWaitForVsync(Callback callback) { FTL_DCHECK(!callback_); callback_ = std::move(callback); - constexpr ftl::TimeDelta interval = ftl::TimeDelta::FromSecondsF(1.0 / 60.0); - ftl::TimePoint now = ftl::TimePoint::Now(); ftl::TimePoint next = SnapToNextTick(now, phase_, interval); @@ -42,7 +42,7 @@ void VsyncWaiterFallback::AsyncWaitForVsync(Callback callback) { ftl::TimePoint frame_time = ftl::TimePoint::Now(); Callback callback = std::move(self->callback_); self->callback_ = Callback(); - callback(frame_time); + callback(frame_time, frame_time + interval); }, next - now); } diff --git a/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java b/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java index 7a8e1e51ab..5c861ea299 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java @@ -35,6 +35,7 @@ import io.flutter.plugin.common.*; import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.editing.TextInputPlugin; import io.flutter.plugin.platform.PlatformPlugin; +import io.flutter.view.VsyncWaiter; import org.json.JSONException; import org.json.JSONObject; @@ -662,6 +663,9 @@ public class FlutterView extends SurfaceView mMetrics.physicalPaddingRight, mMetrics.physicalPaddingBottom, mMetrics.physicalPaddingLeft); + + float fps = getDisplay().getRefreshRate(); + VsyncWaiter.refreshPeriodNanos = (long)(1000000000.0 / fps); } // Called by native to send us a platform message. diff --git a/engine/src/flutter/shell/platform/android/io/flutter/view/VsyncWaiter.java b/engine/src/flutter/shell/platform/android/io/flutter/view/VsyncWaiter.java index a798a67896..c7de31f860 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/view/VsyncWaiter.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/view/VsyncWaiter.java @@ -7,14 +7,17 @@ package io.flutter.view; import android.view.Choreographer; public class VsyncWaiter { + // This estimate will be updated by FlutterView when it is attached to a Display. + public static long refreshPeriodNanos = 1000000000 / 60; + public static void asyncWaitForVsync(final long cookie) { Choreographer.getInstance().postFrameCallback(new Choreographer.FrameCallback() { @Override public void doFrame(long frameTimeNanos) { - nativeOnVsync(frameTimeNanos, cookie); + nativeOnVsync(frameTimeNanos, frameTimeNanos + refreshPeriodNanos, cookie); } }); } - private static native void nativeOnVsync(long frameTimeNanos, long cookie); + private static native void nativeOnVsync(long frameTimeNanos, long frameTargetTimeNanos, long cookie); } diff --git a/engine/src/flutter/shell/platform/android/vsync_waiter_android.cc b/engine/src/flutter/shell/platform/android/vsync_waiter_android.cc index 411868a470..f9692d954e 100644 --- a/engine/src/flutter/shell/platform/android/vsync_waiter_android.cc +++ b/engine/src/flutter/shell/platform/android/vsync_waiter_android.cc @@ -37,18 +37,22 @@ void VsyncWaiterAndroid::AsyncWaitForVsync(Callback callback) { }); } -void VsyncWaiterAndroid::OnVsync(int64_t frameTimeNanos) { +void VsyncWaiterAndroid::OnVsync(int64_t frameTimeNanos, + int64_t frameTargetTimeNanos) { Callback callback = std::move(callback_); callback_ = Callback(); - blink::Threads::UI()->PostTask([callback, frameTimeNanos] { + blink::Threads::UI()->PostTask([callback, frameTimeNanos, frameTargetTimeNanos] { callback(ftl::TimePoint::FromEpochDelta( - ftl::TimeDelta::FromNanoseconds(frameTimeNanos))); + ftl::TimeDelta::FromNanoseconds(frameTimeNanos)), + ftl::TimePoint::FromEpochDelta( + ftl::TimeDelta::FromNanoseconds(frameTargetTimeNanos))); }); } static void OnNativeVsync(JNIEnv* env, jclass jcaller, jlong frameTimeNanos, + jlong frameTargetTimeNanos, jlong cookie) { // Note: The tag name must be "VSYNC" (it is special) so that the "Highlight // Vsync" checkbox in the timeline can be enabled. @@ -61,14 +65,15 @@ static void OnNativeVsync(JNIEnv* env, VsyncWaiterAndroid* waiter = weak->get(); delete weak; if (waiter) { - waiter->OnVsync(static_cast(frameTimeNanos)); + waiter->OnVsync(static_cast(frameTimeNanos), + static_cast(frameTargetTimeNanos)); } } bool VsyncWaiterAndroid::Register(JNIEnv* env) { static const JNINativeMethod methods[] = {{ .name = "nativeOnVsync", - .signature = "(JJ)V", + .signature = "(JJJ)V", .fnPtr = reinterpret_cast(&OnNativeVsync), }}; diff --git a/engine/src/flutter/shell/platform/android/vsync_waiter_android.h b/engine/src/flutter/shell/platform/android/vsync_waiter_android.h index a9cfbd2115..ef7fa35697 100644 --- a/engine/src/flutter/shell/platform/android/vsync_waiter_android.h +++ b/engine/src/flutter/shell/platform/android/vsync_waiter_android.h @@ -22,7 +22,7 @@ class VsyncWaiterAndroid : public VsyncWaiter { void AsyncWaitForVsync(Callback callback) override; - void OnVsync(int64_t frameTimeNanos); + void OnVsync(int64_t frameTimeNanos, int64_t frameTargetTimeNanos); private: Callback callback_; diff --git a/engine/src/flutter/shell/platform/darwin/desktop/vsync_waiter_mac.cc b/engine/src/flutter/shell/platform/darwin/desktop/vsync_waiter_mac.cc index 2a34d09958..b236eee4ca 100644 --- a/engine/src/flutter/shell/platform/darwin/desktop/vsync_waiter_mac.cc +++ b/engine/src/flutter/shell/platform/darwin/desktop/vsync_waiter_mac.cc @@ -40,13 +40,21 @@ void VsyncWaiterMac::OnDisplayLink(void* context) { } void VsyncWaiterMac::OnDisplayLink() { - ftl::TimePoint frame_time = ftl::TimePoint::Now(); + ftl::TimePoint frame_start_time = ftl::TimePoint::Now(); + ftl::TimePoint frame_target_time = + frame_start_time + + ftl::TimeDelta::FromSecondsF( + CVDisplayLinkGetActualOutputVideoRefreshPeriod(link_)); + CVDisplayLinkStop(link_); + auto callback = std::move(callback_); callback_ = Callback(); blink::Threads::UI()->PostTask( - [callback, frame_time] { callback(frame_time); }); + [callback, frame_start_time, frame_target_time] { + callback(frame_start_time, frame_target_time); + }); } void VsyncWaiterMac::AsyncWaitForVsync(Callback callback) { 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 060bc68f56..41686eaae9 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 @@ -47,6 +47,11 @@ } - (void)onDisplayLink:(CADisplayLink*)link { + // ftl::TimePoint and CATimeInterval both use mach_absolute_time. + ftl::TimePoint frame_start_time = ftl::TimePoint::Now(); + ftl::TimePoint frame_target_time = + ftl::TimePoint::FromEpochDelta(ftl::TimeDelta::FromSecondsF(link.targetTimestamp)); + _displayLink.paused = YES; // Note: Even though we know we are on the UI thread already (since the @@ -57,9 +62,9 @@ // // 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()); - }); + blink::Threads::UI()->PostTask([ + callback = _pendingCallback, frame_start_time, frame_target_time + ]() { callback(frame_start_time, frame_target_time); }); _pendingCallback = nullptr; }