From 043d9d2e391120ef31dad0cb1ce195715b42e8c2 Mon Sep 17 00:00:00 2001 From: ColdPaleLight <31977171+ColdPaleLight@users.noreply.github.com> Date: Fri, 18 Feb 2022 05:24:10 +0800 Subject: [PATCH] Add an embedder.h API for scheduling frame (flutter/engine#31304) --- .../shell/common/fixtures/shell_test.dart | 8 +++++ .../src/flutter/shell/common/platform_view.cc | 4 +++ .../src/flutter/shell/common/platform_view.h | 12 +++++++ engine/src/flutter/shell/common/shell.cc | 13 ++++++++ engine/src/flutter/shell/common/shell.h | 3 ++ .../flutter/shell/common/shell_unittests.cc | 32 +++++++++++++++++++ .../Source/FlutterEnginePlatformViewTest.mm | 1 + .../Source/FlutterPlatformViewsTest.mm | 1 + .../Source/accessibility_bridge_test.mm | 1 + .../shell/platform/embedder/embedder.cc | 13 ++++++++ .../shell/platform/embedder/embedder.h | 14 ++++++++ .../platform/embedder/embedder_engine.cc | 13 ++++++++ .../shell/platform/embedder/embedder_engine.h | 2 ++ .../platform/embedder/fixtures/main.dart | 8 +++++ .../embedder/tests/embedder_unittests.cc | 28 ++++++++++++++++ .../fuchsia/flutter/platform_view_unittest.cc | 2 ++ .../tests/flatland_platform_view_unittest.cc | 2 ++ 17 files changed, 157 insertions(+) diff --git a/engine/src/flutter/shell/common/fixtures/shell_test.dart b/engine/src/flutter/shell/common/fixtures/shell_test.dart index fdbaf066a0..337c771a00 100644 --- a/engine/src/flutter/shell/common/fixtures/shell_test.dart +++ b/engine/src/flutter/shell/common/fixtures/shell_test.dart @@ -262,3 +262,11 @@ void canReceiveArgumentsWhenEngineRun(List args) { void canReceiveArgumentsWhenEngineSpawn(List args) { notifyNativeWhenEngineSpawn(args.length == 2 && args[0] == 'arg1' && args[1] == 'arg2'); } + +@pragma('vm:entry-point') +void canScheduleFrameFromPlatform() { + PlatformDispatcher.instance.onBeginFrame = (Duration beginTime) { + nativeOnBeginFrame(beginTime.inMicroseconds); + }; + notifyNative(); +} diff --git a/engine/src/flutter/shell/common/platform_view.cc b/engine/src/flutter/shell/common/platform_view.cc index 81c43c301d..71dbd26829 100644 --- a/engine/src/flutter/shell/common/platform_view.cc +++ b/engine/src/flutter/shell/common/platform_view.cc @@ -83,6 +83,10 @@ void PlatformView::NotifyDestroyed() { delegate_.OnPlatformViewDestroyed(); } +void PlatformView::ScheduleFrame() { + delegate_.OnPlatformViewScheduleFrame(); +} + sk_sp PlatformView::CreateResourceContext() const { FML_DLOG(WARNING) << "This platform does not set up the resource " "context on the IO thread for async texture uploads."; diff --git a/engine/src/flutter/shell/common/platform_view.h b/engine/src/flutter/shell/common/platform_view.h index 060e9ccce9..3e8ab2201b 100644 --- a/engine/src/flutter/shell/common/platform_view.h +++ b/engine/src/flutter/shell/common/platform_view.h @@ -72,6 +72,12 @@ class PlatformView { /// virtual void OnPlatformViewDestroyed() = 0; + //-------------------------------------------------------------------------- + /// @brief Notifies the delegate that the platform needs to schedule a + /// frame to regenerate the layer tree and redraw the surface. + /// + virtual void OnPlatformViewScheduleFrame() = 0; + //-------------------------------------------------------------------------- /// @brief Notifies the delegate that the specified callback needs to /// be invoked after the rasterizer is done rendering the next @@ -477,6 +483,12 @@ class PlatformView { /// virtual void NotifyDestroyed(); + //---------------------------------------------------------------------------- + /// @brief Used by embedders to schedule a frame. In response to this + /// call, the framework may need to start generating a new frame. + /// + void ScheduleFrame(); + //---------------------------------------------------------------------------- /// @brief Used by the shell to obtain a Skia GPU context that is capable /// of operating on the IO thread. The context must be in the same diff --git a/engine/src/flutter/shell/common/shell.cc b/engine/src/flutter/shell/common/shell.cc index aa0d8b2b5c..7ae9ff7a98 100644 --- a/engine/src/flutter/shell/common/shell.cc +++ b/engine/src/flutter/shell/common/shell.cc @@ -879,6 +879,19 @@ void Shell::OnPlatformViewDestroyed() { rasterizer_->TeardownExternalViewEmbedder(); } +// |PlatformView::Delegate| +void Shell::OnPlatformViewScheduleFrame() { + TRACE_EVENT0("flutter", "Shell::OnPlatformViewScheduleFrame"); + FML_DCHECK(is_setup_); + FML_DCHECK(task_runners_.GetPlatformTaskRunner()->RunsTasksOnCurrentThread()); + + task_runners_.GetUITaskRunner()->PostTask([engine = engine_->GetWeakPtr()]() { + if (engine) { + engine->ScheduleFrame(); + } + }); +} + // |PlatformView::Delegate| void Shell::OnPlatformViewSetViewportMetrics(const ViewportMetrics& metrics) { FML_DCHECK(is_setup_); diff --git a/engine/src/flutter/shell/common/shell.h b/engine/src/flutter/shell/common/shell.h index 9864bbd858..099743e6be 100644 --- a/engine/src/flutter/shell/common/shell.h +++ b/engine/src/flutter/shell/common/shell.h @@ -518,6 +518,9 @@ class Shell final : public PlatformView::Delegate, // |PlatformView::Delegate| void OnPlatformViewDestroyed() override; + // |PlatformView::Delegate| + void OnPlatformViewScheduleFrame() override; + // |PlatformView::Delegate| void OnPlatformViewSetViewportMetrics( const ViewportMetrics& metrics) override; diff --git a/engine/src/flutter/shell/common/shell_unittests.cc b/engine/src/flutter/shell/common/shell_unittests.cc index 45007218b5..34c459678a 100644 --- a/engine/src/flutter/shell/common/shell_unittests.cc +++ b/engine/src/flutter/shell/common/shell_unittests.cc @@ -55,6 +55,8 @@ class MockPlatformViewDelegate : public PlatformView::Delegate { MOCK_METHOD0(OnPlatformViewDestroyed, void()); + MOCK_METHOD0(OnPlatformViewScheduleFrame, void()); + MOCK_METHOD1(OnPlatformViewSetNextFrameCallback, void(const fml::closure& closure)); @@ -1828,6 +1830,36 @@ TEST_F(ShellTest, IsolateCanAccessPersistentIsolateData) { DestroyShell(std::move(shell), std::move(task_runners)); } +TEST_F(ShellTest, CanScheduleFrameFromPlatform) { + Settings settings = CreateSettingsForFixture(); + TaskRunners task_runners = GetTaskRunnersForFixture(); + fml::AutoResetWaitableEvent latch; + AddNativeCallback( + "NotifyNative", + CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) { latch.Signal(); })); + fml::AutoResetWaitableEvent check_latch; + AddNativeCallback("NativeOnBeginFrame", + CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) { + check_latch.Signal(); + })); + std::unique_ptr shell = + CreateShell(std::move(settings), std::move(task_runners)); + ASSERT_TRUE(shell->IsSetup()); + + auto configuration = RunConfiguration::InferFromSettings(settings); + configuration.SetEntrypoint("canScheduleFrameFromPlatform"); + RunEngine(shell.get(), std::move(configuration)); + + // Wait for the application to attach the listener. + latch.Wait(); + + fml::TaskRunner::RunNowOrPostTask( + shell->GetTaskRunners().GetPlatformTaskRunner(), + [&shell]() { shell->GetPlatformView()->ScheduleFrame(); }); + check_latch.Wait(); + DestroyShell(std::move(shell), std::move(task_runners)); +} + static void LogSkData(sk_sp data, const char* title) { FML_LOG(ERROR) << "---------- " << title; std::ostringstream ostr; diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEnginePlatformViewTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEnginePlatformViewTest.mm index 4a4a8877ce..9ed0f2ecb5 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEnginePlatformViewTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEnginePlatformViewTest.mm @@ -21,6 +21,7 @@ namespace { class FakeDelegate : public PlatformView::Delegate { void OnPlatformViewCreated(std::unique_ptr surface) override {} void OnPlatformViewDestroyed() override {} + void OnPlatformViewScheduleFrame() override {} void OnPlatformViewSetNextFrameCallback(const fml::closure& closure) override {} void OnPlatformViewSetViewportMetrics(const ViewportMetrics& metrics) override {} void OnPlatformViewDispatchPlatformMessage(std::unique_ptr message) override {} diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViewsTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViewsTest.mm index a7d5a25a46..690fc35073 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViewsTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViewsTest.mm @@ -90,6 +90,7 @@ namespace { class FlutterPlatformViewsTestMockPlatformViewDelegate : public PlatformView::Delegate { void OnPlatformViewCreated(std::unique_ptr surface) override {} void OnPlatformViewDestroyed() override {} + void OnPlatformViewScheduleFrame() override {} void OnPlatformViewSetNextFrameCallback(const fml::closure& closure) override {} void OnPlatformViewSetViewportMetrics(const ViewportMetrics& metrics) override {} void OnPlatformViewDispatchPlatformMessage(std::unique_ptr message) override {} diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm index b8e6ca5fdb..d322184804 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm @@ -74,6 +74,7 @@ namespace { class MockDelegate : public PlatformView::Delegate { void OnPlatformViewCreated(std::unique_ptr surface) override {} void OnPlatformViewDestroyed() override {} + void OnPlatformViewScheduleFrame() override {} void OnPlatformViewSetNextFrameCallback(const fml::closure& closure) override {} void OnPlatformViewSetViewportMetrics(const ViewportMetrics& metrics) override {} void OnPlatformViewDispatchPlatformMessage(std::unique_ptr message) override {} diff --git a/engine/src/flutter/shell/platform/embedder/embedder.cc b/engine/src/flutter/shell/platform/embedder/embedder.cc index 5fd038b792..f55e52c895 100644 --- a/engine/src/flutter/shell/platform/embedder/embedder.cc +++ b/engine/src/flutter/shell/platform/embedder/embedder.cc @@ -2550,6 +2550,18 @@ FlutterEngineResult FlutterEngineNotifyDisplayUpdate( } } +FlutterEngineResult FlutterEngineScheduleFrame(FLUTTER_API_SYMBOL(FlutterEngine) + engine) { + if (engine == nullptr) { + return LOG_EMBEDDER_ERROR(kInvalidArguments, "Invalid engine handle."); + } + + return reinterpret_cast(engine)->ScheduleFrame() + ? kSuccess + : LOG_EMBEDDER_ERROR(kInvalidArguments, + "Could not schedule frame."); +} + FlutterEngineResult FlutterEngineGetProcAddresses( FlutterEngineProcTable* table) { if (!table) { @@ -2600,6 +2612,7 @@ FlutterEngineResult FlutterEngineGetProcAddresses( SET_PROC(PostCallbackOnAllNativeThreads, FlutterEnginePostCallbackOnAllNativeThreads); SET_PROC(NotifyDisplayUpdate, FlutterEngineNotifyDisplayUpdate); + SET_PROC(ScheduleFrame, FlutterEngineScheduleFrame); #undef SET_PROC return kSuccess; diff --git a/engine/src/flutter/shell/platform/embedder/embedder.h b/engine/src/flutter/shell/platform/embedder/embedder.h index b37d0f6261..d20f939796 100644 --- a/engine/src/flutter/shell/platform/embedder/embedder.h +++ b/engine/src/flutter/shell/platform/embedder/embedder.h @@ -2389,6 +2389,17 @@ FlutterEngineResult FlutterEngineNotifyDisplayUpdate( const FlutterEngineDisplay* displays, size_t display_count); +//------------------------------------------------------------------------------ +/// @brief Schedule a new frame to redraw the content. +/// +/// @param[in] engine A running engine instance. +/// +/// @return the result of the call made to the engine. +/// +FLUTTER_EXPORT +FlutterEngineResult FlutterEngineScheduleFrame(FLUTTER_API_SYMBOL(FlutterEngine) + engine); + #endif // !FLUTTER_ENGINE_NO_PROTOTYPES // Typedefs for the function pointers in FlutterEngineProcTable. @@ -2505,6 +2516,8 @@ typedef FlutterEngineResult (*FlutterEngineNotifyDisplayUpdateFnPtr)( FlutterEngineDisplaysUpdateType update_type, const FlutterEngineDisplay* displays, size_t display_count); +typedef FlutterEngineResult (*FlutterEngineScheduleFrameFnPtr)( + FLUTTER_API_SYMBOL(FlutterEngine) engine); /// Function-pointer-based versions of the APIs above. typedef struct { @@ -2549,6 +2562,7 @@ typedef struct { FlutterEnginePostCallbackOnAllNativeThreadsFnPtr PostCallbackOnAllNativeThreads; FlutterEngineNotifyDisplayUpdateFnPtr NotifyDisplayUpdate; + FlutterEngineScheduleFrameFnPtr ScheduleFrame; } FlutterEngineProcTable; //------------------------------------------------------------------------------ diff --git a/engine/src/flutter/shell/platform/embedder/embedder_engine.cc b/engine/src/flutter/shell/platform/embedder/embedder_engine.cc index 5d8d785d17..1108e3e45c 100644 --- a/engine/src/flutter/shell/platform/embedder/embedder_engine.cc +++ b/engine/src/flutter/shell/platform/embedder/embedder_engine.cc @@ -273,6 +273,19 @@ bool EmbedderEngine::PostTaskOnEngineManagedNativeThreads( return true; } +bool EmbedderEngine::ScheduleFrame() { + if (!IsValid()) { + return false; + } + + auto platform_view = shell_->GetPlatformView(); + if (!platform_view) { + return false; + } + platform_view->ScheduleFrame(); + return true; +} + Shell& EmbedderEngine::GetShell() { FML_DCHECK(shell_); return *shell_.get(); diff --git a/engine/src/flutter/shell/platform/embedder/embedder_engine.h b/engine/src/flutter/shell/platform/embedder/embedder_engine.h index d65bc81973..1351856951 100644 --- a/engine/src/flutter/shell/platform/embedder/embedder_engine.h +++ b/engine/src/flutter/shell/platform/embedder/embedder_engine.h @@ -81,6 +81,8 @@ class EmbedderEngine { bool PostTaskOnEngineManagedNativeThreads( std::function closure) const; + bool ScheduleFrame(); + Shell& GetShell(); private: diff --git a/engine/src/flutter/shell/platform/embedder/fixtures/main.dart b/engine/src/flutter/shell/platform/embedder/fixtures/main.dart index d4c1f261ba..8aa9a24d1b 100644 --- a/engine/src/flutter/shell/platform/embedder/fixtures/main.dart +++ b/engine/src/flutter/shell/platform/embedder/fixtures/main.dart @@ -991,3 +991,11 @@ void invalid_backingstore() { }; PlatformDispatcher.instance.scheduleFrame(); } + +@pragma('vm:entry-point') +void can_schedule_frame() { + PlatformDispatcher.instance.onBeginFrame = (Duration beginTime){ + signalNativeCount(beginTime.inMicroseconds); + }; + signalNativeTest(); +} diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests.cc b/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests.cc index 850e453c63..ab695cf1a9 100644 --- a/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests.cc +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests.cc @@ -1744,6 +1744,34 @@ TEST_F(EmbedderTest, VsyncCallbackPostedIntoFuture) { shutdown_latch.Wait(); } +TEST_F(EmbedderTest, CanScheduleFrame) { + auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext); + EmbedderConfigBuilder builder(context); + builder.SetSoftwareRendererConfig(); + builder.SetDartEntrypoint("can_schedule_frame"); + fml::AutoResetWaitableEvent latch; + context.AddNativeCallback( + "SignalNativeTest", + CREATE_NATIVE_ENTRY( + [&latch](Dart_NativeArguments args) { latch.Signal(); })); + + fml::AutoResetWaitableEvent check_latch; + context.AddNativeCallback( + "SignalNativeCount", + CREATE_NATIVE_ENTRY( + [&check_latch](Dart_NativeArguments args) { check_latch.Signal(); })); + + auto engine = builder.LaunchEngine(); + ASSERT_TRUE(engine.is_valid()); + + // Wait for the application to attach the listener. + latch.Wait(); + + ASSERT_EQ(FlutterEngineScheduleFrame(engine.get()), kSuccess); + + check_latch.Wait(); +} + } // namespace testing } // namespace flutter diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view_unittest.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view_unittest.cc index 8654ba3dc1..9e6e92586e 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view_unittest.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/platform_view_unittest.cc @@ -85,6 +85,8 @@ class MockPlatformViewDelegate : public flutter::PlatformView::Delegate { // |flutter::PlatformView::Delegate| void OnPlatformViewDestroyed() {} // |flutter::PlatformView::Delegate| + void OnPlatformViewScheduleFrame() {} + // |flutter::PlatformView::Delegate| void OnPlatformViewSetNextFrameCallback(const fml::closure& closure) {} // |flutter::PlatformView::Delegate| void OnPlatformViewSetViewportMetrics( diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/tests/flatland_platform_view_unittest.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/tests/flatland_platform_view_unittest.cc index c32445bb14..78d2ffb9c7 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/tests/flatland_platform_view_unittest.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/tests/flatland_platform_view_unittest.cc @@ -84,6 +84,8 @@ class MockPlatformViewDelegate : public flutter::PlatformView::Delegate { // |flutter::PlatformView::Delegate| void OnPlatformViewDestroyed() {} // |flutter::PlatformView::Delegate| + void OnPlatformViewScheduleFrame() {} + // |flutter::PlatformView::Delegate| void OnPlatformViewSetNextFrameCallback(const fml::closure& closure) {} // |flutter::PlatformView::Delegate| void OnPlatformViewSetViewportMetrics(