Skip to content

Commit

Permalink
refactor: 💡 use immutable refs only in safe thorvg structs
Browse files Browse the repository at this point in the history
  • Loading branch information
theashraf committed Dec 14, 2023
1 parent 057a33d commit 982c49b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 46 deletions.
12 changes: 6 additions & 6 deletions dotlottie-rs/src/dotlottie_player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct DotLottiePlayer {

impl DotLottiePlayer {
pub fn new() -> Self {
let canvas = thorvg::Canvas::new(thorvg::TvgEngine::TvgEngineSw, 3);
let canvas = thorvg::Canvas::new(thorvg::TvgEngine::TvgEngineSw, 0);
let animation = thorvg::Animation::new();
let buffer = Mutex::new(vec![]);

Expand All @@ -34,7 +34,7 @@ impl DotLottiePlayer {
}
}

pub fn frame(&mut self, no: f32) {
pub fn frame(&self, no: f32) {
self.canvas.clear(false, true);

self.animation.set_frame(no);
Expand Down Expand Up @@ -82,11 +82,11 @@ impl DotLottiePlayer {
buffer_lock.len() as i64
}

pub fn clear(&mut self) {
pub fn clear(&self) {
self.canvas.clear(false, true);
}

pub fn load_animation_from_path(&mut self, path: &str, width: u32, height: u32) -> bool {
pub fn load_animation_from_path(&self, path: &str, width: u32, height: u32) -> bool {
let mut buffer_lock = self.buffer.lock().unwrap();

*buffer_lock = vec![0; (width * height * 4) as usize];
Expand All @@ -99,7 +99,7 @@ impl DotLottiePlayer {
thorvg::TvgColorspace::ABGR8888,
);

if let Some(mut frame_image) = self.animation.get_picture() {
if let Some(frame_image) = self.animation.get_picture() {
if frame_image.load(path).is_err() {
return false;
}
Expand All @@ -124,7 +124,7 @@ impl DotLottiePlayer {
true
}

pub fn load_animation(&mut self, animation_data: &str, width: u32, height: u32) -> bool {
pub fn load_animation(&self, animation_data: &str, width: u32, height: u32) -> bool {
let mut buffer_lock = self.buffer.lock().unwrap();

*buffer_lock = vec![0; (width * height * 4) as usize];
Expand Down
76 changes: 36 additions & 40 deletions dotlottie-rs/src/thorvg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl Canvas {
}

pub fn set_target(
&mut self,
&self,
buffer: *mut u32,
stride: u32,
width: u32,
Expand All @@ -76,37 +76,37 @@ impl Canvas {
convert_tvg_result(result)
}

pub fn clear(&mut self, paints: bool, buffer: bool) -> Result<(), TvgError> {
pub fn clear(&self, paints: bool, buffer: bool) -> Result<(), TvgError> {
let result = unsafe { tvg_canvas_clear(self.raw_canvas, paints, buffer) };

convert_tvg_result(result)
}

pub fn push(&mut self, picture: &Picture) -> Result<(), TvgError> {
pub fn push(&self, picture: &Picture) -> Result<(), TvgError> {
let result = unsafe { tvg_canvas_push(self.raw_canvas, picture.raw_paint()) };

convert_tvg_result(result)
}

pub fn draw(&mut self) -> Result<(), TvgError> {
pub fn draw(&self) -> Result<(), TvgError> {
let result = unsafe { tvg_canvas_draw(self.raw_canvas) };

convert_tvg_result(result)
}

pub fn sync(&mut self) -> Result<(), TvgError> {
pub fn sync(&self) -> Result<(), TvgError> {
let result = unsafe { tvg_canvas_sync(self.raw_canvas) };

convert_tvg_result(result)
}

pub fn update(&mut self) -> Result<(), TvgError> {
pub fn update(&self) -> Result<(), TvgError> {
let result = unsafe { tvg_canvas_update(self.raw_canvas) };

convert_tvg_result(result)
}

pub fn set_mempool(&mut self, policy: Tvg_Mempool_Policy) -> Result<(), TvgError> {
pub fn set_mempool(&self, policy: Tvg_Mempool_Policy) -> Result<(), TvgError> {
let result = unsafe { tvg_swcanvas_set_mempool(self.raw_canvas, policy) };

convert_tvg_result(result)
Expand All @@ -118,8 +118,6 @@ impl Drop for Canvas {
unsafe {
tvg_canvas_destroy(self.raw_canvas);
}

self.raw_canvas = std::ptr::null_mut();
}
}

Expand Down Expand Up @@ -178,22 +176,28 @@ impl Picture {
Ok((width, height))
}

pub fn set_size(&self, width: f32, height: f32) {
unsafe {
tvg_picture_set_size(self.raw_paint, width, height);
}
pub fn set_size(&self, width: f32, height: f32) -> Result<(), TvgError> {
let result = unsafe { tvg_picture_set_size(self.raw_paint, width, height) };

convert_tvg_result(result)
}

pub fn scale(&mut self, factor: f32) -> Tvg_Result {
unsafe { tvg_paint_scale(self.raw_paint(), factor) }
pub fn scale(&self, factor: f32) -> Result<(), TvgError> {
let result = unsafe { tvg_paint_scale(self.raw_paint(), factor) };

convert_tvg_result(result)
}

pub fn translate(&mut self, tx: f32, ty: f32) -> Tvg_Result {
unsafe { tvg_paint_translate(self.raw_paint(), tx, ty) }
pub fn translate(&self, tx: f32, ty: f32) -> Result<(), TvgError> {
let result = unsafe { tvg_paint_translate(self.raw_paint(), tx, ty) };

convert_tvg_result(result)
}

pub fn rotate(&mut self, degree: f32) -> Tvg_Result {
unsafe { tvg_paint_rotate(self.raw_paint(), degree) }
pub fn rotate(&self, degree: f32) -> Result<(), TvgError> {
let result = unsafe { tvg_paint_rotate(self.raw_paint(), degree) };

convert_tvg_result(result)
}
}

Expand All @@ -217,33 +221,27 @@ impl Animation {
}
}

pub fn get_total_frame(&self) -> Result<f32, Tvg_Result> {
unsafe {
let mut total_frame: f32 = 0.0;
pub fn get_total_frame(&self) -> Result<f32, TvgError> {
let mut total_frame: f32 = 0.0;

let result =
tvg_animation_get_total_frame(self.raw_animation, &mut total_frame as *mut f32);
let result = unsafe {
tvg_animation_get_total_frame(self.raw_animation, &mut total_frame as *mut f32)
};

if result != Tvg_Result_TVG_RESULT_SUCCESS {
return Err(result);
}
convert_tvg_result(result)?;

return Ok(total_frame);
}
return Ok(total_frame);
}

pub fn get_duration(&self) -> Result<f32, Tvg_Result> {
unsafe {
let mut duration: f32 = 0.0;
pub fn get_duration(&self) -> Result<f32, TvgError> {
let mut duration: f32 = 0.0;

let result = tvg_animation_get_duration(self.raw_animation, &mut duration as *mut f32);
let result =
unsafe { tvg_animation_get_duration(self.raw_animation, &mut duration as *mut f32) };

if result != Tvg_Result_TVG_RESULT_SUCCESS {
return Err(result);
}
convert_tvg_result(result)?;

return Ok(duration);
}
return Ok(duration);
}

pub fn set_frame(&self, frame_no: f32) -> Result<(), TvgError> {
Expand All @@ -268,7 +266,5 @@ impl Drop for Animation {
unsafe {
tvg_animation_del(self.raw_animation);
}

self.raw_animation = std::ptr::null_mut();
}
}

0 comments on commit 982c49b

Please sign in to comment.