Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions runtime/src/frame_pacer.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +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; waiters blocked on the old grid pick the new one up.
// 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();
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_ = clock_.now_us();
epoch_us_ = now;
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);
Expand Down
76 changes: 76 additions & 0 deletions runtime/test/frame_pacer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,82 @@ 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);
}

// 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;
Expand Down