This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Refactor GLFW embedding to support headless mode #18205
Merged
stuartmorgan-g
merged 9 commits into
flutter:master
from
stuartmorgan-g:glfw-engine-refactor
May 8, 2020
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9218627
Give the window controller an engine state
stuartmorgan-g bd1fd90
Add an event loop that doesn't depend on GLFW
stuartmorgan-g 458f45f
Add a way to run the engine runloop
stuartmorgan-g ebc7009
Make engine state the userdata for the engine
stuartmorgan-g 2af9fa0
Refactor some common engine code
stuartmorgan-g a1397a6
Restructure the state objects so the engine owns most things
stuartmorgan-g a1d98d2
Move the registrar to the engine in the public API
stuartmorgan-g 6b9d9e0
Add a preliminary Engine object to the wrapper
stuartmorgan-g bd89f5d
Use a smart pointer for the engine reference in the wrapper
stuartmorgan-g File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #include "include/flutter/flutter_engine.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <iostream> | ||
|
|
||
| namespace flutter { | ||
|
|
||
| FlutterEngine::FlutterEngine() {} | ||
|
|
||
| FlutterEngine::~FlutterEngine() {} | ||
|
|
||
| bool FlutterEngine::Start(const std::string& icu_data_path, | ||
| const std::string& assets_path, | ||
| const std::vector<std::string>& arguments) { | ||
| if (engine_) { | ||
| std::cerr << "Cannot run an already running engine. Create a new instance " | ||
| "or call ShutDown first." | ||
| << std::endl; | ||
| return false; | ||
| } | ||
|
|
||
| FlutterDesktopEngineProperties c_engine_properties = {}; | ||
| c_engine_properties.assets_path = assets_path.c_str(); | ||
| c_engine_properties.icu_data_path = icu_data_path.c_str(); | ||
| std::vector<const char*> engine_switches; | ||
| std::transform( | ||
| arguments.begin(), arguments.end(), std::back_inserter(engine_switches), | ||
| [](const std::string& arg) -> const char* { return arg.c_str(); }); | ||
| if (engine_switches.size() > 0) { | ||
| c_engine_properties.switches = &engine_switches[0]; | ||
| c_engine_properties.switches_count = engine_switches.size(); | ||
| } | ||
|
|
||
| engine_ = UniqueEnginePtr(FlutterDesktopRunEngine(c_engine_properties), | ||
| FlutterDesktopShutDownEngine); | ||
| if (!engine_) { | ||
| std::cerr << "Failed to start engine." << std::endl; | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| void FlutterEngine::ShutDown() { | ||
| engine_ = nullptr; | ||
| } | ||
|
|
||
| FlutterDesktopPluginRegistrarRef FlutterEngine::GetRegistrarForPlugin( | ||
| const std::string& plugin_name) { | ||
| if (!engine_) { | ||
| std::cerr << "Cannot get plugin registrar on an engine that isn't running; " | ||
| "call Run first." | ||
| << std::endl; | ||
| return nullptr; | ||
| } | ||
| return FlutterDesktopGetPluginRegistrar(engine_.get(), plugin_name.c_str()); | ||
| } | ||
|
|
||
| void FlutterEngine::RunEventLoopWithTimeout(std::chrono::milliseconds timeout) { | ||
| if (!engine_) { | ||
| std::cerr << "Cannot run event loop without a running engine; call " | ||
| "Run first." | ||
| << std::endl; | ||
| return; | ||
| } | ||
| uint32_t timeout_milliseconds; | ||
| if (timeout == std::chrono::milliseconds::max()) { | ||
| // The C API uses 0 to represent no timeout, so convert |max| to 0. | ||
| timeout_milliseconds = 0; | ||
| } else if (timeout.count() > UINT32_MAX) { | ||
| timeout_milliseconds = UINT32_MAX; | ||
| } else { | ||
| timeout_milliseconds = static_cast<uint32_t>(timeout.count()); | ||
| } | ||
| FlutterDesktopRunEngineEventLoopWithTimeout(engine_.get(), | ||
| timeout_milliseconds); | ||
| } | ||
|
|
||
| } // namespace flutter | ||
103 changes: 103 additions & 0 deletions
103
shell/platform/glfw/client_wrapper/flutter_engine_unittests.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #include <memory> | ||
| #include <string> | ||
|
|
||
| #include "flutter/shell/platform/glfw/client_wrapper/include/flutter/flutter_engine.h" | ||
| #include "flutter/shell/platform/glfw/client_wrapper/testing/stub_flutter_glfw_api.h" | ||
| #include "gtest/gtest.h" | ||
|
|
||
| namespace flutter { | ||
|
|
||
| namespace { | ||
|
|
||
| // Stub implementation to validate calls to the API. | ||
| class TestGlfwApi : public testing::StubFlutterGlfwApi { | ||
| public: | ||
| // |flutter::testing::StubFlutterGlfwApi| | ||
| FlutterDesktopEngineRef RunEngine( | ||
| const FlutterDesktopEngineProperties& properties) override { | ||
| run_called_ = true; | ||
| return reinterpret_cast<FlutterDesktopEngineRef>(1); | ||
| } | ||
|
|
||
| // |flutter::testing::StubFlutterGlfwApi| | ||
| void RunEngineEventLoopWithTimeout(uint32_t millisecond_timeout) override { | ||
| last_run_loop_timeout_ = millisecond_timeout; | ||
| } | ||
|
|
||
| // |flutter::testing::StubFlutterGlfwApi| | ||
| bool ShutDownEngine() override { | ||
| shut_down_called_ = true; | ||
| return true; | ||
| } | ||
|
|
||
| bool run_called() { return run_called_; } | ||
|
|
||
| bool shut_down_called() { return shut_down_called_; } | ||
|
|
||
| uint32_t last_run_loop_timeout() { return last_run_loop_timeout_; } | ||
|
|
||
| private: | ||
| bool run_called_ = false; | ||
| bool shut_down_called_ = false; | ||
| uint32_t last_run_loop_timeout_ = 0; | ||
| }; | ||
|
|
||
| } // namespace | ||
|
|
||
| TEST(FlutterEngineTest, CreateDestroy) { | ||
| const std::string icu_data_path = "fake/path/to/icu"; | ||
| const std::string assets_path = "fake/path/to/assets"; | ||
| testing::ScopedStubFlutterGlfwApi scoped_api_stub( | ||
| std::make_unique<TestGlfwApi>()); | ||
| auto test_api = static_cast<TestGlfwApi*>(scoped_api_stub.stub()); | ||
| { | ||
| FlutterEngine engine; | ||
| engine.Start(icu_data_path, assets_path, {}); | ||
| EXPECT_EQ(test_api->run_called(), true); | ||
| EXPECT_EQ(test_api->shut_down_called(), false); | ||
| } | ||
| // Destroying should implicitly shut down if it hasn't been done manually. | ||
| EXPECT_EQ(test_api->shut_down_called(), true); | ||
| } | ||
|
|
||
| TEST(FlutterEngineTest, ExplicitShutDown) { | ||
| const std::string icu_data_path = "fake/path/to/icu"; | ||
| const std::string assets_path = "fake/path/to/assets"; | ||
| testing::ScopedStubFlutterGlfwApi scoped_api_stub( | ||
| std::make_unique<TestGlfwApi>()); | ||
| auto test_api = static_cast<TestGlfwApi*>(scoped_api_stub.stub()); | ||
|
|
||
| FlutterEngine engine; | ||
| engine.Start(icu_data_path, assets_path, {}); | ||
| EXPECT_EQ(test_api->run_called(), true); | ||
| EXPECT_EQ(test_api->shut_down_called(), false); | ||
| engine.ShutDown(); | ||
| EXPECT_EQ(test_api->shut_down_called(), true); | ||
| } | ||
|
|
||
| TEST(FlutterEngineTest, RunloopTimeoutTranslation) { | ||
| const std::string icu_data_path = "fake/path/to/icu"; | ||
| const std::string assets_path = "fake/path/to/assets"; | ||
| testing::ScopedStubFlutterGlfwApi scoped_api_stub( | ||
| std::make_unique<TestGlfwApi>()); | ||
| auto test_api = static_cast<TestGlfwApi*>(scoped_api_stub.stub()); | ||
|
|
||
| FlutterEngine engine; | ||
| engine.Start(icu_data_path, assets_path, {}); | ||
|
|
||
| engine.RunEventLoopWithTimeout(std::chrono::milliseconds(100)); | ||
| EXPECT_EQ(test_api->last_run_loop_timeout(), 100U); | ||
|
|
||
| engine.RunEventLoopWithTimeout(std::chrono::milliseconds::max() - | ||
| std::chrono::milliseconds(1)); | ||
| EXPECT_EQ(test_api->last_run_loop_timeout(), UINT32_MAX); | ||
|
|
||
| engine.RunEventLoopWithTimeout(std::chrono::milliseconds::max()); | ||
| EXPECT_EQ(test_api->last_run_loop_timeout(), 0U); | ||
| } | ||
|
|
||
| } // namespace flutter |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
shell/platform/glfw/client_wrapper/include/flutter/flutter_engine.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #ifndef FLUTTER_SHELL_PLATFORM_GLFW_CLIENT_WRAPPER_INCLUDE_FLUTTER_FLUTTER_ENGINE_H_ | ||
| #define FLUTTER_SHELL_PLATFORM_GLFW_CLIENT_WRAPPER_INCLUDE_FLUTTER_FLUTTER_ENGINE_H_ | ||
|
|
||
| #include <flutter_glfw.h> | ||
|
|
||
| #include <chrono> | ||
| #include <memory> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include "plugin_registrar.h" | ||
| #include "plugin_registry.h" | ||
|
|
||
| namespace flutter { | ||
|
|
||
| // An engine for running a headless Flutter application. | ||
| class FlutterEngine : public PluginRegistry { | ||
| public: | ||
| explicit FlutterEngine(); | ||
|
|
||
| virtual ~FlutterEngine(); | ||
|
|
||
| // Prevent copying. | ||
| FlutterEngine(FlutterEngine const&) = delete; | ||
| FlutterEngine& operator=(FlutterEngine const&) = delete; | ||
|
|
||
| // Starts running the engine with the given parameters, returning true if | ||
| // successful. | ||
| bool Start(const std::string& icu_data_path, | ||
| const std::string& assets_path, | ||
| const std::vector<std::string>& arguments); | ||
|
|
||
| // Terminates the running engine. | ||
| void ShutDown(); | ||
|
|
||
| // Processes the next event for the engine, or returns early if |timeout| is | ||
| // reached before the next event. | ||
| void RunEventLoopWithTimeout( | ||
| std::chrono::milliseconds timeout = std::chrono::milliseconds::max()); | ||
|
|
||
| // flutter::PluginRegistry: | ||
| FlutterDesktopPluginRegistrarRef GetRegistrarForPlugin( | ||
| const std::string& plugin_name) override; | ||
|
|
||
| private: | ||
| using UniqueEnginePtr = std::unique_ptr<FlutterDesktopEngineState, | ||
| bool (*)(FlutterDesktopEngineState*)>; | ||
|
|
||
| // Handle for interacting with the C API's engine reference. | ||
| UniqueEnginePtr engine_ = | ||
| UniqueEnginePtr(nullptr, FlutterDesktopShutDownEngine); | ||
| }; | ||
|
|
||
| } // namespace flutter | ||
|
|
||
| #endif // FLUTTER_SHELL_PLATFORM_GLFW_CLIENT_WRAPPER_INCLUDE_FLUTTER_FLUTTER_ENGINE_H_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.