参考代码: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)
未完,其他问题待续。。。。。。