From c17e21a794461d0fce707b872523e5b0965258f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Sharma?= <737941+loic-sharma@users.noreply.github.com> Date: Fri, 12 Apr 2024 14:45:30 -0700 Subject: [PATCH] [Windows] Refactor client wrapper to prepare for multi-view (flutter/engine#52073) _This is a refactoring with no semantic changes. No new public APIs are introduced by this change._ In the future, a new constructor will be added to `FlutterViewController` to allow it to use an existing `std::shared_ptr` to create the view controller. This updates `FlutterViewController` to store a `std::shared_ptr` and other minor tweaks. For context, we plan to not land the new multi-window `FlutterViewController` constructor until Window's multi-window support is productionized. Until then, we will maintain a [patch](https://github.com/flutter/flutter/issues/143767#issuecomment-2050741185) for folks that want to experiment with multi-window. This change makes that patch as small as possible. Design doc: [flutter.dev/go/desktop-multi-view-runner-apis](https://flutter.dev/go/desktop-multi-view-runner-apis) Part of https://github.com/flutter/flutter/issues/143767 Part of https://github.com/flutter/flutter/issues/142845 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- .../platform/windows/client_wrapper/flutter_engine.cc | 4 ++-- .../windows/client_wrapper/flutter_view_controller.cc | 2 +- .../client_wrapper/include/flutter/flutter_engine.h | 11 ++++++----- .../include/flutter/flutter_view_controller.h | 8 ++++---- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_engine.cc b/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_engine.cc index 53d32f443f..7860947aa0 100644 --- a/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_engine.cc +++ b/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_engine.cc @@ -50,7 +50,7 @@ bool FlutterEngine::Run(const char* entry_point) { std::cerr << "Cannot run an engine that failed creation." << std::endl; return false; } - if (has_been_run_) { + if (run_succeeded_) { std::cerr << "Cannot run an engine more than once." << std::endl; return false; } @@ -58,7 +58,7 @@ bool FlutterEngine::Run(const char* entry_point) { if (!run_succeeded) { std::cerr << "Failed to start engine." << std::endl; } - has_been_run_ = true; + run_succeeded_ = true; return run_succeeded; } diff --git a/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_view_controller.cc b/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_view_controller.cc index 3f59964f2d..98c65e10c2 100644 --- a/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_view_controller.cc +++ b/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_view_controller.cc @@ -12,7 +12,7 @@ namespace flutter { FlutterViewController::FlutterViewController(int width, int height, const DartProject& project) { - engine_ = std::make_unique(project); + engine_ = std::make_shared(project); controller_ = FlutterDesktopViewControllerCreate(width, height, engine_->RelinquishEngine()); if (!controller_) { diff --git a/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_engine.h b/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_engine.h index 61bda1597c..0369db35a1 100644 --- a/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_engine.h +++ b/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_engine.h @@ -95,7 +95,7 @@ class FlutterEngine : public PluginRegistry { LPARAM lparam); private: - // For access to RelinquishEngine. + // For access to the engine handle. friend class FlutterViewController; // Gives up ownership of |engine_|, but keeps a weak reference to it. @@ -113,10 +113,11 @@ class FlutterEngine : public PluginRegistry { // Whether or not this wrapper owns |engine_|. bool owns_engine_ = true; - // Whether the engine has been run. This will be true if Run has been called, - // or if RelinquishEngine has been called (since the view controller will - // run the engine if it hasn't already been run). - bool has_been_run_ = false; + // Whether |Run| has been called successfully. + // + // This is used to improve error messages. This can be false while the engine + // is running if the engine was started by creating a view. + bool run_succeeded_ = false; // The callback to execute once the next frame is drawn. std::function next_frame_callback_ = nullptr; diff --git a/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view_controller.h b/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view_controller.h index 4c658f4799..4007534a5d 100644 --- a/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view_controller.h +++ b/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view_controller.h @@ -27,10 +27,10 @@ class FlutterViewController { // Creates a FlutterView that can be parented into a Windows View hierarchy // either using HWNDs. // + // This also creates a new FlutterEngine. + // // |dart_project| will be used to configure the engine backing this view. - explicit FlutterViewController(int width, - int height, - const DartProject& project); + FlutterViewController(int width, int height, const DartProject& project); virtual ~FlutterViewController(); @@ -65,7 +65,7 @@ class FlutterViewController { FlutterDesktopViewControllerRef controller_ = nullptr; // The backing engine - std::unique_ptr engine_; + std::shared_ptr engine_; // The owned FlutterView. std::unique_ptr view_;