赞
踩
一. 屏幕的唤醒
首先inputread在读取到有keyboard事件上报后,会调用到keydispatch的notifykey,去询问wm是否会对这次按键特殊处理,如果WM不处理,则此处会点亮或者熄灭屏幕。
inputReader.cpp KeyboardInputMapper::processKey
getDispatcher()->notifyKey
inputDispacher.cpp InputDispatcher::notifyKey
mPolicy->interceptKeyBeforeQueueing
com_android_server_inputManager.cpp NativeInputManager::interceptKeyBeforeQueueing
env->CallIntMethod(mCallbacksObj,
gCallbacksClassInfo.interceptKeyBeforeQueueing,
when, action, flags, keyCode, scanCode, policyFlags, isScreenOn); //此处gCallbacksClassInfo中的各种方法就是InputManager的对应的方法,在JNI初始化的时候就注册了,详情请参看register_android_server_InputManager函数,通过jniRegisterNativeMethods将inputmanager的各种callback注册到gCallbacksClassInfo中。
返回的wmaction就是后面WM对此次按键事件的policy,通过此返回值,此处会决定下一步的动作。
InputManager.java interceptKeyBeforeQueueing
mWindowManagerService.mInputMonitor.interceptKeyBeforeQueueing
WindowmanagerService.java InputMonitor::interceptKeyBeforeQueueing
mPolicy.interceptKeyBeforeQueueing
PhonewindowManager.java interceptKeyBeforeQueueing
//摘录部分代码:
wakeWhenReadyLocked
mHandler.obtainMessage(WAKE_WHEN_READY, keyCode, 0);
mHandler.handleMessage
handleWakeWhenReady
mKeyguardViewManager.wakeWhenReadyTq
KeyguardViewManager.java mKeyguardView.wakeWhenReadyTq
LockpatternKeyguardView.java wakeWhenReadyTq
getCallback().pokeWakelock();
KeyguardViewMediator.java pokeWakelock
mWakeLock.acquire(); // mWakeLock即为:mWakeLock = mPM.newWakeLock(
PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP,
"keyguard"); 具有ACQUIRE_CAUSE_WAKUPQ权限的唤醒锁,上层就是通过此锁来唤醒屏幕,接下来就是powermanager的流程了。
PowerManager.java acquire
mService.acquireWakeLock
PowermanagerService.java acquireWakeLock
acquireWakeLockLocked//此处会检查唤醒锁的标志位,作出对应的处理。
setPowerState //此函数为powermanager的核心函数之一,会对屏幕背光/唤醒,睡眠等作出相应的处理
setScreenStateLocked //此函数很关键
Power.setScreenState
power.java setScreenState
android_os_Power.cpp setScreenState
power.c set_screen_state//此函数作为上层的最后一个函数,会打印出标志性的log,*** set_screen_state %d,如果打出这个log,至少证明从APP-HAL都是在正常干活的,那么问题只能是kernel的了,贴出代码看看:
接下来的流程就是到了内核空间。
kernel/kernel/power/main.c state_store
//此函数被宏power_attr(state)声明为设备文件接口 sys/power/state,宏power_attr的定义为(power.h):
#define power_attr(_name) \
static struct kobj_attribute _name##_attr = {\
.attr = {\
.name = __stringify(_name),\
.mode = 0644, \
}, \
.show = _name##_show,\
.store = _name##_store, \
}
kernel/kernel/power/erlysuspend.c request_suspend_state
kernel/kernel/power/erlysuspend.c late_resume
二、 屏幕睡眠
和屏幕唤醒的过程很类似,如下:
inputReader.cpp KeyboardInputMapper::processKey
getDispatcher()->notifyKey
inputDispacher.cpp InputDispatcher::notifyKey
mPolicy->interceptKeyBeforeQueueing
com_android_server_inputManager.cpp NativeInputManager::interceptKeyBeforeQueueing
InputManager.java interceptKeyBeforeQueueing
mWindowManagerService.mInputMonitor.interceptKeyBeforeQueueing
WindowmanagerService.java InputMonitor::interceptKeyBeforeQueueing
mPolicy.interceptKeyBeforeQueueing
PhonewindowManager.java interceptKeyBeforeQueueing
//同上面的分析,此处返回的action是被或上了ACTION_GO_TO_SLEEP的(见1975行对KeyEvent.KEYCODE_POWER的处理).......一级一级的返回后.....com_android_server_inputManager.cpp NativeInputManager::interceptKeyBeforeQueueing //返回值中含有gotosleep的flag,故走到gotosleep分支
android_server_PowerManagerService_goToSleep
com_android_server_PowerManagerService.cpp android_server_PowerManagerService_goToSleep //同上面的inputmanager,此处也会调用到PowerManagerService的gotosleep,也是用register_android_server_PowerManagerService方法来对应起来。
env->CallVoidMethod(gPowerManagerServiceObj, gPowerManagerServiceClassInfo.goToSleep,
nanoseconds_to_milliseconds(eventTime));
PowermanagerService.java goToSleep
goToSleepWithReason
goToSleepLocked
setPowerState(SCREEN_OFF, false, reason);
setPowerState
setScreenStateLocked
Power.setScreenState(false)
power.java setScreenState
android_os_power.java setScreenState
power.c set_screen_state
kernel/kernel/power/main.c state_store
kernel/kernel/power/earlysuspend.c request_suspend_state //此处流程和唤醒大同小异,不在赘述
early_suspend
kernel/kernel/power/wakelock.c wake_unlock
接下来就是linux的suspend了,没有再仔细看过,惭愧惭愧。
总的来说,屏幕的睡眠是和上层的keyguard没有关系,是在WM和PMS以及相关的JNI的配合下对kernel的操作完成的。
屏幕唤醒和睡眠就写到这里,而背光的点亮过程,大部分处理是在PMS中,是在HAL层操作了lights的设备文件并不涉及到唤醒和睡眠,显得比较简单,有时间也写出来分享。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。