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
10 changes: 6 additions & 4 deletions runtime/src/frame_pacer.h
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ class FramePacer {
std::uint64_t token = 0, origin_us = 0, present_us = 0, display_us = 0;
std::uint64_t due_us = 0; // vblank after this token's origin, fixed when issued
float percent_scanned = 0.0f;
bool origin_observed = false;
bool presented = false, displayed = false;
};

Expand Down Expand Up @@ -516,7 +517,7 @@ class FramePacer {
if (token != 0) {
for (const Record& r : records_) {
if (r.token == token) {
if (now >= r.origin_us) {
if (r.origin_observed && 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_;
Expand Down Expand Up @@ -554,20 +555,21 @@ class FramePacer {
auto& tick = last_event_tick_[frame_event_origin];
tick = std::max(tick, newest);
const auto origin = first + static_cast<std::uint64_t>(tick) * period_us_;
*token = issue_token_locked(origin, origin - offset + period_us_);
*token = issue_token_locked(origin, origin - offset + period_us_, now >= origin);
return pacer_hr::ok;
}

bool any_armed_locked() const {
return event_armed_[0] || event_armed_[1] || event_armed_[2];
}

std::uint64_t issue_token_locked(std::uint64_t origin_us, std::uint64_t due_us) {
std::uint64_t issue_token_locked(std::uint64_t origin_us, std::uint64_t due_us, bool origin_observed = true) {
const std::uint64_t tok = tokens_->issue(); // the shared token source (the device proxy and PresentX pace with it)
Record record;
record.token = tok;
record.origin_us = origin_us;
record.due_us = due_us;
record.origin_observed = origin_observed;
records_.push_back(record);
if (records_.size() > 64) records_.pop_front();
return tok;
Expand Down Expand Up @@ -614,7 +616,7 @@ class FramePacer {
if (give_up != no_deadline && now >= give_up) {
if (is_origin && token_on_timeout) {
// Frame started early on timeout: issue a consecutive token without consuming the tick.
*token = issue_token_locked(now, next_us - offset + period_us_);
*token = issue_token_locked(now, next_us - offset + period_us_, false);
return pacer_hr::false_;
}
return token_on_timeout ? pacer_hr::false_ : pacer_hr::timeout;
Expand Down
28 changes: 28 additions & 0 deletions runtime/test/frame_pacer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,25 @@ int main() {
EXPECT(next.get().token == 4);
}

// A current-token poll before the first origin can return a token, but has not observed a tick.
{
ManualFrameClock clock;
FramePacer pacer(clock, nullptr, true);
constexpr std::uint64_t offset = 5000;
EXPECT(setup(pacer, 4, offset));
std::uint64_t early = 0, observed = 0;
EXPECT(pacer.wait_frame_event(frame_event_origin, 0, wait_flag_current_token, &early) == pacer_hr::ok);
clock.advance_us(period + offset + 1000);
pacer.commit_present_token(early, 1);
EXPECT(pacer.pacing_summary().latency_samples == 0);
EXPECT(pacer.wait_frame_event(frame_event_origin, 0, wait_flag_current_token, &observed) == pacer_hr::ok &&
observed == early + 1);
clock.advance_us(1000);
pacer.commit_present_token(observed, 1);
const auto summary = pacer.pacing_summary();
EXPECT(summary.frames == 2 && summary.latency_samples == 1 && summary.latency_p50_us == 2000);
}

// Timeouts: 0 polls, a finite one counts fake time, the token is left alone, and a later wait still works.
{
ManualFrameClock clock;
Expand Down Expand Up @@ -797,6 +816,15 @@ int main() {
EXPECT(s1.frames == 1 && s1.samples == 0 && s1.latency_samples == 1 && s1.latency_p50_us == 2000);
EXPECT(near(s1.mean_us, 0.0) && near(s1.drift_us, 0.0));

std::uint64_t early = 0;
EXPECT(pacer.wait_frame_event(frame_event_origin, 0, frame_wait_flag_token_on_timeout, &early) ==
pacer_hr::false_ && early == tok + 1);
clock.advance_us(3000);
pacer.commit_present_token(early, 1);
const auto after_early = pacer.pacing_summary();
EXPECT(after_early.frames == 2 && after_early.samples == 1 && after_early.p50_us == 3000);
EXPECT(after_early.latency_samples == 1 && after_early.latency_p50_us == 2000);

// 2) 100 known intervals (16000 + 100*i us for i = 1..100) and known origin->present latencies:
// mean = 16000 + 5050 = 21050.0 us, drift = +5050.0 us,
// nearest-rank p50 = 16000 + 50*100 = 21000, p95 = 25500, p99 = 25900.
Expand Down
Loading