diff --git a/packages/native-renderer/src/lib.rs b/packages/native-renderer/src/lib.rs index 0951158e4c..649884ae21 100644 --- a/packages/native-renderer/src/lib.rs +++ b/packages/native-renderer/src/lib.rs @@ -1,2 +1,3 @@ pub mod paint; +pub mod pipeline; pub mod scene; diff --git a/packages/native-renderer/src/pipeline.rs b/packages/native-renderer/src/pipeline.rs new file mode 100644 index 0000000000..e0d757f235 --- /dev/null +++ b/packages/native-renderer/src/pipeline.rs @@ -0,0 +1,115 @@ +use std::io::Write; +use std::process::{Command, Stdio}; +use std::time::Instant; + +use skia_safe::Color4f; + +use crate::paint::{paint_element, RenderSurface}; +use crate::scene::Scene; + +/// Configuration for a static render pass. +pub struct RenderConfig { + pub fps: u32, + pub duration_secs: f64, + pub quality: u32, + pub output_path: String, +} + +/// Timing and metadata returned after a successful render. +pub struct RenderResult { + pub total_frames: u32, + pub total_ms: u64, + pub avg_paint_ms: f64, + pub output_path: String, +} + +/// Render a static scene (no animation) to a video file via FFmpeg pipe. +/// +/// The scene is painted once and the resulting JPEG frame is written +/// `total_frames` times to FFmpeg's stdin, producing a still-image video. +pub fn render_static(scene: &Scene, config: &RenderConfig) -> Result { + let total_frames = (config.fps as f64 * config.duration_secs).ceil() as u32; + if total_frames == 0 { + return Err("total_frames is zero — check fps and duration_secs".into()); + } + + // Paint once. + let paint_start = Instant::now(); + + let mut surface = RenderSurface::new_raster(scene.width as i32, scene.height as i32)?; + surface.clear(Color4f::new(0.0, 0.0, 0.0, 1.0)); + + for element in &scene.elements { + paint_element(surface.canvas(), element); + } + + let frame_jpeg = surface + .encode_jpeg(config.quality) + .ok_or("failed to encode frame as JPEG")?; + + let paint_ms = paint_start.elapsed().as_secs_f64() * 1000.0; + + // Spawn FFmpeg. + let mut child = Command::new("ffmpeg") + .args([ + "-y", + "-f", + "image2pipe", + "-vcodec", + "mjpeg", + "-framerate", + &config.fps.to_string(), + "-i", + "-", + "-c:v", + "libx264", + "-preset", + "fast", + "-crf", + "18", + "-pix_fmt", + "yuv420p", + "-threads", + "0", + &config.output_path, + ]) + .stdin(Stdio::piped()) + .stdout(Stdio::null()) + .stderr(Stdio::piped()) + .spawn() + .map_err(|e| format!("failed to spawn ffmpeg: {e}"))?; + + // Write frame data. + let write_start = Instant::now(); + { + let stdin = child + .stdin + .as_mut() + .ok_or("failed to open ffmpeg stdin")?; + + for _ in 0..total_frames { + stdin + .write_all(&frame_jpeg) + .map_err(|e| format!("failed to write frame to ffmpeg: {e}"))?; + } + } + // stdin is dropped here, signalling EOF to FFmpeg. + + let output = child + .wait_with_output() + .map_err(|e| format!("failed to wait for ffmpeg: {e}"))?; + + if !output.status.success() { + let stderr = String::from_utf8_lossy(&output.stderr); + return Err(format!("ffmpeg exited with {}: {stderr}", output.status)); + } + + let total_ms = write_start.elapsed().as_millis() as u64; + + Ok(RenderResult { + total_frames, + total_ms, + avg_paint_ms: paint_ms, + output_path: config.output_path.clone(), + }) +} diff --git a/packages/native-renderer/tests/pipeline_test.rs b/packages/native-renderer/tests/pipeline_test.rs new file mode 100644 index 0000000000..e509fe7193 --- /dev/null +++ b/packages/native-renderer/tests/pipeline_test.rs @@ -0,0 +1,128 @@ +use std::path::Path; + +use hyperframes_native_renderer::pipeline::{render_static, RenderConfig}; +use hyperframes_native_renderer::scene::{Color, Element, ElementKind, Rect, Scene, Style}; + +/// Build a realistic scene: dark-blue background, white rounded card, text inside the card. +fn make_test_scene() -> Scene { + let text = Element { + id: "heading".into(), + kind: ElementKind::Text { + content: "Hello from Skia!".into(), + }, + bounds: Rect { + x: 24.0, + y: 20.0, + width: 280.0, + height: 40.0, + }, + style: Style { + color: Some(Color { + r: 30, + g: 30, + b: 50, + a: 255, + }), + font_size: Some(28.0), + ..Style::default() + }, + children: vec![], + }; + + let card = Element { + id: "card".into(), + kind: ElementKind::Container, + bounds: Rect { + x: 140.0, + y: 80.0, + width: 360.0, + height: 200.0, + }, + style: Style { + background_color: Some(Color { + r: 255, + g: 255, + b: 255, + a: 255, + }), + border_radius: [16.0; 4], + overflow_hidden: true, + ..Style::default() + }, + children: vec![text], + }; + + let background = Element { + id: "bg".into(), + kind: ElementKind::Container, + bounds: Rect { + x: 0.0, + y: 0.0, + width: 640.0, + height: 360.0, + }, + style: Style { + background_color: Some(Color { + r: 15, + g: 23, + b: 42, + a: 255, + }), + ..Style::default() + }, + children: vec![card], + }; + + Scene { + width: 640, + height: 360, + elements: vec![background], + } +} + +#[test] +fn render_static_scene_to_mp4() { + let scene = make_test_scene(); + let output_path = "/tmp/hyperframes-native-test.mp4"; + + let config = RenderConfig { + fps: 30, + duration_secs: 1.0, + quality: 80, + output_path: output_path.to_string(), + }; + + let result = render_static(&scene, &config).unwrap(); + + assert_eq!(result.total_frames, 30); + assert_eq!(result.output_path, output_path); + + let path = Path::new(output_path); + assert!(path.exists(), "output MP4 must exist"); + + let size = std::fs::metadata(output_path).unwrap().len(); + assert!(size > 1000, "MP4 should be non-trivial, got {size} bytes"); + + std::fs::remove_file(output_path).ok(); +} + +#[test] +fn render_static_fractional_duration() { + let scene = make_test_scene(); + let output_path = "/tmp/hyperframes-native-frac.mp4"; + + let config = RenderConfig { + fps: 24, + duration_secs: 0.5, + quality: 70, + output_path: output_path.to_string(), + }; + + let result = render_static(&scene, &config).unwrap(); + + // ceil(24 * 0.5) = 12 + assert_eq!(result.total_frames, 12); + assert!(Path::new(output_path).exists()); + + std::fs::remove_file(output_path).ok(); +}