use super::*;
#[cfg(target_env = "ohos")]
#[link(name = "ace_ndk.z")]
unsafe extern "C" {
pub fn OH_NativeXComponent_GetXComponentId(
component: *mut OhNativeXComponent,
id: *mut u8,
size: *mut u64,
) -> i32;
pub fn OH_NativeXComponent_GetXComponentSize(
component: *mut OhNativeXComponent,
window: *const OhNativeWindow,
width: *mut u64,
height: *mut u64,
) -> i32;
pub fn OH_NativeXComponent_RegisterCallback(
component: *mut OhNativeXComponent,
callback: *mut OhNativeXComponentCallback,
) -> i32;
pub fn OH_NativeXComponent_GetTouchEvent(
component: *mut OhNativeXComponent,
window: *const OhNativeWindow,
touch_event: *mut OhNativeXComponentTouchEvent,
) -> i32;
}
#[cfg(target_env = "ohos")]
#[link(name = "native_window")]
unsafe extern "C" {
pub fn OH_NativeWindow_NativeWindowHandleOpt(
window: *mut OhNativeWindow,
code: i32,
...
) -> i32;
pub fn OH_NativeWindow_NativeWindowRequestBuffer(
window: *mut OhNativeWindow,
buffer: *mut *mut OhNativeWindowBuffer,
fence_fd: *mut i32,
) -> i32;
pub fn OH_NativeWindow_NativeWindowFlushBuffer(
window: *mut OhNativeWindow,
buffer: *mut OhNativeWindowBuffer,
fence_fd: i32,
region: OhRegion,
) -> i32;
pub fn OH_NativeWindow_NativeWindowAbortBuffer(
window: *mut OhNativeWindow,
buffer: *mut OhNativeWindowBuffer,
) -> i32;
pub fn OH_NativeWindow_GetBufferHandleFromNative(
buffer: *mut OhNativeWindowBuffer,
) -> *mut OhBufferHandle;
}
#[cfg(target_env = "ohos")]
#[link(name = "native_vsync")]
unsafe extern "C" {
pub fn OH_NativeVSync_Create(name: *const c_char, length: u32) -> *mut OhNativeVSync;
pub fn OH_NativeVSync_Destroy(native_vsync: *mut OhNativeVSync);
pub fn OH_NativeVSync_RequestFrame(
native_vsync: *mut OhNativeVSync,
callback: unsafe extern "C" fn(i64, *mut c_void),
data: *mut c_void,
) -> i32;
pub fn OH_NativeVSync_GetPeriod(native_vsync: *mut OhNativeVSync, period: *mut i64) -> i32;
}
#[cfg(target_env = "ohos")]
unsafe extern "C" {
fn close(fd: i32) -> i32;
fn poll(fds: *mut c_void, nfds: usize, timeout: i32) -> i32;
fn mmap(
addr: *mut c_void,
len: usize,
prot: i32,
flags: i32,
fd: i32,
offset: isize,
) -> *mut c_void;
fn munmap(addr: *mut c_void, len: usize) -> i32;
}
#[must_use]
pub fn resolve_harmony_avoid_area(
device_type: HarmonyDeviceType,
avoid: HarmonyAvoidArea,
density: f32,
logical_width: u32,
logical_height: u32,
immersive_fullscreen: bool,
) -> SafeAreaInsets {
let scale: f32 = if density.is_finite() && density >= 0.75 {
density
} else {
1.0
};
let kb_vp: f32 = (avoid.get_keyboard_px() as f32) / scale;
if immersive_fullscreen {
return SafeAreaInsets::new(0.0, 0.0, kb_vp, 0.0);
}
let top_vp: f32 = (avoid.get_top_px() as f32) / scale;
let right_vp: f32 = (avoid.get_right_px() as f32) / scale;
let bottom_vp: f32 = ((avoid.get_bottom_px() as f32) / scale).max(kb_vp);
let left_vp: f32 = (avoid.get_left_px() as f32) / scale;
match device_type {
HarmonyDeviceType::Watch => {
let round_safe: SafeAreaInsets = compute_wearable_safe_area(
WearableShape::Round,
logical_width.max(1) as f32,
logical_height.max(1) as f32,
0.0,
);
SafeAreaInsets::new(
top_vp.max(round_safe.get_top()),
right_vp.max(round_safe.get_right()),
bottom_vp.max(round_safe.get_bottom()),
left_vp.max(round_safe.get_left()),
)
}
HarmonyDeviceType::Tv => SafeAreaInsets::new(
top_vp.max(HARMONY_TV_OVERSCAN_VP),
right_vp.max(HARMONY_TV_OVERSCAN_VP),
bottom_vp.max(HARMONY_TV_OVERSCAN_VP),
left_vp.max(HARMONY_TV_OVERSCAN_VP),
),
HarmonyDeviceType::Automotive => SafeAreaInsets::new(
top_vp.max(HARMONY_CAR_DOCK_VP),
right_vp,
bottom_vp.max(HARMONY_CAR_DOCK_VP),
left_vp,
),
HarmonyDeviceType::Phone | HarmonyDeviceType::Foldable => SafeAreaInsets::new(
top_vp.max(HARMONY_PHONE_SAFE_TOP_VP),
right_vp,
bottom_vp.max(HARMONY_PHONE_SAFE_BOTTOM_VP),
left_vp,
),
HarmonyDeviceType::Tablet | HarmonyDeviceType::Pc2In1 => {
SafeAreaInsets::new(top_vp, right_vp, bottom_vp, left_vp)
}
}
}
#[must_use]
pub const fn harmony_keycode_to_euv(ohos_code: i32) -> KeyCode {
match ohos_code {
HARMONY_KEYCODE_BACK => KeyCode::Back,
HARMONY_KEYCODE_DPAD_UP => KeyCode::ArrowUp,
HARMONY_KEYCODE_DPAD_DOWN => KeyCode::ArrowDown,
HARMONY_KEYCODE_DPAD_LEFT => KeyCode::ArrowLeft,
HARMONY_KEYCODE_DPAD_RIGHT => KeyCode::ArrowRight,
HARMONY_KEYCODE_DPAD_CENTER | HARMONY_KEYCODE_ENTER => KeyCode::Enter,
HARMONY_KEYCODE_TAB => KeyCode::Tab,
HARMONY_KEYCODE_SPACE => KeyCode::Space,
HARMONY_KEYCODE_DEL => KeyCode::Backspace,
HARMONY_KEYCODE_FORWARD_DEL => KeyCode::Delete,
HARMONY_KEYCODE_ESCAPE => KeyCode::Escape,
_ => KeyCode::Unknown,
}
}
#[must_use]
pub const fn harmony_memory_level_to_pressure(level: i32) -> MemoryPressure {
match level {
HARMONY_MEMORY_CRITICAL => MemoryPressure::Critical,
HARMONY_MEMORY_MODERATE | HARMONY_MEMORY_LOW => MemoryPressure::Warning,
_ => MemoryPressure::Normal,
}
}
pub fn harmony_copy_rgba_to_strided_buffer(
framebuffer: &Framebuffer,
dst: &mut [u8],
dst_width: u32,
dst_height: u32,
dst_stride_bytes: usize,
dirty_rect: Option<Rect>,
) -> usize {
let copy_w: u32 = framebuffer.get_width().min(dst_width);
let copy_h: u32 = framebuffer.get_height().min(dst_height);
if copy_w == 0 || copy_h == 0 || dst_stride_bytes < 4 {
return 0;
}
let (x0, y0, x1, y1): (u32, u32, u32, u32) = match dirty_rect {
Some(r) => {
let rx0: u32 = (r.get_x().floor().max(0.0) as u32).min(copy_w);
let ry0: u32 = (r.get_y().floor().max(0.0) as u32).min(copy_h);
let rx1: u32 = ((r.get_x() + r.get_width()).ceil().max(0.0) as u32).min(copy_w);
let ry1: u32 = ((r.get_y() + r.get_height()).ceil().max(0.0) as u32).min(copy_h);
if rx1 <= rx0 || ry1 <= ry0 {
return 0;
}
(rx0, ry0, rx1, ry1)
}
None => (0, 0, copy_w, copy_h),
};
let src_bytes: &[u8] = framebuffer.get_pixels().as_slice();
let src_stride_bytes: usize = (framebuffer.get_width() as usize).saturating_mul(4);
let row_bytes: usize = ((x1 - x0) as usize).saturating_mul(4);
let x_offset_bytes: usize = (x0 as usize).saturating_mul(4);
let mut written: usize = 0;
for row in y0..y1 {
let src_start: usize = (row as usize)
.saturating_mul(src_stride_bytes)
.saturating_add(x_offset_bytes);
let dst_start: usize = (row as usize)
.saturating_mul(dst_stride_bytes)
.saturating_add(x_offset_bytes);
let src_end: usize = src_start.saturating_add(row_bytes);
let dst_end: usize = dst_start.saturating_add(row_bytes);
if src_end <= src_bytes.len() && dst_end <= dst.len() {
dst[dst_start..dst_end].copy_from_slice(&src_bytes[src_start..src_end]);
written = written.saturating_add(row_bytes);
}
}
written
}
#[must_use]
pub(crate) fn harmony_create_native_vsync() -> usize {
#[cfg(target_env = "ohos")]
{
unsafe {
let ptr: *mut OhNativeVSync = OH_NativeVSync_Create(
HARMONY_VSYNC_NAME.as_ptr().cast::<c_char>(),
HARMONY_VSYNC_NAME.len() as u32,
);
ptr as usize
}
}
#[cfg(not(target_env = "ohos"))]
{
0
}
}
pub(crate) fn harmony_destroy_native_vsync(vsync_handle: usize) {
#[cfg(target_env = "ohos")]
if vsync_handle != 0 {
unsafe {
OH_NativeVSync_Destroy(vsync_handle as *mut OhNativeVSync);
}
}
#[cfg(not(target_env = "ohos"))]
{
let _: usize = vsync_handle;
}
}
#[must_use]
pub(crate) fn harmony_query_vsync_refresh_hz(vsync_handle: usize) -> Option<f32> {
if vsync_handle == 0 {
return None;
}
#[cfg(target_env = "ohos")]
{
let mut period_ns: i64 = 0;
let rc: i32 =
unsafe { OH_NativeVSync_GetPeriod(vsync_handle as *mut OhNativeVSync, &mut period_ns) };
if rc == 0
&& period_ns >= HARMONY_VSYNC_MIN_PERIOD_NS
&& period_ns <= HARMONY_VSYNC_MAX_PERIOD_NS
{
let hz: f32 = (1_000_000_000.0_f64 / (period_ns as f64)) as f32;
return Some(hz.clamp(MIN_REFRESH_RATE_HZ, MAX_REFRESH_RATE_HZ));
}
None
}
#[cfg(not(target_env = "ohos"))]
{
None
}
}
pub(crate) fn harmony_request_vsync_frame(
vsync_handle: usize,
callback: unsafe extern "C" fn(i64, usize),
data: usize,
) -> bool {
if vsync_handle == 0 {
return false;
}
#[cfg(target_env = "ohos")]
{
unsafe {
let c_cb: unsafe extern "C" fn(i64, *mut c_void) = transmute(callback);
OH_NativeVSync_RequestFrame(
vsync_handle as *mut OhNativeVSync,
c_cb,
data as *mut c_void,
) == 0
}
}
#[cfg(not(target_env = "ohos"))]
{
let _: (unsafe extern "C" fn(i64, usize), usize) = (callback, data);
false
}
}
pub(crate) fn harmony_present_native_window(
native_window: usize,
framebuffer: &Framebuffer,
cached_w: &mut u32,
cached_h: &mut u32,
dirty_rect: Option<Rect>,
) -> Result<(), PlatformError> {
if native_window == 0 {
return Ok(());
}
#[cfg(target_env = "ohos")]
{
let fb_w: u32 = framebuffer.get_width();
let fb_h: u32 = framebuffer.get_height();
let win: *mut OhNativeWindow = native_window as *mut OhNativeWindow;
unsafe {
if *cached_w != fb_w || *cached_h != fb_h {
let _: i32 = OH_NativeWindow_NativeWindowHandleOpt(
win,
HARMONY_NW_OPT_SET_BUFFER_GEOMETRY,
fb_w as i32,
fb_h as i32,
);
let _: i32 = OH_NativeWindow_NativeWindowHandleOpt(
win,
HARMONY_NW_OPT_SET_FORMAT,
HARMONY_PIXEL_FMT_RGBA_8888,
);
*cached_w = fb_w;
*cached_h = fb_h;
}
let mut buffer: *mut OhNativeWindowBuffer = null_mut();
let mut fence_fd: i32 = -1;
let req_rc: i32 =
OH_NativeWindow_NativeWindowRequestBuffer(win, &mut buffer, &mut fence_fd);
if fence_fd >= 0 {
let mut pfd: HarmonyPollFd = HarmonyPollFd {
fd: fence_fd,
events: HARMONY_POLLIN,
revents: 0,
};
let _: i32 = poll(
(&mut pfd as *mut HarmonyPollFd).cast::<c_void>(),
1,
HARMONY_FENCE_POLL_TIMEOUT_MS,
);
let _: i32 = close(fence_fd);
}
if req_rc != 0 || buffer.is_null() {
return Err(PlatformError::SurfaceError(
ERR_HARMONY_SURFACE_POST.to_string(),
));
}
let handle_ptr: *mut OhBufferHandle = OH_NativeWindow_GetBufferHandleFromNative(buffer);
if handle_ptr.is_null() {
let _: i32 = OH_NativeWindow_NativeWindowAbortBuffer(win, buffer);
return Err(PlatformError::SurfaceError(
ERR_HARMONY_SURFACE_POST.to_string(),
));
}
let handle: OhBufferHandle = *handle_ptr;
let dst_w: u32 = handle.width.max(0) as u32;
let dst_h: u32 = handle.height.max(0) as u32;
let dst_stride: usize = handle.stride.max(0) as usize;
let required_bytes: usize = dst_stride.saturating_mul(dst_h as usize);
let map_bytes: usize = (handle.size.max(0) as usize).max(required_bytes);
let mut mapped_ptr: *mut c_void = null_mut();
let mut needs_munmap: bool = false;
if handle.vir_addr != 0 {
mapped_ptr = handle.vir_addr as *mut c_void;
} else if handle.fd >= 0 && map_bytes > 0 {
let addr: *mut c_void = mmap(
null_mut(),
map_bytes,
HARMONY_PROT_READ_WRITE,
HARMONY_MAP_SHARED,
handle.fd,
0,
);
if !addr.is_null() && (addr as usize) != HARMONY_MAP_FAILED {
mapped_ptr = addr;
needs_munmap = true;
}
}
if !mapped_ptr.is_null() && map_bytes > 0 {
let dst_slice: &mut [u8] = from_raw_parts_mut(mapped_ptr.cast::<u8>(), map_bytes);
let _: usize = harmony_copy_rgba_to_strided_buffer(
framebuffer,
dst_slice,
dst_w,
dst_h,
dst_stride,
dirty_rect,
);
if needs_munmap {
let _: i32 = munmap(mapped_ptr, map_bytes);
}
} else {
let _: i32 = OH_NativeWindow_NativeWindowAbortBuffer(win, buffer);
return Err(PlatformError::SurfaceError(
ERR_HARMONY_SURFACE_POST.to_string(),
));
}
let damage_rect: OhRegionRect = match dirty_rect {
Some(r) => OhRegionRect {
x: r.get_x().floor().max(0.0) as i32,
y: r.get_y().floor().max(0.0) as i32,
w: r.get_width().ceil().max(1.0) as u32,
h: r.get_height().ceil().max(1.0) as u32,
},
None => OhRegionRect::default(),
};
let region: OhRegion = if dirty_rect.is_some() {
OhRegion {
rects: &damage_rect,
rect_number: 1,
}
} else {
OhRegion {
rects: null(),
rect_number: 0,
}
};
let flush_rc: i32 = OH_NativeWindow_NativeWindowFlushBuffer(win, buffer, -1, region);
if flush_rc != 0 {
let _: i32 = OH_NativeWindow_NativeWindowAbortBuffer(win, buffer);
return Err(PlatformError::SurfaceError(
ERR_HARMONY_SURFACE_POST.to_string(),
));
}
}
Ok(())
}
#[cfg(not(target_env = "ohos"))]
{
let _: (&Framebuffer, &mut u32, &mut u32, Option<Rect>) =
(framebuffer, cached_w, cached_h, dirty_rect);
Ok(())
}
}