当前位置:   article > 正文

Android13 SurfaceFlinger commit(提交)流程分析_surfaceflinger::commit

surfaceflinger::commit

SurfaceFlinger的commit方法用于将应用程序的绘制结果提交到屏幕上显示。

主要就是处理app端发起的一系列transaction的事务请求,需要对这些请求进行识别是否当前帧处理,处理过程就是把事务中的属性取出,然后更新到Layer中,偶尔buffer更新的还需要进行相关的latchbuffer操作,SurfaceFlinger的commit代码如下:

  1. //frameworks/native/services/surfaceflinger/Surfaceflinger.cpp
  2. bool SurfaceFlinger::commit(nsecs_t frameTime, int64_t vsyncId, nsecs_t expectedVsyncTime) //这里frameTime代表当前时间,expectedVsyncTime代表硬件vsync时间,即屏幕先的vsync时间
  3. FTL_FAKE_GUARD(kMainThreadContext) {
  4. // we set this once at the beginning of commit to ensure consistency throughout the whole frame
  5. mPowerHintSessionData.sessionEnabled = mPowerAdvisor->usePowerHintSession();
  6. if (mPowerHintSessionData.sessionEnabled) {
  7. mPowerHintSessionData.commitStart = systemTime();
  8. }
  9. // calculate the expected present time once and use the cached
  10. // value throughout this frame to make sure all layers are
  11. // seeing this same value.
  12. if (expectedVsyncTime >= frameTime) {
  13. mExpectedPresentTime = expectedVsyncTime;
  14. } else {
  15. const DisplayStatInfo stats = mScheduler->getDisplayStatInfo(frameTime);
  16. mExpectedPresentTime = calculateExpectedPresentTime(stats);
  17. }
  18. const nsecs_t lastScheduledPresentTime = mScheduledPresentTime;
  19. mScheduledPresentTime = expectedVsyncTime;
  20. if (mPowerHintSessionData.sessionEnabled) {
  21. mPowerAdvisor->setTargetWorkDuration(mExpectedPresentTime -
  22. mPowerHintSessionData.commitStart);
  23. }
  24. const auto vsyncIn = [&] {
  25. if (!ATRACE_ENABLED()) return 0.f;
  26. return (mExpectedPresentTime - systemTime()) / 1e6f;
  27. }();
  28. ATRACE_FORMAT("%s %" PRId64 " vsyncIn %.2fms%s", __func__, vsyncId, vsyncIn,
  29. mExpectedPresentTime == expectedVsyncTime ? "" : " (adjusted)");
  30. // When Backpressure propagation is enabled we want to give a small grace period
  31. // for the present fence to fire instead of just giving up on this frame to handle cases
  32. // where present fence is just about to get signaled.
  33. const int graceTimeForPresentFenceMs =
  34. (mPropagateBackpressureClientComposition || !mHadClientComposition) ? 1 : 0;
  35. // Pending frames may trigger backpressure propagation.
  36. const TracedOrdinal<bool> framePending = {"PrevFramePending",
  37. previousFramePending(graceTimeForPresentFenceMs)};
  38. // Frame missed counts for metrics tracking.
  39. // A frame is missed if the prior frame is still pending. If no longer pending,
  40. // then we still count the frame as missed if the predicted present time
  41. // was further in the past than when the fence actually fired.
  42. // Add some slop to correct for drift. This should generally be
  43. // smaller than a typical frame duration, but should not be so small
  44. // that it reports reasonable drift as a missed frame.
  45. const DisplayStatInfo stats = mScheduler->getDisplayStatInfo(systemTime());
  46. const nsecs_t frameMissedSlop = stats.vsyncPeriod / 2;
  47. const nsecs_t previousPresentTime = previousFramePresentTime();
  48. const TracedOrdinal<bool> frameMissed = {"PrevFrameMissed",
  49. framePending ||
  50. (previousPresentTime >= 0 &&
  51. (lastScheduledPresentTime <
  52. previousPresentTime - frameMissedSlop))};
  53. const TracedOrdinal<bool> hwcFrameMissed = {"PrevHwcFrameMissed",
  54. mHadDeviceComposition && frameMissed};
  55. const TracedOrdinal<bool> gpuFrameMissed = {"PrevGpuFrameMissed",
  56. mHadClientComposition && frameMissed};
  57. if (frameMissed) {
  58. mFrameMissedCount++;
  59. mTimeStats->incrementMissedFrames();
  60. }
  61. if (hwcFrameMissed) {
  62. mHwcFrameMissedCount++;
  63. }
  64. if (gpuFrameMissed) {
  65. mGpuFrameMissedCount++;
  66. }
  67. // If we are in the middle of a mode change and the fence hasn't
  68. // fired yet just wait for the next commit.
  69. // 如果我们正处于模式更改的过程中,并且围栏尚未触发,请等待下一次提交。
  70. if (mSetActiveModePending) {
  71. if (framePending) {
  72. mScheduler->scheduleFrame();
  73. return false;
  74. }
  75. // We received the present fence from the HWC, so we assume it successfully updated
  76. // the mode, hence we update SF.
  77. mSetActiveModePending = false;
  78. {
  79. Mutex::Autolock lock(mStateLock);
  80. updateInternalStateWithChangedMode();
  81. }
  82. }
  83. if (framePending) {
  84. if ((hwcFrameMissed && !gpuFrameMissed) || mPropagateBackpressureClientComposition) {
  85. scheduleCommit(FrameHint::kNone);
  86. return false;
  87. }
  88. }
  89. if (mTracingEnabledChanged) {
  90. mLayerTracingEnabled = mLayerTracing.isEnabled();
  91. mTracingEnabledChanged = false;
  92. }
  93. if (mRefreshRateOverlaySpinner) {
  94. Mutex::Autolock lock(mStateLock);
  95. if (const auto display = getDefaultDisplayDeviceLocked()) {
  96. display->animateRefreshRateOverlay();
  97. }
  98. }
  99. // Composite if transactions were committed, or if requested by HWC.
  100. bool mustComposite = mMustComposite.exchange(false);
  101. {
  102. mFrameTimeline->setSfWakeUp(vsyncId, frameTime, Fps::fromPeriodNsecs(stats.vsyncPeriod));
  103. bool needsTraversal = false;
  104. if (clearTransactionFlags(eTransactionFlushNeeded)) { //满足eTransactionFlushNeeded条件进入
  105. needsTraversal |= commitCreatedLayers(); //负责新创建的layer相关业务处理
  106. needsTraversal |= flushTransactionQueues(vsyncId); //这里是对前面的Transaction处理的核心部分
  107. }
  108. const bool shouldCommit =
  109. (getTransactionFlags() & ~eTransactionFlushNeeded) || needsTraversal;
  110. if (shouldCommit) {
  111. commitTransactions(); //进行Transaction提交主要就是交换mCurrentState和DrawingState
  112. }
  113. if (transactionFlushNeeded()) { //判断是否又要启动新vsync信号
  114. setTransactionFlags(eTransactionFlushNeeded);
  115. }
  116. mustComposite |= shouldCommit;
  117. mustComposite |= latchBuffers(); //进行核心的latchBuffer
  118. // This has to be called after latchBuffers because we want to include the layers that have
  119. // been latched in the commit callback
  120. if (!needsTraversal) {
  121. // Invoke empty transaction callbacks early.
  122. mTransactionCallbackInvoker.sendCallbacks(false /* onCommitOnly */);
  123. } else {
  124. // Invoke OnCommit callbacks.
  125. mTransactionCallbackInvoker.sendCallbacks(true /* onCommitOnly */);
  126. }
  127. updateLayerGeometry(); //对要这次vsync显示刷新的layer进行脏区设置
  128. }
  129. // Layers need to get updated (in the previous line) before we can use them for
  130. // choosing the refresh rate.
  131. // Hold mStateLock as chooseRefreshRateForContent promotes wp<Layer> to sp<Layer>
  132. // and may eventually call to ~Layer() if it holds the last reference
  133. {
  134. Mutex::Autolock _l(mStateLock);
  135. mScheduler->chooseRefreshRateForContent();
  136. setActiveModeInHwcIfNeeded();
  137. }
  138. updateCursorAsync(); //鼠标相关layer处理
  139. updateInputFlinger(); //更新触摸input下的相关的window等,这里也非常关键哈,直接影响触摸是否到app
  140. if (mLayerTracingEnabled && !mLayerTracing.flagIsSet(LayerTracing::TRACE_COMPOSITION)) {
  141. // This will block and tracing should only be enabled for debugging.
  142. mLayerTracing.notify(mVisibleRegionsDirty, frameTime);
  143. }
  144. persistDisplayBrightness(mustComposite);
  145. return mustComposite && CC_LIKELY(mBootStage != BootStage::BOOTLOADER);
  146. }

上面方法主要处理如下:

1、调用SurfaceFlinger的commitCreatedLayers,创建的layer。

2、调用SurfaceFlinger的flushTransactionQueues方法。

3、调用SurfaceFlinger的commitTransactions方法,进行Transaction提交。

