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 Jun 25, 2024
1 parent 32fe733 commit 0a7f720
Show file tree
Hide file tree
Showing 8 changed files with 67 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 @@ -46,7 +46,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
10 changes: 10 additions & 0 deletions src/backend_dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ macro_rules! make_dispatch {
}
}

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

fn present(self) -> Result<(), SoftBufferError> {
match self {
$(
Expand Down
1 change: 1 addition & 0 deletions src/backend_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub(crate) trait BufferInterface {
fn pixels(&self) -> &[u32];
fn pixels_mut(&mut self) -> &mut [u32];
fn age(&self) -> u8;
fn stride(&self) -> u32;
fn present_with_damage(self, damage: &[Rect]) -> Result<(), SoftBufferError>;
fn present(self) -> Result<(), SoftBufferError>;
}
6 changes: 6 additions & 0 deletions src/backends/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ impl<'a, D: HasDisplayHandle + ?Sized, W: HasWindowHandle> BufferImpl<'a, D, W>
}
}

/// The number of _pixels_ that a line in the buffer takes in memory.
#[inline]
pub fn stride(&self) -> u32 {
self.0.stride() as u32
}

pub fn age(&self) -> u8 {
todo!()
}
Expand Down
27 changes: 27 additions & 0 deletions src/backends/kms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,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 @@ -243,6 +246,7 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> SurfaceInterface<D, W> fo
.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 @@ -263,6 +267,7 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> SurfaceInterface<D, W> fo
Ok(BufferImpl {
mapping,
size,
stride,
first_is_front: &mut set.first_is_front,
front_fb,
crtc_handle: self.crtc.handle(),
Expand Down Expand Up @@ -300,6 +305,19 @@ impl<D: ?Sized, W: ?Sized> BufferInterface for BufferImpl<'_, D, W> {
bytemuck::cast_slice_mut(self.mapping.as_mut())
}

/// The number of _pixels_ that a line in the buffer takes in memory.
#[inline]
pub fn stride(&self) -> u32 {

Check failure on line 310 in src/backends/kms.rs

View workflow job for this annotation

GitHub Actions / Tests (1.70.0, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, kms)

visibility qualifiers are not permitted here

Check failure on line 310 in src/backends/kms.rs

View workflow job for this annotation

GitHub Actions / Tests (1.70.0, x86_64-unknown-linux-gnu, ubuntu-latest)

visibility qualifiers are not permitted here

Check failure on line 310 in src/backends/kms.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, i686-unknown-linux-gnu, ubuntu-latest)

visibility qualifiers are not permitted here

Check failure on line 310 in src/backends/kms.rs

View workflow job for this annotation

GitHub Actions / Tests (1.70.0, x86_64-unknown-freebsd, ubuntu-latest)

visibility qualifiers are not permitted here

Check failure on line 310 in src/backends/kms.rs

View workflow job for this annotation

GitHub Actions / Tests (1.70.0, i686-unknown-linux-gnu, ubuntu-latest)

visibility qualifiers are not permitted here

Check failure on line 310 in src/backends/kms.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, kms)

visibility qualifiers are not permitted here

Check failure on line 310 in src/backends/kms.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, i686-unknown-linux-gnu, ubuntu-latest)

visibility qualifiers are not permitted here

Check failure on line 310 in src/backends/kms.rs

View workflow job for this annotation

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

visibility qualifiers are not permitted here

Check failure on line 310 in src/backends/kms.rs

View workflow job for this annotation

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

visibility qualifiers are not permitted here

Check failure on line 310 in src/backends/kms.rs

View workflow job for this annotation

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

visibility qualifiers are not permitted here

Check failure on line 310 in src/backends/kms.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, kms)

visibility qualifiers are not permitted here

Check failure on line 310 in src/backends/kms.rs

View workflow job for this annotation

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

visibility qualifiers are not permitted here
// TODO Return NonZeroU32?
let bpp: u32 = todo!();

Check failure on line 312 in src/backends/kms.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, i686-unknown-linux-gnu, ubuntu-latest)

unused variable: `bpp`

Check failure on line 312 in src/backends/kms.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, kms)

unused variable: `bpp`

Check failure on line 312 in src/backends/kms.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, i686-unknown-linux-gnu, ubuntu-latest)

unused variable: `bpp`

Check failure on line 312 in src/backends/kms.rs

View workflow job for this annotation

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

unused variable: `bpp`

Check failure on line 312 in src/backends/kms.rs

View workflow job for this annotation

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

unused variable: `bpp`

Check failure on line 312 in src/backends/kms.rs

View workflow job for this annotation

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

unused variable: `bpp`

Check failure on line 312 in src/backends/kms.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, kms)

unused variable: `bpp`

Check failure on line 312 in src/backends/kms.rs

View workflow job for this annotation

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

unused variable: `bpp`
assert_eq!(self.stride.get() & bpp, 0);

Check failure on line 313 in src/backends/kms.rs

View workflow job for this annotation

GitHub Actions / Tests (1.70.0, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, kms)

unreachable statement

Check failure on line 313 in src/backends/kms.rs

View workflow job for this annotation

GitHub Actions / Tests (1.70.0, x86_64-unknown-linux-gnu, ubuntu-latest)

unreachable statement

Check failure on line 313 in src/backends/kms.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, i686-unknown-linux-gnu, ubuntu-latest)

unreachable statement

