随笔-118  评论-133  文章-4  trackbacks-0
参考资料:
1、“Android Display System --- Surface Flinger”
2、“Android核心分析(26) ----- Android GDI之SurfaceFlinger”
3、“Android SurfaceFlinger中的SharedClient -- 客户端(Surface)和服务端(Layer)之间的显示缓冲区管理”
4、“Android SurfaceFlinger中的工作线程:threadLoop()”


一、           surfaceflinger server如何启动:

 

1、 System_init.cpp

extern "C" status_t system_init()

{

       ……..

    char propBuf[PROPERTY_VALUE_MAX];

    property_get("system_init.startsurfaceflinger", propBuf, "1");

    if (strcmp(propBuf, "1") == 0) {

        // Start the SurfaceFlinger

        SurfaceFlinger::instantiate();         //实例化

}    

……..

}

 

2、 SurfaceFlinger.cpp

void SurfaceFlinger::instantiate() {

    defaultServiceManager()->addService(

            String16("SurfaceFlinger"), new SurfaceFlinger());     //添加SurfaceFlinger服务

}

 

3、【Threads.cpp

int Thread::_threadLoop(void* user)

{

       ……..

       self->mStatus = self->readyToRun();

       ……..

       result = self->threadLoop();

       ……..

 

4、【SurfaceFlinger.cpp

status_t SurfaceFlinger::readyToRun()

{

       ……..

}

 

5、【SurfaceFlinger.cpp

bool SurfaceFlinger::threadLoop()

{

       ……..

}

 

二、上层如何与底层SurfaceFlinger建立联系

 

1为应用程序创建一个 Client (用于管理和通信)

【SurfaceComposerClient.cpp】
SurfaceComposerClient::SurfaceComposerClient()
{
    sp<ISurfaceComposer> sm(getComposerService());
    if (sm == 0) {
        _init(0, 0);
        return;
    }

    _init(sm, sm->createConnection());

    if (mClient != 0) {
        Mutex::Autolock _l(gLock);
        VERBOSE("Adding client %p to map", this);
        gActiveConnections.add(mClient->asBinder(), this);
    }
}

 

注:

   经过Binder,最终调用SurfaceFlinger的createConnection实现,这里不作详细介绍,有兴趣请看参考:

“Android 核心分析 之六 -----IPC框架分析 Binder,Service,Service manager”
http://blog.csdn.net/maxleng/article/details/5490770

 



【SurfaceFlinger.cpp】

sp<ISurfaceFlingerClient> SurfaceFlinger::createConnection()

{

    Mutex::Autolock _l(mStateLock);

    uint32_t token = mTokens.acquire();

 

    sp<Client> client = new Client(token, this);

    if (client->ctrlblk == 0) {

        mTokens.release(token);

        return 0;

    }

    status_t err = mClientsMap.add(token, client);

    if (err < 0) {

        mTokens.release(token);

        return 0;

    }

    sp<BClient> bclient =

        new BClient(this, token, client->getControlBlockMemory());

    return bclient;

}

 

2为这个 Client 分配 Surface(用于存放数据)


【SurfaceComposerClient.cpp】
sp<SurfaceControl> SurfaceComposerClient::createSurface(
        int pid,
        const String8& name,
        DisplayID display,
        uint32_t w,
        uint32_t h,
        PixelFormat format,
        uint32_t flags)
{
    sp<SurfaceControl> result;
    if (mStatus == NO_ERROR) {
        ISurfaceFlingerClient::surface_data_t data;
        sp<ISurface> surface = mClient->createSurface(&data, pid, name,
                display, w, h, format, flags);
        if (surface != 0) {
            if (uint32_t(data.token) < NUM_LAYERS_MAX) {
                result = new SurfaceControl(this, surface, data, w, h, format, flags);
            }
        }
    }
    return result;
}

【SurfaceFlinger.cpp】

sp<ISurface> BClient::createSurface(
        ISurfaceFlingerClient::surface_data_t* params, int pid,
        const String8& name,
        DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
        uint32_t flags)
{
    return mFlinger->createSurface(mId, pid, name, params, display, w, h,
            format, flags);
}

 

sp<ISurface> SurfaceFlinger::createSurface(ClientID clientId, int pid,

        const String8& name, ISurfaceFlingerClient::surface_data_t* params,

        DisplayID d, uint32_t w, uint32_t h, PixelFormat format,

        uint32_t flags)

{

    sp<LayerBaseClient> layer;

    sp<LayerBaseClient::Surface> surfaceHandle;

 

    if (int32_t(w|h) < 0) {

        LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",

                int(w), int(h));

        return surfaceHandle;

    }

   

    Mutex::Autolock _l(mStateLock);

    sp<Client> client = mClientsMap.valueFor(clientId);

    if (UNLIKELY(client == 0)) {

        LOGE("createSurface() failed, client not found (id=%d)", clientId);

        return surfaceHandle;

    }

 

    //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);

    int32_t id = client->generateId(pid);

    if (uint32_t(id) >= NUM_LAYERS_MAX) {                      //最大支持32layer

        LOGE("createSurface() failed, generateId = %d", id);

        return surfaceHandle;

    }

 

    switch (flags & eFXSurfaceMask) {

        case eFXSurfaceNormal:

            if (UNLIKELY(flags & ePushBuffers)) {

                layer = createPushBuffersSurfaceLocked(client, d, id,

                        w, h, flags);

            } else {

                layer = createNormalSurfaceLocked(client, d, id,

                        w, h, flags, format);

            }

            break;

        case eFXSurfaceBlur:

            layer = createBlurSurfaceLocked(client, d, id, w, h, flags);

            break;

        case eFXSurfaceDim:

            layer = createDimSurfaceLocked(client, d, id, w, h, flags);

            break;

    }

 

    if (layer != 0) {

        layer->setName(name);

        setTransactionFlags(eTransactionNeeded);

        surfaceHandle = layer->getSurface();                        //获取Isurface结构

        if (surfaceHandle != 0) {                                         //保存好layer信息

            params->token = surfaceHandle->getToken();

            params->identity = surfaceHandle->getIdentity();

            params->width = w;

            params->height = h;

            params->format = format;

        }

    }

 

    return surfaceHandle;

}

 

 

 

