void SurfaceFlinger::applyTransactionState(const FrameTimelineInfo& frameTimelineInfo,
const Vector<ComposerState>& states,
Vector<DisplayState>& displays, uint32_t flags,
const InputWindowCommands& inputWindowCommands,
const int64_t desiredPresentTime, bool isAutoTimestamp,
const client_cache_t& uncacheBuffer,
const int64_t postTime, uint32_t permissions,
bool hasListenerCallbacks,
const std::vector<ListenerCallbacks>& listenerCallbacks,
int originPid, int originUid, uint64_t transactionId) {
uint32_t transactionFlags = 0;
for (DisplayState& display : displays) {
display.sanitize(permissions);
transactionFlags |= setDisplayStateLocked(display);
}
遍历 DisplayState。
permissions
移除无权限的 flag。setDisplayStateLocked()
设置 DisplayState。void SurfaceFlinger::applyTransactionState(...
...
// start and end registration for listeners w/ no surface so they can get their callback. Note
// that listeners with SurfaceControls will start registration during setClientStateLocked
// below.
for (const auto& listener : listenerCallbacks) {
mTransactionCallbackInvoker.startRegistration(listener);
mTransactionCallbackInvoker.endRegistration(listener);
}
std::unordered_set<ListenerCallbacks, ListenerCallbacksHash> listenerCallbacksWithSurfaces;
uint32_t clientStateFlags = 0;
for (const ComposerState& state : states) {
clientStateFlags |=
setClientStateLocked(frameTimelineInfo, state, desiredPresentTime, isAutoTimestamp,
postTime, permissions, listenerCallbacksWithSurfaces);
if ((flags & eAnimation) && state.state.surface) {
if (const auto layer = fromHandle(state.state.surface).promote(); layer) {
mScheduler->recordLayerHistory(layer.get(),
isAutoTimestamp ? 0 : desiredPresentTime,
LayerHistory::LayerUpdateType::AnimationTX);
}
}
}
for (const auto& listenerCallback : listenerCallbacksWithSurfaces) {
mTransactionCallbackInvoker.endRegistration(listenerCallback);
}
// If the state doesn't require a traversal and there are callbacks, send them now
if (!(clientStateFlags & eTraversalNeeded) && hasListenerCallbacks) {
mTransactionCallbackInvoker.sendCallbacks();
}
transactionFlags |= clientStateFlags;
listenerCallbacks
,调用 TransactionCallbackInvoker 的 startRegistration()
和 endRegistration()
。recordLayerHistory()
记录 layer。listenerCallbacksWithSurfaces
是在 setClientStateLocked()
设置的,sendCallbacks()
发送回调。void SurfaceFlinger::applyTransactionState(...
...
if (permissions & Permission::ACCESS_SURFACE_FLINGER) {
transactionFlags |= addInputWindowCommands(inputWindowCommands);
} else if (!inputWindowCommands.empty()) {
ALOGE("Only privileged callers are allowed to send input commands.");
}
if (uncacheBuffer.isValid()) {
ClientCache::getInstance().erase(uncacheBuffer);
}
inputWindowCommands
。uncacheBuffer
可用,说明有缓存失效,则从 ClientCache 中移除。 // If a synchronous transaction is explicitly requested without any changes, force a transaction
// anyway. This can be used as a flush mechanism for previous async transactions.
// Empty animation transaction can be used to simulate back-pressure, so also force a
// transaction for empty animation transactions.
if (transactionFlags == 0 &&
((flags & eSynchronous) || (flags & eAnimation))) {
transactionFlags = eTransactionNeeded;
}
if (transactionFlags) {
if (mInterceptor->isEnabled()) {
mInterceptor->saveTransaction(states, mCurrentState.displays, displays, flags,
originPid, originUid, transactionId);
}
// We are on the main thread, we are about to preform a traversal. Clear the traversal bit
// so we don't have to wake up again next frame to preform an unnecessary traversal.
if (transactionFlags & eTraversalNeeded) {
transactionFlags = transactionFlags & (~eTraversalNeeded);
mForceTraversal = true;
}
if (transactionFlags) {
setTransactionFlags(transactionFlags);
}
if (flags & eAnimation) {
mAnimTransactionPending = true;
}
}
}
eSynchronous
或 eAnimation
,则设置 eTransactionNeeded
。saveTransaction()
再看。applyTransactionState()
可能在主线程调用,在 onInitializeDisplays()
。为了避免主线程 traversal,先移除 eTraversalNeeded
,并设置 mForceTraversal
为 true。