Owned pixel buffer for no-copy presentation
This is based on the API that will be used for no-copy presentation. But wraps it in `set_buffer`. This also fixes the Wayland buffer code to set `self.width` and `self.height` on resize, and set the length of the shared memory file when the buffer is created. Co-authored-by: jtnunley <jtnunley01@gmail.com>
This commit is contained in:
parent
e5d546ff9e
commit
a09e4cf679
19 changed files with 1176 additions and 438 deletions
|
|
@ -1,4 +1,5 @@
|
|||
use image::GenericImageView;
|
||||
use std::num::NonZeroU32;
|
||||
use winit::event::{Event, WindowEvent};
|
||||
use winit::event_loop::{ControlFlow, EventLoop};
|
||||
use winit::window::WindowBuilder;
|
||||
|
|
@ -6,16 +7,6 @@ use winit::window::WindowBuilder;
|
|||
fn main() {
|
||||
//see fruit.jpg.license for the license of fruit.jpg
|
||||
let fruit = image::load_from_memory(include_bytes!("fruit.jpg")).unwrap();
|
||||
let buffer = fruit
|
||||
.pixels()
|
||||
.map(|(_x, _y, pixel)| {
|
||||
let red = pixel.0[0] as u32;
|
||||
let green = pixel.0[1] as u32;
|
||||
let blue = pixel.0[2] as u32;
|
||||
|
||||
blue | (green << 8) | (red << 16)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let event_loop = EventLoop::new();
|
||||
let window = WindowBuilder::new()
|
||||
|
|
@ -45,7 +36,25 @@ fn main() {
|
|||
|
||||
match event {
|
||||
Event::RedrawRequested(window_id) if window_id == window.id() => {
|
||||
surface.set_buffer(&buffer, fruit.width() as u16, fruit.height() as u16);
|
||||
surface
|
||||
.resize(
|
||||
NonZeroU32::new(fruit.width()).unwrap(),
|
||||
NonZeroU32::new(fruit.height()).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let mut buffer = surface.buffer_mut().unwrap();
|
||||
let width = fruit.width() as usize;
|
||||
for (x, y, pixel) in fruit.pixels() {
|
||||
let red = pixel.0[0] as u32;
|
||||
let green = pixel.0[1] as u32;
|
||||
let blue = pixel.0[2] as u32;
|
||||
|
||||
let color = blue | (green << 8) | (red << 16);
|
||||
buffer[y as usize * width + x as usize] = color;
|
||||
}
|
||||
|
||||
buffer.present().unwrap();
|
||||
}
|
||||
Event::WindowEvent {
|
||||
event: WindowEvent::CloseRequested,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue