feat: 截屏改为抽样,提升性能。

This commit is contained in:
Ivan Li 2022-12-01 20:15:05 +08:00
parent 479fdba9f6
commit a6e91dfae1
6 changed files with 47 additions and 19 deletions

7
src-tauri/Cargo.lock generated
View File

@ -573,6 +573,7 @@ dependencies = [
"base64", "base64",
"bmp", "bmp",
"color_space", "color_space",
"either",
"futures", "futures",
"hex", "hex",
"once_cell", "once_cell",
@ -611,6 +612,12 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0bd4b30a6560bbd9b4620f4de34c3f14f60848e58a9b7216801afcb4c7b31c3c" checksum = "0bd4b30a6560bbd9b4620f4de34c3f14f60848e58a9b7216801afcb4c7b31c3c"
[[package]]
name = "either"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
[[package]] [[package]]
name = "embed_plist" name = "embed_plist"
version = "1.2.2" version = "1.2.2"

View File

@ -32,6 +32,7 @@ rumqttc = "0.17.0"
time = { version = "0.3.17", features = ["formatting"] } time = { version = "0.3.17", features = ["formatting"] }
color_space = "0.5.3" color_space = "0.5.3"
futures = "0.3.25" futures = "0.3.25"
either = "1.8.0"
[features] [features]
# by default Tauri runs in production mode # by default Tauri runs in production mode

View File

@ -120,13 +120,15 @@ impl CoreManager {
let start_at = Instant::now(); let start_at = Instant::now();
match screenshot.get_top_colors().await { match screenshot.get_top_colors().await {
Ok(colors) => { Ok(colors) => {
let start = screenshot.get_top_of_led_strip_range().min().unwrap_or(0); let start = screenshot.get_top_of_led_start_at().min(screenshot.get_top_of_led_end_at());
let colors_len = colors.len(); let colors_len = colors.len();
for (index, color) in colors.into_iter().enumerate() { for (index, color) in colors.into_iter().enumerate() {
global_colors.insert(index + start, color); global_colors.insert(index + start, color);
} }
info!("led count: {}, spend: {:?}", global_colors.len(), start_at.elapsed());
if global_colors.len() == 60 { if global_colors.len() == 60 {
let mut colors = vec![]; let mut colors = vec![];
for index in 0..global_colors.len() { for index in 0..global_colors.len() {
@ -165,6 +167,7 @@ impl CoreManager {
tx: mpsc::Sender<Screenshot>, tx: mpsc::Sender<Screenshot>,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let mut picker = DisplayPicker::from_config(config)?; let mut picker = DisplayPicker::from_config(config)?;
info!("width: {}", picker.config.display_width);
loop { loop {
let start = Instant::now(); let start = Instant::now();
@ -173,7 +176,7 @@ impl CoreManager {
if let AmbientLightMode::Follow = *lock { if let AmbientLightMode::Follow = *lock {
drop(lock); drop(lock);
let screenshot = picker.take_screenshot()?; let screenshot = picker.take_screenshot()?;
// info!("Take Screenshot Spend: {:?}", start.elapsed()); info!("Take Screenshot Spend: {:?}", start.elapsed());
tx.send(screenshot).await; tx.send(screenshot).await;
} else { } else {
break; break;

View File

@ -35,8 +35,8 @@ impl Picker {
display_height: 1200, display_height: 1200,
top_led_strip: LedStripConfig { top_led_strip: LedStripConfig {
index: 1, index: 1,
global_start_position: 32, global_start_position: 59,
global_end_position: 60, global_end_position: 32,
}, },
bottom_led_strip: LedStripConfig { bottom_led_strip: LedStripConfig {
index: 0, index: 0,
@ -60,8 +60,8 @@ impl Picker {
display_height: 1692, display_height: 1692,
top_led_strip: LedStripConfig { top_led_strip: LedStripConfig {
index: 0, index: 0,
global_start_position: 0, global_start_position: 31,
global_end_position: 32, global_end_position: 0,
}, },
bottom_led_strip: LedStripConfig { bottom_led_strip: LedStripConfig {
index: 0, index: 0,

View File

@ -1,7 +1,7 @@
use std::ops::Range; use std::ops::Range;
use color_space::{Hsv, Rgb}; use color_space::{Hsv, Rgb};
use either::Either;
use super::{ use super::{
config::{DisplayConfig, LedStripConfig}, config::{DisplayConfig, LedStripConfig},
@ -28,9 +28,11 @@ impl Screenshot {
.await .await
} }
pub fn get_top_of_led_strip_range(&self) -> Range<usize> { pub fn get_top_of_led_start_at(&self) -> usize {
self.config.top_led_strip.global_start_position self.config.top_led_strip.global_start_position
..self.config.top_led_strip.global_end_position }
pub fn get_top_of_led_end_at(&self) -> usize {
self.config.top_led_strip.global_end_position
} }
async fn get_x_colors( async fn get_x_colors(
@ -41,7 +43,11 @@ impl Screenshot {
let bitmap = &self.bitmap; let bitmap = &self.bitmap;
let number_of_leds = strip_config let number_of_leds = strip_config
.global_start_position .global_start_position
.abs_diff(strip_config.global_end_position); .abs_diff(strip_config.global_end_position)
+ 1;
if number_of_leds == 0 {
return Ok(vec![]);
}
let cell_size_x = self.config.display_width / number_of_leds; let cell_size_x = self.config.display_width / number_of_leds;
let cell_size_y = self.config.display_height / 8; let cell_size_y = self.config.display_height / 8;
let cell_size = cell_size_x * cell_size_y; let cell_size = cell_size_x * cell_size_y;
@ -50,32 +56,43 @@ impl Screenshot {
XPosition::Bottom => { XPosition::Bottom => {
self.config.display_height - 20 - cell_size_y..self.config.display_height - 20 self.config.display_height - 20 - cell_size_y..self.config.display_height - 20
} }
}
.step_by(5);
let x_range = if strip_config.global_start_position < strip_config.global_end_position {
Either::Left(strip_config.global_start_position..=strip_config.global_end_position)
} else {
Either::Right(
(strip_config.global_end_position..=strip_config.global_start_position).rev(),
)
}; };
let mut colors = Vec::new(); let mut colors = Vec::new();
let stride = bitmap.len() / self.config.display_height; let stride = bitmap.len() / self.config.display_height;
for pos in strip_config.global_start_position..strip_config.global_end_position { for pos in x_range {
let mut r = 0.0; let mut r = 0.0;
let mut g = 0.0; let mut g = 0.0;
let mut b = 0.0; let mut b = 0.0;
for x in pos * cell_size_x..(pos + 1) * cell_size_x { let mut count = 0;
for x in (pos * cell_size_x..(pos + 1) * cell_size_x).step_by(5) {
for y in y_range.to_owned() { for y in y_range.to_owned() {
let i = stride * y + 4 * x; let i = stride * y + 4 * x;
r += bitmap[i + 2] as f64; r += bitmap[i + 2] as f64;
g += bitmap[i + 1] as f64; g += bitmap[i + 1] as f64;
b += bitmap[i] as f64; b += bitmap[i] as f64;
count+=1;
} }
} }
let rgb = Rgb::new( let rgb = Rgb::new(
r / cell_size as f64, r / count as f64,
g / cell_size as f64, g / count as f64,
b / cell_size as f64, b / count as f64,
); );
let hsv = Hsv::from(rgb); let hsv = Hsv::from(rgb);
// info!("HSV: {:?}", [hsv.h, hsv.s, hsv.v]); // info!("HSV: {:?}", [hsv.h, hsv.s, hsv.v]);
let color = LedColor::from_hsv(hsv.h, hsv.s, hsv.v); let color = LedColor::from_hsv(hsv.h, hsv.s, hsv.v);
// info!("color: {:?}", color.get_rgb()); // paris::info!("color: {:?}", color.get_rgb());
colors.push(color); colors.push(color);
} }
return Ok(colors); return Ok(colors);

View File

@ -3,8 +3,8 @@ import ReactDOM from "react-dom/client";
import App from "./App"; import App from "./App";
import "./style.css"; import "./style.css";
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render( ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<React.StrictMode> <React.StrictMode>
<App /> <App />
</React.StrictMode> </React.StrictMode>,
); );