Compare commits

..

No commits in common. "a905c98823613222663866872982382ba5284e06" and "aa7430c54ef0de1e61e6c0d633fca9297e701110" have entirely different histories.

6 changed files with 175 additions and 78 deletions

4
.vscode/tasks.json vendored
View File

@ -11,7 +11,7 @@
// see https://code.visualstudio.com/docs/editor/tasks#_can-a-background-task-be-used-as-a-prelaunchtask-in-launchjson // see https://code.visualstudio.com/docs/editor/tasks#_can-a-background-task-be-used-as-a-prelaunchtask-in-launchjson
"isBackground": true, "isBackground": true,
// change this to your `beforeDevCommand`: // change this to your `beforeDevCommand`:
"command": "pnpm", "command": "yarn",
"args": [ "args": [
"dev" "dev"
] ]
@ -20,7 +20,7 @@
"label": "ui:build", "label": "ui:build",
"type": "shell", "type": "shell",
// change this to your `beforeBuildCommand`: // change this to your `beforeBuildCommand`:
"command": "pnpm", "command": "yarn",
"args": [ "args": [
"build" "build"
] ]

24
src-tauri/Cargo.lock generated
View File

@ -770,7 +770,7 @@ checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.15", "syn 2.0.14",
] ]
[[package]] [[package]]
@ -1916,7 +1916,7 @@ dependencies = [
"base64 0.21.0", "base64 0.21.0",
"indexmap", "indexmap",
"line-wrap", "line-wrap",
"quick-xml 0.28.2", "quick-xml 0.28.1",
"serde", "serde",
"time", "time",
] ]
@ -2006,9 +2006,9 @@ dependencies = [
[[package]] [[package]]
name = "quick-xml" name = "quick-xml"
version = "0.28.2" version = "0.28.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ce5e73202a820a31f8a0ee32ada5e21029c81fd9e3ebf668a40832e4219d9d1" checksum = "e5c1a97b1bc42b1d550bfb48d4262153fe400a12bab1511821736f7eac76d7e2"
dependencies = [ dependencies = [
"memchr", "memchr",
] ]
@ -2272,14 +2272,14 @@ checksum = "291a097c63d8497e00160b166a967a4a79c64f3facdd01cbd7502231688d77df"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.15", "syn 2.0.14",
] ]
[[package]] [[package]]
name = "serde_json" name = "serde_json"
version = "1.0.96" version = "1.0.95"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "057d394a50403bcac12672b2b18fb387ab6d289d957dab67dd201875391e52f1" checksum = "d721eca97ac802aa7777b701877c8004d950fc142651367300d21c1cc0194744"
dependencies = [ dependencies = [
"itoa 1.0.6", "itoa 1.0.6",
"ryu", "ryu",
@ -2294,7 +2294,7 @@ checksum = "bcec881020c684085e55a25f7fd888954d56609ef363479dc5a1305eb0d40cab"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.15", "syn 2.0.14",
] ]
[[package]] [[package]]
@ -2514,9 +2514,9 @@ dependencies = [
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.15" version = "2.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a34fcf3e8b60f57e6a14301a2e916d323af98b0ea63c599441eec8558660c822" checksum = "fcf316d5356ed6847742d036f8a39c3b8435cac10bd528a4bd461928a6ab34d5"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -2859,7 +2859,7 @@ checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.15", "syn 2.0.14",
] ]
[[package]] [[package]]
@ -2941,7 +2941,7 @@ checksum = "61a573bdc87985e9d6ddeed1b3d864e8a302c847e40d647746df2f1de209d1ce"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.15", "syn 2.0.14",
] ]
[[package]] [[package]]

View File

