当前位置: 首页 > news >正文

InputReader与InputDispatcher关系 - android-15.0.0_r23

InputReader与InputDispatcher关系 - android-15.0.0_r23

  • 1、InputManager中创建
  • 2、InputDispatcher
  • 3、InputReader
  • 4、mTracingStages 链表
    • 4.1 链表执行
    • 4.2 不同InputListenerInterface功能

InputManagerService启动-Android12


1、InputManager中创建

  • JNIcom_android_server_input_InputManagerService.cpp)连接 InputManagerService.javaInputManager.cpp
  • InputManager.cpp中去创建InputReaderInputDispatcher,都会持有 JNINativeInputManager
  • JNINativeInputManager 继承 InputReaderPolicyInterfaceInputDispatcherPolicyInterfacePointerControllerPolicyInterfacePointerChoreographerPolicyInterfaceInputFilterPolicyInterface

frameworks/base/services/core/java/com/android/server/input/InputManagerService.java
frameworks/base/services/core/java/com/android/server/input/NativeInputManagerService.java

NativeImpl(InputManagerService service, MessageQueue messageQueue) {mPtr = init(service, messageQueue);
}

frameworks/base/services/core/jni/com_android_server_input_InputManagerService.cpp

static jlong nativeInit(JNIEnv* env, jclass /* clazz */, jobject serviceObj,jobject messageQueueObj) {sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);if (messageQueue == nullptr) {jniThrowRuntimeException(env, "MessageQueue is not initialized.");return 0;}static std::once_flag nativeInitialize;NativeInputManager* im = nullptr;std::call_once(nativeInitialize, [&]() {// Create the NativeInputManager, which should not be destroyed or deallocated for the// lifetime of the process.im = new NativeInputManager(serviceObj, messageQueue->getLooper());});LOG_ALWAYS_FATAL_IF(im == nullptr, "NativeInputManager was already initialized.");return reinterpret_cast<jlong>(im);
}NativeInputManager::NativeInputManager(jobject serviceObj, const sp<Looper>& looper): mLooper(looper) {JNIEnv* env = jniEnv();mServiceObj = env->NewGlobalRef(serviceObj);InputManager* im = new InputManager(this, *this, *this, *this);mInputManager = im;defaultServiceManager()->addService(String16("inputflinger"), im);
}

frameworks/native/services/inputflinger/InputManager.cpp

/*** The event flow is via the "InputListener" interface, as follows:*   InputReader*     -> UnwantedInteractionBlocker*     -> InputFilter*     -> PointerChoreographer*     -> InputProcessor*     -> InputDeviceMetricsCollector*     -> InputDispatcher*/
InputManager::InputManager(const sp<InputReaderPolicyInterface>& readerPolicy,InputDispatcherPolicyInterface& dispatcherPolicy,PointerChoreographerPolicyInterface& choreographerPolicy,InputFilterPolicyInterface& inputFilterPolicy) {mInputFlingerRust = createInputFlingerRust();mDispatcher = createInputDispatcher(dispatcherPolicy);mTracingStages.emplace_back(std::make_unique<TracedInputListener>("InputDispatcher", *mDispatcher));if (ENABLE_INPUT_FILTER_RUST) {mInputFilter = std::make_unique<InputFilter>(*mTracingStages.back(), *mInputFlingerRust,inputFilterPolicy);mTracingStages.emplace_back(std::make_unique<TracedInputListener>("InputFilter", *mInputFilter));}if (ENABLE_INPUT_DEVICE_USAGE_METRICS) {mCollector = std::make_unique<InputDeviceMetricsCollector>(*mTracingStages.back());mTracingStages.emplace_back(std::make_unique<TracedInputListener>("MetricsCollector", *mCollector));}mProcessor = std::make_unique<InputProcessor>(*mTracingStages.back());mTracingStages.emplace_back(std::make_unique<TracedInputListener>("InputProcessor", *mProcessor));mChoreographer =std::make_unique<PointerChoreographer>(*mTracingStages.back(), choreographerPolicy);mTracingStages.emplace_back(std::make_unique<TracedInputListener>("PointerChoreographer", *mChoreographer));mBlocker = std::make_unique<UnwantedInteractionBlocker>(*mTracingStages.back());mTracingStages.emplace_back(std::make_unique<TracedInputListener>("UnwantedInteractionBlocker", *mBlocker));mReader = createInputReader(readerPolicy, *mTracingStages.back());
}

2、InputDispatcher

  • mPolicy(policy) 持有 JNINativeInputManager与上层IMS沟通桥梁
  • mTraceruser版本上Perfetto日志
  • mLooper = sp<Looper>::make(false) 控制线程休眠/激活;上层mNative.start()启动InputDispatcher的线程执行 dispatchOnce(),并激活mLooper->wake()

frameworks/native/services/inputflinger/dispatcher/InputDispatcher.cpp

InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy): InputDispatcher(policy, createInputTracingBackendIfEnabled()) {}InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy,std::unique_ptr<trace::InputTracingBackendInterface> traceBackend): mPolicy(policy),mPendingEvent(nullptr),mLastDropReason(DropReason::NOT_DROPPED),mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER),mMinTimeBetweenUserActivityPokes(DEFAULT_USER_ACTIVITY_POKE_INTERVAL),mNextUnblockedEvent(nullptr),mMonitorDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT),mDispatchEnabled(false),mDispatchFrozen(false),mInputFilterEnabled(false),mMaximumObscuringOpacityForTouch(1.0f),mFocusedDisplayId(ui::LogicalDisplayId::DEFAULT),mWindowTokenWithPointerCapture(nullptr),mAwaitedApplicationDisplayId(ui::LogicalDisplayId::INVALID),mInputEventTimelineProcessor(input_flags::enable_per_device_input_latency_metrics()? std::move(std::unique_ptr<InputEventTimelineProcessor>(new LatencyAggregatorWithHistograms())): std::move(std::unique_ptr<InputEventTimelineProcessor>(new LatencyAggregator()))),mLatencyTracker(*mInputEventTimelineProcessor) {mLooper = sp<Looper>::make(false);mReporter = createInputReporter();mWindowInfoListener = sp<DispatcherWindowListener>::make(*this);
#if defined(__ANDROID__)SurfaceComposerClient::getDefault()->addWindowInfosListener(mWindowInfoListener);
#endifmKeyRepeatState.lastKeyEntry = nullptr;if (traceBackend) {mTracer = std::make_unique<trace::impl::InputTracer>(std::move(traceBackend));}mLastUserActivityTimes.fill(0);
}status_t InputDispatcher::start() {if (mThread) {return ALREADY_EXISTS;}mThread = std::make_unique<InputThread>("InputDispatcher", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); },/*isInCriticalPath=*/true);return OK;
}

3、InputReader

  • mPolicy(policy) 持有 JNINativeInputManager与上层IMS沟通桥梁
  • mEventHub(eventHub) 获取底层input事件;控制start()启动的线程的休眠/激活
  • mNextListener(listener) 连接InputDispatcher,并新增了 mTracingStages 链表处理

frameworks/native/services/inputflinger/reader/InputReader.cpp

InputReader::InputReader(std::shared_ptr<EventHubInterface> eventHub,const sp<InputReaderPolicyInterface>& policy,InputListenerInterface& listener): mContext(this),mEventHub(eventHub),mPolicy(policy),mNextListener(listener),mKeyboardClassifier(std::make_unique<KeyboardClassifier>()),mGlobalMetaState(AMETA_NONE),mLedMetaState(AMETA_NONE),mGeneration(1),mNextInputDeviceId(END_RESERVED_ID),mDisableVirtualKeysTimeout(LLONG_MIN),mNextTimeout(LLONG_MAX),mConfigurationChangesToRefresh(0) {refreshConfigurationLocked(/*changes=*/{});updateGlobalMetaStateLocked();
}status_t InputReader::start() {if (mThread) {return ALREADY_EXISTS;}mThread = std::make_unique<InputThread>("InputReader", [this]() { loopOnce(); }, [this]() { mEventHub->wake(); },/*isInCriticalPath=*/true);return OK;
}

4、mTracingStages 链表

代码注释:The event flow is via the "InputListener" interface, as follows: InputReader -> UnwantedInteractionBlocker -> InputFilter -> PointerChoreographer -> InputProcessor -> InputDeviceMetricsCollector -> InputDispatcher

  • InputReadermNextListener(listener)持有链表head UnwantedInteractionBlocker
  • 当获取到事件后 mNextListener.notify(args) 执行链表mTracingStages

实际链表:

UnwantedInteractionBlocker
PointerChoreographer
InputProcessor
InputDeviceMetricsCollector
InputFilter
InputDispatcher

frameworks/native/services/inputflinger/InputManager.cpp

/*** The event flow is via the "InputListener" interface, as follows:*   InputReader*     -> UnwantedInteractionBlocker*     -> InputFilter*     -> PointerChoreographer*     -> InputProcessor*     -> InputDeviceMetricsCollector*     -> InputDispatcher*/
InputManager::InputManager(const sp<InputReaderPolicyInterface>& readerPolicy,InputDispatcherPolicyInterface& dispatcherPolicy,PointerChoreographerPolicyInterface& choreographerPolicy,InputFilterPolicyInterface& inputFilterPolicy) {mInputFlingerRust = createInputFlingerRust();mDispatcher = createInputDispatcher(dispatcherPolicy);mTracingStages.emplace_back(std::make_unique<TracedInputListener>("InputDispatcher", *mDispatcher));if (ENABLE_INPUT_FILTER_RUST) {mInputFilter = std::make_unique<InputFilter>(*mTracingStages.back(), *mInputFlingerRust,inputFilterPolicy);mTracingStages.emplace_back(std::make_unique<TracedInputListener>("InputFilter", *mInputFilter));}if (ENABLE_INPUT_DEVICE_USAGE_METRICS) {mCollector = std::make_unique<InputDeviceMetricsCollector>(*mTracingStages.back());mTracingStages.emplace_back(std::make_unique<TracedInputListener>("MetricsCollector", *mCollector));}mProcessor = std::make_unique<InputProcessor>(*mTracingStages.back());mTracingStages.emplace_back(std::make_unique<TracedInputListener>("InputProcessor", *mProcessor));mChoreographer =std::make_unique<PointerChoreographer>(*mTracingStages.back(), choreographerPolicy);mTracingStages.emplace_back(std::make_unique<TracedInputListener>("PointerChoreographer", *mChoreographer));mBlocker = std::make_unique<UnwantedInteractionBlocker>(*mTracingStages.back());mTracingStages.emplace_back(std::make_unique<TracedInputListener>("UnwantedInteractionBlocker", *mBlocker));mReader = createInputReader(readerPolicy, *mTracingStages.back());
}

