Android60 亮屏灭屏流程(二)亮度设置_第1页
Android60 亮屏灭屏流程(二)亮度设置_第2页
Android60 亮屏灭屏流程(二)亮度设置_第3页
Android60 亮屏灭屏流程(二)亮度设置_第4页
Android60 亮屏灭屏流程(二)亮度设置_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、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;mScreenReady = false; scheduleScreenUpdate(

2、);scheduleScreenUpdate 主要通过消息方式,最后调用到下面函数。当我们屏幕刚要点亮,这 个时候 mScreenBrightness 为 0,所以这个时候调用 mPhotonicModulator.setState 设置 state 是 点亮,但是 brightness 是 0 的。cpp view plain copy 在 CODE 上查看代码片派生到我的代码片 private final Runnable mScreenUpdateRunnable = new Runnable() Override public void run() mScreenUpdatePendi

3、ng = 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) Slog.d(TAG, Screen not ready);

4、DisplayPowerState 的设置亮度状态逻辑分析 mPhotonicModulator.setState 应该要 PhotonicModulator 的 run 函数结合一起看。cpp view plain copy 在 CODE 上查看代码片派生到我的代码片public boolean setState(int state, int backlight) synchronized (mLock) boolean stateChanged = state != mPendingState;boolean backlightChanged = backlight != mPending

5、Backlight;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 | mBacklightChangeInProgress;mStat

6、eChangeInProgress = stateChanged;mBacklightChangeInProgress = backlightChanged;if (!changeInProgress) Slog.d(TAG,notify set backlight thread run);mLock.notifyAll();return !mStateChangeInProgress;两者结合看先 setState 设置了状态,只有状态改变时,我们才能重新设置状态(设置到 mpendingState 和 mPendingBacklight)。而在 run 函数中,当设置的状态 mPendin

7、gState、 mPendingBacklight 和 mActualState、mActualBacklight (真正设置到背光的状态、亮度)不 一样时,才会调用 mBlanker.requestDisplayState 设置亮度。否则状态没有改变,就会把 mStateChangeInProgress 和 mBacklightChangeInProgress 设置为 false,然后线程就 wait 住。 而此时 setState 重新设置下来的话,这个时候把亮度和状态设置到 mPendingState 和 mPendingBacklight 。然后这时 mStateChangeInPro

8、gress 和 mBacklightChangeInProgress 都是 false。这样就可以调用mLock的notifyAll函数重新唤醒线程,这样就把把前面setState设置 下来的 mPendingState 和 mPendingBacklight 再通过 mBlanker.requestDisplayState设置到背光 设备中去。cpp view plain copy 在 CODE 上查看代码片派生到我的代码片Overridepublic void run() for (;) / Get pending change.final int state;final boolean

9、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. postScr

10、eenUpdateThreadSafe(); mStateChangeInProgress = false;if (!backlightChanged) mBacklightChangeInProgress = false;if (!stateChanged & !backlightChanged) try mLock.wait(); catch (InterruptedException ex) continue;mActualState = state; mActualBacklight = backlight;/ Apply pending change.if (true) Slog.d

11、(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 函数。 cpp view p

12、lain copy 在 CODE 上查看代码片派生到我的代码片DisplayBlanker blanker = new DisplayBlanker() Overridepublic void requestDisplayState(int state, int brightness) / The order of operations is important for legacy reasons.if (state = Display.STATE_OFF) requestGlobalDisplayStateInternal(state, brightness);callbacks.onDi

13、splayStateChange(state);if (state != Display.STATE_OFF) requestGlobalDisplayStateInternal(state, brightness);requestGlobalDisplayStatelnternal函数先是对state和brightness的处理,然后把这个两个变量 放 在 mGlobalDisplayState 和 mGlobalDisplayBrightness 成 员 变 量 中 。 紧 接 着 调 用 applyGlobalDisplayStateLocked 函数 mTempDisplayState

14、WorkQueue 作为参数。最后再调用 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)

15、brightness = 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 changemGlobalD

16、isplayState = 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 av

17、oid blocking other/ threads for a long time.for (int i = 0; i mTempDisplayStateWorkQueue.size(); i+) mTempDisplayStateWorkQueue.get(i).run();/设置亮度、状态到设备 finally mTempDisplayStateWorkQueue.clear();applyGlobalDisplayStateLocked 函 数 会 遍 历 各 个 显 示 设 备 ( 多 显 示 ), 然 后 调 用 updateDisplayStateLocked函数返回一个 Ru

18、nnable,最后把这个 Runnable放入之前传入的 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 = upda

19、teDisplayStateLocked(device);if (runnable != null) workQueue.add(runnable);那下面我们看下 updateDisplayStateLocked 函 数, 主要是调用了 DisplayDevice 的 requestDisplayStateLocked 函数,当然 mGlobalDisplayState 和 mGlobalDisplayBrightness 作为 参数。cpp view plain copy 在 CODE 上查看代码片派生到我的代码片private Runnable updateDisplayStateLoc

