Layer::State介绍
[Layer.cpp]
//Layer::State
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; //表示Layer的旋转,平移,缩放
// 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;//当前显示的transparentRegion,也就是dumpsf的transparentRegion
Region requestedTransparentRegion;
};
[Layer.cpp]
struct Geometry {
uint32_t w; //size的宽高
uint32_t h;
Rect crop; //crop
inline bool operator ==(const Geometry& rhs) const {
return (w == rhs.w && h == rhs.h && crop == rhs.crop);
}
inline bool operator !=(const Geometry& rhs) const {
return !operator ==(rhs);
}
};
Layer::State设置流程
java层的就不看了,直接看Native层的例子 resize
60 SurfaceComposerClient::openGlobalTransaction();
61 surfaceControl->setSize(320, 240);
62 SurfaceComposerClient::closeGlobalTransaction();
http://androidxref.com/6.0.1_r10/xref/frameworks/native/libs/gui/SurfaceControl.cpp#110
107status_t SurfaceControl::setSize(uint32_t w, uint32_t h) {
108 status_t err = validate();
109 if (err < 0) return err;
110 return mClient->setSize(mHandle, w, h);//这个mClinet就是SurfaceCompserClient强引用对象
111}
http://androidxref.com/6.0.1_r10/xref/frameworks/native/libs/gui/SurfaceComposerClient.cpp#570
569status_t SurfaceComposerClient::setSize(const sp<IBinder>& id, uint32_t w, uint32_t h) {
570 return getComposer().setSize(this, id, w, h);//Composer是个单例
571}
http://androidxref.com/6.0.1_r10/xref/frameworks/native/libs/gui/SurfaceComposerClient.cpp#282
279status_t Composer::setSize(const sp<SurfaceComposerClient>& client,
280 const sp<IBinder>& id, uint32_t w, uint32_t h) {
281 Mutex::Autolock _l(mLock);
282 layer_state_t* s = getLayerStateLocked(client, id);
283 if (!s)
284 return BAD_INDEX;
//每层Layer的属性暂存到Layer_state_t Struct
285 s->what |= layer_state_t::eSizeChanged;
286 s->w = w;
287 s->h = h;
288
289 // Resizing a surface makes the transaction synchronous.
290 mForceSynchronous = true;
291
292 return NO_ERROR;
293}
每层Layer的属性暂存到SurfaceomposerClient对象Client,一起包装为ComposerState
250layer_state_t* Composer::getLayerStateLocked(
251 const sp<SurfaceComposerClient>& client, const sp<IBinder>& id) {
252
253 ComposerState s;
254 s.client = client->mClient;
255 s.state.surface = id;
256
257 ssize_t index = mComposerStates.indexOf(s);
258 if (index < 0) {
259 // we don't have it, add an initialized layer_state to our list
260 index = mComposerStates.add(s);
261 }
262
263 ComposerState* const out = mComposerStates.editArray();
264 return &(out[index].state);
265}
closeTransaction这边流程就不具体看了,直接看函数 SurfaceFlinger::setTransactionState
http://androidxref.com/6.0.1_r10/xref/frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp
2093void SurfaceFlinger::setTransactionState(
2094 const Vector<ComposerState>& state,
2095 const Vector<DisplayState>& displays,
2096 uint32_t flags)
2097{
2098 ATRACE_CALL();
2099 Mutex::Autolock _l(mStateLock);
2100 uint32_t transactionFlags = 0;
...
2124 count = state.size();
2125 for (size_t i=0 ; i<count ; i++) { //遍历每层Layer
2126 const ComposerState& s(state[i]);
...
2134 if (s.client != NULL) {
2135 sp<IBinder> binder = IInterface::asBinder(s.client);
2136 if (binder != NULL) {
2137 String16 desc(binder->getInterfaceDescriptor());
2138 if (desc == ISurfaceComposerClient::descriptor) {
2139 sp<Client> client( static_cast<Client *>(s.client.get()) );
//设置Layer属性,返回flags(表示Layer属性是否有变化)
2140 transactionFlags |= setClientStateLocked(client, s.state);
2141 }
2142 }
2143 }
2144 }
2145
2146 if (transactionFlags) {
2147 // this triggers the transaction
2148 setTransactionFlags(transactionFlags);
2149 ...
2168 }
2169}
2221uint32_t SurfaceFlinger::setClientStateLocked(
2222 const sp<Client>& client,
2223 const layer_state_t& s)
2224{
2225 uint32_t flags = 0;
2226 sp<Layer> layer(client->getLayerUser(s.surface));
2227 if (layer != 0) {
...
2244 if (what & layer_state_t::eSizeChanged) {
2245 if (layer->setSize(s.w, s.h)) {
2246 flags |= eTraversalNeeded; //设置eTraversalNeeded flags
2247 }
2248 }
...
2281 return flags;
2282}
[frameworks/native/services/surfaceflinger/Layer.cpp]
1137bool Layer::setSize(uint32_t w, uint32_t h) {
1138 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h)
1139 return false;
// size,crop传给mCurrentState.requested
1140 mCurrentState.requested.w = w;
1141 mCurrentState.requested.h = h;
1142 setTransactionFlags(eTransactionNeeded);
1143 return true;
1144}
2085uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
2086 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
2087 if ((old & flags)==0) { // wake the server up, Layer属性发生变化
2088 signalTransaction();
2089 }
2090 return old;
2091}
760void SurfaceFlinger::signalTransaction() {
761 mEventQueue.invalidate();
762}