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

Commit d0884b38 authored by S Ashwin Balaji's avatar S Ashwin Balaji Committed by Android (Google) Code Review
Browse files

Merge changes from topic "display_type_throttling" into main

* changes:
  display: brightnessThermalClamper to support sensor based throttling
  display: brightnessThrottler to support sensor based Throttling
  display: Allow temperature sensor to be specified per display.
parents 0ec7e254 54b9f5fa
Loading
Loading
Loading
Loading
+48 −13
Original line number Diff line number Diff line
@@ -37,9 +37,11 @@ import android.util.Slog;
import com.android.internal.annotations.VisibleForTesting;
import com.android.server.display.DisplayDeviceConfig.ThermalBrightnessThrottlingData;
import com.android.server.display.DisplayDeviceConfig.ThermalBrightnessThrottlingData.ThrottlingLevel;
import com.android.server.display.config.SensorData;
import com.android.server.display.feature.DeviceConfigParameterProvider;
import com.android.server.display.utils.DebugUtils;
import com.android.server.display.utils.DeviceConfigParsingUtils;
import com.android.server.display.utils.SensorUtils;

import java.io.PrintWriter;
import java.util.HashMap;
@@ -79,7 +81,7 @@ class BrightnessThrottler {

    // Maps the throttling ID to the data. Sourced from DisplayDeviceConfig.
    @NonNull
    private HashMap<String, ThermalBrightnessThrottlingData> mDdcThermalThrottlingDataMap;
    private Map<String, ThermalBrightnessThrottlingData> mDdcThermalThrottlingDataMap;

    // Current throttling data being used.
    // Null if we do not support throttling.
@@ -97,6 +99,10 @@ class BrightnessThrottler {
    // The brightness throttling configuration that should be used.
    private String mThermalBrightnessThrottlingDataId;

    // Temperature Sensor to be monitored for throttling.
    @NonNull
    private SensorData mTempSensor;

    // This is a collection of brightness throttling data that has been written as overrides from
    // the DeviceConfig. This will always take priority over the display device config data.
    // We need to store the data for every display device, so we do not need to update this each
@@ -121,17 +127,19 @@ class BrightnessThrottler {

    BrightnessThrottler(Handler handler, Runnable throttlingChangeCallback, String uniqueDisplayId,
            String throttlingDataId,
            @NonNull HashMap<String, ThermalBrightnessThrottlingData>
                    thermalBrightnessThrottlingDataMap) {
        this(new Injector(), handler, handler, throttlingChangeCallback,
                uniqueDisplayId, throttlingDataId, thermalBrightnessThrottlingDataMap);
            @NonNull DisplayDeviceConfig displayDeviceConfig) {
        this(new Injector(), handler, handler, throttlingChangeCallback, uniqueDisplayId,
                throttlingDataId,
                displayDeviceConfig.getThermalBrightnessThrottlingDataMapByThrottlingId(),
                displayDeviceConfig.getTempSensor());
    }

    @VisibleForTesting
    BrightnessThrottler(Injector injector, Handler handler, Handler deviceConfigHandler,
            Runnable throttlingChangeCallback, String uniqueDisplayId, String throttlingDataId,
            @NonNull HashMap<String, ThermalBrightnessThrottlingData>
                    thermalBrightnessThrottlingDataMap) {
            @NonNull Map<String, ThermalBrightnessThrottlingData>
                    thermalBrightnessThrottlingDataMap,
            @NonNull SensorData tempSensor) {
        mInjector = injector;

        mHandler = handler;
@@ -147,7 +155,7 @@ class BrightnessThrottler {
        mDdcThermalThrottlingDataMap = thermalBrightnessThrottlingDataMap;
        loadThermalBrightnessThrottlingDataFromDeviceConfig();
        loadThermalBrightnessThrottlingDataFromDisplayDeviceConfig(mDdcThermalThrottlingDataMap,
                mThermalBrightnessThrottlingDataId, mUniqueDisplayId);
                tempSensor, mThermalBrightnessThrottlingDataId, mUniqueDisplayId);
    }

    boolean deviceSupportsThrottling() {
@@ -180,12 +188,14 @@ class BrightnessThrottler {
    }

    void loadThermalBrightnessThrottlingDataFromDisplayDeviceConfig(
            HashMap<String, ThermalBrightnessThrottlingData> ddcThrottlingDataMap,
            Map<String, ThermalBrightnessThrottlingData> ddcThrottlingDataMap,
            SensorData tempSensor,
            String brightnessThrottlingDataId,
            String uniqueDisplayId) {
        mDdcThermalThrottlingDataMap = ddcThrottlingDataMap;
        mThermalBrightnessThrottlingDataId = brightnessThrottlingDataId;
        mUniqueDisplayId = uniqueDisplayId;
        mTempSensor = tempSensor;
        resetThermalThrottlingData();
    }

@@ -310,7 +320,7 @@ class BrightnessThrottler {
        }

        if (deviceSupportsThrottling()) {
            mSkinThermalStatusObserver.startObserving();
            mSkinThermalStatusObserver.startObserving(mTempSensor);
        }
    }

@@ -357,6 +367,7 @@ class BrightnessThrottler {
    private final class SkinThermalStatusObserver extends IThermalEventListener.Stub {
        private final Injector mInjector;
        private final Handler mHandler;
        private SensorData mObserverTempSensor;

        private IThermalService mThermalService;
        private boolean mStarted;
@@ -371,28 +382,51 @@ class BrightnessThrottler {
            if (DEBUG) {
                Slog.d(TAG, "New thermal throttling status = " + temp.getStatus());
            }

            if (mObserverTempSensor.name != null
                    && !mObserverTempSensor.name.equals(temp.getName())) {
                Slog.i(TAG, "Skipping thermal throttling notification as monitored sensor: "
                            + mObserverTempSensor.name
                            + " != notified sensor: "
                            + temp.getName());
                return;
            }
            mHandler.post(() -> {
                final @Temperature.ThrottlingStatus int status = temp.getStatus();
                thermalStatusChanged(status);
            });
        }

        void startObserving() {
            if (mStarted) {
        void startObserving(SensorData tempSensor) {
            if (!mStarted || mObserverTempSensor == null) {
                mObserverTempSensor = tempSensor;
                registerThermalListener();
                return;
            }

            String curType = mObserverTempSensor.type;
            mObserverTempSensor = tempSensor;
            if (curType.equals(tempSensor.type)) {
                if (DEBUG) {
                    Slog.d(TAG, "Thermal status observer already started");
                }
                return;
            }
            stopObserving();
            registerThermalListener();
        }

        void registerThermalListener() {
            mThermalService = mInjector.getThermalService();
            if (mThermalService == null) {
                Slog.e(TAG, "Could not observe thermal status. Service not available");
                return;
            }
            int temperatureType = SensorUtils.getSensorTemperatureType(mObserverTempSensor);
            try {
                // We get a callback immediately upon registering so there's no need to query
                // for the current value.
                mThermalService.registerThermalEventListenerWithType(this, Temperature.TYPE_SKIN);
                mThermalService.registerThermalEventListenerWithType(this, temperatureType);
                mStarted = true;
            } catch (RemoteException e) {
                Slog.e(TAG, "Failed to register thermal status listener", e);
@@ -418,6 +452,7 @@ class BrightnessThrottler {
        void dump(PrintWriter writer) {
            writer.println("  SkinThermalStatusObserver:");
            writer.println("    mStarted: " + mStarted);
            writer.println("    mObserverTempSensor: " + mObserverTempSensor);
            if (mThermalService != null) {
                writer.println("    ThermalService available");
            } else {
+25 −4
Original line number Diff line number Diff line
@@ -384,6 +384,10 @@ import javax.xml.datatype.DatatypeConfigurationException;
 *             </point>
 *          </supportedModes>
 *      </proxSensor>
 *      <tempSensor>
 *        <type>DISPLAY</type>
 *        <name>VIRTUAL-SKIN-DISPLAY</name>
 *      </tempSensor>
 *
 *      <ambientLightHorizonLong>10001</ambientLightHorizonLong>
 *      <ambientLightHorizonShort>2001</ambientLightHorizonShort>
@@ -625,6 +629,12 @@ public class DisplayDeviceConfig {
    @Nullable
    private SensorData mProximitySensor;

    // The details of the temperature sensor associated with this display.
    // Throttling will be based on thermal status of this sensor.
    // For empty values default back to sensor of TYPE_SKIN.
    @NonNull
    private SensorData mTempSensor;

    private final List<RefreshRateLimitation> mRefreshRateLimitations =
            new ArrayList<>(2 /*initialCapacity*/);

@@ -821,10 +831,10 @@ public class DisplayDeviceConfig {
    private String mLowBlockingZoneThermalMapId = null;
    private String mHighBlockingZoneThermalMapId = null;

    private final HashMap<String, ThermalBrightnessThrottlingData>
    private final Map<String, ThermalBrightnessThrottlingData>
            mThermalBrightnessThrottlingDataMapByThrottlingId = new HashMap<>();

    private final HashMap<String, PowerThrottlingData>
    private final Map<String, PowerThrottlingData>
            mPowerThrottlingDataMapByThrottlingId = new HashMap<>();

    private final Map<String, SparseArray<SurfaceControl.RefreshRateRange>>
@@ -1489,6 +1499,13 @@ public class DisplayDeviceConfig {
        return mProximitySensor;
    }

    /**
     * @return temperature sensor data associated with the display.
     */
    public SensorData getTempSensor() {
        return mTempSensor;
    }

    boolean isAutoBrightnessAvailable() {
        return mAutoBrightnessAvailable;
    }
@@ -1539,7 +1556,7 @@ public class DisplayDeviceConfig {
    /**
     * @return brightness throttling configuration data for this display, for each throttling id.
     */
    public HashMap<String, ThermalBrightnessThrottlingData>
    public Map<String, ThermalBrightnessThrottlingData>
            getThermalBrightnessThrottlingDataMapByThrottlingId() {
        return mThermalBrightnessThrottlingDataMapByThrottlingId;
    }
@@ -1558,7 +1575,7 @@ public class DisplayDeviceConfig {
    /**
     * @return power throttling configuration data for this display, for each throttling id.
     **/
    public HashMap<String, PowerThrottlingData>
    public Map<String, PowerThrottlingData>
            getPowerThrottlingDataMapByThrottlingId() {
        return mPowerThrottlingDataMapByThrottlingId;
    }
@@ -1871,6 +1888,7 @@ public class DisplayDeviceConfig {
                + "mAmbientLightSensor=" + mAmbientLightSensor
                + ", mScreenOffBrightnessSensor=" + mScreenOffBrightnessSensor
                + ", mProximitySensor=" + mProximitySensor
                + ", mTempSensor=" + mTempSensor
                + ", mRefreshRateLimitations= " + Arrays.toString(mRefreshRateLimitations.toArray())
                + ", mDensityMapping= " + mDensityMapping
                + ", mAutoBrightnessBrighteningLightDebounce= "
@@ -1972,6 +1990,7 @@ public class DisplayDeviceConfig {
                        mContext.getResources());
                mScreenOffBrightnessSensor = SensorData.loadScreenOffBrightnessSensorConfig(config);
                mProximitySensor = SensorData.loadProxSensorConfig(config);
                mTempSensor = SensorData.loadTempSensorConfig(mFlags, config);
                loadAmbientHorizonFromDdc(config);
                loadBrightnessChangeThresholds(config);
                loadAutoBrightnessConfigValues(config);
@@ -1999,6 +2018,7 @@ public class DisplayDeviceConfig {
        loadBrightnessRampsFromConfigXml();
        mAmbientLightSensor = SensorData.loadAmbientLightSensorConfig(mContext.getResources());
        mProximitySensor = SensorData.loadSensorUnspecifiedConfig();
        mTempSensor = SensorData.loadTempSensorUnspecifiedConfig();
        loadBrightnessChangeThresholdsFromXml();
        loadAutoBrightnessConfigsFromConfigXml();
        loadAutoBrightnessAvailableFromConfigXml();
@@ -2026,6 +2046,7 @@ public class DisplayDeviceConfig {
        setSimpleMappingStrategyValues();
        mAmbientLightSensor = SensorData.loadAmbientLightSensorConfig(mContext.getResources());
        mProximitySensor = SensorData.loadSensorUnspecifiedConfig();
        mTempSensor = SensorData.loadTempSensorUnspecifiedConfig();
        loadAutoBrightnessAvailableFromConfigXml();
    }

+3 −1
Original line number Diff line number Diff line
@@ -861,6 +861,7 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
                mThermalBrightnessThrottlingDataId = thermalBrightnessThrottlingDataId;
                mBrightnessThrottler.loadThermalBrightnessThrottlingDataFromDisplayDeviceConfig(
                        config.getThermalBrightnessThrottlingDataMapByThrottlingId(),
                        config.getTempSensor(),
                        mThermalBrightnessThrottlingDataId,
                        mUniqueDisplayId);
            }
@@ -923,6 +924,7 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
        mBrightnessRangeController.loadFromConfig(hbmMetadata, token, info, mDisplayDeviceConfig);
        mBrightnessThrottler.loadThermalBrightnessThrottlingDataFromDisplayDeviceConfig(
                mDisplayDeviceConfig.getThermalBrightnessThrottlingDataMapByThrottlingId(),
                mDisplayDeviceConfig.getTempSensor(),
                mThermalBrightnessThrottlingDataId, mUniqueDisplayId);
    }

@@ -1996,7 +1998,7 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
                    postBrightnessChangeRunnable();
                }, mUniqueDisplayId,
                mLogicalDisplay.getDisplayInfoLocked().thermalBrightnessThrottlingDataId,
                ddConfig.getThermalBrightnessThrottlingDataMapByThrottlingId());
                ddConfig);
    }

    private void blockScreenOn() {
+6 −0
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ import com.android.server.display.DisplayDeviceConfig.PowerThrottlingConfigData;
import com.android.server.display.DisplayDeviceConfig.PowerThrottlingData;
import com.android.server.display.DisplayDeviceConfig.ThermalBrightnessThrottlingData;
import com.android.server.display.brightness.BrightnessReason;
import com.android.server.display.config.SensorData;
import com.android.server.display.feature.DeviceConfigParameterProvider;
import com.android.server.display.feature.DisplayManagerFlags;

@@ -336,5 +337,10 @@ public class BrightnessClamperController {
        public float getBrightnessWearBedtimeModeCap() {
            return mDisplayDeviceConfig.getBrightnessCapForWearBedtimeMode();
        }

        @NonNull
        public SensorData getTempSensor() {
            return mDisplayDeviceConfig.getTempSensor();
        }
    }
}
+95 −41
Original line number Diff line number Diff line
@@ -35,8 +35,10 @@ import android.util.Slog;
import com.android.internal.annotations.VisibleForTesting;
import com.android.server.display.DisplayDeviceConfig.ThermalBrightnessThrottlingData;
import com.android.server.display.DisplayDeviceConfig.ThermalBrightnessThrottlingData.ThrottlingLevel;
import com.android.server.display.config.SensorData;
import com.android.server.display.feature.DeviceConfigParameterProvider;
import com.android.server.display.utils.DeviceConfigParsingUtils;
import com.android.server.display.utils.SensorUtils;

import java.io.PrintWriter;
import java.util.List;
@@ -49,9 +51,8 @@ class BrightnessThermalClamper extends
        BrightnessClamper<BrightnessThermalClamper.ThermalData> {

    private static final String TAG = "BrightnessThermalClamper";

    @Nullable
    private final IThermalService mThermalService;
    @NonNull
    private final ThermalStatusObserver mThermalStatusObserver;
    @NonNull
    private final DeviceConfigParameterProvider mConfigParameterProvider;
    // data from DeviceConfig, for all displays, for all dataSets
@@ -66,7 +67,6 @@ class BrightnessThermalClamper extends
    // otherwise mDataFromDeviceConfig
    @Nullable
    private ThermalBrightnessThrottlingData mThermalThrottlingDataActive = null;
    private boolean mStarted = false;
    @Nullable
    private String mUniqueDisplayId = null;
    @Nullable
@@ -74,14 +74,6 @@ class BrightnessThermalClamper extends
    @Temperature.ThrottlingStatus
    private int mThrottlingStatus = Temperature.THROTTLING_NONE;

    private final IThermalEventListener mThermalEventListener = new IThermalEventListener.Stub() {
        @Override
        public void notifyThrottling(Temperature temperature) {
            @Temperature.ThrottlingStatus int status = temperature.getStatus();
            mHandler.post(() -> thermalStatusChanged(status));
        }
    };

    private final BiFunction<String, String, ThrottlingLevel> mDataPointMapper = (key, value) -> {
        try {
            int status = DeviceConfigParsingUtils.parseThermalStatus(key);
@@ -105,12 +97,11 @@ class BrightnessThermalClamper extends
    BrightnessThermalClamper(Injector injector, Handler handler,
            ClamperChangeListener listener, ThermalData thermalData) {
        super(handler, listener);
        mThermalService = injector.getThermalService();
        mConfigParameterProvider = injector.getDeviceConfigParameterProvider();
        mThermalStatusObserver = new ThermalStatusObserver(injector, handler);
        mHandler.post(() -> {
            setDisplayData(thermalData);
            loadOverrideData();
            start();
        });

    }
@@ -139,32 +130,19 @@ class BrightnessThermalClamper extends

    @Override
    void stop() {
        if (!mStarted) {
            return;
        }
        try {
            mThermalService.unregisterThermalEventListener(mThermalEventListener);
        } catch (RemoteException e) {
            Slog.e(TAG, "Failed to unregister thermal status listener", e);
        }
        mStarted = false;
        mThermalStatusObserver.stopObserving();
    }

    @Override
    void dump(PrintWriter writer) {
        writer.println("BrightnessThermalClamper:");
        writer.println("  mStarted: " + mStarted);
        if (mThermalService != null) {
            writer.println("  ThermalService available");
        } else {
            writer.println("  ThermalService not available");
        }
        writer.println("  mThrottlingStatus: " + mThrottlingStatus);
        writer.println("  mUniqueDisplayId: " + mUniqueDisplayId);
        writer.println("  mDataId: " + mDataId);
        writer.println("  mDataOverride: " + mThermalThrottlingDataOverride);
        writer.println("  mDataFromDeviceConfig: " + mThermalThrottlingDataFromDeviceConfig);
        writer.println("  mDataActive: " + mThermalThrottlingDataActive);
        mThermalStatusObserver.dump(writer);
        super.dump(writer);
    }

@@ -193,6 +171,7 @@ class BrightnessThermalClamper extends
            Slog.wtf(TAG,
                    "Thermal throttling data is missing for thermalThrottlingDataId=" + mDataId);
        }
        mThermalStatusObserver.registerSensor(data.getTempSensor());
    }

    private void recalculateBrightnessCap() {
@@ -226,22 +205,94 @@ class BrightnessThermalClamper extends
        }
    }

    private void start() {

    private final class ThermalStatusObserver extends IThermalEventListener.Stub {
        private final Injector mInjector;
        private final Handler mHandler;
        private IThermalService mThermalService;
        private boolean mStarted;
        private SensorData mObserverTempSensor;

        ThermalStatusObserver(Injector injector, Handler handler) {
            mInjector = injector;
            mHandler = handler;
            mStarted = false;
        }

        void registerSensor(SensorData tempSensor) {
            if (!mStarted || mObserverTempSensor == null) {
                mObserverTempSensor = tempSensor;
                registerThermalListener();
                return;
            }

            String curType = mObserverTempSensor.type;
            mObserverTempSensor = tempSensor;
            if (curType.equals(tempSensor.type)) {
                Slog.d(TAG, "Thermal status observer already started");
                return;
            }
            stopObserving();
            registerThermalListener();
        }

        void registerThermalListener() {
            mThermalService = mInjector.getThermalService();
            if (mThermalService == null) {
                Slog.e(TAG, "Could not observe thermal status. Service not available");
                return;
            }
            int temperatureType = SensorUtils.getSensorTemperatureType(mObserverTempSensor);
            try {
                // We get a callback immediately upon registering so there's no need to query
                // for the current value.
            mThermalService.registerThermalEventListenerWithType(mThermalEventListener,
                    Temperature.TYPE_SKIN);
                mThermalService.registerThermalEventListenerWithType(this, temperatureType);
                mStarted = true;
            } catch (RemoteException e) {
                Slog.e(TAG, "Failed to register thermal status listener", e);
            }
        }

        @Override
        public void notifyThrottling(Temperature temp) {
            Slog.d(TAG, "New thermal throttling status = " + temp.getStatus());
            if (mObserverTempSensor.name != null
                    && !mObserverTempSensor.name.equals(temp.getName())) {
                Slog.i(TAG, "Skipping thermal throttling notification as monitored sensor: "
                            + mObserverTempSensor.name
                            + " != notified sensor: "
                            + temp.getName());
                return;
            }
            @Temperature.ThrottlingStatus int status = temp.getStatus();
            mHandler.post(() -> thermalStatusChanged(status));
        }

        void stopObserving() {
            if (!mStarted) {
                return;
            }
            try {
                mThermalService.unregisterThermalEventListener(this);
                mStarted = false;
            } catch (RemoteException e) {
                Slog.e(TAG, "Failed to unregister thermal status listener", e);
            }
            mThermalService = null;
        }

        void dump(PrintWriter writer) {
            writer.println("  ThermalStatusObserver:");
            writer.println("    mStarted: " + mStarted);
            writer.println("    mObserverTempSensor: " + mObserverTempSensor);
            if (mThermalService != null) {
                writer.println("    ThermalService available");
            } else {
                writer.println("    ThermalService not available");
            }
        }
    }

    interface ThermalData {
        @NonNull
        String getUniqueDisplayId();
@@ -251,6 +302,9 @@ class BrightnessThermalClamper extends

        @Nullable
        ThermalBrightnessThrottlingData getThermalBrightnessThrottlingData();

        @NonNull
        SensorData getTempSensor();
    }

    @VisibleForTesting
Loading