Skip to content

Commit 25ba8f2

Browse files
committed
Optimize Android backend a bit using buffer stride
1 parent 0438367 commit 25ba8f2

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 {
@@ -158,27 +158,19 @@ impl BufferInterface for BufferImpl<'_> {
158158
// when the enlarged damage region is not re-rendered?
159159
let _ = damage;
160160

161-
let input_lines = self.buffer.chunks(self.native_window_buffer.width());
162-
for (output, input) in self
163-
.native_window_buffer
164-
.lines()
165-
// Unreachable as we checked before that this is a valid, mappable format
166-
.unwrap()
167-
.zip(input_lines)
168-
{
169-
// .lines() removed the stride
170-
assert_eq!(output.len(), input.len() * 4);
171-
172-
// Write RGB(A) to the output.
173-
// TODO: Use `slice::write_copy_of_slice` once stable and in MSRV.
174-
// TODO(madsmtm): Verify that this compiles down to an efficient copy.
175-
for (i, pixel) in input.iter().enumerate() {
176-
output[i * 4].write(pixel.r);
177-
output[i * 4 + 1].write(pixel.g);
178-
output[i * 4 + 2].write(pixel.b);
179-
output[i * 4 + 3].write(pixel.a);
180-
}
161+
// Unreachable as we checked before that this is a valid, mappable format
162+
let native_buffer = self.native_window_buffer.bytes().unwrap();
163+
164+
// Write RGB(A) to the output.
165+
// TODO: Use `slice::write_copy_of_slice` once stable and in MSRV.
166+
// TODO(madsmtm): Verify that this compiles down to an efficient copy.
167+
for (pixel, output) in self.buffer.iter().zip(native_buffer.chunks_mut(4)) {
168+
output[0].write(pixel.r);
169+
output[1].write(pixel.g);
170+
output[2].write(pixel.b);
171+
output[3].write(pixel.a);
181172
}
173+
182174
Ok(())
183175
}
184176
}

0 commit comments

Comments
 (0)