Skip to content

Commit

Permalink
Arc -> Rc; Mutex -> RefCell
Browse files Browse the repository at this point in the history
  • Loading branch information
LGFae committed Aug 9, 2024
1 parent 1a377a7 commit a74759d
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 219 deletions.
6 changes: 3 additions & 3 deletions common/src/ipc/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,9 +591,9 @@ impl Animation {

pub struct ImageReq {
pub transition: Transition,
pub imgs: Box<[ImgReq]>,
pub outputs: Box<[Box<[MmappedStr]>]>,
pub animations: Option<Box<[Animation]>>,
pub imgs: Vec<ImgReq>,
pub outputs: Vec<Box<[MmappedStr]>>,
pub animations: Option<Vec<Animation>>,
}

fn deserialize_string(bytes: &[u8]) -> String {
Expand Down
18 changes: 9 additions & 9 deletions common/src/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,26 +293,26 @@ impl<const UTF8: bool> Mmapped<UTF8> {
pub fn bytes(&self) -> &[u8] {
unsafe { std::slice::from_raw_parts(self.ptr.as_ptr().cast(), self.len) }
}
}

impl MmappedStr {
#[inline]
#[must_use]
pub fn str(&self) -> &str {
let s = unsafe { std::slice::from_raw_parts(self.ptr.as_ptr().cast(), self.len) };
unsafe { std::str::from_utf8_unchecked(s) }
pub const fn str(&self) -> &str {
if UTF8 {
unsafe {
let slice = std::slice::from_raw_parts(self.ptr.as_ptr().cast(), self.len);
std::str::from_utf8_unchecked(slice)
}
} else {
panic!("trying to use a mmap that is not a utf8 as str")
}
}
}

impl<const UTF8: bool> Drop for Mmapped<UTF8> {
#[inline]
fn drop(&mut self) {
let len = self.len + self.ptr.as_ptr() as usize - self.base_ptr.as_ptr() as usize;
if let Err(e) = unsafe { munmap(self.base_ptr.as_ptr(), len) } {
eprintln!("ERROR WHEN UNMAPPING MEMORY: {e}");
}
}
}

unsafe impl<const UTF8: bool> Send for Mmapped<UTF8> {}
unsafe impl<const UTF8: bool> Sync for Mmapped<UTF8> {}
16 changes: 9 additions & 7 deletions daemon/src/animations/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use log::error;

use std::{
sync::Arc,
cell::RefCell,
rc::Rc,
time::{Duration, Instant},
};

Expand All @@ -17,7 +18,7 @@ mod transitions;
use transitions::Effect;

pub struct TransitionAnimator {
pub wallpapers: Vec<Arc<Wallpaper>>,
pub wallpapers: Vec<Rc<RefCell<Wallpaper>>>,
fps: Duration,
effect: Effect,
img: MmappedBytes,
Expand All @@ -28,7 +29,7 @@ pub struct TransitionAnimator {

impl TransitionAnimator {
pub fn new(
mut wallpapers: Vec<Arc<Wallpaper>>,
mut wallpapers: Vec<Rc<RefCell<Wallpaper>>>,
transition: &ipc::Transition,
img_req: ImgReq,
animation: Option<Animation>,
Expand All @@ -38,10 +39,11 @@ impl TransitionAnimator {
return None;
}
for w in wallpapers.iter_mut() {
w.set_img_info(BgImg::Img(path.str().to_string()));
w.borrow_mut()
.set_img_info(BgImg::Img(path.str().to_string()));
}

let expect = wallpapers[0].get_dimensions();
let expect = wallpapers[0].borrow().get_dimensions();
if dim != expect {
error!("image has wrong dimensions! Expect {expect:?}, actual {dim:?}");
return None;
Expand Down Expand Up @@ -102,7 +104,7 @@ impl TransitionAnimator {

pub struct ImageAnimator {
now: Instant,
pub wallpapers: Vec<Arc<Wallpaper>>,
pub wallpapers: Vec<Rc<RefCell<Wallpaper>>>,
animation: Animation,
decompressor: Decompressor,
i: usize,
Expand Down Expand Up @@ -132,7 +134,7 @@ impl ImageAnimator {

let mut j = 0;
while j < wallpapers.len() {
let result = wallpapers[j].canvas_change(|canvas| {
let result = wallpapers[j].borrow_mut().canvas_change(|canvas| {
decompressor.decompress(frame, canvas, globals::pixel_format())
});

Expand Down
37 changes: 19 additions & 18 deletions daemon/src/animations/transitions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{sync::Arc, time::Instant};
use std::{cell::RefCell, rc::Rc, time::Instant};

use crate::{wallpaper::Wallpaper, wayland::globals};
use common::ipc::{Transition, TransitionType};
Expand Down Expand Up @@ -41,10 +41,11 @@ impl None {
fn new() -> Self {
Self
}
fn run(&mut self, wallpapers: &mut [Arc<Wallpaper>], img: &[u8]) -> bool {
wallpapers
.iter()
.for_each(|w| w.canvas_change(|canvas| canvas.copy_from_slice(img)));
fn run(&mut self, wallpapers: &mut [Rc<RefCell<Wallpaper>>], img: &[u8]) -> bool {
wallpapers.iter().for_each(|w| {
w.borrow_mut()
.canvas_change(|canvas| canvas.copy_from_slice(img))
});
true
}
}
Expand Down Expand Up @@ -73,7 +74,7 @@ impl Effect {
}
}

pub fn execute(&mut self, wallpapers: &mut [Arc<Wallpaper>], img: &[u8]) -> bool {
pub fn execute(&mut self, wallpapers: &mut [Rc<RefCell<Wallpaper>>], img: &[u8]) -> bool {
let done = match self {
Effect::None(effect) => effect.run(wallpapers, img),
Effect::Simple(effect) => effect.run(wallpapers, img),
Expand Down Expand Up @@ -107,11 +108,11 @@ impl Simple {
fn new(step: u8) -> Self {
Self { step }
}
fn run(&mut self, wallpapers: &mut [Arc<Wallpaper>], img: &[u8]) -> bool {
fn run(&mut self, wallpapers: &mut [Rc<RefCell<Wallpaper>>], img: &[u8]) -> bool {
let step = self.step;
let mut done = true;
for wallpaper in wallpapers.iter() {
wallpaper.canvas_change(|canvas| {
wallpaper.borrow_mut().canvas_change(|canvas| {
for (old, new) in canvas.iter_mut().zip(img) {
change_byte(step, old, new);
}
Expand All @@ -134,9 +135,9 @@ impl Fade {
let step = 0;
Self { start, seq, step }
}
fn run(&mut self, wallpapers: &mut [Arc<Wallpaper>], img: &[u8]) -> bool {
fn run(&mut self, wallpapers: &mut [Rc<RefCell<Wallpaper>>], img: &[u8]) -> bool {
for wallpaper in wallpapers.iter() {
wallpaper.canvas_change(|canvas| {
wallpaper.borrow_mut().canvas_change(|canvas| {
for (old, new) in canvas.iter_mut().zip(img) {
let x = *old as u16 * (256 - self.step);
let y = *new as u16 * self.step;
Expand Down Expand Up @@ -208,7 +209,7 @@ impl Wave {
step,
}
}
fn run(&mut self, wallpapers: &mut [Arc<Wallpaper>], img: &[u8]) -> bool {
fn run(&mut self, wallpapers: &mut [Rc<RefCell<Wallpaper>>], img: &[u8]) -> bool {
let Self {
width,
height,
Expand Down Expand Up @@ -243,7 +244,7 @@ impl Wave {
self.seq.advance_to(self.start.elapsed().as_secs_f64());

for wallpaper in wallpapers.iter() {
wallpaper.canvas_change(|canvas| {
wallpaper.borrow_mut().canvas_change(|canvas| {
// divide in 3 sections: the one we know will not be drawn to, the one we know
// WILL be drawn to, and the one we need to do a more expensive check on.
// We do this by creating 2 lines: the first tangential to the wave's peaks,
Expand Down Expand Up @@ -344,7 +345,7 @@ impl Wipe {
step,
}
}
fn run(&mut self, wallpapers: &mut [Arc<Wallpaper>], img: &[u8]) -> bool {
fn run(&mut self, wallpapers: &mut [Rc<RefCell<Wallpaper>>], img: &[u8]) -> bool {
let Self {
width,
height,
Expand All @@ -360,7 +361,7 @@ impl Wipe {
let offset = self.seq.now() as f64;
self.seq.advance_to(self.start.elapsed().as_secs_f64());
for wallpaper in wallpapers.iter() {
wallpaper.canvas_change(|canvas| {
wallpaper.borrow_mut().canvas_change(|canvas| {
// line formula: (x-h)*a + (y-k)*b + C = r^2
// https://www.desmos.com/calculator/vpvzk12yar
for line in 0..height {
Expand Down Expand Up @@ -432,7 +433,7 @@ impl Grow {
step,
}
}
fn run(&mut self, wallpapers: &mut [Arc<Wallpaper>], img: &[u8]) -> bool {
fn run(&mut self, wallpapers: &mut [Rc<RefCell<Wallpaper>>], img: &[u8]) -> bool {
let Self {
width,
height,
Expand All @@ -446,7 +447,7 @@ impl Grow {
let channels = globals::pixel_format().channels() as usize;

for wallpaper in wallpapers.iter() {
wallpaper.canvas_change(|canvas| {
wallpaper.borrow_mut().canvas_change(|canvas| {
let line_begin = center_y.saturating_sub(dist_center as usize);
let line_end = height.min(center_y + dist_center as usize);

Expand Down Expand Up @@ -517,7 +518,7 @@ impl Outer {
dist_center,
}
}
fn run(&mut self, wallpapers: &mut [Arc<Wallpaper>], img: &[u8]) -> bool {
fn run(&mut self, wallpapers: &mut [Rc<RefCell<Wallpaper>>], img: &[u8]) -> bool {
let Self {
width,
height,
Expand All @@ -530,7 +531,7 @@ impl Outer {
} = *self;
let channels = globals::pixel_format().channels() as usize;
for wallpaper in wallpapers.iter() {
wallpaper.canvas_change(|canvas| {
wallpaper.borrow_mut().canvas_change(|canvas| {
// to plot half a circle with radius r, we do sqrt(r^2 - x^2)
for line in 0..height {
let offset = (dist_center.powi(2) - (center_y as f32 - line as f32).powi(2))
Expand Down
Loading

0 comments on commit a74759d

Please sign in to comment.