4、调用SurfaceFlinger的latchBuffers方法,将应用程序的图形缓冲区(buffer)与显示器进行关联。

5、调用SurfaceFlinger的updateLayerGeometry方法,这次vsync显示刷新的layer进行脏区设置。

6、调用SurfaceFlinger的updateInputFlinger方法,更新输入处理。

下面分别进行分析:

SurfaceFlinger commitCreatedLayers

调用SurfaceFlinger的commitCreatedLayers,创建的layer:

  1. //frameworks/native/services/surfaceflinger/Surfaceflinger.cpp
  2. bool SurfaceFlinger::commitCreatedLayers() {
  3. std::vector<LayerCreatedState> createdLayers;
  4. {
  5. std::scoped_lock<std::mutex> lock(mCreatedLayersLock);
  6. createdLayers = std::move(mCreatedLayers);
  7. mCreatedLayers.clear();
  8. if (createdLayers.size() == 0) {
  9. return false;
  10. }
  11. }
  12. Mutex::Autolock _l(mStateLock);
  13. for (const auto& createdLayer : createdLayers) {
  14. handleLayerCreatedLocked(createdLayer);
  15. }
  16. createdLayers.clear();
  17. mLayersAdded = true;
  18. return true;
  19. }

SurfaceFlinger flushTransactionQueues

调用SurfaceFlinger的flushTransactionQueues方法:

  1. //frameworks/native/services/surfaceflinger/Surfaceflinger.cpp
  2. bool SurfaceFlinger::flushTransactionQueues(int64_t vsyncId) {
  3. // to prevent onHandleDestroyed from being called while the lock is held,
  4. // we must keep a copy of the transactions (specifically the composer
  5. // states) around outside the scope of the lock
  6. std::vector<TransactionState> transactions;
  7. // Layer handles that have transactions with buffers that are ready to be applied.
  8. std::unordered_map<sp<IBinder>, uint64_t, SpHash<IBinder>> bufferLayersReadyToPresent;
  9. std::unordered_set<sp<IBinder>, SpHash<IBinder>> applyTokensWithUnsignaledTransactions;
  10. {
  11. Mutex::Autolock _l(mStateLock);
  12. {
  13. Mutex::Autolock _l(mQueueLock);
  14. int lastTransactionsPendingBarrier = 0;
  15. int transactionsPendingBarrier = 0;
  16. // First collect transactions from the pending transaction queues.
  17. // We are not allowing unsignaled buffers here as we want to
  18. // collect all the transactions from applyTokens that are ready first.
  19. //刷新一下PendingTransactionQueues,这个主要是上次vsync中没有满足条件的Transaction放入的
  20. transactionsPendingBarrier =
  21. flushPendingTransactionQueues(transactions, bufferLayersReadyToPresent,
  22. applyTokensWithUnsignaledTransactions, /*tryApplyUnsignaled*/ false);
  23. // Second, collect transactions from the transaction queue.
  24. // Here as well we are not allowing unsignaled buffers for the same
  25. // reason as above.
  26. //开始遍历当次vsync的mTransactionQueue
  27. while (!mTransactionQueue.empty()) {
  28. auto& transaction = mTransactionQueue.front();
  29. //判断是否处于mPendingTransactionQueues队里里面
  30. const bool pendingTransactions =
  31. mPendingTransactionQueues.find(transaction.applyToken) !=
  32. mPendingTransactionQueues.end();
  33. //这里有个ready的判断,主要就是看看当前的Transaction是否已经ready,没有ready则不予进行传递Transaction
  34. const auto ready = [&]() REQUIRES(mStateLock) {
  35. if (pendingTransactions) {
  36. ATRACE_NAME("pendingTransactions");
  37. return TransactionReadiness::NotReady;
  38. }
  39. return transactionIsReadyToBeApplied(transaction, transaction.frameTimelineInfo,
  40. transaction.isAutoTimestamp,
  41. transaction.desiredPresentTime,
  42. transaction.originUid, transaction.states,
  43. bufferLayersReadyToPresent,
  44. transactions.size(),
  45. /*tryApplyUnsignaled*/ false); //检查当前的事务是否准备好被应用
  46. }();
  47. ATRACE_INT("TransactionReadiness", static_cast<int>(ready));
  48. if (ready != TransactionReadiness::Ready) {
  49. if (ready == TransactionReadiness::NotReadyBarrier) {
  50. transactionsPendingBarrier++;
  51. }
  52. //放入mPendingTransactionQueues
  53. mPendingTransactionQueues[transaction.applyToken].push(std::move(transaction));
  54. } else {
  55. //已经ready则进入如下的操作
  56. transaction.traverseStatesWithBuffers([&](const layer_state_t& state) {
  57. const bool frameNumberChanged = state.bufferData->flags.test(
  58. BufferData::BufferDataChange::frameNumberChanged);
  59. //会把state放入到bufferLayersReadyToPresent这个map中
  60. if (frameNumberChanged) {
  61. bufferLayersReadyToPresent[state.surface] = state.bufferData->frameNumber;
  62. } else {
  63. // Barrier function only used for BBQ which always includes a frame number.
  64. // This value only used for barrier logic.
  65. bufferLayersReadyToPresent[state.surface] =
  66. std::numeric_limits<uint64_t>::max();
  67. }
  68. });
  69. //最重要的放入到transactions
  70. transactions.emplace_back(std::move(transaction));
  71. }
  72. //从mTransactionQueue这里移除
  73. mTransactionQueue.pop_front();
  74. ATRACE_INT("TransactionQueue", mTransactionQueue.size());
  75. }
  76. // Transactions with a buffer pending on a barrier may be on a different applyToken
  77. // than the transaction which satisfies our barrier. In fact this is the exact use case
  78. // that the primitive is designed for. This means we may first process
  79. // the barrier dependent transaction, determine it ineligible to complete
  80. // and then satisfy in a later inner iteration of flushPendingTransactionQueues.
  81. // The barrier dependent transaction was eligible to be presented in this frame
  82. // but we would have prevented it without case. To fix this we continually
  83. // loop through flushPendingTransactionQueues until we perform an iteration
  84. // where the number of transactionsPendingBarrier doesn't change. This way
  85. // we can continue to resolve dependency chains of barriers as far as possible.
  86. while (lastTransactionsPendingBarrier != transactionsPendingBarrier) {
  87. lastTransactionsPendingBarrier = transactionsPendingBarrier;
  88. transactionsPendingBarrier =
  89. flushPendingTransactionQueues(transactions, bufferLayersReadyToPresent,
  90. applyTokensWithUnsignaledTransactions,
  91. /*tryApplyUnsignaled*/ false);
  92. }
  93. // We collected all transactions that could apply without latching unsignaled buffers.
  94. // If we are allowing latch unsignaled of some form, now it's the time to go over the
  95. // transactions that were not applied and try to apply them unsignaled.
  96. //根据enableLatchUnsignaledConfig属性,不是disabled的话需要对前面的notready情况进行第二次的校验放过
  97. if (enableLatchUnsignaledConfig != LatchUnsignaledConfig::Disabled) {
  98. flushUnsignaledPendingTransactionQueues(transactions, bufferLayersReadyToPresent,
  99. applyTokensWithUnsignaledTransactions);
  100. }
  101. //进行关键的applyTransactions操作
  102. return applyTransactions(transactions, vsyncId);
  103. }
  104. }
  105. }

上面方法的主要处理如下:

1、调用SurfaceFlinger的transactionIsReadyToBeApplied方法,检查当前的事务是否准备好被应用

2、调用SurfaceFlinger的applyTransactions方法,应用事务。

下面分别进行分析:

SurfaceFlinger transactionIsReadyToBeApplied

