From 782f3bf0292ff2758469d913eed4a102f65b57bb Mon Sep 17 00:00:00 2001 From: Ivan Li Date: Sat, 15 Apr 2023 13:45:30 +0800 Subject: [PATCH] fix: wrong sample points on mac os 13. --- src-tauri/src/ambient_light/publisher.rs | 12 ++++++-- src-tauri/src/screenshot.rs | 37 +++++++++--------------- src-tauri/src/screenshot_manager.rs | 12 ++++++-- 3 files changed, 32 insertions(+), 29 deletions(-) diff --git a/src-tauri/src/ambient_light/publisher.rs b/src-tauri/src/ambient_light/publisher.rs index c443764..2827ad7 100644 --- a/src-tauri/src/ambient_light/publisher.rs +++ b/src-tauri/src/ambient_light/publisher.rs @@ -51,12 +51,13 @@ impl LedColorsPublisher { &self, display_id: u32, sample_points: Vec>, + bound_scale_factor: f32, display_colors_tx: broadcast::Sender<(u32, Vec)>, ) { let internal_tasks_version = self.inner_tasks_version.clone(); tokio::spawn(async move { - let colors = screenshot_manager::get_display_colors(display_id, &sample_points); + let colors = screenshot_manager::get_display_colors(display_id, &sample_points, bound_scale_factor); if let Err(err) = colors { warn!("Failed to get colors: {}", err); @@ -85,7 +86,7 @@ impl LedColorsPublisher { // log::info!("tick: {}ms", start.elapsed().as_millis()); start = tokio::time::Instant::now(); - let colors = screenshot_manager::get_display_colors(display_id, &sample_points); + let colors = screenshot_manager::get_display_colors(display_id, &sample_points, bound_scale_factor); if let Err(err) = colors { warn!("Failed to get colors: {}", err); @@ -222,10 +223,12 @@ impl LedColorsPublisher { for sample_point_group in configs.sample_point_groups.clone() { let display_id = sample_point_group.display_id; let sample_points = sample_point_group.points; + let bound_scale_factor = sample_point_group.bound_scale_factor; publisher.start_one_display_colors_fetcher( display_id, sample_points, + bound_scale_factor, display_colors_tx.clone(), ); } @@ -338,7 +341,9 @@ impl LedColorsPublisher { .map(|config| screenshot.get_sample_points(&config)) .collect(); - let colors_config = DisplaySamplePointGroup { display_id, points }; + let bound_scale_factor = screenshot.bound_scale_factor; + + let colors_config = DisplaySamplePointGroup { display_id, points, bound_scale_factor }; colors_configs.push(colors_config); } @@ -369,4 +374,5 @@ pub struct AllColorConfig { pub struct DisplaySamplePointGroup { pub display_id: u32, pub points: Vec>, + pub bound_scale_factor: f32, } diff --git a/src-tauri/src/screenshot.rs b/src-tauri/src/screenshot.rs index 9db1327..ff8602f 100644 --- a/src-tauri/src/screenshot.rs +++ b/src-tauri/src/screenshot.rs @@ -1,7 +1,7 @@ + use std::iter; use std::sync::Arc; -use core_graphics::display::CGDisplay; use serde::{Deserialize, Serialize}; use tauri::async_runtime::RwLock; @@ -15,9 +15,10 @@ pub struct Screenshot { pub bytes_per_row: usize, pub bytes: Arc>>, pub scale_factor: f32, + pub bound_scale_factor: f32, } -static SINGLE_AXIS_POINTS: usize = 1; +static SINGLE_AXIS_POINTS: usize = 5; impl Screenshot { pub fn new( @@ -27,6 +28,7 @@ impl Screenshot { bytes_per_row: usize, bytes: Vec, scale_factor: f32, + bound_scale_factor: f32, ) -> Self { Self { display_id, @@ -35,24 +37,22 @@ impl Screenshot { bytes_per_row, bytes: Arc::new(RwLock::new(bytes)), scale_factor, + bound_scale_factor, } } pub fn get_sample_points(&self, config: &LedStripConfig) -> Vec { - let height = CGDisplay::new(self.display_id).bounds().size.height as usize; - let width = CGDisplay::new(self.display_id).bounds().size.width as usize; - + let height = self.height as usize; + let width = self.width as usize; + // let height = CGDisplay::new(self.display_id).bounds().size.height as usize; + // let width = CGDisplay::new(self.display_id).bounds().size.width as usize; + match config.border { crate::ambient_light::Border::Top => { Self::get_one_edge_sample_points(height / 18, width, config.len, SINGLE_AXIS_POINTS) } crate::ambient_light::Border::Bottom => { - let points = Self::get_one_edge_sample_points( - height / 18, - width, - config.len, - SINGLE_AXIS_POINTS, - ); + let points = Self::get_one_edge_sample_points(height / 18, width, config.len, SINGLE_AXIS_POINTS); points .into_iter() .map(|groups| -> Vec { @@ -61,12 +61,7 @@ impl Screenshot { .collect() } crate::ambient_light::Border::Left => { - let points = Self::get_one_edge_sample_points( - width / 32, - height, - config.len, - SINGLE_AXIS_POINTS, - ); + let points = Self::get_one_edge_sample_points(width / 32, height, config.len, SINGLE_AXIS_POINTS); points .into_iter() .map(|groups| -> Vec { @@ -75,12 +70,7 @@ impl Screenshot { .collect() } crate::ambient_light::Border::Right => { - let points = Self::get_one_edge_sample_points( - width / 32, - height, - config.len, - SINGLE_AXIS_POINTS, - ); + let points = Self::get_one_edge_sample_points(width / 32, height, config.len, SINGLE_AXIS_POINTS); points .into_iter() .map(|groups| -> Vec { @@ -168,6 +158,7 @@ impl Screenshot { b += bitmap[position] as f64; g += bitmap[position + 1] as f64; r += bitmap[position + 2] as f64; + // log::info!("position: {}, total: {}", position, bitmap.len()); } let color = LedColor::new((r / len) as u8, (g / len) as u8, (b / len) as u8); colors.push(color); diff --git a/src-tauri/src/screenshot_manager.rs b/src-tauri/src/screenshot_manager.rs index 45ee23f..05c7c1d 100644 --- a/src-tauri/src/screenshot_manager.rs +++ b/src-tauri/src/screenshot_manager.rs @@ -11,7 +11,7 @@ use tokio::time::{self, Duration}; use crate::screenshot::LedSamplePoints; use crate::{ - ambient_light::{SamplePointConfig, SamplePointMapper}, + ambient_light::SamplePointMapper, led_color::LedColor, screenshot::Screenshot, }; @@ -36,6 +36,10 @@ pub fn take_screenshot(display_id: u32, scale_factor: f32) -> anyhow::Result anyhow::Result>, + bound_scale_factor: f32, ) -> anyhow::Result> { log::debug!("take_screenshot"); let cg_display = CGDisplay::new(display_id); @@ -68,8 +74,8 @@ pub fn get_display_colors( let (start_y, end_y) = (usize::min(start_y, end_y), usize::max(start_y, end_y)); let origin = CGPoint { - x: start_x as f64 + cg_display.bounds().origin.x, - y: start_y as f64 + cg_display.bounds().origin.y, + x: start_x as f64 * bound_scale_factor as f64 + cg_display.bounds().origin.x, + y: start_y as f64 * bound_scale_factor as f64 + cg_display.bounds().origin.y, }; let size = CGSize { width: (end_x - start_x + 1) as f64,