@@ -91,6 +91,15 @@ struct FlutterDesktopWindow {
91
91
bool skip_next_window_refresh = false ;
92
92
};
93
93
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
+
94
103
// Struct for storing state of a Flutter engine instance.
95
104
struct FlutterDesktopEngineState {
96
105
// The handle to the Flutter engine instance.
@@ -117,6 +126,9 @@ struct FlutterDesktopEngineState {
117
126
// The controller associated with this engine instance, if any.
118
127
// This will always be null for a headless engine.
119
128
FlutterDesktopWindowControllerState* window_controller = nullptr ;
129
+
130
+ // AOT data for this engine instance, if applicable.
131
+ UniqueAotDataPtr aot_data = nullptr ;
120
132
};
121
133
122
134
// State associated with the plugin registrar.
@@ -550,6 +562,33 @@ static void GLFWErrorCallback(int error_code, const char* description) {
550
562
std::cerr << " GLFW error " << error_code << " : " << description << std::endl;
551
563
}
552
564
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
+
553
592
// Starts an instance of the Flutter Engine.
554
593
//
555
594
// Configures the engine according to |engine_propreties| and using |event_loop|
@@ -574,7 +613,10 @@ static bool RunFlutterEngine(
574
613
std::filesystem::u8path (engine_properties.assets_path );
575
614
std::filesystem::path icu_path =
576
615
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 ())) {
578
620
// Treat relative paths as relative to the directory of this executable.
579
621
std::filesystem::path executable_location =
580
622
flutter::GetExecutableDirectory ();
@@ -585,9 +627,14 @@ static bool RunFlutterEngine(
585
627
}
586
628
assets_path = std::filesystem::path (executable_location) / assets_path;
587
629
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
+ }
588
634
}
589
635
std::string assets_path_string = assets_path.u8string ();
590
636
std::string icu_path_string = icu_path.u8string ();
637
+ std::string lib_path_string = aot_library_path.u8string ();
591
638
592
639
// Configure a task runner using the event loop.
593
640
engine_state->event_loop = std::move (event_loop);
@@ -618,6 +665,16 @@ static bool RunFlutterEngine(
618
665
args.command_line_argv = &argv[0 ];
619
666
args.platform_message_callback = EngineOnFlutterPlatformMessage;
620
667
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
+
621
678
FLUTTER_API_SYMBOL (FlutterEngine) engine = nullptr ;
622
679
auto result = FlutterEngineRun (FLUTTER_ENGINE_VERSION, &config, &args,
623
680
engine_state, &engine);
0 commit comments