Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions shell/platform/windows/flutter_windows.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand All @@ -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(
Expand Down Expand Up @@ -83,7 +90,7 @@ FlutterDesktopEngineRef FlutterDesktopViewControllerGetEngine(

FlutterDesktopViewRef FlutterDesktopViewControllerGetView(
FlutterDesktopViewControllerRef controller) {
return controller->view_wrapper.get();
return HandleForView(controller->view.get());
}

bool FlutterDesktopViewControllerHandleTopLevelWindowProc(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -197,7 +204,7 @@ FlutterDesktopMessengerRef FlutterDesktopRegistrarGetMessenger(
void FlutterDesktopRegistrarSetDestructionHandler(
FlutterDesktopPluginRegistrarRef registrar,
FlutterDesktopOnRegistrarDestroyed callback) {
registrar->destruction_handler = callback;
registrar->engine->SetPluginRegistrarDestructionCallback(callback);
}

bool FlutterDesktopMessengerSendWithReply(FlutterDesktopMessengerRef messenger,
Expand All @@ -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;
Expand All @@ -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);
}

Expand All @@ -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);
}
21 changes: 11 additions & 10 deletions shell/platform/windows/flutter_windows_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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>();
}
Expand Down Expand Up @@ -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;
Expand All @@ -212,14 +209,18 @@ bool FlutterWindowsEngine::Stop() {

void FlutterWindowsEngine::SetView(FlutterWindowsView* view) {
view_ = view;
plugin_registrar_->view->view = view;
}

// Returns the currently configured Plugin Registrar.
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)) {
Expand Down
12 changes: 12 additions & 0 deletions shell/platform/windows/flutter_windows_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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_;
};
Expand Down
1 change: 1 addition & 0 deletions shell/platform/windows/public/flutter_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
39 changes: 13 additions & 26 deletions shell/platform/windows/window_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_