Skip to content

armgit / moli

Public Imported from GitHub
0

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

feat(screenshot): relax full-document capture limits

ldm02b51bdd12a6b
diff --git a/moli-image/src/budget.rs b/moli-image/src/budget.rs
index 6a3ac6d9f..6d68f1cb8 100644
--- a/moli-image/src/budget.rs
+++ b/moli-image/src/budget.rs
@@ -3,6 +3,6 @@ pub const MAX_ENCODED_IMAGE_BYTES: usize = 128 * 1024 * 1024;
/// Maximum materialized straight-RGBA8 buffer owned by one codec operation.
///
-/// Paint applies a tighter screenshot pixel budget before reaching this
-/// boundary. This independent limit also protects future network image decode.
+/// Paint independently bounds transient raster allocations. This codec limit
+/// also protects network image decode.
pub const MAX_DECODED_RGBA_BYTES: usize = 128 * 1024 * 1024;
diff --git a/moli-layout/src/capture.rs b/moli-layout/src/capture.rs
index b7ca9e636..0d97ab79e 100644
--- a/moli-layout/src/capture.rs
+++ b/moli-layout/src/capture.rs
@@ -1,5 +1,9 @@
use crate::{LayoutError, LayoutPoint, LayoutRect, LayoutSize, LayoutTransform2D, LayoutViewport};
+/// Exclusive CSS-pixel limit for either side of an automatic full-document
+/// capture, matching Chromium's `Page.captureScreenshot` boundary.
+pub const FULL_DOCUMENT_CAPTURE_CSS_DIMENSION_LIMIT: f32 = (128 * 1024) as f32;
+
/// Which CSS-pixel region one paint-enabled layout demand should capture.
///
/// Page clips use document coordinates, matching CDP `Page.Viewport`. They do
@@ -127,7 +131,7 @@ impl PaintCaptureRequest {
1.0,
),
PaintCaptureRegion::FullDocument => {
- validate_extent("document content", content_size.width, content_size.height)?;
+ validate_full_document_extent(content_size.width, content_size.height)?;
(
LayoutRect::new(
-viewport_scroll.x,
@@ -211,6 +215,19 @@ fn validate_clip(rect: LayoutRect, scale: f32) -> Result<(), LayoutError> {
Ok(())
}
+fn validate_full_document_extent(width: f32, height: f32) -> Result<(), LayoutError> {
+ validate_extent("document content", width, height)?;
+ if width >= FULL_DOCUMENT_CAPTURE_CSS_DIMENSION_LIMIT
+ || height >= FULL_DOCUMENT_CAPTURE_CSS_DIMENSION_LIMIT
+ {
+ return Err(invalid_capture(format!(
+ "document content width and height must each be less than {} CSS pixels, got {width}x{height}",
+ FULL_DOCUMENT_CAPTURE_CSS_DIMENSION_LIMIT
+ )));
+ }
+ Ok(())
+}
+
fn validate_extent(label: &str, width: f32, height: f32) -> Result<(), LayoutError> {
if !width.is_finite() || !height.is_finite() || width <= 0.0 || height <= 0.0 {
return Err(invalid_capture(format!(
@@ -292,4 +309,54 @@ mod tests {
.expect_err("zero-width clip");
assert!(matches!(error, LayoutError::InvalidPaintCapture { .. }));
}
+
+ #[test]
+ fn full_document_capture_uses_an_exclusive_128k_css_dimension_limit() {
+ let below_limit = FULL_DOCUMENT_CAPTURE_CSS_DIMENSION_LIMIT - 1.0;
+ PaintCaptureRequest::full_document()
+ .resolve(
+ LayoutViewport::new(800, 600, 1.0),
+ LayoutPoint::ZERO,
+ LayoutSize::new(below_limit, below_limit),
+ )
+ .expect("dimensions immediately below 128K CSS pixels");
+
+ for content_size in [
+ LayoutSize::new(FULL_DOCUMENT_CAPTURE_CSS_DIMENSION_LIMIT, 1.0),
+ LayoutSize::new(1.0, FULL_DOCUMENT_CAPTURE_CSS_DIMENSION_LIMIT),
+ ] {
+ let error = PaintCaptureRequest::full_document()
+ .resolve(
+ LayoutViewport::new(800, 600, 1.0),
+ LayoutPoint::ZERO,
+ content_size,
+ )
+ .expect_err("128K CSS pixels is outside the exclusive limit");
+ assert!(
+ error.to_string().contains(
+ "document content width and height must each be less than 131072 CSS pixels"
+ ),
+ "{error}"
+ );
+ }
+ }
+
+ #[test]
+ fn explicit_page_clip_bypasses_the_full_document_dimension_limit() {
+ let clip = PaintCaptureRequest::page_clip(
+ LayoutRect::new(0.0, 0.0, FULL_DOCUMENT_CAPTURE_CSS_DIMENSION_LIMIT, 1.0),
+ 1.0,
+ )
+ .resolve(
+ LayoutViewport::new(800, 600, 1.0),
+ LayoutPoint::ZERO,
+ LayoutSize::new(800.0, 600.0),
+ )
+ .expect("explicit clips are validated by the raster backend");
+
+ assert_eq!(
+ clip.surface.css_width,
+ FULL_DOCUMENT_CAPTURE_CSS_DIMENSION_LIMIT
+ );
+ }
}
diff --git a/moli-layout/src/lib.rs b/moli-layout/src/lib.rs
index eec93627f..09a01f782 100644
--- a/moli-layout/src/lib.rs
+++ b/moli-layout/src/lib.rs
@@ -38,7 +38,10 @@ mod text;
mod world;
pub use builder::build_layout_world;
-pub use capture::{PaintCaptureRegion, PaintCaptureRequest, PaintCaptureSurface};
+pub use capture::{
+ FULL_DOCUMENT_CAPTURE_CSS_DIMENSION_LIMIT, PaintCaptureRegion, PaintCaptureRequest,
+ PaintCaptureSurface,
+};
pub use error::LayoutError;
pub use layout_tree::{
FrozenCoordinateSpace, FrozenLayoutBox, FrozenLayoutTree, GeometryProvider, LayoutAnswers,
diff --git a/moli-paint/src/lib.rs b/moli-paint/src/lib.rs
index 0beff974f..458719228 100644
--- a/moli-paint/src/lib.rs
+++ b/moli-paint/src/lib.rs
@@ -10,6 +10,6 @@ mod raster;
pub use moli_image::RgbaImage as RasterImage;
pub use raster::{
- MAX_PAINT_GLYPHS, MAX_PAINT_LAYER_DEPTH, MAX_PAINT_OPERATIONS, MAX_RASTER_PIXELS,
- MAX_TRANSIENT_RASTER_BYTES, PaintError, raster_snapshot,
+ MAX_PAINT_GLYPHS, MAX_PAINT_LAYER_DEPTH, MAX_PAINT_OPERATIONS, MAX_TRANSIENT_RASTER_BYTES,
+ PaintError, raster_snapshot,
};
diff --git a/moli-paint/src/raster/mod.rs b/moli-paint/src/raster/mod.rs
index 00cd115ae..b129719d7 100644
--- a/moli-paint/src/raster/mod.rs
+++ b/moli-paint/src/raster/mod.rs
@@ -34,8 +34,6 @@ use peniko::{
},
};
-/// Maximum number of pixels admitted to one screenshot/screencast raster.
-pub const MAX_RASTER_PIXELS: u64 = 16 * 1024 * 1024;
/// Maximum estimated RGBA bytes simultaneously owned by the output and nested
/// filter surfaces in one raster call.
pub const MAX_TRANSIENT_RASTER_BYTES: usize = 256 * 1024 * 1024;
@@ -80,13 +78,6 @@ pub enum PaintError {
/// Height in device pixels.
height: u32,
},
- /// The requested surface exceeds the bounded product pixel budget.
- PixelBudgetExceeded {
- width: u32,
- height: u32,
- pixels: u64,
- max_pixels: u64,
- },
/// Nested filters would exceed the transient RGBA budget.
TransientRasterBudgetExceeded {
required_bytes: usize,
@@ -160,15 +151,6 @@ impl fmt::Display for PaintError {
formatter,
"RGBA byte length overflows the host address space for {width}x{height}"
),
- Self::PixelBudgetExceeded {
- width,
- height,
- pixels,
- max_pixels,
- } => write!(
- formatter,
- "raster surface {width}x{height} contains {pixels} pixels, exceeding the {max_pixels}-pixel capture budget"
- ),
Self::TransientRasterBudgetExceeded {
required_bytes,
max_bytes,
@@ -419,15 +401,6 @@ fn validate_resource_budget(
dimensions: RasterDimensions,
stream: PaintStreamMetrics,
) -> Result<(), PaintError> {
- let pixels = u64::from(dimensions.width) * u64::from(dimensions.height);
- if pixels > MAX_RASTER_PIXELS {
- return Err(PaintError::PixelBudgetExceeded {
- width: dimensions.width,
- height: dimensions.height,
- pixels,
- max_pixels: MAX_RASTER_PIXELS,
- });
- }
// Backend filters may crop allocations to content bounds, whereas the
// software color-filter fallback uses a complete surface. Charging every
// nested filter as a complete surface is conservative and backend-neutral.
@@ -2200,15 +2173,17 @@ mod tests {
}
#[test]
- fn rejects_surface_over_pixel_budget_before_backend_creation() {
- assert!(matches!(
- raster_snapshot(&snapshot(4_097, 4_096, 1.0)),
- Err(PaintError::PixelBudgetExceeded {
- width: 4_097,
- height: 4_096,
- ..
- })
- ));
+ fn resource_budget_is_byte_based_instead_of_using_a_pixel_count_limit() {
+ let dimensions = validate_dimensions(PaintCaptureSurface::new(4_097.0, 4_096.0, 1.0))
+ .expect("dimensions supported by the backend");
+ assert!(
+ u64::from(dimensions.width) * u64::from(dimensions.height) > 16 * 1024 * 1024,
+ "test surface must exceed the removed 16M-pixel limit"
+ );
+ assert_eq!(
+ validate_resource_budget(dimensions, PaintStreamMetrics::default()),
+ Ok(())
+ );
}
#[test]
diff --git a/moli-renderer-v8/src/runtime/tests.rs b/moli-renderer-v8/src/runtime/tests.rs
index e2a08045e..6c966adef 100644
--- a/moli-renderer-v8/src/runtime/tests.rs
+++ b/moli-renderer-v8/src/runtime/tests.rs
@@ -872,23 +872,23 @@ async fn capture_screenshot_clip_and_full_document_keep_the_live_layout_viewport
}
#[tokio::test(flavor = "multi_thread")]
-async fn capture_screenshot_rejects_surface_over_the_pixel_budget_before_raster() {
+async fn capture_screenshot_rejects_full_document_at_the_128k_css_boundary() {
let runtime = initialize_layout_test_runtime();
let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default())
.expect("default resource request client");
let url = url::Url::parse("https://example.test/capture-budget").unwrap();
- let page = create_test_html_page(&runtime, &loader, url, "<!doctype html>").await;
+ let page = create_test_html_page(
+ &runtime,
+ &loader,
+ url,
+ "<!doctype html><style>html,body{margin:0}.page{height:131072px}</style><div class=page></div>",
+ )
+ .await;
let request = super::RendererCaptureScreenshotRequest {
purpose: super::RendererScreenshotPurpose::Screenshot,
format: super::RendererScreenshotFormat::Png,
quality: 100,
- region: super::RendererScreenshotRegion::PageClip(super::RendererScreenshotClip {
- x: 0.0,
- y: 0.0,
- width: 5000.0,
- height: 5000.0,
- scale: 1.0,
- }),
+ region: super::RendererScreenshotRegion::FullDocument,
optimize_for_speed: false,
max_width: None,
max_height: None,
@@ -898,11 +898,13 @@ async fn capture_screenshot_rejects_surface_over_the_pixel_budget_before_raster(
.run_async_command(RendererPageCommand::CaptureScreenshot(request))
.await
{
- Ok(_) => panic!("25M-pixel screenshot must exceed the 16M-pixel budget"),
+ Ok(_) => panic!("full-document screenshot at 128K CSS pixels must be rejected"),
Err(error) => error,
};
assert!(
- error.to_string().contains("pixel capture budget"),
+ error
+ .to_string()
+ .contains("must each be less than 131072 CSS pixels"),
"{error:#}"
);
}