1
0
Fork 0
forked from wry/wry

damage: add damage queue

This commit is contained in:
Julian Orth 2024-09-07 16:54:25 +02:00
parent 9f98603121
commit 92f7cb56fd
2 changed files with 63 additions and 8 deletions

View file

@ -3,6 +3,8 @@ mod region;
#[cfg(test)]
mod tests;
#[expect(unused_imports)]
pub use region::DamageQueue;
pub use region::RegionBuilder;
use {
jay_algorithms::rect::RectRaw,
@ -16,7 +18,7 @@ pub struct Rect {
raw: RectRaw,
}
#[derive(Clone, Eq, PartialEq, Debug)]
#[derive(Clone, Eq, PartialEq, Debug, Default)]
pub struct Region {
rects: SmallVec<[RectRaw; 1]>,
extents: Rect,

View file

@ -1,11 +1,17 @@
use {
crate::rect::{Rect, Region},
crate::{
rect::{Rect, Region},
utils::{
array,
ptr_ext::{MutPtrExt, PtrExt},
},
},
jay_algorithms::rect::{
region::{extents, rects_to_bands, subtract, union},
RectRaw,
},
smallvec::SmallVec,
std::{mem, ops::Deref, rc::Rc},
std::{cell::UnsafeCell, mem, ops::Deref, rc::Rc},
};
thread_local! {
@ -18,12 +24,16 @@ thread_local! {
impl Region {
pub fn new(rect: Rect) -> Rc<Self> {
Rc::new(Self::new2(rect))
}
pub fn new2(rect: Rect) -> Self {
let mut rects = SmallVec::new();
rects.push(rect.raw);
Rc::new(Self {
Self {
rects,
extents: rect,
})
}
}
pub fn empty() -> Rc<Self> {
@ -34,16 +44,23 @@ impl Region {
if rects.is_empty() {
return Self::empty();
}
Rc::new(Self::from_rects2(rects))
}
pub fn from_rects2(rects: &[Rect]) -> Self {
if rects.is_empty() {
return Self::default();
}
if rects.len() == 1 {
return Self::new(rects[0]);
return Self::new2(rects[0]);
}
let rects = rects_to_bands(unsafe { mem::transmute::<&[Rect], &[RectRaw]>(rects) });
Rc::new(Self {
Self {
extents: Rect {
raw: extents(&rects),
},
rects,
})
}
}
pub fn union(self: &Rc<Self>, other: &Rc<Self>) -> Rc<Self> {
@ -173,3 +190,39 @@ impl RegionBuilder {
self.pending.clear();
}
}
pub struct DamageQueue {
this: usize,
datas: Rc<UnsafeCell<Vec<Vec<Rect>>>>,
}
impl DamageQueue {
#[expect(dead_code)]
pub fn new<const N: usize>() -> [DamageQueue; N] {
let datas = Rc::new(UnsafeCell::new(vec![vec!(); N]));
array::from_fn(|this| DamageQueue {
this,
datas: datas.clone(),
})
}
#[expect(dead_code)]
pub fn damage(&self, rects: &[Rect]) {
let datas = unsafe { self.datas.get().deref_mut() };
for data in datas {
data.extend(rects);
}
}
#[expect(dead_code)]
pub fn clear(&self) {
let data = unsafe { &mut self.datas.get().deref_mut()[self.this] };
data.clear();
}
#[expect(dead_code)]
pub fn get(&self) -> Region {
let data = unsafe { &self.datas.get().deref()[self.this] };
Region::from_rects2(data)
}
}