From ac2fff231a2b035f61bb987eceab00dc8e03f083 Mon Sep 17 00:00:00 2001 From: alexandersato0830 <66799221+alexandersato0830@users.noreply.github.com> Date: Wed, 25 Nov 2026 12:42:29 -0600 Subject: [PATCH] Preserve first-present timestamps for repeated frame tokens --- runtime/src/frame_pacer.h | 31 +++++++++++++++++------------- runtime/test/frame_pacer_test.cpp | 32 +++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 13 deletions(-) diff --git a/runtime/src/frame_pacer.h b/runtime/src/frame_pacer.h index 2c9f1fd7e4..e8aa650138 100644 --- a/runtime/src/frame_pacer.h +++ b/runtime/src/frame_pacer.h @@ -267,7 +267,8 @@ class FramePacer { // GetFrameStatisticsX(token, type_mask, &count, array). `count` is the room in `out` on entry and the number of // entries written on return. S_OK when every producible requested type was written (event, present, display), // S_FALSE with count 0 or fewer when they are not (there yet, or the token is unknown or has aged out of the - // last 64). With a null `out`, count only receives how many entries are available. + // last 64). With a null `out`, count only receives how many entries are available. A repeated present leaves + // the token's first present and display timestamps intact. std::int32_t get_frame_statistics(std::uint64_t token, std::uint32_t type_mask, std::uint32_t* count, FrameStatistic* out) { if (!count) return pacer_hr::invalid_arg; @@ -352,11 +353,14 @@ class FramePacer { std::lock_guard guard(mutex_); const std::uint64_t now = clock_.now_us(); retire_locked(now); - record_present_timing_locked(now, token); + Record* record = record_present_timing_locked(now, token); + if (record && record->presented) record = nullptr; last_presented_ = std::max(last_presented_, token); if (!interval_set_ || !any_armed_locked() || uncapped_) { // not paced or uncapped: shows at once - for (Record& r : records_) - if (r.token == token) { r.presented = r.displayed = true; r.present_us = r.display_us = now; } + if (record) { + record->presented = record->displayed = true; + record->present_us = record->display_us = now; + } return; } // The vblank after now, and not before the one after the previous queued frame. @@ -366,15 +370,15 @@ class FramePacer { float percent = 0.0f; if (sync == PresentSync::immediate && pending_.empty()) { flip = now; - std::uint64_t late_us = 0; - for (const Record& r : records_) - if (r.token == token && now > r.due_us) late_us = now - r.due_us; + const std::uint64_t late_us = record && now > record->due_us ? now - record->due_us : 0; percent = scan_percent(late_us, period_us_); ++immediate_presents_; } - pending_.push_back(Pending{token, flip, percent}); - for (Record& r : records_) - if (r.token == token) { r.presented = true; r.present_us = now; } + pending_.push_back(Pending{record ? token : 0, flip, percent}); + if (record) { + record->presented = true; + record->present_us = now; + } retire_locked(now); cv_.notify_all(); } @@ -484,7 +488,7 @@ class FramePacer { bool presented = false, displayed = false; }; - void record_present_timing_locked(std::uint64_t now, std::uint64_t token) { + Record* record_present_timing_locked(std::uint64_t now, std::uint64_t token) { ++presents_recorded_; if (has_last_present_ && now >= last_present_us_) { interval_ring_[interval_head_] = now - last_present_us_; @@ -494,17 +498,18 @@ class FramePacer { has_last_present_ = true; last_present_us_ = now; if (token != 0) { - for (const Record& r : records_) { + for (Record& r : records_) { if (r.token == token) { if (now >= r.origin_us) { latency_ring_[latency_head_] = now - r.origin_us; latency_head_ = (latency_head_ + 1) % frame_pacing_window; if (latency_count_ < frame_pacing_window) ++latency_count_; } - break; + return &r; } } } + return nullptr; } // Frames whose vblank has come are shown. diff --git a/runtime/test/frame_pacer_test.cpp b/runtime/test/frame_pacer_test.cpp index 7f92d51a7d..646d8dca7e 100644 --- a/runtime/test/frame_pacer_test.cpp +++ b/runtime/test/frame_pacer_test.cpp @@ -368,6 +368,38 @@ int main() { count == 1); } + // Repeat presents are still paced, but the token's timestamps belong to its first frame. + { + ManualFrameClock clock; + FramePacer pacer(clock); + EXPECT(setup(pacer, 2)); + clock.advance_us(period); + std::uint64_t token = 0; + EXPECT(pacer.wait_frame_event(frame_event_origin, 0, 0, &token) == pacer_hr::ok); + pacer.commit_present_token(token, 1); + + const auto first_presentation_is_intact = [&] { + std::uint32_t count = 2; + FrameStatistic stats[2]; + return pacer.get_frame_statistics(token, stat_present | stat_display, &count, stats) == pacer_hr::ok && + count == 2 && stats[0].time_us == period && stats[1].time_us == period * 2; + }; + + clock.advance_us(period + 1); + pacer.commit_present_token(token, 1); + EXPECT(pacer.frames_in_flight() == 1); + EXPECT(first_presentation_is_intact()); + clock.advance_us(period); + EXPECT(pacer.frames_in_flight() == 0); + EXPECT(first_presentation_is_intact()); + + pacer.set_uncapped(true); + clock.advance_us(period); + pacer.commit_present_token(token, 1); + EXPECT(first_presentation_is_intact()); + EXPECT(pacer.pacing_summary().frames == 3); + } + // Suspend: a blocked waiter fails, presents are refused, registration is gone after resume and must be redone; // tokens keep counting. {