加快小壹交替读取同一画布的整屏预览
This commit is contained in:
parent
eff36efe1b
commit
29f58d2b43
@ -1,6 +1,7 @@
|
||||
use photocraft_doc::Document;
|
||||
use photocraft_geom::Rect;
|
||||
use serde_json::Value;
|
||||
use std::collections::VecDeque;
|
||||
use std::sync::{Arc, Mutex, Weak};
|
||||
|
||||
use super::{R, read_production_screen_with};
|
||||
@ -12,26 +13,59 @@ struct Preview {
|
||||
jpeg: Arc<Vec<u8>>,
|
||||
}
|
||||
|
||||
/// Reuse the last screen image during a Pi run, keyed by the immutable canvas snapshot.
|
||||
// read_output_screen can request up to four pages at once.
|
||||
const SCREEN_CACHE_CAPACITY: usize = 4;
|
||||
|
||||
/// Reuse recent screen images during a Pi run, keyed by the immutable canvas snapshot.
|
||||
#[derive(Default)]
|
||||
pub(in crate::jiansun_backend) struct ScreenReader(Mutex<Option<Preview>>);
|
||||
pub(in crate::jiansun_backend) struct ScreenReader(Mutex<VecDeque<Preview>>);
|
||||
|
||||
impl ScreenReader {
|
||||
pub(in crate::jiansun_backend) fn read(&self, native: &Arc<Document>, input: &Value) -> R<Value> {
|
||||
read_production_screen_with(native, input, |area| {
|
||||
let document = Arc::downgrade(native);
|
||||
let text_gamma = photocraft_compose::psblend::text_gamma().to_bits();
|
||||
{
|
||||
let last = self.0.lock().map_err(|_| "整屏读取缓存状态失效")?;
|
||||
if let Some(last) = last.as_ref().filter(|last| last.area == area && last.text_gamma == text_gamma && last.document.ptr_eq(&document)) {
|
||||
return Ok(last.jpeg.clone());
|
||||
}
|
||||
}
|
||||
let jpeg = Arc::new(render(native, area)?);
|
||||
*self.0.lock().map_err(|_| "整屏读取缓存状态失效")? = Some(Preview { document, area, text_gamma, jpeg: jpeg.clone() });
|
||||
Ok(jpeg)
|
||||
})
|
||||
read_production_screen_with(native, input, |area| self.jpeg(native, area))
|
||||
}
|
||||
|
||||
fn jpeg(&self, native: &Arc<Document>, area: Rect) -> R<Arc<Vec<u8>>> {
|
||||
let document = Arc::downgrade(native);
|
||||
let text_gamma = photocraft_compose::psblend::text_gamma().to_bits();
|
||||
let same_screen = |entry: &Preview| {
|
||||
entry.area == area && entry.text_gamma == text_gamma && entry.document.ptr_eq(&document)
|
||||
};
|
||||
{
|
||||
let mut cache = self.0.lock().map_err(|_| "整屏读取缓存状态失效")?;
|
||||
if let Some(index) = cache.iter().position(same_screen) {
|
||||
let entry = cache.remove(index).unwrap();
|
||||
let jpeg = entry.jpeg.clone();
|
||||
cache.push_back(entry);
|
||||
return Ok(jpeg);
|
||||
}
|
||||
}
|
||||
let jpeg = Arc::new(render(native, area)?);
|
||||
let mut cache = self.0.lock().map_err(|_| "整屏读取缓存状态失效")?;
|
||||
cache.retain(|entry| !same_screen(entry));
|
||||
if cache.len() == SCREEN_CACHE_CAPACITY {
|
||||
cache.pop_front();
|
||||
}
|
||||
cache.push_back(Preview { document, area, text_gamma, jpeg: jpeg.clone() });
|
||||
Ok(jpeg)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn jiansun_screen_cache_reuses_alternating_pages_and_evicts_oldest() {
|
||||
use photocraft_color::{ColorMode, SampleType};
|
||||
use photocraft_doc::Size;
|
||||
let doc = Arc::new(Document::new("test", Size::new(16, 48), ColorMode::Rgb, SampleType::U8));
|
||||
let reader = ScreenReader::default();
|
||||
let area = |page| Rect::from_xywh(0, 8 * page, 16, 8);
|
||||
let first = reader.jpeg(&doc, area(0)).unwrap();
|
||||
let second = reader.jpeg(&doc, area(1)).unwrap();
|
||||
assert!(Arc::ptr_eq(&first, &reader.jpeg(&doc, area(0)).unwrap()));
|
||||
for page in 2..5 {
|
||||
reader.jpeg(&doc, area(page)).unwrap();
|
||||
}
|
||||
assert!(Arc::ptr_eq(&first, &reader.jpeg(&doc, area(0)).unwrap()));
|
||||
assert!(!Arc::ptr_eq(&second, &reader.jpeg(&doc, area(1)).unwrap()));
|
||||
}
|
||||
|
||||
pub(super) fn render(native: &Document, area: Rect) -> R<Vec<u8>> {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user