调用SurfaceFlinger的transactionIsReadyToBeApplied方法,检查当前的事务是否准备好被应用:

  1. //frameworks/native/services/surfaceflinger/Surfaceflinger.cpp
  2. auto SurfaceFlinger::transactionIsReadyToBeApplied(TransactionState& transaction,
  3. const FrameTimelineInfo& info, bool isAutoTimestamp, int64_t desiredPresentTime,
  4. uid_t originUid, const Vector<ComposerState>& states,
  5. const std::unordered_map<
  6. sp<IBinder>, uint64_t, SpHash<IBinder>>& bufferLayersReadyToPresent,
  7. size_t totalTXapplied, bool tryApplyUnsignaled) const -> TransactionReadiness {
  8. ATRACE_FORMAT("transactionIsReadyToBeApplied vsyncId: %" PRId64, info.vsyncId);
  9. const nsecs_t expectedPresentTime = mExpectedPresentTime.load();
  10. // Do not present if the desiredPresentTime has not passed unless it is more than one second
  11. // in the future. We ignore timestamps more than 1 second in the future for stability reasons.
  12. if (!isAutoTimestamp && desiredPresentTime >= expectedPresentTime &&
  13. desiredPresentTime < expectedPresentTime + s2ns(1)) {
  14. ATRACE_NAME("not current");
  15. return TransactionReadiness::NotReady;
  16. }
  17. if (!mScheduler->isVsyncValid(expectedPresentTime, originUid)) {
  18. ATRACE_NAME("!isVsyncValid");
  19. return TransactionReadiness::NotReady;
  20. }
  21. // If the client didn't specify desiredPresentTime, use the vsyncId to determine the expected
  22. // present time of this transaction.
  23. if (isAutoTimestamp && frameIsEarly(expectedPresentTime, info.vsyncId)) {
  24. ATRACE_NAME("frameIsEarly");
  25. return TransactionReadiness::NotReady;
  26. }
  27. bool fenceUnsignaled = false;
  28. auto queueProcessTime = systemTime();
  29. //关键部分对transaction的states进行挨个state遍历情况
  30. for (const ComposerState& state : states) {
  31. const layer_state_t& s = state.state;
  32. sp<Layer> layer = nullptr;
  33. //这里会判断到底有没有surface。没有的话就不会进入下面判断环节
  34. if (s.surface) {
  35. layer = fromHandle(s.surface).promote();
  36. } else if (s.hasBufferChanges()) {
  37. ALOGW("Transaction with buffer, but no Layer?");
  38. continue;
  39. }
  40. if (!layer) {
  41. continue;
  42. }
  43. if (s.hasBufferChanges() && s.bufferData->hasBarrier &&
  44. ((layer->getDrawingState().frameNumber) < s.bufferData->barrierFrameNumber)) {
  45. const bool willApplyBarrierFrame =
  46. (bufferLayersReadyToPresent.find(s.surface) != bufferLayersReadyToPresent.end()) &&
  47. (bufferLayersReadyToPresent.at(s.surface) >= s.bufferData->barrierFrameNumber);
  48. if (!willApplyBarrierFrame) {
  49. ATRACE_NAME("NotReadyBarrier");
  50. return TransactionReadiness::NotReadyBarrier;
  51. }
  52. }
  53. //注意这里会有一个标志allowLatchUnsignaled意思是是否可以latch非signaled的buffer
  54. const bool allowLatchUnsignaled = tryApplyUnsignaled &&
  55. shouldLatchUnsignaled(layer, s, states.size(), totalTXapplied);
  56. ATRACE_FORMAT("%s allowLatchUnsignaled=%s", layer->getName().c_str(),
  57. allowLatchUnsignaled ? "true" : "false");
  58. const bool acquireFenceChanged = s.bufferData &&
  59. s.bufferData->flags.test(BufferData::BufferDataChange::fenceChanged) &&
  60. s.bufferData->acquireFence;
  61. //这里会进行关键的判断,判断state的bufferData->acquireFence是否已经signaled
  62. fenceUnsignaled = fenceUnsignaled ||
  63. (acquireFenceChanged &&
  64. s.bufferData->acquireFence->getStatus() == Fence::Status::Unsignaled);
  65. //如果fenceUnsignaled属于fenceUnsignaled,allowLatchUnsignaled也为false
  66. if (fenceUnsignaled && !allowLatchUnsignaled) {
  67. if (!transaction.sentFenceTimeoutWarning &&
  68. queueProcessTime - transaction.queueTime > std::chrono::nanoseconds(4s).count()) {
  69. transaction.sentFenceTimeoutWarning = true;
  70. auto listener = s.bufferData->releaseBufferListener;
  71. if (listener) {
  72. listener->onTransactionQueueStalled();
  73. }
  74. }
  75. //那么就代表不可以传递transaction,会返回NotReady
  76. ATRACE_NAME("fence unsignaled");
  77. return TransactionReadiness::NotReady;
  78. }
  79. if (s.hasBufferChanges()) {
  80. // If backpressure is enabled and we already have a buffer to commit, keep the
  81. // transaction in the queue.
  82. const bool hasPendingBuffer = bufferLayersReadyToPresent.find(s.surface) !=
  83. bufferLayersReadyToPresent.end();
  84. //如果前面已经有state放入了,则不在放入,会放入pending
  85. if (layer->backpressureEnabled() && hasPendingBuffer && isAutoTimestamp) {
  86. ATRACE_NAME("hasPendingBuffer");
  87. return TransactionReadiness::NotReady;
  88. }
  89. }
  90. }
  91. return fenceUnsignaled ? TransactionReadiness::ReadyUnsignaled : TransactionReadiness::Ready;
  92. }

SurfaceFlinger applyTransactions

调用SurfaceFlinger的applyTransactions方法,应用事务:

  1. //frameworks/native/services/surfaceflinger/Surfaceflinger.cpp
  2. bool SurfaceFlinger::applyTransactions(std::vector<TransactionState>& transactions,
  3. int64_t vsyncId) {
  4. bool needsTraversal = false;
  5. // Now apply all transactions.
  6. //遍历每一个上面判断为ready的transaction
  7. for (auto& transaction : transactions) {
  8. //核心方法又调用到了applyTransactionState
  9. needsTraversal |=
  10. applyTransactionState(transaction.frameTimelineInfo, transaction.states,
  11. transaction.displays, transaction.flags,
  12. transaction.inputWindowCommands,
  13. transaction.desiredPresentTime, transaction.isAutoTimestamp,
  14. transaction.buffer, transaction.postTime,
  15. transaction.permissions, transaction.hasListenerCallbacks,
  16. transaction.listenerCallbacks, transaction.originPid,
  17. transaction.originUid, transaction.id);
  18. if (transaction.transactionCommittedSignal) {
  19. mTransactionCommittedSignals.emplace_back(
  20. std::move(transaction.transactionCommittedSignal));
  21. }
  22. }
  23. if (mTransactionTracing) {
  24. mTransactionTracing->addCommittedTransactions(transactions, vsyncId);
  25. }
  26. return needsTraversal;
  27. }

SurfaceFlinger applyTransactionState

调用SurfaceFlinger的applyTransactionState方法:

  1. //frameworks/native/services/surfaceflinger/Surfaceflinger.cpp
  2. bool SurfaceFlinger::applyTransactionState(const FrameTimelineInfo& frameTimelineInfo,
  3. Vector<ComposerState>& states,
  4. const Vector<DisplayState>& displays, uint32_t flags,
  5. const InputWindowCommands& inputWindowCommands,
  6. const int64_t desiredPresentTime, bool isAutoTimestamp,
  7. const client_cache_t& uncacheBuffer,
  8. const int64_t postTime, uint32_t permissions,
  9. bool hasListenerCallbacks,
  10. const std::vector<ListenerCallbacks>& listenerCallbacks,
  11. int originPid, int originUid, uint64_t transactionId) {
  12. uint32_t transactionFlags = 0;
  13. //遍历是transaction的dispplays
  14. for (const DisplayState& display : displays) {
  15. //遍历是否有dispplay相关的变化
  16. transactionFlags |= setDisplayStateLocked(display);
  17. }
  18. // start and end registration for listeners w/ no surface so they can get their callback. Note
  19. // that listeners with SurfaceControls will start registration during setClientStateLocked
  20. // below.
  21. for (const auto& listener : listenerCallbacks) {
  22. mTransactionCallbackInvoker.addEmptyTransaction(listener);
  23. }
  24. uint32_t clientStateFlags = 0;
  25. for (int i = 0; i < states.size(); i++) { //最关键方法开始遍历一个个的states
  26. ComposerState& state = states.editItemAt(i);
  27. //调用到了setClientStateLocked方法
  28. clientStateFlags |= setClientStateLocked(frameTimelineInfo, state, desiredPresentTime,
  29. isAutoTimestamp, postTime, permissions);
  30. if ((flags & eAnimation) && state.state.surface) {
  31. if (const auto layer = fromHandle(state.state.surface).promote()) {
  32. using LayerUpdateType = scheduler::LayerHistory::LayerUpdateType;
  33. mScheduler->recordLayerHistory(layer.get(),
  34. isAutoTimestamp ? 0 : desiredPresentTime,
  35. LayerUpdateType::AnimationTX);
  36. }
  37. }
  38. }
  39. transactionFlags |= clientStateFlags;
  40. if (permissions & layer_state_t::Permission::ACCESS_SURFACE_FLINGER) {
  41. transactionFlags |= addInputWindowCommands(inputWindowCommands);
  42. } else if (!inputWindowCommands.empty()) {
  43. ALOGE("Only privileged callers are allowed to send input commands.");
  44. }
  45. if (uncacheBuffer.isValid()) {
  46. ClientCache::getInstance().erase(uncacheBuffer);
  47. }
  48. // If a synchronous transaction is explicitly requested without any changes, force a transaction
  49. // anyway. This can be used as a flush mechanism for previous async transactions.
  50. // Empty animation transaction can be used to simulate back-pressure, so also force a
  51. // transaction for empty animation transactions.
  52. if (transactionFlags == 0 &&
  53. ((flags & eSynchronous) || (flags & eAnimation))) {
  54. transactionFlags = eTransactionNeeded;
  55. }
  56. bool needsTraversal = false;
  57. if (transactionFlags) {
  58. if (mInterceptor->isEnabled()) {
  59. mInterceptor->saveTransaction(states, mCurrentState.displays, displays, flags,
  60. originPid, originUid, transactionId);
  61. }
  62. // We are on the main thread, we are about to preform a traversal. Clear the traversal bit
  63. // so we don't have to wake up again next frame to preform an unnecessary traversal.
  64. if (transactionFlags & eTraversalNeeded) {
  65. transactionFlags = transactionFlags & (~eTraversalNeeded);
  66. needsTraversal = true;
  67. }
  68. if (transactionFlags) {
  69. setTransactionFlags(transactionFlags);
  70. }
  71. if (flags & eAnimation) {
  72. mAnimTransactionPending = true;
  73. }
  74. }
  75. return needsTraversal;
  76. }
