[Android] Prevent FlutterRenderer listener from calling JNI after detach (flutter/engine#19558)

This commit is contained in:
Gary Qian
2020-07-13 06:29:30 -07:00
committed by GitHub
parent 8b8f6aadb8
commit d298a08c85
2 changed files with 24 additions and 5 deletions

View File

@@ -131,11 +131,11 @@ public class FlutterRenderer implements TextureRegistry {
new SurfaceTexture.OnFrameAvailableListener() {
@Override
public void onFrameAvailable(@NonNull SurfaceTexture texture) {
if (released) {
// Even though we make sure to unregister the callback before releasing, as of Android
// O
// SurfaceTexture has a data race when accessing the callback, so the callback may
// still be called by a stale reference after released==true and mNativeView==null.
if (released || !flutterJNI.isAttached()) {
// Even though we make sure to unregister the callback before releasing, as of
// Android O, SurfaceTexture has a data race when accessing the callback, so the
// callback may still be called by a stale reference after released==true and
// mNativeView==null.
return;
}
markTextureFrameAvailable(id);

View File

@@ -97,4 +97,23 @@ public class FlutterRendererTest {
// Verify behavior under test.
verify(fakeFlutterJNI, times(1)).onSurfaceDestroyed();
}
@Test
public void itStopsSurfaceTextureCallbackWhenDetached() {
// Setup the test.
FlutterRenderer flutterRenderer = new FlutterRenderer(fakeFlutterJNI);
fakeFlutterJNI.detachFromNativeAndReleaseResources();
FlutterRenderer.SurfaceTextureRegistryEntry entry =
(FlutterRenderer.SurfaceTextureRegistryEntry) flutterRenderer.createSurfaceTexture();
flutterRenderer.startRenderingToSurface(fakeSurface);
// Execute the behavior under test.
flutterRenderer.stopRenderingToSurface();
// Verify behavior under test.
verify(fakeFlutterJNI, times(0)).markTextureFrameAvailable(eq(entry.id()));
}
}