【Android 事件分发】事件分发源码分析 ( 驱动层通过中断传递事件 | WindowManagerService 向 View 层传递事件 )
Android 事件分發 系列文章目錄
【Android 事件分發】事件分發源碼分析 ( 驅動層通過中斷傳遞事件 | WindowManagerService 向 View 層傳遞事件 )
文章目錄
- Android 事件分發 系列文章目錄
- 一、事件分發脈絡
- 二、驅動層通過中斷傳遞事件
- 三、WindowManagerService 向 View 傳遞事件
一、事件分發脈絡
事件分發分析流程 :
① 驅動層 -> Framework 層 : 用戶觸摸 , 或按鍵 后 , 事件在硬件中產生 , 從 硬件驅動層 , 傳遞到 Framework 層 ;
② WMS -> View 層 : WindowManagerService ( 簡稱 WMS ) 將事件傳遞到 View 層 ;
③ View 層內部 : 事件在 View 的容器及下層容器 / 組件 之間傳遞 ;
二、驅動層通過中斷傳遞事件
硬件產生事件后 , 驅動層通過中斷傳遞事件 ;
中斷在嵌入式 Linux 中經常使用 , 分為 外部中斷 和 內部中斷 ;
- 外部中斷 : 由外部事件產生的中斷 , 如這里的由硬件觸摸 / 按鍵 產生的事件產生的中斷 ;
- 內部中斷 : 程序運行出現崩潰 , 異常 等情況 ;
中斷是指在 CPU 正常執行指令時 , 內部或外部事件 / 事先設置好的中斷操作 , 會引起 CPU 中斷當前正在執行的指令 , 轉而運行當前中斷對應的相關指令 , 中斷程序執行完畢后 , 繼續執行后續中斷前未執行完畢的代碼 ;
中斷有兩種方式 : 一種是輪詢的 , CPU 不斷讀取硬件的狀態 ; 另一種是硬件產生事件會后 , 發送信息給 CPU , 讓 CPU 暫停當前工作 , 執行中斷任務 ;
三、WindowManagerService 向 View 傳遞事件
在 【Android 應用開發】UI繪制流程 ( 生命周期機制 | 布局加載機制 | UI 繪制流程 | 布局測量 | 布局擺放 | 組件繪制 | 瀑布流布局案例 ) 博客中 , 分析了 UI 布局繪制流程 , 從 ActivityThread 開始 , 逐步調用 , 繪制 UI 界面 , 調用鏈如下 :
ActivityThread | handleResumeActivity -> WindowManager | addView -> ViewRootImpl | setView ;
最后在 ViewRootImpl 的 performTraversals 方法中 , 完成 測量 , 布局 , 繪畫 操作 ;
在 WindowManagerGlobal 中的 addView 方法的主要作用是添加 DecorView ;
各個窗口的層級如下 : 事件傳遞從 Activity 逐層向下傳遞的 View 組件上 ;
這里開始從 ViewRootImpl 的 setView 方法進行分析 ;
通過 new InputChannel() 直接創建輸入通道 ;
還調用了 WindowSession 的 addToDisplay 方法 , mWindowSession 成員是 IWindowSession 類型 , 通過 mWindowSession = WindowManagerGlobal.getWindowSession() 獲得 ;
WindowManagerGlobal 的 getWindowSession 方法中 , 最終 WindowSession 又調用回了 WMS 的 openSession , 創建了一個 WindowSession 對象 ;
在 ViewRootImpl 的 setView 方法中 , 注冊了 mInputEventReceiver , 傳入 InputChannel 和 Looper 參數 , InputChannel 就是事件傳入的通道 , Looper 用于輪詢事件是否發生 ;
ViewRootImpl 參考源碼 :
public final class ViewRootImpl implements ViewParent,View.AttachInfo.Callbacks, ThreadedRenderer.DrawCallbacks {public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {requestLayout();if ((mWindowAttributes.inputFeatures& WindowManager.LayoutParams.INPUT_FEATURE_NO_INPUT_CHANNEL) == 0) {mInputChannel = new InputChannel();}...res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,getHostVisibility(), mDisplay.getDisplayId(), mWinFrame,mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,mAttachInfo.mOutsets, mAttachInfo.mDisplayCutout, mInputChannel);...if (mInputChannel != null) {if (mInputQueueCallback != null) {mInputQueue = new InputQueue();mInputQueueCallback.onInputQueueCreated(mInputQueue);}mInputEventReceiver = new WindowInputEventReceiver(mInputChannel,Looper.myLooper());}} }/frameworks/base/core/java/android/view/ViewRootImpl.java
WindowManagerService 獲取 WindowSession 方法 : 通過調用 WindowManagerGlobal 的 getWindowSession 獲取 , 最終還是調用了 WindowManagerService 的 openSession 方法 ;
WindowManagerGlobal 參考源碼 :
public final class WindowManagerGlobal {public static IWindowSession getWindowSession() {synchronized (WindowManagerGlobal.class) {if (sWindowSession == null) {try {InputMethodManager imm = InputMethodManager.getInstance();IWindowManager windowManager = getWindowManagerService();sWindowSession = windowManager.openSession(new IWindowSessionCallback.Stub() {@Overridepublic void onAnimatorScaleChanged(float scale) {ValueAnimator.setDurationScale(scale);}},imm.getClient(), imm.getInputContext());} catch (RemoteException e) {throw e.rethrowFromSystemServer();}}return sWindowSession;}} }/frameworks/base/core/java/android/view/WindowManagerGlobal.java
在 WindowManagerService 的 addWindow 方法中 ,
- 初始化了 窗口的狀態 WindowState ,
- 通過調用 WindowState 的 openInputChannel 方法 , 設置了 InputChannel , 就是將 ViewRootImpl 中 setView 方法中 new InputChannel() 創建的 InputChannel 傳遞進來 ;
WindowManagerService 參考源碼 :
/** {@hide} */ public class WindowManagerService extends IWindowManager.Stubimplements Watchdog.Monitor, WindowManagerPolicy.WindowManagerFuncs {public int addWindow(Session session, IWindow client, int seq,LayoutParams attrs, int viewVisibility, int displayId, Rect outFrame,Rect outContentInsets, Rect outStableInsets, Rect outOutsets,DisplayCutout.ParcelableWrapper outDisplayCutout, InputChannel outInputChannel) {// 初始化窗口狀態 final WindowState win = new WindowState(this, session, client, token, parentWindow,appOp[0], seq, attrs, viewVisibility, session.mUid,session.mCanAddInternalSystemWindow);if (openInputChannels) {win.openInputChannel(outInputChannel);}}@Overridepublic IWindowSession openSession(IWindowSessionCallback callback, IInputMethodClient client,IInputContext inputContext) {if (client == null) throw new IllegalArgumentException("null client");if (inputContext == null) throw new IllegalArgumentException("null inputContext");Session session = new Session(this, callback, client, inputContext);return session;} }/frameworks/base/services/core/java/com/android/server/wm/WindowManagerService.java
WindowState 的 openInputChannel 方法 , 其中調用了 InputChannel.openInputChannelPair(name) 靜態方法 , 這是開啟了 222 個 InputChannel , inputChannels[0] 放在服務端 , inputChannels[1] 放在了客戶端 ;
服務端 與 客戶端 需要進行通信 , 二者通過 Looper 進行通信 , 通信前需要進行注冊 , 在 InputDispatcher.cpp 中進行的注冊 ;
WindowState 參考源碼 :
/** A window in the window manager. */ class WindowState extends WindowContainer<WindowState> implements WindowManagerPolicy.WindowState {void openInputChannel(InputChannel outInputChannel) {if (mInputChannel != null) {throw new IllegalStateException("Window already has an input channel.");}String name = getName();InputChannel[] inputChannels = InputChannel.openInputChannelPair(name);// 服務端mInputChannel = inputChannels[0];// 客戶端 mClientChannel = inputChannels[1];mInputWindowHandle.inputChannel = inputChannels[0];if (outInputChannel != null) {mClientChannel.transferTo(outInputChannel);mClientChannel.dispose();mClientChannel = null;} else {// If the window died visible, we setup a dummy input channel, so that taps// can still detected by input monitor channel, and we can relaunch the app.// Create dummy event receiver that simply reports all events as handled.mDeadWindowEventReceiver = new DeadWindowEventReceiver(mClientChannel);}mService.mInputManager.registerInputChannel(mInputChannel, mInputWindowHandle);} }/frameworks/base/services/core/java/com/android/server/wm/WindowState.java
下面分析在 InputDispatcher 中 , 注冊 服務端 與 客戶端 InputChannel 的過程 ;
在 registerInputChannel 方法中 , 創建了 Connection 連接 , 這就是兩個 服務端 與 客戶端 InputChannel 溝通的通道 , 每個 InputChannel 都有一個 fd , 通過 int fd = inputChannel->getFd() 獲取 fd , 調用 mConnectionsByFd.add(fd, connection) 將 fd 與 Connection 對應起來 ;
最后將 fd 注冊在 Looper 中 , mLooper->addFd ;
只要有任何事件輸入 , 該 Looper 就會被喚醒 , 通過 InputChannel 傳遞到 Activity , 進而傳遞給各個層級的 View 組件 ;
status_t InputDispatcher::registerInputChannel(const sp<InputChannel>& inputChannel,const sp<InputWindowHandle>& inputWindowHandle, bool monitor) { #if DEBUG_REGISTRATIONALOGD("channel '%s' ~ registerInputChannel - monitor=%s", inputChannel->getName().c_str(),toString(monitor)); #endif{ // acquire lockAutoMutex _l(mLock);if (getConnectionIndexLocked(inputChannel) >= 0) {ALOGW("Attempted to register already registered input channel '%s'",inputChannel->getName().c_str());return BAD_VALUE;}sp<Connection> connection = new Connection(inputChannel, inputWindowHandle, monitor);int fd = inputChannel->getFd();mConnectionsByFd.add(fd, connection);if (monitor) {mMonitoringChannels.push(inputChannel);}mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this);} // release lock// Wake the looper because some connections have changed.mLooper->wake();return OK; }/frameworks/native/services/inputflinger/InputDispatcher.cpp
總結
以上是生活随笔為你收集整理的【Android 事件分发】事件分发源码分析 ( 驱动层通过中断传递事件 | WindowManagerService 向 View 层传递事件 )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Android TV 开发】焦点处理
- 下一篇: 【Android 事件分发】事件分发源码