From c33cbb854ada9aefb412e0336272a23c1e614d8b Mon Sep 17 00:00:00 2001 From: Lucas Dupin Date: Mon, 26 Jul 2021 12:56:29 -0700 Subject: [PATCH] Offload Luma Sampling IPC to background thread It's observed that, in the field, SystemUI is ANRing while trying to register the luma sampling events. We can offload those to our background executor to keep this from happening. Bug: 193922859 Test: manual Change-Id: Ic5e6c5efa33a166f2e3d81eb12756f606365ae9a --- .../navigationbar/NavigationBarView.java | 4 +++- .../gestural/NavigationBarEdgePanel.java | 4 +++- .../gestural/RegionSamplingHelper.java | 19 ++++++++++++++----- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/navigationbar/NavigationBarView.java b/packages/SystemUI/src/com/android/systemui/navigationbar/NavigationBarView.java index 808b7e2216bf4..2d36de34ad047 100644 --- a/packages/SystemUI/src/com/android/systemui/navigationbar/NavigationBarView.java +++ b/packages/SystemUI/src/com/android/systemui/navigationbar/NavigationBarView.java @@ -98,6 +98,7 @@ import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; import java.util.Optional; +import java.util.concurrent.Executor; import java.util.function.Consumer; public class NavigationBarView extends FrameLayout implements @@ -354,6 +355,7 @@ public class NavigationBarView extends FrameLayout implements mEdgeBackGestureHandler = Dependency.get(EdgeBackGestureHandler.Factory.class) .create(mContext); mEdgeBackGestureHandler.setStateChangeCallback(this::updateStates); + Executor backgroundExecutor = Dependency.get(Dependency.BACKGROUND_EXECUTOR); mRegionSamplingHelper = new RegionSamplingHelper(this, new RegionSamplingHelper.SamplingCallback() { @Override @@ -376,7 +378,7 @@ public class NavigationBarView extends FrameLayout implements public boolean isSamplingEnabled() { return isGesturalModeOnDefaultDisplay(getContext(), mNavBarMode); } - }); + }, backgroundExecutor); mNavBarOverlayController = Dependency.get(NavigationBarOverlayController.class); if (mNavBarOverlayController.isNavigationBarOverlayEnabled()) { diff --git a/packages/SystemUI/src/com/android/systemui/navigationbar/gestural/NavigationBarEdgePanel.java b/packages/SystemUI/src/com/android/systemui/navigationbar/gestural/NavigationBarEdgePanel.java index 7fdb79eae2a95..ea04cefb8f702 100644 --- a/packages/SystemUI/src/com/android/systemui/navigationbar/gestural/NavigationBarEdgePanel.java +++ b/packages/SystemUI/src/com/android/systemui/navigationbar/gestural/NavigationBarEdgePanel.java @@ -58,6 +58,7 @@ import com.android.systemui.plugins.NavigationEdgeBackPlugin; import com.android.systemui.statusbar.VibratorHelper; import java.io.PrintWriter; +import java.util.concurrent.Executor; public class NavigationBarEdgePanel extends View implements NavigationEdgeBackPlugin { @@ -349,6 +350,7 @@ public class NavigationBarEdgePanel extends View implements NavigationEdgeBackPl .getDimension(R.dimen.navigation_edge_action_drag_threshold); setVisibility(GONE); + Executor backgroundExecutor = Dependency.get(Dependency.BACKGROUND_EXECUTOR); boolean isPrimaryDisplay = mContext.getDisplayId() == DEFAULT_DISPLAY; mRegionSamplingHelper = new RegionSamplingHelper(this, new RegionSamplingHelper.SamplingCallback() { @@ -366,7 +368,7 @@ public class NavigationBarEdgePanel extends View implements NavigationEdgeBackPl public boolean isSamplingEnabled() { return isPrimaryDisplay; } - }); + }, backgroundExecutor); mRegionSamplingHelper.setWindowVisible(true); mShowProtection = !isPrimaryDisplay; } diff --git a/packages/SystemUI/src/com/android/systemui/navigationbar/gestural/RegionSamplingHelper.java b/packages/SystemUI/src/com/android/systemui/navigationbar/gestural/RegionSamplingHelper.java index 560d89af8e920..c9a939974cc99 100644 --- a/packages/SystemUI/src/com/android/systemui/navigationbar/gestural/RegionSamplingHelper.java +++ b/packages/SystemUI/src/com/android/systemui/navigationbar/gestural/RegionSamplingHelper.java @@ -30,6 +30,7 @@ import android.view.ViewTreeObserver; import com.android.systemui.R; import java.io.PrintWriter; +import java.util.concurrent.Executor; /** * A helper class to sample regions on the screen and inspect its luminosity. @@ -52,6 +53,7 @@ public class RegionSamplingHelper implements View.OnAttachStateChangeListener, */ private final Rect mRegisteredSamplingBounds = new Rect(); private final SamplingCallback mCallback; + private final Executor mBackgroundExecutor; private boolean mSamplingEnabled = false; private boolean mSamplingListenerRegistered = false; @@ -82,7 +84,9 @@ public class RegionSamplingHelper implements View.OnAttachStateChangeListener, } }; - public RegionSamplingHelper(View sampledView, SamplingCallback samplingCallback) { + public RegionSamplingHelper(View sampledView, SamplingCallback samplingCallback, + Executor backgroundExecutor) { + mBackgroundExecutor = backgroundExecutor; mSamplingListener = new CompositionSamplingListener( sampledView.getContext().getMainExecutor()) { @Override @@ -183,10 +187,13 @@ public class RegionSamplingHelper implements View.OnAttachStateChangeListener, // We only want to reregister if something actually changed unregisterSamplingListener(); mSamplingListenerRegistered = true; - CompositionSamplingListener.register(mSamplingListener, DEFAULT_DISPLAY, - stopLayerControl, mSamplingRequestBounds); + SurfaceControl registeredStopLayer = stopLayerControl; + mBackgroundExecutor.execute(() -> { + CompositionSamplingListener.register(mSamplingListener, DEFAULT_DISPLAY, + registeredStopLayer, mSamplingRequestBounds); + }); mRegisteredSamplingBounds.set(mSamplingRequestBounds); - mRegisteredStopLayer = stopLayerControl; + mRegisteredStopLayer = registeredStopLayer; } mFirstSamplingAfterStart = false; } else { @@ -199,7 +206,9 @@ public class RegionSamplingHelper implements View.OnAttachStateChangeListener, mSamplingListenerRegistered = false; mRegisteredStopLayer = null; mRegisteredSamplingBounds.setEmpty(); - CompositionSamplingListener.unregister(mSamplingListener); + mBackgroundExecutor.execute(() -> { + CompositionSamplingListener.unregister(mSamplingListener); + }); } }