Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Delete is_background_view from FlutterJNI #28566

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 9 additions & 22 deletions shell/platform/android/android_shell_holder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,26 @@ static PlatformData GetDefaultPlatformData() {

AndroidShellHolder::AndroidShellHolder(
flutter::Settings settings,
std::shared_ptr<PlatformViewAndroidJNI> jni_facade,
bool is_background_view)
std::shared_ptr<PlatformViewAndroidJNI> 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<ThreadHost>();
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<PlatformViewAndroid> weak_platform_view;
Shell::CreateCallback<PlatformView> on_create_platform_view =
[is_background_view, &jni_facade, &weak_platform_view](Shell& shell) {
[&jni_facade, &weak_platform_view](Shell& shell) {
std::unique_ptr<PlatformViewAndroid> platform_view_android;
platform_view_android = std::make_unique<PlatformViewAndroid>(
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());
Expand All @@ -84,16 +78,9 @@ AndroidShellHolder::AndroidShellHolder(
fml::RefPtr<fml::TaskRunner> io_runner;
fml::RefPtr<fml::TaskRunner> 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
Expand Down
3 changes: 1 addition & 2 deletions shell/platform/android/android_shell_holder.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ namespace flutter {
class AndroidShellHolder {
public:
AndroidShellHolder(flutter::Settings settings,
std::shared_ptr<PlatformViewAndroidJNI> jni_facade,
bool is_background_view);
std::shared_ptr<PlatformViewAndroidJNI> jni_facade);

~AndroidShellHolder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down
21 changes: 10 additions & 11 deletions shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,9 @@
* <p>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.
*
* <p>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:
Expand Down Expand Up @@ -306,18 +305,18 @@ public boolean isAttached() {
* <p>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.
Expand All @@ -327,7 +326,7 @@ public long performNativeAttach(@NonNull FlutterJNI flutterJNI, boolean isBackgr
* FlutterJNI by calling its standard constructor.
*
* <p>This can only be called once the current FlutterJNI instance is attached by calling {@link
* #attachToNative(boolean)}.
* #attachToNative()}.
*
* <p>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
Expand Down Expand Up @@ -360,7 +359,7 @@ private native FlutterJNI nativeSpawn(
* <p>This method must not be invoked if {@code FlutterJNI} is not already attached to native.
*
* <p>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.
*
* <p>It is permissible to re-attach this instance to native after detaching it from native.
Expand Down
9 changes: 6 additions & 3 deletions shell/platform/android/io/flutter/view/FlutterNativeView.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,16 @@ 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();
mFlutterJNI.addIsDisplayingFlutterUiListener(flutterUiDisplayListener);
this.dartExecutor = new DartExecutor(mFlutterJNI, context.getAssets());
mFlutterJNI.addEngineLifecycleListener(new EngineLifecycleListenerImpl());
attach(this, isBackgroundView);
attach(this);
assertAttached();
}

Expand Down Expand Up @@ -148,8 +151,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();
}

Expand Down
12 changes: 3 additions & 9 deletions shell/platform/android/platform_view_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@ std::unique_ptr<AndroidSurface> AndroidSurfaceFactoryImpl::CreateSurface() {
}

static std::shared_ptr<flutter::AndroidContext> 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<AndroidContext>(AndroidRenderingAPI::kSoftware);
}
Expand All @@ -63,13 +59,11 @@ PlatformViewAndroid::PlatformViewAndroid(
PlatformView::Delegate& delegate,
flutter::TaskRunners task_runners,
std::shared_ptr<PlatformViewAndroidJNI> 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,
Expand Down
3 changes: 1 addition & 2 deletions shell/platform/android/platform_view_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ class PlatformViewAndroid final : public PlatformView {
PlatformViewAndroid(PlatformView::Delegate& delegate,
flutter::TaskRunners task_runners,
std::shared_ptr<PlatformViewAndroidJNI> jni_facade,
bool use_software_rendering,
bool create_onscreen_surface);
bool use_software_rendering);

//----------------------------------------------------------------------------
/// @brief Creates a new PlatformViewAndroid but using an existing
Expand Down
9 changes: 3 additions & 6 deletions shell/platform/android/platform_view_android_jni_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<PlatformViewAndroidJNI> jni_facade =
std::make_shared<PlatformViewAndroidJNIImpl>(java_object);
auto shell_holder = std::make_unique<AndroidShellHolder>(
FlutterMain::Get().GetSettings(), jni_facade, is_background_view);
FlutterMain::Get().GetSettings(), jni_facade);
if (shell_holder->IsValid()) {
return reinterpret_cast<jlong>(shell_holder.release());
} else {
Expand Down Expand Up @@ -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<void*>(&AttachJNI),
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
}
})
.when(flutterJNI)
.attachToNative(false);
.attachToNative();
GeneratedPluginRegistrant.clearRegisteredEngines();
}

Expand Down Expand Up @@ -282,7 +282,7 @@ public void itDoesNotAttachAgainWhenBuiltWithAnAttachedJNI() throws NameNotFound
/*dartVmArgs=*/ new String[] {},
/*automaticallyRegisterPlugins=*/ false);

verify(flutterJNI, never()).attachToNative(false);
verify(flutterJNI, never()).attachToNative();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -912,7 +912,7 @@ public boolean getIsSoftwareRenderingEnabled() {
}

@Implementation
public long performNativeAttach(FlutterJNI flutterJNI, boolean isBackgroundView) {
public long performNativeAttach(FlutterJNI flutterJNI) {
return 1;
}

Expand Down