1、Android60 亮屏灭屏流程DisplayPowerControlerWMS二亮度设置Android6.0 亮屏灭屏流程(DisplayPowerControler、WMS)(二)亮度设置从这个函数开始分析,主要分析下亮度的设置流程。cpp view plain copy 在CODE上查看代码片派生到我的代码片public void setScreenState(int state) if (mScreenState != state) if (DEBUG) Slog.d(TAG, setScreenState: state= + state); mScreenState = state;
2、 mScreenReady = false; scheduleScreenUpdate(); scheduleScreenUpdate主要通过消息方式,最后调用到下面函数。当我们屏幕刚要点亮,这个时候mScreenBrightness为0,所以这个时候调用mPhotonicModulator.setState设置state是点亮,但是brightness是0的。cpp view plain copy 在CODE上查看代码片派生到我的代码片private final Runnable mScreenUpdateRunnable = new Runnable() Override public v
3、oid run() mScreenUpdatePending = false; int brightness = mScreenState != Display.STATE_OFF & mColorFadeLevel 0f ? mScreenBrightness : 0; if (mPhotonicModulator.setState(mScreenState, brightness) if (DEBUG) Slog.d(TAG, Screen ready); mScreenReady = true; invokeCleanListenerIfNeeded(); else if (DEBUG)
4、 Slog.d(TAG, Screen not ready); ; DisplayPowerState的设置亮度状态逻辑分析mPhotonicModulator.setState应该要PhotonicModulator的run函数结合一起看。cpp view plain copy 在CODE上查看代码片派生到我的代码片public boolean setState(int state, int backlight) synchronized (mLock) boolean stateChanged = state != mPendingState; boolean backlightChang
5、ed = backlight != mPendingBacklight; if (stateChanged | backlightChanged) if (DEBUG) Slog.d(TAG, Requesting new screen state: state= + Display.stateToString(state) + , backlight= + backlight); mPendingState = state; mPendingBacklight = backlight; boolean changeInProgress = mStateChangeInProgress | m
6、BacklightChangeInProgress; mStateChangeInProgress = stateChanged; mBacklightChangeInProgress = backlightChanged; if (!changeInProgress) Slog.d(TAG,notify set backlight thread run); mLock.notifyAll(); return !mStateChangeInProgress; 两者结合看先setState设置了状态,只有状态改变时,我们才能重新设置状态(设置到mpendingState和mPendingBack
7、light)。而在run函数中,当设置的状态mPendingState、mPendingBacklight和mActualState、mActualBacklight(真正设置到背光的状态、亮度)不一样时,才会调用mBlanker.requestDisplayState设置亮度。否则状态没有改变,就会把mStateChangeInProgress 和mBacklightChangeInProgress 设置为false,然后线程就wait住。而此时setState重新设置下来的话,这个时候把亮度和状态设置到mPendingState 和mPendingBacklight 。然后这时mState
8、ChangeInProgress 和 mBacklightChangeInProgress都是false。这样就可以调用mLock的notifyAll函数重新唤醒线程,这样就把把前面setState设置下来的mPendingState和mPendingBacklight再通过mBlanker.requestDisplayState设置到背光设备中去。cpp view plain copy 在CODE上查看代码片派生到我的代码片Override public void run() for (;) / Get pending change. final int state; final boole
9、an stateChanged; final int backlight; final boolean backlightChanged; synchronized (mLock) state = mPendingState; stateChanged = (state != mActualState); backlight = mPendingBacklight; backlightChanged = (backlight != mActualBacklight); if (!stateChanged) / State changed applied, notify outer class.
10、 postScreenUpdateThreadSafe(); mStateChangeInProgress = false; if (!backlightChanged) mBacklightChangeInProgress = false; if (!stateChanged & !backlightChanged) try mLock.wait(); catch (InterruptedException ex) continue; mActualState = state; mActualBacklight = backlight; / Apply pending change. if
11、(true) Slog.d(TAG, Updating screen state: state= + Display.stateToString(state) + , backlight= + backlight); mBlanker.requestDisplayState(state, backlight); Slog.d(TAG, kangchen Updating screen state: state=); 设置亮度、状态到背光设备DisplayBlanker的requestDisplayState如下,主要调用requestGlobalDisplayStateInternal函数。c
12、pp view plain copy 在CODE上查看代码片派生到我的代码片DisplayBlanker blanker = new DisplayBlanker() Override public void requestDisplayState(int state, int brightness) / The order of operations is important for legacy reasons. if (state = Display.STATE_OFF) requestGlobalDisplayStateInternal(state, brightness); call
13、backs.onDisplayStateChange(state); if (state != Display.STATE_OFF) requestGlobalDisplayStateInternal(state, brightness); ; requestGlobalDisplayStateInternal函数先是对state和brightness的处理,然后把这个两个变量放在mGlobalDisplayState 和mGlobalDisplayBrightness成员变量中。紧接着调用applyGlobalDisplayStateLocked函数mTempDisplayStateWork
14、Queue作为参数。最后再调用mTempDisplayStateWorkQueue各个成员的run函数(这里返回的是Runnable接口,这里就会设置状态和亮度到设备中去)。cpp view plain copy 在CODE上查看代码片派生到我的代码片private void requestGlobalDisplayStateInternal(int state, int brightness) if (state = Display.STATE_UNKNOWN) state = Display.STATE_ON; if (state = Display.STATE_OFF) brightne
15、ss = PowerManager.BRIGHTNESS_OFF; else if (brightness PowerManager.BRIGHTNESS_ON) brightness = PowerManager.BRIGHTNESS_ON; synchronized (mTempDisplayStateWorkQueue) try synchronized (mSyncRoot) if (mGlobalDisplayState = state & mGlobalDisplayBrightness = brightness) return; / no change mGlobalDispla
16、yState = state; mGlobalDisplayBrightness = brightness; applyGlobalDisplayStateLocked(mTempDisplayStateWorkQueue); / Setting the display power state can take hundreds of milliseconds / to complete so we defer the most expensive part of the work until / after we have exited the critical section to avo
17、id blocking other / threads for a long time. for (int i = 0; i mTempDisplayStateWorkQueue.size(); i+) mTempDisplayStateWorkQueue.get(i).run();/设置亮度、状态到设备 finally mTempDisplayStateWorkQueue.clear(); applyGlobalDisplayStateLocked函数会遍历各个显示设备(多显示),然后调用updateDisplayStateLocked函数返回一个Runnable,最后把这个Runnable
18、放入之前传入的mTempDisplayStateWorkQueue队列中。cpp view plain copy 在CODE上查看代码片派生到我的代码片private void applyGlobalDisplayStateLocked(List workQueue) final int count = mDisplayDevices.size(); for (int i = 0; i count; i+) DisplayDevice device = mDisplayDevices.get(i); Runnable runnable = updateDisplayStateLocked(de
19、vice); if (runnable != null) workQueue.add(runnable); 那下面我们看下updateDisplayStateLocked函数,主要是调用了DisplayDevice的requestDisplayStateLocked函数,当然mGlobalDisplayState和mGlobalDisplayBrightness作为参数。cpp view plain copy 在CODE上查看代码片派生到我的代码片private Runnable updateDisplayStateLocked(DisplayDevice device) / Blank or
20、 unblank the display immediately to match the state requested / by the display power controller (if known). DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked(); if (info.flags & DisplayDeviceInfo.FLAG_NEVER_BLANK) = 0) return device.requestDisplayStateLocked(mGlobalDisplayState, mGlobalDisp
21、layBrightness); return null; 这里的DisplayDevice的requestDisplayStateLocked函数,是在LocalDisplayAdapter中实现的,这里吧state和brightness保存在mState和mBrightness中,然后返回Runnable接口,最后在Runnable接口中设置亮度和状态。cpp view plain copy 在CODE上查看代码片派生到我的代码片public Runnable requestDisplayStateLocked(final int state, final int brightness) /
22、 Assume that the brightness is off if the display is being turned off. assert state != Display.STATE_OFF | brightness = PowerManager.BRIGHTNESS_OFF; final boolean stateChanged = (mState != state); final boolean brightnessChanged = (mBrightness != brightness) & mBacklight != null; if (stateChanged |
23、brightnessChanged) final int displayId = mBuiltInDisplayId; final IBinder token = getDisplayTokenLocked(); final int oldState = mState; if (stateChanged) mState = state;/保存state updateDeviceInfoLocked(); if (brightnessChanged) mBrightness = brightness;/保存brightness / Defer actually setting the displ
24、ay state until after we have exited / the critical section since it can take hundreds of milliseconds / to complete. return new Runnable() /返回Runnable Override public void run() / Exit a suspended state before making any changes. int currentState = oldState; if (Display.isSuspendedState(oldState) |
25、oldState = Display.STATE_UNKNOWN) if (!Display.isSuspendedState(state) setDisplayState(state); currentState = state; else if (state = Display.STATE_DOZE_SUSPEND | oldState = Display.STATE_DOZE_SUSPEND) setDisplayState(Display.STATE_DOZE); currentState = Display.STATE_DOZE; else return; / old state a
26、nd new state is off / Apply brightness changes given that we are in a non-suspended state. if (brightnessChanged) Slog.d(TAG, kangchen setDisplayBrightnessbrightness1= + brightness); setDisplayBrightness(brightness); Slog.d(TAG, kangchen setDisplayBrightnessbrightness2= + brightness); / Enter the fi
27、nal desired state, possibly suspended. if (state != currentState) setDisplayState(state); private void setDisplayState(int state) if (DEBUG) Slog.d(TAG, setDisplayState( + id= + displayId + , state= + Display.stateToString(state) + ); try final int mode = getPowerModeForState(state); SurfaceControl.
28、setDisplayPowerMode(token, mode);/到SurfaceControl设置状态 finally Trace.traceEnd(Trace.TRACE_TAG_POWER); private void setDisplayBrightness(int brightness) try mBacklight.setBrightness(brightness);/设置亮度 finally Trace.traceEnd(Trace.TRACE_TAG_POWER); ; return null; DisplayPowerControl设置亮度逻辑(根据VSync信号将亮度慢慢
29、变亮)上面在DisplayPowerState中仅仅是设置状态,比如刚点亮屏幕这个时候其实设置的brightness为0,我们继续分析DisplayPowerState的updatePowerState函数。在updatePowerState函数中,当设置亮度时会调用如下代码:cpp view plain copy 在CODE上查看代码片派生到我的代码片if (!mPendingScreenOff) if (state = Display.STATE_ON | state = Display.STATE_DOZE) animateScreenBrightness(brightness, slowChange ? BRIGHTNESS_RAMP_RATE_SLOW : BRIGHTNESS_RAMP_RATE_FAST);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1