Android Graphic SurfaceFlinger 疑难解答

本文探讨了Android的SurfaceFlinger如何通过mStateLock、mCurrentStates和mDrawingStates进行客户端同步,确保在VSYNC事件触发时正确处理各层状态。在handleMessageTransaction函数中,SurfaceFlinger更新绘制状态,并在锁定mStateLock期间阻止客户端操作,以确保画面一致性。重点介绍了mStateLock在SurfaceFlinger和Layer状态管理中的关键作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考代码:android4.4.2 aosp
名称约定:
SF : SurfaceFlinger
BQ : BufferQueue


Q:SF如何同步所有Client的合成、设置等需求?
A:mStateLock、SF中的mCurrentStates/mDrawingStates、Layer中的mCurrentStates/mDrawingStates;
mCurrentStates,mCurrentStates是相同的类型,但是在SF中和Layer中分别代表不同的结构:
Layer:

State mCurrentStates;
State mDrawingStates;
struct State {
        Geometry active;
        Geometry requested;
        uint32_t z;
        uint32_t layerStack;                                                
        uint8_t alpha;
        uint8_t flags;
        uint8_t reserved[2];
        int32_t sequence; // changes when visible regions can change        
        Transform transform;
        // the transparentRegion hint is a bit special, it's latched only
        // when we receive a buffer -- this is because it's "content"       
        // dependent.
        Region activeTransparentRegion;
        Region requestedTransparentRegion;
    };

SurfaceFlinger:

State mCurrentStates;
State mDrawingStates;
struct DisplayDeviceState {
        DisplayDeviceState();
        DisplayDeviceState(DisplayDevice::DisplayType type);
        bool isValid() const { return type >= 0; }
        bool isMainDisplay() const { return type == DisplayDevice::DISPLAY_PRIMARY; }
        bool isVirtualDisplay() const { return type >= DisplayDevice::DISPLAY_VIRTUAL; }
        DisplayDevice::DisplayType type;
        sp<IGraphicBufferProducer> surface;
        uint32_t layerStack;
        Rect viewport;
        Rect frame;
        uint8_t orientation;
        String8 displayName;
        bool isSecure;
    };  
    struct State {
        LayerVector layersSortedByZ;
        DefaultKeyedVector< wp<IBinder>, DisplayDeviceState> displays;
    };

使用方法:
当Client设置完后,通过各种方法最终调用到request_vsync();当VSYNC到来后

void SurfaceFlinger::onMessageReceived(int32_t what) {
    ATRACE_CALL();
    switch (what) {
    case MessageQueue::TRANSACTION:
        handleMessageTransaction();
        break;
    case MessageQueue::INVALIDATE:
        handleMessageTransaction();
        handleMessageInvalidate();
        signalRefresh();
        break;
    case MessageQueue::REFRESH:
        handleMessageRefresh();
        break;
    }    
}

SurfaceFlinger::handleMessageTransaction
    SurfaceFlinger::handleTransaction
        Mutex::Autolock _l(mStateLock)

在handleMessageTransaction函数中:
获取mStateLock,
经过一系列的处理,最终SF将mCurrentStates赋值到mDrawingStates,以for循环方式将layersSortedByZ中的layer的mCurrentStates赋值到mDrawingStates;
释放mStateLock;

在获取mStateLock后,所有Client对SF或者对Layer的操作都无法执行,因为它们也需要获取mStateLock;
在释放mStateLock后,所有Client的操作都正常了,Client可以创建新的Surface,也可以设置Surface的参数;
因为在获取释放mStateLock期间,已经将SF、Layer的状态已经保存在mDrawingStates中;而Client每次都是用的是mCurrentStates;

列出几个需要mStateLock的函数:

创建Surface或者说Layer的地方
Client::createSurface
    SurfaceFlinger::createLayer
        SurfaceFlinger::addClientLayer
            Mutex::Autolock _l(mStateLock);
            mCurrentState.layersSortedByZ.add(lbc);
设置Surface的各种参数
SurfaceFlinger::setTransactionState
    Mutex::Autolock _l(mStateLock);
VSYNC信号来时,SF用来处理当前状态的地方
SurfaceFlinger::handleMessageTransaction
    SurfaceFlinger::handleTransaction
        Mutex::Autolock _l(mStateLock)

未完,其他问题待续。。。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值