@ -23,6 +23,8 @@ pub struct LedColorsPublisher {
sorted_colors_tx: Arc<RwLock<watch::Sender<Vec<u8>>>>, sorted_colors_tx: Arc<RwLock<watch::Sender<Vec<u8>>>>,
colors_rx: Arc<RwLock<watch::Receiver<Vec<u8>>>>, colors_rx: Arc<RwLock<watch::Receiver<Vec<u8>>>>,
colors_tx: Arc<RwLock<watch::Sender<Vec<u8>>>>, colors_tx: Arc<RwLock<watch::Sender<Vec<u8>>>>,
display_colors_rx: Arc<RwLock<broadcast::Receiver<(u32, Vec<u8>)>>>,
display_colors_tx: Arc<RwLock<broadcast::Sender<(u32, Vec<u8>)>>>,
inner_tasks_version: Arc<RwLock<usize>>, inner_tasks_version: Arc<RwLock<usize>>,
} }
@ -33,6 +35,7 @@ impl LedColorsPublisher {
let (sorted_tx, sorted_rx) = watch::channel(Vec::new()); let (sorted_tx, sorted_rx) = watch::channel(Vec::new());
let (tx, rx) = watch::channel(Vec::new()); let (tx, rx) = watch::channel(Vec::new());
let (display_colors_tx, display_colors_rx) = broadcast::channel(8);
LED_COLORS_PUBLISHER_GLOBAL LED_COLORS_PUBLISHER_GLOBAL
.get_or_init(|| async { .get_or_init(|| async {
@ -41,6 +44,8 @@ impl LedColorsPublisher {
sorted_colors_tx: Arc::new(RwLock::new(sorted_tx)), sorted_colors_tx: Arc::new(RwLock::new(sorted_tx)),
colors_rx: Arc::new(RwLock::new(rx)), colors_rx: Arc::new(RwLock::new(rx)),
colors_tx: Arc::new(RwLock::new(tx)), colors_tx: Arc::new(RwLock::new(tx)),
display_colors_rx: Arc::new(RwLock::new(display_colors_rx)),
display_colors_tx: Arc::new(RwLock::new(display_colors_tx)),
inner_tasks_version: Arc::new(RwLock::new(0)), inner_tasks_version: Arc::new(RwLock::new(0)),
} }
}) })
@ -51,11 +56,13 @@ impl LedColorsPublisher {
&self, &self,
display_id: u32, display_id: u32,
sample_points: Vec<Vec<LedSamplePoints>>, sample_points: Vec<Vec<LedSamplePoints>>,
display_colors_tx: broadcast::Sender<(u32, Vec<u8>)>,
) { ) {
let display_colors_tx = self.display_colors_tx.clone();
let internal_tasks_version = self.inner_tasks_version.clone(); let internal_tasks_version = self.inner_tasks_version.clone();
tokio::spawn(async move { tokio::spawn(async move {
let display_colors_tx = display_colors_tx.read().await.clone();
let colors = screenshot_manager::get_display_colors(display_id, &sample_points); let colors = screenshot_manager::get_display_colors(display_id, &sample_points);
if let Err(err) = colors { if let Err(err) = colors {
@ -71,9 +78,7 @@ impl LedColorsPublisher {
interval.tick().await; interval.tick().await;
tokio::time::sleep(Duration::from_millis(1)).await; tokio::time::sleep(Duration::from_millis(1)).await;
let version = internal_tasks_version.read().await.clone(); if internal_tasks_version.read().await.clone() != init_version {
if version != init_version {
log::info!( log::info!(
"inner task version changed, stop. {} != {}", "inner task version changed, stop. {} != {}",
internal_tasks_version.read().await.clone(), internal_tasks_version.read().await.clone(),
@ -116,13 +121,14 @@ impl LedColorsPublisher {
}); });
} }
fn start_all_colors_worker(&self, display_ids: Vec<u32>, mappers: Vec<SamplePointMapper>, mut display_colors_rx: broadcast::Receiver<(u32, Vec<u8>)>) { fn start_all_colors_worker(&self, display_ids: Vec<u32>, mappers: Vec<SamplePointMapper>) {
let sorted_colors_tx = self.sorted_colors_tx.clone(); let sorted_colors_tx = self.sorted_colors_tx.clone();
let colors_tx = self.colors_tx.clone(); let colors_tx = self.colors_tx.clone();
log::debug!("start all_colors_worker"); let display_colors_rx = self.display_colors_rx.clone();
tokio::spawn(async move { tokio::spawn(async move {
for _ in 0..10 { for _ in 0..10 {
let mut rx = display_colors_rx.read().await.resubscribe();
let sorted_colors_tx = sorted_colors_tx.write().await; let sorted_colors_tx = sorted_colors_tx.write().await;
let colors_tx = colors_tx.write().await; let colors_tx = colors_tx.write().await;
@ -130,14 +136,15 @@ impl LedColorsPublisher {
let mut all_colors: Vec<Option<Vec<u8>>> = vec![None; display_ids.len()]; let mut all_colors: Vec<Option<Vec<u8>>> = vec![None; display_ids.len()];
let mut start: tokio::time::Instant = tokio::time::Instant::now(); let mut start: tokio::time::Instant = tokio::time::Instant::now();
log::debug!("start all_colors_worker task"); log::info!("start all_colors_worker");
loop { loop {
let color_info = display_colors_rx.recv().await; // log::info!("display_colors_rx changed");
let color_info = rx.recv().await;
if let Err(err) = color_info { if let Err(err) = color_info {
match err { match err {
broadcast::error::RecvError::Closed => { broadcast::error::RecvError::Closed => {
return; break;
} }
broadcast::error::RecvError::Lagged(_) => { broadcast::error::RecvError::Lagged(_) => {
warn!("display_colors_rx lagged"); warn!("display_colors_rx lagged");
@ -193,6 +200,9 @@ impl LedColorsPublisher {
tokio::spawn(async move { tokio::spawn(async move {
let publisher = Self::global().await; let publisher = Self::global().await;
let mut inner_tasks_version = inner_tasks_version.write().await;
*inner_tasks_version = inner_tasks_version.overflowing_add(1).0;
let config_manager = ConfigManager::global().await; let config_manager = ConfigManager::global().await;
let mut config_receiver = config_manager.clone_config_update_receiver(); let mut config_receiver = config_manager.clone_config_update_receiver();
@ -211,34 +221,113 @@ impl LedColorsPublisher {
let configs = configs.unwrap(); let configs = configs.unwrap();
let mut inner_tasks_version = inner_tasks_version.write().await;
*inner_tasks_version = inner_tasks_version.overflowing_add(1).0;
drop(inner_tasks_version);
let (display_colors_tx, display_colors_rx) = broadcast::channel::<(u32, Vec<u8>)>(8);
for sample_point_group in configs.sample_point_groups.clone() { for sample_point_group in configs.sample_point_groups.clone() {
let display_id = sample_point_group.display_id; let display_id = sample_point_group.display_id;
let sample_points = sample_point_group.points; let sample_points = sample_point_group.points;
publisher.start_one_display_colors_fetcher( publisher.start_one_display_colors_fetcher(display_id, sample_points);
display_id,
sample_points,
display_colors_tx.clone(),
);
} }
let display_ids = configs.sample_point_groups; let display_ids = configs.sample_point_groups;
publisher.start_all_colors_worker( publisher.start_all_colors_worker(
display_ids.iter().map(|c| c.display_id).collect(), display_ids.iter().map(|c| c.display_id).collect(),
configs.mappers, configs.mappers,
display_colors_rx,
); );
break;
} }
}); });
// tokio::spawn(async move {
// loop {
// let sorted_colors_tx = sorted_colors_tx.write().await;
// let colors_tx = colors_tx.write().await;
// let screenshot_manager = ScreenshotManager::global().await;
// let config_manager = ConfigManager::global().await;
// let config_receiver = config_manager.clone_config_update_receiver();
// let configs = config_receiver.borrow().clone();
// let configs = Self::get_colors_configs(&configs).await;
// if let Err(err) = configs {
// warn!("Failed to get configs: {}", err);
// sleep(Duration::from_millis(100)).await;
// continue;
// }
// let configs = configs.unwrap();
// let mut merged_screenshot_receiver =
// screenshot_manager.clone_merged_screenshot_rx().await;
// let mut screenshots = HashMap::new();
// // let mut start = tokio::time::Instant::now();
// loop {
// let screenshot = merged_screenshot_receiver.recv().await;
// if let Err(err) = screenshot {
// match err {
// tokio::sync::broadcast::error::RecvError::Closed => {
// warn!("closed");
// continue;
// }
// tokio::sync::broadcast::error::RecvError::Lagged(_) => {
// warn!("lagged");
// continue;
// }
// }
// }
// let screenshot = screenshot.unwrap();
// // log::info!("got screenshot: {:?}", screenshot.display_id);
// screenshots.insert(screenshot.display_id, screenshot);
// if screenshots.len() == configs.sample_point_groups.len() {
// // log::info!("{}", start.elapsed().as_millis().to_string());
// {
// let screenshots = configs
// .sample_point_groups
// .iter()
// .map(|strip| screenshots.get(&strip.display_id).unwrap())
// .collect::<Vec<_>>();
// let colors = screenshot_manager
// .get_all_colors(&configs.sample_point_groups, &screenshots)
// .await;
// let sorted_colors =
// ScreenshotManager::get_sorted_colors(&colors, &configs.mappers)
// .await;
// match colors_tx.send(colors) {
// Ok(_) => {
// // log::info!("colors updated");
// }
// Err(_) => {
// warn!("colors update failed");
// }
// }
// match sorted_colors_tx.send(sorted_colors) {
// Ok(_) => {
// // log::info!("colors updated");
// }
// Err(_) => {
// warn!("colors update failed");
// }
// }
// }
// // screenshots.clear();
// // start = tokio::time::Instant::now();
// }
// }
// }
// });
let rx = self.sorted_colors_rx.clone(); let rx = self.sorted_colors_rx.clone();
tokio::spawn(async move { tokio::spawn(async move {
let mut rx = rx.read().await.clone(); let mut rx = rx.read().await.clone();
@ -291,7 +380,6 @@ impl LedColorsPublisher {
let mut colors_configs = Vec::new(); let mut colors_configs = Vec::new();
let mut merged_screenshot_receiver = screenshot_manager.clone_merged_screenshot_rx().await; let mut merged_screenshot_receiver = screenshot_manager.clone_merged_screenshot_rx().await;
merged_screenshot_receiver.resubscribe();
let mut screenshots = HashMap::new(); let mut screenshots = HashMap::new();
@ -343,11 +431,10 @@ impl LedColorsPublisher {
colors_configs.push(colors_config); colors_configs.push(colors_config);
} }
log::debug!("got all colors configs: {:?}", colors_configs.len());
return Ok(AllColorConfig { return Ok(AllColorConfig {
sample_point_groups: colors_configs, sample_point_groups: colors_configs,
mappers, mappers,
// screenshot_receivers: local_rx_list,
}); });
} }
} }

View File

@ -1,9 +1,9 @@
use std::iter; use std::cell::RefCell;
use std::{iter, cell::Ref};
use std::sync::Arc; use std::sync::Arc;
use core_graphics::display::CGDisplay;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tauri::async_runtime::RwLock; use tauri::async_runtime::{RwLock, Mutex};
use crate::{ambient_light::LedStripConfig, led_color::LedColor}; use crate::{ambient_light::LedStripConfig, led_color::LedColor};
@ -17,7 +17,7 @@ pub struct Screenshot {
pub scale_factor: f32, pub scale_factor: f32,
} }
static SINGLE_AXIS_POINTS: usize = 1; static SINGLE_AXIS_POINTS: usize = 5;
impl Screenshot { impl Screenshot {
pub fn new( pub fn new(
@ -39,20 +39,15 @@ impl Screenshot {
} }
pub fn get_sample_points(&self, config: &LedStripConfig) -> Vec<LedSamplePoints> { pub fn get_sample_points(&self, config: &LedStripConfig) -> Vec<LedSamplePoints> {
let height = CGDisplay::new(self.display_id).bounds().size.height as usize; let height = self.height as usize;
let width = CGDisplay::new(self.display_id).bounds().size.width as usize; let width = self.width as usize;
match config.border { match config.border {
crate::ambient_light::Border::Top => { crate::ambient_light::Border::Top => {
Self::get_one_edge_sample_points(height / 18, width, config.len, SINGLE_AXIS_POINTS) Self::get_one_edge_sample_points(height / 18, width, config.len, SINGLE_AXIS_POINTS)
} }
crate::ambient_light::Border::Bottom => { crate::ambient_light::Border::Bottom => {
let points = Self::get_one_edge_sample_points( let points = Self::get_one_edge_sample_points(height / 18, width, config.len, SINGLE_AXIS_POINTS);
height / 18,
width,
config.len,
SINGLE_AXIS_POINTS,
);
points points
.into_iter() .into_iter()
.map(|groups| -> Vec<Point> { .map(|groups| -> Vec<Point> {
@ -61,12 +56,7 @@ impl Screenshot {
.collect() .collect()
} }
crate::ambient_light::Border::Left => { crate::ambient_light::Border::Left => {
let points = Self::get_one_edge_sample_points( let points = Self::get_one_edge_sample_points(width / 32, height, config.len, SINGLE_AXIS_POINTS);
width / 32,
height,
config.len,
SINGLE_AXIS_POINTS,
);
points points
.into_iter() .into_iter()
.map(|groups| -> Vec<Point> { .map(|groups| -> Vec<Point> {
@ -75,12 +65,7 @@ impl Screenshot {
.collect() .collect()
} }
crate::ambient_light::Border::Right => { crate::ambient_light::Border::Right => {
let points = Self::get_one_edge_sample_points( let points = Self::get_one_edge_sample_points(width / 32, height, config.len, SINGLE_AXIS_POINTS);
width / 32,
height,
config.len,
SINGLE_AXIS_POINTS,
);
points points
.into_iter() .into_iter()
.map(|groups| -> Vec<Point> { .map(|groups| -> Vec<Point> {

View File

@ -1,3 +1,4 @@
use std::cell::{Ref, RefCell};
use std::{collections::HashMap, sync::Arc}; use std::{collections::HashMap, sync::Arc};
use core_graphics::display::{ use core_graphics::display::{
@ -13,7 +14,7 @@ use crate::screenshot::LedSamplePoints;
use crate::{ use crate::{
ambient_light::{SamplePointConfig, SamplePointMapper}, ambient_light::{SamplePointConfig, SamplePointMapper},
led_color::LedColor, led_color::LedColor,
screenshot::Screenshot, screenshot::{ScreenSamplePoints, Screenshot},
}; };
pub fn take_screenshot(display_id: u32, scale_factor: f32) -> anyhow::Result<Screenshot> { pub fn take_screenshot(display_id: u32, scale_factor: f32) -> anyhow::Result<Screenshot> {
@ -76,15 +77,6 @@ pub fn get_display_colors(
height: (end_y - start_y + 1) as f64, height: (end_y - start_y + 1) as f64,
}; };
// log::info!(
// "origin: {:?}, size: {:?}, start_x: {}, start_y: {}, bounds: {:?}",
// origin,
// size,
// start_x,
// start_y,
// cg_display.bounds().size
// );
let cg_image = CGDisplay::screenshot( let cg_image = CGDisplay::screenshot(
CGRect::new(&origin, &size), CGRect::new(&origin, &size),
kCGWindowListOptionOnScreenOnly, kCGWindowListOptionOnScreenOnly,
@ -159,7 +151,7 @@ impl ScreenshotManager {
warn!("take_screenshot_loop: {}", screenshot.err().unwrap()); warn!("take_screenshot_loop: {}", screenshot.err().unwrap());
return; return;
} }
let mut interval = time::interval(Duration::from_millis(1000)); let mut interval = time::interval(Duration::from_millis(3300));
let mut start = tokio::time::Instant::now(); let mut start = tokio::time::Instant::now();
let screenshot = screenshot.unwrap(); let screenshot = screenshot.unwrap();
@ -198,9 +190,7 @@ impl ScreenshotManager {
let screenshot = take_screenshot(display_id, scale_factor); let screenshot = take_screenshot(display_id, scale_factor);
if let Ok(screenshot) = screenshot { if let Ok(screenshot) = screenshot {
match merged_screenshot_tx.send(screenshot.clone()) { match merged_screenshot_tx.send(screenshot.clone()) {
Ok(_) => { Ok(_) => {}
log::info!("take_screenshot_loop: merged_screenshot_tx.send success. display#{}", display_id);
}
Err(err) => { Err(err) => {
// warn!("take_screenshot_loop: merged_screenshot_tx.send failed. display#{}. err: {}", display_id, err); // warn!("take_screenshot_loop: merged_screenshot_tx.send failed. display#{}. err: {}", display_id, err);
} }
@ -212,6 +202,23 @@ impl ScreenshotManager {
} }
} }
pub async fn get_all_colors(
&self,
configs: &Vec<SamplePointConfig>,
screenshots: &Vec<&Screenshot>,
) -> Vec<LedColor> {
let mut all_colors = vec![];
for (index, screenshot) in screenshots.iter().enumerate() {
let config = &configs[index];
let mut colors = screenshot.get_colors_by_sample_points(&config.points).await;
all_colors.append(&mut colors);
}
all_colors
}
pub fn get_sorted_colors(colors: &Vec<u8>, mappers: &Vec<SamplePointMapper>) -> Vec<u8> { pub fn get_sorted_colors(colors: &Vec<u8>, mappers: &Vec<SamplePointMapper>) -> Vec<u8> {
let total_leds = mappers let total_leds = mappers
.iter() .iter()

View File

@ -24,6 +24,12 @@ type PixelProps = {
color: string; color: string;
}; };
async function subscribeScreenshotUpdate(displayId: number) {
await invoke('subscribe_encoded_screenshot_updated', {
displayId,
});
}
export const Pixel: Component<PixelProps> = (props) => { export const Pixel: Component<PixelProps> = (props) => {
const style = createMemo(() => ({ const style = createMemo(() => ({
background: props.color, background: props.color,
@ -45,6 +51,7 @@ export const LedStripPart: Component<LedStripPartProps> = (props) => {
const [localProps, rootProps] = splitProps(props, ['config']); const [localProps, rootProps] = splitProps(props, ['config']);
const [stripConfiguration] = useContext(LedStripConfigurationContext); const [stripConfiguration] = useContext(LedStripConfigurationContext);
const [ledSamplePoints, setLedSamplePoints] = createSignal();
const [colors, setColors] = createSignal<string[]>([]); const [colors, setColors] = createSignal<string[]>([]);
// update led strip colors from global store // update led strip colors from global store
@ -80,6 +87,17 @@ export const LedStripPart: Component<LedStripPartProps> = (props) => {
setColors(colors); setColors(colors);
}); });
// get led strip sample points
createEffect(() => {
if (localProps.config) {
invoke('get_led_strips_sample_points', {
config: localProps.config,
}).then((points) => {
setLedSamplePoints(points);
});
}
});
const [anchor, setAnchor] = createSignal<HTMLElement>(); const [anchor, setAnchor] = createSignal<HTMLElement>();
useTippy(anchor, { useTippy(anchor, {