首先看下其定义: 此类封装了不同刷新率的垂直同步配置。根据所使用的刷新率以及是否在 OpenGL 中进行合成,不同的偏移量将有助于降低延迟。此类会跟踪设备所处的模式,并在需要时返回相应的偏移量。
class VsyncConfiguration {
public:
virtual ~VsyncConfiguration() = default;
virtual VsyncConfigSet getCurrentConfigs() const = 0;
virtual VsyncConfigSet getConfigsForRefreshRate(Fps fps) const = 0;
virtual void reset() = 0;
virtual void setRefreshRateFps(Fps fps) = 0;
virtual void dump(std::string& result) const = 0;
};
virtual VsyncConfigSet getCurrentConfigs() const = 0;
获取当前的 VSYNC 配置集。
调用此方法会返回一个 VsyncConfigSet 结构体。这个结构体包含了在当前屏幕刷新率下,系统处于不同状态(Late、Early、Early GL)时的 App 偏移量和 SF 偏移量。SurfaceFlinger 的调度器(Scheduler)需要用这个方法的返回值来设定即将到来的 VSYNC 唤醒时间。
VsyncConfigSet getConfigsForRefreshRate(Fps fps)
获取指定刷新率下的 VSYNC 配置集。
与上一个方法不同,系统可能需要查询非当前刷新率的配置。例如,当系统准备从 60Hz 切换到 120Hz 时,调度系统需要提前知道 120Hz 下对应的 Phase Offsets 是多少,以便平滑过渡。传入参数 fps,返回对应刷新率的配置集。
void reset()
重置内部状态。将 VSYNC 偏移量配置重置为初始状态。通常在一些关键生命周期事件中使用,比如:
- 设备刚刚启动或屏幕热插拔(Display Hotplug)时。
- 系统属性(System Properties)中的 VSYNC 偏移量调试参数被修改,需要重新加载时。
void setRefreshRateFps(Fps fps)
更新当前活动的屏幕刷新率。
当 SurfaceFlinger 真正完成了屏幕刷新率的切换(例如 90Hz 降到了 60Hz),它会调用这个方法通知 VsyncConfiguration。这样,VsyncConfiguration 内部的状态就会更新,随后再调用 getCurrentConfigs() 时,返回的就是新刷新率(60Hz)对应的偏移量参数了。
void dump(std::string& result)
导出调试信息。
这是 Android 系统服务的标准调试机制。当你通过 ADB 命令行运行 adb shell dumpsys SurfaceFlinger 时,SurfaceFlinger 会遍历其所有核心组件的 dump 方法。这个方法会将 VsyncConfiguration 当前记录的刷新率、各状态的 Offset 纳秒值等内部信息,追加到 result 字符串中,方便开发者排查掉帧或延迟问题。
另外可以看到这个类里都是虚函数,其与子类的关系如下:

impl/VsyncConfiguration
这是相位偏移和持续时间的通用实现方式。PhaseOffsets 和 WorkDuration 都继承自此类,并实现了 constructOffsets 方法。
class VsyncConfiguration : public scheduler::VsyncConfiguration {
public:
explicit VsyncConfiguration(Fps currentFps);
// Returns early, early GL, and late offsets for Apps and SF for a given refresh rate.
VsyncConfigSet getConfigsForRefreshRate(Fps fps) const override EXCLUDES(mLock) {
std::lock_guard lock(mLock);
return getConfigsForRefreshRateLocked(fps);
}
// Returns early, early GL, and late offsets for Apps and SF.
VsyncConfigSet getCurrentConfigs() const override EXCLUDES(mLock) {
std::lock_guard lock(mLock);
return getConfigsForRefreshRateLocked(mRefreshRateFps);
}
// Cleans the internal cache.
void reset() override EXCLUDES(mLock) {
std::lock_guard lock(mLock);
mOffsetsCache.clear();
}
// This function should be called when the device is switching between different
// refresh rates, to properly update the offsets.
void setRefreshRateFps(Fps fps) override EXCLUDES(mLock) {
std::lock_guard lock(mLock);
mRefreshRateFps = fps;
}
// Returns current offsets in human friendly format.
void dump(std::string& result) const override;
protected:
virtual VsyncConfigSet constructOffsets(nsecs_t vsyncDuration) const = 0;
VsyncConfigSet getConfigsForRefreshRateLocked(Fps fps) const REQUIRES(mLock);
mutable ftl::SmallMap<Fps, VsyncConfigSet
353



被折叠的 条评论
为什么被折叠?



