The headless browser for AI agents and web scraping
render: reuse the retained resource cache on the capture fallback
Alex Skinner8a3c8402f809
diff --git a/crates/obscura-browser/src/page.rs b/crates/obscura-browser/src/page.rsindex e1aa4ae6..eeecddb8 100644--- a/crates/obscura-browser/src/page.rs+++ b/crates/obscura-browser/src/page.rs@@ -3400,6 +3400,22 @@ impl Page {.as_ref().map(|js| js.scroll_offset()).unwrap_or((0.0, 0.0));+ // When there IS a runtime, paint against the resource cache it already+ // holds for this document. Building a fresh one here refetched every+ // image on every capture, so a caller repeating a screenshot at a+ // viewport that does not match the prepared key paid the whole network+ // cost per frame.+ if let Some(js) = &self.js {+ if let Some(png) = js.screenshot_unprepared_with_retained_resources(+ viewport,+ base_url,+ scroll,+ animation_sample.time,+ self.capture_surface_color(),+ ) {+ return Some(png);+ }+ }self.with_dom(|dom| {obscura_js::screenshot_png_scrolled_at_animation_time_with_surface_color(dom,diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rsindex f36f742e..5d06b15c 100644--- a/crates/obscura-js/src/runtime.rs+++ b/crates/obscura-js/src/runtime.rs@@ -1117,6 +1117,41 @@ impl ObscuraJsRuntime {)}+ /// Paint an unprepared view of the current document against the runtime's+ /// retained resource cache.+ ///+ /// `Page` falls back to a raw-DOM paint when a capture's viewport does not+ /// match the prepared render key. That fallback used a fresh+ /// `RenderResourceCache`, so it refetched every image on every call and a+ /// repeated capture paid the network cost per frame. Reusing the cache the+ /// runtime already holds for this document keeps the fallback correct while+ /// fetching each resource once.+ #[cfg(feature = "render")]+ pub fn screenshot_unprepared_with_retained_resources(+ &self,+ viewport: (f32, f32),+ base_url: Option<&str>,+ scroll: (f32, f32),+ animation_sample_time: obscura_render::AnimationSampleTime,+ surface_color: [u8; 4],+ ) -> Option<Vec<u8>> {+ let mut state = self.state.borrow_mut();+ let ObscuraState {+ dom,+ render_resources,+ ..+ } = &mut *state;+ obscura_render::screenshot_png_scrolled_at_animation_time_with_surface_color_and_resources(+ dom.as_ref()?,+ viewport,+ base_url,+ scroll,+ animation_sample_time,+ surface_color,+ render_resources,+ )+ }+#[cfg(feature = "render")]pub fn screenshot_prepared_with_surface_color(&self,diff --git a/crates/obscura-render/src/lib.rs b/crates/obscura-render/src/lib.rsindex 6c169ef6..e8416020 100644--- a/crates/obscura-render/src/lib.rs+++ b/crates/obscura-render/src/lib.rs@@ -99,6 +99,7 @@ mod paint;pub use paint::{image_intrinsic_dimensions, paint_dom, paint_dom_scrolled,paint_dom_scrolled_at_animation_time,+ paint_dom_scrolled_at_animation_time_with_surface_color_and_resources,paint_dom_scrolled_at_animation_time_with_surface_color, paint_prepared,paint_prepared_region_with_scroll, paint_prepared_region_with_scroll_and_surface_color,paint_prepared_region_with_scroll_and_surface_color_and_canvas_surfaces,@@ -111,6 +112,7 @@ pub use paint::{prepare_dom_with_dynamic_fonts_and_stylesheet_cache_for_media_with_animation_state,prepare_dom_with_dynamic_fonts_and_stylesheet_cache_with_animation_state,screenshot_png, screenshot_png_scrolled, screenshot_png_scrolled_at_animation_time,+ screenshot_png_scrolled_at_animation_time_with_surface_color_and_resources,screenshot_png_scrolled_at_animation_time_with_surface_color,prepare_dom_with_retained_attribute_styles, prepare_dom_with_retained_styles,prepare_dom_with_retained_styles_at_animation_time,diff --git a/crates/obscura-render/src/paint.rs b/crates/obscura-render/src/paint.rsindex a87e242a..5b2b41e1 100644--- a/crates/obscura-render/src/paint.rs+++ b/crates/obscura-render/src/paint.rs@@ -2104,14 +2104,41 @@ pub fn paint_dom_scrolled_at_animation_time_with_surface_color(surface_color: [u8; 4],) -> Option<Pixmap> {let mut resources = RenderResourceCache::default();- let mut prepared = prepare_dom_at_animation_time(+ paint_dom_scrolled_at_animation_time_with_surface_color_and_resources(tree,viewport,base_url,+ scroll,+ animation_sample_time,+ surface_color,&mut resources,+ )+}++/// As [`paint_dom_scrolled_at_animation_time_with_surface_color`], but painting+/// against a caller-owned resource cache instead of a fresh one.+///+/// The default-cache version starts empty, so every image is fetched again on+/// every call. A caller that already holds a warm cache for this document — a+/// page repeating a capture, for instance — can pass it here and pay the+/// network cost once rather than per frame.+pub fn paint_dom_scrolled_at_animation_time_with_surface_color_and_resources(+ tree: &DomTree,+ viewport: (f32, f32),+ base_url: Option<&str>,+ scroll: (f32, f32),+ animation_sample_time: crate::AnimationSampleTime,+ surface_color: [u8; 4],+ resources: &mut RenderResourceCache,+) -> Option<Pixmap> {+ let mut prepared = prepare_dom_at_animation_time(+ tree,+ viewport,+ base_url,+ resources,animation_sample_time,)?;- paint_prepared_with_surface_color(tree, &mut prepared, &mut resources, scroll, surface_color)+ paint_prepared_with_surface_color(tree, &mut prepared, resources, scroll, surface_color)}/// Resolve image candidates and web fonts, then create the single final layout@@ -6269,6 +6296,28 @@ pub fn screenshot_png_scrolled_at_animation_time_with_surface_color(.and_then(|pixmap| pixmap.encode_png().ok())}+/// PNG wrapper for [`paint_dom_scrolled_at_animation_time_with_surface_color_and_resources`].+pub fn screenshot_png_scrolled_at_animation_time_with_surface_color_and_resources(+ tree: &DomTree,+ viewport: (f32, f32),+ base_url: Option<&str>,+ scroll: (f32, f32),+ animation_sample_time: crate::AnimationSampleTime,+ surface_color: [u8; 4],+ resources: &mut RenderResourceCache,+) -> Option<Vec<u8>> {+ paint_dom_scrolled_at_animation_time_with_surface_color_and_resources(+ tree,+ viewport,+ base_url,+ scroll,+ animation_sample_time,+ surface_color,+ resources,+ )+ .and_then(|pixmap| pixmap.encode_png().ok())+}+/// PNG convenience wrapper for a retained resource-aware layout.pub fn screenshot_prepared(tree: &DomTree,