surfaceflinger中的事务flags
surfacefligner中所涉及事务的flag包括下面几种,
enum {
//需要处理事务
eTransactionNeeded = 0x01,
//需要遍历
eTraversalNeeded = 0x02,
//需要处理display的事务
eDisplayTransactionNeeded = 0x04,
eTransactionMask = 0x07
};
设置和处理flags
mTransactionFlags是surface 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都会去设置事务flag为eTransactionNeeded;
b, setClientStateLocked()会去设置eTraversalNeeded;
c, createDisplay(),destroyDisplay(),onHotplugReceived(),setDisplayStateLocked()会去设置eDisplayTransactionNeeded。
/*----------------------SurfaceFlinger.cpp-------------------------------*/
void SurfaceFlinger::signalTransaction() {
mEventQueue.invalidate();
}
进而调用,
#define INVALIDATE_ON_VSYNC 1
/*----------------------MessageQueue.cpp-------------------------------*/
void MessageQueue::invalidate() {
// INVALIDATE_ON_VSYNC宏默认为true
#if INVALIDATE_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_t count;
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-------------------------------*/
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;
}
}
进而调用handleMessageTransaction
,进而根据mTransactionFlags中设置的bit值做相应的处理。
/*----------------------SurfaceFlinger.cpp-------------------------------*/
void SurfaceFlinger::handleMessageTransaction() {
uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
if (transactionFlags) {
handleTransaction(transactionFlags);
}
}
创建layer时设置flag
下面以surface flinger在创建layer时为例,
/*----------------------SurfaceFlinger.cpp-------------------------------*/
status_t SurfaceFlinger::createLayer(
const String8& name,
const sp<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 is negative (w=%d, h=%d)",
int(w), int(h));
return BAD_VALUE;
}
status_t result = NO_ERROR;
sp<Layer> layer;
switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
case ISurfaceComposerClient::eFXSurfaceNormal:
result = createNormalLayer(client,
name, w, h, flags, format,
handle, gbp, &layer);
break;
case ISurfaceComposerClient::eFXSurfaceDim:
result = createDimLayer(client,
name, w, h, flags,
handle, gbp, &layer);
break;
default:
result = BAD_VALUE;
break;
}
if (result == NO_ERROR) {
//①将handle和layer保存到client中
//②将gbp,也就是server端的BufferQueue放到mGraphicBufferProducerList中
addClientLayer(client, *handle, *gbp, layer);
//因为新建了layer,所以设置事务flag为需要处理事务eTransactionNeeded
setTransactionFlags(eTransactionNeeded);
}
return result;
}
关于Handle,就是个BBinder,其中保存了surfacefligner和layer,用来传递给client,同时继承了 LayerCleaner ,顾名思义,就是删除layer时做一些工作,
sp<IBinder> Layer::getHandle() {
Mutex::Autolock _l(mLock);
LOG_ALWAYS_FATAL_IF(mHasSurface,
"Layer::getHandle() has already been called");
mHasSurface = true;
/*
* The layer handle is just a BBinder object passed to the client
* (remote process) -- we don't keep any reference on our side such that
* the dtor is called when the remote side let go of its reference.
*
* LayerCleaner ensures that mFlinger->onLayerDestroyed() is called for
* this layer when the handle is destroyed.
*/
class Handle : public BBinder, public LayerCleaner {
wp<const Layer> mOwner;
public:
Handle(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer)
: LayerCleaner(flinger, layer), mOwner(layer) {
}
};
return new Handle(mFlinger, this);
}
/*----------------------Layer.cpp-------------------------------*/
// LayerCleaner仅仅保存了layer和surfaceflinger,
// 在析构时,会调用surfaceflinger的onLayerDestroyed(mLayer)函数,去做
// 删除Layer的工作
Layer::LayerCleaner::LayerCleaner(const sp<SurfaceFlinger>& flinger,
const sp<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 state list
Mutex::Autolock _l(mStateLock);
// 将新建的layer添加到mCurrentState的layersSortedByZ
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就是将这个layer和surfaceflinger的信息保存起来,用来析构时候用
void Client::attachLayer(const sp<IBinder>& handle, const sp<Layer>& layer)
{
Mutex::Autolock _l(mLock);
mLayers.add(handle, layer);
}