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

Commit 900509b7 authored by Wei Wang's avatar Wei Wang Committed by Android (Google) Code Review
Browse files

Merge changes I0d002ea4,Id0fd2491 into tm-d1-dev

* changes:
  Demote top app scheduling group when recents is animating
  Demote top app scheduling group when unlocking
parents 9737d33e 50a757a4
Loading
Loading
Loading
Loading
+12 −4
Original line number Diff line number Diff line
@@ -1556,14 +1556,22 @@ public class OomAdjuster {

        boolean foregroundActivities = false;
        boolean hasVisibleActivities = false;
        if (PROCESS_STATE_CUR_TOP == PROCESS_STATE_TOP && app == topApp) {
        if (app == topApp && (PROCESS_STATE_CUR_TOP == PROCESS_STATE_TOP
                || PROCESS_STATE_CUR_TOP == PROCESS_STATE_IMPORTANT_FOREGROUND)) {
            // The last app on the list is the foreground app.
            adj = ProcessList.FOREGROUND_APP_ADJ;
            if (PROCESS_STATE_CUR_TOP == PROCESS_STATE_TOP) {
                schedGroup = ProcessList.SCHED_GROUP_TOP_APP;
                state.setAdjType("top-activity");
            } else {
                // Demote the scheduling group to avoid CPU contention if there is another more
                // important process which also uses top-app, such as if SystemUI is animating.
                schedGroup = ProcessList.SCHED_GROUP_DEFAULT;
                state.setAdjType("intermediate-top-activity");
            }
            foregroundActivities = true;
            hasVisibleActivities = true;
            procState = PROCESS_STATE_CUR_TOP;
            procState = PROCESS_STATE_TOP;
            if (DEBUG_OOM_ADJ_REASON || logUid == appUid) {
                reportOomAdjMessageLocked(TAG_OOM_ADJ, "Making top: " + app);
            }
+47 −3
Original line number Diff line number Diff line
@@ -64,8 +64,8 @@ import static android.provider.Settings.Global.HIDE_ERROR_DIALOGS;
import static android.provider.Settings.System.FONT_SCALE;
import static android.view.Display.DEFAULT_DISPLAY;
import static android.view.Display.INVALID_DISPLAY;
import static android.view.WindowManager.TRANSIT_FLAG_KEYGUARD_GOING_AWAY_TO_LAUNCHER_CLEAR_SNAPSHOT;
import static android.view.WindowManager.TRANSIT_WAKE;
import static android.view.WindowManagerPolicyConstants.KEYGUARD_GOING_AWAY_FLAG_TO_LAUNCHER_CLEAR_SNAPSHOT;

import static com.android.internal.protolog.ProtoLogGroup.WM_DEBUG_CONFIGURATION;
import static com.android.internal.protolog.ProtoLogGroup.WM_DEBUG_FOCUS;
@@ -400,6 +400,26 @@ public class ActivityTaskManagerService extends IActivityTaskManager.Stub {
    /** The time at which the previous process was last visible. */
    private long mPreviousProcessVisibleTime;

    /** It is set from keyguard-going-away to set-keyguard-shown. */
    static final int DEMOTE_TOP_REASON_DURING_UNLOCKING = 1;
    /** It is set if legacy recents animation is running. */
    static final int DEMOTE_TOP_REASON_ANIMATING_RECENTS = 1 << 1;

    @Retention(RetentionPolicy.SOURCE)
    @IntDef({
            DEMOTE_TOP_REASON_DURING_UNLOCKING,
            DEMOTE_TOP_REASON_ANIMATING_RECENTS,
    })
    @interface DemoteTopReason {}

    /**
     * If non-zero, getTopProcessState() will
     * return {@link ActivityManager#PROCESS_STATE_IMPORTANT_FOREGROUND} to avoid top app from
     * preempting CPU while another process is running an important animation.
     */
    @DemoteTopReason
    volatile int mDemoteTopAppReasons;

    /** List of intents that were used to start the most recent tasks. */
    private RecentTasks mRecentTasks;
    /** State of external calls telling us if the device is awake or asleep. */
@@ -2830,12 +2850,24 @@ public class ActivityTaskManagerService extends IActivityTaskManager.Stub {
                        keyguardShowing);
                mH.sendMessage(msg);
            }
            // Always reset the state regardless of keyguard-showing change, because that means the
            // unlock is either completed or canceled.
            if ((mDemoteTopAppReasons & DEMOTE_TOP_REASON_DURING_UNLOCKING) != 0) {
                mDemoteTopAppReasons &= ~DEMOTE_TOP_REASON_DURING_UNLOCKING;
                // The scheduling group of top process was demoted by unlocking, so recompute
                // to restore its real top priority if possible.
                if (mTopApp != null) {
                    mTopApp.scheduleUpdateOomAdj();
                }
            }
            try {
                Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "setLockScreenShown");
                mRootWindowContainer.forAllDisplays(displayContent -> {
                    mKeyguardController.setKeyguardShown(displayContent.getDisplayId(),
                            keyguardShowing, aodShowing);
                });
            } finally {
                Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
                Binder.restoreCallingIdentity(ident);
            }
        }
@@ -2862,6 +2894,7 @@ public class ActivityTaskManagerService extends IActivityTaskManager.Stub {
        // animation of system UI. Even if AOD is not enabled, it should be no harm.
        final WindowProcessController proc;
        synchronized (mGlobalLockWithoutBoost) {
            mDemoteTopAppReasons &= ~DEMOTE_TOP_REASON_DURING_UNLOCKING;
            final WindowState notificationShade = mRootWindowContainer.getDefaultDisplay()
                    .getDisplayPolicy().getNotificationShade();
            proc = notificationShade != null
@@ -3399,8 +3432,11 @@ public class ActivityTaskManagerService extends IActivityTaskManager.Stub {
        try {
            synchronized (mGlobalLock) {
                // Keyguard asked us to clear the home task snapshot before going away, so do that.
                if ((flags & TRANSIT_FLAG_KEYGUARD_GOING_AWAY_TO_LAUNCHER_CLEAR_SNAPSHOT) != 0) {
                if ((flags & KEYGUARD_GOING_AWAY_FLAG_TO_LAUNCHER_CLEAR_SNAPSHOT) != 0) {
                    mActivityClientController.invalidateHomeTaskSnapshot(null /* token */);
                } else if (mKeyguardShown) {
                    // Only set if it is not unlocking to launcher which may also animate.
                    mDemoteTopAppReasons |= DEMOTE_TOP_REASON_DURING_UNLOCKING;
                }

                mRootWindowContainer.forAllDisplays(displayContent -> {
@@ -3968,6 +4004,9 @@ public class ActivityTaskManagerService extends IActivityTaskManager.Stub {
            mTaskOrganizerController.dump(pw, "  ");
            mVisibleActivityProcessTracker.dump(pw, "  ");
            mActiveUids.dump(pw, "  ");
            if (mDemoteTopAppReasons != 0) {
                pw.println("  mDemoteTopAppReasons=" + mDemoteTopAppReasons);
            }
        }

        if (!printedAnything) {
@@ -5595,12 +5634,17 @@ public class ActivityTaskManagerService extends IActivityTaskManager.Stub {
        @HotPath(caller = HotPath.OOM_ADJUSTMENT)
        @Override
        public int getTopProcessState() {
            final int topState = mTopProcessState;
            if (mDemoteTopAppReasons != 0 && topState == ActivityManager.PROCESS_STATE_TOP) {
                // There may be a more important UI/animation than the top app.
                return ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND;
            }
            if (mRetainPowerModeAndTopProcessState) {
                // There is a launching app while device may be sleeping, force the top state so
                // the launching process can have top-app scheduling group.
                return ActivityManager.PROCESS_STATE_TOP;
            }
            return mTopProcessState;
            return topState;
        }

        @HotPath(caller = HotPath.PROCESS_CHANGE)
+22 −6
Original line number Diff line number Diff line
@@ -203,9 +203,7 @@ class RecentsAnimation implements RecentsAnimationCallbacks, OnRootTaskOrderChan
        final LaunchingState launchingState =
                mTaskSupervisor.getActivityMetricsLogger().notifyActivityLaunching(mTargetIntent);

        if (mCaller != null) {
            mCaller.setRunningRecentsAnimation(true);
        }
        setProcessAnimating(true);

        mService.deferWindowLayout();
        try {
@@ -409,15 +407,33 @@ class RecentsAnimation implements RecentsAnimationCallbacks, OnRootTaskOrderChan
                    if (mWindowManager.mRoot.isLayoutNeeded()) {
                        mWindowManager.mRoot.performSurfacePlacement();
                    }
                    if (mCaller != null) {
                        mCaller.setRunningRecentsAnimation(false);
                    }
                    setProcessAnimating(false);
                    Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
                }
            });
        }
    }

    /** Gives the owner of recents animation higher priority. */
    private void setProcessAnimating(boolean animating) {
        if (mCaller == null) return;
        // Apply the top-app scheduling group to who runs the animation.
        mCaller.setRunningRecentsAnimation(animating);
        int demoteReasons = mService.mDemoteTopAppReasons;
        if (animating) {
            demoteReasons |= ActivityTaskManagerService.DEMOTE_TOP_REASON_ANIMATING_RECENTS;
        } else {
            demoteReasons &= ~ActivityTaskManagerService.DEMOTE_TOP_REASON_ANIMATING_RECENTS;
        }
        mService.mDemoteTopAppReasons = demoteReasons;
        // Make the demotion of the real top app take effect. No need to restore top app state for
        // finishing recents because addToStopping -> scheduleIdle -> activityIdleInternal ->
        // trimApplications will have a full update.
        if (animating && mService.mTopApp != null) {
            mService.mTopApp.scheduleUpdateOomAdj();
        }
    }

    @Override
    public void onAnimationFinished(@RecentsAnimationController.ReorderMode int reorderMode,
            boolean sendUserLeaveHint) {
+7 −0
Original line number Diff line number Diff line
@@ -1126,6 +1126,13 @@ public class WindowProcessController extends ConfigurationContainer<Configuratio
        mAtm.mH.sendMessage(m);
    }

    /** Refreshes oom adjustment and process state of this process. */
    void scheduleUpdateOomAdj() {
        mAtm.mH.sendMessage(PooledLambda.obtainMessage(WindowProcessListener::updateProcessInfo,
                mListener, false /* updateServiceConnectionActivities */,
                false /* activityChange */, true /* updateOomAdj */));
    }

    /** Makes the process have top state before oom-adj is computed from a posted message. */
    void addToPendingTop() {
        mAtm.mAmInternal.addPendingTopUid(mUid, mPid, mThread);
+12 −6
Original line number Diff line number Diff line
@@ -37,13 +37,13 @@ import static com.android.server.wm.WindowContainer.POSITION_TOP;

import static com.google.common.truth.Truth.assertThat;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;

import android.app.IApplicationThread;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
@@ -91,9 +91,15 @@ public class RecentsAnimationTest extends WindowTestsBase {
        TaskDisplayArea taskDisplayArea = mRootWindowContainer.getDefaultTaskDisplayArea();
        Task recentsStack = taskDisplayArea.createRootTask(WINDOWING_MODE_FULLSCREEN,
                ACTIVITY_TYPE_RECENTS, true /* onTop */);
        final WindowProcessController wpc = mSystemServicesTestRule.addProcess(
                mRecentsComponent.getPackageName(), mRecentsComponent.getPackageName(),
                // Use real pid/uid of the test so the corresponding process can be mapped by
                // Binder.getCallingPid/Uid.
                android.os.Process.myPid(), android.os.Process.myUid());
        ActivityRecord recentActivity = new ActivityBuilder(mAtm)
                .setComponent(mRecentsComponent)
                .setTask(recentsStack)
                .setUseProcess(wpc)
                .build();
        ActivityRecord topActivity = new ActivityBuilder(mAtm).setCreateTask(true).build();
        topActivity.getRootTask().moveToFront("testRecentsActivityVisiblility");
@@ -106,11 +112,14 @@ public class RecentsAnimationTest extends WindowTestsBase {
                mRecentsComponent, true /* getRecentsAnimation */);
        // The launch-behind state should make the recents activity visible.
        assertTrue(recentActivity.mVisibleRequested);
        assertEquals(ActivityTaskManagerService.DEMOTE_TOP_REASON_ANIMATING_RECENTS,
                mAtm.mDemoteTopAppReasons);

        // Simulate the animation is cancelled without changing the stack order.
        recentsAnimation.onAnimationFinished(REORDER_KEEP_IN_PLACE, false /* sendUserLeaveHint */);
        // The non-top recents activity should be invisible by the restored launch-behind state.
        assertFalse(recentActivity.mVisibleRequested);
        assertEquals(0, mAtm.mDemoteTopAppReasons);
    }

    @Test
@@ -138,11 +147,8 @@ public class RecentsAnimationTest extends WindowTestsBase {
                anyInt() /* startFlags */, any() /* profilerInfo */);

        // Assume its process is alive because the caller should be the recents service.
        WindowProcessController wpc = new WindowProcessController(mAtm, aInfo.applicationInfo,
                aInfo.processName, aInfo.applicationInfo.uid, 0 /* userId */,
                mock(Object.class) /* owner */, mock(WindowProcessListener.class));
        wpc.setThread(mock(IApplicationThread.class));
        doReturn(wpc).when(mAtm).getProcessController(eq(wpc.mName), eq(wpc.mUid));
        mSystemServicesTestRule.addProcess(aInfo.packageName, aInfo.processName, 12345 /* pid */,
                aInfo.applicationInfo.uid);

        Intent recentsIntent = new Intent().setComponent(mRecentsComponent);
        // Null animation indicates to preload.
Loading