20、ked(DisplayDevice device) / Blank or 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) returndevice.requestDisplayStateLocked(

21、mGlobalDisplayState,mGlobalDisplayBrightness);return null;这里的 DisplayDevice 的 requestDisplayStateLocked 函数,是在 LocalDisplayAdapter 中实现的, 这里吧 state 和 brightness 保存在 mState 和 mBrightness 中,然后返回 Runnable 接口,最后 在 Runnable 接口中设置亮度和状态。cpp view plain copy 在 CODE 上查看代码片派生到我的代码片public Runnable requestDisplayS

22、tateLocked(final int state, final int brightness) / 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 != brigh

23、tness) & mBacklight != null; if (stateChanged | brightnessChanged) final int displayId = mBuiltInDisplayId;final IBinder token = getDisplayTokenLocked();final int oldState = mState;if (stateChanged) mState = state;/保存 stateupdateDeviceInfoLocked();if (brightnessChanged) mBrightness = brightness;/保存

24、brightness/ Defer actually setting the display state until after we have exited/ the critical section since it can take hundreds of milliseconds/ to complete.return new Runnable() /返回 RunnableOverridepublic void run() / Exit a suspended state before making any currentState = oldState;if (Display.isS

25、uspendedState(oldState)| 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; els

26、e return; / old state and 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

27、);/ Enter the final 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);Surfa

28、ceControl.setDisplayPowerMode(token, mode);/至U 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设置亮度逻辑(根据VSy

29、nc信号将亮度慢慢变亮)上面在 DisplayPowerState 中仅仅是设置状态,比如刚点亮屏幕这个时候其实设置的brightness 为 0,我们继续分析 DisplayPowerState 的 updatePowerState 函数。在 updatePowerState 函数中,当设置亮度时会调用如下代码:cpp view plain copy 在 CODE 上查看代码片派生至我的代码片if (!mPendingScreenOff) if (state = Display.STATE_ON | state = Display.STATE_DOZE) animateScreenBrigh

30、tness(brightness,slowChange ? BRIGHTNESS_RAMP_RATE_SLOW : BRIGHTNESS_RAMP_RATE_FAST); else animateScreenBrightness(brightness, 0);我 们 注 意 到 这 里 有 一 个 BRIGHTNESS_RAMP_RATE_SLOW 和 BRIGHTNESS_RAMP_RATE_FAST (这里涉及到亮度显示原理我们后面分析),先看 animateScreenBrightness函数 。 这 里 函 数 主 要 调 用了mScreenBrightnessRampAnimator

31、.animateTo 函数。cpp view plain copy 在 CODE 上查看代码片派生到我的代码片private void animateScreenBrightness(int target, int rate) if (mScreenBrightnessRampAnimator.animateTo(target, rate) try mBatteryStats.noteScreenBrightness(target); catch (RemoteException ex) / same process我们再来看 mScreenBrightnessRampAnimator 对象的

32、创建cpp view plain copy 在 CODE 上查看代码片派生到我的代码片 mScreenBrightnessRampAnimator = new RampAnimator(mPowerState, DisplayPowerState.SCREEN_BRIGHTNESS);我 们 注 意 一 个 参 数 是 DisplayPowerState 对 象 mPowerState , 另 一 个 参 数 是 DisplayPowerState.SCREEN_BRIGHTNESScpp view plain copy 在 CODE 上查看代码片派生到我的代码片public static f

33、inal IntProperty SCREEN_BRIGHTNESS =new IntProperty(screenBrightness) Overridepublic void setValue(DisplayPowerState object, int value) object.setScreenBrightness(value);Overridepublic Integer get(DisplayPowerState object) return object.getScreenBrightness();RampAnimator 的构造函数。cpp view plain copy 在

34、CODE 上查看代码片派生到我的代码片public RampAnimator(T object, IntProperty property) mObject = object;mProperty = property; mChoreographer = Choreographer.getInstance();我们结合 RampAnimator 的构造函数,再来分析 RampAnimator 的 animateTo 函数。当ratev=O时,这个时候,我们直接调用mProperty.setValue。就是调用DisplayPowerState 的 setScreenBrightness 函数。这

35、个 setScreenBrightness 函数我们后面分析。当rate0时,这个时候会调用postAnimationCallback函数(这个函数根据VSync信号过 来,把亮度慢慢上升的一个过程),而且mAnimating置为true。cpp view plain copy 在 CODE 上查看代码片派生到我的代码片 public boolean animateTo(int target, int rate) / Immediately jump to the target the first time.if (mFirstTime | rate mRate| (target = mCur

36、rentValue & mCurrentValue = mTargetValue)| (mTargetValue = mCurrentValue & mCurrentValue mCurrentValue) mAnimatedValue = Math.min(mAnimatedValue + amount, mTargetValue); else mAnimatedValue = Math.max(mAnimatedValue - amount, mTargetValue); final int oldCurrentValue = mCurrentValue; mCurrentValue = Math.round(mAnimatedValue);if (oldCurrentValue != mCurrentValue) mProperty.setValue(mObject, mCurrentValue);/ 设 置 到 DisplayPowerS

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论