[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
This commit is contained in:
stuartmorgan
2020-08-20 16:50:06 -07:00
committed by GitHub
parent cee423eb73
commit ab823bd15e
5 changed files with 61 additions and 52 deletions

View File

@@ -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<flutter::FlutterWindowsEngine*>(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<FlutterDesktopEngineRef>(engine);
}
// Returns the view corresponding to the given opaque API handle.
static flutter::FlutterWindowsView* ViewFromHandle(FlutterDesktopViewRef ref) {
return reinterpret_cast<flutter::FlutterWindowsView*>(ref);
}
// Returns the opaque API handle for the given view instance.
static FlutterDesktopViewRef HandleForView(flutter::FlutterWindowsView* view) {
return reinterpret_cast<FlutterDesktopViewRef>(view);
}
FlutterDesktopViewControllerRef FlutterDesktopViewControllerCreate(
int width,
int height,
@@ -54,8 +63,6 @@ FlutterDesktopViewControllerRef FlutterDesktopViewControllerCreate(
state->view =
std::make_unique<flutter::FlutterWindowsView>(std::move(window_wrapper));
state->view->CreateRenderSurface();
state->view_wrapper = std::make_unique<FlutterDesktopView>();
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<HWND>(*view_ref->view->GetRenderTarget());
HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef view) {
return std::get<HWND>(*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);
}

View File

@@ -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<FlutterDesktopMessenger>();
message_dispatcher_ =
std::make_unique<IncomingMessageDispatcher>(messenger_.get());
messenger_->dispatcher = message_dispatcher_.get();
messenger_->engine = this;
plugin_registrar_ = std::make_unique<FlutterDesktopPluginRegistrar>();
plugin_registrar_->engine = this;
plugin_registrar_->view = std::make_unique<FlutterDesktopView>();
message_dispatcher_ =
std::make_unique<IncomingMessageDispatcher>(messenger_.get());
window_proc_delegate_manager_ =
std::make_unique<Win32WindowProcDelegateManager>();
}
@@ -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)) {

View File

@@ -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<FlutterDesktopPluginRegistrar> 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<Win32WindowProcDelegateManager> window_proc_delegate_manager_;
};

View File

@@ -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.

View File

@@ -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<flutter::FlutterWindowsView> view;
// The window handle given to API clients.
std::unique_ptr<FlutterDesktopView> 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<FlutterDesktopView> 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_