update iced_glutin to use new surface api

This commit is contained in:
Richard 2022-10-31 20:23:24 -03:00 committed by bungoboingo
parent aa7164fdde
commit 0553062be1
2 changed files with 258 additions and 91 deletions

View file

@ -16,6 +16,9 @@ debug = ["iced_winit/debug"]
system = ["iced_winit/system"] system = ["iced_winit/system"]
multi_window = ["iced_winit/multi_window"] multi_window = ["iced_winit/multi_window"]
[dependencies.raw-window-handle]
version = "0.5.0"
[dependencies.log] [dependencies.log]
version = "0.4" version = "0.4"

View file

@ -12,14 +12,33 @@ use iced_winit::futures;
use iced_winit::futures::channel::mpsc; use iced_winit::futures::channel::mpsc;
use iced_winit::renderer; use iced_winit::renderer;
use iced_winit::user_interface; use iced_winit::user_interface;
use iced_winit::winit;
use iced_winit::{Clipboard, Command, Debug, Proxy, Settings}; use iced_winit::{Clipboard, Command, Debug, Proxy, Settings};
use glutin::window::Window; use glutin::config::{
Config, ConfigSurfaceTypes, ConfigTemplateBuilder, GlConfig,
};
use glutin::context::{
ContextApi, ContextAttributesBuilder, NotCurrentContext,
NotCurrentGlContextSurfaceAccessor,
PossiblyCurrentContextGlSurfaceAccessor, PossiblyCurrentGlContext,
};
use glutin::display::{Display, DisplayApiPreference, GlDisplay};
use glutin::surface::{
GlSurface, Surface, SurfaceAttributesBuilder, WindowSurface,
};
use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle};
use std::ffi::CString;
use std::mem::ManuallyDrop; use std::mem::ManuallyDrop;
use std::num::NonZeroU32;
#[cfg(feature = "tracing")] #[cfg(feature = "tracing")]
use tracing::{info_span, instrument::Instrument}; use tracing::{info_span, instrument::Instrument};
#[allow(unsafe_code)]
const ONE: NonZeroU32 = unsafe { NonZeroU32::new_unchecked(1) };
/// Runs an [`Application`] with an executor, compositor, and the provided /// Runs an [`Application`] with an executor, compositor, and the provided
/// settings. /// settings.
pub fn run<A, E, C>( pub fn run<A, E, C>(
@ -34,9 +53,8 @@ where
{ {
use futures::task; use futures::task;
use futures::Future; use futures::Future;
use glutin::event_loop::EventLoopBuilder; use winit::event_loop::EventLoopBuilder;
use glutin::platform::run_return::EventLoopExtRunReturn; use winit::platform::run_return::EventLoopExtRunReturn;
use glutin::ContextBuilder;
#[cfg(feature = "trace")] #[cfg(feature = "trace")]
let _guard = iced_winit::Profiler::init(); let _guard = iced_winit::Profiler::init();
@ -63,76 +81,216 @@ where
runtime.enter(|| A::new(flags)) runtime.enter(|| A::new(flags))
}; };
let context = { let builder = settings.window.into_builder(
let builder = settings.window.into_builder( &application.title(),
&application.title(), event_loop.primary_monitor(),
event_loop.primary_monitor(), settings.id,
settings.id, );
);
log::info!("Window builder: {:#?}", builder); log::info!("Window builder: {:#?}", builder);
let opengl_builder = ContextBuilder::new() #[allow(unsafe_code)]
.with_vsync(true) let (display, window, surface, context) = unsafe {
.with_multisampling(C::sample_count(&compositor_settings) as u16); struct Configuration(Config);
use std::fmt;
impl fmt::Debug for Configuration {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let config = &self.0;
let opengles_builder = opengl_builder.clone().with_gl( f.debug_struct("Configuration")
glutin::GlRequest::Specific(glutin::Api::OpenGlEs, (2, 0)), .field("raw", &config)
); .field("samples", &config.num_samples())
.field("buffer_type", &config.color_buffer_type())
.field("surface_type", &config.config_surface_types())
.field("depth", &config.depth_size())
.field("alpha", &config.alpha_size())
.field("stencil", &config.stencil_size())
.field("float_pixels", &config.float_pixels())
.field("srgb", &config.srgb_capable())
.field("api", &config.api())
.finish()
}
}
let (first_builder, second_builder) = if settings.try_opengles_first { impl AsRef<Config> for Configuration {
(opengles_builder, opengl_builder) fn as_ref(&self) -> &Config {
} else { &self.0
(opengl_builder, opengles_builder) }
}
let display_handle = event_loop.raw_display_handle();
#[cfg(all(
any(windows, target_os = "macos"),
not(target_arch = "wasm32")
))]
let (window, window_handle) = {
let window = builder
.build(&event_loop)
.map_err(Error::WindowCreationFailed)?;
let handle = window.raw_window_handle();
(window, handle)
}; };
log::info!("Trying first builder: {:#?}", first_builder); #[cfg(target_arch = "wasm32")]
let preference = Err(Error::GraphicsCreationFailed(
iced_graphics::Error::BackendError(format!(
"target not supported by backend"
)),
))?;
let context = first_builder #[cfg(all(windows, not(target_arch = "wasm32")))]
.build_windowed(builder.clone(), &event_loop) let preference = DisplayApiPreference::WglThenEgl(Some(window_handle));
.or_else(|_| {
log::info!("Trying second builder: {:#?}", second_builder);
second_builder.build_windowed(builder, &event_loop)
})
.map_err(|error| {
use glutin::CreationError;
use iced_graphics::Error as ContextError;
match error { #[cfg(all(target_os = "macos", not(target_arch = "wasm32")))]
CreationError::Window(error) => { let preference = DisplayApiPreference::Cgl;
Error::WindowCreationFailed(error)
} #[cfg(all(
CreationError::OpenGlVersionNotSupported => { unix,
Error::GraphicsCreationFailed( not(target_os = "macos"),
ContextError::VersionNotSupported, not(target_arch = "wasm32")
) ))]
} let preference = DisplayApiPreference::GlxThenEgl(Box::new(
CreationError::NoAvailablePixelFormat => { winit::platform::unix::register_xlib_error_hook,
Error::GraphicsCreationFailed( ));
ContextError::NoAvailablePixelFormat,
) let display =
} Display::new(display_handle, preference).map_err(|error| {
error => Error::GraphicsCreationFailed( Error::GraphicsCreationFailed(
ContextError::BackendError(error.to_string()), iced_graphics::Error::BackendError(format!(
), "failed to create display: {error}"
} )),
)
})?; })?;
#[allow(unsafe_code)] log::debug!("Display: {}", display.version_string());
unsafe {
context let samples = C::sample_count(&compositor_settings) as u8;
.make_current(todo!()) let mut template = ConfigTemplateBuilder::new()
.expect("Make OpenGL context current") .with_surface_type(ConfigSurfaceTypes::WINDOW);
if samples != 0 {
template = template.with_multisampling(samples);
} }
#[cfg(all(windows, not(target_arch = "wasm32")))]
let template = template.compatible_with_native_window(window_handle);
log::debug!("Searching for display configurations");
let configuration = display
.find_configs(template.build())
.map_err(|_| {
Error::GraphicsCreationFailed(
iced_graphics::Error::NoAvailablePixelFormat,
)
})?
.map(Configuration)
.inspect(|config| {
log::trace!("{config:#?}");
})
.min_by_key(|config| {
config.as_ref().num_samples().saturating_sub(samples)
})
.ok_or(Error::GraphicsCreationFailed(
iced_graphics::Error::NoAvailablePixelFormat,
))?;
log::debug!("Selected: {configuration:#?}");
#[cfg(all(
unix,
not(target_os = "macos"),
not(target_arch = "wasm32")
))]
let (window, window_handle) = {
use glutin::platform::x11::X11GlConfigExt;
let builder =
if let Some(visual) = configuration.as_ref().x11_visual() {
use winit::platform::unix::WindowBuilderExtUnix;
builder.with_x11_visual(visual.into_raw())
} else {
builder
};
let window = builder
.build(&event_loop)
.map_err(Error::WindowCreationFailed)?;
let handle = window.raw_window_handle();
(window, handle)
};
let attributes =
ContextAttributesBuilder::new().build(Some(window_handle));
let fallback_attributes = ContextAttributesBuilder::new()
.with_context_api(ContextApi::Gles(None))
.build(Some(window_handle));
let context = display
.create_context(configuration.as_ref(), &attributes)
.or_else(|_| {
display.create_context(
configuration.as_ref(),
&fallback_attributes,
)
})
.map_err(|error| {
Error::GraphicsCreationFailed(
iced_graphics::Error::BackendError(format!(
"failed to create context: {error}"
)),
)
})?;
let (width, height) = window.inner_size().into();
let surface_attributes =
SurfaceAttributesBuilder::<WindowSurface>::new()
.with_srgb(Some(true))
.build(
window_handle,
NonZeroU32::new(width).unwrap_or(ONE),
NonZeroU32::new(height).unwrap_or(ONE),
);
let surface = display
.create_window_surface(configuration.as_ref(), &surface_attributes)
.map_err(|error| {
Error::GraphicsCreationFailed(
iced_graphics::Error::BackendError(format!(
"failed to create surface: {error}"
)),
)
})?;
let context = {
context
.make_current(&surface)
.expect("make context current")
};
if let Err(error) = surface.set_swap_interval(
&context,
glutin::surface::SwapInterval::Wait(ONE),
) {
log::error!("set swap interval failed: {}", error);
}
(display, window, surface, context)
}; };
#[allow(unsafe_code)] #[allow(unsafe_code)]
let (compositor, renderer) = unsafe { let (compositor, renderer) = unsafe {
C::new(compositor_settings, |address| { C::new(compositor_settings, |address| {
context.get_proc_address(address) let address = CString::new(address).expect("address error");
display.get_proc_address(address.as_c_str())
})? })?
}; };
let context = { context.make_not_current().expect("make context current") };
let (mut sender, receiver) = mpsc::unbounded(); let (mut sender, receiver) = mpsc::unbounded();
let mut instance = Box::pin({ let mut instance = Box::pin({
@ -144,6 +302,8 @@ where
proxy, proxy,
debug, debug,
receiver, receiver,
window,
surface,
context, context,
init_command, init_command,
settings.exit_on_close_request, settings.exit_on_close_request,
@ -159,22 +319,22 @@ where
let mut context = task::Context::from_waker(task::noop_waker_ref()); let mut context = task::Context::from_waker(task::noop_waker_ref());
let _ = event_loop.run_return(move |event, _, control_flow| { let _ = event_loop.run_return(move |event, _, control_flow| {
use glutin::event_loop::ControlFlow; use winit::event_loop::ControlFlow;
if let ControlFlow::ExitWithCode(_) = control_flow { if let ControlFlow::ExitWithCode(_) = control_flow {
return; return;
} }
let event = match event { let event = match event {
glutin::event::Event::WindowEvent { winit::event::Event::WindowEvent {
event: event:
glutin::event::WindowEvent::ScaleFactorChanged { winit::event::WindowEvent::ScaleFactorChanged {
new_inner_size, new_inner_size,
.. ..
}, },
window_id, window_id,
} => Some(glutin::event::Event::WindowEvent { } => Some(winit::event::Event::WindowEvent {
event: glutin::event::WindowEvent::Resized(*new_inner_size), event: winit::event::WindowEvent::Resized(*new_inner_size),
window_id, window_id,
}), }),
_ => event.to_static(), _ => event.to_static(),
@ -200,10 +360,12 @@ async fn run_instance<A, E, C>(
mut compositor: C, mut compositor: C,
mut renderer: A::Renderer, mut renderer: A::Renderer,
mut runtime: Runtime<E, Proxy<A::Message>, A::Message>, mut runtime: Runtime<E, Proxy<A::Message>, A::Message>,
mut proxy: glutin::event_loop::EventLoopProxy<A::Message>, mut proxy: winit::event_loop::EventLoopProxy<A::Message>,
mut debug: Debug, mut debug: Debug,
mut receiver: mpsc::UnboundedReceiver<glutin::event::Event<'_, A::Message>>, mut receiver: mpsc::UnboundedReceiver<winit::event::Event<'_, A::Message>>,
mut context: glutin::ContextWrapper<glutin::PossiblyCurrent, Window>, window: winit::window::Window,
surface: Surface<WindowSurface>,
context: NotCurrentContext,
init_command: Command<A::Message>, init_command: Command<A::Message>,
exit_on_close_request: bool, exit_on_close_request: bool,
) where ) where
@ -212,12 +374,18 @@ async fn run_instance<A, E, C>(
C: window::GLCompositor<Renderer = A::Renderer> + 'static, C: window::GLCompositor<Renderer = A::Renderer> + 'static,
<A::Renderer as iced_native::Renderer>::Theme: StyleSheet, <A::Renderer as iced_native::Renderer>::Theme: StyleSheet,
{ {
use glutin::event;
use iced_winit::futures::stream::StreamExt; use iced_winit::futures::stream::StreamExt;
use winit::event;
let mut clipboard = Clipboard::connect(context.window()); let context = {
context
.make_current(&surface)
.expect("make context current")
};
let mut clipboard = Clipboard::connect(&window);
let mut cache = user_interface::Cache::default(); let mut cache = user_interface::Cache::default();
let mut state = application::State::new(&application, context.window()); let mut state = application::State::new(&application, &window);
let mut viewport_version = state.viewport_version(); let mut viewport_version = state.viewport_version();
let mut should_exit = false; let mut should_exit = false;
@ -232,7 +400,7 @@ async fn run_instance<A, E, C>(
&mut should_exit, &mut should_exit,
&mut proxy, &mut proxy,
&mut debug, &mut debug,
context.window(), &window,
|| compositor.fetch_information(), || compositor.fetch_information(),
); );
runtime.track(application.subscription()); runtime.track(application.subscription());
@ -296,12 +464,12 @@ async fn run_instance<A, E, C>(
&mut proxy, &mut proxy,
&mut debug, &mut debug,
&mut messages, &mut messages,
context.window(), &window,
|| compositor.fetch_information(), || compositor.fetch_information(),
); );
// Update window // Update window
state.synchronize(&application, context.window()); state.synchronize(&application, &window);
user_interface = user_interface =
ManuallyDrop::new(application::build_user_interface( ManuallyDrop::new(application::build_user_interface(
@ -329,14 +497,14 @@ async fn run_instance<A, E, C>(
debug.draw_finished(); debug.draw_finished();
if new_mouse_interaction != mouse_interaction { if new_mouse_interaction != mouse_interaction {
context.window().set_cursor_icon( window.set_cursor_icon(conversion::mouse_interaction(
conversion::mouse_interaction(new_mouse_interaction), new_mouse_interaction,
); ));
mouse_interaction = new_mouse_interaction; mouse_interaction = new_mouse_interaction;
} }
context.window().request_redraw(); window.request_redraw();
} }
event::Event::PlatformSpecific(event::PlatformSpecific::MacOS( event::Event::PlatformSpecific(event::PlatformSpecific::MacOS(
event::MacOS::ReceivedUrl(url), event::MacOS::ReceivedUrl(url),
@ -357,13 +525,10 @@ async fn run_instance<A, E, C>(
debug.render_started(); debug.render_started();
#[allow(unsafe_code)] if !context.is_current() {
unsafe { context
if !context.is_current() { .make_current(&surface)
context = context .expect("Make OpenGL context current");
.make_current(todo!())
.expect("Make OpenGL context current");
}
} }
let current_viewport_version = state.viewport_version(); let current_viewport_version = state.viewport_version();
@ -391,19 +556,18 @@ async fn run_instance<A, E, C>(
debug.draw_finished(); debug.draw_finished();
if new_mouse_interaction != mouse_interaction { if new_mouse_interaction != mouse_interaction {
context.window().set_cursor_icon( window.set_cursor_icon(conversion::mouse_interaction(
conversion::mouse_interaction( new_mouse_interaction,
new_mouse_interaction, ));
),
);
mouse_interaction = new_mouse_interaction; mouse_interaction = new_mouse_interaction;
} }
context.resize(glutin::dpi::PhysicalSize::new( surface.resize(
physical_size.width, &context,
physical_size.height, NonZeroU32::new(physical_size.width).unwrap_or(ONE),
)); NonZeroU32::new(physical_size.height).unwrap_or(ONE),
);
compositor.resize_viewport(physical_size); compositor.resize_viewport(physical_size);
@ -417,7 +581,7 @@ async fn run_instance<A, E, C>(
&debug.overlay(), &debug.overlay(),
); );
context.swap_buffers(todo!()).expect("Swap buffers"); surface.swap_buffers(&context).expect("Swap buffers");
debug.render_finished(); debug.render_finished();
@ -434,7 +598,7 @@ async fn run_instance<A, E, C>(
break; break;
} }
state.update(context.window(), &window_event, &mut debug); state.update(&window, &window_event, &mut debug);
if let Some(event) = conversion::window_event( if let Some(event) = conversion::window_event(
crate::window::Id::MAIN, crate::window::Id::MAIN,