SurfaceFlinger setClientStateLocked

调用SurfaceFlinger的setClientStateLocked方法:

  1. //frameworks/native/services/surfaceflinger/Surfaceflinger.cpp
  2. uint32_t SurfaceFlinger::setClientStateLocked(const FrameTimelineInfo& frameTimelineInfo,
  3. ComposerState& composerState,
  4. int64_t desiredPresentTime, bool isAutoTimestamp,
  5. int64_t postTime, uint32_t permissions) {
  6. layer_state_t& s = composerState.state;
  7. s.sanitize(permissions);
  8. std::vector<ListenerCallbacks> filteredListeners;
  9. for (auto& listener : s.listeners) {
  10. // Starts a registration but separates the callback ids according to callback type. This
  11. // allows the callback invoker to send on latch callbacks earlier.
  12. // note that startRegistration will not re-register if the listener has
  13. // already be registered for a prior surface control
  14. ListenerCallbacks onCommitCallbacks = listener.filter(CallbackId::Type::ON_COMMIT);
  15. if (!onCommitCallbacks.callbackIds.empty()) {
  16. filteredListeners.push_back(onCommitCallbacks);
  17. }
  18. ListenerCallbacks onCompleteCallbacks = listener.filter(CallbackId::Type::ON_COMPLETE);
  19. if (!onCompleteCallbacks.callbackIds.empty()) {
  20. filteredListeners.push_back(onCompleteCallbacks);
  21. }
  22. }
  23. const uint64_t what = s.what;
  24. uint32_t flags = 0;
  25. sp<Layer> layer = nullptr;
  26. if (s.surface) {
  27. layer = fromHandle(s.surface).promote();
  28. } else {
  29. // The client may provide us a null handle. Treat it as if the layer was removed.
  30. ALOGW("Attempt to set client state with a null layer handle");
  31. }
  32. if (layer == nullptr) {
  33. for (auto& [listener, callbackIds] : s.listeners) {
  34. mTransactionCallbackInvoker.registerUnpresentedCallbackHandle(
  35. new CallbackHandle(listener, callbackIds, s.surface));
  36. }
  37. return 0;
  38. }
  39. // Only set by BLAST adapter layers
  40. if (what & layer_state_t::eProducerDisconnect) {
  41. layer->onDisconnect();
  42. }
  43. if (what & layer_state_t::ePositionChanged) {
  44. if (layer->setPosition(s.x, s.y)) {
  45. flags |= eTraversalNeeded;
  46. }
  47. }
  48. if (what & layer_state_t::eLayerChanged) {
  49. // NOTE: index needs to be calculated before we update the state
  50. const auto& p = layer->getParent();
  51. if (p == nullptr) {
  52. ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
  53. if (layer->setLayer(s.z) && idx >= 0) {
  54. mCurrentState.layersSortedByZ.removeAt(idx);
  55. mCurrentState.layersSortedByZ.add(layer);
  56. // we need traversal (state changed)
  57. // AND transaction (list changed)
  58. flags |= eTransactionNeeded|eTraversalNeeded;
  59. }
  60. } else {
  61. if (p->setChildLayer(layer, s.z)) {
  62. flags |= eTransactionNeeded|eTraversalNeeded;
  63. }
  64. }
  65. }
  66. if (what & layer_state_t::eRelativeLayerChanged) {
  67. // NOTE: index needs to be calculated before we update the state
  68. const auto& p = layer->getParent();
  69. const auto& relativeHandle = s.relativeLayerSurfaceControl ?
  70. s.relativeLayerSurfaceControl->getHandle() : nullptr;
  71. if (p == nullptr) {
  72. ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
  73. if (layer->setRelativeLayer(relativeHandle, s.z) &&
  74. idx >= 0) {
  75. mCurrentState.layersSortedByZ.removeAt(idx);
  76. mCurrentState.layersSortedByZ.add(layer);
  77. // we need traversal (state changed)
  78. // AND transaction (list changed)
  79. flags |= eTransactionNeeded|eTraversalNeeded;
  80. }
  81. } else {
  82. if (p->setChildRelativeLayer(layer, relativeHandle, s.z)) {
  83. flags |= eTransactionNeeded|eTraversalNeeded;
  84. }
  85. }
  86. }
  87. if (what & layer_state_t::eSizeChanged) {
  88. if (layer->setSize(s.w, s.h)) {
  89. flags |= eTraversalNeeded;
  90. }
  91. }
  92. if (what & layer_state_t::eAlphaChanged) {
  93. if (layer->setAlpha(s.alpha))
  94. flags |= eTraversalNeeded;
  95. }
  96. if (what & layer_state_t::eColorChanged) {
  97. if (layer->setColor(s.color))
  98. flags |= eTraversalNeeded;
  99. }
  100. if (what & layer_state_t::eColorTransformChanged) {
  101. if (layer->setColorTransform(s.colorTransform)) {
  102. flags |= eTraversalNeeded;
  103. }
  104. }
  105. if (what & layer_state_t::eBackgroundColorChanged) {
  106. if (layer->setBackgroundColor(s.color, s.bgColorAlpha, s.bgColorDataspace)) {
  107. flags |= eTraversalNeeded;
  108. }
  109. }
  110. if (what & layer_state_t::eMatrixChanged) {
  111. if (layer->setMatrix(s.matrix)) flags |= eTraversalNeeded;
  112. }
  113. if (what & layer_state_t::eTransparentRegionChanged) {
  114. if (layer->setTransparentRegionHint(s.transparentRegion))
  115. flags |= eTraversalNeeded;
  116. }
  117. if (what & layer_state_t::eFlagsChanged) {
  118. if (layer->setFlags(s.flags, s.mask)) flags |= eTraversalNeeded;
  119. }
  120. if (what & layer_state_t::eCornerRadiusChanged) {
  121. if (layer->setCornerRadius(s.cornerRadius))
  122. flags |= eTraversalNeeded;
  123. }
  124. if (what & layer_state_t::eBackgroundBlurRadiusChanged && mSupportsBlur) {
  125. if (layer->setBackgroundBlurRadius(s.backgroundBlurRadius)) flags |= eTraversalNeeded;
  126. }
  127. if (what & layer_state_t::eBlurRegionsChanged) {
  128. if (layer->setBlurRegions(s.blurRegions)) flags |= eTraversalNeeded;
  129. }
  130. if (what & layer_state_t::eLayerStackChanged) {
  131. ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
  132. // We only allow setting layer stacks for top level layers,
  133. // everything else inherits layer stack from its parent.
  134. if (layer->hasParent()) {
  135. ALOGE("Attempt to set layer stack on layer with parent (%s) is invalid",
  136. layer->getDebugName());
  137. } else if (idx < 0) {
  138. ALOGE("Attempt to set layer stack on layer without parent (%s) that "
  139. "that also does not appear in the top level layer list. Something"
  140. " has gone wrong.",
  141. layer->getDebugName());
  142. } else if (layer->setLayerStack(s.layerStack)) {
  143. mCurrentState.layersSortedByZ.removeAt(idx);
  144. mCurrentState.layersSortedByZ.add(layer);
  145. // we need traversal (state changed)
  146. // AND transaction (list changed)
  147. flags |= eTransactionNeeded | eTraversalNeeded | eTransformHintUpdateNeeded;
  148. }
  149. }
  150. if (what & layer_state_t::eTransformChanged) {
  151. if (layer->setTransform(s.transform)) flags |= eTraversalNeeded;
  152. }
  153. if (what & layer_state_t::eTransformToDisplayInverseChanged) {
  154. if (layer->setTransformToDisplayInverse(s.transformToDisplayInverse))
  155. flags |= eTraversalNeeded;
  156. }
  157. if (what & layer_state_t::eCropChanged) {
  158. if (layer->setCrop(s.crop)) flags |= eTraversalNeeded;
  159. }
  160. if (what & layer_state_t::eDataspaceChanged) {
  161. if (layer->setDataspace(s.dataspace)) flags |= eTraversalNeeded;
  162. }
  163. if (what & layer_state_t::eHdrMetadataChanged) {
  164. if (layer->setHdrMetadata(s.hdrMetadata)) flags |= eTraversalNeeded;
  165. }
  166. if (what & layer_state_t::eSurfaceDamageRegionChanged) {
  167. if (layer->setSurfaceDamageRegion(s.surfaceDamageRegion)) flags |= eTraversalNeeded;
  168. }
  169. if (what & layer_state_t::eApiChanged) {
  170. if (layer->setApi(s.api)) flags |= eTraversalNeeded;
  171. }
  172. if (what & layer_state_t::eSidebandStreamChanged) {
  173. if (layer->setSidebandStream(s.sidebandStream)) flags |= eTraversalNeeded;
  174. }
  175. if (what & layer_state_t::eInputInfoChanged) {
  176. layer->setInputInfo(*s.windowInfoHandle->getInfo());
  177. flags |= eTraversalNeeded;
  178. }
  179. std::optional<nsecs_t> dequeueBufferTimestamp;
  180. if (what & layer_state_t::eMetadataChanged) {
  181. dequeueBufferTimestamp = s.metadata.getInt64(METADATA_DEQUEUE_TIME);
  182. if (const int32_t gameMode = s.metadata.getInt32(METADATA_GAME_MODE, -1); gameMode != -1) {
  183. // The transaction will be received on the Task layer and needs to be applied to all
  184. // child layers. Child layers that are added at a later point will obtain the game mode
  185. // info through addChild().
  186. layer->setGameModeForTree(static_cast<GameMode>(gameMode));
  187. }
  188. if (layer->setMetadata(s.metadata)) flags |= eTraversalNeeded;
  189. }
  190. if (what & layer_state_t::eColorSpaceAgnosticChanged) {
  191. if (layer->setColorSpaceAgnostic(s.colorSpaceAgnostic)) {
  192. flags |= eTraversalNeeded;
  193. }
  194. }
  195. if (what & layer_state_t::eShadowRadiusChanged) {
  196. if (layer->setShadowRadius(s.shadowRadius)) flags |= eTraversalNeeded;
  197. }
  198. if (what & layer_state_t::eFrameRateSelectionPriority) {
  199. if (layer->setFrameRateSelectionPriority(s.frameRateSelectionPriority)) {
  200. flags |= eTraversalNeeded;
  201. }
  202. }
  203. if (what & layer_state_t::eFrameRateChanged) {
  204. const auto compatibility =
  205. Layer::FrameRate::convertCompatibility(s.frameRateCompatibility);
  206. const auto strategy =
  207. Layer::FrameRate::convertChangeFrameRateStrategy(s.changeFrameRateStrategy);
  208. if (layer->setFrameRate(
  209. Layer::FrameRate(Fps::fromValue(s.frameRate), compatibility, strategy))) {
  210. flags |= eTraversalNeeded;
  211. }
  212. }
  213. if (what & layer_state_t::eFixedTransformHintChanged) {
  214. if (layer->setFixedTransformHint(s.fixedTransformHint)) {
  215. flags |= eTraversalNeeded | eTransformHintUpdateNeeded;
  216. }
  217. }
  218. if (what & layer_state_t::eAutoRefreshChanged) {
  219. layer->setAutoRefresh(s.autoRefresh);
  220. }
  221. if (what & layer_state_t::eDimmingEnabledChanged) {
  222. if (layer->setDimmingEnabled(s.dimmingEnabled)) flags |= eTraversalNeeded;
  223. }
  224. if (what & layer_state_t::eTrustedOverlayChanged) {
  225. if (layer->setTrustedOverlay(s.isTrustedOverlay)) {
  226. flags |= eTraversalNeeded;
  227. }
  228. }
  229. if (what & layer_state_t::eStretchChanged) {
  230. if (layer->setStretchEffect(s.stretchEffect)) {
  231. flags |= eTraversalNeeded;
  232. }
  233. }
  234. if (what & layer_state_t::eBufferCropChanged) {
  235. if (layer->setBufferCrop(s.bufferCrop)) {
  236. flags |= eTraversalNeeded;
  237. }
  238. }
  239. if (what & layer_state_t::eDestinationFrameChanged) {
  240. if (layer->setDestinationFrame(s.destinationFrame)) {
  241. flags |= eTraversalNeeded;
  242. }
  243. }
  244. if (what & layer_state_t::eDropInputModeChanged) {
  245. if (layer->setDropInputMode(s.dropInputMode)) {
  246. flags |= eTraversalNeeded;
  247. mInputInfoChanged = true;
  248. }
  249. }
  250. // This has to happen after we reparent children because when we reparent to null we remove
  251. // child layers from current state and remove its relative z. If the children are reparented in
  252. // the same transaction, then we have to make sure we reparent the children first so we do not
  253. // lose its relative z order.
  254. if (what & layer_state_t::eReparent) {
  255. bool hadParent = layer->hasParent();
  256. auto parentHandle = (s.parentSurfaceControlForChild)
  257. ? s.parentSurfaceControlForChild->getHandle()
  258. : nullptr;
  259. if (layer->reparent(parentHandle)) {
  260. if (!hadParent) {
  261. layer->setIsAtRoot(false);
  262. mCurrentState.layersSortedByZ.remove(layer);
  263. }
  264. flags |= eTransactionNeeded | eTraversalNeeded;
  265. }
  266. }
  267. std::vector<sp<CallbackHandle>> callbackHandles;
  268. if ((what & layer_state_t::eHasListenerCallbacksChanged) && (!filteredListeners.empty())) {
  269. for (auto& [listener, callbackIds] : filteredListeners) {
  270. callbackHandles.emplace_back(new CallbackHandle(listener, callbackIds, s.surface));
  271. }
  272. }
  273. if (what & layer_state_t::eBufferChanged) { //如果发现有buffer变化
  274. std::shared_ptr<renderengine::ExternalTexture> buffer =
  275. getExternalTextureFromBufferData(*s.bufferData, layer->getDebugName());
  276. //把state的bufferData相关信息设置到了layer中去
  277. if (layer->setBuffer(buffer, *s.bufferData, postTime, desiredPresentTime, isAutoTimestamp,
  278. dequeueBufferTimestamp, frameTimelineInfo)) {
  279. flags |= eTraversalNeeded;
  280. }
  281. } else if (frameTimelineInfo.vsyncId != FrameTimelineInfo::INVALID_VSYNC_ID) {
  282. layer->setFrameTimelineVsyncForBufferlessTransaction(frameTimelineInfo, postTime);
  283. }
  284. if (layer->setTransactionCompletedListeners(callbackHandles)) flags |= eTraversalNeeded;
  285. // Do not put anything that updates layer state or modifies flags after
  286. // setTransactionCompletedListener
  287. return flags;
  288. }

