[windows] Rework controller/engine interaction in the API (flutter/engine#20266)

Changes the interaction between the view controller and engine in both the C API and
the engine API, so that there's always an engine (as on other platforms) rather than
the engine APIs being specific to headless mode.

While adjusting the C API, this does a large cleanup:
- Renames all methods to follow a `FlutterDesktop` (prefix) + "class" name + method-style name.
  E.g., `FlutterDestkopViewControllerCreate` rather than `FlutterDesktopCreateViewController`.
  This makes it easier to see what functions operate on which conceptual "object" in the API.
- Reorders and groups them by the object they operate on.

Fixes https://github.com/flutter/flutter/issues/61966
This commit is contained in:
stuartmorgan
2020-08-10 09:41:18 -07:00
committed by GitHub
parent b5e486230b
commit e297b7d731
17 changed files with 346 additions and 248 deletions

View File

@@ -5,13 +5,13 @@
#ifndef FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_PLUGIN_REGISTRAR_H_
#define FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_PLUGIN_REGISTRAR_H_
#include <flutter_plugin_registrar.h>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <flutter_plugin_registrar.h>
#include "binary_messenger.h"
namespace flutter {
@@ -48,11 +48,8 @@ class PluginRegistrar {
// that they stay valid for any registered callbacks.
void AddPlugin(std::unique_ptr<Plugin> plugin);
// Enables input blocking on the given channel name.
//
// If set, then the parent window should disable input callbacks
// while waiting for the handler for messages on that channel to run.
void EnableInputBlockingForChannel(const std::string& channel);
protected:
FlutterDesktopPluginRegistrarRef registrar() { return registrar_; }
private:
// Handle for interacting with the C API's registrar.

View File

@@ -140,11 +140,6 @@ void PluginRegistrar::AddPlugin(std::unique_ptr<Plugin> plugin) {
plugins_.insert(std::move(plugin));
}
void PluginRegistrar::EnableInputBlockingForChannel(
const std::string& channel) {
FlutterDesktopRegistrarEnableInputBlocking(registrar_, channel.c_str());
}
// ===== PluginRegistrarManager =====
// static

View File

@@ -78,6 +78,8 @@ executable("client_wrapper_glfw_unittests") {
"flutter_window_unittests.cc",
]
defines = [ "FLUTTER_DESKTOP_LIBRARY" ]
deps = [
":client_wrapper_glfw",
":client_wrapper_glfw_fixtures",

View File

@@ -5,10 +5,10 @@
#ifndef FLUTTER_SHELL_PLATFORM_GLFW_CLIENT_WRAPPER_INCLUDE_FLUTTER_PLUGIN_REGISTRAR_GLFW_H_
#define FLUTTER_SHELL_PLATFORM_GLFW_CLIENT_WRAPPER_INCLUDE_FLUTTER_PLUGIN_REGISTRAR_GLFW_H_
#include <memory>
#include <flutter_glfw.h>
#include <memory>
#include "flutter_window.h"
#include "plugin_registrar.h"
@@ -34,6 +34,14 @@ class PluginRegistrarGlfw : public PluginRegistrar {
FlutterWindow* window() { return window_.get(); }
// Enables input blocking on the given channel name.
//
// If set, then the parent window should disable input callbacks
// while waiting for the handler for messages on that channel to run.
void EnableInputBlockingForChannel(const std::string& channel) {
FlutterDesktopRegistrarEnableInputBlocking(registrar(), channel.c_str());
}
private:
// The owned FlutterWindow, if any.
std::unique_ptr<FlutterWindow> window_;

View File

@@ -79,6 +79,9 @@ executable("client_wrapper_windows_unittests") {
"plugin_registrar_windows_unittests.cc",
]
# Set embedder.h to export, not import, since the stubs are locally defined.
defines = [ "FLUTTER_DESKTOP_LIBRARY" ]
deps = [
":client_wrapper_library_stubs_windows",
":client_wrapper_windows",

View File

@@ -9,29 +9,14 @@
namespace flutter {
FlutterEngine::FlutterEngine(const DartProject& project)
: project_(std::make_unique<DartProject>(project)) {}
FlutterEngine::~FlutterEngine() {
ShutDown();
}
bool FlutterEngine::Run(const char* entry_point) {
if (engine_) {
std::cerr << "Cannot run an already running engine. Create a new instance "
"or call ShutDown first."
<< std::endl;
return false;
}
FlutterEngine::FlutterEngine(const DartProject& project) {
FlutterDesktopEngineProperties c_engine_properties = {};
c_engine_properties.assets_path = project_->assets_path().c_str();
c_engine_properties.icu_data_path = project_->icu_data_path().c_str();
c_engine_properties.aot_library_path = project_->aot_library_path().c_str();
c_engine_properties.entry_point = entry_point;
c_engine_properties.assets_path = project.assets_path().c_str();
c_engine_properties.icu_data_path = project.icu_data_path().c_str();
c_engine_properties.aot_library_path = project.aot_library_path().c_str();
std::vector<const char*> engine_switches;
std::transform(
project_->engine_switches().begin(), project_->engine_switches().end(),
project.engine_switches().begin(), project.engine_switches().end(),
std::back_inserter(engine_switches),
[](const std::string& arg) -> const char* { return arg.c_str(); });
if (engine_switches.size() > 0) {
@@ -39,21 +24,39 @@ bool FlutterEngine::Run(const char* entry_point) {
c_engine_properties.switches_count = engine_switches.size();
}
engine_ = UniqueEnginePtr(FlutterDesktopRunEngine(c_engine_properties),
FlutterDesktopShutDownEngine);
engine_ = FlutterDesktopEngineCreate(c_engine_properties);
}
FlutterEngine::~FlutterEngine() {
ShutDown();
}
bool FlutterEngine::Run(const char* entry_point) {
if (!engine_) {
std::cerr << "Failed to start engine." << std::endl;
std::cerr << "Cannot run an engine that failed creation." << std::endl;
return false;
}
return true;
if (has_been_run_) {
std::cerr << "Cannot run an engine more than once." << std::endl;
return false;
}
bool run_succeeded = FlutterDesktopEngineRun(engine_, entry_point);
if (!run_succeeded) {
std::cerr << "Failed to start engine." << std::endl;
}
has_been_run_ = true;
return run_succeeded;
}
void FlutterEngine::ShutDown() {
if (engine_ && owns_engine_) {
FlutterDesktopEngineDestroy(engine_);
}
engine_ = nullptr;
}
std::chrono::nanoseconds FlutterEngine::ProcessMessages() {
return std::chrono::nanoseconds(FlutterDesktopProcessMessages(engine_.get()));
return std::chrono::nanoseconds(FlutterDesktopEngineProcessMessages(engine_));
}
FlutterDesktopPluginRegistrarRef FlutterEngine::GetRegistrarForPlugin(
@@ -64,7 +67,12 @@ FlutterDesktopPluginRegistrarRef FlutterEngine::GetRegistrarForPlugin(
<< std::endl;
return nullptr;
}
return FlutterDesktopGetPluginRegistrar(engine_.get(), plugin_name.c_str());
return FlutterDesktopEngineGetPluginRegistrar(engine_, plugin_name.c_str());
}
FlutterDesktopEngineRef FlutterEngine::RelinquishEngine() {
owns_engine_ = false;
return engine_;
}
} // namespace flutter

View File

@@ -17,28 +17,37 @@ namespace {
class TestFlutterWindowsApi : public testing::StubFlutterWindowsApi {
public:
// |flutter::testing::StubFlutterWindowsApi|
FlutterDesktopEngineRef RunEngine(
const FlutterDesktopEngineProperties& properties) override {
run_called_ = true;
FlutterDesktopEngineRef EngineCreate(
const FlutterDesktopEngineProperties& engine_properties) {
create_called_ = true;
return reinterpret_cast<FlutterDesktopEngineRef>(1);
}
// |flutter::testing::StubFlutterWindowsApi|
uint64_t ProcessMessages() override { return 99; }
// |flutter::testing::StubFlutterWindowsApi|
bool ShutDownEngine() override {
shut_down_called_ = true;
bool EngineRun(const char* entry_point) override {
run_called_ = true;
return true;
}
// |flutter::testing::StubFlutterWindowsApi|
bool EngineDestroy() override {
destroy_called_ = true;
return true;
}
// |flutter::testing::StubFlutterWindowsApi|
uint64_t EngineProcessMessages() override { return 99; }
bool create_called() { return create_called_; }
bool run_called() { return run_called_; }
bool shut_down_called() { return shut_down_called_; }
bool destroy_called() { return destroy_called_; }
private:
bool create_called_ = false;
bool run_called_ = false;
bool shut_down_called_ = false;
bool destroy_called_ = false;
};
} // namespace
@@ -50,11 +59,12 @@ TEST(FlutterEngineTest, CreateDestroy) {
{
FlutterEngine engine(DartProject(L"fake/project/path"));
engine.Run();
EXPECT_EQ(test_api->create_called(), true);
EXPECT_EQ(test_api->run_called(), true);
EXPECT_EQ(test_api->shut_down_called(), false);
EXPECT_EQ(test_api->destroy_called(), false);
}
// Destroying should implicitly shut down if it hasn't been done manually.
EXPECT_EQ(test_api->shut_down_called(), true);
EXPECT_EQ(test_api->destroy_called(), true);
}
TEST(FlutterEngineTest, ExplicitShutDown) {
@@ -64,10 +74,11 @@ TEST(FlutterEngineTest, ExplicitShutDown) {
FlutterEngine engine(DartProject(L"fake/project/path"));
engine.Run();
EXPECT_EQ(test_api->create_called(), true);
EXPECT_EQ(test_api->run_called(), true);
EXPECT_EQ(test_api->shut_down_called(), false);
EXPECT_EQ(test_api->destroy_called(), false);
engine.ShutDown();
EXPECT_EQ(test_api->shut_down_called(), true);
EXPECT_EQ(test_api->destroy_called(), true);
}
TEST(FlutterEngineTest, ProcessMessages) {

View File

@@ -12,46 +12,30 @@ namespace flutter {
FlutterViewController::FlutterViewController(int width,
int height,
const DartProject& project) {
std::vector<const char*> switches;
std::transform(
project.engine_switches().begin(), project.engine_switches().end(),
std::back_inserter(switches),
[](const std::string& arg) -> const char* { return arg.c_str(); });
size_t switch_count = switches.size();
FlutterDesktopEngineProperties properties = {};
properties.assets_path = project.assets_path().c_str();
properties.icu_data_path = project.icu_data_path().c_str();
// It is harmless to pass this in non-AOT mode.
properties.aot_library_path = project.aot_library_path().c_str();
properties.switches = switch_count > 0 ? switches.data() : nullptr;
properties.switches_count = switch_count;
controller_ = FlutterDesktopCreateViewController(width, height, properties);
engine_ = std::make_unique<FlutterEngine>(project);
controller_ = FlutterDesktopViewControllerCreate(width, height,
engine_->RelinquishEngine());
if (!controller_) {
std::cerr << "Failed to create view controller." << std::endl;
return;
}
view_ = std::make_unique<FlutterView>(FlutterDesktopGetView(controller_));
engine_ = FlutterDesktopGetEngine(controller_);
view_ = std::make_unique<FlutterView>(
FlutterDesktopViewControllerGetView(controller_));
}
FlutterViewController::~FlutterViewController() {
if (controller_) {
FlutterDesktopDestroyViewController(controller_);
FlutterDesktopViewControllerDestroy(controller_);
}
}
std::chrono::nanoseconds FlutterViewController::ProcessMessages() {
return std::chrono::nanoseconds(FlutterDesktopProcessMessages(engine_));
return engine_->ProcessMessages();
}
FlutterDesktopPluginRegistrarRef FlutterViewController::GetRegistrarForPlugin(
const std::string& plugin_name) {
if (!engine_) {
std::cerr << "Cannot get plugin registrar without an engine." << std::endl;
return nullptr;
}
return FlutterDesktopGetPluginRegistrar(engine_, plugin_name.c_str());
return engine_->GetRegistrarForPlugin(plugin_name);
}
} // namespace flutter

View File

@@ -15,12 +15,36 @@ namespace {
// Stub implementation to validate calls to the API.
class TestWindowsApi : public testing::StubFlutterWindowsApi {
FlutterDesktopViewControllerRef CreateViewController(
public:
// |flutter::testing::StubFlutterWindowsApi|
FlutterDesktopViewControllerRef ViewControllerCreate(
int width,
int height,
const FlutterDesktopEngineProperties& engine_properties) override {
return reinterpret_cast<FlutterDesktopViewControllerRef>(1);
FlutterDesktopEngineRef engine) override {
return reinterpret_cast<FlutterDesktopViewControllerRef>(2);
}
// |flutter::testing::StubFlutterWindowsApi|
void ViewControllerDestroy() override { view_controller_destroyed_ = true; }
// |flutter::testing::StubFlutterWindowsApi|
FlutterDesktopEngineRef EngineCreate(
const FlutterDesktopEngineProperties& engine_properties) override {
return reinterpret_cast<FlutterDesktopEngineRef>(1);
}
// |flutter::testing::StubFlutterWindowsApi|
bool EngineDestroy() override {
engine_destroyed_ = true;
return true;
}
bool engine_destroyed() { return engine_destroyed_; }
bool view_controller_destroyed() { return view_controller_destroyed_; }
private:
bool engine_destroyed_ = false;
bool view_controller_destroyed_ = false;
};
} // namespace
@@ -31,6 +55,19 @@ TEST(FlutterViewControllerTest, CreateDestroy) {
std::make_unique<TestWindowsApi>());
auto test_api = static_cast<TestWindowsApi*>(scoped_api_stub.stub());
{ FlutterViewController controller(100, 100, project); }
EXPECT_TRUE(test_api->view_controller_destroyed());
// Per the C API, once a view controller has taken ownership of an engine
// the engine destruction method should not be called.
EXPECT_FALSE(test_api->engine_destroyed());
}
TEST(FlutterViewControllerTest, GetEngine) {
DartProject project(L"data");
testing::ScopedStubFlutterWindowsApi scoped_api_stub(
std::make_unique<TestWindowsApi>());
auto test_api = static_cast<TestWindowsApi*>(scoped_api_stub.stub());
FlutterViewController controller(100, 100, project);
EXPECT_NE(controller.engine(), nullptr);
}
TEST(FlutterViewControllerTest, GetView) {

View File

@@ -34,9 +34,12 @@ class FlutterEngine : public PluginRegistry {
FlutterEngine(FlutterEngine const&) = delete;
FlutterEngine& operator=(FlutterEngine const&) = delete;
// Starts running the engine.
// Starts running the engine, with an optional entry point.
//
// If the optional entry point is not provided, defaults to main().
// If provided, entry_point must be the name of a top-level function from the
// same Dart library that contains the app's main() function, and must be
// decorated with `@pragma(vm:entry-point)` to ensure the method is not
// tree-shaken by the Dart compiler. If not provided, defaults to main().
bool Run(const char* entry_point = nullptr);
// Terminates the running engine.
@@ -55,14 +58,25 @@ class FlutterEngine : public PluginRegistry {
const std::string& plugin_name) override;
private:
using UniqueEnginePtr =
std::unique_ptr<FlutterDesktopEngine, bool (*)(FlutterDesktopEngine*)>;
// For access to RelinquishEngine.
friend class FlutterViewController;
// Gives up ownership of |engine_|, but keeps a weak reference to it.
//
// This is intended to be used by FlutterViewController, since the underlying
// C API for view controllers takes over engine ownership.
FlutterDesktopEngineRef RelinquishEngine();
// Handle for interacting with the C API's engine reference.
UniqueEnginePtr engine_ =
UniqueEnginePtr(nullptr, FlutterDesktopShutDownEngine);
FlutterDesktopEngineRef engine_ = nullptr;
std::unique_ptr<DartProject> project_;
// 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;
};
} // namespace flutter

View File

@@ -7,11 +7,8 @@
#include <flutter_windows.h>
#include <chrono>
#include <string>
#include <vector>
#include "dart_project.h"
#include "flutter_engine.h"
#include "flutter_view.h"
#include "plugin_registrar.h"
#include "plugin_registry.h"
@@ -39,17 +36,16 @@ class FlutterViewController : public PluginRegistry {
FlutterViewController(FlutterViewController const&) = delete;
FlutterViewController& operator=(FlutterViewController const&) = delete;
// Returns the engine running Flutter content in this view.
FlutterEngine* engine() { return engine_.get(); }
// Returns the view managed by this controller.
FlutterView* view() { return view_.get(); }
// Processes any pending events in the Flutter engine, and returns the
// nanosecond delay until the next scheduled event (or max, if none).
//
// This should be called on every run of the application-level runloop, and
// a wait for native events in the runloop should never be longer than the
// last return value from this function.
// DEPRECATED. Call engine()->ProcessMessages() instead.
std::chrono::nanoseconds ProcessMessages();
// flutter::PluginRegistry:
// DEPRECATED. Call engine()->GetRegistrarForPlugin() instead.
FlutterDesktopPluginRegistrarRef GetRegistrarForPlugin(
const std::string& plugin_name) override;
@@ -57,8 +53,8 @@ class FlutterViewController : public PluginRegistry {
// Handle for interacting with the C API's view controller, if any.
FlutterDesktopViewControllerRef controller_ = nullptr;
// |controller_|'s engine.
FlutterDesktopEngineRef engine_ = nullptr;
// The backing engine
std::unique_ptr<FlutterEngine> engine_;
// The owned FlutterView.
std::unique_ptr<FlutterView> view_;

View File

@@ -24,7 +24,7 @@ class PluginRegistrarWindows : public PluginRegistrar {
FlutterDesktopPluginRegistrarRef core_registrar)
: PluginRegistrar(core_registrar) {
view_ = std::make_unique<FlutterView>(
FlutterDesktopRegistrarGetView(core_registrar));
FlutterDesktopPluginRegistrarGetView(core_registrar));
}
virtual ~PluginRegistrarWindows() = default;

View File

@@ -35,43 +35,72 @@ ScopedStubFlutterWindowsApi::~ScopedStubFlutterWindowsApi() {
// Forwarding dummy implementations of the C API.
FlutterDesktopViewControllerRef FlutterDesktopCreateViewController(
FlutterDesktopViewControllerRef FlutterDesktopViewControllerCreate(
int width,
int height,
const FlutterDesktopEngineProperties& engine_properties) {
FlutterDesktopEngineRef engine) {
if (s_stub_implementation) {
return s_stub_implementation->CreateViewController(width, height,
engine_properties);
return s_stub_implementation->ViewControllerCreate(width, height, engine);
}
return nullptr;
}
void FlutterDesktopDestroyViewController(
void FlutterDesktopViewControllerDestroy(
FlutterDesktopViewControllerRef controller) {
if (s_stub_implementation) {
s_stub_implementation->DestroyViewController();
s_stub_implementation->ViewControllerDestroy();
}
}
FlutterDesktopViewRef FlutterDesktopGetView(
FlutterDesktopViewControllerRef controller) {
// The stub ignores this, so just return an arbitrary non-zero value.
return reinterpret_cast<FlutterDesktopViewRef>(1);
}
FlutterDesktopEngineRef FlutterDesktopGetEngine(
FlutterDesktopEngineRef FlutterDesktopViewControllerGetEngine(
FlutterDesktopViewControllerRef controller) {
// The stub ignores this, so just return an arbitrary non-zero value.
return reinterpret_cast<FlutterDesktopEngineRef>(1);
}
uint64_t FlutterDesktopProcessMessages(FlutterDesktopEngineRef engine) {
FlutterDesktopViewRef FlutterDesktopViewControllerGetView(
FlutterDesktopViewControllerRef controller) {
// The stub ignores this, so just return an arbitrary non-zero value.
return reinterpret_cast<FlutterDesktopViewRef>(1);
}
FlutterDesktopEngineRef FlutterDesktopEngineCreate(
const FlutterDesktopEngineProperties& engine_properties) {
if (s_stub_implementation) {
return s_stub_implementation->ProcessMessages();
return s_stub_implementation->EngineCreate(engine_properties);
}
return nullptr;
}
bool FlutterDesktopEngineDestroy(FlutterDesktopEngineRef engine_ref) {
if (s_stub_implementation) {
return s_stub_implementation->EngineDestroy();
}
return true;
}
bool FlutterDesktopEngineRun(FlutterDesktopEngineRef engine,
const char* entry_point) {
if (s_stub_implementation) {
return s_stub_implementation->EngineRun(entry_point);
}
return true;
}
uint64_t FlutterDesktopEngineProcessMessages(FlutterDesktopEngineRef engine) {
if (s_stub_implementation) {
return s_stub_implementation->EngineProcessMessages();
}
return 0;
}
FlutterDesktopPluginRegistrarRef FlutterDesktopEngineGetPluginRegistrar(
FlutterDesktopEngineRef engine,
const char* plugin_name) {
// The stub ignores this, so just return an arbitrary non-zero value.
return reinterpret_cast<FlutterDesktopPluginRegistrarRef>(1);
}
HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef controller) {
if (s_stub_implementation) {
return s_stub_implementation->ViewGetHWND();
@@ -79,29 +108,7 @@ HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef controller) {
return reinterpret_cast<HWND>(-1);
}
FlutterDesktopEngineRef FlutterDesktopRunEngine(
const FlutterDesktopEngineProperties& engine_properties) {
if (s_stub_implementation) {
return s_stub_implementation->RunEngine(engine_properties);
}
return nullptr;
}
bool FlutterDesktopShutDownEngine(FlutterDesktopEngineRef engine_ref) {
if (s_stub_implementation) {
return s_stub_implementation->ShutDownEngine();
}
return true;
}
FlutterDesktopPluginRegistrarRef FlutterDesktopGetPluginRegistrar(
FlutterDesktopEngineRef engine,
const char* plugin_name) {
// The stub ignores this, so just return an arbitrary non-zero value.
return reinterpret_cast<FlutterDesktopPluginRegistrarRef>(1);
}
FlutterDesktopViewRef FlutterDesktopRegistrarGetView(
FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetView(
FlutterDesktopPluginRegistrarRef controller) {
// The stub ignores this, so just return an arbitrary non-zero value.
return reinterpret_cast<FlutterDesktopViewRef>(1);

View File

@@ -29,31 +29,32 @@ class StubFlutterWindowsApi {
virtual ~StubFlutterWindowsApi() {}
// Called for FlutterDesktopCreateViewController.
virtual FlutterDesktopViewControllerRef CreateViewController(
int width,
int height,
// Called for FlutterDesktopViewControllerCreate.
virtual FlutterDesktopViewControllerRef
ViewControllerCreate(int width, int height, FlutterDesktopEngineRef engine) {
return nullptr;
}
// Called for FlutterDesktopViewControllerDestroy.
virtual void ViewControllerDestroy() {}
// Called for FlutterDesktopEngineCreate.
virtual FlutterDesktopEngineRef EngineCreate(
const FlutterDesktopEngineProperties& engine_properties) {
return nullptr;
}
// Called for FlutterDesktopDestroyView.
virtual void DestroyViewController() {}
// Called for FlutterDesktopEngineDestroy.
virtual bool EngineDestroy() { return true; }
// Called for FlutterDesktopProcessMessages.
virtual uint64_t ProcessMessages() { return 0; }
// Called for FlutterDesktopEngineRun.
virtual bool EngineRun(const char* entry_point) { return true; }
// Called for FlutterDesktopEngineProcessMessages.
virtual uint64_t EngineProcessMessages() { return 0; }
// Called for FlutterDesktopViewGetHWND.
virtual HWND ViewGetHWND() { return reinterpret_cast<HWND>(1); }
// Called for FlutterDesktopRunEngine.
virtual FlutterDesktopEngineRef RunEngine(
const FlutterDesktopEngineProperties& engine_properties) {
return nullptr;
}
// Called for FlutterDesktopShutDownEngine.
virtual bool ShutDownEngine() { return true; }
};
// A test helper that owns a stub implementation, making it the test stub for

View File

@@ -43,10 +43,10 @@ static FlutterDesktopEngineRef HandleForEngine(
return reinterpret_cast<FlutterDesktopEngineRef>(engine);
}
FlutterDesktopViewControllerRef FlutterDesktopCreateViewController(
FlutterDesktopViewControllerRef FlutterDesktopViewControllerCreate(
int width,
int height,
const FlutterDesktopEngineProperties& engine_properties) {
FlutterDesktopEngineRef engine) {
std::unique_ptr<flutter::WindowBindingHandler> window_wrapper =
std::make_unique<flutter::Win32FlutterWindow>(width, height);
@@ -57,34 +57,59 @@ FlutterDesktopViewControllerRef FlutterDesktopCreateViewController(
state->view_wrapper = std::make_unique<FlutterDesktopView>();
state->view_wrapper->view = state->view.get();
flutter::FlutterProjectBundle project(engine_properties);
auto engine = std::make_unique<flutter::FlutterWindowsEngine>(project);
if (!engine) {
return nullptr;
}
state->view->SetEngine(std::move(engine));
if (!state->view->GetEngine()->RunWithEntrypoint(
engine_properties.entry_point)) {
return nullptr;
// Take ownership of the engine, starting it if necessary.
state->view->SetEngine(
std::unique_ptr<flutter::FlutterWindowsEngine>(EngineFromHandle(engine)));
if (!state->view->GetEngine()->running()) {
if (!state->view->GetEngine()->RunWithEntrypoint(nullptr)) {
return nullptr;
}
}
return state.release();
}
uint64_t FlutterDesktopProcessMessages(FlutterDesktopEngineRef engine) {
return EngineFromHandle(engine)->task_runner()->ProcessTasks().count();
}
void FlutterDesktopDestroyViewController(
void FlutterDesktopViewControllerDestroy(
FlutterDesktopViewControllerRef controller) {
delete controller;
}
FlutterDesktopEngineRef FlutterDesktopGetEngine(
FlutterDesktopEngineRef FlutterDesktopViewControllerGetEngine(
FlutterDesktopViewControllerRef controller) {
return HandleForEngine(controller->view->GetEngine());
}
FlutterDesktopPluginRegistrarRef FlutterDesktopGetPluginRegistrar(
FlutterDesktopViewRef FlutterDesktopViewControllerGetView(
FlutterDesktopViewControllerRef controller) {
return controller->view_wrapper.get();
}
FlutterDesktopEngineRef FlutterDesktopEngineCreate(
const FlutterDesktopEngineProperties& engine_properties) {
flutter::FlutterProjectBundle project(engine_properties);
auto engine = std::make_unique<flutter::FlutterWindowsEngine>(project);
return HandleForEngine(engine.release());
}
bool FlutterDesktopEngineDestroy(FlutterDesktopEngineRef engine_ref) {
flutter::FlutterWindowsEngine* engine = EngineFromHandle(engine_ref);
bool result = true;
if (engine->running()) {
result = engine->Stop();
}
delete engine;
return result;
}
bool FlutterDesktopEngineRun(FlutterDesktopEngineRef engine,
const char* entry_point) {
return EngineFromHandle(engine)->RunWithEntrypoint(entry_point);
}
uint64_t FlutterDesktopEngineProcessMessages(FlutterDesktopEngineRef engine) {
return EngineFromHandle(engine)->task_runner()->ProcessTasks().count();
}
FlutterDesktopPluginRegistrarRef FlutterDesktopEngineGetPluginRegistrar(
FlutterDesktopEngineRef engine,
const char* plugin_name) {
// Currently, one registrar acts as the registrar for all plugins, so the
@@ -94,15 +119,15 @@ FlutterDesktopPluginRegistrarRef FlutterDesktopGetPluginRegistrar(
return EngineFromHandle(engine)->GetRegistrar();
}
FlutterDesktopViewRef FlutterDesktopGetView(
FlutterDesktopViewControllerRef controller) {
return controller->view_wrapper.get();
}
HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef view_ref) {
return std::get<HWND>(*view_ref->view->GetRenderTarget());
}
FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetView(
FlutterDesktopPluginRegistrarRef registrar) {
return registrar->view.get();
}
UINT FlutterDesktopGetDpiForHWND(HWND hwnd) {
return flutter::GetDpiForHWND(hwnd);
}
@@ -122,29 +147,7 @@ void FlutterDesktopResyncOutputStreams() {
std::ios::sync_with_stdio();
}
FlutterDesktopEngineRef FlutterDesktopRunEngine(
const FlutterDesktopEngineProperties& engine_properties) {
flutter::FlutterProjectBundle project(engine_properties);
auto engine = std::make_unique<flutter::FlutterWindowsEngine>(project);
if (!engine->RunWithEntrypoint(engine_properties.entry_point)) {
return nullptr;
}
return HandleForEngine(engine.release());
}
bool FlutterDesktopShutDownEngine(FlutterDesktopEngineRef engine_ref) {
std::cout << "Shutting down flutter engine process." << std::endl;
flutter::FlutterWindowsEngine* engine = EngineFromHandle(engine_ref);
bool result = engine->Stop();
delete engine;
return result;
}
void FlutterDesktopRegistrarEnableInputBlocking(
FlutterDesktopPluginRegistrarRef registrar,
const char* channel) {
registrar->messenger->dispatcher->EnableInputBlockingForChannel(channel);
}
// Implementations of common/cpp/ API methods.
FlutterDesktopMessengerRef FlutterDesktopRegistrarGetMessenger(
FlutterDesktopPluginRegistrarRef registrar) {
@@ -157,11 +160,6 @@ void FlutterDesktopRegistrarSetDestructionHandler(
registrar->destruction_handler = callback;
}
FlutterDesktopViewRef FlutterDesktopRegistrarGetView(
FlutterDesktopPluginRegistrarRef registrar) {
return registrar->view.get();
}
bool FlutterDesktopMessengerSendWithReply(FlutterDesktopMessengerRef messenger,
const char* channel,
const uint8_t* message,

View File

@@ -40,6 +40,9 @@ class FlutterWindowsEngine {
// Returns false if the engine couldn't be started.
bool RunWithEntrypoint(const char* entrypoint);
// Returns true if the engine is currently running.
bool running() { return engine_ != nullptr; }
// Stops the engine. This invalidates the pointer returned by engine().
//
// Returns false if stopping the engine fails, or if it was not running.

View File

@@ -46,9 +46,6 @@ typedef struct {
// it will be ignored in that case.
const wchar_t* aot_library_path;
// The entry point to use in the Dart project. If null, defaults to main().
const char* entry_point;
// The switches to pass to the Flutter engine.
//
// See: https://github.com/flutter/engine/blob/master/shell/common/switches.h
@@ -59,41 +56,69 @@ typedef struct {
size_t switches_count;
} FlutterDesktopEngineProperties;
// Creates a View with the given dimensions running a Flutter Application.
// ========== View Controller ==========
// Creates a view that hosts and displays the given engine instance.
//
// This will set up and run an associated Flutter engine using the settings in
// |engine_properties|.
// This takes ownership of |engine|, so FlutterDesktopEngineDestroy should no
// longer be called on it, as it will be called internally when the view
// controller is destroyed. If creating the view controller fails, the engine
// will be destroyed immediately.
//
// Returns a null pointer in the event of an error.
// If |engine| is not already running, the view controller will start running
// it automatically before displaying the window.
//
// The caller owns the returned reference, and is responsible for calling
// FlutterDesktopViewControllerDestroy. Returns a null pointer in the event of
// an error.
FLUTTER_EXPORT FlutterDesktopViewControllerRef
FlutterDesktopCreateViewController(
int width,
int height,
const FlutterDesktopEngineProperties& engine_properties);
FlutterDesktopViewControllerCreate(int width,
int height,
FlutterDesktopEngineRef engine);
// Shuts down the engine instance associated with |controller|, and cleans up
// associated state.
//
// |controller| is no longer valid after this call.
FLUTTER_EXPORT void FlutterDesktopDestroyViewController(
FLUTTER_EXPORT void FlutterDesktopViewControllerDestroy(
FlutterDesktopViewControllerRef controller);
// Returns the handle for the engine running in FlutterDesktopViewControllerRef.
//
// Its lifetime is the same as the |controller|'s.
FLUTTER_EXPORT FlutterDesktopEngineRef
FlutterDesktopGetEngine(FlutterDesktopViewControllerRef controller);
// Returns the plugin registrar handle for the plugin with the given name.
//
// The name must be unique across the application.
FLUTTER_EXPORT FlutterDesktopPluginRegistrarRef
FlutterDesktopGetPluginRegistrar(FlutterDesktopEngineRef engine,
const char* plugin_name);
FLUTTER_EXPORT FlutterDesktopEngineRef FlutterDesktopViewControllerGetEngine(
FlutterDesktopViewControllerRef controller);
// Returns the view managed by the given controller.
FLUTTER_EXPORT FlutterDesktopViewRef
FlutterDesktopGetView(FlutterDesktopViewControllerRef controller);
FlutterDesktopViewControllerGetView(FlutterDesktopViewControllerRef controller);
// ========== Engine ==========
// Creates a Flutter engine with the given properties.
//
// The caller owns the returned reference, and is responsible for calling
// FlutterDesktopEngineDestroy.
FLUTTER_EXPORT FlutterDesktopEngineRef FlutterDesktopEngineCreate(
const FlutterDesktopEngineProperties& engine_properties);
// Shuts down and destroys the given engine instance. Returns true if the
// shutdown was successful, or if the engine was not running.
//
// |engine| is no longer valid after this call.
FLUTTER_EXPORT bool FlutterDesktopEngineDestroy(FlutterDesktopEngineRef engine);
// Starts running the given engine instance and optional entry point in the Dart
// project. If the entry point is null, defaults to main().
//
// If provided, entry_point must be the name of a top-level function from the
// same Dart library that contains the app's main() function, and must be
// decorated with `@pragma(vm:entry-point)` to ensure the method is not
// tree-shaken by the Dart compiler.
//
// Returns false if running the engine failed.
FLUTTER_EXPORT bool FlutterDesktopEngineRun(FlutterDesktopEngineRef engine,
const char* entry_point);
// Processes any pending events in the Flutter engine, and returns the
// number of nanoseconds until the next scheduled event (or max, if none).
@@ -102,18 +127,43 @@ FlutterDesktopGetView(FlutterDesktopViewControllerRef controller);
// a wait for native events in the runloop should never be longer than the
// last return value from this function.
FLUTTER_EXPORT uint64_t
FlutterDesktopProcessMessages(FlutterDesktopEngineRef engine);
FlutterDesktopEngineProcessMessages(FlutterDesktopEngineRef engine);
// Returns the plugin registrar handle for the plugin with the given name.
//
// The name must be unique across the application.
FLUTTER_EXPORT FlutterDesktopPluginRegistrarRef
FlutterDesktopEngineGetPluginRegistrar(FlutterDesktopEngineRef engine,
const char* plugin_name);
// ========== View ==========
// Return backing HWND for manipulation in host application.
FLUTTER_EXPORT HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef view);
// ========== Plugin Registrar (extensions) ==========
// Returns the view associated with this registrar's engine instance
// This is a Windows-specific extension to flutter_plugin_registrar.h.
FLUTTER_EXPORT FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetView(
FlutterDesktopPluginRegistrarRef registrar);
// ========== Freestanding Utilities ==========
// Gets the DPI for a given |hwnd|, depending on the supported APIs per
// windows version and DPI awareness mode. If nullptr is passed, returns the DPI
// of the primary monitor.
//
// This uses the same logic and fallback for older Windows versions that is used
// internally by Flutter to determine the DPI to use for displaying Flutter
// content, so should be used by any code (e.g., in plugins) that translates
// between Windows and Dart sizes/offsets.
FLUTTER_EXPORT UINT FlutterDesktopGetDpiForHWND(HWND hwnd);
// Gets the DPI for a given |monitor|. If the API is not available, a default
// DPI of 96 is returned.
//
// See FlutterDesktopGetDpiForHWND for more information.
FLUTTER_EXPORT UINT FlutterDesktopGetDpiForMonitor(HMONITOR monitor);
// Reopens stdout and stderr and resysncs the standard library output streams.
@@ -121,22 +171,6 @@ FLUTTER_EXPORT UINT FlutterDesktopGetDpiForMonitor(HMONITOR monitor);
// (e.g., after an AllocConsole call).
FLUTTER_EXPORT void FlutterDesktopResyncOutputStreams();
// Runs an instance of a headless Flutter engine.
//
// Returns a null pointer in the event of an error.
FLUTTER_EXPORT FlutterDesktopEngineRef FlutterDesktopRunEngine(
const FlutterDesktopEngineProperties& engine_properties);
// Shuts down the given engine instance. Returns true if the shutdown was
// successful. |engine| is no longer valid after this call.
FLUTTER_EXPORT bool FlutterDesktopShutDownEngine(
FlutterDesktopEngineRef engine);
// Returns the view associated with this registrar's engine instance
// This is a Windows-specific extension to flutter_plugin_registrar.h.
FLUTTER_EXPORT FlutterDesktopViewRef
FlutterDesktopRegistrarGetView(FlutterDesktopPluginRegistrarRef registrar);
#if defined(__cplusplus)
} // extern "C"
#endif