surfaceflinger 代码追踪

转载 2012年03月21日 13:25:29

转自:http://www.cnitblog.com/luofuchong/archive/2011/10/13/76022.html

参考资料:
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);
}


相关文章推荐

更系统的SurfaceFlinger思路以及代码追踪

转自:http://blog.csdn.net/droidphone/article/details/5972568 SurfaceFlinger在系统启动阶段作为系统服务被加载。应用程序中的每个窗...

surfaceFlinger代码分析

  • 2014年03月10日 21:31
  • 39KB
  • 下载

android应用程序fps meter[帧数显示]的分析 —— SurfaceFlinger被注入统计代码 (1)

fps meter是常用的检测帧率的软件,该软件需要root权限才能工作,一直比较好奇它一个apk是如何知道系统当前的帧率情况的,就针对此apk分析了一下其工作原理。...
  • freshui
  • freshui
  • 2013年07月09日 13:22
  • 9860

android应用程序fps meter[帧数显示]的分析 —— SurfaceFlinger被注入统计代码(3)

上节已经详细说了下注入过程,最后寄生进程在宿主进程中下了个蛋,本节再具体分析注入后的感染过程。 一旦系统被root后,普通的应用程序几乎可以做它想做的任何事情,如果能再允许remount系统的只读分区...
  • freshui
  • freshui
  • 2013年07月11日 09:17
  • 10515

android应用程序fps meter[帧数显示]的分析 —— SurfaceFlinger被注入统计代码 (2)

上一节已经分析应用程序启动后,会通过RootTools库的Shell类,获取root权限并执行/data/data/com.aatt.fpsm/files/0,也就是apk包中的res/raw/bin...
  • freshui
  • freshui
  • 2013年07月10日 09:01
  • 5784

surfaceflinger图片

  • 2013年03月20日 18:07
  • 694KB
  • 下载

surfaceflinger源码

  • 2015年09月12日 22:43
  • 116KB
  • 下载

GUI系统之SurfaceFlinger(12)VSync信号的产生和处理

文章都是通过阅读源码分析出来的,还在不断完善与改进中,其中难免有些地方理解得不对,欢迎大家批评指正。转载请注明:From LXS. http://blog.csdn.net/uiop78uiop78/...

surfaceflinger 大杂烩

  • 2012年10月30日 21:14
  • 4.29MB
  • 下载

Android_Surfaceflinger研究-显示系统

  • 2012年04月08日 22:44
  • 242KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:surfaceflinger 代码追踪
举报原因:
原因补充:

(最多只允许输入30个字)