Skip to content

Commit 3e41353

Browse files
committed
BADHACK: Add BufferImpl::stride() getter for Android
1 parent 6a30809 commit 3e41353

File tree

6 files changed

+57
-1
lines changed

6 files changed

+57
-1
lines changed

examples/winit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub(crate) fn run(event_loop: EventLoop<()>) {
6363
let red = x % 255;
6464
let green = y % 255;
6565
let blue = (x * y) % 255;
66-
let index = y as usize * width.get() as usize + x as usize;
66+
let index = y as usize * buffer.stride() as usize + x as usize;
6767
buffer[index] = blue | (green << 8) | (red << 16);
6868
}
6969
}

src/android.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ impl<'a, D: HasDisplayHandle + ?Sized, W: HasWindowHandle> BufferImpl<'a, D, W>
130130
}
131131
}
132132

133+
#[inline]
134+
pub fn stride(&self) -> u32 {
135+
self.0.stride() as u32
136+
}
137+
133138
pub fn age(&self) -> u8 {
134139
todo!()
135140
}

src/kms.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ pub(crate) struct BufferImpl<'a, D: ?Sized, W: ?Sized> {
107107
/// The current size.
108108
size: (NonZeroU32, NonZeroU32),
109109

110+
/// The current stride/pitch (length of a single row of pixels) in bytes.
111+
stride: NonZeroU32,
112+
110113
/// The display implementation.
111114
display: &'a KmsDisplayImpl<D>,
112115

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

246249
let size = set.size();
250+
let stride = set.pitch();
247251

248252
let [first_buffer, second_buffer] = &mut set.buffers;
249253
let (front_buffer, back_buffer) = if set.first_is_front {
@@ -264,6 +268,7 @@ impl<D: ?Sized, W: HasWindowHandle> KmsImpl<D, W> {
264268
Ok(BufferImpl {
265269
mapping,
266270
size,
271+
stride,
267272
first_is_front: &mut set.first_is_front,
268273
front_fb,
269274
crtc_handle: self.crtc.handle(),
@@ -304,6 +309,12 @@ impl<D: ?Sized, W: ?Sized> BufferImpl<'_, D, W> {
304309
bytemuck::cast_slice_mut(self.mapping.as_mut())
305310
}
306311

312+
#[inline]
313+
pub fn stride(&self) -> u32 {
314+
// TODO Return NonZeroU32?
315+
self.stride.get()
316+
}
317+
307318
#[inline]
308319
pub fn age(&self) -> u8 {
309320
*self.front_age
@@ -403,11 +414,20 @@ impl SharedBuffer {
403414
.and_then(|width| NonZeroU32::new(height).map(|height| (width, height)))
404415
.expect("buffer size is zero")
405416
}
417+
418+
pub(crate) fn pitch(&self) -> NonZeroU32 {
419+
NonZeroU32::new(self.db.pitch()).expect("Pitch (stride in bytes) is zero")
420+
}
406421
}
407422

408423
impl Buffers {
409424
/// Get the size of this buffer.
410425
pub(crate) fn size(&self) -> (NonZeroU32, NonZeroU32) {
411426
self.buffers[0].size()
412427
}
428+
429+
/// Get the pitch (stride) of this buffer.
430+
pub(crate) fn pitch(&self) -> NonZeroU32 {
431+
self.buffers[0].pitch()
432+
}
413433
}

src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,15 @@ macro_rules! make_dispatch {
153153
}
154154
}
155155

156+
pub fn stride(&self) -> u32 {
157+
match self {
158+
$(
159+
$(#[$attr])*
160+
Self::$name(inner) => inner.stride(),
161+
)*
162+
}
163+
}
164+
156165
pub fn present(self) -> Result<(), SoftBufferError> {
157166
match self {
158167
$(
@@ -413,6 +422,11 @@ impl<'a, D: HasDisplayHandle, W: HasWindowHandle> Buffer<'a, D, W> {
413422
self.buffer_impl.age()
414423
}
415424

425+
/// Stride in pixels
426+
pub fn stride(&self) -> u32 {
427+
self.buffer_impl.stride()
428+
}
429+
416430
/// Presents buffer to the window.
417431
///
418432
/// # Platform dependent behavior

src/wayland/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> WaylandImpl<D, W> {
168168
Ok(unsafe { buffer.buffers.as_mut().unwrap().1.mapped_mut() })
169169
})?,
170170
age,
171+
width: width.get() as u32,
171172
})
172173
}
173174

@@ -238,6 +239,7 @@ impl<D: ?Sized, W: ?Sized> Drop for WaylandImpl<D, W> {
238239
pub struct BufferImpl<'a, D: ?Sized, W> {
239240
stack: util::BorrowStack<'a, WaylandImpl<D, W>, [u32]>,
240241
age: u8,
242+
width: u32,
241243
}
242244

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

260+
#[inline]
261+
pub fn stride(&self) -> u32 {
262+
self.width
263+
}
264+
258265
pub fn present_with_damage(self, damage: &[Rect]) -> Result<(), SoftBufferError> {
259266
self.stack.into_container().present_with_damage(damage)
260267
}

src/x11.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,16 @@ impl<'a, D: HasDisplayHandle + ?Sized, W: HasWindowHandle + ?Sized> BufferImpl<'
384384
}
385385
}
386386

387+
#[inline]
388+
pub fn stride(&self) -> u32 {
389+
let (surface_width, _surface_height) = self
390+
.0
391+
.size
392+
.expect("Must set size of surface before calling `present_with_damage()`");
393+
394+
surface_width.get() as u32
395+
}
396+
387397
/// Push the buffer to the window.
388398
pub fn present_with_damage(self, damage: &[Rect]) -> Result<(), SoftBufferError> {
389399
let imp = self.0;

0 commit comments

Comments
 (0)