Skip to content

Commit

Permalink
chore: debug for ashraf
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelOsborne committed Jan 17, 2024
1 parent 76f5dc8 commit 54e3891
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 46 deletions.
Binary file added demo-player/src/emoji.lottie
Binary file not shown.
19 changes: 16 additions & 3 deletions demo-player/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fs::{self, read_to_string, File};
use std::io::Read;
use std::{env, path, time::Instant};

use dotlottie_player_core::{Config, DotLottiePlayer, Mode};
Expand All @@ -20,7 +22,7 @@ impl Timer {
fn tick(&mut self, animation: &mut DotLottiePlayer) {
let next_frame = animation.request_frame();

println!("next_frame: {}", next_frame);
// println!("next_frame: {}", next_frame);
animation.set_frame(next_frame);
animation.render();

Expand All @@ -42,7 +44,7 @@ fn main() {
let base_path = env::var("CARGO_MANIFEST_DIR").unwrap();

let mut path = path::PathBuf::from(base_path);
path.push("src/cartoon.json");
path.push("src/emoji.lottie");

let mut lottie_player: DotLottiePlayer = DotLottiePlayer::new(Config {
mode: Mode::ReverseBounce,
Expand All @@ -51,13 +53,24 @@ fn main() {
use_frame_interpolation: true,
autoplay: true,
});
lottie_player.load_animation_path(path.to_str().unwrap(), WIDTH as u32, HEIGHT as u32);

// read dotlottie in to vec<u8>
let mut f = File::open("src/emoji.lottie").expect("no file found");
let metadata = fs::metadata("src/emoji.lottie").expect("unable to read metadata");
let mut buffer = vec![0; metadata.len() as usize];
f.read(&mut buffer).expect("buffer overflow");

lottie_player.load_dotlottie(&buffer, WIDTH as u32, HEIGHT as u32);

let mut timer = Timer::new();

while window.is_open() && !window.is_key_down(Key::Escape) {
timer.tick(&mut lottie_player);

if window.is_key_down(Key::Space) {
lottie_player.next_animation(WIDTH as u32, HEIGHT as u32);
}

let (buffer_ptr, buffer_len) = (lottie_player.buffer_ptr(), lottie_player.buffer_len());

let buffer =
Expand Down
90 changes: 47 additions & 43 deletions dotlottie-rs/src/dotlottie_player.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use instant::Instant;
use std::sync::RwLock;

use dotlottie_fms::DotLottieManager;

use crate::lottie_renderer::{LottieRenderer, LottieRendererError};

pub enum PlaybackState {
Expand Down Expand Up @@ -38,6 +40,7 @@ struct DotLottieRuntime {
start_time: Instant,
loop_count: u32,
config: Config,
dotlottie_manager: DotLottieManager,
direction: Direction,
}

Expand All @@ -57,6 +60,7 @@ impl DotLottieRuntime {
start_time: Instant::now(),
loop_count: 0,
config,
dotlottie_manager: DotLottieManager::new(None),
direction,
}
}
Expand Down Expand Up @@ -127,27 +131,21 @@ impl DotLottieRuntime {
}
}

pub fn load_dotlottie(&self, file_data: &Vec<u8>, width: u32, height: u32) -> bool {
self.dotlottie_manager
.write()
.unwrap()
.init(file_data.clone());
pub fn load_dotlottie(&mut self, file_data: &Vec<u8>, width: u32, height: u32) -> bool {
self.dotlottie_manager.init(file_data.clone());

// let animations = self.dotlottie_manager.read().unwrap().get_animations();

let first_animation = self
.dotlottie_manager
.write()
.unwrap()
.get_current_animation();
let first_animation = self.dotlottie_manager.get_current_animation();

match first_animation {
Ok(animation_data) => {
// let (width, height) = get_width_height(&animation_data);

println!("{}", animation_data);
// println!("{}", animation_data);
println!("Animation data loaded");

return self.load_animation(&animation_data, width, height);
return self.load_animation_data(&animation_data, width, height);
}
Err(error) => {
// Handle the error case
Expand All @@ -159,28 +157,15 @@ impl DotLottieRuntime {
}
}

pub fn next_animation(&self, width: u32, height: u32) -> bool {
let animation_data = self.dotlottie_manager.write().unwrap().next_animation();
pub fn next_animation(&mut self, width: u32, height: u32) -> bool {
let animation_data = self.dotlottie_manager.next_animation();

match animation_data {
Ok(animation_data) => {
// let (width, height) = get_width_height(&animsation_data);

self.clear();
let mut canvas = self.canvas.write().unwrap();
*canvas = thorvg::Canvas::new(thorvg::TvgEngine::TvgEngineSw, 0);

let mut animation = self.animation.write().unwrap();
*animation = thorvg::Animation::new();

let mut buffer_lock = self.buffer.lock().unwrap();
*buffer_lock = vec![];
// std::mem::drop(animation);
println!("2. animation data OK .. sending to load function");

std::mem::drop(buffer_lock);
std::mem::drop(canvas);
std::mem::drop(animation);

return self.load_animation(&animation_data, width, height);
return self.load_animation_data(&animation_data, width, height);
}
Err(error) => {
// Handle the error case
Expand All @@ -192,28 +177,28 @@ impl DotLottieRuntime {
}
}

pub fn previous_animation(&self, width: u32, height: u32) -> bool {
let animation_data = self.dotlottie_manager.write().unwrap().previous_animation();
pub fn previous_animation(&mut self, width: u32, height: u32) -> bool {
let animation_data = self.dotlottie_manager.previous_animation();

match animation_data {
Ok(animation_data) => {
// let (width, height) = get_width_height(&animsation_data);

self.clear();
let mut canvas = self.canvas.write().unwrap();
*canvas = thorvg::Canvas::new(thorvg::TvgEngine::TvgEngineSw, 0);
// self.clear();
// let mut canvas = self.canvas.write().unwrap();
// *canvas = thorvg::Canvas::new(thorvg::TvgEngine::TvgEngineSw, 0);

let mut animation = self.animation.write().unwrap();
*animation = thorvg::Animation::new();
// let mut animation = self.animation.write().unwrap();
// *animation = thorvg::Animation::new();

let mut buffer_lock = self.buffer.lock().unwrap();
*buffer_lock = vec![];
// let mut buffer_lock = self.buffer.lock().unwrap();
// *buffer_lock = vec![];

std::mem::drop(buffer_lock);
std::mem::drop(canvas);
std::mem::drop(animation);
// std::mem::drop(buffer_lock);
// std::mem::drop(canvas);
// std::mem::drop(animation);

return self.load_animation(&animation_data, width, height);
return self.load_animation_data(&animation_data, width, height);
}
Err(error) => {
// Handle the error case
Expand Down Expand Up @@ -546,6 +531,25 @@ impl DotLottiePlayer {
self.runtime.write().unwrap().request_frame()
}

pub fn load_dotlottie(&self, file_data: &Vec<u8>, width: u32, height: u32) -> bool {
self.runtime
.write()
.unwrap()
.load_dotlottie(file_data, width, height)
}

pub fn next_animation(&self, width: u32, height: u32) -> bool {
println!("1. next animation");
self.runtime.write().unwrap().next_animation(width, height)
}

pub fn previous_animation(&self, width: u32, height: u32) -> bool {
self.runtime
.write()
.unwrap()
.previous_animation(width, height)
}

pub fn set_frame(&self, no: f32) -> bool {
self.runtime.write().unwrap().set_frame(no)
}
Expand Down
12 changes: 12 additions & 0 deletions dotlottie-rs/src/lottie_renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ impl LottieRenderer {
Ok(())
}

pub fn clear_canvas(&mut self) -> Result<(), LottieRendererError> {
let thorvg_canvas = self
.thorvg_canvas
.as_mut()
.ok_or(LottieRendererError::AnimationNotLoaded)?;

thorvg_canvas.clear(true)?;

Ok(())
}

pub fn load_data(
&mut self,
data: &str,
Expand Down Expand Up @@ -120,6 +131,7 @@ impl LottieRenderer {
.map_err(LottieRendererError::ThorvgError)?;

if let Some(picture) = &mut thorvg_animation.get_picture() {
// println!("{:?}", data.as_bytes());
picture.load_data(data.as_bytes(), "lottie", copy)?;

thorvg_canvas.push(picture)?;
Expand Down
5 changes: 5 additions & 0 deletions dotlottie-rs/src/thorvg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ impl<'a> Picture<'a> {
pub fn load_data(&mut self, data: &[u8], mimetype: &str, copy: bool) -> Result<(), TvgError> {
let mimetype = CString::new(mimetype).expect("Failed to create CString");

// println!("{:?}", self.raw_paint);
println!("{}", mimetype.to_str().unwrap());

let result = unsafe {
tvg_picture_load_data(
self.raw_paint,
Expand All @@ -187,6 +190,8 @@ impl<'a> Picture<'a> {
)
};

println!("Result : {}", result);

convert_tvg_result(result, "tvg_picture_load_data")?;

Ok(())
Expand Down

0 comments on commit 54e3891

Please sign in to comment.