当前位置:   article > 正文

Android13 SurfaceFlinger onLayerUpdate流程分析_surfaceflinger composite

surfaceflinger composite

SurfaceFlinger的onLayerUpdate方法用于在图形层更新时进行相应的处理,代码如下:

  1. void SurfaceFlinger::onLayerUpdate() {
  2. scheduleCommit(FrameHint::kActive);
  3. }

调用SurfaceFlinger的scheduleCommit方法:

  1. //frameworks/native/service/surfaceflinger/SurfaeFlinger.cpp
  2. std::unique_ptr<scheduler::Scheduler> mScheduler;
  3. void SurfaceFlinger::scheduleCommit(FrameHint hint) {
  4. if (hint == FrameHint::kActive) {
  5. mScheduler->resetIdleTimer();
  6. }
  7. mPowerAdvisor->notifyDisplayUpdateImminent();
  8. mScheduler->scheduleFrame();
  9. }

调用mScheduler(Scheduler)的scheduleFrame方法,Scheduler继承于MessageQueue,调用MessageQueue的scheduleFrame方法:

  1. //frameworks/native/services/surfaceflinger/Scheduler/MessageQueue.cpp
  2. Vsync mVsync;
  3. std::unique_ptr<scheduler::VSyncCallbackRegistration> registration;
  4. void MessageQueue::scheduleFrame() {
  5. ATRACE_CALL();
  6. {
  7. std::lock_guard lock(mInjector.mutex);
  8. if (CC_UNLIKELY(mInjector.connection)) {
  9. ALOGD("%s while injecting VSYNC", __FUNCTION__);
  10. mInjector.connection->requestNextVsync();
  11. return;
  12. }
  13. }
  14. std::lock_guard lock(mVsync.mutex);
  15. mVsync.scheduledFrameTime =
  16. mVsync.registration->schedule({.workDuration = mVsync.workDuration.get().count(),
  17. .readyDuration = 0,
  18. .earliestVsync = mVsync.lastCallbackTime.count()});
  19. }

调用mVsync(Vsync)成员变量registration的schedule方法,而mVsync.registration在MessageQueue的initVsync中注册:

  1. //frameworks/native/services/surfaceflinger/Scheduler/MessageQueue.cpp
  2. void MessageQueue::initVsync(scheduler::VSyncDispatch& dispatch,
  3. frametimeline::TokenManager& tokenManager,
  4. std::chrono::nanoseconds workDuration) {
  5. setDuration(workDuration);
  6. mVsync.tokenManager = &tokenManager;
  7. mVsync.registration = std::make_unique<
  8. scheduler::VSyncCallbackRegistration>(dispatch,
  9. std::bind(&MessageQueue::vsyncCallback, this,
  10. std::placeholders::_1,
  11. std::placeholders::_2,
  12. std::placeholders::_3),
  13. "sf");
  14. }

因此会调用到MessageQueue的vsyncCallback方法:

  1. //frameworks/native/services/surfaceflinger/Scheduler/MessageQueue.cpp
  2. void MessageQueue::vsyncCallback(nsecs_t vsyncTime, nsecs_t targetWakeupTime, nsecs_t readyTime) {
  3. ATRACE_CALL();
  4. // Trace VSYNC-sf
  5. mVsync.value = (mVsync.value + 1) % 2;
  6. {
  7. std::lock_guard lock(mVsync.mutex);
  8. mVsync.lastCallbackTime = std::chrono::nanoseconds(vsyncTime);
  9. mVsync.scheduledFrameTime.reset();
  10. }
  11. const auto vsyncId = mVsync.tokenManager->generateTokenForPredictions(
  12. {targetWakeupTime, readyTime, vsyncTime});
  13. mHandler->dispatchFrame(vsyncId, vsyncTime);
  14. }

调用Handler的dispatchFrame方法:

  1. //frameworks/native/services/surfaceflinger/Scheduler/MessageQueue.cpp
  2. void MessageQueue::Handler::dispatchFrame(int64_t vsyncId, nsecs_t expectedVsyncTime) {
  3. if (!mFramePending.exchange(true)) {
  4. mVsyncId = vsyncId;
  5. mExpectedVsyncTime = expectedVsyncTime;
  6. mQueue.mLooper->sendMessage(this, Message());
  7. }
  8. }

发送Message,消息在Handler的handleMessage方法中处理:

  1. //frameworks/native/services/surfaceflinger/Scheduler/MessageQueue.cpp
  2. ICompositor& mCompositor;
  3. void MessageQueue::Handler::handleMessage(const Message&) {
  4. mFramePending.store(false);
  5. const nsecs_t frameTime = systemTime();
  6. auto& compositor = mQueue.mCompositor;
  7. if (!compositor.commit(frameTime, mVsyncId, mExpectedVsyncTime)) {
  8. return;
  9. }
  10. compositor.composite(frameTime, mVsyncId);
  11. compositor.sample();
  12. }

上面方法主要处理如下:

1、调用mCompositor(ICompositor)的commit方法。

2、调用mCompositor(ICompositor)的composite方法。

下面分别进行分析:

SurfaceFlinger commit

调用mCompositor(ICompositor)的commit方法,ICompositor是一个接口,由SurfaceFlinger实现,SurfaceFlinger的commit方法用于将应用程序的绘制结果提交到屏幕上显示:

Android13 SurfaceFlinger commit(提交)流程分析-CSDN博客

SurfaceFlinger composite

调用mCompositor(ICompositor)的commit方法,ICompositor是一个接口,由SurfaceFlinger实现,SurfaceFlinger的composite用于将多个窗口的图像进行合成:

Android13 SurfaceFlinger composite(合成)流程分析-CSDN博客

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号