如何用 wp_presentation 协议在 Wayland 上测量显示延迟

Wayland 上应用从不直接接触显示器——合成器管。所以“我的帧到底何时出现”从客户端无从知晓,除非合成器告诉你。wp_presentation 协议(2017 年起稳定,KWin、Mutter、Weston、wlroots 支持——不普遍)正是这条反馈通道。

flow.txt
app:  wl_surface.commit(frame)  ──►  compositor: composites, scans out
app:  feedback.request()        ◄──  presented(tv_sec, tv_nsec, refresh)
                                   discarded (frame dropped)

获取接口

setup.cpp
// registry handler:
static void registry_add(void* data, wl_registry* reg, uint32_t name,
                         const char* iface, uint32_t ver) {
    if (!strcmp(iface, wp_presentation_interface.name))
        *static_cast<wp_presentation**>(data) =
            (wp_presentation*)wl_registry_bind(reg, name,
                &wp_presentation_interface, 1);
}
// generate wp_presentation client header + code:
//   wayland-scanner client-header \
//     stable/presentation-time/presentation-time.xml .
//   wayland-scanner private-code  ... presentation-time.xml .

// Which clock do the timestamps use? (usually CLOCK_MONOTONIC):
static void clock_cb(void*, wp_presentation*, uint32_t clock_id) {
    g_clock = clock_id;   // e.g. CLOCK_MONOTONIC
}
static const wp_presentation_listener pres_l = { clock_cb };
wp_presentation_add_listener(pres, &pres_l, nullptr);

逐帧请求反馈

frame.cpp
// Attach a feedback request to THIS commit:
wp_presentation_feedback* fb =
    wp_presentation_feedback(pres, surface);
wp_presentation_feedback_add_listener(fb, &fb_l, this);
wl_surface_commit(surface);     // the commit the feedback tracks

static void presented_cb(void*, wp_presentation_feedback*,
    uint32_t tv_hi, uint32_t tv_lo, uint32_t tv_nsec,
    uint32_t refresh,   // ns per refresh cycle
    uint32_t seq_hi, uint32_t seq_lo, uint32_t flags) {
    uint64_t t = (uint64_t(tv_hi) << 32) | tv_lo;
    double presented_ns = t * 1e9 + tv_nsec;
    logLatency(presented_ns - commitTime_ns);   // real display latency
}
static void discarded_cb(void*, wp_presentation_feedback* fb) {
    wp_presentation_feedback_destroy(fb);       // frame never shown
}
static const wp_presentation_feedback_listener fb_l = {
    .sync_output = nullptr, .presented = presented_cb,
    .discarded = discarded_cb };

数值含义

字段含义
tv_sec/tv_nsec包含你那一帧的刷新周期开始时刻——clock_id 时钟
refresh刷新间隔(ns)→ 精确算下一次 vsync
flagsVSYNC(垂直同步更新)、HW_CLOCK(显示硬件时间戳而非估算)、HW_COMPLETION(拷贝也完成)、ZERO_COPY(你的 buffer 被直接扫描输出)
seq刷新计数器——数丢掉的周期

ZERO_COPY 对延迟工作最有意思:它说明合成器把你的 buffer 直接交给扫描输出而没有合成——通常要全屏 + 格式匹配。看到它熄灭,说明有什么让这帧走了合成(缩放、重叠、damage)。

与 Vulkan 搭配

wp_presentation 覆盖合成器→显示器这一段;VK_EXT_present_timing 覆盖提交→呈现引擎。合起来闭环:

e2e.txt
vkQueuePresentKHR ─► present_timing (engine stages)
                 └► wp_presentation.presented (compositor→display)
submit→photon = presented.tv_nsec - submitTime

实用规则

  • 每次 commit 一个 feedback 对象——被 presented 或 discarded 消费后 destroy。
  • 查 clock_id——几乎总是 CLOCK_MONOTONIC,于是 clock_gettime(CLOCK_MONOTONIC) 时间戳可直接比较。
  • 没有 HW_CLOCK = 合成器在估算(仍有用,按 ±1ms 存疑对待)。
  • 注册表里没有 wp_presentation 就是合成器不支持——退回只用 VK_EXT_present_timing 的阶段。
  • surface 被遮挡或帧被取代时收到 discarded 反馈是正常的——不算错误,但要计进掉帧统计。

按类别查看类似文章: Wayland Linux