Skip to content

Commit

Permalink
BADHACK: Add BufferImpl::stride() getter for Android
Browse files Browse the repository at this point in the history
  • Loading branch information
MarijnS95 committed Nov 8, 2023
1 parent 6a30809 commit 3e41353
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub(crate) fn run(event_loop: EventLoop<()>) {
let red = x % 255;
let green = y % 255;
let blue = (x * y) % 255;
let index = y as usize * width.get() as usize + x as usize;
let index = y as usize * buffer.stride() as usize + x as usize;
buffer[index] = blue | (green << 8) | (red << 16);
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ impl<'a, D: HasDisplayHandle + ?Sized, W: HasWindowHandle> BufferImpl<'a, D, W>
}
}

#[inline]
pub fn stride(&self) -> u32 {
self.0.stride() as u32
}

pub fn age(&self) -> u8 {
todo!()
}
Expand Down
20 changes: 20 additions & 0 deletions src/kms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ pub(crate) struct BufferImpl<'a, D: ?Sized, W: ?Sized> {
/// The current size.
size: (NonZeroU32, NonZeroU32),

/// The current stride/pitch (length of a single row of pixels) in bytes.
stride: NonZeroU32,

/// The display implementation.
display: &'a KmsDisplayImpl<D>,

Expand Down Expand Up @@ -244,6 +247,7 @@ impl<D: ?Sized, W: HasWindowHandle> KmsImpl<D, W> {
.expect("Must set size of surface before calling `buffer_mut()`");

let size = set.size();
let stride = set.pitch();

let [first_buffer, second_buffer] = &mut set.buffers;
let (front_buffer, back_buffer) = if set.first_is_front {
Expand All @@ -264,6 +268,7 @@ impl<D: ?Sized, W: HasWindowHandle> KmsImpl<D, W> {
Ok(BufferImpl {
mapping,
size,
stride,
first_is_front: &mut set.first_is_front,
front_fb,
crtc_handle: self.crtc.handle(),
Expand Down Expand Up @@ -304,6 +309,12 @@ impl<D: ?Sized, W: ?Sized> BufferImpl<'_, D, W> {
bytemuck::cast_slice_mut(self.mapping.as_mut())
}

#[inline]
pub fn stride(&self) -> u32 {
// TODO Return NonZeroU32?
self.stride.get()
}

#[inline]
pub fn age(&self) -> u8 {
*self.front_age
Expand Down Expand Up @@ -403,11 +414,20 @@ impl SharedBuffer {
.and_then(|width| NonZeroU32::new(height).map(|height| (width, height)))
.expect("buffer size is zero")
}

pub(crate) fn pitch(&self) -> NonZeroU32 {
NonZeroU32::new(self.db.pitch()).expect("Pitch (stride in bytes) is zero")
}
}

impl Buffers {
/// Get the size of this buffer.
pub(crate) fn size(&self) -> (NonZeroU32, NonZeroU32) {
self.buffers[0].size()
}

/// Get the pitch (stride) of this buffer.
pub(crate) fn pitch(&self) -> NonZeroU32 {
self.buffers[0].pitch()
}
}
14 changes: 14 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,15 @@ macro_rules! make_dispatch {
}
}

pub fn stride(&self) -> u32 {
match self {
$(
$(#[$attr])*
Self::$name(inner) => inner.stride(),

Check failure on line 160 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Tests (1.65.0, x86_64-pc-windows-msvc, windows-latest)

no method named `stride` found for reference `&BufferImpl<'_, D, W>` in the current scope

Check failure on line 160 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Tests (1.65.0, i686-pc-windows-msvc, windows-latest)

no method named `stride` found for reference `&BufferImpl<'_, D, W>` in the current scope

Check failure on line 160 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Tests (1.65.0, x86_64-unknown-redox, ubuntu-latest)

no method named `stride` found for reference `&BufferImpl<'_, D, W>` in the current scope

Check failure on line 160 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Tests (1.65.0, x86_64-apple-darwin, macos-latest)

no method named `stride` found for reference `&BufferImpl<'_, D, W>` in the current scope

Check failure on line 160 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Tests (1.65.0, wasm32-unknown-unknown, ubuntu-latest)

no method named `stride` found for reference `&BufferImpl<'_, D, W>` in the current scope

Check failure on line 160 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-pc-windows-msvc, windows-latest)

no method named `stride` found for reference `&BufferImpl<'_, D, W>` in the current scope

Check failure on line 160 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, i686-pc-windows-msvc, windows-latest)

no method named `stride` found for reference `&BufferImpl<'_, D, W>` in the current scope

Check failure on line 160 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-unknown-redox, ubuntu-latest)

no method named `stride` found for reference `&BufferImpl<'_, D, W>` in the current scope

Check failure on line 160 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-apple-darwin, macos-latest)

no method named `stride` found for reference `&BufferImpl<'_, D, W>` in the current scope

Check failure on line 160 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, wasm32-unknown-unknown, ubuntu-latest)

no method named `stride` found for reference `&BufferImpl<'_, D, W>` in the current scope

Check failure on line 160 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-pc-windows-msvc, windows-latest)

no method named `stride` found for reference `&BufferImpl<'_, D, W>` in the current scope

Check failure on line 160 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, i686-pc-windows-msvc, windows-latest)

