Winit: Share the resized size instead of using egl display field (#328)

This commit is contained in:
i509VCB 2021-07-04 16:39:01 -05:00 committed by GitHub
parent f27658b759
commit fb11dcb251
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 6 deletions

View File

@ -29,6 +29,7 @@ use winit::{
}; };
use slog::{debug, error, info, o, trace, warn}; use slog::{debug, error, info, o, trace, warn};
use std::cell::Cell;
/// Errors thrown by the `winit` backends /// Errors thrown by the `winit` backends
#[derive(thiserror::Error, Debug)] #[derive(thiserror::Error, Debug)]
@ -64,6 +65,7 @@ pub struct WinitGraphicsBackend {
egl: Rc<EGLSurface>, egl: Rc<EGLSurface>,
window: Rc<WinitWindow>, window: Rc<WinitWindow>,
size: Rc<RefCell<WindowSize>>, size: Rc<RefCell<WindowSize>>,
resize_notification: Rc<Cell<Option<(u32, u32)>>>,
} }
/// Abstracted event loop of a [`WinitWindow`] implementing the [`InputBackend`] trait /// Abstracted event loop of a [`WinitWindow`] implementing the [`InputBackend`] trait
@ -72,7 +74,6 @@ pub struct WinitGraphicsBackend {
/// periodically to receive any events. /// periodically to receive any events.
#[derive(Debug)] #[derive(Debug)]
pub struct WinitInputBackend { pub struct WinitInputBackend {
egl: Rc<EGLSurface>,
window: Rc<WinitWindow>, window: Rc<WinitWindow>,
events_loop: EventLoop<()>, events_loop: EventLoop<()>,
time: Instant, time: Instant,
@ -80,6 +81,7 @@ pub struct WinitInputBackend {
logger: ::slog::Logger, logger: ::slog::Logger,
initialized: bool, initialized: bool,
size: Rc<RefCell<WindowSize>>, size: Rc<RefCell<WindowSize>>,
resize_notification: Rc<Cell<Option<(u32, u32)>>>,
} }
/// Create a new [`WinitGraphicsBackend`], which implements the [`Renderer`] trait and a corresponding [`WinitInputBackend`], /// Create a new [`WinitGraphicsBackend`], which implements the [`Renderer`] trait and a corresponding [`WinitInputBackend`],
@ -185,19 +187,21 @@ where
let window = Rc::new(winit_window); let window = Rc::new(winit_window);
let egl = Rc::new(surface); let egl = Rc::new(surface);
let renderer = unsafe { Gles2Renderer::new(context, log.clone())? }; let renderer = unsafe { Gles2Renderer::new(context, log.clone())? };
let resize_notification = Rc::new(Cell::new(None));
Ok(( Ok((
WinitGraphicsBackend { WinitGraphicsBackend {
window: window.clone(), window: window.clone(),
display, display,
egl: egl.clone(), egl,
renderer, renderer,
size: size.clone(), size: size.clone(),
resize_notification: resize_notification.clone(),
}, },
WinitInputBackend { WinitInputBackend {
resize_notification,
events_loop, events_loop,
window, window,
egl,
time: Instant::now(), time: Instant::now(),
key_counter: 0, key_counter: 0,
initialized: false, initialized: false,
@ -245,6 +249,11 @@ impl WinitGraphicsBackend {
where where
F: FnOnce(&mut Gles2Renderer, &mut Gles2Frame) -> R, F: FnOnce(&mut Gles2Renderer, &mut Gles2Frame) -> R,
{ {
// Were we told to resize?
if let Some((width, height)) = self.resize_notification.take() {
self.egl.resize(width as i32, height as i32, 0, 0);
}
let (width, height) = { let (width, height) = {
let size = self.size.borrow(); let size = self.size.borrow();
size.physical_size.into() size.physical_size.into()
@ -621,7 +630,7 @@ impl InputBackend for WinitInputBackend {
let key_counter = &mut self.key_counter; let key_counter = &mut self.key_counter;
let time = &self.time; let time = &self.time;
let window = &self.window; let window = &self.window;
let egl = &self.egl; let resize_notification = &self.resize_notification;
let logger = &self.logger; let logger = &self.logger;
let window_size = &self.size; let window_size = &self.size;
@ -651,7 +660,9 @@ impl InputBackend for WinitInputBackend {
let mut wsize = window_size.borrow_mut(); let mut wsize = window_size.borrow_mut();
wsize.physical_size = psize; wsize.physical_size = psize;
wsize.scale_factor = scale_factor; wsize.scale_factor = scale_factor;
egl.resize(psize.width as i32, psize.height as i32, 0, 0);
resize_notification.set(Some((psize.width, psize.height)));
callback(InputEvent::Special(WinitEvent::Resized { callback(InputEvent::Special(WinitEvent::Resized {
size: psize.into(), size: psize.into(),
scale_factor, scale_factor,
@ -667,7 +678,9 @@ impl InputBackend for WinitInputBackend {
} => { } => {
let mut wsize = window_size.borrow_mut(); let mut wsize = window_size.borrow_mut();
wsize.scale_factor = scale_factor; wsize.scale_factor = scale_factor;
egl.resize(new_psize.width as i32, new_psize.height as i32, 0, 0);
resize_notification.set(Some((new_psize.width, new_psize.height)));
let psize_f64: (f64, f64) = (new_psize.width.into(), new_psize.height.into()); let psize_f64: (f64, f64) = (new_psize.width.into(), new_psize.height.into());
callback(InputEvent::Special(WinitEvent::Resized { callback(InputEvent::Special(WinitEvent::Resized {
size: psize_f64, size: psize_f64,