VsyncDispatchTimerQueueEntry
上一篇文章讲到,displaysyncsource会将自己的 onVsyncCallback 接口注册到 VSyncDispatchTimerQueueEntry 中
VSyncDispatchTimerQueueEntry::VSyncDispatchTimerQueueEntry(std::string const& name,
VSyncDispatch::Callback const& cb,
nsecs_t minVsyncDistance)
: mName(name),
mCallback(cb),
mMinVsyncDistance(minVsyncDistance) {}
这个 Entry 类内部会维护 一个 mArmedInfo 结构体,这里面分别记录了nextWakeupTime, nextVsyncTime, nextReadyTime。 executing() 接口会将该结构体清空
nextWakeupTime 线程需要被唤醒的时间
nextVsyncTime 真正的Vsync time
nextReadyTime
然后每个 entry 类 会将自己 作为 callback注册到 VsyncDispatchTimerQueue 中
VsyncDispatchTimerQueue
这个类负责 在 wakeuptime 来临时,唤醒 DispSyncSource,并且更新Entry 类的mArmedInfo
他会将自己的timerCallback 接口作为callback注册给 Time类
Timer
timer 顾名思义,是一个计时器。
他是一个线程,在启动是,会创建一个 timerfd用于计时,一个epollfd,用于通知消息
Timer::Timer() {
reset();
mDispatchThread = std::thread([this]() { threadMain(); });
}
Timer::~Timer() {
endDispatch();
mDispatchThread.join();
cleanup();
}
void Timer::reset() {
cleanup();
mTimerFd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK);
mEpollFd = epoll_create1(EPOLL_CLOEXEC);
if (pipe2(mPipes.data(), O_CLOEXEC | O_NONBLOCK)) {
ALOGE("could not create TimerDispatch mPipes");
return;
};
setDebugState(DebugState::Reset);
}
每次 VsyncDispatchTimerQueue 从 entry类 获得的 wakeuptime,会给timer 作为 target time,然后设置等待。在timer的线程函数中,会用epoll 等待时间到来的消息,当时间到来时,会按照该流程callback
Timer ->VsyncDispatchTimerQueue -> VsyncDispatchTimerQueueEntry->DispSyncSource->EventThread -> MessageQueue -> SurfaceFlinger