Android7.0自动亮度调节分析

/Users/tracyliu/AndroidStudioProjects/EBook3.2-7.1/frameworks/base/services/core/java/com/android/server/power/PowerManagerService.java

PowerManagerService.java

mScreenBrightnessModeSetting 表示亮度调节的模式,分为自动和手动,对应设置中的选项。

1
2
3
mScreenBrightnessModeSetting = Settings.System.getIntForUser(resolver,
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL, UserHandle.USER_CURRENT);
PowerManagerService#updateDisplayPowerStateLocked()

mScreenBrightnessModeSetting在updateDisplayPowerStateLocked()中被使用

该方法用于更新设备显示状态,在这个方法中,会计算出最终需要显示的亮度值和其他值,然后将这些值封装到DisplayPowerRequest对象中,向DisplayMangerService请求Display状态,完成屏幕亮度显示等

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
private boolean updateDisplayPowerStateLocked(int dirty) {
final boolean oldDisplayReady = mDisplayReady;
if ((dirty & (DIRTY_WAKE_LOCKS | DIRTY_USER_ACTIVITY | DIRTY_WAKEFULNESS
| DIRTY_ACTUAL_DISPLAY_POWER_STATE_UPDATED | DIRTY_BOOT_COMPLETED
| DIRTY_SETTINGS | DIRTY_SCREEN_BRIGHTNESS_BOOST | DIRTY_VR_MODE_CHANGED)) != 0) {
mDisplayPowerRequest.policy = getDesiredScreenPolicyLocked();

// Determine appropriate screen brightness and auto-brightness adjustments.
boolean brightnessSetByUser = true;
int screenBrightness = mScreenBrightnessSettingDefault;//默认亮度120
float screenAutoBrightnessAdjustment = 0.0f;
boolean autoBrightness = (mScreenBrightnessModeSetting ==
Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
if (!mBootCompleted) {
// Keep the brightness steady during boot. This requires the
// bootloader brightness and the default brightness to be identical.
autoBrightness = false;
brightnessSetByUser = false;
} else if (mIsVrModeEnabled) { //判断VR模式
screenBrightness = mScreenBrightnessForVrSetting;
autoBrightness = false;//亮度值是否有效
} else if (isValidBrightness(mScreenBrightnessOverrideFromWindowManager)) {
screenBrightness = mScreenBrightnessOverrideFromWindowManager;
autoBrightness = false;
brightnessSetByUser = false;//亮度值是否有效
} else if (isValidBrightness(mTemporaryScreenBrightnessSettingOverride)) {
screenBrightness = mTemporaryScreenBrightnessSettingOverride;
} else if (isValidBrightness(mScreenBrightnessSetting)) {
screenBrightness = mScreenBrightnessSetting;
}
if (autoBrightness) {
screenBrightness = mScreenBrightnessSettingDefault;
if (isValidAutoBrightnessAdjustment(
mTemporaryScreenAutoBrightnessAdjustmentSettingOverride)) {
screenAutoBrightnessAdjustment =
mTemporaryScreenAutoBrightnessAdjustmentSettingOverride;
} else if (isValidAutoBrightnessAdjustment(
mScreenAutoBrightnessAdjustmentSetting)) {
screenAutoBrightnessAdjustment = mScreenAutoBrightnessAdjustmentSetting;
}
}
screenBrightness = Math.max(Math.min(screenBrightness,
mScreenBrightnessSettingMaximum), mScreenBrightnessSettingMinimum);//亮度
screenAutoBrightnessAdjustment = Math.max(Math.min(
screenAutoBrightnessAdjustment, 1.0f), -1.0f);

// Update display power request.//更新DisplayPowerRequest
mDisplayPowerRequest.screenBrightness = screenBrightness;
mDisplayPowerRequest.screenAutoBrightnessAdjustment =
screenAutoBrightnessAdjustment;
mDisplayPowerRequest.brightnessSetByUser = brightnessSetByUser;
mDisplayPowerRequest.useAutoBrightness = autoBrightness;
mDisplayPowerRequest.useProximitySensor = shouldUseProximitySensorLocked();
mDisplayPowerRequest.lowPowerMode = mLowPowerModeEnabled;
mDisplayPowerRequest.boostScreenBrightness = shouldBoostScreenBrightness();
mDisplayPowerRequest.useTwilight = mBrightnessUseTwilight;
if (mDisplayPowerRequest.policy == DisplayPowerRequest.POLICY_DOZE) {
mDisplayPowerRequest.dozeScreenState = mDozeScreenStateOverrideFromDreamManager;
if (mDisplayPowerRequest.dozeScreenState == Display.STATE_DOZE_SUSPEND
&& (mWakeLockSummary & WAKE_LOCK_DRAW) != 0) {
mDisplayPowerRequest.dozeScreenState = Display.STATE_DOZE;
}
mDisplayPowerRequest.dozeScreenBrightness =
mDozeScreenBrightnessOverrideFromDreamManager;
} else {
mDisplayPowerRequest.dozeScreenState = Display.STATE_UNKNOWN;
mDisplayPowerRequest.dozeScreenBrightness = PowerManager.BRIGHTNESS_DEFAULT;
}
//重点关注该方法
mDisplayReady = mDisplayManagerInternal.requestPowerState(mDisplayPowerRequest,
mRequestWaitForNegativeProximity);//
mRequestWaitForNegativeProximity = false;

}
return mDisplayReady && !oldDisplayReady;
}

DisplayManagerInternal.java

/Users/tracyliu/AndroidStudioProjects/EBook3.2-7.1/frameworks/base/core/java/android/hardware/display/DisplayManagerInternal.java

1
2
public abstract boolean requestPowerState(DisplayPowerRequest request,
boolean waitForNegativeProximity);

DisplayManagerService.java

DMS中的LocalService继承了DisplayManagerInternal,代码进入DisplayPowerController.java

1
2
3
4
5
6
7
8
9
private final class LocalService extends DisplayManagerInternal {
@Override

@Override
public boolean requestPowerState(DisplayPowerRequest request,
boolean waitForNegativeProximity) {
return mDisplayPowerController.requestPowerState(request,
waitForNegativeProximity);
}

DisplayPowerController.java

/Users/tracyliu/AndroidStudioProjects/EBook3.2-7.1/frameworks/base/services/core/java/com/android/server/display/DisplayPowerController.java

1
2
3
4
5
public boolean requestPowerState(DisplayPowerRequest request,
boolean waitForNegativeProximity) {

synchronized (mLock) {
boolean changed = false;
if (changed && !mPendingRequestChangedLocked) {
    mPendingRequestChangedLocked = true;
    sendUpdatePowerStateLocked();//重点看这个
}
        
1
2
3
4
        return mDisplayReadyLocked;
}

}
  • sendUpdatePowerStateLocked(); - MSG_UPDATE_POWER_STATE

  • updatePowerState()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

private void updatePowerState() {


// Configure auto-brightness.
boolean autoBrightnessEnabled = false;
if (mAutomaticBrightnessController != null) {
final boolean autoBrightnessEnabledInDoze = mAllowAutoBrightnessWhileDozingConfig
&& (state == Display.STATE_DOZE || state == Display.STATE_DOZE_SUSPEND);
autoBrightnessEnabled = (mPowerRequest.useAutoBrightness
&& (state == Display.STATE_ON || autoBrightnessEnabledInDoze)
|| mUseActiveDozeLightSensorConfig && autoBrightnessEnabledInDoze)
&& brightness < 0;
final boolean userInitiatedChange = autoBrightnessAdjustmentChanged
&& mPowerRequest.brightnessSetByUser;
mAutomaticBrightnessController.configure(autoBrightnessEnabled,
mPowerRequest.screenAutoBrightnessAdjustment, state != Display.STATE_ON,
userInitiatedChange);
}


}

AutomaticBrightnessController.java

/Users/tracyliu/AndroidStudioProjects/EBook3.2-7.1/frameworks/base/services/core/java/com/android/server/display/AutomaticBrightnessController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

public void configure(boolean enable, float adjustment, boolean dozing,
boolean userInitiatedChange) {


···
boolean changed = setLightSensorEnabled(enableSensor); //控制sensor是否enable
changed |= setScreenAutoBrightnessAdjustment(adjustment);
if (changed) {
updateAutoBrightness(false /*sendUpdate*/); //进行亮度调节
}
if (enableSensor && userInitiatedChange) {
prepareBrightnessAdjustmentSample();
}
···
}



private boolean setLightSensorEnabled(boolean enable) {
if (enable) {
if (!mLightSensorEnabled) {
if (DEBUG) {
Slog.d(TAG, "setLightSensorEnabled: sensor enabled");
}
mLightSensorEnabled = true;
mAmbientLightRingBuffer.clear();
mInitialHorizonAmbientLightRingBuffer.clear();
mAmbientLuxValid = !mResetAmbientLuxAfterWarmUpConfig;
mLightSensorEnableTime = SystemClock.uptimeMillis();
mCurrentLightSensorRate = mInitialLightSensorRate;
mSensorManager.registerListener(mLightSensorListener, mLightSensor, //注册 mLightSensorListener
mCurrentLightSensorRate * 1000, mHandler);
return true;
}
} else {
if (mLightSensorEnabled) {
if (DEBUG) {
Slog.d(TAG, "setLightSensorEnabled: sensor disabled");
}
mLightSensorEnabled = false;
mRecentLightSamples = 0;
mCurrentLightSensorRate = -1;
mHandler.removeMessages(MSG_UPDATE_AMBIENT_LUX);
mSensorManager.unregisterListener(mLightSensorListener);
}
}
return false;
}

SensorEventListener是一个interface 对应的SystemSensorManager在dispatchSensorEvent()会调用lisenter的onSensorChanged源源不断的从native层接受消息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

private final SensorEventListener mLightSensorListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
if (mLightSensorEnabled) {
final long time = SystemClock.uptimeMillis();
final float lux = event.values[0];
handleLightSensorEvent(time, lux);//
}
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// Not used.
}
};

handleLightSensorEvent(time, lux);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

private void handleLightSensorEvent(long time, float lux) {
mHandler.removeMessages(MSG_UPDATE_AMBIENT_LUX);

if (mAmbientLightRingBuffer.size() == 0) {
// switch to using the steady-state sample rate after grabbing the initial light sample
adjustLightSensorRate(mNormalLightSensorRate);
}
applyLightSensorMeasurement(time, lux);
updateAmbientLux(time); // 循环更新sensor 发送MSG_UPDATE_AMBIENT_LUX消息
if (mUseActiveDozeLightSensorConfig && mDozing) {
// disable the ambient light sensor and update the screen brightness
setLightSensorEnabled(false);
updateAutoBrightness(true /*sendUpdate*/);
}
}

整体流程

1.从Settings设置的Mode可以锁定PMS中的mScreenBrightnessModeSetting以及updateDisplayPowerStateLocked()调用到DisplayManagerService
2.DisplayPowerController的requestPowerState()发送MSG_UPDATE_POWER_STATE消息,在updatePowerState()调用AutomaticBrightnessController的configure()
3.configure方法中做了两件事儿,一个是更新亮度,另外一个注册sensorlistener从native层接受消息调节亮度

```