Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 47c89bc4 authored by Winson Chung's avatar Winson Chung
Browse files

Only use the transaction to update taskview bg if it can be done synchronously

- Otherwise, there can be a race between applying the given transaction,
  and the update to the transaction running on the view thread.  This
  only affects TaskViews that aren't created on the shell main thread.

Bug: 310802730
Test: atest WMShellUnitTests
Change-Id: Iedce1fb827e4892dba194e2fe5da5d6a868696f9
parent a71c1820
Loading
Loading
Loading
Loading
+8 −1
Original line number Diff line number Diff line
@@ -151,7 +151,14 @@ public class TaskView extends SurfaceView implements SurfaceHolder.Callback,

    @Override
    public void setResizeBgColor(SurfaceControl.Transaction t, int bgColor) {
        if (mHandler.getLooper().isCurrentThread()) {
            // We can only use the transaction if it can updated synchronously, otherwise the tx
            // will be applied immediately after but also used/updated on the view thread which
            // will lead to a race and/or crash
            runOnViewThread(() -> setResizeBackgroundColor(t, bgColor));
        } else {
            runOnViewThread(() -> setResizeBackgroundColor(bgColor));
        }
    }

    /**
+37 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.wm.shell;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;


/**
 * Basic test handler that immediately executes anything that is posted on it.
 */
public class TestHandler extends Handler {
    public TestHandler(Looper looper) {
        super(looper);
    }

    @Override
    public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
        dispatchMessage(msg);
        return true;
    }
}
+24 −3
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ import android.app.ActivityManager;
import android.app.ActivityOptions;
import android.app.PendingIntent;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Insets;
import android.graphics.Rect;
import android.graphics.Region;
@@ -58,6 +59,7 @@ import androidx.test.filters.SmallTest;

import com.android.wm.shell.ShellTaskOrganizer;
import com.android.wm.shell.ShellTestCase;
import com.android.wm.shell.TestHandler;
import com.android.wm.shell.common.HandlerExecutor;
import com.android.wm.shell.common.SyncTransactionQueue;
import com.android.wm.shell.common.SyncTransactionQueue.TransactionRunnable;
@@ -92,8 +94,7 @@ public class TaskViewTest extends ShellTestCase {
    Transitions mTransitions;
    @Mock
    Looper mViewLooper;
    @Mock
    Handler mViewHandler;
    TestHandler mViewHandler;

    SurfaceSession mSession;
    SurfaceControl mLeash;
@@ -112,7 +113,7 @@ public class TaskViewTest extends ShellTestCase {

        mContext = getContext();
        doReturn(true).when(mViewLooper).isCurrentThread();
        doReturn(mViewLooper).when(mViewHandler).getLooper();
        mViewHandler = spy(new TestHandler(mViewLooper));

        mTaskInfo = new ActivityManager.RunningTaskInfo();
        mTaskInfo.token = mToken;
@@ -668,4 +669,24 @@ public class TaskViewTest extends ShellTestCase {
        mTaskViewTaskController.onTaskInfoChanged(mTaskInfo);
        verify(mViewHandler).post(any());
    }

    @Test
    public void testSetResizeBgOnSameUiThread_expectUsesTransaction() {
        SurfaceControl.Transaction tx = mock(SurfaceControl.Transaction.class);
        mTaskView = spy(mTaskView);
        mTaskView.setResizeBgColor(tx, Color.BLUE);
        verify(mViewHandler, never()).post(any());
        verify(mTaskView, never()).setResizeBackgroundColor(eq(Color.BLUE));
        verify(mTaskView).setResizeBackgroundColor(eq(tx), eq(Color.BLUE));
    }

    @Test
    public void testSetResizeBgOnDifferentUiThread_expectDoesNotUseTransaction() {
        doReturn(false).when(mViewLooper).isCurrentThread();
        SurfaceControl.Transaction tx = mock(SurfaceControl.Transaction.class);
        mTaskView = spy(mTaskView);
        mTaskView.setResizeBgColor(tx, Color.BLUE);
        verify(mViewHandler).post(any());
        verify(mTaskView).setResizeBackgroundColor(eq(Color.BLUE));
    }
}