Skip to content

Commit

Permalink
feat: made thorvg an optional dependency
Browse files Browse the repository at this point in the history
This makes building the crate easier without having Conan installed
and it should enable docs being generated on docs.rs.
  • Loading branch information
dragostis committed Oct 17, 2024
1 parent d285036 commit 4048c66
Show file tree
Hide file tree
Showing 9 changed files with 770 additions and 160 deletions.
2 changes: 1 addition & 1 deletion dotlottie-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ path = "uniffi-bindgen.rs"

[dependencies]
uniffi = { version = "0.28", features = ["cli"] }
dotlottie-rs = { path = "../dotlottie-rs" }
dotlottie-rs = { path = "../dotlottie-rs", features = ["thorvg"] }
cfg-if = "1.0"
bitflags = "2.6.0"

Expand Down
5 changes: 5 additions & 0 deletions dotlottie-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ links = "thorvg"
[lib]
crate-type = ["staticlib", "cdylib", "rlib"]

[features]
thorvg = []

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
thiserror = "1.0.48"
Expand All @@ -25,6 +28,8 @@ conan2 = "0.1"

[dev-dependencies]
criterion = "0.5.1"
# This is a workaround to enable this feature only on dev.
dotlottie-rs= { path = ".", features = ["thorvg"] }

[[bench]]
name = "benchmarks"
Expand Down
4 changes: 4 additions & 0 deletions dotlottie-rs/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ fn apply_build_settings(build_settings: &BuildSettings) {
}

