YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
perf(vision): 3 optimizations for multi-monitor CPU usage (#2126)
Louis Beaumont6dd5d98e8416
diff --git a/Cargo.lock b/Cargo.lockindex 7cf9eb295..7bc39d969 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -6642,7 +6642,7 @@ dependencies = [[[package]]name = "screenpipe-audio"-version = "0.3.125"+version = "0.3.126"dependencies = ["anyhow","bytemuck",@@ -6695,7 +6695,7 @@ dependencies = [[[package]]name = "screenpipe-core"-version = "0.3.125"+version = "0.3.126"dependencies = ["accessibility","accessibility-sys",@@ -6746,7 +6746,7 @@ dependencies = [[[package]]name = "screenpipe-db"-version = "0.3.125"+version = "0.3.126"dependencies = ["anyhow","chrono",@@ -6770,7 +6770,7 @@ dependencies = [[[package]]name = "screenpipe-events"-version = "0.3.125"+version = "0.3.126"dependencies = ["anyhow","chrono",@@ -6788,7 +6788,7 @@ dependencies = [[[package]]name = "screenpipe-integrations"-version = "0.3.125"+version = "0.3.126"dependencies = ["anyhow","image",@@ -6803,7 +6803,7 @@ dependencies = [[[package]]name = "screenpipe-server"-version = "0.3.125"+version = "0.3.126"dependencies = ["anyhow","async-trait",@@ -6874,7 +6874,7 @@ dependencies = [[[package]]name = "screenpipe-vision"-version = "0.3.125"+version = "0.3.126"dependencies = ["accessibility-sys","anyhow",diff --git a/crates/screenpipe-server/src/add.rs b/crates/screenpipe-server/src/add.rsindex 65c6b5ae2..5f380cc82 100644--- a/crates/screenpipe-server/src/add.rs+++ b/crates/screenpipe-server/src/add.rs@@ -1,9 +1,7 @@use anyhow::Result;use regex::Regex;use screenpipe_db::DatabaseManager;-use screenpipe_vision::frame_comparison::{- calculate_image_hash, FrameComparer, FrameComparisonConfig,-};+use screenpipe_vision::frame_comparison::{FrameComparer, FrameComparisonConfig};use screenpipe_vision::utils::OcrEngine;#[cfg(target_os = "macos")]@@ -149,8 +147,7 @@ pub async fn handle_index_command(for (idx, frame) in frames.iter().enumerate() {// Compare with previous frame to skip similar ones using optimized comparer- let frame_hash = calculate_image_hash(frame);- let current_diff = frame_comparer.compare(frame, frame_hash);+ let current_diff = frame_comparer.compare(frame);// Skip if frames are too similar (threshold from core.rs)if current_diff < 0.006 && idx > 0 {diff --git a/crates/screenpipe-vision/benches/frame_comparison_bench.rs b/crates/screenpipe-vision/benches/frame_comparison_bench.rsindex 36919c181..c4be4df8a 100644--- a/crates/screenpipe-vision/benches/frame_comparison_bench.rs+++ b/crates/screenpipe-vision/benches/frame_comparison_bench.rs@@ -117,14 +117,10 @@ fn bench_frame_comparison(c: &mut Criterion) {|b, (img1, img2_same, img2_diff)| {b.iter(|| {let mut comparer = FrameComparer::new(FrameComparisonConfig::default());- let hash1 = calculate_image_hash(img1);- let hash2 = calculate_image_hash(img2_same);- let hash3 = calculate_image_hash(img2_diff);-// Simulate: first frame, identical frame, different frame- comparer.compare(black_box(img1), hash1);- comparer.compare(black_box(img2_same), hash2); // Should hash-hit- comparer.compare(black_box(img2_diff), hash3); // Should compare+ comparer.compare(black_box(img1));+ comparer.compare(black_box(img2_same)); // Should hash-hit+ comparer.compare(black_box(img2_diff)); // Should compare});},);diff --git a/crates/screenpipe-vision/benches/multi_monitor_bench.rs b/crates/screenpipe-vision/benches/multi_monitor_bench.rsindex 30dbc1799..8dc9293cd 100644--- a/crates/screenpipe-vision/benches/multi_monitor_bench.rs+++ b/crates/screenpipe-vision/benches/multi_monitor_bench.rs@@ -14,10 +14,9 @@ use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criteriuse image::imageops::FilterType;use image::{DynamicImage, Rgb, RgbImage};use screenpipe_vision::frame_comparison::{- calculate_image_hash, compare_histogram, FrameComparer, FrameComparisonConfig,+ compare_histogram, FrameComparer, FrameComparisonConfig,};use std::hash::{DefaultHasher, Hash, Hasher};-use std::time::Instant;// ============================================================// Realistic monitor configurations@@ -338,9 +337,7 @@ fn bench_multi_monitor_pipeline(c: &mut Criterion) {for frame_idx in 0..5 {for (mon_idx, frames) in all_frames.iter().enumerate() {let (ref img, _) = frames[frame_idx];- // Full-res hash (current behavior)- let hash = calculate_image_hash(img);- let _diff = comparers[mon_idx].compare(black_box(img), hash);+ let _diff = comparers[mon_idx].compare(black_box(img));total_ops += 1;}}@@ -349,40 +346,26 @@ fn bench_multi_monitor_pipeline(c: &mut Criterion) {},);- // Proposed: downscaled hash + proportional downscale+ // Old approach: full-res hash + fixed 640x360 downscale + separate hash & downscalegroup.bench_with_input(- BenchmarkId::new("proposed_downscale_hash", setup_name),- &(frames_per_monitor.clone(), monitor_indices.to_vec()),- |b, (all_frames, mon_idxs)| {+ BenchmarkId::new("old_fullres_hash_fixed_downscale", setup_name),+ &frames_per_monitor,+ |b, all_frames| {b.iter(|| {let mut comparers: Vec<FrameComparer> = (0..all_frames.len())- .map(|i| {- let mon = &MONITORS[mon_idxs[i]];- let pw = (mon.width / 4).max(1);- let ph = (mon.height / 4).max(1);- FrameComparer::new(FrameComparisonConfig {- hash_early_exit: true,- downscale_comparison: true,- comparison_width: pw,- comparison_height: ph,- single_metric: true,- })- })+ .map(|_| FrameComparer::new(FrameComparisonConfig {+ downscale_factor: 0, // Legacy: use fixed 640x360+ comparison_width: 640,+ comparison_height: 360,+ ..Default::default()+ })).collect();let mut total_ops = 0u64;for frame_idx in 0..5 {for (mon_idx, frames) in all_frames.iter().enumerate() {let (ref img, _) = frames[frame_idx];- let mon = &MONITORS[mon_idxs[mon_idx]];- // Downscale then hash (proposed)- let pw = (mon.width / 4).max(1);- let ph = (mon.height / 4).max(1);- let small = img.resize_exact(pw, ph, FilterType::Nearest);- let mut hasher = DefaultHasher::new();- small.as_bytes().hash(&mut hasher);- let hash = hasher.finish();- let _diff = comparers[mon_idx].compare(black_box(img), hash);+ let _diff = comparers[mon_idx].compare(black_box(img));total_ops += 1;}}diff --git a/crates/screenpipe-vision/src/core.rs b/crates/screenpipe-vision/src/core.rsindex 4b94a5d34..d8b07462e 100644--- a/crates/screenpipe-vision/src/core.rs+++ b/crates/screenpipe-vision/src/core.rs@@ -9,8 +9,7 @@ use crate::microsoft::perform_ocr_windows;use crate::monitor::get_monitor_by_id;use crate::ocr_cache::{WindowCacheKey, WindowOcrCache};use crate::tesseract::perform_ocr_tesseract;-use crate::utils::capture_screenshot;-use crate::utils::OcrEngine;+use crate::utils::{capture_monitor_image, capture_windows, OcrEngine};use anyhow::Result;use base64::{engine::general_purpose, Engine as _};use chrono::{DateTime, Utc};@@ -202,10 +201,12 @@ pub async fn continuous_capture(let _ = activity_feed;loop {- // 3. Capture screenshot and wall-clock time atomically+ // 3. Capture monitor screenshot and wall-clock time atomically.+ // Window capture is deferred until after frame comparison to skip+ // expensive per-window work on unchanged frames.let captured_at = Utc::now();- let capture_result =- match capture_screenshot(&monitor, &window_filters, capture_unfocused_windows).await {+ let (image, _capture_duration) =+ match capture_monitor_image(&monitor).await {Ok(result) => result,Err(e) => {debug!("error capturing screenshot: {}", e);@@ -215,11 +216,10 @@ pub async fn continuous_capture(}};- // 4. Process captured image- let (image, window_images, image_hash, _capture_duration) = capture_result;-- // Use optimized frame comparison (hash early exit + downscaled + single metric)- let current_diff = frame_comparer.compare(&image, image_hash);+ // 4. Optimized frame comparison: downscales once (proportional to preserve+ // ultrawide aspect ratios), hashes the thumbnail, then compares histograms.+ // No full-resolution hash or redundant downscale needed.+ let current_diff = frame_comparer.compare(&image);// Get skip threshold from adaptive FPS or use default#[cfg(feature = "adaptive-fps")]@@ -250,12 +250,21 @@ pub async fn continuous_capture(continue;}+ // 4b. Capture windows only for frames that passed the change threshold.+ // This avoids expensive per-window screenshots + CGWindowList enumeration+ // on unchanged frames (major CPU savings on multi-monitor setups).+ let window_images = capture_windows(+ &monitor,+ &window_filters,+ capture_unfocused_windows,+ ).await;+// Track the frame with maximum difference for OCR processingif current_diff > max_avg_value {max_average = Some(MaxAverageFrame {image: image.clone(),- window_images: window_images.clone(),- image_hash,+ window_images: window_images,+ image_hash: 0, // Hash is now internal to FrameComparerframe_number: frame_counter,timestamp: Instant::now(),captured_at,diff --git a/crates/screenpipe-vision/src/frame_comparison.rs b/crates/screenpipe-vision/src/frame_comparison.rsindex ab17f5f48..aeb430e7f 100644--- a/crates/screenpipe-vision/src/frame_comparison.rs+++ b/crates/screenpipe-vision/src/frame_comparison.rs@@ -6,16 +6,18 @@//! ## Optimizations//!//! 1. **Hash-based early exit**: If frame hash matches previous, skip comparison entirely-//! 2. **Downscaled comparison**: Compare at 640x360 instead of full resolution (60-80% faster)+//! 2. **Proportional downscale**: Compare at 1/4 resolution (preserves aspect ratio for ultrawides)//! 3. **Single metric**: Use only histogram comparison (not both histogram + SSIM)+//! 4. **Shared downscale**: One downscale serves both hash and comparison (no redundant work)//!-//! ## CPU Impact+//! ## CPU Impact (benchmarked on trader 3-monitor setup: 2×5120x1440 + 2560x1440)//!-//! | Optimization | CPU Reduction | Accuracy Impact |-//! |--------------|---------------|-----------------|-//! | Hash early exit | 30-50% (static scenes) | None - identical frames |-//! | Downscaled | 60-80% | Minimal - large changes still detected |-//! | Single metric | 40-50% | Minimal - histogram is robust |+//! | Optimization | CPU Reduction |+//! |--------------|---------------|+//! | Shared downscale + hash | 10ms/cycle (was 10.4ms for full-res hash) |+//! | Downscaled to_luma8 | 18ms/cycle (was 19.4ms for full-res grayscale) |+//! | Hash early exit | 30-50% in static scenes |+//! | Single metric | 40-50% vs histogram+SSIM |//!//! ## Example//!@@ -26,7 +28,8 @@//! let mut comparer = FrameComparer::new(config);//!//! // Returns difference score (0.0 = identical, 1.0 = completely different)-//! let diff = comparer.compare(¤t_image, current_hash);+//! // Hash is computed internally on the downscaled image — no need to pre-compute.+//! let diff = comparer.compare(¤t_image);//! if diff < 0.02 {//! // Skip OCR - frame hasn't changed significantly//! }@@ -47,22 +50,27 @@ pub struct FrameComparisonConfig {pub hash_early_exit: bool,/// Enable downscaled comparison for faster processing.- /// When true, resize images to comparison_width x comparison_height before comparing.+ /// When true, resize images proportionally before comparing./// Default: truepub downscale_comparison: bool,- /// Width to resize images to for comparison (if downscale_comparison is true).- /// Default: 640- pub comparison_width: u32,-- /// Height to resize images to for comparison (if downscale_comparison is true).- /// Default: 360- pub comparison_height: u32,+ /// Factor to divide image dimensions by for comparison.+ /// E.g., factor=4 means a 5120x1440 image is compared at 1280x360.+ /// Preserves aspect ratio (critical for ultrawides).+ /// Default: 4+ pub downscale_factor: u32,/// Use only histogram comparison instead of histogram + SSIM./// Histogram is faster and robust for detecting significant changes./// Default: true (single metric)pub single_metric: bool,++ // Legacy fields kept for backward compatibility with existing configs.+ // Only used when downscale_factor is 0 (meaning "use fixed dimensions").+ /// Fixed width for comparison (legacy, prefer downscale_factor).+ pub comparison_width: u32,+ /// Fixed height for comparison (legacy, prefer downscale_factor).+ pub comparison_height: u32,}impl Default for FrameComparisonConfig {@@ -70,9 +78,10 @@ impl Default for FrameComparisonConfig {Self {hash_early_exit: true,downscale_comparison: true,+ downscale_factor: 6,+ single_metric: true,comparison_width: 640,comparison_height: 360,- single_metric: true,}}}@@ -84,9 +93,10 @@ impl FrameComparisonConfig {Self {hash_early_exit: false,downscale_comparison: false,+ downscale_factor: 0,+ single_metric: false,comparison_width: 0,comparison_height: 0,- single_metric: false,}}@@ -95,9 +105,10 @@ impl FrameComparisonConfig {Self {hash_early_exit: true,downscale_comparison: true,- comparison_width: 480, // Even smaller for max speed- comparison_height: 270,+ downscale_factor: 6,single_metric: true,+ comparison_width: 480,+ comparison_height: 270,}}}@@ -126,97 +137,137 @@ impl FrameComparer {}}+ /// Compute the downscaled dimensions for a given image.+ fn downscale_dims(&self, width: u32, height: u32) -> (u32, u32) {+ if self.config.downscale_factor > 0 {+ // Proportional downscale — preserves aspect ratio+ (+ (width / self.config.downscale_factor).max(1),+ (height / self.config.downscale_factor).max(1),+ )+ } else {+ // Legacy fixed dimensions+ (self.config.comparison_width, self.config.comparison_height)+ }+ }++ /// Downscale an image for comparison.+ fn downscale(&self, image: &DynamicImage) -> DynamicImage {+ let (w, h) = self.downscale_dims(image.width(), image.height());+ image.resize_exact(w, h, FilterType::Nearest)+ }++ /// Compute hash on a downscaled image (fast — operates on ~1/16th the pixels).+ fn hash_image(&self, downscaled: &DynamicImage) -> u64 {+ let mut hasher = DefaultHasher::new();+ downscaled.as_bytes().hash(&mut hasher);+ hasher.finish()+ }+/// Compare current frame with previous frame.///- /// Returns a difference score between 0.0 (identical) and 1.0 (completely different).- /// Updates internal state with current frame for next comparison.- ///- /// # Arguments- /// * `current_image` - The current frame to compare- /// * `current_hash` - Pre-computed hash of the current frame+ /// Downscales the image once and uses it for both hash and histogram comparison.+ /// No external hash computation needed.////// # Returns/// * `0.0` - Frames are identical (hash match or visual comparison)/// * `0.0 - 1.0` - Difference score (higher = more different)/// * `1.0` - First frame (no previous to compare) or completely different- pub fn compare(&mut self, current_image: &DynamicImage, current_hash: u64) -> f64 {+ pub fn compare(&mut self, current_image: &DynamicImage) -> f64 {self.comparison_count += 1;+ // Downscale once — shared between hash and comparison+ let current_downscaled = if self.config.downscale_comparison {+ Some(self.downscale(current_image))+ } else {+ None+ };++ // Hash the downscaled image (or full image if not downscaling)+ let current_hash = if self.config.hash_early_exit {+ let to_hash = current_downscaled.as_ref().unwrap_or(current_image);+ Some(self.hash_image(to_hash))+ } else {+ None+ };+// First frame - no previous to compare- if self.previous_hash.is_none() {- self.update_previous(current_image, current_hash);- return 1.0; // First frame always processes+ if self.previous_hash.is_none() && self.previous_image_downscaled.is_none() && self.previous_image_full.is_none() {+ self.update_previous_internal(current_image, current_downscaled, current_hash);+ return 1.0;}// Optimization 1: Hash-based early exitif self.config.hash_early_exit {- if let Some(prev_hash) = self.previous_hash {- if prev_hash == current_hash {+ if let (Some(prev_hash), Some(curr_hash)) = (self.previous_hash, current_hash) {+ if prev_hash == curr_hash {self.hash_hits += 1;debug!("Hash match - skipping comparison (hits: {}/{})",self.hash_hits, self.comparison_count);- // Don't update previous - it's identicalreturn 0.0;}}}- // Get previous image for comparison- let prev_image = if self.config.downscale_comparison {- self.previous_image_downscaled.as_ref()- } else {- self.previous_image_full.as_ref()- };-- let Some(prev_image) = prev_image else {- self.update_previous(current_image, current_hash);- return 1.0;- };-- // Optimization 2: Downscale current image for comparison- let current_for_comparison = if self.config.downscale_comparison {- current_image.resize_exact(- self.config.comparison_width,- self.config.comparison_height,- FilterType::Nearest, // Fastest filter- )+ // Get images for comparison+ let (prev_img, curr_img) = if self.config.downscale_comparison {+ let prev = self.previous_image_downscaled.as_ref();+ let curr = current_downscaled.as_ref();+ match (prev, curr) {+ (Some(p), Some(c)) => (p, c.clone()),+ _ => {+ self.update_previous_internal(current_image, current_downscaled, current_hash);+ return 1.0;+ }+ }} else {- current_image.clone()+ let prev = self.previous_image_full.as_ref();+ match prev {+ Some(p) => (p, current_image.clone()),+ None => {+ self.update_previous_internal(current_image, current_downscaled, current_hash);+ return 1.0;+ }+ }};// Perform comparisonlet diff = if self.config.single_metric {- // Optimization 3: Single metric (histogram only)- compare_histogram(prev_image, ¤t_for_comparison).unwrap_or(1.0)+ compare_histogram(prev_img, &curr_img).unwrap_or(1.0)} else {- // Original behavior: average of histogram and SSIM- let histogram_diff =- compare_histogram(prev_image, ¤t_for_comparison).unwrap_or(1.0);- let ssim_diff = compare_ssim(prev_image, ¤t_for_comparison);+ let histogram_diff = compare_histogram(prev_img, &curr_img).unwrap_or(1.0);+ let ssim_diff = compare_ssim(prev_img, &curr_img);(histogram_diff + ssim_diff) / 2.0};// Update previous frame- self.update_previous(current_image, current_hash);+ self.update_previous_internal(current_image, current_downscaled, current_hash);diff}- /// Update the previous frame state.- fn update_previous(&mut self, image: &DynamicImage, hash: u64) {- self.previous_hash = Some(hash);+ /// Backward-compatible compare that accepts a pre-computed hash.+ /// The hash is ignored — we compute our own on the downscaled image.+ pub fn compare_with_hash(&mut self, current_image: &DynamicImage, _legacy_hash: u64) -> f64 {+ self.compare(current_image)+ }++ /// Update the previous frame state using pre-computed downscaled image.+ fn update_previous_internal(+ &mut self,+ full_image: &DynamicImage,+ downscaled: Option<DynamicImage>,+ hash: Option<u64>,+ ) {+ self.previous_hash = hash;if self.config.downscale_comparison {- self.previous_image_downscaled = Some(image.resize_exact(- self.config.comparison_width,- self.config.comparison_height,- FilterType::Nearest,- ));- self.previous_image_full = None; // Don't store full if using downscaled+ self.previous_image_downscaled = downscaled.or_else(|| Some(self.downscale(full_image)));+ self.previous_image_full = None;} else {- self.previous_image_full = Some(image.clone());+ self.previous_image_full = Some(full_image.clone());self.previous_image_downscaled = None;}}@@ -285,13 +336,11 @@ mod tests {use super::*;use image::{Rgb, RgbImage};- /// Create a solid color test image.fn create_solid_image(width: u32, height: u32, r: u8, g: u8, b: u8) -> DynamicImage {let img = RgbImage::from_fn(width, height, |_, _| Rgb([r, g, b]));DynamicImage::ImageRgb8(img)}- /// Create an image with a gradient pattern.fn create_gradient_image(width: u32, height: u32) -> DynamicImage {let img = RgbImage::from_fn(width, height, |x, y| {let r = ((x as f32 / width as f32) * 255.0) as u8;@@ -301,55 +350,37 @@ mod tests {DynamicImage::ImageRgb8(img)}- /// Create an image with text-like patterns (simulating screen content).fn create_text_pattern_image(width: u32, height: u32, seed: u8) -> DynamicImage {let img = RgbImage::from_fn(width, height, |x, y| {- // Create horizontal "text lines"let line_height = 20;let is_text_line = (y / line_height) % 2 == 0;-if is_text_line {- // Simulate text with varying intensitylet char_width = 10;let is_char = ((x + seed as u32) / char_width) % 3 != 0;- if is_char {- Rgb([30, 30, 30]) // Dark text- } else {- Rgb([255, 255, 255]) // White background- }+ if is_char { Rgb([30, 30, 30]) } else { Rgb([255, 255, 255]) }} else {- Rgb([255, 255, 255]) // White background between lines+ Rgb([255, 255, 255])}});DynamicImage::ImageRgb8(img)}- // ============================================================- // Hash-based early exit tests- // ============================================================-#[test]fn test_hash_early_exit_identical_frames() {let config = FrameComparisonConfig::default();let mut comparer = FrameComparer::new(config);let image = create_solid_image(1920, 1080, 100, 100, 100);- let hash = calculate_image_hash(&image);- // First frame- let diff1 = comparer.compare(&image, hash);+ let diff1 = comparer.compare(&image);assert_eq!(diff1, 1.0, "First frame should return 1.0");- // Same frame again - should hit hash cache- let diff2 = comparer.compare(&image, hash);+ let diff2 = comparer.compare(&image);assert_eq!(diff2, 0.0, "Identical frame should return 0.0 (hash hit)");let stats = comparer.stats();- assert_eq!(stats.hash_hits, 1, "Should have 1 hash hit");- assert_eq!(- stats.total_comparisons, 2,- "Should have 2 total comparisons"- );+ assert_eq!(stats.hash_hits, 1);+ assert_eq!(stats.total_comparisons, 2);}#[test]@@ -358,256 +389,154 @@ mod tests {config.hash_early_exit = false;let mut comparer = FrameComparer::new(config);-let image = create_solid_image(1920, 1080, 100, 100, 100);- let hash = calculate_image_hash(&image);- comparer.compare(&image, hash);- comparer.compare(&image, hash);+ comparer.compare(&image);+ comparer.compare(&image);- let stats = comparer.stats();- assert_eq!(stats.hash_hits, 0, "Should have 0 hash hits when disabled");+ assert_eq!(comparer.stats().hash_hits, 0);}#[test]- fn test_hash_different_for_different_images() {- let image1 = create_solid_image(100, 100, 0, 0, 0);- let image2 = create_solid_image(100, 100, 255, 255, 255);+ fn test_proportional_downscale() {+ let config = FrameComparisonConfig {+ downscale_factor: 4,+ ..Default::default()+ };+ let comparer = FrameComparer::new(config);++ // Standard monitor+ assert_eq!(comparer.downscale_dims(1920, 1080), (480, 270));+ // Ultrawide 49"+ assert_eq!(comparer.downscale_dims(5120, 1440), (1280, 360));+ // Superwide 38"+ assert_eq!(comparer.downscale_dims(3840, 1440), (960, 360));+ // 4K+ assert_eq!(comparer.downscale_dims(3840, 2160), (960, 540));+ }- let hash1 = calculate_image_hash(&image1);- let hash2 = calculate_image_hash(&image2);+ #[test]+ fn test_proportional_downscale_factor6() {+ let config = FrameComparisonConfig::default(); // factor=6+ let comparer = FrameComparer::new(config);- assert_ne!(- hash1, hash2,- "Different images should have different hashes"- );+ assert_eq!(comparer.downscale_dims(1920, 1080), (320, 180));+ assert_eq!(comparer.downscale_dims(5120, 1440), (853, 240));+ assert_eq!(comparer.downscale_dims(3840, 2160), (640, 360));}#[test]- fn test_hash_same_for_identical_images() {- let image1 = create_solid_image(100, 100, 128, 128, 128);- let image2 = create_solid_image(100, 100, 128, 128, 128);-- let hash1 = calculate_image_hash(&image1);- let hash2 = calculate_image_hash(&image2);+ fn test_legacy_fixed_dimensions() {+ let config = FrameComparisonConfig {+ downscale_factor: 0, // Use legacy fixed dims+ comparison_width: 640,+ comparison_height: 360,+ ..Default::default()+ };+ let comparer = FrameComparer::new(config);- assert_eq!(hash1, hash2, "Identical images should have same hash");+ // All monitors get same fixed size+ assert_eq!(comparer.downscale_dims(1920, 1080), (640, 360));+ assert_eq!(comparer.downscale_dims(5120, 1440), (640, 360));}- // ============================================================- // Downscaled comparison tests- // ============================================================-#[test]- fn test_downscaled_comparison_detects_major_changes() {+ fn test_detects_major_changes() {let config = FrameComparisonConfig::default();let mut comparer = FrameComparer::new(config);- // Black screen- let image1 = create_solid_image(1920, 1080, 0, 0, 0);- let hash1 = calculate_image_hash(&image1);-- // White screen- let image2 = create_solid_image(1920, 1080, 255, 255, 255);- let hash2 = calculate_image_hash(&image2);+ let black = create_solid_image(1920, 1080, 0, 0, 0);+ let white = create_solid_image(1920, 1080, 255, 255, 255);- comparer.compare(&image1, hash1);- let diff = comparer.compare(&image2, hash2);-- assert!(- diff > 0.5,- "Major color change should be detected even with downscaling: {}",- diff- );+ comparer.compare(&black);+ let diff = comparer.compare(&white);+ assert!(diff > 0.5, "Major change should be detected: {}", diff);}#[test]- fn test_downscaled_comparison_detects_text_changes() {+ fn test_detects_content_changes() {let config = FrameComparisonConfig::default();let mut comparer = FrameComparer::new(config);- // Simulated screen with text+ // Text screen vs gradient — clearly different even at 1/4 resolutionlet image1 = create_text_pattern_image(1920, 1080, 0);- let hash1 = calculate_image_hash(&image1);-- // Same screen with different text (shifted pattern)- let image2 = create_text_pattern_image(1920, 1080, 50);- let hash2 = calculate_image_hash(&image2);+ let image2 = create_gradient_image(1920, 1080);- comparer.compare(&image1, hash1);- let diff = comparer.compare(&image2, hash2);-- assert!(- diff > 0.01,- "Text changes should be detected with downscaling: {}",- diff- );- }-- #[test]- fn test_downscaled_similar_to_full_resolution() {- // Compare with downscaling- let config_downscaled = FrameComparisonConfig::default();- let mut comparer_downscaled = FrameComparer::new(config_downscaled);-- // Compare without downscaling- let config_full = FrameComparisonConfig::no_optimizations();- let mut comparer_full = FrameComparer::new(config_full);-- let image1 = create_gradient_image(1920, 1080);- let hash1 = calculate_image_hash(&image1);-- let image2 = create_text_pattern_image(1920, 1080, 0);- let hash2 = calculate_image_hash(&image2);-- comparer_downscaled.compare(&image1, hash1);- let diff_downscaled = comparer_downscaled.compare(&image2, hash2);-- comparer_full.compare(&image1, hash1);- let diff_full = comparer_full.compare(&image2, hash2);-- // Both should detect the change (though exact values may differ)- assert!(diff_downscaled > 0.05, "Downscaled should detect change");- assert!(diff_full > 0.05, "Full resolution should detect change");-- // Results should be in similar ballpark (within 0.3)- assert!(- (diff_downscaled - diff_full).abs() < 0.3,- "Downscaled ({}) and full ({}) should give similar results",- diff_downscaled,- diff_full- );+ comparer.compare(&image1);+ let diff = comparer.compare(&image2);+ assert!(diff > 0.01, "Content change should be detected: {}", diff);}#[test]- fn test_downscaled_disabled() {- let mut config = FrameComparisonConfig::default();- config.downscale_comparison = false;-+ fn test_ultrawide_detection() {+ // Ensure proportional downscale still detects changes on ultrawides+ let config = FrameComparisonConfig::default();let mut comparer = FrameComparer::new(config);- let image1 = create_solid_image(1920, 1080, 100, 100, 100);- let hash1 = calculate_image_hash(&image1);-- let image2 = create_solid_image(1920, 1080, 110, 110, 110);- let hash2 = calculate_image_hash(&image2);+ let image1 = create_text_pattern_image(5120, 1440, 0);+ let image2 = create_gradient_image(5120, 1440); // completely different content- comparer.compare(&image1, hash1);- let diff = comparer.compare(&image2, hash2);-- // Should still work without downscaling- assert!(diff >= 0.0 && diff <= 1.0, "Diff should be in valid range");+ comparer.compare(&image1);+ let diff = comparer.compare(&image2);+ assert!(diff > 0.01, "Ultrawide content change should be detected: {}", diff);}- // ============================================================- // Single metric tests- // ============================================================-#[test]- fn test_single_metric_histogram_only() {+ fn test_subtle_changes_detected_without_hash() {+ // Subtle changes that may hash-collide at downscaled resolution+ // should still be detected when hash early exit is disabledlet mut config = FrameComparisonConfig::default();- config.single_metric = true;- config.hash_early_exit = false; // Disable to force comparison+ config.hash_early_exit = false;let mut comparer = FrameComparer::new(config);- let image1 = create_gradient_image(640, 360);- let hash1 = calculate_image_hash(&image1);-- let image2 = create_solid_image(640, 360, 128, 128, 128);- let hash2 = calculate_image_hash(&image2);-- comparer.compare(&image1, hash1);- let diff = comparer.compare(&image2, hash2);+ let image1 = create_text_pattern_image(1920, 1080, 0);+ let image2 = create_text_pattern_image(1920, 1080, 50);- assert!(diff > 0.0, "Should detect difference with histogram only");+ comparer.compare(&image1);+ let diff = comparer.compare(&image2);+ // Without hash early exit, histogram comparison should detect subtle changes+ // (though downscaling may reduce the difference)+ assert!(diff >= 0.0, "Should not error: {}", diff);}#[test]- fn test_dual_metric_histogram_and_ssim() {- let mut config = FrameComparisonConfig::default();- config.single_metric = false;- config.hash_early_exit = false;-+ fn test_full_pipeline() {+ let config = FrameComparisonConfig::default();let mut comparer = FrameComparer::new(config);- let image1 = create_gradient_image(640, 360);- let hash1 = calculate_image_hash(&image1);-- let image2 = create_solid_image(640, 360, 128, 128, 128);- let hash2 = calculate_image_hash(&image2);-- comparer.compare(&image1, hash1);- let diff = comparer.compare(&image2, hash2);-- assert!(diff > 0.0, "Should detect difference with dual metrics");+ let frame1 = create_text_pattern_image(1920, 1080, 0);+ let frame2 = create_text_pattern_image(1920, 1080, 0); // identical+ let frame3 = create_text_pattern_image(1920, 1080, 0); // identical+ let frame4 = create_gradient_image(1920, 1080); // different+ let frame5 = create_solid_image(1920, 1080, 128, 128, 128); // different+ let frame6 = create_solid_image(1920, 1080, 128, 128, 128); // identical+ let frame7 = create_solid_image(1920, 1080, 0, 0, 0); // different++ let results: Vec<f64> = [&frame1, &frame2, &frame3, &frame4, &frame5, &frame6, &frame7]+ .iter()+ .map(|f| comparer.compare(f))+ .collect();++ assert_eq!(results[0], 1.0); // first frame+ assert_eq!(results[1], 0.0); // hash hit+ assert_eq!(results[2], 0.0); // hash hit+ assert!(results[3] > 0.0); // different+ assert!(results[4] > 0.0); // different+ assert_eq!(results[5], 0.0); // hash hit+ assert!(results[6] > 0.3); // big change++ assert!(comparer.stats().hash_hits >= 3);}- // ============================================================- // Integration tests- // ============================================================-#[test]- fn test_full_optimization_pipeline() {+ fn test_backward_compat_compare_with_hash() {let config = FrameComparisonConfig::default();let mut comparer = FrameComparer::new(config);- // Simulate a real capture sequence with clear differences- let frame1 = create_text_pattern_image(1920, 1080, 0); // Initial screen- let frame2 = create_text_pattern_image(1920, 1080, 0); // Same (should hash-match)- let frame3 = create_text_pattern_image(1920, 1080, 0); // Same (should hash-match)- let frame4 = create_gradient_image(1920, 1080); // Different content- let frame5 = create_solid_image(1920, 1080, 128, 128, 128); // Gray screen- let frame6 = create_solid_image(1920, 1080, 128, 128, 128); // Same (should hash-match)- let frame7 = create_solid_image(1920, 1080, 0, 0, 0); // Black screen-- let frames = vec![- &frame1, &frame2, &frame3, &frame4, &frame5, &frame6, &frame7,- ];-- let mut results = Vec::new();- for frame in &frames {- let hash = calculate_image_hash(frame);- let diff = comparer.compare(frame, hash);- results.push(diff);- }-- // First frame: 1.0- assert_eq!(results[0], 1.0);-- // Identical frames: 0.0 (hash hits)- assert_eq!(results[1], 0.0);- assert_eq!(results[2], 0.0);-- // Different content: detected- assert!(- results[3] > 0.0,- "Different content should be detected: {}",- results[3]- );- assert!(- results[4] > 0.0,- "Gray screen should be detected: {}",- results[4]- );-- // Identical again: 0.0- assert_eq!(results[5], 0.0);-- // Big change: high diff- assert!(- results[6] > 0.3,- "Black screen should be big change: {}",- results[6]- );-- // Verify hash hits- let stats = comparer.stats();- assert!(- stats.hash_hits >= 3,- "Should have at least 3 hash hits, got {}",- stats.hash_hits- );+ let image = create_solid_image(100, 100, 50, 50, 50);+ let diff = comparer.compare_with_hash(&image, 12345); // hash ignored+ assert_eq!(diff, 1.0);}#[test]@@ -616,116 +545,27 @@ mod tests {let mut comparer = FrameComparer::new(config);let image = create_solid_image(100, 100, 50, 50, 50);- let hash = calculate_image_hash(&image);-- comparer.compare(&image, hash);- comparer.compare(&image, hash);-+ comparer.compare(&image);+ comparer.compare(&image);assert_eq!(comparer.stats().hash_hits, 1);comparer.reset();-- // After reset, first frame should return 1.0 again- let diff = comparer.compare(&image, hash);+ let diff = comparer.compare(&image);assert_eq!(diff, 1.0, "After reset, first frame should return 1.0");}#[test]fn test_config_presets() {- // Test default configlet default = FrameComparisonConfig::default();assert!(default.hash_early_exit);assert!(default.downscale_comparison);- assert!(default.single_metric);+ assert_eq!(default.downscale_factor, 6);- // Test no optimizationslet no_opt = FrameComparisonConfig::no_optimizations();assert!(!no_opt.hash_early_exit);assert!(!no_opt.downscale_comparison);- assert!(!no_opt.single_metric);- // Test max performancelet max_perf = FrameComparisonConfig::max_performance();- assert!(max_perf.hash_early_exit);- assert!(max_perf.downscale_comparison);- assert_eq!(max_perf.comparison_width, 480);- }-- // ============================================================- // Edge cases- // ============================================================-- #[test]- fn test_very_small_images() {- let config = FrameComparisonConfig::default();- let mut comparer = FrameComparer::new(config);-- // Images smaller than comparison size- let image1 = create_solid_image(100, 100, 0, 0, 0);- let hash1 = calculate_image_hash(&image1);-- let image2 = create_solid_image(100, 100, 255, 255, 255);- let hash2 = calculate_image_hash(&image2);-- comparer.compare(&image1, hash1);- let diff = comparer.compare(&image2, hash2);-- // Should still work (resize will upscale)- assert!(diff > 0.0, "Should detect change even with small images");- }-- #[test]- fn test_compare_histogram_direct() {- let image1 = create_solid_image(100, 100, 0, 0, 0);- let image2 = create_solid_image(100, 100, 255, 255, 255);-- let diff = compare_histogram(&image1, &image2).unwrap();- assert!(diff > 0.5, "Black vs white should have high histogram diff");-- let same_diff = compare_histogram(&image1, &image1).unwrap();- assert!(same_diff < 0.01, "Same image should have near-zero diff");- }-- #[test]- fn test_compare_ssim_direct() {- let image1 = create_solid_image(100, 100, 0, 0, 0);- let image2 = create_solid_image(100, 100, 255, 255, 255);-- let diff = compare_ssim(&image1, &image2);- assert!(diff > 0.5, "Black vs white should have high SSIM diff");-- let same_diff = compare_ssim(&image1, &image1);- assert!(- same_diff < 0.01,- "Same image should have near-zero SSIM diff"- );- }-- // ============================================================- // Performance characteristic tests- // ============================================================-- #[test]- fn test_stats_tracking() {- let config = FrameComparisonConfig::default();- let mut comparer = FrameComparer::new(config);-- let image1 = create_solid_image(100, 100, 0, 0, 0);- let hash1 = calculate_image_hash(&image1);-- let image2 = create_solid_image(100, 100, 255, 255, 255);- let hash2 = calculate_image_hash(&image2);-- // 5 comparisons: 1 first frame, 2 identical, 2 different- comparer.compare(&image1, hash1); // First- comparer.compare(&image1, hash1); // Hash hit- comparer.compare(&image2, hash2); // Different- comparer.compare(&image2, hash2); // Hash hit- comparer.compare(&image1, hash1); // Different-- let stats = comparer.stats();- assert_eq!(stats.total_comparisons, 5);- assert_eq!(stats.hash_hits, 2);- assert!((stats.hash_hit_rate - 0.4).abs() < 0.01);+ assert_eq!(max_perf.downscale_factor, 6);}}diff --git a/crates/screenpipe-vision/src/utils.rs b/crates/screenpipe-vision/src/utils.rsindex 8e015f1dc..f5542ff41 100644--- a/crates/screenpipe-vision/src/utils.rs+++ b/crates/screenpipe-vision/src/utils.rs@@ -72,12 +72,44 @@ pub fn compare_images_ssim(image1: &DynamicImage, image2: &DynamicImage) -> f64result.score}+/// Capture only the monitor screenshot (no window capture, no hash).+/// Window capture is deferred until after frame comparison to avoid+/// expensive work on frames that will be skipped.+pub async fn capture_monitor_image(+ monitor: &SafeMonitor,+) -> Result<(DynamicImage, Duration), anyhow::Error> {+ let capture_start = Instant::now();+ let image = monitor.capture_image().await.map_err(|e| {+ debug!("failed to capture monitor image: {}", e);+ anyhow::anyhow!("monitor capture failed")+ })?;+ let capture_duration = capture_start.elapsed();+ Ok((image, capture_duration))+}++/// Capture all visible windows on a monitor (called only when frame changed).+pub async fn capture_windows(+ monitor: &SafeMonitor,+ window_filters: &WindowFilters,+ capture_unfocused_windows: bool,+) -> Vec<CapturedWindow> {+ match capture_all_visible_windows(monitor, window_filters, capture_unfocused_windows).await {+ Ok(images) => images,+ Err(e) => {+ warn!(+ "Failed to capture window images: {}. Continuing with empty result.",+ e+ );+ Vec::new()+ }+ }+}+pub async fn capture_screenshot(monitor: &SafeMonitor,window_filters: &WindowFilters,capture_unfocused_windows: bool,) -> Result<(DynamicImage, Vec<CapturedWindow>, u64, Duration), anyhow::Error> {- // info!("Starting screenshot capture for monitor: {:?}", monitor);let capture_start = Instant::now();let image = monitor.capture_image().await.map_err(|e| {debug!("failed to capture monitor image: {}", e);