Optimizing performance by avoiding multiple GC operations caused by multiple surface destruction notifications (flutter/engine#43587)

Fixes: https://github.com/flutter/flutter/issues/130379

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
Rulong Chen(陈汝龙)
2023-07-15 16:44:04 +08:00
committed by GitHub
parent 9e98b682c5
commit b761cc404b
2 changed files with 27 additions and 10 deletions

View File

@@ -370,19 +370,20 @@ public class FlutterRenderer implements TextureRegistry {
* android.view.TextureView.SurfaceTextureListener}
*/
public void stopRenderingToSurface() {
flutterJNI.onSurfaceDestroyed();
if (surface != null) {
flutterJNI.onSurfaceDestroyed();
surface = null;
// TODO(mattcarroll): the source of truth for this call should be FlutterJNI, which is where
// the call to onFlutterUiDisplayed() comes from. However, no such native callback exists yet,
// so until the engine and FlutterJNI are configured to call us back when rendering stops,
// we will manually monitor that change here.
if (isDisplayingFlutterUi) {
flutterUiDisplayListener.onFlutterUiNoLongerDisplayed();
}
// TODO(mattcarroll): the source of truth for this call should be FlutterJNI, which is where
// the call to onFlutterUiDisplayed() comes from. However, no such native callback exists yet,
// so until the engine and FlutterJNI are configured to call us back when rendering stops,
// we will manually monitor that change here.
if (isDisplayingFlutterUi) {
flutterUiDisplayListener.onFlutterUiNoLongerDisplayed();
isDisplayingFlutterUi = false;
surface = null;
}
isDisplayingFlutterUi = false;
}
/**

View File

@@ -393,4 +393,20 @@ public class FlutterRendererTest {
// Verify behavior under test.
assertEquals(1, invocationCount.get());
}
@Test
public void itDoesDispatchSurfaceDestructionNotificationOnlyOnce() {
// Setup the test.
FlutterRenderer flutterRenderer = new FlutterRenderer(fakeFlutterJNI);
flutterRenderer.startRenderingToSurface(fakeSurface, /*keepCurrentSurface=*/ false);
// Execute the behavior under test.
// Simulate calling |FlutterRenderer#stopRenderingToSurface| twice with different code paths.
flutterRenderer.stopRenderingToSurface();
flutterRenderer.stopRenderingToSurface();
// Verify behavior under test.
verify(fakeFlutterJNI, times(1)).onSurfaceDestroyed();
}
}