android6.0 power显示(亮度等)深入分析(二)DisplayManagerServic
更新时间:2024-05-30 17:57:01 阅读量: 综合文库 文档下载
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(\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)); } }
再来看看registerDisplayAdapterLocked
[cpp] view plain copy 在CODE上查看代码片派生到我的代码片 private void registerDisplayAdapterLocked(DisplayAdapter adapter) { mDisplayAdapters.add(adapter); adapter.registerLocked(); }
这里就是register了DefaultDisplay的适配层,就是和背光相关的。在新建LocalDisplayAdapter的时候我们把mDisplayAdapterListener传过去了。
二、LocalDisplayAdapter & LocalDisplayDevice
LocalDisplayAdapter构造函数调用了父类的,而父类也就是保存了变量 [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 can't use it Slog.w(TAG, \ builtInDisplayId); return;
}
int activeConfig = SurfaceControl.getActiveConfig(displayToken); if (activeConfig < 0) {
// There is no active config, and for now we don't have the // policy to set one.
Slog.w(TAG, \ 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, we'll 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函数,就是调用了传入的参数DisplayAdapterListener
[cpp] 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, \ return; }
Slog.i(TAG, \ 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 < 0) {
brightness = PowerManager.BRIGHTNESS_DEFAULT; } 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, \
+ Display.stateToString(state)
+ \ 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
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, \ + \
+ \ }
Trace.traceBegin(Trace.TRACE_TAG_POWER, \ + \
+ \ 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, \
+ \ }
Trace.traceBegin(Trace.TRACE_TAG_POWER, \ + \ 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, \ + Display.stateToString(state)
+ \ 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层
我们先来看看LightsService
[cpp] view plain copy 在CODE上查看代码片派生到我的代码片 public class LightsService extends SystemService { static final String TAG = \ static final boolean DEBUG = false;
final LightImpl mLights[] = new LightImpl[LightsManager.LIGHT_ID_COUNT];
private final class LightImpl extends Light {
private LightImpl(int id) { mId = id; }
@Override
public void setBrightness(int brightness) {
setBrightness(brightness, BRIGHTNESS_MODE_USER); }
@Override
public void setBrightness(int brightness, int brightnessMode) { synchronized (thwww.sm136.comis) { int color = brightness & 0x000000ff;
正在阅读:
android6.0 power显示(亮度等)深入分析(二)DisplayManagerServic05-30
山东省乡村学校少年宫考核评估标准03-19
义卖活动作文500字07-08
说明书105-09
消防验收规范标准(最新完整版)08-21
钢筋工程劳务分包合同05-30
通识课练习-唐宋词与人生11-17
数学建模习题及答案课后习题11-10
发言稿范文|发言稿格式|发言稿模板08-01
- 《江苏省环境水质(地表水)自动监测预警系统运行管理办法(试行)》
- 安乐死合法化辩论赛立论稿(浙大新生赛)
- 公共科目模拟试卷公务员考试资料
- 我国固定资产投资FAI对GDP的影响
- 大学生创新创业训练计划项目申请书大创项目申报表
- 完美版—单片机控制步进电机
- 2013资阳中考化学试题
- 18.两位数减一位数退位(397道)
- 工程量计算规则
- 二年级操行评语(下)
- 第3章 流程控制语句
- 浅基桥墩加固技术
- 课题研究的主要方法
- 5100软件说明书 - 图文
- 车间技术员年终总结
- 关于印发《中铁建工集团开展项目管理实验室活动方案》的通知
- 经典诵读结题报告
- 地下水动力学习题答案
- 2018年全国各地高考数学模拟试题平面解析几何试题汇编(含答案解
- 街道办事处主任2018年度述职述廉报告
- DisplayManagerSe
- 亮度
- 深入
- android
- 分析
- 显示
- power
- rvic
- 6.0