提交 b478563e 编写于 作者: J Jason Simmons

Support ahead-of-time compilation on Android in the engine (#2614)

This include build system changes for selecting Dart's precompiler mode
plus a way to locate and load the precompiled snapshot library from an
Android application
上级 71a211ba
......@@ -3,6 +3,7 @@
# found in the LICENSE file.
import("//mojo/dart/packages/mojo/sdk_ext_sources.gni")
import("//sky/engine/config.gni")
bindings_output_dir = "$root_gen_dir/sky/bindings"
......@@ -19,6 +20,11 @@ if (target_os == "ios" && !use_ios_simulator) {
assert(false, "Unknown active architecture on iOS")
}
}
if (target_os == "android" && flutter_aot) {
if (host_os == "linux" && target_cpu == "arm") {
dart_host_toolchain = "//build/toolchain/linux:clang_x86"
}
}
template("dart_precompile") {
assert(defined(invoker.dart_package_root),
......
dart:jni,JniObject,JniObject.
dart:jni,JniFloat,JniFloat.
dart:jni,JniFloat,get:value
......@@ -28,6 +28,13 @@ declare_args() {
} else {
sky_dart_strict_mode = false
}
# Enable ahead-of-time compilation on platforms where AOT is optional.
flutter_aot = false
# Set if the Dart runtime is being built in product mode, which excludes
# development features such as the debugger.
flutter_product_mode = false
}
# feature_defines_list ---------------------------------------------------------
......@@ -56,3 +63,11 @@ if (sky_dart_strict_mode) {
if (sky_use_dart) {
feature_defines_list += [ "WTF_USE_DART=1" ]
}
if (flutter_aot) {
feature_defines_list += [ "FLUTTER_AOT=1" ]
}
if (flutter_product_mode) {
feature_defines_list += [ "FLUTTER_PRODUCT_MODE=1" ]
}
......@@ -201,8 +201,6 @@ sky_core_files = [
"rendering/style/StyleVisualData.h",
"script/dart_controller.cc",
"script/dart_controller.h",
"script/dart_debugger.cc",
"script/dart_debugger.h",
"script/dart_init.cc",
"script/dart_init.h",
"script/dart_service_isolate.cc",
......@@ -222,6 +220,13 @@ sky_core_files = [
"window/window.h",
]
if (!flutter_product_mode) {
sky_core_files += [
"script/dart_debugger.cc",
"script/dart_debugger.h",
]
}
core_dart_files = get_path_info([
"dart/compositing.dart",
"dart/hash_codes.dart",
......
......@@ -261,11 +261,32 @@ static void ServiceStreamCancelCallback(const char* stream_id) {
const char* kDartVmIsolateSnapshotBufferName = "kDartVmIsolateSnapshotBuffer";
const char* kDartIsolateSnapshotBufferName = "kDartIsolateSnapshotBuffer";
#if OS(IOS)
const char* kInstructionsSnapshotName = "kInstructionsSnapshot";
const char* kDataSnapshotName = "kDataSnapshot";
const char* kDartApplicationLibraryPath =
"FlutterApplication.framework/FlutterApplication";
const char* GetDartApplicationLibraryPath() {
return "FlutterApplication.framework/FlutterApplication";
}
#elif OS(ANDROID)
const char* kInstructionsSnapshotName = "_kInstructionsSnapshot";
const char* kDataSnapshotName = "_kDataSnapshot";
const char* GetDartApplicationLibraryPath() {
const std::string& aot_snapshot_path = SkySettings::Get().aot_snapshot_path;
CHECK(!aot_snapshot_path.empty());
return aot_snapshot_path.c_str();
}
#else
#error "AOT mode is not supported on this platform"
#endif
static void* DartLookupSymbolInLibrary(const char* symbol_name,
const char* library) {
......@@ -293,7 +314,7 @@ void* _DartSymbolLookup(const char* symbol_name) {
// symbols resolved. Once the application library is loaded, there is
// currently no provision to unload the same.
void* symbol =
DartLookupSymbolInLibrary(symbol_name, kDartApplicationLibraryPath);
DartLookupSymbolInLibrary(symbol_name, GetDartApplicationLibraryPath());
if (symbol != nullptr) {
return symbol;
}
......@@ -429,11 +450,13 @@ void InitDartVM() {
}
CHECK(Dart_SetVMFlags(args.size(), args.data()));
#ifndef FLUTTER_PRODUCT_MODE
{
TRACE_EVENT0("flutter", "DartDebugger::InitDebugger");
// This should be called before calling Dart_Initialize.
DartDebugger::InitDebugger();
}
#endif
DartUI::InitForGlobal();
#ifdef OS_ANDROID
......
......@@ -13,13 +13,11 @@
namespace blink {
#define DART_ALLOW_DYNAMIC_RESOLUTION OS(IOS)
#define DART_ALLOW_DYNAMIC_RESOLUTION (OS(IOS) || FLUTTER_AOT)
#if DART_ALLOW_DYNAMIC_RESOLUTION
extern const char* kDartVmIsolateSnapshotBufferName;
extern const char* kDartIsolateSnapshotBufferName;
extern const char* kInstructionsSnapshotName;
void* _DartSymbolLookup(const char* symbol_name);
......
......@@ -4,20 +4,24 @@
#include "sky/engine/public/platform/sky_settings.h"
#include <memory>
#include "base/lazy_instance.h"
#include "base/logging.h"
namespace blink {
static SkySettings s_settings;
static base::LazyInstance<SkySettings> s_settings = LAZY_INSTANCE_INITIALIZER;
static bool s_have_settings = false;
const SkySettings& SkySettings::Get() {
return s_settings;
return s_settings.Get();
}
void SkySettings::Set(const SkySettings& settings) {
CHECK(!s_have_settings);
s_settings = settings;
s_settings.Get() = settings;
s_have_settings = true;
}
......
......@@ -7,6 +7,8 @@
#include <stdint.h>
#include <string>
namespace blink {
struct SkySettings {
......@@ -17,6 +19,7 @@ struct SkySettings {
bool start_paused = false;
bool enable_dart_checked_mode = false;
bool trace_startup = false;
std::string aot_snapshot_path;
static const SkySettings& Get();
static void Set(const SkySettings& settings);
......
......@@ -9,9 +9,11 @@ import android.util.Log;
import android.content.res.AssetManager;
import android.os.SystemClock;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
......@@ -59,6 +61,7 @@ public class FlutterMain {
private static final String[] SKY_RESOURCES = {"icudtl.dat", APP_BUNDLE, MANIFEST};
private static boolean sInitialized = false;
private static ResourceExtractor sResourceExtractor;
private static String sAotSnapshotPath;
/**
* Starts initialization of the native system.
......@@ -68,6 +71,7 @@ public class FlutterMain {
initJavaUtils(applicationContext);
initResources(applicationContext);
initNative(applicationContext);
initAot(applicationContext);
// We record the initialization time using SystemClock because at the start of the
// initialization we have not yet loaded the native library to call into dart_tools_api.h.
......@@ -89,7 +93,17 @@ public class FlutterMain {
}
try {
sResourceExtractor.waitForCompletion();
nativeInit(applicationContext, args);
String[] shellArgs;
if (sAotSnapshotPath != null) {
shellArgs = (args != null) ? Arrays.copyOf(args, args.length + 1) : new String[1];
shellArgs[shellArgs.length - 1] = "--aot-snapshot-path=" + sAotSnapshotPath;
} else {
shellArgs = args;
}
nativeInit(applicationContext, shellArgs);
// Create the mojo run loop.
CoreImpl.getInstance().createDefaultRunLoop();
sInitialized = true;
......@@ -246,4 +260,16 @@ public class FlutterMain {
throw new RuntimeException(e);
}
}
private static void initAot(Context applicationContext) {
File aotSnapshot = new File(applicationContext.getApplicationInfo().nativeLibraryDir,
System.mapLibraryName("snapshot_aot"));
if (aotSnapshot.exists()) {
sAotSnapshotPath = aotSnapshot.getPath();
}
}
public static boolean isRunningPrecompiledCode() {
return sAotSnapshotPath != null;
}
}
......@@ -385,11 +385,15 @@ public class FlutterView extends SurfaceView
resetAccessibilityTree();
String scriptUri = "file://" + bundlePath;
if (snapshotPath != null) {
mSkyEngine.runFromBundleAndSnapshot(scriptUri, bundlePath, snapshotPath);
if (FlutterMain.isRunningPrecompiledCode()) {
mSkyEngine.runFromPrecompiledSnapshot(bundlePath);
} else {
mSkyEngine.runFromBundle(scriptUri, bundlePath);
String scriptUri = "file://" + bundlePath;
if (snapshotPath != null) {
mSkyEngine.runFromBundleAndSnapshot(scriptUri, bundlePath, snapshotPath);
} else {
mSkyEngine.runFromBundle(scriptUri, bundlePath);
}
}
// Connect to the ApplicationMessages service exported by the Flutter framework
......
......@@ -136,6 +136,8 @@ void Shell::InitStandalone(std::string icu_data_path) {
settings.enable_dart_checked_mode =
command_line.HasSwitch(switches::kEnableCheckedMode);
settings.trace_startup = command_line.HasSwitch(switches::kTraceStartup);
settings.aot_snapshot_path = command_line.GetSwitchValueASCII(
switches::kAotSnapshotPath);
blink::SkySettings::Set(settings);
Init();
......
......@@ -19,6 +19,7 @@ const char kPackages[] = "packages";
const char kStartPaused[] = "start-paused";
const char kTraceStartup[] = "trace-startup";
const char kDeviceObservatoryPort[] = "observatory-port";
const char kAotSnapshotPath[] = "aot-snapshot-path";
void PrintUsage(const std::string& executable_name) {
std::cerr << "Usage: " << executable_name
......
......@@ -20,6 +20,7 @@ extern const char kPackages[];
extern const char kStartPaused[];
extern const char kTraceStartup[];
extern const char kDeviceObservatoryPort[];
extern const char kAotSnapshotPath[];
void PrintUsage(const std::string& executable_name);
......
......@@ -67,7 +67,6 @@ def to_gn_args(args):
aot = False
else:
# The iOS simulator snapshot is host targetted
gn_args['dart_target_arch'] = ios_target_cpu
aot = True
elif args.target_os == 'fnl':
gn_args['target_os'] = 'fnl'
......@@ -91,6 +90,8 @@ def to_gn_args(args):
gn_args['target_cpu'] = 'x64'
if aot:
gn_args['flutter_aot'] = True
gn_args['dart_target_arch'] = gn_args['target_cpu']
if args.debug:
gn_args['dart_runtime_mode'] = 'profile'
else:
......@@ -98,6 +99,8 @@ def to_gn_args(args):
else:
gn_args['dart_runtime_mode'] = 'develop'
gn_args['flutter_product_mode'] = (gn_args['dart_runtime_mode'] == 'release')
if args.target_sysroot:
gn_args['target_sysroot'] = args.target_sysroot
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册