android 5.0的input init过程

来源:互联网 发布:linux内核编译过程 编辑:程序博客网 时间:2024/06/07 07:05
SystemServer.java:
public static void main(String[] args) {
        new SystemServer().run();
    }
到run():
private void run() {
.........
 try {
            startBootstrapServices();
            startCoreServices();
            startOtherServices();
        } catch (Throwable ex) {
            Slog.e("System", "******************************************");
            Slog.e("System", "************ Failure starting system services", ex);
            throw ex;
        }
.........
}

进入startOtherServices:
 private void startOtherServices() {
.........

inputManager = new InputManagerService(context); //来创建了Java层的input管理器,inputmanager是个                                                                                                            inputmanagerservice变量


.........
wm = WindowManagerService.main(context, inputManager,
                    mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL,
                    !mFirstBoot, mOnlyCore);
            ServiceManager.addService(Context.WINDOW_SERVICE, wm);
            ServiceManager.addService(Context.INPUT_SERVICE, inputManager);
            mActivityManagerService.setWindowManager(wm);
            inputManager.setWindowManagerCallbacks(wm.getInputMonitor());
            inputManager.start();
.........


}




到InputManagerService.java里
public InputManagerService(Context context) {
        this.mContext = context;
        this.mHandler = new InputManagerHandler(DisplayThread.get().getLooper());
        mUseDevInputEventForAudioJack =
                context.getResources().getBoolean(R.bool.config_useDevInputEventForAudioJack);
        Slog.i(TAG, "Initializing input manager, mUseDevInputEventForAudioJack="
                + mUseDevInputEventForAudioJack);
        mPtr = nativeInit(this, mContext, mHandler.getLooper().getQueue());  //这里是关键
        LocalServices.addService(InputManagerInternal.class, new LocalService());
    }

在com_android_server_input_InputManagerService.cpp里
static JNINativeMethod gInputManagerMethods[] = {
    /* name, signature, funcPtr */
    { "nativeInit",
            "(Lcom/android/server/input/InputManagerService;Landroid/content/Context;Landroid/os/MessageQueue;)J",          ////定义一个 JNINativeMethod然后在下面函数中注册
            (void*) nativeInit },
......
}

下面的函数:
int register_android_server_InputManager(JNIEnv* env) {
    int res = jniRegisterNativeMethods(env, "com/android/server/input/InputManagerService",
            gInputManagerMethods, NELEM(gInputManagerMethods));
.......
}

再看
static jlong nativeInit(JNIEnv* env, jclass clazz,
        jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
    sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
    if (messageQueue == NULL) {
        jniThrowRuntimeException(env, "MessageQueue is not initialized.");
        return 0;
    }


    NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
            messageQueue->getLooper());
    im->incStrong(0);
    return reinterpret_cast<jlong>(im);
}
再看
NativeInputManager::NativeInputManager(jobject contextObj,
        jobject serviceObj, const sp<Looper>& looper) :
        mLooper(looper), mInteractive(true) {
    JNIEnv* env = jniEnv();


    mContextObj = env->NewGlobalRef(contextObj);
    mServiceObj = env->NewGlobalRef(serviceObj);


    {
        AutoMutex _l(mLock);
        mLocked.systemUiVisibility = ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE;
        mLocked.pointerSpeed = 0;
        mLocked.pointerGesturesEnabled = true;
        mLocked.showTouches = false;
    }


    sp<EventHub> eventHub = new EventHub();           //创建了eventhub
    mInputManager = new InputManager(eventHub, this, this);//InputManager负责一些input的系统的初始化和正常工作时候一些管理工作。
}


EventHub是与kernel打交道的管理器 它处理了所有kernel传上来
的input事件并转换处理发送给framework层使用。  

inputmanager的初始化:
InputManager::InputManager(
        const sp<EventHubInterface>& eventHub,
        const sp<InputReaderPolicyInterface>& readerPolicy,
        const sp<InputDispatcherPolicyInterface>& dispatcherPolicy) {
    mDispatcher = new InputDispatcher(dispatcherPolicy);
    mReader = new InputReader(eventHub, readerPolicy, mDispatcher);//这里把Dispatcher和eventHub作为参数传了进去
    initialize();
}


回头看之前的 inputManager.start();


  public void start() {
        .......
        nativeStart(mPtr);
        ......
}


在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.");
    }
}


跑到InputManager.cpp终于看到两个线程都跑起来了
status_t InputManager::start() {
    status_t result = mDispatcherThread->run("InputDispatcher", PRIORITY_URGENT_DISPLAY);
    if (result) {
        ALOGE("Could not start InputDispatcher thread due to error %d.", result);
        return result;
    }


    result = mReaderThread->run("InputReader", PRIORITY_URGENT_DISPLAY);//调用了system/core/threads.cpp
    if (result) {
        ALOGE("Could not start InputReader thread due to error %d.", result);


        mDispatcherThread->requestExit();
        return result;
    }


    return OK;
}
这两个类并没有实现这个函数,因此他们会调用他们的父类public Thread里的 Run 方法我们来看这个
status_t Thread::run(const char* name, int32_t priority, size_t stack)
{
    Mutex::Autolock _l(mLock);


    if (mRunning) {
        // thread already started
        return INVALID_OPERATION;
    }
    mStatus = NO_ERROR;
    mExitPending = false;
    mThread = thread_id_t(-1);
    mHoldSelf = this;
    mRunning = true;
    bool res;
    if (mCanCallJava) {
        res = createThreadEtc(_threadLoop,  //这个_threadLoop很关键
                this, name, priority, stack, &mThread);
    } else {
        res = androidCreateRawThreadEtc(_threadLoop,
                this, name, priority, stack, &mThread);
    }
    
    if (res == false) {
        mStatus = UNKNOWN_ERROR;   // something happened!
        mRunning = false;
        mThread = thread_id_t(-1);
        mHoldSelf.clear();  // "this" may have gone away after this.


        return UNKNOWN_ERROR;
    }  
    return NO_ERROR;


    // Exiting scope of mLock is a memory barrier and allows new thread to run
}
然后
int Thread::_threadLoop(void* user)
{
    Thread* const self = static_cast<Thread*>(user);


    sp<Thread> strong(self->mHoldSelf);
    wp<Thread> weak(strong);
    self->mHoldSelf.clear();


#ifdef HAVE_ANDROID_OS
    // this is very useful for debugging with gdb
    self->mTid = gettid();
#endif


    bool first = true;


    do {
        bool result;
        if (first) {
            first = false;
            self->mStatus = self->readyToRun();
            result = (self->mStatus == NO_ERROR);


            if (result && !self->exitPending()) {
            
                result = self->threadLoop();   //最后调用threadLoop()
            }
        } else {
            result = self->threadLoop();//最后调用threadLoop()
        }


        // establish a scope for mLock
        {
        Mutex::Autolock _l(self->mLock);
        if (result == false || self->mExitPending) {
            self->mExitPending = true;
            self->mRunning = false;
           
            self->mThread = thread_id_t(-1);    
            self->mThreadExitedCondition.broadcast();
            break;
        }
        }
        
        // Release our strong reference, to let a chance to the thread
        // to die a peaceful death.
        strong.clear();
        // And immediately, re-acquire a strong reference for the next loop
        strong = weak.promote();
    } while(strong != 0);
    
    return 0;
}
所以最后调用了
InputReader.cpp:


bool InputReaderThread::threadLoop() {
    mReader->loopOnce();
    return true;
}


bool InputDispatcherThread::threadLoop() {
    mDispatcher->dispatchOnce();
    return true;
}
0 0
原创粉丝点击