Flush all embedded Android views on hot restart. (flutter/engine#5929)
* Flush all embedded Android view on hot restart. Adds an OnEngineRestarted method to PlatformView, this is currently only implemented for Android where we need to use it for embedded views. * review comments followup * rename to OnPreEngineRestart, call before Clone
This commit is contained in:
@@ -101,6 +101,7 @@ bool Engine::Restart(RunConfiguration configuration) {
|
||||
FML_LOG(ERROR) << "Engine run configuration was invalid.";
|
||||
return false;
|
||||
}
|
||||
delegate_.OnPreEngineRestart();
|
||||
runtime_controller_ = runtime_controller_->Clone();
|
||||
UpdateAssetManager(nullptr);
|
||||
return Run(std::move(configuration));
|
||||
|
||||
@@ -39,6 +39,8 @@ class Engine final : public blink::RuntimeDelegate {
|
||||
virtual void OnEngineHandlePlatformMessage(
|
||||
const Engine& engine,
|
||||
fml::RefPtr<blink::PlatformMessage> message) = 0;
|
||||
|
||||
virtual void OnPreEngineRestart() = 0;
|
||||
};
|
||||
|
||||
Engine(Delegate& delegate,
|
||||
|
||||
@@ -88,6 +88,8 @@ void PlatformView::HandlePlatformMessage(
|
||||
response->CompleteEmpty();
|
||||
}
|
||||
|
||||
void PlatformView::OnPreEngineRestart() const {}
|
||||
|
||||
void PlatformView::RegisterTexture(std::shared_ptr<flow::Texture> texture) {
|
||||
delegate_.OnPlatformViewRegisterTexture(*this, std::move(texture));
|
||||
}
|
||||
|
||||
@@ -108,6 +108,8 @@ class PlatformView {
|
||||
virtual void HandlePlatformMessage(
|
||||
fml::RefPtr<blink::PlatformMessage> message);
|
||||
|
||||
virtual void OnPreEngineRestart() const;
|
||||
|
||||
void SetNextFrameCallback(fml::closure closure);
|
||||
|
||||
void DispatchPointerDataPacket(
|
||||
|
||||
@@ -740,6 +740,25 @@ void Shell::OnEngineHandlePlatformMessage(
|
||||
});
|
||||
}
|
||||
|
||||
// |shell::Engine::Delegate|
|
||||
void Shell::OnPreEngineRestart() {
|
||||
FML_DCHECK(is_setup_);
|
||||
FML_DCHECK(task_runners_.GetUITaskRunner()->RunsTasksOnCurrentThread());
|
||||
|
||||
fml::AutoResetWaitableEvent latch;
|
||||
fml::TaskRunner::RunNowOrPostTask(
|
||||
task_runners_.GetPlatformTaskRunner(),
|
||||
[view = platform_view_->GetWeakPtr(), &latch]() {
|
||||
if (view) {
|
||||
view->OnPreEngineRestart();
|
||||
}
|
||||
latch.Signal();
|
||||
});
|
||||
// This is blocking as any embedded platform views has to be flushed before
|
||||
// we re-run the Dart code.
|
||||
latch.Wait();
|
||||
}
|
||||
|
||||
// |blink::ServiceProtocol::Handler|
|
||||
fml::RefPtr<fml::TaskRunner> Shell::GetServiceProtocolHandlerTaskRunner(
|
||||
fml::StringView method) const {
|
||||
|
||||
@@ -195,6 +195,9 @@ class Shell final : public PlatformView::Delegate,
|
||||
const Engine& engine,
|
||||
fml::RefPtr<blink::PlatformMessage> message) override;
|
||||
|
||||
// |shell::Engine::Delegate|
|
||||
void OnPreEngineRestart() override;
|
||||
|
||||
// |blink::ServiceProtocol::Handler|
|
||||
fml::RefPtr<fml::TaskRunner> GetServiceProtocolHandlerTaskRunner(
|
||||
fml::StringView method) const override;
|
||||
|
||||
@@ -82,6 +82,10 @@ public class FlutterPluginRegistry
|
||||
mActivity = null;
|
||||
}
|
||||
|
||||
public void onPreEngineRestart() {
|
||||
mPlatformViewsController.onPreEngineRestart();
|
||||
}
|
||||
|
||||
private class FlutterRegistrar implements Registrar {
|
||||
private final String pluginKey;
|
||||
|
||||
|
||||
@@ -69,10 +69,11 @@ public class PlatformViewsController implements MethodChannel.MethodCallHandler
|
||||
}
|
||||
|
||||
public void onFlutterViewDestroyed() {
|
||||
for (VirtualDisplayController controller : vdControllers.values()) {
|
||||
controller.dispose();
|
||||
}
|
||||
vdControllers.clear();
|
||||
flushAllViews();
|
||||
}
|
||||
|
||||
public void onPreEngineRestart() {
|
||||
flushAllViews();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -297,4 +298,10 @@ public class PlatformViewsController implements MethodChannel.MethodCallHandler
|
||||
return (int) Math.round(logicalPixels * density);
|
||||
}
|
||||
|
||||
private void flushAllViews() {
|
||||
for (VirtualDisplayController controller : vdControllers.values()) {
|
||||
controller.dispose();
|
||||
}
|
||||
vdControllers.clear();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -198,6 +198,14 @@ public class FlutterNativeView implements BinaryMessenger {
|
||||
mFlutterView.onFirstFrame();
|
||||
}
|
||||
|
||||
// Called by native to notify when the engine is restarted (cold reload).
|
||||
@SuppressWarnings("unused")
|
||||
private void onPreEngineRestart() {
|
||||
if (mPluginRegistry == null)
|
||||
return;
|
||||
mPluginRegistry.onPreEngineRestart();
|
||||
}
|
||||
|
||||
private static native long nativeAttach(FlutterNativeView view);
|
||||
private static native void nativeDestroy(long nativePlatformViewAndroid);
|
||||
private static native void nativeDetach(long nativePlatformViewAndroid);
|
||||
|
||||
@@ -157,6 +157,17 @@ void PlatformViewAndroid::HandlePlatformMessage(
|
||||
}
|
||||
}
|
||||
|
||||
// |shell::PlatformView|
|
||||
void PlatformViewAndroid::OnPreEngineRestart() const {
|
||||
JNIEnv* env = fml::jni::AttachCurrentThread();
|
||||
fml::jni::ScopedJavaLocalRef<jobject> view = java_object_.get(env);
|
||||
if (view.is_null()) {
|
||||
// The Java object died.
|
||||
return;
|
||||
}
|
||||
FlutterViewOnPreEngineRestart(fml::jni::AttachCurrentThread(), view.obj());
|
||||
}
|
||||
|
||||
void PlatformViewAndroid::DispatchSemanticsAction(JNIEnv* env,
|
||||
jint id,
|
||||
jint action,
|
||||
|
||||
@@ -83,6 +83,9 @@ class PlatformViewAndroid final : public PlatformView {
|
||||
void HandlePlatformMessage(
|
||||
fml::RefPtr<blink::PlatformMessage> message) override;
|
||||
|
||||
// |shell::PlatformView|
|
||||
void OnPreEngineRestart() const override;
|
||||
|
||||
// |shell::PlatformView|
|
||||
std::unique_ptr<VsyncWaiter> CreateVSyncWaiter() override;
|
||||
|
||||
|
||||
@@ -96,6 +96,12 @@ void FlutterViewOnFirstFrame(JNIEnv* env, jobject obj) {
|
||||
FML_CHECK(CheckException(env));
|
||||
}
|
||||
|
||||
static jmethodID g_on_engine_restart_method = nullptr;
|
||||
void FlutterViewOnPreEngineRestart(JNIEnv* env, jobject obj) {
|
||||
env->CallVoidMethod(obj, g_on_engine_restart_method);
|
||||
FML_CHECK(CheckException(env));
|
||||
}
|
||||
|
||||
static jmethodID g_attach_to_gl_context_method = nullptr;
|
||||
void SurfaceTextureAttachToGLContext(JNIEnv* env, jobject obj, jint textureId) {
|
||||
env->CallVoidMethod(obj, g_attach_to_gl_context_method, textureId);
|
||||
@@ -698,6 +704,13 @@ bool PlatformViewAndroid::Register(JNIEnv* env) {
|
||||
return false;
|
||||
}
|
||||
|
||||
g_on_engine_restart_method = env->GetMethodID(
|
||||
g_flutter_native_view_class->obj(), "onPreEngineRestart", "()V");
|
||||
|
||||
if (g_on_engine_restart_method == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
g_attach_to_gl_context_method = env->GetMethodID(
|
||||
g_surface_texture_class->obj(), "attachToGLContext", "(I)V");
|
||||
|
||||
|
||||
@@ -34,6 +34,8 @@ void FlutterViewUpdateCustomAccessibilityActions(JNIEnv* env,
|
||||
|
||||
void FlutterViewOnFirstFrame(JNIEnv* env, jobject obj);
|
||||
|
||||
void FlutterViewOnPreEngineRestart(JNIEnv* env, jobject obj);
|
||||
|
||||
void SurfaceTextureAttachToGLContext(JNIEnv* env, jobject obj, jint textureId);
|
||||
|
||||
void SurfaceTextureUpdateTexImage(JNIEnv* env, jobject obj);
|
||||
|
||||
Reference in New Issue
Block a user