代码路径
基于AndroidS(12.0)代码
system/core/libutils/Threads.cpp
frameworks/base/services
- java/com/android/server/SystemServer.java
- core
- java/com/android/server/input/InputManagerService.java
- jni/com_android_server_input_InputManagerService.cpp
frameworks/native/services/inputflinger/
- InputManager.cpp
- InputThread.cpp
- reader
- InputReader.cpp
- dispatcher
- InputDispatcher.cpp
启动流程图
从流程图可以看出,启动过程的有两条主线:create、start,下面分别针对这两条主线进行源码分析
create主线
create主线主要是创建 Input 系统相关对象,从 Java 层一直到 Native 层。
SystemServer启动
//=====> SystemServer.java
private void startOtherServices(@NonNull TimingsTraceAndSlog t) {
inputManager = new InputManagerService(context);
... ...
//将InputManagerService注册到ServiceManager,其名字为 input
ServiceManager.addService(Context.INPUT_SERVICE, inputManager,
/* allowIsolated= */ false, DUMP_FLAG_PRIORITY_CRITICAL);
... ..
inputManager.start();
}
创建InputManagerService
//=====> InputManagerService.java
public InputManagerService(Context context) {
// 运行在线程"android.display"
this.mHandler = new InputManagerHandler(DisplayThread.get().getLooper());
//初始化 NativeInputManager,并返回其句柄
mPtr = nativeInit(this, mContext, mHandler.getLooper().getQueue());
}
Jni.nativeInit
//=====> com_android_server_input_InputManagerService.cpp
//JNI原型 -> long nativeInit(InputManagerService, Context, MessageQueue);
static jlong nativeInit(JNIEnv* env, jclass /* clazz */,
jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
//android_os_MessageQueue.h中的方法,获取 MessageQueue 中保存的 NativeMessageQueue 对象
sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
//初始化 NativeInputManager
NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
messageQueue->getLooper());
im->incStrong(0);
return reinterpret_cast<jlong>(im);
}
创建 NativeInputManager
//=====> com_android_server_input_InputManagerService.cpp
NativeInputManager::NativeInputManager(jobject contextObj,
jobject serviceObj, const sp<Looper>& looper) :
mLooper(looper), mInteractive(true) {
InputManager* im = new InputManager(this, this);
mInputManager = im;
//注册 InputManager 到 SystemServer,服务名为 inputflinger
defaultServiceManager()->addService(String16("inputflinger"), im);
}
创建 InputManager
//=====> InputManager.cpp
//这里传入的readerPolicy、dispatcherPolicy 都是 NativeInputManager
InputManager::InputManager(
const sp<InputReaderPolicyInterface>& readerPolicy,
const sp<InputDispatcherPolicyInterface>& dispatcherPolicy) {
//创建 InputDispatcher
mDispatcher = createInputDispatcher(dispatcherPolicy);
//对事件分类,事件分发前必须经过的阶段
mClassifier = new InputClassifier(mDispatcher);
//创建 InputReader
mReader = createInputReader(readerPolicy, mClassifier);
}
创建 InputDispatcher
//=====> InputDispatcher.cpp
InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy)
: mPolicy(policy),
... ...
mCompatService(getCompatService()) {
mLooper = new Looper(false);
mReporter = createInputReporter();
mKeyRepeatState.lastKeyEntry = nullptr;
policy->getDispatcherConfiguration(&mConfig);
}
创建 InputReader
//=====> InputReader.cpp
InputReader::InputReader(std::shared_ptr<EventHubInterface> eventHub,
const sp<InputReaderPolicyInterface>& policy,
const sp<InputListenerInterface>& listener)
: mContext(this),
mEventHub(eventHub),
mPolicy(policy),
... ...
mConfigurationChangesToRefresh(0) {
mQueuedListener = new QueuedInputListener(listener);
.... ...
}
start主线
start主线会启动两条线程 InputReader(读取事件)、InputDispatcher(分发事件),并且这两条线程会一直循环执行,不会终止。
Jni.nativeStart
//=====> com_android_server_input_InputManagerService.cpp
//Jni原型 -> void nativeStart(long ptr),其 ptr 参数为 NativeInputManager 的句柄
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.");
}
}
InputManager.start
在创建 InputManager 的时候创建了 mDispatcher、mReader
//=====> InputManager.cpp
status_t InputManager::start() {
//开启 InputDispatcher Looper
status_t result = mDispatcher->start();
if (result) {
return result;
}
//开启 InputReader 的 Looper
result = mReader->start();
if (result) {
mDispatcher->stop();
return result;
}
return OK;
}
InputReader.start
创建一个InputThread线程,线程名为 InputReader
status_t InputReader::start() {
if (mThread) {
return ALREADY_EXISTS;
}
mThread = std::make_unique<InputThread>(
"InputReader", [this]() { loopOnce(); }, [this]() { mEventHub->wake(); });
return OK;
}
InputThread 会创建 InputThreadImpl ,InputThreadImpl 继承自 libutils 中的 Thread,其会一直运行threadLoop 函数,直到该函数返回 false 则会停止循环。结合上面的代码可以知道 InputReader 线程一直运行的是 loopOnce 方法,mEventHub->wake是在InputThread析构时候调用。
//=====> InputThread.cpp
class InputThreadImpl : public Thread {
public:
explicit InputThreadImpl(std::function<void()> loop)
: Thread(/* canCallJava */ true), mThreadLoop(loop) {}
private:
std::function<void()> mThreadLoop;
//返回true则会一直运行
bool threadLoop() override {
mThreadLoop();
return true;
}
};
InputThread::InputThread(std::string name, std::function<void()> loop, std::function<void()> wake)
: mName(name), mThreadWake(wake) {
mThread = new InputThreadImpl(loop);
//开始运行Thread
mThread->run(mName.c_str(), ANDROID_PRIORITY_URGENT_DISPLAY);
}
InputDispatcher.start
同样 InputDispatcher 也会启动一个线程,线程名为 InputDispatcher,然后一直运行 dispatchOnce 方法。
//=====> InputDispatcher.cpp
status_t InputDispatcher::start() {
if (mThread) {
return ALREADY_EXISTS;
}
mThread = std::make_unique<InputThread>(
"InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); });
return OK;
}
参考
- Input系统 - 启动篇