未验证 提交 24cf8ebd 编写于 作者: J Jason Simmons 提交者: GitHub

Support hot and cold reload when using the APK asset provider on Android (#4746)

* deprecate snapshot_override, which is an obsolete predecessor of hot reload
* give the APKAssetProvider to the engine in the initial call to RunBundle
* later calls to Engine::RunBundleAndSource or Engine::SetAssetBundlePath
  will replace the APK asset provider with a DirectoryAssetBundle that uses
  the newly pushed assets
上级 54cda7b2
......@@ -301,7 +301,14 @@ void Engine::RunBundle(const std::string& bundle_path,
bool reuse_runtime_controller) {
TRACE_EVENT0("flutter", "Engine::RunBundle");
ConfigureAssetBundle(bundle_path);
ConfigureRuntime(GetScriptUriFromPath(bundle_path), reuse_runtime_controller);
DoRunBundle(GetScriptUriFromPath(bundle_path), entrypoint,
reuse_runtime_controller);
}
void Engine::DoRunBundle(const std::string& script_uri,
const std::string& entrypoint,
bool reuse_runtime_controller) {
ConfigureRuntime(script_uri, reuse_runtime_controller);
if (blink::IsRunningPrecompiledCode()) {
runtime_->dart_controller()->RunFromPrecompiledSnapshot(entrypoint);
} else {
......@@ -318,26 +325,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,
bool reuse_runtime_controller) {
TRACE_EVENT0("flutter", "Engine::RunBundleAndSnapshot");
if (snapshot_override.empty()) {
RunBundle(bundle_path, entrypoint, reuse_runtime_controller);
return;
}
ConfigureAssetBundle(bundle_path);
ConfigureRuntime(GetScriptUriFromPath(bundle_path), reuse_runtime_controller);
if (blink::IsRunningPrecompiledCode()) {
runtime_->dart_controller()->RunFromPrecompiledSnapshot(entrypoint);
} else {
std::vector<uint8_t> snapshot;
if (!files::ReadFileToVector(snapshot_override, &snapshot))
return;
runtime_->dart_controller()->RunFromScriptSnapshot(
snapshot.data(), snapshot.size(), entrypoint);
}
// TODO(jsimmons): merge this with RunBundle
void Engine::RunBundleWithAssets(
fxl::RefPtr<blink::AssetProvider> asset_provider,
const std::string& bundle_path,
const std::string& entrypoint,
bool reuse_runtime_controller) {
TRACE_EVENT0("flutter", "Engine::RunBundleWithAssets");
asset_provider_ = asset_provider;
DoRunBundle(GetScriptUriFromPath(bundle_path), entrypoint,
reuse_runtime_controller);
}
void Engine::RunBundleAndSource(const std::string& bundle_path,
......@@ -572,11 +569,7 @@ void Engine::SetSemanticsEnabled(bool enabled) {
}
void Engine::ConfigureAssetBundle(const std::string& path) {
auto platform_view = platform_view_.lock();
asset_provider_ = platform_view->GetAssetProvider();
if (!asset_provider_) {
asset_provider_ = fxl::MakeRefCounted<blink::DirectoryAssetBundle>(path);
}
asset_provider_ = fxl::MakeRefCounted<blink::DirectoryAssetBundle>(path);
struct stat stat_result = {};
......
......@@ -40,13 +40,11 @@ class Engine : public blink::RuntimeDelegate {
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_,
bool reuse_runtime_controller = false);
// Uses the given provider to locate assets.
void RunBundleWithAssets(fxl::RefPtr<blink::AssetProvider> asset_provider,
const std::string& bundle_path,
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.
......@@ -95,6 +93,10 @@ class Engine : public blink::RuntimeDelegate {
void StopAnimator();
void StartAnimatorIfPossible();
void DoRunBundle(const std::string& script_uri,
const std::string& entrypoint,
bool reuse_runtime_controller);
void ConfigureAssetBundle(const std::string& path);
void ConfigureRuntime(const std::string& script_uri,
bool reuse_runtime_controller = false);
......
......@@ -126,10 +126,6 @@ VsyncWaiter* PlatformView::GetVsyncWaiter() {
return vsync_waiter_.get();
}
fxl::RefPtr<blink::AssetProvider> PlatformView::GetAssetProvider() {
return asset_provider_;
}
void PlatformView::UpdateSemantics(blink::SemanticsNodeUpdates update) {}
void PlatformView::HandlePlatformMessage(
......
......@@ -7,7 +7,6 @@
#include <memory>
#include "flutter/assets/asset_provider.h"
#include "flutter/flow/texture.h"
#include "flutter/lib/ui/semantics/semantics_node.h"
#include "flutter/shell/common/engine.h"
......@@ -65,8 +64,6 @@ class PlatformView : public std::enable_shared_from_this<PlatformView> {
virtual void HandlePlatformMessage(
fxl::RefPtr<blink::PlatformMessage> message);
virtual fxl::RefPtr<blink::AssetProvider> GetAssetProvider();
// Called once per texture, on the platform thread.
void RegisterTexture(std::shared_ptr<flow::Texture> texture);
......@@ -100,7 +97,6 @@ class PlatformView : public std::enable_shared_from_this<PlatformView> {
flow::TextureRegistry texture_registry_;
std::unique_ptr<Engine> engine_;
std::unique_ptr<VsyncWaiter> vsync_waiter_;
fxl::RefPtr<blink::AssetProvider> asset_provider_;
SkISize size_;
......
......@@ -213,22 +213,28 @@ void PlatformViewAndroid::RunBundleAndSnapshot(JNIEnv* env, std::string bundle_p
std::string entrypoint,
bool reuse_runtime_controller,
jobject assetManager) {
// The flutter assets directory name is the last directory of the bundle_path and the path into the APK
// TODO(jsimmons): remove snapshot_override from the public FlutterView API
FXL_CHECK(snapshot_override.empty()) << "snapshot_override is obsolete";
// The flutter assets directory name is the last directory of the bundle_path
// and the path into the APK
size_t last_slash_idx = bundle_path.rfind("/", bundle_path.size());
std::string flutter_assets_dir = bundle_path.substr(last_slash_idx+1, bundle_path.size()-last_slash_idx);
std::string flutter_assets_dir = bundle_path.substr(
last_slash_idx + 1, bundle_path.size() - last_slash_idx);
asset_provider_ = fxl::MakeRefCounted<blink::APKAssetProvider>(env, assetManager, flutter_assets_dir);
blink::Threads::UI()->PostTask([
engine = engine_->GetWeakPtr(), bundle_path = std::move(bundle_path),
snapshot_override = std::move(snapshot_override),
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), reuse_runtime_controller);
});
fxl::RefPtr<blink::AssetProvider> asset_provider =
fxl::MakeRefCounted<blink::APKAssetProvider>(env, assetManager,
flutter_assets_dir);
blink::Threads::UI()->PostTask(
[engine = engine_->GetWeakPtr(),
asset_provider = std::move(asset_provider),
bundle_path = std::move(bundle_path), entrypoint = std::move(entrypoint),
reuse_runtime_controller = reuse_runtime_controller] {
if (engine)
engine->RunBundleWithAssets(
std::move(asset_provider), std::move(bundle_path),
std::move(entrypoint), reuse_runtime_controller);
});
}
void PlatformViewAndroid::RunBundleAndSource(std::string bundle_path,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册