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

Commit 18a0ffdb authored by Piotr Wilczyński's avatar Piotr Wilczyński
Browse files

Create setBrightnessFromOffload method

The method will be used by the offload chip to set brightness temporarily before we get a lux reading. It should not affect the short-term model.
- create a new strategy for the brightness from offload
- add isActive property to the session and don't use offload brightness if the session isn't active

Bug: 307894098
Test: atest com.android.server.display
Change-Id: Ifeef8b26a60f0a04db1937f2b03f1a43335be85e
parent 813da462
Loading
Loading
Loading
Loading
+13 −2
Original line number Original line Diff line number Diff line
@@ -721,8 +721,19 @@ public abstract class DisplayManagerInternal {
    public interface DisplayOffloadSession {
    public interface DisplayOffloadSession {
        /** Provide the display state to use in place of state DOZE. */
        /** Provide the display state to use in place of state DOZE. */
        void setDozeStateOverride(int displayState);
        void setDozeStateOverride(int displayState);
        /** Returns the associated DisplayOffloader. */

        DisplayOffloader getDisplayOffloader();
        /** Whether the session is active. */
        boolean isActive();

        /**
         * Update the brightness from the offload chip.
         * @param brightness The brightness value between {@link PowerManager.BRIGHTNESS_MIN} and
         *                   {@link PowerManager.BRIGHTNESS_MAX}, or
         *                   {@link PowerManager.BRIGHTNESS_INVALID_FLOAT} which removes
         *                   the brightness from offload. Other values will be ignored.
         */
        void updateBrightness(float brightness);

        /** Returns whether displayoffload supports the given display state. */
        /** Returns whether displayoffload supports the given display state. */
        static boolean isSupportedOffloadState(int displayState) {
        static boolean isSupportedOffloadState(int displayState) {
            return Display.isSuspendedState(displayState);
            return Display.isSuspendedState(displayState);
+37 −4
Original line number Original line Diff line number Diff line
@@ -38,6 +38,7 @@ public final class DisplayBrightnessState {
    private final boolean mShouldUseAutoBrightness;
    private final boolean mShouldUseAutoBrightness;


    private final boolean mIsSlowChange;
    private final boolean mIsSlowChange;
    private final boolean mShouldUpdateScreenBrightnessSetting;


    private final float mCustomAnimationRate;
    private final float mCustomAnimationRate;


@@ -50,6 +51,7 @@ public final class DisplayBrightnessState {
        mIsSlowChange = builder.isSlowChange();
        mIsSlowChange = builder.isSlowChange();
        mMaxBrightness = builder.getMaxBrightness();
        mMaxBrightness = builder.getMaxBrightness();
        mCustomAnimationRate = builder.getCustomAnimationRate();
        mCustomAnimationRate = builder.getCustomAnimationRate();
        mShouldUpdateScreenBrightnessSetting = builder.shouldUpdateScreenBrightnessSetting();
    }
    }


    /**
    /**
@@ -109,6 +111,13 @@ public final class DisplayBrightnessState {
        return mCustomAnimationRate;
        return mCustomAnimationRate;
    }
    }


    /**
     * @return {@code true} if the screen brightness setting should be updated
     */
    public boolean shouldUpdateScreenBrightnessSetting() {
        return mShouldUpdateScreenBrightnessSetting;
    }

    @Override
    @Override
    public String toString() {
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder("DisplayBrightnessState:");
        StringBuilder stringBuilder = new StringBuilder("DisplayBrightnessState:");
@@ -123,6 +132,8 @@ public final class DisplayBrightnessState {
        stringBuilder.append("\n    isSlowChange:").append(mIsSlowChange);
        stringBuilder.append("\n    isSlowChange:").append(mIsSlowChange);
        stringBuilder.append("\n    maxBrightness:").append(mMaxBrightness);
        stringBuilder.append("\n    maxBrightness:").append(mMaxBrightness);
        stringBuilder.append("\n    customAnimationRate:").append(mCustomAnimationRate);
        stringBuilder.append("\n    customAnimationRate:").append(mCustomAnimationRate);
        stringBuilder.append("\n    shouldUpdateScreenBrightnessSetting:")
                .append(mShouldUpdateScreenBrightnessSetting);
        return stringBuilder.toString();
        return stringBuilder.toString();
    }
    }


@@ -149,13 +160,16 @@ public final class DisplayBrightnessState {
                && mShouldUseAutoBrightness == otherState.getShouldUseAutoBrightness()
                && mShouldUseAutoBrightness == otherState.getShouldUseAutoBrightness()
                && mIsSlowChange == otherState.isSlowChange()
                && mIsSlowChange == otherState.isSlowChange()
                && mMaxBrightness == otherState.getMaxBrightness()
                && mMaxBrightness == otherState.getMaxBrightness()
                && mCustomAnimationRate == otherState.getCustomAnimationRate();
                && mCustomAnimationRate == otherState.getCustomAnimationRate()
                && mShouldUpdateScreenBrightnessSetting
                    == otherState.shouldUpdateScreenBrightnessSetting();
    }
    }


    @Override
    @Override
    public int hashCode() {
    public int hashCode() {
        return Objects.hash(mBrightness, mSdrBrightness, mBrightnessReason,
        return Objects.hash(mBrightness, mSdrBrightness, mBrightnessReason,
                mShouldUseAutoBrightness, mIsSlowChange, mMaxBrightness, mCustomAnimationRate);
                mShouldUseAutoBrightness, mIsSlowChange, mMaxBrightness, mCustomAnimationRate,
                mShouldUpdateScreenBrightnessSetting);
    }
    }


    /**
    /**
@@ -177,6 +191,7 @@ public final class DisplayBrightnessState {
        private boolean mIsSlowChange;
        private boolean mIsSlowChange;
        private float mMaxBrightness;
        private float mMaxBrightness;
        private float mCustomAnimationRate = CUSTOM_ANIMATION_RATE_NOT_SET;
        private float mCustomAnimationRate = CUSTOM_ANIMATION_RATE_NOT_SET;
        private boolean mShouldUpdateScreenBrightnessSetting;


        /**
        /**
         * Create a builder starting with the values from the specified {@link
         * Create a builder starting with the values from the specified {@link
@@ -194,6 +209,8 @@ public final class DisplayBrightnessState {
            builder.setIsSlowChange(state.isSlowChange());
            builder.setIsSlowChange(state.isSlowChange());
            builder.setMaxBrightness(state.getMaxBrightness());
            builder.setMaxBrightness(state.getMaxBrightness());
            builder.setCustomAnimationRate(state.getCustomAnimationRate());
            builder.setCustomAnimationRate(state.getCustomAnimationRate());
            builder.setShouldUpdateScreenBrightnessSetting(
                    state.shouldUpdateScreenBrightnessSetting());
            return builder;
            return builder;
        }
        }


@@ -290,8 +307,8 @@ public final class DisplayBrightnessState {
        /**
        /**
         * See {@link DisplayBrightnessState#isSlowChange()}.
         * See {@link DisplayBrightnessState#isSlowChange()}.
         */
         */
        public Builder setIsSlowChange(boolean shouldUseAutoBrightness) {
        public Builder setIsSlowChange(boolean isSlowChange) {
            this.mIsSlowChange = shouldUseAutoBrightness;
            this.mIsSlowChange = isSlowChange;
            return this;
            return this;
        }
        }


@@ -333,6 +350,22 @@ public final class DisplayBrightnessState {
            return mCustomAnimationRate;
            return mCustomAnimationRate;
        }
        }


        /**
         * See {@link DisplayBrightnessState#shouldUpdateScreenBrightnessSetting()}.
         */
        public boolean shouldUpdateScreenBrightnessSetting() {
            return mShouldUpdateScreenBrightnessSetting;
        }

        /**
         * See {@link DisplayBrightnessState#shouldUpdateScreenBrightnessSetting()}.
         */
        public Builder setShouldUpdateScreenBrightnessSetting(
                boolean shouldUpdateScreenBrightnessSetting) {
            mShouldUpdateScreenBrightnessSetting = shouldUpdateScreenBrightnessSetting;
            return this;
        }

        /**
        /**
         * This is used to construct an immutable DisplayBrightnessState object from its builder
         * This is used to construct an immutable DisplayBrightnessState object from its builder
         */
         */
+1 −1
Original line number Original line Diff line number Diff line
@@ -209,7 +209,7 @@ abstract class DisplayDevice {
            int state,
            int state,
            float brightnessState,
            float brightnessState,
            float sdrBrightnessState,
            float sdrBrightnessState,
            @Nullable DisplayOffloadSession displayOffloadSession) {
            @Nullable DisplayOffloadSessionImpl displayOffloadSession) {
        return null;
        return null;
    }
    }


+2 −14
Original line number Original line Diff line number Diff line
@@ -4935,20 +4935,8 @@ public final class DisplayManagerService extends SystemService {
                    return null;
                    return null;
                }
                }


                DisplayOffloadSession session =
                DisplayOffloadSessionImpl session = new DisplayOffloadSessionImpl(displayOffloader,
                        new DisplayOffloadSession() {
                        displayPowerController);
                            @Override
                            public void setDozeStateOverride(int displayState) {
                                synchronized (mSyncRoot) {
                                    displayPowerController.overrideDozeScreenState(displayState);
                                }
                            }

                            @Override
                            public DisplayOffloader getDisplayOffloader() {
                                return displayOffloader;
                            }
                        };
                logicalDisplay.setDisplayOffloadSessionLocked(session);
                logicalDisplay.setDisplayOffloadSessionLocked(session);
                displayPowerController.setDisplayOffloadSession(session);
                displayPowerController.setDisplayOffloadSession(session);
                return session;
                return session;
+91 −0
Original line number Original line 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.server.display;

import android.annotation.Nullable;
import android.hardware.display.DisplayManagerInternal;
import android.os.PowerManager;
import android.os.Trace;

/**
 * An implementation of the offload session that keeps track of whether the session is active.
 * An offload session is used to control the display's brightness using the offload chip.
 */
public class DisplayOffloadSessionImpl implements DisplayManagerInternal.DisplayOffloadSession {

    @Nullable
    private final DisplayManagerInternal.DisplayOffloader mDisplayOffloader;
    private final DisplayPowerControllerInterface mDisplayPowerController;
    private boolean mIsActive;

    public DisplayOffloadSessionImpl(
            @Nullable DisplayManagerInternal.DisplayOffloader displayOffloader,
            DisplayPowerControllerInterface displayPowerController) {
        mDisplayOffloader = displayOffloader;
        mDisplayPowerController = displayPowerController;
    }

    @Override
    public void setDozeStateOverride(int displayState) {
        mDisplayPowerController.overrideDozeScreenState(displayState);
    }

    @Override
    public boolean isActive() {
        return mIsActive;
    }

    @Override
    public void updateBrightness(float brightness) {
        if (mIsActive) {
            mDisplayPowerController.setBrightnessFromOffload(brightness);
        }
    }

    /**
     * Start the offload session. The method returns if the session is already active.
     * @return Whether the session was started successfully
     */
    public boolean startOffload() {
        if (mDisplayOffloader == null || mIsActive) {
            return false;
        }
        Trace.traceBegin(Trace.TRACE_TAG_POWER, "DisplayOffloader#startOffload");
        try {
            return mIsActive = mDisplayOffloader.startOffload();
        } finally {
            Trace.traceEnd(Trace.TRACE_TAG_POWER);
        }
    }

    /**
     * Stop the offload session. The method returns if the session is not active.
     */
    public void stopOffload() {
        if (mDisplayOffloader == null || !mIsActive) {
            return;
        }
        Trace.traceBegin(Trace.TRACE_TAG_POWER, "DisplayOffloader#stopOffload");
        try {
            mDisplayOffloader.stopOffload();
            mIsActive = false;
            mDisplayPowerController.setBrightnessFromOffload(PowerManager.BRIGHTNESS_INVALID_FLOAT);
        } finally {
            Trace.traceEnd(Trace.TRACE_TAG_POWER);
        }
    }
}
Loading