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

Commit e0c354e9 authored by Yeabkal Wubshit's avatar Yeabkal Wubshit
Browse files

Match screen state change reasons to wakefulness reasons

This change attempts at matching display state reasons to wakefulness
reasons, whenever a display state change is driven by a wakefulness
change. DisplayPowerRequest now has fields to capture wakefulness change
reasons (if the request is driven by a wakefulness change), and these
reasons are populated by PowerGroup. DisplayStateController then
accounts for these wakefulness change reasons when deciding the display
state and state reasons.

Bug: 364349703
Flag: com.android.server.power.feature.flags.policy_reason_in_display_power_request
Test: atest PowerGroupTest DisplayStateControllerTest
Change-Id: I50c528208d90caba30d74daf5e597fa26b555fe2
parent d5dfcfcc
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -491,10 +491,16 @@ public abstract class DisplayManagerInternal {
        public static final int POLICY_DIM = 2;
        // Policy: Make the screen bright as usual.
        public static final int POLICY_BRIGHT = 3;
        // The maximum policy constant. Useful for iterating through all constants in tests.
        public static final int POLICY_MAX = POLICY_BRIGHT;

        // The basic overall policy to apply: off, doze, dim or bright.
        public int policy;

        // The reason behind the current policy.
        @Display.StateReason
        public int policyReason;

        // If true, the proximity sensor overrides the screen state when an object is
        // nearby, turning it off temporarily until the object is moved away.
        public boolean useProximitySensor;
@@ -541,6 +547,7 @@ public abstract class DisplayManagerInternal {

        public DisplayPowerRequest() {
            policy = POLICY_BRIGHT;
            policyReason = Display.STATE_REASON_DEFAULT_POLICY;
            useProximitySensor = false;
            screenBrightnessOverride = PowerManager.BRIGHTNESS_INVALID_FLOAT;
            screenAutoBrightnessAdjustmentOverride = Float.NaN;
@@ -561,6 +568,7 @@ public abstract class DisplayManagerInternal {

        public void copyFrom(DisplayPowerRequest other) {
            policy = other.policy;
            policyReason = other.policyReason;
            useProximitySensor = other.useProximitySensor;
            screenBrightnessOverride = other.screenBrightnessOverride;
            screenBrightnessOverrideTag = other.screenBrightnessOverrideTag;
+9 −1
Original line number Diff line number Diff line
@@ -517,9 +517,15 @@ public final class PowerManager {
    public static final int GO_TO_SLEEP_REASON_DEVICE_FOLD = 13;

    /**
     * Go to sleep reason code: reason unknown.
     * @hide
     */
    public static final int GO_TO_SLEEP_REASON_MAX =  GO_TO_SLEEP_REASON_DEVICE_FOLD;
    public static final int GO_TO_SLEEP_REASON_UNKNOWN = 14;

    /**
     * @hide
     */
    public static final int GO_TO_SLEEP_REASON_MAX =  GO_TO_SLEEP_REASON_UNKNOWN;

    /**
     * @hide
@@ -540,6 +546,7 @@ public final class PowerManager {
            case GO_TO_SLEEP_REASON_QUIESCENT: return "quiescent";
            case GO_TO_SLEEP_REASON_SLEEP_BUTTON: return "sleep_button";
            case GO_TO_SLEEP_REASON_TIMEOUT: return "timeout";
            case GO_TO_SLEEP_REASON_UNKNOWN: return "unknown";
            default: return Integer.toString(sleepReason);
        }
    }
@@ -635,6 +642,7 @@ public final class PowerManager {
            GO_TO_SLEEP_REASON_QUIESCENT,
            GO_TO_SLEEP_REASON_SLEEP_BUTTON,
            GO_TO_SLEEP_REASON_TIMEOUT,
            GO_TO_SLEEP_REASON_UNKNOWN,
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface GoToSleepReason{}
+1 −1
Original line number Diff line number Diff line
@@ -61,7 +61,7 @@ public class DisplayStateController {
        // We might override this below based on other factors.
        // Initialise brightness as invalid.
        int state;
        int reason = Display.STATE_REASON_DEFAULT_POLICY;
        int reason = displayPowerRequest.policyReason;
        switch (displayPowerRequest.policy) {
            case DisplayManagerInternal.DisplayPowerRequest.POLICY_OFF:
                state = Display.STATE_OFF;
+78 −20
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@ import android.view.Display;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.LatencyTracker;
import com.android.server.power.feature.PowerManagerFlags;

/**
 * Used to store power related requests to every display in a
@@ -62,6 +63,8 @@ public class PowerGroup {
    private final DisplayManagerInternal mDisplayManagerInternal;
    private final boolean mSupportsSandman;
    private final int mGroupId;
    private final PowerManagerFlags mFeatureFlags;

    /** True if DisplayManagerService has applied all the latest display states that were requested
     *  for this group. */
    private boolean mReady;
@@ -82,10 +85,15 @@ public class PowerGroup {
    private long mLastWakeTime;
    /** Timestamp (milliseconds since boot) of the last time the power group was put to sleep. */
    private long mLastSleepTime;
    /** The last reason that woke the power group. */
    private @PowerManager.WakeReason int mLastWakeReason = PowerManager.WAKE_REASON_UNKNOWN;
    /** The last reason that put the power group to sleep. */
    private @PowerManager.GoToSleepReason int mLastSleepReason =
            PowerManager.GO_TO_SLEEP_REASON_UNKNOWN;

    PowerGroup(int groupId, PowerGroupListener wakefulnessListener, Notifier notifier,
            DisplayManagerInternal displayManagerInternal, int wakefulness, boolean ready,
            boolean supportsSandman, long eventTime) {
            boolean supportsSandman, long eventTime, PowerManagerFlags featureFlags) {
        mGroupId = groupId;
        mWakefulnessListener = wakefulnessListener;
        mNotifier = notifier;
@@ -95,10 +103,12 @@ public class PowerGroup {
        mSupportsSandman = supportsSandman;
        mLastWakeTime = eventTime;
        mLastSleepTime = eventTime;
        mFeatureFlags = featureFlags;
    }

    PowerGroup(int wakefulness, PowerGroupListener wakefulnessListener, Notifier notifier,
            DisplayManagerInternal displayManagerInternal, long eventTime) {
            DisplayManagerInternal displayManagerInternal, long eventTime,
            PowerManagerFlags featureFlags) {
        mGroupId = Display.DEFAULT_DISPLAY_GROUP;
        mWakefulnessListener = wakefulnessListener;
        mNotifier = notifier;
@@ -108,6 +118,7 @@ public class PowerGroup {
        mSupportsSandman = true;
        mLastWakeTime = eventTime;
        mLastSleepTime = eventTime;
        mFeatureFlags = featureFlags;
    }

    long getLastWakeTimeLocked() {
@@ -138,8 +149,14 @@ public class PowerGroup {
                setLastPowerOnTimeLocked(eventTime);
                setIsPoweringOnLocked(true);
                mLastWakeTime = eventTime;
                if (mFeatureFlags.isPolicyReasonInDisplayPowerRequestEnabled()) {
                    mLastWakeReason = reason;
                }
            } else if (isInteractive(mWakefulness) && !isInteractive(newWakefulness)) {
                mLastSleepTime = eventTime;
                if (mFeatureFlags.isPolicyReasonInDisplayPowerRequestEnabled()) {
                    mLastSleepReason = reason;
                }
            }
            mWakefulness = newWakefulness;
            mWakefulnessListener.onWakefulnessChangedLocked(mGroupId, mWakefulness, eventTime,
@@ -393,37 +410,51 @@ public class PowerGroup {
        return false;
    }

    @VisibleForTesting
    int getDesiredScreenPolicyLocked(boolean quiescent, boolean dozeAfterScreenOff,
    // TODO: create and use more specific policy reasons, beyond the ones that correlate to
    // interactivity state
    private void updateScreenPolicyLocked(boolean quiescent, boolean dozeAfterScreenOff,
            boolean bootCompleted, boolean screenBrightnessBoostInProgress,
            boolean brightWhenDozing) {
        final int wakefulness = getWakefulnessLocked();
        final int wakeLockSummary = getWakeLockSummaryLocked();
        if (wakefulness == WAKEFULNESS_ASLEEP || quiescent) {
            return DisplayPowerRequest.POLICY_OFF;
        int policyReason = Display.STATE_REASON_DEFAULT_POLICY;
        int policy = Integer.MAX_VALUE; // do not set to real policy to start with.
        if (quiescent) {
            policy = DisplayPowerRequest.POLICY_OFF;
        } else if (wakefulness == WAKEFULNESS_ASLEEP) {
            policy = DisplayPowerRequest.POLICY_OFF;
            policyReason = sleepReasonToDisplayStateReason(mLastSleepReason);
        } else if (wakefulness == WAKEFULNESS_DOZING) {
            if ((wakeLockSummary & WAKE_LOCK_DOZE) != 0) {
                return DisplayPowerRequest.POLICY_DOZE;
            }
            if (dozeAfterScreenOff) {
                return DisplayPowerRequest.POLICY_OFF;
            }
            if (brightWhenDozing) {
                return DisplayPowerRequest.POLICY_BRIGHT;
                policy = DisplayPowerRequest.POLICY_DOZE;
            } else if (dozeAfterScreenOff) {
                policy = DisplayPowerRequest.POLICY_OFF;
            } else if (brightWhenDozing) {
                policy = DisplayPowerRequest.POLICY_BRIGHT;
            }
            // Fall through and preserve the current screen policy if not configured to
            // bright when dozing or doze after screen off.  This causes the screen off transition
            // to be skipped.
        }

        if (policy == Integer.MAX_VALUE) { // policy is not set yet.
            if (isInteractive(wakefulness)) {
                policyReason = wakeReasonToDisplayStateReason(mLastWakeReason);
            }
            if ((wakeLockSummary & WAKE_LOCK_SCREEN_BRIGHT) != 0
                    || !bootCompleted
                    || (getUserActivitySummaryLocked() & USER_ACTIVITY_SCREEN_BRIGHT) != 0
                    || screenBrightnessBoostInProgress) {
            return DisplayPowerRequest.POLICY_BRIGHT;
                policy = DisplayPowerRequest.POLICY_BRIGHT;
            } else {
                policy = DisplayPowerRequest.POLICY_DIM;
            }
        }

        return DisplayPowerRequest.POLICY_DIM;
        if (mFeatureFlags.isPolicyReasonInDisplayPowerRequestEnabled()) {
            mDisplayPowerRequest.policyReason = policyReason;
        }
        mDisplayPowerRequest.policy = policy;
    }

    int getPolicyLocked() {
@@ -439,7 +470,7 @@ public class PowerGroup {
            boolean dozeAfterScreenOff, boolean bootCompleted,
            boolean screenBrightnessBoostInProgress, boolean waitForNegativeProximity,
            boolean brightWhenDozing) {
        mDisplayPowerRequest.policy = getDesiredScreenPolicyLocked(quiescent, dozeAfterScreenOff,
        updateScreenPolicyLocked(quiescent, dozeAfterScreenOff,
                bootCompleted, screenBrightnessBoostInProgress, brightWhenDozing);
        mDisplayPowerRequest.screenBrightnessOverride = screenBrightnessOverride;
        mDisplayPowerRequest.screenBrightnessOverrideTag = overrideTag;
@@ -478,6 +509,33 @@ public class PowerGroup {
        return ready;
    }

    /** Determines the respective display state reason for a given PowerManager WakeReason. */
    private static int wakeReasonToDisplayStateReason(@PowerManager.WakeReason int wakeReason) {
        switch (wakeReason) {
            case PowerManager.WAKE_REASON_POWER_BUTTON:
            case PowerManager.WAKE_REASON_WAKE_KEY:
                return Display.STATE_REASON_KEY;
            case PowerManager.WAKE_REASON_WAKE_MOTION:
                return Display.STATE_REASON_MOTION;
            case PowerManager.WAKE_REASON_TILT:
                return Display.STATE_REASON_TILT;
            default:
                return Display.STATE_REASON_DEFAULT_POLICY;
        }
    }

    /** Determines the respective display state reason for a given PowerManager GoToSleepReason. */
    private static int sleepReasonToDisplayStateReason(
            @PowerManager.GoToSleepReason int sleepReason) {
        switch (sleepReason) {
            case PowerManager.GO_TO_SLEEP_REASON_POWER_BUTTON:
            case PowerManager.GO_TO_SLEEP_REASON_SLEEP_BUTTON:
                return Display.STATE_REASON_KEY;
            default:
                return Display.STATE_REASON_DEFAULT_POLICY;
        }
    }

    protected interface PowerGroupListener {
        /**
         * Informs the recipient about a wakefulness change of a {@link PowerGroup}.
+6 −11
Original line number Diff line number Diff line
@@ -790,7 +790,8 @@ public final class PowerManagerService extends SystemService
                        WAKEFULNESS_AWAKE,
                        /* ready= */ false,
                        supportsSandman,
                        mClock.uptimeMillis());
                        mClock.uptimeMillis(),
                        mFeatureFlags);
                mPowerGroups.append(groupId, powerGroup);
                onPowerGroupEventLocked(DISPLAY_GROUP_ADDED, powerGroup);
            }
@@ -1375,7 +1376,8 @@ public final class PowerManagerService extends SystemService

            mPowerGroups.append(Display.DEFAULT_DISPLAY_GROUP,
                    new PowerGroup(WAKEFULNESS_AWAKE, mPowerGroupWakefulnessChangeListener,
                            mNotifier, mDisplayManagerInternal, mClock.uptimeMillis()));
                            mNotifier, mDisplayManagerInternal, mClock.uptimeMillis(),
                            mFeatureFlags));
            DisplayGroupPowerChangeListener displayGroupPowerChangeListener =
                    new DisplayGroupPowerChangeListener();
            mDisplayManagerInternal.registerDisplayGroupListener(displayGroupPowerChangeListener);
@@ -3737,14 +3739,6 @@ public final class PowerManagerService extends SystemService
        return value >= PowerManager.BRIGHTNESS_MIN && value <= PowerManager.BRIGHTNESS_MAX;
    }

    @VisibleForTesting
    @GuardedBy("mLock")
    int getDesiredScreenPolicyLocked(int groupId) {
        return mPowerGroups.get(groupId).getDesiredScreenPolicyLocked(sQuiescent,
                mDozeAfterScreenOff, mBootCompleted,
                mScreenBrightnessBoostInProgress, mBrightWhenDozingConfig);
    }

    @VisibleForTesting
    int getDreamsBatteryLevelDrain() {
        return mDreamsBatteryLevelDrain;
@@ -4588,7 +4582,8 @@ public final class PowerManagerService extends SystemService
                    WAKEFULNESS_AWAKE,
                    /* ready= */ false,
                    /* supportsSandman= */ false,
                    mClock.uptimeMillis());
                    mClock.uptimeMillis(),
                    mFeatureFlags);
            mPowerGroups.append(displayGroupId, powerGroup);
        }
        mDirty |= DIRTY_DISPLAY_GROUP_WAKEFULNESS;
Loading