From fb4a7968cab89a38870642d7528cb32943d07785 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Thu, 9 Jan 2020 03:56:13 +0100 Subject: [PATCH 1/5] Update `winit` to `0.20` --- native/src/window/renderer.rs | 12 ++++---- wgpu/src/renderer.rs | 44 ++++++++++++++++------------ wgpu/src/renderer/target.rs | 36 +++++++++++------------ wgpu/src/transformation.rs | 6 ++-- winit/Cargo.toml | 2 +- winit/src/application.rs | 55 ++++++++++++++++++++++------------- winit/src/conversion.rs | 8 ++--- 7 files changed, 92 insertions(+), 71 deletions(-) diff --git a/native/src/window/renderer.rs b/native/src/window/renderer.rs index be9765fa..3828f7d4 100644 --- a/native/src/window/renderer.rs +++ b/native/src/window/renderer.rs @@ -39,9 +39,9 @@ pub trait Target { /// [`Target`]: trait.Target.html fn new( window: &W, - width: u16, - height: u16, - dpi: f32, + width: u32, + height: u32, + scale_factor: f32, renderer: &Self::Renderer, ) -> Self; @@ -50,9 +50,9 @@ pub trait Target { /// [`Target`]: trait.Target.html fn resize( &mut self, - width: u16, - height: u16, - dpi: f32, + width: u32, + height: u32, + scale_factor: f32, renderer: &Self::Renderer, ); } diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index 6f35e247..93d2bb13 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -92,7 +92,7 @@ impl Renderer { log::debug!("Drawing"); let (width, height) = target.dimensions(); - let dpi = target.dpi(); + let scale_factor = target.scale_factor(); let transformation = target.transformation(); let frame = target.next_frame(); @@ -132,7 +132,13 @@ impl Renderer { self.draw_overlay(overlay, &mut layers); for layer in layers { - self.flush(dpi, transformation, &layer, &mut encoder, &frame.view); + self.flush( + scale_factor, + transformation, + &layer, + &mut encoder, + &frame.view, + ); } self.queue.submit(&[encoder.finish()]); @@ -330,19 +336,19 @@ impl Renderer { fn flush( &mut self, - dpi: f32, + scale_factor: f32, transformation: Transformation, layer: &Layer<'_>, encoder: &mut wgpu::CommandEncoder, target: &wgpu::TextureView, ) { - let bounds = layer.bounds * dpi; + let bounds = layer.bounds * scale_factor; if layer.meshes.len() > 0 { let translated = transformation * Transformation::translate( - -(layer.offset.x as f32) * dpi, - -(layer.offset.y as f32) * dpi, + -(layer.offset.x as f32) * scale_factor, + -(layer.offset.y as f32) * scale_factor, ); self.triangle_pipeline.draw( @@ -350,7 +356,7 @@ impl Renderer { encoder, target, translated, - dpi, + scale_factor, &layer.meshes, bounds, ); @@ -362,7 +368,7 @@ impl Renderer { encoder, &layer.quads, transformation, - dpi, + scale_factor, bounds, target, ); @@ -370,7 +376,7 @@ impl Renderer { if layer.images.len() > 0 { let translated_and_scaled = transformation - * Transformation::scale(dpi, dpi) + * Transformation::scale(scale_factor, scale_factor) * Transformation::translate( -(layer.offset.x as f32), -(layer.offset.y as f32), @@ -383,7 +389,7 @@ impl Renderer { translated_and_scaled, bounds, target, - dpi, + scale_factor, ); } @@ -396,25 +402,25 @@ impl Renderer { // bit "jumpy". We may be able to do better once we improve // our text rendering/caching pipeline. screen_position: ( - (text.screen_position.0 * dpi).round(), - (text.screen_position.1 * dpi).round(), + (text.screen_position.0 * scale_factor).round(), + (text.screen_position.1 * scale_factor).round(), ), - // TODO: Fix precision issues with some DPI factors. + // TODO: Fix precision issues with some scale factors. // // The `ceil` here can cause some words to render on the // same line when they should not. // // Ideally, `wgpu_glyph` should be able to compute layout // using logical positions, and then apply the proper - // DPI scaling. This would ensure that both measuring and - // rendering follow the same layout rules. + // scaling when rendering. This would ensure that both + // measuring and rendering follow the same layout rules. bounds: ( - (text.bounds.0 * dpi).ceil(), - (text.bounds.1 * dpi).ceil(), + (text.bounds.0 * scale_factor).ceil(), + (text.bounds.1 * scale_factor).ceil(), ), scale: wgpu_glyph::Scale { - x: text.scale.x * dpi, - y: text.scale.y * dpi, + x: text.scale.x * scale_factor, + y: text.scale.y * scale_factor, }, ..*text }; diff --git a/wgpu/src/renderer/target.rs b/wgpu/src/renderer/target.rs index 5fe4b50b..3fd0cd83 100644 --- a/wgpu/src/renderer/target.rs +++ b/wgpu/src/renderer/target.rs @@ -7,20 +7,20 @@ use raw_window_handle::HasRawWindowHandle; #[derive(Debug)] pub struct Target { surface: wgpu::Surface, - width: u16, - height: u16, - dpi: f32, + width: u32, + height: u32, + scale_factor: f32, transformation: Transformation, swap_chain: wgpu::SwapChain, } impl Target { - pub(crate) fn dimensions(&self) -> (u16, u16) { + pub(crate) fn dimensions(&self) -> (u32, u32) { (self.width, self.height) } - pub(crate) fn dpi(&self) -> f32 { - self.dpi + pub(crate) fn scale_factor(&self) -> f32 { + self.scale_factor } pub(crate) fn transformation(&self) -> Transformation { @@ -37,9 +37,9 @@ impl window::Target for Target { fn new( window: &W, - width: u16, - height: u16, - dpi: f32, + width: u32, + height: u32, + scale_factor: f32, renderer: &Renderer, ) -> Target { let surface = wgpu::Surface::create(window); @@ -50,7 +50,7 @@ impl window::Target for Target { surface, width, height, - dpi, + scale_factor, transformation: Transformation::orthographic(width, height), swap_chain, } @@ -58,14 +58,14 @@ impl window::Target for Target { fn resize( &mut self, - width: u16, - height: u16, - dpi: f32, + width: u32, + height: u32, + scale_factor: f32, renderer: &Renderer, ) { self.width = width; self.height = height; - self.dpi = dpi; + self.scale_factor = scale_factor; self.transformation = Transformation::orthographic(width, height); self.swap_chain = new_swap_chain(&self.surface, width, height, &renderer.device); @@ -74,8 +74,8 @@ impl window::Target for Target { fn new_swap_chain( surface: &wgpu::Surface, - width: u16, - height: u16, + width: u32, + height: u32, device: &wgpu::Device, ) -> wgpu::SwapChain { device.create_swap_chain( @@ -83,8 +83,8 @@ fn new_swap_chain( &wgpu::SwapChainDescriptor { usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT, format: wgpu::TextureFormat::Bgra8UnormSrgb, - width: u32::from(width), - height: u32::from(height), + width, + height, present_mode: wgpu::PresentMode::Vsync, }, ) diff --git a/wgpu/src/transformation.rs b/wgpu/src/transformation.rs index c8a7ee75..666696f3 100644 --- a/wgpu/src/transformation.rs +++ b/wgpu/src/transformation.rs @@ -13,10 +13,10 @@ impl Transformation { /// Creates an orthographic projection. #[rustfmt::skip] - pub fn orthographic(width: u16, height: u16) -> Transformation { + pub fn orthographic(width: u32, height: u32) -> Transformation { Transformation(Mat4::from_cols( - Vec4::new(2.0 / f32::from(width), 0.0, 0.0, 0.0), - Vec4::new(0.0, 2.0 / f32::from(height), 0.0, 0.0), + Vec4::new(2.0 / width as f32, 0.0, 0.0, 0.0), + Vec4::new(0.0, 2.0 / height as f32, 0.0, 0.0), Vec4::new(0.0, 0.0, -1.0, 0.0), Vec4::new(-1.0, -1.0, 0.0, 1.0) )) diff --git a/winit/Cargo.toml b/winit/Cargo.toml index cef41e9c..c1975448 100644 --- a/winit/Cargo.toml +++ b/winit/Cargo.toml @@ -14,7 +14,7 @@ categories = ["gui"] debug = [] [dependencies] -winit = { version = "0.20.0-alpha3", git = "https://github.com/hecrj/winit", rev = "709808eb4e69044705fcb214bcc30556db761405"} +winit = "0.20" log = "0.4" [dependencies.iced_native] diff --git a/winit/src/application.rs b/winit/src/application.rs index 4b21a930..31561689 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -138,10 +138,7 @@ pub trait Application: Sized { window_builder = window_builder .with_title(&title) - .with_inner_size(winit::dpi::LogicalSize { - width: f64::from(width), - height: f64::from(height), - }) + .with_inner_size(winit::dpi::LogicalSize { width, height }) .with_resizable(settings.window.resizable) .with_decorations(settings.window.decorations) .with_fullscreen(conversion::fullscreen( @@ -161,18 +158,22 @@ pub trait Application: Sized { window_builder.build(&event_loop).expect("Open window") }; - let dpi = window.hidpi_factor(); - let mut size = window.inner_size(); + let mut scale_factor = window.scale_factor(); + let mut size = window.inner_size().to_logical::(scale_factor); let mut resized = false; let clipboard = Clipboard::new(&window); let mut renderer = Self::Renderer::new(renderer_settings); let mut target = { - let (width, height) = to_physical(size, dpi); + let (width, height) = to_physical(size, scale_factor); ::Target::new( - &window, width, height, dpi as f32, &renderer, + &window, + width, + height, + scale_factor as f32, + &renderer, ) }; @@ -191,6 +192,7 @@ pub trait Application: Sized { let mut cache = Some(user_interface.into_cache()); let mut events = Vec::new(); let mut mouse_cursor = MouseCursor::OutOfBounds; + let mut modifiers = winit::event::ModifiersState::default(); debug.startup_finished(); window.request_redraw(); @@ -303,13 +305,12 @@ pub trait Application: Sized { debug.render_started(); if resized { - let dpi = window.hidpi_factor(); - let (width, height) = to_physical(size, dpi); + let (width, height) = to_physical(size, scale_factor); target.resize( width, height, - window.hidpi_factor() as f32, + scale_factor as f32, &renderer, ); @@ -337,18 +338,21 @@ pub trait Application: Sized { .. } => match window_event { WindowEvent::Resized(new_size) => { + size = new_size.to_logical(scale_factor); + events.push(Event::Window(window::Event::Resized { - width: new_size.width.round() as u32, - height: new_size.height.round() as u32, + width: size.width.round() as u32, + height: size.height.round() as u32, })); - size = new_size; resized = true; } WindowEvent::CloseRequested => { *control_flow = ControlFlow::Exit; } WindowEvent::CursorMoved { position, .. } => { + let position = position.to_logical::(scale_factor); + events.push(Event::Mouse(mouse::Event::CursorMoved { x: position.x as f32, y: position.y as f32, @@ -397,7 +401,6 @@ pub trait Application: Sized { winit::event::KeyboardInput { virtual_keycode: Some(virtual_keycode), state, - modifiers, .. }, .. @@ -427,8 +430,20 @@ pub trait Application: Sized { WindowEvent::HoveredFileCancelled => { events.push(Event::Window(window::Event::FilesHoveredLeft)); } + WindowEvent::ScaleFactorChanged { + scale_factor: new_scale_factor, + .. + } => { + scale_factor = new_scale_factor; + } _ => {} }, + event::Event::DeviceEvent { + event: event::DeviceEvent::ModifiersChanged(new_modifiers), + .. + } => { + modifiers = new_modifiers; + } _ => { *control_flow = ControlFlow::Wait; } @@ -440,7 +455,7 @@ fn build_user_interface<'a, A: Application>( application: &'a mut A, cache: Cache, renderer: &mut A::Renderer, - size: winit::dpi::LogicalSize, + size: winit::dpi::LogicalSize, debug: &mut Debug, ) -> UserInterface<'a, A::Message, A::Renderer> { debug.view_started(); @@ -459,12 +474,12 @@ fn build_user_interface<'a, A: Application>( user_interface } -fn to_physical(size: winit::dpi::LogicalSize, dpi: f64) -> (u16, u16) { - let physical_size = size.to_physical(dpi); +fn to_physical(size: winit::dpi::LogicalSize, dpi: f64) -> (u32, u32) { + let physical_size = size.to_physical::(dpi); ( - physical_size.width.round() as u16, - physical_size.height.round() as u16, + physical_size.width.round() as u32, + physical_size.height.round() as u32, ) } diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 725b2d86..af0c4c9f 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -74,10 +74,10 @@ pub fn modifiers_state( modifiers: winit::event::ModifiersState, ) -> ModifiersState { ModifiersState { - shift: modifiers.shift, - control: modifiers.ctrl, - alt: modifiers.alt, - logo: modifiers.logo, + shift: modifiers.shift(), + control: modifiers.ctrl(), + alt: modifiers.alt(), + logo: modifiers.logo(), } } From 1d71f78f906dc74aa3d724dd09cb6c6225686d76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Thu, 9 Jan 2020 03:58:33 +0100 Subject: [PATCH 2/5] Keep `is_private_use_character` filter for now --- winit/src/application.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/winit/src/application.rs b/winit/src/application.rs index 31561689..045d5872 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -484,7 +484,6 @@ fn to_physical(size: winit::dpi::LogicalSize, dpi: f64) -> (u32, u32) { } // As defined in: http://www.unicode.org/faq/private_use.html -// TODO: Remove once https://github.com/rust-windowing/winit/pull/1254 lands fn is_private_use_character(c: char) -> bool { match c { '\u{E000}'..='\u{F8FF}' From fbc9deb424b0bfbc1ae7c7ee89ba08fecd049b2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Thu, 9 Jan 2020 04:22:27 +0100 Subject: [PATCH 3/5] Implement an opaque `Size` type It immutably ties physical and logical sizes to a specific scale factor. --- winit/src/application.rs | 55 +++++++++++++++++----------------------- winit/src/lib.rs | 2 ++ winit/src/size.rs | 30 ++++++++++++++++++++++ 3 files changed, 55 insertions(+), 32 deletions(-) create mode 100644 winit/src/size.rs diff --git a/winit/src/application.rs b/winit/src/application.rs index 045d5872..853cbb5e 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -158,21 +158,20 @@ pub trait Application: Sized { window_builder.build(&event_loop).expect("Open window") }; - let mut scale_factor = window.scale_factor(); - let mut size = window.inner_size().to_logical::(scale_factor); + let mut size = Size::new(window.inner_size(), window.scale_factor()); let mut resized = false; let clipboard = Clipboard::new(&window); let mut renderer = Self::Renderer::new(renderer_settings); let mut target = { - let (width, height) = to_physical(size, scale_factor); + let physical_size = size.physical(); ::Target::new( &window, - width, - height, - scale_factor as f32, + physical_size.width, + physical_size.height, + size.scale_factor() as f32, &renderer, ) }; @@ -181,7 +180,7 @@ pub trait Application: Sized { &mut application, Cache::default(), &mut renderer, - size, + size.logical(), &mut debug, ); @@ -213,7 +212,7 @@ pub trait Application: Sized { &mut application, cache.take().unwrap(), &mut renderer, - size, + size.logical(), &mut debug, ); @@ -285,7 +284,7 @@ pub trait Application: Sized { &mut application, temp_cache, &mut renderer, - size, + size.logical(), &mut debug, ); @@ -305,12 +304,12 @@ pub trait Application: Sized { debug.render_started(); if resized { - let (width, height) = to_physical(size, scale_factor); + let physical_size = size.physical(); target.resize( - width, - height, - scale_factor as f32, + physical_size.width, + physical_size.height, + size.scale_factor() as f32, &renderer, ); @@ -338,11 +337,11 @@ pub trait Application: Sized { .. } => match window_event { WindowEvent::Resized(new_size) => { - size = new_size.to_logical(scale_factor); + size = Size::new(new_size, size.scale_factor()); events.push(Event::Window(window::Event::Resized { - width: size.width.round() as u32, - height: size.height.round() as u32, + width: size.logical().width.round() as u32, + height: size.logical().height.round() as u32, })); resized = true; @@ -351,7 +350,8 @@ pub trait Application: Sized { *control_flow = ControlFlow::Exit; } WindowEvent::CursorMoved { position, .. } => { - let position = position.to_logical::(scale_factor); + let position = + position.to_logical::(size.scale_factor()); events.push(Event::Mouse(mouse::Event::CursorMoved { x: position.x as f32, @@ -430,11 +430,8 @@ pub trait Application: Sized { WindowEvent::HoveredFileCancelled => { events.push(Event::Window(window::Event::FilesHoveredLeft)); } - WindowEvent::ScaleFactorChanged { - scale_factor: new_scale_factor, - .. - } => { - scale_factor = new_scale_factor; + WindowEvent::ScaleFactorChanged { scale_factor, .. } => { + size = Size::new(size.physical(), scale_factor); } _ => {} }, @@ -465,7 +462,10 @@ fn build_user_interface<'a, A: Application>( debug.layout_started(); let user_interface = UserInterface::build( view, - Size::new(size.width.round() as f32, size.height.round() as f32), + iced_native::Size::new( + size.width.round() as f32, + size.height.round() as f32, + ), cache, renderer, ); @@ -474,15 +474,6 @@ fn build_user_interface<'a, A: Application>( user_interface } -fn to_physical(size: winit::dpi::LogicalSize, dpi: f64) -> (u32, u32) { - let physical_size = size.to_physical::(dpi); - - ( - physical_size.width.round() as u32, - physical_size.height.round() as u32, - ) -} - // As defined in: http://www.unicode.org/faq/private_use.html fn is_private_use_character(c: char) -> bool { match c { diff --git a/winit/src/lib.rs b/winit/src/lib.rs index b0f235ad..225907a4 100644 --- a/winit/src/lib.rs +++ b/winit/src/lib.rs @@ -32,6 +32,7 @@ mod application; mod clipboard; mod mode; mod proxy; +mod size; // We disable debug capabilities on release builds unless the `debug` feature // is explicitly enabled. @@ -49,3 +50,4 @@ pub use settings::Settings; use debug::Debug; use proxy::Proxy; +use size::Size; diff --git a/winit/src/size.rs b/winit/src/size.rs new file mode 100644 index 00000000..7e3056d4 --- /dev/null +++ b/winit/src/size.rs @@ -0,0 +1,30 @@ +pub struct Size { + physical: winit::dpi::PhysicalSize, + logical: winit::dpi::LogicalSize, + scale_factor: f64, +} + +impl Size { + pub fn new( + physical: winit::dpi::PhysicalSize, + scale_factor: f64, + ) -> Size { + Size { + logical: physical.to_logical(scale_factor), + physical, + scale_factor, + } + } + + pub fn physical(&self) -> winit::dpi::PhysicalSize { + self.physical + } + + pub fn logical(&self) -> winit::dpi::LogicalSize { + self.logical + } + + pub fn scale_factor(&self) -> f64 { + self.scale_factor + } +} From 7d9378752ab3a0676c62ab269be9bf64e371a17d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Thu, 9 Jan 2020 04:54:32 +0100 Subject: [PATCH 4/5] Increase precision of `scale_factor` in `Windowed` --- native/src/window/renderer.rs | 4 ++-- wgpu/src/renderer/target.rs | 8 ++++---- winit/src/application.rs | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/native/src/window/renderer.rs b/native/src/window/renderer.rs index 3828f7d4..a3cbb8ce 100644 --- a/native/src/window/renderer.rs +++ b/native/src/window/renderer.rs @@ -41,7 +41,7 @@ pub trait Target { window: &W, width: u32, height: u32, - scale_factor: f32, + scale_factor: f64, renderer: &Self::Renderer, ) -> Self; @@ -52,7 +52,7 @@ pub trait Target { &mut self, width: u32, height: u32, - scale_factor: f32, + scale_factor: f64, renderer: &Self::Renderer, ); } diff --git a/wgpu/src/renderer/target.rs b/wgpu/src/renderer/target.rs index 3fd0cd83..20974976 100644 --- a/wgpu/src/renderer/target.rs +++ b/wgpu/src/renderer/target.rs @@ -39,7 +39,7 @@ impl window::Target for Target { window: &W, width: u32, height: u32, - scale_factor: f32, + scale_factor: f64, renderer: &Renderer, ) -> Target { let surface = wgpu::Surface::create(window); @@ -50,7 +50,7 @@ impl window::Target for Target { surface, width, height, - scale_factor, + scale_factor: scale_factor as f32, transformation: Transformation::orthographic(width, height), swap_chain, } @@ -60,12 +60,12 @@ impl window::Target for Target { &mut self, width: u32, height: u32, - scale_factor: f32, + scale_factor: f64, renderer: &Renderer, ) { self.width = width; self.height = height; - self.scale_factor = scale_factor; + self.scale_factor = scale_factor as f32; self.transformation = Transformation::orthographic(width, height); self.swap_chain = new_swap_chain(&self.surface, width, height, &renderer.device); diff --git a/winit/src/application.rs b/winit/src/application.rs index 853cbb5e..3c0332ed 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -171,7 +171,7 @@ pub trait Application: Sized { &window, physical_size.width, physical_size.height, - size.scale_factor() as f32, + size.scale_factor(), &renderer, ) }; @@ -309,7 +309,7 @@ pub trait Application: Sized { target.resize( physical_size.width, physical_size.height, - size.scale_factor() as f32, + size.scale_factor(), &renderer, ); From 8e4741ad778e227628d36dcfc748c5cdc23310f3 Mon Sep 17 00:00:00 2001 From: Imbris Date: Fri, 7 Feb 2020 22:58:23 -0500 Subject: [PATCH 5/5] Update `winit` to `0.21` --- winit/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/winit/Cargo.toml b/winit/Cargo.toml index c1975448..63df1d63 100644 --- a/winit/Cargo.toml +++ b/winit/Cargo.toml @@ -14,7 +14,7 @@ categories = ["gui"] debug = [] [dependencies] -winit = "0.20" +winit = "0.21" log = "0.4" [dependencies.iced_native]