From 631fa1f1769ae962fa85ff714a481324fc18a0fb Mon Sep 17 00:00:00 2001 From: richerfu Date: Sat, 18 Jan 2025 10:55:51 +0800 Subject: [PATCH] feat(glutin-winit): support openharmony platform --- CHANGELOG.md | 1 + README.md | 15 +++++++++++++++ glutin-winit/build.rs | 3 ++- glutin_examples/Cargo.toml | 8 ++++++++ glutin_examples/examples/ohos.rs | 16 ++++++++++++++++ glutin_examples/src/lib.rs | 20 +++++++++++++++++--- 6 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 glutin_examples/examples/ohos.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index ef55ffb894..107967aafa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Unreleased - **Breaking:** Added `make_current_surfaceless(self)` for `{Possibly,Not}CurrentGlContext`. +- Added OpenHarmony support for `glutin-winit`. # Version 0.32.2 diff --git a/README.md b/README.md index 2473be5e29..e4c878349a 100644 --- a/README.md +++ b/README.md @@ -59,3 +59,18 @@ and start the app using: ```console $ cargo apk r -p glutin_examples --example android ``` + +### OpenHarmony/HarmonyNext + +Be sure to handle OpenHarmony's lifecycle correctly when using a `winit` window +by only creating a GL surface after `winit` raises `Event::Resumed`, and +destroy it again upon receiving `Event::Suspended`. See this in action in the +[`ohos.rs` example](./glutin_examples/examples/ohos.rs). + +To compile and run the OpenHarmony example on your device, +install [`ohrs`](https://crates.io/crates/ohrs) +and start the app using: + +```console +$ ohrs build --arch aarch +``` diff --git a/glutin-winit/build.rs b/glutin-winit/build.rs index 32078de541..5dd734fdcd 100644 --- a/glutin-winit/build.rs +++ b/glutin-winit/build.rs @@ -6,12 +6,13 @@ fn main() { // Setup alias to reduce `cfg` boilerplate. cfg_aliases! { // Systems. + ohos_platform: { target_env = "ohos" }, android_platform: { target_os = "android" }, wasm_platform: { target_family = "wasm" }, macos_platform: { target_os = "macos" }, ios_platform: { target_os = "ios" }, apple: { any(ios_platform, macos_platform) }, - free_unix: { all(unix, not(apple), not(android_platform)) }, + free_unix: { all(unix, not(apple), not(android_platform), not(ohos_platform)) }, // Native displays. x11_platform: { all(feature = "x11", free_unix, not(wasm_platform)) }, diff --git a/glutin_examples/Cargo.toml b/glutin_examples/Cargo.toml index 4ff9927a18..3d00eb7202 100644 --- a/glutin_examples/Cargo.toml +++ b/glutin_examples/Cargo.toml @@ -29,6 +29,10 @@ drm = { version = "0.12", optional = true } [target.'cfg(target_os = "android")'.dependencies] winit = { version = "0.30.0", default-features = false, features = ["android-native-activity", "rwh_06"] } +[target.'cfg(target_env = "ohos")'.dependencies] +openharmony_ability = { version = "0.0.1" } +openharmony_ability_derive = { version = "0.0.1" } + [build-dependencies] gl_generator = "0.14" cfg_aliases = "0.2.1" @@ -37,6 +41,10 @@ cfg_aliases = "0.2.1" name = "android" crate-type = ["cdylib"] +[[example]] +name = "ohos_test" +crate-type = ["cdylib"] + [[example]] name = "egl_device" required-features = ["egl"] diff --git a/glutin_examples/examples/ohos.rs b/glutin_examples/examples/ohos.rs new file mode 100644 index 0000000000..f74fc9151e --- /dev/null +++ b/glutin_examples/examples/ohos.rs @@ -0,0 +1,16 @@ +#![cfg(ohos_platform)] + +use openharmony_ability::OpenHarmonyApp; +use openharmony_ability_derive::ability; + +use winit::event_loop::EventLoop; +use winit::platform::ohos::EventLoopBuilderExtOpenHarmony; + +mod app; + +#[ability] +pub fn openharmony(openharmony_app: OpenHarmonyApp) { + let a = openharmony_app.clone(); + let event_loop = EventLoop::builder().with_openharmony_app(a).build().unwrap(); + app::main(event_loop).unwrap() +} diff --git a/glutin_examples/src/lib.rs b/glutin_examples/src/lib.rs index e5b75e17bf..1214ec21ad 100644 --- a/glutin_examples/src/lib.rs +++ b/glutin_examples/src/lib.rs @@ -21,6 +21,9 @@ use glutin::surface::{Surface, SwapInterval, WindowSurface}; use glutin_winit::{DisplayBuilder, GlWindow}; +#[cfg(target_env = "ohos")] +use winit::platform::ohos::EventLoopBuilderExtOpenHarmony; + pub mod gl { #![allow(clippy::all)] include!(concat!(env!("OUT_DIR"), "/gl_bindings.rs")); @@ -42,10 +45,21 @@ pub fn main(event_loop: winit::event_loop::EventLoop<()>) -> Result<(), Box