Skip to content

Commit

Permalink
Add native image bindings
Browse files Browse the repository at this point in the history
Signed-off-by: Jonathan Schwender <[email protected]>
  • Loading branch information
jschwe committed Aug 29, 2024
1 parent 208d5ea commit 1d2a503
Show file tree
Hide file tree
Showing 10 changed files with 528 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ hilog = []
napi = []
## Enables bindings to `native_buffer`
native_buffer = ['native_window']
## Enables bindings to `native_image`
native_image = ['native_window']
## Enables bindings to `native_window`
native_window = ['native_buffer']
## Enables bindings to `native_vsync`
Expand Down
13 changes: 13 additions & 0 deletions scripts/generate_bindings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,19 @@ bindgen "${BASE_BINDGEN_ARGS[@]}" \
-x c++ \
"${BASE_CLANG_ARGS[@]}"

bindgen "${BASE_BINDGEN_ARGS[@]}" \
--raw-line="use crate::native_window::OHNativeWindow;" \
--allowlist-file ".*/native_image/.*\.h" \
--blocklist-item '^(OH)?NativeWindow?$' \
--default-enum-style=newtype \
--no-copy '^OH_NativeImage$' \
--no-copy 'OH_OnFrameAvailableListener' \
--no-debug '^OH_NativeImage$' \
--output "${ROOT_DIR}/src/native_image/native_image_api${OHOS_API_VERSION}.rs" \
"${OHOS_SYSROOT_DIR}/usr/include/native_image/native_image.h" \
-- \
"${BASE_CLANG_ARGS[@]}"

# NativeWindowOperation has wrong documentation for one of the parameters in API 10.
block_native_window_operation=""
if [[ ${OHOS_API_VERSION} -eq 10 ]]; then
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ pub mod napi;
#[cfg_attr(docsrs, doc(cfg(feature = "native_buffer")))]
pub mod native_buffer;

#[cfg(feature = "native_image")]
#[cfg_attr(docsrs, doc(cfg(feature = "native_image")))]
pub mod native_image;

#[cfg(feature = "native_window")]
#[cfg_attr(docsrs, doc(cfg(feature = "native_window")))]
pub mod native_window;
Expand Down
36 changes: 36 additions & 0 deletions src/native_image.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//! Bindings to `native_image`
//!
//! The native image module is used for associating a surface with an OpenGL external texture.
//! It functions as the consumer of a graphics queue.
//! You can use the APIs of this module to obtain and use a buffer, and output the buffer content to an OpenGL external texture.
//!
//! The following scenario is common for native image development:
//!
//! Use the native image APIs to create an OH_NativeImage instance as the consumer and obtain the corresponding OHNativeWindow instance (functioning as the producer).
//! Use the native window APIs to fill in and flush the buffer, and then use the native image APIs to update the buffer content to an OpenGL ES texture.
//!
//! Source: [Official Native Image documentation](https://docs.openharmony.cn/pages/v5.0/en/application-dev/graphics/native-image-guidelines.md)
//!
#[link(name = "EGL")]
#[link(name = "GLESv3")]
#[link(name = "native_image")]
extern "C" {}

mod native_image_api10;
pub use native_image_api10::*;

#[cfg(feature = "api-11")]
#[cfg_attr(docsrs, doc(cfg(feature = "api-11")))]
mod api11_additions;
#[cfg(feature = "api-11")]
#[cfg_attr(docsrs, doc(cfg(feature = "api-11")))]
pub use api11_additions::*;

#[cfg(feature = "api-12")]
#[cfg_attr(docsrs, doc(cfg(feature = "api-12")))]
mod api12_additions;

#[cfg(feature = "api-12")]
#[cfg_attr(docsrs, doc(cfg(feature = "api-12")))]
pub use api12_additions::*;
58 changes: 58 additions & 0 deletions src/native_image/api11_additions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

use super::OH_NativeImage;

/** @brief The callback function of frame available.
@syscap SystemCapability.Graphic.Graphic2D.NativeImage
@param context User defined context, returned to the user in the callback function
@since 11
@version 1.0*/
pub type OH_OnFrameAvailable =
::core::option::Option<unsafe extern "C" fn(context: *mut ::core::ffi::c_void)>;
/** @brief A listener for native image, use <b>OH_NativeImage_SetOnFrameAvailableListener</b> to register \n
the listener object to <b>OH_NativeImage</b>, the callback will be triggered when there is available frame
@since 11
@version 1.0*/
#[repr(C)]
#[derive(Debug)]
pub struct OH_OnFrameAvailableListener {
/// User defined context, returned to the user in the callback function
pub context: *mut ::core::ffi::c_void,
/// The callback function of frame available.
pub onFrameAvailable: OH_OnFrameAvailable,
}

extern "C" {
/** @brief Return the native image's surface id.
@syscap SystemCapability.Graphic.Graphic2D.NativeImage
@param image Indicates the pointer to a <b>OH_NativeImage</b> instance.
@param surfaceId Indicates the surface id.
@return Returns an error code, 0 is success, otherwise, failed.
@since 11
@version 1.0*/
pub fn OH_NativeImage_GetSurfaceId(image: *mut OH_NativeImage, surfaceId: *mut u64) -> i32;
/** @brief Set the frame available callback.
@syscap SystemCapability.Graphic.Graphic2D.NativeImage
@param image Indicates the pointer to a <b>OH_NativeImage</b> instance.
@param listener Indicates the callback function.
@return Returns an error code, 0 is success, otherwise, failed.
@since 11
@version 1.0*/
pub fn OH_NativeImage_SetOnFrameAvailableListener(
image: *mut OH_NativeImage,
listener: OH_OnFrameAvailableListener,
) -> i32;
/** @brief Unset the frame available callback.
@syscap SystemCapability.Graphic.Graphic2D.NativeImage
@param image Indicates the pointer to a <b>OH_NativeImage</b> instance.
@return Returns an error code, 0 is success, otherwise, failed.
@since 11
@version 1.0*/
pub fn OH_NativeImage_UnsetOnFrameAvailableListener(image: *mut OH_NativeImage) -> i32;
}
18 changes: 18 additions & 0 deletions src/native_image/api12_additions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

