From 2354f99f89d4522b27c8e97d213faa70880c0b48 Mon Sep 17 00:00:00 2001 From: ColdPaleLight Date: Sat, 11 Sep 2021 22:51:22 +0800 Subject: [PATCH 1/2] Delete is_background_view from FlutterJNI --- .../platform/android/android_shell_holder.cc | 31 ++++++------------- shell/platform/android/android_shell_holder.h | 3 +- .../embedding/engine/FlutterEngine.java | 3 +- .../flutter/embedding/engine/FlutterJNI.java | 21 ++++++------- .../io/flutter/view/FlutterNativeView.java | 6 ++-- .../platform/android/platform_view_android.cc | 12 ++----- .../platform/android/platform_view_android.h | 3 +- .../android/platform_view_android_jni_impl.cc | 9 ++---- .../FlutterEngineGroupComponentTest.java | 2 +- .../embedding/engine/FlutterEngineTest.java | 4 +-- .../embedding/engine/PluginComponentTest.java | 2 +- .../platform/PlatformViewsControllerTest.java | 18 +++++------ 12 files changed, 44 insertions(+), 70 deletions(-) diff --git a/shell/platform/android/android_shell_holder.cc b/shell/platform/android/android_shell_holder.cc index dd0160fff7c86..a933c2a416e79 100644 --- a/shell/platform/android/android_shell_holder.cc +++ b/shell/platform/android/android_shell_holder.cc @@ -39,32 +39,26 @@ static PlatformData GetDefaultPlatformData() { AndroidShellHolder::AndroidShellHolder( flutter::Settings settings, - std::shared_ptr jni_facade, - bool is_background_view) + std::shared_ptr jni_facade) : settings_(std::move(settings)), jni_facade_(jni_facade) { static size_t thread_host_count = 1; auto thread_label = std::to_string(thread_host_count++); thread_host_ = std::make_shared(); - if (is_background_view) { - *thread_host_ = {thread_label, ThreadHost::Type::UI}; - } else { - *thread_host_ = {thread_label, ThreadHost::Type::UI | - ThreadHost::Type::RASTER | - ThreadHost::Type::IO}; - } + *thread_host_ = {thread_label, ThreadHost::Type::UI | + ThreadHost::Type::RASTER | + ThreadHost::Type::IO}; fml::WeakPtr weak_platform_view; Shell::CreateCallback on_create_platform_view = - [is_background_view, &jni_facade, &weak_platform_view](Shell& shell) { + [&jni_facade, &weak_platform_view](Shell& shell) { std::unique_ptr platform_view_android; platform_view_android = std::make_unique( shell, // delegate shell.GetTaskRunners(), // task runners jni_facade, // JNI interop shell.GetSettings() - .enable_software_rendering, // use software rendering - !is_background_view // create onscreen surface + .enable_software_rendering // use software rendering ); weak_platform_view = platform_view_android->GetWeakPtr(); auto display = Display(jni_facade->GetDisplayRefreshRate()); @@ -84,16 +78,9 @@ AndroidShellHolder::AndroidShellHolder( fml::RefPtr io_runner; fml::RefPtr platform_runner = fml::MessageLoop::GetCurrent().GetTaskRunner(); - if (is_background_view) { - auto single_task_runner = thread_host_->ui_thread->GetTaskRunner(); - raster_runner = single_task_runner; - ui_runner = single_task_runner; - io_runner = single_task_runner; - } else { - raster_runner = thread_host_->raster_thread->GetTaskRunner(); - ui_runner = thread_host_->ui_thread->GetTaskRunner(); - io_runner = thread_host_->io_thread->GetTaskRunner(); - } + raster_runner = thread_host_->raster_thread->GetTaskRunner(); + ui_runner = thread_host_->ui_thread->GetTaskRunner(); + io_runner = thread_host_->io_thread->GetTaskRunner(); flutter::TaskRunners task_runners(thread_label, // label platform_runner, // platform diff --git a/shell/platform/android/android_shell_holder.h b/shell/platform/android/android_shell_holder.h index 613f47da09881..d6ac5272667cf 100644 --- a/shell/platform/android/android_shell_holder.h +++ b/shell/platform/android/android_shell_holder.h @@ -40,8 +40,7 @@ namespace flutter { class AndroidShellHolder { public: AndroidShellHolder(flutter::Settings settings, - std::shared_ptr jni_facade, - bool is_background_view); + std::shared_ptr jni_facade); ~AndroidShellHolder(); diff --git a/shell/platform/android/io/flutter/embedding/engine/FlutterEngine.java b/shell/platform/android/io/flutter/embedding/engine/FlutterEngine.java index d52cce3f4f3db..b6d7814253769 100644 --- a/shell/platform/android/io/flutter/embedding/engine/FlutterEngine.java +++ b/shell/platform/android/io/flutter/embedding/engine/FlutterEngine.java @@ -357,8 +357,7 @@ public FlutterEngine( private void attachToJni() { Log.v(TAG, "Attaching to JNI."); - // TODO(mattcarroll): update native call to not take in "isBackgroundView" - flutterJNI.attachToNative(false); + flutterJNI.attachToNative(); if (!isAttachedToJni()) { throw new RuntimeException("FlutterEngine failed to attach to its native Object reference."); diff --git a/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java b/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java index 3a2d4fcd48f57..2c4baa80d7bb2 100644 --- a/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java +++ b/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java @@ -62,10 +62,9 @@ *

Despite the fact that individual JNI calls are inherently static, there is state that exists * within {@code FlutterJNI}. Most calls within {@code FlutterJNI} correspond to a specific * "platform view", of which there may be many. Therefore, each {@code FlutterJNI} instance holds - * onto a "native platform view ID" after {@link #attachToNative(boolean)}, which is shared with the - * native C/C++ engine code. That ID is passed to every platform-view-specific native method. ID - * management is handled within {@code FlutterJNI} so that developers don't have to hold onto that - * ID. + * onto a "native platform view ID" after {@link #attachToNative()}, which is shared with the native + * C/C++ engine code. That ID is passed to every platform-view-specific native method. ID management + * is handled within {@code FlutterJNI} so that developers don't have to hold onto that ID. * *

To connect part of an Android app to Flutter's C/C++ engine, instantiate a {@code FlutterJNI} * and then attach it to the native side: @@ -306,18 +305,18 @@ public boolean isAttached() { *

This method must not be invoked if {@code FlutterJNI} is already attached to native. */ @UiThread - public void attachToNative(boolean isBackgroundView) { + public void attachToNative() { ensureRunningOnMainThread(); ensureNotAttachedToNative(); - nativeShellHolderId = performNativeAttach(this, isBackgroundView); + nativeShellHolderId = performNativeAttach(this); } @VisibleForTesting - public long performNativeAttach(@NonNull FlutterJNI flutterJNI, boolean isBackgroundView) { - return nativeAttach(flutterJNI, isBackgroundView); + public long performNativeAttach(@NonNull FlutterJNI flutterJNI) { + return nativeAttach(flutterJNI); } - private native long nativeAttach(@NonNull FlutterJNI flutterJNI, boolean isBackgroundView); + private native long nativeAttach(@NonNull FlutterJNI flutterJNI); /** * Spawns a new FlutterJNI instance from the current instance. @@ -327,7 +326,7 @@ public long performNativeAttach(@NonNull FlutterJNI flutterJNI, boolean isBackgr * FlutterJNI by calling its standard constructor. * *

This can only be called once the current FlutterJNI instance is attached by calling {@link - * #attachToNative(boolean)}. + * #attachToNative()}. * *

Static methods that should be only called once such as {@link #init(Context, String[], * String, String, String, long)} or {@link #setRefreshRateFPS(float)} shouldn't be called again @@ -360,7 +359,7 @@ private native FlutterJNI nativeSpawn( *

This method must not be invoked if {@code FlutterJNI} is not already attached to native. * *

Invoking this method will result in the release of all native-side resources that were set - * up during {@link #attachToNative(boolean)} or {@link #spawn(String, String)}, or accumulated + * up during {@link #attachToNative()} or {@link #spawn(String, String)}, or accumulated * thereafter. * *

It is permissible to re-attach this instance to native after detaching it from native. diff --git a/shell/platform/android/io/flutter/view/FlutterNativeView.java b/shell/platform/android/io/flutter/view/FlutterNativeView.java index 2b7afda518c1f..fd6ac03f4a429 100644 --- a/shell/platform/android/io/flutter/view/FlutterNativeView.java +++ b/shell/platform/android/io/flutter/view/FlutterNativeView.java @@ -59,7 +59,7 @@ public FlutterNativeView(@NonNull Context context, boolean isBackgroundView) { mFlutterJNI.addIsDisplayingFlutterUiListener(flutterUiDisplayListener); this.dartExecutor = new DartExecutor(mFlutterJNI, context.getAssets()); mFlutterJNI.addEngineLifecycleListener(new EngineLifecycleListenerImpl()); - attach(this, isBackgroundView); + attach(this); assertAttached(); } @@ -148,8 +148,8 @@ public void setMessageHandler(String channel, BinaryMessageHandler handler) { return mFlutterJNI; } - private void attach(FlutterNativeView view, boolean isBackgroundView) { - mFlutterJNI.attachToNative(isBackgroundView); + private void attach(FlutterNativeView view) { + mFlutterJNI.attachToNative(); dartExecutor.onAttachedToJNI(); } diff --git a/shell/platform/android/platform_view_android.cc b/shell/platform/android/platform_view_android.cc index 899870961c639..576438c8f6ddf 100644 --- a/shell/platform/android/platform_view_android.cc +++ b/shell/platform/android/platform_view_android.cc @@ -46,11 +46,7 @@ std::unique_ptr AndroidSurfaceFactoryImpl::CreateSurface() { } static std::shared_ptr CreateAndroidContext( - bool use_software_rendering, - bool create_onscreen_surface) { - if (!create_onscreen_surface) { - return nullptr; - } + bool use_software_rendering) { if (use_software_rendering) { return std::make_shared(AndroidRenderingAPI::kSoftware); } @@ -63,13 +59,11 @@ PlatformViewAndroid::PlatformViewAndroid( PlatformView::Delegate& delegate, flutter::TaskRunners task_runners, std::shared_ptr jni_facade, - bool use_software_rendering, - bool create_onscreen_surface) + bool use_software_rendering) : PlatformViewAndroid(delegate, std::move(task_runners), std::move(jni_facade), - CreateAndroidContext(use_software_rendering, - create_onscreen_surface)) {} + CreateAndroidContext(use_software_rendering)) {} PlatformViewAndroid::PlatformViewAndroid( PlatformView::Delegate& delegate, diff --git a/shell/platform/android/platform_view_android.h b/shell/platform/android/platform_view_android.h index 3bb4195126373..1bd959ef855dd 100644 --- a/shell/platform/android/platform_view_android.h +++ b/shell/platform/android/platform_view_android.h @@ -44,8 +44,7 @@ class PlatformViewAndroid final : public PlatformView { PlatformViewAndroid(PlatformView::Delegate& delegate, flutter::TaskRunners task_runners, std::shared_ptr jni_facade, - bool use_software_rendering, - bool create_onscreen_surface); + bool use_software_rendering); //---------------------------------------------------------------------------- /// @brief Creates a new PlatformViewAndroid but using an existing diff --git a/shell/platform/android/platform_view_android_jni_impl.cc b/shell/platform/android/platform_view_android_jni_impl.cc index 840f71317bd98..ac5d5cdf7be8b 100644 --- a/shell/platform/android/platform_view_android_jni_impl.cc +++ b/shell/platform/android/platform_view_android_jni_impl.cc @@ -123,15 +123,12 @@ static jmethodID g_mutators_stack_push_cliprect_method = nullptr; static jmethodID g_mutators_stack_push_cliprrect_method = nullptr; // Called By Java -static jlong AttachJNI(JNIEnv* env, - jclass clazz, - jobject flutterJNI, - jboolean is_background_view) { +static jlong AttachJNI(JNIEnv* env, jclass clazz, jobject flutterJNI) { fml::jni::JavaObjectWeakGlobalRef java_object(env, flutterJNI); std::shared_ptr jni_facade = std::make_shared(java_object); auto shell_holder = std::make_unique( - FlutterMain::Get().GetSettings(), jni_facade, is_background_view); + FlutterMain::Get().GetSettings(), jni_facade); if (shell_holder->IsValid()) { return reinterpret_cast(shell_holder.release()); } else { @@ -613,7 +610,7 @@ bool RegisterApi(JNIEnv* env) { // Start of methods from FlutterJNI { .name = "nativeAttach", - .signature = "(Lio/flutter/embedding/engine/FlutterJNI;Z)J", + .signature = "(Lio/flutter/embedding/engine/FlutterJNI;)J", .fnPtr = reinterpret_cast(&AttachJNI), }, { diff --git a/shell/platform/android/test/io/flutter/embedding/engine/FlutterEngineGroupComponentTest.java b/shell/platform/android/test/io/flutter/embedding/engine/FlutterEngineGroupComponentTest.java index 42e1296505e5a..363ca14fc8514 100644 --- a/shell/platform/android/test/io/flutter/embedding/engine/FlutterEngineGroupComponentTest.java +++ b/shell/platform/android/test/io/flutter/embedding/engine/FlutterEngineGroupComponentTest.java @@ -50,7 +50,7 @@ public void setUp() { MockitoAnnotations.initMocks(this); jniAttached = false; when(mockflutterJNI.isAttached()).thenAnswer(invocation -> jniAttached); - doAnswer(invocation -> jniAttached = true).when(mockflutterJNI).attachToNative(false); + doAnswer(invocation -> jniAttached = true).when(mockflutterJNI).attachToNative(); GeneratedPluginRegistrant.clearRegisteredEngines(); when(mockFlutterLoader.findAppBundlePath()).thenReturn("some/path/to/flutter_assets"); diff --git a/shell/platform/android/test/io/flutter/embedding/engine/FlutterEngineTest.java b/shell/platform/android/test/io/flutter/embedding/engine/FlutterEngineTest.java index 8066946c62e85..26b0ee9eaa90a 100644 --- a/shell/platform/android/test/io/flutter/embedding/engine/FlutterEngineTest.java +++ b/shell/platform/android/test/io/flutter/embedding/engine/FlutterEngineTest.java @@ -57,7 +57,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable { } }) .when(flutterJNI) - .attachToNative(false); + .attachToNative(); GeneratedPluginRegistrant.clearRegisteredEngines(); } @@ -282,7 +282,7 @@ public void itDoesNotAttachAgainWhenBuiltWithAnAttachedJNI() throws NameNotFound /*dartVmArgs=*/ new String[] {}, /*automaticallyRegisterPlugins=*/ false); - verify(flutterJNI, never()).attachToNative(false); + verify(flutterJNI, never()).attachToNative(); } @Test diff --git a/shell/platform/android/test/io/flutter/embedding/engine/PluginComponentTest.java b/shell/platform/android/test/io/flutter/embedding/engine/PluginComponentTest.java index 0c9927deda55e..22f52148f340c 100644 --- a/shell/platform/android/test/io/flutter/embedding/engine/PluginComponentTest.java +++ b/shell/platform/android/test/io/flutter/embedding/engine/PluginComponentTest.java @@ -32,7 +32,7 @@ public void pluginsCanAccessFlutterAssetPaths() { FlutterJNI flutterJNI = mock(FlutterJNI.class); jniAttached = false; when(flutterJNI.isAttached()).thenAnswer(invocation -> jniAttached); - doAnswer(invocation -> jniAttached = true).when(flutterJNI).attachToNative(false); + doAnswer(invocation -> jniAttached = true).when(flutterJNI).attachToNative(); FlutterLoader flutterLoader = new FlutterLoader(mockFlutterJNI); diff --git a/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsControllerTest.java b/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsControllerTest.java index 81d884411c0b9..87d5e7a021a1e 100644 --- a/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsControllerTest.java +++ b/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsControllerTest.java @@ -441,7 +441,7 @@ public void onEndFrame__destroysOverlaySurfaceAfterFrameOnFlutterSurfaceView() { platformViewsController.getRegistry().registerViewFactory("testType", viewFactory); final FlutterJNI jni = new FlutterJNI(); - jni.attachToNative(false); + jni.attachToNative(); attach(jni, platformViewsController); jni.onFirstFrame(); @@ -504,7 +504,7 @@ public void onEndFrame__removesPlatformView() { platformViewsController.getRegistry().registerViewFactory("testType", viewFactory); final FlutterJNI jni = new FlutterJNI(); - jni.attachToNative(false); + jni.attachToNative(); attach(jni, platformViewsController); jni.onFirstFrame(); @@ -540,7 +540,7 @@ public void onEndFrame__removesPlatformViewParent() { platformViewsController.getRegistry().registerViewFactory("testType", viewFactory); final FlutterJNI jni = new FlutterJNI(); - jni.attachToNative(false); + jni.attachToNative(); final FlutterView flutterView = attach(jni, platformViewsController); @@ -577,7 +577,7 @@ public void detach__destroysOverlaySurfaces() { platformViewsController.getRegistry().registerViewFactory("testType", viewFactory); final FlutterJNI jni = new FlutterJNI(); - jni.attachToNative(false); + jni.attachToNative(); attach(jni, platformViewsController); jni.onFirstFrame(); @@ -632,7 +632,7 @@ public void detachFromView__removesOverlaySurfaces() { platformViewsController.getRegistry().registerViewFactory("testType", viewFactory); final FlutterJNI jni = new FlutterJNI(); - jni.attachToNative(false); + jni.attachToNative(); attach(jni, platformViewsController); final FlutterImageView overlayImageView = mock(FlutterImageView.class); @@ -670,7 +670,7 @@ public void destroyOverlaySurfaces__doesNotThrowIfControllerIsDetached() { platformViewsController.getRegistry().registerViewFactory("testType", viewFactory); final FlutterJNI jni = new FlutterJNI(); - jni.attachToNative(false); + jni.attachToNative(); attach(jni, platformViewsController); final FlutterImageView overlayImageView = mock(FlutterImageView.class); @@ -710,7 +710,7 @@ public void convertPlatformViewRenderSurfaceAsDefault() { platformViewsController.getRegistry().registerViewFactory("testType", viewFactory); final FlutterJNI jni = new FlutterJNI(); - jni.attachToNative(false); + jni.attachToNative(); final FlutterView flutterView = attach(jni, platformViewsController); jni.onFirstFrame(); @@ -756,7 +756,7 @@ public void dontConverRenderSurfaceWhenFlagIsTrue() { platformViewsController.getRegistry().registerViewFactory("testType", viewFactory); final FlutterJNI jni = new FlutterJNI(); - jni.attachToNative(false); + jni.attachToNative(); final FlutterView flutterView = attach(jni, platformViewsController); jni.onFirstFrame(); @@ -912,7 +912,7 @@ public boolean getIsSoftwareRenderingEnabled() { } @Implementation - public long performNativeAttach(FlutterJNI flutterJNI, boolean isBackgroundView) { + public long performNativeAttach(FlutterJNI flutterJNI) { return 1; } From 9e3b166abdd258d14feae55901149570149a8f16 Mon Sep 17 00:00:00 2001 From: ColdPaleLight Date: Mon, 13 Sep 2021 23:18:00 +0800 Subject: [PATCH 2/2] Ignore isBackgroundView in FlutterNativeView --- shell/platform/android/io/flutter/view/FlutterNativeView.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/shell/platform/android/io/flutter/view/FlutterNativeView.java b/shell/platform/android/io/flutter/view/FlutterNativeView.java index fd6ac03f4a429..63005dc8bb534 100644 --- a/shell/platform/android/io/flutter/view/FlutterNativeView.java +++ b/shell/platform/android/io/flutter/view/FlutterNativeView.java @@ -53,6 +53,9 @@ public FlutterNativeView(@NonNull Context context) { } public FlutterNativeView(@NonNull Context context, boolean isBackgroundView) { + if (isBackgroundView) { + Log.w(TAG, "'isBackgroundView' is no longer supported and will be ignored"); + } mContext = context; mPluginRegistry = new FlutterPluginRegistry(this, context); mFlutterJNI = new FlutterJNI();