From 631fa1f1769ae962fa85ff714a481324fc18a0fb Mon Sep 17 00:00:00 2001 From: richerfu Date: Sat, 18 Jan 2025 10:55:51 +0800 Subject: [PATCH 1/4] 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 Date: Fri, 24 Jan 2025 15:28:29 +0800 Subject: [PATCH 2/4] fix: build error --- glutin-winit/Cargo.toml | 5 ++++- glutin_examples/Cargo.toml | 21 ++++++++++++++++----- glutin_examples/build.rs | 3 ++- glutin_examples/examples/ohos.rs | 4 +--- glutin_examples/src/lib.rs | 2 +- 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/glutin-winit/Cargo.toml b/glutin-winit/Cargo.toml index a27cd8ef77..821a614773 100644 --- a/glutin-winit/Cargo.toml +++ b/glutin-winit/Cargo.toml @@ -21,7 +21,10 @@ wayland = ["glutin/wayland", "winit/wayland"] [dependencies] glutin = { version = "0.32.0", path = "../glutin", default-features = false } raw-window-handle = "0.6" -winit = { version = "0.30.0", default-features = false, features = ["rwh_06"] } +# winit = { version = "0.30.0", default-features = false, features = ["rwh_06"] } +winit = { git = "https://github.com/richerfu/winit.git", branch = "feat-ohos-30", default-features = false, features = [ + "rwh_06", +] } [build-dependencies] cfg_aliases = "0.2.1" diff --git a/glutin_examples/Cargo.toml b/glutin_examples/Cargo.toml index 3d00eb7202..057809f587 100644 --- a/glutin_examples/Cargo.toml +++ b/glutin_examples/Cargo.toml @@ -23,15 +23,26 @@ glutin = { path = "../glutin", default-features = false } glutin-winit = { path = "../glutin-winit", default-features = false } png = { version = "0.17.6", optional = true } raw-window-handle = "0.6" -winit = { version = "0.30.0", default-features = false, features = ["rwh_06"] } +# winit = { version = "0.30.0", default-features = false, features = ["rwh_06"] } +winit = { git = "https://github.com/richerfu/winit.git", branch = "feat-ohos-30", default-features = false, features = [ + "rwh_06", +] } 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"] } +# winit = { version = "0.30.0", default-features = false, features = ["android-native-activity", "rwh_06"] } +winit = { git = "https://github.com/richerfu/winit.git", branch = "feat-ohos-30", default-features = false, features = [ + "rwh_06", + "android-native-activity" +] } [target.'cfg(target_env = "ohos")'.dependencies] -openharmony_ability = { version = "0.0.1" } -openharmony_ability_derive = { version = "0.0.1" } +openharmony-ability = { version = "0.0.1" } +openharmony-ability-derive = { version = "0.0.1" } +winit = { git = "https://github.com/richerfu/winit.git", branch = "feat-ohos-30", default-features = false, features = [ + "rwh_06", +] } +napi-ohos = {version = "1.0", features = ["napi8", "async"]} [build-dependencies] gl_generator = "0.14" @@ -42,7 +53,7 @@ name = "android" crate-type = ["cdylib"] [[example]] -name = "ohos_test" +name = "ohos" crate-type = ["cdylib"] [[example]] diff --git a/glutin_examples/build.rs b/glutin_examples/build.rs index 2fbd6cc476..b7b3e11ec8 100644 --- a/glutin_examples/build.rs +++ b/glutin_examples/build.rs @@ -12,11 +12,12 @@ fn main() { cfg_aliases! { // Systems. android_platform: { target_os = "android" }, + ohos_platform: { target_env = "ohos" }, 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/examples/ohos.rs b/glutin_examples/examples/ohos.rs index f74fc9151e..fd59e8562d 100644 --- a/glutin_examples/examples/ohos.rs +++ b/glutin_examples/examples/ohos.rs @@ -6,11 +6,9 @@ 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() + glutin_examples::main(event_loop).unwrap() } diff --git a/glutin_examples/src/lib.rs b/glutin_examples/src/lib.rs index 1214ec21ad..e04583592d 100644 --- a/glutin_examples/src/lib.rs +++ b/glutin_examples/src/lib.rs @@ -22,7 +22,7 @@ use glutin::surface::{Surface, SwapInterval, WindowSurface}; use glutin_winit::{DisplayBuilder, GlWindow}; #[cfg(target_env = "ohos")] -use winit::platform::ohos::EventLoopBuilderExtOpenHarmony; +use winit::platform::ohos::EventLoopExtOpenHarmony; pub mod gl { #![allow(clippy::all)] From e3471549f2d50584223fb3e0317caa382ee4bc81 Mon Sep 17 00:00:00 2001 From: richerfu Date: Thu, 6 Feb 2025 11:23:06 +0800 Subject: [PATCH 3/4] Update deps --- .gitignore | 1 + README.md | 4 +++- glutin_examples/Cargo.toml | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 4461cbc942..c7da511384 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ target/ .DS_Store *~ #*# +glutin_examples/dist diff --git a/README.md b/README.md index e4c878349a..0d4643ebdd 100644 --- a/README.md +++ b/README.md @@ -72,5 +72,7 @@ install [`ohrs`](https://crates.io/crates/ohrs) and start the app using: ```console -$ ohrs build --arch aarch +$ ohrs build --arch aarch -- -p glutin_examples --example ohos ``` + +Then copy the `$PWD/dist/arm64-v8a/libohos.so` into your OpenHarmony or HarmonyNext project. diff --git a/glutin_examples/Cargo.toml b/glutin_examples/Cargo.toml index 057809f587..24534ff0a3 100644 --- a/glutin_examples/Cargo.toml +++ b/glutin_examples/Cargo.toml @@ -37,7 +37,7 @@ winit = { git = "https://github.com/richerfu/winit.git", branch = "feat-ohos-30" ] } [target.'cfg(target_env = "ohos")'.dependencies] -openharmony-ability = { version = "0.0.1" } +openharmony-ability = { version = "0.0.2" } openharmony-ability-derive = { version = "0.0.1" } winit = { git = "https://github.com/richerfu/winit.git", branch = "feat-ohos-30", default-features = false, features = [ "rwh_06", From 68efe277ce951adebceeac67cbbf0514933d29ff Mon Sep 17 00:00:00 2001 From: richerfu Date: Thu, 6 Nov 2025 12:00:58 +0800 Subject: [PATCH 4/4] chore: fmt code --- glutin-winit/Cargo.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/glutin-winit/Cargo.toml b/glutin-winit/Cargo.toml index 821a614773..54d58acf09 100644 --- a/glutin-winit/Cargo.toml +++ b/glutin-winit/Cargo.toml @@ -22,9 +22,10 @@ wayland = ["glutin/wayland", "winit/wayland"] glutin = { version = "0.32.0", path = "../glutin", default-features = false } raw-window-handle = "0.6" # winit = { version = "0.30.0", default-features = false, features = ["rwh_06"] } -winit = { git = "https://github.com/richerfu/winit.git", branch = "feat-ohos-30", default-features = false, features = [ +winit = { git = "https://github.com/richerfu/winit.git", branch = "feat-ohos-3x", default-features = false, features = [ "rwh_06", ] } +# winit = {path = "../../winit", default-features = false, features = ["rwh_06"]} [build-dependencies] cfg_aliases = "0.2.1"