[macOS] Allow loading of AOT snapshots and instructions from elf bundle (flutter/engine#21670)

This commit is contained in:
Kaushik Iska 2020-10-07 16:34:49 -07:00 committed by GitHub
parent 6db42cf2a9
commit 7287246cb2

View File

@ -7,6 +7,7 @@
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h"
#include <algorithm>
#include <filesystem>
#include <vector>
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject_Internal.h"
@ -162,6 +163,16 @@ static bool OnAcquireExternalTexture(FlutterEngine* engine,
#pragma mark -
namespace {
struct AotDataDeleter {
void operator()(FlutterEngineAOTData aot_data) { FlutterEngineCollectAOTData(aot_data); }
};
using UniqueAotDataPtr = std::unique_ptr<_FlutterEngineAOTData, AotDataDeleter>;
}
@implementation FlutterEngine {
// The embedding-API-level engine object.
FLUTTER_API_SYMBOL(FlutterEngine) _engine;
@ -184,6 +195,9 @@ static bool OnAcquireExternalTexture(FlutterEngine* engine,
// A mapping of textureID to internal FlutterExternalTextureGL adapter.
NSMutableDictionary<NSNumber*, FlutterExternalTextureGL*>* _textures;
// Pointer to the Dart AOT snapshot and instruction data.
UniqueAotDataPtr _aotData;
}
- (instancetype)initWithName:(NSString*)labelPrefix project:(FlutterDartProject*)project {
@ -274,6 +288,11 @@ static bool OnAcquireExternalTexture(FlutterEngine* engine,
};
flutterArguments.custom_task_runners = &custom_task_runners;
_aotData = [self loadAotData:flutterArguments.assets_path];
if (_aotData) {
flutterArguments.aot_data = _aotData.get();
}
FlutterEngineResult result = FlutterEngineInitialize(
FLUTTER_ENGINE_VERSION, &rendererConfig, &flutterArguments, (__bridge void*)(self), &_engine);
if (result != kSuccess) {
@ -293,6 +312,33 @@ static bool OnAcquireExternalTexture(FlutterEngine* engine,
return YES;
}
- (UniqueAotDataPtr)loadAotData:(std::string)assetsDir {
if (!FlutterEngineRunsAOTCompiledDartCode()) {
return nullptr;
}
std::filesystem::path assetsFsDir(assetsDir);
std::filesystem::path elfFile("app_elf_snapshot.so");
auto fullElfPath = assetsFsDir / elfFile;
if (!std::filesystem::exists(fullElfPath)) {
return nullptr;
}
FlutterEngineAOTDataSource source = {};
source.type = kFlutterEngineAOTDataSourceTypeElfPath;
source.elf_path = fullElfPath.c_str();
FlutterEngineAOTData data = nullptr;
auto result = FlutterEngineCreateAOTData(&source, &data);
if (result != kSuccess) {
NSLog(@"Failed to load AOT data from: %@", @(fullElfPath.c_str()));
return nullptr;
}
return UniqueAotDataPtr(data);
}
- (void)setViewController:(FlutterViewController*)controller {
_viewController = controller;
_mainOpenGLContext = controller.flutterView.openGLContext;