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

Commit ec7f080

Browse files
committed
Optimizing performance by avoiding multiple GC operations caused by multiple surface destruction notifications
1 parent 3dbe7db commit ec7f080

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

shell/platform/android/io/flutter/embedding/engine/renderer/FlutterRenderer.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -370,19 +370,20 @@ public void surfaceChanged(int width, int height) {
370370
* android.view.TextureView.SurfaceTextureListener}
371371
*/
372372
public void stopRenderingToSurface() {
373-
flutterJNI.onSurfaceDestroyed();
374-
375-
surface = null;
373+
if (surface != null) {
374+
flutterJNI.onSurfaceDestroyed();
375+
376+
// TODO(mattcarroll): the source of truth for this call should be FlutterJNI, which is where
377+
// the call to onFlutterUiDisplayed() comes from. However, no such native callback exists yet,
378+
// so until the engine and FlutterJNI are configured to call us back when rendering stops,
379+
// we will manually monitor that change here.
380+
if (isDisplayingFlutterUi) {
381+
flutterUiDisplayListener.onFlutterUiNoLongerDisplayed();
382+
}
376383

377-
// TODO(mattcarroll): the source of truth for this call should be FlutterJNI, which is where
378-
// the call to onFlutterUiDisplayed() comes from. However, no such native callback exists yet,
379-
// so until the engine and FlutterJNI are configured to call us back when rendering stops,
380-
// we will manually monitor that change here.
381-
if (isDisplayingFlutterUi) {
382-
flutterUiDisplayListener.onFlutterUiNoLongerDisplayed();
384+
isDisplayingFlutterUi = false;
385+
surface = null;
383386
}
384-
385-
isDisplayingFlutterUi = false;
386387
}
387388

388389
/**

shell/platform/android/test/io/flutter/embedding/engine/renderer/FlutterRendererTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,4 +393,20 @@ public void onTrimMemory(int level) {
393393
// Verify behavior under test.
394394
assertEquals(1, invocationCount.get());
395395
}
396+
397+
@Test
398+
public void itDoesDispatchSurfaceDestructionNotificationOnlyOnce() {
399+
// Setup the test.
400+
FlutterRenderer flutterRenderer = new FlutterRenderer(fakeFlutterJNI);
401+
402+
flutterRenderer.startRenderingToSurface(fakeSurface, /*keepCurrentSurface=*/ false);
403+
404+
// Execute the behavior under test.
405+
// Simulate calling |FlutterRenderer#stopRenderingToSurface| twice with different code paths.
406+
flutterRenderer.stopRenderingToSurface();
407+
flutterRenderer.stopRenderingToSurface();
408+
409+
// Verify behavior under test.
410+
verify(fakeFlutterJNI, times(1)).onSurfaceDestroyed();
411+
}
396412
}

0 commit comments

Comments
 (0)