From dbce4f859f573df994e4f5ce10c81a2f0c880324 Mon Sep 17 00:00:00 2001 From: Chavi Weingarten Date: Wed, 5 Apr 2023 14:23:26 +0000 Subject: [PATCH] Check if already on main thread when getting SSG for SCVH SCVH can be added in process so if they are included in a SSG the call to getSurfaceSyncGroup could run on the main thread. If this were the case, the method would never return a value since the call is is blocking and waiting to run on the main thread. Instead, check if the incoming thread is the main thread and return a value immediately instead of a post to main thread. Change-Id: Id233eecc46afb1f5425df97986e7563e970aff12 Bug: 276356641 Test: SurfaceSyncGroupContinuousTest --- core/java/android/view/SurfaceControlViewHost.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/core/java/android/view/SurfaceControlViewHost.java b/core/java/android/view/SurfaceControlViewHost.java index ac50d09cc091c..cd89a561074cb 100644 --- a/core/java/android/view/SurfaceControlViewHost.java +++ b/core/java/android/view/SurfaceControlViewHost.java @@ -99,9 +99,16 @@ public class SurfaceControlViewHost { @Override public ISurfaceSyncGroup getSurfaceSyncGroup() { CompletableFuture surfaceSyncGroup = new CompletableFuture<>(); - mViewRoot.mHandler.post( - () -> surfaceSyncGroup.complete( - mViewRoot.getOrCreateSurfaceSyncGroup().mISurfaceSyncGroup)); + // If the call came from in process and it's already running on the UI thread, return + // results immediately instead of posting to the main thread. If we post to the main + // thread, it will block itself and the return value will always be null. + if (Thread.currentThread() == mViewRoot.mThread) { + return mViewRoot.getOrCreateSurfaceSyncGroup().mISurfaceSyncGroup; + } else { + mViewRoot.mHandler.post( + () -> surfaceSyncGroup.complete( + mViewRoot.getOrCreateSurfaceSyncGroup().mISurfaceSyncGroup)); + } try { return surfaceSyncGroup.get(1, TimeUnit.SECONDS); } catch (InterruptedException | ExecutionException | TimeoutException e) {