2020-05-04 13:04:30 +00:00
|
|
|
use std::{cell::RefCell, rc::Rc, sync::atomic::Ordering, time::Duration};
|
2017-06-11 21:03:25 +00:00
|
|
|
|
2021-07-11 18:56:08 +00:00
|
|
|
#[cfg(feature = "debug")]
|
|
|
|
use smithay::backend::renderer::gles2::Gles2Texture;
|
2021-06-09 20:12:35 +00:00
|
|
|
#[cfg(feature = "egl")]
|
2021-06-27 20:18:45 +00:00
|
|
|
use smithay::{
|
|
|
|
backend::renderer::{ImportDma, ImportEgl},
|
|
|
|
wayland::dmabuf::init_dmabuf_global,
|
|
|
|
};
|
2018-10-02 21:37:24 +00:00
|
|
|
use smithay::{
|
2021-10-17 22:47:13 +00:00
|
|
|
backend::{
|
2022-01-05 20:44:38 +00:00
|
|
|
renderer::{Renderer, Transform},
|
2021-10-17 22:47:13 +00:00
|
|
|
winit::{self, WinitEvent},
|
|
|
|
SwapBuffersError,
|
|
|
|
},
|
2019-02-05 16:26:09 +00:00
|
|
|
reexports::{
|
|
|
|
calloop::EventLoop,
|
|
|
|
wayland_server::{protocol::wl_output, Display},
|
|
|
|
},
|
2018-10-02 21:37:24 +00:00
|
|
|
wayland::{
|
2021-06-21 19:29:40 +00:00
|
|
|
output::{Mode, PhysicalProperties},
|
2020-05-04 13:04:30 +00:00
|
|
|
seat::CursorImageStatus,
|
2018-10-02 21:37:24 +00:00
|
|
|
},
|
|
|
|
};
|
2018-05-07 17:56:38 +00:00
|
|
|
|
|
|
|
use slog::Logger;
|
|
|
|
|
2021-05-30 22:56:37 +00:00
|
|
|
use crate::state::{AnvilState, Backend};
|
2021-08-21 21:55:10 +00:00
|
|
|
use crate::{drawing::*, render::render_layers_and_windows};
|
2017-09-03 17:53:29 +00:00
|
|
|
|
2021-06-30 17:58:13 +00:00
|
|
|
pub const OUTPUT_NAME: &str = "winit";
|
2021-06-21 19:29:40 +00:00
|
|
|
|
2021-07-11 18:56:08 +00:00
|
|
|
pub struct WinitData {
|
|
|
|
#[cfg(feature = "debug")]
|
|
|
|
fps_texture: Gles2Texture,
|
|
|
|
#[cfg(feature = "debug")]
|
|
|
|
pub fps: fps_ticker::Fps,
|
|
|
|
}
|
2021-05-30 20:01:36 +00:00
|
|
|
|
2021-05-30 22:56:37 +00:00
|
|
|
impl Backend for WinitData {
|
|
|
|
fn seat_name(&self) -> String {
|
|
|
|
String::from("winit")
|
2021-05-30 20:01:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-08 14:27:12 +00:00
|
|
|
pub fn run_winit(log: Logger) {
|
|
|
|
let mut event_loop = EventLoop::try_new().unwrap();
|
|
|
|
let display = Rc::new(RefCell::new(Display::new()));
|
|
|
|
|
2022-01-05 20:44:38 +00:00
|
|
|
let (backend, mut winit) = match winit::init(log.clone()) {
|
2021-07-08 14:27:12 +00:00
|
|
|
Ok(ret) => ret,
|
|
|
|
Err(err) => {
|
|
|
|
slog::crit!(log, "Failed to initialize Winit backend: {}", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
2022-01-05 20:44:38 +00:00
|
|
|
let backend = Rc::new(RefCell::new(backend));
|
2017-03-07 10:53:57 +00:00
|
|
|
|
2018-12-15 19:18:38 +00:00
|
|
|
#[cfg(feature = "egl")]
|
2022-01-05 20:44:38 +00:00
|
|
|
if backend
|
2021-06-27 20:18:45 +00:00
|
|
|
.borrow_mut()
|
|
|
|
.renderer()
|
|
|
|
.bind_wl_display(&display.borrow())
|
|
|
|
.is_ok()
|
|
|
|
{
|
2021-05-13 17:59:47 +00:00
|
|
|
info!(log, "EGL hardware-acceleration enabled");
|
2022-01-05 20:44:38 +00:00
|
|
|
let dmabuf_formats = backend
|
2021-06-09 20:12:35 +00:00
|
|
|
.borrow_mut()
|
|
|
|
.renderer()
|
|
|
|
.dmabuf_formats()
|
|
|
|
.cloned()
|
|
|
|
.collect::<Vec<_>>();
|
2022-01-05 20:44:38 +00:00
|
|
|
let backend = backend.clone();
|
2021-06-09 20:12:35 +00:00
|
|
|
init_dmabuf_global(
|
|
|
|
&mut *display.borrow_mut(),
|
|
|
|
dmabuf_formats,
|
2022-01-05 20:44:38 +00:00
|
|
|
move |buffer, _| backend.borrow_mut().renderer().import_dmabuf(buffer).is_ok(),
|
2021-06-09 20:12:35 +00:00
|
|
|
log.clone(),
|
|
|
|
);
|
2021-05-13 17:59:47 +00:00
|
|
|
};
|
2020-02-03 05:42:12 +00:00
|
|
|
|
2022-01-05 20:44:38 +00:00
|
|
|
let size = backend.borrow().window_size().physical_size;
|
2020-06-27 16:27:47 +00:00
|
|
|
|
2017-06-11 21:03:25 +00:00
|
|
|
/*
|
2017-09-20 13:03:58 +00:00
|
|
|
* Initialize the globals
|
2017-06-11 21:03:25 +00:00
|
|
|
*/
|
2017-06-13 14:52:43 +00:00
|
|
|
|
2021-07-11 18:56:08 +00:00
|
|
|
let data = WinitData {
|
|
|
|
#[cfg(feature = "debug")]
|
|
|
|
fps_texture: import_bitmap(
|
2022-01-05 20:44:38 +00:00
|
|
|
backend.borrow_mut().renderer(),
|
2021-07-11 18:56:08 +00:00
|
|
|
&image::io::Reader::with_format(std::io::Cursor::new(FPS_NUMBERS_PNG), image::ImageFormat::Png)
|
|
|
|
.decode()
|
|
|
|
.unwrap()
|
|
|
|
.to_rgba8(),
|
|
|
|
)
|
|
|
|
.expect("Unable to upload FPS texture"),
|
|
|
|
#[cfg(feature = "debug")]
|
|
|
|
fps: fps_ticker::Fps::default(),
|
|
|
|
};
|
2021-07-08 15:15:54 +00:00
|
|
|
let mut state = AnvilState::init(display.clone(), event_loop.handle(), data, log.clone(), true);
|
2017-09-22 12:57:11 +00:00
|
|
|
|
2021-06-21 19:29:40 +00:00
|
|
|
let mode = Mode {
|
2021-07-05 17:21:15 +00:00
|
|
|
size,
|
2021-06-21 19:29:40 +00:00
|
|
|
refresh: 60_000,
|
|
|
|
};
|
|
|
|
|
|
|
|
state.output_map.borrow_mut().add(
|
|
|
|
OUTPUT_NAME,
|
2017-10-01 17:14:25 +00:00
|
|
|
PhysicalProperties {
|
2021-07-05 17:21:15 +00:00
|
|
|
size: (0, 0).into(),
|
2017-10-01 17:14:25 +00:00
|
|
|
subpixel: wl_output::Subpixel::Unknown,
|
2018-09-24 22:30:39 +00:00
|
|
|
make: "Smithay".into(),
|
2017-10-01 17:14:25 +00:00
|
|
|
model: "Winit".into(),
|
|
|
|
},
|
2021-06-21 19:29:40 +00:00
|
|
|
mode,
|
2017-10-01 17:14:25 +00:00
|
|
|
);
|
|
|
|
|
2020-10-10 18:01:18 +00:00
|
|
|
let start_time = std::time::Instant::now();
|
2021-05-23 21:03:43 +00:00
|
|
|
let mut cursor_visible = true;
|
2020-10-10 18:01:18 +00:00
|
|
|
|
2021-06-07 17:04:15 +00:00
|
|
|
#[cfg(feature = "xwayland")]
|
|
|
|
state.start_xwayland();
|
|
|
|
|
2018-05-07 17:56:38 +00:00
|
|
|
info!(log, "Initialization completed, starting the main loop.");
|
2017-03-07 10:53:57 +00:00
|
|
|
|
2020-04-17 16:27:26 +00:00
|
|
|
while state.running.load(Ordering::SeqCst) {
|
2021-10-17 22:47:13 +00:00
|
|
|
if winit
|
|
|
|
.dispatch_new_events(|event| match event {
|
|
|
|
WinitEvent::Resized { size, .. } => {
|
|
|
|
state.output_map.borrow_mut().update_mode_by_name(
|
|
|
|
Mode {
|
|
|
|
size,
|
|
|
|
refresh: 60_000,
|
|
|
|
},
|
2021-10-28 21:01:40 +00:00
|
|
|
OUTPUT_NAME,
|
2021-10-17 22:47:13 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
let output_mut = state.output_map.borrow();
|
2021-10-28 21:01:40 +00:00
|
|
|
let output = output_mut.find_by_name(OUTPUT_NAME).unwrap();
|
2021-10-17 22:47:13 +00:00
|
|
|
|
|
|
|
state.window_map.borrow_mut().layers.arange_layers(output);
|
|
|
|
}
|
|
|
|
|
2021-10-28 21:01:40 +00:00
|
|
|
WinitEvent::Input(event) => state.process_input_event_windowed(event, OUTPUT_NAME),
|
2021-10-17 22:47:13 +00:00
|
|
|
|
|
|
|
_ => (),
|
|
|
|
})
|
2021-04-06 23:15:03 +00:00
|
|
|
.is_err()
|
|
|
|
{
|
|
|
|
state.running.store(false, Ordering::SeqCst);
|
|
|
|
break;
|
|
|
|
}
|
2017-06-02 13:26:50 +00:00
|
|
|
|
2018-12-10 08:18:00 +00:00
|
|
|
// drawing logic
|
|
|
|
{
|
2022-01-05 20:44:38 +00:00
|
|
|
let mut backend = backend.borrow_mut();
|
2021-06-21 19:29:40 +00:00
|
|
|
// This is safe to do as with winit we are guaranteed to have exactly one output
|
2021-07-01 17:51:06 +00:00
|
|
|
let (output_geometry, output_scale) = state
|
2021-06-21 19:29:40 +00:00
|
|
|
.output_map
|
|
|
|
.borrow()
|
2021-07-01 17:51:06 +00:00
|
|
|
.find_by_name(OUTPUT_NAME)
|
|
|
|
.map(|output| (output.geometry(), output.scale()))
|
2021-06-21 19:29:40 +00:00
|
|
|
.unwrap();
|
2021-06-29 15:30:48 +00:00
|
|
|
|
2022-01-05 20:44:38 +00:00
|
|
|
let result = backend
|
|
|
|
.bind()
|
|
|
|
.and_then(|_| {
|
|
|
|
backend
|
|
|
|
.renderer()
|
2022-01-10 18:31:07 +00:00
|
|
|
.render(
|
|
|
|
output_geometry
|
|
|
|
.size
|
|
|
|
.to_f64()
|
|
|
|
.to_physical(output_scale as f64)
|
|
|
|
.to_i32_round(),
|
|
|
|
Transform::Flipped180,
|
|
|
|
|renderer, frame| {
|
|
|
|
render_layers_and_windows(
|
|
|
|
renderer,
|
|
|
|
frame,
|
|
|
|
&*state.window_map.borrow(),
|
|
|
|
output_geometry,
|
|
|
|
output_scale,
|
|
|
|
&log,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let (x, y) = state.pointer_location.into();
|
|
|
|
|
|
|
|
// draw the dnd icon if any
|
|
|
|
{
|
|
|
|
let guard = state.dnd_icon.lock().unwrap();
|
|
|
|
if let Some(ref surface) = *guard {
|
|
|
|
if surface.as_ref().is_alive() {
|
|
|
|
draw_dnd_icon(
|
|
|
|
renderer,
|
|
|
|
frame,
|
|
|
|
surface,
|
|
|
|
(x as i32, y as i32).into(),
|
|
|
|
output_scale,
|
|
|
|
&log,
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// draw the cursor as relevant
|
|
|
|
{
|
|
|
|
let mut guard = state.cursor_status.lock().unwrap();
|
|
|
|
// reset the cursor if the surface is no longer alive
|
|
|
|
let mut reset = false;
|
|
|
|
if let CursorImageStatus::Image(ref surface) = *guard {
|
|
|
|
reset = !surface.as_ref().is_alive();
|
|
|
|
}
|
|
|
|
if reset {
|
|
|
|
*guard = CursorImageStatus::Default;
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw as relevant
|
|
|
|
if let CursorImageStatus::Image(ref surface) = *guard {
|
|
|
|
cursor_visible = false;
|
|
|
|
draw_cursor(
|
2022-01-05 20:44:38 +00:00
|
|
|
renderer,
|
|
|
|
frame,
|
|
|
|
surface,
|
|
|
|
(x as i32, y as i32).into(),
|
|
|
|
output_scale,
|
|
|
|
&log,
|
|
|
|
)?;
|
2022-01-10 18:31:07 +00:00
|
|
|
} else {
|
|
|
|
cursor_visible = true;
|
2022-01-05 20:44:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 18:31:07 +00:00
|
|
|
#[cfg(feature = "debug")]
|
|
|
|
{
|
|
|
|
let fps = state.backend_data.fps.avg().round() as u32;
|
|
|
|
|
|
|
|
draw_fps(
|
2022-01-05 20:44:38 +00:00
|
|
|
renderer,
|
|
|
|
frame,
|
2022-01-10 18:31:07 +00:00
|
|
|
&state.backend_data.fps_texture,
|
|
|
|
output_scale as f64,
|
|
|
|
fps,
|
2022-01-05 20:44:38 +00:00
|
|
|
)?;
|
|
|
|
}
|
|
|
|
|
2022-01-10 18:31:07 +00:00
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
)
|
2022-01-05 20:44:38 +00:00
|
|
|
.map_err(Into::<SwapBuffersError>::into)
|
|
|
|
.and_then(|x| x)
|
2021-05-26 17:12:45 +00:00
|
|
|
})
|
2022-01-05 20:44:38 +00:00
|
|
|
.and_then(|_| backend.submit(None, 1.0));
|
2021-04-28 22:32:47 +00:00
|
|
|
|
2022-01-05 20:44:38 +00:00
|
|
|
backend.window().set_cursor_visible(cursor_visible);
|
2018-12-10 08:18:00 +00:00
|
|
|
|
2021-05-23 21:03:43 +00:00
|
|
|
if let Err(SwapBuffersError::ContextLost(err)) = result {
|
2021-04-06 23:15:03 +00:00
|
|
|
error!(log, "Critical Rendering Error: {}", err);
|
|
|
|
state.running.store(false, Ordering::SeqCst);
|
2018-12-10 08:18:00 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-31 17:33:18 +00:00
|
|
|
|
2021-05-16 18:08:10 +00:00
|
|
|
// Send frame events so that client start drawing their next frame
|
|
|
|
state
|
|
|
|
.window_map
|
|
|
|
.borrow()
|
|
|
|
.send_frames(start_time.elapsed().as_millis() as u32);
|
|
|
|
display.borrow_mut().flush_clients(&mut state);
|
|
|
|
|
2019-06-17 01:10:50 +00:00
|
|
|
if event_loop
|
2020-04-21 17:42:03 +00:00
|
|
|
.dispatch(Some(Duration::from_millis(16)), &mut state)
|
2019-06-17 01:10:50 +00:00
|
|
|
.is_err()
|
|
|
|
{
|
2020-04-17 16:27:26 +00:00
|
|
|
state.running.store(false, Ordering::SeqCst);
|
2019-06-17 01:10:50 +00:00
|
|
|
} else {
|
2020-04-17 16:27:26 +00:00
|
|
|
display.borrow_mut().flush_clients(&mut state);
|
2020-04-21 16:56:59 +00:00
|
|
|
state.window_map.borrow_mut().refresh();
|
2021-06-21 19:29:40 +00:00
|
|
|
state.output_map.borrow_mut().refresh();
|
2019-06-17 01:10:50 +00:00
|
|
|
}
|
2021-07-11 18:56:08 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "debug")]
|
|
|
|
state.backend_data.fps.tick();
|
2017-06-02 13:26:50 +00:00
|
|
|
}
|
2019-06-17 01:10:50 +00:00
|
|
|
|
|
|
|
// Cleanup stuff
|
2020-04-21 16:56:59 +00:00
|
|
|
state.window_map.borrow_mut().clear();
|
2018-05-08 10:47:09 +00:00
|
|
|
}
|