Skip to content

Add Buffer::width and Buffer::height #269

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
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- Update to `objc2` 0.6.0.
- Bump MSRV to Rust 1.71.
- Added `Buffer::width` and `Buffer::height`.

# 0.4.6

Expand Down
20 changes: 20 additions & 0 deletions src/backend_dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,26 @@ macro_rules! make_dispatch {
}

impl<'a, D: HasDisplayHandle, W: HasWindowHandle> BufferInterface for BufferDispatch<'a, D, W> {
#[inline]
fn width(&self) -> usize {
match self {
$(
$(#[$attr])*
Self::$name(inner) => inner.width(),
)*
}
}

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

#[inline]
fn pixels(&self) -> &[u32] {
match self {
Expand Down
2 changes: 2 additions & 0 deletions src/backend_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ pub(crate) trait SurfaceInterface<D: HasDisplayHandle + ?Sized, W: HasWindowHand
}

pub(crate) trait BufferInterface {
fn width(&self) -> usize;
fn height(&self) -> usize;
fn pixels(&self) -> &[u32];
fn pixels_mut(&mut self) -> &mut [u32];
fn age(&self) -> u8;
Expand Down
8 changes: 8 additions & 0 deletions src/backends/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ pub struct BufferImpl<'a, D: ?Sized, W> {
unsafe impl<'a, D, W> Send for BufferImpl<'a, D, W> {}

impl<'a, D: HasDisplayHandle, W: HasWindowHandle> BufferInterface for BufferImpl<'a, D, W> {
fn width(&self) -> usize {
self.native_window_buffer.width()
}

fn height(&self) -> usize {
self.native_window_buffer.height()
}

#[inline]
fn pixels(&self) -> &[u32] {
&self.buffer
Expand Down
8 changes: 8 additions & 0 deletions src/backends/cg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,14 @@ pub struct BufferImpl<'a, D, W> {
}

impl<D: HasDisplayHandle, W: HasWindowHandle> BufferInterface for BufferImpl<'_, D, W> {
fn width(&self) -> usize {
self.imp.width
}

fn height(&self) -> usize {
self.imp.height
}

#[inline]
fn pixels(&self) -> &[u32] {
&self.buffer
Expand Down
8 changes: 8 additions & 0 deletions src/backends/kms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,14 @@ impl<D: ?Sized, W: ?Sized> Drop for KmsImpl<D, W> {
}

impl<D: ?Sized, W: ?Sized> BufferInterface for BufferImpl<'_, D, W> {
fn width(&self) -> usize {
self.size.0.get() as usize
}

fn height(&self) -> usize {
self.size.1.get() as usize
}

#[inline]
fn pixels(&self) -> &[u32] {
bytemuck::cast_slice(self.mapping.as_ref())
Expand Down
8 changes: 8 additions & 0 deletions src/backends/orbital.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,14 @@ pub struct BufferImpl<'a, D, W> {
}

impl<D: HasDisplayHandle, W: HasWindowHandle> BufferInterface for BufferImpl<'_, D, W> {
fn width(&self) -> usize {
self.imp.width as usize
}

fn height(&self) -> usize {
self.imp.height as usize
}

#[inline]
fn pixels(&self) -> &[u32] {
match &self.pixels {
Expand Down
8 changes: 8 additions & 0 deletions src/backends/wayland/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,14 @@
}

impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> BufferInterface for BufferImpl<'_, D, W> {
fn width(&self) -> usize {
self.stack.member().width

Check failure on line 267 in src/backends/wayland/mod.rs

View workflow job for this annotation

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

no field `width` on type `&[u32]`

Check failure on line 267 in src/backends/wayland/mod.rs

View workflow job for this annotation

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

no field `width` on type `&[u32]`

Check failure on line 267 in src/backends/wayland/mod.rs

View workflow job for this annotation

GitHub Actions / Tests (1.71.0, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, wayland,wayland-dl...

no field `width` on type `&[u32]`

Check failure on line 267 in src/backends/wayland/mod.rs

View workflow job for this annotation

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

no field `width` on type `&[u32]`

Check failure on line 267 in src/backends/wayland/mod.rs

View workflow job for this annotation

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

no field `width` on type `&[u32]`

Check failure on line 267 in src/backends/wayland/mod.rs

View workflow job for this annotation

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

no field `width` on type `&[u32]`

Check failure on line 267 in src/backends/wayland/mod.rs

View workflow job for this annotation

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

no field `width` on type `&[u32]`

Check failure on line 267 in src/backends/wayland/mod.rs

View workflow job for this annotation

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

no field `width` on type `&[u32]`

Check failure on line 267 in src/backends/wayland/mod.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, wayland,wayland-dl...

no field `width` on type `&[u32]`

Check failure on line 267 in src/backends/wayland/mod.rs

View workflow job for this annotation

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

no field `width` on type `&[u32]`

Check failure on line 267 in src/backends/wayland/mod.rs

View workflow job for this annotation

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

no field `width` on type `&[u32]`

Check failure on line 267 in src/backends/wayland/mod.rs

View workflow job for this annotation

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

no field `width` on type `&[u32]`

Check failure on line 267 in src/backends/wayland/mod.rs

View workflow job for this annotation

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

no field `width` on type `&[u32]`

Check failure on line 267 in src/backends/wayland/mod.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, wayland,wayland-d...

no field `width` on type `&[u32]`

Check failure on line 267 in src/backends/wayland/mod.rs

View workflow job for this annotation

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

no field `width` on type `&[u32]`
}

fn height(&self) -> usize {
self.stack.member().height

Check failure on line 271 in src/backends/wayland/mod.rs

View workflow job for this annotation

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

no field `height` on type `&[u32]`

Check failure on line 271 in src/backends/wayland/mod.rs

View workflow job for this annotation

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

no field `height` on type `&[u32]`

Check failure on line 271 in src/backends/wayland/mod.rs

View workflow job for this annotation

GitHub Actions / Tests (1.71.0, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, wayland,wayland-dl...

no field `height` on type `&[u32]`

Check failure on line 271 in src/backends/wayland/mod.rs

View workflow job for this annotation

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

no field `height` on type `&[u32]`

Check failure on line 271 in src/backends/wayland/mod.rs

View workflow job for this annotation

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

no field `height` on type `&[u32]`

Check failure on line 271 in src/backends/wayland/mod.rs

View workflow job for this annotation

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

no field `height` on type `&[u32]`

Check failure on line 271 in src/backends/wayland/mod.rs

View workflow job for this annotation

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

no field `height` on type `&[u32]`

Check failure on line 271 in src/backends/wayland/mod.rs

View workflow job for this annotation

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

no field `height` on type `&[u32]`

Check failure on line 271 in src/backends/wayland/mod.rs

View workflow job for this annotation

GitHub Actions / Tests (stable, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, wayland,wayland-dl...

no field `height` on type `&[u32]`

Check failure on line 271 in src/backends/wayland/mod.rs

View workflow job for this annotation

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

no field `height` on type `&[u32]`

Check failure on line 271 in src/backends/wayland/mod.rs

View workflow job for this annotation

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

no field `height` on type `&[u32]`

Check failure on line 271 in src/backends/wayland/mod.rs

View workflow job for this annotation

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

no field `height` on type `&[u32]`

Check failure on line 271 in src/backends/wayland/mod.rs

View workflow job for this annotation

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

no field `height` on type `&[u32]`

Check failure on line 271 in src/backends/wayland/mod.rs

View workflow job for this annotation

GitHub Actions / Tests (nightly, x86_64-unknown-linux-gnu, ubuntu-latest, --no-default-features, wayland,wayland-d...

no field `height` on type `&[u32]`

Check failure on line 271 in src/backends/wayland/mod.rs

View workflow job for this annotation

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

no field `height` on type `&[u32]`
}

#[inline]
fn pixels(&self) -> &[u32] {
self.stack.member()
Expand Down
16 changes: 16 additions & 0 deletions src/backends/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,22 @@ pub struct BufferImpl<'a, D, W> {
}

impl<D: HasDisplayHandle, W: HasWindowHandle> BufferInterface for BufferImpl<'_, D, W> {
fn width(&self) -> usize {
self.imp
.size
.expect("must set size of surface before calling `width()` on the buffer")
.0
.get() as usize
}

fn height(&self) -> usize {
self.imp
.size
.expect("must set size of surface before calling `height()` on the buffer")
.1
.get() as usize
}

fn pixels(&self) -> &[u32] {
&self.imp.buffer
}
Expand Down
8 changes: 8 additions & 0 deletions src/backends/win32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,14 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for Win32Im
pub struct BufferImpl<'a, D, W>(&'a mut Win32Impl<D, W>);

impl<D: HasDisplayHandle, W: HasWindowHandle> BufferInterface for BufferImpl<'_, D, W> {
fn width(&self) -> usize {
self.0.buffer.as_ref().unwrap().width.get() as usize
}

fn height(&self) -> usize {
self.0.buffer.as_ref().unwrap().height.get() as usize
}

#[inline]
fn pixels(&self) -> &[u32] {
self.0.buffer.as_ref().unwrap().pixels()
Expand Down
8 changes: 8 additions & 0 deletions src/backends/x11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,14 @@ pub struct BufferImpl<'a, D: ?Sized, W: ?Sized>(&'a mut X11Impl<D, W>);
impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle + ?Sized> BufferInterface
for BufferImpl<'_, D, W>
{
fn width(&self) -> usize {
self.0.size.unwrap().0.get() as usize
}

fn height(&self) -> usize {
self.0.size.unwrap().1.get() as usize
}

#[inline]
fn pixels(&self) -> &[u32] {
// SAFETY: We called `finish_wait` on the buffer, so it is safe to call `buffer()`.
Expand Down
22 changes: 22 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,28 @@ pub struct Buffer<'a, D, W> {
}

impl<D: HasDisplayHandle, W: HasWindowHandle> Buffer<'_, D, W> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit unsure whether these should go on the buffer or the surface? Or does it make sense both places?

/// The amount of pixels wide the buffer is.
pub fn width(&self) -> usize {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went with usize here, since it was just much more usable in examples.

I'd like to change resize to follow this direction as well, see:
#238

let width = self.buffer_impl.width();
debug_assert_eq!(
width * self.buffer_impl.height(),
self.len(),
"buffer must be sized correctly"
);
width
}

/// The amount of pixels tall the buffer is.
pub fn height(&self) -> usize {
let height = self.buffer_impl.height();
debug_assert_eq!(
height * self.buffer_impl.width(),
self.len(),
"buffer must be sized correctly"
);
height
}

/// `age` is the number of frames ago this buffer was last presented. So if the value is
/// `1`, it is the same as the last frame, and if it is `2`, it is the same as the frame
/// before that (for backends using double buffering). If the value is `0`, it is a new
Expand Down
Loading