sp<LayerBaseClient> SurfaceFlinger::createNormalSurfaceLocked(

        const sp<Client>& client, DisplayID display,

        int32_t id, uint32_t w, uint32_t h, uint32_t flags,

        PixelFormat& format)

{

    // initialize the surfaces

    switch (format) { // TODO: take h/w into account

    case PIXEL_FORMAT_TRANSPARENT:

    case PIXEL_FORMAT_TRANSLUCENT:

        format = PIXEL_FORMAT_RGBA_8888;

        break;

    case PIXEL_FORMAT_OPAQUE:

        format = PIXEL_FORMAT_RGB_565;

        break;

    }

 

    sp<Layer> layer = new Layer(this, display, client, id);

    status_t err = layer->setBuffers(w, h, format, flags);

    if (LIKELY(err == NO_ERROR)) {

        layer->initStates(w, h, flags);

        addLayer_l(layer);

    } else {

        LOGE("createNormalSurfaceLocked() failed (%s)", strerror(-err));

        layer.clear();

    }

    return layer;

}


 

【Layer.cpp】
Layer::Layer(SurfaceFlinger* flinger, DisplayID display,
        const sp<Client>& c, int32_t i)
    :   LayerBaseClient(flinger, display, c, i),
        mSecure(false),
        mNoEGLImageForSwBuffers(false),
        mNeedsBlending(true),
        mNeedsDithering(false)
{
    // no OpenGL operation is possible here, since we might not be
    // in the OpenGL thread.
    mFrontBufferIndex = lcblk->getFrontBuffer();
}

【LayerBase.cpp】
LayerBaseClient::LayerBaseClient(SurfaceFlinger* flinger, DisplayID display,
        const sp<Client>& client, int32_t i)
    : LayerBase(flinger, display), lcblk(NULL), client(client), mIndex(i),
      mIdentity(uint32_t(android_atomic_inc(&sIdentity)))
{
    lcblk = new SharedBufferServer(
            client->ctrlblk, i, NUM_BUFFERS,         //建立双buffer
            mIdentity);
}

 

posted on 2011-10-13 16:08 lfc 阅读(6874) 评论(0)  编辑 收藏 引用
只有注册用户登录后才能发表评论。