From ab823bd15e8c7b91be7798dc1b67da70aba44a73 Mon Sep 17 00:00:00 2001 From: stuartmorgan Date: Thu, 20 Aug 2020 16:50:06 -0700 Subject: [PATCH] [windows] Mostly eliminate the state structs (flutter/engine#20662) The Windows embedding was based on the GLFW embedding, which grew organically from a singe-file implementation that used structs to manage all of the important state. It is in the process of being converted to a cleaner object-based architecture, but currently it is a hybrid of objects and structs that have redundant data, making it very prone to errors of forgetting to update pointers in multiple locations. This reduces the remaining structs to only a single pointer to the larger object that manages the responsibilities that handle is associated with, so that there is no need to wire things together in multiple places. For now they continue to exist as projections of the larger objects, but that will be eliminated over time by having an object structure that better reflects the API structure. Fixes https://github.com/flutter/flutter/issues/64250 --- .../shell/platform/windows/flutter_windows.cc | 40 +++++++++++-------- .../windows/flutter_windows_engine.cc | 21 +++++----- .../platform/windows/flutter_windows_engine.h | 12 ++++++ .../platform/windows/public/flutter_windows.h | 1 + .../shell/platform/windows/window_state.h | 39 ++++++------------ 5 files changed, 61 insertions(+), 52 deletions(-) diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows.cc b/engine/src/flutter/shell/platform/windows/flutter_windows.cc index 1e997da80d..1871021e40 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows.cc @@ -24,7 +24,6 @@ #include "flutter/shell/platform/windows/flutter_windows_view.h" #include "flutter/shell/platform/windows/win32_dpi_utils.h" #include "flutter/shell/platform/windows/win32_flutter_window.h" -#include "flutter/shell/platform/windows/win32_platform_handler.h" #include "flutter/shell/platform/windows/win32_task_runner.h" #include "flutter/shell/platform/windows/window_binding_handler.h" #include "flutter/shell/platform/windows/window_state.h" @@ -37,12 +36,22 @@ static flutter::FlutterWindowsEngine* EngineFromHandle( return reinterpret_cast(ref); } -// Returns opaque API handle for the given engine instance. +// Returns the opaque API handle for the given engine instance. static FlutterDesktopEngineRef HandleForEngine( flutter::FlutterWindowsEngine* engine) { return reinterpret_cast(engine); } +// Returns the view corresponding to the given opaque API handle. +static flutter::FlutterWindowsView* ViewFromHandle(FlutterDesktopViewRef ref) { + return reinterpret_cast(ref); +} + +// Returns the opaque API handle for the given view instance. +static FlutterDesktopViewRef HandleForView(flutter::FlutterWindowsView* view) { + return reinterpret_cast(view); +} + FlutterDesktopViewControllerRef FlutterDesktopViewControllerCreate( int width, int height, @@ -54,8 +63,6 @@ FlutterDesktopViewControllerRef FlutterDesktopViewControllerCreate( state->view = std::make_unique(std::move(window_wrapper)); state->view->CreateRenderSurface(); - state->view_wrapper = std::make_unique(); - state->view_wrapper->view = state->view.get(); // Take ownership of the engine, starting it if necessary. state->view->SetEngine( @@ -83,7 +90,7 @@ FlutterDesktopEngineRef FlutterDesktopViewControllerGetEngine( FlutterDesktopViewRef FlutterDesktopViewControllerGetView( FlutterDesktopViewControllerRef controller) { - return controller->view_wrapper.get(); + return HandleForView(controller->view.get()); } bool FlutterDesktopViewControllerHandleTopLevelWindowProc( @@ -144,13 +151,13 @@ FlutterDesktopMessengerRef FlutterDesktopEngineGetMessenger( return EngineFromHandle(engine)->messenger(); } -HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef view_ref) { - return std::get(*view_ref->view->GetRenderTarget()); +HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef view) { + return std::get(*ViewFromHandle(view)->GetRenderTarget()); } FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetView( FlutterDesktopPluginRegistrarRef registrar) { - return registrar->view.get(); + return HandleForView(registrar->engine->view()); } void FlutterDesktopPluginRegistrarRegisterTopLevelWindowProcDelegate( @@ -197,7 +204,7 @@ FlutterDesktopMessengerRef FlutterDesktopRegistrarGetMessenger( void FlutterDesktopRegistrarSetDestructionHandler( FlutterDesktopPluginRegistrarRef registrar, FlutterDesktopOnRegistrarDestroyed callback) { - registrar->destruction_handler = callback; + registrar->engine->SetPluginRegistrarDestructionCallback(callback); } bool FlutterDesktopMessengerSendWithReply(FlutterDesktopMessengerRef messenger, @@ -209,7 +216,7 @@ bool FlutterDesktopMessengerSendWithReply(FlutterDesktopMessengerRef messenger, FlutterPlatformMessageResponseHandle* response_handle = nullptr; if (reply != nullptr && user_data != nullptr) { FlutterEngineResult result = FlutterPlatformMessageCreateResponseHandle( - messenger->engine, reply, user_data, &response_handle); + messenger->engine->engine(), reply, user_data, &response_handle); if (result != kSuccess) { std::cout << "Failed to create response handle\n"; return false; @@ -224,11 +231,11 @@ bool FlutterDesktopMessengerSendWithReply(FlutterDesktopMessengerRef messenger, response_handle, }; - FlutterEngineResult message_result = - FlutterEngineSendPlatformMessage(messenger->engine, &platform_message); + FlutterEngineResult message_result = FlutterEngineSendPlatformMessage( + messenger->engine->engine(), &platform_message); if (response_handle != nullptr) { - FlutterPlatformMessageReleaseResponseHandle(messenger->engine, + FlutterPlatformMessageReleaseResponseHandle(messenger->engine->engine(), response_handle); } @@ -248,13 +255,14 @@ void FlutterDesktopMessengerSendResponse( const FlutterDesktopMessageResponseHandle* handle, const uint8_t* data, size_t data_length) { - FlutterEngineSendPlatformMessageResponse(messenger->engine, handle, data, - data_length); + FlutterEngineSendPlatformMessageResponse(messenger->engine->engine(), handle, + data, data_length); } void FlutterDesktopMessengerSetCallback(FlutterDesktopMessengerRef messenger, const char* channel, FlutterDesktopMessageCallback callback, void* user_data) { - messenger->dispatcher->SetMessageCallback(channel, callback, user_data); + messenger->engine->message_dispatcher()->SetMessageCallback(channel, callback, + user_data); } diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc index 5090eb4c35..76af353ab0 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc @@ -103,17 +103,14 @@ FlutterWindowsEngine::FlutterWindowsEngine(const FlutterProjectBundle& project) } }); - // Set up the structure of the state/handle objects; engine and view - // paramaters will be filled in later. + // Set up the legacy structs backing the API handles. messenger_ = std::make_unique(); - message_dispatcher_ = - std::make_unique(messenger_.get()); - messenger_->dispatcher = message_dispatcher_.get(); - + messenger_->engine = this; plugin_registrar_ = std::make_unique(); plugin_registrar_->engine = this; - plugin_registrar_->view = std::make_unique(); + message_dispatcher_ = + std::make_unique(messenger_.get()); window_proc_delegate_manager_ = std::make_unique(); } @@ -200,8 +197,8 @@ bool FlutterWindowsEngine::RunWithEntrypoint(const char* entrypoint) { bool FlutterWindowsEngine::Stop() { if (engine_) { - if (plugin_registrar_ && plugin_registrar_->destruction_handler) { - plugin_registrar_->destruction_handler(plugin_registrar_.get()); + if (plugin_registrar_destruction_callback_) { + plugin_registrar_destruction_callback_(plugin_registrar_.get()); } FlutterEngineResult result = FlutterEngineShutdown(engine_); engine_ = nullptr; @@ -212,7 +209,6 @@ bool FlutterWindowsEngine::Stop() { void FlutterWindowsEngine::SetView(FlutterWindowsView* view) { view_ = view; - plugin_registrar_->view->view = view; } // Returns the currently configured Plugin Registrar. @@ -220,6 +216,11 @@ FlutterDesktopPluginRegistrarRef FlutterWindowsEngine::GetRegistrar() { return plugin_registrar_.get(); } +void FlutterWindowsEngine::SetPluginRegistrarDestructionCallback( + FlutterDesktopOnRegistrarDestroyed callback) { + plugin_registrar_destruction_callback_ = callback; +} + void FlutterWindowsEngine::HandlePlatformMessage( const FlutterPlatformMessage* engine_message) { if (engine_message->struct_size != sizeof(FlutterPlatformMessage)) { diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h index 7381eb162e..d18a414ee5 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h @@ -61,10 +61,18 @@ class FlutterWindowsEngine { // Returns the currently configured Plugin Registrar. FlutterDesktopPluginRegistrarRef GetRegistrar(); + // Sets |callback| to be called when the plugin registrar is destroyed. + void SetPluginRegistrarDestructionCallback( + FlutterDesktopOnRegistrarDestroyed callback); + FLUTTER_API_SYMBOL(FlutterEngine) engine() { return engine_; } FlutterDesktopMessengerRef messenger() { return messenger_.get(); } + IncomingMessageDispatcher* message_dispatcher() { + return message_dispatcher_.get(); + } + Win32TaskRunner* task_runner() { return task_runner_.get(); } Win32WindowProcDelegateManager* window_proc_delegate_manager() { @@ -105,6 +113,10 @@ class FlutterWindowsEngine { // The plugin registrar handle given to API clients. std::unique_ptr plugin_registrar_; + // A callback to be called when the engine (and thus the plugin registrar) + // is being destroyed. + FlutterDesktopOnRegistrarDestroyed plugin_registrar_destruction_callback_; + // The manager for WindowProc delegate registration and callbacks. std::unique_ptr window_proc_delegate_manager_; }; diff --git a/engine/src/flutter/shell/platform/windows/public/flutter_windows.h b/engine/src/flutter/shell/platform/windows/public/flutter_windows.h index 56eefb281f..3a636e60cb 100644 --- a/engine/src/flutter/shell/platform/windows/public/flutter_windows.h +++ b/engine/src/flutter/shell/platform/windows/public/flutter_windows.h @@ -22,6 +22,7 @@ typedef struct FlutterDesktopViewControllerState* FlutterDesktopViewControllerRef; // Opaque reference to a Flutter window. +struct FlutterDesktopView; typedef struct FlutterDesktopView* FlutterDesktopViewRef; // Opaque reference to a Flutter engine instance. diff --git a/engine/src/flutter/shell/platform/windows/window_state.h b/engine/src/flutter/shell/platform/windows/window_state.h index c26a26680f..3092a7bc0b 100644 --- a/engine/src/flutter/shell/platform/windows/window_state.h +++ b/engine/src/flutter/shell/platform/windows/window_state.h @@ -9,48 +9,35 @@ #include "flutter/shell/platform/common/cpp/incoming_message_dispatcher.h" #include "flutter/shell/platform/embedder/embedder.h" +// Structs backing the opaque references used in the C API. +// +// DO NOT ADD ANY NEW CODE HERE. These are legacy, and are being phased out +// in favor of objects that own and manage the relevant functionality. + namespace flutter { struct FlutterWindowsEngine; struct FlutterWindowsView; } // namespace flutter -// Struct for storing state within an instance of the windows native (HWND or -// CoreWindow) Window. +// Wrapper to distinguish the view controller ref from the view ref given out +// in the C API. struct FlutterDesktopViewControllerState { // The view that backs this state object. std::unique_ptr view; - - // The window handle given to API clients. - std::unique_ptr view_wrapper; }; -// Opaque reference for the native windows itself. This is separate from the -// controller so that it can be provided to plugins without giving them access -// to all of the controller-based functionality. -struct FlutterDesktopView { - // The view that (indirectly) owns this state object. - flutter::FlutterWindowsView* view = nullptr; -}; - -// State associated with the plugin registrar. +// Wrapper to distinguish the plugin registrar ref from the engine ref given out +// in the C API. struct FlutterDesktopPluginRegistrar { // The engine that owns this state object. flutter::FlutterWindowsEngine* engine = nullptr; - - // The handle for the view associated with this registrar. - std::unique_ptr view; - - // Callback to be called on registrar destruction. - FlutterDesktopOnRegistrarDestroyed destruction_handler; }; -// State associated with the messenger used to communicate with the engine. +// Wrapper to distinguish the messenger ref from the engine ref given out +// in the C API. struct FlutterDesktopMessenger { - // The Flutter engine this messenger sends outgoing messages to. - FLUTTER_API_SYMBOL(FlutterEngine) engine; - - // The message dispatcher for handling incoming messages. - flutter::IncomingMessageDispatcher* dispatcher; + // The engine that owns this state object. + flutter::FlutterWindowsEngine* engine = nullptr; }; #endif // FLUTTER_SHELL_PLATFORM_WINDOWS_FLUTTER_WINDOW_STATE_H_