Skip to content

Commit 37adcb2

Browse files
committed
Optimize Android backend a bit using buffer stride
1 parent d9361a1 commit 37adcb2

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

src/backends/android.rs

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for Android
100100
}
101101

102102
let buffer =
103-
vec![Pixel::default(); native_window_buffer.width() * native_window_buffer.height()];
103+
vec![Pixel::default(); native_window_buffer.stride() * native_window_buffer.height()];
104104

105105
Ok(BufferImpl {
106106
native_window_buffer,
@@ -125,7 +125,7 @@ unsafe impl Send for BufferImpl<'_> {}
125125

126126
impl BufferInterface for BufferImpl<'_> {
127127
fn byte_stride(&self) -> NonZeroU32 {
128-
NonZeroU32::new(self.width().get() * 4).unwrap()
128+
NonZeroU32::new(self.native_window_buffer.stride() as u32 * 4).unwrap()
129129
}
130130

131131
fn width(&self) -> NonZeroU32 {
@@ -148,27 +148,19 @@ impl BufferInterface for BufferImpl<'_> {
148148

149149
// TODO: This function is pretty slow this way
150150
fn present(mut self) -> Result<(), SoftBufferError> {
151-
let input_lines = self.buffer.chunks(self.native_window_buffer.width());
152-
for (output, input) in self
153-
.native_window_buffer
154-
.lines()
155-
// Unreachable as we checked before that this is a valid, mappable format
156-
.unwrap()
157-
.zip(input_lines)
158-
{
159-
// .lines() removed the stride
160-
assert_eq!(output.len(), input.len() * 4);
161-
162-
// Write RGB(A) to the output.
163-
// TODO: Use `slice::write_copy_of_slice` once stable and in MSRV.
164-
// TODO(madsmtm): Verify that this compiles down to an efficient copy.
165-
for (i, pixel) in input.iter().enumerate() {
166-
output[i * 4].write(pixel.r);
167-
output[i * 4 + 1].write(pixel.g);
168-
output[i * 4 + 2].write(pixel.b);
169-
output[i * 4 + 3].write(pixel.a);
170-
}
151+
// Unreachable as we checked before that this is a valid, mappable format
152+
let native_buffer = self.native_window_buffer.bytes().unwrap();
153+
154+
// Write RGB(A) to the output.
155+
// TODO: Use `slice::write_copy_of_slice` once stable and in MSRV.
156+
// TODO(madsmtm): Verify that this compiles down to an efficient copy.
157+
for (pixel, output) in self.buffer.iter().zip(native_buffer.chunks_mut(4)) {
158+
output[0].write(pixel.r);
159+
output[1].write(pixel.g);
160+
output[2].write(pixel.b);
161+
output[3].write(pixel.a);
171162
}
163+
172164
Ok(())
173165
}
174166

0 commit comments

Comments
 (0)