From a14f74c2b2b2716148e853219ba787fd4ae035b1 Mon Sep 17 00:00:00 2001 From: DeadlySimp <58283438+lgr123@users.noreply.github.com> Date: Wed, 25 Nov 2026 11:35:53 -0600 Subject: [PATCH 1/2] Rebase pending flips when the frame interval changes --- runtime/src/frame_pacer.h | 11 ++++++-- runtime/test/frame_pacer_test.cpp | 43 +++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/runtime/src/frame_pacer.h b/runtime/src/frame_pacer.h index 2c9f1fd7e4..c0b7eecccb 100644 --- a/runtime/src/frame_pacer.h +++ b/runtime/src/frame_pacer.h @@ -212,15 +212,22 @@ class FramePacer { // SetFrameIntervalX(output, interval, latency, flags). `period_us` is the interval as a period; `latency` is // the number of frames allowed in flight (0 counts as 1). The vblank grid is re-based: its epoch is now. A // scheduled origin event stays armed (with its offset, kept below the new period) and its next origin is one - // new period from now; waiters blocked on the old grid pick the new one up. + // new period from now. Pending frames receive consecutive vblanks on the new grid in their original order. std::int32_t set_frame_interval(std::uint32_t period_us, std::uint32_t latency, std::uint32_t flags) { (void)flags; if (period_us == 0) return pacer_hr::invalid_arg; std::lock_guard guard(mutex_); if (suspended_) return pacer_hr::invalid_state; + const auto now = clock_.now_us(); + retire_locked(now); period_us_ = period_us; latency_ = std::max(latency, 1u); - epoch_us_ = clock_.now_us(); + epoch_us_ = now; + auto next_flip = epoch_us_; + for (Pending& frame : pending_) { + next_flip += period_us_; + frame.flip_us = next_flip; + } interval_set_ = true; for (std::uint32_t k = 0; k < frame_event_count; ++k) { event_offset_us_[k] = std::min(event_offset_us_[k], period_us - 1); diff --git a/runtime/test/frame_pacer_test.cpp b/runtime/test/frame_pacer_test.cpp index 7f92d51a7d..8d3f686773 100644 --- a/runtime/test/frame_pacer_test.cpp +++ b/runtime/test/frame_pacer_test.cpp @@ -311,6 +311,49 @@ int main() { EXPECT(pacer.registered()); } + // Preserve already displayed frames and move each remaining frame to a vblank on the rebased grid. + { + ManualFrameClock clock; + FramePacer pacer(clock); + EXPECT(setup(pacer, 3)); + clock.advance_us(period); + std::uint64_t first = 0, second = 0, third = 0, fourth = 0; + EXPECT(pacer.wait_frame_event(frame_event_origin, 0, 0, &first) == pacer_hr::ok); + pacer.commit_present_token(first, 1); + EXPECT(pacer.wait_frame_event(frame_event_origin, 0, frame_wait_flag_token_on_timeout, &second) == + pacer_hr::false_); + pacer.commit_present_token(second, 1); + EXPECT(pacer.wait_frame_event(frame_event_origin, 0, frame_wait_flag_token_on_timeout, &third) == + pacer_hr::false_); + pacer.commit_present_token(third, 1); + EXPECT(pacer.frames_in_flight() == 3); + + constexpr auto new_epoch = 2 * period + period / 4; + constexpr auto new_period = period / 2; + clock.advance_us(new_epoch - period); + EXPECT(pacer.set_frame_interval(new_period, 3, 0) == pacer_hr::ok); + EXPECT(pacer.frames_in_flight() == 2); + + const auto displayed_at = [&](std::uint64_t token) { + FrameStatistic stats; + std::uint32_t count = 1; + return pacer.get_frame_statistics(token, stat_display, &count, &stats) == pacer_hr::ok ? + stats.time_us : no_deadline; + }; + EXPECT(displayed_at(first) == 2 * period); + EXPECT(displayed_at(second) == no_deadline); + clock.advance_us(new_period); + EXPECT(displayed_at(second) == new_epoch + new_period); + EXPECT(displayed_at(third) == no_deadline); + EXPECT(pacer.wait_frame_event(frame_event_origin, 0, 0, &fourth) == pacer_hr::ok); + pacer.commit_present_token(fourth, 1); + clock.advance_us(new_period); + EXPECT(displayed_at(third) == new_epoch + 2 * new_period); + EXPECT(displayed_at(fourth) == no_deadline); + clock.advance_us(new_period); + EXPECT(displayed_at(fourth) == new_epoch + 3 * new_period); + } + // Scheduling inside a period, before the offset has passed: that period's origin is still to come. { ManualFrameClock clock; From 61c34218759c7e27e52e9d56395d42d3c6736217 Mon Sep 17 00:00:00 2001 From: DeadlySimp <58283438+lgr123@users.noreply.github.com> Date: Wed, 25 Nov 2026 12:46:44 -0600 Subject: [PATCH 2/2] Keep queued flips when reapplying the frame period --- runtime/src/frame_pacer.h | 17 +++++++++------- runtime/test/frame_pacer_test.cpp | 33 +++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/runtime/src/frame_pacer.h b/runtime/src/frame_pacer.h index c0b7eecccb..775d318956 100644 --- a/runtime/src/frame_pacer.h +++ b/runtime/src/frame_pacer.h @@ -212,22 +212,25 @@ class FramePacer { // SetFrameIntervalX(output, interval, latency, flags). `period_us` is the interval as a period; `latency` is // the number of frames allowed in flight (0 counts as 1). The vblank grid is re-based: its epoch is now. A // scheduled origin event stays armed (with its offset, kept below the new period) and its next origin is one - // new period from now. Pending frames receive consecutive vblanks on the new grid in their original order. + // new period from now. When the period changes, pending frames receive consecutive vblanks on that grid in + // their original order. Reapplying the same period preserves their display deadlines. std::int32_t set_frame_interval(std::uint32_t period_us, std::uint32_t latency, std::uint32_t flags) { (void)flags; if (period_us == 0) return pacer_hr::invalid_arg; std::lock_guard guard(mutex_); if (suspended_) return pacer_hr::invalid_state; const auto now = clock_.now_us(); - retire_locked(now); + if (period_us_ != period_us) { + retire_locked(now); + auto next_flip = now; + for (Pending& frame : pending_) { + next_flip += period_us; + frame.flip_us = next_flip; + } + } period_us_ = period_us; latency_ = std::max(latency, 1u); epoch_us_ = now; - auto next_flip = epoch_us_; - for (Pending& frame : pending_) { - next_flip += period_us_; - frame.flip_us = next_flip; - } interval_set_ = true; for (std::uint32_t k = 0; k < frame_event_count; ++k) { event_offset_us_[k] = std::min(event_offset_us_[k], period_us - 1); diff --git a/runtime/test/frame_pacer_test.cpp b/runtime/test/frame_pacer_test.cpp index 8d3f686773..7640f85db2 100644 --- a/runtime/test/frame_pacer_test.cpp +++ b/runtime/test/frame_pacer_test.cpp @@ -354,6 +354,39 @@ int main() { EXPECT(displayed_at(fourth) == new_epoch + 3 * new_period); } + // Reapplying the same interval leaves a queued frame on its original display deadline. + { + constexpr std::uint32_t unchanged_period = 16000; + ManualFrameClock clock; + FramePacer pacer(clock); + EXPECT(pacer.set_frame_interval(unchanged_period, 3, 0) == pacer_hr::ok); + EXPECT(pacer.schedule_frame_event(frame_event_origin, 0, 0) == pacer_hr::ok); + clock.advance_us(unchanged_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); + clock.advance_us(unchanged_period / 2); + EXPECT(pacer.set_frame_interval(unchanged_period, 3, 0) == pacer_hr::ok); + + FrameStatistic stat; + std::uint32_t count = 1; + clock.advance_us(unchanged_period / 2 - 1); + EXPECT(pacer.get_frame_statistics(token, stat_display, &count, &stat) == pacer_hr::false_ && count == 0); + count = 1; + clock.advance_us(1); + EXPECT(pacer.get_frame_statistics(token, stat_display, &count, &stat) == pacer_hr::ok); + EXPECT(stat.time_us == 2 * unchanged_period); + + // Origin events still use the epoch set by the latest call. + std::uint64_t next = 0; + EXPECT(pacer.wait_frame_event(frame_event_origin, 0, 0, &next) == pacer_hr::timeout); + clock.advance_us(unchanged_period / 2); + EXPECT(pacer.wait_frame_event(frame_event_origin, 0, 0, &next) == pacer_hr::ok); + count = 1; + EXPECT(pacer.get_frame_statistics(next, stat_frame_event, &count, &stat) == pacer_hr::ok); + EXPECT(stat.time_us == 2 * unchanged_period + unchanged_period / 2); + } + // Scheduling inside a period, before the offset has passed: that period's origin is still to come. { ManualFrameClock clock;