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