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(glutin-winit): support openharmony platform #1724

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ target/
.DS_Store
*~
#*#
glutin_examples/dist
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,20 @@ 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 -- -p glutin_examples --example ohos
```

Then copy the `$PWD/dist/arm64-v8a/libohos.so` into your OpenHarmony or HarmonyNext project.
5 changes: 4 additions & 1 deletion glutin-winit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
3 changes: 2 additions & 1 deletion glutin-winit/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)) },
Expand Down
23 changes: 21 additions & 2 deletions glutin_examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +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.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",
] }
napi-ohos = {version = "1.0", features = ["napi8", "async"]}

[build-dependencies]
gl_generator = "0.14"
Expand All @@ -37,6 +52,10 @@ cfg_aliases = "0.2.1"
name = "android"
crate-type = ["cdylib"]

[[example]]
name = "ohos"
crate-type = ["cdylib"]

[[example]]
name = "egl_device"
required-features = ["egl"]
Expand Down
3 changes: 2 additions & 1 deletion glutin_examples/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)) },
Expand Down
14 changes: 14 additions & 0 deletions glutin_examples/examples/ohos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#![cfg(ohos_platform)]

use openharmony_ability::OpenHarmonyApp;
use openharmony_ability_derive::ability;

use winit::event_loop::EventLoop;
use winit::platform::ohos::EventLoopBuilderExtOpenHarmony;

#[ability]
pub fn openharmony(openharmony_app: OpenHarmonyApp) {
let a = openharmony_app.clone();
let event_loop = EventLoop::builder().with_openharmony_app(a).build().unwrap();
glutin_examples::main(event_loop).unwrap()
}
20 changes: 17 additions & 3 deletions glutin_examples/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ use glutin::surface::{Surface, SwapInterval, WindowSurface};

use glutin_winit::{DisplayBuilder, GlWindow};

#[cfg(target_env = "ohos")]
use winit::platform::ohos::EventLoopExtOpenHarmony;

pub mod gl {
#![allow(clippy::all)]
include!(concat!(env!("OUT_DIR"), "/gl_bindings.rs"));
Expand All @@ -42,10 +45,21 @@ pub fn main(event_loop: winit::event_loop::EventLoop<()>) -> Result<(), Box<dyn

let display_builder = DisplayBuilder::new().with_window_attributes(Some(window_attributes()));

let mut app = App::new(template, display_builder);
event_loop.run_app(&mut app)?;
#[cfg(not(target_env = "ohos"))]
{
let mut app = App::new(template, display_builder);
event_loop.run_app(&mut app)?;

app.exit_state
}

app.exit_state
#[cfg(target_env = "ohos")]
{
let app = App::new(template, display_builder);
// For OpenHarmony, we need to use `spawn_app` instead of `run_app`.
event_loop.spawn_app(app);
Ok(())
}
}

impl ApplicationHandler for App {
Expand Down
Loading