void SurfaceFlinger::handleTransaction(uint32_t transactionFlags) {
ATRACE_CALL();
// here we keep a copy of the drawing state (that is the state that's
// going to be overwritten by handleTransactionLocked()) outside of
// mStateLock so that the side-effects of the State assignment
// don't happen with mStateLock held (which can cause deadlocks).
State drawingState(mDrawingState);
Mutex::Autolock _l(mStateLock);
mDebugInTransaction = systemTime();
// Here we're guaranteed that some transaction flags are set
// so we can call handleTransactionLocked() unconditionally.
// We call getTransactionFlags(), which will also clear the flags,
// with mStateLock held to guarantee that mCurrentState won't change
// until the transaction is committed.
modulateVsync(&VsyncModulator::onTransactionCommit);
transactionFlags = getTransactionFlags(eTransactionMask);
handleTransactionLocked(transactionFlags);
mDebugInTransaction = 0;
// here the transaction has been committed
}
mDrawingState
?handleTransactionLocked()
。void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags) {
// Commit display transactions
const bool displayTransactionNeeded = transactionFlags & eDisplayTransactionNeeded;
if (displayTransactionNeeded) {
processDisplayChangesLocked();
processDisplayHotplugEventsLocked();
}
mForceTraversal = false;
mForceTransactionDisplayChange = displayTransactionNeeded;
if (mSomeChildrenChanged) {
mVisibleRegionsDirty = true;
mSomeChildrenChanged = false;
}
processDisplayChangesLocked()
和 processDisplayHotplugEventsLocked()
。mSomeChildrenChanged
为 true:
Layer::setLayer()
Layer::setRelativeLayer()
Layer::addChild()
Layer::removeChild()
void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags) {
...
if (mLayersAdded) {
mLayersAdded = false;
// Layers have been added.
mVisibleRegionsDirty = true;
}
// some layers might have been removed, so
// we need to update the regions they're exposing.
if (mLayersRemoved) {
mLayersRemoved = false;
mVisibleRegionsDirty = true;
mDrawingState.traverseInZOrder([&](Layer* layer) {
if (mLayersPendingRemoval.indexOf(layer) >= 0) {
// this layer is not visible anymore
Region visibleReg;
visibleReg.set(layer->getScreenBounds());
invalidateLayerStack(layer, visibleReg);
}
});
}
如果有 layer 新增,设置 mVisibleRegionsDirty
为 true。
如果有 layer 移除,设置 mVisibleRegionsDirty
为 true,同时遍历 mDrawingState
,找到 mLayersPendingRemoval
中存在的 layer,调用 invalidateLayerStack()
把它的 getScreenBounds()
或操作到屏幕 dirtyRegion
上。
void SurfaceFlinger::invalidateLayerStack(const sp<const Layer>& layer, const Region& dirty) {
for (const auto& [token, displayDevice] : ON_MAIN_THREAD(mDisplays)) {
auto display = displayDevice->getCompositionDisplay();
if (display->belongsInOutput(layer->getLayerStack(), layer->getPrimaryDisplayOnly())) {
display->editState().dirtyRegion.orSelf(dirty);
}
}
}
void SurfaceFlinger::handleTransactionLocked(uint32_ttransactionFlags) {
...
commitTransaction();
}