upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src
diff options
context:
space:
mode:
authorAlxcBTW <187763517+AlxcBTW@users.noreply.github.com>2026-11-22 02:04:12 +0100
committerAlxcBTW <187763517+AlxcBTW@users.noreply.github.com>2026-11-22 05:28:21 +0100
commit5241cf2cfec1e7fbc76125e29a2f3a3f5ad62c1f (patch)
tree28fa17e7b6ea7c32e468a0476b01cc3f929ef7f0 /runtime/src
parent2481dd29979c6644fae30da75d7496c6dd890c11 (diff)
FramePacer: lenient tokens, SetFrameIntervalX keeps the origin armed, schedule keeps this period's origin
Only a token that was never handed out is refused; null, stale and repeated tokens are reported through TokenIssue and presented. SetFrameIntervalX re-bases the grid and leaves the origin event armed. ScheduleFrameEventX takes the origin whose offset has not passed yet. ManualFrameClock calls its wake under the lock. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/frame_pacer.h39
1 files changed, 24 insertions, 15 deletions
diff --git a/runtime/src/frame_pacer.h b/runtime/src/frame_pacer.h
index 0b34834c5..9c35c5c96 100644
--- a/runtime/src/frame_pacer.h
+++ b/runtime/src/frame_pacer.h
@@ -82,12 +82,9 @@ public:
82 } 82 }
83 void advance_us(std::uint64_t microseconds) { 83 void advance_us(std::uint64_t microseconds) {
84 now_ += microseconds; 84 now_ += microseconds;
85 std::function<void()> wake; 85 // Called under the lock, so a pacer that is being destroyed (set_wake({})) is never woken after it is gone.
86 { 86 std::lock_guard guard(wake_mutex_);
87 std::lock_guard guard(wake_mutex_); 87 if (wake_) wake_();
88 wake = wake_;
89 }
90 if (wake) wake();
91 } 88 }
92 89
93private: 90private:
@@ -135,8 +132,9 @@ public:
135 FramePacer& operator=(const FramePacer&) = delete; 132 FramePacer& operator=(const FramePacer&) = delete;
136 133
137 // SetFrameIntervalX(output, interval, latency, flags). `period_us` is the interval as a period; `latency` is 134 // SetFrameIntervalX(output, interval, latency, flags). `period_us` is the interval as a period; `latency` is
138 // the number of frames allowed in flight (0 counts as 1). The epoch of the vblank grid is now. Drops a 135 // the number of frames allowed in flight (0 counts as 1). The vblank grid is re-based: its epoch is now. A
139 // scheduled origin event: it has to be scheduled again, as after ResumeX. 136 // scheduled origin event stays armed (with its offset, kept below the new period) and its next origin is one
137 // new period from now; waiters blocked on the old grid pick the new one up.
140 std::int32_t set_frame_interval(std::uint32_t period_us, std::uint32_t latency, std::uint32_t flags) { 138 std::int32_t set_frame_interval(std::uint32_t period_us, std::uint32_t latency, std::uint32_t flags) {
141 (void)flags; 139 (void)flags;
142 if (period_us == 0) return pacer_hr::invalid_arg; 140 if (period_us == 0) return pacer_hr::invalid_arg;
@@ -146,7 +144,7 @@ public:
146 latency_ = std::max(latency, 1u); 144 latency_ = std::max(latency, 1u);
147 epoch_us_ = clock_.now_us(); 145 epoch_us_ = clock_.now_us();
148 interval_set_ = true; 146 interval_set_ = true;
149 armed_ = false; 147 offset_us_ = std::min(offset_us_, period_us - 1);
150 last_origin_ = 0; // origin 0 is the epoch itself, which has passed 148 last_origin_ = 0; // origin 0 is the epoch itself, which has passed
151 cv_.notify_all(); 149 cv_.notify_all();
152 return pacer_hr::ok; 150 return pacer_hr::ok;
@@ -161,7 +159,9 @@ public:
161 if (offset_us >= period_us_) return pacer_hr::invalid_arg; 159 if (offset_us >= period_us_) return pacer_hr::invalid_arg;
162 offset_us_ = offset_us; 160 offset_us_ = offset_us;
163 armed_ = true; 161 armed_ = true;
164 last_origin_ = std::int64_t((clock_.now_us() - epoch_us_) / period_us_); // the first origin is the next one 162 // The first origin is the next one that has not come yet: origin n is due at epoch + n * period + offset.
163 const std::uint64_t now = clock_.now_us();
164 last_origin_ = now >= epoch_us_ + offset_us_ ? std::int64_t((now - epoch_us_ - offset_us_) / period_us_) : 0;
165 cv_.notify_all(); 165 cv_.notify_all();
166 return pacer_hr::ok; 166 return pacer_hr::ok;
167 } 167 }
@@ -220,13 +220,22 @@ public:
220 } 220 }
221 221
222 // Queue side. check_present_token before the frame is presented, commit_present_token after it was queued. 222 // Queue side. check_present_token before the frame is presented, commit_present_token after it was queued.
223 // A token is valid once: it must have been issued by a wait and be newer than the last presented one (frames 223 // What real hardware does with a null, repeated or out-of-order token is unmeasured, and a title that presents
224 // whose token was never presented are skipped, which is fine). invalid_arg otherwise (null, unknown, stale or 224 // with Token = 0 (a loading screen), re-presents with one token, or presents from a render thread out of order
225 // repeated), invalid_state while suspended. 225 // must not freeze. So the only refusal is a token that was never handed out (invalid_arg, which is a bug in the
226 std::int32_t check_present_token(std::uint64_t token) { 226 // caller); null, stale (older than the last presented) and repeated tokens return ok and say which kind they
227 // were in `issue`, so the caller can log it. invalid_state while suspended.
228 enum class TokenIssue { none, null_token, stale, repeated };
229 std::int32_t check_present_token(std::uint64_t token, TokenIssue* issue = nullptr) {
227 std::lock_guard guard(mutex_); 230 std::lock_guard guard(mutex_);
231 if (issue) *issue = TokenIssue::none;
228 if (suspended_) return pacer_hr::invalid_state; 232 if (suspended_) return pacer_hr::invalid_state;
229 if (token == 0 || token > issued_ || token <= last_presented_) return pacer_hr::invalid_arg; 233 if (token > issued_) return pacer_hr::invalid_arg;
234 if (issue) {
235 if (token == 0) *issue = TokenIssue::null_token;
236 else if (token == last_presented_) *issue = TokenIssue::repeated;
237 else if (token < last_presented_) *issue = TokenIssue::stale;
238 }
230 return pacer_hr::ok; 239 return pacer_hr::ok;
231 } 240 }
232 void commit_present_token(std::uint64_t token, std::uint32_t plane_count) { 241 void commit_present_token(std::uint64_t token, std::uint32_t plane_count) {