script: Create a CrossProcessInstant to enable serializable monotonic time (#33282)

Up until now, Servo was using a very old version of time to get a
cross-process monotonic timestamp (using `time::precise_time_ns()`).
This change replaces the usage of old time with a new serializable
monotonic time called `CrossProcessInstant` and uses it where `u64`
timestamps were stored before. The standard library doesn't provide this
functionality because it isn't something you can do reliably on all
platforms. The idea is that we do our best and then fall back
gracefully.

This is a big change, because Servo was using `u64` timestamps all over
the place some as raw values taken from `time::precise_time_ns()` and
some as relative offsets from the "navigation start," which is a concept
similar to DOM's `timeOrigin` (but not exactly the same). It's very
difficult to fix this situation without fixing it everywhere as the
`Instant` concept is supposed to be opaque. The good thing is that this
change clears up all ambiguity when passing times as a `time::Duration`
is unit agnostic and a `CrossProcessInstant` represents an absolute
moment in time.

The `time` version of `Duration` is used because it can both be negative
and is also serializable.

Good things:
 - No need too pass around `time` and `time_precise` any longer.
   `CrossProcessInstant` is also precise and monotonic.
 - The distinction between a time that is unset or at `0` (at some kind
   of timer epoch) is now gone.

There still a lot of work to do to clean up timing, but this is the
first step. In general, I've tried to preserve existing behavior, even
when not spec compliant, as much as possible. I plan to submit followup
PRs fixing some of the issues I've noticed.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
Martin Robinson 2024-09-05 11:50:09 -07:00 committed by GitHub
parent 35baf056f6
commit 312cf0df08
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
51 changed files with 854 additions and 665 deletions

View file

@ -8,6 +8,7 @@ use std::f64::consts::{FRAC_PI_2, PI};
use std::rc::Rc;
use std::{mem, ptr};
use base::cross_process_instant::CrossProcessInstant;
use dom_struct::dom_struct;
use euclid::{RigidTransform3D, Transform3D, Vector3D};
use ipc_channel::ipc::IpcReceiver;
@ -15,7 +16,6 @@ use ipc_channel::router::ROUTER;
use js::jsapi::JSObject;
use js::jsval::JSVal;
use js::typedarray::Float32Array;
use metrics::ToMs;
use profile_traits::ipc;
use servo_atoms::Atom;
use webxr_api::{
@ -52,7 +52,6 @@ use crate::dom::bindings::utils::to_frozen_array;
use crate::dom::event::Event;
use crate::dom::eventtarget::EventTarget;
use crate::dom::globalscope::GlobalScope;
use crate::dom::performance::reduce_timing_resolution;
use crate::dom::promise::Promise;
use crate::dom::xrboundedreferencespace::XRBoundedReferenceSpace;
use crate::dom::xrframe::XRFrame;
@ -203,7 +202,7 @@ impl XRSession {
frame_receiver.to_opaque(),
Box::new(move |message| {
let frame: Frame = message.to().unwrap();
let time = time::precise_time_ns();
let time = CrossProcessInstant::now();
let this = this.clone();
let _ = task_source.queue_with_canceller(
task!(xr_raf_callback: move || {
@ -377,7 +376,7 @@ impl XRSession {
}
/// <https://immersive-web.github.io/webxr/#xr-animation-frame>
fn raf_callback(&self, mut frame: Frame, time: u64) {
fn raf_callback(&self, mut frame: Frame, time: CrossProcessInstant) {
debug!("WebXR RAF callback {:?}", frame);
// Step 1-2 happen in the xebxr device thread
@ -427,10 +426,10 @@ impl XRSession {
assert!(current.is_empty());
mem::swap(&mut *self.raf_callback_list.borrow_mut(), &mut current);
}
let start = self.global().as_window().get_navigation_start();
let time = reduce_timing_resolution((time - start).to_ms());
let time = self.global().performance().to_dom_high_res_time_stamp(time);
let frame = XRFrame::new(&self.global(), self, frame);
// Step 8-9
frame.set_active(true);
frame.set_animation_frame(true);