no method named `stride` found for reference `&BufferImpl<'_, D, W>` in the current scope

Check failure on line 160 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-unknown-redox, ubuntu-latest)

no method named `stride` found for reference `&BufferImpl<'_, D, W>` in the current scope

Check failure on line 160 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-apple-darwin, macos-latest)

no method named `stride` found for reference `&BufferImpl<'_, D, W>` in the current scope

Check failure on line 160 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, wasm32-unknown-unknown, ubuntu-latest)

no method named `stride` found for reference `&BufferImpl<'_, D, W>` in the current scope

Check failure on line 160 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, wasm32-unknown-unknown, ubuntu-latest, -Zbuild-std=panic_abort,std, -Ctarget-feat...

no method named `stride` found for reference `&BufferImpl<'_, D, W>` in the current scope
)*
}
}

pub fn present(self) -> Result<(), SoftBufferError> {
match self {
$(
Expand Down Expand Up @@ -413,6 +422,11 @@ impl<'a, D: HasDisplayHandle, W: HasWindowHandle> Buffer<'a, D, W> {
self.buffer_impl.age()
}

/// Stride in pixels
pub fn stride(&self) -> u32 {
self.buffer_impl.stride()
}

/// Presents buffer to the window.
///
/// # Platform dependent behavior
Expand Down
7 changes: 7 additions & 0 deletions src/wayland/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> WaylandImpl<D, W> {
Ok(unsafe { buffer.buffers.as_mut().unwrap().1.mapped_mut() })
})?,
age,
width: width.get() as u32,
})
}

Expand Down Expand Up @@ -238,6 +239,7 @@ impl<D: ?Sized, W: ?Sized> Drop for WaylandImpl<D, W> {
pub struct BufferImpl<'a, D: ?Sized, W> {
stack: util::BorrowStack<'a, WaylandImpl<D, W>, [u32]>,
age: u8,
width: u32,
}

impl<'a, D: HasDisplayHandle + ?Sized, W: HasWindowHandle> BufferImpl<'a, D, W> {
Expand All @@ -255,6 +257,11 @@ impl<'a, D: HasDisplayHandle + ?Sized, W: HasWindowHandle> BufferImpl<'a, D, W>
self.age
}

#[inline]
pub fn stride(&self) -> u32 {
self.width
}

pub fn present_with_damage(self, damage: &[Rect]) -> Result<(), SoftBufferError> {
self.stack.into_container().present_with_damage(damage)
}
Expand Down
10 changes: 10 additions & 0 deletions src/x11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,16 @@ impl<'a, D: HasDisplayHandle + ?Sized, W: HasWindowHandle + ?Sized> BufferImpl<'
}
}

#[inline]
pub fn stride(&self) -> u32 {
let (surface_width, _surface_height) = self
.0
.size
.expect("Must set size of surface before calling `present_with_damage()`");

surface_width.get() as u32
}

/// Push the buffer to the window.
pub fn present_with_damage(self, damage: &[Rect]) -> Result<(), SoftBufferError> {
let imp = self.0;
Expand Down

0 comments on commit 3e41353

Please sign in to comment.