Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

feat(emulation): apply native view scaling and capture viewport

ldm017e961433e68
diff --git a/moli-layout/src/capture.rs b/moli-layout/src/capture.rs
index 36c275262..d1db5ce79 100644
--- a/moli-layout/src/capture.rs
+++ b/moli-layout/src/capture.rs
@@ -18,6 +18,13 @@ pub enum PaintCaptureRegion {
FullDocument,
/// One explicit page-coordinate rectangle.
PageClip { rect: LayoutRect, scale: f32 },
+ /// Present the existing viewport on a separate raster surface. Scroll
+ /// compensation is resolved after layout samples the current scroll offset.
+ TransformedViewport {
+ surface: PaintCaptureSurface,
+ transform: LayoutTransform2D,
+ scroll_scale: f64,
+ },
}
/// Output constraints for one short-lived screenshot or screencast capture.
@@ -129,9 +136,12 @@ impl PaintCaptureSurface {
pub(crate) struct ResolvedPaintCapture {
/// Captured rectangle in the projection's viewport coordinate system.
pub(crate) viewport_rect: LayoutRect,
- /// Translation applied after local-to-viewport transforms.
+ /// Transform applied after local-to-viewport transforms.
pub(crate) viewport_to_surface: LayoutTransform2D,
pub(crate) surface: PaintCaptureSurface,
+ /// Clip painted content to the transformed live viewport. The propagated
+ /// canvas color still clears the whole surface, matching the compositor.
+ pub(crate) clip_to_viewport: bool,
pub(crate) include_backgrounds: bool,
/// Viewport captures include the root viewport's native controls. Full
/// document and page-clip captures paint page content only, matching
@@ -148,17 +158,16 @@ impl PaintCaptureRequest {
content_size: LayoutSize,
) -> Result<ResolvedPaintCapture, LayoutError> {
validate_viewport(viewport)?;
- let paint_root_scrollbars = self.include_viewport_controls;
+ let viewport_bounds = LayoutRect::new(
+ 0.0,
+ 0.0,
+ viewport.css_width as f32,
+ viewport.css_height as f32,
+ );
let (viewport_rect, capture_scale) = match self.region {
- PaintCaptureRegion::Viewport => (
- LayoutRect::new(
- 0.0,
- 0.0,
- viewport.css_width as f32,
- viewport.css_height as f32,
- ),
- 1.0,
- ),
+ PaintCaptureRegion::Viewport | PaintCaptureRegion::TransformedViewport { .. } => {
+ (viewport_bounds, 1.0)
+ }
PaintCaptureRegion::FullDocument => {
validate_full_document_extent(content_size.width, content_size.height)?;
(
@@ -185,18 +194,42 @@ impl PaintCaptureRequest {
}
};
- let mut device_scale = viewport.device_pixel_ratio * capture_scale;
+ let (mut surface, viewport_to_surface, clip_to_viewport) = match self.region {
+ PaintCaptureRegion::TransformedViewport {
+ surface,
+ mut transform,
+ scroll_scale,
+ } => {
+ transform.coefficients[4] += f64::from(viewport_scroll.x) * scroll_scale;
+ transform.coefficients[5] += f64::from(viewport_scroll.y) * scroll_scale;
+ if !transform.is_finite() || !scroll_scale.is_finite() {
+ return Err(invalid_capture("viewport transform must be finite"));
+ }
+ (surface, transform, true)
+ }
+ _ => (
+ PaintCaptureSurface::new(
+ viewport_rect.width,
+ viewport_rect.height,
+ viewport.device_pixel_ratio * capture_scale,
+ ),
+ LayoutTransform2D::translation(-viewport_rect.x, -viewport_rect.y),
+ false,
+ ),
+ };
+ validate_extent("capture surface", surface.css_width, surface.css_height)?;
+ let mut device_scale = surface.device_scale;
if let Some(max_width) = self.max_width {
if max_width == 0 {
return Err(invalid_capture("maximum width must be greater than zero"));
}
- device_scale = device_scale.min(max_width as f32 / viewport_rect.width);
+ device_scale = device_scale.min(max_width as f32 / surface.css_width);
}
if let Some(max_height) = self.max_height {
if max_height == 0 {
return Err(invalid_capture("maximum height must be greater than zero"));
}
- device_scale = device_scale.min(max_height as f32 / viewport_rect.height);
+ device_scale = device_scale.min(max_height as f32 / surface.css_height);
}
if !device_scale.is_finite() || device_scale <= 0.0 {
return Err(invalid_capture(
@@ -204,16 +237,14 @@ impl PaintCaptureRequest {
));
}
+ surface.device_scale = device_scale;
Ok(ResolvedPaintCapture {
viewport_rect,
- viewport_to_surface: LayoutTransform2D::translation(-viewport_rect.x, -viewport_rect.y),
- surface: PaintCaptureSurface::new(
- viewport_rect.width,
- viewport_rect.height,
- device_scale,
- ),
+ viewport_to_surface,
+ surface,
+ clip_to_viewport,
include_backgrounds: self.include_backgrounds,
- paint_root_scrollbars,
+ paint_root_scrollbars: self.include_viewport_controls,
base_background_color: self.base_background_color,
})
}
diff --git a/moli-layout/src/paint/mod.rs b/moli-layout/src/paint/mod.rs
index 42d94d95e..145e0a920 100644
--- a/moli-layout/src/paint/mod.rs
+++ b/moli-layout/src/paint/mod.rs
@@ -153,6 +153,12 @@ where
..PaintProjectionMetrics::default()
};
let mut current_cull = PaintCullRegion::for_capture(capture.viewport_rect);
+ if capture.clip_to_viewport {
+ snapshot.push_fragment(PaintFragment::PushClip {
+ shape: PaintShape::Rect(capture.viewport_rect),
+ transform: capture.viewport_to_surface,
+ });
+ }
if let Some(background) = propagated_background {
let transform = capture.viewport_to_surface;
let paint_space = PaintSpace::ROOT.with_outer_transform(transform);
@@ -466,6 +472,9 @@ where
active_clips.clear(&mut snapshot);
debug_assert!(context_layers.is_empty());
debug_assert!(context_cull_regions.is_empty());
+ if capture.clip_to_viewport {
+ snapshot.push_fragment(PaintFragment::PopLayer);
+ }
PaintProjectionResult { snapshot, metrics }
}
diff --git a/moli-page-types/src/lib.rs b/moli-page-types/src/lib.rs
index c50decb86..691f1330c 100644
--- a/moli-page-types/src/lib.rs
+++ b/moli-page-types/src/lib.rs
@@ -3137,6 +3137,36 @@ pub struct ViewportSurface {
pub window_x: i32,
pub window_y: i32,
pub screen_orientation: ScreenOrientation,
+ /// Presentation of the native widget, independent of the CSS layout size.
+ pub emulated_view: Option<EmulatedView>,
+}
+
+#[derive(Debug, Clone, Copy, PartialEq)]
+pub struct EmulatedView {
+ pub width: u32,
+ pub height: u32,
+ pub scale: f64,
+ /// The compositor keeps its native DPR even when CSS reports an override.
+ pub native_device_pixel_ratio: f64,
+ pub viewport: Option<EmulatedViewport>,
+}
+
+/// Page-coordinate origin and scale of a forced compositor viewport. Its
+/// requested extent is already accounted for in the native widget dimensions.
+#[derive(Debug, Clone, Copy, PartialEq)]
+pub struct EmulatedViewport {
+ pub x: f64,
+ pub y: f64,
+ pub scale: f64,
+}
+
+impl ViewportSurface {
+ pub fn visible_size(self) -> (u32, u32) {
+ self.emulated_view
+ .map_or((self.inner_width, self.inner_height), |view| {
+ (view.width, view.height)
+ })
+ }
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -3171,6 +3201,7 @@ impl Default for ViewportSurface {
window_x: 0,
window_y: 0,
screen_orientation: Default::default(),
+ emulated_view: None,
}
}
}
diff --git a/moli-protocol/src/conn/state/emulation.rs b/moli-protocol/src/conn/state/emulation.rs
index 3760f71fd..1c6cf993d 100644
--- a/moli-protocol/src/conn/state/emulation.rs
+++ b/moli-protocol/src/conn/state/emulation.rs
@@ -2,9 +2,7 @@
pub struct EmulatedDeviceMetrics {
pub width: u32,
pub height: u32,
- /// Actual visible widget dimensions, retained across single-axis overrides.
- pub visible_width: u32,
- pub visible_height: u32,
+ pub view: Option<moli_page_types::EmulatedView>,
pub outer_width: u32,
pub outer_height: u32,
pub device_scale_factor: f64,
@@ -60,6 +58,7 @@ impl EmulatedDeviceMetrics {
window_x: self.window_x,
window_y: self.window_y,
screen_orientation: self.screen_orientation,
+ emulated_view: self.view,
}
}
}
@@ -226,8 +225,7 @@ mod tests {
let mut metrics = EmulatedDeviceMetrics {
width: 800,
height: 600,
- visible_width: 800,
- visible_height: 600,
+ view: None,
outer_width: 800,
outer_height: 600,
device_scale_factor: 2.0,
diff --git a/moli-protocol/src/domains/emulation.rs b/moli-protocol/src/domains/emulation.rs
index 4a6605f7f..123d5be47 100644
--- a/moli-protocol/src/domains/emulation.rs
+++ b/moli-protocol/src/domains/emulation.rs
@@ -1096,8 +1096,7 @@ fn set_viewport_metrics_from_current(
Ok(EmulatedDeviceMetrics {
width,
height,
- visible_width: width,
- visible_height: height,
+ view: None,
outer_width: width,
outer_height: height,
device_scale_factor,
diff --git a/moli-protocol/src/domains/emulation/device.rs b/moli-protocol/src/domains/emulation/device.rs
index e7a630fc0..480d5b979 100644
--- a/moli-protocol/src/domains/emulation/device.rs
+++ b/moli-protocol/src/domains/emulation/device.rs
@@ -61,19 +61,55 @@ pub(super) fn metrics_from_cdp(
{
return Err(invalid());
}
- let mut visible_width = previous.map_or(base.inner_width, |m| m.visible_width);
- let mut visible_height = previous.map_or(base.inner_height, |m| m.visible_height);
- if width != 0 && height != 0 && !params.dont_set_visible_size.unwrap_or(false) {
- visible_width = width;
- visible_height = height;
+ let device_scale_factor = if params.device_scale_factor == 0.0 {
+ base.device_pixel_ratio
+ } else {
+ params.device_scale_factor
+ };
+ let mut visible_size = previous.map_or(base.visible_size(), |metrics| {
+ metrics.viewport_surface().visible_size()
+ });
+ let mut requested_size = (width, height);
+ let mut viewport = None;
+ if let Some(clip) = params.viewport {
+ if ![clip.x, clip.y, clip.width, clip.height, clip.scale]
+ .into_iter()
+ .all(f64::is_finite)
+ {
+ return Err(invalid());
+ }
+ let viewport_scale = clip.scale * device_scale_factor / base.device_pixel_ratio;
+ if !viewport_scale.is_finite() {
+ return Err(invalid());
+ }
+ // Chromium accepts zero/negative clip scales. They can leave no visible
+ // content, but must not turn a negative extent into an enormous widget.
+ let dimension = |value: f64| value.round().clamp(0.0, f64::from(i32::MAX)) as u32;
+ requested_size = (
+ dimension(clip.width * viewport_scale),
+ dimension(clip.height * viewport_scale),
+ );
+ if clip.x >= 0.0 {
+ viewport = Some(moli_page_types::EmulatedViewport {
+ x: clip.x,
+ y: clip.y,
+ scale: viewport_scale,
+ });
+ }
+ }
+ if requested_size.0 != 0
+ && requested_size.1 != 0
+ && !params.dont_set_visible_size.unwrap_or(false)
+ {
+ visible_size = requested_size;
}
let width = if width == 0 {
- (f64::from(visible_width) / scale).round() as u32
+ (f64::from(visible_size.0) / scale).round() as u32
} else {
width
};
let height = if height == 0 {
- (f64::from(visible_height) / scale).round() as u32
+ (f64::from(visible_size.1) / scale).round() as u32
} else {
height
};
@@ -87,8 +123,13 @@ pub(super) fn metrics_from_cdp(
Ok(EmulatedDeviceMetrics {
width,
height,
- visible_width,
- visible_height,
+ view: Some(moli_page_types::EmulatedView {
+ width: visible_size.0,
+ height: visible_size.1,
+ scale,
+ native_device_pixel_ratio: base.device_pixel_ratio,
+ viewport,
+ }),
outer_width: if params.mobile {
width
} else {
@@ -99,11 +140,7 @@ pub(super) fn metrics_from_cdp(
} else {
base.outer_height
},
- device_scale_factor: if params.device_scale_factor == 0.0 {
- base.device_pixel_ratio
- } else {
- params.device_scale_factor
- },
+ device_scale_factor,
screen_width,
screen_height,
screen_avail_height: screen_height,
diff --git a/moli-protocol/src/domains/page/tests/capture.rs b/moli-protocol/src/domains/page/tests/capture.rs
index 87f629413..b172546a8 100644
--- a/moli-protocol/src/domains/page/tests/capture.rs
+++ b/moli-protocol/src/domains/page/tests/capture.rs
@@ -5,6 +5,8 @@ use crate::domains::page::{
};
use serde_json::Value;
+mod emulation;
+
/// cdp.page: captureScreenshot – invalid image format
#[tokio::test(flavor = "multi_thread")]
async fn capture_screenshot_bad_format() {
@@ -655,8 +657,7 @@ async fn capture_screenshot_targets_loaded_background_owner_without_activation()
.emulated_device_metrics = Some(EmulatedDeviceMetrics {
width: 320,
height: 240,
- visible_width: 320,
- visible_height: 240,
+ view: None,
outer_width: 320,
outer_height: 240,
device_scale_factor: 2.0,
@@ -712,8 +713,7 @@ async fn capture_screenshot_targets_inactive_loaded_owner_without_activation() {
.emulated_device_metrics = Some(EmulatedDeviceMetrics {
width: 500,
height: 300,
- visible_width: 500,
- visible_height: 300,
+ view: None,
outer_width: 500,
outer_height: 300,
device_scale_factor: 1.5,
diff --git a/moli-protocol/src/domains/page/tests/capture/emulation.rs b/moli-protocol/src/domains/page/tests/capture/emulation.rs
new file mode 100644
index 000000000..cf972845a
--- /dev/null
+++ b/moli-protocol/src/domains/page/tests/capture/emulation.rs
@@ -0,0 +1,425 @@
+use super::*;
+use crate::domains::page::{
+ PageScreencastCaptureCompletion, PageScreencastCaptureStart, PageScreencastRegistration,
+};
+use moli_core::page::RendererVisualStateToken;
+
+const SESSION: &str = "SID-VIEW-CAPTURE";
+const HTML: &str = r#"<!doctype html><style>
+ html { background: blue; scrollbar-width: none }
+ body { margin: 0 }
+ #stripe { width: 40px; height: 100px; background: red }
+</style><div id="stripe"></div>"#;
+
+async fn command(ctx: &mut TestContext, method: &str, params: Value) -> Value {
+ ctx.process_async(json!({
+ "id": 88500, "sessionId": SESSION, "method": method, "params": params
+ }))
+ .await;
+ let response = ctx
+ .wait_for_scheduler_message(method, |message| message["id"] == 88500)
+ .await;
+ assert!(response["error"].is_null(), "{method}: {response}");
+ assert!(
+ response["result"]["exceptionDetails"].is_null(),
+ "{method}: {response}"
+ );
+ response["result"].clone()
+}
+
+async fn set_metrics(ctx: &mut TestContext, extra: Value) {
+ let mut params = json!({
+ "width": 200, "height": 100, "deviceScaleFactor": 1, "mobile": false
+ });
+ params
+ .as_object_mut()
+ .unwrap()
+ .extend(extra.as_object().unwrap().clone());
+ command(ctx, "Emulation.setDeviceMetricsOverride", params).await;
+}
+
+async fn install_page(ctx: &mut TestContext) {
+ install_active_screenshot_page(
+ ctx,
+ "BID-VIEW-CAPTURE",
+ "TID-VIEW-CAPTURE",
+ SESSION,
+ &screenshot_data_url(HTML),
+ )
+ .await;
+ set_metrics(ctx, json!({})).await;
+}
+
+fn start_screencast(ctx: &mut TestContext, extra: Value) -> PageScreencastRegistration {
+ let mut params = json!({"format": "png"});
+ params
+ .as_object_mut()
+ .unwrap()
+ .extend(extra.as_object().unwrap().clone());
+ let raw = json!({
+ "id": 88501, "sessionId": SESSION, "method": "Page.startScreencast", "params": params
+ })
+ .to_string();
+ let CdpCommandTaskStep::Complete(outcome) = ctx.conn.start_command_dispatch(&raw) else {
+ panic!("screencast registration should complete synchronously");
+ };
+ let (messages, events) = outcome.into_parts();
+ assert!(
+ messages
+ .iter()
+ .any(|message| message["id"] == 88501 && message["result"] == json!({}))
+ );
+ events
+ .into_iter()
+ .find_map(|event| match event {
+ CdpSchedulerEvent::PageScreencastStarted { registration } => Some(registration),
+ _ => None,
+ })
+ .expect("screencast registration")
+}
+
+async fn poll_screencast(
+ ctx: &mut TestContext,
+ registration: &PageScreencastRegistration,
+ previous: Option<RendererVisualStateToken>,
+) -> PageScreencastCaptureCompletion {
+ let PageScreencastCaptureStart::Pending(capture) = ctx
+ .conn
+ .start_page_screencast_frame_capture(registration, previous)
+ else {
+ panic!("registered screencast should start a renderer capture");
+ };
+ ctx.conn
+ .complete_page_screencast_frame_capture(capture.wait().await)
+}
+
+fn take_frame(
+ ctx: &mut TestContext,
+ registration: &PageScreencastRegistration,
+ completion: PageScreencastCaptureCompletion,
+) -> (Value, RendererVisualStateToken) {
+ let PageScreencastCaptureCompletion::Frame {
+ event,
+ visual_state,
+ } = completion
+ else {
+ panic!("changed presentation must produce a frame");
+ };
+ let (event, _) = event.into_parts();
+ assert_eq!(
+ ctx.conn
+ .acknowledge_page_screencast_frame_for_session_owner(
+ Some(SESSION),
+ registration.generation(),
+ ),
+ Some(true)
+ );
+ (event["params"].clone(), visual_state)
+}
+
+fn png(data: &Value) -> Vec<u8> {
+ BASE64_STANDARD
+ .decode(data.as_str().expect("encoded PNG"))
+ .unwrap()
+}
+
+#[track_caller]
+fn assert_blue_boundary(bytes: &[u8], dimensions: (u32, u32), boundary: Option<u32>) {
+ assert_png_dimensions(bytes, dimensions.0, dimensions.1);
+ let image = moli_image::decode_png(bytes).unwrap();
+ let row = 10 * image.width as usize * 4;
+ let first_blue = image.rgba[row..row + image.width as usize * 4]
+ .chunks_exact(4)
+ .position(|pixel| pixel == [0, 0, 255, 255])
+ .map(|x| x as u32);
+ assert_eq!(first_blue, boundary);
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn emulated_view_capture_distinguishes_layout_widget_and_explicit_clip() {
+ let mut ctx = TestContext::new();
+ install_page(&mut ctx).await;
+
+ // Expected pixels were checked against Chromium. In particular, an
+ // ordinary surface screenshot resets preview scale but keeps a forced
+ // viewport; a clip supplied on captureScreenshot replaces that viewport.
+ let viewport = json!({"x": 30, "y": 0, "width": 60, "height": 50, "scale": 2});
+ let cases = [
+ (
+ json!({"scale": 0.5}),
+ (200, 100),
+ Some(20),
+ (200, 100),
+ Some(40),
+ 1,
+ ),
+ (
+ json!({"scale": 2}),
+ (200, 100),
+ Some(80),
+ (200, 100),
+ Some(40),
+ 1,
+ ),
+ (
+ json!({"viewport": viewport}),
+ (120, 100),
+ Some(20),
+ (200, 100),
+ Some(20),
+ 1,
+ ),
+ (
+ json!({"deviceScaleFactor": 2, "viewport": viewport}),
+ (240, 200),
+ Some(40),
+ (400, 200),
+ Some(200),
+ 2,
+ ),
+ (
+ json!({"scale": 0.5, "viewport": viewport}),
+ (120, 100),
+ Some(0),
+ (200, 100),
+ Some(20),
+ 1,
+ ),
+ (
+ json!({"dontSetVisibleSize": true, "viewport": viewport}),
+ (200, 100),
+ Some(20),
+ (200, 100),
+ Some(20),
+ 1,
+ ),
+ (
+ json!({"viewport": {"x": 30, "y": 0, "width": 0, "height": 0, "scale": 0}}),
+ (200, 100),
+ Some(0),
+ (200, 100),
+ Some(0),
+ 1,
+ ),
+ (
+ json!({"viewport": {"x": 30, "y": 0, "width": 60, "height": 50, "scale": -1}}),
+ (200, 100),
+ Some(0),
+ (200, 100),
+ Some(0),
+ 1,
+ ),
+ (
+ json!({"viewport": {"x": -1, "y": 0, "width": 60, "height": 50, "scale": 2}}),
+ (120, 100),
+ Some(40),
+ (200, 100),
+ Some(40),
+ 1,
+ ),
+ ];
+ for (params, widget_size, widget_boundary, surface_size, surface_boundary, dpr) in cases {
+ eprintln!("capture presentation: {params}");
+ set_metrics(&mut ctx, json!({})).await;
+ set_metrics(&mut ctx, params.clone()).await;
+ let registration = start_screencast(&mut ctx, json!({}));
+ let completion = poll_screencast(&mut ctx, &registration, None).await;
+ let (frame, _) = take_frame(&mut ctx, &registration, completion);
+ let image = png(&frame["data"]);
+ assert_blue_boundary(&image, widget_size, widget_boundary);
+ assert_eq!(
+ frame["metadata"]["deviceWidth"].as_f64(),
+ Some(f64::from(widget_size.0))
+ );
+ assert_eq!(
+ frame["metadata"]["deviceHeight"].as_f64(),
+ Some(f64::from(widget_size.1))
+ );
+ if params == json!({"scale": 0.5}) {
+ assert_eq!(
+ decode_png_pixel(&image, 150, 80),
+ [0, 0, 255, 255],
+ "the propagated solid canvas color fills the compositor surface"
+ );
+ }
+ command(&mut ctx, "Page.stopScreencast", json!({})).await;
+
+ let screenshot = command(&mut ctx, "Page.captureScreenshot", json!({})).await;
+ assert_blue_boundary(&png(&screenshot["data"]), surface_size, surface_boundary);
+ let clipped = command(
+ &mut ctx,
+ "Page.captureScreenshot",
+ json!({
+ "clip": {"x": 10, "y": 0, "width": 60, "height": 30, "scale": 1}
+ }),
+ )
+ .await;
+ assert_blue_boundary(&png(&clipped["data"]), (60 * dpr, 30 * dpr), Some(30 * dpr));
+ let layout = command(
+ &mut ctx,
+ "Runtime.evaluate",
+ json!({
+ "expression": "[innerWidth, innerHeight, devicePixelRatio, visualViewport.scale]",
+ "returnByValue": true
+ }),
+ )
+ .await;
+ assert_eq!(
+ layout["result"]["value"],
+ json!([200, 100, dpr, 1]),
+ "{params}"
+ );
+ }
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn emulated_view_capture_invalidates_frames_survives_navigation_and_clears() {
+ let mut ctx = TestContext::new();
+ install_page(&mut ctx).await;
+ let registration = start_screencast(&mut ctx, json!({"maxWidth": 100, "maxHeight": 50}));
+ let completion = poll_screencast(&mut ctx, &registration, None).await;
+ let (_, original) = take_frame(&mut ctx, &registration, completion);
+
+ set_metrics(&mut ctx, json!({"scale": 0.5})).await;
+ let completion = poll_screencast(&mut ctx, &registration, Some(original.clone())).await;
+ let (frame, scaled) = take_frame(&mut ctx, &registration, completion);
+ assert_ne!(original, scaled);
+ assert_blue_boundary(&png(&frame["data"]), (100, 50), Some(10));
+ assert_eq!(frame["metadata"]["deviceWidth"].as_f64(), Some(200.0));
+ assert_eq!(frame["metadata"]["deviceHeight"].as_f64(), Some(100.0));
+ assert!(matches!(
+ poll_screencast(&mut ctx, &registration, Some(scaled)).await,
+ PageScreencastCaptureCompletion::Unchanged
+ ));
+
+ ctx.install_navigation_fixture_for_session_owner(&screenshot_data_url(HTML), Some(SESSION))
+ .await;
+ let completion = poll_screencast(&mut ctx, &registration, None).await;
+ let (frame, _) = take_frame(&mut ctx, &registration, completion);
+ assert_blue_boundary(&png(&frame["data"]), (100, 50), Some(10));
+
+ command(&mut ctx, "Emulation.clearDeviceMetricsOverride", json!({})).await;
+ let layout = command(
+ &mut ctx,
+ "Runtime.evaluate",
+ json!({
+ "expression": "[innerWidth, innerHeight]", "returnByValue": true
+ }),
+ )
+ .await;
+ let completion = poll_screencast(&mut ctx, &registration, None).await;
+ let (frame, _) = take_frame(&mut ctx, &registration, completion);
+ assert_eq!(
+ frame["metadata"]["deviceWidth"].as_f64(),
+ layout["result"]["value"][0].as_f64()
+ );
+ assert_eq!(
+ frame["metadata"]["deviceHeight"].as_f64(),
+ layout["result"]["value"][1].as_f64()
+ );
+ let image = moli_image::decode_png(&png(&frame["data"])).unwrap();
+ assert!(image.width <= 100 && image.height <= 50);
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn emulated_view_capture_resolves_zero_dimensions_and_scroll_at_layout() {
+ let mut ctx = TestContext::new();
+ install_page(&mut ctx).await;
+ set_metrics(
+ &mut ctx,
+ json!({
+ "width": 0, "height": 0, "scale": 2,
+ "viewport": {"x": 30, "y": 0, "width": 60, "height": 50, "scale": 2}
+ }),
+ )
+ .await;
+ let layout = command(
+ &mut ctx,
+ "Runtime.evaluate",
+ json!({
+ "expression": "[innerWidth, innerHeight]", "returnByValue": true
+ }),
+ )
+ .await;
+ assert_eq!(layout["result"]["value"], json!([60, 50]));
+ let screenshot = command(&mut ctx, "Page.captureScreenshot", json!({})).await;
+ assert_blue_boundary(&png(&screenshot["data"]), (60, 50), Some(20));
+
+ ctx.install_navigation_fixture_for_session_owner(
+ &screenshot_data_url(&format!("{HTML}<style>body {{ height: 500px }}</style>")),
+ Some(SESSION),
+ )
+ .await;
+ set_metrics(
+ &mut ctx,
+ json!({
+ "viewport": {"x": 0, "y": 30, "width": 60, "height": 50, "scale": 1}
+ }),
+ )
+ .await;
+ command(&mut ctx, "Page.captureScreenshot", json!({})).await;
+ let scroll = command(
+ &mut ctx,
+ "Runtime.evaluate",
+ json!({
+ "expression": "scrollTo(0, 50); new Promise(requestAnimationFrame).then(() => scrollY)",
+ "returnByValue": true, "awaitPromise": true
+ }),
+ )
+ .await;
+ assert_eq!(scroll["result"]["value"], 50);
+ command(
+ &mut ctx,
+ "Emulation.setDefaultBackgroundColorOverride",
+ json!({
+ "color": {"r": 0, "g": 0, "b": 0, "a": 0}
+ }),
+ )
+ .await;
+ let registration = start_screencast(&mut ctx, json!({}));
+ let completion = poll_screencast(&mut ctx, &registration, None).await;
+ let (frame, _) = take_frame(&mut ctx, &registration, completion);
+ let image = png(&frame["data"]);
+ assert_png_dimensions(&image, 60, 50);
+ assert_eq!(decode_png_pixel(&image, 10, 0), [0, 0, 255, 255]);
+ assert_eq!(decode_png_pixel(&image, 10, 25), [255, 0, 0, 255]);
+ assert_eq!(decode_png_pixel(&image, 50, 25), [0, 0, 255, 255]);
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn emulated_view_capture_clips_background_images_over_a_transparent_base() {
+ let mut ctx = TestContext::new();
+ install_page(&mut ctx).await;
+ let gradient =
+ screenshot_data_url("<style>html{background:linear-gradient(blue,blue)}</style>");
+ ctx.install_navigation_fixture_for_session_owner(&gradient, Some(SESSION))
+ .await;
+ command(
+ &mut ctx,
+ "Emulation.setDefaultBackgroundColorOverride",
+ json!({
+ "color": {"r": 0, "g": 0, "b": 0, "a": 0}
+ }),
+ )
+ .await;
+ set_metrics(&mut ctx, json!({"scale": 0.5})).await;
+ let registration = start_screencast(&mut ctx, json!({}));
+ let completion = poll_screencast(&mut ctx, &registration, None).await;
+ let (frame, _) = take_frame(&mut ctx, &registration, completion);
+ let image = png(&frame["data"]);
+ assert_png_dimensions(&image, 200, 100);
+ assert_eq!(decode_png_pixel(&image, 10, 10), [0, 0, 255, 255]);
+ assert_eq!(decode_png_pixel(&image, 150, 80), [0; 4]);
+
+ set_metrics(
+ &mut ctx,
+ json!({
+ "viewport": {"x": 0, "y": 0, "width": 0, "height": 0, "scale": 0}
+ }),
+ )
+ .await;
+ let completion = poll_screencast(&mut ctx, &registration, None).await;
+ let (frame, _) = take_frame(&mut ctx, &registration, completion);
+ let image = moli_image::decode_png(&png(&frame["data"])).unwrap();
+ assert!(image.rgba.iter().all(|byte| *byte == 0));
+}
diff --git a/moli-renderer-v8/src/runtime/page_screenshot.rs b/moli-renderer-v8/src/runtime/page_screenshot.rs
index 9b72f2c5f..0c2cb656c 100644
--- a/moli-renderer-v8/src/runtime/page_screenshot.rs
+++ b/moli-renderer-v8/src/runtime/page_screenshot.rs
@@ -1,6 +1,8 @@
-use moli_browser_profile::DEFAULT_WINDOW_SURFACE_PROFILE;
-use moli_layout::{LayoutRect, PaintCaptureRequest, PaintViewport};
-use moli_page_types::LayoutPolicy;
+use moli_layout::{
+ LayoutRect, LayoutTransform2D, PaintCaptureRegion, PaintCaptureRequest, PaintCaptureSurface,
+ PaintViewport,
+};
+use moli_page_types::{LayoutPolicy, ViewportSurface};
use std::sync::Arc;
use std::time::Instant;
@@ -197,7 +199,8 @@ impl PageVm {
RendererScreenshotPurpose::Print { .. } => moli_action_window::ActionBarrier::Explicit,
};
self.flush_page_action_window(barrier)?;
- let paint_capture = request.paint_capture_request()?;
+ let paint_capture =
+ request.paint_capture_request(self.viewport_surface.unwrap_or_default())?;
let restore_media = if matches!(request.purpose, RendererScreenshotPurpose::Print { .. })
&& self.emulated_media.media.is_none()
{
@@ -249,9 +252,7 @@ impl PageVm {
if self.layout_policy == LayoutPolicy::Mock {
return Ok(RendererCaptureScreencastFrameReply::LayoutDisabled);
}
- let surface = self
- .viewport_surface
- .unwrap_or_else(default_viewport_surface);
+ let surface = self.viewport_surface.unwrap_or_default();
let viewport = PaintViewport::new(
surface.inner_width,
surface.inner_height,
@@ -267,7 +268,7 @@ impl PageVm {
}
before_layout();
let paint_capture = PaintCaptureRequest {
- region: moli_layout::PaintCaptureRegion::Viewport,
+ region: paint_viewport_region(surface, ViewportCapture::Widget)?,
include_backgrounds: true,
include_viewport_controls: true,
base_background_color: paint_background_color(request.base_background_color),
@@ -298,7 +299,7 @@ impl PageVm {
visual_state_for_captured_screencast_frame(visual_state_before, visual_state_after);
Ok(RendererCaptureScreencastFrameReply::Captured(
RendererCapturedScreencastFrame {
- viewport_size: (surface.inner_width, surface.inner_height),
+ viewport_size: surface.visible_size(),
image,
visual_state,
},
@@ -318,9 +319,7 @@ impl PageVm {
if self.layout_policy == LayoutPolicy::Mock {
return Ok(RendererImageCaptureOutcome::LayoutDisabled);
}
- let surface = self
- .viewport_surface
- .unwrap_or_else(default_viewport_surface);
+ let surface = self.viewport_surface.unwrap_or_default();
let viewport = PaintViewport::new(
surface.inner_width,
surface.inner_height,
@@ -449,13 +448,18 @@ fn visual_state_for_captured_screencast_frame(
}
impl RendererCaptureScreenshotRequest {
- fn paint_capture_request(&self) -> anyhow::Result<PaintCaptureRequest> {
+ fn paint_capture_request(
+ &self,
+ surface: ViewportSurface,
+ ) -> anyhow::Result<PaintCaptureRequest> {
let include_viewport_controls = matches!(
self.region,
RendererScreenshotRegion::Viewport | RendererScreenshotRegion::ViewportClip(_)
);
let region = match self.region {
- RendererScreenshotRegion::Viewport => moli_layout::PaintCaptureRegion::Viewport,
+ RendererScreenshotRegion::Viewport => {
+ paint_viewport_region(surface, ViewportCapture::Surface)?
+ }
RendererScreenshotRegion::FullDocument => moli_layout::PaintCaptureRegion::FullDocument,
RendererScreenshotRegion::ViewportClip(clip)
| RendererScreenshotRegion::PageClip(clip) => {
@@ -496,23 +500,55 @@ fn finite_f32(label: &str, value: f64) -> anyhow::Result<f32> {
Ok(value as f32)
}
-fn default_viewport_surface() -> crate::protocol_types::ViewportSurface {
- fn dimension(value: f64) -> u32 {
- debug_assert!(value.is_finite() && value >= 0.0 && value <= f64::from(u32::MAX));
- value as u32
- }
-
- crate::protocol_types::ViewportSurface {
- inner_width: dimension(DEFAULT_WINDOW_SURFACE_PROFILE.inner_width),
- inner_height: dimension(DEFAULT_WINDOW_SURFACE_PROFILE.inner_height),
- outer_width: dimension(DEFAULT_WINDOW_SURFACE_PROFILE.inner_width),
- outer_height: dimension(DEFAULT_WINDOW_SURFACE_PROFILE.inner_height),
- device_pixel_ratio: DEFAULT_WINDOW_SURFACE_PROFILE.device_pixel_ratio,
- screen_width: dimension(DEFAULT_WINDOW_SURFACE_PROFILE.screen_width),
- screen_height: dimension(DEFAULT_WINDOW_SURFACE_PROFILE.screen_height),
- screen_avail_width: dimension(DEFAULT_WINDOW_SURFACE_PROFILE.screen_avail_width),
- screen_avail_height: dimension(DEFAULT_WINDOW_SURFACE_PROFILE.screen_avail_height),
+enum ViewportCapture {
+ /// Screenshots restore the emulated DPR, regardless of the preview scale.
+ Surface,
+ /// Screencasts capture the actual widget, including the preview scale.
+ Widget,
+}
- ..Default::default()
- }
+fn paint_viewport_region(
+ surface: ViewportSurface,
+ capture: ViewportCapture,
+) -> anyhow::Result<PaintCaptureRegion> {
+ let Some(view) = surface.emulated_view else {
+ return Ok(PaintCaptureRegion::Viewport);
+ };
+ let (scale, width, height) = match capture {
+ ViewportCapture::Surface => {
+ let ratio = surface.device_pixel_ratio / view.native_device_pixel_ratio;
+ (
+ ratio,
+ f64::from(surface.inner_width) * ratio,
+ f64::from(surface.inner_height) * ratio,
+ )
+ }
+ ViewportCapture::Widget => (view.scale, f64::from(view.width), f64::from(view.height)),
+ };
+ let (transform, scroll_scale) = if let Some(viewport) = view.viewport {
+ // Blink applies the preview scale first, then the page-coordinate
+ // viewport offset and scale. Resolve scroll compensation during layout.
+ (
+ LayoutTransform2D::new([
+ viewport.scale * scale,
+ 0.0,
+ 0.0,
+ viewport.scale * scale,
+ -viewport.x * viewport.scale,
+ -viewport.y * viewport.scale,
+ ]),
+ viewport.scale,
+ )
+ } else {
+ (LayoutTransform2D::scale(scale, scale), 0.0)
+ };
+ Ok(PaintCaptureRegion::TransformedViewport {
+ surface: PaintCaptureSurface::new(
+ finite_f32("capture width", width)?,
+ finite_f32("capture height", height)?,
+ finite_f32("native device-pixel ratio", view.native_device_pixel_ratio)?,
+ ),
+ transform,
+ scroll_scale,
+ })
}