SurfaceFlinger commitTransactions

调用SurfaceFlinger的commitTransactions方法,进行Transaction提交:

  1. //frameworks/native/services/surfaceflinger/Surfaceflinger.cpp
  2. void SurfaceFlinger::commitTransactions() {
  3. ATRACE_CALL();
  4. // Keep a copy of the drawing state (that is going to be overwritten
  5. // by commitTransactionsLocked) outside of mStateLock so that the side
  6. // effects of the State assignment don't happen with mStateLock held,
  7. // which can cause deadlocks.
  8. State drawingState(mDrawingState);
  9. Mutex::Autolock lock(mStateLock);
  10. mDebugInTransaction = systemTime();
  11. // Here we're guaranteed that some transaction flags are set
  12. // so we can call commitTransactionsLocked unconditionally.
  13. // We clear the flags with mStateLock held to guarantee that
  14. // mCurrentState won't change until the transaction is committed.
  15. modulateVsync(&VsyncModulator::onTransactionCommit);
  16. commitTransactionsLocked(clearTransactionFlags(eTransactionMask));
  17. mDebugInTransaction = 0;
  18. }

调用SurfaceFlinger的commitTransactionsLocked方法:

  1. //frameworks/native/services/surfaceflinger/Surfaceflinger.cpp
  2. void SurfaceFlinger::commitTransactionsLocked(uint32_t transactionFlags) {
  3. // Commit display transactions.
  4. const bool displayTransactionNeeded = transactionFlags & eDisplayTransactionNeeded;
  5. if (displayTransactionNeeded) { //如果需要显示
  6. processDisplayChangesLocked(); //处理显示变化相关
  7. processDisplayHotplugEventsLocked();
  8. }
  9. mForceTransactionDisplayChange = displayTransactionNeeded;
  10. if (mSomeChildrenChanged) {
  11. mVisibleRegionsDirty = true;
  12. mSomeChildrenChanged = false;
  13. }
  14. // Update transform hint.
  15. if (transactionFlags & (eTransformHintUpdateNeeded | eDisplayTransactionNeeded)) {
  16. // Layers and/or displays have changed, so update the transform hint for each layer.
  17. //
  18. // NOTE: we do this here, rather than when presenting the display so that
  19. // the hint is set before we acquire a buffer from the surface texture.
  20. //
  21. // NOTE: layer transactions have taken place already, so we use their
  22. // drawing state. However, SurfaceFlinger's own transaction has not
  23. // happened yet, so we must use the current state layer list
  24. // (soon to become the drawing state list).
  25. //
  26. sp<const DisplayDevice> hintDisplay;
  27. ui::LayerStack layerStack;
  28. mCurrentState.traverse([&](Layer* layer) REQUIRES(mStateLock) {
  29. // NOTE: we rely on the fact that layers are sorted by
  30. // layerStack first (so we don't have to traverse the list
  31. // of displays for every layer).
  32. if (const auto filter = layer->getOutputFilter(); layerStack != filter.layerStack) {
  33. layerStack = filter.layerStack;
  34. hintDisplay = nullptr;
  35. // Find the display that includes the layer.
  36. for (const auto& [token, display] : mDisplays) {
  37. if (!display->getCompositionDisplay()->includesLayer(filter)) {
  38. continue;
  39. }
  40. // Pick the primary display if another display mirrors the layer.
  41. if (hintDisplay) {
  42. hintDisplay = nullptr;
  43. break;
  44. }
  45. hintDisplay = display;
  46. }
  47. }
  48. if (!hintDisplay) {
  49. // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
  50. // redraw after transform hint changes. See bug 8508397.
  51. // could be null when this layer is using a layerStack
  52. // that is not visible on any display. Also can occur at
  53. // screen off/on times.
  54. hintDisplay = getDefaultDisplayDeviceLocked();
  55. }
  56. layer->updateTransformHint(hintDisplay->getTransformHint());
  57. });
  58. }
  59. if (mLayersAdded) {
  60. mLayersAdded = false;
  61. // Layers have been added.
  62. mVisibleRegionsDirty = true;
  63. }
  64. // some layers might have been removed, so
  65. // we need to update the regions they're exposing.
  66. if (mLayersRemoved) {
  67. mLayersRemoved = false;
  68. mVisibleRegionsDirty = true;
  69. mDrawingState.traverseInZOrder([&](Layer* layer) {
  70. if (mLayersPendingRemoval.indexOf(layer) >= 0) {
  71. // this layer is not visible anymore
  72. Region visibleReg;
  73. visibleReg.set(layer->getScreenBounds());
  74. invalidateLayerStack(layer, visibleReg);
  75. }
  76. });
  77. }
  78. doCommitTransactions();
  79. signalSynchronousTransactions(CountDownLatch::eSyncTransaction);
  80. mAnimTransactionPending = false;
  81. }

