Add Servo scroll snapshots

This commit is contained in:
2026-05-08 13:54:56 -04:00
parent 8068d4ba32
commit 7178174e20
12 changed files with 738 additions and 268 deletions
@@ -8,7 +8,7 @@ use std::{
use ely_domain::{ProfileId, TabId, UrlText};
use ely_servo_host::{
NavigationRequest, RenderedFrame, ServoHost, ServoHostError, ServoSurfaceSize,
NavigationRequest, RenderedFrame, ScrollRequest, ServoHost, ServoHostError, ServoSurfaceSize,
SoftwareServoHost, WebViewSnapshot, WebViewState,
};
use serde::Serialize;
@@ -17,6 +17,7 @@ use thiserror::Error;
const WAIT_ITERATIONS: usize = 5_000;
const WAIT_INTERVAL: Duration = Duration::from_millis(2);
const RENDER_TIMEOUT: Duration = Duration::from_secs(20);
const SCROLL_SETTLE_TIMEOUT: Duration = Duration::from_millis(700);
fn main() -> Result<(), SidecarError> {
match parse_command(env::args())? {
@@ -33,6 +34,8 @@ struct SnapshotArgs {
rgba_out: PathBuf,
width: u32,
height: u32,
scroll_x: i32,
scroll_y: i32,
}
#[derive(Debug, Error)]
@@ -52,7 +55,7 @@ enum SidecarError {
#[error("unknown argument: {value}")]
UnknownArgument { value: String },
#[error("{name} must be a positive integer: {value}")]
#[error("{name} must be an integer: {value}")]
InvalidInteger {
name: &'static str,
value: String,
@@ -101,6 +104,8 @@ fn parse_snapshot_args(
let mut rgba_out = None;
let mut width = None;
let mut height = None;
let mut scroll_x = 0;
let mut scroll_y = 0;
while let Some(name) = args.next() {
match name.as_str() {
@@ -114,6 +119,14 @@ fn parse_snapshot_args(
"--height" => {
height = Some(parse_dimension("--height", next_argument(&mut args, "--height")?)?)
}
"--scroll-x" => {
scroll_x =
parse_scroll_delta("--scroll-x", next_argument(&mut args, "--scroll-x")?)?
}
"--scroll-y" => {
scroll_y =
parse_scroll_delta("--scroll-y", next_argument(&mut args, "--scroll-y")?)?
}
_ => return Err(SidecarError::UnknownArgument { value: name }),
}
}
@@ -123,6 +136,8 @@ fn parse_snapshot_args(
rgba_out: rgba_out.ok_or(SidecarError::MissingRequiredArgument { name: "--rgba-out" })?,
width: width.ok_or(SidecarError::MissingRequiredArgument { name: "--width" })?,
height: height.ok_or(SidecarError::MissingRequiredArgument { name: "--height" })?,
scroll_x,
scroll_y,
})
}
@@ -146,6 +161,10 @@ fn parse_dimension(name: &'static str, value: String) -> Result<u32, SidecarErro
Ok(dimension)
}
fn parse_scroll_delta(name: &'static str, value: String) -> Result<i32, SidecarError> {
value.parse::<i32>().map_err(|source| SidecarError::InvalidInteger { name, value, source })
}
fn parse_output_path(value: String) -> Result<PathBuf, SidecarError> {
if value.trim().is_empty() {
return Err(SidecarError::EmptyRgbaOutputPath);
@@ -167,16 +186,45 @@ fn run_snapshot(args: SnapshotArgs) -> Result<(), SidecarError> {
})?;
let snapshot = wait_for_frame(&mut host, &webview_id, args.url.as_str())?;
let (snapshot, scroll_changed_frame) =
apply_scroll_if_requested(&mut host, &webview_id, &args, snapshot)?;
let frame = host.last_rendered_frame()?;
std::fs::write(&args.rgba_out, frame.rgba_bytes())?;
serde_json::to_writer(
std::io::stdout().lock(),
&SnapshotReport::new(args.url.as_str(), &args.rgba_out, &snapshot, &frame),
&SnapshotReport::new(
args.url.as_str(),
&args.rgba_out,
&snapshot,
&frame,
args.scroll_x,
args.scroll_y,
scroll_changed_frame,
),
)?;
Ok(())
}
fn apply_scroll_if_requested(
host: &mut SoftwareServoHost,
webview_id: &ely_domain::WebViewId,
args: &SnapshotArgs,
snapshot: WebViewSnapshot,
) -> Result<(WebViewSnapshot, bool), SidecarError> {
if args.scroll_x == 0 && args.scroll_y == 0 {
return Ok((snapshot, false));
}
let previous_frame_hash = host.last_rendered_frame()?.sample_hash();
host.scroll(ScrollRequest {
webview_id: webview_id.clone(),
delta_x: args.scroll_x,
delta_y: args.scroll_y,
})?;
wait_for_changed_or_settled_frame(host, webview_id, previous_frame_hash)
}
fn wait_for_frame(
host: &mut SoftwareServoHost,
webview_id: &ely_domain::WebViewId,
@@ -210,6 +258,39 @@ fn wait_for_frame(
})
}
fn wait_for_changed_or_settled_frame(
host: &mut SoftwareServoHost,
webview_id: &ely_domain::WebViewId,
previous_frame_hash: u64,
) -> Result<(WebViewSnapshot, bool), SidecarError> {
let started_at = Instant::now();
let mut latest_snapshot = host.snapshot(webview_id)?;
for _ in 0..WAIT_ITERATIONS {
if started_at.elapsed() >= SCROLL_SETTLE_TIMEOUT {
break;
}
host.tick();
let snapshot = host.snapshot(webview_id)?;
if snapshot.has_pending_frame() {
host.paint(webview_id)?;
}
latest_snapshot = host.snapshot(webview_id)?;
let changed_frame = host.last_rendered_frame().is_ok_and(|frame| {
frame.non_white_pixel_count() > 0 && frame.sample_hash() != previous_frame_hash
});
if changed_frame {
return Ok((latest_snapshot, true));
}
thread::sleep(WAIT_INTERVAL);
}
Ok((latest_snapshot, false))
}
#[derive(Serialize)]
struct SnapshotReport {
requested_url: String,
@@ -224,6 +305,9 @@ struct SnapshotReport {
non_white_pixel_count: u64,
content_pixel_count: u64,
sample_hash: u64,
scroll_x: i32,
scroll_y: i32,
scroll_changed_frame: bool,
}
impl SnapshotReport {
@@ -232,6 +316,9 @@ impl SnapshotReport {
rgba_path: &std::path::Path,
snapshot: &WebViewSnapshot,
frame: &RenderedFrame,
scroll_x: i32,
scroll_y: i32,
scroll_changed_frame: bool,
) -> Self {
Self {
requested_url: requested_url.to_string(),
@@ -246,6 +333,9 @@ impl SnapshotReport {
non_white_pixel_count: frame.non_white_pixel_count(),
content_pixel_count: frame.content_pixel_count(),
sample_hash: frame.sample_hash(),
scroll_x,
scroll_y,
scroll_changed_frame,
}
}
}
+9
View File
@@ -213,6 +213,13 @@ pub struct NavigationRequest {
pub url: UrlText,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ScrollRequest {
pub webview_id: WebViewId,
pub delta_x: i32,
pub delta_y: i32,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PermissionRequest {
pub webview_id: WebViewId,
@@ -237,6 +244,8 @@ pub trait ServoHost {
fn navigate(&mut self, request: NavigationRequest) -> Result<(), ServoHostError>;
fn scroll(&mut self, request: ScrollRequest) -> Result<(), ServoHostError>;
fn set_permission(
&mut self,
request: PermissionRequest,
+1 -1
View File
@@ -6,7 +6,7 @@ mod runtime;
pub use error::ServoHostError;
pub use host::{
NavigationRequest, PermissionDecision, PermissionRequest, RenderedFrame, RenderedFrameSummary,
ServoHost, WebViewSnapshot, WebViewState,
ScrollRequest, ServoHost, WebViewSnapshot, WebViewState,
};
#[cfg(feature = "servo-engine")]
pub use runtime::{ServoSurfaceSize, SoftwareServoHost};
+25 -4
View File
@@ -11,14 +11,15 @@ use std::{
use dpi::PhysicalSize;
use ely_domain::{ProfileId, TabId, WebViewId};
use servo::{
DeviceIntPoint, DeviceIntRect, DeviceIntSize, EventLoopWaker, LoadStatus, RenderingContext,
Servo, ServoBuilder, WebView, WebViewBuilder, WebViewDelegate,
DeviceIntPoint, DeviceIntRect, DeviceIntSize, DevicePoint, DeviceVector2D, EventLoopWaker,
LoadStatus, RenderingContext, Scroll, Servo, ServoBuilder, WebView, WebViewBuilder,
WebViewDelegate, WebViewPoint, WebViewVector,
};
use url::Url;
use crate::{
NavigationRequest, PermissionDecision, PermissionRequest, RenderedFrame, ServoHost,
ServoHostError, WebViewSnapshot, WebViewState,
NavigationRequest, PermissionDecision, PermissionRequest, RenderedFrame, ScrollRequest,
ServoHost, ServoHostError, WebViewSnapshot, WebViewState,
};
static SERVO_RUNTIME_STARTED: AtomicBool = AtomicBool::new(false);
@@ -142,6 +143,26 @@ impl ServoHost for SoftwareServoHost {
Ok(())
}
fn scroll(&mut self, request: ScrollRequest) -> Result<(), ServoHostError> {
let webview = self
.webviews
.get(&request.webview_id)
.ok_or_else(|| ServoHostError::WebViewNotFound { id: request.webview_id.clone() })?;
if request.delta_x == 0 && request.delta_y == 0 {
return Ok(());
}
webview.webview.notify_scroll_event(
Scroll::Delta(WebViewVector::Device(DeviceVector2D::new(
request.delta_x as f32,
request.delta_y as f32,
))),
WebViewPoint::Device(DevicePoint::zero()),
);
Ok(())
}
fn set_permission(
&mut self,
request: PermissionRequest,
+85 -11
View File
@@ -20,6 +20,10 @@ const PRD_SITE_COMPATIBILITY_SIZES: &[FrameSize] = &[
FrameSize { width: 934, height: 657 },
FrameSize { width: 1614, height: 980 },
];
const SERVO_SCROLL_SITE: PrdSiteCompatibilityCase =
PrdSiteCompatibilityCase { url: "https://servo.org", title_fragment: "Servo" };
const SERVO_SCROLL_SIZE: FrameSize = FrameSize { width: 934, height: 657 };
const SERVO_SCROLL_OFFSET: ScrollOffset = ScrollOffset { x: 0, y: 480 };
struct PrdSiteCompatibilityCase {
url: &'static str,
@@ -32,38 +36,81 @@ struct FrameSize {
height: u64,
}
#[derive(Clone, Copy)]
struct ScrollOffset {
x: i64,
y: i64,
}
impl ScrollOffset {
const ZERO: Self = Self { x: 0, y: 0 };
}
#[test]
fn sidecar_snapshots_prd_sites_to_rgba_files() -> Result<(), Box<dyn Error>> {
for case in PRD_SITE_COMPATIBILITY_CASES {
for size in PRD_SITE_COMPATIBILITY_SIZES {
snapshot_prd_site(case, *size)?;
snapshot_prd_site(case, *size, ScrollOffset::ZERO)?;
}
}
Ok(())
}
#[test]
fn sidecar_scrolls_prd_site_with_servo_input() -> Result<(), Box<dyn Error>> {
let initial_report =
snapshot_prd_site(&SERVO_SCROLL_SITE, SERVO_SCROLL_SIZE, ScrollOffset::ZERO)?;
let scrolled_report =
snapshot_prd_site(&SERVO_SCROLL_SITE, SERVO_SCROLL_SIZE, SERVO_SCROLL_OFFSET)?;
assert_eq!(report_field_as_i64(&scrolled_report, "scroll_x")?, SERVO_SCROLL_OFFSET.x);
assert_eq!(report_field_as_i64(&scrolled_report, "scroll_y")?, SERVO_SCROLL_OFFSET.y);
assert_eq!(
report_field_as_u64(&scrolled_report, "width")?,
SERVO_SCROLL_SIZE.width,
"{}",
SERVO_SCROLL_SITE.url
);
assert!(
report_field_as_bool(&scrolled_report, "scroll_changed_frame")?,
"{}",
SERVO_SCROLL_SITE.url
);
assert_ne!(
report_field_as_u64(&initial_report, "sample_hash")?,
report_field_as_u64(&scrolled_report, "sample_hash")?,
"{}",
SERVO_SCROLL_SITE.url
);
Ok(())
}
fn snapshot_prd_site(
case: &PrdSiteCompatibilityCase,
size: FrameSize,
) -> Result<(), Box<dyn Error>> {
scroll_offset: ScrollOffset,
) -> Result<serde_json::Value, Box<dyn Error>> {
let site_name = case
.url
.chars()
.map(|character| if character.is_ascii_alphanumeric() { character } else { '-' })
.collect::<String>();
let output_path = std::env::temp_dir().join(format!(
"ely-servo-sidecar-{}-{site_name}-{}x{}.rgba",
"ely-servo-sidecar-{}-{site_name}-{}x{}-{}-{}.rgba",
std::process::id(),
size.width,
size.height
size.height,
scroll_offset.x,
scroll_offset.y
));
if output_path.exists() {
std::fs::remove_file(&output_path)?;
}
let output = run_sidecar_snapshot(case.url, &output_path, size)?;
let output = run_sidecar_snapshot(case.url, &output_path, size, scroll_offset)?;
assert!(
output.status.success(),
@@ -97,15 +144,17 @@ fn snapshot_prd_site(
assert_eq!(std::fs::metadata(&output_path)?.len(), size.width * size.height * 4);
std::fs::remove_file(&output_path)?;
Ok(())
Ok(report)
}
fn run_sidecar_snapshot(
site_url: &str,
output_path: &std::path::Path,
size: FrameSize,
scroll_offset: ScrollOffset,
) -> Result<Output, Box<dyn Error>> {
let mut child = Command::new(env!("CARGO_BIN_EXE_ely_servo_sidecar"))
let mut command = Command::new(env!("CARGO_BIN_EXE_ely_servo_sidecar"));
command
.arg("snapshot")
.arg("--url")
.arg(site_url)
@@ -114,10 +163,15 @@ fn run_sidecar_snapshot(
.arg("--width")
.arg(size.width.to_string())
.arg("--height")
.arg(size.height.to_string())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()?;
.arg(size.height.to_string());
if scroll_offset.x != 0 {
command.arg("--scroll-x").arg(scroll_offset.x.to_string());
}
if scroll_offset.y != 0 {
command.arg("--scroll-y").arg(scroll_offset.y.to_string());
}
let mut child = command.stdout(Stdio::piped()).stderr(Stdio::piped()).spawn()?;
let started_at = Instant::now();
loop {
@@ -162,6 +216,26 @@ fn assert_report_text_contains(
Ok(())
}
fn report_field_as_bool(
report: &serde_json::Value,
field: &'static str,
) -> Result<bool, Box<dyn Error>> {
report
.get(field)
.and_then(serde_json::Value::as_bool)
.ok_or_else(|| format!("missing boolean report field: {field}").into())
}
fn report_field_as_i64(
report: &serde_json::Value,
field: &'static str,
) -> Result<i64, Box<dyn Error>> {
report
.get(field)
.and_then(serde_json::Value::as_i64)
.ok_or_else(|| format!("missing signed report field: {field}").into())
}
fn report_field_as_u64(
report: &serde_json::Value,
field: &'static str,
+9 -1
View File
@@ -4,7 +4,8 @@ use std::{error::Error, thread, time::Duration};
use ely_domain::{ProfileId, TabId, UrlText};
use ely_servo_host::{
NavigationRequest, ServoHost, ServoHostError, ServoSurfaceSize, SoftwareServoHost, WebViewState,
NavigationRequest, ScrollRequest, ServoHost, ServoHostError, ServoSurfaceSize,
SoftwareServoHost, WebViewState,
};
const MINIMUM_CONTENT_PIXELS: u64 = 1_000;
@@ -69,6 +70,13 @@ fn manages_real_servo_webview_lifecycle() -> Result<(), Box<dyn Error>> {
previous_frame_hash = Some(host.last_rendered_frame()?.sample_hash());
}
let previous_frame_hash = host.last_rendered_frame()?.sample_hash();
host.scroll(ScrollRequest { webview_id: webview_id.clone(), delta_x: 0, delta_y: 480 })?;
let snapshot = wait_for_rendered_webview(&mut host, &webview_id, Some(previous_frame_hash))?;
assert_eq!(snapshot.state(), &WebViewState::Complete, "snapshot: {snapshot:?}");
assert_rendered_frame_has_content(&host, "https://servo.org scrolled", MINIMUM_CONTENT_PIXELS)?;
assert_ne!(host.last_rendered_frame()?.sample_hash(), previous_frame_hash);
assert!(matches!(
SoftwareServoHost::new(ServoSurfaceSize::new(640, 480)),
Err(ServoHostError::RuntimeAlreadyStarted)