Android输入系统分为Java和Native部分,其中初始化是从Java部分开始的,然后再完成Native部分的初始化。
代码位置:frameworks/base/services/java/com/android/server/SystemServer.java startOtherServices
t.traceBegin("StartInputManagerService");
inputManager = new InputManagerService(context);
t.traceEnd();
...
wm = WindowManagerService.main(context, inputManager, !mFirstBoot, mOnlyCore,
new PhoneWindowManager(), mActivityManagerService.mActivityTaskManager);
...
ServiceManager.addService(Context.INPUT_SERVICE, inputManager,
/* allowIsolated= */ false, DUMP_FLAG_PRIORITY_CRITICAL);
...
inputManager.setWindowManagerCallbacks(wm.getInputManagerCallback());
inputManager.start();
- 创建IMS的对象
- 添加IMS对象到ServiceManager
- 设置向WMS发起回调的callback对象
- 启动IMS
创建IMS的对象
public InputManagerService(Context context) {
this.mContext = context;
this.mHandler = new InputManagerHandler(DisplayThread.get().getLooper());
...
mPtr = nativeInit(this, mContext, mHandler.getLooper().getQueue());
...
}
- 使用DisplayThread的Looper创建InputManagerHandler。即InputManagerHandler将运行在DisplayThread线程。
- 初始化Native层的IMS。每一个分为Java和Native两部分的对象在创建时都会有一个nativeInit函数。
private static native long nativeInit(InputManagerService service,
Context context, MessageQueue messageQueue);
NativeInputManager对象的创建
代码位置:frameworks/base/services/core/jni/com_android_server_input_InputManagerService.cpp
static jlong nativeInit(JNIEnv* env, jclass /* clazz */,
jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
if (messageQueue == nullptr) {
jniThrowRuntimeException(env, "MessageQueue is not initialized.");
return 0;
}
NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
messageQueue->getLooper());
im->incStrong(0);
return reinterpret_cast<jlong>(im);
}
- 获取Java层的messageQueue对象的引用
- 创建NativeInputManager对象,此对象是Native层组件与Java层IMS进行通信的桥梁
- 返回NativeInputManager对象的指针给Java层的IMS,IMS将其保存在mPtr成员变量中
class NativeInputManager : public virtual RefBase,
public virtual InputReaderPolicyInterface,
public virtual InputDispatcherPolicyInterface,
public virtual PointerControllerPolicyInterface
NativeInputManager对象继承自InputReaderPolicyInterface、InputDispatcherPolicyInterface、PointerControllerPolicyInterface,可知它们是由NativeInputManager实现的,但是这种实现仅仅只是接口上的实现,并不是策略的实际实现者,NativeInputManager通过JNI回调Java层的IMS,由它完成决策。
NativeInputManager::NativeInputManager(jobject contextObj,
jobject serviceObj, const sp<Looper>& looper) :
mLooper(looper), mInteractive(true) {
JNIEnv* env = jniEnv();
mServiceObj = env->NewGlobalRef(serviceObj);
{
AutoMutex _l(mLock);
mLocked.systemUiLightsOut = false;
mLocked.pointerSpeed = 0;
mLocked.pointerGesturesEnabled = true;
mLocked.showTouches = false;
mLocked.pointerCapture = false;
mLocked.pointerDisplayId = ADISPLAY_ID_DEFAULT;
}
mInteractive = true;
InputManager* im = new InputManager(this, this);
mInputManager = im;
defaultServiceManager()->addService(String16("inputflinger"), im);
}
NativeInputManager又创建了InputManager对象。
InputManager对象的创建
代码位置:frameworks/native/services/inputflinger/InputManager.cpp
InputManager::InputManager(
const sp<InputReaderPolicyInterface>& readerPolicy,
const sp<InputDispatcherPolicyInterface>& dispatcherPolicy) {
mDispatcher = createInputDispatcher(dispatcherPolicy);
mClassifier = new InputClassifier(mDispatcher);
mReader = createInputReader(readerPolicy, mClassifier);
}
sp<InputReaderInterface> mReader;
sp<InputClassifierInterface> mClassifier;
sp<InputDispatcherInterface> mDispatcher;
InputManager创建了InputReader、InputClassifier、InputDispatcher对象。注意:这里的dispatcherPolicy和readerPolicy都是NativeInputManager。
createInputDispatcher
代码位置:frameworks/native/services/inputflinger/dispatcher/InputDispatcherFactory.cpp
sp<InputDispatcherInterface> createInputDispatcher(
const sp<InputDispatcherPolicyInterface>& policy) {
return new android::inputdispatcher::InputDispatcher(policy);
}
createInputReader
代码位置:frameworks/native/services/inputflinger/reader/InputReaderFactory.cpp
sp<InputReaderInterface> createInputReader(const sp<InputReaderPolicyInterface>& policy,
const sp<InputListenerInterface>& listener) {
return new InputReader(std::make_unique<EventHub>(), policy, listener);
}
注意:这里通过make_unique创建了EventHub对象。
IMS的启动和运行
回到inputManager.start();
public void start() {
Slog.i(TAG, "Starting input manager");
nativeStart(mPtr);
...
}
private static native void nativeStart(long ptr);
在IMS中调用JNI函数nativeStart. 函数的代码位置: frameworks/base/services/core/jni/com_android_server_input_InputManagerService.cpp
static void nativeStart(JNIEnv* env, jclass /* clazz */, jlong ptr) {
NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
status_t result = im->getInputManager()->start();
if (result) {
jniThrowRuntimeException(env, "Input manager could not be started.");
}
}
此时获取之前保存的NativeInputManager对象,通过NativeInputManager对象获取InputManager对象,然后再调用其start函数。
status_t InputManager::start() {
status_t result = mDispatcher->start();
if (result) {
ALOGE("Could not start InputDispatcher thread due to error %d.", result);
return result;
}
result = mReader->start();
if (result) {
ALOGE("Could not start InputReader due to error %d.", result);
mDispatcher->stop();
return result;
}
return OK;
}
在InputManager的start函数中又执行了mDispatcher和mReader的start方法。
先看InputReader的start方法:
status_t InputReader::start() {
if (mThread) {
return ALREADY_EXISTS;
}
mThread = std::make_unique<InputThread>(
"InputReader", [this]() { loopOnce(); }, [this]() { mEventHub->wake(); });
return OK;
}
在这里又创建了InputReader线程。当线程启动的时候执行loopOnce函数,当线程结束时执行mEventHub->wake()。
status_t InputDispatcher::start() {
if (mThread) {
return ALREADY_EXISTS;
}
mThread = std::make_unique<InputThread>(
"InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); });
return OK;
}
在这里又创建了InputDispatcher线程。当线程启动时执行dispatchOnce函数,当线程结束时执行mLooper->wake()。
总结
输入系统结构体系.png三个线程,三台水泵.png
网友评论