Skip to content

Commit 22692a6

Browse files
author
Marcus Tomlinson
authored
Update GLFW embedding to support AOT mode (flutter#18423)
Unblocks: flutter#57135 Part of flutter#38478
1 parent 89cf074 commit 22692a6

File tree

6 files changed

+80
-7
lines changed

6 files changed

+80
-7
lines changed

shell/platform/glfw/client_wrapper/flutter_engine.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ FlutterEngine::~FlutterEngine() {}
1515

1616
bool FlutterEngine::Start(const std::string& icu_data_path,
1717
const std::string& assets_path,
18-
const std::vector<std::string>& arguments) {
18+
const std::vector<std::string>& arguments,
19+
const std::string& aot_library_path) {
1920
if (engine_) {
2021
std::cerr << "Cannot run an already running engine. Create a new instance "
2122
"or call ShutDown first."
@@ -26,6 +27,7 @@ bool FlutterEngine::Start(const std::string& icu_data_path,
2627
FlutterDesktopEngineProperties c_engine_properties = {};
2728
c_engine_properties.assets_path = assets_path.c_str();
2829
c_engine_properties.icu_data_path = icu_data_path.c_str();
30+
c_engine_properties.aot_library_path = aot_library_path.c_str();
2931
std::vector<const char*> engine_switches;
3032
std::transform(
3133
arguments.begin(), arguments.end(), std::back_inserter(engine_switches),

shell/platform/glfw/client_wrapper/flutter_window_controller.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ FlutterWindowController::~FlutterWindowController() {
2727
bool FlutterWindowController::CreateWindow(
2828
const WindowProperties& window_properties,
2929
const std::string& assets_path,
30-
const std::vector<std::string>& arguments) {
30+
const std::vector<std::string>& arguments,
31+
const std::string& aot_library_path) {
3132
if (!init_succeeded_) {
3233
std::cerr << "Could not create window; FlutterDesktopInit failed."
3334
<< std::endl;
@@ -47,6 +48,7 @@ bool FlutterWindowController::CreateWindow(
4748

4849
FlutterDesktopEngineProperties c_engine_properties = {};
4950
c_engine_properties.assets_path = assets_path.c_str();
51+
c_engine_properties.aot_library_path = aot_library_path.c_str();
5052
c_engine_properties.icu_data_path = icu_data_path_.c_str();
5153
std::vector<const char*> engine_switches;
5254
std::transform(

shell/platform/glfw/client_wrapper/include/flutter/flutter_engine.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ class FlutterEngine : public PluginRegistry {
3232
// successful.
3333
bool Start(const std::string& icu_data_path,
3434
const std::string& assets_path,
35-
const std::vector<std::string>& arguments);
35+
const std::vector<std::string>& arguments,
36+
const std::string& aot_library_path = "");
3637

3738
// Terminates the running engine.
3839
void ShutDown();

shell/platform/glfw/client_wrapper/include/flutter/flutter_window_controller.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ class FlutterWindowController : public PluginRegistry {
4646
// There must be only one instance of this class in an application at any
4747
// given time, as Flutter does not support multiple engines in one process,
4848
// or multiple views in one engine.
49+
//
50+
// |icu_data_path| is the path to the icudtl.dat file for the version of
51+
// Flutter you are using.
4952
explicit FlutterWindowController(const std::string& icu_data_path);
5053

5154
virtual ~FlutterWindowController();
@@ -57,17 +60,21 @@ class FlutterWindowController : public PluginRegistry {
5760
// Creates and displays a window for displaying Flutter content.
5861
//
5962
// The |assets_path| is the path to the flutter_assets folder for the Flutter
60-
// application to be run. |icu_data_path| is the path to the icudtl.dat file
61-
// for the version of Flutter you are using.
63+
// application to be run.
6264
//
6365
// The |arguments| are passed to the Flutter engine. See:
6466
// https://github.com/flutter/engine/blob/master/shell/common/switches.h for
6567
// for details. Not all arguments will apply to desktop.
6668
//
69+
// The |aot_library_path| is the path to the libapp.so file for the Flutter
70+
// application to be run. While this parameter is only required in AOT mode,
71+
// it is perfectly safe to provide the path in non-AOT mode too.
72+
//
6773
// Only one Flutter window can exist at a time; see constructor comment.
6874
bool CreateWindow(const WindowProperties& window_properties,
6975
const std::string& assets_path,
70-
const std::vector<std::string>& arguments);
76+
const std::vector<std::string>& arguments,
77+
const std::string& aot_library_path = "");
7178

7279
// Destroys the current window, if any.
7380
//

shell/platform/glfw/flutter_glfw.cc

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ struct FlutterDesktopWindow {
9191
bool skip_next_window_refresh = false;
9292
};
9393

94+
// Custom deleter for FlutterEngineAOTData.
95+
struct AOTDataDeleter {
96+
void operator()(FlutterEngineAOTData aot_data) {
97+
FlutterEngineCollectAOTData(aot_data);
98+
}
99+
};
100+
101+
using UniqueAotDataPtr = std::unique_ptr<_FlutterEngineAOTData, AOTDataDeleter>;
102+
94103
// Struct for storing state of a Flutter engine instance.
95104
struct FlutterDesktopEngineState {
96105
// The handle to the Flutter engine instance.
@@ -117,6 +126,9 @@ struct FlutterDesktopEngineState {
117126
// The controller associated with this engine instance, if any.
118127
// This will always be null for a headless engine.
119128
FlutterDesktopWindowControllerState* window_controller = nullptr;
129+
130+
// AOT data for this engine instance, if applicable.
131+
UniqueAotDataPtr aot_data = nullptr;
120132
};
121133

122134
// State associated with the plugin registrar.
@@ -550,6 +562,33 @@ static void GLFWErrorCallback(int error_code, const char* description) {
550562
std::cerr << "GLFW error " << error_code << ": " << description << std::endl;
551563
}
552564

565+
// Attempts to load AOT data from the given path, which must be absolute and
566+
// non-empty. Logs and returns nullptr on failure.
567+
UniqueAotDataPtr LoadAotData(std::filesystem::path aot_data_path) {
568+
if (aot_data_path.empty()) {
569+
std::cerr
570+
<< "Attempted to load AOT data, but no aot_data_path was provided."
571+
<< std::endl;
572+
return nullptr;
573+
}
574+
if (!std::filesystem::exists(aot_data_path)) {
575+
std::cerr << "Can't load AOT data from " << aot_data_path.u8string()
576+
<< "; no such file." << std::endl;
577+
return nullptr;
578+
}
579+
std::string path_string = aot_data_path.u8string();
580+
FlutterEngineAOTDataSource source = {};
581+
source.type = kFlutterEngineAOTDataSourceTypeElfPath;
582+
source.elf_path = path_string.c_str();
583+
FlutterEngineAOTData data = nullptr;
584+
auto result = FlutterEngineCreateAOTData(&source, &data);
585+
if (result != kSuccess) {
586+
std::cerr << "Failed to load AOT data from: " << path_string << std::endl;
587+
return nullptr;
588+
}
589+
return UniqueAotDataPtr(data);
590+
}
591+
553592
// Starts an instance of the Flutter Engine.
554593
//
555594
// Configures the engine according to |engine_propreties| and using |event_loop|
@@ -574,7 +613,10 @@ static bool RunFlutterEngine(
574613
std::filesystem::u8path(engine_properties.assets_path);
575614
std::filesystem::path icu_path =
576615
std::filesystem::u8path(engine_properties.icu_data_path);
577-
if (assets_path.is_relative() || icu_path.is_relative()) {
616+
std::filesystem::path aot_library_path =
617+
std::filesystem::u8path(engine_properties.aot_library_path);
618+
if (assets_path.is_relative() || icu_path.is_relative() ||
619+
(!aot_library_path.empty() && aot_library_path.is_relative())) {
578620
// Treat relative paths as relative to the directory of this executable.
579621
std::filesystem::path executable_location =
580622
flutter::GetExecutableDirectory();
@@ -585,9 +627,14 @@ static bool RunFlutterEngine(
585627
}
586628
assets_path = std::filesystem::path(executable_location) / assets_path;
587629
icu_path = std::filesystem::path(executable_location) / icu_path;
630+
if (!aot_library_path.empty()) {
631+
aot_library_path =
632+
std::filesystem::path(executable_location) / aot_library_path;
633+
}
588634
}
589635
std::string assets_path_string = assets_path.u8string();
590636
std::string icu_path_string = icu_path.u8string();
637+
std::string lib_path_string = aot_library_path.u8string();
591638

592639
// Configure a task runner using the event loop.
593640
engine_state->event_loop = std::move(event_loop);
@@ -618,6 +665,16 @@ static bool RunFlutterEngine(
618665
args.command_line_argv = &argv[0];
619666
args.platform_message_callback = EngineOnFlutterPlatformMessage;
620667
args.custom_task_runners = &task_runners;
668+
669+
if (FlutterEngineRunsAOTCompiledDartCode()) {
670+
engine_state->aot_data = LoadAotData(lib_path_string);
671+
if (!engine_state->aot_data) {
672+
std::cerr << "Unable to start engine without AOT data." << std::endl;
673+
return false;
674+
}
675+
args.aot_data = engine_state->aot_data.get();
676+
}
677+
621678
FLUTTER_API_SYMBOL(FlutterEngine) engine = nullptr;
622679
auto result = FlutterEngineRun(FLUTTER_ENGINE_VERSION, &config, &args,
623680
engine_state, &engine);

shell/platform/glfw/public/flutter_glfw.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ typedef struct {
4545
// This can either be an absolute path, or on Windows or Linux, a path
4646
// relative to the directory containing the executable.
4747
const char* icu_data_path;
48+
// The path to the libapp.so file for the application to be run.
49+
// This can either be an absolute path or a path relative to the directory
50+
// containing the executable.
51+
const char* aot_library_path;
4852
// The switches to pass to the Flutter engine.
4953
//
5054
// See: https://github.com/flutter/engine/blob/master/shell/common/switches.h

0 commit comments

Comments
 (0)