diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformViewsChannel.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformViewsChannel.java
index 3d3f78f73f..87891710bd 100644
--- a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformViewsChannel.java
+++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformViewsChannel.java
@@ -73,6 +73,9 @@ public class PlatformViewsChannel {
case "clearFocus":
clearFocus(call, result);
break;
+ case "synchronizeToNativeViewHierarchy":
+ synchronizeToNativeViewHierarchy(call, result);
+ break;
default:
result.notImplemented();
}
@@ -199,6 +202,17 @@ public class PlatformViewsChannel {
result.error("error", detailedExceptionString(exception), null);
}
}
+
+ private void synchronizeToNativeViewHierarchy(
+ @NonNull MethodCall call, @NonNull MethodChannel.Result result) {
+ boolean yes = call.arguments();
+ try {
+ handler.synchronizeToNativeViewHierarchy(yes);
+ result.success(null);
+ } catch (IllegalStateException exception) {
+ result.error("error", detailedExceptionString(exception), null);
+ }
+ }
};
/**
@@ -282,6 +296,15 @@ public class PlatformViewsChannel {
/** Clears the focus from the platform view with a give id if it is currently focused. */
void clearFocus(int viewId);
+
+ /**
+ * Whether the render surface of {@code FlutterView} should be converted to a {@code
+ * FlutterImageView} when a {@code PlatformView} is added.
+ *
+ *
This is done to syncronize the rendering of the PlatformView and the FlutterView. Defaults
+ * to true.
+ */
+ void synchronizeToNativeViewHierarchy(boolean yes);
}
/** Request sent from Flutter to create a new platform view. */
diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java
index ca5247cc84..8d59a263d7 100644
--- a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java
+++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java
@@ -105,6 +105,11 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
// Tracks whether the flutterView has been converted to use a FlutterImageView.
private boolean flutterViewConvertedToImageView = false;
+ // When adding platform views using Hybrid Composition, the engine converts the render surface
+ // to a FlutterImageView to help improve animation synchronization on Android. This flag allows
+ // disabling this conversion through the PlatformView platform channel.
+ private boolean synchronizeToNativeViewHierarchy = true;
+
// Overlay layer IDs that were displayed since the start of the current frame.
private HashSet currentFrameUsedOverlayLayerIds;
@@ -356,6 +361,11 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
+ minSdkVersion);
}
}
+
+ @Override
+ public void synchronizeToNativeViewHierarchy(boolean yes) {
+ synchronizeToNativeViewHierarchy = yes;
+ }
};
@VisibleForTesting
@@ -704,7 +714,7 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
}
private void initializeRootImageViewIfNeeded() {
- if (!flutterViewConvertedToImageView) {
+ if (synchronizeToNativeViewHierarchy && !flutterViewConvertedToImageView) {
((FlutterView) flutterView).convertToImageView();
flutterViewConvertedToImageView = true;
}
@@ -890,13 +900,17 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
final int viewId = platformViewParent.keyAt(i);
final View parentView = platformViewParent.get(viewId);
- // Show platform views only if the surfaces have images available in this frame,
- // and if the platform view is rendered in this frame.
+ // This should only show platform views that are rendered in this frame and either:
+ // 1. Surface has images available in this frame or,
+ // 2. Surface does not have images available in this frame because the render surface should
+ // not be an ImageView.
+ //
// The platform view is appended to a mutator view.
//
// Otherwise, hide the platform view, but don't remove it from the view hierarchy yet as
// they are removed when the framework diposes the platform view widget.
- if (isFrameRenderedUsingImageReaders && currentFrameUsedPlatformViewIds.contains(viewId)) {
+ if (currentFrameUsedPlatformViewIds.contains(viewId)
+ && (isFrameRenderedUsingImageReaders || !synchronizeToNativeViewHierarchy)) {
parentView.setVisibility(View.VISIBLE);
} else {
parentView.setVisibility(View.GONE);
diff --git a/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsControllerTest.java b/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsControllerTest.java
index 12db03678b..d2e3944a20 100644
--- a/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsControllerTest.java
+++ b/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsControllerTest.java
@@ -630,6 +630,100 @@ public class PlatformViewsControllerTest {
assertFalse(shouldProxying);
}
+ @Test
+ @Config(shadows = {ShadowFlutterJNI.class})
+ public void convertPlatformViewRenderSurfaceAsDefault() {
+ final PlatformViewsController platformViewsController = new PlatformViewsController();
+
+ final int platformViewId = 0;
+ assertNull(platformViewsController.getPlatformViewById(platformViewId));
+
+ final PlatformViewFactory viewFactory = mock(PlatformViewFactory.class);
+ final PlatformView platformView = mock(PlatformView.class);
+ final View androidView = mock(View.class);
+ when(platformView.getView()).thenReturn(androidView);
+ when(viewFactory.create(any(), eq(platformViewId), any())).thenReturn(platformView);
+
+ platformViewsController.getRegistry().registerViewFactory("testType", viewFactory);
+
+ final FlutterJNI jni = new FlutterJNI();
+ jni.attachToNative(false);
+ final FlutterView flutterView = attach(jni, platformViewsController);
+
+ jni.onFirstFrame();
+
+ // Simulate create call from the framework.
+ createPlatformView(jni, platformViewsController, platformViewId, "testType");
+
+ // Produce a frame that displays a platform view and an overlay surface.
+ platformViewsController.onBeginFrame();
+ platformViewsController.onDisplayPlatformView(
+ platformViewId,
+ /* x=*/ 0,
+ /* y=*/ 0,
+ /* width=*/ 10,
+ /* height=*/ 10,
+ /* viewWidth=*/ 10,
+ /* viewHeight=*/ 10,
+ /* mutatorsStack=*/ new FlutterMutatorsStack());
+
+ assertEquals(flutterView.getChildCount(), 3);
+
+ final View view = flutterView.getChildAt(1);
+ assertTrue(view instanceof FlutterImageView);
+
+ // Simulate dispose call from the framework.
+ disposePlatformView(jni, platformViewsController, platformViewId);
+ }
+
+ @Test
+ @Config(shadows = {ShadowFlutterJNI.class})
+ public void dontConverRenderSurfaceWhenFlagIsTrue() {
+ final PlatformViewsController platformViewsController = new PlatformViewsController();
+
+ final int platformViewId = 0;
+ assertNull(platformViewsController.getPlatformViewById(platformViewId));
+
+ final PlatformViewFactory viewFactory = mock(PlatformViewFactory.class);
+ final PlatformView platformView = mock(PlatformView.class);
+ final View androidView = mock(View.class);
+ when(platformView.getView()).thenReturn(androidView);
+ when(viewFactory.create(any(), eq(platformViewId), any())).thenReturn(platformView);
+
+ platformViewsController.getRegistry().registerViewFactory("testType", viewFactory);
+
+ final FlutterJNI jni = new FlutterJNI();
+ jni.attachToNative(false);
+ final FlutterView flutterView = attach(jni, platformViewsController);
+
+ jni.onFirstFrame();
+
+ // Simulate setting render surface conversion flag.
+ synchronizeToNativeViewHierarchy(jni, platformViewsController, false);
+
+ // Simulate create call from the framework.
+ createPlatformView(jni, platformViewsController, platformViewId, "testType");
+
+ // Produce a frame that displays a platform view and an overlay surface.
+ platformViewsController.onBeginFrame();
+ platformViewsController.onDisplayPlatformView(
+ platformViewId,
+ /* x=*/ 0,
+ /* y=*/ 0,
+ /* width=*/ 10,
+ /* height=*/ 10,
+ /* viewWidth=*/ 10,
+ /* viewHeight=*/ 10,
+ /* mutatorsStack=*/ new FlutterMutatorsStack());
+
+ assertEquals(flutterView.getChildCount(), 2);
+ assertTrue(!(flutterView.getChildAt(0) instanceof FlutterImageView));
+ assertTrue(flutterView.getChildAt(1) instanceof FlutterMutatorView);
+
+ // Simulate dispose call from the framework.
+ disposePlatformView(jni, platformViewsController, platformViewId);
+ }
+
private static ByteBuffer encodeMethodCall(MethodCall call) {
final ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeMethodCall(call);
buffer.rewind();
@@ -667,6 +761,15 @@ public class PlatformViewsControllerTest {
"flutter/platform_views", encodeMethodCall(platformDisposeMethodCall), /*replyId=*/ 0);
}
+ private static void synchronizeToNativeViewHierarchy(
+ FlutterJNI jni, PlatformViewsController platformViewsController, boolean yes) {
+
+ final MethodCall convertMethodCall = new MethodCall("synchronizeToNativeViewHierarchy", yes);
+
+ jni.handlePlatformMessage(
+ "flutter/platform_views", encodeMethodCall(convertMethodCall), /*replyId=*/ 0);
+ }
+
private static FlutterView attach(
FlutterJNI jni, PlatformViewsController platformViewsController) {
final DartExecutor executor = new DartExecutor(jni, mock(AssetManager.class));