fix: 降低截图崩溃概率。

This commit is contained in:
Ivan Li 2023-01-16 19:57:04 +08:00
parent 6802dbb7c0
commit cb5fb901f9
3 changed files with 20 additions and 20 deletions

View File

@ -14,10 +14,16 @@ use paris::*;
use picker::config::DisplayConfig; use picker::config::DisplayConfig;
use picker::manager::Picker; use picker::manager::Picker;
use picker::screenshot::ScreenshotDto; use picker::screenshot::ScreenshotDto;
use std::vec; use tauri::async_runtime::Mutex;
use once_cell::sync::OnceCell;
static GET_SCREENSHOT_LOCK: OnceCell<Mutex<bool>> = OnceCell::new();
#[tauri::command] #[tauri::command]
async fn take_snapshot() -> Vec<ScreenshotDto> { async fn take_snapshot() -> Vec<ScreenshotDto> {
info!("Hi?");
let _lock = GET_SCREENSHOT_LOCK.get_or_init(|| Mutex::new(false)).lock().await;
info!("Hi!");
let manager = Picker::global().await; let manager = Picker::global().await;
let start = time::Instant::now(); let start = time::Instant::now();
@ -38,17 +44,17 @@ async fn take_snapshot() -> Vec<ScreenshotDto> {
#[tauri::command] #[tauri::command]
async fn get_screenshot_by_config(config: DisplayConfig) -> Result<ScreenshotDto, String> { async fn get_screenshot_by_config(config: DisplayConfig) -> Result<ScreenshotDto, String> {
info!("Hi"); info!("Hi?");
let manager = Picker::global(); // let _lock = GET_SCREENSHOT_LOCK.get_or_init(|| Mutex::new(false)).lock().await;
// info!("Hi!");
let start = time::Instant::now(); let start = time::Instant::now();
let screenshot_dto = manager.await.get_screenshot_by_config(config).await; let screenshot_dto = Picker::preview_display_by_config(config).await;
info!("截图耗时 {} s", start.elapsed().as_seconds_f32()); info!("截图耗时 {} s", start.elapsed().as_seconds_f32());
match screenshot_dto { match screenshot_dto {
Ok(screenshot_dto) => Ok(screenshot_dto), Ok(screenshot_dto) => Ok(screenshot_dto),
Err(error) => { Err(error) => {
error!("get_screenshot_by_config failed. {}", error); error!("preview_display_by_config failed. {}", error);
Err(format!("get_screenshot_by_config failed. {}", error)) Err(format!("preview_display_by_config failed. {}", error))
} }
} }
} }

View File

@ -84,16 +84,4 @@ impl Picker {
anyhow::Ok(screenshot.to_dto().await) anyhow::Ok(screenshot.to_dto().await)
} }
pub async fn get_screenshot_by_config(
&self,
config: DisplayConfig,
) -> anyhow::Result<ScreenshotDto> {
let start = time::Instant::now();
let mut picker = DisplayPicker::from_config(config)?;
let screenshot = picker.take_screenshot()?;
info!("Take Screenshot Spend: {}", start.elapsed());
anyhow::Ok(screenshot.to_dto().await)
}
} }

View File

@ -1,8 +1,9 @@
use image::ImageBuffer; use image::ImageBuffer;
use image::{ImageOutputFormat, Rgb}; use image::{ImageOutputFormat, Rgb};
use paris::error; use paris::{error, info};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::iter; use std::iter;
use std::time::SystemTime;
use super::{config::DisplayConfig, led_color::LedColor}; use super::{config::DisplayConfig, led_color::LedColor};
@ -265,9 +266,14 @@ impl Screenshot {
} }
pub async fn to_dto(&self) -> ScreenshotDto { pub async fn to_dto(&self) -> ScreenshotDto {
let rk = SystemTime::now();
info!("[{:?} {:p}] to_dto", rk.elapsed(), &self);
let encode_image = self.to_webp_base64().await; let encode_image = self.to_webp_base64().await;
info!("[{:?} {:p}] image", rk.elapsed(), &self);
let config = self.config.clone(); let config = self.config.clone();
info!("[{:?} {:p}] cloned", rk.elapsed(), &self);
let colors = self.get_colors(); let colors = self.get_colors();
info!("[{:?} {:p}] colors", rk.elapsed(), &self);
ScreenshotDto { ScreenshotDto {
encode_image, encode_image,
config, config,