android graphic(13)—surface flinger中的“事务”

http://blog.csdn.net/lewif/article/details/50856358

目录(?)[+]


surfaceflinger中的事务flags

surfacefligner中所涉及事务的flag包括下面几种,

enum {

    //需要处理事务

   eTransactionNeeded        = 0x01,

    //需要遍历

   eTraversalNeeded          = 0x02,

    //需要处理display的事务

   eDisplayTransactionNeeded = 0x04,

   eTransactionMask          = 0x07

};

设置和处理flags

mTransactionFlagssurface flinger中的一个成员,注意在layer中也存在一个同名的mTransactionFlags,通过下面的函数设置该flag

uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags){

    //或返回的是mTransactionFlags旧的值

    uint32_t old = android_atomic_or(flags, &mTransactionFlags);

    //如果旧值和flags没有bit是相同的??

    if ((old & flags)==0) { // wake the server up

       signalTransaction();

    }

    return old;

}

surfaceflinger中,

a, createLayer()removeLayer()setClientStateLocked都会去设置事务flageTransactionNeeded

b, setClientStateLocked()会去设置eTraversalNeeded

c, createDisplay()destroyDisplay()onHotplugReceived()setDisplayStateLocked()会去设置eDisplayTransactionNeeded

/*----------------------SurfaceFlinger.cpp-------------------------------*/

void SurfaceFlinger::signalTransaction() {

    mEventQueue.invalidate();

}

进而调用,

#defineINVALIDATE_ON_VSYNC 1

/*----------------------MessageQueue.cpp-------------------------------*/

voidMessageQueue::invalidate() {

//INVALIDATE_ON_VSYNC宏默认为true

#ifINVALIDATE_ON_VSYNC

   mEvents->requestNextVsync();

#else

   mHandler->dispatchInvalidate();

#endif

}

mEvents就是EventThread类中的Connection类,

/*----------------------EventThread.cpp-------------------------------*/

void EventThread::Connection::requestNextVsync() {

    mEventThread->requestNextVsync(this);

}

 

//关于Connection中的count,当为0时为一次性的事件,即触发一次sync信号

//count >= 1 : continuous event. count is the vsync rate

//count == 0 : one-shot event that has not fired

//count ==-1 : one-shot event that fired this round / disabled

        int32_tcount;

 

void EventThread::requestNextVsync(

        const sp<EventThread::Connection>& connection) {

    Mutex::Autolock _l(mLock);

    if (connection->count <0) {

        connection->count =0;

        mCondition.broadcast();

    }

}

从上面代码分析,各种事务都会去触发一次vsync,前面文章分析过,在每次vsync信号到来时,会去调用,

/*----------------------SurfaceFlinger.cpp-------------------------------*/

voidSurfaceFlinger::onMessageReceived(int32_t what) {

    ATRACE_CALL();

    switch (what) {

    caseMessageQueue::TRANSACTION:

       handleMessageTransaction();

        break;

    //这个分支

    caseMessageQueue::INVALIDATE:

       handleMessageTransaction();

       handleMessageInvalidate();

        signalRefresh();

        break;

    caseMessageQueue::REFRESH:

       handleMessageRefresh();

        break;

    }

}

进而调用handleMessageTransaction,进而根据mTransactionFlags中设置的bit值做相应的处理。

/*----------------------SurfaceFlinger.cpp-------------------------------*/

void SurfaceFlinger::handleMessageTransaction() {

    uint32_ttransactionFlags =peekTransactionFlags(eTransactionMask);

    if (transactionFlags){

       handleTransaction(transactionFlags);

    }

}

创建layer时设置flag

下面以surface flinger在创建layer时为例,

/*----------------------SurfaceFlinger.cpp-------------------------------*/

status_t SurfaceFlinger::createLayer(

        constString8& name,

        constsp<Client>& client,

        uint32_t w,uint32_t h, PixelFormat format, uint32_t flags,

       sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)

