上篇博客我们说到SurfaceControl会调用openTransaction和closeTransaction来一起讲所有SurfaceControl的属性等传给SurfaceFlinger。我们来看下这个过程。
一、SurfaceControl
我们先来看下SurfaceControl.java中的相关函数,都是调用了JNI函数。
-
public static void openTransaction() {
-
nativeOpenTransaction();
-
}
-
/** end a transaction */
-
public static void closeTransaction() {
-
nativeCloseTransaction();
-
}
-
/** flag the transaction as an animation */
-
public static void setAnimationTransaction() {
-
nativeSetAnimationTransaction();
-
}
-
public void setLayer(int zorder) {
-
checkNotReleased();
-
nativeSetLayer(mNativeObject, zorder);
-
}
我们再来看下android_view_SurfaceControl.cpp下面的一些JNI函数,其中nativeOpenTransaction和nativeCloseTransaction都是调用了SurfaceComposerClient的静态函数,其他都是调用了c层的SurfaceControl的函数。
-
static void nativeOpenTransaction(JNIEnv* env, jclass clazz) {
-
SurfaceComposerClient::openGlobalTransaction();
-
}
-
static void nativeCloseTransaction(JNIEnv* env, jclass clazz) {
-
SurfaceComposerClient::closeGlobalTransaction();
-
}
-
static void nativeSetAnimationTransaction(JNIEnv* env, jclass clazz) {
-
SurfaceComposerClient::setAnimationTransaction();
-
}
-
static void nativeSetLayer(JNIEnv* env, jclass clazz, jlong nativeObject, jint zorder) {
-
SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
-
status_t err = ctrl->setLayer(zorder);
-
if (err < 0 && err != NO_INIT) {
-
doThrowIAE(env);
-
}
-
}
-
static void nativeSetPosition(JNIEnv* env, jclass clazz, jlong nativeObject, jfloat x, jfloat y) {
-
SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
-
status_t err = ctrl->setPosition(x, y);
-
if (err < 0 && err != NO_INIT) {
-
doThrowIAE(env);
-
}
-
}
而SurfaceControl.cpp的函数都是调用了SurfaceComposerClient的相关函数。
-
status_t SurfaceControl::setLayerStack(uint32_t layerStack) {
-
status_t err = validate();
-
if (err < 0) return err;
-
return mClient->setLayerStack(mHandle, layerStack);
-
}
-
status_t SurfaceControl::setLayer(uint32_t layer) {
-
status_t err = validate();
-
if (err < 0) return err;
-
return mClient->setLayer(mHandle, layer);
-
}
-
status_t SurfaceControl::setPosition(float x, float y) {
-
status_t err = validate();
-
if (err < 0) return err;
-
return mClient->setPosition(mHandle, x, y);
-
}
1.1 SurfaceComposerClient设置相关函数
而SurfaceComposerClient中的相关函数又分成两类一类是layer的状态,另一类是display的状态。
1.1.1 设置layer相关函数
下面我们先看layer的状态,有如下函数,都是通过getLayerStateLocked去查询相关layer的状态,没有的话就去添加到mComposerStates中。下面是ComposerState的结构体。
-
struct ComposerState {
-
sp<ISurfaceComposerClient> client;
-
layer_state_t state;
-
status_t write(Parcel& output) const;
-
status_t read(const Parcel& input);
-
};
getLayerStateLocked函数,就是去mComposerStates中获取相关layer的state。
-
layer_state_t* Composer::getLayerStateLocked(
-
const sp<SurfaceComposerClient>& client, const sp<IBinder>& id) {
-
ComposerState s;
-
s.client = client->mClient;
-
s.state.surface = id;
-
ssize_t index = mComposerStates.indexOf(s);
-
if (index < 0) {
-
// we don't have it, add an initialized layer_state to our list
-
index = mComposerStates.add(s);
-
}
-
ComposerState* const out = mComposerStates.editArray();
-
return &(out[index].state);
-
}
-
status_t Composer::setPosition(const sp<SurfaceComposerClient>& client,
-
const sp<IBinder>& id, float x, float y) {
-
Mutex::Autolock _l(mLock);
-
layer_state_t* s = getLayerStateLocked(client, id);
-
if (!s)
-
return BAD_INDEX;
-
s->what |= layer_state_t::ePositionChanged;
-
s->x = x;
-
s->y = y;
-
return NO_ERROR;
-
}
-
status_t Composer::setSize(const sp<SurfaceComposerClient>& client,
-
const sp<IBinder>& id, uint32_t w, uint32_t h) {
-
Mutex::Autolock _l(mLock);
-
layer_state_t* s = getLayerStateLocked(client, id);
-
if (!s)
-
return BAD_INDEX;
-
s->what |= layer_state_t::eSizeChanged;
-
s->w = w;
-
s->h = h;
-
// Resizing a surface makes the transaction synchronous.
-
mForceSynchronous = true;
-
return NO_ERROR;
-
}
-
status_t Composer::setLayer(const sp<SurfaceComposerClient>& client,
-
const sp<IBinder>& id, uint32_t z) {
-
Mutex::Autolock _l(mLock);
-
layer_state_t* s = getLayerStateLocked(client, id);
-
if (!s)
-
return BAD_INDEX;
-
s->what |= layer_state_t::eLayerChanged;
-
s->z = z;
-
return NO_ERROR;
-
}
-
status_t Composer::setFlags(const sp<SurfaceComposerClient>& client,
-
const sp<IBinder>& id, uint32_t flags,
-
uint32_t mask) {
-
Mutex::Autolock _l(mLock);
-
layer_state_t* s = getLayerStateLocked(client, id);
-
if (!s)
-
return BAD_INDEX;
-
if (mask & layer_state_t::eLayerOpaque ||
-
mask & layer_state_t::eLayerHidden ||
-
mask & layer_state_t::eLayerSecure) {
-
s->what |= layer_state_t::eFlagsChanged;
-
}
-
s->flags &= ~mask;
-
s->flags |= (flags & mask);
-
s->mask |= mask;
-
return NO_ERROR;
-
}
-
status_t Composer::setTransparentRegionHint(
-
const sp<SurfaceComposerClient>& client, const sp<IBinder>& id,
-
const Region& transparentRegion) {
-
Mutex::Autolock _l(mLock);
-
layer_state_t* s = getLayerStateLocked(client, id);
-
if (!s)
-
return BAD_INDEX;
-
s->what |= layer_state_t::eTransparentRegionChanged;
-
s->transparentRegion = transparentRegion;
-
return NO_ERROR;
-
}
-
status_t Composer::setAlpha(const sp<SurfaceComposerClient>& client,
-
const sp<IBinder>& id, float alpha) {
-
Mutex::Autolock _l(mLock);
-
layer_state_t* s = getLayerStateLocked(client, id);
-
if (!s)
-
return BAD_INDEX;
-
s->what |= layer_state_t::eAlphaChanged;
-
s->alpha = alpha;
-
return NO_ERROR;
-
}
-
status_t Composer::setLayerStack(const sp<SurfaceComposerClient>& client,
-
const sp<IBinder>& id, uint32_t layerStack) {
-
Mutex::Autolock _l(mLock);
-
layer_state_t* s = getLayerStateLocked(client, id);
-
if (!s)
-
return BAD_INDEX;
-
s->what |= layer_state_t::eLayerStackChanged;
-
s->layerStack = layerStack;
-
return NO_ERROR;
-
}
-
status_t Composer::setMatrix(const sp<SurfaceComposerClient>& client,
-
const sp<IBinder>& id, float dsdx, float dtdx,
-
float dsdy, float dtdy) {
-
Mutex::Autolock _l(mLock);
-
layer_state_t* s = getLayerStateLocked(client, id);
-
if (!s)
-
return BAD_INDEX;
-
s->what |= layer_state_t::eMatrixChanged;
-
layer_state_t::matrix22_t matrix;
-
matrix.dsdx = dsdx;
-
matrix.dtdx = dtdx;
-
matrix.dsdy = dsdy;
-
matrix.dtdy = dtdy;
-
s->matrix = matrix;
-
return NO_ERROR;
-
}
-
status_t Composer::setCrop(const sp<SurfaceComposerClient>& client,
-
const sp<IBinder>& id, const Rect& crop) {
-
Mutex::Autolock _l(mLock);
-
layer_state_t* s = getLayerStateLocked(client, id);
-
if (!s)
-
return BAD_INDEX;
-
s->what |= layer_state_t::eCropChanged;
-
s->crop = crop;
-
return NO_ERROR;
-
}
1.1.2 设置display相关函数
设置display相关函数,和前面类似。都是先调用getDisplayStateLocked函数获取相关display状态,如果没有加入到mDisplayStates中。
DisplayState的结构体如下:
-
struct DisplayState {
-
enum {
-
eOrientationDefault = 0,
-
eOrientation90 = 1,
-
eOrientation180 = 2,
-
eOrientation270 = 3,
-
eOrientationUnchanged = 4,
-
eOrientationSwapMask = 0x01
-
};
-
enum {
-
eSurfaceChanged = 0x01,
-
eLayerStackChanged = 0x02,
-
eDisplayProjectionChanged = 0x04,
-
eDisplaySizeChanged = 0x08
-
};
-
uint32_t what;
-
sp<IBinder> token;
-
sp<IGraphicBufferProducer> surface;
-
uint32_t layerStack;
-
uint32_t orientation;
-
Rect viewport;
-
Rect frame;
-
uint32_t width, height;
-
status_t write(Parcel& output) const;
-
status_t read(const Parcel& input);
-
};
每个相关设置display的函数都是先调用getDisplayStateLocked函数来获取相关display状态,如果没有加入到mDisplayStates中。
-
DisplayState& Composer::getDisplayStateLocked(const sp<IBinder>& token) {
-
DisplayState s;
-
s.token = token;
-
ssize_t index = mDisplayStates.indexOf(s);
-
if (index < 0) {
-
// we don't have it, add an initialized layer_state to our list
-
s.what = 0;
-
index = mDisplayStates.add(s);
-
}
-
return mDisplayStates.editItemAt(static_cast<size_t>(index));
-
}
-
void Composer::setDisplaySurface(const sp<IBinder>& token,
-
const sp<IGraphicBufferProducer>& bufferProducer) {
-
Mutex::Autolock _l(mLock);
-
DisplayState& s(getDisplayStateLocked(token));
-
s.surface = bufferProducer;
-
s.what |= DisplayState::eSurfaceChanged;
-
}
-
void Composer::setDisplayLayerStack(const sp<IBinder>& token,
-
uint32_t layerStack) {
-
Mutex::Autolock _l(mLock);
-
DisplayState& s(getDisplayStateLocked(token));
-
s.layerStack = layerStack;
-
s.what |= DisplayState::eLayerStackChanged;
-
}
-
void Composer::setDisplayProjection(const sp<IBinder>& token,
-
uint32_t orientation,
-
const Rect& layerStackRect,
-
const Rect& displayRect) {
-
Mutex::Autolock _l(mLock);
-
DisplayState& s(getDisplayStateLocked(token));
-
s.orientation = orientation;
-
s.viewport = layerStackRect;
-
s.frame = displayRect;
-
s.what |= DisplayState::eDisplayProjectionChanged;
-
mForceSynchronous = true; // TODO: do we actually still need this?
-
}
-
void Composer::setDisplaySize(const sp<IBinder>& token, uint32_t width, uint32_t height) {
-
Mutex::Autolock _l(mLock);
-
DisplayState& s(getDisplayStateLocked(token));
-
s.width = width;
-
s.height = height;
-
s.what |= DisplayState::eDisplaySizeChanged;
-
}
1.3和SurfaceFlinger通信
看下面openGlobalTransactionImpl函数,上锁直接将mTransactionNestCount加1,然后在closeGlobalTransactionImpl函数中检查mTransactionNestCount的值是否为1.如果大于1,将直接退出。这是多个地方调用openGlobalTransaction和closeGlobalTransaction时,只有最后一次才会和SurfaceFlinger通信。最后把mComposerStates和mDisplayStates通过调用SurfaceFlinger的setTransactionState方法传入SurfaceFlinger中。
-
void Composer::openGlobalTransactionImpl() {
-
{ // scope for the lock
-
Mutex::Autolock _l(mLock);
-
mTransactionNestCount += 1;
-
}
-
}
-
void Composer::closeGlobalTransactionImpl(bool synchronous) {
-
sp<ISurfaceComposer> sm(ComposerService::getComposerService());
-
Vector<ComposerState> transaction;
-
Vector<DisplayState> displayTransaction;
-
uint32_t flags = 0;
-
{ // scope for the lock
-
Mutex::Autolock _l(mLock);
-
mForceSynchronous |= synchronous;
-
if (!mTransactionNestCount) {
-
ALOGW("At least one call to closeGlobalTransaction() was not matched by a prior "
-
"call to openGlobalTransaction().");
-
} else if (--mTransactionNestCount) {//确保mTransactionNestCount在1的时候才会和SurfaceFlinger通信
-
return;
-
}
-
transaction = mComposerStates;
-
mComposerStates.clear();
-
displayTransaction = mDisplayStates;
-
mDisplayStates.clear();
-
if (mForceSynchronous) {
-
flags |= ISurfaceComposer::eSynchronous;
-
}
-
if (mAnimation) {
-
flags |= ISurfaceComposer::eAnimation;
-
}
-
mForceSynchronous = false;
-
mAnimation = false;
-
}
-
sm->setTransactionState(transaction, displayTransaction, flags);//调用SurfaceFlinger的setTransactionState函数
-
}
二、SurfaceFlinger
在SurfaceFlinger中是在setTransactionState函数中处理,然后遍历各个DisplayState调用setDisplayStateLocked设置display状态,接着遍历ComposerState然后调用setClientStateLocked设置状态。最后会调用setTransactionFlags函数,最后会发送INVALIDATE消息,这个消息的处理在之前的博客分析过。
-
void SurfaceFlinger::setTransactionState(
-
const Vector<ComposerState>& state,
-
const Vector<DisplayState>& displays,
-
uint32_t flags)
-
{
-
ATRACE_CALL();
-
Mutex::Autolock _l(mStateLock);
-
uint32_t transactionFlags = 0;
-
if (flags & eAnimation) {
-
// For window updates that are part of an animation we must wait for
-
// previous animation "frames" to be handled.
-
while (mAnimTransactionPending) {
-
status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
-
if (CC_UNLIKELY(err != NO_ERROR)) {
-
// just in case something goes wrong in SF, return to the
-
// caller after a few seconds.
-
ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
-
"waiting for previous animation frame");
-
mAnimTransactionPending = false;
-
break;
-
}
-
}
-
}
-
size_t count = displays.size();
-
for (size_t i=0 ; i<count ; i++) {
-
const DisplayState& s(displays[i]);
-
transactionFlags |= setDisplayStateLocked(s);
-
}
-
count = state.size();
-
for (size_t i=0 ; i<count ; i++) {
-
const ComposerState& s(state[i]);
-
// Here we need to check that the interface we're given is indeed
-
// one of our own. A malicious client could give us a NULL
-
// IInterface, or one of its own or even one of our own but a
-
// different type. All these situations would cause us to crash.
-
//
-
// NOTE: it would be better to use RTTI as we could directly check
-
// that we have a Client*. however, RTTI is disabled in Android.
-
if (s.client != NULL) {
-
sp<IBinder> binder = IInterface::asBinder(s.client);
-
if (binder != NULL) {
-
String16 desc(binder->getInterfaceDescriptor());
-
if (desc == ISurfaceComposerClient::descriptor) {
-
sp<Client> client( static_cast<Client *>(s.client.get()) );
-
transactionFlags |= setClientStateLocked(client, s.state);
-
}
-
}
-
}
-
}
-
if (transactionFlags) {
-
// this triggers the transaction
-
setTransactionFlags(transactionFlags);
-
// if this is a synchronous transaction, wait for it to take effect
-
// before returning.
-
if (flags & eSynchronous) {
-
mTransactionPending = true;
-
}
-
if (flags & eAnimation) {
-
mAnimTransactionPending = true;
-
}
-
while (mTransactionPending) {
-
status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
-
if (CC_UNLIKELY(err != NO_ERROR)) {
-
// just in case something goes wrong in SF, return to the
-
// called after a few seconds.
-
ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
-
mTransactionPending = false;
-
break;
-
}
-
}
-
}
-
}
setDisplayStateLocked函数是从mCurrentState中获取其DisplayState,然后设置其一些变量。
-
uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
-
{
-
ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
-
if (dpyIdx < 0)
-
return 0;
-
uint32_t flags = 0;
-
DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
-
if (disp.isValid()) {
-
const uint32_t what = s.what;
-
if (what & DisplayState::eSurfaceChanged) {
-
if (IInterface::asBinder(disp.surface) != IInterface::asBinder(s.surface)) {
-
disp.surface = s.surface;
-
flags |= eDisplayTransactionNeeded;
-
}
-
}
-
if (what & DisplayState::eLayerStackChanged) {
-
if (disp.layerStack != s.layerStack) {
-
disp.layerStack = s.layerStack;
-
flags |= eDisplayTransactionNeeded;
-
}
-
}
-
if (what & DisplayState::eDisplayProjectionChanged) {
-
if (disp.orientation != s.orientation) {
-
disp.orientation = s.orientation;
-
flags |= eDisplayTransactionNeeded;
-
}
-
if (disp.frame != s.frame) {
-
disp.frame = s.frame;
-
flags |= eDisplayTransactionNeeded;
-
}
-
if (disp.viewport != s.viewport) {
-
disp.viewport = s.viewport;
-
flags |= eDisplayTransactionNeeded;
-
}
-
}
-
if (what & DisplayState::eDisplaySizeChanged) {
-
if (disp.width != s.width) {
-
disp.width = s.width;
-
flags |= eDisplayTransactionNeeded;
-
}
-
if (disp.height != s.height) {
-
disp.height = s.height;
-
flags |= eDisplayTransactionNeeded;
-
}
-
}
-
}
-
return flags;
-
}
setClientStateLocked是获取layer,然后设置一些layer的属性,最后重新放入mCurrentState.layersSortedByZ中。
-
uint32_t SurfaceFlinger::setClientStateLocked(
-
const sp<Client>& client,
-
const layer_state_t& s)
-
{
-
uint32_t flags = 0;
-
sp<Layer> layer(client->getLayerUser(s.surface));//获取layer
-
if (layer != 0) {
-
const uint32_t what = s.what;
-
if (what & layer_state_t::ePositionChanged) {
-
if (layer->setPosition(s.x, s.y))
-
flags |= eTraversalNeeded;
-
}
-
if (what & layer_state_t::eLayerChanged) {
-
// NOTE: index needs to be calculated before we update the state
-
ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
-
if (layer->setLayer(s.z)) {
-
mCurrentState.layersSortedByZ.removeAt(idx);
-
mCurrentState.layersSortedByZ.add(layer);
-
// we need traversal (state changed)
-
// AND transaction (list changed)
-
flags |= eTransactionNeeded|eTraversalNeeded;
-
}
-
}
-
if (what & layer_state_t::eSizeChanged) {
-
if (layer->setSize(s.w, s.h)) {
-
flags |= eTraversalNeeded;
-
}
-
}
-
if (what & layer_state_t::eAlphaChanged) {
-
if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
-
flags |= eTraversalNeeded;
-
}
-
if (what & layer_state_t::eMatrixChanged) {
-
if (layer->setMatrix(s.matrix))
-
flags |= eTraversalNeeded;
-
}
-
if (what & layer_state_t::eTransparentRegionChanged) {
-
if (layer->setTransparentRegionHint(s.transparentRegion))
-
flags |= eTraversalNeeded;
-
}
-
if (what & layer_state_t::eFlagsChanged) {
-
if (layer->setFlags(s.flags, s.mask))
-
flags |= eTraversalNeeded;
-
}
-
if (what & layer_state_t::eCropChanged) {
-
if (layer->setCrop(s.crop))
-
flags |= eTraversalNeeded;
-
}
-
if (what & layer_state_t::eLayerStackChanged) {
-
// NOTE: index needs to be calculated before we update the state
-
ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
-
if (layer->setLayerStack(s.layerStack)) {
-
mCurrentState.layersSortedByZ.removeAt(idx);
-
mCurrentState.layersSortedByZ.add(layer);
-
// we need traversal (state changed)
-
// AND transaction (list changed)
-
flags |= eTransactionNeeded|eTraversalNeeded;
-
}
-
}
-
}
-
return flags;
-
}
这些函数我们都在博客http://blog.csdn.net/kc58236582/article/details/52778333中,每个layer的onFrameAvailable中最后也会调用signalLayerUpdate函数,最后一样会发送INVALIDATE消息。至于这个消息的处理在这篇博客中分析过了,会对各个layer属性、display更新。
三、Surface
至于Surface,之前我们分析过,在Surface中会先调用mGraphicBufferProducer的dequeueBuffer函数,然后再调用mGraphicBufferProducer的queueBuffer函数。这样在SurfaceFlinger的 每个Layer的onFrameAvailable函数就会执行,这样也会统计到每个layer的数据有变化了。