From c7746aa3deb9deffcab5a50b80ffec4a08740982 Mon Sep 17 00:00:00 2001 From: Tiger Huang Date: Thu, 24 Jun 2021 13:40:22 +0800 Subject: Release leashes of insets controls if they are not needed Previously, leashes would be only released by GC in some cases, but the timing could be late. This CL proactively release them as long as they are not needed. The cases were: 1. The leashes in DisplayImeController. It didn't release leashes while receiving a new one. 2. The leashes returned from addWindow/relayoutWindow. The leashes would be copied to prevent others from releasing them before writeToParcel. The copied leashes could be redundant after writeToParcel. 3. The leashes held by the client whose window is removed. If a window is removed, the server won't invoke mClient.insetsControlChanged, so the client would never get notified about losing control to release the leashes. This could happen if the window doesn't have an exiting animation. Fix: 175851610 Test: Steps in the bug. Test: Show and dismiss a dialog which doesn't have FLAG_ALT_FOCUSABLE_IM or any window animation. Repeat this thousands of times. And see if there are many insets leashes as offscreen layers in `dumpsys SurfaceFlinger` Change-Id: I5eb774ac071154a8d7205dbd1ab4a5f8eca215c3 --- core/java/android/view/ViewRootImpl.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'core/java/android/view/ViewRootImpl.java') diff --git a/core/java/android/view/ViewRootImpl.java b/core/java/android/view/ViewRootImpl.java index d42e0c367763..a6464e75795c 100644 --- a/core/java/android/view/ViewRootImpl.java +++ b/core/java/android/view/ViewRootImpl.java @@ -5269,7 +5269,16 @@ public final class ViewRootImpl implements ViewParent, // b) When loosing control, controller can restore server state by taking last // dispatched state as truth. mInsetsController.onStateChanged((InsetsState) args.arg1); - mInsetsController.onControlsChanged((InsetsSourceControl[]) args.arg2); + InsetsSourceControl[] controls = (InsetsSourceControl[]) args.arg2; + if (mAdded) { + mInsetsController.onControlsChanged(controls); + } else if (controls != null) { + for (InsetsSourceControl control : controls) { + if (control != null) { + control.release(SurfaceControl::release); + } + } + } args.recycle(); break; } @@ -8107,6 +8116,10 @@ public final class ViewRootImpl implements ViewParent, } } + // If our window is removed, we might not get notified about losing control. + // Invoking this can release the leashes as soon as possible instead of relying on GC. + mInsetsController.onControlsChanged(null); + mAdded = false; } WindowManagerGlobal.getInstance().doRemoveView(this); -- cgit v1.2.3