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

Commit e1a40a40 authored by Rupesh Bansal's avatar Rupesh Bansal
Browse files

Added temporary screen brightness strategy

Bug: 260061783
Test: atest com.android.server.display
Change-Id: I9386ffce6cbecf13abaf14b25810f6f415c963cc
parent 071357b6
Loading
Loading
Loading
Loading
+8 −23
Original line number Diff line number Diff line
@@ -299,7 +299,6 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
    private boolean mAppliedAutoBrightness;
    private boolean mAppliedDimming;
    private boolean mAppliedLowPower;
    private boolean mAppliedTemporaryBrightness;
    private boolean mAppliedTemporaryAutoBrightnessAdjustment;
    private boolean mAppliedBrightnessBoost;
    private boolean mAppliedThrottling;
@@ -395,11 +394,6 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
    // behalf of the user.
    private float mCurrentScreenBrightnessSetting;

    // The temporary screen brightness. Typically set when a user is interacting with the
    // brightness slider but hasn't settled on a choice yet. Set to
    // PowerManager.BRIGHTNESS_INVALID_FLOAT when there's no temporary brightness set.
    private float mTemporaryScreenBrightness;

    // The current screen brightness while in VR mode.
    private float mScreenBrightnessForVr;

@@ -566,7 +560,6 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
        mCurrentScreenBrightnessSetting = getScreenBrightnessSetting();
        mScreenBrightnessForVr = getScreenBrightnessForVrSetting();
        mAutoBrightnessAdjustment = getAutoBrightnessAdjustmentSetting();
        mTemporaryScreenBrightness = PowerManager.BRIGHTNESS_INVALID_FLOAT;
        mPendingScreenBrightnessSetting = PowerManager.BRIGHTNESS_INVALID_FLOAT;
        mTemporaryAutoBrightnessAdjustment = PowerManager.BRIGHTNESS_INVALID_FLOAT;
        mPendingAutoBrightnessAdjustment = PowerManager.BRIGHTNESS_INVALID_FLOAT;
@@ -1218,16 +1211,6 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal

        final boolean userSetBrightnessChanged = updateUserSetScreenBrightness();

        // Use the temporary screen brightness if there isn't an override, either from
        // WindowManager or based on the display state.
        if (isValidBrightnessValue(mTemporaryScreenBrightness)) {
            brightnessState = mTemporaryScreenBrightness;
            mAppliedTemporaryBrightness = true;
            mBrightnessReasonTemp.setReason(BrightnessReason.REASON_TEMPORARY);
        } else {
            mAppliedTemporaryBrightness = false;
        }

        final boolean autoBrightnessAdjustmentChanged = updateAutoBrightnessAdjustment();

        // Use the autobrightness adjustment override if set.