use super::OH_NativeImage;

extern "C" {
/** @brief Obtains the transform matrix of the texture image by producer transform type.\n
@syscap SystemCapability.Graphic.Graphic2D.NativeImage
@param image Indicates the pointer to a <b>OH_NativeImage</b> instance.
@param matrix Indicates the retrieved 4*4 transform matrix .
@return 0 - Success.
40001000 - image is NULL.
@since 12
@version 1.0*/
pub fn OH_NativeImage_GetTransformMatrixV2(image: *mut OH_NativeImage, matrix: *mut f32)
-> i32;
}
84 changes: 84 additions & 0 deletions src/native_image/native_image_api10.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/* automatically generated by rust-bindgen 0.69.4 */

#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
use crate::native_window::OHNativeWindow;

#[repr(C)]
pub struct OH_NativeImage {
_unused: [u8; 0],
}
extern "C" {
/** @brief Create a <b>OH_NativeImage</b> related to an Opengl ES texture and target. \n
@syscap SystemCapability.Graphic.Graphic2D.NativeImage
@param textureId Indicates the id of the Opengl ES texture which the native image attached to.
@param textureTarget Indicates the Opengl ES target.
@return Returns the pointer to the <b>OH_NativeImage</b> instance created if the operation is successful, \n
returns <b>NULL</b> otherwise.
@since 9
@version 1.0*/
pub fn OH_NativeImage_Create(textureId: u32, textureTarget: u32) -> *mut OH_NativeImage;
/** @brief Acquire the OHNativeWindow for the OH_NativeImage. This OHNativeWindow should be released by \n
OH_NativeWindow_DestroyNativeWindow when no longer needed.
@syscap SystemCapability.Graphic.Graphic2D.NativeImage
@param image Indicates the pointer to a <b>OH_NativeImage</b> instance.
@return Returns the pointer to the OHNativeWindow if the operation is successful, returns <b>NULL</b> otherwise.
@since 9
@version 1.0*/
pub fn OH_NativeImage_AcquireNativeWindow(image: *mut OH_NativeImage) -> *mut OHNativeWindow;
/** @brief Attach the OH_NativeImage to Opengl ES context, and the Opengl ES texture is bound to the \n
GL_TEXTURE_EXTERNAL_OES, which will update by the OH_NativeImage.
@syscap SystemCapability.Graphic.Graphic2D.NativeImage
@param image Indicates the pointer to a <b>OH_NativeImage</b> instance.
@param textureId Indicates the id of the Opengl ES texture which the native image attached to.
@return Returns an error code, 0 is success, otherwise, failed.
@since 9
@version 1.0*/
pub fn OH_NativeImage_AttachContext(image: *mut OH_NativeImage, textureId: u32) -> i32;
/** @brief Detach the OH_NativeImage from the Opengl ES context.
@syscap SystemCapability.Graphic.Graphic2D.NativeImage
@param image Indicates the pointer to a <b>OH_NativeImage</b> instance.
@return Returns an error code, 0 is success, otherwise, failed.
@since 9
@version 1.0*/
pub fn OH_NativeImage_DetachContext(image: *mut OH_NativeImage) -> i32;
/** @brief Update the related Opengl ES texture with the OH_NativeImage acquired buffer.
@syscap SystemCapability.Graphic.Graphic2D.NativeImage
@param image Indicates the pointer to a <b>OH_NativeImage</b> instance.
@return Returns an error code, 0 is success, otherwise, failed.
@since 9
@version 1.0*/
pub fn OH_NativeImage_UpdateSurfaceImage(image: *mut OH_NativeImage) -> i32;
/** @brief Get the timestamp of the texture image set by the most recent call to OH_NativeImage_UpdateSurfaceImage.
@syscap SystemCapability.Graphic.Graphic2D.NativeImage
@param image Indicates the pointer to a <b>OH_NativeImage</b> instance.
@return Returns the timestamp associated to the texture image.
@since 9
@version 1.0*/
pub fn OH_NativeImage_GetTimestamp(image: *mut OH_NativeImage) -> i64;
/** @brief Return the transform matrix of the texture image set by the most recent call to \n
OH_NativeImage_UpdateSurfaceImage.
@syscap SystemCapability.Graphic.Graphic2D.NativeImage
@param image Indicates the pointer to a <b>OH_NativeImage</b> instance.
@param matrix Indicates the retrieved 4*4 transform matrix .
@return Returns an error code, 0 is success, otherwise, failed.
@since 9
@version 1.0*/
pub fn OH_NativeImage_GetTransformMatrix(image: *mut OH_NativeImage, matrix: *mut f32) -> i32;
/** @brief Destroy the <b>OH_NativeImage</b> created by OH_NativeImage_Create, and the pointer to \n
<b>OH_NativeImage</b> will be null after this operation.
@syscap SystemCapability.Graphic.Graphic2D.NativeImage
@param image Indicates the pointer to a <b>OH_NativeImage</b> pointer.
@since 9
@version 1.0*/
pub fn OH_NativeImage_Destroy(image: *mut *mut OH_NativeImage);
}
Loading

0 comments on commit 1d2a503

Please sign in to comment.