{

    //ALOGD("createLayer for (%d x %d),name=%s", w, h, name.string());

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

        ALOGE("createLayer() failed, w or h isnegative (w=%d, h=%d)",

                int(w), int(h));

        return BAD_VALUE;

    }

 

    status_t result= NO_ERROR;

 

    sp<Layer>layer;

 

    switch (flags &ISurfaceComposerClient::eFXSurfaceMask) {

        caseISurfaceComposerClient::eFXSurfaceNormal:

            result= createNormalLayer(client,

                   name, w, h, flags, format,

                   handle, gbp, &layer);

            break;

        caseISurfaceComposerClient::eFXSurfaceDim:

            result= createDimLayer(client,

                   name, w, h, flags,

                   handle, gbp, &layer);

            break;

        default:

            result= BAD_VALUE;

            break;

    }

 

    if (result ==NO_ERROR) {

       //handlelayer保存到client

       //gbp,也就是server端的BufferQueue放到mGraphicBufferProducerList

       addClientLayer(client, *handle, *gbp, layer);

       //因为新建了layer,所以设置事务flag为需要处理事务eTransactionNeeded

       setTransactionFlags(eTransactionNeeded);

    }

    return result;

}

关于Handle,就是个BBinder,其中保存了surfaceflignerlayer,用来传递给client,同时继承了 LayerCleaner ,顾名思义,就是删除layer时做一些工作,

sp<IBinder> Layer::getHandle() {

    Mutex::Autolock_l(mLock);

 

   LOG_ALWAYS_FATAL_IF(mHasSurface,

            "Layer::getHandle() has alreadybeen called");

 

    mHasSurface = true;

 

    /*

     * The layerhandle is just a BBinderobject passed tothe client

     * (remoteprocess) -- we don't keepany reference on our side such that

     * the dtor is called when the remote side letgo ofitsreference.

     *

     * LayerCleanerensures thatmFlinger->onLayerDestroyed() is called for

     * this layerwhen the handle is destroyed.

     */

 

    class Handle : publicBBinder, public LayerCleaner {

        wp<constLayer> mOwner;

    public:

       Handle(const sp<SurfaceFlinger>& flinger, constsp<Layer>& layer)

            :LayerCleaner(flinger, layer), mOwner(layer) {

        }

    };

 

    return newHandle(mFlinger, this);

}

/*----------------------Layer.cpp-------------------------------*/

//LayerCleaner仅仅保存了layersurfaceflinger

//在析构时,会调用surfaceflingeronLayerDestroyed(mLayer)函数,去做

//删除Layer的工作

 

Layer::LayerCleaner::LayerCleaner(constsp<SurfaceFlinger>& flinger,

        constsp<Layer>& layer)

    :mFlinger(flinger), mLayer(layer) {

}

 

Layer::LayerCleaner::~LayerCleaner() {

    // destroy client resources

   mFlinger->onLayerDestroyed(mLayer);

}

/*----------------------SurfaceFlinger.cpp-------------------------------*/

void SurfaceFlinger::addClientLayer(const sp<Client>& client,

        const sp<IBinder>&handle,

        const sp<IGraphicBufferProducer>& gbc,

        const sp<Layer>& lbc)

{

    // attach this layer to the client

    client->attachLayer(handle, lbc);

 

    // add this layer to the current statelist

    Mutex::Autolock _l(mStateLock);

    // 将新建的layer添加到mCurrentStatelayersSortedByZ

    mCurrentState.layersSortedByZ.add(lbc);

    // BufferQueue添加到mGraphicBufferProducerList

    // SortedVector< wp<IBinder>> mGraphicBufferProducerList;

   mGraphicBufferProducerList.add(gbc->asBinder());

}

/*----------------------Client.cpp-------------------------------*/

//应该是每个app对应一个Client

//每个Client里面有这个app对应的所有layers

//DefaultKeyedVector< wp<IBinder>, wp<Layer> > mLayers;

//handle就是将这个layersurfaceflinger的信息保存起来,用来析构时候用

void Client::attachLayer(const sp<IBinder>&handle, const sp<Layer>& layer)

{

    Mutex::Autolock _l(mLock);

    mLayers.add(handle, layer);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值