From c2815422317e1e28227facc9534d6333d2369532 Mon Sep 17 00:00:00 2001 From: Zachary Anderson Date: Fri, 20 Oct 2017 10:17:21 -0700 Subject: [PATCH] Pass option to reuse an existing runtime controller (flutter/engine#4253) --- engine/src/flutter/shell/common/engine.cc | 40 +++++++++++-------- engine/src/flutter/shell/common/engine.h | 12 ++++-- .../android/io/flutter/view/FlutterView.java | 11 +++-- .../platform/android/platform_view_android.cc | 12 +++--- .../platform/android/platform_view_android.h | 3 +- .../android/platform_view_android_jni.cc | 8 ++-- 6 files changed, 54 insertions(+), 32 deletions(-) diff --git a/engine/src/flutter/shell/common/engine.cc b/engine/src/flutter/shell/common/engine.cc index 7439d38dc5..4fd8c62dcf 100644 --- a/engine/src/flutter/shell/common/engine.cc +++ b/engine/src/flutter/shell/common/engine.cc @@ -189,12 +189,14 @@ void Engine::Init() { const std::string Engine::main_entrypoint_ = "main"; void Engine::RunBundle(const std::string& bundle_path, - const std::string& entrypoint) { + const std::string& entrypoint, + bool reuse_runtime_controller) { TRACE_EVENT0("flutter", "Engine::RunBundle"); ConfigureAssetBundle(bundle_path); std::vector platform_kernel; GetAssetAsBuffer(blink::kPlatformKernelAssetKey, &platform_kernel); - ConfigureRuntime(GetScriptUriFromPath(bundle_path), platform_kernel); + ConfigureRuntime(GetScriptUriFromPath(bundle_path), platform_kernel, + reuse_runtime_controller); if (blink::IsRunningPrecompiledCode()) { runtime_->dart_controller()->RunFromPrecompiledSnapshot(entrypoint); @@ -214,14 +216,16 @@ void Engine::RunBundle(const std::string& bundle_path, void Engine::RunBundleAndSnapshot(const std::string& bundle_path, const std::string& snapshot_override, - const std::string& entrypoint) { + const std::string& entrypoint, + bool reuse_runtime_controller) { TRACE_EVENT0("flutter", "Engine::RunBundleAndSnapshot"); if (snapshot_override.empty()) { RunBundle(bundle_path, entrypoint); return; } ConfigureAssetBundle(bundle_path); - ConfigureRuntime(GetScriptUriFromPath(bundle_path)); + ConfigureRuntime(GetScriptUriFromPath(bundle_path), std::vector(), + reuse_runtime_controller); if (blink::IsRunningPrecompiledCode()) { runtime_->dart_controller()->RunFromPrecompiledSnapshot(entrypoint); } else { @@ -235,7 +239,8 @@ void Engine::RunBundleAndSnapshot(const std::string& bundle_path, void Engine::RunBundleAndSource(const std::string& bundle_path, const std::string& main, - const std::string& packages) { + const std::string& packages, + bool reuse_runtime_controller) { TRACE_EVENT0("flutter", "Engine::RunBundleAndSource"); FXL_CHECK(!blink::IsRunningPrecompiledCode()) << "Cannot run from source in a precompiled build."; @@ -248,7 +253,8 @@ void Engine::RunBundleAndSource(const std::string& bundle_path, ConfigureAssetBundle(bundle_path); GetAssetAsBuffer(blink::kPlatformKernelAssetKey, &platform_kernel); } - ConfigureRuntime(GetScriptUriFromPath(bundle_path), platform_kernel); + ConfigureRuntime(GetScriptUriFromPath(bundle_path), platform_kernel, + reuse_runtime_controller); if (!platform_kernel.empty()) { std::vector kernel; @@ -477,17 +483,19 @@ void Engine::ConfigureAssetBundle(const std::string& path) { } void Engine::ConfigureRuntime(const std::string& script_uri, - const std::vector& platform_kernel) { - if (!runtime_) { - runtime_ = blink::RuntimeController::Create(this); - runtime_->CreateDartController( - std::move(script_uri), default_isolate_snapshot_data, - default_isolate_snapshot_instr, platform_kernel); - runtime_->SetViewportMetrics(viewport_metrics_); - runtime_->SetLocale(language_code_, country_code_); - runtime_->SetUserSettingsData(user_settings_data_); - runtime_->SetSemanticsEnabled(semantics_enabled_); + const std::vector& platform_kernel, + bool reuse_runtime_controller) { + if (runtime_ && reuse_runtime_controller) { + return; } + runtime_ = blink::RuntimeController::Create(this); + runtime_->CreateDartController( + std::move(script_uri), default_isolate_snapshot_data, + default_isolate_snapshot_instr, platform_kernel); + runtime_->SetViewportMetrics(viewport_metrics_); + runtime_->SetLocale(language_code_, country_code_); + runtime_->SetUserSettingsData(user_settings_data_); + runtime_->SetSemanticsEnabled(semantics_enabled_); } void Engine::DidCreateMainIsolate(Dart_Isolate isolate) { diff --git a/engine/src/flutter/shell/common/engine.h b/engine/src/flutter/shell/common/engine.h index 4cee55554a..18cc8297c1 100644 --- a/engine/src/flutter/shell/common/engine.h +++ b/engine/src/flutter/shell/common/engine.h @@ -36,20 +36,23 @@ class Engine : public blink::RuntimeDelegate { static void Init(); void RunBundle(const std::string& bundle_path, - const std::string& entrypoint = main_entrypoint_); + const std::string& entrypoint = main_entrypoint_, + bool reuse_runtime_controller = false); // Uses the given snapshot instead of looking inside the bundle for the // snapshot. If |snapshot_override| is empty, this function looks for the // snapshot in the bundle itself. void RunBundleAndSnapshot(const std::string& bundle_path, const std::string& snapshot_override, - const std::string& entrypoint = main_entrypoint_); + const std::string& entrypoint = main_entrypoint_, + bool reuse_runtime_controller = false); // Uses the given source code instead of looking inside the bundle for the // source code. void RunBundleAndSource(const std::string& bundle_path, const std::string& main, - const std::string& packages); + const std::string& packages, + bool reuse_runtime_controller = false); void BeginFrame(fxl::TimePoint frame_time); void NotifyIdle(int64_t deadline); @@ -91,7 +94,8 @@ class Engine : public blink::RuntimeDelegate { void ConfigureAssetBundle(const std::string& path); void ConfigureRuntime( const std::string& script_uri, - const std::vector& platform_kernel = std::vector()); + const std::vector& platform_kernel = std::vector(), + bool reuse_runtime_controller = false); bool HandleLifecyclePlatformMessage(blink::PlatformMessage* message); bool HandleNavigationPlatformMessage( diff --git a/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java b/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java index c742acc190..fa0d579243 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java @@ -565,13 +565,17 @@ public class FlutterView extends SurfaceView } public void runFromBundle(String bundlePath, String snapshotOverride) { - runFromBundle(bundlePath, snapshotOverride, "main"); + runFromBundle(bundlePath, snapshotOverride, "main", false); } public void runFromBundle(String bundlePath, String snapshotOverride, String entrypoint) { + runFromBundle(bundlePath, snapshotOverride, entrypoint, false); + } + + public void runFromBundle(String bundlePath, String snapshotOverride, String entrypoint, boolean reuseRuntimeController) { assertAttached(); preRun(); - nativeRunBundleAndSnapshot(mNativePlatformView, bundlePath, snapshotOverride, entrypoint); + nativeRunBundleAndSnapshot(mNativePlatformView, bundlePath, snapshotOverride, entrypoint, reuseRuntimeController); postRun(); } @@ -631,7 +635,8 @@ public class FlutterView extends SurfaceView private static native void nativeRunBundleAndSnapshot(long nativePlatformViewAndroid, String bundlePath, String snapshotOverride, - String entrypoint); + String entrypoint, + boolean reuseRuntimeController); private static native void nativeRunBundleAndSource(long nativePlatformViewAndroid, String bundlePath, diff --git a/engine/src/flutter/shell/platform/android/platform_view_android.cc b/engine/src/flutter/shell/platform/android/platform_view_android.cc index a0e353b0ea..55a8f0306c 100644 --- a/engine/src/flutter/shell/platform/android/platform_view_android.cc +++ b/engine/src/flutter/shell/platform/android/platform_view_android.cc @@ -210,16 +210,18 @@ void PlatformViewAndroid::SurfaceDestroyed() { void PlatformViewAndroid::RunBundleAndSnapshot(std::string bundle_path, std::string snapshot_override, - std::string entrypoint) { + std::string entrypoint, + bool reuse_runtime_controller) { blink::Threads::UI()->PostTask([ engine = engine_->GetWeakPtr(), bundle_path = std::move(bundle_path), snapshot_override = std::move(snapshot_override), - entrypoint = std::move(entrypoint) + entrypoint = std::move(entrypoint), + reuse_runtime_controller = reuse_runtime_controller ] { if (engine) - engine->RunBundleAndSnapshot(std::move(bundle_path), - std::move(snapshot_override), - std::move(entrypoint)); + engine->RunBundleAndSnapshot( + std::move(bundle_path), std::move(snapshot_override), + std::move(entrypoint), reuse_runtime_controller); }); } diff --git a/engine/src/flutter/shell/platform/android/platform_view_android.h b/engine/src/flutter/shell/platform/android/platform_view_android.h index 91c48a42cb..9ea9ef7990 100644 --- a/engine/src/flutter/shell/platform/android/platform_view_android.h +++ b/engine/src/flutter/shell/platform/android/platform_view_android.h @@ -40,7 +40,8 @@ class PlatformViewAndroid : public PlatformView { void RunBundleAndSnapshot(std::string bundle_path, std::string snapshot_override, - std::string entrypoint); + std::string entrypoint, + bool reuse_isolate); void RunBundleAndSource(std::string bundle_path, std::string main, diff --git a/engine/src/flutter/shell/platform/android/platform_view_android_jni.cc b/engine/src/flutter/shell/platform/android/platform_view_android_jni.cc index f4093ae9e6..f9180d49a2 100644 --- a/engine/src/flutter/shell/platform/android/platform_view_android_jni.cc +++ b/engine/src/flutter/shell/platform/android/platform_view_android_jni.cc @@ -105,11 +105,13 @@ static void RunBundleAndSnapshot(JNIEnv* env, jlong platform_view, jstring bundlePath, jstring snapshotOverride, - jstring entrypoint) { + jstring entrypoint, + jboolean reuse_runtime_controller) { return PLATFORM_VIEW->RunBundleAndSnapshot( fml::jni::JavaStringToString(env, bundlePath), // fml::jni::JavaStringToString(env, snapshotOverride), // - fml::jni::JavaStringToString(env, entrypoint) // + fml::jni::JavaStringToString(env, entrypoint), // + reuse_runtime_controller // ); } @@ -260,7 +262,7 @@ bool PlatformViewAndroid::Register(JNIEnv* env) { { .name = "nativeRunBundleAndSnapshot", .signature = - "(JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", + "(JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)V", .fnPtr = reinterpret_cast(&shell::RunBundleAndSnapshot), }, {