android surfaceflinger(1)-启动初始化1

  从本篇文章起,将对Android display系统框架进行分析,分析Android display必然涉及到surfaceflinger,故先分析一下,surfaceflinger的启动初始化流程。若文中有什么分析不对的,希望大家多多指出分享。谢谢!

1、surfaceflinger启动触发

  surfaceflinger是Android系统的一个重要后台本地服务,它是有init进程去启动的。

1.1 祖先进程init入口

system/core/init/init.cpp

int main(int argc, char** argv) {
.......
    init_parse_config_file("/init.rc");//解析init.rc文件
    action_for_each_trigger("late-init",action_add_queue_tail);//启动服务
    return 0;
}

1.2 init.rc解析

在init.rc文件中,late-init->trigger boot-> class_start->surfaceflinger中的class core。依赖关系如下所示:

on late-init
    trigger early-fs
    trigger fs
    trigger post-fs
    trigger early-boot
    trigger boot  

on boot
    class_start core

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

在1.1节中,init_parse_config_file函数对init.rc解析完成后会将service类放在一个队列中,后面执行action_for_each_trigger(“late-init”,action_add_queue_tail)就会去启动对应的服务。

2、surfaceflinger进程启动

时序图如下:
android surfaceflinger(1)-启动初始化1_第1张图片
  当init进程触发启动surfaceflinger服务时,系统就会fork一个surfaceflinger进程;进程入口为:
framwork/native/services/surfaceflinger/main_surfaceflinger.cpp

int main(int, char**) {
    //限制binder 线程池最多可有4个线程
    ProcessState::self()->setThreadPoolMaxThreadCount(4);
    ALOGD("=zsx=  start surfaceflinger");
    // start the thread pool
    sp<ProcessState> ps(ProcessState::self());
    ps->startThreadPool();

    // 创建surfaceflinger实例
    sp<SurfaceFlinger> flinger = new SurfaceFlinger();//见2.1节

    // initialize before clients can connect
    flinger->init();//见2.3节

    // publish surface flinger
    sp<IServiceManager> sm(defaultServiceManager());
    sm->addService(String16(SurfaceFlinger::getServiceName()), flinger, false);//加入到ServiceManager服务管理器

    // run in this thread
    flinger->run();
    return 0;
}

2.1 创建surfaceflinger实例

new surfaceflinger时会去调用构造函数,第一次创建时还会执行onFirstRef();

SurfaceFlinger::SurfaceFlinger()
    :   BnSurfaceComposer(),
        mTransactionFlags(0),
        mTransactionPending(false),
        mAnimTransactionPending(false),
        mLayersRemoved(false),
        mRepaintEverything(0),
        mRenderEngine(NULL),
        mBootTime(systemTime()),
        mVisibleRegionsDirty(false),
        mHwWorkListDirty(false),
        mAnimCompositionPending(false),
        mDebugRegion(0),
        mDebugDDMS(0),
        mDebugDisableHWC(0),
        mDebugDisableTransformHint(0),
        mDebugInSwapBuffers(0),
        mLastSwapBufferTime(0),
        mDebugInTransaction(0),
        mLastTransactionTime(0),
        mBootFinished(false),
        mForceFullDamage(false),
        mPrimaryHWVsyncEnabled(false),
        mHWVsyncAvailable(false),
        mDaltonize(false),
        mHasColorMatrix(false),
        mHasPoweredOff(false),
        mFrameBuckets(),
        mTotalTime(0),
        mLastSwapTime(0),
        mDebugFps(0)
{
    ALOGI("SurfaceFlinger is starting");

    // debugging stuff...
    char value[PROPERTY_VALUE_MAX];

    property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
    mGpuToCpuSupported = !atoi(value);

    property_get("debug.sf.drop_missed_frames", value, "0");
    mDropMissedFrames = atoi(value);

    property_get("debug.sf.showupdates", value, "0");
    mDebugRegion = atoi(value);

    property_get("debug.sf.ddms", value, "0");
    mDebugDDMS = atoi(value);
    if (mDebugDDMS) {
        if (!startDdmConnection()) {
            // start failed, and DDMS debugging not enabled
            mDebugDDMS = 0;
        }
    }

    property_get("debug.sf.showfps", value, "0");
    mDebugFps = atoi(value);

    ALOGI_IF(mDebugRegion, "showupdates enabled");
    ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
    ALOGI_IF(mDebugFps,  "showfps enabled");
}

void SurfaceFlinger::onFirstRef()
{
    mEventQueue.init(this);//见2.2节
}

2.2 消息队列初始化

void MessageQueue::init(const sp& flinger)
{
    mFlinger = flinger;
    mLooper = new Looper(true);//实例化Looper
    mHandler = new Handler(*this);//创建消息MessageQueue处理者Handler
}

2.3 surfaceflinger类初始化

void SurfaceFlinger::init() {
    Mutex::Autolock _l(mStateLock);

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

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

    // 实例化硬件抽象层
    mHwc = new HWComposer(this,
            *static_cast(this));

    // get a RenderEngine for the given display / config (can't fail)
    mRenderEngine = RenderEngine::create(mEGLDisplay, mHwc->getVisualID());

    // retrieve the EGL context that was selected/created
    mEGLContext = mRenderEngine->getEGLContext();

    LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
            "couldn't create EGLContext");

    // initialize our non-virtual displays
    for (size_t i=0 ; i// set-up the displays that are already connected
        if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
            // All non-virtual displays are currently considered secure.
            bool isSecure = true;
            createBuiltinDisplayLocked(type);
            wp token = mBuiltinDisplays[i];

            sp producer;
            sp consumer;
            BufferQueue::createBufferQueue(&producer, &consumer,
                    new GraphicBufferAlloc());

            sp fbs = new FramebufferSurface(*mHwc, i,
                    consumer);
            int32_t hwcId = allocateHwcDisplayId(type);
            sp hw = new DisplayDevice(this,
                    type, hwcId, mHwc->getFormat(hwcId), isSecure, token,
                    fbs, producer,
                    mRenderEngine->getEGLConfig());
            if (i > DisplayDevice::DISPLAY_PRIMARY) {
                hw->setPowerMode(HWC_POWER_MODE_NORMAL);
            }
            mDisplays.add(token, hw);
        }
    }

    // make the GLContext current so that we can create textures when creating Layers
    // (which may happens before we render something)
    getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);

    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();
}

SurfaceFlinger::init()里面做了很多初始化操作,后面将逐步分析。未完待续。。。

你可能感兴趣的:(Android,C++,C,Android,surface显示系统源码分析)