Support software rendering when textures are used (flutter/engine#32178)
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
* <p>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.
|
||||
*
|
||||
* <p>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.
|
||||
*
|
||||
|
||||
@@ -235,8 +235,12 @@ void PlatformViewAndroid::UpdateSemantics(
|
||||
void PlatformViewAndroid::RegisterExternalTexture(
|
||||
int64_t texture_id,
|
||||
const fml::jni::ScopedJavaGlobalRef<jobject>& surface_texture) {
|
||||
RegisterTexture(std::make_shared<AndroidExternalTextureGL>(
|
||||
texture_id, surface_texture, std::move(jni_facade_)));
|
||||
if (android_context_->RenderingApi() == AndroidRenderingAPI::kOpenGLES) {
|
||||
RegisterTexture(std::make_shared<AndroidExternalTextureGL>(
|
||||
texture_id, surface_texture, std::move(jni_facade_)));
|
||||
} else {
|
||||
FML_LOG(INFO) << "Attempted to use a GL texture in a non GL context.";
|
||||
}
|
||||
}
|
||||
|
||||
// |PlatformView|
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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 {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user