@@ -1414,7 +1397,8 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
        // Skip the animation when the screen is off or suspended or transition to/from VR.
        boolean brightnessAdjusted = false;
        final boolean brightnessIsTemporary =
                mAppliedTemporaryBrightness || mAppliedTemporaryAutoBrightnessAdjustment;
                (mBrightnessReason.getReason() == BrightnessReason.REASON_TEMPORARY)
                        || mAppliedTemporaryAutoBrightnessAdjustment;
        if (!mPendingScreenOff) {
            if (mSkipScreenOnBrightnessRamp) {
                if (state == Display.STATE_ON) {
@@ -2202,13 +2186,15 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
        }
        if (mCurrentScreenBrightnessSetting == mPendingScreenBrightnessSetting) {
            mPendingScreenBrightnessSetting = PowerManager.BRIGHTNESS_INVALID_FLOAT;
            mTemporaryScreenBrightness = PowerManager.BRIGHTNESS_INVALID_FLOAT;
            mDisplayBrightnessController
                    .setTemporaryBrightness(PowerManager.BRIGHTNESS_INVALID_FLOAT);
            return false;
        }
        setCurrentScreenBrightness(mPendingScreenBrightnessSetting);
        mLastUserSetScreenBrightness = mPendingScreenBrightnessSetting;
        mPendingScreenBrightnessSetting = PowerManager.BRIGHTNESS_INVALID_FLOAT;
        mTemporaryScreenBrightness = PowerManager.BRIGHTNESS_INVALID_FLOAT;
        mDisplayBrightnessController
                .setTemporaryBrightness(PowerManager.BRIGHTNESS_INVALID_FLOAT);
        return true;
    }

@@ -2291,7 +2277,6 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
        pw.println("  mLastUserSetScreenBrightness=" + mLastUserSetScreenBrightness);
        pw.println("  mPendingScreenBrightnessSetting="
                + mPendingScreenBrightnessSetting);
        pw.println("  mTemporaryScreenBrightness=" + mTemporaryScreenBrightness);
        pw.println("  mAutoBrightnessAdjustment=" + mAutoBrightnessAdjustment);
        pw.println("  mBrightnessReason=" + mBrightnessReason);
        pw.println("  mTemporaryAutoBrightnessAdjustment=" + mTemporaryAutoBrightnessAdjustment);
@@ -2301,7 +2286,6 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal
        pw.println("  mAppliedDimming=" + mAppliedDimming);
        pw.println("  mAppliedLowPower=" + mAppliedLowPower);
        pw.println("  mAppliedThrottling=" + mAppliedThrottling);
        pw.println("  mAppliedTemporaryBrightness=" + mAppliedTemporaryBrightness);
        pw.println("  mAppliedTemporaryAutoBrightnessAdjustment="
                + mAppliedTemporaryAutoBrightnessAdjustment);
        pw.println("  mAppliedBrightnessBoost=" + mAppliedBrightnessBoost);
@@ -2541,7 +2525,8 @@ final class DisplayPowerController2 implements AutomaticBrightnessController.Cal

                case MSG_SET_TEMPORARY_BRIGHTNESS:
                    // TODO: Should we have a a timeout for the temporary brightness?
                    mTemporaryScreenBrightness = Float.intBitsToFloat(msg.arg1);
                    mDisplayBrightnessController
                            .setTemporaryBrightness(Float.intBitsToFloat(msg.arg1));
                    updatePowerState();
                    break;

+1 −1
Original line number Diff line number Diff line
@@ -28,7 +28,7 @@ public final class BrightnessUtils {
     * Checks whether the brightness is within the valid brightness range, not including off.
     */
    public static boolean isValidBrightnessValue(float brightness) {
        return brightness >= PowerManager.BRIGHTNESS_MIN
        return !Float.isNaN(brightness) && brightness >= PowerManager.BRIGHTNESS_MIN
                && brightness <= PowerManager.BRIGHTNESS_MAX;
    }

+15 −0
Original line number Diff line number Diff line
@@ -67,6 +67,21 @@ public final class DisplayBrightnessController {
        return mDisplayBrightnessStrategy.updateBrightness(displayPowerRequest);
    }

    /**
     * Sets the temporary brightness
     */
    public void setTemporaryBrightness(Float temporaryBrightness) {
        mDisplayBrightnessStrategySelector.getTemporaryDisplayBrightnessStrategy()
                .setTemporaryScreenBrightness(temporaryBrightness);
    }

    /**
     * Returns the current selected DisplayBrightnessStrategy
     */
    public DisplayBrightnessStrategy getCurrentDisplayBrightnessStrategy() {
        return mDisplayBrightnessStrategy;
    }

    /**
     * Returns a boolean flag indicating if the light sensor is to be used to decide the screen
     * brightness when dozing
+19 −1
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.server.display.brightness;
import android.annotation.NonNull;
import android.content.Context;
import android.hardware.display.DisplayManagerInternal;
import android.util.IndentingPrintWriter;
import android.util.Slog;
import android.view.Display;

@@ -29,6 +30,7 @@ import com.android.server.display.brightness.strategy.DozeBrightnessStrategy;
import com.android.server.display.brightness.strategy.InvalidBrightnessStrategy;
import com.android.server.display.brightness.strategy.OverrideBrightnessStrategy;
import com.android.server.display.brightness.strategy.ScreenOffBrightnessStrategy;
import com.android.server.display.brightness.strategy.TemporaryBrightnessStrategy;

import java.io.PrintWriter;

@@ -48,7 +50,9 @@ public class DisplayBrightnessStrategySelector {
    // The brightness strategy used to manage the brightness state when the request state is
    // invalid.
    private final OverrideBrightnessStrategy mOverrideBrightnessStrategy;
    // The brightness strategy used to manage the brightness state request is invalid.
    // The brightness strategy used to manage the brightness state in temporary state
    private final TemporaryBrightnessStrategy mTemporaryBrightnessStrategy;
    // The brightness strategy used to manage the brightness state when the request is invalid.
    private final InvalidBrightnessStrategy mInvalidBrightnessStrategy;

    // We take note of the old brightness strategy so that we can know when the strategy changes.
@@ -67,6 +71,7 @@ public class DisplayBrightnessStrategySelector {
        mDozeBrightnessStrategy = injector.getDozeBrightnessStrategy();
        mScreenOffBrightnessStrategy = injector.getScreenOffBrightnessStrategy();
        mOverrideBrightnessStrategy = injector.getOverrideBrightnessStrategy();
        mTemporaryBrightnessStrategy = injector.getTemporaryBrightnessStrategy();
        mInvalidBrightnessStrategy = injector.getInvalidBrightnessStrategy();
        mAllowAutoBrightnessWhileDozingConfig = context.getResources().getBoolean(
                R.bool.config_allowAutoBrightnessWhileDozing);
@@ -89,6 +94,9 @@ public class DisplayBrightnessStrategySelector {
        } else if (BrightnessUtils
                .isValidBrightnessValue(displayPowerRequest.screenBrightnessOverride)) {
            displayBrightnessStrategy = mOverrideBrightnessStrategy;
        } else if (BrightnessUtils.isValidBrightnessValue(
                mTemporaryBrightnessStrategy.getTemporaryScreenBrightness())) {
            displayBrightnessStrategy = mTemporaryBrightnessStrategy;
        }

        if (!mOldBrightnessStrategyName.equals(displayBrightnessStrategy.getName())) {
@@ -101,6 +109,10 @@ public class DisplayBrightnessStrategySelector {
        return displayBrightnessStrategy;
    }

    public TemporaryBrightnessStrategy getTemporaryDisplayBrightnessStrategy() {
        return mTemporaryBrightnessStrategy;
    }

    /**
     * Returns a boolean flag indicating if the light sensor is to be used to decide the screen
     * brightness when dozing
@@ -120,6 +132,8 @@ public class DisplayBrightnessStrategySelector {
        writer.println(
                "  mAllowAutoBrightnessWhileDozingConfig= "
                        + mAllowAutoBrightnessWhileDozingConfig);
        IndentingPrintWriter ipw = new IndentingPrintWriter(writer, " ");
        mTemporaryBrightnessStrategy.dump(ipw);
    }

    /**
@@ -152,6 +166,10 @@ public class DisplayBrightnessStrategySelector {
            return new OverrideBrightnessStrategy();
        }

        TemporaryBrightnessStrategy getTemporaryBrightnessStrategy() {
            return new TemporaryBrightnessStrategy();
        }

        InvalidBrightnessStrategy getInvalidBrightnessStrategy() {
            return new InvalidBrightnessStrategy();
        }
+75 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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.server.display.brightness.strategy;

import android.hardware.display.DisplayManagerInternal;
import android.os.PowerManager;

import com.android.server.display.DisplayBrightnessState;
import com.android.server.display.brightness.BrightnessReason;
import com.android.server.display.brightness.BrightnessUtils;

import java.io.PrintWriter;

/**
 * Manages the brightness of the display when the system brightness is temporary
 */
public class TemporaryBrightnessStrategy implements DisplayBrightnessStrategy {
    // The temporary screen brightness. Typically set when a user is interacting with the
    // brightness slider but hasn't settled on a choice yet. Set to
    // PowerManager.BRIGHTNESS_INVALID_FLOAT when there's no temporary brightness set.
    private float mTemporaryScreenBrightness;

    public TemporaryBrightnessStrategy() {
        mTemporaryScreenBrightness = PowerManager.BRIGHTNESS_INVALID_FLOAT;
    }

    // Use the temporary screen brightness if there isn't an override, either from
    // WindowManager or based on the display state.
    @Override
    public DisplayBrightnessState updateBrightness(
            DisplayManagerInternal.DisplayPowerRequest displayPowerRequest) {
        // Todo(brup): Introduce a validator class and add validations before setting the brightness
        DisplayBrightnessState displayBrightnessState =
                BrightnessUtils.constructDisplayBrightnessState(BrightnessReason.REASON_TEMPORARY,
                        mTemporaryScreenBrightness,
                        mTemporaryScreenBrightness);
        mTemporaryScreenBrightness = Float.NaN;
        return displayBrightnessState;
    }

    @Override
    public String getName() {
        return "TemporaryBrightnessStrategy";
    }

    public float getTemporaryScreenBrightness() {
        return mTemporaryScreenBrightness;
    }

    public void setTemporaryScreenBrightness(float temporaryScreenBrightness) {
        mTemporaryScreenBrightness = temporaryScreenBrightness;
    }

    /**
     * Dumps the state of this class.
     */
    public void dump(PrintWriter writer) {
        writer.println("TemporaryBrightnessStrategy:");
        writer.println("  mTemporaryScreenBrightness:" + mTemporaryScreenBrightness);
    }
}
Loading