上面方法主要处理如下:

1、调用SurfaceFlinger的processDisplayChangesLocked方法,处理显示器变化相关。

2、调用SurfaceFlinger的doCommitTransactions方法,执行提交事务。

下面分别进行分析:

SurfaceFlinger processDisplayChangesLocked

调用SurfaceFlinger的processDisplayChangesLocked方法:

  1. //frameworks/native/services/surfaceflinger/Surfaceflinger.cpp
  2. void SurfaceFlinger::processDisplayChangesLocked() {
  3. // here we take advantage of Vector's copy-on-write semantics to
  4. // improve performance by skipping the transaction entirely when
  5. // know that the lists are identical
  6. const KeyedVector<wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
  7. const KeyedVector<wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
  8. if (!curr.isIdenticalTo(draw)) {
  9. mVisibleRegionsDirty = true;
  10. // find the displays that were removed
  11. // (ie: in drawing state but not in current state)
  12. // also handle displays that changed
  13. // (ie: displays that are in both lists)
  14. for (size_t i = 0; i < draw.size(); i++) {
  15. const wp<IBinder>& displayToken = draw.keyAt(i);
  16. const ssize_t j = curr.indexOfKey(displayToken);
  17. if (j < 0) {
  18. // in drawing state but not in current state
  19. // 处于绘图状态,但不在当前状态
  20. processDisplayRemoved(displayToken);
  21. } else {
  22. // this display is in both lists. see if something changed.
  23. // 此显示在两个列表中。看看是否有变化。
  24. const DisplayDeviceState& currentState = curr[j];
  25. const DisplayDeviceState& drawingState = draw[i];
  26. processDisplayChanged(displayToken, currentState, drawingState);
  27. }
  28. }
  29. // find displays that were added
  30. // 查找已添加的显示
  31. // (ie: in current state but not in drawing state)
  32. for (size_t i = 0; i < curr.size(); i++) {
  33. const wp<IBinder>& displayToken = curr.keyAt(i);
  34. if (draw.indexOfKey(displayToken) < 0) {
  35. processDisplayAdded(displayToken, curr[i]);
  36. }
  37. }
  38. }
  39. mDrawingState.displays = mCurrentState.displays;
  40. }

上面方法根据不同的调节调用如下方法:

1、调用processDisplayRemoved方法,处理显示器删除。

2、调用processDisplayChanged方法,处理显示器变更。

3、调用processDisplayAdded方法,处理显示器添加。

下面分别进行分析:

SurfaceFlinger processDisplayRemoved

调用processDisplayRemoved方法,处理显示器删除:

  1. //frameworks/native/services/surfaceflinger/Surfaceflinger.cpp
  2. void SurfaceFlinger::processDisplayRemoved(const wp<IBinder>& displayToken) {
  3. auto display = getDisplayDeviceLocked(displayToken);
  4. if (display) {
  5. display->disconnect(); //与Display断开连接
  6. if (display->isVirtual()) {
  7. releaseVirtualDisplay(display->getVirtualId()); //如果是虚拟显示器,就进行资源释放
  8. } else {
  9. dispatchDisplayHotplugEvent(display->getPhysicalId(), false); //分配显示器热插拔事件
  10. }
  11. }
  12. mDisplays.erase(displayToken);
  13. if (display && display->isVirtual()) {
  14. static_cast<void>(mScheduler->schedule([display = std::move(display)] {
  15. // Destroy the display without holding the mStateLock.
  16. // This is a temporary solution until we can manage transaction queues without
  17. // holding the mStateLock.
  18. // With blast, the IGBP that is passed to the VirtualDisplaySurface is owned by the
  19. // client. When the IGBP is disconnected, its buffer cache in SF will be cleared
  20. // via SurfaceComposerClient::doUncacheBufferTransaction. This call from the client
  21. // ends up running on the main thread causing a deadlock since setTransactionstate
  22. // will try to acquire the mStateLock. Instead we extend the lifetime of
  23. // DisplayDevice and destroy it in the main thread without holding the mStateLock.
  24. // The display will be disconnected and removed from the mDisplays list so it will
  25. // not be accessible.
  26. }));
  27. }
  28. }

SurfaceFlinger processDisplayChanged

调用processDisplayChanged方法,处理显示器变更:

  1. //frameworks/native/services/surfaceflinger/Surfaceflinger.cpp
  2. void SurfaceFlinger::processDisplayChanged(const wp<IBinder>& displayToken,
  3. const DisplayDeviceState& currentState,
  4. const DisplayDeviceState& drawingState) {
  5. const sp<IBinder> currentBinder = IInterface::asBinder(currentState.surface);
  6. const sp<IBinder> drawingBinder = IInterface::asBinder(drawingState.surface);
  7. // Recreate the DisplayDevice if the surface or sequence ID changed.
  8. // 如果图面或序列 ID 发生更改,请重新创建 DisplayDevice。
  9. if (currentBinder != drawingBinder || currentState.sequenceId != drawingState.sequenceId) {
  10. getRenderEngine().cleanFramebufferCache();
  11. if (const auto display = getDisplayDeviceLocked(displayToken)) {
  12. display->disconnect();
  13. if (display->isVirtual()) {
  14. releaseVirtualDisplay(display->getVirtualId());
  15. }
  16. }
  17. mDisplays.erase(displayToken);
  18. if (const auto& physical = currentState.physical) {
  19. getHwComposer().allocatePhysicalDisplay(physical->hwcDisplayId, physical->id);
  20. }
  21. processDisplayAdded(displayToken, currentState);
  22. if (currentState.physical) {
  23. const auto display = getDisplayDeviceLocked(displayToken);
  24. setPowerModeInternal(display, hal::PowerMode::ON);
  25. // TODO(b/175678251) Call a listener instead.
  26. if (currentState.physical->hwcDisplayId == getHwComposer().getPrimaryHwcDisplayId()) {
  27. updateInternalDisplayVsyncLocked(display);
  28. }
  29. }
  30. return;
  31. }
  32. if (const auto display = getDisplayDeviceLocked(displayToken)) {
  33. if (currentState.layerStack != drawingState.layerStack) {
  34. display->setLayerStack(currentState.layerStack); //设置显示器层堆栈
  35. }
  36. if (currentState.flags != drawingState.flags) {
  37. display->setFlags(currentState.flags); //设置显示器标志
  38. }
  39. if ((currentState.orientation != drawingState.orientation) ||
  40. (currentState.layerStackSpaceRect != drawingState.layerStackSpaceRect) ||
  41. (currentState.orientedDisplaySpaceRect != drawingState.orientedDisplaySpaceRect)) {
  42. display->setProjection(currentState.orientation, currentState.layerStackSpaceRect,
  43. currentState.orientedDisplaySpaceRect); //设置显示器投影
  44. if (isDisplayActiveLocked(display)) {
  45. mActiveDisplayTransformHint = display->getTransformHint();
  46. }
  47. }
  48. if (currentState.width != drawingState.width ||
  49. currentState.height != drawingState.height) {
  50. display->setDisplaySize(currentState.width, currentState.height); //设置显示器尺寸
  51. if (isDisplayActiveLocked(display)) {
  52. onActiveDisplaySizeChanged(display);
  53. }
  54. }
  55. }
  56. }

