Android13 InputManagerService createInputChannel流程分析

InputManagerService的createInputChannel方法用于创建输入Channel,代码如下:

//frameworks/base/services/core/java/android/server/input/InputManagerService.java
public class InputManagerService extends IInputManager.Stub
        implements Watchdog.Monitor {
    private final NativeInputManagerService mNative;
    public InputChannel createInputChannel(String name) {
        return mNative.createInputChannel(name);
    }
}

调用NativeInputManagerService的createInputChannel方法,NativeInputManagerService是一个接口,由NativeImpl实现,createInputChannel是一个Native方法,在com_android_server_input_inputManagerService.cpp中实现:

//frameworks/base/services/core/jni/com_android_server_input_inputManagerService.cpp
static jobject nativeCreateInputChannel(JNIEnv* env, jobject nativeImplObj, jstring nameObj) {
    NativeInputManager* im = getNativeInputManager(env, nativeImplObj);


    ScopedUtfChars nameChars(env, nameObj);
    std::string name = nameChars.c_str();


    base::Result<std::unique_ptr<InputChannel>> inputChannel = im->createInputChannel(name);


    if (!inputChannel.ok()) {
        std::string message = inputChannel.error().message();
        message += StringPrintf(" Status=%d", static_cast<int>(inputChannel.error().code()));
        jniThrowRuntimeException(env, message.c_str());
        return nullptr;
    }


    jobject inputChannelObj =
            android_view_InputChannel_createJavaObject(env, std::move(*inputChannel));
    if (!inputChannelObj) {
        return nullptr;
    }


    android_view_InputChannel_setDisposeCallback(env, inputChannelObj,
            handleInputChannelDisposed, im);
    return inputChannelObj;
}

调用NativeInputManager的createInputChannel方法:

//frameworks/base/services/core/jni/com_android_server_input_inputManagerService.cpp
sp<InputManagerInterface> mInputManager;
base::Result<std::unique_ptr<InputChannel>> NativeInputManager::createInputChannel(
        const std::string& name) {
    ATRACE_CALL();
    return mInputManager->getDispatcher().createInputChannel(name);
}

InputDispatcher createInputChannel

调用mInputManager(InputManagerInterface)的getDispatcher方法,取得InputDispatcherInterface,然后调用InputDispatcherInterface的createInputChannel方法,由InputDispatcher实现:

//frameworks/native/services/inputflinger/dispatcher/InputDispatcher.cpp
Result<std::unique_ptr<InputChannel>> InputDispatcher::createInputChannel(const std::string& name) {
    if (DEBUG_CHANNEL_CREATION) {
        ALOGD("channel '%s' ~ createInputChannel", name.c_str());
    }


    std::unique_ptr<InputChannel> serverChannel;
    std::unique_ptr<InputChannel> clientChannel;
    // 将serverChannel(接收端) 与 clientChannel(发送端) 绑定
    status_t result = InputChannel::openInputChannelPair(name, serverChannel, clientChannel);


    if (result) {
        return base::Error(result) << "Failed to open input channel pair with name " << name;
    }


    { // acquire lock
        std::scoped_lock _l(mLock);
        const sp<IBinder>& token = serverChannel->getConnectionToken();
        int fd = serverChannel->getFd();
        sp<Connection> connection =
                new Connection(std::move(serverChannel), false /*monitor*/, mIdGenerator);


        if (mConnectionsByToken.find(token) != mConnectionsByToken.end()) {
            ALOGE("Created a new connection, but the token %p is already known", token.get());
        }
        mConnectionsByToken.emplace(token, connection);


         // 注册回调 handleReceiveCallback 方法
        std::function<int(int events)> callback = std::bind(&InputDispatcher::handleReceiveCallback,
                                                            this, std::placeholders::_1, token);


// 将callback 与 FD(即serverChannel) 关联起来
        mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, new LooperEventCallback(callback), nullptr);
    } // release lock


    // Wake the looper because some connections have changed.
    mLooper->wake();
    return clientChannel;
}

InputDispatcher handleReceiveCallback

当有输入事件时就会调用InputDispatcher的handleReceiveCallback方法:

待更新

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值