fn main() {
if !cfg!(feature = "thorvg") {
return;
}

let mut builder = bindgen::Builder::default().header("wrapper.h");
if is_artifacts_provided() {
let include_dir = find_path(ARTIFACTS_INCLUDE_DIR, true);
Expand Down
51 changes: 40 additions & 11 deletions dotlottie-rs/src/dotlottie_player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
lottie_renderer::{LottieRenderer, LottieRendererError},
Marker, MarkersMap, StateMachine,
};
use crate::{DotLottieError, DotLottieManager, Manifest, ManifestAnimation};
use crate::{DotLottieError, DotLottieManager, Manifest, ManifestAnimation, Renderer};
use crate::{StateMachineObserver, StateMachineStatus};

pub trait Observer: Send + Sync {
Expand Down Expand Up @@ -128,7 +128,7 @@ impl Default for LayerBoundingBox {
}

struct DotLottieRuntime {
renderer: LottieRenderer,
renderer: Box<dyn LottieRenderer>,
playback_state: PlaybackState,
is_loaded: bool,
start_time: Instant,
Expand All @@ -142,7 +142,15 @@ struct DotLottieRuntime {
}

impl DotLottieRuntime {
#[cfg(feature = "thorvg")]
pub fn new(config: Config) -> Self {
Self::with_renderer(
config,
crate::TvgRenderer::new(crate::TvgEngine::TvgEngineSw, 0),
)
}

pub fn with_renderer<R: Renderer>(config: Config, renderer: R) -> Self {
let direction = match config.mode {
Mode::Forward => Direction::Forward,
Mode::Reverse => Direction::Reverse,
Expand All @@ -151,7 +159,7 @@ impl DotLottieRuntime {
};

DotLottieRuntime {
renderer: LottieRenderer::new(),
renderer: <dyn LottieRenderer>::new(renderer),
playback_state: PlaybackState::Stopped,
is_loaded: false,
start_time: Instant::now(),
Expand Down Expand Up @@ -302,7 +310,7 @@ impl DotLottieRuntime {
}

pub fn size(&self) -> (u32, u32) {
(self.renderer.width, self.renderer.height)
(self.renderer.width(), self.renderer.height())
}

pub fn get_state_machine(&self, state_machine_id: &str) -> Option<String> {
Expand Down Expand Up @@ -575,7 +583,7 @@ impl DotLottieRuntime {
}

pub fn current_frame(&self) -> f32 {
self.renderer.current_frame
self.renderer.current_frame()
}

pub fn loop_count(&self) -> u32 {
Expand All @@ -587,7 +595,7 @@ impl DotLottieRuntime {
}

pub fn buffer(&self) -> &[u32] {
&self.renderer.buffer
&self.renderer.buffer()
}

pub fn clear(&mut self) {
Expand Down Expand Up @@ -662,20 +670,22 @@ impl DotLottieRuntime {

fn load_animation_common<F>(&mut self, loader: F, width: u32, height: u32) -> bool
where
F: FnOnce(&mut LottieRenderer, u32, u32) -> Result<(), LottieRendererError>,
F: FnOnce(&mut dyn LottieRenderer, u32, u32) -> Result<(), LottieRendererError>,
{
self.clear();
self.playback_state = PlaybackState::Stopped;
self.start_time = Instant::now();
self.loop_count = 0;

let loaded = loader(&mut self.renderer, width, height).is_ok()
let loaded = loader(&mut *self.renderer, width, height).is_ok()
&& self
.renderer
.set_background_color(self.config.background_color)
.is_ok();

self.renderer.set_layout(&self.config.layout).unwrap();
if self.renderer.set_layout(&self.config.layout).is_err() {
return false;
}

self.is_loaded = loaded;

Expand Down Expand Up @@ -901,6 +911,7 @@ pub struct DotLottiePlayerContainer {
}

impl DotLottiePlayerContainer {
#[cfg(feature = "thorvg")]
pub fn new(config: Config) -> Self {
DotLottiePlayerContainer {
runtime: RwLock::new(DotLottieRuntime::new(config)),
Expand All @@ -909,6 +920,14 @@ impl DotLottiePlayerContainer {
}
}

pub fn with_renderer<R: Renderer>(config: Config, renderer: R) -> Self {
DotLottiePlayerContainer {
runtime: RwLock::new(DotLottieRuntime::with_renderer(config, renderer)),
observers: RwLock::new(Vec::new()),
state_machine: Rc::new(RwLock::new(None)),
}
}

pub fn load_animation_data(&self, animation_data: &str, width: u32, height: u32) -> bool {
let is_ok = self
.runtime
Expand Down Expand Up @@ -1222,8 +1241,8 @@ impl DotLottiePlayerContainer {
pub fn animation_size(&self) -> Vec<f32> {
match self.runtime.try_read() {
Ok(runtime) => vec![
runtime.renderer.picture_width,
runtime.renderer.picture_height,
runtime.renderer.picture_width(),
runtime.renderer.picture_height(),
],
_ => vec![0.0, 0.0],
}
Expand Down Expand Up @@ -1267,13 +1286,23 @@ pub struct DotLottiePlayer {
}

impl DotLottiePlayer {
#[cfg(feature = "thorvg")]
pub fn new(config: Config) -> Self {
DotLottiePlayer {
player: Rc::new(RwLock::new(DotLottiePlayerContainer::new(config))),
state_machine: Rc::new(RwLock::new(None)),
}
}

pub fn with_renderer<R: Renderer>(config: Config, renderer: R) -> Self {
DotLottiePlayer {
player: Rc::new(RwLock::new(DotLottiePlayerContainer::with_renderer(
config, renderer,
))),
state_machine: Rc::new(RwLock::new(None)),
}
}

pub fn load_animation_data(&self, animation_data: &str, width: u32, height: u32) -> bool {
self.player
.write()
Expand Down
4 changes: 2 additions & 2 deletions dotlottie-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod layout;
mod lottie_renderer;
mod markers;
mod state_machine;
mod thorvg;
// mod thorvg;

pub use dotlottie_player::*;
pub use fms::*;
Expand All @@ -13,4 +13,4 @@ pub use lottie_renderer::*;
pub use markers::*;
pub use state_machine::events::*;
pub use state_machine::*;
pub use thorvg::*;
// pub use thorvg::*;
Loading

0 comments on commit 4048c66

Please sign in to comment.