SurfaceFlinger processDisplayAdded

调用processDisplayAdded方法,处理显示器添加:

  1. //frameworks/native/services/surfaceflinger/Surfaceflinger.cpp
  2. void SurfaceFlinger::processDisplayAdded(const wp<IBinder>& displayToken,
  3. const DisplayDeviceState& state) {
  4. ui::Size resolution(0, 0);
  5. ui::PixelFormat pixelFormat = static_cast<ui::PixelFormat>(PIXEL_FORMAT_UNKNOWN);
  6. if (state.physical) {
  7. resolution = state.physical->activeMode->getResolution();
  8. pixelFormat = static_cast<ui::PixelFormat>(PIXEL_FORMAT_RGBA_8888);
  9. } else if (state.surface != nullptr) {
  10. int status = state.surface->query(NATIVE_WINDOW_WIDTH, &resolution.width);
  11. ALOGE_IF(status != NO_ERROR, "Unable to query width (%d)", status);
  12. status = state.surface->query(NATIVE_WINDOW_HEIGHT, &resolution.height);
  13. ALOGE_IF(status != NO_ERROR, "Unable to query height (%d)", status);
  14. int format;
  15. status = state.surface->query(NATIVE_WINDOW_FORMAT, &format);
  16. ALOGE_IF(status != NO_ERROR, "Unable to query format (%d)", status);
  17. pixelFormat = static_cast<ui::PixelFormat>(format);
  18. } else {
  19. // Virtual displays without a surface are dormant:
  20. // they have external state (layer stack, projection,
  21. // etc.) but no internal state (i.e. a DisplayDevice).
  22. return;
  23. }
  24. compositionengine::DisplayCreationArgsBuilder builder;
  25. if (const auto& physical = state.physical) {
  26. builder.setId(physical->id);
  27. } else {
  28. builder.setId(acquireVirtualDisplay(resolution, pixelFormat));
  29. }
  30. builder.setPixels(resolution);
  31. builder.setIsSecure(state.isSecure);
  32. builder.setPowerAdvisor(mPowerAdvisor.get());
  33. builder.setName(state.displayName);
  34. auto compositionDisplay = getCompositionEngine().createDisplay(builder.build());
  35. compositionDisplay->setLayerCachingEnabled(mLayerCachingEnabled);
  36. sp<compositionengine::DisplaySurface> displaySurface;
  37. sp<IGraphicBufferProducer> producer;
  38. sp<IGraphicBufferProducer> bqProducer;
  39. sp<IGraphicBufferConsumer> bqConsumer;
  40. getFactory().createBufferQueue(&bqProducer, &bqConsumer, /*consumerIsSurfaceFlinger =*/false);
  41. if (state.isVirtual()) {
  42. const auto displayId = VirtualDisplayId::tryCast(compositionDisplay->getId());
  43. LOG_FATAL_IF(!displayId);
  44. auto surface = sp<VirtualDisplaySurface>::make(getHwComposer(), *displayId, state.surface,
  45. bqProducer, bqConsumer, state.displayName);
  46. displaySurface = surface;
  47. producer = std::move(surface);
  48. } else {
  49. ALOGE_IF(state.surface != nullptr,
  50. "adding a supported display, but rendering "
  51. "surface is provided (%p), ignoring it",
  52. state.surface.get());
  53. const auto displayId = PhysicalDisplayId::tryCast(compositionDisplay->getId());
  54. LOG_FATAL_IF(!displayId);
  55. displaySurface =
  56. sp<FramebufferSurface>::make(getHwComposer(), *displayId, bqConsumer,
  57. state.physical->activeMode->getResolution(),
  58. ui::Size(maxGraphicsWidth, maxGraphicsHeight));
  59. producer = bqProducer;
  60. }
  61. LOG_FATAL_IF(!displaySurface);
  62. auto display = setupNewDisplayDeviceInternal(displayToken, std::move(compositionDisplay), state,
  63. displaySurface, producer);
  64. if (display->isPrimary()) {
  65. initScheduler(display);
  66. }
  67. if (!state.isVirtual()) {
  68. dispatchDisplayHotplugEvent(display->getPhysicalId(), true);
  69. }
  70. mDisplays.try_emplace(displayToken, std::move(display));
  71. }

SurfaceFlinger doCommitTransactions

调用SurfaceFlinger的doCommitTransactions方法:

  1. //frameworks/native/services/surfaceflinger/Surfaceflinger.cpp
  2. void SurfaceFlinger::doCommitTransactions() {
  3. ATRACE_CALL();
  4. if (!mLayersPendingRemoval.isEmpty()) {
  5. // Notify removed layers now that they can't be drawn from
  6. for (const auto& l : mLayersPendingRemoval) {
  7. // Ensure any buffers set to display on any children are released.
  8. if (l->isRemovedFromCurrentState()) {
  9. l->latchAndReleaseBuffer();
  10. }
  11. // If a layer has a parent, we allow it to out-live it's handle
  12. // with the idea that the parent holds a reference and will eventually
  13. // be cleaned up. However no one cleans up the top-level so we do so
  14. // here.
  15. if (l->isAtRoot()) {
  16. l->setIsAtRoot(false);
  17. mCurrentState.layersSortedByZ.remove(l);
  18. }
  19. // If the layer has been removed and has no parent, then it will not be reachable
  20. // when traversing layers on screen. Add the layer to the offscreenLayers set to
  21. // ensure we can copy its current to drawing state.
  22. if (!l->getParent()) {
  23. mOffscreenLayers.emplace(l.get());
  24. }
  25. }
  26. mLayersPendingRemoval.clear();
  27. }
  28. // If this transaction is part of a window animation then the next frame
  29. // we composite should be considered an animation as well.
  30. //最关键mCurrentState赋值给了mDrawingState
  31. mAnimCompositionPending = mAnimTransactionPending;
  32. mDrawingState = mCurrentState;
  33. // clear the "changed" flags in current state
  34. mCurrentState.colorMatrixChanged = false;
  35. if (mVisibleRegionsDirty) {
  36. for (const auto& rootLayer : mDrawingState.layersSortedByZ) {
  37. rootLayer->commitChildList();
  38. }
  39. }
  40. commitOffscreenLayers();
  41. if (mNumClones > 0) {
  42. mDrawingState.traverse([&](Layer* layer) { layer->updateMirrorInfo(); });
  43. }
  44. }

SurfaceFlinger latchBuffers

调用SurfaceFlinger的latchBuffers方法,将应用程序的图形缓冲区(buffer)与显示器进行关联:

  1. //frameworks/native/services/surfaceflinger/Surfaceflinger.cpp
  2. bool SurfaceFlinger::latchBuffers() {
  3. ATRACE_CALL();
  4. const nsecs_t latchTime = systemTime();
  5. bool visibleRegions = false;
  6. bool frameQueued = false;
  7. bool newDataLatched = false;
  8. const nsecs_t expectedPresentTime = mExpectedPresentTime.load();
  9. // Store the set of layers that need updates. This set must not change as
  10. // buffers are being latched, as this could result in a deadlock.
  11. // Example: Two producers share the same command stream and:
  12. // 1.) Layer 0 is latched
  13. // 2.) Layer 0 gets a new frame
  14. // 2.) Layer 1 gets a new frame
  15. // 3.) Layer 1 is latched.
  16. // Display is now waiting on Layer 1's frame, which is behind layer 0's
  17. // second frame. But layer 0's second frame could be waiting on display.
  18. mDrawingState.traverse([&](Layer* layer) {
  19. if (layer->clearTransactionFlags(eTransactionNeeded) || mForceTransactionDisplayChange) {
  20. const uint32_t flags = layer->doTransaction(0);
  21. if (flags & Layer::eVisibleRegion) {
  22. mVisibleRegionsDirty = true;
  23. }
  24. }
  25. //判断是否有buffer
  26. if (layer->hasReadyFrame()) {
  27. frameQueued = true;
  28. //是否应该显示
  29. if (layer->shouldPresentNow(expectedPresentTime)) {
  30. //如果要显示放入到mLayersWithQueuedFrames队列
  31. mLayersWithQueuedFrames.emplace(layer);
  32. } else {
  33. ATRACE_NAME("!layer->shouldPresentNow()");
  34. layer->useEmptyDamage();
  35. }
  36. } else {
  37. layer->useEmptyDamage();
  38. }
  39. });
  40. //上面主要是为了从sf的mDrawingState遍历寻找出有buffer的layer
  41. mForceTransactionDisplayChange = false;
  42. // The client can continue submitting buffers for offscreen layers, but they will not
  43. // be shown on screen. Therefore, we need to latch and release buffers of offscreen
  44. // layers to ensure dequeueBuffer doesn't block indefinitely.
  45. for (Layer* offscreenLayer : mOffscreenLayers) {
  46. offscreenLayer->traverse(LayerVector::StateSet::Drawing,
  47. [&](Layer* l) { l->latchAndReleaseBuffer(); });
  48. }
  49. //如果buffer队列不为空
  50. if (!mLayersWithQueuedFrames.empty()) {
  51. // mStateLock is needed for latchBuffer as LayerRejecter::reject()
  52. // writes to Layer current state. See also b/119481871
  53. Mutex::Autolock lock(mStateLock);
  54. for (const auto& layer : mLayersWithQueuedFrames) {
  55. //进行关键的latchBuffer操作,然后newDataLatched设置成了true
  56. if (layer->latchBuffer(visibleRegions, latchTime, expectedPresentTime)) {
  57. mLayersPendingRefresh.push_back(layer); /这里有latchBuffer说明有buffer刷新,放入mLayersPendingRefresh
  58. newDataLatched = true;
  59. }
  60. layer->useSurfaceDamage();
  61. }
  62. }
  63. mVisibleRegionsDirty |= visibleRegions;
  64. // If we will need to wake up at some time in the future to deal with a
  65. // queued frame that shouldn't be displayed during this vsync period, wake
  66. // up during the next vsync period to check again.
  67. if (frameQueued && (mLayersWithQueuedFrames.empty() || !newDataLatched)) {
  68. scheduleCommit(FrameHint::kNone);
  69. }
  70. // enter boot animation on first buffer latch
  71. if (CC_UNLIKELY(mBootStage == BootStage::BOOTLOADER && newDataLatched)) {
  72. ALOGI("Enter boot animation");
  73. mBootStage = BootStage::BOOTANIMATION;
  74. }
  75. if (mNumClones > 0) {
  76. mDrawingState.traverse([&](Layer* layer) { layer->updateCloneBufferInfo(); });
  77. }
  78. // Only continue with the refresh if there is actually new work to do
  79. return !mLayersWithQueuedFrames.empty() && newDataLatched;
  80. }

