From eb5ac3144df40e0e56c13a49bd84caed6cf1d1fb Mon Sep 17 00:00:00 2001 From: Emmanuel Garcia Date: Tue, 22 Mar 2022 14:10:10 -0700 Subject: [PATCH] Support software rendering when textures are used (flutter/engine#32178) --- .../FlutterEngineConnectionRegistry.java | 6 +++ .../plugin/platform/PlatformViewWrapper.java | 9 ++-- .../platform/PlatformViewsController.java | 35 ++++++++++++++-- .../platform/android/platform_view_android.cc | 8 +++- .../FlutterEngineConnectionRegistryTest.java | 42 ++++++++++++++++--- .../platform/PlatformViewWrapperTest.java | 27 ++++++++++++ 6 files changed, 112 insertions(+), 15 deletions(-) diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterEngineConnectionRegistry.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterEngineConnectionRegistry.java index 9aca6d93cb..b3203179de 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterEngineConnectionRegistry.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterEngineConnectionRegistry.java @@ -330,6 +330,12 @@ import java.util.Set; private void attachToActivityInternal(@NonNull Activity activity, @NonNull Lifecycle lifecycle) { this.activityPluginBinding = new FlutterEngineActivityPluginBinding(activity, lifecycle); + final boolean useSoftwareRendering = + activity + .getIntent() + .getBooleanExtra(FlutterShellArgs.ARG_KEY_ENABLE_SOFTWARE_RENDERING, false); + flutterEngine.getPlatformViewsController().setSoftwareRendering(useSoftwareRendering); + // Activate the PlatformViewsController. This must happen before any plugins attempt // to use it, otherwise an error stack trace will appear that says there is no // flutter/platform_views channel. diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewWrapper.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewWrapper.java index 4cbc514cb7..347ba1ccbf 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewWrapper.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewWrapper.java @@ -54,7 +54,6 @@ class PlatformViewWrapper extends FrameLayout { private AndroidTouchProcessor touchProcessor; @Nullable @VisibleForTesting ViewTreeObserver.OnGlobalFocusChangeListener activeFocusListener; - @Nullable private TextureRegistry.SurfaceTextureEntry textureEntry; private final AtomicLong pendingFramesCount = new AtomicLong(0L); private final TextureRegistry.OnFrameConsumedListener listener = @@ -88,7 +87,6 @@ class PlatformViewWrapper extends FrameLayout { public PlatformViewWrapper( @NonNull Context context, @NonNull TextureRegistry.SurfaceTextureEntry textureEntry) { this(context); - this.textureEntry = textureEntry; textureEntry.setOnFrameConsumedListener(listener); setTexture(textureEntry.surfaceTexture()); } @@ -230,7 +228,12 @@ class PlatformViewWrapper extends FrameLayout { @Override @SuppressLint("NewApi") public void draw(Canvas canvas) { - if (surface == null || !surface.isValid()) { + if (surface == null) { + super.draw(canvas); + Log.e(TAG, "Platform view cannot be composed without a surface."); + return; + } + if (!surface.isValid()) { Log.e(TAG, "Invalid surface. The platform view cannot be displayed."); return; } 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 eeb55d2b99..3b7fb9faf8 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 @@ -111,6 +111,9 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega // Used to acquire the original motion events using the motionEventIds. private final MotionEventTracker motionEventTracker; + // Whether software rendering is used. + private boolean usesSoftwareRendering = false; + private final PlatformViewsChannel.PlatformViewsHandler channelHandler = new PlatformViewsChannel.PlatformViewsHandler() { @@ -188,9 +191,17 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega final PlatformView platformView = viewFactory.create(context, viewId, createParams); platformViews.put(viewId, platformView); - final TextureRegistry.SurfaceTextureEntry textureEntry = - textureRegistry.createSurfaceTexture(); - final PlatformViewWrapper wrapperView = new PlatformViewWrapper(context, textureEntry); + PlatformViewWrapper wrapperView; + long txId; + if (usesSoftwareRendering) { + wrapperView = new PlatformViewWrapper(context); + txId = -1; + } else { + final TextureRegistry.SurfaceTextureEntry textureEntry = + textureRegistry.createSurfaceTexture(); + wrapperView = new PlatformViewWrapper(context, textureEntry); + txId = textureEntry.id(); + } wrapperView.setTouchProcessor(androidTouchProcessor); final int physicalWidth = toPhysicalPixels(request.logicalWidth); @@ -227,7 +238,7 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega flutterView.addView(wrapperView); viewWrappers.append(viewId, wrapperView); - return textureEntry.id(); + return txId; } @Override @@ -483,6 +494,22 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega platformViewsChannel.setPlatformViewsHandler(channelHandler); } + /** + * Sets whether Flutter uses software rendering. + * + *

When software rendering is used, no GL context is available on the raster thread. When this + * is set to true, there's no Flutter composition of Android views and Flutter widgets since GL + * textures cannot be used. + * + *

Software rendering is only used for testing in emulators, and it should never be set to true + * in a production environment. + * + * @param useSoftwareRendering Whether software rendering is used. + */ + public void setSoftwareRendering(boolean useSoftwareRendering) { + usesSoftwareRendering = useSoftwareRendering; + } + /** * Detaches this platform views controller. * diff --git a/engine/src/flutter/shell/platform/android/platform_view_android.cc b/engine/src/flutter/shell/platform/android/platform_view_android.cc index 63c4b42d5e..a41018c914 100644 --- a/engine/src/flutter/shell/platform/android/platform_view_android.cc +++ b/engine/src/flutter/shell/platform/android/platform_view_android.cc @@ -235,8 +235,12 @@ void PlatformViewAndroid::UpdateSemantics( void PlatformViewAndroid::RegisterExternalTexture( int64_t texture_id, const fml::jni::ScopedJavaGlobalRef& surface_texture) { - RegisterTexture(std::make_shared( - texture_id, surface_texture, std::move(jni_facade_))); + if (android_context_->RenderingApi() == AndroidRenderingAPI::kOpenGLES) { + RegisterTexture(std::make_shared( + texture_id, surface_texture, std::move(jni_facade_))); + } else { + FML_LOG(INFO) << "Attempted to use a GL texture in a non GL context."; + } } // |PlatformView| diff --git a/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/FlutterEngineConnectionRegistryTest.java b/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/FlutterEngineConnectionRegistryTest.java index 5faf547bf5..8bbc729d8b 100644 --- a/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/FlutterEngineConnectionRegistryTest.java +++ b/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/FlutterEngineConnectionRegistryTest.java @@ -1,10 +1,8 @@ package io.flutter.embedding.engine; -import static junit.framework.TestCase.assertTrue; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +import static org.junit.Assert.*; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.*; import android.app.Activity; import android.content.Context; @@ -80,8 +78,10 @@ public class FlutterEngineConnectionRegistryTest { Activity activity = mock(Activity.class); when(appComponent.getAppComponent()).thenReturn(activity); - Lifecycle lifecycle = mock(Lifecycle.class); Intent intent = mock(Intent.class); + when(activity.getIntent()).thenReturn(intent); + + Lifecycle lifecycle = mock(Lifecycle.class); AtomicBoolean isFirstCall = new AtomicBoolean(true); // Set up the environment to get the required internal data @@ -113,6 +113,36 @@ public class FlutterEngineConnectionRegistryTest { assertEquals(3, listener1.callCount + listener2.callCount); } + @Test + public void softwareRendering() { + Context context = mock(Context.class); + + FlutterEngine flutterEngine = mock(FlutterEngine.class); + PlatformViewsController platformViewsController = mock(PlatformViewsController.class); + when(flutterEngine.getPlatformViewsController()).thenReturn(platformViewsController); + + FlutterLoader flutterLoader = mock(FlutterLoader.class); + + ExclusiveAppComponent appComponent = mock(ExclusiveAppComponent.class); + Activity activity = mock(Activity.class); + when(appComponent.getAppComponent()).thenReturn(activity); + + Intent intent = mock(Intent.class); + when(intent.getBooleanExtra("enable-software-rendering", false)).thenReturn(false); + when(activity.getIntent()).thenReturn(intent); + + FlutterEngineConnectionRegistry registry = + new FlutterEngineConnectionRegistry(context, flutterEngine, flutterLoader); + + registry.attachToActivity(appComponent, mock(Lifecycle.class)); + verify(platformViewsController).setSoftwareRendering(false); + + when(intent.getBooleanExtra("enable-software-rendering", false)).thenReturn(true); + + registry.attachToActivity(appComponent, mock(Lifecycle.class)); + verify(platformViewsController).setSoftwareRendering(true); + } + private static class FakeFlutterPlugin implements FlutterPlugin { public int attachmentCallCount = 0; public int detachmentCallCount = 0; diff --git a/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java b/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java index 5ed19a7383..df488d4728 100644 --- a/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java +++ b/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java @@ -21,6 +21,8 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; +import org.robolectric.annotation.Implements; @TargetApi(31) @RunWith(AndroidJUnit4.class) @@ -117,6 +119,28 @@ public class PlatformViewWrapperTest { verifyNoMoreInteractions(canvas); } + @Test + @Config( + shadows = { + ShadowView.class, + }) + public void draw_withoutSurface() { + final Context ctx = ApplicationProvider.getApplicationContext(); + final PlatformViewWrapper wrapper = + new PlatformViewWrapper(ctx) { + @Override + public void onDraw(Canvas canvas) { + canvas.drawColor(Color.RED); + } + }; + // Test. + final Canvas canvas = mock(Canvas.class); + wrapper.draw(canvas); + + // Verify. + verify(canvas, times(1)).drawColor(Color.RED); + } + @Test public void release() { final Surface surface = mock(Surface.class); @@ -274,4 +298,7 @@ public class PlatformViewWrapperTest { view.unsetOnDescendantFocusChangeListener(); verify(viewTreeObserver, times(1)).removeOnGlobalFocusChangeListener(activeFocusListener); } + + @Implements(View.class) + public static class ShadowView {} }