android6.0power显示(亮度等)深入分析(二)DisplayManagerService难点.doc_第1页
android6.0power显示(亮度等)深入分析(二)DisplayManagerService难点.doc_第2页
android6.0power显示(亮度等)深入分析(二)DisplayManagerService难点.doc_第3页
android6.0power显示(亮度等)深入分析(二)DisplayManagerService难点.doc_第4页
android6.0power显示(亮度等)深入分析(二)DisplayManagerService难点.doc_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

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

文档简介

android6.0 power显示(亮度等)深入分析(二)DisplayManagerService一、DisplayManagerService注册localDisplay的适配层我们先来看构造函数:cpp view plain copy 在CODE上查看代码片派生到我的代码片public DisplayManagerService(Context context) super(context); mContext = context; mHandler = new DisplayManagerHandler(DisplayThread.get().getLooper();/消息处理 mUiHandler = UiThread.getHandler(); mDisplayAdapterListener = new DisplayAdapterListener();/display适配层监视器 mSingleDisplayDemoMode = SystemProperties.getBoolean(persist.demo.singledisplay, false); PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); mGlobalDisplayBrightness = pm.getDefaultScreenBrightnessSetting();/成员变量屏幕亮度 我们再来看onStart函数,publish了一个BinderService和LocalService,还有发送了一个消息。cpp view plain copy 在CODE上查看代码片派生到我的代码片Override public void onStart() mHandler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTER); publishBinderService(Context.DISPLAY_SERVICE, new BinderService(), true /*allowIsolated*/); publishLocalService(DisplayManagerInternal.class, new LocalService(); 我们看消息处理,就是调用了registerDefaultDisplayAdapter函数:cpp view plain copy 在CODE上查看代码片派生到我的代码片Override public void handleMessage(Message msg) switch (msg.what) case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTER: registerDefaultDisplayAdapter(); break; registerDefaultDisplayAdapter函数cpp view plain copy 在CODE上查看代码片派生到我的代码片private void registerDefaultDisplayAdapter() / Register default display adapter. synchronized (mSyncRoot) registerDisplayAdapterLocked(new LocalDisplayAdapter( mSyncRoot, mContext, mHandler, mDisplayAdapterListener); 再来看看registerDisplayAdapterLockedcpp view plain copy 在CODE上查看代码片派生到我的代码片private void registerDisplayAdapterLocked(DisplayAdapter adapter) mDisplayAdapters.add(adapter); adapter.registerLocked(); 这里就是register了DefaultDisplay的适配层,就是和背光相关的。在新建LocalDisplayAdapter的时候我们把mDisplayAdapterListener传过去了。二、LocalDisplayAdapter & LocalDisplayDeviceLocalDisplayAdapter构造函数调用了父类的,而父类也就是保存了变量cpp view plain copy 在CODE上查看代码片派生到我的代码片public LocalDisplayAdapter(DisplayManagerService.SyncRoot syncRoot, Context context, Handler handler, Listener listener) super(syncRoot, context, handler, listener, TAG); 上面又紧跟着调用了registerLocked函数cpp view plain copy 在CODE上查看代码片派生到我的代码片public void registerLocked() super.registerLocked(); mHotplugReceiver = new HotplugDisplayEventReceiver(getHandler().getLooper(); for (int builtInDisplayId : BUILT_IN_DISPLAY_IDS_TO_SCAN) tryConnectDisplayLocked(builtInDisplayId); tryConnectDisplayLocked函数,先是看传入的builtInDisplayId是否支持,一个是main,一个是hdmi的。cpp view plain copy 在CODE上查看代码片派生到我的代码片private void tryConnectDisplayLocked(int builtInDisplayId) IBinder displayToken = SurfaceControl.getBuiltInDisplay(builtInDisplayId); if (displayToken != null) SurfaceControl.PhysicalDisplayInfo configs = SurfaceControl.getDisplayConfigs(displayToken); if (configs = null) / There are no valid configs for this device, so we cant use it Slog.w(TAG, No valid configs found for display device + builtInDisplayId); return; int activeConfig = SurfaceControl.getActiveConfig(displayToken); if (activeConfig 0) / There is no active config, and for now we dont have the / policy to set one. Slog.w(TAG, No active config found for display device + builtInDisplayId); return; LocalDisplayDevice device = mDevices.get(builtInDisplayId); if (device = null) / Display was added. device = new LocalDisplayDevice(displayToken, builtInDisplayId, configs, activeConfig); mDevices.put(builtInDisplayId, device); sendDisplayDeviceEventLocked(device, DISPLAY_DEVICE_EVENT_ADDED); else if (device.updatePhysicalDisplayInfoLocked(configs, activeConfig) / Display properties changed. sendDisplayDeviceEventLocked(device, DISPLAY_DEVICE_EVENT_CHANGED); else / The display is no longer available. Ignore the attempt to add it. / If it was connected but has already been disconnected, well get a / disconnect event that will remove it from mDevices. 然后再去查找这个LocalDisplayDevice,如果是找到了需要更新下configs,没找到需要新建一个LocalDisplayDevice。最后都调用了sendDisplayDeviceEventLocked函数。我们再来看LocalDisplayDevice,如果传入的是BUILT_IN_DISPLAY_ID_MAIN就是背光的,我们获取背光的Light,保存在mBackLight变量。cpp view plain copy 在CODE上查看代码片派生到我的代码片public LocalDisplayDevice(IBinder displayToken, int builtInDisplayId, SurfaceControl.PhysicalDisplayInfo physicalDisplayInfos, int activeDisplayInfo) super(LocalDisplayAdapter.this, displayToken, UNIQUE_ID_PREFIX + builtInDisplayId); mBuiltInDisplayId = builtInDisplayId; updatePhysicalDisplayInfoLocked(physicalDisplayInfos, activeDisplayInfo); if (mBuiltInDisplayId = SurfaceControl.BUILT_IN_DISPLAY_ID_MAIN) LightsManager lights = LocalServices.getService(LightsManager.class); mBacklight = lights.getLight(LightsManager.LIGHT_ID_BACKLIGHT); else mBacklight = null; 然后上面函数调用了sendDisplayDeviceEventLocked函数,就是调用了传入的参数DisplayAdapterListenercpp view plain copy 在CODE上查看代码片派生到我的代码片protected final void sendDisplayDeviceEventLocked( final DisplayDevice device, final int event) mHandler.post(new Runnable() Override public void run() mListener.onDisplayDeviceEvent(device, event); ); 如果是新建就调用了handleDisplayDeviceAdded函数,cpp view plain copy 在CODE上查看代码片派生到我的代码片private final class DisplayAdapterListener implements DisplayAdapter.Listener Override public void onDisplayDeviceEvent(DisplayDevice device, int event) switch (event) case DisplayAdapter.DISPLAY_DEVICE_EVENT_ADDED: handleDisplayDeviceAdded(device); break; case DisplayAdapter.DISPLAY_DEVICE_EVENT_CHANGED: handleDisplayDeviceChanged(device); break; case DisplayAdapter.DISPLAY_DEVICE_EVENT_REMOVED: handleDisplayDeviceRemoved(device); break; Override public void onTraversalRequested() synchronized (mSyncRoot) scheduleTraversalLocked(false); 我们先来看看handleDisplayDeviceAdded,最后将device保存在了mDisplayDevices中。cpp view plain copy 在CODE上查看代码片派生到我的代码片private void handleDisplayDeviceAdded(DisplayDevice device) synchronized (mSyncRoot) handleDisplayDeviceAddedLocked(device); private void handleDisplayDeviceAddedLocked(DisplayDevice device) DisplayDeviceInfo info = device.getDisplayDeviceInfoLocked(); if (mDisplayDevices.contains(device) Slog.w(TAG, Attempted to add already added display device: + info); return; Slog.i(TAG, Display device added: + info); device.mDebugLastLoggedDeviceInfo = info; mDisplayDevices.add(device); addLogicalDisplayLocked(device); Runnable work = updateDisplayStateLocked(device); if (work != null) work.run(); scheduleTraversalLocked(false); 三、设置背光现在我们在上篇博客不是说背光的调制最后是在DisplayManagerService中,是在下面函数的requestGlobalDisplayStateInternal中调用的cpp view plain copy 在CODE上查看代码片派生到我的代码片public void initPowerManagement(final DisplayPowerCallbacks callbacks, Handler handler, SensorManager sensorManager) synchronized (mSyncRoot) 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); callbacks.onDisplayStateChange(state); if (state != Display.STATE_OFF) requestGlobalDisplayStateInternal(state, brightness); ; mDisplayPowerController = new DisplayPowerController( mContext, callbacks, handler, sensorManager, blanker); 我们再来看看requestGlobalDisplayStateInternal函数: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) brightness = PowerManager.BRIGHTNESS_OFF; else if (brightness PowerManager.BRIGHTNESS_ON) brightness = PowerManager.BRIGHTNESS_ON; synchronized (mTempDisplayStateWorkQueue) try / Update the display state within the lock. / Note that we do not need to schedule traversals here although it / may happen as a side-effect of displays changing state. synchronized (mSyncRoot) if (mGlobalDisplayState = state & mGlobalDisplayBrightness = brightness) return; / no change Trace.traceBegin(Trace.TRACE_TAG_POWER, requestGlobalDisplayState( + Display.stateToString(state) + , brightness= + brightness + ); mGlobalDisplayState = 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 avoid blocking other / threads for a long time. for (int i = 0; i mTempDisplayStateWorkQueue.size(); i+) mTempDisplayStateWorkQueue.get(i).run(); Trace.traceEnd(Trace.TRACE_TAG_POWER); finally mTempDisplayStateWorkQueue.clear(); 再看看applyGlobalDisplayStateLocked函数,最后遍历device调用updateDisplayStateLocked函数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(device); if (runnable != null) workQueue.add(runnable); updateDisplayStateLocked函数调用device的requestDisplayStateLocked返回是Runnable,最后放在workQueue队列中cpp view plain copy 在CODE上查看代码片派生到我的代码片private Runnable updateDisplayStateLocked(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) return device.requestDisplayStateLocked(mGlobalDisplayState, mGlobalDisplayBrightness); return null; 我们再来看看LocalDisplayDevice的requestDisplayStateLocked函数cpp view plain copy 在CODE上查看代码片派生到我的代码片public Runnable requestDisplayStateLocked(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 != brightness) & mBacklight != null; if (stateChanged | brightnessChanged) final int displayId = mBuiltInDisplayId; final IBinder token = getDisplayTokenLocked(); final int oldState = mState; if (stateChanged) mState = state;/ 状态 updateDeviceInfoLocked(); if (brightnessChanged) mBrightness = 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() Override public void run() / Exit a suspended state before making any changes. int currentState = oldState; if (Display.isSuspendedState(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; else return; / old state and new state is off / Apply brightness changes given that we are in a non-suspended state. if (brightnessChanged) setDisplayBrightness(brightness);/设置亮度 / 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) + ); Trace.traceBegin(Trace.TRACE_TAG_POWER, setDisplayState( + id= + displayId + , state= + Display.stateToString(state) + ); try final int mode = getPowerModeForState(state); SurfaceControl.setDisplayPowerMode(token, mode); finally Trace.traceEnd(Trace.TRACE_TAG_POWER); private void setDisplayBrightness(int brightness) if (DEBUG) Slog.d(TAG, setDisplayBrightness( + id= + displayId + , brightness= + brightness + ); Trace.traceBegin(Trace.TRACE_TAG_POWER, setDisplayBrightness( + id= + displayId + , brightness= + brightness + ); try mBacklight.setBrightness(brightness);/真正的设置背光 finally Trace.traceEnd(Trace.TRACE_TAG_POWER); ; return null; 上面函数返回一个Runnable放在workQueue,在Runnable 中会调用mBacklight.setBrightness设置背光。之前是将Runnable接口都放在了mTempDisplayStateWorkQueue中,然后遍历调用了run函数。最后就调用到了LocalDisplayDevice的Runnable接口中设置背光了。cpp view plain copy 在CODE上查看代码片派生到我的代码片synchronized (mSyncRoot) if (mGlobalDisplayState = state & mGlobalDisplayBrightness = brightness) return; / no change Trace.traceBegin(Trace.TRACE_TAG_POWER, requestGlobalDisplayState( + Display.stateToString(state) + , brightness= + brightness + ); mGlobalDisplayState = 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 avoid blocking other / threads for a long time. for (int i = 0; i mTempDisplayStateWorkQueue.size(); i+) mTempDisplayStateWorkQueue.get(i).run(); 四、背光hal层我们先来看看LightsServicecpp view plain copy 在CODE上查看代码片派生到我的代码片public class LightsService extends SystemService static final String TAG = LightsService; static final boolean DEBUG = false; final LightImpl mLights = new LightImplLightsManager.LIGHT_ID_COUNT; private final class LightImpl extends Light private LightImpl(int id) mId = id; Override public void setBrightness(int brightness) setBrightness(bri

温馨提示

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

评论

0/150

提交评论