diff --git a/engine/src/flutter/shell/common/animator.cc b/engine/src/flutter/shell/common/animator.cc index b65e1c64a6..a668448afb 100644 --- a/engine/src/flutter/shell/common/animator.cc +++ b/engine/src/flutter/shell/common/animator.cc @@ -199,8 +199,9 @@ void Animator::Render(std::unique_ptr layer_tree) { std::move(frame_timings_recorder_)); } -const VsyncWaiter& Animator::GetVsyncWaiter() const { - return *waiter_.get(); +const std::weak_ptr Animator::GetVsyncWaiter() const { + std::weak_ptr weak = waiter_; + return weak; } bool Animator::CanReuseLastLayerTree() { diff --git a/engine/src/flutter/shell/common/animator.h b/engine/src/flutter/shell/common/animator.h index ab6e2c253d..23bf222478 100644 --- a/engine/src/flutter/shell/common/animator.h +++ b/engine/src/flutter/shell/common/animator.h @@ -57,7 +57,7 @@ class Animator final { void Render(std::unique_ptr layer_tree); - const VsyncWaiter& GetVsyncWaiter() const; + const std::weak_ptr GetVsyncWaiter() const; //-------------------------------------------------------------------------- /// @brief Schedule a secondary callback to be executed right after the diff --git a/engine/src/flutter/shell/common/engine.cc b/engine/src/flutter/shell/common/engine.cc index fed5bbd28b..c550ae2be1 100644 --- a/engine/src/flutter/shell/common/engine.cc +++ b/engine/src/flutter/shell/common/engine.cc @@ -557,7 +557,7 @@ void Engine::LoadDartDeferredLibraryError(intptr_t loading_unit_id, } } -const VsyncWaiter& Engine::GetVsyncWaiter() const { +const std::weak_ptr Engine::GetVsyncWaiter() const { return animator_->GetVsyncWaiter(); } diff --git a/engine/src/flutter/shell/common/engine.h b/engine/src/flutter/shell/common/engine.h index 85ae92d64f..96c703950c 100644 --- a/engine/src/flutter/shell/common/engine.h +++ b/engine/src/flutter/shell/common/engine.h @@ -870,7 +870,7 @@ class Engine final : public RuntimeDelegate, PointerDataDispatcher::Delegate { return runtime_controller_.get(); } - const VsyncWaiter& GetVsyncWaiter() const; + const std::weak_ptr GetVsyncWaiter() const; private: // |RuntimeDelegate| diff --git a/engine/src/flutter/shell/common/shell.cc b/engine/src/flutter/shell/common/shell.cc index 8cc7eb3178..04efda8ba2 100644 --- a/engine/src/flutter/shell/common/shell.cc +++ b/engine/src/flutter/shell/common/shell.cc @@ -1915,7 +1915,7 @@ Shell::GetPlatformMessageHandler() const { return platform_message_handler_; } -const VsyncWaiter& Shell::GetVsyncWaiter() const { +const std::weak_ptr Shell::GetVsyncWaiter() const { return engine_->GetVsyncWaiter(); } diff --git a/engine/src/flutter/shell/common/shell.h b/engine/src/flutter/shell/common/shell.h index 5aeb9c1085..562d64ded2 100644 --- a/engine/src/flutter/shell/common/shell.h +++ b/engine/src/flutter/shell/common/shell.h @@ -401,7 +401,7 @@ class Shell final : public PlatformView::Delegate, const std::shared_ptr& GetPlatformMessageHandler() const; - const VsyncWaiter& GetVsyncWaiter() const; + const std::weak_ptr GetVsyncWaiter() const; private: using ServiceProtocolHandler = diff --git a/engine/src/flutter/shell/common/variable_refresh_rate_display.cc b/engine/src/flutter/shell/common/variable_refresh_rate_display.cc index 4957fbdee4..6fda9cde45 100644 --- a/engine/src/flutter/shell/common/variable_refresh_rate_display.cc +++ b/engine/src/flutter/shell/common/variable_refresh_rate_display.cc @@ -5,21 +5,30 @@ #include "flutter/shell/common/variable_refresh_rate_display.h" #include "flutter/fml/logging.h" +static double GetInitialRefreshRate( + const std::weak_ptr& + refresh_rate_reporter) { + if (auto reporter = refresh_rate_reporter.lock()) { + return reporter->GetRefreshRate(); + } + return 0; +} + namespace flutter { VariableRefreshRateDisplay::VariableRefreshRateDisplay( DisplayId display_id, - const VariableRefreshRateReporter& refresh_rate_reporter) - : Display(display_id, refresh_rate_reporter.GetRefreshRate()), + const std::weak_ptr refresh_rate_reporter) + : Display(display_id, GetInitialRefreshRate(refresh_rate_reporter)), refresh_rate_reporter_(refresh_rate_reporter) {} VariableRefreshRateDisplay::VariableRefreshRateDisplay( - const VariableRefreshRateReporter& refresh_rate_reporter) - : Display(refresh_rate_reporter.GetRefreshRate()), + const std::weak_ptr refresh_rate_reporter) + : Display(GetInitialRefreshRate(refresh_rate_reporter)), refresh_rate_reporter_(refresh_rate_reporter) {} double VariableRefreshRateDisplay::GetRefreshRate() const { - return refresh_rate_reporter_.GetRefreshRate(); + return GetInitialRefreshRate(refresh_rate_reporter_); } } // namespace flutter diff --git a/engine/src/flutter/shell/common/variable_refresh_rate_display.h b/engine/src/flutter/shell/common/variable_refresh_rate_display.h index 5ba0a571af..b26f1014bc 100644 --- a/engine/src/flutter/shell/common/variable_refresh_rate_display.h +++ b/engine/src/flutter/shell/common/variable_refresh_rate_display.h @@ -18,16 +18,16 @@ class VariableRefreshRateDisplay : public Display { public: explicit VariableRefreshRateDisplay( DisplayId display_id, - const VariableRefreshRateReporter& refresh_rate_reporter); + const std::weak_ptr refresh_rate_reporter); explicit VariableRefreshRateDisplay( - const VariableRefreshRateReporter& refresh_rate_reporter); + const std::weak_ptr refresh_rate_reporter); ~VariableRefreshRateDisplay() = default; // |Display| double GetRefreshRate() const override; private: - const VariableRefreshRateReporter& refresh_rate_reporter_; + const std::weak_ptr refresh_rate_reporter_; FML_DISALLOW_COPY_AND_ASSIGN(VariableRefreshRateDisplay); }; diff --git a/engine/src/flutter/shell/common/variable_refresh_rate_display_unittests.cc b/engine/src/flutter/shell/common/variable_refresh_rate_display_unittests.cc index 25d24afc09..e65641264d 100644 --- a/engine/src/flutter/shell/common/variable_refresh_rate_display_unittests.cc +++ b/engine/src/flutter/shell/common/variable_refresh_rate_display_unittests.cc @@ -11,19 +11,37 @@ namespace flutter { namespace testing { TEST(VariableRefreshRateDisplayTest, ReportCorrectInitialRefreshRate) { - auto refresh_rate_reporter = std::make_unique(60); - auto display = - flutter::VariableRefreshRateDisplay(*refresh_rate_reporter.get()); + auto refresh_rate_reporter = std::make_shared(60); + auto display = flutter::VariableRefreshRateDisplay( + std::weak_ptr(refresh_rate_reporter)); ASSERT_EQ(display.GetRefreshRate(), 60); } TEST(VariableRefreshRateDisplayTest, ReportCorrectRefreshRateWhenUpdated) { - auto refresh_rate_reporter = std::make_unique(60); - auto display = - flutter::VariableRefreshRateDisplay(*refresh_rate_reporter.get()); + auto refresh_rate_reporter = std::make_shared(60); + auto display = flutter::VariableRefreshRateDisplay( + std::weak_ptr(refresh_rate_reporter)); refresh_rate_reporter->UpdateRefreshRate(30); ASSERT_EQ(display.GetRefreshRate(), 30); } +TEST(VariableRefreshRateDisplayTest, + Report0IfReporterSharedPointerIsDestroyedAfterDisplayCreation) { + auto refresh_rate_reporter = std::make_shared(60); + auto display = flutter::VariableRefreshRateDisplay( + std::weak_ptr(refresh_rate_reporter)); + refresh_rate_reporter.reset(); + ASSERT_EQ(display.GetRefreshRate(), 0); +} + +TEST(VariableRefreshRateDisplayTest, + Report0IfReporterSharedPointerIsDestroyedBeforeDisplayCreation) { + auto refresh_rate_reporter = std::make_shared(60); + refresh_rate_reporter.reset(); + auto display = flutter::VariableRefreshRateDisplay( + std::weak_ptr(refresh_rate_reporter)); + ASSERT_EQ(display.GetRefreshRate(), 0); +} + } // namespace testing } // namespace flutter diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm index fabec4e111..2105e4027a 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm @@ -757,8 +757,8 @@ static void SetEntryPoint(flutter::Settings* settings, NSString* entrypoint, NSS } - (void)initializeDisplays { - const flutter::VsyncWaiterIOS& vsync_waiter_ios = - static_cast(_shell->GetVsyncWaiter()); + auto vsync_waiter = std::shared_ptr(_shell->GetVsyncWaiter().lock()); + auto vsync_waiter_ios = std::static_pointer_cast(vsync_waiter); std::vector> displays; displays.push_back(std::make_unique(vsync_waiter_ios)); _shell->OnDisplayUpdates(flutter::DisplayUpdateType::kStartup, std::move(displays));