Skip to content

Commit 32a5fb8

Browse files
iskakaushikgspencergoog
authored andcommitted
[macOS] Allow loading of AOT snapshots and instructions from elf bundle (flutter#21670)
1 parent a7e7964 commit 32a5fb8

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

shell/platform/darwin/macos/framework/Source/FlutterEngine.mm

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h"
88

99
#include <algorithm>
10+
#include <filesystem>
1011
#include <vector>
1112

1213
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject_Internal.h"
@@ -162,6 +163,16 @@ static bool OnAcquireExternalTexture(FlutterEngine* engine,
162163

163164
#pragma mark -
164165

166+
namespace {
167+
168+
struct AotDataDeleter {
169+
void operator()(FlutterEngineAOTData aot_data) { FlutterEngineCollectAOTData(aot_data); }
170+
};
171+
172+
using UniqueAotDataPtr = std::unique_ptr<_FlutterEngineAOTData, AotDataDeleter>;
173+
174+
}
175+
165176
@implementation FlutterEngine {
166177
// The embedding-API-level engine object.
167178
FLUTTER_API_SYMBOL(FlutterEngine) _engine;
@@ -184,6 +195,9 @@ @implementation FlutterEngine {
184195

185196
// A mapping of textureID to internal FlutterExternalTextureGL adapter.
186197
NSMutableDictionary<NSNumber*, FlutterExternalTextureGL*>* _textures;
198+
199+
// Pointer to the Dart AOT snapshot and instruction data.
200+
UniqueAotDataPtr _aotData;
187201
}
188202

189203
- (instancetype)initWithName:(NSString*)labelPrefix project:(FlutterDartProject*)project {
@@ -274,6 +288,11 @@ - (BOOL)runWithEntrypoint:(NSString*)entrypoint {
274288
};
275289
flutterArguments.custom_task_runners = &custom_task_runners;
276290

291+
_aotData = [self loadAotData:flutterArguments.assets_path];
292+
if (_aotData) {
293+
flutterArguments.aot_data = _aotData.get();
294+
}
295+
277296
FlutterEngineResult result = FlutterEngineInitialize(
278297
FLUTTER_ENGINE_VERSION, &rendererConfig, &flutterArguments, (__bridge void*)(self), &_engine);
279298
if (result != kSuccess) {
@@ -293,6 +312,33 @@ - (BOOL)runWithEntrypoint:(NSString*)entrypoint {
293312
return YES;
294313
}
295314

315+
- (UniqueAotDataPtr)loadAotData:(std::string)assetsDir {
316+
if (!FlutterEngineRunsAOTCompiledDartCode()) {
317+
return nullptr;
318+
}
319+
320+
std::filesystem::path assetsFsDir(assetsDir);
321+
std::filesystem::path elfFile("app_elf_snapshot.so");
322+
auto fullElfPath = assetsFsDir / elfFile;
323+
324+
if (!std::filesystem::exists(fullElfPath)) {
325+
return nullptr;
326+
}
327+
328+
FlutterEngineAOTDataSource source = {};
329+
source.type = kFlutterEngineAOTDataSourceTypeElfPath;
330+
source.elf_path = fullElfPath.c_str();
331+
332+
FlutterEngineAOTData data = nullptr;
333+
auto result = FlutterEngineCreateAOTData(&source, &data);
334+
if (result != kSuccess) {
335+
NSLog(@"Failed to load AOT data from: %@", @(fullElfPath.c_str()));
336+
return nullptr;
337+
}
338+
339+
return UniqueAotDataPtr(data);
340+
}
341+
296342
- (void)setViewController:(FlutterViewController*)controller {
297343
_viewController = controller;
298344
_mainOpenGLContext = controller.flutterView.openGLContext;

0 commit comments

Comments
 (0)