Skip to content

Commit

Permalink
feat(glutin-winit): support openharmony platform
Browse files Browse the repository at this point in the history
  • Loading branch information
richerfu committed Jan 18, 2025
1 parent 73ce4f9 commit 631fa1f
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 4 deletions.
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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
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
8 changes: 8 additions & 0 deletions glutin_examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"]
Expand Down
16 changes: 16 additions & 0 deletions glutin_examples/examples/ohos.rs
Original file line number Diff line number Diff line change
@@ -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()
}
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::EventLoopBuilderExtOpenHarmony;

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

0 comments on commit 631fa1f

Please sign in to comment.