fix: pace and retain Quest XR sessions

This commit is contained in:
XxJSONDeruloxX committed 2026-10-14 20:47:24 -04:00
1 parent dbd277d5bd
commit 8bf7ba7eca
10 files changed
+202 -24

No files matched your search

+1 -1
View File
@@ -9,7 +9,7 @@ if (-not (Test-Path -LiteralPath $compiler -PathType Leaf)) { throw 'Android ARM
Push-Location $repo
try {
New-Item -ItemType Directory -Path build\vr\android-tests -Force | Out-Null
foreach ($name in @('test_xr_tracking', 'test_xr_frames', 'send_xr_frame')) {
foreach ($name in @('test_xr_tracking', 'test_xr_frames', 'test_xr_vsync', 'send_xr_frame')) {
& $compiler -std=c++17 -Wall -Wextra -Werror -static-libstdc++ -pthread "tools/vr/tests/$name.cpp" -o "build/vr/android-tests/$name"
if ($LASTEXITCODE -ne 0) { throw "Android test compilation failed: $name" }
}
+9 -14
View File
@@ -19,6 +19,7 @@
#include "presenter.h"
#include "xr_tracking_file.h"
#include "xr_frame_math.h"
#include "xr_vsync_timing.h"
#define DD_EXPORT extern "C" __attribute__((visibility("default")))
@@ -137,11 +138,12 @@ private:
class Vsync {
public:
static constexpr std::chrono::nanoseconds kInterval{16666667};
static constexpr auto kInterval = XrVsyncTiming::kDefaultPeriod;
static constexpr std::chrono::microseconds kLead{2000};
static void Start() {
if (running_.exchange(true)) return;
timing_.Reset();
thread_ = std::thread([] {
XrTrackingFile xrFile;
auto next = std::chrono::steady_clock::now() + kInterval;
@@ -150,19 +152,15 @@ public:
DroidDeckXrTracking xr;
if (xrFile.Read(&xr) && XrTrackingFile::Fresh(xr)) {
interval = std::chrono::nanoseconds(xr.period_ns);
auto tick = std::chrono::steady_clock::time_point(std::chrono::nanoseconds(xr.predicted_ns));
auto now = std::chrono::steady_clock::now();
if (xr.predicted_ns > 0 && tick > now && tick < now + interval * 3) next = tick;
next = timing_.Target(now, next, xr.predicted_ns, interval);
}
auto announce = next - kLead;
std::this_thread::sleep_until(announce);
auto now = std::chrono::steady_clock::now();
double offset = std::chrono::duration<double>(next - now).count();
timing_.Record(next, interval);
VRServerDriverHost()->VsyncEvent(offset);
{
std::lock_guard<std::mutex> lock(mutex_);
last_ = next;
}
next += interval;
now = std::chrono::steady_clock::now();
while (next <= now) next += interval;
@@ -173,16 +171,14 @@ public:
running_ = false;
if (thread_.joinable()) thread_.join();
}
static std::chrono::steady_clock::time_point LastTick() {
std::lock_guard<std::mutex> lock(mutex_);
return last_;
static std::chrono::steady_clock::time_point Deadline(uint32_t framesToThrottle) {
return timing_.Deadline(framesToThrottle);
}
private:
static inline std::atomic<bool> running_{false};
static inline std::thread thread_;
static inline std::mutex mutex_;
static inline std::chrono::steady_clock::time_point last_{};
static inline XrVsyncTiming timing_;
};
class DirectMode : public IVRDriverDirectModeComponent {
@@ -296,8 +292,7 @@ public:
}
void PostPresent(const Throttling_t *t) override {
auto last = Vsync::LastTick();
auto until = last + Vsync::kInterval * (1 + (t ? t->nFramesToThrottle : 0));
auto until = Vsync::Deadline(t ? t->nFramesToThrottle : 0);
if (until > std::chrono::steady_clock::now()) std::this_thread::sleep_until(until);
}
+36
View File
@@ -0,0 +1,36 @@
#pragma once
#include <chrono>
#include <cstdint>
#include <mutex>
class XrVsyncTiming {
public:
using Clock = std::chrono::steady_clock;
static constexpr std::chrono::nanoseconds kDefaultPeriod{16666667};
void Reset() { Record({}, kDefaultPeriod); }
Clock::time_point Target(Clock::time_point now, Clock::time_point scheduled,
int64_t predictedNs, std::chrono::nanoseconds period) const {
std::lock_guard<std::mutex> lock(mutex_);
auto predicted = Clock::time_point(std::chrono::nanoseconds(predictedNs));
return predictedNs > 0 && predicted > now && predicted > last_ && predicted < now + period * 3
? predicted : scheduled;
}
void Record(Clock::time_point target, std::chrono::nanoseconds period) {
std::lock_guard<std::mutex> lock(mutex_);
last_ = target;
period_ = period;
}
Clock::time_point Deadline(uint32_t framesToThrottle) const {
std::lock_guard<std::mutex> lock(mutex_);
return last_ + period_ * (1 + static_cast<int64_t>(framesToThrottle));
}
private:
mutable std::mutex mutex_;
Clock::time_point last_{};
std::chrono::nanoseconds period_{kDefaultPeriod};
};
+52
View File
@@ -0,0 +1,52 @@
#include "../src/xr_vsync_timing.h"
#include <atomic>
#include <cassert>
#include <cstdio>
#include <initializer_list>
#include <limits>
#include <thread>
int main() {
using namespace std::chrono;
using Clock = XrVsyncTiming::Clock;
XrVsyncTiming timing;
assert(timing.Deadline(0) == Clock::time_point(XrVsyncTiming::kDefaultPeriod));
auto target = Clock::time_point(seconds(1));
for (int64_t periodNs : {13888889LL, 11111111LL, 8333333LL}) {
auto period = nanoseconds(periodNs);
timing.Record(target, period);
assert(timing.Deadline(0) == target + period);
assert(timing.Deadline(2) == target + period * 3);
assert(timing.Deadline(std::numeric_limits<uint32_t>::max()) == target + period * 4294967296LL);
auto now = target - milliseconds(2);
auto scheduled = target + period;
assert(timing.Target(now, scheduled, 1000000000LL, period) == scheduled);
assert(timing.Target(now, scheduled, 0, period) == scheduled);
assert(timing.Target(now, scheduled, 900000000LL, period) == scheduled);
assert(timing.Target(now, scheduled, 2000000000LL, period) == scheduled);
auto prediction = target + period * 2;
auto predictedNs = duration_cast<nanoseconds>(prediction.time_since_epoch()).count();
assert(timing.Target(now, scheduled, predictedNs, period) == prediction);
}
auto first = Clock::time_point(seconds(1));
auto second = Clock::time_point(seconds(2));
auto firstPeriod = nanoseconds(13888889);
auto secondPeriod = nanoseconds(8333333);
timing.Record(first, firstPeriod);
std::atomic<bool> finished{false};
std::thread writer([&] {
for (int count = 0; count < 100000; count++) {
timing.Record(second, secondPeriod);
timing.Record(first, firstPeriod);
}
finished = true;
});
do {
auto deadline = timing.Deadline(0);
assert(deadline == first + firstPeriod || deadline == second + secondPeriod);
} while (!finished);
writer.join();
timing.Reset();
assert(timing.Deadline(0) == Clock::time_point(XrVsyncTiming::kDefaultPeriod));
std::puts("XR pacing validation passed: refresh rates, throttling, repeated predictions, atomic tick/period, restart");
}