|
| 1 | +//! Implementation of software buffering for OpenHarmony. |
| 2 | +
|
| 3 | +use std::marker::PhantomData; |
| 4 | +use std::num::{NonZeroI32, NonZeroU32}; |
| 5 | + |
| 6 | +#[cfg(doc)] |
| 7 | +use raw_window_handle::OhosNdkWindowHandle; |
| 8 | +use raw_window_handle::{HasDisplayHandle, HasWindowHandle, RawWindowHandle}; |
| 9 | + |
| 10 | +use crate::error::InitError; |
| 11 | +use crate::{BufferInterface, Rect, SoftBufferError, SurfaceInterface}; |
| 12 | +use ohos_native_window_binding::{NativeBufferFormat, NativeWindow, NativeWindowBuffer}; |
| 13 | + |
| 14 | +/// The handle to a window for software buffering. |
| 15 | +pub struct OpenHarmonyImpl<D, W> { |
| 16 | + native_window: NativeWindow, |
| 17 | + window: W, |
| 18 | + _display: PhantomData<D>, |
| 19 | +} |
| 20 | + |
| 21 | +impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for OpenHarmonyImpl<D, W> { |
| 22 | + type Context = D; |
| 23 | + type Buffer<'a> |
| 24 | + = BufferImpl<'a, D, W> |
| 25 | + where |
| 26 | + Self: 'a; |
| 27 | + |
| 28 | + /// Create a new [`OpenHarmonyImpl`] from an [`OhosNdkWindowHandle`]. |
| 29 | + fn new(window: W, _display: &Self::Context) -> Result<Self, InitError<W>> { |
| 30 | + let raw = window.window_handle()?.as_raw(); |
| 31 | + let RawWindowHandle::OhosNdk(a) = raw else { |
| 32 | + return Err(InitError::Unsupported(window)); |
| 33 | + }; |
| 34 | + |
| 35 | + // Acquire a new owned reference to the window, that will be freed on drop. |
| 36 | + // SAFETY: We have confirmed that the window handle is valid. |
| 37 | + let native_window = NativeWindow::clone_from_ptr(a.native_window.as_ptr()); |
| 38 | + |
| 39 | + Ok(Self { |
| 40 | + native_window, |
| 41 | + _display: PhantomData, |
| 42 | + window, |
| 43 | + }) |
| 44 | + } |
| 45 | + |
| 46 | + #[inline] |
| 47 | + fn window(&self) -> &W { |
| 48 | + &self.window |
| 49 | + } |
| 50 | + |
| 51 | + /// Also changes the pixel format to [`HardwareBufferFormat::R8G8B8A8_UNORM`]. |
| 52 | + fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> { |
| 53 | + let (width, height) = (|| { |
| 54 | + let width = NonZeroI32::try_from(width).ok()?; |
| 55 | + let height = NonZeroI32::try_from(height).ok()?; |
| 56 | + Some((width, height)) |
| 57 | + })() |
| 58 | + .ok_or(SoftBufferError::SizeOutOfRange { width, height })?; |
| 59 | + |
| 60 | + self.native_window |
| 61 | + .set_buffer_geometry(width.into(), height.into()) |
| 62 | + .map_err(|err| { |
| 63 | + SoftBufferError::PlatformError( |
| 64 | + Some("Failed to set buffer geometry on NativeWindow".to_owned()), |
| 65 | + Some(Box::new(err)), |
| 66 | + ) |
| 67 | + }) |
| 68 | + } |
| 69 | + |
| 70 | + fn buffer_mut(&mut self) -> Result<BufferImpl<'_, D, W>, SoftBufferError> { |
| 71 | + let native_window_buffer = self.native_window.request_buffer(None).map_err(|err| { |
| 72 | + SoftBufferError::PlatformError( |
| 73 | + Some("Failed to request native window buffer".to_owned()), |
| 74 | + Some(Box::new(err)), |
| 75 | + ) |
| 76 | + })?; |
| 77 | + |
| 78 | + if !matches!( |
| 79 | + native_window_buffer.format(), |
| 80 | + // These are the only formats we support |
| 81 | + NativeBufferFormat::RGBA_8888 | NativeBufferFormat::RGBX_8888 |
| 82 | + ) { |
| 83 | + return Err(SoftBufferError::PlatformError( |
| 84 | + Some(format!( |
| 85 | + "Unexpected buffer format {:?}, please call \ |
| 86 | + .resize() first to change it to RGBx8888", |
| 87 | + native_window_buffer.format() |
| 88 | + )), |
| 89 | + None, |
| 90 | + )); |
| 91 | + } |
| 92 | + let size = (native_window_buffer.width() * native_window_buffer.height()) |
| 93 | + .try_into() |
| 94 | + .map_err(|e| { |
| 95 | + SoftBufferError::PlatformError( |
| 96 | + Some("Failed to convert width to u32".to_owned()), |
| 97 | + Some(Box::new(e)), |
| 98 | + ) |
| 99 | + })?; |
| 100 | + let buffer = vec![0; size]; |
| 101 | + |
| 102 | + Ok(BufferImpl { |
| 103 | + native_window_buffer, |
| 104 | + buffer, |
| 105 | + marker: PhantomData, |
| 106 | + }) |
| 107 | + } |
| 108 | + |
| 109 | + /// Fetch the buffer from the window. |
| 110 | + fn fetch(&mut self) -> Result<Vec<u32>, SoftBufferError> { |
| 111 | + Err(SoftBufferError::Unimplemented) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +pub struct BufferImpl<'a, D: ?Sized, W> { |
| 116 | + native_window_buffer: NativeWindowBuffer<'a>, |
| 117 | + buffer: Vec<u32>, |
| 118 | + marker: PhantomData<(&'a D, &'a W)>, |
| 119 | +} |
| 120 | + |
| 121 | +unsafe impl<'a, D, W> Send for BufferImpl<'a, D, W> {} |
| 122 | + |
| 123 | +impl<'a, D: HasDisplayHandle, W: HasWindowHandle> BufferInterface for BufferImpl<'a, D, W> { |
| 124 | + #[inline] |
| 125 | + fn pixels(&self) -> &[u32] { |
| 126 | + &self.buffer |
| 127 | + } |
| 128 | + |
| 129 | + #[inline] |
| 130 | + fn pixels_mut(&mut self) -> &mut [u32] { |
| 131 | + &mut self.buffer |
| 132 | + } |
| 133 | + |
| 134 | + #[inline] |
| 135 | + fn age(&self) -> u8 { |
| 136 | + 0 |
| 137 | + } |
| 138 | + |
| 139 | + // TODO: This function is pretty slow this way |
| 140 | + fn present(mut self) -> Result<(), SoftBufferError> { |
| 141 | + let input_lines = self.buffer.chunks(self.native_window_buffer.width() as _); |
| 142 | + for (output, input) in self |
| 143 | + .native_window_buffer |
| 144 | + .lines() |
| 145 | + // Unreachable as we checked before that this is a valid, mappable format |
| 146 | + .unwrap() |
| 147 | + .zip(input_lines) |
| 148 | + { |
| 149 | + // .lines() removed the stride |
| 150 | + assert_eq!(output.len(), input.len() * 4); |
| 151 | + |
| 152 | + for (i, pixel) in input.iter().enumerate() { |
| 153 | + // Swizzle colors from RGBX to BGR |
| 154 | + let [b, g, r, _] = pixel.to_le_bytes(); |
| 155 | + output[i * 4].write(b); |
| 156 | + output[i * 4 + 1].write(g); |
| 157 | + output[i * 4 + 2].write(r); |
| 158 | + } |
| 159 | + } |
| 160 | + Ok(()) |
| 161 | + } |
| 162 | + |
| 163 | + fn present_with_damage(self, _damage: &[Rect]) -> Result<(), SoftBufferError> { |
| 164 | + self.present() |
| 165 | + } |
| 166 | +} |
0 commit comments