Check failure on line 313 in src/backends/kms.rs

View workflow job for this annotation

GitHub Actions / Tests (1.70.0, x86_64-unknown-freebsd, ubuntu-latest)

unreachable statement

Check failure on line 313 in src/backends/kms.rs

View workflow job for this annotation

GitHub Actions / Tests (1.70.0, i686-unknown-linux-gnu, ubuntu-latest)

unreachable statement

Check failure on line 313 in src/backends/kms.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, kms)

unreachable statement

Check failure on line 313 in src/backends/kms.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, i686-unknown-linux-gnu, ubuntu-latest)

unreachable statement

Check failure on line 313 in src/backends/kms.rs

View workflow job for this annotation

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

unreachable statement

Check failure on line 313 in src/backends/kms.rs

View workflow job for this annotation

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

unreachable statement

Check failure on line 313 in src/backends/kms.rs

View workflow job for this annotation

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

unreachable statement

Check failure on line 313 in src/backends/kms.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, kms)

unreachable statement

Check failure on line 313 in src/backends/kms.rs

View workflow job for this annotation

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

unreachable statement
// TODO: Since this may not always be a multiple of BPP, this API should return the size in
// bytes... And then the user is left to mess around with `fn pixels()`. We'll need a helper
// iterator accessor like:
// https://docs.rs/ndk/latest/ndk/native_window/struct.NativeWindowBufferLockGuard.html#method.lines
self.stride.get() / bpp
}

#[inline]
fn age(&self) -> u8 {
*self.front_age
Expand Down Expand Up @@ -398,11 +416,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()
}
}
7 changes: 7 additions & 0 deletions src/backends/wayland/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> SurfaceInterface<D, W>
Ok(unsafe { buffer.buffers.as_mut().unwrap().1.mapped_mut() })
})?,
age,
width: width.get() as u32,
})
}
}
Expand All @@ -257,6 +258,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> BufferInterface
Expand All @@ -276,6 +278,11 @@ impl<'a, D: HasDisplayHandle + ?Sized, W: HasWindowHandle> BufferInterface
self.age
}

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

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/backends/x11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,16 @@ impl<'a, D: HasDisplayHandle + ?Sized, W: HasWindowHandle + ?Sized> BufferInterf
}
}

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

Check failure on line 409 in src/backends/x11.rs

View workflow job for this annotation

GitHub Actions / Tests (1.70.0, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, x11,x11-dlopen)

visibility qualifiers are not permitted here

Check failure on line 409 in src/backends/x11.rs

View workflow job for this annotation

GitHub Actions / Tests (1.70.0, x86_64-unknown-netbsd, ubuntu-latest, --no-default-features, x11,x11-dlopen,waylan...

visibility qualifiers are not permitted here

Check failure on line 409 in src/backends/x11.rs

View workflow job for this annotation

GitHub Actions / Tests (1.70.0, x86_64-unknown-linux-gnu, ubuntu-latest)

visibility qualifiers are not permitted here

Check failure on line 409 in src/backends/x11.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, i686-unknown-linux-gnu, ubuntu-latest)

visibility qualifiers are not permitted here

Check failure on line 409 in src/backends/x11.rs

View workflow job for this annotation

GitHub Actions / Tests (1.70.0, x86_64-unknown-freebsd, ubuntu-latest)

visibility qualifiers are not permitted here

Check failure on line 409 in src/backends/x11.rs

View workflow job for this annotation

GitHub Actions / Tests (1.70.0, i686-unknown-linux-gnu, ubuntu-latest)

visibility qualifiers are not permitted here

Check failure on line 409 in src/backends/x11.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, i686-unknown-linux-gnu, ubuntu-latest)

visibility qualifiers are not permitted here

Check failure on line 409 in src/backends/x11.rs

View workflow job for this annotation

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

visibility qualifiers are not permitted here

Check failure on line 409 in src/backends/x11.rs

View workflow job for this annotation

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

visibility qualifiers are not permitted here

Check failure on line 409 in src/backends/x11.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, x11,x11-dlopen)

visibility qualifiers are not permitted here

Check failure on line 409 in src/backends/x11.rs

View workflow job for this annotation

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

visibility qualifiers are not permitted here

Check failure on line 409 in src/backends/x11.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-unknown-netbsd, ubuntu-latest, --no-default-features, x11,x11-dlopen,wayla...

visibility qualifiers are not permitted here

Check failure on line 409 in src/backends/x11.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, x11,x11-dlopen)

visibility qualifiers are not permitted here

Check failure on line 409 in src/backends/x11.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-unknown-netbsd, ubuntu-latest, --no-default-features, x11,x11-dlopen,waylan...

visibility qualifiers are not permitted here

Check failure on line 409 in src/backends/x11.rs

View workflow job for this annotation

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

visibility qualifiers are not permitted here
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.
fn present_with_damage(self, damage: &[Rect]) -> Result<(), SoftBufferError> {
let imp = self.0;
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ impl<'a, D: HasDisplayHandle, W: HasWindowHandle> Buffer<'a, D, W> {
self.buffer_impl.age()
}

/// The number of _pixels_ that a line in the buffer takes in memory.
pub fn stride(&self) -> u32 {
self.buffer_impl.stride()
}

/// Presents buffer to the window.
///
/// # Platform dependent behavior
Expand Down

0 comments on commit 0a7f720

Please sign in to comment.