Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

[Android] Prevent listener from calling JNI after detach #19558

Merged
merged 4 commits into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ final class SurfaceTextureRegistryEntry implements TextureRegistry.SurfaceTextur
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,23 @@ public void itStopsRenderingToSurfaceWhenRequested() {
// 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()));
}
}