-
Notifications
You must be signed in to change notification settings - Fork 59
feat: add OpenHarmony platform support #261
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
Open
richerfu
wants to merge
1
commit into
rust-windowing:master
Choose a base branch
from
richerfu:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,7 @@ | |
|
||
# X11 | ||
/src/x11.rs @notgull | ||
|
||
|
||
# OpenHarmony | ||
/src/ohos.rs @richerfu |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#![cfg(target_env = "ohos")] | ||
|
||
pub use winit::platform::ohos::{ability::OpenHarmonyApp, EventLoopBuilderExtOpenHarmony}; | ||
use winit::{event_loop::EventLoop, platform::ohos::ability::ability}; | ||
|
||
#[path = "winit.rs"] | ||
mod desktop_example; | ||
|
||
/// Run with `ohrs build -- --example winit_ohos` | ||
#[ability] | ||
fn openharmony(app: OpenHarmonyApp) { | ||
let mut builder = EventLoop::builder(); | ||
|
||
// Install the Android event loop extension if necessary. | ||
builder.with_openharmony_app(app); | ||
|
||
desktop_example::entry(builder.build().unwrap()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
//! Implementation of software buffering for OpenHarmony. | ||
|
||
use std::marker::PhantomData; | ||
use std::num::{NonZeroI32, NonZeroU32}; | ||
|
||
#[cfg(doc)] | ||
use raw_window_handle::OhosNdkWindowHandle; | ||
use raw_window_handle::{HasDisplayHandle, HasWindowHandle, RawWindowHandle}; | ||
|
||
use crate::error::InitError; | ||
use crate::{BufferInterface, Rect, SoftBufferError, SurfaceInterface}; | ||
use ohos_native_window_binding::{NativeBufferFormat, NativeWindow, NativeWindowBuffer}; | ||
|
||
/// The handle to a window for software buffering. | ||
pub struct OpenHarmonyImpl<D, W> { | ||
native_window: NativeWindow, | ||
window: W, | ||
_display: PhantomData<D>, | ||
} | ||
|
||
impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for OpenHarmonyImpl<D, W> { | ||
type Context = D; | ||
type Buffer<'a> | ||
= BufferImpl<'a, D, W> | ||
where | ||
Self: 'a; | ||
|
||
/// Create a new [`OpenHarmonyImpl`] from an [`OhosNdkWindowHandle`]. | ||
fn new(window: W, _display: &Self::Context) -> Result<Self, InitError<W>> { | ||
let raw = window.window_handle()?.as_raw(); | ||
let RawWindowHandle::OhosNdk(a) = raw else { | ||
return Err(InitError::Unsupported(window)); | ||
}; | ||
|
||
// Acquire a new owned reference to the window, that will be freed on drop. | ||
// SAFETY: We have confirmed that the window handle is valid. | ||
let native_window = NativeWindow::clone_from_ptr(a.native_window.as_ptr()); | ||
|
||
Ok(Self { | ||
native_window, | ||
_display: PhantomData, | ||
window, | ||
}) | ||
} | ||
|
||
#[inline] | ||
fn window(&self) -> &W { | ||
&self.window | ||
} | ||
|
||
/// Also changes the pixel format to [`HardwareBufferFormat::R8G8B8A8_UNORM`]. | ||
fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> { | ||
let (width, height) = (|| { | ||
let width = NonZeroI32::try_from(width).ok()?; | ||
let height = NonZeroI32::try_from(height).ok()?; | ||
Some((width, height)) | ||
})() | ||
.ok_or(SoftBufferError::SizeOutOfRange { width, height })?; | ||
|
||
self.native_window | ||
.set_buffer_geometry(width.into(), height.into()) | ||
.map_err(|err| { | ||
SoftBufferError::PlatformError( | ||
Some("Failed to set buffer geometry on NativeWindow".to_owned()), | ||
Some(Box::new(err)), | ||
) | ||
}) | ||
} | ||
|
||
fn buffer_mut(&mut self) -> Result<BufferImpl<'_, D, W>, SoftBufferError> { | ||
let native_window_buffer = self.native_window.request_buffer(None).map_err(|err| { | ||
SoftBufferError::PlatformError( | ||
Some("Failed to request native window buffer".to_owned()), | ||
Some(Box::new(err)), | ||
) | ||
})?; | ||
|
||
if !matches!( | ||
native_window_buffer.format(), | ||
// These are the only formats we support | ||
NativeBufferFormat::RGBA_8888 | NativeBufferFormat::RGBX_8888 | ||
) { | ||
return Err(SoftBufferError::PlatformError( | ||
Some(format!( | ||
"Unexpected buffer format {:?}, please call \ | ||
.resize() first to change it to RGBx8888", | ||
native_window_buffer.format() | ||
)), | ||
None, | ||
)); | ||
} | ||
let size = (native_window_buffer.width() * native_window_buffer.height()) | ||
.try_into() | ||
.map_err(|e| { | ||
SoftBufferError::PlatformError( | ||
Some("Failed to convert width to u32".to_owned()), | ||
Some(Box::new(e)), | ||
) | ||
})?; | ||
let buffer = vec![0; size]; | ||
|
||
Ok(BufferImpl { | ||
native_window_buffer, | ||
buffer, | ||
marker: PhantomData, | ||
}) | ||
} | ||
|
||
/// Fetch the buffer from the window. | ||
fn fetch(&mut self) -> Result<Vec<u32>, SoftBufferError> { | ||
Err(SoftBufferError::Unimplemented) | ||
} | ||
} | ||
|
||
pub struct BufferImpl<'a, D: ?Sized, W> { | ||
native_window_buffer: NativeWindowBuffer<'a>, | ||
buffer: Vec<u32>, | ||
marker: PhantomData<(&'a D, &'a W)>, | ||
} | ||
|
||
unsafe impl<'a, D, W> Send for BufferImpl<'a, D, W> {} | ||
|
||
impl<'a, D: HasDisplayHandle, W: HasWindowHandle> BufferInterface for BufferImpl<'a, D, W> { | ||
#[inline] | ||
fn pixels(&self) -> &[u32] { | ||
&self.buffer | ||
} | ||
|
||
#[inline] | ||
fn pixels_mut(&mut self) -> &mut [u32] { | ||
&mut self.buffer | ||
} | ||
|
||
#[inline] | ||
fn age(&self) -> u8 { | ||
0 | ||
} | ||
|
||
// TODO: This function is pretty slow this way | ||
fn present(mut self) -> Result<(), SoftBufferError> { | ||
let input_lines = self.buffer.chunks(self.native_window_buffer.width() as _); | ||
for (output, input) in self | ||
.native_window_buffer | ||
.lines() | ||
// Unreachable as we checked before that this is a valid, mappable format | ||
.unwrap() | ||
.zip(input_lines) | ||
{ | ||
// .lines() removed the stride | ||
assert_eq!(output.len(), input.len() * 4); | ||
|
||
for (i, pixel) in input.iter().enumerate() { | ||
// Swizzle colors from RGBX to BGR | ||
let [b, g, r, _] = pixel.to_le_bytes(); | ||
output[i * 4].write(b); | ||
output[i * 4 + 1].write(g); | ||
output[i * 4 + 2].write(r); | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn present_with_damage(self, _damage: &[Rect]) -> Result<(), SoftBufferError> { | ||
self.present() | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.