[Android] Fix the issue of blank or frozen pages in shared engine scenarios (flutter/engine#50947)

Consider this scenario: In an add-to-app context, where multiple Flutter activities share the same engine, a situation occurs. When navigating from FlutterActivity1 to FlutterActivity2, the Flutter view associated with FlutterActivity1 is detached from the engine. Then, the Flutter view of FlutterActivity2 is attached. Upon navigating back to FlutterActivity1, its Flutter view is re-attached to the shared engine.

The expected behavior is: When a Flutter view detaches from the shared engine, the associated surface should be released. When the Flutter view re-attaches, a new surface should be created.

After #47358, no new surface is created when the Flutter view is attached again. This results in the Flutter view having no underlying surface, which causes the page to appear blank or freeze without responding.

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
Rulong Chen(陈汝龙)
2024-03-22 11:38:35 +08:00
committed by GitHub
parent 9c54875efc
commit a697f6e769
4 changed files with 134 additions and 6 deletions

View File

@@ -12,6 +12,7 @@ import android.view.SurfaceHolder;
import android.view.SurfaceView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import io.flutter.Log;
import io.flutter.embedding.engine.renderer.FlutterRenderer;
import io.flutter.embedding.engine.renderer.FlutterUiDisplayListener;
@@ -168,6 +169,10 @@ public class FlutterSurfaceView extends SurfaceView implements RenderSurface {
return flutterRenderer;
}
@VisibleForTesting
/* package */ boolean isSurfaceAvailableForRendering() {
return isSurfaceAvailableForRendering;
}
/**
* Invoked by the owner of this {@code FlutterSurfaceView} when it wants to begin rendering a
* Flutter UI to this {@code FlutterSurfaceView}.
@@ -215,8 +220,6 @@ public class FlutterSurfaceView extends SurfaceView implements RenderSurface {
disconnectSurfaceFromRenderer();
}
pause();
// Make the SurfaceView invisible to avoid showing a black rectangle.
setAlpha(0.0f);
flutterRenderer.removeIsDisplayingFlutterUiListener(flutterUiDisplayListener);
@@ -248,7 +251,7 @@ public class FlutterSurfaceView extends SurfaceView implements RenderSurface {
// If we're already attached to an Android window then we're now attached to both a renderer
// and the Android window. We can begin rendering now.
if (isSurfaceAvailableForRendering) {
if (isSurfaceAvailableForRendering()) {
Log.v(
TAG,
"Surface is available for rendering. Connecting FlutterRenderer to Android surface.");

View File

@@ -124,6 +124,11 @@ public class FlutterTextureView extends TextureView implements RenderSurface {
return flutterRenderer;
}
@VisibleForTesting
/* package */ boolean isSurfaceAvailableForRendering() {
return isSurfaceAvailableForRendering;
}
/**
* Invoked by the owner of this {@code FlutterTextureView} when it wants to begin rendering a
* Flutter UI to this {@code FlutterTextureView}.
@@ -170,8 +175,6 @@ public class FlutterTextureView extends TextureView implements RenderSurface {
disconnectSurfaceFromRenderer();
}
pause();
flutterRenderer = null;
} else {
Log.w(TAG, "detachFromRenderer() invoked when no FlutterRenderer was attached.");
@@ -198,7 +201,7 @@ public class FlutterTextureView extends TextureView implements RenderSurface {
// If we're already attached to an Android window then we're now attached to both a renderer
// and the Android window. We can begin rendering now.
if (isSurfaceAvailableForRendering) {
if (isSurfaceAvailableForRendering()) {
Log.v(
TAG,
"Surface is available for rendering. Connecting FlutterRenderer to Android surface.");

View File

@@ -0,0 +1,72 @@
package io.flutter.embedding.android;
import static io.flutter.Build.API_LEVELS;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.annotation.TargetApi;
import android.view.Surface;
import android.view.SurfaceHolder;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import io.flutter.embedding.engine.FlutterJNI;
import io.flutter.embedding.engine.renderer.FlutterRenderer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
@Config(manifest = Config.NONE)
@RunWith(AndroidJUnit4.class)
@TargetApi(API_LEVELS.API_30)
public class FlutterSurfaceViewTest {
@Test
public void itShouldCreateANewSurfaceWhenReattachedAfterDetachingFromRenderer() {
// Consider this scenario: In an add-to-app context, where multiple Flutter activities share the
// same engine, a situation occurs. When navigating from FlutterActivity1 to FlutterActivity2,
// the Flutter view associated with FlutterActivity1 is detached from the engine. Then, the
// Flutter view of FlutterActivity2 is attached. Upon navigating back to FlutterActivity1, its
// Flutter view is re-attached to the shared engine.
//
// The expected behavior is: When a Flutter view detaches from the shared engine, the associated
// surface should be released. When the Flutter view re-attaches, a new surface should be
// created.
// Setup the test.
final FlutterSurfaceView surfaceView =
spy(new FlutterSurfaceView(ApplicationProvider.getApplicationContext()));
FlutterJNI fakeFlutterJNI = mock(FlutterJNI.class);
FlutterRenderer flutterRenderer = new FlutterRenderer(fakeFlutterJNI);
SurfaceHolder fakeSurfaceHolder = mock(SurfaceHolder.class);
Surface fakeSurface = mock(Surface.class);
when(surfaceView.getHolder()).thenReturn(fakeSurfaceHolder);
when(fakeSurfaceHolder.getSurface()).thenReturn(fakeSurface);
when(surfaceView.isSurfaceAvailableForRendering()).thenReturn(true);
when(surfaceView.getWindowToken()).thenReturn(mock(android.os.IBinder.class));
// Execute the behavior under test.
surfaceView.attachToRenderer(flutterRenderer);
// Verify the behavior under test.
verify(fakeFlutterJNI, times(1)).onSurfaceCreated(any(Surface.class));
// Execute the behavior under test.
surfaceView.detachFromRenderer();
// Verify the behavior under test.
verify(fakeFlutterJNI, times(1)).onSurfaceDestroyed();
// Execute the behavior under test.
surfaceView.attachToRenderer(flutterRenderer);
// Verify the behavior under test.
verify(fakeFlutterJNI, never()).onSurfaceWindowChanged(any(Surface.class));
verify(fakeFlutterJNI, times(2)).onSurfaceCreated(any(Surface.class));
}
}

View File

@@ -1,8 +1,13 @@
package io.flutter.embedding.android;
import static io.flutter.Build.API_LEVELS;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.annotation.TargetApi;
import android.graphics.SurfaceTexture;
@@ -10,6 +15,8 @@ import android.view.Surface;
import android.view.TextureView;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import io.flutter.embedding.engine.FlutterJNI;
import io.flutter.embedding.engine.renderer.FlutterRenderer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
@@ -31,4 +38,47 @@ public class FlutterTextureViewTest {
verify(mockRenderSurface).release();
}
@Test
public void itShouldCreateANewSurfaceWhenReattachedAfterDetachingFromRenderer() {
// Consider this scenario: In an add-to-app context, where multiple Flutter activities share the
// same engine, a situation occurs. When navigating from FlutterActivity1 to FlutterActivity2,
// the Flutter view associated with FlutterActivity1 is detached from the engine. Then, the
// Flutter view of FlutterActivity2 is attached. Upon navigating back to FlutterActivity1, its
// Flutter view is re-attached to the shared engine.
//
// The expected behavior is: When a Flutter view detaches from the shared engine, the associated
// surface should be released. When the Flutter view re-attaches, a new surface should be
// created.
// Setup the test.
final FlutterTextureView textureView =
spy(new FlutterTextureView(ApplicationProvider.getApplicationContext()));
FlutterJNI fakeFlutterJNI = mock(FlutterJNI.class);
FlutterRenderer flutterRenderer = new FlutterRenderer(fakeFlutterJNI);
when(textureView.isSurfaceAvailableForRendering()).thenReturn(true);
when(textureView.getSurfaceTexture()).thenReturn(mock(SurfaceTexture.class));
when(textureView.getWindowToken()).thenReturn(mock(android.os.IBinder.class));
// Execute the behavior under test.
textureView.attachToRenderer(flutterRenderer);
// Verify the behavior under test.
verify(fakeFlutterJNI, times(1)).onSurfaceCreated(any(Surface.class));
// Execute the behavior under test.
textureView.detachFromRenderer();
// Verify the behavior under test.
verify(fakeFlutterJNI, times(1)).onSurfaceDestroyed();
// Execute the behavior under test.
textureView.attachToRenderer(flutterRenderer);
// Verify the behavior under test.
verify(fakeFlutterJNI, never()).onSurfaceWindowChanged(any(Surface.class));
verify(fakeFlutterJNI, times(2)).onSurfaceCreated(any(Surface.class));
}
}