Skip to content

Commit a7abd65

Browse files
committed
Optimize Android backend a bit using buffer stride
1 parent badc098 commit a7abd65

File tree

2 files changed

+16
-23
lines changed

2 files changed

+16
-23
lines changed

src/backends/android.rs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for Android
9999
));
100100
}
101101

102-
let buffer = vec![0; native_window_buffer.width() * native_window_buffer.height()];
102+
let buffer = vec![0; native_window_buffer.stride() * native_window_buffer.height()];
103103

104104
Ok(BufferImpl {
105105
native_window_buffer,
@@ -124,7 +124,7 @@ unsafe impl Send for BufferImpl<'_> {}
124124

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

130130
fn width(&self) -> NonZeroU32 {
@@ -147,26 +147,18 @@ impl BufferInterface for BufferImpl<'_> {
147147

148148
// TODO: This function is pretty slow this way
149149
fn present(mut self) -> Result<(), SoftBufferError> {
150-
let input_lines = self.buffer.chunks(self.native_window_buffer.width());
151-
for (output, input) in self
152-
.native_window_buffer
153-
.lines()
154-
// Unreachable as we checked before that this is a valid, mappable format
155-
.unwrap()
156-
.zip(input_lines)
157-
{
158-
// .lines() removed the stride
159-
assert_eq!(output.len(), input.len() * 4);
160-
161-
for (i, pixel) in input.iter().enumerate() {
162-
// Swizzle colors from BGR(A) to RGB(A)
163-
let [b, g, r, a] = pixel.to_le_bytes();
164-
output[i * 4].write(r);
165-
output[i * 4 + 1].write(g);
166-
output[i * 4 + 2].write(b);
167-
output[i * 4 + 3].write(a);
168-
}
150+
// Unreachable as we checked before that this is a valid, mappable format
151+
let native_buffer = self.native_window_buffer.bytes().unwrap();
152+
153+
for (pixel, output) in self.buffer.iter().zip(native_buffer.chunks_mut(4)) {
154+
// Swizzle colors from BGR(A) to RGB(A)
155+
let [b, g, r, a] = pixel.to_le_bytes();
156+
output[0].write(r);
157+
output[1].write(g);
158+
output[2].write(b);
159+
output[3].write(a);
169160
}
161+
170162
Ok(())
171163
}
172164

src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,18 +241,19 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> HasWindowHandle for Surface<D, W>
241241
/// B: Blue channel
242242
///
243243
/// # Platform dependent behavior
244+
///
244245
/// No-copy presentation is currently supported on:
245246
/// - Wayland
246247
/// - X, when XShm is available
247248
/// - Win32
248249
/// - Orbital, when buffer size matches window size
249250
///
250251
/// Currently [`Buffer::present`] must block copying image data on:
251-
/// - Web
252252
/// - AppKit
253253
/// - UIKit
254254
///
255-
/// Buffer copies an channel swizzling happen on:
255+
/// Buffer copies and channel swizzling happen on:
256+
/// - Web
256257
/// - Android
257258
#[derive(Debug)]
258259
pub struct Buffer<'a> {

0 commit comments

Comments
 (0)