4.1 链表执行

  • 根据 NotifyArgs 事件类型调用对应方法。这里使用 std::visit 配合 lambda 访问者(Visitor) 来处理 std::variant 类型的事件分发。实质就是检查generalArgs 参数类型调用对应方法。
  • 按照任务链表mTracingStages依次执行。

frameworks/native/services/inputflinger/include/InputListener.h
frameworks/native/services/inputflinger/InputListener.cpp

// Helper to std::visit with lambdas.
template <typename... V>
struct Visitor : V... { using V::operator()...; };
// explicit deduction guide (not needed as of C++20)
template <typename... V>
Visitor(V...) -> Visitor<V...>;void InputListenerInterface::notify(const NotifyArgs& generalArgs) {Visitor v{[&](const NotifyInputDevicesChangedArgs& args) { notifyInputDevicesChanged(args); },[&](const NotifyKeyArgs& args) { notifyKey(args); },[&](const NotifyMotionArgs& args) { notifyMotion(args); },[&](const NotifySwitchArgs& args) { notifySwitch(args); },[&](const NotifySensorArgs& args) { notifySensor(args); },[&](const NotifyVibratorStateArgs& args) { notifyVibratorState(args); },[&](const NotifyDeviceResetArgs& args) { notifyDeviceReset(args); },[&](const NotifyPointerCaptureChangedArgs& args) { notifyPointerCaptureChanged(args); },};std::visit(v, generalArgs);
}

4.2 不同InputListenerInterface功能

InputListenerInterface功能
UnwantedInteractionBlockerUnwantedInteractionBlocker接口的实现。表示输入处理的一个单独阶段。所有输入事件都经过这个阶段。充当除运动事件之外的所有输入事件的直通。运动类型的事件被发送到PalmRejectorsPalmRejector检测不需要的触摸,并在删除错误指针的情况下发出输入流。
PointerChoreographerPointerChrographer管理系统显示的用于输入交互的图标。这包括显示鼠标光标、手写笔悬停图标和触摸点。它负责累积鼠标光标的位置,并在必要时为传入事件填充光标位置。
InputProcessorInputProcessor接口的实现。表示输入处理的一个单独阶段。所有输入事件都经过这个阶段。充当除运动事件之外的所有输入事件的直通。运动类型的事件被发送到MotionClassifier。
InputDeviceMetricsCollector为测试而注入的度量收集器的日志接口。ENABLE_INPUT_DEVICE_USAGE_METRICS开关控制
InputFilterInputFilterC++组件被设计为rust实现的包装器。ENABLE_INPUT_FILTER_RUST开关控制
InputDispatcherInputEvent 分发给目标窗口
http://www.dtcms.com/a/446180.html

相关文章:

  • 基于Android Framework的C/C++开发实战
  • 个人主页网站制作教程营销策划的六个步骤
  • 第7章树和二叉树:二叉树的定义和性质
  • 网站建设首选玖艺建站信得过wordpress企业主题下载
  • TDengine 比较函数 IFNULL 用户手册
  • 【2026计算机毕业设计】基于jsp的毕业论文管理系统
  • 最小二乘问题详解3:线性最小二乘实例
  • OneData:数据驱动与AI落地的统一数据底座方法论——从规范到实践的全链路拆解
  • 与众不同的网站wordpress内容批量替换
  • 自己做网站要买什么微信制作网站设计
  • 笔记·线性回归(属于监督学习)
  • 同国外做贸易的网站怎么查看网站是用什么系统做的
  • 打印机专题
  • Vue 虚拟列表实现方案详解:三种方法的完整对比与实践
  • Oracle OCP认证考试题目详解082系列第48题
  • 第一章:单例模式 - 武林中的孤高剑客
  • sql题目基础50题
  • 哪些网站做的最好网站建设功能报
  • 第十三章:眼观六路,耳听八方——Observer的观察艺术
  • Kubernetes集群安全机制
  • 建站行业的发展趋势网站建设网络
  • AI大事记9:从 AlexNet 到 ChatGPT——深度学习的十年跃迁(下)
  • 网站收录了但是搜索不到全网霸屏推广系统
  • 张量分解 | CP / Tucker / BTD
  • 网站推广及建设ppt河北网站建设企业
  • 【数据结构】二叉搜索树的递归与非递归实现
  • 九亭镇村镇建设办官方网站1688接代加工订单
  • GJOI 9.27/10.3 题解
  • Python实例入门
  • 多线程核心知识点与高并发应用指南