Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add native_display_soloist #23

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ ohos-ability-access-control-binding = { version = "0.0.1", path = "crates/abilit
ohos-xcomponent-binding = { version = "0.0.1", path = "crates/xcomponent" }
ohos-arkui-binding = { version = "0.0.1", path = "crates/arkui" }
ohos-vsync-binding = { version = "0.0.1", path = "crates/vsync" }
ohos-display-soloist-binding = { version = "0.0.1", path = "crates/native_display_soloist" }

ohos-bundle-sys = { version = "0.0.2", path = "sys/ohos-bundle-sys" }
ohos-init-sys = { version = "0.0.2", path = "sys/ohos-init-sys" }
Expand All @@ -24,6 +25,7 @@ ohos-ability-access-control-sys = { version = "0.0.1", path = "sys/ohos-ability-
ohos-xcomponent-sys = { version = "0.0.1", path = "sys/ohos-xcomponent-sys" }
ohos-arkui-sys = { version = "0.0.1", path = "sys/ohos-arkui-sys" }
ohos-vsync-sys = { version = "0.0.1", path = "sys/ohos-vsync-sys" }
ohos-display-soloist-sys = { version = "0.0.1", path = "sys/ohos-display-soloist-sys" }

enum_macro = { version = "0.0.1", path = "crates/enum_macro" }

Expand Down
9 changes: 9 additions & 0 deletions crates/native_display_soloist/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "ohos-display-soloist-binding"
version = "0.0.1"
edition = "2021"
license = "MIT OR Apache-2.0"
description = "OpenHarmony's NativeDisplaySoloist binding for rust"

[dependencies]
ohos-display-soloist-sys = { workspace = true }
67 changes: 67 additions & 0 deletions crates/native_display_soloist/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use std::{cell::RefCell, os::raw::c_void};

use ohos_display_soloist_sys::{
DisplaySoloist_ExpectedRateRange, OH_DisplaySoloist, OH_DisplaySoloist_Create,
OH_DisplaySoloist_Destroy, OH_DisplaySoloist_SetExpectedFrameRateRange,
OH_DisplaySoloist_Start, OH_DisplaySoloist_Stop,
};

thread_local! {
static DISPLAY_SOLOIST: RefCell<Option<Box<dyn Fn(i64, i64, *mut c_void)>>> = RefCell::new(None);
}

pub struct DisplaySoloist {
raw: *mut OH_DisplaySoloist,
running: RefCell<bool>,
}

impl DisplaySoloist {
pub fn new(use_exclusive_thread: bool) -> Self {
Self {
raw: unsafe { OH_DisplaySoloist_Create(use_exclusive_thread) },
running: RefCell::new(false),
}
}

pub fn set_frame_rate(&self, frame_rate: DisplaySoloist_ExpectedRateRange) {
unsafe {
OH_DisplaySoloist_SetExpectedFrameRateRange(self.raw, &frame_rate as *const _ as *mut _)
};
}

pub fn on_frame<F: Fn(i64, i64, *mut c_void) + 'static>(&self, data: *mut c_void, callback: F) {
DISPLAY_SOLOIST.with(|display_soloist| {
*display_soloist.borrow_mut() = Some(Box::new(callback));
});
unsafe {
OH_DisplaySoloist_Start(self.raw, Some(frame_callback), data);
};
self.running.replace(true);
}

pub fn stop(&self) {
unsafe {
OH_DisplaySoloist_Stop(self.raw);
}
self.running.replace(false);
}
}

extern "C" fn frame_callback(timestamp: i64, target_timestamp: i64, data: *mut c_void) {
DISPLAY_SOLOIST.with(|display_soloist| {
if let Some(callback) = &*display_soloist.borrow() {
callback(timestamp, target_timestamp, data);
}
});
}

impl Drop for DisplaySoloist {
fn drop(&mut self) {
if self.running.borrow().clone() {
self.stop();
}
unsafe {
OH_DisplaySoloist_Destroy(self.raw);
}
}
}
8 changes: 8 additions & 0 deletions sys/ohos-display-soloist-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "ohos-display-soloist-sys"
version = "0.0.1"
edition = "2021"
license = "MIT OR Apache-2.0"
description = "OpenHarmony's NativeDisplaySoloist binding for rust"

[dependencies]
6 changes: 6 additions & 0 deletions sys/ohos-display-soloist-sys/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use std::env;

fn main() {
let _ndk = env::var("OHOS_NDK_HOME").expect("OHOS_NDK_HOME not set");
println!("cargo:rustc-link-lib=dylib=native_display_soloist");
}
47 changes: 47 additions & 0 deletions sys/ohos-display-soloist-sys/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* automatically generated by rust-bindgen 0.65.1 */

#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct OH_DisplaySoloist {
_unused: [u8; 0],
}
pub type OH_DisplaySoloist_FrameCallback = ::std::option::Option<
unsafe extern "C" fn(
timestamp: ::std::os::raw::c_longlong,
targetTimestamp: ::std::os::raw::c_longlong,
data: *mut ::std::os::raw::c_void,
),
>;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct DisplaySoloist_ExpectedRateRange {
pub min: i32,
pub max: i32,
pub expected: i32,
}
extern "C" {
pub fn OH_DisplaySoloist_Create(useExclusiveThread: bool) -> *mut OH_DisplaySoloist;
}
extern "C" {
pub fn OH_DisplaySoloist_Destroy(displaySoloist: *mut OH_DisplaySoloist) -> i32;
}
extern "C" {
pub fn OH_DisplaySoloist_Start(
displaySoloist: *mut OH_DisplaySoloist,
callback: OH_DisplaySoloist_FrameCallback,
data: *mut ::std::os::raw::c_void,
) -> i32;
}
extern "C" {
pub fn OH_DisplaySoloist_Stop(displaySoloist: *mut OH_DisplaySoloist) -> i32;
}
extern "C" {
pub fn OH_DisplaySoloist_SetExpectedFrameRateRange(
displaySoloist: *mut OH_DisplaySoloist,
range: *mut DisplaySoloist_ExpectedRateRange,
) -> i32;
}
2 changes: 2 additions & 0 deletions tools/generate/build/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod asset;
mod bundle;
mod hilog;
mod init;
mod native_display_soloist;
mod raw;
mod vsync;

Expand All @@ -24,5 +25,6 @@ pub use asset::*;
pub use bundle::*;
pub use hilog::*;
pub use init::*;
pub use native_display_soloist::*;
pub use raw::*;
pub use vsync::*;
11 changes: 11 additions & 0 deletions tools/generate/build/config/native_display_soloist.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use once_cell::sync::Lazy;

use super::SysConfig;

pub const NATIVE_DISPLAY_SOLOIST: Lazy<SysConfig> = Lazy::new(|| SysConfig {
name: "ohos-display-soloist-sys",
headers: vec!["native_display_soloist/native_display_soloist.h"],
white_list: vec!["OH_.*", "DisplaySoloist.*"],
block_list: vec![],
extra: "",
});
1 change: 1 addition & 0 deletions tools/generate/build/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ static CONFIG: Lazy<Vec<Lazy<SysConfig>>> = Lazy::new(|| {
config::HILOG,
config::INIT,
config::VSYNC,
config::NATIVE_DISPLAY_SOLOIST,
]
});

Expand Down