Delete is_background_view from FlutterJNI (flutter/engine#28566)

This commit is contained in:
ColdPaleLight
2021-09-24 04:58:01 +08:00
committed by GitHub
parent 1b2b1cae4c
commit b5b786f13a
12 changed files with 47 additions and 70 deletions

View File

@@ -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());
@@ -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

View File

@@ -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();

View File

@@ -357,8 +357,7 @@ public class 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.");

View File

@@ -62,10 +62,9 @@ import java.util.concurrent.CopyOnWriteArraySet;
* <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:
@@ -306,18 +305,18 @@ public class FlutterJNI {
* <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.
@@ -327,7 +326,7 @@ public class FlutterJNI {
* 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
@@ -360,7 +359,7 @@ public class FlutterJNI {
* <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.

View File

@@ -53,13 +53,16 @@ public class FlutterNativeView implements BinaryMessenger {
}
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();
}
@@ -148,8 +151,8 @@ public class FlutterNativeView implements BinaryMessenger {
return mFlutterJNI;
}
private void attach(FlutterNativeView view, boolean isBackgroundView) {
mFlutterJNI.attachToNative(isBackgroundView);
private void attach(FlutterNativeView view) {
mFlutterJNI.attachToNative();
dartExecutor.onAttachedToJNI();
}

View File

@@ -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);
}
@@ -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,

View File

@@ -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

View File

@@ -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 {
@@ -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),
},
{

View File

@@ -50,7 +50,7 @@ public class FlutterEngineGroupComponentTest {
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");

View File

@@ -57,7 +57,7 @@ public class FlutterEngineTest {
}
})
.when(flutterJNI)
.attachToNative(false);
.attachToNative();
GeneratedPluginRegistrant.clearRegisteredEngines();
}
@@ -282,7 +282,7 @@ public class FlutterEngineTest {
/*dartVmArgs=*/ new String[] {},
/*automaticallyRegisterPlugins=*/ false);
verify(flutterJNI, never()).attachToNative(false);
verify(flutterJNI, never()).attachToNative();
}
@Test

View File

@@ -32,7 +32,7 @@ public class PluginComponentTest {
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);

View File

@@ -441,7 +441,7 @@ public class PlatformViewsControllerTest {
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 class PlatformViewsControllerTest {
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 class PlatformViewsControllerTest {
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 class PlatformViewsControllerTest {
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 class PlatformViewsControllerTest {
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 class PlatformViewsControllerTest {
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 class PlatformViewsControllerTest {
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 class PlatformViewsControllerTest {
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 class PlatformViewsControllerTest {
}
@Implementation
public long performNativeAttach(FlutterJNI flutterJNI, boolean isBackgroundView) {
public long performNativeAttach(FlutterJNI flutterJNI) {
return 1;
}