BufferLayer latchBuffer

调用layer的latchBuffer方法:

  1. //frameworks/native/services/surfaceflinger/BufferLayer.cpp
  2. bool BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime,
  3. nsecs_t expectedPresentTime) {
  4. ATRACE_CALL();
  5. bool refreshRequired = latchSidebandStream(recomputeVisibleRegions);
  6. if (refreshRequired) {
  7. return refreshRequired;
  8. }
  9. // If the head buffer's acquire fence hasn't signaled yet, return and
  10. // try again later
  11. // 如果头部缓冲区的采集围栏尚未发出信号,请返回并稍后重试
  12. if (!fenceHasSignaled()) {
  13. ATRACE_NAME("!fenceHasSignaled()");
  14. mFlinger->onLayerUpdate(); // (692) SurfaceFlinger onLayerUpdate流程分析 | 知识管理 - PingCode 
  15. return false;
  16. }
  17. // Capture the old state of the layer for comparisons later
  18. const State& s(getDrawingState());
  19. const bool oldOpacity = isOpaque(s);
  20. BufferInfo oldBufferInfo = mBufferInfo;
  21. //调用到BufferStateLayer中的updateTexImage,这里业务主要返回对应的recomputeVisibleRegions
  22. status_t err = updateTexImage(recomputeVisibleRegions, latchTime, expectedPresentTime);
  23. if (err != NO_ERROR) {
  24. return false;
  25. }
  26. //这里主要吧state的相关buffer数据赋值给mBufferInfo
  27. err = updateActiveBuffer();
  28. if (err != NO_ERROR) {
  29. return false;
  30. }
  31. //赋值一下framenumber
  32. err = updateFrameNumber();
  33. if (err != NO_ERROR) {
  34. return false;
  35. }
  36. //这里又一次吧mDrawingState中BufferInfo需要的大部分数据进行赋值
  37. gatherBufferInfo();
  38. if (oldBufferInfo.mBuffer == nullptr) {
  39. // the first time we receive a buffer, we need to trigger a
  40. // geometry invalidation.
  41. recomputeVisibleRegions = true;
  42. }
  43. if ((mBufferInfo.mCrop != oldBufferInfo.mCrop) ||
  44. (mBufferInfo.mTransform != oldBufferInfo.mTransform) ||
  45. (mBufferInfo.mScaleMode != oldBufferInfo.mScaleMode) ||
  46. (mBufferInfo.mTransformToDisplayInverse != oldBufferInfo.mTransformToDisplayInverse)) {
  47. recomputeVisibleRegions = true;
  48. }
  49. if (oldBufferInfo.mBuffer != nullptr) {
  50. uint32_t bufWidth = mBufferInfo.mBuffer->getWidth();
  51. uint32_t bufHeight = mBufferInfo.mBuffer->getHeight();
  52. if (bufWidth != oldBufferInfo.mBuffer->getWidth() ||
  53. bufHeight != oldBufferInfo.mBuffer->getHeight()) {
  54. recomputeVisibleRegions = true;
  55. }
  56. }
  57. if (oldOpacity != isOpaque(s)) {
  58. recomputeVisibleRegions = true;
  59. }
  60. return true;
  61. }

BufferQueueLayer updateTexImage

调用BufferLayer的updateTexImage方法,BufferQueueLayer继承与于BufferLayer,调用BufferQueueLayer的updateTexImage方法:

Android13 BufferQueueLayer updateTexImage流程分析-CSDN博客

SurfaceFlinger updateLayerGeometry

调用SurfaceFlinger的updateLayerGeometry方法,这次vsync显示刷新的layer进行脏区设置:

  1. //frameworks/native/services/surfaceflinger/Surfaceflinger.cpp
  2. void SurfaceFlinger::updateLayerGeometry() {
  3. ATRACE_CALL();
  4. if (mVisibleRegionsDirty) {
  5. computeLayerBounds(); //触发各个layer计算对于的bound
  6. }
  7. //前面有buffer的集合mLayersPendingRefresh进行对应display的dirtyRegion更新
  8. for (auto& layer : mLayersPendingRefresh) {
  9. Region visibleReg;
  10. visibleReg.set(layer->getScreenBounds());
  11. invalidateLayerStack(layer, visibleReg); //刷新一下display的dirtyRegion
  12. }
  13. mLayersPendingRefresh.clear();
  14. }

SurfaceFlinger invalidateLayerStack

调用SurfaceFlinger的invalidateLayerStack方法:

  1. //frameworks/native/services/surfaceflinger/Surfaceflinger.cpp
  2. void SurfaceFlinger::invalidateLayerStack(const sp<const Layer>& layer, const Region& dirty) {
  3. for (const auto& [token, displayDevice] : FTL_FAKE_GUARD(mStateLock, mDisplays)) {
  4. auto display = displayDevice->getCompositionDisplay();
  5. if (display->includesLayer(layer->getOutputFilter())) { //寻找到对应的display
  6. display->editState().dirtyRegion.orSelf(dirty); //设置到了display->editState中
  7. }
  8. }
  9. }

上面主要就是干了一件事,根据这次有buffer的Layer进行遍历,刷新对应display的dirtyRegion。

SurfaceFlinger updateInputFlinger

调用SurfaceFlinger的updateInputFlinger方法,更新输入处理:

  1. //frameworks/native/services/surfaceflinger/Surfaceflinger.cpp
  2. void SurfaceFlinger::updateInputFlinger() {
  3. ATRACE_CALL();
  4. if (!mInputFlinger) {
  5. return;
  6. }
  7. std::vector<WindowInfo> windowInfos;
  8. std::vector<DisplayInfo> displayInfos;
  9. bool updateWindowInfo = false;
  10. if (mVisibleRegionsDirty || mInputInfoChanged) {
  11. mInputInfoChanged = false;
  12. updateWindowInfo = true;
  13. //进行遍历整个系统的layer和display转变成windowInfos,displayInfos信息
  14. buildWindowInfos(windowInfos, displayInfos);
  15. }
  16. if (!updateWindowInfo && mInputWindowCommands.empty()) {
  17. return;
  18. }
  19. //这里放入子线程进行相对应跨进程通讯
  20. BackgroundExecutor::getInstance().sendCallbacks({[updateWindowInfo,
  21. windowInfos = std::move(windowInfos),
  22. displayInfos = std::move(displayInfos),
  23. inputWindowCommands =
  24. std::move(mInputWindowCommands),
  25. inputFlinger = mInputFlinger, this]() {
  26. ATRACE_NAME("BackgroundExecutor::updateInputFlinger");
  27. if (updateWindowInfo) {
  28. //这里调用是mWindowInfosListenerInvoker进行的windowInfos, displayInfos进行跨进程传递
  29. mWindowInfosListenerInvoker->windowInfosChanged(windowInfos, displayInfos,
  30. inputWindowCommands.syncInputWindows);
  31. } else if (inputWindowCommands.syncInputWindows) {
  32. // If the caller requested to sync input windows, but there are no
  33. // changes to input windows, notify immediately.
  34. windowInfosReported();
  35. }
  36. for (const auto& focusRequest : inputWindowCommands.focusRequests) {
  37. //直接调用inputFlinger的bpbinder跨进程设置setFocusedWindow
  38. inputFlinger->setFocusedWindow(focusRequest);
  39. }
  40. }});
  41. mInputWindowCommands.clear();
  42. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/825174
推荐阅读
相关标签
  

闽ICP备14008679号