android L 的surfaceflinger服务启动分析

android L 与android 4.4相同,surfaceflinger直接由init启动,不是system server。




init进程根据init.rc启动surfaceflinger服务

service surfaceflinger /system/bin/surfaceflinger
    class core
    user system
    group graphics drmrpc
    onrestart restart zygote

surfaceflinger进程的main函数


frameworks/native/services/surfaceflinger/main_surfaceflinger.cpp

int main(int, char**) {
...

    // instantiate surfaceflinger
    sp<SurfaceFlinger> flinger = new SurfaceFlinger();//实例化SurfaceFlinger

...

    // initialize before clients can connect
    flinger->init();//初始化

    // publish surface flinger
    sp<IServiceManager> sm(defaultServiceManager());
    sm->addService(String16(SurfaceFlinger::getServiceName()), flinger, false);//向service manager注册

    // run in this thread
    flinger->run();//run起来

    return 0;
}

首先实例化SurfaceFlinger类,然后调用类的成员t函数init初始化,最后调用类的成员函数run等待消息。


init()函数实现


frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp

void SurfaceFlinger::init() {
...

    // initialize EGL for the default display
    mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    eglInitialize(mEGLDisplay, NULL, NULL);

    // Initialize the H/W composer object.  There may or may not be an
    // actual hardware composer underneath.
    mHwc = new HWComposer(this,
            *static_cast<HWComposer::EventHandler *>(this));

...

    // start the EventThread
    sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
            vsyncPhaseOffsetNs, true, "app");
    mEventThread = new EventThread(vsyncSrc);
    sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
            sfVsyncPhaseOffsetNs, true, "sf");
    mSFEventThread = new EventThread(sfVsyncSrc);
    mEventQueue.setEventThread(mSFEventThread);

    mEventControlThread = new EventControlThread(this);
    mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);

    // set a fake vsync period if there is no HWComposer
    if (mHwc->initCheck() != NO_ERROR) {
        mPrimaryDispSync.setPeriod(16666667);
    }

    // initialize our drawing state
    mDrawingState = mCurrentState;

    // set initial conditions (e.g. unblank default device)
    initializeDisplays();

    // start boot animation
    startBootAnim();
}

这个函数挺长,首先初始化open GL,初始化H/W composer,初始化消息thread,启动开机动画。

run()函数实现如下


void SurfaceFlinger::run() {
    do {
        waitForEvent();
    } while (true);
}


在这里一直等待消息。


你可能感兴趣的:(android L 的surfaceflinger服务启动分析)